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

Commit

Permalink
refactor evm/state circuit to share range check
Browse files Browse the repository at this point in the history
  • Loading branch information
hero78119 committed Jun 1, 2023
1 parent 21e3473 commit a10abca
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 75 deletions.
64 changes: 19 additions & 45 deletions zkevm-circuits/src/evm_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ pub use crate::witness;
use crate::{
evm_circuit::param::{MAX_STEP_HEIGHT, STEP_STATE_HEIGHT},
table::{
BlockTable, BytecodeTable, CopyTable, ExpTable, KeccakTable, LookupTable, RwTable, TxTable,
BlockTable, BytecodeTable, CopyTable, ExpTable, KeccakTable, LookupTable, MaxNBitTable,
RwTable, TxTable,
},
util::{Challenges, SubCircuit, SubCircuitConfig},
};
Expand All @@ -37,8 +38,8 @@ use witness::Block;
#[derive(Clone, Debug)]
pub struct EvmCircuitConfig<F> {
fixed_table: [Column<Fixed>; 4],
u8_table: [Column<Fixed>; 1], // byte table
u16_table: [Column<Fixed>; 1], // 2 bytes table.
u8_table: MaxNBitTable<8>,
u16_table: MaxNBitTable<16>,
pub(crate) execution: Box<ExecutionConfig<F>>,
// External tables
tx_table: TxTable,
Expand Down Expand Up @@ -68,6 +69,10 @@ pub struct EvmCircuitConfigArgs<F: Field> {
pub keccak_table: KeccakTable,
/// ExpTable
pub exp_table: ExpTable,
/// U8Table
pub u8_table: MaxNBitTable<8>,
/// U16Table
pub u16_table: MaxNBitTable<16>,
}

impl<F: Field> SubCircuitConfig<F> for EvmCircuitConfig<F> {
Expand All @@ -86,11 +91,11 @@ impl<F: Field> SubCircuitConfig<F> for EvmCircuitConfig<F> {
copy_table,
keccak_table,
exp_table,
u8_table,
u16_table,
}: Self::ConfigArgs,
) -> Self {
let fixed_table = [(); 4].map(|_| meta.fixed_column());
let u8_table = [(); 1].map(|_| meta.fixed_column());
let u16_table = [(); 1].map(|_| meta.fixed_column());
let execution = Box::new(ExecutionConfig::configure(
meta,
challenges,
Expand Down Expand Up @@ -118,6 +123,8 @@ impl<F: Field> SubCircuitConfig<F> for EvmCircuitConfig<F> {
copy_table.annotate_columns(meta);
keccak_table.annotate_columns(meta);
exp_table.annotate_columns(meta);
u8_table.annotate_columns(meta);
u16_table.annotate_columns(meta);

Self {
fixed_table,
Expand Down Expand Up @@ -158,44 +165,6 @@ impl<F: Field> EvmCircuitConfig<F> {
},
)
}

/// Load u8 table
pub fn load_u8_table(&self, layouter: &mut impl Layouter<F>) -> Result<(), Error> {
layouter.assign_region(
|| "u8 table",
|mut region| {
for offset in 0..(1 << 8) {
region.assign_fixed(
|| "",
self.u8_table[0],
offset,
|| Value::known(F::from(offset as u64)),
)?;
}

Ok(())
},
)
}

/// Load u16 table
pub fn load_u16_table(&self, layouter: &mut impl Layouter<F>) -> Result<(), Error> {
layouter.assign_region(
|| "u16 table",
|mut region| {
for offset in 0..(1 << 16) {
region.assign_fixed(
|| "",
self.u16_table[0],
offset,
|| Value::known(F::from(offset as u64)),
)?;
}

Ok(())
},
)
}
}

/// Tx Circuit for verifying transaction signatures
Expand Down Expand Up @@ -294,8 +263,6 @@ impl<F: Field> SubCircuit<F> for EvmCircuit<F> {
let block = self.block.as_ref().unwrap();

config.load_fixed_table(layouter, self.fixed_table_tags.clone())?;
config.load_u8_table(layouter)?;
config.load_u16_table(layouter)?;
config.execution.assign_block(layouter, block, challenges)
}
}
Expand Down Expand Up @@ -398,6 +365,8 @@ impl<F: Field> Circuit<F> for EvmCircuit<F> {
let copy_table = CopyTable::construct(meta, q_copy_table);
let keccak_table = KeccakTable::construct(meta);
let exp_table = ExpTable::construct(meta);
let u8_table = MaxNBitTable::construct(meta);
let u16_table = MaxNBitTable::construct(meta);
let challenges = Challenges::construct(meta);
let challenges_expr = challenges.exprs(meta);

Expand All @@ -413,6 +382,8 @@ impl<F: Field> Circuit<F> for EvmCircuit<F> {
copy_table,
keccak_table,
exp_table,
u8_table,
u16_table,
},
),
challenges,
Expand Down Expand Up @@ -455,6 +426,9 @@ impl<F: Field> Circuit<F> for EvmCircuit<F> {
.dev_load(&mut layouter, &block.sha3_inputs, &challenges)?;
config.exp_table.load(&mut layouter, block)?;

config.u8_table.load(&mut layouter);
config.u16_table.load(&mut layouter);

self.synthesize_sub(&config, &challenges, &mut layouter)
}
}
Expand Down
2 changes: 2 additions & 0 deletions zkevm-circuits/src/evm_circuit/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -836,13 +836,15 @@ impl<F: Field> ExecutionConfig<F> {
for column in cell_manager.columns().iter() {
if let CellType::LookupU8 = column.cell_type {
meta.lookup_any("u8 lookup", |meta| {
debug_assert_eq!(u8_table.table_exprs(meta).len(), 1);
let u8_table_expression = u8_table.table_exprs(meta)[0].clone();
vec![(column.expr(), u8_table_expression)]
});
}
}
for column in cell_manager.columns().iter() {
if let CellType::LookupU16 = column.cell_type {
debug_assert_eq!(u16_table.table_exprs(meta).len(), 1);
meta.lookup_any("u16 lookup", |meta| {
let u16_table_expression = u16_table.table_exprs(meta)[0].clone();
vec![(column.expr(), u16_table_expression)]
Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/src/evm_circuit/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ pub(crate) enum CellType {
StoragePhase1,
StoragePhase2,
StoragePermutation,
// TODO combine LookupU8, LookupU16 with Lookup(Table::U8 | Table::U16)
LookupU8,
LookupU16,
Lookup(Table),
Expand Down
15 changes: 13 additions & 2 deletions zkevm-circuits/src/state_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ use self::{
};
use crate::{
evm_circuit::{param::N_BYTES_WORD, util::rlc},
table::{AccountFieldTag, LookupTable, MPTProofType, MptTable, RwTable, RwTableTag},
table::{
AccountFieldTag, LookupTable, MPTProofType, MaxNBitTable, MptTable, RwTable, RwTableTag,
},
util::{Challenges, Expr, SubCircuit, SubCircuitConfig},
witness::{self, MptUpdates, Rw, RwMap},
};
Expand Down Expand Up @@ -79,6 +81,12 @@ pub struct StateCircuitConfigArgs<F: Field> {
pub rw_table: RwTable,
/// MptTable
pub mpt_table: MptTable,
/// U8Table
u8_table: MaxNBitTable<8>,
/// U10Table
u10_table: MaxNBitTable<10>,
/// U16Table
u16_table: MaxNBitTable<16>,
/// Challenges
pub challenges: Challenges<Expression<F>>,
}
Expand All @@ -92,11 +100,14 @@ impl<F: Field> SubCircuitConfig<F> for StateCircuitConfig<F> {
Self::ConfigArgs {
rw_table,
mpt_table,
u8_table,
u10_table,
u16_table,
challenges,
}: Self::ConfigArgs,
) -> Self {
let selector = meta.fixed_column();
let lookups = LookupsChip::configure(meta);
let lookups = LookupsChip::configure(meta, u8_table, u10_table, u16_table);
let power_of_randomness: [Expression<F>; 31] = challenges.evm_word_powers_of_randomness();

let rw_counter = MpiChip::configure(meta, selector, rw_table.rw_counter, lookups);
Expand Down
7 changes: 7 additions & 0 deletions zkevm-circuits/src/state_circuit/dev.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::test::MaxNBitTable;
pub use super::StateCircuit;

use crate::{
Expand Down Expand Up @@ -26,6 +27,9 @@ where
let rw_table = RwTable::construct(meta);
let mpt_table = MptTable::construct(meta);
let challenges = Challenges::construct(meta);
let u8_table = MaxNBitTable::construct(meta);
let u10_table = MaxNBitTable::construct(meta);
let u16_table = MaxNBitTable::construct(meta);

let config = {
let challenges = challenges.exprs(meta);
Expand All @@ -34,6 +38,9 @@ where
StateCircuitConfigArgs {
rw_table,
mpt_table,
u8_table,
u10_table,
u16_table,
challenges,
},
)
Expand Down
44 changes: 17 additions & 27 deletions zkevm-circuits/src/state_circuit/lookups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use halo2_proofs::{
use std::marker::PhantomData;
use strum::IntoEnumIterator;

use super::test::{LookupTable, MaxNBitTable};

#[derive(Clone, Copy, Debug)]
pub struct Config {
// Can these be TableColumn's?
Expand Down Expand Up @@ -86,43 +88,31 @@ impl<F: Field> Chip<F> {
}
}

pub fn configure(meta: &mut ConstraintSystem<F>) -> Config {
pub fn configure(
meta: &mut ConstraintSystem<F>,
u8_table: MaxNBitTable<8>,
u10_table: MaxNBitTable<10>,
u16_table: MaxNBitTable<16>,
) -> Config {
debug_assert_eq!(u8_table.columns().len(), 1);
debug_assert_eq!(u10_table.columns().len(), 1);
debug_assert_eq!(u16_table.columns().len(), 1);
let config = Config {
u8: meta.fixed_column(),
u10: meta.fixed_column(),
u16: meta.fixed_column(),
u8: u8_table.columns()[0],
u10: u10_table.columns()[0],
u16: u16_table.columns()[0],
call_context_field_tag: meta.fixed_column(),
};
meta.annotate_lookup_any_column(config.u8, || "LOOKUP_u8");
meta.annotate_lookup_any_column(config.u10, || "LOOKUP_u10");
meta.annotate_lookup_any_column(config.u16, || "LOOKUP_u16");
u8_table.annotate_columns(meta);
u10_table.annotate_columns(meta);
u16_table.annotate_columns(meta);
meta.annotate_lookup_any_column(config.call_context_field_tag, || {
"LOOKUP_call_context_field_tag"
});
config
}

pub fn load(&self, layouter: &mut impl Layouter<F>) -> Result<(), Error> {
for (column, exponent) in [
(self.config.u8, 8),
(self.config.u10, 10),
(self.config.u16, 16),
] {
layouter.assign_region(
|| format!("assign u{} fixed column", exponent),
|mut region| {
for i in 0..(1 << exponent) {
region.assign_fixed(
|| format!("assign {} in u{} fixed column", i, exponent),
column,
i,
|| Value::known(F::from(i as u64)),
)?;
}
Ok(())
},
)?;
}
layouter.assign_region(
|| "assign call_context_field_tags fixed column",
|mut region| {
Expand Down
21 changes: 20 additions & 1 deletion zkevm-circuits/src/super_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ use crate::{
pi_circuit::{PiCircuit, PiCircuitConfig, PiCircuitConfigArgs},
state_circuit::{StateCircuit, StateCircuitConfig, StateCircuitConfigArgs},
table::{
BlockTable, BytecodeTable, CopyTable, ExpTable, KeccakTable, MptTable, RwTable, TxTable,
BlockTable, BytecodeTable, CopyTable, ExpTable, KeccakTable, MaxNBitTable, MptTable,
RwTable, TxTable,
},
tx_circuit::{TxCircuit, TxCircuitConfig, TxCircuitConfigArgs},
util::{log2_ceil, Challenges, SubCircuit, SubCircuitConfig},
Expand All @@ -87,6 +88,9 @@ use std::array;
pub struct SuperCircuitConfig<F: Field> {
block_table: BlockTable,
mpt_table: MptTable,
u8_table: MaxNBitTable<8>,
u10_table: MaxNBitTable<10>,
u16_table: MaxNBitTable<16>,
evm_circuit: EvmCircuitConfig<F>,
state_circuit: StateCircuitConfig<F>,
tx_circuit: TxCircuitConfig<F>,
Expand Down Expand Up @@ -128,6 +132,9 @@ impl<F: Field> SubCircuitConfig<F> for SuperCircuitConfig<F> {
let copy_table = CopyTable::construct(meta, q_copy_table);
let exp_table = ExpTable::construct(meta);
let keccak_table = KeccakTable::construct(meta);
let u8_table = MaxNBitTable::construct(meta);
let u10_table = MaxNBitTable::construct(meta);
let u16_table = MaxNBitTable::construct(meta);

// Use a mock randomness instead of the randomness derived from the challange
// (either from mock or real prover) to help debugging assignments.
Expand Down Expand Up @@ -190,6 +197,9 @@ impl<F: Field> SubCircuitConfig<F> for SuperCircuitConfig<F> {
StateCircuitConfigArgs {
rw_table,
mpt_table,
u8_table,
u10_table,
u16_table,
challenges: challenges.clone(),
},
);
Expand All @@ -205,12 +215,17 @@ impl<F: Field> SubCircuitConfig<F> for SuperCircuitConfig<F> {
copy_table,
keccak_table,
exp_table,
u8_table,
u16_table,
},
);

Self {
block_table,
mpt_table,
u8_table,
u10_table,
u16_table,
evm_circuit,
state_circuit,
copy_circuit,
Expand Down Expand Up @@ -414,6 +429,10 @@ impl<F: Field, const MAX_TXS: usize, const MAX_CALLDATA: usize, const MOCK_RANDO
Value::known(block.randomness),
)?;

config.u8_table.load(&mut layouter);
config.u10_table.load(&mut layouter);
config.u16_table.load(&mut layouter);

self.synthesize_sub(&config, &challenges, &mut layouter)
}
}
Expand Down
Loading

0 comments on commit a10abca

Please sign in to comment.