Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor linked lists initial hashing #581

Merged
merged 46 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
bf9894e
Deattach hashed nodes from initial trie
4l0n50 Aug 8, 2024
df38d3b
Merge mpt_set_payload with mpt_hash
4l0n50 Aug 8, 2024
3ce9cf5
Add missing files
4l0n50 Aug 9, 2024
0d5861b
Merge branch 'feat/continuations' into refactor_ll_hashing
4l0n50 Aug 9, 2024
7c31745
Fix errors in the stack
4l0n50 Aug 9, 2024
2e7ba8a
Fix hash mismatch on initial trie
4l0n50 Aug 19, 2024
e0e7ec9
Refactor final state hash
4l0n50 Aug 20, 2024
376b415
Fix final hash
4l0n50 Aug 21, 2024
d1115b0
[WIP] debugging erc721
4l0n50 Aug 23, 2024
65e8d46
Fix erc721
4l0n50 Aug 25, 2024
1618b67
Merge into develop
4l0n50 Aug 25, 2024
5fef5f5
[WIP] debugging block 28
4l0n50 Aug 26, 2024
c67acf6
Fix b28
4l0n50 Aug 26, 2024
9d33eb9
Merge
4l0n50 Aug 27, 2024
dd2dbff
Merge
4l0n50 Aug 27, 2024
ba89d45
Fix block 28
4l0n50 Aug 28, 2024
82902da
Minor
4l0n50 Aug 28, 2024
ee613fd
[WIP] Debugging block 20240058
4l0n50 Aug 28, 2024
baee99c
Fix extension nodes bug
4l0n50 Aug 28, 2024
d5b15b7
Fix block 20240058
4l0n50 Aug 28, 2024
ac457ca
[WIP] benchmarking
4l0n50 Aug 28, 2024
038af77
Set inital trie with insertions
4l0n50 Aug 29, 2024
8e3584d
Remove hash nodes
4l0n50 Aug 29, 2024
4e9cc3d
Fix missing segment number
4l0n50 Aug 29, 2024
a383475
Clean code
4l0n50 Aug 30, 2024
4117e0c
Fix unit tests
4l0n50 Aug 30, 2024
3edcd94
Fix erc20
4l0n50 Sep 1, 2024
c06e184
[WIP] Fixing blocks
4l0n50 Sep 1, 2024
783a811
Merge with develop
4l0n50 Sep 2, 2024
9df960b
Clean code
4l0n50 Sep 2, 2024
465240b
Clean and fmt
4l0n50 Sep 2, 2024
1b394be
Address reviews
4l0n50 Sep 11, 2024
e91891e
Apply suggestions from code review
4l0n50 Sep 11, 2024
0349c48
[WIP] Fixing trie data length
4l0n50 Sep 11, 2024
409c563
Merge with develop
4l0n50 Sep 11, 2024
9afab40
Fix trie_data_length mismatch
4l0n50 Sep 12, 2024
4447e8f
Check correctness of inital next node ptr and check strict keys monot…
4l0n50 Sep 12, 2024
71e7021
Merge remote-tracking branch 'origin/develop' into refactor_ll_initia…
4l0n50 Sep 12, 2024
1137c9b
Address review comment
4l0n50 Sep 12, 2024
fc286a9
Apply suggestions from code review
4l0n50 Sep 13, 2024
7c3387f
Minor
4l0n50 Sep 13, 2024
9c329b6
Merge with develop
4l0n50 Sep 16, 2024
ffa1ace
Fix circuit sizes
Nashtare Sep 16, 2024
d3a2b60
Apply suggestions from code review
4l0n50 Sep 18, 2024
0751b65
Add missing stack comment
4l0n50 Sep 18, 2024
3319861
Merge remote-tracking branch 'origin/develop' into refactor_ll_initia…
4l0n50 Sep 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion evm_arithmetization/src/cpu/kernel/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ pub static KERNEL_FILES: [&str; NUMBER_KERNEL_FILES] = [
include_str!("asm/mpt/insert/insert_leaf.asm"),
include_str!("asm/mpt/insert/insert_trie_specific.asm"),
include_str!("asm/mpt/linked_list/linked_list.asm"),
include_str!("asm/mpt/linked_list/initial_tries.asm"),
include_str!("asm/mpt/linked_list/final_tries.asm"),
include_str!("asm/mpt/linked_list/initial_tries.asm"),
include_str!("asm/mpt/read.asm"),
include_str!("asm/mpt/storage/storage_read.asm"),
include_str!("asm/mpt/storage/storage_write.asm"),
Expand Down
8 changes: 5 additions & 3 deletions evm_arithmetization/src/cpu/kernel/asm/main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,16 @@ global check_state_trie:
// `GLOBAL_METADATA_TRIE_DATA_SIZE` is correct.
%get_trie_data_size
// stack: trie_data_len
PROVER_INPUT(trie_ptr::state)
PROVER_INPUT(trie_ptr::initial_state)

%mstore_global_metadata(@GLOBAL_METADATA_STATE_TRIE_ROOT)

PROVER_INPUT(trie_ptr::trie_data_size)
%mstore_global_metadata(@GLOBAL_METADATA_TRIE_DATA_SIZE)

%set_initial_tries
// stack: trie_data_len
%set_initial_state_trie
// stack: trie_data_len

PUSH @INITIAL_RLP_ADDR
// stack: rlp_start, trie_data_len
Expand All @@ -233,7 +235,7 @@ global check_state_trie:
%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)
%mload_global_metadata(@GLOBAL_METADATA_TRIE_DATA_SIZE)
%assert_eq

