From f244bdae6bba22c13161b54af88c03717f309d80 Mon Sep 17 00:00:00 2001 From: Zhang Junyu Date: Tue, 31 Oct 2023 14:57:38 +0800 Subject: [PATCH] add scaffold for post image table --- Cargo.lock | 1 + crates/zkwasm/Cargo.toml | 3 +- crates/zkwasm/src/circuits/mod.rs | 1 + .../post_image_table/continuation/mod.rs | 49 +++++++++++++++++++ .../src/circuits/post_image_table/mod.rs | 43 ++++++++++++++++ .../circuits/post_image_table/trivial/mod.rs | 43 ++++++++++++++++ .../zkwasm/src/circuits/test_circuit/mod.rs | 23 +++++++-- 7 files changed, 159 insertions(+), 4 deletions(-) create mode 100644 crates/zkwasm/src/circuits/post_image_table/continuation/mod.rs create mode 100644 crates/zkwasm/src/circuits/post_image_table/mod.rs create mode 100644 crates/zkwasm/src/circuits/post_image_table/trivial/mod.rs diff --git a/Cargo.lock b/Cargo.lock index fc6415f3a..53c052919 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -424,6 +424,7 @@ dependencies = [ "anyhow", "ark-std", "bitvec", + "cfg-if 0.1.10", "downcast-rs", "halo2_proofs", "halo2aggregator-s", diff --git a/crates/zkwasm/Cargo.toml b/crates/zkwasm/Cargo.toml index edde03095..1f92ab6e7 100644 --- a/crates/zkwasm/Cargo.toml +++ b/crates/zkwasm/Cargo.toml @@ -9,6 +9,7 @@ edition = "2021" ark-std = { version = "0.3.0", features = ["print-trace"] } bitvec = "1.0.1" downcast-rs = "1.2.0" +cfg-if = "0.1" hex = "0.4.3" log = "0.4.17" num-integer = "0.1" @@ -32,6 +33,6 @@ wasmi.workspace = true rusty-fork = "0.3.0" [features] -default = [] +default = ["continuation"] cuda = ["halo2_proofs/cuda", "specs/cuda"] continuation = ["specs/continuation"] \ No newline at end of file diff --git a/crates/zkwasm/src/circuits/mod.rs b/crates/zkwasm/src/circuits/mod.rs index 4546e91e8..2b9bf1b80 100644 --- a/crates/zkwasm/src/circuits/mod.rs +++ b/crates/zkwasm/src/circuits/mod.rs @@ -19,6 +19,7 @@ mod traits; pub mod config; pub mod image_table; pub mod jtable; +pub mod post_image_table; pub mod rtable; pub mod test_circuit; pub mod utils; diff --git a/crates/zkwasm/src/circuits/post_image_table/continuation/mod.rs b/crates/zkwasm/src/circuits/post_image_table/continuation/mod.rs new file mode 100644 index 000000000..1fe5958ba --- /dev/null +++ b/crates/zkwasm/src/circuits/post_image_table/continuation/mod.rs @@ -0,0 +1,49 @@ +use halo2_proofs::arithmetic::FieldExt; +use halo2_proofs::circuit::Cell; +use halo2_proofs::circuit::Layouter; +use halo2_proofs::plonk::Error; + +use crate::circuits::image_table::ImageTableChip; +use crate::circuits::image_table::ImageTableConfig; +use crate::circuits::image_table::ImageTableLayouter; + +use super::PostImageTableChipTrait; +use super::PostImageTableConfigTrait; + +#[derive(Clone)] +pub(in crate::circuits) struct ContinuationPostImageTableConfig { + config: ImageTableConfig, +} + +impl PostImageTableConfigTrait for ContinuationPostImageTableConfig { + fn configure(meta: &mut halo2_proofs::plonk::ConstraintSystem) -> Self { + Self { + config: ImageTableConfig::configure(meta), + } + } +} + +pub(in crate::circuits) struct ContinuationPostImageTableChip { + chip: ImageTableChip, +} + +impl PostImageTableChipTrait> + for ContinuationPostImageTableChip +{ + fn new(config: ContinuationPostImageTableConfig) -> Self { + Self { + chip: ImageTableChip::new(config.config), + } + } + + fn assign( + self, + layouter: &mut impl Layouter, + image_table: ImageTableLayouter, + permutation_cells: ImageTableLayouter, + ) -> Result<(), Error> { + todo!(); + + Ok(()) + } +} diff --git a/crates/zkwasm/src/circuits/post_image_table/mod.rs b/crates/zkwasm/src/circuits/post_image_table/mod.rs new file mode 100644 index 000000000..e97a27bad --- /dev/null +++ b/crates/zkwasm/src/circuits/post_image_table/mod.rs @@ -0,0 +1,43 @@ +use halo2_proofs::arithmetic::FieldExt; +use halo2_proofs::circuit::Cell; +use halo2_proofs::circuit::Layouter; +use halo2_proofs::plonk::ConstraintSystem; +use halo2_proofs::plonk::Error; + +use super::image_table::ImageTableLayouter; + +pub(self) mod continuation; +pub(self) mod trivial; + +pub(in crate::circuits) trait PostImageTableConfigTrait { + fn configure(_meta: &mut ConstraintSystem) -> Self; +} + +pub(in crate::circuits) trait PostImageTableChipTrait< + F: FieldExt, + Config: PostImageTableConfigTrait, +> +{ + fn new(config: Config) -> Self; + fn assign( + self, + layouter: &mut impl Layouter, + image_table: ImageTableLayouter, + permutation_cells: ImageTableLayouter, + ) -> Result<(), Error>; +} + +cfg_if::cfg_if! { + if #[cfg(feature = "continuation")] { + use self::continuation::*; + + pub(in crate::circuits) type PostImageTableConfig = ContinuationPostImageTableConfig; + pub(in crate::circuits) type PostImageTableChip = ContinuationPostImageTableChip; + + } else { + use self::continuation::*; + + pub(in crate::circuits) type PostImageTableConfig = TrivialPostImageConfig; + pub(in crate::circuits) type PostImageTableChip = TrivialPostImageTableChip; + } +} diff --git a/crates/zkwasm/src/circuits/post_image_table/trivial/mod.rs b/crates/zkwasm/src/circuits/post_image_table/trivial/mod.rs new file mode 100644 index 000000000..4f9f8ea22 --- /dev/null +++ b/crates/zkwasm/src/circuits/post_image_table/trivial/mod.rs @@ -0,0 +1,43 @@ +use std::marker::PhantomData; + +use halo2_proofs::arithmetic::FieldExt; +use halo2_proofs::circuit::Cell; +use halo2_proofs::circuit::Layouter; +use halo2_proofs::plonk::Error; + +use crate::circuits::image_table::ImageTableLayouter; + +use super::PostImageTableChipTrait; +use super::PostImageTableConfigTrait; + +#[derive(Clone)] +pub(in crate::circuits) struct TrivialPostImageTableConfig { + _mark: PhantomData, +} + +impl PostImageTableConfigTrait for TrivialPostImageTableConfig { + fn configure(_meta: &mut halo2_proofs::plonk::ConstraintSystem) -> Self { + Self { _mark: PhantomData } + } +} + +pub(in crate::circuits) struct TrivialPostImageTableChip { + _mark: PhantomData, +} + +impl PostImageTableChipTrait> + for TrivialPostImageTableChip +{ + fn new(_config: TrivialPostImageTableConfig) -> Self { + Self { _mark: PhantomData } + } + + fn assign( + self, + layouter: &mut impl Layouter, + image_table: ImageTableLayouter, + permutation_cells: ImageTableLayouter, + ) -> Result<(), Error> { + Ok(()) + } +} diff --git a/crates/zkwasm/src/circuits/test_circuit/mod.rs b/crates/zkwasm/src/circuits/test_circuit/mod.rs index edbc0e8c2..f7db39cf6 100644 --- a/crates/zkwasm/src/circuits/test_circuit/mod.rs +++ b/crates/zkwasm/src/circuits/test_circuit/mod.rs @@ -26,6 +26,10 @@ use crate::circuits::image_table::ImageTableLayouter; use crate::circuits::jtable::JumpTableChip; use crate::circuits::jtable::JumpTableConfig; use crate::circuits::mtable::MemoryTableConfig; +use crate::circuits::post_image_table::PostImageTableChip; +use crate::circuits::post_image_table::PostImageTableChipTrait; +use crate::circuits::post_image_table::PostImageTableConfig; +use crate::circuits::post_image_table::PostImageTableConfigTrait; use crate::circuits::rtable::RangeTableChip; use crate::circuits::rtable::RangeTableConfig; use crate::circuits::utils::table_entry::EventTableWithMemoryInfo; @@ -58,8 +62,10 @@ const RESERVE_ROWS: usize = crate::circuits::bit_table::STEP_SIZE; #[derive(Clone)] pub struct TestCircuitConfig { - rtable: RangeTableConfig, pre_image_table: ImageTableConfig, + post_image_table: PostImageTableConfig, + + rtable: RangeTableConfig, _mtable: MemoryTableConfig, jtable: JumpTableConfig, etable: EventTableConfig, @@ -97,8 +103,10 @@ impl Circuit for TestCircuit { let mut cols = [(); VAR_COLUMNS].map(|_| meta.advice_column()).into_iter(); - let rtable = RangeTableConfig::configure(meta); let pre_image_table = ImageTableConfig::configure(meta); + let post_image_table = PostImageTableConfig::configure(meta); + + let rtable = RangeTableConfig::configure(meta); let mtable = MemoryTableConfig::configure(meta, &mut cols, &rtable, &pre_image_table); let jtable = JumpTableConfig::configure(meta, &mut cols); let external_host_call_table = ExternalHostCallTableConfig::configure(meta); @@ -138,8 +146,10 @@ impl Circuit for TestCircuit { debug!("max_available_rows: {:?}", max_available_rows); Self::Config { - rtable, pre_image_table, + post_image_table, + + rtable, // TODO: open mtable _mtable: mtable, jtable, @@ -162,6 +172,8 @@ impl Circuit for TestCircuit { let rchip = RangeTableChip::new(config.rtable); let pre_image_chip = ImageTableChip::new(config.pre_image_table); + let post_image_chip = PostImageTableChip::new(config.post_image_table); + // TODO: open mtable // let mchip = MemoryTableChip::new(config.mtable, config.max_available_rows); let jchip = JumpTableChip::new(config.jtable, config.max_available_rows); @@ -287,6 +299,11 @@ impl Circuit for TestCircuit { )? ); + exec_with_profile!( + || "Assign Post Image Table", + post_image_chip.assign(&mut layouter, todo!(), todo!(),)? + ); + end_timer!(assign_timer); Ok(())