Skip to content

Commit

Permalink
Fix MaybeRelocatable subtraction to unlock if_reloc_equal (keep-s…
Browse files Browse the repository at this point in the history
…tarknet-strange#491)

* Unlock if_reloc_equal by fixing mayberelocatable sub

* add unit test
  • Loading branch information
tcoratger authored Mar 22, 2024
1 parent 126eb86 commit a61b1c2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
4 changes: 1 addition & 3 deletions src/integration_tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ pub fn main() void {
.{ .pathname = "cairo_programs/if_and_prime.json", .layout = "all_cairo" },
.{ .pathname = "cairo_programs/if_in_function.json", .layout = "all_cairo" },
.{ .pathname = "cairo_programs/if_list.json", .layout = "all_cairo" },

// TODO: panic: integer overflow
// .{ .pathname = "cairo_programs/if_reloc_equal.json", .layout = "all_cairo" },
.{ .pathname = "cairo_programs/if_reloc_equal.json", .layout = "all_cairo" },
// TODO: panic index out of bounds
// .{ .pathname = "cairo_programs/integration_with_alloc_locals.json", .layout = "all_cairo" },
// TODO: panic index outt of bound
Expand Down
20 changes: 19 additions & 1 deletion src/vm/memory/relocatable.zig
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,12 @@ pub const MaybeRelocatable = union(enum) {
// If `other` is also `relocatable`, call `sub` method on `self_value`
.relocatable => |r| blk: {
if (self_value.segment_index == r.segment_index) {
break :blk MaybeRelocatable.fromInt(u64, self_value.offset - r.offset);
const res: i128 = @as(i128, self_value.offset) - @as(i128, r.offset);

break :blk if (res < 0)
MaybeRelocatable.fromFelt(Felt252.fromInt(u128, @intCast(-res)).neg())
else
MaybeRelocatable.fromFelt(Felt252.fromInt(u128, @intCast(res)));
}

break :blk CairoVMError.TypeMismatchNotRelocatable;
Expand Down Expand Up @@ -1492,3 +1497,16 @@ test "MaybeRelocatable.mul: should return Felt multiplication operation if both
(try result2.intoFelt()).toInteger(),
);
}

test "MaybeRelocatable.sub: wiht negative relocatable subtraction" {
const a = MaybeRelocatable.fromSegment(4, 0);
const b = MaybeRelocatable.fromSegment(4, 5);

try expectEqual(
MaybeRelocatable.fromInt(
u256,
3618502788666131213697322783095070105623107215331596699973092056135872020476,
),
try a.sub(b),
);
}

0 comments on commit a61b1c2

Please sign in to comment.