From cdd0824e59a229e275f013b2cc9f8898258e1a00 Mon Sep 17 00:00:00 2001 From: Marko Atanasievski Date: Thu, 15 Aug 2024 16:39:55 +0200 Subject: [PATCH] chore: add common crate for project wide definitions (#500) * chore: add common crate for project wide definitions * fix: labeling * fix: review * fix: nitpick --- .github/labeler.yml | 7 +++++- Cargo.lock | 12 ++++++++++ Cargo.toml | 2 ++ common/Cargo.toml | 17 +++++++++++++ common/src/lib.rs | 28 ++++++++++++++++++++++ mpt_trie/Cargo.toml | 1 + mpt_trie/src/builder.rs | 7 +----- trace_decoder/Cargo.toml | 1 + trace_decoder/src/processed_block_trace.rs | 13 +--------- 9 files changed, 69 insertions(+), 19 deletions(-) create mode 100644 common/Cargo.toml create mode 100644 common/src/lib.rs diff --git a/.github/labeler.yml b/.github/labeler.yml index 4805743d5..cc9748609 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -26,4 +26,9 @@ # Add 'specs' label to any changes within 'docs' folder. 'specs': - changed-files: - - any-glob-to-any-file: docs/** \ No newline at end of file + - any-glob-to-any-file: docs/** + +# Add 'crate: common' label to any changes within 'common' folder. +'crate: common': + - changed-files: + - any-glob-to-any-file: common/** diff --git a/Cargo.lock b/Cargo.lock index 78efddfd1..33b8be650 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3050,6 +3050,7 @@ dependencies = [ "serde_json", "thiserror", "uint", + "zk_evm_common", ] [[package]] @@ -5180,6 +5181,7 @@ dependencies = [ "thiserror", "u4", "winnow 0.6.13", + "zk_evm_common", ] [[package]] @@ -5883,6 +5885,16 @@ dependencies = [ "syn 2.0.70", ] +[[package]] +name = "zk_evm_common" +version = "0.1.0" +dependencies = [ + "bytes", + "ethereum-types", + "keccak-hash 0.10.0", + "rlp", +] + [[package]] name = "zk_evm_proc_macro" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 910ae4d11..8e1279fdb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [workspace] members = [ + "common", "compat", "evm_arithmetization", "mpt_trie", @@ -114,6 +115,7 @@ mpt_trie = { path = "mpt_trie", version = "0.4.1" } proof_gen = { path = "proof_gen", version = "0.4.0" } smt_trie = { path = "smt_trie", version = "0.1.1" } trace_decoder = { path = "trace_decoder", version = "0.6.0" } +zk_evm_common = { path = "common", version = "0.1.0" } zk_evm_proc_macro = { path = "proc_macro", version = "0.1.0" } # zero-bin related dependencies diff --git a/common/Cargo.toml b/common/Cargo.toml new file mode 100644 index 000000000..c11038439 --- /dev/null +++ b/common/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "zk_evm_common" +version = "0.1.0" +edition.workspace = true +license.workspace = true +repository.workspace = true +homepage.workspace = true +keywords.workspace = true +categories.workspace = true + +[dependencies] +ethereum-types = { workspace = true } +keccak-hash = { workspace = true } + +[dev-dependencies] +bytes = { workspace = true } +rlp = { workspace = true } diff --git a/common/src/lib.rs b/common/src/lib.rs new file mode 100644 index 000000000..9f33bfaf1 --- /dev/null +++ b/common/src/lib.rs @@ -0,0 +1,28 @@ +use ethereum_types::H256; + +/// The hash value of an account empty EVM code. +/// 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 +pub const EMPTY_CODE_HASH: H256 = H256([ + 197, 210, 70, 1, 134, 247, 35, 60, 146, 126, 125, 178, 220, 199, 3, 192, 229, 0, 182, 83, 202, + 130, 39, 59, 123, 250, 216, 4, 93, 133, 164, 112, +]); + +/// The hash of an empty Merkle Patricia trie. +/// 0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421 +pub const EMPTY_TRIE_HASH: H256 = H256([ + 86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, 110, 91, 72, 224, 27, 153, + 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33, +]); + +#[test] +fn test_empty_code_hash() { + assert_eq!(EMPTY_CODE_HASH, keccak_hash::keccak([])); +} + +#[test] +fn test_empty_trie_hash() { + assert_eq!( + EMPTY_TRIE_HASH, + keccak_hash::keccak(bytes::Bytes::from_static(&rlp::NULL_RLP)) + ); +} diff --git a/mpt_trie/Cargo.toml b/mpt_trie/Cargo.toml index 0d64fafa6..84bd01b4f 100644 --- a/mpt_trie/Cargo.toml +++ b/mpt_trie/Cargo.toml @@ -28,6 +28,7 @@ impl-rlp = { workspace = true } impl-codec = { workspace = true } impl-serde = { workspace = true } impl-num-traits = { workspace = true } +zk_evm_common = {workspace = true} [dev-dependencies] eth_trie = { workspace = true } diff --git a/mpt_trie/src/builder.rs b/mpt_trie/src/builder.rs index fc4ff1f75..43aebb724 100644 --- a/mpt_trie/src/builder.rs +++ b/mpt_trie/src/builder.rs @@ -6,18 +6,13 @@ use std::sync::Arc; use ethereum_types::H256; use keccak_hash::keccak; use rlp::{Prototype, Rlp}; +use zk_evm_common::EMPTY_TRIE_HASH; use super::{ nibbles::Nibbles, partial_trie::{Node, PartialTrie, WrappedNode}, }; -/// The hash of an empty trie. -const EMPTY_TRIE_HASH: H256 = H256([ - 0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0xf8, 0x6e, - 0x5b, 0x48, 0xe0, 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, 0xb4, 0x21, -]); - #[derive(Clone, Debug)] /// A builder for constructing a partial trie from a collection of nodes. pub struct PartialTrieBuilder { diff --git a/trace_decoder/Cargo.toml b/trace_decoder/Cargo.toml index bc1f58625..cac9547c0 100644 --- a/trace_decoder/Cargo.toml +++ b/trace_decoder/Cargo.toml @@ -37,6 +37,7 @@ strum = { version = "0.26.3", features = ["derive"] } thiserror = { workspace = true } u4 = { workspace = true } winnow = { workspace = true } +zk_evm_common = {workspace = true} [dev-dependencies] criterion = { workspace = true } diff --git a/trace_decoder/src/processed_block_trace.rs b/trace_decoder/src/processed_block_trace.rs index 3d99d64d0..6ab2434ba 100644 --- a/trace_decoder/src/processed_block_trace.rs +++ b/trace_decoder/src/processed_block_trace.rs @@ -4,24 +4,13 @@ use std::iter::once; use ethereum_types::{Address, H256, U256}; use evm_arithmetization::generation::mpt::{AccountRlp, LegacyReceiptRlp}; +use zk_evm_common::{EMPTY_CODE_HASH, EMPTY_TRIE_HASH}; use crate::hash; use crate::typed_mpt::TrieKey; use crate::PartialTriePreImages; use crate::{ContractCodeUsage, TxnInfo}; -// 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 -const EMPTY_CODE_HASH: H256 = H256([ - 197, 210, 70, 1, 134, 247, 35, 60, 146, 126, 125, 178, 220, 199, 3, 192, 229, 0, 182, 83, 202, - 130, 39, 59, 123, 250, 216, 4, 93, 133, 164, 112, -]); - -/// 0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421 -pub const EMPTY_TRIE_HASH: H256 = H256([ - 86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, 110, 91, 72, 224, 27, 153, - 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33, -]); - const FIRST_PRECOMPILE_ADDRESS: U256 = U256([1, 0, 0, 0]); const LAST_PRECOMPILE_ADDRESS: U256 = U256([10, 0, 0, 0]);