---
eip: 1051
label: EIP-1051
title: Overflow checking for the EVM
status: Stagnant
type: Standards Track
category: Core
created: 2018-05-02
official: https://eips.ethereum.org/EIPS/eip-1051
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1051.md
canonical: https://eips.huatuo.xyz/eip-1051/
lang: en
tldr: 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.
tags: [evm, opcode, arithmetic, overflow, stagnant]
---

# EIP-1051: Overflow checking for the EVM

> This EIP adds overflow checking for EVM arithmetic operations, and two new opcodes that check and clear the overflow flags.

**TL;DR:** 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:
  - `ADD` exceeds `2^256 - 1`
  - `SUB` goes below 0
  - `MUL` exceeds `2^256 - 1`
- **`sovf`** (signed overflow) is set whenever `ovf` is 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:

```python
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 `0x0c` and `0x0d` do 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](/eip-145/) and `PUSH0` in [EIP-3855](/eip-3855/). Overflow flags did not.


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-1051
