From e42d12951d8e9947d2d7334350b7cd377f665247 Mon Sep 17 00:00:00 2001 From: lightsing Date: Mon, 31 Jul 2023 17:34:19 +0800 Subject: [PATCH 01/17] add range check --- gadgets/src/less_than.rs | 68 ++++++++++++++++++++++++++++++++++------ 1 file changed, 58 insertions(+), 10 deletions(-) diff --git a/gadgets/src/less_than.rs b/gadgets/src/less_than.rs index c2b51ce05d..a01720f675 100644 --- a/gadgets/src/less_than.rs +++ b/gadgets/src/less_than.rs @@ -3,16 +3,14 @@ use eth_types::Field; use halo2_proofs::{ arithmetic::FieldExt, - circuit::{Chip, Region, Value}, - plonk::{Advice, Column, ConstraintSystem, Error, Expression, VirtualCells}, + circuit::{Chip, Layouter, Region, Value}, + plonk::{Advice, Column, ConstraintSystem, Error, Expression, Fixed, VirtualCells}, poly::Rotation, }; -use crate::util::sum; - -use super::{ +use crate::{ bool_check, - util::{expr_from_bytes, pow_of_two}, + util::{expr_from_bytes, pow_of_two, sum}, }; /// Instruction that the Lt chip needs to implement. @@ -25,6 +23,9 @@ pub trait LtInstruction { lhs: F, rhs: F, ) -> Result<(), Error>; + + /// Load the u8x2 lookup table. + fn load(&self, layouter: &mut impl Layouter) -> Result<(), Error>; } /// Config for the Lt chip. @@ -33,8 +34,9 @@ pub struct LtConfig { /// Denotes the lt outcome. If lhs < rhs then lt == 1, otherwise lt == 0. pub lt: Column, /// Denotes the bytes representation of the difference between lhs and rhs. - /// Note that the range of each byte is not checked by this config. pub diff: [Column; N_BYTES], + /// Denotes the range within which each byte should lie. + pub u8x2: Column, /// Denotes the range within which both lhs and rhs lie. pub range: F, } @@ -68,6 +70,7 @@ impl LtChip { ) -> LtConfig { let lt = meta.advice_column(); let diff = [(); N_BYTES].map(|_| meta.advice_column()); + let u8x2 = meta.fixed_column(); let range = pow_of_two(N_BYTES * 8); meta.create_gate("lt gate", |meta| { @@ -89,7 +92,29 @@ impl LtChip { .map(move |poly| q_enable.clone() * poly) }); - LtConfig { lt, diff, range } + meta.annotate_lookup_any_column(u8x2, || "LOOKUP_u8x2"); + + for cell_columns in diff.chunks(2) { + meta.lookup_any("range check for u8x2", |meta| { + let cell_expr = if cell_columns.len() == 2 { + meta.query_advice(cell_columns[0], Rotation::cur()) + * Expression::Constant(pow_of_two(8)) + + (meta.query_advice(cell_columns[1], Rotation::cur())) + } else { + meta.query_advice(cell_columns[0], Rotation::cur()) + * Expression::Constant(pow_of_two(8)) + }; + let u8x2_range = meta.query_fixed(u8x2, Rotation::cur()); + vec![(cell_expr, u8x2_range)] + }); + } + + LtConfig { + lt, + diff, + u8x2, + range, + } } /// Constructs a Lt chip given a config. @@ -129,6 +154,25 @@ impl LtInstruction for LtChip { Ok(()) } + + fn load(&self, layouter: &mut impl Layouter) -> Result<(), Error> { + const RANGE: usize = u16::MAX as usize; + + layouter.assign_region( + || "load u8x2 range check table", + |mut region| { + for i in 0..=RANGE { + region.assign_fixed( + || "assign cell in fixed column", + self.config.u8x2, + i, + || Value::known(F::from(i as u64)), + )?; + } + Ok(()) + }, + ) + } } impl Chip for LtChip { @@ -164,7 +208,7 @@ mod test { // TODO: remove zk blinding factors in halo2 to restore the // correct k (without the extra + 2). - let k = usize::BITS - $values.len().leading_zeros() + 2; + let k = (usize::BITS - $values.len().leading_zeros() + 2).max(17); let circuit = TestCircuit:: { values: Some($values), checks: Some($checks), @@ -181,7 +225,7 @@ mod test { // TODO: remove zk blinding factors in halo2 to restore the // correct k (without the extra + 2). - let k = usize::BITS - $values.len().leading_zeros() + 2; + let k = (usize::BITS - $values.len().leading_zeros() + 2).max(17); let circuit = TestCircuit:: { values: Some($values), checks: Some($checks), @@ -265,6 +309,8 @@ mod test { let (first_value, values) = values.split_at(1); let first_value = first_value[0]; + chip.load(&mut layouter)?; + layouter.assign_region( || "witness", |mut region| { @@ -387,6 +433,8 @@ mod test { .ok_or(Error::Synthesis)?; let checks = self.checks.as_ref().ok_or(Error::Synthesis)?; + chip.load(&mut layouter)?; + layouter.assign_region( || "witness", |mut region| { From 87cda410f237f9a3012b732e566f8476bdb046bf Mon Sep 17 00:00:00 2001 From: lightsing Date: Tue, 1 Aug 2023 12:32:47 +0800 Subject: [PATCH 02/17] add range table --- zkevm-circuits/src/table.rs | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/zkevm-circuits/src/table.rs b/zkevm-circuits/src/table.rs index ce4a980b2d..ca3e1ceaaf 100644 --- a/zkevm-circuits/src/table.rs +++ b/zkevm-circuits/src/table.rs @@ -2678,3 +2678,43 @@ impl LookupTable for PowOfRandTable { ] } } + +/// Lookup table for [0, MAX) range +#[derive(Clone, Copy, Debug)] +pub struct RangeTable(Column); + +impl RangeTable { + /// Construct the range table. + pub fn construct(meta: &mut ConstraintSystem) -> Self { + Self(meta.fixed_column()) + } + + /// Assign values to the table. + pub fn load(&self, layouter: &mut impl Layouter) -> Result<(), Error> { + layouter.assign_region( + || format!("range table [0, {MAX})"), + |mut region| { + for i in 0..MAX { + region.assign_fixed( + || format!("range at offset = {i}"), + self.range, + i, + || Value::known(F::from(i as u64)), + )?; + } + + Ok(()) + }, + ) + } +} + +impl LookupTable for RangeTable { + fn columns(&self) -> Vec> { + vec![self.0.into()] + } + + fn annotations(&self) -> Vec { + vec![format!("value in [0, {MAX})")] + } +} From 28b8efbc72a54ada97bf4940862764800ad8c52c Mon Sep 17 00:00:00 2001 From: lightsing Date: Tue, 1 Aug 2023 12:54:43 +0800 Subject: [PATCH 03/17] replace Range256Table of rlp_circuit_fsm --- zkevm-circuits/src/rlp_circuit_fsm.rs | 53 ++++------------------- zkevm-circuits/src/rlp_circuit_fsm/dev.rs | 5 ++- zkevm-circuits/src/super_circuit.rs | 16 ++++++- zkevm-circuits/src/table.rs | 2 +- 4 files changed, 28 insertions(+), 48 deletions(-) diff --git a/zkevm-circuits/src/rlp_circuit_fsm.rs b/zkevm-circuits/src/rlp_circuit_fsm.rs index 691765017e..66f842ade0 100644 --- a/zkevm-circuits/src/rlp_circuit_fsm.rs +++ b/zkevm-circuits/src/rlp_circuit_fsm.rs @@ -27,7 +27,7 @@ use strum::IntoEnumIterator; use crate::{ evm_circuit::util::constraint_builder::{BaseConstraintBuilder, ConstrainBuilderCommon}, - table::{LookupTable, RlpFsmRlpTable}, + table::{LookupTable, RangeTable, RlpFsmRlpTable}, util::{Challenges, SubCircuit, SubCircuitConfig}, witness::{ Block, DataTable, Format, RlpFsmWitnessGen, RlpFsmWitnessRow, RlpTag, RomTableRow, State, @@ -38,43 +38,6 @@ use crate::{ }, }; -/// Fixed table to check if a value is a byte, i.e. 0 <= value < 256. -#[derive(Clone, Debug)] -pub struct Range256Table(Column); - -impl LookupTable for Range256Table { - fn columns(&self) -> Vec> { - vec![self.0.into()] - } - - fn annotations(&self) -> Vec { - vec![String::from("byte_value")] - } -} - -impl Range256Table { - pub(crate) fn construct(meta: &mut ConstraintSystem) -> Self { - Self(meta.fixed_column()) - } - - pub(crate) fn load(&self, layouter: &mut impl Layouter) -> Result<(), Error> { - layouter.assign_region( - || "RLP Range256 table", - |mut region| { - for row in 0..256 { - region.assign_fixed( - || "RLP range256", - self.0, - row, - || Value::known(F::from(row as u64)), - )?; - } - Ok(()) - }, - ) - } -} - /// Data table allows us a lookup argument from the RLP circuit to check the byte value at an index /// while decoding a tx of a given format. #[derive(Clone, Copy, Debug)] @@ -317,7 +280,7 @@ pub struct RlpCircuitConfig { /// ROM table rom_table: RlpFsmRomTable, /// Range256 table - range256_table: Range256Table, + u8_table: RangeTable<{ 1 << 8 }>, } impl RlpCircuitConfig { @@ -326,7 +289,7 @@ impl RlpCircuitConfig { meta: &mut ConstraintSystem, rom_table: RlpFsmRomTable, data_table: RlpFsmDataTable, - range256_table: Range256Table, + u8_table: RangeTable<{ 1 << 8 }>, rlp_table: RlpFsmRlpTable, challenges: &Challenges>, ) -> Self { @@ -583,7 +546,7 @@ impl RlpCircuitConfig { vec![meta.query_advice(data_table.byte_value, Rotation::cur())] .into_iter() - .zip(range256_table.table_exprs(meta).into_iter()) + .zip(u8_table.table_exprs(meta).into_iter()) .map(|(arg, table)| (cond.expr() * arg, table)) .collect() }); @@ -1398,7 +1361,7 @@ impl RlpCircuitConfig { // internal tables data_table, rom_table, - range256_table, + u8_table, } } @@ -1764,7 +1727,6 @@ impl RlpCircuitConfig { debug_assert!(sm_rows.len() <= last_row); - self.range256_table.load(layouter)?; self.rom_table.load(layouter)?; log::debug!("num_sm_rows: {}", sm_rows.len()); @@ -1812,6 +1774,8 @@ impl RlpCircuitConfig { pub struct RlpCircuitConfigArgs { /// RLP table. pub rlp_table: RlpFsmRlpTable, + /// u8 table + pub u8_table: RangeTable<{ 1 << 8 }>, /// Challenge API. pub challenges: Challenges>, } @@ -1822,13 +1786,12 @@ impl SubCircuitConfig for RlpCircuitConfig { fn new(meta: &mut ConstraintSystem, args: Self::ConfigArgs) -> Self { let data_table = RlpFsmDataTable::construct(meta); let rom_table = RlpFsmRomTable::construct(meta); - let range256_table = Range256Table::construct(meta); Self::configure( meta, rom_table, data_table, - range256_table, + args.u8_table, args.rlp_table, &args.challenges, ) diff --git a/zkevm-circuits/src/rlp_circuit_fsm/dev.rs b/zkevm-circuits/src/rlp_circuit_fsm/dev.rs index aed7e29828..c7bf84cdb3 100644 --- a/zkevm-circuits/src/rlp_circuit_fsm/dev.rs +++ b/zkevm-circuits/src/rlp_circuit_fsm/dev.rs @@ -1,6 +1,6 @@ use crate::{ rlp_circuit_fsm::{RlpCircuit, RlpCircuitConfig, RlpCircuitConfigArgs}, - table::RlpFsmRlpTable, + table::{RangeTable, RlpFsmRlpTable}, util::{Challenges, SubCircuit, SubCircuitConfig}, witness::Transaction, }; @@ -22,11 +22,13 @@ impl Circuit for RlpCircuit { let rlp_table = RlpFsmRlpTable::construct(meta); let challenges = Challenges::construct(meta); let challenge_exprs = challenges.exprs(meta); + let u8_table = RangeTable::construct(meta); let config = RlpCircuitConfig::new( meta, RlpCircuitConfigArgs { rlp_table, + u8_table, challenges: challenge_exprs, }, ); @@ -41,6 +43,7 @@ impl Circuit for RlpCircuit { mut layouter: impl Layouter, ) -> Result<(), Error> { let challenges = &config.1.values(&layouter); + config.0.u8_table.load(&mut layouter)?; self.synthesize_sub(&config.0, challenges, &mut layouter) } diff --git a/zkevm-circuits/src/super_circuit.rs b/zkevm-circuits/src/super_circuit.rs index 942eaf9bab..d2a54677f3 100644 --- a/zkevm-circuits/src/super_circuit.rs +++ b/zkevm-circuits/src/super_circuit.rs @@ -82,7 +82,8 @@ use crate::{ state_circuit::{StateCircuit, StateCircuitConfig, StateCircuitConfigArgs}, table::{ BlockTable, BytecodeTable, CopyTable, EccTable, ExpTable, KeccakTable, MptTable, - PoseidonTable, PowOfRandTable, RlpFsmRlpTable as RlpTable, RwTable, SigTable, TxTable, + PoseidonTable, PowOfRandTable, RangeTable, RlpFsmRlpTable as RlpTable, RwTable, SigTable, + TxTable, }, }; @@ -114,6 +115,8 @@ pub struct SuperCircuitConfig { rlp_table: RlpTable, tx_table: TxTable, poseidon_table: PoseidonTable, + u8_table: RangeTable<{ 1 << 8 }>, + u16_table: RangeTable<{ 1 << 16 }>, evm_circuit: EvmCircuitConfig, state_circuit: StateCircuitConfig, tx_circuit: TxCircuitConfig, @@ -198,6 +201,11 @@ impl SubCircuitConfig for SuperCircuitConfig { let pow_of_rand_table = PowOfRandTable::construct(meta, &challenges_expr); log_circuit_info(meta, "power of randomness table"); + let u8_table = RangeTable::construct(meta); + log_circuit_info(meta, "u8 table"); + let u16_table = RangeTable::construct(meta); + log_circuit_info(meta, "u16 table"); + let keccak_circuit = KeccakCircuitConfig::new( meta, KeccakCircuitConfigArgs { @@ -215,6 +223,7 @@ impl SubCircuitConfig for SuperCircuitConfig { meta, RlpCircuitConfigArgs { rlp_table, + u8_table, challenges: challenges_expr.clone(), }, ); @@ -357,6 +366,8 @@ impl SubCircuitConfig for SuperCircuitConfig { tx_table, rlp_table, poseidon_table, + u8_table, + u16_table, evm_circuit, state_circuit, copy_circuit, @@ -650,6 +661,9 @@ impl< &challenges, )?; + config.u8_table.load(&mut layouter)?; + config.u16_table.load(&mut layouter)?; + self.synthesize_sub(&config, &challenges, &mut layouter) } } diff --git a/zkevm-circuits/src/table.rs b/zkevm-circuits/src/table.rs index ca3e1ceaaf..072f76e209 100644 --- a/zkevm-circuits/src/table.rs +++ b/zkevm-circuits/src/table.rs @@ -2697,7 +2697,7 @@ impl RangeTable { for i in 0..MAX { region.assign_fixed( || format!("range at offset = {i}"), - self.range, + self.0, i, || Value::known(F::from(i as u64)), )?; From afb1ef831e3e8c50d23835acb607cfdb4d90e9dd Mon Sep 17 00:00:00 2001 From: lightsing Date: Tue, 1 Aug 2023 13:09:47 +0800 Subject: [PATCH 04/17] replace u16_table of tx_circuit --- zkevm-circuits/src/super_circuit.rs | 1 + zkevm-circuits/src/tx_circuit.rs | 42 ++++++++-------------------- zkevm-circuits/src/tx_circuit/dev.rs | 13 ++++++++- 3 files changed, 25 insertions(+), 31 deletions(-) diff --git a/zkevm-circuits/src/super_circuit.rs b/zkevm-circuits/src/super_circuit.rs index d2a54677f3..cec492be41 100644 --- a/zkevm-circuits/src/super_circuit.rs +++ b/zkevm-circuits/src/super_circuit.rs @@ -251,6 +251,7 @@ impl SubCircuitConfig for SuperCircuitConfig { keccak_table: keccak_table.clone(), rlp_table, sig_table, + u16_table, challenges: challenges_expr.clone(), }, ); diff --git a/zkevm-circuits/src/tx_circuit.rs b/zkevm-circuits/src/tx_circuit.rs index 88289fbe14..fb0a43290c 100644 --- a/zkevm-circuits/src/tx_circuit.rs +++ b/zkevm-circuits/src/tx_circuit.rs @@ -53,12 +53,12 @@ use crate::{ }; #[cfg(feature = "onephase")] use halo2_proofs::plonk::FirstPhase as SecondPhase; +use halo2_proofs::plonk::Fixed; #[cfg(not(feature = "onephase"))] use halo2_proofs::plonk::SecondPhase; -use halo2_proofs::plonk::{Fixed, TableColumn}; use crate::{ - table::{BlockContextFieldTag::CumNumTxs, TxFieldTag::ChainID}, + table::{BlockContextFieldTag::CumNumTxs, RangeTable, TxFieldTag::ChainID}, util::rlc_be_bytes, witness::{ Format::{L1MsgHash, TxHashEip155, TxHashPreEip155, TxSignEip155, TxSignPreEip155}, @@ -106,7 +106,7 @@ pub struct TxCircuitConfig { // Whether tag's RLP-encoded value is 0x80 = rlp([]) is_none: Column, - u16_table: TableColumn, + u16_table: RangeTable<{ 1 << 16 }>, /// Verify if the tx_id is zero or not. tx_id_is_zero: IsZeroConfig, @@ -165,6 +165,8 @@ pub struct TxCircuitConfigArgs { pub rlp_table: RlpTable, /// SigTable pub sig_table: SigTable, + /// Reusable u16 lookup table, + pub u16_table: RangeTable<{ 1 << 16 }>, /// Challenges pub challenges: crate::util::Challenges>, } @@ -181,6 +183,7 @@ impl SubCircuitConfig for TxCircuitConfig { keccak_table, rlp_table, sig_table, + u16_table, challenges: _, }: Self::ConfigArgs, ) -> Self { @@ -201,9 +204,6 @@ impl SubCircuitConfig for TxCircuitConfig { let is_final = meta.advice_column(); let calldata_gas_cost_acc = meta.advice_column(); - // fixed column for showing (tx_id' - tx_id) < 2^16 - let u16_table = meta.lookup_table_column(); - // booleans to reduce degree let is_l1_msg = meta.advice_column(); let is_calldata = meta.advice_column(); @@ -732,7 +732,7 @@ impl SubCircuitConfig for TxCircuitConfig { |meta| meta.query_advice(tx_table.tx_id, Rotation::next()), ); - meta.lookup("tx_id_diff must in u16", |meta| { + meta.lookup_any("tx_id_diff must in u16", |meta| { let q_enable = meta.query_fixed(q_enable, Rotation::next()); let is_calldata = meta.query_advice(is_calldata, Rotation::cur()); let tx_id = meta.query_advice(tx_table.tx_id, Rotation::cur()); @@ -742,7 +742,11 @@ impl SubCircuitConfig for TxCircuitConfig { let lookup_condition = and::expr([q_enable, is_calldata, not::expr(tx_id_next_is_zero)]); - vec![(lookup_condition * (tx_id_next - tx_id), u16_table)] + vec![tx_id_next - tx_id] + .into_iter() + .zip(u16_table.table_exprs(meta).into_iter()) + .map(|(arg, table)| (lookup_condition.expr() * arg, table)) + .collect() }); meta.create_gate("tx call data bytes", |meta| { @@ -1173,26 +1177,6 @@ impl TxCircuitConfig { }); } - /// Load ECDSA RangeChip table. - pub fn load_aux_tables(&self, layouter: &mut impl Layouter) -> Result<(), Error> { - layouter.assign_table( - || "u16 fixed table", - |mut table| { - for i in 0..(1 << 16) { - table.assign_cell( - || format!("u16_row_{i}"), - self.u16_table, - i, - || Value::known(F::from(i as u64)), - )?; - } - Ok(()) - }, - )?; - - Ok(()) - } - /// Assigns a tx circuit row and returns the assigned cell of the value in /// the row. #[allow(clippy::too_many_arguments)] @@ -2044,8 +2028,6 @@ impl SubCircuit for TxCircuit { }) .collect::, Error>>()?; - config.load_aux_tables(layouter)?; - // check if tx.caller_address == recovered_pk let recovered_pks = keccak_inputs_sign_verify(&sign_datas) .into_iter() diff --git a/zkevm-circuits/src/tx_circuit/dev.rs b/zkevm-circuits/src/tx_circuit/dev.rs index 8ed52c4f35..86bf973717 100644 --- a/zkevm-circuits/src/tx_circuit/dev.rs +++ b/zkevm-circuits/src/tx_circuit/dev.rs @@ -6,7 +6,7 @@ pub use super::TxCircuit; use crate::{ sig_circuit::{SigCircuit, SigCircuitConfig, SigCircuitConfigArgs}, - table::{BlockTable, KeccakTable, RlpFsmRlpTable as RlpTable, SigTable, TxTable}, + table::{BlockTable, KeccakTable, RangeTable, RlpFsmRlpTable as RlpTable, SigTable, TxTable}, tx_circuit::{TxCircuitConfig, TxCircuitConfigArgs}, util::{Challenges, SubCircuit, SubCircuitConfig}, witness::Transaction, @@ -29,6 +29,8 @@ pub struct TxCircuitTesterConfigArgs { pub keccak_table: KeccakTable, /// SigTable pub sig_table: SigTable, + /// u16 lookup table, + pub u16_table: RangeTable<{ 1 << 16 }>, /// Challenges pub challenges: Challenges>, } @@ -39,6 +41,8 @@ pub struct TxCircuitTesterConfig { tx_config: TxCircuitConfig, // SigTable is assigned inside SigCircuit sig_config: SigCircuitConfig, + /// u16 lookup table, + pub u16_table: RangeTable<{ 1 << 16 }>, } impl SubCircuitConfig for TxCircuitTesterConfig { @@ -52,6 +56,7 @@ impl SubCircuitConfig for TxCircuitTesterConfig { keccak_table, rlp_table, sig_table, + u16_table, challenges, }: Self::ConfigArgs, ) -> Self { @@ -71,12 +76,14 @@ impl SubCircuitConfig for TxCircuitTesterConfig { tx_table, keccak_table, rlp_table, + u16_table, challenges, }, ); TxCircuitTesterConfig { tx_config, sig_config, + u16_table, } } } @@ -143,6 +150,7 @@ impl Circuit for TxCircuitTester { let keccak_table = KeccakTable::construct(meta); let rlp_table = RlpTable::construct(meta); let sig_table = SigTable::construct(meta); + let u16_table = RangeTable::construct(meta); let challenges = Challenges::construct(meta); let config = { @@ -163,12 +171,14 @@ impl Circuit for TxCircuitTester { tx_table, keccak_table, rlp_table, + u16_table, challenges, }, ); TxCircuitTesterConfig { tx_config, sig_config, + u16_table, } }; @@ -181,6 +191,7 @@ impl Circuit for TxCircuitTester { mut layouter: impl Layouter, ) -> Result<(), Error> { let challenges = challenges.values(&layouter); + config.u16_table.load(&mut layouter)?; let padding_txs = (self.tx_circuit.txs.len()..self.tx_circuit.max_txs) .into_iter() From 4ee21f7e41eced6314e74fe5676819d8d143b296 Mon Sep 17 00:00:00 2001 From: lightsing Date: Tue, 1 Aug 2023 13:15:45 +0800 Subject: [PATCH 05/17] use TableColumn --- zkevm-circuits/src/rlp_circuit_fsm.rs | 11 +++++------ zkevm-circuits/src/table.rs | 21 +++++++++------------ zkevm-circuits/src/tx_circuit.rs | 8 ++------ 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/zkevm-circuits/src/rlp_circuit_fsm.rs b/zkevm-circuits/src/rlp_circuit_fsm.rs index 66f842ade0..2912c8027b 100644 --- a/zkevm-circuits/src/rlp_circuit_fsm.rs +++ b/zkevm-circuits/src/rlp_circuit_fsm.rs @@ -538,17 +538,16 @@ impl RlpCircuitConfig { cb.gate(meta.query_fixed(q_enabled, Rotation::cur())) }); - meta.lookup_any("byte value check", |meta| { + meta.lookup("byte value check", |meta| { let cond = and::expr([ meta.query_fixed(q_enabled, Rotation::cur()), is_padding_in_dt.expr(Rotation::cur())(meta), ]); - vec![meta.query_advice(data_table.byte_value, Rotation::cur())] - .into_iter() - .zip(u8_table.table_exprs(meta).into_iter()) - .map(|(arg, table)| (cond.expr() * arg, table)) - .collect() + vec![( + cond * meta.query_advice(data_table.byte_value, Rotation::cur()), + u8_table.into(), + )] }); debug_assert!(meta.degree() <= 9); diff --git a/zkevm-circuits/src/table.rs b/zkevm-circuits/src/table.rs index 072f76e209..64d6158dfc 100644 --- a/zkevm-circuits/src/table.rs +++ b/zkevm-circuits/src/table.rs @@ -45,6 +45,7 @@ use halo2_proofs::plonk::FirstPhase as SecondPhase; #[cfg(not(feature = "onephase"))] use halo2_proofs::plonk::SecondPhase; +use halo2_proofs::plonk::TableColumn; use itertools::Itertools; use keccak256::plain::Keccak; use std::array; @@ -2681,21 +2682,21 @@ impl LookupTable for PowOfRandTable { /// Lookup table for [0, MAX) range #[derive(Clone, Copy, Debug)] -pub struct RangeTable(Column); +pub struct RangeTable(TableColumn); impl RangeTable { /// Construct the range table. pub fn construct(meta: &mut ConstraintSystem) -> Self { - Self(meta.fixed_column()) + Self(meta.lookup_table_column()) } /// Assign values to the table. pub fn load(&self, layouter: &mut impl Layouter) -> Result<(), Error> { - layouter.assign_region( + layouter.assign_table( || format!("range table [0, {MAX})"), - |mut region| { + |mut table| { for i in 0..MAX { - region.assign_fixed( + table.assign_cell( || format!("range at offset = {i}"), self.0, i, @@ -2709,12 +2710,8 @@ impl RangeTable { } } -impl LookupTable for RangeTable { - fn columns(&self) -> Vec> { - vec![self.0.into()] - } - - fn annotations(&self) -> Vec { - vec![format!("value in [0, {MAX})")] +impl From> for TableColumn { + fn from(table: RangeTable) -> TableColumn { + table.0 } } diff --git a/zkevm-circuits/src/tx_circuit.rs b/zkevm-circuits/src/tx_circuit.rs index fb0a43290c..4c2bb7a0d4 100644 --- a/zkevm-circuits/src/tx_circuit.rs +++ b/zkevm-circuits/src/tx_circuit.rs @@ -732,7 +732,7 @@ impl SubCircuitConfig for TxCircuitConfig { |meta| meta.query_advice(tx_table.tx_id, Rotation::next()), ); - meta.lookup_any("tx_id_diff must in u16", |meta| { + meta.lookup("tx_id_diff must in u16", |meta| { let q_enable = meta.query_fixed(q_enable, Rotation::next()); let is_calldata = meta.query_advice(is_calldata, Rotation::cur()); let tx_id = meta.query_advice(tx_table.tx_id, Rotation::cur()); @@ -742,11 +742,7 @@ impl SubCircuitConfig for TxCircuitConfig { let lookup_condition = and::expr([q_enable, is_calldata, not::expr(tx_id_next_is_zero)]); - vec![tx_id_next - tx_id] - .into_iter() - .zip(u16_table.table_exprs(meta).into_iter()) - .map(|(arg, table)| (lookup_condition.expr() * arg, table)) - .collect() + vec![(lookup_condition * (tx_id_next - tx_id), u16_table.into())] }); meta.create_gate("tx call data bytes", |meta| { From da73ccd47df608d5142588ea200553fe37f69e4d Mon Sep 17 00:00:00 2001 From: lightsing Date: Tue, 1 Aug 2023 15:26:06 +0800 Subject: [PATCH 06/17] merge #694 --- gadgets/src/comparator.rs | 13 ++++--- gadgets/src/less_than.rs | 43 ++++++++++++----------- zkevm-circuits/src/rlp_circuit_fsm.rs | 11 ++++++ zkevm-circuits/src/rlp_circuit_fsm/dev.rs | 2 ++ zkevm-circuits/src/super_circuit.rs | 1 + zkevm-circuits/src/tx_circuit.rs | 1 + 6 files changed, 46 insertions(+), 25 deletions(-) diff --git a/gadgets/src/comparator.rs b/gadgets/src/comparator.rs index cd39e38c51..395db41578 100644 --- a/gadgets/src/comparator.rs +++ b/gadgets/src/comparator.rs @@ -5,13 +5,14 @@ use eth_types::Field; use halo2_proofs::{ arithmetic::FieldExt, circuit::{Chip, Region, Value}, - plonk::{ConstraintSystem, Error, Expression, VirtualCells}, + plonk::{ConstraintSystem, Error, Expression, TableColumn, VirtualCells}, poly::Rotation, }; -use crate::{is_equal::IsEqualInstruction, less_than::LtInstruction}; - -use super::{is_equal::IsEqualChip, less_than::LtChip}; +use crate::{ + is_equal::{IsEqualChip, IsEqualInstruction}, + less_than::{LtChip, LtInstruction}, +}; /// Instruction that the Comparator chip needs to implement. pub trait ComparatorInstruction { @@ -61,8 +62,10 @@ impl ComparatorChip { q_enable: impl FnOnce(&mut VirtualCells) -> Expression + Clone, lhs: impl FnOnce(&mut VirtualCells) -> Expression + Clone, rhs: impl FnOnce(&mut VirtualCells) -> Expression + Clone, + u16_table: TableColumn, ) -> ComparatorConfig { - let lt_config = LtChip::configure(meta, q_enable.clone(), lhs.clone(), rhs.clone()); + let lt_config = + LtChip::configure(meta, q_enable.clone(), lhs.clone(), rhs.clone(), u16_table); let eq_config = IsEqualChip::configure(meta, q_enable, lhs, rhs); ComparatorConfig { diff --git a/gadgets/src/less_than.rs b/gadgets/src/less_than.rs index a01720f675..37921142af 100644 --- a/gadgets/src/less_than.rs +++ b/gadgets/src/less_than.rs @@ -3,8 +3,8 @@ use eth_types::Field; use halo2_proofs::{ arithmetic::FieldExt, - circuit::{Chip, Layouter, Region, Value}, - plonk::{Advice, Column, ConstraintSystem, Error, Expression, Fixed, VirtualCells}, + circuit::{Chip, Region, Value}, + plonk::{Advice, Column, ConstraintSystem, Error, Expression, TableColumn, VirtualCells}, poly::Rotation, }; @@ -24,8 +24,9 @@ pub trait LtInstruction { rhs: F, ) -> Result<(), Error>; - /// Load the u8x2 lookup table. - fn load(&self, layouter: &mut impl Layouter) -> Result<(), Error>; + #[cfg(test)] + /// Load the u16 lookup table. + fn dev_load(&self, layouter: &mut impl Layouter) -> Result<(), Error>; } /// Config for the Lt chip. @@ -36,7 +37,7 @@ pub struct LtConfig { /// Denotes the bytes representation of the difference between lhs and rhs. pub diff: [Column; N_BYTES], /// Denotes the range within which each byte should lie. - pub u8x2: Column, + pub u16_table: TableColumn, /// Denotes the range within which both lhs and rhs lie. pub range: F, } @@ -67,10 +68,10 @@ impl LtChip { q_enable: impl FnOnce(&mut VirtualCells<'_, F>) -> Expression, lhs: impl FnOnce(&mut VirtualCells) -> Expression, rhs: impl FnOnce(&mut VirtualCells) -> Expression, + u16_table: TableColumn, ) -> LtConfig { let lt = meta.advice_column(); let diff = [(); N_BYTES].map(|_| meta.advice_column()); - let u8x2 = meta.fixed_column(); let range = pow_of_two(N_BYTES * 8); meta.create_gate("lt gate", |meta| { @@ -92,10 +93,8 @@ impl LtChip { .map(move |poly| q_enable.clone() * poly) }); - meta.annotate_lookup_any_column(u8x2, || "LOOKUP_u8x2"); - for cell_columns in diff.chunks(2) { - meta.lookup_any("range check for u8x2", |meta| { + meta.lookup("range check for u16", |meta| { let cell_expr = if cell_columns.len() == 2 { meta.query_advice(cell_columns[0], Rotation::cur()) * Expression::Constant(pow_of_two(8)) @@ -104,15 +103,14 @@ impl LtChip { meta.query_advice(cell_columns[0], Rotation::cur()) * Expression::Constant(pow_of_two(8)) }; - let u8x2_range = meta.query_fixed(u8x2, Rotation::cur()); - vec![(cell_expr, u8x2_range)] + vec![(cell_expr, u16_table)] }); } LtConfig { lt, diff, - u8x2, + u16_table, range, } } @@ -155,16 +153,17 @@ impl LtInstruction for LtChip { Ok(()) } - fn load(&self, layouter: &mut impl Layouter) -> Result<(), Error> { + #[cfg(test)] + fn dev_load(&self, layouter: &mut impl Layouter) -> Result<(), Error> { const RANGE: usize = u16::MAX as usize; - layouter.assign_region( - || "load u8x2 range check table", - |mut region| { + layouter.assign_table( + || "load u16 range check table", + |mut table| { for i in 0..=RANGE { - region.assign_fixed( + table.assign_cell( || "assign cell in fixed column", - self.config.u8x2, + self.config.u16_table, i, || Value::known(F::from(i as u64)), )?; @@ -266,12 +265,14 @@ mod test { let q_enable = meta.complex_selector(); let value = meta.advice_column(); let check = meta.advice_column(); + let u16_table = meta.lookup_table_column(); let lt = LtChip::configure( meta, |meta| meta.query_selector(q_enable), |meta| meta.query_advice(value, Rotation::prev()), |meta| meta.query_advice(value, Rotation::cur()), + u16_table, ); let config = Self::Config { @@ -309,7 +310,7 @@ mod test { let (first_value, values) = values.split_at(1); let first_value = first_value[0]; - chip.load(&mut layouter)?; + chip.dev_load(&mut layouter)?; layouter.assign_region( || "witness", @@ -386,12 +387,14 @@ mod test { let q_enable = meta.complex_selector(); let (value_a, value_b) = (meta.advice_column(), meta.advice_column()); let check = meta.advice_column(); + let u16_table = meta.lookup_table_column(); let lt = LtChip::configure( meta, |meta| meta.query_selector(q_enable), |meta| meta.query_advice(value_a, Rotation::cur()), |meta| meta.query_advice(value_b, Rotation::cur()), + u16_table, ); let config = Self::Config { @@ -433,7 +436,7 @@ mod test { .ok_or(Error::Synthesis)?; let checks = self.checks.as_ref().ok_or(Error::Synthesis)?; - chip.load(&mut layouter)?; + chip.dev_load(&mut layouter)?; layouter.assign_region( || "witness", diff --git a/zkevm-circuits/src/rlp_circuit_fsm.rs b/zkevm-circuits/src/rlp_circuit_fsm.rs index 2912c8027b..06dc452564 100644 --- a/zkevm-circuits/src/rlp_circuit_fsm.rs +++ b/zkevm-circuits/src/rlp_circuit_fsm.rs @@ -281,6 +281,8 @@ pub struct RlpCircuitConfig { rom_table: RlpFsmRomTable, /// Range256 table u8_table: RangeTable<{ 1 << 8 }>, + /// Range u16 table + u16_table: RangeTable<{ 1 << 16 }>, } impl RlpCircuitConfig { @@ -290,6 +292,7 @@ impl RlpCircuitConfig { rom_table: RlpFsmRomTable, data_table: RlpFsmDataTable, u8_table: RangeTable<{ 1 << 8 }>, + u16_table: RangeTable<{ 1 << 16 }>, rlp_table: RlpFsmRlpTable, challenges: &Challenges>, ) -> Self { @@ -619,6 +622,7 @@ impl RlpCircuitConfig { cmp_enabled, |meta| meta.query_advice(byte_value, Rotation::cur()), |_| $value.expr(), + u16_table.into(), ); }; } @@ -629,6 +633,7 @@ impl RlpCircuitConfig { cmp_enabled, |_| $value.expr(), |meta| meta.query_advice(byte_value, Rotation::cur()), + u16_table.into(), ); }; } @@ -711,12 +716,14 @@ impl RlpCircuitConfig { cmp_enabled, |meta| meta.query_advice(tag_idx, Rotation::cur()), |meta| meta.query_advice(tag_length, Rotation::cur()), + u16_table.into(), ); let mlength_lte_0x20 = ComparatorChip::configure( meta, cmp_enabled, |meta| meta.query_advice(max_length, Rotation::cur()), |_meta| 0x20.expr(), + u16_table.into(), ); let depth_check = IsEqualChip::configure( meta, @@ -1361,6 +1368,7 @@ impl RlpCircuitConfig { data_table, rom_table, u8_table, + u16_table, } } @@ -1775,6 +1783,8 @@ pub struct RlpCircuitConfigArgs { pub rlp_table: RlpFsmRlpTable, /// u8 table pub u8_table: RangeTable<{ 1 << 8 }>, + /// u16 table + pub u16_table: RangeTable<{ 1 << 16 }>, /// Challenge API. pub challenges: Challenges>, } @@ -1791,6 +1801,7 @@ impl SubCircuitConfig for RlpCircuitConfig { rom_table, data_table, args.u8_table, + args.u16_table, args.rlp_table, &args.challenges, ) diff --git a/zkevm-circuits/src/rlp_circuit_fsm/dev.rs b/zkevm-circuits/src/rlp_circuit_fsm/dev.rs index c7bf84cdb3..bc6fa3c203 100644 --- a/zkevm-circuits/src/rlp_circuit_fsm/dev.rs +++ b/zkevm-circuits/src/rlp_circuit_fsm/dev.rs @@ -23,12 +23,14 @@ impl Circuit for RlpCircuit { let challenges = Challenges::construct(meta); let challenge_exprs = challenges.exprs(meta); let u8_table = RangeTable::construct(meta); + let u16_table = RangeTable::construct(meta); let config = RlpCircuitConfig::new( meta, RlpCircuitConfigArgs { rlp_table, u8_table, + u16_table, challenges: challenge_exprs, }, ); diff --git a/zkevm-circuits/src/super_circuit.rs b/zkevm-circuits/src/super_circuit.rs index 669d1c06e4..f917527d0b 100644 --- a/zkevm-circuits/src/super_circuit.rs +++ b/zkevm-circuits/src/super_circuit.rs @@ -228,6 +228,7 @@ impl SubCircuitConfig for SuperCircuitConfig { RlpCircuitConfigArgs { rlp_table, u8_table, + u16_table, challenges: challenges_expr.clone(), }, ); diff --git a/zkevm-circuits/src/tx_circuit.rs b/zkevm-circuits/src/tx_circuit.rs index 4c2bb7a0d4..d2c730e974 100644 --- a/zkevm-circuits/src/tx_circuit.rs +++ b/zkevm-circuits/src/tx_circuit.rs @@ -686,6 +686,7 @@ impl SubCircuitConfig for TxCircuitConfig { |meta| meta.query_fixed(q_enable, Rotation::cur()), |meta| meta.query_advice(tx_table.tx_id, Rotation::cur()), |meta| meta.query_advice(cum_num_txs, Rotation::cur()), + u16_table.into(), ); meta.create_gate("tx_id <= cum_num_txs", |meta| { From 12f64eb34c1ddafe409964c173e6e04d41ddf29f Mon Sep 17 00:00:00 2001 From: lightsing Date: Tue, 1 Aug 2023 15:32:19 +0800 Subject: [PATCH 07/17] add type alias --- zkevm-circuits/src/rlp_circuit_fsm.rs | 8 ++++---- zkevm-circuits/src/super_circuit.rs | 12 ++++++------ zkevm-circuits/src/table.rs | 5 +++++ zkevm-circuits/src/tx_circuit.rs | 6 +++--- zkevm-circuits/src/tx_circuit/dev.rs | 8 ++++---- 5 files changed, 22 insertions(+), 17 deletions(-) diff --git a/zkevm-circuits/src/rlp_circuit_fsm.rs b/zkevm-circuits/src/rlp_circuit_fsm.rs index 2912c8027b..3e69709b81 100644 --- a/zkevm-circuits/src/rlp_circuit_fsm.rs +++ b/zkevm-circuits/src/rlp_circuit_fsm.rs @@ -27,7 +27,7 @@ use strum::IntoEnumIterator; use crate::{ evm_circuit::util::constraint_builder::{BaseConstraintBuilder, ConstrainBuilderCommon}, - table::{LookupTable, RangeTable, RlpFsmRlpTable}, + table::{LookupTable, RlpFsmRlpTable, U8Table}, util::{Challenges, SubCircuit, SubCircuitConfig}, witness::{ Block, DataTable, Format, RlpFsmWitnessGen, RlpFsmWitnessRow, RlpTag, RomTableRow, State, @@ -280,7 +280,7 @@ pub struct RlpCircuitConfig { /// ROM table rom_table: RlpFsmRomTable, /// Range256 table - u8_table: RangeTable<{ 1 << 8 }>, + u8_table: U8Table, } impl RlpCircuitConfig { @@ -289,7 +289,7 @@ impl RlpCircuitConfig { meta: &mut ConstraintSystem, rom_table: RlpFsmRomTable, data_table: RlpFsmDataTable, - u8_table: RangeTable<{ 1 << 8 }>, + u8_table: U8Table, rlp_table: RlpFsmRlpTable, challenges: &Challenges>, ) -> Self { @@ -1774,7 +1774,7 @@ pub struct RlpCircuitConfigArgs { /// RLP table. pub rlp_table: RlpFsmRlpTable, /// u8 table - pub u8_table: RangeTable<{ 1 << 8 }>, + pub u8_table: U8Table, /// Challenge API. pub challenges: Challenges>, } diff --git a/zkevm-circuits/src/super_circuit.rs b/zkevm-circuits/src/super_circuit.rs index 669d1c06e4..bc7bc53a18 100644 --- a/zkevm-circuits/src/super_circuit.rs +++ b/zkevm-circuits/src/super_circuit.rs @@ -83,8 +83,8 @@ use crate::{ state_circuit::{StateCircuit, StateCircuitConfig, StateCircuitConfigArgs}, table::{ BlockTable, BytecodeTable, CopyTable, EccTable, ExpTable, KeccakTable, ModExpTable, - MptTable, PoseidonTable, PowOfRandTable, RangeTable, RlpFsmRlpTable as RlpTable, RwTable, - SigTable, TxTable, + MptTable, PoseidonTable, PowOfRandTable, RlpFsmRlpTable as RlpTable, RwTable, SigTable, + TxTable, U16Table, U8Table, }, }; @@ -116,8 +116,8 @@ pub struct SuperCircuitConfig { rlp_table: RlpTable, tx_table: TxTable, poseidon_table: PoseidonTable, - u8_table: RangeTable<{ 1 << 8 }>, - u16_table: RangeTable<{ 1 << 16 }>, + u8_table: U8Table, + u16_table: U16Table, evm_circuit: EvmCircuitConfig, state_circuit: StateCircuitConfig, tx_circuit: TxCircuitConfig, @@ -205,9 +205,9 @@ impl SubCircuitConfig for SuperCircuitConfig { let pow_of_rand_table = PowOfRandTable::construct(meta, &challenges_expr); log_circuit_info(meta, "power of randomness table"); - let u8_table = RangeTable::construct(meta); + let u8_table = U8Table::construct(meta); log_circuit_info(meta, "u8 table"); - let u16_table = RangeTable::construct(meta); + let u16_table = U16Table::construct(meta); log_circuit_info(meta, "u16 table"); let keccak_circuit = KeccakCircuitConfig::new( diff --git a/zkevm-circuits/src/table.rs b/zkevm-circuits/src/table.rs index 43eec05bf9..18e1b129fe 100644 --- a/zkevm-circuits/src/table.rs +++ b/zkevm-circuits/src/table.rs @@ -2870,6 +2870,11 @@ impl LookupTable for PowOfRandTable { #[derive(Clone, Copy, Debug)] pub struct RangeTable(TableColumn); +/// Type Alias of u8 table, [0, 1 << 8) +pub type U8Table = RangeTable<{ 1 << 8 }>; +/// Type Alias of u16 table, [0, 1 << 16) +pub type U16Table = RangeTable<{ 1 << 16 }>; + impl RangeTable { /// Construct the range table. pub fn construct(meta: &mut ConstraintSystem) -> Self { diff --git a/zkevm-circuits/src/tx_circuit.rs b/zkevm-circuits/src/tx_circuit.rs index 4c2bb7a0d4..c355430e82 100644 --- a/zkevm-circuits/src/tx_circuit.rs +++ b/zkevm-circuits/src/tx_circuit.rs @@ -58,7 +58,7 @@ use halo2_proofs::plonk::Fixed; use halo2_proofs::plonk::SecondPhase; use crate::{ - table::{BlockContextFieldTag::CumNumTxs, RangeTable, TxFieldTag::ChainID}, + table::{BlockContextFieldTag::CumNumTxs, TxFieldTag::ChainID, U16Table}, util::rlc_be_bytes, witness::{ Format::{L1MsgHash, TxHashEip155, TxHashPreEip155, TxSignEip155, TxSignPreEip155}, @@ -106,7 +106,7 @@ pub struct TxCircuitConfig { // Whether tag's RLP-encoded value is 0x80 = rlp([]) is_none: Column, - u16_table: RangeTable<{ 1 << 16 }>, + u16_table: U16Table, /// Verify if the tx_id is zero or not. tx_id_is_zero: IsZeroConfig, @@ -166,7 +166,7 @@ pub struct TxCircuitConfigArgs { /// SigTable pub sig_table: SigTable, /// Reusable u16 lookup table, - pub u16_table: RangeTable<{ 1 << 16 }>, + pub u16_table: U16Table, /// Challenges pub challenges: crate::util::Challenges>, } diff --git a/zkevm-circuits/src/tx_circuit/dev.rs b/zkevm-circuits/src/tx_circuit/dev.rs index 86bf973717..6f8c16886a 100644 --- a/zkevm-circuits/src/tx_circuit/dev.rs +++ b/zkevm-circuits/src/tx_circuit/dev.rs @@ -6,7 +6,7 @@ pub use super::TxCircuit; use crate::{ sig_circuit::{SigCircuit, SigCircuitConfig, SigCircuitConfigArgs}, - table::{BlockTable, KeccakTable, RangeTable, RlpFsmRlpTable as RlpTable, SigTable, TxTable}, + table::{BlockTable, KeccakTable, RlpFsmRlpTable as RlpTable, SigTable, TxTable, U16Table}, tx_circuit::{TxCircuitConfig, TxCircuitConfigArgs}, util::{Challenges, SubCircuit, SubCircuitConfig}, witness::Transaction, @@ -30,7 +30,7 @@ pub struct TxCircuitTesterConfigArgs { /// SigTable pub sig_table: SigTable, /// u16 lookup table, - pub u16_table: RangeTable<{ 1 << 16 }>, + pub u16_table: U16Table, /// Challenges pub challenges: Challenges>, } @@ -42,7 +42,7 @@ pub struct TxCircuitTesterConfig { // SigTable is assigned inside SigCircuit sig_config: SigCircuitConfig, /// u16 lookup table, - pub u16_table: RangeTable<{ 1 << 16 }>, + pub u16_table: U16Table, } impl SubCircuitConfig for TxCircuitTesterConfig { @@ -150,7 +150,7 @@ impl Circuit for TxCircuitTester { let keccak_table = KeccakTable::construct(meta); let rlp_table = RlpTable::construct(meta); let sig_table = SigTable::construct(meta); - let u16_table = RangeTable::construct(meta); + let u16_table = U16Table::construct(meta); let challenges = Challenges::construct(meta); let config = { From 8950effaeb92b7f257e58294fac7ef5d2f6ab670 Mon Sep 17 00:00:00 2001 From: lightsing Date: Tue, 1 Aug 2023 15:33:40 +0800 Subject: [PATCH 08/17] use type alias --- zkevm-circuits/src/rlp_circuit_fsm/dev.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zkevm-circuits/src/rlp_circuit_fsm/dev.rs b/zkevm-circuits/src/rlp_circuit_fsm/dev.rs index c7bf84cdb3..6926c15425 100644 --- a/zkevm-circuits/src/rlp_circuit_fsm/dev.rs +++ b/zkevm-circuits/src/rlp_circuit_fsm/dev.rs @@ -1,6 +1,6 @@ use crate::{ rlp_circuit_fsm::{RlpCircuit, RlpCircuitConfig, RlpCircuitConfigArgs}, - table::{RangeTable, RlpFsmRlpTable}, + table::{RlpFsmRlpTable, U8Table}, util::{Challenges, SubCircuit, SubCircuitConfig}, witness::Transaction, }; @@ -22,7 +22,7 @@ impl Circuit for RlpCircuit { let rlp_table = RlpFsmRlpTable::construct(meta); let challenges = Challenges::construct(meta); let challenge_exprs = challenges.exprs(meta); - let u8_table = RangeTable::construct(meta); + let u8_table = U8Table::construct(meta); let config = RlpCircuitConfig::new( meta, From 183673ac892c550b2f3480e1f9b4d7d081219509 Mon Sep 17 00:00:00 2001 From: lightsing Date: Tue, 1 Aug 2023 15:43:54 +0800 Subject: [PATCH 09/17] fix import --- gadgets/src/less_than.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/gadgets/src/less_than.rs b/gadgets/src/less_than.rs index 37921142af..7e79e9e781 100644 --- a/gadgets/src/less_than.rs +++ b/gadgets/src/less_than.rs @@ -26,7 +26,8 @@ pub trait LtInstruction { #[cfg(test)] /// Load the u16 lookup table. - fn dev_load(&self, layouter: &mut impl Layouter) -> Result<(), Error>; + fn dev_load(&self, layouter: &mut impl halo2_proofs::circuit::Layouter) + -> Result<(), Error>; } /// Config for the Lt chip. @@ -154,7 +155,10 @@ impl LtInstruction for LtChip { } #[cfg(test)] - fn dev_load(&self, layouter: &mut impl Layouter) -> Result<(), Error> { + fn dev_load( + &self, + layouter: &mut impl halo2_proofs::circuit::Layouter, + ) -> Result<(), Error> { const RANGE: usize = u16::MAX as usize; layouter.assign_table( From 24e3cf20a1db4d98f9aba97a3dcfa9e9e09d6a15 Mon Sep 17 00:00:00 2001 From: lightsing Date: Tue, 1 Aug 2023 16:25:51 +0800 Subject: [PATCH 10/17] missing q_enable --- gadgets/src/less_than.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/gadgets/src/less_than.rs b/gadgets/src/less_than.rs index 7e79e9e781..5db2233ac2 100644 --- a/gadgets/src/less_than.rs +++ b/gadgets/src/less_than.rs @@ -66,7 +66,7 @@ impl LtChip { /// Configures the Lt chip. pub fn configure( meta: &mut ConstraintSystem, - q_enable: impl FnOnce(&mut VirtualCells<'_, F>) -> Expression, + q_enable: impl FnOnce(&mut VirtualCells<'_, F>) -> Expression + Clone, lhs: impl FnOnce(&mut VirtualCells) -> Expression, rhs: impl FnOnce(&mut VirtualCells) -> Expression, u16_table: TableColumn, @@ -76,7 +76,7 @@ impl LtChip { let range = pow_of_two(N_BYTES * 8); meta.create_gate("lt gate", |meta| { - let q_enable = q_enable(meta); + let q_enable = q_enable.clone()(meta); let lt = meta.query_advice(lt, Rotation::cur()); let diff_bytes = diff @@ -96,6 +96,7 @@ impl LtChip { for cell_columns in diff.chunks(2) { meta.lookup("range check for u16", |meta| { + let q_enable = q_enable.clone()(meta); let cell_expr = if cell_columns.len() == 2 { meta.query_advice(cell_columns[0], Rotation::cur()) * Expression::Constant(pow_of_two(8)) @@ -104,7 +105,7 @@ impl LtChip { meta.query_advice(cell_columns[0], Rotation::cur()) * Expression::Constant(pow_of_two(8)) }; - vec![(cell_expr, u16_table)] + vec![(q_enable * cell_expr, u16_table)] }); } From 76094d13bc7268a97bcbe41f1744d303b791abeb Mon Sep 17 00:00:00 2001 From: lightsing Date: Tue, 1 Aug 2023 20:05:38 +0800 Subject: [PATCH 11/17] annotate lookup column --- zkevm-circuits/src/table.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/zkevm-circuits/src/table.rs b/zkevm-circuits/src/table.rs index 34156db772..f9a76298eb 100644 --- a/zkevm-circuits/src/table.rs +++ b/zkevm-circuits/src/table.rs @@ -2884,7 +2884,9 @@ pub type U16Table = RangeTable<{ 1 << 16 }>; impl RangeTable { /// Construct the range table. pub fn construct(meta: &mut ConstraintSystem) -> Self { - Self(meta.lookup_table_column()) + let inner = meta.lookup_table_column(); + meta.annotate_lookup_column(inner, || format!("range table [0, {MAX})")); + Self(inner) } /// Assign values to the table. From 0852e77ad42b40199586c9e5ef93bb852f76fadd Mon Sep 17 00:00:00 2001 From: lightsing Date: Wed, 2 Aug 2023 10:12:30 +0800 Subject: [PATCH 12/17] fix dev table load --- zkevm-circuits/src/rlp_circuit_fsm/dev.rs | 1 + zkevm-circuits/src/rlp_circuit_fsm/test.rs | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/zkevm-circuits/src/rlp_circuit_fsm/dev.rs b/zkevm-circuits/src/rlp_circuit_fsm/dev.rs index fc0c40a570..4fe5cfd454 100644 --- a/zkevm-circuits/src/rlp_circuit_fsm/dev.rs +++ b/zkevm-circuits/src/rlp_circuit_fsm/dev.rs @@ -46,6 +46,7 @@ impl Circuit for RlpCircuit { ) -> Result<(), Error> { let challenges = &config.1.values(&layouter); config.0.u8_table.load(&mut layouter)?; + config.0.u16_table.load(&mut layouter)?; self.synthesize_sub(&config.0, challenges, &mut layouter) } diff --git a/zkevm-circuits/src/rlp_circuit_fsm/test.rs b/zkevm-circuits/src/rlp_circuit_fsm/test.rs index 91a6198c5d..04c72b4f6b 100644 --- a/zkevm-circuits/src/rlp_circuit_fsm/test.rs +++ b/zkevm-circuits/src/rlp_circuit_fsm/test.rs @@ -52,7 +52,7 @@ fn test_eip_155_tx() { _marker: Default::default(), }; - let mock_prover = MockProver::run(14, &rlp_circuit, vec![]); + let mock_prover = MockProver::run(17, &rlp_circuit, vec![]); assert!(mock_prover.is_ok()); let mock_prover = mock_prover.unwrap(); if let Err(errors) = mock_prover.verify_par() { @@ -72,7 +72,7 @@ fn test_pre_eip155_tx() { _marker: Default::default(), }; - let mock_prover = MockProver::run(16, &rlp_circuit, vec![]); + let mock_prover = MockProver::run(17, &rlp_circuit, vec![]); assert!(mock_prover.is_ok()); let mock_prover = mock_prover.unwrap(); if let Err(errors) = mock_prover.verify_par() { @@ -99,7 +99,7 @@ fn test_l1_msg_tx() { _marker: Default::default(), }; - let mock_prover = MockProver::run(16, &rlp_circuit, vec![]); + let mock_prover = MockProver::run(17, &rlp_circuit, vec![]); assert!(mock_prover.is_ok()); let mock_prover = mock_prover.unwrap(); @@ -126,7 +126,7 @@ fn test_eip1559_tx() { _marker: Default::default(), }; - let mock_prover = MockProver::run(16, &rlp_circuit, vec![]); + let mock_prover = MockProver::run(17, &rlp_circuit, vec![]); assert!(mock_prover.is_ok()); let mock_prover = mock_prover.unwrap(); if let Err(errors) = mock_prover.verify_par() { From 8c34319730581c8afa6fa5e64b44bdd792df45b9 Mon Sep 17 00:00:00 2001 From: lightsing Date: Fri, 4 Aug 2023 14:37:43 +0800 Subject: [PATCH 13/17] revert to u8 table lookup --- gadgets/src/comparator.rs | 4 +- gadgets/src/less_than.rs | 25 ++++----- zkevm-circuits/src/rlp_circuit_fsm.rs | 49 ++++++++---------- zkevm-circuits/src/rlp_circuit_fsm/dev.rs | 5 +- zkevm-circuits/src/super_circuit.rs | 2 +- zkevm-circuits/src/tx_circuit.rs | 62 +++++++++++++---------- zkevm-circuits/src/tx_circuit/dev.rs | 15 +++++- 7 files changed, 83 insertions(+), 79 deletions(-) diff --git a/gadgets/src/comparator.rs b/gadgets/src/comparator.rs index 395db41578..407198ca98 100644 --- a/gadgets/src/comparator.rs +++ b/gadgets/src/comparator.rs @@ -62,10 +62,10 @@ impl ComparatorChip { q_enable: impl FnOnce(&mut VirtualCells) -> Expression + Clone, lhs: impl FnOnce(&mut VirtualCells) -> Expression + Clone, rhs: impl FnOnce(&mut VirtualCells) -> Expression + Clone, - u16_table: TableColumn, + u8_table: TableColumn, ) -> ComparatorConfig { let lt_config = - LtChip::configure(meta, q_enable.clone(), lhs.clone(), rhs.clone(), u16_table); + LtChip::configure(meta, q_enable.clone(), lhs.clone(), rhs.clone(), u8_table); let eq_config = IsEqualChip::configure(meta, q_enable, lhs, rhs); ComparatorConfig { diff --git a/gadgets/src/less_than.rs b/gadgets/src/less_than.rs index 5db2233ac2..cd27a32488 100644 --- a/gadgets/src/less_than.rs +++ b/gadgets/src/less_than.rs @@ -38,7 +38,7 @@ pub struct LtConfig { /// Denotes the bytes representation of the difference between lhs and rhs. pub diff: [Column; N_BYTES], /// Denotes the range within which each byte should lie. - pub u16_table: TableColumn, + pub u8_table: TableColumn, /// Denotes the range within which both lhs and rhs lie. pub range: F, } @@ -69,7 +69,7 @@ impl LtChip { q_enable: impl FnOnce(&mut VirtualCells<'_, F>) -> Expression + Clone, lhs: impl FnOnce(&mut VirtualCells) -> Expression, rhs: impl FnOnce(&mut VirtualCells) -> Expression, - u16_table: TableColumn, + u8_table: TableColumn, ) -> LtConfig { let lt = meta.advice_column(); let diff = [(); N_BYTES].map(|_| meta.advice_column()); @@ -94,25 +94,20 @@ impl LtChip { .map(move |poly| q_enable.clone() * poly) }); - for cell_columns in diff.chunks(2) { - meta.lookup("range check for u16", |meta| { + for cell_column in diff { + meta.lookup("range check for u8", |meta| { let q_enable = q_enable.clone()(meta); - let cell_expr = if cell_columns.len() == 2 { - meta.query_advice(cell_columns[0], Rotation::cur()) - * Expression::Constant(pow_of_two(8)) - + (meta.query_advice(cell_columns[1], Rotation::cur())) - } else { - meta.query_advice(cell_columns[0], Rotation::cur()) - * Expression::Constant(pow_of_two(8)) - }; - vec![(q_enable * cell_expr, u16_table)] + vec![( + q_enable * meta.query_advice(cell_column, Rotation::cur()), + u8_table, + )] }); } LtConfig { lt, diff, - u16_table, + u8_table, range, } } @@ -168,7 +163,7 @@ impl LtInstruction for LtChip { for i in 0..=RANGE { table.assign_cell( || "assign cell in fixed column", - self.config.u16_table, + self.config.u8_table, i, || Value::known(F::from(i as u64)), )?; diff --git a/zkevm-circuits/src/rlp_circuit_fsm.rs b/zkevm-circuits/src/rlp_circuit_fsm.rs index 5fd816fc9d..5796a64017 100644 --- a/zkevm-circuits/src/rlp_circuit_fsm.rs +++ b/zkevm-circuits/src/rlp_circuit_fsm.rs @@ -5,9 +5,21 @@ mod dev; #[cfg(any(feature = "test", test))] mod test; -use std::marker::PhantomData; - -use crate::util::is_zero::{IsZeroChip, IsZeroConfig}; +use crate::{ + evm_circuit::util::constraint_builder::{BaseConstraintBuilder, ConstrainBuilderCommon}, + table::{LookupTable, RlpFsmRlpTable, U8Table}, + util::{ + is_zero::{IsZeroChip, IsZeroConfig}, + Challenges, SubCircuit, SubCircuitConfig, + }, + witness::{ + Block, DataTable, Format, RlpFsmWitnessGen, RlpFsmWitnessRow, RlpTag, RomTableRow, State, + State::{DecodeTagStart, End}, + Tag, + Tag::{BeginList, EndList, TxType}, + Transaction, + }, +}; use eth_types::Field; use gadgets::{ binary_number::{BinaryNumberChip, BinaryNumberConfig}, @@ -23,21 +35,9 @@ use halo2_proofs::{ poly::Rotation, }; use itertools::Itertools; +use std::marker::PhantomData; use strum::IntoEnumIterator; -use crate::{ - evm_circuit::util::constraint_builder::{BaseConstraintBuilder, ConstrainBuilderCommon}, - table::{LookupTable, RlpFsmRlpTable, U16Table, U8Table}, - util::{Challenges, SubCircuit, SubCircuitConfig}, - witness::{ - Block, DataTable, Format, RlpFsmWitnessGen, RlpFsmWitnessRow, RlpTag, RomTableRow, State, - State::{DecodeTagStart, End}, - Tag, - Tag::{BeginList, EndList, TxType}, - Transaction, - }, -}; - /// Data table allows us a lookup argument from the RLP circuit to check the byte value at an index /// while decoding a tx of a given format. #[derive(Clone, Copy, Debug)] @@ -279,10 +279,8 @@ pub struct RlpCircuitConfig { data_table: RlpFsmDataTable, /// ROM table rom_table: RlpFsmRomTable, - /// Range256 table + /// Range u8 table u8_table: U8Table, - /// Range u16 table - u16_table: U16Table, } impl RlpCircuitConfig { @@ -292,7 +290,6 @@ impl RlpCircuitConfig { rom_table: RlpFsmRomTable, data_table: RlpFsmDataTable, u8_table: U8Table, - u16_table: U16Table, rlp_table: RlpFsmRlpTable, challenges: &Challenges>, ) -> Self { @@ -622,7 +619,7 @@ impl RlpCircuitConfig { cmp_enabled, |meta| meta.query_advice(byte_value, Rotation::cur()), |_| $value.expr(), - u16_table.into(), + u8_table.into(), ); }; } @@ -633,7 +630,7 @@ impl RlpCircuitConfig { cmp_enabled, |_| $value.expr(), |meta| meta.query_advice(byte_value, Rotation::cur()), - u16_table.into(), + u8_table.into(), ); }; } @@ -716,14 +713,14 @@ impl RlpCircuitConfig { cmp_enabled, |meta| meta.query_advice(tag_idx, Rotation::cur()), |meta| meta.query_advice(tag_length, Rotation::cur()), - u16_table.into(), + u8_table.into(), ); let mlength_lte_0x20 = ComparatorChip::configure( meta, cmp_enabled, |meta| meta.query_advice(max_length, Rotation::cur()), |_meta| 0x20.expr(), - u16_table.into(), + u8_table.into(), ); let depth_check = IsEqualChip::configure( meta, @@ -1368,7 +1365,6 @@ impl RlpCircuitConfig { data_table, rom_table, u8_table, - u16_table, } } @@ -1783,8 +1779,6 @@ pub struct RlpCircuitConfigArgs { pub rlp_table: RlpFsmRlpTable, /// u8 table pub u8_table: U8Table, - /// u16 table - pub u16_table: U16Table, /// Challenge API. pub challenges: Challenges>, } @@ -1801,7 +1795,6 @@ impl SubCircuitConfig for RlpCircuitConfig { rom_table, data_table, args.u8_table, - args.u16_table, args.rlp_table, &args.challenges, ) diff --git a/zkevm-circuits/src/rlp_circuit_fsm/dev.rs b/zkevm-circuits/src/rlp_circuit_fsm/dev.rs index 4fe5cfd454..6926c15425 100644 --- a/zkevm-circuits/src/rlp_circuit_fsm/dev.rs +++ b/zkevm-circuits/src/rlp_circuit_fsm/dev.rs @@ -1,6 +1,6 @@ use crate::{ rlp_circuit_fsm::{RlpCircuit, RlpCircuitConfig, RlpCircuitConfigArgs}, - table::{RlpFsmRlpTable, U16Table, U8Table}, + table::{RlpFsmRlpTable, U8Table}, util::{Challenges, SubCircuit, SubCircuitConfig}, witness::Transaction, }; @@ -23,14 +23,12 @@ impl Circuit for RlpCircuit { let challenges = Challenges::construct(meta); let challenge_exprs = challenges.exprs(meta); let u8_table = U8Table::construct(meta); - let u16_table = U16Table::construct(meta); let config = RlpCircuitConfig::new( meta, RlpCircuitConfigArgs { rlp_table, u8_table, - u16_table, challenges: challenge_exprs, }, ); @@ -46,7 +44,6 @@ impl Circuit for RlpCircuit { ) -> Result<(), Error> { let challenges = &config.1.values(&layouter); config.0.u8_table.load(&mut layouter)?; - config.0.u16_table.load(&mut layouter)?; self.synthesize_sub(&config.0, challenges, &mut layouter) } diff --git a/zkevm-circuits/src/super_circuit.rs b/zkevm-circuits/src/super_circuit.rs index 717b8d2681..68efeea3dd 100644 --- a/zkevm-circuits/src/super_circuit.rs +++ b/zkevm-circuits/src/super_circuit.rs @@ -228,7 +228,6 @@ impl SubCircuitConfig for SuperCircuitConfig { RlpCircuitConfigArgs { rlp_table, u8_table, - u16_table, challenges: challenges_expr.clone(), }, ); @@ -256,6 +255,7 @@ impl SubCircuitConfig for SuperCircuitConfig { keccak_table: keccak_table.clone(), rlp_table, sig_table, + u8_table, u16_table, challenges: challenges_expr.clone(), }, diff --git a/zkevm-circuits/src/tx_circuit.rs b/zkevm-circuits/src/tx_circuit.rs index ffd3523890..4a7edaacca 100644 --- a/zkevm-circuits/src/tx_circuit.rs +++ b/zkevm-circuits/src/tx_circuit.rs @@ -16,17 +16,41 @@ use crate::{ evm_circuit::util::constraint_builder::{BaseConstraintBuilder, ConstrainBuilderCommon}, sig_circuit::SigCircuit, table::{ + BlockContextFieldTag::CumNumTxs, BlockTable, KeccakTable, LookupTable, RlpFsmRlpTable as RlpTable, SigTable, TxFieldTag, - TxTable, + TxFieldTag::{ + BlockNumber, CallData, CallDataGasCost, CallDataLength, CallDataRLC, CalleeAddress, + CallerAddress, ChainID, Gas, GasPrice, IsCreate, Nonce, SigR, SigS, SigV, + TxDataGasCost, TxHashLength, TxHashRLC, TxSignHash, TxSignLength, TxSignRLC, + }, + TxTable, U16Table, U8Table, + }, + util::{ + is_zero::{IsZeroChip, IsZeroConfig}, + keccak, random_linear_combine_word as rlc, rlc_be_bytes, SubCircuit, SubCircuitConfig, }, - util::{keccak, random_linear_combine_word as rlc, SubCircuit, SubCircuitConfig}, witness, - witness::{rlp_fsm::Tag, RlpTag, Transaction}, + witness::{ + rlp_fsm::Tag, + Format::{L1MsgHash, TxHashEip155, TxHashPreEip155, TxSignEip155, TxSignPreEip155}, + RlpTag, + RlpTag::{GasCost, Len, Null, RLC}, + Tag::TxType as RLPTxType, + Transaction, + }, }; use bus_mapping::circuit_input_builder::keccak_inputs_sign_verify; -use eth_types::{sign_types::SignData, Address, Field, ToAddress, ToLittleEndian, ToScalar}; +use eth_types::{ + geth_types::{ + TxType, + TxType::{Eip155, L1Msg, PreEip155}, + }, + sign_types::SignData, + Address, Field, ToAddress, ToLittleEndian, ToScalar, +}; use gadgets::{ binary_number::{BinaryNumberChip, BinaryNumberConfig}, + comparator::{ComparatorChip, ComparatorConfig, ComparatorInstruction}, is_equal::{IsEqualChip, IsEqualConfig, IsEqualInstruction}, util::{and, not, select, sum, Expr}, }; @@ -43,35 +67,12 @@ use std::{ marker::PhantomData, }; -use crate::{ - table::TxFieldTag::{ - BlockNumber, CallData, CallDataGasCost, CallDataLength, CallDataRLC, CalleeAddress, - CallerAddress, Gas, GasPrice, IsCreate, Nonce, SigR, SigS, SigV, TxDataGasCost, - TxHashLength, TxHashRLC, TxSignHash, TxSignLength, TxSignRLC, - }, - util::is_zero::{IsZeroChip, IsZeroConfig}, -}; #[cfg(feature = "onephase")] use halo2_proofs::plonk::FirstPhase as SecondPhase; use halo2_proofs::plonk::Fixed; #[cfg(not(feature = "onephase"))] use halo2_proofs::plonk::SecondPhase; -use crate::{ - table::{BlockContextFieldTag::CumNumTxs, TxFieldTag::ChainID, U16Table}, - util::rlc_be_bytes, - witness::{ - Format::{L1MsgHash, TxHashEip155, TxHashPreEip155, TxSignEip155, TxSignPreEip155}, - RlpTag::{GasCost, Len, Null, RLC}, - Tag::TxType as RLPTxType, - }, -}; -use eth_types::geth_types::{ - TxType, - TxType::{Eip155, L1Msg, PreEip155}, -}; -use gadgets::comparator::{ComparatorChip, ComparatorConfig, ComparatorInstruction}; - /// Number of rows of one tx occupies in the fixed part of tx table pub const TX_LEN: usize = 22; /// Offset of TxHash tag in the tx table @@ -106,6 +107,7 @@ pub struct TxCircuitConfig { // Whether tag's RLP-encoded value is 0x80 = rlp([]) is_none: Column, + u8_table: U8Table, u16_table: U16Table, /// Verify if the tx_id is zero or not. @@ -165,6 +167,8 @@ pub struct TxCircuitConfigArgs { pub rlp_table: RlpTable, /// SigTable pub sig_table: SigTable, + /// Reusable u8 lookup table, + pub u8_table: U8Table, /// Reusable u16 lookup table, pub u16_table: U16Table, /// Challenges @@ -183,6 +187,7 @@ impl SubCircuitConfig for TxCircuitConfig { keccak_table, rlp_table, sig_table, + u8_table, u16_table, challenges: _, }: Self::ConfigArgs, @@ -686,7 +691,7 @@ impl SubCircuitConfig for TxCircuitConfig { |meta| meta.query_fixed(q_enable, Rotation::cur()), |meta| meta.query_advice(tx_table.tx_id, Rotation::cur()), |meta| meta.query_advice(cum_num_txs, Rotation::cur()), - u16_table.into(), + u8_table.into(), ); meta.create_gate("tx_id <= cum_num_txs", |meta| { @@ -876,6 +881,7 @@ impl SubCircuitConfig for TxCircuitConfig { tx_type_bits, rlp_tag, is_none, + u8_table, u16_table, tx_id_is_zero, value_is_zero, diff --git a/zkevm-circuits/src/tx_circuit/dev.rs b/zkevm-circuits/src/tx_circuit/dev.rs index 6f8c16886a..7c6fa7057a 100644 --- a/zkevm-circuits/src/tx_circuit/dev.rs +++ b/zkevm-circuits/src/tx_circuit/dev.rs @@ -6,7 +6,9 @@ pub use super::TxCircuit; use crate::{ sig_circuit::{SigCircuit, SigCircuitConfig, SigCircuitConfigArgs}, - table::{BlockTable, KeccakTable, RlpFsmRlpTable as RlpTable, SigTable, TxTable, U16Table}, + table::{ + BlockTable, KeccakTable, RlpFsmRlpTable as RlpTable, SigTable, TxTable, U16Table, U8Table, + }, tx_circuit::{TxCircuitConfig, TxCircuitConfigArgs}, util::{Challenges, SubCircuit, SubCircuitConfig}, witness::Transaction, @@ -29,6 +31,8 @@ pub struct TxCircuitTesterConfigArgs { pub keccak_table: KeccakTable, /// SigTable pub sig_table: SigTable, + /// u8 lookup table, + pub u8_table: U8Table, /// u16 lookup table, pub u16_table: U16Table, /// Challenges @@ -42,6 +46,8 @@ pub struct TxCircuitTesterConfig { // SigTable is assigned inside SigCircuit sig_config: SigCircuitConfig, /// u16 lookup table, + pub u8_table: U8Table, + /// u16 lookup table, pub u16_table: U16Table, } @@ -56,6 +62,7 @@ impl SubCircuitConfig for TxCircuitTesterConfig { keccak_table, rlp_table, sig_table, + u8_table, u16_table, challenges, }: Self::ConfigArgs, @@ -76,6 +83,7 @@ impl SubCircuitConfig for TxCircuitTesterConfig { tx_table, keccak_table, rlp_table, + u8_table, u16_table, challenges, }, @@ -83,6 +91,7 @@ impl SubCircuitConfig for TxCircuitTesterConfig { TxCircuitTesterConfig { tx_config, sig_config, + u8_table, u16_table, } } @@ -150,6 +159,7 @@ impl Circuit for TxCircuitTester { let keccak_table = KeccakTable::construct(meta); let rlp_table = RlpTable::construct(meta); let sig_table = SigTable::construct(meta); + let u8_table = U8Table::construct(meta); let u16_table = U16Table::construct(meta); let challenges = Challenges::construct(meta); @@ -171,6 +181,7 @@ impl Circuit for TxCircuitTester { tx_table, keccak_table, rlp_table, + u8_table, u16_table, challenges, }, @@ -178,6 +189,7 @@ impl Circuit for TxCircuitTester { TxCircuitTesterConfig { tx_config, sig_config, + u8_table, u16_table, } }; @@ -191,6 +203,7 @@ impl Circuit for TxCircuitTester { mut layouter: impl Layouter, ) -> Result<(), Error> { let challenges = challenges.values(&layouter); + config.u8_table.load(&mut layouter)?; config.u16_table.load(&mut layouter)?; let padding_txs = (self.tx_circuit.txs.len()..self.tx_circuit.max_txs) From 26ad03384f916b66f7d1abc7e95540aeccb10942 Mon Sep 17 00:00:00 2001 From: lightsing Date: Fri, 4 Aug 2023 15:27:44 +0800 Subject: [PATCH 14/17] fix dev_load --- gadgets/src/less_than.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gadgets/src/less_than.rs b/gadgets/src/less_than.rs index cd27a32488..8dcb6f17eb 100644 --- a/gadgets/src/less_than.rs +++ b/gadgets/src/less_than.rs @@ -25,7 +25,7 @@ pub trait LtInstruction { ) -> Result<(), Error>; #[cfg(test)] - /// Load the u16 lookup table. + /// Load the u8 lookup table. fn dev_load(&self, layouter: &mut impl halo2_proofs::circuit::Layouter) -> Result<(), Error>; } @@ -155,10 +155,10 @@ impl LtInstruction for LtChip { &self, layouter: &mut impl halo2_proofs::circuit::Layouter, ) -> Result<(), Error> { - const RANGE: usize = u16::MAX as usize; + const RANGE: usize = u8::MAX as usize; layouter.assign_table( - || "load u16 range check table", + || "load u8 range check table", |mut table| { for i in 0..=RANGE { table.assign_cell( From a4e262c3de0e43aae4321d377a0ca05510b1d203 Mon Sep 17 00:00:00 2001 From: lightsing Date: Sat, 5 Aug 2023 11:20:15 +0800 Subject: [PATCH 15/17] revert degree changes --- gadgets/src/less_than.rs | 8 ++++---- zkevm-circuits/src/rlp_circuit_fsm/test.rs | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/gadgets/src/less_than.rs b/gadgets/src/less_than.rs index 8dcb6f17eb..cb6ca74132 100644 --- a/gadgets/src/less_than.rs +++ b/gadgets/src/less_than.rs @@ -207,7 +207,7 @@ mod test { // TODO: remove zk blinding factors in halo2 to restore the // correct k (without the extra + 2). - let k = (usize::BITS - $values.len().leading_zeros() + 2).max(17); + let k = usize::BITS - $values.len().leading_zeros() + 2; let circuit = TestCircuit:: { values: Some($values), checks: Some($checks), @@ -224,7 +224,7 @@ mod test { // TODO: remove zk blinding factors in halo2 to restore the // correct k (without the extra + 2). - let k = (usize::BITS - $values.len().leading_zeros() + 2).max(17); + let k = usize::BITS - $values.len().leading_zeros() + 2; let circuit = TestCircuit:: { values: Some($values), checks: Some($checks), @@ -265,14 +265,14 @@ mod test { let q_enable = meta.complex_selector(); let value = meta.advice_column(); let check = meta.advice_column(); - let u16_table = meta.lookup_table_column(); + let u8_table = meta.lookup_table_column(); let lt = LtChip::configure( meta, |meta| meta.query_selector(q_enable), |meta| meta.query_advice(value, Rotation::prev()), |meta| meta.query_advice(value, Rotation::cur()), - u16_table, + u8_table, ); let config = Self::Config { diff --git a/zkevm-circuits/src/rlp_circuit_fsm/test.rs b/zkevm-circuits/src/rlp_circuit_fsm/test.rs index 04c72b4f6b..c770dac4b0 100644 --- a/zkevm-circuits/src/rlp_circuit_fsm/test.rs +++ b/zkevm-circuits/src/rlp_circuit_fsm/test.rs @@ -99,7 +99,7 @@ fn test_l1_msg_tx() { _marker: Default::default(), }; - let mock_prover = MockProver::run(17, &rlp_circuit, vec![]); + let mock_prover = MockProver::run(14, &rlp_circuit, vec![]); assert!(mock_prover.is_ok()); let mock_prover = mock_prover.unwrap(); @@ -126,7 +126,7 @@ fn test_eip1559_tx() { _marker: Default::default(), }; - let mock_prover = MockProver::run(17, &rlp_circuit, vec![]); + let mock_prover = MockProver::run(14, &rlp_circuit, vec![]); assert!(mock_prover.is_ok()); let mock_prover = mock_prover.unwrap(); if let Err(errors) = mock_prover.verify_par() { From 31febbf98a381735cb2e4028242f9db1e721369e Mon Sep 17 00:00:00 2001 From: lightsing Date: Mon, 7 Aug 2023 10:23:28 +0800 Subject: [PATCH 16/17] the degree should fit in u8 table --- gadgets/src/less_than.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gadgets/src/less_than.rs b/gadgets/src/less_than.rs index cb6ca74132..bbc187f9c0 100644 --- a/gadgets/src/less_than.rs +++ b/gadgets/src/less_than.rs @@ -207,7 +207,7 @@ mod test { // TODO: remove zk blinding factors in halo2 to restore the // correct k (without the extra + 2). - let k = usize::BITS - $values.len().leading_zeros() + 2; + let k = (usize::BITS - $values.len().leading_zeros() + 2).max(8); let circuit = TestCircuit:: { values: Some($values), checks: Some($checks), @@ -224,7 +224,7 @@ mod test { // TODO: remove zk blinding factors in halo2 to restore the // correct k (without the extra + 2). - let k = usize::BITS - $values.len().leading_zeros() + 2; + let k = (usize::BITS - $values.len().leading_zeros() + 2).max(8); let circuit = TestCircuit:: { values: Some($values), checks: Some($checks), From 21a156fa4be3214809aa651696fd39d6cb57de59 Mon Sep 17 00:00:00 2001 From: lightsing Date: Mon, 7 Aug 2023 10:30:29 +0800 Subject: [PATCH 17/17] the degree should fit in u8 table --- gadgets/src/less_than.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gadgets/src/less_than.rs b/gadgets/src/less_than.rs index bbc187f9c0..c2613a1fc6 100644 --- a/gadgets/src/less_than.rs +++ b/gadgets/src/less_than.rs @@ -207,7 +207,7 @@ mod test { // TODO: remove zk blinding factors in halo2 to restore the // correct k (without the extra + 2). - let k = (usize::BITS - $values.len().leading_zeros() + 2).max(8); + let k = (usize::BITS - $values.len().leading_zeros() + 2).max(9); let circuit = TestCircuit:: { values: Some($values), checks: Some($checks), @@ -224,7 +224,7 @@ mod test { // TODO: remove zk blinding factors in halo2 to restore the // correct k (without the extra + 2). - let k = (usize::BITS - $values.len().leading_zeros() + 2).max(8); + let k = (usize::BITS - $values.len().leading_zeros() + 2).max(9); let circuit = TestCircuit:: { values: Some($values), checks: Some($checks),