---
eip: 3670
label: EIP-3670
title: EOF - Code Validation
status: Stagnant
type: Standards Track
category: Core
created: 2021-06-23
requires: 3540
official: https://eips.ethereum.org/EIPS/eip-3670
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-3670.md
canonical: https://eips.huatuo.xyz/eip-3670/
lang: en
tldr: Proposed deploy-time validation for EOF contracts that rejects undefined opcodes and truncated PUSH data, so every EOF contract on chain is well-formed bytecode.
tags: [evm, eof, bytecode, validation, core]
---

# EIP-3670: EOF - Code Validation

> Validate EOF bytecode for correctness at the time of deployment.

**TL;DR:** Proposed deploy-time validation for EOF contracts that rejects undefined opcodes and truncated PUSH data, so every EOF contract on chain is well-formed bytecode.

## What problem does it solve?

Today's (legacy) EVM accepts any byte sequence as contract code. A contract can contain bytes that are not assigned to any instruction, or end in the middle of a `PUSH` whose data runs past the end of the code. Each EVM implementation has to handle these cases at run time, and nobody can reason about "the instructions in this contract" without caveats.

There is also a forward-compatibility trap: if a contract already contains an unassigned opcode, giving that opcode a meaning later could change how the deployed contract behaves. That makes adding new instructions risky.

EIP-3670 brings code validity into consensus for the EVM Object Format ([EIP-3540](/eip-3540/)). Because EOF contracts are validated once at creation, the EVM can trust them afterwards.

## How it works

The rule activates together with EIP-3540 and applies only to EOF-formatted code. Legacy bytecode is untouched.

At contract creation, every code section of the EOF container is walked instruction by instruction. The code is rejected if:

1. **An opcode is undefined.** `INVALID` (`0xfe`) counts as defined. Deprecated instructions such as `CALLCODE` (`0xf2`) and `SELFDESTRUCT` (`0xff`), plus others removed by EIP-3540 and later EOF EIPs, are treated as undefined.
2. **Immediate data is truncated.** If a `PUSH1`…`PUSH32` near the end of the section is missing some of its data bytes, the code is invalid. Legacy EVM would implicitly pad with zeros; EOF does not.

Bytes in the data section are not checked; only code sections are.

`BLOCKHASH` stays valid even though [EIP-2935](/eip-2935/) offers a replacement, to avoid diverging from legacy code without need.

## Sample code

Simplified validator, adapted from the spec's reference implementation (opcode table abbreviated):

```python
# valid_opcodes: the fork's assigned opcodes minus those rejected in EOF
immediate_sizes = [0] * 256
for op in range(0x60, 0x80):            # PUSH1..PUSH32
    immediate_sizes[op] = op - 0x5f

def validate_instructions(code: bytes, valid_opcodes: set[int]) -> None:
    assert len(code) > 0
    pos = 0
    while pos < len(code):
        op = code[pos]
        if op not in valid_opcodes:
            raise ValueError(f"undefined opcode 0x{op:02x} at {pos}")
        pos += 1 + immediate_sizes[op]  # skip PUSH data
    if pos != len(code):
        raise ValueError("truncated PUSH data")

validate_instructions(bytes([0x60, 0x01, 0x00]), VALID)   # PUSH1 1, STOP -> ok
validate_instructions(bytes([0x61, 0x01]), VALID)         # PUSH2 with 1 byte -> error
```

## Things to know

- Status: **Stagnant**. It is one piece of the EOF package; the bundle is tracked in the meta [EIP-7692](/eip-7692/). Later EOF EIPs such as [EIP-5450](/eip-5450/) (stack validation) and [EIP-4750](/eip-4750/) (functions) add further deploy-time checks.
- Validation only happens at creation, so a deployed EOF contract never needs the run-time checks legacy code does.
- Legacy contracts are never re-validated. The `0xEF` prefix reserved by [EIP-3541](/eip-3541/) is what lets the EVM tell EOF code apart.
- Developers normally do not run this check themselves; compilers that target EOF emit valid code, and clients reject anything else at deploy time.


---

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