Skip to content

Commit f4cf1c3

Browse files
committed
keccak mode
1 parent 3d7b60c commit f4cf1c3

File tree

11 files changed

+192
-78
lines changed

11 files changed

+192
-78
lines changed

Cargo.lock

+5-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ tiny-keccak = "2.0"
2121

2222
# dependencies from scroll-tech
2323
poseidon-bn254 = { git = "https://github.com/scroll-tech/poseidon-bn254", branch = "master", features = ["bn254"] }
24-
zktrie-ng = { git = "https://github.com/scroll-tech/zktrie-ng", branch = "master", features = ["scroll"] }
24+
zktrie-ng = { git = "https://github.com/scroll-tech/zktrie-ng", branch = "feat/keccak", features = ["scroll"] }
2525

2626
# binary dependencies
2727
anyhow = "1.0"
@@ -102,9 +102,9 @@ alloy-primitives = { git = "https://github.com/scroll-tech/alloy-core", branch =
102102
alloy-sol-types = {git = "https://github.com/scroll-tech/alloy-core", branch = "v0.8.10" }
103103

104104
# for local development
105-
# [patch."https://github.com/scroll-tech/revm"]
106-
# revm = { path = "../revm/crates/revm" }
107-
# revm-primitives = { path = "../revm/crates/primitives" }
105+
#[patch."https://github.com/scroll-tech/revm"]
106+
#revm = { path = "../revm/crates/revm" }
107+
#revm-primitives = { path = "../revm/crates/primitives" }
108108

109109
#[profile.release]
110110
#debug-assertions = true

crates/bin/src/commands/run_file.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use sbv::{
55
core::{BlockExecutionResult, ChunkInfo, EvmExecutorBuilder, HardforkConfig},
66
primitives::{
77
types::{BlockTrace, LegacyStorageTrace},
8-
zk_trie::db::kv::HashMapDb,
8+
zk_trie::{db::kv::HashMapDb, hash::poseidon::Poseidon},
99
Block, B256,
1010
},
1111
};
@@ -88,6 +88,7 @@ impl RunFileCommand {
8888
let mut executor = EvmExecutorBuilder::new(&mut code_db, &mut zktrie_db)
8989
.hardfork_config(fork_config)
9090
.chain_id(traces[0].chain_id())
91+
.hash_scheme(Poseidon)
9192
.build(traces[0].root_before())?;
9293
for trace in traces.iter() {
9394
executor.insert_codes(trace)?;

crates/bin/src/utils.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use sbv::primitives::zk_trie::db::NodeDb;
22
use sbv::{
33
core::{EvmExecutorBuilder, HardforkConfig, VerificationError},
4-
primitives::{zk_trie::db::kv::HashMapDb, Block},
4+
primitives::{zk_trie::db::kv::HashMapDb, zk_trie::hash::poseidon::Poseidon, Block},
55
};
66

77
pub fn verify<T: Block + Clone>(
@@ -53,6 +53,7 @@ fn verify_inner<T: Block + Clone>(
5353
let mut executor = EvmExecutorBuilder::new(&mut code_db, &mut zktrie_db)
5454
.hardfork_config(*fork_config)
5555
.chain_id(l2_trace.chain_id())
56+
.hash_scheme(Poseidon)
5657
.build(root_before)?;
5758

5859
executor.insert_codes(&l2_trace)?;

crates/core/src/database.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use sbv_primitives::{
1010
kv::{KVDatabase, KVDatabaseItem},
1111
NodeDb,
1212
},
13-
hash::{key_hasher::NoCacheHasher, poseidon::Poseidon, ZkHash},
13+
hash::{key_hasher::NoCacheHasher, HashScheme, ZkHash},
1414
scroll_types::Account,
1515
trie::ZkTrie,
1616
},
@@ -21,7 +21,7 @@ use std::{cell::RefCell, collections::HashMap, fmt};
2121
type Result<T, E = DatabaseError> = std::result::Result<T, E>;
2222

2323
/// A database that consists of account and storage information.
24-
pub struct EvmDatabase<'a, CodeDb, ZkDb> {
24+
pub struct EvmDatabase<'a, CodeDb, ZkDb, H> {
2525
/// Map of code hash to bytecode.
2626
pub(crate) code_db: &'a mut CodeDb,
2727
/// Cache of analyzed code
@@ -30,24 +30,26 @@ pub struct EvmDatabase<'a, CodeDb, ZkDb> {
3030
storage_root_caches: RefCell<HashMap<Address, ZkHash>>,
3131
/// Storage trie cache, avoid re-creating trie for the same account.
3232
/// Need to invalidate before `update`, otherwise the trie root may be outdated
33-
storage_trie_caches: RefCell<HashMap<ZkHash, Option<ZkTrie<Poseidon>>>>,
33+
storage_trie_caches: RefCell<HashMap<ZkHash, Option<ZkTrie<H>>>>,
3434
/// Current uncommitted zkTrie root based on the block trace.
3535
committed_zktrie_root: B256,
3636
/// The underlying zkTrie database.
3737
pub(crate) zktrie_db: &'a mut NodeDb<ZkDb>,
3838
/// Current view of zkTrie database.
39-
zktrie: ZkTrie<Poseidon>,
39+
zktrie: ZkTrie<H>,
4040
}
4141

42-
impl<CodeDb, Db> fmt::Debug for EvmDatabase<'_, CodeDb, Db> {
42+
impl<CodeDb, Db, HashScheme> fmt::Debug for EvmDatabase<'_, CodeDb, Db, HashScheme> {
4343
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4444
f.debug_struct("EvmDatabase")
4545
.field("committed_zktrie_root", &self.committed_zktrie_root)
4646
.finish()
4747
}
4848
}
4949

50-
impl<'a, CodeDb: KVDatabase, ZkDb: KVDatabase + 'static> EvmDatabase<'a, CodeDb, ZkDb> {
50+
impl<'a, CodeDb: KVDatabase, ZkDb: KVDatabase + 'static, H: HashScheme>
51+
EvmDatabase<'a, CodeDb, ZkDb, H>
52+
{
5153
/// Initialize an EVM database from a zkTrie root.
5254
pub fn new_from_root(
5355
committed_zktrie_root: B256,
@@ -79,7 +81,7 @@ impl<'a, CodeDb: KVDatabase, ZkDb: KVDatabase + 'static> EvmDatabase<'a, CodeDb,
7981
}
8082

8183
#[inline]
82-
pub(crate) fn update_storage_root_cache(&self, address: Address, storage_root: ZkTrie) {
84+
pub(crate) fn update_storage_root_cache(&self, address: Address, storage_root: ZkTrie<H>) {
8385
let new_root = *storage_root.root().unwrap_ref();
8486
let old = self
8587
.storage_root_caches
@@ -142,7 +144,9 @@ impl<'a, CodeDb: KVDatabase, ZkDb: KVDatabase + 'static> EvmDatabase<'a, CodeDb,
142144
}
143145
}
144146

145-
impl<CodeDb: KVDatabase, ZkDb: KVDatabase + 'static> DatabaseRef for EvmDatabase<'_, CodeDb, ZkDb> {
147+
impl<CodeDb: KVDatabase, ZkDb: KVDatabase + 'static, H: HashScheme> DatabaseRef
148+
for EvmDatabase<'_, CodeDb, ZkDb, H>
149+
{
146150
type Error = DatabaseError;
147151

148152
/// Get basic account information.

crates/core/src/executor/builder.rs

+41-13
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,125 @@
11
use crate::{error::DatabaseError, EvmDatabase, EvmExecutor, HardforkConfig};
22
use revm::db::CacheDB;
33
use sbv_primitives::{
4-
alloy_primitives::ChainId, zk_trie::db::kv::KVDatabase, zk_trie::db::NodeDb, B256,
4+
alloy_primitives::ChainId,
5+
zk_trie::{
6+
db::{kv::KVDatabase, NodeDb},
7+
hash::HashScheme,
8+
},
9+
B256,
510
};
611
use std::fmt::{self, Debug};
712

813
/// Builder for EVM executor.
9-
pub struct EvmExecutorBuilder<'a, H, C, CodeDb, ZkDb> {
10-
hardfork_config: H,
14+
pub struct EvmExecutorBuilder<'a, HC, C, CodeDb, ZkDb, H> {
15+
hardfork_config: HC,
1116
chain_id: C,
1217
code_db: &'a mut CodeDb,
1318
zktrie_db: &'a mut NodeDb<ZkDb>,
19+
hash_scheme: H,
1420
}
1521

16-
impl<H: Debug, C: Debug, CodeDb, ZkDb> Debug for EvmExecutorBuilder<'_, H, C, CodeDb, ZkDb> {
22+
impl<HC: Debug, C: Debug, CodeDb, ZkDb, H: Debug> Debug
23+
for EvmExecutorBuilder<'_, HC, C, CodeDb, ZkDb, H>
24+
{
1725
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1826
f.debug_struct("EvmExecutorBuilder")
1927
.field("hardfork_config", &self.hardfork_config)
2028
.field("chain_id", &self.chain_id)
2129
.field("code_db", &"...")
2230
.field("zktrie_db", &"...")
31+
.field("hash_scheme", &self.hash_scheme)
2332
.finish()
2433
}
2534
}
2635

27-
impl<'a, CodeDb, ZkDb> EvmExecutorBuilder<'a, (), (), CodeDb, ZkDb> {
36+
impl<'a, CodeDb, ZkDb> EvmExecutorBuilder<'a, (), (), CodeDb, ZkDb, ()> {
2837
/// Create a new builder.
2938
pub fn new(code_db: &'a mut CodeDb, zktrie_db: &'a mut NodeDb<ZkDb>) -> Self {
3039
Self {
3140
hardfork_config: (),
3241
chain_id: (),
3342
code_db,
3443
zktrie_db,
44+
hash_scheme: (),
3545
}
3646
}
3747
}
3848

39-
impl<'a, H, C, CodeDb, ZkDb> EvmExecutorBuilder<'a, H, C, CodeDb, ZkDb> {
49+
impl<'a, HC, C, CodeDb, ZkDb, H> EvmExecutorBuilder<'a, HC, C, CodeDb, ZkDb, H> {
4050
/// Set hardfork config.
4151
pub fn hardfork_config<H1>(
4252
self,
4353
hardfork_config: H1,
44-
) -> EvmExecutorBuilder<'a, H1, C, CodeDb, ZkDb> {
54+
) -> EvmExecutorBuilder<'a, H1, C, CodeDb, ZkDb, H> {
4555
EvmExecutorBuilder {
4656
hardfork_config,
4757
chain_id: self.chain_id,
4858
code_db: self.code_db,
4959
zktrie_db: self.zktrie_db,
60+
hash_scheme: self.hash_scheme,
5061
}
5162
}
5263

5364
/// Set chain id.
54-
pub fn chain_id<C1>(self, chain_id: C1) -> EvmExecutorBuilder<'a, H, C1, CodeDb, ZkDb> {
65+
pub fn chain_id<C1>(self, chain_id: C1) -> EvmExecutorBuilder<'a, HC, C1, CodeDb, ZkDb, H> {
5566
EvmExecutorBuilder {
5667
hardfork_config: self.hardfork_config,
5768
chain_id,
5869
code_db: self.code_db,
5970
zktrie_db: self.zktrie_db,
71+
hash_scheme: self.hash_scheme,
6072
}
6173
}
6274

6375
/// Set code db.
6476
pub fn code_db<CodeDb1>(
6577
self,
6678
code_db: &'a mut CodeDb1,
67-
) -> EvmExecutorBuilder<'a, H, C, CodeDb1, ZkDb> {
79+
) -> EvmExecutorBuilder<'a, HC, C, CodeDb1, ZkDb, H> {
6880
EvmExecutorBuilder {
6981
hardfork_config: self.hardfork_config,
7082
chain_id: self.chain_id,
7183
code_db,
7284
zktrie_db: self.zktrie_db,
85+
hash_scheme: self.hash_scheme,
7386
}
7487
}
7588

