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

MPT max nodes #1745

Merged
merged 6 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions circuit-benchmarks/src/mpt_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ mod tests {
}
}

let max_nodes = 720;
let circuit = MPTCircuit::<Fr> {
nodes,
keccak_data,
degree: degree as usize,
max_nodes,
disable_preimage_check: false,
_marker: PhantomData,
};
Expand Down
4 changes: 3 additions & 1 deletion light-client-poc/src/circuit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ mod witness;

pub use prover::StateUpdateCircuitKeys;

pub use state_update::{StateUpdateCircuit, DEFAULT_CIRCUIT_DEGREE, DEFAULT_MAX_PROOF_COUNT};
pub use state_update::{
StateUpdateCircuit, DEFAULT_CIRCUIT_DEGREE, DEFAULT_MAX_NODES, DEFAULT_MAX_PROOF_COUNT,
};
pub use witness::{PublicInputs, StateUpdateWitness};
17 changes: 11 additions & 6 deletions light-client-poc/src/circuit/state_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use zkevm_circuits::{
util::{SubCircuit, SubCircuitConfig},
};

pub const DEFAULT_MAX_NODES: usize = 4000;
pub const DEFAULT_MAX_PROOF_COUNT: usize = 20;
pub const DEFAULT_CIRCUIT_DEGREE: usize = 14;

Expand Down Expand Up @@ -90,6 +91,7 @@ impl<F: Field> Circuit<F> for StateUpdateCircuit<F> {
MPTCircuitParams {
degree: self.mpt_circuit.degree,
disable_preimage_check: self.mpt_circuit.disable_preimage_check,
max_nodes: self.mpt_circuit.max_nodes,
}
}

