Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Start of SMT implementation for Type 2 zkEVM (0xPolygonZero#1315)
Browse files Browse the repository at this point in the history
* Add SMT types

* Progress

* Progress

* Working smt hashing

* Minor

* Fix hash

* Working insert

* Minor

* Add insert test for storage trie

* Add missing constraints for DUP/SWAP (0xPolygonZero#1310)

* Refactor wcopy syscalls

* Refactor memcpy

* Working test_simple_transfer

* Modify add11_yml.rs

* Refactor codecopy

* Fix

* Fix calldatacopy

* Fix test on interpreter side

* Remove new_stack_top_channel from StackBehavior (0xPolygonZero#1296)

* Working add11_yml.rs

* All tests compile

* Working test_balance

* Minor

* Working test_extcodesize

* All non-ignored tests pass

* Fix test_empty_txn_list

* Clippy

* smt_utils point to github

* Comments

* Fix kexit_info in test

* Review

* Update Cargo.toml

* Move empty check inside final iteration

* Remerge context flags (0xPolygonZero#1292)

* Remerge context flags

* Apply comments and revert some unwanted changes

* Merge NOT and POP flags. (0xPolygonZero#1257)

* Merge NOT and POP flags

* Add comments

* Disable remaining memory channels for POP

* Apply comments

* Fix stack

* More of memcpy_bytes

* Add some documentation in EVM crate (0xPolygonZero#1295)

Co-authored-by: Linda Guiga <[email protected]>

* Combine PUSH0 and PC flags. (0xPolygonZero#1256)

* PR feedback

* Add context constraints (0xPolygonZero#1260)

* Combine JUMPDEST and KECCAK_GENERAL flags. (0xPolygonZero#1259)

* Combine JUMPDEST and KECCAK_GENERAL flags.

* Apply comments

* Fix merging of jumpdest and keccak_general.

* Add test for selfdestruct (0xPolygonZero#1321)

* Add test for selfdestruct

* Comment

* Fix test

---------

Co-authored-by: Hamy Ratoanina <[email protected]>
Co-authored-by: Robin Salen <[email protected]>
Co-authored-by: Linda Guiga <[email protected]>
Co-authored-by: Robin Salen <[email protected]>
Co-authored-by: Linda Guiga <[email protected]>
Co-authored-by: Linda Guiga <[email protected]>
  • Loading branch information
7 people authored and pgebheim committed Nov 3, 2023
1 parent f71f227 commit 7e4de49
Show file tree
Hide file tree
Showing 46 changed files with 1,165 additions and 1,632 deletions.
1 change: 1 addition & 0 deletions evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ static_assertions = "1.1.0"
hashbrown = { version = "0.14.0" }
tiny-keccak = "2.0.2"
serde_json = "1.0"
smt_utils = { git = "https://github.com/0xPolygonZero/smt_utils" }

[target.'cfg(not(target_env = "msvc"))'.dependencies]
jemallocator = "0.5.0"
Expand Down
7 changes: 6 additions & 1 deletion evm/src/cpu/kernel/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ pub(crate) fn combined_kernel() -> Kernel {
include_str!("asm/memory/packing.asm"),
include_str!("asm/memory/syscalls.asm"),
include_str!("asm/memory/txn_fields.asm"),
include_str!("asm/mpt/accounts.asm"),
include_str!("asm/smt/load.asm"),
include_str!("asm/smt/hash.asm"),
include_str!("asm/smt/insert.asm"),
include_str!("asm/smt/read.asm"),
include_str!("asm/smt/utils.asm"),
include_str!("asm/smt/accounts.asm"),
include_str!("asm/mpt/delete/delete.asm"),
include_str!("asm/mpt/delete/delete_branch.asm"),
include_str!("asm/mpt/delete/delete_extension.asm"),
Expand Down
2 changes: 1 addition & 1 deletion evm/src/cpu/kernel/asm/account_code.asm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extcodehash_dead:

global extcodehash:
// stack: address, retdest
%mpt_read_state_trie
%smt_read_state
// stack: account_ptr, retdest
DUP1 ISZERO %jumpi(retzero)
%add_const(3)
Expand Down
4 changes: 2 additions & 2 deletions evm/src/cpu/kernel/asm/balance.asm
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ global sys_balance:

global balance:
// stack: address, retdest
%mpt_read_state_trie
%smt_read_state
// stack: account_ptr, retdest
DUP1 ISZERO %jumpi(retzero) // If the account pointer is null, return 0.
// No need to consider the case where `account_ptr=0` because `trie_data[1]=0`.
%add_const(1)
// stack: balance_ptr, retdest
%mload_trie_data
Expand Down
4 changes: 2 additions & 2 deletions evm/src/cpu/kernel/asm/core/nonce.asm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Post stack: (empty)
global nonce:
// stack: address, retdest
%mpt_read_state_trie
%smt_read_state
// stack: account_ptr, retdest
// The nonce is the first account field, so we deref the account pointer itself.
// Note: We don't need to handle account_ptr=0, as trie_data[0] = 0,
Expand All @@ -23,7 +23,7 @@ global nonce:
global increment_nonce:
// stack: address, retdest
DUP1
%mpt_read_state_trie
%smt_read_state
// stack: account_ptr, address, retdest
DUP1 ISZERO %jumpi(increment_nonce_no_such_account)
// stack: nonce_ptr, address, retdest
Expand Down
7 changes: 4 additions & 3 deletions evm/src/cpu/kernel/asm/core/transfer.asm
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ global transfer_eth_failure:
global deduct_eth:
// stack: addr, amount, retdest
DUP1 %insert_touched_addresses
%mpt_read_state_trie
%smt_read_state
// stack: account_ptr, amount, retdest
DUP1 ISZERO %jumpi(deduct_eth_no_such_account) // If the account pointer is null, return 1.
%add_const(1)
Expand Down Expand Up @@ -65,7 +65,7 @@ global deduct_eth_insufficient_balance:
global add_eth:
// stack: addr, amount, retdest
DUP1 %insert_touched_addresses
DUP1 %mpt_read_state_trie
DUP1 %smt_read_state
// stack: account_ptr, addr, amount, retdest
DUP1 ISZERO %jumpi(add_eth_new_account) // If the account pointer is null, we need to create the account.
%add_const(1)
Expand All @@ -90,6 +90,7 @@ global add_eth_new_account:
// stack: new_account_ptr, addr, amount, retdest
SWAP2
// stack: amount, addr, new_account_ptr, retdest
PUSH 0 %append_to_trie_data // key
PUSH 0 %append_to_trie_data // nonce
%append_to_trie_data // balance
// stack: addr, new_account_ptr, retdest
Expand All @@ -98,7 +99,7 @@ global add_eth_new_account:
// stack: addr, new_account_ptr, retdest
%addr_to_state_key
// stack: key, new_account_ptr, retdest
%jump(mpt_insert_state_trie)
%jump(smt_insert_state)

add_eth_new_account_zero:
// stack: addr, amount, retdest
Expand Down
2 changes: 1 addition & 1 deletion evm/src/cpu/kernel/asm/core/util.asm
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
// Returns 1 if the account is empty, 0 otherwise.
%macro is_empty
// stack: addr
%mpt_read_state_trie
%smt_read_state
// stack: account_ptr
DUP1 ISZERO %jumpi(%%false)
// stack: account_ptr
Expand Down
4 changes: 2 additions & 2 deletions evm/src/cpu/kernel/asm/main.asm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ global main:
%jump(load_all_mpts)

global hash_initial_tries:
%mpt_hash_state_trie %mload_global_metadata(@GLOBAL_METADATA_STATE_TRIE_DIGEST_BEFORE) %assert_eq
%smt_hash_state %mload_global_metadata(@GLOBAL_METADATA_STATE_TRIE_DIGEST_BEFORE) %assert_eq
%mpt_hash_txn_trie %mload_global_metadata(@GLOBAL_METADATA_TXN_TRIE_DIGEST_BEFORE) %assert_eq
%mpt_hash_receipt_trie %mload_global_metadata(@GLOBAL_METADATA_RECEIPT_TRIE_DIGEST_BEFORE) %assert_eq

Expand Down Expand Up @@ -55,7 +55,7 @@ global hash_final_tries:
DUP3 %mload_global_metadata(@GLOBAL_METADATA_TXN_NUMBER_AFTER) %assert_eq
%pop3
%check_metadata_block_bloom
%mpt_hash_state_trie %mload_global_metadata(@GLOBAL_METADATA_STATE_TRIE_DIGEST_AFTER) %assert_eq
%smt_hash_state %mload_global_metadata(@GLOBAL_METADATA_STATE_TRIE_DIGEST_AFTER) %assert_eq
%mpt_hash_txn_trie %mload_global_metadata(@GLOBAL_METADATA_TXN_TRIE_DIGEST_AFTER) %assert_eq
%mpt_hash_receipt_trie %mload_global_metadata(@GLOBAL_METADATA_RECEIPT_TRIE_DIGEST_AFTER) %assert_eq
%jump(halt)
Expand Down
6 changes: 1 addition & 5 deletions evm/src/cpu/kernel/asm/mpt/load/load.asm
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
// Load all partial trie data from prover inputs.
global load_all_mpts:
// stack: retdest
// First set @GLOBAL_METADATA_TRIE_DATA_SIZE = 1.
// We don't want it to start at 0, as we use 0 as a null pointer.
PUSH 1
%set_trie_data_size

%load_mpt(mpt_load_state_trie_value) %mstore_global_metadata(@GLOBAL_METADATA_STATE_TRIE_ROOT)
%load_state_smt
%load_mpt(mpt_load_txn_trie_value) %mstore_global_metadata(@GLOBAL_METADATA_TXN_TRIE_ROOT)
%load_mpt(mpt_load_receipt_trie_value) %mstore_global_metadata(@GLOBAL_METADATA_RECEIPT_TRIE_ROOT)

Expand Down
7 changes: 3 additions & 4 deletions evm/src/cpu/kernel/asm/mpt/storage/storage_read.asm
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ global sload_current:
%stack (slot) -> (slot, after_storage_read)
%slot_to_storage_key
// stack: storage_key, after_storage_read
PUSH 64 // storage_key has 64 nibbles
%current_storage_trie
// stack: storage_root_ptr, 64, storage_key, after_storage_read
%jump(mpt_read)
%current_storage_smt
// stack: storage_root_ptr, storage_key, after_storage_read
%jump(smt_read)

global after_storage_read:
// stack: value_ptr, retdest
Expand Down
22 changes: 12 additions & 10 deletions evm/src/cpu/kernel/asm/mpt/storage/storage_write.asm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Write a word to the current account's storage trie.
// Write a word to the current account's storage SMT.
//
// Pre stack: kexit_info, slot, value
// Post stack: (empty)
Expand Down Expand Up @@ -92,26 +92,27 @@ sstore_after_refund:
DUP2 %address %journal_add_storage_change
// stack: slot, value, kexit_info

// If the value is zero, delete the slot from the storage trie.
// If the value is zero, delete the slot from the storage SMT.
// stack: slot, value, kexit_info
DUP2 ISZERO %jumpi(sstore_delete)

// First we write the value to MPT data, and get a pointer to it.
// First we write the value to SMT data, and get a pointer to it.
%get_trie_data_size
// stack: value_ptr, slot, value, kexit_info
PUSH 0 %append_to_trie_data // For the key.
// stack: value_ptr, slot, value, kexit_info
SWAP2
// stack: value, slot, value_ptr, kexit_info
%append_to_trie_data
// stack: slot, value_ptr, kexit_info

// Next, call mpt_insert on the current account's storage root.
// Next, call smt_insert on the current account's storage root.
%stack (slot, value_ptr) -> (slot, value_ptr, after_storage_insert)
%slot_to_storage_key
// stack: storage_key, value_ptr, after_storage_insert, kexit_info
PUSH 64 // storage_key has 64 nibbles
%current_storage_trie
// stack: storage_root_ptr, 64, storage_key, value_ptr, after_storage_insert, kexit_info
%jump(mpt_insert)
%current_storage_smt
// stack: storage_root_ptr, storage_key, value_ptr, after_storage_insert, kexit_info
%jump(smt_insert)

after_storage_insert:
// stack: new_storage_root_ptr, kexit_info
Expand All @@ -130,15 +131,16 @@ sstore_noop:
%pop3
EXIT_KERNEL

// Delete the slot from the storage trie.
// Delete the slot from the storage SMT.
sstore_delete:
PANIC // TODO: Not implemented for SMT.
// stack: slot, value, kexit_info
SWAP1 POP
PUSH after_storage_insert SWAP1
// stack: slot, after_storage_insert, kexit_info
%slot_to_storage_key
// stack: storage_key, after_storage_insert, kexit_info
PUSH 64 // storage_key has 64 nibbles
%current_storage_trie
%current_storage_smt
// stack: storage_root_ptr, 64, storage_key, after_storage_insert, kexit_info
%jump(mpt_delete)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Return a pointer to the current account's data in the state trie.
%macro current_account_data
%address %mpt_read_state_trie
%address %smt_read_state
// stack: account_ptr
// account_ptr should be non-null as long as the prover provided the proper
// Merkle data. But a bad prover may not have, and we don't want return a
Expand All @@ -10,7 +10,7 @@
%endmacro

// Returns a pointer to the root of the storage trie associated with the current account.
%macro current_storage_trie
%macro current_storage_smt
// stack: (empty)
%current_account_data
// stack: account_ptr
Expand Down
Loading

0 comments on commit 7e4de49

Please sign in to comment.