EIP-1051 · Stagnant · Core · 2018-05-02
EIP-1051: Overflow checking for the EVM
Proposed CPU-style overflow flags for EVM ADD, SUB and MUL, plus two opcodes to read and clear them, so contracts could catch overflow cheaply; it is Stagnant and was never adopted.
What problem does it solve?
EVM arithmetic works on 256-bit integers modulo 2^256. ADD, SUB and MUL wrap around silently: 0 - 1 becomes 2^256 - 1, and nothing signals that it happened. Many contracts are correct only if overflow never occurs, as with token balances. They had to add an explicit check after every operation (the SafeMath pattern), which costs gas and makes code longer.
EIP-1051 proposes doing what real CPUs do: keep overflow flags that the hardware sets automatically, and let the program check them whenever it likes. A contract could then run a series of operations and check once at the end.
How it works
Two flags are added to the EVM's execution state:
ovf(unsigned overflow) is set when, treating inputs as unsigned:ADDexceeds2^256 - 1SUBgoes below 0MULexceeds2^256 - 1
sovf(signed overflow) is set wheneverovfis set, and also when a two's-complement result has the wrong sign. For example, adding two positives gives a negative, subtracting a negative from a positive gives a negative, or a product has the wrong sign for its inputs.
Two new opcodes, each taking no stack inputs:
| Opcode | Name | Effect |
|---|---|---|
0x0c |
OVF |
Push 1 if ovf is set, else 0, then clear ovf |
0x0d |
SOVF |
Push 1 if sovf is set, else 0, then clear sovf |
The arithmetic opcodes themselves behave exactly as before, so existing contracts are unaffected. The flags are separate because a signed overflow does not always cause an unsigned one.
The spec considered an opcode that turns on "revert on overflow" mode and rejected it, because it would force one way of handling overflow on every contract.
Sample code
Checking a batch of operations once instead of after each step (EVM assembly, hypothetical):
OVF POP // clear any stale flag first
PUSH 0x04 CALLDATALOAD
PUSH 0x24 CALLDATALOAD
ADD // a + b
PUSH 0x44 CALLDATALOAD
MUL // (a + b) * c
OVF // 1 if either step overflowed (unsigned)
PUSH @revert JUMPI
The same idea in pseudocode:
evm.ovf = False
x = (a + b) % 2**256 # sets ovf if a + b >= 2**256
y = (x * c) % 2**256 # sets ovf if x * c >= 2**256
if read_and_clear(evm.ovf):
revert()
Things to know
- This proposal is Stagnant and was never adopted. Opcodes
0x0cand0x0ddo not have this meaning on any Ethereum network. - The spec leaves open how the flags interact with calls. It does not say whether they are saved per call frame or reset across
CALL, and both test cases and implementation are marked TBD. - The problem was solved in the compiler instead. Since Solidity 0.8.0, arithmetic is checked by default and reverts on overflow, with
unchecked { ... }blocks to opt out. That made SafeMath libraries largely unnecessary. - Other EVM arithmetic changes did ship, such as the shift opcodes in EIP-145 and
PUSH0in EIP-3855. Overflow flags did not.