// We set a dummy value as an initial trie data length,
Expand Down
5 changes: 3 additions & 2 deletions evm_arithmetization/src/cpu/kernel/asm/mpt/hash/hash.asm
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ mpt_hash_hash_if_rlp:
mpt_hash_hash_rlp:
// stack: result, result_len, new_len, retdest
%stack (result, result_len, new_len)
-> (@SEGMENT_RLP_RAW, result, result_len, mpt_hash_hash_rlp_after_unpacking, result_len, new_len)
-> (@INITIAL_RLP_ADDR, result, result_len, mpt_hash_hash_rlp_after_unpacking, result_len, new_len)
// stack: addr, result, result_len, mpt_hash_hash_rlp_after_unpacking, result_len, new_len
%jump(mstore_unpacking)
mpt_hash_hash_rlp_after_unpacking:
// stack: result_addr, result_len, new_len, retdest
POP PUSH @SEGMENT_RLP_RAW // ctx == virt == 0
POP PUSH @INITIAL_RLP_ADDR // ctx == virt == 0
4l0n50 marked this conversation as resolved.
Show resolved Hide resolved
// stack: result_addr, result_len, new_len, retdest
KECCAK_GENERAL
// stack: hash, new_len, retdest
Expand Down Expand Up @@ -293,3 +293,4 @@ encode_node_leaf_after_encode_value:
%stack (rlp_prefix_start_pos, rlp_len, cur_len, retdest)
-> (retdest, rlp_prefix_start_pos, rlp_len, cur_len)
JUMP

Original file line number Diff line number Diff line change
Expand Up @@ -209,21 +209,17 @@ after_mpt_delete_slot:

global set_final_tries:
PUSH set_final_tries_after
PUSH @SEGMENT_STORAGE_LINKED_LIST
%add_const(@STORAGE_LINKED_LISTS_NODE_SIZE) // Skip the first node.
%first_initial_slot // Skip the first node.
%mload_global_metadata(@GLOBAL_METADATA_STATE_TRIE_ROOT)
PUSH @SEGMENT_ACCOUNTS_LINKED_LIST
%add_const(@ACCOUNTS_LINKED_LISTS_NODE_SIZE) // Skip the first node.
%first_initial_account // Skip the first node.
%jump(delete_removed_accounts)
set_final_tries_after:
// stack: new_state_root
PUSH set_final_tries_after_after SWAP1
// stack: new_state_root, set_final_tries_after_after
PUSH @SEGMENT_STORAGE_LINKED_LIST
%next_slot
%first_slot
SWAP1
PUSH @SEGMENT_ACCOUNTS_LINKED_LIST
%next_account
%first_account
%jump(insert_all_accounts)
set_final_tries_after_after:
//stack: new_state_root
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ loop_store_initial_accounts:
store_initial_accounts_end:
%pop2
// stack: cur_len, retdest
DUP1
%mstore_global_metadata(@GLOBAL_METADATA_INITIAL_ACCOUNTS_LINKED_LIST_LEN)
%mstore_global_metadata(@GLOBAL_METADATA_ACCOUNTS_LINKED_LIST_NEXT_AVAILABLE)
JUMP

