Skip to content

Commit abde00a

Browse files
committed
Merged SmallDiffHelper and build_128bit_diff fns.
SIERRA_UPDATE_NO_CHANGE_TAG=Refactor only.
1 parent 3f1cd1e commit abde00a

File tree

3 files changed

+14
-49
lines changed

3 files changed

+14
-49
lines changed

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

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ impl<'a> SmallDiffHelper<'a> {
9595
deref a;
9696
deref b;
9797
};
98+
let same_limit = u128_bound() == &limit;
9899
casm_build_extend! {casm_builder,
99100
let orig_range_check = range_check;
100101
tempvar a_ge_b;
@@ -107,7 +108,14 @@ impl<'a> SmallDiffHelper<'a> {
107108
// Here we know that 0 - (limit - 1) <= a - b < 0.
108109
tempvar fixed_a_minus_b = a_minus_b + u128_limit;
109110
assert fixed_a_minus_b = *(range_check++);
110-
let wrapping_a_minus_b = a_minus_b + limit;
111+
};
112+
let wrapping_a_minus_b = if same_limit {
113+
fixed_a_minus_b
114+
} else {
115+
casm_build_extend!(casm_builder, let wrapping_a_minus_b = a_minus_b + limit;);
116+
wrapping_a_minus_b
117+
};
118+
casm_build_extend! {casm_builder,
111119
jump Overflow;
112120
NoOverflow:
113121
assert a_minus_b = *(range_check++);
@@ -161,47 +169,3 @@ fn build_small_diff(
161169
let overflow_res: &[&[Var]] = &[&[data.range_check], &[data.wrapping_a_minus_b]];
162170
data.finalize(no_overflow_res, overflow_res)
163171
}
164-
165-
/// Handles a 128 bit diff operation.
166-
fn build_128bit_diff(
167-
builder: CompiledInvocationBuilder<'_>,
168-
) -> Result<CompiledInvocation, InvocationError> {
169-
let failure_handle_statement_id = get_non_fallthrough_statement_id(&builder);
170-
let [range_check, a, b] = builder.try_get_single_cells()?;
171-
let mut casm_builder = CasmBuilder::default();
172-
add_input_variables! {casm_builder,
173-
buffer(0) range_check;
174-
deref a;
175-
deref b;
176-
};
177-
casm_build_extend! {casm_builder,
178-
let orig_range_check = range_check;
179-
tempvar a_ge_b;
180-
tempvar a_minus_b = a - b;
181-
const u128_limit = BigInt::from(u128::MAX) + BigInt::from(1);
182-
hint TestLessThan {lhs: a_minus_b, rhs: u128_limit} into {dst: a_ge_b};
183-
jump NoOverflow if a_ge_b != 0;
184-
// Overflow (negative):
185-
// Here we know that 0 - (2**128 - 1) <= a - b < 0.
186-
tempvar wrapping_a_minus_b = a_minus_b + u128_limit;
187-
assert wrapping_a_minus_b = *(range_check++);
188-
jump Target;
189-
NoOverflow:
190-
assert a_minus_b = *(range_check++);
191-
};
192-
Ok(builder.build_from_casm_builder(
193-
casm_builder,
194-
[
195-
("Fallthrough", &[&[range_check], &[a_minus_b]], None),
196-
("Target", &[&[range_check], &[wrapping_a_minus_b]], Some(failure_handle_statement_id)),
197-
],
198-
CostValidationInfo {
199-
builtin_infos: vec![BuiltinInfo {
200-
cost_token_ty: CostTokenType::RangeCheck,
201-
start: orig_range_check,
202-
end: range_check,
203-
}],
204-
extra_costs: None,
205-
},
206-
))
207-
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use cairo_lang_sierra::extensions::int::signed128::Sint128Concrete;
22

33
use super::signed::{build_sint_from_felt252, build_sint_overflowing_operation};
44
use super::{
5-
CompiledInvocation, CompiledInvocationBuilder, InvocationError, build_128bit_diff, build_const,
5+
CompiledInvocation, CompiledInvocationBuilder, InvocationError, build_const, build_small_diff,
6+
u128_bound,
67
};
78
use crate::invocations::misc;
89

@@ -19,6 +20,6 @@ pub fn build(
1920
Sint128Concrete::Operation(libfunc) => {
2021
build_sint_overflowing_operation(builder, i128::MIN, i128::MAX, libfunc.operator)
2122
}
22-
Sint128Concrete::Diff(_) => build_128bit_diff(builder),
23+
Sint128Concrete::Diff(_) => build_small_diff(builder, u128_bound().clone()),
2324
}
2425
}

crates/cairo-lang-sierra-to-casm/src/invocations/int/unsigned128.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 num_bigint::BigInt;
88
use num_traits::{Num, One};
99

10-
use super::{bounded, build_128bit_diff, build_const, u128_bound};
10+
use super::{bounded, build_const, build_small_diff, u128_bound};
1111
use crate::invocations::{
1212
BuiltinInfo, CompiledInvocation, CompiledInvocationBuilder, CostValidationInfo,
1313
InvocationError, add_input_variables, bitwise, get_non_fallthrough_statement_id, misc,
@@ -21,7 +21,7 @@ pub fn build(
2121
match libfunc {
2222
Uint128Concrete::Operation(libfunc) => match libfunc.operator {
2323
IntOperator::OverflowingAdd => build_u128_overflowing_add(builder),
24-
IntOperator::OverflowingSub => build_128bit_diff(builder),
24+
IntOperator::OverflowingSub => build_small_diff(builder, u128_bound().clone()),
2525
},
2626
Uint128Concrete::Divmod(_) => bounded::build_div_rem(
2727
builder,

0 commit comments

Comments
 (0)