Skip to content
Merged
Show file tree
Hide file tree
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 Oct 28, 2024
cfd46fc
refactor: refactor HashBuilder implementation
frisitano Oct 29, 2024
51fa6bb
feat: introduce StateCommitment in StateProviders
frisitano Nov 16, 2024
a3bd117
feat: introduce binary partricia trie state components
frisitano Nov 19, 2024
52426db
refactor: introduce StateCommimentProvider
frisitano Nov 20, 2024
3a9fa49
Merge remote-tracking branch 'upstream/main' into feat/provider-state…
frisitano Nov 20, 2024
10af3b1
feat: introduce HashedPostStateProvider
frisitano Nov 25, 2024
57544dd
Merge remote-tracking branch 'upstream/main' into feat/provider-state…
frisitano Nov 25, 2024
5f6977e
Merge branch 'feat/provider-state-commitment' into feat/hashed-post-s…
frisitano Nov 25, 2024
500f8c2
feat: HashedPostState from reverts
frisitano Nov 25, 2024
8b24bc3
feat: introduce HashedStorageProvider
frisitano Nov 25, 2024
0e79635
lint: revm/test-utils feature propogation
frisitano Nov 25, 2024
9a66c9d
Merge branch 'feat/hashed-post-state-provider' into feat/hashed-stora…
frisitano Nov 25, 2024
593587e
fix: add Send + Sync bound on introduced storage state api methods
frisitano Nov 25, 2024
59a99c8
Merge branch 'feat/hashed-post-state-provider' into feat/hashed-stora…
frisitano Nov 25, 2024
dbeb344
feat: introduce KeyHasherProvider
frisitano Nov 25, 2024
60b4f7a
feat: introduce StateRootProviderExt and integrate it (and StateRootP…
frisitano Nov 26, 2024
f275c6c
chore: address PR feedback and enhance test coverage
frisitano Dec 1, 2024
b638e50
Merge remote-tracking branch 'scroll/scroll' into feat/scroll-hashed-…
frisitano Dec 2, 2024
8cfb70e
fix: add merge files
frisitano Dec 2, 2024
534a907
fix lint
frisitano Dec 2, 2024
d2c9d32
fix lint
frisitano Dec 2, 2024
963eb21
fmt
frisitano Dec 2, 2024
430f7b9
Merge branch 'feat/scroll-hashed-post-state-provider' into feat/scrol…
frisitano Dec 2, 2024
2662104
add KeyHasher generic to DatabaseHashedStorage::from_reverts trait
frisitano Dec 2, 2024
3dbe4db
Merge branch 'feat/scroll-hashed-storage-provider' into feat/scroll-k…
frisitano Dec 2, 2024
893de64
add merge files
frisitano Dec 2, 2024
4970895
Merge branch 'feat/scroll-key-hasher-provider' into feat/scroll-state…
frisitano Dec 2, 2024
810a4f8
add merge files
frisitano Dec 2, 2024
94c9788
fix: propagate feature
frisitano Dec 2, 2024
6278d2e
Merge remote-tracking branch 'frisitano/feat/scroll-state-root-provid…
frisitano Dec 3, 2024
5f143f1
add merge files
frisitano Dec 3, 2024
dc7ae24
cleanup Cargo.toml files
frisitano Dec 4, 2024
b04a441
Merge remote-tracking branch 'origin/scroll' into feat/scroll-bmpt
frisitano Dec 4, 2024
fd062c5
fix: Cargo.toml dependencies
frisitano Dec 4, 2024
fb85220
refactor: refactor Cargo.toml and put tests behind scroll feature
frisitano Dec 5, 2024
5bfafcc
lints and replace keccak with poseidon for HashedStorage instantiation
frisitano Dec 5, 2024
85cf050
fix deny license and add scroll specific tests to ci
frisitano Dec 5, 2024
85266db
fix unit github workflow
frisitano Dec 5, 2024
7639f64
lint and deny
frisitano Dec 5, 2024
d99b1b7
fix Cargo.toml
frisitano Dec 5, 2024
6f6b08f
add go build to allowed sources
frisitano Dec 5, 2024
bacb07f
Merge remote-tracking branch 'origin/scroll' into feat/scroll-bmpt
frisitano Dec 6, 2024
0bd6fe7
update Cargo.lock
frisitano Dec 6, 2024
65de4cd
update unit ci workflow to exclude --workspace by default
frisitano Dec 6, 2024
d7c4438
fix ci and address PR feedback
frisitano Dec 7, 2024
d84a329
replace TODO(frisitano) with TODO(scroll)
frisitano Dec 7, 2024
2cd2da4
replace use of unwrap(..) in library code with expect(..)
frisitano Dec 7, 2024
c960cb7
add zktrie specification to crates/scroll/trie
frisitano Dec 7, 2024
f428081
chore: fix clsoing bracket in Cargo.toml
frisitano Dec 8, 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
200 changes: 179 additions & 21 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ members = [
"crates/rpc/rpc-server-types/",
"crates/rpc/rpc-testing-util/",
"crates/rpc/rpc-types-compat/",
"crates/rpc/rpc/",
"crates/rpc/rpc/",
"crates/scroll/primitives",
"crates/scroll/state-commitment",
"crates/scroll/trie",
"crates/stages/api/",
"crates/stages/stages/",
"crates/stages/types/",
Expand Down Expand Up @@ -409,6 +412,11 @@ reth-trie-common = { path = "crates/trie/common" }
reth-trie-db = { path = "crates/trie/db" }
reth-trie-parallel = { path = "crates/trie/parallel" }

# scroll
scroll-state-commitment = { path = "crates/scroll/state-commitment" }
scroll-trie = { path = "crates/scroll/trie" }
scroll-primitives = { path = "crates/scroll/primitives" }

# revm
revm = { version = "14.0.3", features = [
"std",
Expand Down
17 changes: 17 additions & 0 deletions crates/scroll/primitives/Cargo.toml
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
42 changes: 42 additions & 0 deletions crates/scroll/primitives/src/lib.rs
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`
// 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,
}
}
}
74 changes: 74 additions & 0 deletions crates/scroll/primitives/src/poseidon.rs
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!(
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)
}

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")
}
46 changes: 46 additions & 0 deletions crates/scroll/state-commitment/Cargo.toml
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"]


26 changes: 26 additions & 0 deletions crates/scroll/state-commitment/src/key.rs
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 {
/// 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()
}
}
11 changes: 11 additions & 0 deletions crates/scroll/state-commitment/src/lib.rs
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;
Loading