From 9613b2cb1df35ca2e9177823fd891f9d1d7b6c5d Mon Sep 17 00:00:00 2001 From: Mason Liang Date: Mon, 25 Sep 2023 21:07:52 -0400 Subject: [PATCH 1/4] Remove unused function --- src/constraint_builder.rs | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/src/constraint_builder.rs b/src/constraint_builder.rs index 7b146ddb..d4272488 100644 --- a/src/constraint_builder.rs +++ b/src/constraint_builder.rs @@ -83,27 +83,6 @@ impl ConstraintBuilder { self.lookups.push((name, lookup)) } - pub fn add_lookup_with_default( - &mut self, - name: &'static str, - left: [Query; N], - right: [Query; N], - default: [Query; N], - ) { - let condition = self - .conditions - .iter() - .skip(1) // Save a degree by skipping every row selector - .fold(BinaryQuery::one(), |a, b| a.and(b.clone())); - let lookup = left - .into_iter() - .zip(default.into_iter()) - .map(|(a, b)| condition.select(a, b)) - .zip(right.into_iter()) - .collect(); - self.lookups.push((name, lookup)) - } - pub fn build_columns( &self, cs: &mut ConstraintSystem, From 3667289cb46e69bfb3075b9fccaa2e3f58693f10 Mon Sep 17 00:00:00 2001 From: Mason Liang Date: Mon, 25 Sep 2023 21:12:36 -0400 Subject: [PATCH 2/4] Move poseidon lookup to constraint builder --- src/constraint_builder.rs | 35 ++++++++++++++++++++++++++++++ src/gadgets/poseidon.rs | 45 ++++----------------------------------- 2 files changed, 39 insertions(+), 41 deletions(-) diff --git a/src/constraint_builder.rs b/src/constraint_builder.rs index d4272488..f2f881d2 100644 --- a/src/constraint_builder.rs +++ b/src/constraint_builder.rs @@ -1,3 +1,4 @@ +use crate::gadgets::poseidon::PoseidonLookup; use halo2_proofs::{ arithmetic::FieldExt, plonk::{ConstraintSystem, SecondPhase}, @@ -83,6 +84,40 @@ impl ConstraintBuilder { self.lookups.push((name, lookup)) } + pub fn poseidon_lookup( + &mut self, + name: &'static str, + [left, right, domain, hash]: [Query; 4], + poseidon: &impl PoseidonLookup, + ) { + let extended_queries = [ + Query::one(), + hash, + left, + right, + Query::zero(), + domain, + Query::one(), + ]; + + let (q_enable, [hash, left, right, control, domain_spec, head_mark]) = + poseidon.lookup_columns(); + + self.add_lookup( + name, + extended_queries, + [ + q_enable.current(), + hash.current(), + left.current(), + right.current(), + control.current(), + domain_spec.current(), + head_mark.current(), + ], + ) + } + pub fn build_columns( &self, cs: &mut ConstraintSystem, diff --git a/src/gadgets/poseidon.rs b/src/gadgets/poseidon.rs index 0d5ed41c..2586490e 100644 --- a/src/gadgets/poseidon.rs +++ b/src/gadgets/poseidon.rs @@ -1,11 +1,10 @@ -use crate::constraint_builder::{AdviceColumn, ConstraintBuilder, FixedColumn, Query}; +use crate::constraint_builder::{AdviceColumn, FixedColumn}; +use halo2_proofs::plonk::{Advice, Column, Fixed}; +#[cfg(test)] use halo2_proofs::{ - arithmetic::FieldExt, - plonk::{Advice, Column, Fixed}, + arithmetic::FieldExt, circuit::Region, halo2curves::bn256::Fr, plonk::ConstraintSystem, }; #[cfg(test)] -use halo2_proofs::{circuit::Region, halo2curves::bn256::Fr, plonk::ConstraintSystem}; -#[cfg(test)] use hash_circuit::hash::Hashable; /// Lookup represent the poseidon table in zkevm circuit @@ -20,42 +19,6 @@ pub trait PoseidonLookup { } } -impl ConstraintBuilder { - pub fn poseidon_lookup( - &mut self, - name: &'static str, - [left, right, domain, hash]: [Query; 4], - poseidon: &impl PoseidonLookup, - ) { - let extended_queries = [ - Query::one(), - hash, - left, - right, - Query::zero(), - domain, - Query::one(), - ]; - - let (q_enable, [hash, left, right, control, domain_spec, head_mark]) = - poseidon.lookup_columns(); - - self.add_lookup( - name, - extended_queries, - [ - q_enable.current(), - hash.current(), - left.current(), - right.current(), - control.current(), - domain_spec.current(), - head_mark.current(), - ], - ) - } -} - #[cfg(test)] #[derive(Clone, Copy)] pub struct PoseidonTable { From bcf16365c4730a5ba6434d879f17dc07ca0d2ce5 Mon Sep 17 00:00:00 2001 From: Mason Liang Date: Mon, 25 Sep 2023 21:20:53 -0400 Subject: [PATCH 3/4] Ensure row is enabled for non-poseidon lookups --- src/constraint_builder.rs | 41 +++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/src/constraint_builder.rs b/src/constraint_builder.rs index f2f881d2..b653a5f7 100644 --- a/src/constraint_builder.rs +++ b/src/constraint_builder.rs @@ -3,6 +3,7 @@ use halo2_proofs::{ arithmetic::FieldExt, plonk::{ConstraintSystem, SecondPhase}, }; +use itertools::Itertools; mod binary_column; mod binary_query; @@ -74,13 +75,14 @@ impl ConstraintBuilder { let condition = self .conditions .iter() - .skip(1) // Save a degree by skipping every row selector .fold(BinaryQuery::one(), |a, b| a.and(b.clone())); - let lookup = left + let mut lookup: Vec<_> = left .into_iter() .map(|q| q * condition.clone()) .zip(right.into_iter()) .collect(); + // If condition is true, every_row_selector must be enabled. + lookup.push((condition.into(), self.every_row_selector().into())); self.lookups.push((name, lookup)) } @@ -90,6 +92,11 @@ impl ConstraintBuilder { [left, right, domain, hash]: [Query; 4], poseidon: &impl PoseidonLookup, ) { + let condition = self + .conditions + .iter() + .skip(1) // Save a degree by skipping every row selector + .fold(BinaryQuery::one(), |a, b| a.and(b.clone())); let extended_queries = [ Query::one(), hash, @@ -98,24 +105,28 @@ impl ConstraintBuilder { Query::zero(), domain, Query::one(), - ]; + ] + .map(|q| q * condition.clone()); let (q_enable, [hash, left, right, control, domain_spec, head_mark]) = poseidon.lookup_columns(); + let poseidon_lookup_queries = [ + q_enable.current(), + hash.current(), + left.current(), + right.current(), + control.current(), + domain_spec.current(), + head_mark.current(), + ]; - self.add_lookup( + self.lookups.push(( name, - extended_queries, - [ - q_enable.current(), - hash.current(), - left.current(), - right.current(), - control.current(), - domain_spec.current(), - head_mark.current(), - ], - ) + extended_queries + .into_iter() + .zip_eq(poseidon_lookup_queries) + .collect(), + )) } pub fn build_columns( From 578c210ceb88d3c143ee2a013ad836d19285d9c1 Mon Sep 17 00:00:00 2001 From: Mason Liang Date: Mon, 25 Sep 2023 21:28:40 -0400 Subject: [PATCH 4/4] Use enough rows to allow tests to pass --- src/gadgets/byte_representation.rs | 2 +- src/gadgets/canonical_representation.rs | 2 +- src/gadgets/key_bit.rs | 2 +- src/tests.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/gadgets/byte_representation.rs b/src/gadgets/byte_representation.rs index 20485787..4ce4a331 100644 --- a/src/gadgets/byte_representation.rs +++ b/src/gadgets/byte_representation.rs @@ -219,7 +219,7 @@ mod test { layouter.assign_region( || "", |mut region| { - for offset in 0..1024 { + for offset in 0..(8 * 256) { selector.enable(&mut region, offset); } byte_bit.assign(&mut region); diff --git a/src/gadgets/canonical_representation.rs b/src/gadgets/canonical_representation.rs index 98383cc9..7e2ae9b5 100644 --- a/src/gadgets/canonical_representation.rs +++ b/src/gadgets/canonical_representation.rs @@ -250,7 +250,7 @@ mod test { layouter.assign_region( || "", |mut region| { - for offset in 0..256 { + for offset in 0..(8 * 256) { selector.enable(&mut region, offset); } byte_bit.assign(&mut region); diff --git a/src/gadgets/key_bit.rs b/src/gadgets/key_bit.rs index cc2299d3..8891665d 100644 --- a/src/gadgets/key_bit.rs +++ b/src/gadgets/key_bit.rs @@ -191,7 +191,7 @@ mod test { layouter.assign_region( || "", |mut region| { - for offset in 0..32 { + for offset in 0..(8 * 256) { selector.enable(&mut region, offset); } diff --git a/src/tests.rs b/src/tests.rs index 5601076b..91855616 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -11,7 +11,7 @@ use halo2_proofs::{ }; use mpt_zktrie::state::{builder::HASH_SCHEME_DONE, witness::WitnessGenerator, ZktrieState}; -const N_ROWS: usize = 1024; +const N_ROWS: usize = 8 * 256 + 1; const STORAGE_ADDRESS: Address = Address::repeat_byte(1); fn initial_generator() -> WitnessGenerator {