7689
/// Set zktrie db.
7790
pub fn zktrie_db<ZkDb1>(
7891
self,
7992
zktrie_db: &'a mut NodeDb<ZkDb1>,
80-
) -> EvmExecutorBuilder<H, C, CodeDb, ZkDb1> {
93+
) -> EvmExecutorBuilder<HC, C, CodeDb, ZkDb1, H> {
8194
EvmExecutorBuilder {
8295
hardfork_config: self.hardfork_config,
8396
chain_id: self.chain_id,
8497
code_db: self.code_db,
8598
zktrie_db,
99+
hash_scheme: self.hash_scheme,
100+
}
101+
}
102+
103+
/// Set hash scheme.
104+
pub fn hash_scheme<H1>(
105+
self,
106+
hash_scheme: H1,
107+
) -> EvmExecutorBuilder<'a, HC, C, CodeDb, ZkDb, H1> {
108+
EvmExecutorBuilder {
109+
hardfork_config: self.hardfork_config,
110+
chain_id: self.chain_id,
111+
code_db: self.code_db,
112+
zktrie_db: self.zktrie_db,
113+
hash_scheme,
86114
}
87115
}
88116
}
89117

90-
impl<'a, CodeDb: KVDatabase, ZkDb: KVDatabase + 'static>
91-
EvmExecutorBuilder<'a, HardforkConfig, ChainId, CodeDb, ZkDb>
118+
impl<'a, CodeDb: KVDatabase, ZkDb: KVDatabase + 'static, H: HashScheme>
119+
EvmExecutorBuilder<'a, HardforkConfig, ChainId, CodeDb, ZkDb, H>
92120
{
93121
/// Initialize an EVM executor from a block trace as the initial state.
94-
pub fn build(self, root: B256) -> Result<EvmExecutor<'a, CodeDb, ZkDb>, DatabaseError> {
122+
pub fn build(self, root: B256) -> Result<EvmExecutor<'a, CodeDb, ZkDb, H>, DatabaseError> {
95123
let db = cycle_track!(
96124
CacheDB::new(EvmDatabase::new_from_root(
97125
root,

crates/core/src/executor/mod.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use sbv_primitives::{
1212
alloy_primitives::Bytes,
1313
zk_trie::{
1414
db::kv::KVDatabase,
15-
hash::{key_hasher::NoCacheHasher, poseidon::Poseidon},
15+
hash::{key_hasher::NoCacheHasher, HashScheme},
1616
scroll_types::Account,
1717
trie::ZkTrie,
1818
},
@@ -24,10 +24,10 @@ mod builder;
2424
pub use builder::EvmExecutorBuilder;
2525

2626
/// EVM executor that handles the block.
27-
pub struct EvmExecutor<'db, CodeDb, ZkDb> {
27+
pub struct EvmExecutor<'db, CodeDb, ZkDb, H> {
2828
chain_id: ChainId,
2929
hardfork_config: HardforkConfig,
30-
db: CacheDB<EvmDatabase<'db, CodeDb, ZkDb>>,
30+
db: CacheDB<EvmDatabase<'db, CodeDb, ZkDb, H>>,
3131
}
3232

3333
/// Block execution result
@@ -39,9 +39,11 @@ pub struct BlockExecutionResult {
3939
pub tx_rlps: Vec<Bytes>,
4040
}
4141

42-
impl<CodeDb: KVDatabase, ZkDb: KVDatabase + 'static> EvmExecutor<'_, CodeDb, ZkDb> {
42+
impl<CodeDb: KVDatabase, ZkDb: KVDatabase + 'static, H: HashScheme>
43+
EvmExecutor<'_, CodeDb, ZkDb, H>
44+
{
4345
/// Get reference to the DB
44-
pub fn db(&self) -> &CacheDB<EvmDatabase<CodeDb, ZkDb>> {
46+
pub fn db(&self) -> &CacheDB<EvmDatabase<CodeDb, ZkDb, H>> {
4547
&self.db
4648
}
4749

@@ -190,7 +192,7 @@ impl<CodeDb: KVDatabase, ZkDb: KVDatabase + 'static> EvmExecutor<'_, CodeDb, ZkD
190192
}
191193

192194
fn commit_changes_inner(&mut self) -> Result<B256, DatabaseError> {
193-
let mut zktrie = ZkTrie::<Poseidon>::new_with_root(
195+
let mut zktrie = ZkTrie::<H>::new_with_root(
194196
self.db.db.zktrie_db,
195197
NoCacheHasher,
196198
self.db.db.committed_zktrie_root(),
@@ -245,7 +247,7 @@ impl<CodeDb: KVDatabase, ZkDb: KVDatabase + 'static> EvmExecutor<'_, CodeDb, ZkD
245247
// get storage tire
246248
cycle_tracker_start!("update storage_tire");
247249
let mut storage_trie = cycle_track!(
248-
ZkTrie::<Poseidon>::new_with_root(
250+
ZkTrie::<H>::new_with_root(
249251
self.db.db.zktrie_db,
250252
NoCacheHasher,
251253
storage_root_before,
@@ -357,7 +359,7 @@ impl<CodeDb: KVDatabase, ZkDb: KVDatabase + 'static> EvmExecutor<'_, CodeDb, ZkD
357359
}
358360
}
359361

360-
impl<CodeDb, ZkDb> Debug for EvmExecutor<'_, CodeDb, ZkDb> {
362+
impl<CodeDb, ZkDb, H> Debug for EvmExecutor<'_, CodeDb, ZkDb, H> {
361363
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
362364
f.debug_struct("EvmExecutor").field("db", &self.db).finish()
363365
}

crates/stateful/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ thiserror.workspace = true
2121
tokio = { workspace = true, features = ["fs", "io-util", "macros", "rt-multi-thread", "signal", "sync", "time"] }
2222
tokio-retry.workspace = true
2323
url.workspace = true
24+
zktrie-ng = { workspace = true, features = ["clap", "serde"] }
2425

25-
sbv = { workspace = true, features = ["sled"]}
26+
sbv = { workspace = true, features = ["sled"] }
2627

2728
tracing-subscriber = { workspace = true, optional = true }
2829

0 commit comments

Comments
 (0)