EIP-6888 EVM-level arithmetic overflow checking (Arithmetic verification at EVM level)

No call decision · EVM & execution · EIP status Stagnant · created 2023-04-16

A proposal to add new opcodes JUMPC and JUMPO that let the EVM directly check for overflow or division by zero in arithmetic operations like addition and multiplication, flag the result, and branch on that flag.

Why it is needed

Today, for a smart contract to prevent overflow, the compiler or a library must insert separate checking code. Solidity has had overflow checks turned on by default for all arithmetic operations since 0.8, and before that OpenZeppelin's SafeMath library served the same role. From the EVM's perspective, both approaches mean performing extra comparison or inverse operations, which costs more gas. This proposal aims to push overflow checking down into the EVM itself, so that arithmetic can be used safely while saving gas, without needing to insert checking logic into the code every time.

What changes

Today

After EIP-6888

Who is affected

In depth

Two state flags: carry and overflow

This proposal adds two boolean flags, carry and overflow, to EVM state. carry indicates overflow in unsigned operations, and overflow indicates overflow in signed (two's complement) operations. Both flags start as false, and their scope is specified to be the same as the program counter (pc).

Conditions under which the carry flag is set

carry becomes true when res < a in ADD, when a != 0 and res / a != b in MUL, when b > a in SUB, when b == 0 in DIV and MOD, when c == 0 in ADDMOD and MULMOD, when a ** b exceeds UINT_MAX in EXP, and when res >> a != b in SHL. These are all overflow/division-by-zero determinations based on unsigned integers.

Conditions under which the overflow flag is set

overflow applies only to signed operations. For ADD and SUB, it is determined by comparing the sign patterns of a and b (same or different) against the sign of the result res; for MUL, it looks at the combination of INT_MIN and -1 or cases where the SDIV inverse operation fails to restore the value. For SDIV and SMOD it becomes true when b == 0, or when a == INT_MIN and b == -1; for SHL it becomes true when the value differs after reverting with SAR.

The new opcodes JUMPC and JUMPO

JUMPC and JUMPO each pop a single jump destination pc from the stack, and jump to that pc or proceed to the next instruction depending on the value of the carry and overflow flags respectively. Both opcodes reset the carry and overflow flags to false simultaneously when executed. Their gas cost is set at G_high, the same as JUMPI.

Why the flags are cleared together

According to the Rationale, since both flags are expected to be used at points where code transitions between signed and unsigned handling, they are always reset together whenever JUMPC or JUMPO executes. In other words, even if only one flag is checked before moving on, the state is cleanly reset for the next operation.

Exceptional handling of the EXP operation

While other operations determine overflow via an inverse operation (e.g., verifying ADD with SUB), it explains that EXP has no corresponding LOG operation in the EVM, making it hard to build a similarly concise check. So it proposes that, since most EXP implementations internally use repeated MUL operations, EXP's carry flag can simply be drawn from that internal MUL operation.

Backwards compatibility and status

This EIP adds new opcodes and changes the EVM's behavior for existing integer operations, so it affects backwards compatibility. It is defined to take effect from blocks where BLOCK_TIMESTAMP is at or above HARDFORK_TIMESTAMP (TBD), and both Test Cases and the Reference Implementation are left as TBD. Its current status is Stagnant, meaning it is not an active proposal under discussion for fork inclusion.

Original EIP · ← EIPs