-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add initial
bench_builder
cmd to BaseTester
- Loading branch information
1 parent
1b41b14
commit 35ef1db
Showing
3 changed files
with
107 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,39 @@ | ||
use halo2_base::gates::builder::{GateThreadBuilder, RangeCircuitBuilder}; | ||
#![cfg(feature = "test-utils")] | ||
use halo2_base::gates::flex_gate::{GateChip, GateInstructions}; | ||
use halo2_base::halo2_proofs::{ | ||
arithmetic::Field, | ||
dev::MockProver, | ||
halo2curves::bn256::{Bn256, Fr}, | ||
plonk::*, | ||
poly::kzg::commitment::ParamsKZG, | ||
}; | ||
use halo2_base::utils::testing::{check_proof, gen_proof}; | ||
use halo2_base::halo2_proofs::{arithmetic::Field, halo2curves::bn256::Fr}; | ||
use halo2_base::safe_types::RangeInstructions; | ||
use halo2_base::utils::testing::base_test; | ||
use halo2_base::utils::ScalarField; | ||
use halo2_base::{Context, QuantumCell::Existing}; | ||
use itertools::Itertools; | ||
use rand::rngs::OsRng; | ||
|
||
const K: u32 = 19; | ||
|
||
fn inner_prod_bench<F: ScalarField>(ctx: &mut Context<F>, a: Vec<F>, b: Vec<F>) { | ||
fn inner_prod_bench<F: ScalarField>( | ||
ctx: &mut Context<F>, | ||
gate: &GateChip<F>, | ||
a: Vec<F>, | ||
b: Vec<F>, | ||
) { | ||
assert_eq!(a.len(), b.len()); | ||
let a = ctx.assign_witnesses(a); | ||
let b = ctx.assign_witnesses(b); | ||
|
||
let chip = GateChip::default(); | ||
for _ in 0..(1 << K) / 16 - 10 { | ||
chip.inner_product(ctx, a.clone(), b.clone().into_iter().map(Existing)); | ||
gate.inner_product(ctx, a.clone(), b.clone().into_iter().map(Existing)); | ||
} | ||
} | ||
|
||
fn main() { | ||
let k = 10u32; | ||
// create circuit for keygen | ||
let mut builder = GateThreadBuilder::new(false); | ||
inner_prod_bench(builder.main(0), vec![Fr::zero(); 5], vec![Fr::zero(); 5]); | ||
let config_params = builder.config(k as usize, Some(20)); | ||
let circuit = RangeCircuitBuilder::mock(builder, config_params.clone()); | ||
|
||
// check the circuit is correct just in case | ||
MockProver::run(k, &circuit, vec![]).unwrap().assert_satisfied(); | ||
|
||
let params = ParamsKZG::<Bn256>::setup(k, OsRng); | ||
let vk = keygen_vk(¶ms, &circuit).expect("vk should not fail"); | ||
let pk = keygen_pk(¶ms, vk, &circuit).expect("pk should not fail"); | ||
|
||
let break_points = circuit.0.break_points.take(); | ||
|
||
let mut builder = GateThreadBuilder::new(true); | ||
let a = (0..5).map(|_| Fr::random(OsRng)).collect_vec(); | ||
let b = (0..5).map(|_| Fr::random(OsRng)).collect_vec(); | ||
inner_prod_bench(builder.main(0), a, b); | ||
let circuit = RangeCircuitBuilder::prover(builder, config_params, break_points); | ||
|
||
let proof = gen_proof(¶ms, &pk, circuit); | ||
check_proof(¶ms, pk.get_vk(), &proof, true); | ||
base_test().k(12).bench_builder( | ||
(vec![Fr::ZERO; 5], vec![Fr::ZERO; 5]), | ||
( | ||
(0..5).map(|_| Fr::random(OsRng)).collect_vec(), | ||
(0..5).map(|_| Fr::random(OsRng)).collect_vec(), | ||
), | ||
|builder, range, (a, b)| { | ||
inner_prod_bench(builder.main(0), range.gate(), a, b); | ||
}, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters