Skip to content

Commit d452d7b

Browse files
FrancoGiachettaJulianGCalderongabrielbosio
authored
Fix int_operation range_check counting (#1303)
* fix int_operation range_check counting * mend * fix overflow with i128 * format * add documentation link * reuse block_in_range to simplify operations * reviews * use let instead of reassigning range_check * Update comment Co-authored-by: Julian Gonzalez Calderon <gonzalezcalderonjulian@gmail.com> * change cairo's version used in links * fix doc links * fix doc links * fix typo Co-authored-by: Julian Gonzalez Calderon <gonzalezcalderonjulian@gmail.com> --------- Co-authored-by: Julian Gonzalez Calderon <gonzalezcalderonjulian@gmail.com> Co-authored-by: Gabriel Bosio <38794644+gabrielbosio@users.noreply.github.com>
1 parent b3ad66c commit d452d7b

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/libfuncs/int.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::{BlockExt, LibfuncHelper};
22
use 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

Comments
 (0)