Skip to content

Commit

Permalink
feat: (try) Support u384 numeric literals
Browse files Browse the repository at this point in the history
  • Loading branch information
feltroidprime committed Aug 6, 2024
1 parent 003919e commit 7fcb8ec
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions corelib/src/circuit.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct u384 {
pub limb2: u96,
pub limb3: u96,
}
impl NumericLiteralU384 of NumericLiteral<u384>;

pub type u96 = core::internal::BoundedInt<0, 79228162514264337593543950335>;
pub extern type RangeCheck96;
Expand Down
15 changes: 15 additions & 0 deletions corelib/src/test/circuit_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ fn test_u96() {
assert_eq!(a, 0x123);
}

#[test]
fn test_u384() {
let a: u384 =
0x1A0111EA397FE69A4B1BA7B6434BACD764774B84F38512BF6730D2A0F6B0F6241EABFFFEB153FFFFB9FEFFFFFFFFAAAB;
assert_eq!(
a,
u384 {
limb0: 0xb153ffffb9feffffffffaaab,
limb1: 0x6730d2a0f6b0f6241eabfffe,
limb2: 0x434bacd764774b84f38512bf,
limb3: 0x1a0111ea397fe69a4b1ba7b6
}
);
}

#[test]
fn test_try_into_u96() {
assert_eq!(0x123_felt252.try_into(), Option::<u96>::Some(0x123));
Expand Down
8 changes: 7 additions & 1 deletion crates/cairo-lang-semantic/src/corelib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,10 +860,16 @@ pub fn validate_literal(
value.to_i64().is_none()
} else if ty == get_core_ty_by_name(db, "i128".into(), vec![]) {
value.to_i128().is_none()
} else if ty == get_core_ty_by_name(db, "u384".into(), vec![]) {
value.is_negative() || value.bits() > 384
} else {
return Err(LiteralError::InvalidTypeForLiteral(ty));
};
if is_out_of_range { Err(LiteralError::OutOfRange(ty)) } else { Ok(()) }
if is_out_of_range {
Err(LiteralError::OutOfRange(ty))
} else {
Ok(())
}
}

/// Returns the type if the inner value of a `NonZero` type, if it is wrapped in one.
Expand Down
20 changes: 17 additions & 3 deletions crates/cairo-lang-semantic/src/items/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,9 @@ pub fn value_as_const_value(
validate_literal(db.upcast(), ty, value.clone())?;
let get_basic_const_value = |ty| {
let u256_ty = get_core_ty_by_name(db.upcast(), "u256".into(), vec![]);
let u384_ty = get_core_ty_by_name(db.upcast(), "u384".into(), vec![]);

if ty != u256_ty {
ConstValue::Int(value.clone(), ty)
} else {
if ty == u256_ty {
let u128_ty = get_core_ty_by_name(db.upcast(), "u128".into(), vec![]);
let mask128 = BigInt::from(u128::MAX);
let low = value & mask128;
Expand All @@ -401,6 +400,21 @@ pub fn value_as_const_value(
vec![(ConstValue::Int(low, u128_ty)), (ConstValue::Int(high, u128_ty))],
ty,
)
} else if ty == u384_ty {
let u96_ty = get_core_ty_by_name(db.upcast(), "u96".into(), vec![]);
let mask96 = BigInt::from(2).pow(96) - 1;

ConstValue::Struct(
vec![
(ConstValue::Int(value & &mask96, u96_ty)),
(ConstValue::Int((value >> 96) & &mask96, u96_ty)),
(ConstValue::Int((value >> 192) & &mask96, u96_ty)),
(ConstValue::Int((value >> 288) & &mask96, u96_ty)),
],
ty,
)
} else {
ConstValue::Int(value.clone(), ty)
}
};

Expand Down

0 comments on commit 7fcb8ec

Please sign in to comment.