Browse Source

fix bench.rs (#103)

* fix bench.rs

* fix script

* fix rayon bug

* simplify high degree tests

* temporarily turnoff a bench subset

* minor

* uncomment

* minor fix

* comment

* uncomment

* minor fix

Co-authored-by: zhenfei <zhenfei.zhang@hotmail.com>
main
chancharles92 1 year ago
committed by GitHub
parent
commit
94092ca6a4
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 43 deletions
  1. +60
    -34
      hyperplonk/benches/bench.rs
  2. BIN
      hyperplonk/srs.params
  3. +8
    -9
      scripts/run_benchmarks.sh

+ 60
- 34
hyperplonk/benches/bench.rs

@ -1,13 +1,12 @@
use std::{env, fs::File, time::Instant};
use std::{fs::File, io, time::Instant};
use ark_bls12_381::{Bls12_381, Fr}; use ark_bls12_381::{Bls12_381, Fr};
use ark_serialize::Write;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, Write};
use ark_std::test_rng; use ark_std::test_rng;
use hyperplonk::{ use hyperplonk::{
prelude::{CustomizedGates, HyperPlonkErrors, MockCircuit}, prelude::{CustomizedGates, HyperPlonkErrors, MockCircuit},
HyperPlonkSNARK, HyperPlonkSNARK,
}; };
use rayon::ThreadPoolBuilder;
use subroutines::{ use subroutines::{
pcs::{ pcs::{
prelude::{MultilinearKzgPCS, MultilinearUniversalParams}, prelude::{MultilinearKzgPCS, MultilinearUniversalParams},
@ -16,37 +15,58 @@ use subroutines::{
poly_iop::PolyIOP, poly_iop::PolyIOP,
}; };
const SUPPORTED_SIZE: usize = 15;
const SUPPORTED_SIZE: usize = 20;
const MIN_NUM_VARS: usize = 8; const MIN_NUM_VARS: usize = 8;
const MAX_NUM_VARS: usize = 15;
const MAX_NUM_VARS: usize = 20;
const MIN_CUSTOM_DEGREE: usize = 1; const MIN_CUSTOM_DEGREE: usize = 1;
const MAX_CUSTOM_DEGREE: usize = 32; const MAX_CUSTOM_DEGREE: usize = 32;
const MAX_NUM_THREADS: usize = 24;
const HIGH_DEGREE_TEST_NV: usize = 15;
fn main() -> Result<(), HyperPlonkErrors> { fn main() -> Result<(), HyperPlonkErrors> {
let args: Vec<String> = env::args().collect();
let thread = args[1].parse().unwrap_or(MAX_NUM_THREADS);
let thread = rayon::current_num_threads();
println!("start benchmark with #{} threads", thread);
let mut rng = test_rng(); let mut rng = test_rng();
let pcs_srs = MultilinearKzgPCS::<Bls12_381>::gen_srs_for_testing(&mut rng, SUPPORTED_SIZE)?;
ThreadPoolBuilder::new()
.num_threads(thread)
.build()
.unwrap();
let pcs_srs = {
match read_srs() {
Ok(p) => p,
Err(_e) => {
let srs =
MultilinearKzgPCS::<Bls12_381>::gen_srs_for_testing(&mut rng, SUPPORTED_SIZE)?;
write_srs(&srs);
srs
},
}
};
bench_jellyfish_plonk(&pcs_srs, thread)?;
println!();
bench_vanilla_plonk(&pcs_srs, thread)?; bench_vanilla_plonk(&pcs_srs, thread)?;
for degree in MIN_CUSTOM_DEGREE..MAX_CUSTOM_DEGREE {
println!();
for degree in MIN_CUSTOM_DEGREE..=MAX_CUSTOM_DEGREE {
bench_high_degree_plonk(&pcs_srs, degree, thread)?; bench_high_degree_plonk(&pcs_srs, degree, thread)?;
println!();
} }
println!();
Ok(()) Ok(())
} }
fn read_srs() -> Result<MultilinearUniversalParams<Bls12_381>, io::Error> {
let mut f = File::open("srs.params")?;
Ok(MultilinearUniversalParams::<Bls12_381>::deserialize_unchecked(&mut f).unwrap())
}
fn write_srs(pcs_srs: &MultilinearUniversalParams<Bls12_381>) {
let mut f = File::create("srs.params").unwrap();
pcs_srs.serialize_uncompressed(&mut f).unwrap();
}
fn bench_vanilla_plonk( fn bench_vanilla_plonk(
pcs_srs: &MultilinearUniversalParams<Bls12_381>, pcs_srs: &MultilinearUniversalParams<Bls12_381>,
thread: usize, thread: usize,
) -> Result<(), HyperPlonkErrors> { ) -> Result<(), HyperPlonkErrors> {
let filename = format!("vanilla threads {}.txt", thread); let filename = format!("vanilla threads {}.txt", thread);
let mut file = File::create(filename).unwrap(); let mut file = File::create(filename).unwrap();
for nv in MIN_NUM_VARS..MAX_NUM_VARS {
for nv in MIN_NUM_VARS..=MAX_NUM_VARS {
let vanilla_gate = CustomizedGates::vanilla_plonk_gate(); let vanilla_gate = CustomizedGates::vanilla_plonk_gate();
bench_mock_circuit_zkp_helper(&mut file, nv, &vanilla_gate, &pcs_srs)?; bench_mock_circuit_zkp_helper(&mut file, nv, &vanilla_gate, &pcs_srs)?;
} }
@ -54,6 +74,20 @@ fn bench_vanilla_plonk(
Ok(()) Ok(())
} }
fn bench_jellyfish_plonk(
pcs_srs: &MultilinearUniversalParams<Bls12_381>,
thread: usize,
) -> Result<(), HyperPlonkErrors> {
let filename = format!("jellyfish threads {}.txt", thread);
let mut file = File::create(filename).unwrap();
for nv in MIN_NUM_VARS..=MAX_NUM_VARS {
let jf_gate = CustomizedGates::jellyfish_turbo_plonk_gate();
bench_mock_circuit_zkp_helper(&mut file, nv, &jf_gate, &pcs_srs)?;
}
Ok(())
}
fn bench_high_degree_plonk( fn bench_high_degree_plonk(
pcs_srs: &MultilinearUniversalParams<Bls12_381>, pcs_srs: &MultilinearUniversalParams<Bls12_381>,
degree: usize, degree: usize,
@ -61,10 +95,9 @@ fn bench_high_degree_plonk(
) -> Result<(), HyperPlonkErrors> { ) -> Result<(), HyperPlonkErrors> {
let filename = format!("high degree {} thread {}.txt", degree, thread); let filename = format!("high degree {} thread {}.txt", degree, thread);
let mut file = File::create(filename).unwrap(); let mut file = File::create(filename).unwrap();
for nv in MIN_NUM_VARS..MAX_NUM_VARS {
let vanilla_gate = CustomizedGates::mock_gate(2, degree);
bench_mock_circuit_zkp_helper(&mut file, nv, &vanilla_gate, &pcs_srs)?;
}
println!("custom gate of degree {}", degree);
let vanilla_gate = CustomizedGates::mock_gate(2, degree);
bench_mock_circuit_zkp_helper(&mut file, HIGH_DEGREE_TEST_NV, &vanilla_gate, &pcs_srs)?;
Ok(()) Ok(())
} }
@ -84,17 +117,6 @@ fn bench_mock_circuit_zkp_helper(
}; };
//========================================================== //==========================================================
let start = Instant::now();
for _ in 0..repetition {
let circuit = MockCircuit::<Fr>::new(1 << nv, gate);
assert!(circuit.is_satisfied());
}
println!(
"mock circuit gen for {} variables: {} ns",
nv,
start.elapsed().as_nanos() / repetition as u128
);
let circuit = MockCircuit::<Fr>::new(1 << nv, gate); let circuit = MockCircuit::<Fr>::new(1 << nv, gate);
assert!(circuit.is_satisfied()); assert!(circuit.is_satisfied());
let index = circuit.index; let index = circuit.index;
@ -123,17 +145,21 @@ fn bench_mock_circuit_zkp_helper(
let _proof = let _proof =
<PolyIOP<Fr> as HyperPlonkSNARK<Bls12_381, MultilinearKzgPCS<Bls12_381>>>::prove( <PolyIOP<Fr> as HyperPlonkSNARK<Bls12_381, MultilinearKzgPCS<Bls12_381>>>::prove(
&pk, &pk,
&circuit.witnesses[0].coeff_ref(),
&circuit.public_inputs,
&circuit.witnesses, &circuit.witnesses,
)?; )?;
} }
let t = start.elapsed().as_micros() / repetition as u128; let t = start.elapsed().as_micros() / repetition as u128;
println!(
"proving for {} variables: {} us",
nv,
start.elapsed().as_micros() / repetition as u128
);
file.write_all(format!("{} {}\n", nv, t).as_ref()).unwrap(); file.write_all(format!("{} {}\n", nv, t).as_ref()).unwrap();
let proof = <PolyIOP<Fr> as HyperPlonkSNARK<Bls12_381, MultilinearKzgPCS<Bls12_381>>>::prove( let proof = <PolyIOP<Fr> as HyperPlonkSNARK<Bls12_381, MultilinearKzgPCS<Bls12_381>>>::prove(
&pk, &pk,
&circuit.witnesses[0].coeff_ref(),
&circuit.public_inputs,
&circuit.witnesses, &circuit.witnesses,
)?; )?;
//========================================================== //==========================================================
@ -143,7 +169,7 @@ fn bench_mock_circuit_zkp_helper(
let verify = let verify =
<PolyIOP<Fr> as HyperPlonkSNARK<Bls12_381, MultilinearKzgPCS<Bls12_381>>>::verify( <PolyIOP<Fr> as HyperPlonkSNARK<Bls12_381, MultilinearKzgPCS<Bls12_381>>>::verify(
&vk, &vk,
&circuit.witnesses[0].coeff_ref(),
&circuit.public_inputs,
&proof, &proof,
)?; )?;
assert!(verify); assert!(verify);

BIN
hyperplonk/srs.params


+ 8
- 9
scripts/run_benchmarks.sh

@ -1,12 +1,11 @@
#!/bin/bash #!/bin/bash
cd ..
cd hyperplonk cd hyperplonk
# Run the benchmark binary # Run the benchmark binary
cargo bench 64 --no-default-features --features=bench
cargo bench 32 --no-default-features --features=bench
cargo bench 16 --no-default-features --features=bench
cargo bench 8 --no-default-features --features=bench
cargo bench 4 --no-default-features --features=bench
cargo bench 2 --no-default-features --features=bench
cargo bench 1 --no-default-features --features=bench
RAYON_NUM_THREADS=64 cargo bench --no-default-features --features=bench
RAYON_NUM_THREADS=32 cargo bench --no-default-features --features=bench
RAYON_NUM_THREADS=16 cargo bench --no-default-features --features=bench
RAYON_NUM_THREADS=8 cargo bench --no-default-features --features=bench
RAYON_NUM_THREADS=4 cargo bench --no-default-features --features=bench
RAYON_NUM_THREADS=2 cargo bench --no-default-features --features=bench
RAYON_NUM_THREADS=1 cargo bench --no-default-features --features=bench

Loading…
Cancel
Save