Skip to content

Commit

Permalink
perf: use shuffle to check u8/u16 range (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
junyu0312 authored Jun 20, 2024
1 parent 98bf77c commit 8138fd9
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 78 deletions.
10 changes: 6 additions & 4 deletions crates/playground/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions crates/zkwasm/src/circuits/etable/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,19 @@ impl<F: FieldExt> EventTableCellAllocator<F> {
pub(super) fn new(
meta: &mut ConstraintSystem<F>,
sel: Column<Fixed>,
(l_0, l_active, l_active_last): (Column<Fixed>, Column<Fixed>, Column<Fixed>),
rtable: &RangeTableConfig<F>,
mtable: &impl ConfigureLookupTable<F>,
cols: &mut impl Iterator<Item = Column<Advice>>,
) -> Self {
let mut allocator = Self::_new(meta, sel, rtable, mtable, cols);
let mut allocator = Self::_new(
meta,
sel,
(l_0, l_active, l_active_last),
rtable,
mtable,
cols,
);
for _ in 0..U32_CELLS {
let cell = allocator.prepare_alloc_u32_cell();
allocator.free_u32_cells.push(cell);
Expand All @@ -371,6 +379,7 @@ impl<F: FieldExt> EventTableCellAllocator<F> {
fn _new(
meta: &mut ConstraintSystem<F>,
sel: Column<Fixed>,
(l_0, l_active, l_active_last): (Column<Fixed>, Column<Fixed>, Column<Fixed>),
rtable: &RangeTableConfig<F>,
mtable: &impl ConfigureLookupTable<F>,
cols: &mut impl Iterator<Item = Column<Advice>>,
Expand All @@ -386,14 +395,14 @@ impl<F: FieldExt> EventTableCellAllocator<F> {
all_cols.insert(
EventTableCellType::U8,
[0; U8_COLUMNS]
.map(|_| vec![U8Column::configure(meta, cols, rtable, |_| constant_from!(1)).col])
.map(|_| vec![U8Column::configure(meta, (l_0, l_active, l_active_last)).col])
.into_iter()
.collect(),
);
all_cols.insert(
EventTableCellType::U16,
[0; U16_COLUMNS]
.map(|_| vec![U16Column::configure(meta, cols, rtable, |_| constant_from!(1)).col])
.map(|_| vec![U16Column::configure(meta, (l_0, l_active, l_active_last)).col])
.into_iter()
.collect(),
);
Expand Down
10 changes: 9 additions & 1 deletion crates/zkwasm/src/circuits/etable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ pub struct EventTableConfig<F: FieldExt> {
impl<F: FieldExt> EventTableConfig<F> {
pub(crate) fn configure(
meta: &mut ConstraintSystem<F>,
(l_0, l_active, l_active_last): (Column<Fixed>, Column<Fixed>, Column<Fixed>),
cols: &mut (impl Iterator<Item = Column<Advice>> + Clone),
rtable: &RangeTableConfig<F>,
image_table: &ImageTableConfig<F>,
Expand All @@ -242,7 +243,14 @@ impl<F: FieldExt> EventTableConfig<F> {
) -> EventTableConfig<F> {
let step_sel = meta.fixed_column();

let mut allocator = EventTableCellAllocator::new(meta, step_sel, rtable, mtable, cols);
let mut allocator = EventTableCellAllocator::new(
meta,
step_sel,
(l_0, l_active, l_active_last),
rtable,
mtable,
cols,
);

let ops = [0; OP_CAPABILITY].map(|_| allocator.alloc_bit_cell());
let enabled_cell = allocator.alloc_bit_cell();
Expand Down
12 changes: 10 additions & 2 deletions crates/zkwasm/src/circuits/mtable/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,17 @@ impl<F: FieldExt> MemoryTableCellAllocator<F> {
pub(super) fn new(
meta: &mut ConstraintSystem<F>,
sel: Column<Fixed>,
(l_0, l_active, l_active_last): (Column<Fixed>, Column<Fixed>, Column<Fixed>),
rtable: &RangeTableConfig<F>,
cols: &mut impl Iterator<Item = Column<Advice>>,
) -> Self {
let mut allocator = Self::_new(meta, sel.clone(), rtable, cols);
let mut allocator = Self::_new(
meta,
sel.clone(),
(l_0, l_active, l_active_last),
rtable,
cols,
);
for _ in 0..U32_CELLS {
let cell = allocator.prepare_alloc_u32_cell();
allocator.free_u32_cells.push(cell);
Expand All @@ -170,6 +177,7 @@ impl<F: FieldExt> MemoryTableCellAllocator<F> {
fn _new(
meta: &mut ConstraintSystem<F>,
sel: Column<Fixed>,
(l_0, l_active, l_active_last): (Column<Fixed>, Column<Fixed>, Column<Fixed>),
rtable: &RangeTableConfig<F>,
cols: &mut impl Iterator<Item = Column<Advice>>,
) -> Self {
Expand All @@ -184,7 +192,7 @@ impl<F: FieldExt> MemoryTableCellAllocator<F> {
all_cols.insert(
MemoryTableCellType::U16,
[0; U16_COLUMNS]
.map(|_| U16Column::configure(meta, cols, rtable, |_| constant_from!(1)).col)
.map(|_| U16Column::configure(meta, (l_0, l_active, l_active_last)).col)
.into_iter()
.collect(),
);
Expand Down
9 changes: 8 additions & 1 deletion crates/zkwasm/src/circuits/mtable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,20 @@ pub struct MemoryTableConfig<F: FieldExt> {
impl<F: FieldExt> MemoryTableConfig<F> {
pub(crate) fn configure(
meta: &mut ConstraintSystem<F>,
(l_0, l_active, l_active_last): (Column<Fixed>, Column<Fixed>, Column<Fixed>),
cols: &mut (impl Iterator<Item = Column<Advice>> + Clone),
rtable: &RangeTableConfig<F>,
image_table: &ImageTableConfig<F>,
) -> Self {
let entry_sel = meta.fixed_column();

let mut allocator = MemoryTableCellAllocator::new(meta, entry_sel, rtable, cols);
let mut allocator = MemoryTableCellAllocator::new(
meta,
entry_sel,
(l_0, l_active, l_active_last),
rtable,
cols,
);
allocator.enable_equality(meta, &MemoryTableCellType::CommonRange);

let enabled_cell = allocator.alloc_bit_cell();
Expand Down
40 changes: 0 additions & 40 deletions crates/zkwasm/src/circuits/rtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ struct OpTable {
pub struct RangeTableConfig<F: FieldExt> {
// [0 .. common_range())
common_range_col: TableColumn,
// [0 .. 65536)
u16_col: TableColumn,
// [0 .. 256)
u8_col: TableColumn,

op_table: OpTable,

_mark: PhantomData<F>,
Expand All @@ -71,8 +66,6 @@ impl<F: FieldExt> RangeTableConfig<F> {

RangeTableConfig {
common_range_col: meta.lookup_table_column(),
u16_col: meta.lookup_table_column(),
u8_col: u8_col_multiset,
op_table: OpTable {
op: meta.lookup_table_column(),
left: u8_col_multiset,
Expand All @@ -92,24 +85,6 @@ impl<F: FieldExt> RangeTableConfig<F> {
meta.lookup(key, |meta| vec![(expr(meta), self.common_range_col)]);
}

pub fn configure_in_u16_range(
&self,
meta: &mut ConstraintSystem<F>,
key: &'static str,
expr: impl FnOnce(&mut VirtualCells<'_, F>) -> Expression<F>,
) {
meta.lookup(key, |meta| vec![(expr(meta), self.u16_col)]);
}

pub fn configure_in_u8_range(
&self,
meta: &mut ConstraintSystem<F>,
key: &'static str,
expr: impl FnOnce(&mut VirtualCells<'_, F>) -> Expression<F>,
) {
meta.lookup(key, |meta| vec![(expr(meta), self.u8_col)]);
}

pub fn configure_in_op_table(
&self,
meta: &mut ConstraintSystem<F>,
Expand Down Expand Up @@ -175,21 +150,6 @@ impl<F: FieldExt> RangeTableChip<F> {
},
)?;

layouter.assign_table(
|| "u16 range table",
|table| {
for i in 0..(1 << 16) {
table.assign_cell(
|| "range table",
self.config.u16_col,
i,
|| Ok(F::from(i as u64)),
)?;
}
Ok(())
},
)?;

{
layouter.assign_table(
|| "op lookup table",
Expand Down
20 changes: 10 additions & 10 deletions crates/zkwasm/src/circuits/utils/u16.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use super::Context;
use crate::circuits::rtable::RangeTableConfig;
use crate::curr;
use halo2_proofs::arithmetic::FieldExt;
use halo2_proofs::plonk::Advice;
use halo2_proofs::plonk::Column;
use halo2_proofs::plonk::ConstraintSystem;
use halo2_proofs::plonk::Error;
use halo2_proofs::plonk::Expression;
use halo2_proofs::plonk::VirtualCells;
use halo2_proofs::plonk::Fixed;
use std::marker::PhantomData;

#[derive(Clone)]
Expand All @@ -19,13 +16,16 @@ pub struct U16Column<F: FieldExt> {
impl<F: FieldExt> U16Column<F> {
pub fn configure(
meta: &mut ConstraintSystem<F>,
cols: &mut impl Iterator<Item = Column<Advice>>,
rtable: &RangeTableConfig<F>,
enable: impl Fn(&mut VirtualCells<'_, F>) -> Expression<F>,
(l_0, l_active, l_active_last): (Column<Fixed>, Column<Fixed>, Column<Fixed>),
) -> Self {
let col = cols.next().unwrap();

rtable.configure_in_u16_range(meta, "u16", |meta| curr!(meta, col) * enable(meta));
let col = meta.advice_column_range(
l_0,
l_active,
l_active_last,
(0, F::zero()),
(u16::MAX as u32, F::from(u16::MAX as u64)),
(2, F::from(2)),
);

Self {
col,
Expand Down
20 changes: 10 additions & 10 deletions crates/zkwasm/src/circuits/utils/u8.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use super::Context;
use crate::circuits::rtable::RangeTableConfig;
use crate::curr;
use halo2_proofs::arithmetic::FieldExt;
use halo2_proofs::plonk::Advice;
use halo2_proofs::plonk::Column;
use halo2_proofs::plonk::ConstraintSystem;
use halo2_proofs::plonk::Error;
use halo2_proofs::plonk::Expression;
use halo2_proofs::plonk::VirtualCells;
use halo2_proofs::plonk::Fixed;
use std::marker::PhantomData;

#[derive(Clone)]
Expand All @@ -19,13 +16,16 @@ pub struct U8Column<F: FieldExt> {
impl<F: FieldExt> U8Column<F> {
pub fn configure(
meta: &mut ConstraintSystem<F>,
cols: &mut impl Iterator<Item = Column<Advice>>,
rtable: &RangeTableConfig<F>,
enable: impl Fn(&mut VirtualCells<'_, F>) -> Expression<F>,
(l_0, l_active, l_active_last): (Column<Fixed>, Column<Fixed>, Column<Fixed>),
) -> Self {
let col = cols.next().unwrap();

rtable.configure_in_u8_range(meta, "u8", |meta| curr!(meta, col) * enable(meta));
let col = meta.advice_column_range(
l_0,
l_active,
l_active_last,
(0, F::from(0)),
(u8::MAX as u32, F::from(u8::MAX as u64)),
(1, F::one()),
);

Self {
col,
Expand Down
Loading

0 comments on commit 8138fd9

Please sign in to comment.