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

Commit

Permalink
evm circuit u16_lookup and rename byte_table to u8_table
Browse files Browse the repository at this point in the history
  • Loading branch information
hero78119 committed Jun 1, 2023
1 parent bf27373 commit dfbd36c
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 48 deletions.
56 changes: 42 additions & 14 deletions zkevm-circuits/src/evm_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ use witness::Block;
#[derive(Clone, Debug)]
pub struct EvmCircuitConfig<F> {
fixed_table: [Column<Fixed>; 4],
byte_table: [Column<Fixed>; 1],
u8_table: [Column<Fixed>; 1], // byte table
u16_table: [Column<Fixed>; 1], // 2 bytes table.
pub(crate) execution: Box<ExecutionConfig<F>>,
// External tables
tx_table: TxTable,
Expand Down Expand Up @@ -88,12 +89,14 @@ impl<F: Field> SubCircuitConfig<F> for EvmCircuitConfig<F> {
}: Self::ConfigArgs,
) -> Self {
let fixed_table = [(); 4].map(|_| meta.fixed_column());
let byte_table = [(); 1].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,
&fixed_table,
&byte_table,
&u8_table,
&u16_table,
&tx_table,
&rw_table,
&bytecode_table,
Expand All @@ -103,7 +106,8 @@ impl<F: Field> SubCircuitConfig<F> for EvmCircuitConfig<F> {
&exp_table,
));

