EIPs Explained

EIP-6888 · Stagnant · Core · 2023-04-16

EIP-6888: Arithmetic verification at EVM level

Proposes CPU-style carry and overflow flags for EVM arithmetic plus two conditional jumps (JUMPC, JUMPO), so overflow and division-by-zero checks become cheap at the EVM level.

What problem does it solve?

EVM arithmetic silently wraps: ADD of two large numbers returns the low 256 bits, and DIV by zero returns 0. Safe contracts must therefore check every operation. That used to be done by libraries like OpenZeppelin's SafeMath, and since Solidity 0.8 the compiler inserts checks by default. Either way, each check is several extra opcodes (compare, conditional jump) after every arithmetic step, costing gas.

Physical CPUs solve this with status flags set as a side effect of arithmetic. EIP-6888 proposes the same for the EVM.

How it works

From a hard-fork timestamp (TBD), two boolean flags are added to the execution state, scoped like the program counter:

  • carry: unsigned problem.
  • overflow: signed (two's-complement) problem.

Arithmetic opcodes set them as a side effect, without changing their results:

Opcode Sets carry when Sets overflow when
ADD res < a both operands share a sign and the result's sign differs
SUB b > a signed subtraction overflows
MUL a != 0 and res / a != b signed multiplication overflows (incl. -1 × INT_MIN)
DIV, MOD b == 0 –
SDIV, SMOD – b == 0 or INT_MIN / -1
ADDMOD, MULMOD modulus c == 0 –
EXP a ** b > 2^256 - 1 –
SHL bits are shifted out signed shift changes the value

Two new jump opcodes read the flags:

  • JUMPC: pops a destination; jumps if carry is set, otherwise continues.
  • JUMPO: pops a destination; jumps if overflow is set, otherwise continues.

Both clear both flags and cost G_high, the same as JUMPI. Clearing both at once fits the expected use when moving between code that treats numbers as signed or unsigned.

Sample code

Illustrative bytecode for a checked unsigned add (opcode bytes for JUMPC were never assigned):

// stack: a b
ADD                 // res; sets carry if a + b wrapped
PUSH2 @overflow     // destination
JUMPC               // jump if carry; clears flags
// ... continue with res on the stack ...

@overflow:
JUMPDEST
PUSH1 0 DUP1 REVERT

Today's equivalent emitted by Solidity ≥0.8 needs an extra comparison (DUP, LT/GT, JUMPI) for every checked operation; the flag approach replaces that with a single jump.

Things to know

  • Status is Stagnant; no hard fork includes it, and opcode numbers for JUMPC / JUMPO are not defined.
  • Test cases and reference implementation are listed as TBD.
  • The text has apparent typos: the JUMPO formula refers to carry, and some opcode numbers in the condition list do not match existing opcodes (e.g. MULMOD and SMOD). Treat the table above as the intent, not a byte-exact spec.
  • Flags are set by existing opcodes, which is a change to EVM behaviour, even though it only matters for code that uses the new jumps.
  • Compilers would need to emit the new opcodes before contracts benefit.

#evm #opcodes #arithmetic #overflow #core

Details

Authors
Renan Rodrigues de Souza (@RenanSouza2)
Status
Stagnant
Type
Standards Track / Core
Created
2023-04-16

Machine-readable: Markdown · JSON