Skip to content

Commit

Permalink
Benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
ed255 committed Jan 3, 2024
1 parent bfc5d86 commit 8495457
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
2 changes: 2 additions & 0 deletions halo2_proofs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ criterion = "0.3"
gumdrop = "0.8"
proptest = "1"
rand_core = { version = "0.6", default-features = false, features = ["getrandom"] }
dhat = "0.3.2"

[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dev-dependencies]
getrandom = { version = "0.2", features = ["js"] }
Expand All @@ -91,6 +92,7 @@ thread-safe-region = []
sanity-checks = []
batch = ["rand_core/getrandom"]
circuit-params = []
dhat-heap = []

[lib]
bench = false
Expand Down
2 changes: 1 addition & 1 deletion halo2_proofs/src/plonk/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1936,7 +1936,7 @@ impl<'a, F: Field, ConcreteCircuit: Circuit<F>> WitnessCalculator<'a, F, Concret
0 => FirstPhase.to_sealed(),
1 => SecondPhase.to_sealed(),
2 => ThirdPhase.to_sealed(),
_ => unreachable!("only phase [0..2] supported"),
_ => unreachable!("only phase [0,2] supported"),
};
let mut witness = WitnessCollection {
k: self.k,
Expand Down
36 changes: 31 additions & 5 deletions halo2_proofs/tests/frontend_backend_split.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#![allow(clippy::many_single_char_names)]
#![allow(clippy::op_ref)]

#[cfg(feature = "dhat-heap")]
#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc;

use assert_matches::assert_matches;

Check warning on line 8 in halo2_proofs/tests/frontend_backend_split.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

unused import: `assert_matches::assert_matches`

warning: unused import: `assert_matches::assert_matches` --> halo2_proofs/tests/frontend_backend_split.rs:8:5 | 8 | use assert_matches::assert_matches; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
use ff::{FromUniformBytes, WithSmallOrderMulGroup};

Check warning on line 9 in halo2_proofs/tests/frontend_backend_split.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

unused imports: `FromUniformBytes`, `WithSmallOrderMulGroup`

warning: unused imports: `FromUniformBytes`, `WithSmallOrderMulGroup` --> halo2_proofs/tests/frontend_backend_split.rs:9:10 | 9 | use ff::{FromUniformBytes, WithSmallOrderMulGroup}; | ^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^
use halo2_proofs::arithmetic::Field;
Expand Down Expand Up @@ -469,18 +473,27 @@ fn test_mycircuit_mock() {
prover.assert_satisfied();
}

use std::time::Instant;

const K: u32 = 16;
const WIDTH_FACTOR: usize = 4;

#[test]
fn test_mycircuit_full_legacy() {
let k = 6;
const WIDTH_FACTOR: usize = 1;
#[cfg(feature = "dhat-heap")]
let _profiler = dhat::Profiler::new_heap();

let k = K;
let circuit: MyCircuit<Fr, WIDTH_FACTOR> = MyCircuit::new(k, 42);

// Setup
let mut rng = BlockRng::new(OneNg {});
let params = ParamsKZG::<Bn256>::setup(k, &mut rng);
let verifier_params = params.verifier_params();
let start = Instant::now();
let vk = keygen_vk(&params, &circuit).expect("keygen_vk should not fail");
let pk = keygen_pk(&params, vk.clone(), &circuit).expect("keygen_pk should not fail");
println!("Keygen: {:?}", start.elapsed());

// Proving
let instances = circuit.instances();
Expand All @@ -489,6 +502,7 @@ fn test_mycircuit_full_legacy() {
.map(|instance| instance.as_slice())
.collect::<Vec<_>>());

let start = Instant::now();
let mut transcript = Blake2bWrite::<_, G1Affine, Challenge255<_>>::init(vec![]);
create_proof::<KZGCommitmentScheme<Bn256>, ProverSHPLONK<'_, Bn256>, _, _, _, _>(
&params,
Expand All @@ -504,8 +518,10 @@ fn test_mycircuit_full_legacy() {
// for word in proof.chunks(32) {
// println!(" {:02x?}", word);
// }
println!("Prove: {:?}", start.elapsed());

// Verify
let start = Instant::now();
let mut verifier_transcript =
Blake2bRead::<_, G1Affine, Challenge255<_>>::init(proof.as_slice());
let strategy = SingleStrategy::new(&verifier_params);

Check warning on line 527 in halo2_proofs/tests/frontend_backend_split.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> halo2_proofs/tests/frontend_backend_split.rs:527:40 | 527 | let strategy = SingleStrategy::new(&verifier_params); | ^^^^^^^^^^^^^^^^ help: change this to: `verifier_params` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `-W clippy::needless-borrow` implied by `-W clippy::all`
Expand All @@ -518,23 +534,29 @@ fn test_mycircuit_full_legacy() {
&mut verifier_transcript,
)
.expect("verify succeeds");
println!("Verify: {:?}", start.elapsed());
}

#[test]
fn test_mycircuit_full_split() {
let k = 6;
const WIDTH_FACTOR: usize = 1;
#[cfg(feature = "dhat-heap")]
let _profiler = dhat::Profiler::new_heap();

let k = K;
let circuit: MyCircuit<Fr, WIDTH_FACTOR> = MyCircuit::new(k, 42);
let (compiled_circuit, config, cs) = compile_circuit(k, &circuit, false).unwrap();

// Setup
let mut rng = BlockRng::new(OneNg {});
let params = ParamsKZG::<Bn256>::setup(k, &mut rng);
let verifier_params = params.verifier_params();
let start = Instant::now();
let vk = keygen_vk_v2(&params, &compiled_circuit).expect("keygen_vk should not fail");
// println!("vk: {:#?}", vk);
let pk =
keygen_pk_v2(&params, vk.clone(), &compiled_circuit).expect("keygen_pk should not fail");
println!("Keygen: {:?}", start.elapsed());
drop(compiled_circuit);

// Proving
println!("DBG Proving...");
Expand All @@ -543,8 +565,9 @@ fn test_mycircuit_full_split() {
.iter()
.map(|instance| instance.as_slice())
.collect::<Vec<_>>());
let mut witness_calc = WitnessCalculator::new(k, &circuit, &config, &cs, instances_slice);

let start = Instant::now();
let mut witness_calc = WitnessCalculator::new(k, &circuit, &config, &cs, instances_slice);
let mut transcript = Blake2bWrite::<_, G1Affine, Challenge255<_>>::init(vec![]);

Check warning on line 571 in halo2_proofs/tests/frontend_backend_split.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

variable does not need to be mutable

warning: variable does not need to be mutable --> halo2_proofs/tests/frontend_backend_split.rs:571:9 | 571 | let mut transcript = Blake2bWrite::<_, G1Affine, Challenge255<_>>::init(vec![]); | ----^^^^^^^^^^ | | | help: remove this `mut` | = note: `#[warn(unused_mut)]` on by default
let mut prover =
ProverV2::<KZGCommitmentScheme<Bn256>, ProverSHPLONK<'_, Bn256>, _, _, _>::new(
Expand All @@ -570,8 +593,10 @@ fn test_mycircuit_full_split() {
// for word in proof.chunks(32) {
// println!(" {:02x?}", word);
// }
println!("Prove: {:?}", start.elapsed());

// Verify
let start = Instant::now();
println!("DBG Verifying...");
let mut verifier_transcript =
Blake2bRead::<_, G1Affine, Challenge255<_>>::init(proof.as_slice());
Expand All @@ -585,4 +610,5 @@ fn test_mycircuit_full_split() {
&mut verifier_transcript,
)
.expect("verify succeeds");
println!("Verify: {:?}", start.elapsed());
}

0 comments on commit 8495457

Please sign in to comment.