Expand Down Expand Up @@ -301,14 +303,15 @@ impl<F: Field> Circuit<F> for StateUpdateCircuit<F> {

// assign MPT witness

let height =
config
.mpt_config
.assign(&mut layouter, &self.mpt_circuit.nodes, &challenges)?;
config.mpt_config.load_fixed_table(&mut layouter)?;
config
.mpt_config
.load_mult_table(&mut layouter, &challenges, height)?;
.assign(&mut layouter, &self.mpt_circuit.nodes, &challenges)?;
config.mpt_config.load_fixed_table(&mut layouter)?;
config.mpt_config.load_mult_table(
&mut layouter,
&challenges,
self.mpt_circuit.max_nodes,
)?;

#[cfg(feature = "disable-keccak")]
config.mpt_config.keccak_table.dev_load(
Expand Down Expand Up @@ -466,6 +469,7 @@ impl StateUpdateCircuit<Fr> {
pub fn new(
witness: StateUpdateWitness<Fr>,
degree: usize,
max_nodes: usize,
max_proof_count: usize,
) -> Result<StateUpdateCircuit<Fr>> {
let StateUpdateWitness {
Expand All @@ -489,6 +493,7 @@ impl StateUpdateCircuit<Fr> {
nodes: mpt_witness,
keccak_data: keccak_data.clone(),
degree,
max_nodes,
disable_preimage_check,
_marker: std::marker::PhantomData,
};
Expand Down
10 changes: 7 additions & 3 deletions light-client-poc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{collections::HashMap, str::FromStr, time::SystemTime};

use crate::circuit::{
PublicInputs, StateUpdateCircuit, StateUpdateCircuitKeys, StateUpdateWitness,
DEFAULT_CIRCUIT_DEGREE, DEFAULT_MAX_PROOF_COUNT,
DEFAULT_CIRCUIT_DEGREE, DEFAULT_MAX_NODES, DEFAULT_MAX_PROOF_COUNT,
};

pub async fn serve() -> Result<()> {
Expand Down Expand Up @@ -62,8 +62,12 @@ pub async fn serve() -> Result<()> {
};

let public_inputs: PublicInputs<Fr> = (&witness.lc_witness).into();
let circuit =
StateUpdateCircuit::new(witness, DEFAULT_CIRCUIT_DEGREE, DEFAULT_MAX_PROOF_COUNT)?;
let circuit = StateUpdateCircuit::new(
witness,
DEFAULT_CIRCUIT_DEGREE,
DEFAULT_MAX_NODES,
DEFAULT_MAX_PROOF_COUNT,
)?;

println!("trns: {:#?}", circuit.transforms);

Expand Down
60 changes: 52 additions & 8 deletions light-client-poc/src/tests/mainnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod test {

use crate::circuit::{
PublicInputs, StateUpdateCircuit, StateUpdateCircuitKeys, StateUpdateWitness,
DEFAULT_CIRCUIT_DEGREE, DEFAULT_MAX_PROOF_COUNT,
DEFAULT_CIRCUIT_DEGREE, DEFAULT_MAX_NODES, DEFAULT_MAX_PROOF_COUNT,
};

#[ctor::ctor]
Expand All @@ -23,6 +23,7 @@ mod test {
block_no: u64,
access_list: &[(&str, Vec<&str>)],
degree: usize,
max_nodes: usize,
max_proof_count: usize,
) -> Result<StateUpdateCircuit<Fr>> {
const PVK: &str = "7ccb34dc5fd31fd0aa7860de89a4adc37ccb34dc5fd31fd0aa7860de89a4adc3";
Expand Down Expand Up @@ -54,7 +55,7 @@ mod test {

println!("trns: {:#?}", witness.transforms);

let circuit = StateUpdateCircuit::new(witness, degree, max_proof_count)?;
let circuit = StateUpdateCircuit::new(witness, degree, max_nodes, max_proof_count)?;

circuit.assert_satisfied();

Expand Down Expand Up @@ -150,7 +151,14 @@ mod test {
async fn test_block_436875() -> Result<()> {
let block_no = 436875;
let access_list = blocks().get(&block_no).unwrap().clone();
let _ = mock_prove(block_no, &access_list, 16, DEFAULT_MAX_PROOF_COUNT).await?;
let _ = mock_prove(
block_no,
&access_list,
16,
DEFAULT_MAX_NODES,
DEFAULT_MAX_PROOF_COUNT,
)
.await?;
Ok(())
}

Expand All @@ -159,7 +167,14 @@ mod test {
async fn test_block_107() -> Result<()> {
let block_no = 107;
let access_list = blocks().get(&block_no).unwrap().clone();
let _ = mock_prove(block_no, &access_list, 15, DEFAULT_MAX_PROOF_COUNT).await?;
let _ = mock_prove(
block_no,
&access_list,
15,
DEFAULT_MAX_NODES,
DEFAULT_MAX_PROOF_COUNT,
)
.await?;
Ok(())
}

Expand All @@ -169,7 +184,14 @@ mod test {
let block_no = 107;
let access_list = blocks().get(&block_no).unwrap().clone();

let circuit = mock_prove(block_no, &access_list, 15, DEFAULT_MAX_PROOF_COUNT).await?;
let circuit = mock_prove(
block_no,
&access_list,
15,
DEFAULT_MAX_NODES,
DEFAULT_MAX_PROOF_COUNT,
)
.await?;
let public_inputs: PublicInputs<Fr> = (&circuit.lc_witness).into();

let keys = StateUpdateCircuitKeys::new(&circuit);
Expand All @@ -186,6 +208,7 @@ mod test {
block_no,
&access_list,
DEFAULT_CIRCUIT_DEGREE,
DEFAULT_MAX_NODES,
DEFAULT_MAX_PROOF_COUNT,
)
.await?;
Expand Down Expand Up @@ -214,7 +237,14 @@ mod test {
async fn test_block_2000007() -> Result<()> {
let block_no = 2000007;
let access_list = blocks().get(&block_no).unwrap().clone();
let _ = mock_prove(block_no, &access_list, 18, DEFAULT_MAX_PROOF_COUNT).await?;
let _ = mock_prove(
block_no,
&access_list,
18,
DEFAULT_MAX_NODES,
DEFAULT_MAX_PROOF_COUNT,
)
.await?;
Ok(())
}

Expand All @@ -223,7 +253,14 @@ mod test {
async fn test_block_2000004() -> Result<()> {
let block_no = 2000004;
let access_list = blocks().get(&block_no).unwrap().clone();
let _ = mock_prove(block_no, &access_list, 18, DEFAULT_MAX_PROOF_COUNT).await?;
let _ = mock_prove(
block_no,
&access_list,
18,
DEFAULT_MAX_NODES,
DEFAULT_MAX_PROOF_COUNT,
)
.await?;
Ok(())
}

Expand All @@ -232,7 +269,14 @@ mod test {
async fn test_block_2000070() -> Result<()> {
let block_no = 2000070;
let access_list = blocks().get(&block_no).unwrap().clone();
let _ = mock_prove(block_no, &access_list, 18, DEFAULT_MAX_PROOF_COUNT).await?;
let _ = mock_prove(
block_no,
&access_list,
18,
DEFAULT_MAX_NODES,
DEFAULT_MAX_PROOF_COUNT,
)
.await?;
Ok(())
}
}
2 changes: 2 additions & 0 deletions light-client-poc/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,12 @@ pub fn verify_mpt_witness(nodes: Vec<Node>) -> Result<()> {
// verify the circuit
let disable_preimage_check = nodes[0].start.clone().unwrap().disable_preimage_check;
let degree = 15;
let max_nodes = 520;
let circuit = zkevm_circuits::mpt_circuit::MPTCircuit::<Fr> {
nodes,
keccak_data,
degree,
max_nodes,
disable_preimage_check,
_marker: std::marker::PhantomData,
};
Expand Down
39 changes: 23 additions & 16 deletions zkevm-circuits/src/mpt_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ impl<F: Field> MPTContext<F> {
pub struct MPTConfig<F: Field> {
pub(crate) q_enable: Column<Fixed>,
pub(crate) q_first: Column<Fixed>,
pub(crate) q_last: Column<Fixed>,
pub(crate) memory: MptMemory<F>,
/// MPT table
pub mpt_table: MptTable,
Expand Down Expand Up @@ -219,7 +218,6 @@ impl<F: Field> MPTConfig<F> {
) -> Self {
let q_enable = meta.fixed_column();
let q_first = meta.fixed_column();
let q_last = meta.fixed_column();

let mpt_table = MptTable::construct(meta);

Expand Down Expand Up @@ -308,7 +306,7 @@ impl<F: Field> MPTConfig<F> {
// Main MPT circuit
// State machine
cb.base.set_cell_manager(state_cm.clone());
ifx! {f!(q_first) + f!(q_last) => {
ifx! {f!(q_first) => {
require!(a!(state_machine.is_start) => true);
}};
// Main state machine
Expand Down Expand Up @@ -370,7 +368,6 @@ impl<F: Field> MPTConfig<F> {
MPTConfig {
q_enable,
q_first,
q_last,
memory,
keccak_table,
fixed_table,
Expand All @@ -390,8 +387,7 @@ impl<F: Field> MPTConfig<F> {
layouter: &mut impl Layouter<F>,
nodes: &[Node],
challenges: &Challenges<Value<F>>,
) -> Result<usize, Error> {
let mut height = 0;
) -> Result<(), Error> {
layouter.assign_region(
|| "MPT",
|mut region| {
Expand Down Expand Up @@ -497,24 +493,28 @@ impl<F: Field> MPTConfig<F> {

cached_region.assign_stored_expressions(&self.cb.base, challenges)?;
}
height = offset;

// Make sure the circuit is high enough for the mult table
while height < (2 * HASH_WIDTH + 1) {
height += 1;
}
assert!( self.params.max_nodes >= (2 * HASH_WIDTH + 1),
"The parameter max_nodes is set too low for the mult table: {}, mult table height: {}",
self.params.max_nodes,
2 * HASH_WIDTH + 1,
);
assert!( offset <= self.params.max_nodes,
"The parameter max_nodes is set too low, max_nodes: {}, offset: {}",
self.params.max_nodes,
offset,
);

for offset in 0..height {
for offset in 0..self.params.max_nodes {
assignf!(region, (self.q_enable, offset) => true.scalar())?;
assignf!(region, (self.q_first, offset) => (offset == 0).scalar())?;
assignf!(region, (self.q_last, offset) => (offset == height - 2).scalar())?;
}

Ok(())
},
)?;

Ok(height)
Ok(())
}

/// Loads MPT fixed table
Expand Down Expand Up @@ -653,6 +653,8 @@ pub struct MPTCircuit<F: Field> {
pub keccak_data: Vec<Vec<u8>>,
/// log2(height)
pub degree: usize,
/// Maximal number of nodes MPT can prove (for example, one branch has 16 nodes)
pub max_nodes: usize,
/// Can be used to test artificially created tests with keys without known their known
/// preimage. ONLY ENABLE FOR TESTS!
pub disable_preimage_check: bool,
Expand All @@ -667,6 +669,8 @@ pub struct MPTCircuitParams {
pub degree: usize,
///
pub disable_preimage_check: bool,
/// Maximal number of nodes MPT can prove (for example, one branch has 16 nodes)
pub max_nodes: usize,
}

impl MPTCircuitParams {
Expand Down Expand Up @@ -694,6 +698,7 @@ impl<F: Field> Circuit<F> for MPTCircuit<F> {
MPTCircuitParams {
degree: self.degree,
disable_preimage_check: self.disable_preimage_check,
max_nodes: self.max_nodes,
}
}

Expand All @@ -717,9 +722,9 @@ impl<F: Field> Circuit<F> for MPTCircuit<F> {
mut layouter: impl Layouter<F>,
) -> Result<(), Error> {
let challenges = _challenges.values(&mut layouter);
let height = config.assign(&mut layouter, &self.nodes, &challenges)?;
config.assign(&mut layouter, &self.nodes, &challenges)?;
config.load_fixed_table(&mut layouter)?;
config.load_mult_table(&mut layouter, &challenges, height)?;
config.load_mult_table(&mut layouter, &challenges, self.max_nodes)?;
config
.keccak_table
.dev_load(&mut layouter, &self.keccak_data, &challenges)?;
Expand Down Expand Up @@ -789,10 +794,12 @@ mod tests {

let disable_preimage_check = nodes[0].start.clone().unwrap().disable_preimage_check;
let degree = 15;
let max_nodes = 520;
let circuit = MPTCircuit::<Fr> {
nodes,
keccak_data,
degree,
max_nodes,
disable_preimage_check,
_marker: PhantomData,
};
Expand Down
Loading