@@ -2,6 +2,7 @@ use super::{BlockExt, LibfuncHelper};
22use crate :: {
33 error:: { panic:: ToNativeAssertError , Result } ,
44 execution_result:: BITWISE_BUILTIN_SIZE ,
5+ libfuncs:: { increment_builtin_counter, increment_builtin_counter_by} ,
56 metadata:: MetadataStorage ,
67 native_panic,
78 types:: TypeBuilder ,
@@ -584,10 +585,19 @@ fn build_operation<'ctx, 'this>(
584585 metadata : & mut MetadataStorage ,
585586 info : & IntOperationConcreteLibfunc ,
586587) -> Result < ( ) > {
587- let range_check = super :: increment_builtin_counter ( context, entry, location, entry. arg ( 0 ) ?) ?;
588-
588+ // Regardless of the operation range, the range check builtin pointer is always increased at least once.
589+ // * for signed ints: https://github.com/starkware-libs/cairo/blob/v2.12.0-dev.1/crates/cairo-lang-sierra-to-casm/src/invocations/int/signed.rs#L68
590+ // * for signed128: behaves the same as the signed ints case.
591+ // * for unsinged ints:
592+ // * for overflowing add: https://github.com/starkware-libs/cairo/blob/v2.12.0-dev.1/crates/cairo-lang-sierra-to-casm/src/invocations/int/unsigned.rs#L19
593+ // * for overflowing sub: https://github.com/starkware-libs/cairo/blob/v2.12.0-dev.1/crates/cairo-lang-sierra-to-casm/src/invocations/int/mod.rs#L67
594+ // * for unsigned128:
595+ // * for overflowing add: https://github.com/starkware-libs/cairo/blob/v2.12.0-dev.1/crates/cairo-lang-sierra-to-casm/src/invocations/int/unsigned128.rs#L45
596+ // * for overflowing sub: https://github.com/starkware-libs/cairo/blob/v2.12.0-dev.1/crates/cairo-lang-sierra-to-casm/src/invocations/int/mod.rs#L104
597+ let range_check = increment_builtin_counter ( context, entry, location, entry. arg ( 0 ) ?) ?;
589598 let value_ty = registry. get_type ( & info. signature . param_signatures [ 1 ] . ty ) ?;
590- let is_signed = !value_ty. integer_range ( registry) ?. lower . is_zero ( ) ;
599+ let value_range = value_ty. integer_range ( registry) ?;
600+ let is_signed = !value_range. lower . is_zero ( ) ;
591601 let value_ty = value_ty. build (
592602 context,
593603 helper,
@@ -636,8 +646,20 @@ fn build_operation<'ctx, 'this>(
636646 location,
637647 ) ) ;
638648
639- helper. br ( block_in_range, 0 , & [ range_check, result] , location) ?;
649+ {
650+ let is_not_i128 =
651+ !( value_range. lower == i128:: MIN . into ( ) && value_range. upper == i128:: MAX . into ( ) ) ;
652+
653+ // if we are not handling an i128 and the in_range condition is met, increase the range check builtin by 1:
654+ // https://github.com/starkware-libs/cairo/blob/v2.12.0-dev.1/crates/cairo-lang-sierra-to-casm/src/invocations/int/signed.rs#L105
655+ let range_check = if is_not_i128 {
656+ increment_builtin_counter_by ( context, block_in_range, location, range_check, 1 ) ?
657+ } else {
658+ range_check
659+ } ;
640660
661+ helper. br ( block_in_range, 0 , & [ range_check, result] , location) ?;
662+ }
641663 {
642664 let k0 = block_overflow. const_int_from_type ( context, location, 0 , result. r#type ( ) ) ?;
643665 let is_positive =
0 commit comments