diff --git a/Cargo.lock b/Cargo.lock index 9f13da24b..2439551d5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -353,7 +353,7 @@ dependencies = [ [[package]] name = "circuits-batcher" version = "0.1.0" -source = "git+https://github.com/DelphinusLab/continuation-batcher.git?branch=para-synthesize#9a416f88d26fda9901d6eee560f6398233cf43b3" +source = "git+https://github.com/DelphinusLab/continuation-batcher.git#ddc922b1e6a1d9f983fad703fae2e79c437db673" dependencies = [ "anyhow", "ark-std 0.4.0", @@ -364,7 +364,6 @@ dependencies = [ "ff", "halo2_proofs", "halo2aggregator-s", - "halo2ecc-s", "hex", "lazy_static", "log", @@ -375,12 +374,12 @@ dependencies = [ "num-derive", "num-integer", "num-traits", - "poseidon", "quote", "rand", "serde", "serde_json", "sha2", + "sha3", "strum", "strum_macros", "uuid", @@ -1134,9 +1133,9 @@ dependencies = [ [[package]] name = "halo2_proofs" version = "0.1.0-beta.1" -source = "git+https://github.com/DelphinusLab/halo2-gpu-specific.git?branch=zhangjunyu/para-synthesize#a382f2d261a0c1279df03944ec06c0953254e564" +source = "git+https://github.com/DelphinusLab/halo2-gpu-specific.git#9a81f601607bdc3ff3360a9ad66885c9796f487b" dependencies = [ - "ark-std 0.3.0", + "ark-std 0.4.0", "blake2b_simd", "bumpalo", "cfg-if 0.1.10", @@ -1146,6 +1145,7 @@ dependencies = [ "ff", "group", "lazy_static", + "log", "memmap", "num", "num-bigint", @@ -1162,24 +1162,36 @@ dependencies = [ [[package]] name = "halo2aggregator-s" version = "0.1.0" -source = "git+https://github.com/DelphinusLab/halo2aggregator-s.git?branch=para-synthesize#78b441b7c0c3ef091239e109d7f06e96f0bec739" +source = "git+https://github.com/DelphinusLab/halo2aggregator-s.git#747788c3331014d8854ed4746c51a1582e5271c1" dependencies = [ "ark-std 0.4.0", "blake2b_simd", "halo2_proofs", - "halo2ecc-s", + "halo2ecc-s 0.3.2 (git+https://github.com/lanbones/halo2ecc-s.git?tag=bisect-lookup-0.4.0)", "lazy_static", "num-bigint", "num-integer", "poseidon", "sha2", + "sha3", "tera", ] [[package]] name = "halo2ecc-s" version = "0.3.2" -source = "git+https://github.com/lanbones/halo2ecc-s.git?branch=para-synthesize#e86efc4791385caf0d07e812c5d006484c536cbf" +source = "git+https://github.com/DelphinusLab/halo2ecc-s.git?tag=bisect-lookup-0.4.0#eef56995f5729083d54b89faa30d5f90277acc22" +dependencies = [ + "halo2_proofs", + "num-bigint", + "num-integer", + "rayon", +] + +[[package]] +name = "halo2ecc-s" +version = "0.3.2" +source = "git+https://github.com/lanbones/halo2ecc-s.git?tag=bisect-lookup-0.4.0#eef56995f5729083d54b89faa30d5f90277acc22" dependencies = [ "halo2_proofs", "num-bigint", @@ -1453,6 +1465,15 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "keccak" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecc2af9a1119c51f12a14607e783cb977bde58bc069ff0c3da1095e635d70654" +dependencies = [ + "cpufeatures", +] + [[package]] name = "kernel32-sys" version = "0.2.2" @@ -1509,9 +1530,9 @@ checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" [[package]] name = "libloading" -version = "0.8.2" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2caa5afb8bf9f3a2652760ce7d4f62d21c4d5a423e68466fca30df82f2330164" +checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19" dependencies = [ "cfg-if 1.0.0", "windows-targets 0.48.1", @@ -2023,7 +2044,7 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "poseidon" version = "0.2.0" -source = "git+https://github.com/DelphinusLab/poseidon?branch=para-synthesize#07db5bfcfafc2fc2144666a0a9a83cd081f3357f" +source = "git+https://github.com/DelphinusLab/poseidon#58835e054512ad7833df6d20479e9e0e034a073b" dependencies = [ "halo2_proofs", "subtle", @@ -2504,6 +2525,16 @@ dependencies = [ "digest", ] +[[package]] +name = "sha3" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75872d278a8f37ef87fa0ddbda7802605cb18344497949862c0d4dcb291eba60" +dependencies = [ + "digest", + "keccak", +] + [[package]] name = "shlex" version = "1.3.0" @@ -3459,7 +3490,7 @@ dependencies = [ [[package]] name = "zkwasm-host-circuits" version = "0.1.0" -source = "git+https://github.com/DelphinusLab/zkWasm-host-circuits.git?branch=para-synthesize#4e13e40ace94b071785d66fe0ff355760890cd3c" +source = "git+https://github.com/DelphinusLab/zkWasm-host-circuits.git#50e6bb6c13669855082dc0166186d9efd70d727f" dependencies = [ "ark-std 0.3.0", "cfg-if 1.0.0", @@ -3467,7 +3498,7 @@ dependencies = [ "clap", "ff", "halo2_proofs", - "halo2ecc-s", + "halo2ecc-s 0.3.2 (git+https://github.com/DelphinusLab/halo2ecc-s.git?tag=bisect-lookup-0.4.0)", "hex", "itertools", "lazy_static", @@ -3488,7 +3519,7 @@ dependencies = [ [[package]] name = "zkwasm-prover" version = "0.1.0" -source = "git+ssh://git@github.com/lanbones/zkWasm-prover.git?branch=zhangjunyu/para-synthesize#37e17876a5e9a39feba73ee1bb9d6382ba4d3918" +source = "git+ssh://git@github.com/lanbones/zkWasm-prover.git#4e37b117d4976f2261b00de1cc72f864e5a933b9" dependencies = [ "ark-std 0.4.0", "cc", diff --git a/Cargo.toml b/Cargo.toml index cd35a2e4b..3fbdcb3da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,12 +6,12 @@ resolver = "2" [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 = "para-synthesize", features = ["unsafe"] } -halo2_proofs = { git = "https://github.com/DelphinusLab/halo2-gpu-specific.git", branch = "zhangjunyu/para-synthesize", default-features = true } +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", branch = "para-synthesize" } -zkwasm-host-circuits = { git = "https://github.com/DelphinusLab/zkWasm-host-circuits.git", branch="para-synthesize" } +circuits-batcher = { git = "https://github.com/DelphinusLab/continuation-batcher.git" } +zkwasm-host-circuits = { git = "https://github.com/DelphinusLab/zkWasm-host-circuits.git" } [profile.dev] opt-level = 3 diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 80a306170..51b182824 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -27,6 +27,7 @@ circuits-batcher.workspace = true [features] default = [] +perf = ["circuits-batcher/perf"] cuda = ["delphinus-zkwasm/cuda"] uniform-circuit = ["delphinus-zkwasm/uniform-circuit"] diff --git a/crates/cli/src/exec.rs b/crates/cli/src/exec.rs index d0022df47..7c6bf0f49 100644 --- a/crates/cli/src/exec.rs +++ b/crates/cli/src/exec.rs @@ -1,18 +1,18 @@ use anyhow::Result; -use circuits_batcher::proof::CircuitInfo; +use circuits_batcher::args::HashType::Poseidon; use circuits_batcher::proof::ParamsCache; use circuits_batcher::proof::ProofInfo; use circuits_batcher::proof::ProofLoadInfo; +use circuits_batcher::proof::ProofPieceInfo; use circuits_batcher::proof::ProvingKeyCache; -use delphinus_zkwasm::circuits::ZkWasmCircuit; use delphinus_zkwasm::loader::ZkWasmLoader; use delphinus_zkwasm::runtime::host::HostEnvBuilder; use halo2_proofs::pairing::bn256::Bn256; -use halo2_proofs::pairing::bn256::Fr; +use halo2_proofs::plonk::verify_proof_with_shplonk; +use halo2_proofs::plonk::SingleVerifier; use halo2_proofs::poly::commitment::ParamsVerifier; use halo2aggregator_s::circuits::utils::load_or_build_unsafe_params; -use halo2aggregator_s::circuits::utils::TranscriptHash; -use halo2aggregator_s::native_verifier; +use halo2aggregator_s::transcript::poseidon::PoseidonRead; use log::info; use std::io::Write; use std::path::PathBuf; @@ -156,25 +156,27 @@ pub fn exec_create_proof<Builder: HostEnvBuilder>( info!("Mock test passed"); } - let circuit: CircuitInfo<Bn256, ZkWasmCircuit<Fr>> = CircuitInfo::new( - circuit, - prefix.to_string(), - vec![instances], - zkwasm_k as usize, - circuits_batcher::args::HashType::Poseidon, - ); + let prover: ProofPieceInfo = ProofPieceInfo::new(prefix.to_string(), 0, instances.len() as u32); - // save the proof load info for the zkwasm circuit - circuit.proofloadinfo.save(output_dir); + let mut param_cache = ParamsCache::<Bn256>::new(5); + let mut pkey_cache = ProvingKeyCache::<Bn256>::new(5); - // Cli saves zkwasm.0.instance.data as the - // first instance file for .loadinfo - // Thus we provide arg index = 0 to generate a - // proof with the first instance file - let mut param_cache = ParamsCache::new(5); - let mut pkey_cache = ProvingKeyCache::new(5); + let mut proof_load_info = ProofLoadInfo::new(prefix, zkwasm_k as usize, Poseidon); - circuit.exec_create_proof(output_dir, param_dir, &mut pkey_cache, 0, &mut param_cache); + prover.exec_create_proof( + &circuit, + &vec![instances], + output_dir.as_path(), + param_dir.as_path(), + format!("K{}.params", zkwasm_k), + zkwasm_k as usize, + &mut pkey_cache, + &mut param_cache, + circuits_batcher::args::HashType::Poseidon, + ); + //prover.mock_proof(k as u32); + proof_load_info.append_single_proof(prover); + proof_load_info.save(output_dir); info!("Proof has been created."); @@ -206,14 +208,16 @@ pub fn exec_verify_proof( } let params_verifier: ParamsVerifier<Bn256> = params.verifier(public_inputs_size).unwrap(); - for (_, proof) in proofs.iter().enumerate() { - native_verifier::verify_single_proof::<Bn256>( + for (_, proof) in proofs.into_iter().enumerate() { + let strategy = SingleVerifier::new(¶ms_verifier); + verify_proof_with_shplonk::<Bn256, _, _, _>( ¶ms_verifier, &proof.vkey, - &proof.instances, - proof.transcripts.clone(), - TranscriptHash::Poseidon, - ); + strategy, + &[&proof.instances.iter().map(|x| &x[..]).collect::<Vec<_>>()[..]], + &mut PoseidonRead::init(&proof.transcripts[..]), + ) + .unwrap(); } info!("Verifing proof passed"); diff --git a/crates/host/Cargo.toml b/crates/host/Cargo.toml index e8558c3ad..15ee420da 100644 --- a/crates/host/Cargo.toml +++ b/crates/host/Cargo.toml @@ -26,7 +26,7 @@ delphinus-zkwasm = { path = "../zkwasm" } ff = "0.12" sha2 = "0.10.6" -poseidon = { git = "https://github.com/DelphinusLab/poseidon", branch="para-synthesize" } +poseidon = { git = "https://github.com/DelphinusLab/poseidon" } anyhow.workspace = true halo2aggregator-s.workspace = true diff --git a/crates/zkwasm/Cargo.toml b/crates/zkwasm/Cargo.toml index aeea414fc..ef089fb6a 100644 --- a/crates/zkwasm/Cargo.toml +++ b/crates/zkwasm/Cargo.toml @@ -24,7 +24,7 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" ff = "0.12" sha2 = "0.10.6" -poseidon = { git = "https://github.com/DelphinusLab/poseidon", branch="para-synthesize" } +poseidon = { git = "https://github.com/DelphinusLab/poseidon" } rayon = "1.5" anyhow.workspace = true diff --git a/crates/zkwasm/src/circuits/test_circuit/mod.rs b/crates/zkwasm/src/circuits/test_circuit/mod.rs index 7715e587a..433d04c4d 100644 --- a/crates/zkwasm/src/circuits/test_circuit/mod.rs +++ b/crates/zkwasm/src/circuits/test_circuit/mod.rs @@ -3,8 +3,8 @@ use std::collections::BTreeMap; use ark_std::end_timer; use ark_std::start_timer; use halo2_proofs::arithmetic::FieldExt; +use halo2_proofs::circuit::floor_planner::FlatFloorPlanner; use halo2_proofs::circuit::Layouter; -use halo2_proofs::circuit::FlatFloorPlanner; use halo2_proofs::plonk::Circuit; use halo2_proofs::plonk::Column; use halo2_proofs::plonk::ConstraintSystem; @@ -148,11 +148,7 @@ impl<F: FieldExt> Circuit<F> for ZkWasmCircuit<F> { } } - fn synthesize( - &self, - config: Self::Config, - layouter: impl Layouter<F>, - ) -> Result<(), Error> { + fn synthesize(&self, config: Self::Config, layouter: impl Layouter<F>) -> Result<(), Error> { let assign_timer = start_timer!(|| "Assign"); let rchip = RangeTableChip::new(config.rtable); @@ -170,7 +166,6 @@ impl<F: FieldExt> Circuit<F> for ZkWasmCircuit<F> { let host_assigner = layouter.clone(); let jme_assigner = layouter.clone(); - let itable = self.tables.compilation_tables.itable.clone_internal(); let config_table = self.tables.compilation_tables.configure_table; @@ -181,7 +176,6 @@ impl<F: FieldExt> Circuit<F> for ZkWasmCircuit<F> { let memory_writing_table: MemoryWritingTable = self.tables.execution_tables.mtable.clone().into(); - let fid_of_entry = self.tables.compilation_tables.fid_of_entry; let (sender, receiver) = std::sync::mpsc::channel(); @@ -190,21 +184,23 @@ impl<F: FieldExt> Circuit<F> for ZkWasmCircuit<F> { let context_inputs = etable.get_context_inputs(); let context_outputs = etable.get_context_outputs(); s.spawn(move |_| { - foreign_assigner.assign_region( - || "foreign helper", - |region| { - for offset in 0..foreign_table_enable_lines() { - region.assign_fixed( - || "foreign table from zero index", - config.foreign_table_from_zero_index, - offset, - || Ok(F::from(offset as u64)), + foreign_assigner + .assign_region( + || "foreign helper", + |region| { + for offset in 0..foreign_table_enable_lines() { + region.assign_fixed( + || "foreign table from zero index", + config.foreign_table_from_zero_index, + offset, + || Ok(F::from(offset as u64)), )?; - } + } - Ok(()) - }, - ).unwrap() + Ok(()) + }, + ) + .unwrap() }); s.spawn(move |_| { exec_with_profile!(|| "Init range chip", rchip.init(&range_assigner).unwrap()); @@ -212,66 +208,60 @@ impl<F: FieldExt> Circuit<F> for ZkWasmCircuit<F> { s.spawn(move |_| { exec_with_profile!( || "Assign context cont chip", - context_chip.assign( - &context_assigner, - &context_inputs, - &context_outputs - ).unwrap() - ); - + context_chip + .assign(&context_assigner, &context_inputs, &context_outputs) + .unwrap() + ); }); s.spawn(move |_| { exec_with_profile!( || "Assign external host call table", - external_host_call_chip.assign( - &host_assigner, - &external_call_table, - ).unwrap() - ); + external_host_call_chip + .assign(&host_assigner, &external_call_table,) + .unwrap() + ); }); s.spawn(move |_| { - jme_assigner.assign_region( - || "jtable mtable etable", - |region| { - let mut ctx = Context::new(region.clone()); - - let etable = exec_with_profile!( - || "Prepare memory info for etable", - EventTableWithMemoryInfo::new( - &etable, - &memory_writing_table, - ) + jme_assigner + .assign_region( + || "jtable mtable etable", + |region| { + let mut ctx = Context::new(region.clone()); + + let etable = exec_with_profile!( + || "Prepare memory info for etable", + EventTableWithMemoryInfo::new(&etable, &memory_writing_table,) ); - let etable_permutation_cells = exec_with_profile!( - || "Assign etable", - echip.assign( - &mut ctx, - &itable.clone().into(), - &etable, - &config_table, - fid_of_entry, + let etable_permutation_cells = exec_with_profile!( + || "Assign etable", + echip.assign( + &mut ctx, + &itable.clone().into(), + &etable, + &config_table, + fid_of_entry, )? ); - sender.send((etable, etable_permutation_cells)).expect("can not send obj in rayon"); - Ok(()) - } - ).unwrap(); + sender + .send((etable, etable_permutation_cells)) + .expect("can not send obj in rayon"); + Ok(()) + }, + ) + .unwrap(); }); }); let (etable, etable_permutation_cells) = receiver.recv().expect("can not receiver obj ..."); - let memory_writing_table: MemoryWritingTable = self.tables.execution_tables.mtable.clone().into(); - let (entry_fid, initial_memory_pages, maximal_memory_pages) = ( etable_permutation_cells.fid_of_entry, etable_permutation_cells.initial_memory_pages, etable_permutation_cells.maximal_memory_pages, - ); - + ); let rest_mops = etable_permutation_cells.rest_mops.clone(); let imtable = self.tables.compilation_tables.imtable.clone(); @@ -279,88 +269,90 @@ impl<F: FieldExt> Circuit<F> for ZkWasmCircuit<F> { let mtable_assigner = layouter.clone(); let jme_assigner = layouter.clone(); - let jtable = self.tables.execution_tables.jtable.clone(); let rest_jops = etable_permutation_cells.rest_jops; let static_jtable = self.tables.compilation_tables.static_jtable.clone(); - let encoded_compilation_table_values = - self.tables + let encoded_compilation_table_values = self + .tables .compilation_tables .encode_compilation_table_values(); - rayon::scope(|s| { s.spawn(move |_| { - mtable_assigner.assign_region( - || "jtable mtable etable", - |region| { - let mut ctx = Context::new(region.clone()); - { - ctx.reset(); - exec_with_profile!( - || "Assign mtable", - mchip.assign( - &mut ctx, - rest_mops, - &memory_writing_table, - &imtable, + mtable_assigner + .assign_region( + || "jtable mtable etable", + |region| { + let mut ctx = Context::new(region.clone()); + { + ctx.reset(); + exec_with_profile!( + || "Assign mtable", + mchip.assign( + &mut ctx, + rest_mops, + &memory_writing_table, + &imtable, )? ); - } - Ok(()) - }).unwrap(); + } + Ok(()) + }, + ) + .unwrap(); }); s.spawn(move |_| { let image_chip = ImageTableChip::new(config.image_table.clone()); - let static_frame_entries = jme_assigner.assign_region( - || "jtable mtable etable", - |region| { - let mut ctx = Context::new(region.clone()); - let static_frame_entries = { - exec_with_profile!( - || "Assign frame table", - jchip.assign( - &mut ctx, - &jtable, - rest_jops, - &static_jtable, - )? + let static_frame_entries = jme_assigner + .assign_region( + || "jtable mtable etable", + |region| { + let mut ctx = Context::new(region.clone()); + let static_frame_entries = { + exec_with_profile!( + || "Assign frame table", + jchip.assign(&mut ctx, &jtable, rest_jops, &static_jtable,)? ) - }; - Ok(static_frame_entries) - } - ).unwrap(); + }; + Ok(static_frame_entries) + }, + ) + .unwrap(); exec_with_profile!( || "Assign Image Table", - image_chip.assign( - &jme_assigner, - encoded_compilation_table_values, - ImageTableLayouter { - entry_fid, - static_frame_entries, - initial_memory_pages, - maximal_memory_pages, - lookup_entries: None - } - ).unwrap() - ); + image_chip + .assign( + &jme_assigner, + encoded_compilation_table_values, + ImageTableLayouter { + entry_fid, + static_frame_entries, + initial_memory_pages, + maximal_memory_pages, + lookup_entries: None + } + ) + .unwrap() + ); }); s.spawn(move |_| { - layouter.assign_region( - || "jtable mtable etable", - |region| { - let mut ctx = Context::new(region.clone()); - exec_with_profile!( - || "Assign bit table", - bit_chip.assign(&mut ctx, &etable)? + layouter + .assign_region( + || "jtable mtable etable", + |region| { + let mut ctx = Context::new(region.clone()); + exec_with_profile!( + || "Assign bit table", + bit_chip.assign(&mut ctx, &etable)? ); - Ok(()) - }, - ).unwrap(); + Ok(()) + }, + ) + .unwrap(); }); }); diff --git a/crates/zkwasm/src/loader/mod.rs b/crates/zkwasm/src/loader/mod.rs index 3a1db65a9..d7796eedb 100644 --- a/crates/zkwasm/src/loader/mod.rs +++ b/crates/zkwasm/src/loader/mod.rs @@ -2,7 +2,7 @@ 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; +use halo2_proofs::plonk::verify_proof_with_shplonk; use halo2_proofs::plonk::SingleVerifier; use halo2_proofs::plonk::VerifyingKey; use halo2_proofs::poly::commitment::Params; @@ -242,6 +242,7 @@ impl<E: MultiMillerLoop, T, EnvBuilder: HostEnvBuilder<Arg = T>> ZkWasmLoader<E, None, TranscriptHash::Poseidon, false, + true, )) } @@ -262,7 +263,7 @@ impl<E: MultiMillerLoop, T, EnvBuilder: HostEnvBuilder<Arg = T>> ZkWasmLoader<E, let params_verifier: ParamsVerifier<E> = params.verifier(instances.len()).unwrap(); let strategy = SingleVerifier::new(¶ms_verifier); - verify_proof( + verify_proof_with_shplonk( ¶ms_verifier, &vkey, strategy,