Expand Down Expand Up @@ -360,6 +362,8 @@ loop_store_initial_slots:
store_initial_slots_end:
POP
// stack: cur_len, retdest
DUP1
%mstore_global_metadata(@GLOBAL_METADATA_INITIAL_STORAGE_LINKED_LIST_LEN)
%mstore_global_metadata(@GLOBAL_METADATA_STORAGE_LINKED_LIST_NEXT_AVAILABLE)
JUMP

Expand Down Expand Up @@ -894,22 +898,62 @@ remove_all_slots_end:
%next_account
%endmacro

%macro first_initial_account
// stack: empty
PUSH @SEGMENT_ACCOUNTS_LINKED_LIST
%next_initial_account
%endmacro

%macro next_account
// stack: node_ptr
%add_const(@ACCOUNTS_NEXT_NODE_PTR)
MLOAD_GENERAL
// stack: next_node_ptr
%endmacro

%macro next_initial_account
// stack: node_ptr
%add_const(@ACCOUNTS_LINKED_LISTS_NODE_SIZE)
// stack: next_node_ptr
%endmacro

%macro first_slot
// stack: empty
PUSH @SEGMENT_STORAGE_LINKED_LIST
%next_slot
%endmacro

%macro first_initial_slot
// stack: empty
PUSH @SEGMENT_STORAGE_LINKED_LIST
%next_initial_slot
%endmacro

%macro next_slot
// stack: node_ptr
%add_const(@STORAGE_NEXT_NODE_PTR)
MLOAD_GENERAL
// stack: next_node_ptr
%endmacro

%macro next_initial_slot
// stack: node_ptr
%add_const(@STORAGE_LINKED_LISTS_NODE_SIZE)
// stack: next_node_ptr
%endmacro

%macro next_hash_node
// stack: hash_node_ptr
%add_const(4)
// stack: next_hash_node_ptr
%endmacro

// Skip over the the first three words (number of nibbles and keys)
// and load the hash from memory.
%macro get_hash
// stack: hash_node_ptr
%add_const(3)
// stack: next_ptr
MLOAD_GENERAL
// stack: hash
%endmacro
6 changes: 3 additions & 3 deletions evm_arithmetization/src/cpu/kernel/asm/mpt/read.asm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ global mpt_read_state_trie:
// - the key, as a U256
// - return destination
//
// This function returns a pointer to the value, or 0 if the key is not found.
// This function returns a pointer to the value, or 0 if the key is not found. If the key
// is a leaf, it returns a pointer to a pointer.
global mpt_read:
// stack: node_ptr, num_nibbles, key, retdest
DUP1
Expand Down Expand Up @@ -145,7 +146,6 @@ global mpt_read_leaf_found:
// stack: node_payload_ptr, retdest
%add_const(2) // The value pointer is located after num_nibbles and the key.
// stack: value_ptr_ptr, retdest
%mload_trie_data
// stack: value_ptr, retdest
SWAP1
// For leafs we return the pointer
4l0n50 marked this conversation as resolved.
Show resolved Hide resolved
JUMP
59 changes: 20 additions & 39 deletions evm_arithmetization/src/cpu/kernel/tests/account_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,36 +190,30 @@ pub(crate) fn prepare_interpreter<F: RichField>(
interpreter.stack()
);

// Set initial tries.
// Now, set the payload.
interpreter
.push(0xDEADBEEFu32.into())
.expect("The stack should not overflow");
interpreter
.push(0.into()) // Initial nibbles
.expect("The stack should not overflow");
interpreter
.push(0.into()) // Initial number of nibbles
.expect("The stack should not overflow");
interpreter
.push((Segment::StorageLinkedList as usize + 8).into())
.expect("The stack should not overflow");
interpreter
.push((Segment::AccountsLinkedList as usize + 6).into())
.push((Segment::StorageLinkedList as usize + 5).into())
.expect("The stack should not overflow");
interpreter
.push(interpreter.get_global_metadata_field(GlobalMetadata::StateTrieRoot))
.unwrap();
interpreter
.push((Segment::AccountsLinkedList as usize + 4).into())
.expect("The stack should not overflow");

