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.
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.
Define 0x02
as an allowed type
in types_section
, as defined in EIP-7960. This denotes an EVM64 code section.
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.
We define the following gas cost constants:
G_BASE64
: 1G_VERYLOW64
: 2G_LOW64
: 3G_MID64
: 5G_HIGH64
: 7G_EXP64_STATIC
: 5G_EXP64_DYNAMIC
: 25G_RJUMPIV64
: 3The 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
.
01
) and SUB (03
): a op b mod 2^64
, gas cost G_VERYLOW64
.02
), DIV (04
), SDIV (05
), MOD (06
), SMOD (07
), SIGNEXTEND (0B
): a op b mod 2^64
, gas cost G_LOW64
.08
), MULMOD (09
): a op b % N mod 2^64
, gas cost G_MID64
.0A
): a EXP b mod 2^64
, gas cost static_gas = G_EXP64_STATIC, dynamic_gas = G_EXP64_DYNAMIC * exponent_byte_size
.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.
10
), GT (11
), SLT (12
), SGT (13
), EQ (14
), AND (16
), OR (17
), XOR (18
): a op b mod 2^64
, gas cost G_VERYLOW64
15
), NOT (19
): op a mod 2^64
, gas cost G_VERYLOW64
1B
), SHR (1C
), SAR (1D
): a op N mod 2^64
, gas cost G_VERYLOW64
1A
) is defined as (x >> i * 8) & 0xFF
. Note that the definition is changed from big endian to little endian.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.
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.
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:
RJUMPI64
(0xe1), the condition popped from stack is only read for the last 64 bits. Gas cost is G_RJUMPIV64
.RJUMPV64
(0xe2), the case popped from stack is only read for the last 64 bits. Gas cost is G_RJUMPIV64
."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:
inputs
to 64 bits.0
to outputs
to make it 256 bits.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.
No backward compatibility issues found.
Needs discussion.
Copyright and related rights waived via CC0.