EIP-7961 EOF code section dedicated to EVM64 (EVM64 - EOF code section)

No call decision · EVM & execution · EIP status Draft · created 2025-05-28

Adds a code section type (0x02) dedicated to EVM64 to the EOF container. Entering this section executes in 'pure EVM64' mode, where every operation only operates on the lower 64 bits of a value.

Why it is needed

The Ethereum Virtual Machine (EVM) operates on 256-bit values by default. EIP-7937 already proposes an EVM64 instruction set that uses only 64-bit operations, but it does so by prefixing existing opcodes, which gives it the best compatibility with the existing EVM at the cost of longer, multi-byte instructions. This EIP takes a different approach. It marks a code section in the new EOF (EVM Object Format) container contract format as an 'EVM64 type', so that the moment execution enters that section the interpreter switches entirely into 64-bit mode. Since no prefix is needed, code size shrinks, and within the section the interpreter has room to optimize execution with a 64-bit stack. The tradeoff is that it only works in EOF contracts, and interacting with system calls that read block information, balances, or storage becomes harder.

What changes

Today

After EIP-7961

Who is affected

In depth

Type 0x02 and pure EVM64 mode

An allowed type 0x02 is added to the types_section defined by EIP-7960. This type indicates that the code section is EVM64 code. When a contract call begins, or the section is entered via CALLF, the interpreter enters 'pure EVM64 mode'. In this mode, only the opcodes listed in the specification are allowed, and every opcode treats only the lowest 64 bits of a stack value as little endian. During EOF validation, the validation function must enforce that no disallowed opcode exists within the section.

Dedicated gas constants

New gas constants are defined for EVM64 mode. G_BASE64 is 1, G_VERYLOW64 is 2, G_LOW64 is 3, G_MID64 is 5, and G_HIGH64 is 7. For exponentiation there is G_EXP64_STATIC at 5 and G_EXP64_DYNAMIC at 25, and for conditional branching G_RJUMPIV64 is 3. Each subsequent opcode definition references these constants for its gas cost.

Arithmetic, comparison, and bitwise operations

Arithmetic opcodes keep their existing meaning but compute the operand modulo 2^64. ADD(01) and SUB(03) cost G_VERYLOW64; MUL(02), DIV(04), SDIV(05), MOD(06), SMOD(07), and SIGNEXTEND(0B) cost G_LOW64; ADDMOD(08) and MULMOD(09) cost G_MID64. EXP(0A) adds the static cost G_EXP64_STATIC to a dynamic cost of G_EXP64_DYNAMIC multiplied by exponent_byte_size. The comparison and bitwise opcodes LT(10), GT(11), SLT(12), SGT(13), EQ(14), ISZERO(15), AND(16), OR(17), XOR(18), NOT(19), SHL(1B), SHR(1C), and SAR(1D) all cost G_VERYLOW64. BYTE(1A) is redefined as (x >> i * 8) & 0xFF, changing the byte-counting direction from big endian to little endian.

Memory and stack opcodes

MLOAD64(0x51) reads a 64-bit integer from memory as little endian and pushes it onto the stack, while MSTORE64(0x52) writes a 64-bit integer from the stack to memory as little endian. Both opcodes cost G_VERYLOW64, and memory expansion cost is calculated on an 8-byte basis. MSTORE8 can still be used, with the same gas cost as in the regular EVM. PUSH0(0x5f) through PUSH8(0x67) are followed by a 0-byte to 8-byte literal, which is read as little endian and pushed onto the stack. Gas cost is G_VERYLOW64. POP, SWAPn, and DUPn can also be used, with the same gas cost as the regular EVM.

Control flow and termination opcodes

RETURN, REVERT, and INVALID can be used in EVM64 mode, with unchanged behavior and gas cost, except that every stack item is read as only its lowest 64 bits. CALLF, RETF, and RJUMP also keep their behavior and gas cost unchanged. Conditional branching differs: RJUMPI64(0xe1) reads only the last 64 bits of the condition value popped from the stack, and RJUMPV64(0xe2) reads only the last 64 bits of the case value; both cost G_RJUMPIV64.

The stack is still 256-bit; optimization is left to the implementation

This specification does not separately define a 64-bit stack. The stack remains 256-bit, but since every opcode only handles the lower 64 bits, the upper 192 bits become unobservable while inside an EVM64 section. This allows the interpreter to optimize by truncating inputs to 64 bits on entering the section, using a 64-bit stack during execution, and prefixing outputs with zeros to restore them to 256 bits on exiting the section. Because memory is shared across the entire execution, EVM64 code can put data into memory and retrieve it at any time.

Tradeoffs compared with EIP-7937

EIP-7937 uses prefix opcodes to stay maximally compatible with the existing EVM, while this EIP is an alternative that uses EOF code sections. This approach has the advantage of shrinking code size since it needs no multi-byte opcodes, but it only works in EOF contracts and cannot be used on the legacy EVM. It also cannot make system calls that read block values, addresses, balances, or storage from within an EVM64 code section, making it less seamless than EIP-7937, which can move freely in and out of 64-bit mode. The source notes that whether this difference is an advantage or a disadvantage depends on how the 64-bit optimization is implemented.

Current status: Draft

This EIP is in Draft status, so its content may still change. The source marks test cases and the reference implementation as unfinished (TODO), and merely notes that security considerations still need discussion. No decision to include it in a specific fork has been confirmed by core developer calls.

Original EIP · ← EIPs