diff --git a/.gitignore b/.gitignore index 829691c6..ec2971fb 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,7 @@ /target testdata +Cargo.lock +params +agg.pk +break_points.json \ No newline at end of file diff --git a/snark-verifier-sdk/examples/halo2_lib.rs b/snark-verifier-sdk/examples/halo2_lib.rs new file mode 100644 index 00000000..278a3cd2 --- /dev/null +++ b/snark-verifier-sdk/examples/halo2_lib.rs @@ -0,0 +1,104 @@ +use ark_std::{end_timer, start_timer}; +use halo2_base::gates::builder::{CircuitBuilderStage, BASE_CONFIG_PARAMS}; +use halo2_base::utils::fs::gen_srs; + +use snark_verifier_sdk::halo2::read_snark; +use snark_verifier_sdk::SHPLONK; +use snark_verifier_sdk::{ + gen_pk, + halo2::{aggregation::AggregationCircuit, gen_snark_shplonk}, + Snark, +}; +use std::fs::File; +use std::io::{BufReader, BufWriter}; +use std::path::Path; + +fn read_snark_from_file(file_name: &str) -> Snark { + let snark_path = Path::new(file_name); + let snark = read_snark(snark_path) + .unwrap_or_else(|e| panic!("Snark not found at {snark_path:?}. {e:?}")); + snark +} + +fn gen_agg_break_points(agg_circuit: AggregationCircuit, path: &Path) -> Vec> { + let file = File::open(path); + let break_points = match file { + Ok(file) => { + let reader = BufReader::new(file); + let break_points: Vec> = serde_json::from_reader(reader).unwrap(); + break_points + } + Err(_) => { + let break_points = agg_circuit.break_points(); + let file = File::create(path).unwrap(); + let writer = BufWriter::new(file); + serde_json::to_writer(writer, &break_points).unwrap(); + break_points + } + }; + break_points +} + +fn main() { + let dummy_snark = read_snark_from_file("./examples/halo2_lib_snarks/range.snark"); + + let k = 14u32; + let params = gen_srs(k); + let lookup_bits = k as usize - 1; + BASE_CONFIG_PARAMS.with(|config| { + config.borrow_mut().lookup_bits = Some(lookup_bits); + config.borrow_mut().k = k as usize; + }); + let agg_circuit = AggregationCircuit::new::( + CircuitBuilderStage::Keygen, + None, + lookup_bits, + ¶ms, + vec![dummy_snark.clone()], + true, + ); + agg_circuit.config(k, Some(10)); + + let start0 = start_timer!(|| "gen vk & pk"); + let pk = gen_pk(¶ms, &agg_circuit, Some(Path::new("./examples/agg.pk"))); + end_timer!(start0); + let break_points = gen_agg_break_points(agg_circuit, Path::new("./examples/break_points.json")); + + let snarks = [ + "./examples/halo2_lib_snarks/range.snark", + "./examples/halo2_lib_snarks/halo2_lib.snark", + "./examples/halo2_lib_snarks/poseidon.snark", + ] + .map(|file| read_snark_from_file(file)); + // let snarks = [dummy_snark]; + for (i, snark) in snarks.into_iter().enumerate() { + let agg_circuit = AggregationCircuit::new::( + CircuitBuilderStage::Prover, + Some(break_points.clone()), + lookup_bits, + ¶ms, + vec![snark], + true, + ); + let _snark = gen_snark_shplonk(¶ms, &pk, agg_circuit, None::<&str>); + println!("snark {i} success"); + } + + /* + #[cfg(feature = "loader_evm")] + { + // do one more time to verify + let num_instances = agg_circuit.num_instance(); + let instances = agg_circuit.instances(); + let proof_calldata = gen_evm_proof_shplonk(¶ms, &pk, agg_circuit, instances.clone()); + + let deployment_code = gen_evm_verifier_shplonk::>( + ¶ms, + pk.get_vk(), + num_instances, + Some(Path::new("./examples/standard_plonk.yul")), + ); + evm_verify(deployment_code, instances, proof_calldata); + } + */ +} diff --git a/snark-verifier-sdk/examples/halo2_lib_snarks/halo2_lib.snark b/snark-verifier-sdk/examples/halo2_lib_snarks/halo2_lib.snark new file mode 100644 index 00000000..9a3a4e01 Binary files /dev/null and b/snark-verifier-sdk/examples/halo2_lib_snarks/halo2_lib.snark differ diff --git a/snark-verifier-sdk/examples/halo2_lib_snarks/poseidon.snark b/snark-verifier-sdk/examples/halo2_lib_snarks/poseidon.snark new file mode 100644 index 00000000..815a8231 Binary files /dev/null and b/snark-verifier-sdk/examples/halo2_lib_snarks/poseidon.snark differ diff --git a/snark-verifier-sdk/examples/halo2_lib_snarks/range.snark b/snark-verifier-sdk/examples/halo2_lib_snarks/range.snark new file mode 100644 index 00000000..0f2c62eb Binary files /dev/null and b/snark-verifier-sdk/examples/halo2_lib_snarks/range.snark differ diff --git a/snark-verifier-sdk/examples/standard_plonk.rs b/snark-verifier-sdk/examples/standard_plonk.rs index 35bb573b..d2e58e3d 100644 --- a/snark-verifier-sdk/examples/standard_plonk.rs +++ b/snark-verifier-sdk/examples/standard_plonk.rs @@ -1,22 +1,22 @@ use application::ComputeFlag; use ark_std::{end_timer, start_timer}; -use halo2_base::gates::builder::{set_lookup_bits, CircuitBuilderStage, BASE_CONFIG_PARAMS}; +use halo2_base::gates::builder::{CircuitBuilderStage, BASE_CONFIG_PARAMS}; use halo2_base::halo2_proofs; use halo2_base::halo2_proofs::arithmetic::Field; use halo2_base::halo2_proofs::halo2curves::bn256::Fr; -use halo2_base::halo2_proofs::poly::commitment::Params; use halo2_base::utils::fs::gen_srs; use halo2_proofs::halo2curves as halo2_curves; -use halo2_proofs::plonk::Circuit; + use halo2_proofs::{halo2curves::bn256::Bn256, poly::kzg::commitment::ParamsKZG}; use rand::rngs::OsRng; +use snark_verifier_sdk::SHPLONK; use snark_verifier_sdk::{ - evm::{evm_verify, gen_evm_proof_shplonk, gen_evm_verifier_shplonk}, gen_pk, halo2::{aggregation::AggregationCircuit, gen_snark_shplonk}, Snark, }; -use snark_verifier_sdk::{CircuitExt, SHPLONK}; +use std::fs::File; +use std::io::{BufReader, BufWriter}; use std::path::Path; mod application { @@ -26,7 +26,7 @@ mod application { plonk::{Advice, Circuit, Column, ConstraintSystem, Error, Fixed, Instance}, poly::Rotation, }; - use rand::RngCore; + use snark_verifier_sdk::CircuitExt; #[derive(Clone, Copy)] @@ -149,11 +149,30 @@ fn gen_application_snark(params: &ParamsKZG, flag: ComputeFlag) -> Snark gen_snark_shplonk(params, &pk, circuit, None::<&str>) } +fn gen_agg_break_points(agg_circuit: AggregationCircuit, path: &Path) -> Vec> { + let file = File::open(path); + let break_points = match file { + Ok(file) => { + let reader = BufReader::new(file); + let break_points: Vec> = serde_json::from_reader(reader).unwrap(); + break_points + } + Err(_) => { + let break_points = agg_circuit.break_points(); + let file = File::create(path).unwrap(); + let writer = BufWriter::new(file); + serde_json::to_writer(writer, &break_points).unwrap(); + break_points + } + }; + break_points +} + fn main() { let params_app = gen_srs(8); let dummy_snark = gen_application_snark(¶ms_app, ComputeFlag::All); - let k = 22u32; + let k = 14u32; let params = gen_srs(k); let lookup_bits = k as usize - 1; BASE_CONFIG_PARAMS.with(|config| { @@ -173,7 +192,7 @@ fn main() { let start0 = start_timer!(|| "gen vk & pk"); let pk = gen_pk(¶ms, &agg_circuit, Some(Path::new("./examples/agg.pk"))); end_timer!(start0); - let break_points = agg_circuit.break_points(); + let break_points = gen_agg_break_points(agg_circuit, Path::new("./examples/break_points.json")); let snarks = [ComputeFlag::All, ComputeFlag::SkipFixed, ComputeFlag::SkipCopy] .map(|flag| gen_application_snark(¶ms_app, flag)); diff --git a/snark-verifier-sdk/src/halo2/aggregation.rs b/snark-verifier-sdk/src/halo2/aggregation.rs index b6bbabf1..ae9ce7ee 100644 --- a/snark-verifier-sdk/src/halo2/aggregation.rs +++ b/snark-verifier-sdk/src/halo2/aggregation.rs @@ -300,7 +300,7 @@ impl AggregationCircuit { let ecc_chip = BaseFieldEccChip::new(&fp_chip); let loader = Halo2Loader::new(ecc_chip, builder); - let SnarkAggregationWitness { previous_instances, accumulator, preprocessed_digest } = + let SnarkAggregationWitness { previous_instances, accumulator, preprocessed_digest: _ } = aggregate::(&svk, &loader, &snarks, as_proof.as_slice(), preprocessed_as_witness); let lhs = accumulator.lhs.assigned(); let rhs = accumulator.rhs.assigned();