diff --git a/CHANGELOG.md b/CHANGELOG.md index e67b60077c..f6b9a9722d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ #### [0.5.1] - 2023-6-7 +* fix: substraction of `MaybeRelocatable` always behaves as signed [#1218](https://github.com/lambdaclass/cairo-rs/pull/1218) + * fix: fix overflow for `QUAD_BIT` and `DI_BIT` hints [#1209](https://github.com/lambdaclass/cairo-rs/pull/1209) Fixes [#1205](https://github.com/lambdaclass/cairo-rs/issue/1205) diff --git a/cairo_programs/if_reloc_equal.cairo b/cairo_programs/if_reloc_equal.cairo new file mode 100644 index 0000000000..8dd2bb1623 --- /dev/null +++ b/cairo_programs/if_reloc_equal.cairo @@ -0,0 +1,16 @@ +from starkware.cairo.common.alloc import alloc + +func main{}() { + let arr: felt* = alloc(); + + assert arr[0] = 1; + assert arr[5] = 2; + + let end = arr + 5; + + if (arr == end) { + ret; + } + + ret; +} diff --git a/src/tests/cairo_run_test.rs b/src/tests/cairo_run_test.rs index 6cc0267187..e9a27d6465 100644 --- a/src/tests/cairo_run_test.rs +++ b/src/tests/cairo_run_test.rs @@ -967,3 +967,9 @@ fn cairo_run_reduce() { let program_data = include_bytes!("../../cairo_programs/reduce.json"); run_program_simple(program_data.as_slice()); } + +#[test] +fn cairo_run_if_reloc_equal() { + let program_data = include_bytes!("../../cairo_programs/if_reloc_equal.json"); + run_program_simple_with_memory_holes(program_data, 4); +} diff --git a/src/types/relocatable.rs b/src/types/relocatable.rs index 26b1bda832..7d383ab954 100644 --- a/src/types/relocatable.rs +++ b/src/types/relocatable.rs @@ -256,7 +256,9 @@ impl MaybeRelocatable { MaybeRelocatable::RelocatableValue(rel_b), ) => { if rel_a.segment_index == rel_b.segment_index { - return Ok(MaybeRelocatable::from(Felt252::new((*rel_a - *rel_b)?))); + return Ok(MaybeRelocatable::from(Felt252::from( + rel_a.offset as i128 - rel_b.offset as i128, + ))); } Err(MathError::RelocatableSubDiffIndex(Box::new(( *rel_a, *rel_b,