Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
mpt back to main
Browse files Browse the repository at this point in the history
  • Loading branch information
adria0 committed Feb 5, 2024
1 parent f336620 commit e3ffe44
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 91 deletions.
21 changes: 1 addition & 20 deletions bin/mpt-test/src/circuit/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,26 +308,7 @@ impl<F: Field> Witness<F> {
&trns.trie_modifications,
provider,
);

let mut nodes: Vec<Node> = serde_json::from_str(&json).unwrap();

// Add the address and the key to the list of values in the Account and Storage nodes
for node in nodes.iter_mut() {
if node.account.is_some() {
let account = node.account.clone().unwrap();
node.values
.push([vec![148], account.address.to_vec()].concat().into());
node.values
.push([vec![160], account.key.to_vec()].concat().into());
}
if node.storage.is_some() {
let storage = node.storage.clone().unwrap();
node.values
.push([vec![160], storage.address.to_vec()].concat().into());
node.values
.push([vec![160], storage.key.to_vec()].concat().into());
}
}
let nodes = zkevm_circuits::mpt_circuit::load_proof(json.as_bytes())?;

let witness_previous_state_root = H256::from_slice(&nodes[0].values[0][1..33]);
let non_disabled_node = |n: &&Node| {
Expand Down
4 changes: 2 additions & 2 deletions circuit-benchmarks/src/mpt_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod tests {
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;
use std::{env::var, ops::Deref};
use zkevm_circuits::mpt_circuit::{load_proof, witness_row::Node, MPTCircuit};
use zkevm_circuits::mpt_circuit::{load_proof_from_file, witness_row::Node, MPTCircuit};

#[cfg_attr(not(feature = "benches"), ignore)]
#[test]
Expand All @@ -39,7 +39,7 @@ mod tests {
.expect("Cannot parse DEGREE env var as u32");

let path = "../zkevm-circuits/src/mpt_circuit/tests/UpdateOneLevel.json";
let nodes: Vec<Node> = load_proof(path);
let nodes: Vec<Node> = load_proof_from_file(path);

let mut keccak_data = vec![];
for node in nodes.iter() {
Expand Down
11 changes: 1 addition & 10 deletions geth-utils/gethutil/mpt/witness/leaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,16 +493,7 @@ func prepareStorageLeafInfo(row []byte, valueIsZero, isPlaceholder bool) ([]byte
} else {
// [226,160,59,138,106,70,105,186,37,13,38[227,32,161,160,187,239,170,18,88,1,56,188,38,60,149,117,120,38,223,78,36,235,129,201,170,170,170,170,170,170,170,170,170,170,170,170]
keyLen = row[1] - 128
if keyLen+2 > valueLen {
// This happens for StorageDoesNotExist when the trie is empty. In this case, the key
// occupies 33 (33 = 161 - 128) bytes, as in the example below:
// [227 161 32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
// Currently, the length for the RLP items is set valueLen = 34, changing this to 35 would
// require significant changes in the circuit.
copy(key, row[keyRlpLen:valueLen])
} else {
copy(key, row[keyRlpLen:keyLen+2])
}
copy(key, row[keyRlpLen:keyLen+2])
offset = byte(2)
}
}
Expand Down
51 changes: 4 additions & 47 deletions geth-utils/src/mpt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,52 +95,9 @@ pub fn get_witness(block_no: u64, mods: &[TrieModification], node_url: &str) ->

unsafe { go::FreeString(c_str.as_ptr()) };

json

// let mut nodes: Vec<Node> = serde_json::from_str(json).unwrap();
//
// Add the address and the key to the list of values in the Account and Storage nodes
// for node in nodes.iter_mut() {
// if node.account.is_some() {
// let account = node.account.clone().unwrap();
// node.values
// .push([vec![148], account.address.to_vec()].concat().into());
// node.values
// .push([vec![160], account.key.to_vec()].concat().into());
// }
// if node.storage.is_some() {
// let storage = node.storage.clone().unwrap();
// node.values
// .push([vec![160], storage.address.to_vec()].concat().into());
// node.values
// .push([vec![160], storage.key.to_vec()].concat().into());
// }
// }
// nodes
}
// Note: previously this function returned a Vec of Nodes, but now returning a JSON string
// to avoid imporing zkEVM circuit here (that will create a circular dependency).
// TODO: consider defining Node types in another crate.

#[cfg(test)]
mod tests {
use std::str::FromStr;

use super::*;

#[test]
fn it_works() {
let nodes = get_witness(
14359865,
&[TrieModification {
typ: ProofType::StorageChanged,
key: H256::from_low_u64_le(0x12),
value: 0x1123e2.into(),
address: Address::from_str("0x4E5B2e1dc63F6b91cb6Cd759936495434C7e972F").unwrap(),
nonce: 0.into(),
balance: 0.into(),
code_hash: H256::zero(),
}],
"https://mainnet.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161",
);
println!("{:?}", nodes);
assert_eq!(nodes.len(), 12);
}
json
}
29 changes: 17 additions & 12 deletions zkevm-circuits/src/mpt_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use halo2_proofs::{
poly::Rotation,
};

use std::{convert::TryInto, env::var, marker::PhantomData};
use std::{convert::TryInto, env::var, io::Read, marker::PhantomData};

mod account_leaf;
mod branch;
Expand Down Expand Up @@ -359,10 +359,10 @@ impl<F: Field> MPTConfig<F> {
}
let cell_columns = [rlp_cm.columns(), state_cm.columns()].concat();

// println!("max expression degree: {}", meta.degree());
// println!("num lookups: {}", meta.lookups().len());
// println!("num advices: {}", meta.num_advice_columns());
// println!("num fixed: {}", meta.num_fixed_columns());
log::info!("max expression degree: {}", meta.degree());
log::info!("num lookups: {}", meta.lookups().len());
log::info!("num advices: {}", meta.num_advice_columns());
log::info!("num fixed: {}", meta.num_fixed_columns());
// cb.base.print_stats();

MPTConfig {
Expand Down Expand Up @@ -733,11 +733,9 @@ impl<F: Field> Circuit<F> for MPTCircuit<F> {
}
}

/// Loads an MPT proof from disk
pub fn load_proof(path: &str) -> Vec<Node> {
let file = std::fs::File::open(path);
let reader = std::io::BufReader::new(file.unwrap());
let mut nodes: Vec<Node> = serde_json::from_reader(reader).unwrap();
/// Loads an MPT proof from reader
pub fn load_proof<R: Read>(reader: R) -> Result<Vec<Node>, serde_json::Error> {
let mut nodes: Vec<Node> = serde_json::from_reader(reader)?;

// Add the address and the key to the list of values in the Account and Storage nodes
for node in nodes.iter_mut() {
Expand All @@ -754,7 +752,14 @@ pub fn load_proof(path: &str) -> Vec<Node> {
.push([vec![160], storage.key.to_vec()].concat().into());
}
}
nodes
Ok(nodes)
}

/// Loads an MPT proof from disk
pub fn load_proof_from_file(path: &str) -> Vec<Node> {
let file = std::fs::File::open(path);
let reader = std::io::BufReader::new(file.unwrap());
load_proof(reader).unwrap()
}

#[cfg(test)]
Expand Down Expand Up @@ -782,7 +787,7 @@ mod tests {
let mut parts = path.to_str().unwrap().split('-');
parts.next();

let nodes = load_proof(path.to_str().unwrap());
let nodes = load_proof_from_file(path.to_str().unwrap());
let num_rows: usize = nodes.iter().map(|node| node.values.len()).sum();

let mut keccak_data = vec![];
Expand Down

0 comments on commit e3ffe44

Please sign in to comment.