From 6c2c2b58de42d0c65ebb7d3696963ba295b92dfa Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Thu, 19 Dec 2019 16:46:16 -0600 Subject: [PATCH] Migrate pairing benchmarks to criterion --- Cargo.lock | 1 + pairing/Cargo.toml | 5 + pairing/benches/bls12_381/ec.rs | 110 +++++++++------ pairing/benches/bls12_381/fq.rs | 207 +++++++++++++++++------------ pairing/benches/bls12_381/fq12.rs | 81 ++++++----- pairing/benches/bls12_381/fq2.rs | 95 +++++++------ pairing/benches/bls12_381/fr.rs | 207 +++++++++++++++++------------ pairing/benches/bls12_381/mod.rs | 85 +++++++----- pairing/benches/pairing_benches.rs | 20 +-- 9 files changed, 477 insertions(+), 334 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 032e7e5a7..8e8c90577 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -600,6 +600,7 @@ name = "pairing" version = "0.15.0" dependencies = [ "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "criterion 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "ff 0.5.0", "group 0.2.0", "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/pairing/Cargo.toml b/pairing/Cargo.toml index 32fc1befe..8c9e291cf 100644 --- a/pairing/Cargo.toml +++ b/pairing/Cargo.toml @@ -24,6 +24,7 @@ rand_core = "0.5" subtle = "2.2.1" [dev-dependencies] +criterion = "0.3" rand_xorshift = "0.2" [features] @@ -31,5 +32,9 @@ unstable-features = ["expose-arith"] expose-arith = [] default = [] +[[bench]] +name = "pairing_benches" +harness = false + [badges] maintenance = { status = "actively-developed" } diff --git a/pairing/benches/bls12_381/ec.rs b/pairing/benches/bls12_381/ec.rs index 04bed0d5b..34113629d 100644 --- a/pairing/benches/bls12_381/ec.rs +++ b/pairing/benches/bls12_381/ec.rs @@ -1,4 +1,5 @@ -mod g1 { +pub(crate) mod g1 { + use criterion::{criterion_group, Criterion}; use rand_core::SeedableRng; use rand_xorshift::XorShiftRng; @@ -6,8 +7,7 @@ mod g1 { use group::CurveProjective; use pairing::bls12_381::*; - #[bench] - fn bench_g1_mul_assign(b: &mut ::test::Bencher) { + fn bench_g1_mul_assign(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -20,16 +20,17 @@ mod g1 { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - tmp.mul_assign(v[count].1); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("G1::mul_assign", |b| { + b.iter(|| { + let mut tmp = v[count].0; + tmp.mul_assign(v[count].1); + count = (count + 1) % SAMPLES; + tmp + }) }); } - #[bench] - fn bench_g1_add_assign(b: &mut ::test::Bencher) { + fn bench_g1_add_assign(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -42,16 +43,17 @@ mod g1 { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - tmp.add_assign(&v[count].1); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("G1::add_assign", |b| { + b.iter(|| { + let mut tmp = v[count].0; + tmp.add_assign(&v[count].1); + count = (count + 1) % SAMPLES; + tmp + }) }); } - #[bench] - fn bench_g1_add_assign_mixed(b: &mut ::test::Bencher) { + fn bench_g1_add_assign_mixed(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -64,16 +66,26 @@ mod g1 { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - tmp.add_assign_mixed(&v[count].1); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("G1::add_assign_mixed", |b| { + b.iter(|| { + let mut tmp = v[count].0; + tmp.add_assign_mixed(&v[count].1); + count = (count + 1) % SAMPLES; + tmp + }) }); } + + criterion_group!( + benches, + bench_g1_add_assign, + bench_g1_add_assign_mixed, + bench_g1_mul_assign, + ); } -mod g2 { +pub(crate) mod g2 { + use criterion::{criterion_group, Criterion}; use rand_core::SeedableRng; use rand_xorshift::XorShiftRng; @@ -81,8 +93,7 @@ mod g2 { use group::CurveProjective; use pairing::bls12_381::*; - #[bench] - fn bench_g2_mul_assign(b: &mut ::test::Bencher) { + fn bench_g2_mul_assign(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -95,16 +106,17 @@ mod g2 { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - tmp.mul_assign(v[count].1); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("G2::mul_assign", |b| { + b.iter(|| { + let mut tmp = v[count].0; + tmp.mul_assign(v[count].1); + count = (count + 1) % SAMPLES; + tmp + }) }); } - #[bench] - fn bench_g2_add_assign(b: &mut ::test::Bencher) { + fn bench_g2_add_assign(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -117,16 +129,17 @@ mod g2 { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - tmp.add_assign(&v[count].1); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("G2::add_assign", |b| { + b.iter(|| { + let mut tmp = v[count].0; + tmp.add_assign(&v[count].1); + count = (count + 1) % SAMPLES; + tmp + }) }); } - #[bench] - fn bench_g2_add_assign_mixed(b: &mut ::test::Bencher) { + fn bench_g2_add_assign_mixed(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -139,11 +152,20 @@ mod g2 { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - tmp.add_assign_mixed(&v[count].1); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("G2::add_assign_mixed", |b| { + b.iter(|| { + let mut tmp = v[count].0; + tmp.add_assign_mixed(&v[count].1); + count = (count + 1) % SAMPLES; + tmp + }) }); } + + criterion_group!( + benches, + bench_g2_add_assign, + bench_g2_add_assign_mixed, + bench_g2_mul_assign, + ); } diff --git a/pairing/benches/bls12_381/fq.rs b/pairing/benches/bls12_381/fq.rs index 5ef576802..244c161bc 100644 --- a/pairing/benches/bls12_381/fq.rs +++ b/pairing/benches/bls12_381/fq.rs @@ -1,3 +1,4 @@ +use criterion::{criterion_group, Criterion}; use rand_core::SeedableRng; use rand_xorshift::XorShiftRng; use std::ops::{AddAssign, MulAssign, Neg, SubAssign}; @@ -5,8 +6,7 @@ use std::ops::{AddAssign, MulAssign, Neg, SubAssign}; use ff::{Field, PrimeField, PrimeFieldRepr, SqrtField}; use pairing::bls12_381::*; -#[bench] -fn bench_fq_repr_add_nocarry(b: &mut ::test::Bencher) { +fn bench_fq_repr_add_nocarry(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -28,16 +28,17 @@ fn bench_fq_repr_add_nocarry(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - tmp.add_nocarry(&v[count].1); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("FqRepr::add_nocarry", |b| { + b.iter(|| { + let mut tmp = v[count].0; + tmp.add_nocarry(&v[count].1); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fq_repr_sub_noborrow(b: &mut ::test::Bencher) { +fn bench_fq_repr_sub_noborrow(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -58,16 +59,17 @@ fn bench_fq_repr_sub_noborrow(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - tmp.sub_noborrow(&v[count].1); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("FqRepr::sub_noborrow", |b| { + b.iter(|| { + let mut tmp = v[count].0; + tmp.sub_noborrow(&v[count].1); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fq_repr_num_bits(b: &mut ::test::Bencher) { +fn bench_fq_repr_num_bits(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -80,15 +82,16 @@ fn bench_fq_repr_num_bits(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let tmp = v[count].num_bits(); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("FqRepr::num_bits", |b| { + b.iter(|| { + let tmp = v[count].num_bits(); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fq_repr_mul2(b: &mut ::test::Bencher) { +fn bench_fq_repr_mul2(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -101,16 +104,17 @@ fn bench_fq_repr_mul2(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count]; - tmp.mul2(); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("FqRepr::mul2", |b| { + b.iter(|| { + let mut tmp = v[count]; + tmp.mul2(); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fq_repr_div2(b: &mut ::test::Bencher) { +fn bench_fq_repr_div2(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -123,16 +127,17 @@ fn bench_fq_repr_div2(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count]; - tmp.div2(); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("FqRepr::div2", |b| { + b.iter(|| { + let mut tmp = v[count]; + tmp.div2(); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fq_add_assign(b: &mut ::test::Bencher) { +fn bench_fq_add_assign(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -145,16 +150,17 @@ fn bench_fq_add_assign(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - tmp.add_assign(&v[count].1); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Fq::add_assign", |b| { + b.iter(|| { + let mut tmp = v[count].0; + tmp.add_assign(&v[count].1); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fq_sub_assign(b: &mut ::test::Bencher) { +fn bench_fq_sub_assign(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -167,16 +173,17 @@ fn bench_fq_sub_assign(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - tmp.sub_assign(&v[count].1); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Fq::sub_assign", |b| { + b.iter(|| { + let mut tmp = v[count].0; + tmp.sub_assign(&v[count].1); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fq_mul_assign(b: &mut ::test::Bencher) { +fn bench_fq_mul_assign(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -189,16 +196,17 @@ fn bench_fq_mul_assign(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - tmp.mul_assign(&v[count].1); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Fq::mul_assign", |b| { + b.iter(|| { + let mut tmp = v[count].0; + tmp.mul_assign(&v[count].1); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fq_square(b: &mut ::test::Bencher) { +fn bench_fq_square(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -209,15 +217,16 @@ fn bench_fq_square(b: &mut ::test::Bencher) { let v: Vec = (0..SAMPLES).map(|_| Fq::random(&mut rng)).collect(); let mut count = 0; - b.iter(|| { - let tmp = v[count].square(); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Fq::square", |b| { + b.iter(|| { + let tmp = v[count].square(); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fq_invert(b: &mut ::test::Bencher) { +fn bench_fq_invert(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -228,14 +237,15 @@ fn bench_fq_invert(b: &mut ::test::Bencher) { let v: Vec = (0..SAMPLES).map(|_| Fq::random(&mut rng)).collect(); let mut count = 0; - b.iter(|| { - count = (count + 1) % SAMPLES; - v[count].invert() + c.bench_function("Fq::invert", |b| { + b.iter(|| { + count = (count + 1) % SAMPLES; + v[count].invert() + }) }); } -#[bench] -fn bench_fq_neg(b: &mut ::test::Bencher) { +fn bench_fq_neg(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -246,15 +256,16 @@ fn bench_fq_neg(b: &mut ::test::Bencher) { let v: Vec = (0..SAMPLES).map(|_| Fq::random(&mut rng)).collect(); let mut count = 0; - b.iter(|| { - let tmp = v[count].neg(); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Fq::neg", |b| { + b.iter(|| { + let tmp = v[count].neg(); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fq_sqrt(b: &mut ::test::Bencher) { +fn bench_fq_sqrt(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -267,14 +278,15 @@ fn bench_fq_sqrt(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - count = (count + 1) % SAMPLES; - v[count].sqrt() + c.bench_function("Fq::sqrt", |b| { + b.iter(|| { + count = (count + 1) % SAMPLES; + v[count].sqrt() + }) }); } -#[bench] -fn bench_fq_into_repr(b: &mut ::test::Bencher) { +fn bench_fq_into_repr(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -285,14 +297,15 @@ fn bench_fq_into_repr(b: &mut ::test::Bencher) { let v: Vec = (0..SAMPLES).map(|_| Fq::random(&mut rng)).collect(); let mut count = 0; - b.iter(|| { - count = (count + 1) % SAMPLES; - v[count].into_repr() + c.bench_function("Fq::into_repr", |b| { + b.iter(|| { + count = (count + 1) % SAMPLES; + v[count].into_repr() + }) }); } -#[bench] -fn bench_fq_from_repr(b: &mut ::test::Bencher) { +fn bench_fq_from_repr(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -305,8 +318,28 @@ fn bench_fq_from_repr(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - count = (count + 1) % SAMPLES; - Fq::from_repr(v[count]) + c.bench_function("Fq::from_repr", |b| { + b.iter(|| { + count = (count + 1) % SAMPLES; + Fq::from_repr(v[count]) + }) }); } + +criterion_group!( + benches, + bench_fq_repr_add_nocarry, + bench_fq_repr_sub_noborrow, + bench_fq_repr_num_bits, + bench_fq_repr_mul2, + bench_fq_repr_div2, + bench_fq_add_assign, + bench_fq_sub_assign, + bench_fq_mul_assign, + bench_fq_square, + bench_fq_invert, + bench_fq_neg, + bench_fq_sqrt, + bench_fq_into_repr, + bench_fq_from_repr, +); diff --git a/pairing/benches/bls12_381/fq12.rs b/pairing/benches/bls12_381/fq12.rs index eedd9f8de..2d6ed490e 100644 --- a/pairing/benches/bls12_381/fq12.rs +++ b/pairing/benches/bls12_381/fq12.rs @@ -1,3 +1,4 @@ +use criterion::{criterion_group, Criterion}; use rand_core::SeedableRng; use rand_xorshift::XorShiftRng; use std::ops::{AddAssign, MulAssign, SubAssign}; @@ -5,8 +6,7 @@ use std::ops::{AddAssign, MulAssign, SubAssign}; use ff::Field; use pairing::bls12_381::*; -#[bench] -fn bench_fq12_add_assign(b: &mut ::test::Bencher) { +fn bench_fq12_add_assign(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -19,16 +19,17 @@ fn bench_fq12_add_assign(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - tmp.add_assign(&v[count].1); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Fq12::add_assign", |b| { + b.iter(|| { + let mut tmp = v[count].0; + tmp.add_assign(&v[count].1); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fq12_sub_assign(b: &mut ::test::Bencher) { +fn bench_fq12_sub_assign(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -41,16 +42,17 @@ fn bench_fq12_sub_assign(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - tmp.sub_assign(&v[count].1); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Fq12::sub_assign", |b| { + b.iter(|| { + let mut tmp = v[count].0; + tmp.sub_assign(&v[count].1); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fq12_mul_assign(b: &mut ::test::Bencher) { +fn bench_fq12_mul_assign(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -63,16 +65,17 @@ fn bench_fq12_mul_assign(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - tmp.mul_assign(&v[count].1); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Fq12::mul_assign", |b| { + b.iter(|| { + let mut tmp = v[count].0; + tmp.mul_assign(&v[count].1); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fq12_squaring(b: &mut ::test::Bencher) { +fn bench_fq12_squaring(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -83,15 +86,16 @@ fn bench_fq12_squaring(b: &mut ::test::Bencher) { let v: Vec = (0..SAMPLES).map(|_| Fq12::random(&mut rng)).collect(); let mut count = 0; - b.iter(|| { - let tmp = v[count].square(); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Fq12::square", |b| { + b.iter(|| { + let tmp = v[count].square(); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fq12_invert(b: &mut ::test::Bencher) { +fn bench_fq12_invert(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -102,9 +106,20 @@ fn bench_fq12_invert(b: &mut ::test::Bencher) { let v: Vec = (0..SAMPLES).map(|_| Fq12::random(&mut rng)).collect(); let mut count = 0; - b.iter(|| { - let tmp = v[count].invert(); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Fq12::invert", |b| { + b.iter(|| { + let tmp = v[count].invert(); + count = (count + 1) % SAMPLES; + tmp + }) }); } + +criterion_group!( + benches, + bench_fq12_add_assign, + bench_fq12_sub_assign, + bench_fq12_mul_assign, + bench_fq12_squaring, + bench_fq12_invert, +); diff --git a/pairing/benches/bls12_381/fq2.rs b/pairing/benches/bls12_381/fq2.rs index d3a2b4d51..1eebb92db 100644 --- a/pairing/benches/bls12_381/fq2.rs +++ b/pairing/benches/bls12_381/fq2.rs @@ -1,3 +1,4 @@ +use criterion::{criterion_group, Criterion}; use rand_core::SeedableRng; use rand_xorshift::XorShiftRng; use std::ops::{AddAssign, MulAssign, SubAssign}; @@ -5,8 +6,7 @@ use std::ops::{AddAssign, MulAssign, SubAssign}; use ff::{Field, SqrtField}; use pairing::bls12_381::*; -#[bench] -fn bench_fq2_add_assign(b: &mut ::test::Bencher) { +fn bench_fq2_add_assign(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -19,16 +19,17 @@ fn bench_fq2_add_assign(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - tmp.add_assign(&v[count].1); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Fq2::add_assign", |b| { + b.iter(|| { + let mut tmp = v[count].0; + tmp.add_assign(&v[count].1); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fq2_sub_assign(b: &mut ::test::Bencher) { +fn bench_fq2_sub_assign(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -41,16 +42,17 @@ fn bench_fq2_sub_assign(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - tmp.sub_assign(&v[count].1); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Fq2::sub_assign", |b| { + b.iter(|| { + let mut tmp = v[count].0; + tmp.sub_assign(&v[count].1); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fq2_mul_assign(b: &mut ::test::Bencher) { +fn bench_fq2_mul_assign(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -63,16 +65,17 @@ fn bench_fq2_mul_assign(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - tmp.mul_assign(&v[count].1); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Fq2::mul_assign", |b| { + b.iter(|| { + let mut tmp = v[count].0; + tmp.mul_assign(&v[count].1); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fq2_squaring(b: &mut ::test::Bencher) { +fn bench_fq2_squaring(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -83,15 +86,16 @@ fn bench_fq2_squaring(b: &mut ::test::Bencher) { let v: Vec = (0..SAMPLES).map(|_| Fq2::random(&mut rng)).collect(); let mut count = 0; - b.iter(|| { - let tmp = v[count].square(); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Fq2::square", |b| { + b.iter(|| { + let tmp = v[count].square(); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fq2_invert(b: &mut ::test::Bencher) { +fn bench_fq2_invert(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -102,15 +106,16 @@ fn bench_fq2_invert(b: &mut ::test::Bencher) { let v: Vec = (0..SAMPLES).map(|_| Fq2::random(&mut rng)).collect(); let mut count = 0; - b.iter(|| { - let tmp = v[count].invert(); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Fq2::invert", |b| { + b.iter(|| { + let tmp = v[count].invert(); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fq2_sqrt(b: &mut ::test::Bencher) { +fn bench_fq2_sqrt(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -121,9 +126,21 @@ fn bench_fq2_sqrt(b: &mut ::test::Bencher) { let v: Vec = (0..SAMPLES).map(|_| Fq2::random(&mut rng)).collect(); let mut count = 0; - b.iter(|| { - let tmp = v[count].sqrt(); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Fq2::sqrt", |b| { + b.iter(|| { + let tmp = v[count].sqrt(); + count = (count + 1) % SAMPLES; + tmp + }) }); } + +criterion_group!( + benches, + bench_fq2_add_assign, + bench_fq2_sub_assign, + bench_fq2_mul_assign, + bench_fq2_squaring, + bench_fq2_invert, + bench_fq2_sqrt, +); diff --git a/pairing/benches/bls12_381/fr.rs b/pairing/benches/bls12_381/fr.rs index 4e3d4c2e2..d2dbc4c21 100644 --- a/pairing/benches/bls12_381/fr.rs +++ b/pairing/benches/bls12_381/fr.rs @@ -1,3 +1,4 @@ +use criterion::{criterion_group, Criterion}; use rand_core::SeedableRng; use rand_xorshift::XorShiftRng; use std::ops::{AddAssign, MulAssign, Neg, SubAssign}; @@ -5,8 +6,7 @@ use std::ops::{AddAssign, MulAssign, Neg, SubAssign}; use ff::{Field, PrimeField, PrimeFieldRepr, SqrtField}; use pairing::bls12_381::*; -#[bench] -fn bench_fr_repr_add_nocarry(b: &mut ::test::Bencher) { +fn bench_fr_repr_add_nocarry(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -28,16 +28,17 @@ fn bench_fr_repr_add_nocarry(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - tmp.add_nocarry(&v[count].1); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("FrRepr::add_nocarry", |b| { + b.iter(|| { + let mut tmp = v[count].0; + tmp.add_nocarry(&v[count].1); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fr_repr_sub_noborrow(b: &mut ::test::Bencher) { +fn bench_fr_repr_sub_noborrow(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -58,16 +59,17 @@ fn bench_fr_repr_sub_noborrow(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - tmp.sub_noborrow(&v[count].1); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("FrRepr::sub_noborrow", |b| { + b.iter(|| { + let mut tmp = v[count].0; + tmp.sub_noborrow(&v[count].1); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fr_repr_num_bits(b: &mut ::test::Bencher) { +fn bench_fr_repr_num_bits(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -80,15 +82,16 @@ fn bench_fr_repr_num_bits(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let tmp = v[count].num_bits(); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("FrRepr::num_bits", |b| { + b.iter(|| { + let tmp = v[count].num_bits(); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fr_repr_mul2(b: &mut ::test::Bencher) { +fn bench_fr_repr_mul2(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -101,16 +104,17 @@ fn bench_fr_repr_mul2(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count]; - tmp.mul2(); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("FrRepr::mul2", |b| { + b.iter(|| { + let mut tmp = v[count]; + tmp.mul2(); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fr_repr_div2(b: &mut ::test::Bencher) { +fn bench_fr_repr_div2(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -123,16 +127,17 @@ fn bench_fr_repr_div2(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count]; - tmp.div2(); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("FrRepr::div2", |b| { + b.iter(|| { + let mut tmp = v[count]; + tmp.div2(); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fr_add_assign(b: &mut ::test::Bencher) { +fn bench_fr_add_assign(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -145,16 +150,17 @@ fn bench_fr_add_assign(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - tmp.add_assign(&v[count].1); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Fr::add_assign", |b| { + b.iter(|| { + let mut tmp = v[count].0; + tmp.add_assign(&v[count].1); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fr_sub_assign(b: &mut ::test::Bencher) { +fn bench_fr_sub_assign(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -167,16 +173,17 @@ fn bench_fr_sub_assign(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - tmp.sub_assign(&v[count].1); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Fr::sub_assign", |b| { + b.iter(|| { + let mut tmp = v[count].0; + tmp.sub_assign(&v[count].1); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fr_mul_assign(b: &mut ::test::Bencher) { +fn bench_fr_mul_assign(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -189,16 +196,17 @@ fn bench_fr_mul_assign(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - tmp.mul_assign(&v[count].1); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Fr::mul_assign", |b| { + b.iter(|| { + let mut tmp = v[count].0; + tmp.mul_assign(&v[count].1); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fr_square(b: &mut ::test::Bencher) { +fn bench_fr_square(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -209,15 +217,16 @@ fn bench_fr_square(b: &mut ::test::Bencher) { let v: Vec = (0..SAMPLES).map(|_| Fr::random(&mut rng)).collect(); let mut count = 0; - b.iter(|| { - let tmp = v[count].square(); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Fr::square", |b| { + b.iter(|| { + let tmp = v[count].square(); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fr_invert(b: &mut ::test::Bencher) { +fn bench_fr_invert(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -228,14 +237,15 @@ fn bench_fr_invert(b: &mut ::test::Bencher) { let v: Vec = (0..SAMPLES).map(|_| Fr::random(&mut rng)).collect(); let mut count = 0; - b.iter(|| { - count = (count + 1) % SAMPLES; - v[count].invert() + c.bench_function("Fr::invert", |b| { + b.iter(|| { + count = (count + 1) % SAMPLES; + v[count].invert() + }) }); } -#[bench] -fn bench_fr_neg(b: &mut ::test::Bencher) { +fn bench_fr_neg(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -246,15 +256,16 @@ fn bench_fr_neg(b: &mut ::test::Bencher) { let v: Vec = (0..SAMPLES).map(|_| Fr::random(&mut rng)).collect(); let mut count = 0; - b.iter(|| { - let tmp = v[count].neg(); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Fr::neg", |b| { + b.iter(|| { + let tmp = v[count].neg(); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_fr_sqrt(b: &mut ::test::Bencher) { +fn bench_fr_sqrt(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -267,14 +278,15 @@ fn bench_fr_sqrt(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - count = (count + 1) % SAMPLES; - v[count].sqrt() + c.bench_function("Fr::sqrt", |b| { + b.iter(|| { + count = (count + 1) % SAMPLES; + v[count].sqrt() + }) }); } -#[bench] -fn bench_fr_into_repr(b: &mut ::test::Bencher) { +fn bench_fr_into_repr(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -285,14 +297,15 @@ fn bench_fr_into_repr(b: &mut ::test::Bencher) { let v: Vec = (0..SAMPLES).map(|_| Fr::random(&mut rng)).collect(); let mut count = 0; - b.iter(|| { - count = (count + 1) % SAMPLES; - v[count].into_repr() + c.bench_function("Fr::into_repr", |b| { + b.iter(|| { + count = (count + 1) % SAMPLES; + v[count].into_repr() + }) }); } -#[bench] -fn bench_fr_from_repr(b: &mut ::test::Bencher) { +fn bench_fr_from_repr(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -305,8 +318,28 @@ fn bench_fr_from_repr(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - count = (count + 1) % SAMPLES; - Fr::from_repr(v[count]) + c.bench_function("Fr::from_repr", |b| { + b.iter(|| { + count = (count + 1) % SAMPLES; + Fr::from_repr(v[count]) + }) }); } + +criterion_group!( + benches, + bench_fr_repr_add_nocarry, + bench_fr_repr_sub_noborrow, + bench_fr_repr_num_bits, + bench_fr_repr_mul2, + bench_fr_repr_div2, + bench_fr_add_assign, + bench_fr_sub_assign, + bench_fr_mul_assign, + bench_fr_square, + bench_fr_invert, + bench_fr_neg, + bench_fr_sqrt, + bench_fr_into_repr, + bench_fr_from_repr, +); diff --git a/pairing/benches/bls12_381/mod.rs b/pairing/benches/bls12_381/mod.rs index 2c23c2a2e..aa2e2f087 100644 --- a/pairing/benches/bls12_381/mod.rs +++ b/pairing/benches/bls12_381/mod.rs @@ -1,9 +1,10 @@ -mod ec; -mod fq; -mod fq12; -mod fq2; -mod fr; +pub(crate) mod ec; +pub(crate) mod fq; +pub(crate) mod fq12; +pub(crate) mod fq2; +pub(crate) mod fr; +use criterion::{criterion_group, Criterion}; use rand_core::SeedableRng; use rand_xorshift::XorShiftRng; @@ -11,8 +12,7 @@ use group::CurveProjective; use pairing::bls12_381::*; use pairing::{Engine, PairingCurveAffine}; -#[bench] -fn bench_pairing_g1_preparation(b: &mut ::test::Bencher) { +fn bench_pairing_g1_preparation(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -23,15 +23,16 @@ fn bench_pairing_g1_preparation(b: &mut ::test::Bencher) { let v: Vec = (0..SAMPLES).map(|_| G1::random(&mut rng)).collect(); let mut count = 0; - b.iter(|| { - let tmp = G1Affine::from(v[count]).prepare(); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("G1 preparation", |b| { + b.iter(|| { + let tmp = G1Affine::from(v[count]).prepare(); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_pairing_g2_preparation(b: &mut ::test::Bencher) { +fn bench_pairing_g2_preparation(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -42,15 +43,16 @@ fn bench_pairing_g2_preparation(b: &mut ::test::Bencher) { let v: Vec = (0..SAMPLES).map(|_| G2::random(&mut rng)).collect(); let mut count = 0; - b.iter(|| { - let tmp = G2Affine::from(v[count]).prepare(); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("G2 preparation", |b| { + b.iter(|| { + let tmp = G2Affine::from(v[count]).prepare(); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_pairing_miller_loop(b: &mut ::test::Bencher) { +fn bench_pairing_miller_loop(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -68,15 +70,16 @@ fn bench_pairing_miller_loop(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let tmp = Bls12::miller_loop(&[(&v[count].0, &v[count].1)]); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Miller loop", |b| { + b.iter(|| { + let tmp = Bls12::miller_loop(&[(&v[count].0, &v[count].1)]); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_pairing_final_exponentiation(b: &mut ::test::Bencher) { +fn bench_pairing_final_exponentiation(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -95,15 +98,16 @@ fn bench_pairing_final_exponentiation(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let tmp = Bls12::final_exponentiation(&v[count]); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Final exponentiation", |b| { + b.iter(|| { + let tmp = Bls12::final_exponentiation(&v[count]); + count = (count + 1) % SAMPLES; + tmp + }) }); } -#[bench] -fn bench_pairing_full(b: &mut ::test::Bencher) { +fn bench_pairing_full(c: &mut Criterion) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::from_seed([ @@ -116,9 +120,20 @@ fn bench_pairing_full(b: &mut ::test::Bencher) { .collect(); let mut count = 0; - b.iter(|| { - let tmp = Bls12::pairing(v[count].0, v[count].1); - count = (count + 1) % SAMPLES; - tmp + c.bench_function("Full pairing", |b| { + b.iter(|| { + let tmp = Bls12::pairing(v[count].0, v[count].1); + count = (count + 1) % SAMPLES; + tmp + }) }); } + +criterion_group!( + benches, + bench_pairing_g1_preparation, + bench_pairing_g2_preparation, + bench_pairing_miller_loop, + bench_pairing_final_exponentiation, + bench_pairing_full, +); diff --git a/pairing/benches/pairing_benches.rs b/pairing/benches/pairing_benches.rs index b083b4250..7abc82453 100644 --- a/pairing/benches/pairing_benches.rs +++ b/pairing/benches/pairing_benches.rs @@ -1,10 +1,12 @@ -#![feature(test)] - -extern crate ff; -extern crate group; -extern crate pairing; -extern crate rand_core; -extern crate rand_xorshift; -extern crate test; - +use criterion::criterion_main; mod bls12_381; + +criterion_main!( + bls12_381::benches, + bls12_381::ec::g1::benches, + bls12_381::ec::g2::benches, + bls12_381::fq::benches, + bls12_381::fq12::benches, + bls12_381::fq2::benches, + bls12_381::fr::benches, +);