Appearance
Abstract
Four new instrutions are introduced, that allow to read EOF container's data section: DATALOAD
loads 32-byte word to stack, DATALOADN
loads 32-byte word to stack where the word is addressed by a static immediate argument, DATASIZE
loads data section size and DATACOPY
copies a segment of data section to memory.
Motivation
Clear separation between code and data is one of the main features of EOF1. Data section may contain anything, e.g. compiler's metadata, but to make it useful for smart contracts, EVM has to have instructions that allow to read from data section. Previously existing instructions for bytecode inspection (CODECOPY
, CODESIZE
etc.) are deprecated in EOF1 and cannot be used for this purpose.
The DATALOAD
, DATASIZE
, DATACOPY
instruction pattern follows the design of existing instructions for reading other kinds of data (i.e. returndata and calldata).
DATALOADN
is an optimized version of DATALOAD
, where data offset to read is set at compilation time, and therefore need not be validated at run-time, which makes the instruction cheaper.
Specification
We introduce four new instructions on the same block number EIP-3540 is activated on:
1 DATALOAD
(0xe8) 2.DATALOADN
(0xe9) 3.DATASIZE
(0xea) 4.DATACOPY
(0xeb)
If the code is legacy bytecode, all of these instructions result in an exceptional halt. (Note: This means no change to behaviour.)
If the code is valid EOF1, the following execution rules apply:
DATALOAD
- Pops one value,
offset
, from the stack. - If
offset + 32
is greater than the data section size of the active container, execution results in exceptional halt. - Reads
[offset:offset+32]
segment from the data section and pushes it as 32-byte value to the stack. - Deducts 4 gas.
DATALOADN
- Has one immediate argument,
offset
, encoded as a 16-bit unsigned big-endian value. - Pops nothing from the stack.
- Reads
[offset:offset+32]
segment from the data section and pushes it as 32-byte value to the stack. - Deducts 3 gas.
[offset:offset+32]
is guaranteed to be within data bounds by code validation.
DATASIZE
- Pops nothing from the stack.
- Pushes the size of the data section of the active container to the stack.
- Deducts 2 gas.
DATACOPY
- Pops three values from the stack:
mem_offset
,offset
,size
. - Performs memory expansion to
mem_offset + size
and deducts memory expansion cost. - Deducts
3 * ((size + 31) // 32)
gas for copying. - If
offset + size
is greater than data section size of the active container, execution results in exceptional halt. - Reads
[offset:offset+size]
segment from the data section and writes it to memory starting at offsetmem_offset
.
Code Validation
We extend code section validation rules (as defined in EIP-3670).
- Code section is invalid in case an immediate argument
offset
of anyDATALOADN
is such thatoffset + 32
is greater than data section size. RJUMP
,RJUMPI
andRJUMPV
immediate argument value (jump destination relative offset) validation: code section is invalid in case offset points to one of two bytes directly followingDATALOADN
instruction.
Rationale
TBA
Backwards Compatibility
This change poses no risk to backwards compatibility, as it is introduced only for EOF1 contracts, for which deploying undefined instructions is not allowed, therefore there are no existing contracts using these instructions. The new instructions are not introduced for legacy bytecode (code which is not EOF formatted).
Security Considerations
TBA
Copyright
Copyright and related rights waived via CC0.