Skip to content

Commit

Permalink
Cherry pick what's left of Linked lists for the state trie (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nashtare committed Aug 6, 2024
1 parent fd0f7d5 commit 2dc42ca
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,4 @@ global remove_accessed_storage_keys:
// stack: next_next_ptr, next_ptr_ptr, addr, key, retdest
MSTORE_GENERAL
%pop2
JUMP
JUMP
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ revert_account_destroyed_contd:
// stack: address, prev_balance, retdest
%read_accounts_linked_list
// stack: account_payload_ptr, prev_balance, retdest
DUP1 %assert_nonzero
DUP1
%assert_nonzero
%increment
// stack: account_balance_payload_ptr, prev_balance, retdest
%mstore_trie_data
JUMP

Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ global revert_nonce_change:
%mstore_trie_data
// stack: retdest
JUMP

9 changes: 2 additions & 7 deletions evm_arithmetization/src/cpu/kernel/asm/main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,12 @@ global main:
// Encode constant nodes
%initialize_rlp_segment

// Initialize linked list and trie data constants.
// TODO: Validate them.
PROVER_INPUT(linked_list::accounts_linked_list_len)
%mstore_global_metadata(@GLOBAL_METADATA_ACCOUNTS_LINKED_LIST_LEN)
PROVER_INPUT(linked_list::storage_linked_list_len)
%mstore_global_metadata(@GLOBAL_METADATA_STORAGE_LINKED_LIST_LEN)
// Initialize trie data size.
PROVER_INPUT(trie_ptr::trie_data_size)
%mstore_global_metadata(@GLOBAL_METADATA_TRIE_DATA_SIZE)

global store_initial:
// Store the inital accounts and slots for hashing later
// Store the initial accounts and slots for hashing later
%store_initial_accounts
%store_initial_slots
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ global delete_account:
%jump(delete_account)
%%after:
// stack: (empty)
%endmacro
%endmacro
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ global encode_receipt:
// stack: first_value, rlp_addr, value_ptr, cur_len, retdest
// The first value is either the transaction type or the payload length.
// Since the receipt contains at least the 256-bytes long bloom filter, payload_len > 3.
DUP1 %lt_const(4) %jumpi(encode_nonzero_receipt_type)
DUP1 %lt_const(3) %jumpi(encode_nonzero_receipt_type)
// If we are here, then the first byte is the payload length.
%rlp_list_len
// stack: rlp_receipt_len, rlp_addr, value_ptr, cur_len, retdest
Expand Down Expand Up @@ -348,4 +348,3 @@ global encode_storage_value:
// stack: rlp_addr', cur_len, retdest
%stack (rlp_addr, cur_len, retdest) -> (retdest, rlp_addr, cur_len)
JUMP

Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ after_insert_slot:
%jump(insert_all_slots)

// Delete all the accounts, referenced by the respective nodes in the linked list starting at
// `account_ptr_ptr`, which where deleted from the intial state. Delete also all slots of non-deleted accounts
// `account_ptr_ptr`, which where deleted from the initial state. Delete also all slots of non-deleted accounts
// deleted from the storage trie.
// Pre stack: account_ptr_ptr, root_ptr, storage_ptr_ptr, retdest
// Post stack: new_root_ptr.
Expand All @@ -100,7 +100,7 @@ global delete_removed_accounts:
// We assume that the size of the initial accounts linked list, containing the accounts
// of the initial state, was stored at `@GLOBAL_METADATA_INITIAL_ACCOUNTS_LINKED_LIST_LEN`.
%mload_global_metadata(@GLOBAL_METADATA_INITIAL_ACCOUNTS_LINKED_LIST_LEN)
// The inital accounts linked list was stored at addresses smaller than `@GLOBAL_METADATA_INITIAL_ACCOUNTS_LINKED_LIST_LEN`.
// The initial accounts linked list was stored at addresses smaller than `@GLOBAL_METADATA_INITIAL_ACCOUNTS_LINKED_LIST_LEN`.
// If we also know that `@SEGMENT_ACCOUNT_LINKED_LIST <= account_ptr_ptr`, for deleting node at `addr_ptr_ptr` it
// suffices to check that `account_ptr_ptr` != `@GLOBAL_METADATA_INITIAL_ACCOUNTS_LINKED_LIST_LEN`
EQ
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// to mem[payload_ptr_ptr] + step*i,
// for i =0..n_leaves. This is used to constraint the
// initial state and account tries payload pointers such that they are exactly
// those of the inital accounts and linked lists.
// those of the initial accounts and linked lists.
// Pre stack: node_ptr, account_ptr_ptr, storage_ptr_ptr, retdest
// Post stack: account_ptr_ptr, storage_ptr_ptr
global mpt_set_payload:
Expand All @@ -17,6 +17,8 @@ global mpt_set_payload:
DUP1 %eq_const(@MPT_NODE_BRANCH) %jumpi(set_payload_branch)
DUP1 %eq_const(@MPT_NODE_EXTENSION) %jumpi(set_payload_extension)
DUP1 %eq_const(@MPT_NODE_LEAF) %jumpi(set_payload_leaf)
DUP1 %eq_const(@MPT_NODE_HASH) %jumpi(skip)
PANIC