meta.annotate_lookup_any_column(byte_table[0], || "byte_range");
meta.annotate_lookup_any_column(u8_table[0], || "u8_range");
meta.annotate_lookup_any_column(u16_table[0], || "u16_range");
fixed_table.iter().enumerate().for_each(|(idx, &col)| {
meta.annotate_lookup_any_column(col, || format!("fix_table_{}", idx))
});
Expand All @@ -117,7 +121,8 @@ impl<F: Field> SubCircuitConfig<F> for EvmCircuitConfig<F> {

Self {
fixed_table,
byte_table,
u8_table,
u16_table,
execution,
tx_table,
rw_table,
Expand Down Expand Up @@ -154,15 +159,34 @@ impl<F: Field> EvmCircuitConfig<F> {
)
}

/// Load byte table
pub fn load_byte_table(&self, layouter: &mut impl Layouter<F>) -> Result<(), Error> {
/// Load u8 table
pub fn load_u8_table(&self, layouter: &mut impl Layouter<F>) -> Result<(), Error> {
layouter.assign_region(
|| "byte table",
|| "u8 table",
|mut region| {
for offset in 0..256 {
for offset in 0..(1 << 8) {
region.assign_fixed(
|| "",
self.byte_table[0],
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)),
)?;
Expand Down Expand Up @@ -270,7 +294,8 @@ 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_byte_table(layouter)?;
config.load_u8_table(layouter)?;
config.load_u16_table(layouter)?;
config.execution.assign_block(layouter, block, challenges)
}
}
Expand Down Expand Up @@ -439,7 +464,8 @@ mod evm_circuit_stats {
use crate::{
evm_circuit::{
param::{
LOOKUP_CONFIG, N_BYTE_LOOKUPS, N_COPY_COLUMNS, N_PHASE1_COLUMNS, N_PHASE2_COLUMNS,
LOOKUP_CONFIG, N_COPY_COLUMNS, N_PHASE1_COLUMNS, N_PHASE2_COLUMNS, N_U16_LOOKUPS,
N_U8_LOOKUPS,
},
step::ExecutionState,
EvmCircuit,
Expand Down Expand Up @@ -594,8 +620,10 @@ mod evm_circuit_stats {
N_PHASE2_COLUMNS,
storage_perm,
N_COPY_COLUMNS,
byte_lookup,
N_BYTE_LOOKUPS,
u8_lookup,
N_U8_LOOKUPS,
u16_lookup,
N_U16_LOOKUPS,
fixed_table,
LOOKUP_CONFIG[0].1,
tx_table,
Expand Down
32 changes: 22 additions & 10 deletions zkevm-circuits/src/evm_circuit/execution.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::{
param::{
BLOCK_TABLE_LOOKUPS, BYTECODE_TABLE_LOOKUPS, COPY_TABLE_LOOKUPS, EXP_TABLE_LOOKUPS,
FIXED_TABLE_LOOKUPS, KECCAK_TABLE_LOOKUPS, N_BYTE_LOOKUPS, N_COPY_COLUMNS,
N_PHASE1_COLUMNS, RW_TABLE_LOOKUPS, TX_TABLE_LOOKUPS,
FIXED_TABLE_LOOKUPS, KECCAK_TABLE_LOOKUPS, N_COPY_COLUMNS, N_PHASE1_COLUMNS, N_U16_LOOKUPS,
N_U8_LOOKUPS, RW_TABLE_LOOKUPS, TX_TABLE_LOOKUPS,
},
util::{instrumentation::Instrument, CachedRegion, CellManager, StoredExpression},
};
Expand Down Expand Up @@ -319,7 +319,8 @@ impl<F: Field> ExecutionConfig<F> {
meta: &mut ConstraintSystem<F>,
challenges: Challenges<Expression<F>>,
fixed_table: &dyn LookupTable<F>,
byte_table: &dyn LookupTable<F>,
u8_table: &dyn LookupTable<F>,
u16_table: &dyn LookupTable<F>,
tx_table: &dyn LookupTable<F>,
rw_table: &dyn LookupTable<F>,
bytecode_table: &dyn LookupTable<F>,
Expand Down Expand Up @@ -581,7 +582,8 @@ impl<F: Field> ExecutionConfig<F> {
Self::configure_lookup(
meta,
fixed_table,
byte_table,
u8_table,
u16_table,
tx_table,
rw_table,
bytecode_table,
Expand Down Expand Up @@ -797,7 +799,8 @@ impl<F: Field> ExecutionConfig<F> {
fn configure_lookup(
meta: &mut ConstraintSystem<F>,
fixed_table: &dyn LookupTable<F>,
byte_table: &dyn LookupTable<F>,
u8_table: &dyn LookupTable<F>,
u16_table: &dyn LookupTable<F>,
tx_table: &dyn LookupTable<F>,
rw_table: &dyn LookupTable<F>,
bytecode_table: &dyn LookupTable<F>,
Expand Down Expand Up @@ -831,10 +834,18 @@ impl<F: Field> ExecutionConfig<F> {
}
}
for column in cell_manager.columns().iter() {
if let CellType::LookupByte = column.cell_type {
meta.lookup_any("Byte lookup", |meta| {
let byte_table_expression = byte_table.table_exprs(meta)[0].clone();
vec![(column.expr(), byte_table_expression)]
if let CellType::LookupU8 = column.cell_type {
meta.lookup_any("u8 lookup", |meta| {
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 {
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 Expand Up @@ -1036,7 +1047,8 @@ impl<F: Field> ExecutionConfig<F> {
("EVM_lookup_exp", EXP_TABLE_LOOKUPS),
("EVM_adv_phase2", N_PHASE2_COLUMNS),
("EVM_copy", N_COPY_COLUMNS),
("EVM_lookup_byte", N_BYTE_LOOKUPS),
("EVM_lookup_u8", N_U8_LOOKUPS),
("EVM_lookup_u16", N_U16_LOOKUPS),
("EVM_adv_phase1", N_PHASE1_COLUMNS),
];
let mut group_index = 0;
Expand Down
6 changes: 4 additions & 2 deletions zkevm-circuits/src/evm_circuit/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ pub(crate) const N_PHASE2_COLUMNS: usize = 4;

/// Number of Advice Phase1 columns in the EVM circuit
pub(crate) const N_PHASE1_COLUMNS: usize =
STEP_WIDTH - EVM_LOOKUP_COLS - N_PHASE2_COLUMNS - N_COPY_COLUMNS - N_BYTE_LOOKUPS;
STEP_WIDTH - EVM_LOOKUP_COLS - N_PHASE2_COLUMNS - N_COPY_COLUMNS - N_U8_LOOKUPS - N_U16_LOOKUPS;

// Number of copy columns
pub(crate) const N_COPY_COLUMNS: usize = 2;

pub(crate) const N_BYTE_LOOKUPS: usize = 24;
pub(crate) const N_U8_LOOKUPS: usize = 12;

pub(crate) const N_U16_LOOKUPS: usize = 6;

/// Amount of lookup columns in the EVM circuit dedicated to lookups.
pub(crate) const EVM_LOOKUP_COLS: usize = FIXED_TABLE_LOOKUPS
Expand Down
18 changes: 13 additions & 5 deletions zkevm-circuits/src/evm_circuit/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use crate::util::{
use crate::{
evm_circuit::{
param::{
LOOKUP_CONFIG, N_BYTES_MEMORY_ADDRESS, N_BYTE_LOOKUPS, N_COPY_COLUMNS, N_PHASE2_COLUMNS,
LOOKUP_CONFIG, N_BYTES_MEMORY_ADDRESS, N_COPY_COLUMNS, N_PHASE2_COLUMNS, N_U8_LOOKUPS,
},
table::Table,
},
Expand Down Expand Up @@ -34,7 +34,7 @@ pub(crate) mod memory_gadget;

pub use gadgets::util::{and, not, or, select, sum};

use super::param::{N_BYTES_ACCOUNT_ADDRESS, N_BYTES_U64};
use super::param::{N_BYTES_ACCOUNT_ADDRESS, N_BYTES_U64, N_U16_LOOKUPS};

#[derive(Clone, Debug)]
pub(crate) struct Cell<F> {
Expand Down Expand Up @@ -276,7 +276,8 @@ pub(crate) enum CellType {
StoragePhase1,
StoragePhase2,
StoragePermutation,
LookupByte,
LookupU8,
LookupU16,
Lookup(Table),
}

Expand Down Expand Up @@ -379,8 +380,15 @@ impl<F: Field> CellManager<F> {
}

// Mark columns used for byte lookup
for _ in 0..N_BYTE_LOOKUPS {
columns[column_idx].cell_type = CellType::LookupByte;
for _ in 0..N_U8_LOOKUPS {
columns[column_idx].cell_type = CellType::LookupU8;
assert_eq!(advices[column_idx].column_type().phase(), 0);
column_idx += 1;
}

// Mark columns used for byte lookup
for _ in 0..N_U16_LOOKUPS {
columns[column_idx].cell_type = CellType::LookupU16;
assert_eq!(advices[column_idx].column_type().phase(), 0);
column_idx += 1;
}
Expand Down
27 changes: 13 additions & 14 deletions zkevm-circuits/src/evm_circuit/util/constraint_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
}

pub(crate) fn query_byte(&mut self) -> Cell<F> {
self.query_cell_with_type(CellType::LookupByte)
self.query_cell_with_type(CellType::LookupU8)
}

#[deprecated(note = "query_word* are favored")]
Expand Down Expand Up @@ -454,7 +454,7 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
}

/// query_word4_unchecked get word with 4 limbs. Each limb is not guaranteed to be 64 bits.
pub fn query_word4_unchecked<const N: usize, const N2: usize>(&mut self) -> Word4<Cell<F>> {
pub fn query_word4_unchecked<const N: usize>(&mut self) -> Word4<Cell<F>> {
Word4::new(
self.query_cells(CellType::StoragePhase1, N)
.try_into()
Expand All @@ -463,13 +463,8 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
}

// each limb is 16 bits, and any conversion to smaller limbs inherits the type check.
// TODO implement 16bits range check
pub(crate) fn query_word16<const N: usize>(&mut self) -> Word16<Cell<F>> {
Word16::new(
self.query_cells(CellType::StoragePhase1, N)
.try_into()
.unwrap(),
)
Word16::new(self.query_u16_dyn(N))
}

// query_word32 each limb is 8 bits, and any conversion to smaller limbs inherits the type
Expand All @@ -483,23 +478,27 @@ impl<'a, F: Field> EVMConstraintBuilder<'a, F> {
}

pub(crate) fn query_u64(&mut self) -> U64Cell<F> {
U64Cell::new(self.query_bytes(), 256u64.expr())
U64Cell::new(self.query_bytes(), (1 << 8).expr())
}

pub(crate) fn query_account_address(&mut self) -> AccountAddress<F> {
AccountAddress::<F>::new(self.query_bytes(), 256u64.expr())
AccountAddress::<F>::new(self.query_bytes(), (1 << 8).expr())
}

pub(crate) fn query_memory_address(&mut self) -> MemoryAddress<F> {
MemoryAddress::<F>::new(self.query_bytes(), 256u64.expr())
MemoryAddress::<F>::new(self.query_bytes(), (1 << 8).expr())
}

pub(crate) fn query_bytes<const N: usize>(&mut self) -> [Cell<F>; N] {
self.query_bytes_dyn(N).try_into().unwrap()
self.query_u8_dyn(N).try_into().unwrap()
}

pub(crate) fn query_u8_dyn(&mut self, count: usize) -> Vec<Cell<F>> {
self.query_cells(CellType::LookupU8, count)
}

pub(crate) fn query_bytes_dyn(&mut self, count: usize) -> Vec<Cell<F>> {
self.query_cells(CellType::LookupByte, count)
pub(crate) fn query_u16_dyn(&mut self, count: usize) -> Vec<Cell<F>> {
self.query_cells(CellType::LookupU16, count)
}

pub(crate) fn query_cell(&mut self) -> Cell<F> {
Expand Down
10 changes: 7 additions & 3 deletions zkevm-circuits/src/evm_circuit/util/instrumentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ impl Instrument {
CellType::StoragePermutation => {
report.storage_perm = data_entry;
}
CellType::LookupByte => {
report.byte_lookup = data_entry;
CellType::LookupU8 => {
report.u8_lookup = data_entry;
}
CellType::LookupU16 => {
report.u16_lookup = data_entry;
}
CellType::Lookup(Table::Fixed) => {
report.fixed_table = data_entry;
Expand Down Expand Up @@ -116,7 +119,8 @@ pub(crate) struct ExecStateReport {
pub(crate) storage_1: StateReportRow,
pub(crate) storage_2: StateReportRow,
pub(crate) storage_perm: StateReportRow,
pub(crate) byte_lookup: StateReportRow,
pub(crate) u8_lookup: StateReportRow,
pub(crate) u16_lookup: StateReportRow,
pub(crate) fixed_table: StateReportRow,
pub(crate) tx_table: StateReportRow,
pub(crate) rw_table: StateReportRow,
Expand Down

0 comments on commit dfbd36c

Please sign in to comment.