diff --git a/Cargo.lock b/Cargo.lock index 864b44bba..bc9a901c7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -493,6 +493,7 @@ name = "jubjub" version = "0.3.0" dependencies = [ "bls12_381 0.1.0", + "criterion 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand_xorshift 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "subtle 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/jubjub/Cargo.toml b/jubjub/Cargo.toml index 47f3b746d..31221efc7 100644 --- a/jubjub/Cargo.toml +++ b/jubjub/Cargo.toml @@ -22,6 +22,9 @@ default-features = false version = "^2.2.1" default-features = false +[dev-dependencies] +criterion = "0.3" + [dev-dependencies.rand_core] version = "0.5" default-features = false @@ -32,3 +35,15 @@ default-features = false [features] default = [] + +[[bench]] +name = "fq_bench" +harness = false + +[[bench]] +name = "fr_bench" +harness = false + +[[bench]] +name = "point_bench" +harness = false diff --git a/jubjub/benches/fq_bench.rs b/jubjub/benches/fq_bench.rs index 39fc1483f..65eceafc8 100644 --- a/jubjub/benches/fq_bench.rs +++ b/jubjub/benches/fq_bench.rs @@ -1,51 +1,58 @@ -#![feature(test)] - -extern crate test; - +use criterion::{criterion_group, criterion_main, Criterion}; use jubjub::*; -use test::Bencher; -#[bench] -fn bench_mul_assign(bencher: &mut Bencher) { +fn bench_add_assign(c: &mut Criterion) { let mut n = Fq::one(); - let b = -Fq::one(); - bencher.iter(move || { - n *= &b; + let neg_one = -Fq::one(); + c.bench_function("Fq add_assign", |b| { + b.iter(move || { + n += &neg_one; + }) }); } -#[bench] -fn bench_sub_assign(bencher: &mut Bencher) { +fn bench_sub_assign(c: &mut Criterion) { let mut n = Fq::one(); - let b = -Fq::one(); - bencher.iter(move || { - n -= &b; + let neg_one = -Fq::one(); + c.bench_function("Fq sub_assign", |b| { + b.iter(move || { + n -= &neg_one; + }) }); } -#[bench] -fn bench_add_assign(bencher: &mut Bencher) { +fn bench_mul_assign(c: &mut Criterion) { let mut n = Fq::one(); - let b = -Fq::one(); - bencher.iter(move || { - n += &b; + let neg_one = -Fq::one(); + c.bench_function("Fq mul_assign", |b| { + b.iter(move || { + n *= &neg_one; + }) }); } -#[bench] -fn bench_square_assign(bencher: &mut Bencher) { +fn bench_square(c: &mut Criterion) { let n = Fq::one(); - bencher.iter(move || n.square()); + c.bench_function("Fq square", |b| b.iter(move || n.square())); } -#[bench] -fn bench_invert(bencher: &mut Bencher) { +fn bench_invert(c: &mut Criterion) { let n = Fq::one(); - bencher.iter(move || n.invert()); + c.bench_function("Fq invert", |b| b.iter(move || n.invert())); } -#[bench] -fn bench_sqrt(bencher: &mut Bencher) { +fn bench_sqrt(c: &mut Criterion) { let n = Fq::one().double().double(); - bencher.iter(move || n.sqrt()); + c.bench_function("Fq sqrt", |b| b.iter(move || n.sqrt())); } + +criterion_group!( + benches, + bench_add_assign, + bench_sub_assign, + bench_mul_assign, + bench_square, + bench_invert, + bench_sqrt, +); +criterion_main!(benches); diff --git a/jubjub/benches/fr_bench.rs b/jubjub/benches/fr_bench.rs index b84c1b566..8dc9ce221 100644 --- a/jubjub/benches/fr_bench.rs +++ b/jubjub/benches/fr_bench.rs @@ -1,51 +1,58 @@ -#![feature(test)] - -extern crate test; - +use criterion::{criterion_group, criterion_main, Criterion}; use jubjub::*; -use test::Bencher; -#[bench] -fn bench_mul_assign(bencher: &mut Bencher) { +fn bench_add_assign(c: &mut Criterion) { let mut n = Fr::one(); - let b = -Fr::one(); - bencher.iter(move || { - n *= &b; + let neg_one = -Fr::one(); + c.bench_function("Fr add_assign", |b| { + b.iter(move || { + n += &neg_one; + }) }); } -#[bench] -fn bench_sub_assign(bencher: &mut Bencher) { +fn bench_sub_assign(c: &mut Criterion) { let mut n = Fr::one(); - let b = -Fr::one(); - bencher.iter(move || { - n -= &b; + let neg_one = -Fr::one(); + c.bench_function("Fr sub_assign", |b| { + b.iter(move || { + n -= &neg_one; + }) }); } -#[bench] -fn bench_add_assign(bencher: &mut Bencher) { +fn bench_mul_assign(c: &mut Criterion) { let mut n = Fr::one(); - let b = -Fr::one(); - bencher.iter(move || { - n += &b; + let neg_one = -Fr::one(); + c.bench_function("Fr mul_assign", |b| { + b.iter(move || { + n *= &neg_one; + }) }); } -#[bench] -fn bench_square_assign(bencher: &mut Bencher) { +fn bench_square(c: &mut Criterion) { let n = Fr::one(); - bencher.iter(move || n.square()); + c.bench_function("Fr square", |b| b.iter(move || n.square())); } -#[bench] -fn bench_invert(bencher: &mut Bencher) { +fn bench_invert(c: &mut Criterion) { let n = Fr::one(); - bencher.iter(move || n.invert()); + c.bench_function("Fr invert", |b| b.iter(move || n.invert())); } -#[bench] -fn bench_sqrt(bencher: &mut Bencher) { +fn bench_sqrt(c: &mut Criterion) { let n = Fr::one().double().double(); - bencher.iter(move || n.sqrt()); + c.bench_function("Fr sqrt", |b| b.iter(move || n.sqrt())); } + +criterion_group!( + benches, + bench_add_assign, + bench_sub_assign, + bench_mul_assign, + bench_square, + bench_invert, + bench_sqrt, +); +criterion_main!(benches); diff --git a/jubjub/benches/point_bench.rs b/jubjub/benches/point_bench.rs index d5b33a939..1659ea5f9 100644 --- a/jubjub/benches/point_bench.rs +++ b/jubjub/benches/point_bench.rs @@ -1,58 +1,73 @@ -#![feature(test)] - -extern crate test; - +use criterion::{criterion_group, criterion_main, Criterion}; use jubjub::*; -use test::Bencher; // Non-Niels -#[bench] -fn bench_point_doubling(bencher: &mut Bencher) { +fn bench_point_doubling(c: &mut Criterion) { let a = ExtendedPoint::identity(); - bencher.iter(move || a.double()); + c.bench_function("Jubjub point doubling", |bencher| { + bencher.iter(move || a.double()) + }); } -#[bench] -fn bench_point_addition(bencher: &mut Bencher) { +fn bench_point_addition(c: &mut Criterion) { let a = ExtendedPoint::identity(); let b = -ExtendedPoint::identity(); - bencher.iter(move || a + b); + c.bench_function("Jubjub point addition", |bencher| { + bencher.iter(move || a + b) + }); } -#[bench] -fn bench_point_subtraction(bencher: &mut Bencher) { +fn bench_point_subtraction(c: &mut Criterion) { let a = ExtendedPoint::identity(); let b = -ExtendedPoint::identity(); - bencher.iter(move || a + b); + c.bench_function("Jubjub point subtraction", |bencher| { + bencher.iter(move || a + b) + }); } // Niels -#[bench] -fn bench_cached_point_addition(bencher: &mut Bencher) { +fn bench_cached_point_addition(c: &mut Criterion) { let a = ExtendedPoint::identity(); let b = ExtendedPoint::identity().to_niels(); - bencher.iter(move || &a + &b); + c.bench_function("Jubjub cached point addition", |bencher| { + bencher.iter(move || a + b) + }); } -#[bench] -fn bench_cached_affine_point_subtraction(bencher: &mut Bencher) { - let a = ExtendedPoint::identity(); - let b = AffinePoint::identity().to_niels(); - bencher.iter(move || &a + &b); -} - -#[bench] -fn bench_cached_point_subtraction(bencher: &mut Bencher) { +fn bench_cached_point_subtraction(c: &mut Criterion) { let a = ExtendedPoint::identity(); let b = ExtendedPoint::identity().to_niels(); - bencher.iter(move || &a + &b); + c.bench_function("Jubjub cached point subtraction", |bencher| { + bencher.iter(move || a + b) + }); } -#[bench] -fn bench_cached_affine_point_addition(bencher: &mut Bencher) { +fn bench_cached_affine_point_addition(c: &mut Criterion) { let a = ExtendedPoint::identity(); let b = AffinePoint::identity().to_niels(); - bencher.iter(move || &a + &b); + c.bench_function("Jubjub cached affine point addition", |bencher| { + bencher.iter(move || a + b) + }); } + +fn bench_cached_affine_point_subtraction(c: &mut Criterion) { + let a = ExtendedPoint::identity(); + let b = AffinePoint::identity().to_niels(); + c.bench_function("Jubjub cached affine point subtraction", |bencher| { + bencher.iter(move || a + b) + }); +} + +criterion_group!( + benches, + bench_point_doubling, + bench_point_addition, + bench_point_subtraction, + bench_cached_point_addition, + bench_cached_point_subtraction, + bench_cached_affine_point_addition, + bench_cached_affine_point_subtraction, +); +criterion_main!(benches);