Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit e9261c6

Browse files
authored
add withdraw_proof to MptUpdates (#678)
* withdraw proof * Update witness.rs * Update mpt.rs
1 parent 9cbbd35 commit e9261c6

File tree

5 files changed

+65
-17
lines changed

5 files changed

+65
-17
lines changed

Cargo.lock

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

zkevm-circuits/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ mpt-zktrie = { path = "../zktrie" }
2828
keccak256 = { path = "../keccak256"}
2929
log = "0.4"
3030
env_logger = "0.9"
31+
serde = { version = "1.0", features = ["derive"] }
3132
serde_json = "1.0.78"
3233

3334
hash-circuit = { package = "poseidon-circuit", git = "https://github.com/scroll-tech/poseidon-circuit.git", branch = "scroll-dev-0619", features=['short']}
@@ -58,8 +59,6 @@ itertools = "0.10.1"
5859
mock = { path = "../mock" }
5960
pretty_assertions = "1.0.0"
6061
cli-table = "0.4"
61-
serde = { version = "1.0.130", features = ["derive"] }
62-
serde_json = "1.0.78"
6362
paste = "1.0"
6463

6564
[features]

zkevm-circuits/src/witness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mod call;
1212
pub use call::Call;
1313

1414
mod mpt;
15-
pub use mpt::{MptUpdate, MptUpdateRow, MptUpdates};
15+
pub use mpt::{MptUpdate, MptUpdateRow, MptUpdates, WithdrawProof};
1616

1717
mod receipt;
1818
pub use receipt::Receipt;

zkevm-circuits/src/witness/mpt.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,22 @@ use mpt_zktrie::{
1010
state,
1111
state::witness::WitnessGenerator,
1212
};
13+
use serde::{Deserialize, Serialize};
1314
use std::collections::BTreeMap;
1415

1516
pub use state::ZktrieState;
1617

18+
/// Used to store withdraw proof
19+
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
20+
pub struct WithdrawProof {
21+
/// state root after a block
22+
pub state_root: U256,
23+
/// account proof for withdraw bridge contract
24+
pub account_proof: Vec<Vec<u8>>,
25+
/// storage proof for withdraw bridge contract, withdraw root storage key
26+
pub storage_proof: Vec<Vec<u8>>,
27+
}
28+
1729
/// An MPT update whose validity is proved by the MptCircuit
1830
#[derive(Debug, Clone)]
1931
pub struct MptUpdate {
@@ -56,6 +68,9 @@ pub struct MptUpdates {
5668
old_root: Word,
5769
new_root: Word,
5870
updates: BTreeMap<Key, MptUpdate>,
71+
/// TODO: is here the best place for this?
72+
/// Withdraw proof after this block
73+
pub withdraw_proof: WithdrawProof,
5974
pub(crate) smt_traces: Vec<SMTTrace>,
6075
pub(crate) proof_types: Vec<MPTProofType>,
6176
}
@@ -135,6 +150,20 @@ impl MptUpdates {
135150
);
136151
wit_gen.dump();
137152
}
153+
154+
// generate withdraw proof
155+
let address = *bus_mapping::l2_predeployed::message_queue::ADDRESS;
156+
let key = *bus_mapping::l2_predeployed::message_queue::WITHDRAW_TRIE_ROOT_SLOT;
157+
let account_proof = wit_gen.account_proof(address);
158+
let storage_proof = wit_gen.storage_proof(address, key);
159+
// TODO: add withdraw_root to WithdrawProof?
160+
let withdraw_proof = WithdrawProof {
161+
state_root: self.new_root,
162+
account_proof,
163+
storage_proof,
164+
};
165+
log::debug!("withdraw proof {withdraw_proof:?}");
166+
self.withdraw_proof = withdraw_proof;
138167
}
139168

140169
fn fill_state_roots_from_generator(
@@ -176,6 +205,7 @@ impl MptUpdates {
176205
self.old_root,
177206
self.new_root
178207
);
208+
179209
wit_gen
180210
}
181211

zktrie/src/state/witness.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,26 @@ impl WitnessGenerator {
9999
pub fn dump(&self) {
100100
log::info!("account data {:#?}", self.accounts);
101101
}
102-
102+
/// get account proof
103+
pub fn account_proof(&self, address: Address) -> Vec<Vec<u8>> {
104+
self.trie.prove(address.as_bytes()).unwrap()
105+
}
106+
/// get storage proof
107+
pub fn storage_proof(&self, address: Address, key: Word) -> Vec<Vec<u8>> {
108+
let (_storage_key, key) = {
109+
let mut word_buf = [0u8; 32];
110+
key.to_big_endian(word_buf.as_mut_slice());
111+
(hash_zktrie_key(&word_buf), HexBytes(word_buf))
112+
};
113+
// TODO: use or_else to optimize
114+
let default_trie = &ZktrieState::default()
115+
.zk_db
116+
.borrow_mut()
117+
.new_trie(&ZkTrieHash::default())
118+
.unwrap();
119+
let trie: &ZkTrie = self.storages.get(&address).unwrap_or(default_trie);
120+
trie.prove(key.as_ref()).unwrap()
121+
}
103122
fn trace_storage_update(
104123
&mut self,
105124
address: Address,

0 commit comments

Comments
 (0)