EIP-7961 - EVM64 - EOF code section

Created 2025-05-28
Status Draft
Category Core
Type Standards Track
Authors
Requires

Abstract

An EOF-only specification for EVM64 instruction set. This defines a separate "EVM64" type for EOF code section in addition to "regular EVM". The interpreter then enters EVM64 mode when entering the code section. This EIP is an alternative to EIP-7937.

Motivation

EIP-7937 has maximum compatibility with existing EVM. It implements EVM64 simply as a group of additional opcodes (using a prefix opcode). This EIP defines an alternative method, using EOF container's code section. It has its pros and cons. The code size will obviously become shorter, due to not needing multibyte opcodes any more. On the other hand, interop with EVM system calls become more difficult because it cannot be done in an EVM64 code section. The advantages and disadvantages are discussed further in the Rationale section.

Specification

Define 0x02 as an allowed type in types_section, as defined in EIP-7960. This denotes an EVM64 code section.

EOF function execution

When entering an EOF code section (either at the beginning of the contract call, or through CALLF), it enters "pure EVM64 mode". Unless defined below, no other opcodes are allowed. Those opcodes all only operates on the least significant 64-bit, in little endian.

During EOF validation, the validation function should enforce that only allowed opcodes exist.

Gas cost constants

We define the following gas cost constants:

Arithmetic opcodes

The 64-bit mode arithmetic opcodes are defined the same as non-64-bit mode, except that it only operates on the least significant 64-bits. In the below definition, a, b, N is a mod 2^64, b mod 2^64 and N mod 2^64.

Comparison and bitwise opcodes

The 64-bit mode comparison and bitwise opcodes are defined the same as non-64-bit mode, except that they only operates on the least significant 64 bits.

Memory opcodes

MLOAD64 (0x51) will load a 64-bits integer in little endian onto the stack. MSTORE64 (0x52) will read an 64-bits integer from the stack, and store it to memory in little endian.

The gas cost for both opcodes is G_VERYLOW64. The memory resizing costs count as 8 bytes.

MSTORE8 is available in EVM64 mode, and its gas cost is the same as in "normal" EVM.

Stack opcodes

PUSH0 (0x59) to PUSH8 (0x67) follows 0-byte to 8-byte literal. The literal is read little endian and pushed onto the stack. The gas cost for them is G_VERYLOW64.

POP, SWAPn and DUPn are available in EVM64 mode, and their gas costs are the same as in "normal" EVM.

Other opcodes

Contract opcodes RETURN, REVERT, INVALID are available in EVM64 mode. Their behaviors, including gas costs, are unchanged. However, for all stack items, only the least significant 64 bits are read.

CALLF, RETF, RJUMP are available in EVM64 mode. Their behaviors, including gas costs, are unchanged.

For flow operations RJUMPI and RJUMPV, the 64-bit mode has following changes:

Rationale

Stack behavior

"Pure" in "pure EVM64" refers to the fact that all opcodes in EVM64 mode only operates on the least significant 64 bits. In this specification, we don't specifically define 64-bit stack. As far as this EIP is concerned, stack is still 256-bit. However, because it only operates on the least significant 64 bits. The most significant 192 bits becomes unobservable as long as the interpreter is in an EVM64 code section. Thus an EVM interpreter can optimize EVM64 execution as follows:

Discussions

This alternative definition (compared with EIP-7937) has the advantage that the code size is now shorter (because no multibyte opcodes are needed). It however will only work with EOF contract but not "legacy" EVM. The interaction between EVM64 and "system calls" (those calls that reads Ethereum block values, addresses, balances and storages) will be more difficult. It's not "seamless" like EIP-7937 where one can enter/exit 64-bit mode at ease. Depending on how the 64-bit optimization works out, this may be an advantage or an disadvantage.

The memory is, as usual, still shared during the entire execution. So in EVM64, the contract can always use the memory to push/fetch data.

Backwards Compatibility

No backward compatibility issues found.

Security Considerations

Needs discussion.

Copyright

Copyright and related rights waived via CC0.