Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make uniform circuit as feature and support regex for phantom #208

Merged
merged 4 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exclude = ["third-party/wasmi", "crates/playground"]

[workspace.dependencies]
anyhow = { version = "1.0.68", features = ["backtrace"] }
cfg-if = "1.0.0"
halo2aggregator-s = { git = "https://github.com/DelphinusLab/halo2aggregator-s.git", branch = "main", features = ["unsafe"] }
halo2_proofs = { git = "https://github.com/DelphinusLab/halo2-gpu-specific.git", default-features = true }
parity-wasm = { version = "0.42.0", features = ["sign_ext"] }
Expand Down
1 change: 1 addition & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ circuits-batcher = { git = "https://github.com/DelphinusLab/continuation-batcher
[features]
default = []
cuda = ["delphinus-zkwasm/cuda"]
uniform-circuit = ["delphinus-zkwasm/uniform-circuit"]
18 changes: 10 additions & 8 deletions crates/playground/Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/zkwasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ sha2 = "0.10.6"
poseidon = { git = "https://github.com/lanbones/poseidon" }

anyhow.workspace = true
cfg-if.workspace = true
halo2aggregator-s.workspace = true
halo2_proofs.workspace = true
parity-wasm.workspace = true
Expand All @@ -42,3 +43,4 @@ rusty-fork = "0.3.0"
[features]
default = []
cuda = ["halo2_proofs/cuda", "specs/cuda"]
uniform-circuit = []
50 changes: 36 additions & 14 deletions crates/zkwasm/src/circuits/image_table/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,44 @@ impl<F: FieldExt> ImageTableChip<F> {
|region| {
let mut ctx = Context::new(region);

macro_rules! assign_one_line {
($v: expr) => {{
let cell = ctx
.region
.assign_advice(
|| "image table",
self.config.col,
ctx.offset,
|| Ok($v),
)?
.cell();
cfg_if::cfg_if! {
if #[cfg(feature="uniform-circuit")] {
macro_rules! assign_one_line {
($v: expr) => {{
let cell = ctx
.region
.assign_advice(
|| "image table",
self.config.col,
ctx.offset,
|| Ok($v),
)?
.cell();

ctx.next();
ctx.next();

cell
}};
cell
}};
}
} else {
macro_rules! assign_one_line {
($v: expr) => {{
let cell = ctx
.region
.assign_fixed(
|| "image table",
self.config.col,
ctx.offset,
|| Ok($v),
)?
.cell();

ctx.next();

cell
}};
}
}
}

let entry_fid_cell = assign_one_line!(image_table.entry_fid);
Expand Down
26 changes: 20 additions & 6 deletions crates/zkwasm/src/circuits/image_table/configure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,26 @@ use halo2_proofs::plonk::VirtualCells;
use specs::encode::image_table::ImageTableEncoder;

use super::ImageTableConfig;
use super::IMAGE_COL_NAME;
use crate::curr;

impl<F: FieldExt> ImageTableConfig<F> {
fn expr(&self, meta: &mut VirtualCells<F>) -> Expression<F> {
cfg_if::cfg_if! {
if #[cfg(feature="uniform-circuit")] {
crate::curr!(meta, self.col)
} else {
crate::fixed_curr!(meta, self.col)
}
}
}

pub(in crate::circuits) fn configure(meta: &mut ConstraintSystem<F>) -> Self {
let col = meta.named_advice_column(IMAGE_COL_NAME.to_owned());
cfg_if::cfg_if! {
if #[cfg(feature="uniform-circuit")] {
let col = meta.named_advice_column(super::IMAGE_COL_NAME.to_owned());
} else {
let col = meta.fixed_column();
}
}
meta.enable_equality(col);
Self {
col,
Expand All @@ -29,7 +43,7 @@ impl<F: FieldExt> ImageTableConfig<F> {
meta.lookup_any(key, |meta| {
vec![(
ImageTableEncoder::Instruction.encode(expr(meta)),
curr!(meta, self.col),
self.expr(meta),
)]
});
}
Expand All @@ -43,7 +57,7 @@ impl<F: FieldExt> ImageTableConfig<F> {
meta.lookup_any(key, |meta| {
vec![(
ImageTableEncoder::InitMemory.encode(expr(meta)),
curr!(meta, self.col),
self.expr(meta),
)]
});
}
Expand All @@ -57,7 +71,7 @@ impl<F: FieldExt> ImageTableConfig<F> {
meta.lookup_any(key, |meta| {
vec![(
ImageTableEncoder::BrTable.encode(expr(meta)),
curr!(meta, self.col),
self.expr(meta),
)]
});
}
Expand Down
11 changes: 9 additions & 2 deletions crates/zkwasm/src/circuits/image_table/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use halo2_proofs::arithmetic::FieldExt;
use halo2_proofs::plonk::Advice;
use halo2_proofs::plonk::Column;
use num_bigint::BigUint;
use specs::brtable::BrTable;
Expand Down Expand Up @@ -198,9 +197,17 @@ impl<F: FieldExt> EncodeCompilationTableValues<F> for CompilationTable {
}
}

#[cfg(feature = "uniform-circuit")]
#[derive(Clone)]
pub struct ImageTableConfig<F: FieldExt> {
col: Column<Advice>,
col: Column<halo2_proofs::plonk::Advice>,
_mark: PhantomData<F>,
}

#[cfg(not(feature = "uniform-circuit"))]
#[derive(Clone)]
pub struct ImageTableConfig<F: FieldExt> {
col: Column<halo2_proofs::plonk::Fixed>,
_mark: PhantomData<F>,
}

Expand Down
19 changes: 11 additions & 8 deletions crates/zkwasm/src/loader/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use anyhow::Result;
use halo2_proofs::arithmetic::MultiMillerLoop;
use halo2_proofs::dev::MockProver;
use halo2_proofs::plonk::get_advice_commitments_from_transcript;
use halo2_proofs::plonk::keygen_vk;
use halo2_proofs::plonk::verify_proof;
use halo2_proofs::plonk::SingleVerifier;
Expand All @@ -25,7 +24,6 @@ use crate::checksum::CompilationTableWithParams;
use crate::checksum::ImageCheckSum;
use crate::circuits::config::init_zkwasm_runtime;
use crate::circuits::config::set_zkwasm_k;
use crate::circuits::image_table::IMAGE_COL_NAME;
use crate::circuits::TestCircuit;
use crate::circuits::ZkWasmCircuitBuilder;
use crate::loader::err::Error;
Expand All @@ -51,9 +49,7 @@ pub struct ZkWasmLoader<E: MultiMillerLoop, Arg, EnvBuilder: HostEnvBuilder<Arg
k: u32,
module: wasmi::Module,
phantom_functions: Vec<String>,
_data_arg: PhantomData<Arg>,
_data_builder: PhantomData<EnvBuilder>,
_data_e: PhantomData<E>,
_mark: PhantomData<(Arg, EnvBuilder, E)>,
}

impl<E: MultiMillerLoop, T, EnvBuilder: HostEnvBuilder<Arg = T>> ZkWasmLoader<E, T, EnvBuilder> {
Expand Down Expand Up @@ -115,6 +111,11 @@ impl<E: MultiMillerLoop, T, EnvBuilder: HostEnvBuilder<Arg = T>> ZkWasmLoader<E,
Ok(builder.build_circuit::<E::Scalar>())
}

/// Create a ZkWasm Loader
/// Arguments:
/// - k: the size of circuit
/// - image: wasm binary
/// - phantom_functions: regular expressions of phantom function
pub fn new(k: u32, image: Vec<u8>, phantom_functions: Vec<String>) -> Result<Self> {
set_zkwasm_k(k);

Expand All @@ -124,9 +125,7 @@ impl<E: MultiMillerLoop, T, EnvBuilder: HostEnvBuilder<Arg = T>> ZkWasmLoader<E,
k,
module,
phantom_functions,
_data_e: PhantomData,
_data_builder: PhantomData,
_data_arg: PhantomData,
_mark: PhantomData,
};

loader.precheck()?;
Expand Down Expand Up @@ -255,7 +254,11 @@ impl<E: MultiMillerLoop, T, EnvBuilder: HostEnvBuilder<Arg = T>> ZkWasmLoader<E,
)
.unwrap();

#[cfg(feature = "uniform-circuit")]
{
use crate::circuits::image_table::IMAGE_COL_NAME;
use halo2_proofs::plonk::get_advice_commitments_from_transcript;

let img_col_idx = vkey
.cs
.named_advices
Expand Down
1 change: 1 addition & 0 deletions crates/zkwasm/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod test_wasm_instructions;
mod spec;
mod test_rlp;
mod test_start;
#[cfg(feature = "uniform-circuit")]
mod test_uniform_verifier;

/// Create circuit with trace and run mock test.
Expand Down
2 changes: 1 addition & 1 deletion third-party/wasmi
Submodule wasmi updated 3 files
+1 −0 Cargo.toml
+1 −1 src/module.rs
+9 −4 src/tracer/mod.rs
Loading