Skip to content

Commit

Permalink
fix: save break points when generating agg circuit for first time (#23)
Browse files Browse the repository at this point in the history
* fix: save break points when generating agg circuit for first time

* chore: add circuit files to gitignore

* feat: halo2-lib universal verifier example

* chore: cargo fix
  • Loading branch information
rpalakkal authored Jul 26, 2023
1 parent df44945 commit aa79228
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@
/target
testdata

Cargo.lock
params
agg.pk
break_points.json
104 changes: 104 additions & 0 deletions snark-verifier-sdk/examples/halo2_lib.rs
Original file line number Diff line number Diff line change
@@ -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<Vec<usize>> {
let file = File::open(path);
let break_points = match file {
Ok(file) => {
let reader = BufReader::new(file);
let break_points: Vec<Vec<usize>> = 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::<SHPLONK>(
CircuitBuilderStage::Keygen,
None,
lookup_bits,
&params,
vec![dummy_snark.clone()],
true,
);
agg_circuit.config(k, Some(10));

let start0 = start_timer!(|| "gen vk & pk");
let pk = gen_pk(&params, &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::<SHPLONK>(
CircuitBuilderStage::Prover,
Some(break_points.clone()),
lookup_bits,
&params,
vec![snark],
true,
);
let _snark = gen_snark_shplonk(&params, &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(&params, &pk, agg_circuit, instances.clone());
let deployment_code = gen_evm_verifier_shplonk::<AggregationCircuit<SHPLONK>>(
&params,
pk.get_vk(),
num_instances,
Some(Path::new("./examples/standard_plonk.yul")),
);
evm_verify(deployment_code, instances, proof_calldata);
}
*/
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
35 changes: 27 additions & 8 deletions snark-verifier-sdk/examples/standard_plonk.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)]
Expand Down Expand Up @@ -149,11 +149,30 @@ fn gen_application_snark(params: &ParamsKZG<Bn256>, flag: ComputeFlag) -> Snark
gen_snark_shplonk(params, &pk, circuit, None::<&str>)
}

fn gen_agg_break_points(agg_circuit: AggregationCircuit, path: &Path) -> Vec<Vec<usize>> {
let file = File::open(path);
let break_points = match file {
Ok(file) => {
let reader = BufReader::new(file);
let break_points: Vec<Vec<usize>> = 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(&params_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| {
Expand All @@ -173,7 +192,7 @@ fn main() {
let start0 = start_timer!(|| "gen vk & pk");
let pk = gen_pk(&params, &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(&params_app, flag));
Expand Down
2 changes: 1 addition & 1 deletion snark-verifier-sdk/src/halo2/aggregation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<AS>(&svk, &loader, &snarks, as_proof.as_slice(), preprocessed_as_witness);
let lhs = accumulator.lhs.assigned();
let rhs = accumulator.rhs.assigned();
Expand Down

0 comments on commit aa79228

Please sign in to comment.