Skip to content

Commit 82c9c71

Browse files
authored
Added u128_bound function removing some minor calculations. (#9390)
1 parent bffefac commit 82c9c71

File tree

6 files changed

+24
-18
lines changed

6 files changed

+24
-18
lines changed

crates/cairo-lang-sierra-to-casm/src/invocations/gas_reserve.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use cairo_lang_casm::builder::{CasmBuilder, Var};
22
use cairo_lang_casm::casm_build_extend;
33
use cairo_lang_sierra::extensions::gas_reserve::GasReserveConcreteLibfunc;
4-
use num_bigint::BigInt;
54

6-
use crate::invocations::int::SmallDiffHelper;
5+
use crate::invocations::int::{SmallDiffHelper, u128_bound};
76
use crate::invocations::{
87
CompiledInvocation, CompiledInvocationBuilder, InvocationError, add_input_variables,
98
};
@@ -23,7 +22,7 @@ pub fn build(
2322
fn build_gas_reserve_create(
2423
builder: CompiledInvocationBuilder<'_>,
2524
) -> Result<CompiledInvocation, InvocationError> {
26-
let data = SmallDiffHelper::new(builder, BigInt::from(u128::MAX) + BigInt::from(1))?;
25+
let data = SmallDiffHelper::new(builder, u128_bound().clone())?;
2726
let no_overflow_res: &[&[Var]] = &[&[data.range_check], &[data.a_minus_b], &[data.b]];
2827
let overflow_res: &[&[Var]] = &[&[data.range_check], &[data.a]];
2928
data.finalize(no_overflow_res, overflow_res)

crates/cairo-lang-sierra-to-casm/src/invocations/int/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::sync::LazyLock;
2+
13
use cairo_lang_casm::builder::{CasmBuilder, Var};
24
use cairo_lang_casm::casm_build_extend;
35
use cairo_lang_casm::cell_expression::CellExpression;
@@ -19,6 +21,12 @@ pub mod unsigned128;
1921
pub mod unsigned256;
2022
pub mod unsigned512;
2123

24+
/// The value of 2^128.
25+
pub fn u128_bound() -> &'static BigInt {
26+
static U128_BOUND: LazyLock<BigInt> = LazyLock::new(|| BigInt::from(1) << 128);
27+
&U128_BOUND
28+
}
29+
2230
/// Builds invocations for uint const values.
2331
fn build_const<TIntTraits: IntTraits>(
2432
libfunc: &IntConstConcreteLibfunc<TIntTraits>,
@@ -79,7 +87,7 @@ impl<'a> SmallDiffHelper<'a> {
7987
let orig_range_check = range_check;
8088
tempvar a_ge_b;
8189
tempvar a_minus_b = a - b;
82-
const u128_limit = BigInt::from(u128::MAX) + BigInt::from(1);
90+
const u128_limit = u128_bound().clone();
8391
const limit = limit;
8492
hint TestLessThan {lhs: a_minus_b, rhs: limit} into {dst: a_ge_b};
8593
jump NoOverflow if a_ge_b != 0;

crates/cairo-lang-sierra-to-casm/src/invocations/int/signed.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use cairo_lang_sierra::extensions::utils::Range;
77
use cairo_lang_sierra::program::{BranchInfo, BranchTarget};
88
use num_bigint::BigInt;
99

10-
use super::{add_input_variables, build_const, build_small_diff, build_small_wide_mul};
10+
use super::{add_input_variables, build_const, build_small_diff, build_small_wide_mul, u128_bound};
1111
use crate::invocations::range_reduction::build_felt252_range_reduction;
1212
use crate::invocations::{
1313
BuiltinInfo, CompiledInvocation, CompiledInvocationBuilder, CostValidationInfo,
@@ -77,7 +77,7 @@ pub fn build_sint_overflowing_operation(
7777
// Bound for addition or subtraction of any 2 numbers in [i128::MIN, i128::MAX].
7878
// max(2 * i128::MAX, i128::MAX - i128::MIN) + 1
7979
// ==> max(2*(2**127 - 1), 2**127 - 1 -(-2**127)) + 1 ==> 2**128
80-
const above_bound = BigInt::from(u128::MAX) + BigInt::from(1);
80+
const above_bound = u128_bound().clone();
8181
hint TestLessThan {lhs: value, rhs: above_bound} into {dst: is_above};
8282
jump IsAbove if is_above != 0;
8383
// Below range case.

crates/cairo-lang-sierra-to-casm/src/invocations/int/unsigned128.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use cairo_lang_sierra::extensions::utils::Range;
77
use num_bigint::BigInt;
88
use num_traits::{Num, One};
99

10-
use super::{bounded, build_128bit_diff, build_const};
10+
use super::{bounded, build_128bit_diff, build_const, u128_bound};
1111
use crate::invocations::{
1212
BuiltinInfo, CompiledInvocation, CompiledInvocationBuilder, CostValidationInfo,
1313
InvocationError, add_input_variables, bitwise, get_non_fallthrough_statement_id, misc,
@@ -25,8 +25,8 @@ pub fn build(
2525
},
2626
Uint128Concrete::Divmod(_) => bounded::build_div_rem(
2727
builder,
28-
&Range::closed(0, u128::MAX),
29-
&Range::closed(1, u128::MAX),
28+
&Range::half_open(0, u128_bound().clone()),
29+
&Range::half_open(1, u128_bound().clone()),
3030
),
3131
Uint128Concrete::GuaranteeMul(_) => build_u128_guarantee_mul(builder),
3232
Uint128Concrete::MulGuaranteeVerify(_) => build_u128_mul_guarantee_verify(builder),
@@ -57,7 +57,7 @@ fn build_u128_overflowing_add(
5757
let orig_range_check = range_check;
5858
tempvar no_overflow;
5959
tempvar a_plus_b = a + b;
60-
const u128_limit = (BigInt::from(u128::MAX) + 1) as BigInt;
60+
const u128_limit = u128_bound().clone();
6161
hint TestLessThan {lhs: a_plus_b, rhs: u128_limit} into {dst: no_overflow};
6262
jump NoOverflow if no_overflow != 0;
6363
// Overflow:
@@ -194,7 +194,7 @@ fn build_u128_mul_guarantee_verify(
194194
// Note that `lower_uint128_with_carry` is bounded by 193 bits, as `a0_b` is capped
195195
// at 192 bits and `shifted_a1_b0_bottom` can contribute at most 1 additional bit,
196196
// added to (the carry of) `lower_uint128_with_carry = a0_b + shifted_a1_b0_bottom`.
197-
const u128_limit = (BigInt::from(u128::MAX) + 1) as BigInt;
197+
const u128_limit = u128_bound().clone();
198198
hint DivMod {
199199
lhs: lower_uint128_with_carry,
200200
rhs: u128_limit
@@ -239,7 +239,7 @@ fn build_u128_from_felt252(
239239
let value = expr_value.try_unpack_single()?;
240240

241241
let failure_handle_statement_id = get_non_fallthrough_statement_id(&builder);
242-
let u128_bound: BigInt = BigInt::from(u128::MAX) + 1; // = 2**128.
242+
let u128_bound = u128_bound(); // = 2**128.
243243
// Represent the maximal possible value (PRIME - 1) as 2**128 * max_x + max_y.
244244
let max_x: i128 = 10633823966279327296825105735305134080;
245245
let max_y: i128 = 0;
@@ -275,7 +275,7 @@ fn build_u128_from_felt252(
275275
assert x_minus_max_x = x + minus_max_x;
276276
jump XNotMaxX if x_minus_max_x != 0;
277277
// If x == max_x, check that y <= max_y.
278-
const le_max_y_fix = (u128_bound.clone() - max_y - 1) as BigInt;
278+
const le_max_y_fix = (u128_bound - max_y - 1) as BigInt;
279279
assert rced_value = y + le_max_y_fix;
280280
jump WriteRcedValue;
281281
XNotMaxX:

crates/cairo-lang-sierra-to-casm/src/invocations/range.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use cairo_lang_casm::builder::CasmBuilder;
22
use cairo_lang_casm::casm_build_extend;
33
use cairo_lang_sierra::extensions::gas::CostTokenType;
44
use cairo_lang_sierra::extensions::range::IntRangeConcreteLibfunc;
5-
use num_bigint::BigInt;
65

76
use super::{CompiledInvocation, CompiledInvocationBuilder, InvocationError};
7+
use crate::invocations::int::u128_bound;
88
use crate::invocations::{
99
BuiltinInfo, CostValidationInfo, add_input_variables, get_non_fallthrough_statement_id,
1010
};
@@ -39,7 +39,7 @@ fn build_try_new(
3939
let orig_range_check = range_check;
4040

4141
tempvar diff = end - start;
42-
const bound = BigInt::from(u128::MAX) + BigInt::from(1);
42+
const bound = u128_bound().clone();
4343
tempvar is_valid_range;
4444
hint TestLessThan {lhs: diff, rhs: bound} into {dst: is_valid_range};
4545
jump Valid if is_valid_range != 0;

crates/cairo-lang-sierra-to-casm/src/invocations/range_reduction.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::ops::Shl;
2-
31
use cairo_lang_casm::builder::CasmBuilder;
42
use cairo_lang_casm::casm_build_extend;
53
use cairo_lang_sierra::extensions::gas::CostTokenType;
@@ -11,6 +9,7 @@ use super::{
119
CompiledInvocation, CompiledInvocationBuilder, InvocationError,
1210
get_non_fallthrough_statement_id,
1311
};
12+
use crate::invocations::int::u128_bound;
1413
use crate::invocations::misc::validate_under_limit;
1514
use crate::invocations::{BuiltinInfo, CostValidationInfo, add_input_variables};
1615

@@ -89,7 +88,7 @@ pub fn build_felt252_range_reduction(
8988
maybe_tempvar rc_val = value + minus_range_lower;
9089
assert rc_val = *(range_check++);
9190
}
92-
let rc_size = BigInt::from(1).shl(128);
91+
let rc_size = u128_bound().clone();
9392
// If the out range is exactly `rc_size` the previous addition to the buffer validated this
9493
// case as well.
9594
if out_range.size() < rc_size {

0 commit comments

Comments
 (0)