Browse Source

Adds NIZK benchmarking, updates crypto-primitives benchmarks

master
howardwu 4 years ago
parent
commit
2949c0ef9d
4 changed files with 107 additions and 2 deletions
  1. +6
    -0
      crypto-primitives/Cargo.toml
  2. +1
    -1
      crypto-primitives/benches/crypto_primitives/comm.rs
  3. +1
    -1
      crypto-primitives/benches/crypto_primitives/crh.rs
  4. +99
    -0
      crypto-primitives/benches/crypto_primitives/nizk.rs

+ 6
- 0
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

+ 1
- 1
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
}

+ 1
- 1
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
}

+ 99
- 0
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<Bls12_377, Bench<Fr>, Fr>;
struct Bench<F: Field> {
inputs: Vec<Option<F>>,
num_constraints: usize,
}
impl<F: Field> ConstraintSynthesizer<F> for Bench<F> {
fn generate_constraints<CS: ConstraintSystem<F>>(
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<Option<Fr>> = 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::<Fr> { 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<Option<Fr>> = Vec::with_capacity(num_inputs);
for _ in 0..num_inputs {
inputs.push(Some(rng.gen()));
}
let params = TestProofSystem::setup(Bench::<Fr> { inputs: vec![None; num_inputs], num_constraints }, rng).unwrap();
c.bench_function("gm17_prove", move |b| {
b.iter(|| TestProofSystem::prove(&params.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);

Loading…
Cancel
Save