From b0b3514fa7a9d9e97b6de70081c9bc4548d5ab7c Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Tue, 6 Mar 2018 10:21:30 -0700 Subject: [PATCH] Add bench_50 example. --- examples/bench.rs | 83 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 examples/bench.rs diff --git a/examples/bench.rs b/examples/bench.rs new file mode 100644 index 0000000..04a72f8 --- /dev/null +++ b/examples/bench.rs @@ -0,0 +1,83 @@ +extern crate sapling_crypto; +extern crate bellman; +extern crate rand; +extern crate pairing; + +use std::time::{Duration, Instant}; +use sapling_crypto::jubjub::{ + JubjubBls12, + edwards, + fs, + Unknown +}; +use sapling_crypto::circuit::{ + Spend +}; +use bellman::groth16::*; +use rand::{XorShiftRng, SeedableRng, Rng}; +use pairing::bls12_381::Bls12; + +const TREE_DEPTH: usize = 32; + +fn main() { + let jubjub_params = &JubjubBls12::new(); + let rng = &mut XorShiftRng::from_seed([0x3dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]); + + println!("Creating sample parameters..."); + let groth_params = generate_random_parameters::( + Spend { + params: jubjub_params, + /// Value of the note being spent + value: None, + /// Randomness that will hide the value + value_randomness: None, + /// Key which allows the proof to be constructed + /// as defense-in-depth against a flaw in the + /// protocol that would otherwise be exploitable + /// by a holder of a viewing key. + rsk: None, + /// The public key that will be re-randomized for + /// use as a nullifier and signing key for the + /// transaction. + ak: None, + /// The diversified base used to compute pk_d. + g_d: None, + /// The randomness used to hide the note commitment data + commitment_randomness: None, + /// The authentication path of the commitment in the tree + auth_path: vec![None; TREE_DEPTH] + }, + rng + ).unwrap(); + + const SAMPLES: u32 = 50; + + let mut total_time = Duration::new(0, 0); + for _ in 0..SAMPLES { + let value: u64 = 1; + let value_randomness: fs::Fs = rng.gen(); + let ak: edwards::Point = edwards::Point::rand(rng, jubjub_params); + let g_d: edwards::Point = edwards::Point::rand(rng, jubjub_params); + let commitment_randomness: fs::Fs = rng.gen(); + let rsk: fs::Fs = rng.gen(); + let auth_path = (0..TREE_DEPTH).map(|_| Some((rng.gen(), rng.gen()))).collect(); + + let start = Instant::now(); + let _ = create_random_proof(Spend { + params: jubjub_params, + value: Some(value), + value_randomness: Some(value_randomness), + ak: Some(ak), + g_d: Some(g_d), + commitment_randomness: Some(commitment_randomness), + rsk: Some(rsk), + auth_path: auth_path + }, &groth_params, rng).unwrap(); + total_time += start.elapsed(); + } + let avg = total_time / SAMPLES; + let avg = avg.subsec_nanos() as f64 / 1_000_000_000f64 + + (avg.as_secs() as f64); + + println!("Average proving time (in seconds): {}", avg); +}