From d318e70dd325047c03f7c333457cf4c81fab17b1 Mon Sep 17 00:00:00 2001 From: Pratyush Mishra Date: Tue, 24 Sep 2019 20:27:42 -0700 Subject: [PATCH] Fix benches --- crypto-primitives/Cargo.toml | 40 +++- .../benches/crypto_primitives/comm.rs | 57 ++++++ .../benches/crypto_primitives/crh.rs | 50 +++++ .../benches/crypto_primitives/prf.rs | 25 +++ .../benches/crypto_primitives/signature.rs | 180 ++++++++++++++++++ 5 files changed, 350 insertions(+), 2 deletions(-) create mode 100644 crypto-primitives/benches/crypto_primitives/comm.rs create mode 100644 crypto-primitives/benches/crypto_primitives/crh.rs create mode 100644 crypto-primitives/benches/crypto_primitives/prf.rs create mode 100644 crypto-primitives/benches/crypto_primitives/signature.rs diff --git a/crypto-primitives/Cargo.toml b/crypto-primitives/Cargo.toml index b4d781b..2152a8b 100644 --- a/crypto-primitives/Cargo.toml +++ b/crypto-primitives/Cargo.toml @@ -1,10 +1,25 @@ [package] name = "crypto-primitives" version = "0.1.0" -authors = ["Pratyush Mishra "] +authors = [ + "Sean Bowe", + "Alessandro Chiesa", + "Matthew Green", + "Ian Miers", + "Pratyush Mishra", + "Howard Wu" +] +description = "A library of cryptographic primitives that are used by Zexe" +homepage = "https://libzexe.org" +repository = "https://github.com/scipr/zexe" +documentation = "https://docs.rs/crypto-primitives/" +keywords = ["r1cs", "gm17", "pedersen", "blake2s"] +categories = ["cryptography"] +include = ["Cargo.toml", "src", "README.md", "LICENSE-APACHE", "LICENSE-MIT"] +license = "MIT/Apache-2.0" edition = "2018" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +################################# Dependencies ################################ [dependencies] algebra = { path = "../algebra" } @@ -27,3 +42,24 @@ r1cs = [ "r1cs-core", "r1cs-std" ] criterion = "0.2" rand_xorshift = { version = "0.2" } +################################# Benchmarks ################################## + +[[bench]] +name = "pedersen_crh" +path = "benches/crypto_primitives/crh.rs" +harness = false + +[[bench]] +name = "pedersen_comm" +path = "benches/crypto_primitives/comm.rs" +harness = false + +[[bench]] +name = "blake2s_prf" +path = "benches/crypto_primitives/prf.rs" +harness = false + +[[bench]] +name = "schnorr_sig" +path = "benches/crypto_primitives/signature.rs" +harness = false diff --git a/crypto-primitives/benches/crypto_primitives/comm.rs b/crypto-primitives/benches/crypto_primitives/comm.rs new file mode 100644 index 0000000..b82085e --- /dev/null +++ b/crypto-primitives/benches/crypto_primitives/comm.rs @@ -0,0 +1,57 @@ +use rand; + +#[macro_use] +extern crate criterion; + +use algebra::{UniformRand, curves::edwards_bls12::EdwardsProjective as Edwards}; +use criterion::Criterion; +use crypto_primitives::commitment::{pedersen::*, CommitmentScheme}; + +#[derive(Clone, PartialEq, Eq, Hash)] +pub struct CommWindow; + +impl PedersenWindow for CommWindow { + const WINDOW_SIZE: usize = 250; + const NUM_WINDOWS: usize = 8; +} + +fn pedersen_comm_setup(c: &mut Criterion) { + c.bench_function("Pedersen Commitment Setup", move |b| { + b.iter(|| { + let mut rng = &mut rand::thread_rng(); + PedersenCommitment::::setup(&mut rng).unwrap() + }) + }); +} + +fn pedersen_comm_eval(c: &mut Criterion) { + let mut rng = &mut rand::thread_rng(); + let parameters = PedersenCommitment::::setup(&mut rng).unwrap(); + let input = vec![5u8; 128]; + c.bench_function("Pedersen Commitment Eval", move |b| { + b.iter(|| { + let rng = &mut rand::thread_rng(); + let commitment_randomness = PedersenRandomness::rand(rng); + PedersenCommitment::::commit( + ¶meters, + &input, + &commitment_randomness, + ) + .unwrap(); + }) + }); +} + +criterion_group! { + name = comm_setup; + config = Criterion::default().sample_size(5); + targets = pedersen_comm_setup +} + +criterion_group! { + name = comm_eval; + config = Criterion::default().sample_size(10); + targets = pedersen_comm_eval +} + +criterion_main!(comm_setup, comm_eval); diff --git a/crypto-primitives/benches/crypto_primitives/crh.rs b/crypto-primitives/benches/crypto_primitives/crh.rs new file mode 100644 index 0000000..8313ac1 --- /dev/null +++ b/crypto-primitives/benches/crypto_primitives/crh.rs @@ -0,0 +1,50 @@ +use rand; + +#[macro_use] +extern crate criterion; + +use algebra::curves::edwards_bls12::EdwardsProjective as Edwards; +use criterion::Criterion; +use crypto_primitives::crh::{pedersen::*, FixedLengthCRH}; + +#[derive(Clone, PartialEq, Eq, Hash)] +pub struct HashWindow; + +impl PedersenWindow for HashWindow { + const WINDOW_SIZE: usize = 250; + const NUM_WINDOWS: usize = 8; +} + +fn pedersen_crh_setup(c: &mut Criterion) { + c.bench_function("Pedersen CRH Setup", move |b| { + b.iter(|| { + let mut rng = &mut rand::thread_rng(); + PedersenCRH::::setup(&mut rng).unwrap() + }) + }); +} + +fn pedersen_crh_eval(c: &mut Criterion) { + let mut rng = &mut rand::thread_rng(); + let parameters = PedersenCRH::::setup(&mut rng).unwrap(); + let input = vec![5u8; 128]; + c.bench_function("Pedersen CRH Eval", move |b| { + b.iter(|| { + PedersenCRH::::evaluate(¶meters, &input).unwrap(); + }) + }); +} + +criterion_group! { + name = crh_setup; + config = Criterion::default().sample_size(5); + targets = pedersen_crh_setup +} + +criterion_group! { + name = crh_eval; + config = Criterion::default().sample_size(10); + targets = pedersen_crh_eval +} + +criterion_main!(crh_setup, crh_eval); diff --git a/crypto-primitives/benches/crypto_primitives/prf.rs b/crypto-primitives/benches/crypto_primitives/prf.rs new file mode 100644 index 0000000..fc355fd --- /dev/null +++ b/crypto-primitives/benches/crypto_primitives/prf.rs @@ -0,0 +1,25 @@ +use rand; + +#[macro_use] +extern crate criterion; + +use criterion::Criterion; +use crypto_primitives::prf::*; +use rand::Rng; + +fn blake2s_prf_eval(c: &mut Criterion) { + let rng = &mut rand::thread_rng(); + let input: [u8; 32] = rng.gen(); + let seed: [u8; 32] = rng.gen(); + c.bench_function("Blake2s PRF Eval", move |b| { + b.iter(|| Blake2s::evaluate(&seed, &input).unwrap()) + }); +} + +criterion_group! { + name = prf_eval; + config = Criterion::default().sample_size(50); + targets = blake2s_prf_eval +} + +criterion_main!(prf_eval); diff --git a/crypto-primitives/benches/crypto_primitives/signature.rs b/crypto-primitives/benches/crypto_primitives/signature.rs new file mode 100644 index 0000000..2a517bc --- /dev/null +++ b/crypto-primitives/benches/crypto_primitives/signature.rs @@ -0,0 +1,180 @@ +#[macro_use] +extern crate criterion; + +mod affine { + use algebra::curves::edwards_bls12::EdwardsAffine as Edwards; + use blake2::Blake2s; + use criterion::Criterion; + use crypto_primitives::signature::{schnorr::*, SignatureScheme}; + use rand::{self, Rng}; + + type SchnorrEdwards = SchnorrSignature; + fn schnorr_signature_setup(c: &mut Criterion) { + c.bench_function("SchnorrEdwardsAffine: Setup", move |b| { + b.iter(|| { + let mut rng = &mut rand::thread_rng(); + SchnorrEdwards::setup(&mut rng).unwrap() + }) + }); + } + + fn schnorr_signature_keygen(c: &mut Criterion) { + let mut rng = &mut rand::thread_rng(); + let parameters = SchnorrEdwards::setup(&mut rng).unwrap(); + + c.bench_function("SchnorrEdwardsAffine: KeyGen", move |b| { + b.iter(|| { + let mut rng = &mut rand::thread_rng(); + SchnorrEdwards::keygen(¶meters, &mut rng).unwrap() + }) + }); + } + + fn schnorr_signature_sign(c: &mut Criterion) { + let mut rng = &mut rand::thread_rng(); + let parameters = SchnorrEdwards::setup(&mut rng).unwrap(); + let (_, sk) = SchnorrEdwards::keygen(¶meters, &mut rng).unwrap(); + let message = [100u8; 128]; + + c.bench_function("SchnorrEdwardsAffine: Sign", move |b| { + b.iter(|| { + let mut rng = &mut rand::thread_rng(); + SchnorrEdwards::sign(¶meters, &sk, &message, &mut rng).unwrap() + }) + }); + } + + fn schnorr_signature_verify(c: &mut Criterion) { + let mut rng = &mut rand::thread_rng(); + let parameters = SchnorrEdwards::setup(&mut rng).unwrap(); + let (pk, sk) = SchnorrEdwards::keygen(¶meters, &mut rng).unwrap(); + let message = [100u8; 128]; + let signature = SchnorrEdwards::sign(¶meters, &sk, &message, &mut rng).unwrap(); + + c.bench_function("SchnorrEdwardsAffine: Verify", move |b| { + b.iter(|| SchnorrEdwards::verify(¶meters, &pk, &message, &signature).unwrap()) + }); + } + + fn schnorr_signature_randomize_pk(c: &mut Criterion) { + let mut rng = &mut rand::thread_rng(); + let parameters = SchnorrEdwards::setup(&mut rng).unwrap(); + let (pk, _) = SchnorrEdwards::keygen(¶meters, &mut rng).unwrap(); + let randomness: [u8; 32] = rng.gen(); + + c.bench_function("SchnorrEdwardsAffine: Randomize PubKey", move |b| { + b.iter(|| SchnorrEdwards::randomize_public_key(¶meters, &pk, &randomness).unwrap()) + }); + } + + fn schnorr_signature_randomize_signature(c: &mut Criterion) { + let mut rng = &mut rand::thread_rng(); + let parameters = SchnorrEdwards::setup(&mut rng).unwrap(); + let (_, sk) = SchnorrEdwards::keygen(¶meters, &mut rng).unwrap(); + let randomness: [u8; 32] = rng.gen(); + let message = [100u8; 128]; + let signature = SchnorrEdwards::sign(¶meters, &sk, &message, &mut rng).unwrap(); + + c.bench_function("SchnorrEdwardsAffine: Randomize Signature", move |b| { + b.iter(|| { + SchnorrEdwards::randomize_signature(¶meters, &signature, &randomness).unwrap() + }) + }); + } + criterion_group! { + name = schnorr_sig_affine; + config = Criterion::default().sample_size(20); + targets = schnorr_signature_setup, schnorr_signature_keygen, schnorr_signature_sign, + schnorr_signature_verify, schnorr_signature_randomize_pk, schnorr_signature_randomize_signature + } +} + +mod projective { + use algebra::curves::edwards_bls12::EdwardsProjective as Edwards; + use blake2::Blake2s; + use criterion::Criterion; + use crypto_primitives::signature::{schnorr::*, SignatureScheme}; + use rand::{self, Rng}; + + type SchnorrEdwards = SchnorrSignature; + fn schnorr_signature_setup(c: &mut Criterion) { + c.bench_function("SchnorrEdwardsProjective: Setup", move |b| { + b.iter(|| { + let mut rng = &mut rand::thread_rng(); + SchnorrEdwards::setup(&mut rng).unwrap() + }) + }); + } + + fn schnorr_signature_keygen(c: &mut Criterion) { + let mut rng = &mut rand::thread_rng(); + let parameters = SchnorrEdwards::setup(&mut rng).unwrap(); + + c.bench_function("SchnorrEdwardsProjective: KeyGen", move |b| { + b.iter(|| { + let mut rng = &mut rand::thread_rng(); + SchnorrEdwards::keygen(¶meters, &mut rng).unwrap() + }) + }); + } + + fn schnorr_signature_sign(c: &mut Criterion) { + let mut rng = &mut rand::thread_rng(); + let parameters = SchnorrEdwards::setup(&mut rng).unwrap(); + let (_, sk) = SchnorrEdwards::keygen(¶meters, &mut rng).unwrap(); + let message = [100u8; 128]; + + c.bench_function("SchnorrEdwardsProjective: Sign", move |b| { + b.iter(|| { + let mut rng = &mut rand::thread_rng(); + SchnorrEdwards::sign(¶meters, &sk, &message, &mut rng).unwrap() + }) + }); + } + + fn schnorr_signature_verify(c: &mut Criterion) { + let mut rng = &mut rand::thread_rng(); + let parameters = SchnorrEdwards::setup(&mut rng).unwrap(); + let (pk, sk) = SchnorrEdwards::keygen(¶meters, &mut rng).unwrap(); + let message = [100u8; 128]; + let signature = SchnorrEdwards::sign(¶meters, &sk, &message, &mut rng).unwrap(); + + c.bench_function("SchnorrEdwardsProjective: Verify", move |b| { + b.iter(|| SchnorrEdwards::verify(¶meters, &pk, &message, &signature).unwrap()) + }); + } + + fn schnorr_signature_randomize_pk(c: &mut Criterion) { + let mut rng = &mut rand::thread_rng(); + let parameters = SchnorrEdwards::setup(&mut rng).unwrap(); + let (pk, _) = SchnorrEdwards::keygen(¶meters, &mut rng).unwrap(); + let randomness: [u8; 32] = rng.gen(); + + c.bench_function("SchnorrEdwardsProjective: Randomize PubKey", move |b| { + b.iter(|| SchnorrEdwards::randomize_public_key(¶meters, &pk, &randomness).unwrap()) + }); + } + + fn schnorr_signature_randomize_signature(c: &mut Criterion) { + let mut rng = &mut rand::thread_rng(); + let parameters = SchnorrEdwards::setup(&mut rng).unwrap(); + let (_, sk) = SchnorrEdwards::keygen(¶meters, &mut rng).unwrap(); + let randomness: [u8; 32] = rng.gen(); + let message = [100u8; 128]; + let signature = SchnorrEdwards::sign(¶meters, &sk, &message, &mut rng).unwrap(); + + c.bench_function("SchnorrEdwardsProjective: Randomize Signature", move |b| { + b.iter(|| { + SchnorrEdwards::randomize_signature(¶meters, &signature, &randomness).unwrap() + }) + }); + } + criterion_group! { + name = schnorr_sig_projective; + config = Criterion::default().sample_size(20); + targets = schnorr_signature_setup, schnorr_signature_keygen, schnorr_signature_sign, + schnorr_signature_verify, schnorr_signature_randomize_pk, schnorr_signature_randomize_signature + } +} +use crate::{affine::schnorr_sig_affine, projective::schnorr_sig_projective}; +criterion_main!(schnorr_sig_affine, schnorr_sig_projective);