plonky2/evm/src/cpu/kernel/asm/account_code.asm

136 lines
4.9 KiB
NASM
Raw Normal View History

2022-11-02 10:37:19 +01:00
retzero:
%stack (account_ptr, retdest) -> (retdest, 0)
JUMP
global extcodehash:
// stack: address, retdest
2022-10-21 18:00:41 +02:00
%mpt_read_state_trie
// stack: account_ptr
2022-11-02 10:37:19 +01:00
DUP1 ISZERO %jumpi(retzero)
2022-10-21 18:00:41 +02:00
%add_const(3)
// stack: codehash_ptr
%mload_trie_data
// stack: codehash
2022-10-27 11:06:24 +02:00
%macro codesize
2022-10-27 14:57:17 +02:00
// stack: (empty)
2022-10-31 10:15:43 +01:00
%address
2022-10-27 11:06:24 +02:00
%extcodesize
%endmacro
2022-10-21 18:11:27 +02:00
%macro extcodesize
%stack (address) -> (address, %%after)
2022-10-21 18:00:41 +02:00
%jump(load_code)
%%after:
%endmacro
2022-10-24 13:06:53 +02:00
global extcodesize:
2022-10-27 09:32:29 +02:00
// stack: address, retdest
2022-10-24 13:06:53 +02:00
%extcodesize
2022-10-27 09:32:29 +02:00
// stack: extcodesize(address), retdest
SWAP1 JUMP
2022-10-24 13:06:53 +02:00
2022-10-21 18:00:41 +02:00
2022-10-21 18:11:27 +02:00
%macro codecopy
// stack: dest_offset, offset, size, retdest
2022-10-31 10:15:43 +01:00
%address
2022-10-21 18:11:27 +02:00
// stack: address, dest_offset, offset, size, retdest
%jump(extcodecopy)
%endmacro
2022-10-27 14:57:17 +02:00
// Pre stack: address, dest_offset, offset, size, retdest
// Post stack: (empty)
2022-10-21 18:00:41 +02:00
global extcodecopy:
// stack: address, dest_offset, offset, size, retdest
%stack (address, dest_offset, offset, size, retdest) -> (address, extcodecopy_contd, size, offset, dest_offset, retdest)
%jump(load_code)
2022-10-27 14:57:17 +02:00
2022-10-21 18:00:41 +02:00
extcodecopy_contd:
// stack: code_length, size, offset, dest_offset, retdest
SWAP1
// stack: size, code_length, offset, dest_offset, retdest
PUSH 0
2022-10-27 14:57:17 +02:00
// Loop copying the `code[offset]` to `memory[dest_offset]` until `i==size`.
// Each iteration increments `offset, dest_offset, i`.
2022-10-21 18:00:41 +02:00
extcodecopy_loop:
// stack: i, size, code_length, offset, dest_offset, retdest
DUP2 DUP2 EQ
// stack: i == size, i, size, code_length, offset, dest_offset, retdest
%jumpi(extcodecopy_end)
2022-10-21 18:11:27 +02:00
%stack (i, size, code_length, offset, dest_offset, retdest) -> (offset, code_length, offset, code_length, dest_offset, i, size, retdest)
2022-10-21 18:00:41 +02:00
LT
// stack: offset < code_length, offset, code_length, dest_offset, i, size, retdest
DUP2
// stack: offset, offset < code_length, offset, code_length, dest_offset, i, size, retdest
%mload_current(@SEGMENT_KERNEL_ACCOUNT_CODE)
// stack: opcode, offset < code_length, offset, code_length, dest_offset, i, size, retdest
2022-10-21 18:11:27 +02:00
%stack (opcode, offset_lt_code_length, offset, code_length, dest_offset, i, size, retdest) -> (offset_lt_code_length, 0, opcode, offset, code_length, dest_offset, i, size, retdest)
2022-10-27 14:57:17 +02:00
// If `offset >= code_length`, use `opcode=0`. Necessary since `SEGMENT_KERNEL_ACCOUNT_CODE` might be clobbered from previous calls.
2022-10-21 18:00:41 +02:00
%select_bool
// stack: opcode, offset, code_length, dest_offset, i, size, retdest
DUP4
// stack: dest_offset, opcode, offset, code_length, dest_offset, i, size, retdest
2022-10-21 18:11:27 +02:00
%mstore_main
// stack: offset, code_length, dest_offset, i, size, retdest
%increment
// stack: offset+1, code_length, dest_offset, i, size, retdest
SWAP2
// stack: dest_offset, code_length, offset+1, i, size, retdest
%increment
// stack: dest_offset+1, code_length, offset+1, i, size, retdest
SWAP3
// stack: i, code_length, offset+1, dest_offset+1, size, retdest
%increment
// stack: i+1, code_length, offset+1, dest_offset+1, size, retdest
%stack (i, code_length, offset, dest_offset, size, retdest) -> (i, size, code_length, offset, dest_offset, retdest)
%jump(extcodecopy_loop)
2022-10-21 18:00:41 +02:00
extcodecopy_end:
2022-10-27 11:06:24 +02:00
%stack (i, size, code_length, offset, dest_offset, retdest) -> (retdest)
2022-10-21 18:00:41 +02:00
JUMP
2022-10-27 11:06:24 +02:00
// Loads the code at `address` in the `SEGMENT_KERNEL_ACCOUNT_CODE` at the current context and starting at offset 0.
// Checks that the hash of the loaded code corresponds to the `codehash` in the state trie.
// Pre stack: address, retdest
// Post stack: extcodesize(address)
2022-10-21 18:00:41 +02:00
load_code:
2022-11-02 10:37:19 +01:00
%stack: (address, retdest) -> (extcodehash, address, load_code_ctd, retdest)
JUMP
load_code_ctd:
2022-10-21 18:00:41 +02:00
// stack: codehash, retdest
PROVER_INPUT(account_code::length)
// stack: code_length, codehash, retdest
PUSH 0
2022-10-27 14:57:17 +02:00
// Loop non-deterministically querying `code[i]` and storing it in `SEGMENT_KERNEL_ACCOUNT_CODE` at offset `i`, until `i==code_length`.
2022-10-21 18:00:41 +02:00
load_code_loop:
// stack: i, code_length, codehash, retdest
DUP2 DUP2 EQ
// stack: i == code_length, i, code_length, codehash, retdest
%jumpi(load_code_check)
PROVER_INPUT(account_code::get)
// stack: opcode, i, code_length, codehash, retdest
DUP2
// stack: i, opcode, i, code_length, codehash, retdest
%mstore_current(@SEGMENT_KERNEL_ACCOUNT_CODE)
// stack: i, code_length, codehash, retdest
%increment
// stack: i+1, code_length, codehash, retdest
%jump(load_code_loop)
2022-10-27 14:57:17 +02:00
// Check that the hash of the loaded code equals `codehash`.
2022-10-21 18:00:41 +02:00
load_code_check:
// stack: i, code_length, codehash, retdest
POP
// stack: code_length, codehash, retdest
2022-10-27 14:57:17 +02:00
%stack (code_length, codehash, retdest) -> (@SEGMENT_KERNEL_ACCOUNT_CODE, 0, code_length, codehash, retdest, code_length)
2022-10-21 18:00:41 +02:00
GET_CONTEXT
// stack: context, segment, 0, code_length, codehash, retdest, code_length
KECCAK_GENERAL
// stack: shouldbecodehash, codehash, retdest, code_length
%assert_eq
JUMP