forked from paradigmxyz/reth
-
Notifications
You must be signed in to change notification settings - Fork 6
Introduce Scroll Binary Patricia Merkle Trie Components #36
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
258e58e
feat: introduce scroll trie hash builder
frisitano cfd46fc
refactor: refactor HashBuilder implementation
frisitano 51fa6bb
feat: introduce StateCommitment in StateProviders
frisitano a3bd117
feat: introduce binary partricia trie state components
frisitano 52426db
refactor: introduce StateCommimentProvider
frisitano 3a9fa49
Merge remote-tracking branch 'upstream/main' into feat/provider-state…
frisitano 10af3b1
feat: introduce HashedPostStateProvider
frisitano 57544dd
Merge remote-tracking branch 'upstream/main' into feat/provider-state…
frisitano 5f6977e
Merge branch 'feat/provider-state-commitment' into feat/hashed-post-s…
frisitano 500f8c2
feat: HashedPostState from reverts
frisitano 8b24bc3
feat: introduce HashedStorageProvider
frisitano 0e79635
lint: revm/test-utils feature propogation
frisitano 9a66c9d
Merge branch 'feat/hashed-post-state-provider' into feat/hashed-stora…
frisitano 593587e
fix: add Send + Sync bound on introduced storage state api methods
frisitano 59a99c8
Merge branch 'feat/hashed-post-state-provider' into feat/hashed-stora…
frisitano dbeb344
feat: introduce KeyHasherProvider
frisitano 60b4f7a
feat: introduce StateRootProviderExt and integrate it (and StateRootP…
frisitano f275c6c
chore: address PR feedback and enhance test coverage
frisitano b638e50
Merge remote-tracking branch 'scroll/scroll' into feat/scroll-hashed-…
frisitano 8cfb70e
fix: add merge files
frisitano 534a907
fix lint
frisitano d2c9d32
fix lint
frisitano 963eb21
fmt
frisitano 430f7b9
Merge branch 'feat/scroll-hashed-post-state-provider' into feat/scrol…
frisitano 2662104
add KeyHasher generic to DatabaseHashedStorage::from_reverts trait
frisitano 3dbe4db
Merge branch 'feat/scroll-hashed-storage-provider' into feat/scroll-k…
frisitano 893de64
add merge files
frisitano 4970895
Merge branch 'feat/scroll-key-hasher-provider' into feat/scroll-state…
frisitano 810a4f8
add merge files
frisitano 94c9788
fix: propagate feature
frisitano 6278d2e
Merge remote-tracking branch 'frisitano/feat/scroll-state-root-provid…
frisitano 5f143f1
add merge files
frisitano dc7ae24
cleanup Cargo.toml files
frisitano b04a441
Merge remote-tracking branch 'origin/scroll' into feat/scroll-bmpt
frisitano fd062c5
fix: Cargo.toml dependencies
frisitano fb85220
refactor: refactor Cargo.toml and put tests behind scroll feature
frisitano 5bfafcc
lints and replace keccak with poseidon for HashedStorage instantiation
frisitano 85cf050
fix deny license and add scroll specific tests to ci
frisitano 85266db
fix unit github workflow
frisitano 7639f64
lint and deny
frisitano d99b1b7
fix Cargo.toml
frisitano 6f6b08f
add go build to allowed sources
frisitano bacb07f
Merge remote-tracking branch 'origin/scroll' into feat/scroll-bmpt
frisitano 0bd6fe7
update Cargo.lock
frisitano 65de4cd
update unit ci workflow to exclude --workspace by default
frisitano d7c4438
fix ci and address PR feedback
frisitano d84a329
replace TODO(frisitano) with TODO(scroll)
frisitano 2cd2da4
replace use of unwrap(..) in library code with expect(..)
frisitano c960cb7
add zktrie specification to crates/scroll/trie
frisitano f428081
chore: fix clsoing bracket in Cargo.toml
frisitano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| [package] | ||
| name = "scroll-primitives" | ||
| version.workspace = true | ||
| edition.workspace = true | ||
| rust-version.workspace = true | ||
| license.workspace = true | ||
| homepage.workspace = true | ||
| repository.workspace = true | ||
| exclude.workspace = true | ||
|
|
||
| [dependencies] | ||
| alloy-primitives.workspace = true | ||
| reth-trie.workspace = true | ||
| poseidon-bn254 = { git = "https://github.com/scroll-tech/poseidon-bn254", branch = "master" } | ||
|
|
||
| [lints] | ||
| workspace = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| //! Standalone crate for Scroll-specific Reth primitive types. | ||
|
|
||
| use alloy_primitives::{B256, U256}; | ||
| use reth_trie::TrieAccount; | ||
|
|
||
| /// Poseidon hashing primitives. | ||
| pub mod poseidon; | ||
|
|
||
| /// A Scroll account as represented in the trie. | ||
| #[derive(Debug)] | ||
| pub struct ScrollTrieAccount { | ||
| /// nonce | ||
| pub nonce: u64, | ||
| /// code size | ||
| pub code_size: u64, | ||
| /// balance | ||
| pub balance: U256, | ||
| /// storage root | ||
| pub storage_root: B256, | ||
| /// keccak code hash | ||
| pub code_hash: B256, | ||
| /// poseidon code hash | ||
| pub poseidon_code_hash: B256, | ||
| } | ||
|
|
||
| // TODO: Temporary method to convert from standard ethereum `TrieAccount` to `ScrollTrieAccount` | ||
frisitano marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // TODO: Fix cast | ||
| impl From<TrieAccount> for ScrollTrieAccount { | ||
| fn from(value: TrieAccount) -> Self { | ||
| ScrollTrieAccount { | ||
| // TODO(frisitano): introduce code size and poseidon code hash following integration | ||
| // with Account changes | ||
| poseidon_code_hash: Default::default(), | ||
| code_size: Default::default(), | ||
| nonce: value.nonce, | ||
| balance: value.balance, | ||
| // TODO(frisitano): introduce storage root | ||
| storage_root: Default::default(), | ||
| code_hash: value.code_hash, | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| pub use poseidon_bn254::{hash_with_domain, Fr, PrimeField}; | ||
|
|
||
| /// Type that is used to represent a field element in binary representation. | ||
| pub type FieldElementBytes = <Fr as PrimeField>::Repr; | ||
|
|
||
| /// The number of bytes in the binary representation of a field element. | ||
| pub const FIELD_ELEMENT_REPR_BYTES: usize = core::mem::size_of::<FieldElementBytes>(); | ||
|
|
||
| // Half the number of bytes in the binary representation of a field element. | ||
| const HALF_FIELD_ELEMENT_REPR_BYTES: usize = FIELD_ELEMENT_REPR_BYTES / 2; | ||
|
|
||
| /// The domain multiplier per field element. | ||
| pub const DOMAIN_MULTIPLIER_PER_FIELD_ELEMENT: u64 = 256; | ||
|
|
||
| /// The domain for hashing two field elements. | ||
| pub const DOMAIN_TWO_FIELD_ELEMENTS: Fr = | ||
| Fr::from_raw([DOMAIN_MULTIPLIER_PER_FIELD_ELEMENT * 2, 0, 0, 0]); | ||
|
|
||
| /// Hash two field elements using poseidon. | ||
| pub fn hash(element_1: Fr, element_2: Fr) -> Fr { | ||
| hash_with_domain(&[element_1, element_2], DOMAIN_TWO_FIELD_ELEMENTS) | ||
| } | ||
|
|
||
| /// Split and transform input be bytes into two field elements and hash using poseidon. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// This function will panic if more than 32 bytes are provided as input. | ||
| pub fn split_and_hash_be_bytes<T: AsRef<[u8]>>(bytes: T) -> Fr { | ||
| debug_assert!( | ||
frisitano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| bytes.as_ref().len() <= FIELD_ELEMENT_REPR_BYTES, | ||
| "bytes length should be less than or equal to field element bytes" | ||
| ); | ||
| let (bytes_lo, bytes_hi) = split_and_parse_field_elements(bytes.as_ref()); | ||
| hash(bytes_lo, bytes_hi) | ||
| } | ||
|
|
||
| /// Parse input bytes into two field elements which represent the lower bytes and the upper bytes. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// This function will panic if more than 32 bytes are provided as input. | ||
| fn split_and_parse_field_elements(bytes: &[u8]) -> (Fr, Fr) { | ||
| debug_assert!( | ||
| bytes.len() <= FIELD_ELEMENT_REPR_BYTES, | ||
| "bytes length should be less than or equal to field element bytes" | ||
| ); | ||
| let mut bytes_lo = FieldElementBytes::default(); | ||
| let mut bytes_hi = FieldElementBytes::default(); | ||
|
|
||
| if bytes.len() > (HALF_FIELD_ELEMENT_REPR_BYTES) { | ||
| bytes_lo[HALF_FIELD_ELEMENT_REPR_BYTES..] | ||
| .copy_from_slice(&bytes[..HALF_FIELD_ELEMENT_REPR_BYTES]); | ||
| bytes_hi[HALF_FIELD_ELEMENT_REPR_BYTES..bytes.len()] | ||
| .copy_from_slice(&bytes[HALF_FIELD_ELEMENT_REPR_BYTES..]); | ||
| } else { | ||
| bytes_lo[HALF_FIELD_ELEMENT_REPR_BYTES..(HALF_FIELD_ELEMENT_REPR_BYTES + bytes.len())] | ||
| .copy_from_slice(bytes) | ||
greged93 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| let bytes_lo = field_element_from_be_bytes(bytes_lo); | ||
| let bytes_hi = field_element_from_be_bytes(bytes_hi); | ||
| (bytes_lo, bytes_hi) | ||
| } | ||
|
|
||
| /// Parses a field element from big endian bytes. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// This function will panic if the bytes are not a valid field element. | ||
| pub fn field_element_from_be_bytes(mut bytes: FieldElementBytes) -> Fr { | ||
| bytes.reverse(); | ||
| Fr::from_repr_vartime(bytes).expect("valid field element") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| [package] | ||
| name = "scroll-state-commitment" | ||
| version.workspace = true | ||
| edition.workspace = true | ||
| rust-version.workspace = true | ||
| license.workspace = true | ||
| homepage.workspace = true | ||
| repository.workspace = true | ||
| exclude.workspace = true | ||
|
|
||
| [dependencies] | ||
| alloy-primitives.workspace = true | ||
| alloy-rlp.workspace = true | ||
| reth-execution-errors.workspace = true | ||
| reth-trie.workspace = true | ||
| reth-primitives.workspace = true | ||
| tracing.workspace = true | ||
| scroll-trie.workspace = true | ||
| scroll-primitives.workspace = true | ||
| poseidon-bn254 = { git = "https://github.com/scroll-tech/poseidon-bn254", branch = "master" } | ||
| reth-trie-db.workspace = true | ||
| reth-db = { workspace = true, features = ["test-utils"] } | ||
|
|
||
| # `metrics` feature | ||
| reth-metrics = { workspace = true, optional = true } | ||
| metrics = { workspace = true, optional = true } | ||
|
|
||
| [dev-dependencies] | ||
| alloy-consensus.workspace = true | ||
| reth-primitives = { workspace = true, features = ["test-utils", "arbitrary"] } | ||
| reth-trie = { workspace = true, features = ["test-utils"] } | ||
| reth-trie-common = { workspace = true, features = ["test-utils", "arbitrary"] } | ||
| reth-provider = { workspace = true, features = ["test-utils"] } | ||
| proptest.workspace = true | ||
| proptest-arbitrary-interop.workspace = true | ||
| zktrie_rust = { path = "/Users/f/dev/scroll/zktrie/rs_zktrie" } | ||
| zktrie = { path = "/Users/f/dev/scroll/zktrie", features = ["rs_zktrie"] } | ||
| tracing-subscriber = { version = "0.3", default-features = false, features = ["env-filter", "fmt", "json"] } | ||
|
|
||
| [lints] | ||
| workspace = true | ||
|
|
||
| [features] | ||
| metrics = ["reth-metrics", "dep:metrics"] | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| use alloy_primitives::B256; | ||
| use scroll_primitives::poseidon::{split_and_hash_be_bytes, PrimeField, FIELD_ELEMENT_REPR_BYTES}; | ||
|
|
||
| // TODO(frisitano): Implement `KeyHasher` trait from upstream. Also consider introducing a | ||
| // `HashingScheme` trait that combines both `KeyHasher` and `ValueHasher` traits via GATs. | ||
|
|
||
| /// An implementation of a key hasher that uses Poseidon. | ||
| #[derive(Debug)] | ||
| pub struct PoseidonKeyHasher; | ||
|
|
||
| impl PoseidonKeyHasher { | ||
frisitano marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// Hashes the key using the Poseidon hash function. | ||
| /// | ||
| /// The bytes are expected to be provided in big endian format. | ||
| /// | ||
| /// Panics if the number of bytes provided is greater than the number of bytes in the | ||
| /// binary representation of a field element (32). | ||
| /// | ||
| /// Returns the hash digest in little endian representation with bits reversed. | ||
| pub fn hash_key<T: AsRef<[u8]>>(bytes: T) -> B256 { | ||
| debug_assert!(bytes.as_ref().len() <= FIELD_ELEMENT_REPR_BYTES); | ||
| let mut bytes = split_and_hash_be_bytes(bytes.as_ref()).to_repr(); | ||
| bytes.iter_mut().for_each(|byte| *byte = byte.reverse_bits()); | ||
| bytes.into() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| //! The implementation of scrolls binary Merkle Patricia Trie used a cryptographic state commitment. | ||
|
|
||
| mod root; | ||
| pub use root::{StateRoot, StorageRoot}; | ||
|
|
||
| mod key; | ||
| mod value; | ||
|
|
||
| // RE-EXPORTS | ||
| pub use key::PoseidonKeyHasher; | ||
| pub use value::PosiedonValueHasher; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.