From 2949c0ef9d7a83214fe60d0dae61f2fbb7d39c31 Mon Sep 17 00:00:00 2001 From: howardwu Date: Sat, 15 Feb 2020 18:16:19 -0800 Subject: [PATCH] Adds NIZK benchmarking, updates crypto-primitives benchmarks --- crypto-primitives/Cargo.toml | 6 ++ .../benches/crypto_primitives/comm.rs | 2 +- .../benches/crypto_primitives/crh.rs | 2 +- .../benches/crypto_primitives/nizk.rs | 99 +++++++++++++++++++ 4 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 crypto-primitives/benches/crypto_primitives/nizk.rs diff --git a/crypto-primitives/Cargo.toml b/crypto-primitives/Cargo.toml index d37f02c..01e99e3 100644 --- a/crypto-primitives/Cargo.toml +++ b/crypto-primitives/Cargo.toml @@ -64,3 +64,9 @@ harness = false name = "schnorr_sig" path = "benches/crypto_primitives/signature.rs" harness = false + +[[bench]] +name = "gm17" +path = "benches/crypto_primitives/nizk.rs" +required-features = ["r1cs", "gm17"] +harness = false diff --git a/crypto-primitives/benches/crypto_primitives/comm.rs b/crypto-primitives/benches/crypto_primitives/comm.rs index 8eb9d59..b53f497 100644 --- a/crypto-primitives/benches/crypto_primitives/comm.rs +++ b/crypto-primitives/benches/crypto_primitives/comm.rs @@ -44,7 +44,7 @@ fn pedersen_comm_eval(c: &mut Criterion) { criterion_group! { name = comm_setup; - config = Criterion::default().sample_size(5); + config = Criterion::default().sample_size(10); targets = pedersen_comm_setup } diff --git a/crypto-primitives/benches/crypto_primitives/crh.rs b/crypto-primitives/benches/crypto_primitives/crh.rs index 8313ac1..24124b7 100644 --- a/crypto-primitives/benches/crypto_primitives/crh.rs +++ b/crypto-primitives/benches/crypto_primitives/crh.rs @@ -37,7 +37,7 @@ fn pedersen_crh_eval(c: &mut Criterion) { criterion_group! { name = crh_setup; - config = Criterion::default().sample_size(5); + config = Criterion::default().sample_size(10); targets = pedersen_crh_setup } diff --git a/crypto-primitives/benches/crypto_primitives/nizk.rs b/crypto-primitives/benches/crypto_primitives/nizk.rs new file mode 100644 index 0000000..d449272 --- /dev/null +++ b/crypto-primitives/benches/crypto_primitives/nizk.rs @@ -0,0 +1,99 @@ +#[macro_use] +extern crate criterion; + +use algebra::{ + curves::bls12_377::Bls12_377, + fields::bls12_377::Fr, + Field, +}; +use crypto_primitives::nizk::*; +use r1cs_core::{ConstraintSynthesizer, ConstraintSystem, SynthesisError}; + +use criterion::Criterion; +use rand::{thread_rng, Rng}; + +type TestProofSystem = Gm17, Fr>; + +struct Bench { + inputs: Vec>, + num_constraints: usize, +} + +impl ConstraintSynthesizer for Bench { + fn generate_constraints>( + self, + cs: &mut CS, + ) -> Result<(), SynthesisError> { + assert!(self.inputs.len() >= 2); + assert!(self.num_constraints >= self.inputs.len()); + + let mut variables: Vec<_> = Vec::with_capacity(self.inputs.len()); + for (i, input) in self.inputs.into_iter().enumerate() { + let input_var = cs.alloc_input( + || format!("Input {}", i), + || input.ok_or(SynthesisError::AssignmentMissing), + )?; + variables.push((input, input_var)); + } + + for i in 0..self.num_constraints { + let new_entry = { + let (input_1_val, input_1_var) = variables[i]; + let (input_2_val, input_2_var) = variables[i + 1]; + let result_val = input_1_val + .and_then(|input_1| input_2_val.map(|input_2| input_1 * &input_2)); + let result_var = cs.alloc( + || format!("Result {}", i), + || result_val.ok_or(SynthesisError::AssignmentMissing), + )?; + cs.enforce( + || format!("Enforce constraint {}", i), + |lc| lc + input_1_var, + |lc| lc + input_2_var, + |lc| lc + result_var, + ); + (result_val, result_var) + }; + variables.push(new_entry); + } + Ok(()) + } +} + +fn gm17_setup(c: &mut Criterion) { + let num_inputs = 100; + let num_constraints = num_inputs; + let rng = &mut thread_rng(); + let mut inputs: Vec> = Vec::with_capacity(num_inputs); + for _ in 0..num_inputs { + inputs.push(Some(rng.gen())); + } + + c.bench_function("gm17_setup", move |b| { + b.iter(|| TestProofSystem::setup(Bench:: { inputs: vec![None; num_inputs], num_constraints }, rng).unwrap()) + }); +} + +fn gm17_prove(c: &mut Criterion) { + let num_inputs = 100; + let num_constraints = num_inputs; + let rng = &mut thread_rng(); + let mut inputs: Vec> = Vec::with_capacity(num_inputs); + for _ in 0..num_inputs { + inputs.push(Some(rng.gen())); + } + + let params = TestProofSystem::setup(Bench:: { inputs: vec![None; num_inputs], num_constraints }, rng).unwrap(); + + c.bench_function("gm17_prove", move |b| { + b.iter(|| TestProofSystem::prove(¶ms.0, Bench { inputs: inputs.clone(), num_constraints }, rng).unwrap()) + }); +} + +criterion_group! { + name = nizk_eval; + config = Criterion::default().sample_size(10); + targets = gm17_setup, gm17_prove +} + +criterion_main!(nizk_eval);