You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Current behavior:
The add mod opcode performs addition of two uint256, without handling the one bit carry returned by the uint256_add. This causes the addition to be performed modulo 2^256, which shouldn't be the case (see Ethereum yellow paper opcode ADDMOD).
let (stack, popped) = Stack.pop_n(self=stack, n=3);
let a = popped[0];
let b = popped[1];
let c = popped[2];
// Compute the addition
let (result, _) = uint256_add(a, b);
// Compute the modulo
let (_, rem) = uint256_unsigned_div_rem(result, c);
The add mod opcode should return the operation (a+b)%c, without subjecting the intermediate result a+b to modulo 2^256. However, since the bit carry from uint256_add isn't used, the operation is performed modulo 2^256.
Indeed, in our case, the performed operation is: (-1 + -2) % 5 = ((0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe) % 2^256) % 5 = (0x1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd % 2^256) % 5 = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd % 5 = 3.
Failing output should be:
[!] Case /Users/greg/code/rust/ef-tests/crates/ef-testing/ethereum-tests/BlockchainTests/GeneralStateTests/VMTests/vmArithmeticTest/addmod.json failed (description: addmod): Test failed: failed test addmod_d10g0v0_Shanghai: expected storage value 0x0000000000000000000000000000000000000000000000000000000000000004, got 0x0000000000000000000000000000000000000000000000000000000000000003
The text was updated successfully, but these errors were encountered:
<!--- Please provide a general summary of your changes in the title
above -->
<!-- Give an estimate of the time you spent on this PR in terms of work
days.
Did you spend 0.5 days on this PR or rather 2 days? -->
Time spent on this PR: 0.5d
## Pull request type
<!-- Please try to limit your pull request to one type,
submit multiple pull requests if needed. -->
Please check the type of change your PR introduces:
- [x] Bugfix
- [ ] Feature
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] Documentation content changes
- [ ] Other (please describe):
## What is the current behavior?
<!-- Please describe the current behavior that you are modifying,
or link to a relevant issue. -->
Resolves#695Resolves#691
## What is the new behavior?
<!-- Please describe the behavior or changes that are being added by
this PR. -->
- Fixed the addmod implementation
-
-
Bug Report
Kakarot version: ee6458a
Current behavior:
The add mod opcode performs addition of two uint256, without handling the one bit carry returned by the
uint256_add
. This causes the addition to be performed modulo2^256
, which shouldn't be the case (see Ethereum yellow paper opcode ADDMOD).Expected behavior:
kakarot/src/kakarot/instructions/stop_and_arithmetic_operations.cairo
Lines 320 to 333 in ee6458a
The add mod opcode should return the operation
(a+b)%c
, without subjecting the intermediate resulta+b
to modulo2^256
. However, since the bit carry fromuint256_add
isn't used, the operation is performed modulo2^256
.Steps to reproduce:
This test will fail due to the incorrect addition performed modulo
2^256
:https://github.com/ethereum/tests/blob/develop/src/GeneralStateTestsFiller/VMTests/vmArithmeticTest/addmodFiller.yml#L109
Indeed, in our case, the performed operation is: (-1 + -2) % 5 = ((0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe) % 2^256) % 5 = (0x1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd % 2^256) % 5 = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd % 5 = 3.
Failing output should be:
The text was updated successfully, but these errors were encountered: