Skip to content
This repository has been archived by the owner on Jan 9, 2025. It is now read-only.

fix: addmod impl #865

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions blockchain-tests-skip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ testname:
- push0Gas_d0g0v0_Shanghai
- push0_d1g0v0_Shanghai
vmArithmeticTest:
- addmod_d11g0v0_Shanghai
- addmod_d10g0v0_Shanghai
- addmod_d9g0v0_Shanghai
- addmod_d8g0v0_Shanghai
- divByZero_d63g0v0_Shanghai
- divByZero_d64g0v0_Shanghai
- divByZero_d65g0v0_Shanghai
Expand Down
4 changes: 4 additions & 0 deletions solidity_contracts/src/PlainOpcodes/PlainOpcodes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ contract PlainOpcodes {
return mulmod(type(uint256).max, type(uint256).max, type(uint256).max);
}

function addmodMax() public pure returns (uint) {
return addmod(type(uint256).max, type(uint256).max, type(uint256).max);
}

receive() external payable {}
fallback() external payable {}
}
22 changes: 17 additions & 5 deletions src/kakarot/instructions/stop_and_math_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -224,27 +224,39 @@ namespace StopAndMathOperations {
let range_check_ptr = [ap - 2];
let popped = cast([ap - 1], Uint256*);

let (sum, _) = uint256_add(popped[0], popped[1]);
let (_, remainder) = uint256_unsigned_div_rem(sum, popped[2]);
tempvar mod_is_not_zero = popped[2].low + popped[2].high;
jmp addmod_not_zero if mod_is_not_zero != 0;

tempvar bitwise_ptr = cast([fp - 7], BitwiseBuiltin*);
tempvar range_check_ptr = range_check_ptr;
tempvar result = Uint256(remainder.low, remainder.high);
tempvar result = Uint256(0, 0);
jmp end;

addmod_not_zero:
// (a + b) mod n = (a mod n + b mod n)mod n
let (_, a_mod_n) = uint256_unsigned_div_rem(popped[0], popped[2]);
let (_, b_mod_n) = uint256_unsigned_div_rem(popped[1], popped[2]);
let (sum, carry) = uint256_add(a_mod_n, b_mod_n);
let (_, result) = uint256_unsigned_div_rem(sum, popped[2]);
tempvar bitwise_ptr = cast([fp - 7], BitwiseBuiltin*);
tempvar range_check_ptr = range_check_ptr;
tempvar result = result;

jmp end;

MULMOD:
let range_check_ptr = [ap - 2];
let popped = cast([ap - 1], Uint256*);

tempvar mod_is_not_zero = popped[2].low + popped[2].high;
jmp not_zero if mod_is_not_zero != 0;
jmp mulmod_not_zero if mod_is_not_zero != 0;

tempvar bitwise_ptr = cast([fp - 7], BitwiseBuiltin*);
tempvar range_check_ptr = range_check_ptr;
tempvar result = Uint256(0, 0);
jmp end;

not_zero:
mulmod_not_zero:
let (_, _, result) = uint256_mul_div_mod(popped[0], popped[1], popped[2]);

tempvar bitwise_ptr = cast([fp - 7], BitwiseBuiltin*);
Expand Down
4 changes: 4 additions & 0 deletions tests/end_to_end/PlainOpcodes/test_plain_opcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,7 @@ async def test_should_revert_on_fallbacks(
class TestMulmod:
async def test_should_return_0(self, plain_opcodes):
assert 0 == await plain_opcodes.mulmodMax()

class TestAddmod:
async def test_should_return_0(self, plain_opcodes):
assert 0 == await plain_opcodes.addmodMax()