diff --git a/Cargo.lock b/Cargo.lock index b079eeb8e..2b3456808 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -71,9 +71,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.72" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b13c32d80ecc7ab747b80c3784bce54ee8a7a0cc4fbda9bf4cda2cf6fe90854" +checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" dependencies = [ "backtrace", ] @@ -353,7 +353,7 @@ dependencies = [ [[package]] name = "circuits-batcher" version = "0.1.0" -source = "git+https://github.com/DelphinusLab/continuation-batcher.git#174d270816af2fd19fb28e509b84845325d70792" +source = "git+https://github.com/DelphinusLab/continuation-batcher.git#b26f281c2f7e4d673e873f2c05accd29952eb332" dependencies = [ "anyhow", "ark-std 0.4.0", @@ -623,14 +623,13 @@ name = "delphinus-cli" version = "0.1.0" dependencies = [ "anyhow", - "ark-std 0.3.0", + "ark-std 0.4.0", "circuits-batcher", "clap", "delphinus-host", "delphinus-zkwasm", "env_logger", "halo2_proofs", - "halo2aggregator-s", "hex", "log", "md5", @@ -647,13 +646,12 @@ name = "delphinus-host" version = "0.1.0" dependencies = [ "anyhow", - "ark-std 0.3.0", + "ark-std 0.4.0", "bitvec", "delphinus-zkwasm", "downcast-rs", "ff", "halo2_proofs", - "halo2aggregator-s", "hex", "lazy_static", "log", @@ -680,13 +678,12 @@ name = "delphinus-zkwasm" version = "0.1.0" dependencies = [ "anyhow", - "ark-std 0.3.0", + "ark-std 0.4.0", "bitvec", "cfg-if 1.0.0", "downcast-rs", "ff", "halo2_proofs", - "halo2aggregator-s", "hex", "lazy_static", "log", @@ -1180,7 +1177,7 @@ dependencies = [ [[package]] name = "halo2ecc-s" version = "0.3.2" -source = "git+https://github.com/DelphinusLab/halo2ecc-s.git?tag=bisect-lookup-0.4.0#eef56995f5729083d54b89faa30d5f90277acc22" +source = "git+https://github.com/lanbones/halo2ecc-s.git?tag=bisect-lookup-0.4.0#eef56995f5729083d54b89faa30d5f90277acc22" dependencies = [ "halo2_proofs", "num-bigint", @@ -1191,8 +1188,9 @@ dependencies = [ [[package]] name = "halo2ecc-s" version = "0.3.2" -source = "git+https://github.com/lanbones/halo2ecc-s.git?tag=bisect-lookup-0.4.0#eef56995f5729083d54b89faa30d5f90277acc22" +source = "git+https://github.com/DelphinusLab/halo2ecc-s.git?tag=bisect-lookup-0.5.7#c6b5e8e190431c1206cbdc4b13228eb7bc409283" dependencies = [ + "ark-std 0.4.0", "halo2_proofs", "num-bigint", "num-integer", @@ -3490,15 +3488,16 @@ dependencies = [ [[package]] name = "zkwasm-host-circuits" version = "0.1.0" -source = "git+https://github.com/DelphinusLab/zkWasm-host-circuits.git#d32de5ec45141c030d335b473cf9333fa405aebc" +source = "git+https://github.com/DelphinusLab/zkWasm-host-circuits.git?branch=xgao/dynamic-db-uri#5b253d62e68ef0c227cc4aa3fc99cf35f81c583d" dependencies = [ - "ark-std 0.3.0", + "anyhow", + "ark-std 0.4.0", "cfg-if 1.0.0", "circuits-batcher", "clap", "ff", "halo2_proofs", - "halo2ecc-s 0.3.2 (git+https://github.com/DelphinusLab/halo2ecc-s.git?tag=bisect-lookup-0.4.0)", + "halo2ecc-s 0.3.2 (git+https://github.com/DelphinusLab/halo2ecc-s.git?tag=bisect-lookup-0.5.7)", "hex", "itertools", "lazy_static", @@ -3519,7 +3518,7 @@ dependencies = [ [[package]] name = "zkwasm-prover" version = "0.1.0" -source = "git+ssh://git@github.com/xgaozoyoe/zkWasm-prover.git#4e37b117d4976f2261b00de1cc72f864e5a933b9" +source = "git+https://github.com/DelphinusLab/zkWasm-prover.git#5f352e1852cc27af47378c4a355d36d000edecb1" dependencies = [ "ark-std 0.4.0", "cc", @@ -3532,5 +3531,4 @@ dependencies = [ "libc", "rand", "rayon", - "thread_local", ] diff --git a/Cargo.toml b/Cargo.toml index 3fbdcb3da..dd0c51694 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,14 +4,14 @@ exclude = ["third-party/wasmi", "crates/playground"] resolver = "2" [workspace.dependencies] -anyhow = { version = "1.0.68", features = ["backtrace"] } +anyhow = { version = "1.0.86", features = ["backtrace"] } cfg-if = "1.0.0" -halo2aggregator-s = { git = "https://github.com/DelphinusLab/halo2aggregator-s.git", 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"] } wasmi = { path = "third-party/wasmi" } circuits-batcher = { git = "https://github.com/DelphinusLab/continuation-batcher.git" } -zkwasm-host-circuits = { git = "https://github.com/DelphinusLab/zkWasm-host-circuits.git" } +zkwasm-host-circuits = { git = "https://github.com/DelphinusLab/zkWasm-host-circuits.git", branch="xgao/dynamic-db-uri" } [profile.dev] opt-level = 3 + diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 51b182824..b76c8c99a 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -ark-std = { version = "0.3.0", features = ["print-trace"] } +ark-std = { version = "0.4.0"} env_logger = "0.9.3" log = "0.4.17" md5 = "0.7.0" @@ -20,7 +20,6 @@ serde_json = "1.0" delphinus-zkwasm = { path = "../zkwasm" } delphinus-host = { path = "../host" } anyhow.workspace = true -halo2aggregator-s.workspace = true halo2_proofs.workspace = true wasmi.workspace = true circuits-batcher.workspace = true @@ -30,4 +29,4 @@ default = [] perf = ["circuits-batcher/perf"] cuda = ["delphinus-zkwasm/cuda"] uniform-circuit = ["delphinus-zkwasm/uniform-circuit"] - +profile = ["ark-std/print-trace"] diff --git a/crates/cli/src/app_builder.rs b/crates/cli/src/app_builder.rs index dfb7857bb..80c8beef9 100644 --- a/crates/cli/src/app_builder.rs +++ b/crates/cli/src/app_builder.rs @@ -154,14 +154,14 @@ pub trait AppBuilder: CommandBuilder { wasm_binary, (), phantom_functions, - &output_dir, + ¶m_dir, ), HostMode::STANDARD => exec_image_checksum::( zkwasm_k, wasm_binary, HostEnvConfig::default(), phantom_functions, - &output_dir, + ¶m_dir, ), }, diff --git a/crates/cli/src/exec.rs b/crates/cli/src/exec.rs index 640413bfd..ecfb54198 100644 --- a/crates/cli/src/exec.rs +++ b/crates/cli/src/exec.rs @@ -1,5 +1,6 @@ use anyhow::Result; use circuits_batcher::args::HashType::Poseidon; +use circuits_batcher::args::OpenSchema; use circuits_batcher::proof::ParamsCache; use circuits_batcher::proof::ProofGenerationInfo; use circuits_batcher::proof::ProofInfo; @@ -8,12 +9,12 @@ use circuits_batcher::proof::ProvingKeyCache; use delphinus_zkwasm::loader::ZkWasmLoader; use delphinus_zkwasm::runtime::host::HostEnvBuilder; use halo2_proofs::pairing::bn256::Bn256; -use halo2_proofs::plonk::verify_proof_with_shplonk; -use halo2_proofs::plonk::SingleVerifier; +use halo2_proofs::pairing::bn256::G1Affine; +use halo2_proofs::poly::commitment::Params; use halo2_proofs::poly::commitment::ParamsVerifier; -use halo2aggregator_s::circuits::utils::load_or_build_unsafe_params; -use halo2aggregator_s::transcript::poseidon::PoseidonRead; use log::info; +use std::fs::File; +use std::io; use std::io::Write; use std::path::PathBuf; @@ -29,22 +30,27 @@ pub fn exec_setup( ) -> Result<()> { info!("Setup Params and VerifyingKey"); - macro_rules! prepare_params { - ($k: expr) => {{ - let params_path = ¶m_dir.join(format!("K{}.params", $k)); + let prepare_params = |k: u32| { + let params_path = ¶m_dir.join(format!("K{}.params", k)); - if params_path.exists() { - info!("Found Params with K = {} at {:?}", $k, params_path); - } else { - info!("Create Params with K = {} to {:?}", $k, params_path); - } + if params_path.exists() { + info!("Found Params with K = {} at {:?}", k, params_path); - load_or_build_unsafe_params::($k, Some(params_path)) - }}; - } + Ok::<_, io::Error>(Params::read(&mut File::open(params_path)?)?) + } else { + info!("Create Params with K = {} to {:?}", k, params_path); + + let params = Params::::unsafe_setup::(k); + + let mut fd = std::fs::File::create(params_path)?; + params.write(&mut fd)?; + + Ok(params) + } + }; - let params = prepare_params!(zkwasm_k); - prepare_params!(aggregate_k); + let params = prepare_params(zkwasm_k)?; + prepare_params(aggregate_k)?; // Setup ZkWasm Vkey { @@ -75,7 +81,7 @@ pub fn exec_image_checksum( wasm_binary: Vec, hostenv: Builder::HostConfig, phantom_functions: Vec, - output_dir: &PathBuf, + params_dir: &PathBuf, ) -> Result<()> where Builder: HostEnvBuilder, @@ -86,10 +92,9 @@ where phantom_functions, )?; - let params = load_or_build_unsafe_params::( - zkwasm_k, - Some(&output_dir.join(format!("K{}.params", zkwasm_k))), - ); + let params = Params::read(&mut File::open( + ¶ms_dir.join(format!("K{}.params", zkwasm_k)), + )?)?; let checksum = loader.checksum(¶ms, hostenv)?; assert_eq!(checksum.len(), 1); @@ -98,7 +103,7 @@ where println!("image checksum: {:?}", checksum); let mut fd = - std::fs::File::create(&output_dir.join(format!("checksum.data",)).as_path()).unwrap(); + std::fs::File::create(¶ms_dir.join(format!("checksum.data",)).as_path()).unwrap(); write!(fd, "{:?}", checksum)?; @@ -170,7 +175,7 @@ pub fn exec_create_proof( &mut pkey_cache, &mut param_cache, circuits_batcher::args::HashType::Poseidon, - circuits_batcher::args::OpenSchema::GWC, + circuits_batcher::args::OpenSchema::Shplonk, ); prover.save_proof_data(&vec![instances], &transcript, output_dir); @@ -193,10 +198,9 @@ pub fn exec_verify_proof( let proofloadinfo = ProofGenerationInfo::load(&load_info); let proofs: Vec> = ProofInfo::load_proof(&output_dir, ¶m_dir, &proofloadinfo); - let params = load_or_build_unsafe_params::( - proofloadinfo.k as u32, - Some(¶m_dir.join(format!("K{}.params", proofloadinfo.k))), - ); + let params = Params::read(&mut File::open( + ¶m_dir.join(format!("K{}.params", proofloadinfo.k)), + )?)?; let mut public_inputs_size = 0; for proof in proofs.iter() { public_inputs_size = usize::max( @@ -210,15 +214,7 @@ pub fn exec_verify_proof( let params_verifier: ParamsVerifier = params.verifier(public_inputs_size).unwrap(); for (_, proof) in proofs.into_iter().enumerate() { - let strategy = SingleVerifier::new(¶ms_verifier); - verify_proof_with_shplonk::( - ¶ms_verifier, - &proof.vkey, - strategy, - &[&proof.instances.iter().map(|x| &x[..]).collect::>()[..]], - &mut PoseidonRead::init(&proof.transcripts[..]), - ) - .unwrap(); + proof.verify_proof(¶ms_verifier, OpenSchema::GWC)?; } info!("Verifing proof passed"); diff --git a/crates/cli/test_cli.sh b/crates/cli/test_cli.sh index 5206903b0..d68502fc2 100755 --- a/crates/cli/test_cli.sh +++ b/crates/cli/test_cli.sh @@ -7,8 +7,8 @@ rm -rf params/*.data rm -rf output/*.data # Single test -RUST_LOG=info cargo run --release --features cuda -- --host default -k 18 --function zkmain --param ./params --output ./output --wasm ../zkwasm/wasm/wasm_output.wasm setup -RUST_LOG=info cargo run --release --features cuda -- --host default -k 18 --function zkmain --param ./params --output ./output --wasm ../zkwasm/wasm/wasm_output.wasm checksum +RUST_LOG=info cargo run --release --features cuda -- --host default -k 22 --function zkmain --param ./params --output ./output --wasm ../zkwasm/wasm/wasm_output.wasm setup +RUST_LOG=info cargo run --release --features cuda -- --host default -k 22 --function zkmain --param ./params --output ./output --wasm ../zkwasm/wasm/wasm_output.wasm checksum -RUST_LOG=info cargo run --release --features cuda -- --host default -k 18 --function zkmain --param ./params --output ./output --wasm ../zkwasm/wasm/wasm_output.wasm single-prove --public 133:i64 --public 2:i64 -RUST_LOG=info cargo run --release --features cuda -- --host default -k 18 --function zkmain --param ./params --output ./output --wasm ../zkwasm/wasm/wasm_output.wasm single-verify +RUST_LOG=info cargo run --release --features cuda -- --host default -k 22 --function zkmain --param ./params --output ./output --wasm ../zkwasm/wasm/wasm_output.wasm single-prove --public 133:i64 --public 2:i64 +RUST_LOG=info cargo run --release --features cuda -- --host default -k 22 --function zkmain --param ./params --output ./output --wasm ../zkwasm/wasm/wasm_output.wasm single-verify diff --git a/crates/host/Cargo.toml b/crates/host/Cargo.toml index 15ee420da..e7a4d381f 100644 --- a/crates/host/Cargo.toml +++ b/crates/host/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -ark-std = { version = "0.3.0", features = ["print-trace"] } +ark-std = { version = "0.4.0"} bitvec = "1.0.1" downcast-rs = "1.2.0" hex = "0.4.3" @@ -29,7 +29,6 @@ sha2 = "0.10.6" poseidon = { git = "https://github.com/DelphinusLab/poseidon" } anyhow.workspace = true -halo2aggregator-s.workspace = true halo2_proofs.workspace = true parity-wasm.workspace = true wasmi.workspace = true @@ -43,3 +42,4 @@ rusty-fork = "0.3.0" [features] default = [] cuda = ["halo2_proofs/cuda", "specs/cuda"] +profile = ["ark-std/print-trace"] diff --git a/crates/host/src/host/merkle_helper/merkle.rs b/crates/host/src/host/merkle_helper/merkle.rs index b3a3ab5e9..32114fb55 100644 --- a/crates/host/src/host/merkle_helper/merkle.rs +++ b/crates/host/src/host/merkle_helper/merkle.rs @@ -124,14 +124,14 @@ impl MerkleContext { .mongo_merkle .as_ref() .expect("merkle db not initialized"); - let (leaf, _) = mt - .get_leaf_with_proof(index) - .expect("Unexpected failure: get leaf fail"); - let values = leaf.data_as_u64(); if self.data_cursor == 0 { + let (leaf, _) = mt + .get_leaf_with_proof(index) + .expect("Unexpected failure: get leaf fail"); + let values = leaf.data_as_u64(); self.data = values; } - let v = values[self.data_cursor]; + let v = self.data[self.data_cursor]; self.data_cursor += 1; return v; } diff --git a/crates/playground/Cargo.lock b/crates/playground/Cargo.lock index 8ea6d013e..3b6e5788d 100644 --- a/crates/playground/Cargo.lock +++ b/crates/playground/Cargo.lock @@ -566,7 +566,6 @@ dependencies = [ "downcast-rs", "ff", "halo2_proofs", - "halo2aggregator-s", "hex", "lazy_static", "log", diff --git a/crates/zkwasm/Cargo.toml b/crates/zkwasm/Cargo.toml index ef089fb6a..c49373865 100644 --- a/crates/zkwasm/Cargo.toml +++ b/crates/zkwasm/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -ark-std = { version = "0.3.0", features = ["print-trace"] } +ark-std = { version = "0.4.0"} bitvec = "1.0.1" downcast-rs = "1.2.0" hex = "0.4.3" @@ -29,7 +29,6 @@ rayon = "1.5" anyhow.workspace = true cfg-if.workspace = true -halo2aggregator-s.workspace = true halo2_proofs.workspace = true parity-wasm.workspace = true wasmi.workspace = true @@ -44,4 +43,5 @@ rusty-fork = "0.3.0" [features] default = [] cuda = ["halo2_proofs/cuda", "specs/cuda"] +profile = ["ark-std/print-trace"] uniform-circuit = [] diff --git a/crates/zkwasm/src/circuits/etable/assign.rs b/crates/zkwasm/src/circuits/etable/assign.rs index 304edcf78..a1ed8badf 100644 --- a/crates/zkwasm/src/circuits/etable/assign.rs +++ b/crates/zkwasm/src/circuits/etable/assign.rs @@ -1,9 +1,11 @@ use ark_std::end_timer; use ark_std::start_timer; +use ark_std::Zero; use halo2_proofs::arithmetic::FieldExt; use halo2_proofs::circuit::Cell; use halo2_proofs::plonk::Error; use log::debug; +use num_bigint::BigUint; use specs::configure_table::ConfigureTable; use specs::itable::InstructionTable; use specs::itable::OpcodeClassPlain; @@ -37,14 +39,12 @@ impl EventTableChip { op_configs: &BTreeMap>>, itable: &InstructionTable, event_table: &EventTableWithMemoryInfo, - ) -> Vec<(u32, u32)> { + ) -> Vec<(u32, BigUint)> { let mut rest_ops = vec![]; - event_table - .0 - .iter() - .rev() - .fold((0, 0), |(rest_mops_sum, rest_jops_sum), entry| { + event_table.0.iter().rev().fold( + (0, BigUint::zero()), + |(rest_mops_sum, rest_jops_sum), entry| { let instruction = entry.eentry.get_instruction(itable); let op_config = op_configs.get(&((&instruction.opcode).into())).unwrap(); @@ -54,10 +54,11 @@ impl EventTableChip { rest_jops_sum + op_config.0.jops(), ); - rest_ops.push(acc); + rest_ops.push(acc.clone()); acc - }); + }, + ); rest_ops.reverse(); @@ -99,7 +100,7 @@ impl EventTableChip { &self, ctx: &mut Context<'_, F>, rest_mops: u32, - rest_jops: u32, + rest_jops: &BigUint, ) -> Result<(Cell, Cell), Error> { let rest_mops_cell = self .config @@ -111,7 +112,7 @@ impl EventTableChip { .config .common_config .rest_jops_cell - .assign(ctx, F::from(rest_jops as u64))?; + .assign(ctx, bn_to_field(rest_jops))?; Ok((rest_mops_cell.cell(), rest_mops_jell.cell())) } @@ -124,7 +125,7 @@ impl EventTableChip { event_table: &EventTableWithMemoryInfo, configure_table: &ConfigureTable, fid_of_entry: u32, - rest_ops: Vec<(u32, u32)>, + rest_ops: Vec<(u32, BigUint)>, ) -> Result<(Cell, Cell, Cell), Error> { macro_rules! assign_advice { ($cell:ident, $value:expr) => { @@ -274,7 +275,7 @@ impl EventTableChip { .collect::>(); println!("instruction length {}", instructions.len()); - let chunk_len = instructions.len() / 4; + let chunk_len = instructions.len() / 8; let chunk_len = if chunk_len == 0 { instructions.len() } else { @@ -329,7 +330,7 @@ impl EventTableChip { self.config .common_config .rest_jops_cell - .assign(ctx, F::from(*rest_jops as u64))?; + .assign(ctx, bn_to_field(rest_jops))?; self.config .common_config .input_index_cell @@ -447,7 +448,9 @@ impl EventTableChip { let (rest_mops_cell, rest_jops_cell) = self.assign_rest_ops_first_step( ctx, rest_ops.first().map_or(0u32, |(rest_mops, _)| *rest_mops), - rest_ops.first().map_or(0u32, |(_, rest_jops)| *rest_jops), + rest_ops + .first() + .map_or(&BigUint::zero(), |(_, rest_jops)| rest_jops), )?; ctx.reset(); diff --git a/crates/zkwasm/src/circuits/etable/mod.rs b/crates/zkwasm/src/circuits/etable/mod.rs index 1ee8ecc68..981447bf5 100644 --- a/crates/zkwasm/src/circuits/etable/mod.rs +++ b/crates/zkwasm/src/circuits/etable/mod.rs @@ -46,6 +46,7 @@ use crate::foreign::wasm_input_helper::etable_op_configure::ETableWasmInputHelpe use crate::foreign::EventTableForeignCallConfigBuilder; use crate::foreign::ForeignTableConfig; use crate::foreign::InternalHostPluginBuilder; +use ark_std::Zero; use halo2_proofs::arithmetic::FieldExt; use halo2_proofs::plonk::Advice; use halo2_proofs::plonk::Column; @@ -54,6 +55,7 @@ use halo2_proofs::plonk::Error; use halo2_proofs::plonk::Expression; use halo2_proofs::plonk::Fixed; use halo2_proofs::plonk::VirtualCells; +use num_bigint::BigUint; use specs::encode::instruction_table::encode_instruction_table_entry; use specs::etable::EventTableEntry; use specs::itable::OpcodeClass; @@ -78,7 +80,7 @@ pub struct EventTableCommonConfig { ops: [AllocatedBitCell; OP_CAPABILITY], rest_mops_cell: AllocatedCommonRangeCell, - rest_jops_cell: AllocatedCommonRangeCell, + rest_jops_cell: AllocatedUnlimitedCell, pub(crate) input_index_cell: AllocatedCommonRangeCell, pub(crate) context_input_index_cell: AllocatedCommonRangeCell, pub(crate) context_output_index_cell: AllocatedCommonRangeCell, @@ -142,8 +144,8 @@ pub trait EventTableOpcodeConfig { fn jops_expr(&self, _meta: &mut VirtualCells<'_, F>) -> Option> { None } - fn jops(&self) -> u32 { - 0 + fn jops(&self) -> BigUint { + BigUint::zero() } fn mops(&self, _meta: &mut VirtualCells<'_, F>) -> Option> { None @@ -241,7 +243,7 @@ impl EventTableConfig { let enabled_cell = allocator.alloc_bit_cell(); let rest_mops_cell = allocator.alloc_common_range_cell(); - let rest_jops_cell = allocator.alloc_common_range_cell(); + let rest_jops_cell = allocator.alloc_unlimited_cell(); let input_index_cell = allocator.alloc_common_range_cell(); let context_input_index_cell = allocator.alloc_common_range_cell(); let context_output_index_cell = allocator.alloc_common_range_cell(); @@ -255,16 +257,28 @@ impl EventTableConfig { let maximal_memory_pages_cell = allocator.alloc_common_range_cell(); // We only need to enable equality for the cells of states - let used_common_range_cells_for_state = allocator - .free_cells - .get(&EventTableCellType::CommonRange) - .unwrap(); - allocator.enable_equality( - meta, - &EventTableCellType::CommonRange, - used_common_range_cells_for_state.0 - + (used_common_range_cells_for_state.1 != 0) as usize, - ); + { + let used_common_range_cells_for_state = allocator + .free_cells + .get(&EventTableCellType::CommonRange) + .unwrap(); + allocator.enable_equality( + meta, + &EventTableCellType::CommonRange, + used_common_range_cells_for_state.0 + + (used_common_range_cells_for_state.1 != 0) as usize, + ); + + let used_unlimited_cells_for_state = allocator + .free_cells + .get(&EventTableCellType::Unlimited) + .unwrap(); + allocator.enable_equality( + meta, + &EventTableCellType::Unlimited, + used_unlimited_cells_for_state.0 + (used_unlimited_cells_for_state.1 != 0) as usize, + ); + } let itable_lookup_cell = allocator.alloc_unlimited_cell(); let brtable_lookup_cell = allocator.alloc_unlimited_cell(); diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_call.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_call.rs index 3f6e88976..128a00964 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_call.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_call.rs @@ -15,6 +15,8 @@ use halo2_proofs::arithmetic::FieldExt; use halo2_proofs::plonk::Error; use halo2_proofs::plonk::Expression; use halo2_proofs::plonk::VirtualCells; +use num_bigint::BigUint; +use num_traits::One; use specs::encode::frame_table::encode_frame_table_entry; use specs::encode::opcode::encode_call; use specs::step::StepInfo; @@ -98,8 +100,8 @@ impl EventTableOpcodeConfig for CallConfig { Some(constant_from!(1)) } - fn jops(&self) -> u32 { - 1 + fn jops(&self) -> BigUint { + BigUint::one() } fn next_frame_id( diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_call_indirect.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_call_indirect.rs index 3cd28e4fe..58326dfd2 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_call_indirect.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_call_indirect.rs @@ -16,6 +16,7 @@ use halo2_proofs::plonk::Error; use halo2_proofs::plonk::Expression; use halo2_proofs::plonk::VirtualCells; use num_bigint::BigUint; +use num_traits::One; use specs::encode::br_table::encode_elem_entry; use specs::encode::frame_table::encode_frame_table_entry; use specs::encode::opcode::encode_call_indirect; @@ -188,8 +189,8 @@ impl EventTableOpcodeConfig for CallIndirectConfig { Some(constant_from!(1)) } - fn jops(&self) -> u32 { - 1 + fn jops(&self) -> BigUint { + BigUint::one() } fn next_frame_id( diff --git a/crates/zkwasm/src/circuits/etable/op_configure/op_return.rs b/crates/zkwasm/src/circuits/etable/op_configure/op_return.rs index c3b6e9574..48c3b430f 100644 --- a/crates/zkwasm/src/circuits/etable/op_configure/op_return.rs +++ b/crates/zkwasm/src/circuits/etable/op_configure/op_return.rs @@ -6,18 +6,21 @@ use crate::circuits::etable::EventTableOpcodeConfig; use crate::circuits::etable::EventTableOpcodeConfigBuilder; use crate::circuits::jtable::expression::JtableLookupEntryEncode; use crate::circuits::jtable::JumpTableConfig; +use crate::circuits::jtable::JOPS_SEPARATE; use crate::circuits::utils::bn_to_field; use crate::circuits::utils::step_status::StepStatus; use crate::circuits::utils::table_entry::EventTableEntryWithMemoryInfo; use crate::circuits::utils::Context; use crate::constant; use crate::constant_from; +use crate::constant_from_bn; use halo2_proofs::arithmetic::FieldExt; use halo2_proofs::plonk::Error; use halo2_proofs::plonk::Expression; use halo2_proofs::plonk::VirtualCells; use num_bigint::BigUint; use num_bigint::ToBigUint; +use num_traits::One; use specs::encode::frame_table::encode_frame_table_entry; use specs::etable::EventTableEntry; use specs::itable::OpcodeClass; @@ -206,11 +209,11 @@ impl EventTableOpcodeConfig for ReturnConfig { } fn jops_expr(&self, _meta: &mut VirtualCells<'_, F>) -> Option> { - Some(constant_from!(self.jops())) + Some(constant_from_bn!(&self.jops())) } - fn jops(&self) -> u32 { - 1 + fn jops(&self) -> BigUint { + BigUint::one() << JOPS_SEPARATE } fn next_frame_id( diff --git a/crates/zkwasm/src/circuits/jtable/assign.rs b/crates/zkwasm/src/circuits/jtable/assign.rs index 89fab14ca..3580e6dcd 100644 --- a/crates/zkwasm/src/circuits/jtable/assign.rs +++ b/crates/zkwasm/src/circuits/jtable/assign.rs @@ -1,11 +1,14 @@ use halo2_proofs::arithmetic::FieldExt; use halo2_proofs::circuit::Cell; use halo2_proofs::plonk::Error; +use num_bigint::BigUint; +use num_traits::One; use specs::jtable::JumpTable; use specs::jtable::StaticFrameEntry; use super::JtableOffset; use super::JumpTableChip; +use super::JOPS_SEPARATE; use crate::circuits::utils::bn_to_field; use crate::circuits::utils::Context; @@ -64,7 +67,7 @@ impl JumpTableChip { fn assign_static_entries( &self, ctx: &mut Context<'_, F>, - rest_jops: &mut u64, + rest_jops: &mut BigUint, static_entries: &Vec, ) -> Result, Error> { let mut static_entries = static_entries.clone(); @@ -108,7 +111,7 @@ impl JumpTableChip { || "jtable rest", self.config.data, ctx.offset, - || Ok((*rest_jops).into()), + || Ok(bn_to_field(rest_jops)), )?; ctx.next(); @@ -126,7 +129,7 @@ impl JumpTableChip { cells.push((enable_cell, entry_cell)); if entry.enable { - *rest_jops -= 1; + *rest_jops -= BigUint::one() << JOPS_SEPARATE; } } @@ -136,11 +139,11 @@ impl JumpTableChip { fn assign_jtable_entries( &self, ctx: &mut Context<'_, F>, - rest_jops: &mut u64, + rest_jops: &mut BigUint, jtable: &JumpTable, ) -> Result<(), Error> { for entry in jtable.entries().iter() { - let rest_f = (*rest_jops).into(); + let rest_f = bn_to_field(rest_jops); let entry_f = bn_to_field(&entry.encode()); ctx.region.assign_advice( @@ -167,7 +170,7 @@ impl JumpTableChip { )?; ctx.next(); - *rest_jops -= 2; + *rest_jops -= (BigUint::one() << JOPS_SEPARATE) + BigUint::one(); } { @@ -213,7 +216,9 @@ impl JumpTableChip { self.init(ctx)?; ctx.reset(); - let mut rest_jops = jtable.entries().len() as u64 * 2 + static_entries.len() as u64; + let mut rest_jops = BigUint::one() * jtable.entries().len() + + (BigUint::one() << JOPS_SEPARATE) + * (BigUint::from((static_entries.len() + jtable.entries().len()) as u64)); let frame_table_start_jump_cells = self.assign_static_entries(ctx, &mut rest_jops, static_entries)?; diff --git a/crates/zkwasm/src/circuits/jtable/configure.rs b/crates/zkwasm/src/circuits/jtable/configure.rs index 3a4554711..742d0beab 100644 --- a/crates/zkwasm/src/circuits/jtable/configure.rs +++ b/crates/zkwasm/src/circuits/jtable/configure.rs @@ -1,6 +1,9 @@ use super::JumpTableConfig; +use crate::circuits::jtable::JOPS_SEPARATE; +use crate::circuits::utils::bn_to_field; use crate::circuits::Lookup; use crate::constant_from; +use crate::constant_from_bn; use crate::fixed_curr; use halo2_proofs::arithmetic::FieldExt; use halo2_proofs::plonk::Advice; @@ -8,6 +11,8 @@ use halo2_proofs::plonk::Column; use halo2_proofs::plonk::ConstraintSystem; use halo2_proofs::plonk::Expression; use halo2_proofs::plonk::VirtualCells; +use num_bigint::BigUint; +use num_traits::One; pub trait JTableConstraint { fn configure(&self, meta: &mut ConstraintSystem) { @@ -43,7 +48,9 @@ impl JTableConstraint for JumpTableConfig { fn configure_rest_jops_decrease(&self, meta: &mut ConstraintSystem) { meta.create_gate("c3. jtable rest decrease", |meta| { vec![ - (self.rest(meta) - self.next_rest(meta) - constant_from!(2) + (self.rest(meta) + - self.next_rest(meta) + - constant_from_bn!(&((BigUint::one() << JOPS_SEPARATE) + BigUint::one())) + self.static_bit(meta)) * self.enable(meta) * fixed_curr!(meta, self.sel), diff --git a/crates/zkwasm/src/circuits/jtable/mod.rs b/crates/zkwasm/src/circuits/jtable/mod.rs index a4cd595e2..c2f839ff6 100644 --- a/crates/zkwasm/src/circuits/jtable/mod.rs +++ b/crates/zkwasm/src/circuits/jtable/mod.rs @@ -10,6 +10,9 @@ mod assign; mod configure; pub(crate) mod expression; +// high 128 bit counts 'return' instructions, low 128 bit counts 'call' instructions. +pub(crate) const JOPS_SEPARATE: usize = 128; + pub enum JtableOffset { JtableOffsetEnable = 0, JtableOffsetRest = 1, diff --git a/crates/zkwasm/src/lib.rs b/crates/zkwasm/src/lib.rs index 9cc6ffcc8..d9b90c19c 100644 --- a/crates/zkwasm/src/lib.rs +++ b/crates/zkwasm/src/lib.rs @@ -19,5 +19,4 @@ extern crate lazy_static; extern crate downcast_rs; pub extern crate halo2_proofs; -pub extern crate halo2aggregator_s; pub extern crate zkwasm_host_circuits; diff --git a/crates/zkwasm/src/loader/mod.rs b/crates/zkwasm/src/loader/mod.rs index d7796eedb..2b9578f55 100644 --- a/crates/zkwasm/src/loader/mod.rs +++ b/crates/zkwasm/src/loader/mod.rs @@ -2,18 +2,11 @@ use anyhow::Result; use halo2_proofs::arithmetic::MultiMillerLoop; use halo2_proofs::dev::MockProver; use halo2_proofs::plonk::keygen_vk; -use halo2_proofs::plonk::verify_proof_with_shplonk; -use halo2_proofs::plonk::SingleVerifier; use halo2_proofs::plonk::VerifyingKey; use halo2_proofs::poly::commitment::Params; -use halo2_proofs::poly::commitment::ParamsVerifier; use log::warn; use std::marker::PhantomData; -use halo2aggregator_s::circuits::utils::load_or_create_proof; -use halo2aggregator_s::circuits::utils::TranscriptHash; -use halo2aggregator_s::transcript::poseidon::PoseidonRead; - use specs::ExecutionTable; use specs::Tables; use wasmi::tracer::Tracer; @@ -216,84 +209,19 @@ impl> ZkWasmLoader, - instances: &Vec, - ) -> Result<()> { - let prover = MockProver::run(self.k, circuit, vec![instances.clone()])?; - assert_eq!(prover.verify(), Ok(())); - - Ok(()) - } - - pub fn create_proof( - &self, - params: &Params, - vkey: VerifyingKey, - circuit: ZkWasmCircuit, - instances: &Vec, - ) -> Result> { - Ok(load_or_create_proof::( - ¶ms, - vkey, - circuit, - &[instances], - None, - TranscriptHash::Poseidon, - false, - true, - )) - } - pub fn init_env(&self) -> Result<()> { init_zkwasm_runtime(self.k); Ok(()) } - pub fn verify_proof( + pub fn mock_test( &self, - params: &Params, - vkey: VerifyingKey, - instances: Vec, - proof: Vec, - #[cfg(feature = "uniform-circuit")] config: EnvBuilder::HostConfig, + circuit: &ZkWasmCircuit, + instances: &Vec, ) -> Result<()> { - let params_verifier: ParamsVerifier = params.verifier(instances.len()).unwrap(); - let strategy = SingleVerifier::new(¶ms_verifier); - - verify_proof_with_shplonk( - ¶ms_verifier, - &vkey, - strategy, - &[&[&instances]], - &mut PoseidonRead::init(&proof[..]), - ) - .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 - .iter() - .find(|(k, _)| k == IMAGE_COL_NAME) - .unwrap() - .1; - let img_col_commitment: Vec = - get_advice_commitments_from_transcript::( - &vkey, - &mut PoseidonRead::init(&proof[..]), - ) - .unwrap(); - let checksum = self.checksum(params, config)?; - - assert!(vec![img_col_commitment[img_col_idx as usize]] == checksum) - } + let prover = MockProver::run(self.k, circuit, vec![instances.clone()])?; + assert_eq!(prover.verify(), Ok(())); Ok(()) } @@ -301,12 +229,22 @@ impl> ZkWasmLoader { + pub fn create_proof( + &self, + params: &Params, + pk: &ProvingKey, + circuit: ZkWasmCircuit, + instances: &Vec, + ) -> Result> { + let mut transcript = Blake2bWrite::init(vec![]); + + create_proof( + params, + pk, + &[circuit], + &[&[&instances[..]]], + OsRng, + &mut transcript, + )?; + + Ok(transcript.finalize()) + } + + pub fn verify_proof( + &self, + params: &Params, + vkey: &VerifyingKey, + instances: Vec, + proof: Vec, + #[cfg(feature = "uniform-circuit")] config: EnvBuilder::HostConfig, + ) -> Result<()> { + use halo2_proofs::plonk::verify_proof; + + let params_verifier: ParamsVerifier = params.verifier(instances.len()).unwrap(); + let strategy = SingleVerifier::new(¶ms_verifier); + + verify_proof( + ¶ms_verifier, + &vkey, + strategy, + &[&[&instances]], + &mut Blake2bRead::init(&proof[..]), + ) + .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 + .iter() + .find(|(k, _)| k == IMAGE_COL_NAME) + .unwrap() + .1; + let img_col_commitment: Vec = + get_advice_commitments_from_transcript::( + &vkey, + &mut PoseidonRead::init(&proof[..]), + ) + .unwrap(); + let checksum = self.checksum(params, config)?; + + assert!(vec![img_col_commitment[img_col_idx as usize]] == checksum) + } + + Ok(()) + } + pub(crate) fn bench_test(&self, circuit: ZkWasmCircuit, instances: Vec) { fn prepare_param(k: u32) -> Params { let path = PathBuf::from(format!("test_param.{}.data", k)); @@ -344,11 +351,13 @@ mod tests { let params = prepare_param(self.k); let vkey = self.create_vkey(¶ms, ()).unwrap(); + let pkey = keygen_pk(¶ms, vkey, &circuit).unwrap(); let proof = self - .create_proof(¶ms, vkey.clone(), circuit, &instances) + .create_proof(¶ms, &pkey, circuit, &instances) + .unwrap(); + self.verify_proof(¶ms, pkey.get_vk(), instances, proof) .unwrap(); - self.verify_proof(¶ms, vkey, instances, proof).unwrap(); } } }