Skip to content

Commit

Permalink
Check the global variable GLOBAL_METADATA_TRIE_DATA_SIZE (#483)
Browse files Browse the repository at this point in the history
* Check trie data length

* Change order of checks and improve comments

* Apply comments
  • Loading branch information
LindaGuiga authored Aug 14, 2024
1 parent 7d8fece commit f47590f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
43 changes: 36 additions & 7 deletions evm_arithmetization/src/cpu/kernel/asm/main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,28 @@ global hash_initial_tries:
// We compute the length of the trie data segment in `mpt_hash` so that we
// can check the value provided by the prover.
// The trie data segment is already written by the linked lists
%get_trie_data_size

// stack: trie_data_len
// First, we compute the initial size of the trie data segment.
PUSH @ACCOUNTS_LINKED_LISTS_NODE_SIZE
PUSH @SEGMENT_ACCOUNTS_LINKED_LIST
%mload_global_metadata(@GLOBAL_METADATA_ACCOUNTS_LINKED_LIST_NEXT_AVAILABLE)
SUB
// stack: accounts_ll_full_len, accounts_ll_node_size
DIV
%decrement
// stack: actual_nb_accounts
// The initial payloads are written twice, and each payload requires 4 elements.
PUSH 8 MUL
%increment
// stack: init_trie_data_len

%mpt_hash_txn_trie %mload_global_metadata(@GLOBAL_METADATA_TXN_TRIE_DIGEST_BEFORE) %assert_eq
// stack: trie_data_len
%mpt_hash_receipt_trie %mload_global_metadata(@GLOBAL_METADATA_RECEIPT_TRIE_DIGEST_BEFORE) %assert_eq
// stack: trie_data_full_len

%set_trie_data_size
// Check that the trie data length is correct.
%mload_global_metadata(@GLOBAL_METADATA_TRIE_DATA_SIZE)
%assert_eq

global start_txns:
// stack: (empty)
Expand Down Expand Up @@ -157,14 +170,24 @@ global perform_final_checks:
%mload_global_metadata(@GLOBAL_METADATA_TXN_NUMBER_AFTER) %assert_eq
%pop3

PUSH 1 // initial trie data length
// We set a dummy value as an initial trie data length,
// since the final transaction and receipt tries have already been
// added to `GLOBAL_METADATA_TRIE_DATA_SIZE`.
PUSH 1
global check_txn_trie:
%mpt_hash_txn_trie %mload_global_metadata(@GLOBAL_METADATA_TXN_TRIE_DIGEST_AFTER) %assert_eq
global check_receipt_trie:
%mpt_hash_receipt_trie %mload_global_metadata(@GLOBAL_METADATA_RECEIPT_TRIE_DIGEST_AFTER) %assert_eq
global check_state_trie:
// First, check initial trie.
// We pop the dummy trie data length that was computed.
POP
// Now, we get the trie data size so we can add the values from the
// initial trie data size and check that the value stored in
// `GLOBAL_METADATA_TRIE_DATA_SIZE` is correct.
%get_trie_data_size
// stack: trie_data_len
PROVER_INPUT(trie_ptr::state)

%mstore_global_metadata(@GLOBAL_METADATA_STATE_TRIE_ROOT)
Expand All @@ -173,13 +196,19 @@ global check_state_trie:
%mstore_global_metadata(@GLOBAL_METADATA_TRIE_DATA_SIZE)

%set_initial_tries
%get_trie_data_size
%mpt_hash_state_trie

SWAP1 %set_trie_data_size
// stack: init_state_hash, trie_data_len
// Check that the initial trie is correct.
%mload_global_metadata(@GLOBAL_METADATA_STATE_TRIE_DIGEST_BEFORE)
%assert_eq
// Check that the stored trie data length is correct.
%mload_global_metadata(@GLOBAL_METADATA_TRIE_DATA_SIZE)
%assert_eq

// We set a dummy value as an initial trie data length,
// as we do not need to compute the actual trie data length here.
PUSH 1
global check_final_state_trie:
%set_final_tries
%mpt_hash_state_trie %mload_global_metadata(@GLOBAL_METADATA_STATE_TRIE_DIGEST_AFTER) %assert_eq
Expand Down
8 changes: 7 additions & 1 deletion evm_arithmetization/src/generation/mpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ fn parse_storage_value(value_rlp: &[u8]) -> Result<Vec<U256>, ProgramError> {
Ok(vec![value])
}

fn parse_storage_value_no_return(value_rlp: &[u8]) -> Result<Vec<U256>, ProgramError> {
Ok(vec![])
}

const fn empty_nibbles() -> Nibbles {
Nibbles {
count: 0,
Expand Down Expand Up @@ -302,7 +306,9 @@ fn load_state_trie(
let storage_ptr_ptr = trie_data.len();
trie_data.push(Some((trie_data.len() + 2).into()));
trie_data.push(Some(code_hash.into_uint()));
let storage_ptr = load_mpt(storage_trie, trie_data, &parse_storage_value)?;
// We don't need to store the slot values, as they will be overwritten in
// `mpt_set_payload`.
let storage_ptr = load_mpt(storage_trie, trie_data, &parse_storage_value_no_return)?;
if storage_ptr == 0 {
trie_data[storage_ptr_ptr] = Some(0.into());
}
Expand Down

0 comments on commit f47590f

Please sign in to comment.