// Now, set the payload.
interpreter.generation_state.registers.program_counter =
KERNEL.global_labels["mpt_set_payload"];
KERNEL.global_labels["insert_all_initial_accounts"];

interpreter.run()?;

let acc_ptr = interpreter.pop().expect("The stack should not be empty") - 2;
let storage_ptr = interpreter.pop().expect("The stack should not be empty") - 3;
interpreter.set_global_metadata_field(GlobalMetadata::InitialAccountsLinkedListLen, acc_ptr);
interpreter.set_global_metadata_field(GlobalMetadata::InitialStorageLinkedListLen, storage_ptr);
assert_eq!(interpreter.stack_len(), 1);

let state_root = interpreter.pop().expect("The stack should not be empty");
interpreter.set_global_metadata_field(GlobalMetadata::StateTrieRoot, state_root);

// Now, execute `mpt_hash_state_trie`.
state_trie.insert(k, rlp::encode(account).to_vec())?;
Expand Down Expand Up @@ -410,43 +404,30 @@ fn prepare_interpreter_all_accounts<F: RichField>(
KERNEL.global_labels["store_initial_slots"];
interpreter.run()?;

// Set the pointers to the initial payloads.
// Now, set the payload.
interpreter
.push(0xDEADBEEFu32.into())
.expect("The stack should not overflow");
interpreter
.push(0.into()) // Initial nibbles
.expect("The stack should not overflow");
interpreter
.push(0.into()) // Initial number of nibbles
.expect("The stack should not overflow");
interpreter
.push((Segment::StorageLinkedList as usize + 8).into())
.expect("The stack should not overflow");
interpreter
.push((Segment::AccountsLinkedList as usize + 6).into())
.push((Segment::StorageLinkedList as usize + 5).into())
.expect("The stack should not overflow");
interpreter
.push(interpreter.get_global_metadata_field(GlobalMetadata::StateTrieRoot))
.unwrap();
interpreter
.push((Segment::AccountsLinkedList as usize + 4).into())
.expect("The stack should not overflow");

// Now, set the payloads in the state trie leaves.
// Now, set the payload.
interpreter.generation_state.registers.program_counter =
KERNEL.global_labels["mpt_set_payload"];
KERNEL.global_labels["insert_all_initial_accounts"];

interpreter.run()?;

assert_eq!(
interpreter.stack().len(),
2,
"Expected 2 items on stack after setting the initial trie payloads, found {:?}",
interpreter.stack()
);
assert_eq!(interpreter.stack_len(), 1);

let acc_ptr = interpreter.pop().expect("The stack should not be empty") - 2;
let storage_ptr = interpreter.pop().expect("The stack should not be empty") - 3;
interpreter.set_global_metadata_field(GlobalMetadata::InitialAccountsLinkedListLen, acc_ptr);
interpreter.set_global_metadata_field(GlobalMetadata::InitialStorageLinkedListLen, storage_ptr);
let state_root = interpreter.pop().expect("The stack should not be empty");
interpreter.set_global_metadata_field(GlobalMetadata::StateTrieRoot, state_root);

// Switch context and initialize memory with the data we need for the tests.
interpreter.generation_state.registers.program_counter = 0;
Expand Down
24 changes: 8 additions & 16 deletions evm_arithmetization/src/cpu/kernel/tests/mpt/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,33 +121,25 @@ fn test_state_trie(
.push(0xDEADBEEFu32.into())
.expect("The stack should not overflow");
interpreter
.push(0.into()) // Initial nibbles
.expect("The stack should not overflow");
interpreter
.push(0.into()) // Initial number of nibbles
.expect("The stack should not overflow");
interpreter
.push((Segment::StorageLinkedList as usize + 8).into())
.expect("The stack should not overflow");
interpreter
.push((Segment::AccountsLinkedList as usize + 6).into())
.push((Segment::StorageLinkedList as usize + 5).into())
.expect("The stack should not overflow");
interpreter
.push(interpreter.get_global_metadata_field(GlobalMetadata::StateTrieRoot))
.unwrap();
interpreter
.push((Segment::AccountsLinkedList as usize + 4).into())
.expect("The stack should not overflow");

// Now, set the payload.
interpreter.generation_state.registers.program_counter =
KERNEL.global_labels["mpt_set_payload"];
KERNEL.global_labels["insert_all_initial_accounts"];

interpreter.run()?;

assert_eq!(interpreter.stack_len(), 2);
assert_eq!(interpreter.stack_len(), 1);

let acc_ptr = interpreter.pop().expect("The stack should not be empty") - 2;
let storage_ptr = interpreter.pop().expect("The stack should not be empty") - 3;
interpreter.set_global_metadata_field(GlobalMetadata::InitialAccountsLinkedListLen, acc_ptr);
interpreter.set_global_metadata_field(GlobalMetadata::InitialStorageLinkedListLen, storage_ptr);
let state_root = interpreter.pop().expect("The stack should not be empty");
interpreter.set_global_metadata_field(GlobalMetadata::StateTrieRoot, state_root);

// Next, execute mpt_insert_state_trie.
interpreter.generation_state.registers.program_counter = mpt_insert_state_trie;
Expand Down
29 changes: 9 additions & 20 deletions evm_arithmetization/src/cpu/kernel/tests/mpt/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,36 +190,29 @@ fn test_state_trie(
interpreter.generation_state.registers.program_counter = KERNEL.global_labels["store_initial"];
interpreter.run()?;

// Set initial tries.
interpreter
.push(0xDEADBEEFu32.into())
.expect("The stack should not overflow");
interpreter
.push(0.into()) // Initial nibbles
.expect("The stack should not overflow");
interpreter
.push(0.into()) // Initial number of nibbles
.expect("The stack should not overflow");
interpreter
.push((Segment::StorageLinkedList as usize + 8).into())
.expect("The stack should not overflow");
interpreter
.push((Segment::AccountsLinkedList as usize + 6).into())
.push((Segment::StorageLinkedList as usize + 5).into())
.expect("The stack should not overflow");
interpreter
.push(interpreter.get_global_metadata_field(GlobalMetadata::StateTrieRoot))
.unwrap();
interpreter
.push((Segment::AccountsLinkedList as usize + 4).into())
.expect("The stack should not overflow");

// Now, set the payload.
interpreter.generation_state.registers.program_counter =
KERNEL.global_labels["mpt_set_payload"];
KERNEL.global_labels["insert_all_initial_accounts"];

interpreter.run()?;

let acc_ptr = interpreter.pop().expect("The stack should not be empty") - 2;
let storage_ptr = interpreter.pop().expect("The stack should not be empty") - 3;
interpreter.set_global_metadata_field(GlobalMetadata::InitialAccountsLinkedListLen, acc_ptr);
interpreter.set_global_metadata_field(GlobalMetadata::InitialStorageLinkedListLen, storage_ptr);
assert_eq!(interpreter.stack_len(), 1);

let state_root = interpreter.pop().expect("The stack should not be empty");
interpreter.set_global_metadata_field(GlobalMetadata::StateTrieRoot, state_root);

// Next, execute mpt_insert_state_trie.
interpreter.generation_state.registers.program_counter = mpt_insert_state_trie;
Expand Down Expand Up @@ -267,10 +260,6 @@ fn test_state_trie(
);

interpreter.generation_state.registers.program_counter = check_state_trie;
interpreter
.halt_offsets
.push(KERNEL.global_labels["check_txn_trie"]);

interpreter
.push(interpreter.get_global_metadata_field(GlobalMetadata::TrieDataSize)) // Initial trie data segment size, unused.
.expect("The stack should not overflow");
Expand Down
4 changes: 3 additions & 1 deletion evm_arithmetization/src/cpu/kernel/tests/mpt/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ fn mpt_read() -> Result<()> {
interpreter.run()?;

assert_eq!(interpreter.stack().len(), 1);
let result_ptr = interpreter.stack()[0].as_usize();
// mp_read returns a pointer to the accounts pointer
4l0n50 marked this conversation as resolved.
Show resolved Hide resolved
let result_ptr_ptr = interpreter.stack()[0].as_usize();
let result_ptr = interpreter.get_trie_data()[result_ptr_ptr..][..4][0].as_usize();
let result = &interpreter.get_trie_data()[result_ptr..][..4];
assert_eq!(result[0], test_account_1().nonce);
assert_eq!(result[1], test_account_1().balance);
Expand Down
Loading
Loading