Skip to content

Commit

Permalink
faster uint256_add
Browse files Browse the repository at this point in the history
  • Loading branch information
feltroidprime committed Nov 30, 2023
1 parent 51c287a commit 6b7107d
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions src/starkware/cairo/common/uint256.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,39 @@ func uint256_add{range_check_ptr}(a: Uint256, b: Uint256) -> (res: Uint256, carr
ids.carry_high = 1 if sum_high >= ids.SHIFT else 0
%}

assert carry_low * carry_low = carry_low;
assert carry_high * carry_high = carry_high;

assert res.low = a.low + b.low - carry_low * SHIFT;
assert res.high = a.high + b.high + carry_low - carry_high * SHIFT;
uint256_check(res);

return (res, carry_high);
if (carry_low != 0) {
if (carry_high != 0) {
assert res.low = a.low + b.low - SHIFT;
assert res.high = a.high + b.high + 1 - SHIFT;
assert [range_check_ptr] = res.low;
assert [range_check_ptr + 1] = res.high;
let range_check_ptr = range_check_ptr + 2;
return (res, 1);
} else {
assert res.low = a.low + b.low - SHIFT;
assert res.high = a.high + b.high + 1;
assert [range_check_ptr] = res.low;
assert [range_check_ptr + 1] = res.high;
let range_check_ptr = range_check_ptr + 2;
return (res, 0);
}
} else {
if (has_carry_high != 0) {
assert res.low = a.low + b.low;
assert res.high = a.high + b.high - SHIFT;
assert [range_check_ptr] = res.low;
assert [range_check_ptr + 1] = res.high;
let range_check_ptr = range_check_ptr + 2;
return (res, 1);
} else {
assert res.low = a.low + b.low;
assert res.high = a.high + b.high;
assert [range_check_ptr] = res.low;
assert [range_check_ptr + 1] = res.high;
let range_check_ptr = range_check_ptr + 2;
return (res, 0);
}
}
}

// Splits a field element in the range [0, 2^192) to its low 64-bit and high 128-bit parts.
Expand Down

0 comments on commit 6b7107d

Please sign in to comment.