skip:
// stack: node_type, after_node_type, account_ptr_ptr, storage_ptr_ptr, retdest
Expand Down Expand Up @@ -46,7 +48,7 @@ skip:
%mstore_global_metadata(@GLOBAL_METADATA_INITIAL_STORAGE_LINKED_LIST_LEN)
%endmacro

// Pre stack: node_ptr, account_ptr_ptr, retdest
// Pre stack: node_ptr, storage_ptr_ptr, retdest
// Post stack: storage_ptr_ptr
global mpt_set_storage_payload:
// stack: node_ptr, storage_ptr_ptr, retdest
Expand Down Expand Up @@ -116,34 +118,33 @@ set_payload_storage_branch:
JUMP

set_payload_extension:
// stack: node_type, after_node_type, storage_ptr_ptr, retdest
// stack: node_type, after_node_type, account_ptr_ptr, storage_ptr_ptr, retdest
POP
// stack: after_node_type, storage_ptr_ptr, retdest
// stack: after_node_type, account_ptr_ptr, storage_ptr_ptr, retdest
%add_const(2) %mload_trie_data
// stack: child_ptr, after_node_type, storage_ptr_ptr, retdest
// stack: child_ptr, after_node_type, account_ptr_ptr, storage_ptr_ptr, retdest
%jump(mpt_set_payload)

set_payload_storage_extension:
// stack: node_type, after_node_type, account_ptr_ptr, storage_ptr_ptr, retdest
// stack: node_type, after_node_type, storage_ptr_ptr, retdest
POP
// stack: after_node_type, account_ptr_ptr, storage_ptr_ptr, retdest
// stack: after_node_type, storage_ptr_ptr, retdest
%add_const(2) %mload_trie_data
// stack: child_ptr, account_ptr_ptr, storage_ptr_ptr, retdest
// stack: child_ptr, storage_ptr_ptr, retdest
%jump(mpt_set_storage_payload)

set_payload_leaf:
// stack: node_type, after_node_type, account_ptr_ptr, storage_ptr_ptr, retdest
POP
%add_const(2) // The payload pointer starts at index 3, after num_nibbles and packed_nibbles.
DUP1
// stack payload_ptr_ptr, payload_ptr_ptr, account_ptr_ptr, storage_ptr_ptr, retdest
// stack: payload_ptr_ptr, payload_ptr_ptr, account_ptr_ptr, storage_ptr_ptr, retdest
%mload_trie_data
// stack account_ptr, payload_ptr_ptr, account_ptr_ptr, storage_ptr_ptr, retdest
// stack: account_ptr, payload_ptr_ptr, account_ptr_ptr, storage_ptr_ptr, retdest
%add_const(2)
%mload_trie_data // storage_root_ptr = account[2]

DUP1 %mload_trie_data
POP
// stack storage_root_ptr, payload_ptr_ptr, account_ptr_ptr, storage_ptr_ptr, retdest
// stack: storage_root_ptr, payload_ptr_ptr, account_ptr_ptr, storage_ptr_ptr, retdest
%stack
(storage_root_ptr, payload_ptr_ptr, account_ptr_ptr, storage_ptr_ptr) ->
(storage_root_ptr, storage_ptr_ptr, after_set_storage_payload, storage_root_ptr, payload_ptr_ptr, account_ptr_ptr)
Expand All @@ -165,7 +166,7 @@ after_set_storage_payload:
%mstore_trie_data // The dynamic account pointer in the linked list has no storage root so we need to manually set it.
%mstore_trie_data // Set the leaf payload pointing to next account in the linked list.
// stack: account_ptr_ptr, storage_ptr_ptr', retdest
%add_const(@STORAGE_NEXT_NODE_PTR) // The next pointer is at distance `STORAGE_NEXT_NODE_PTR`
%add_const(@ACCOUNTS_LINKED_LISTS_NODE_SIZE) // The next pointer is at distance `ACCOUNTS_LINKED_LISTS_NODE_SIZE`
// stack: payload_ptr_ptr', storage_ptr_ptr', retdest
SWAP1
SWAP2
Expand Down
Loading

0 comments on commit 2dc42ca

Please sign in to comment.