From 651f772755d78601bcf1b012a8a8661bf30f32fb Mon Sep 17 00:00:00 2001 From: Pratyush Mishra Date: Thu, 10 Dec 2020 16:43:01 -0800 Subject: [PATCH] Move to criterion --- curve-benches/Cargo.toml | 53 +- .../{src/curves => benches}/bls12_377.rs | 13 +- .../{src/curves => benches}/bls12_381.rs | 13 +- .../{src/curves => benches}/bn254.rs | 13 +- .../{src/curves => benches}/bw6_761.rs | 12 +- .../{src/curves => benches}/cp6_782.rs | 9 +- .../curves => benches}/ed_on_bls12_381.rs | 3 + .../{src/curves => benches}/mnt4_298.rs | 13 +- .../{src/curves => benches}/mnt4_753.rs | 13 +- .../{src/curves => benches}/mnt6_298.rs | 13 +- .../{src/curves => benches}/mnt6_753.rs | 13 +- curve-benches/src/curves/mod.rs | 20 - curve-benches/src/lib.rs | 17 +- curve-benches/src/macros/ec.rs | 51 +- curve-benches/src/macros/field.rs | 690 +++++++++--------- curve-benches/src/macros/mod.rs | 6 +- curve-benches/src/macros/pairing.rs | 41 +- curve-benches/src/macros/utils.rs | 41 +- 18 files changed, 560 insertions(+), 474 deletions(-) rename curve-benches/{src/curves => benches}/bls12_377.rs (71%) rename curve-benches/{src/curves => benches}/bls12_381.rs (71%) rename curve-benches/{src/curves => benches}/bn254.rs (71%) rename curve-benches/{src/curves => benches}/bw6_761.rs (73%) rename curve-benches/{src/curves => benches}/cp6_782.rs (74%) rename curve-benches/{src/curves => benches}/ed_on_bls12_381.rs (86%) rename curve-benches/{src/curves => benches}/mnt4_298.rs (67%) rename curve-benches/{src/curves => benches}/mnt4_753.rs (67%) rename curve-benches/{src/curves => benches}/mnt6_298.rs (67%) rename curve-benches/{src/curves => benches}/mnt6_753.rs (67%) delete mode 100644 curve-benches/src/curves/mod.rs diff --git a/curve-benches/Cargo.toml b/curve-benches/Cargo.toml index b17061b..b3bc5c6 100644 --- a/curve-benches/Cargo.toml +++ b/curve-benches/Cargo.toml @@ -38,10 +38,11 @@ ark-bls12-381 = { path = "../bls12_381", optional = true } ark-ed-on-bls12-381 = { path = "../ed_on_bls12_381", optional = true } ark-bw6-761 = { path = "../bw6_761", optional = true } ark-cp6-782 = { path = "../cp6_782", optional = true } +criterion = { version = "0.3", optional = true } +paste = { version = "1.0" } rand = "0.7" rand_xorshift = { version = "0.2" } -paste = "1.0" [features] asm = [ "ark-ff/asm"] @@ -49,3 +50,53 @@ n_fold = [] [build-dependencies] rustc_version = "0.2" + +[[bench]] +name = "bls12_377" +path = "benches/bls12_377.rs" +harness = false + +[[bench]] +name = "bls12_381" +path = "benches/bls12_381.rs" +harness = false + +[[bench]] +name = "bn254" +path = "benches/bn254.rs" +harness = false + +[[bench]] +name = "bw6_761" +path = "benches/bw6_761.rs" +harness = false + +[[bench]] +name = "cp6_782" +path = "benches/cp6_782.rs" +harness = false + +[[bench]] +name = "ed_on_bls12_381" +path = "benches/ed_on_bls12_381.rs" +harness = false + +[[bench]] +name = "mnt4_298" +path = "benches/mnt4_298.rs" +harness = false + +[[bench]] +name = "mnt6_298" +path = "benches/mnt6_298.rs" +harness = false + +[[bench]] +name = "mnt4_753" +path = "benches/mnt4_753.rs" +harness = false + +[[bench]] +name = "mnt6_753" +path = "benches/mnt6_753.rs" +harness = false diff --git a/curve-benches/src/curves/bls12_377.rs b/curve-benches/benches/bls12_377.rs similarity index 71% rename from curve-benches/src/curves/bls12_377.rs rename to curve-benches/benches/bls12_377.rs index c198c25..bea6cee 100644 --- a/curve-benches/src/curves/bls12_377.rs +++ b/curve-benches/benches/bls12_377.rs @@ -1,13 +1,13 @@ use rand::SeedableRng; use rand_xorshift::XorShiftRng; use std::ops::{AddAssign, MulAssign, SubAssign}; +use ark_curve_benches::*; use ark_bls12_377::{ fq::Fq, fq2::Fq2, fr::Fr, Bls12_377, Fq12, G1Affine, G1Projective as G1, G2Affine, - G2Projective as G2, Parameters, + G2Projective as G2, }; use ark_ec::{ - bls12::{G1Prepared, G2Prepared}, PairingEngine, ProjectiveCurve, }; use ark_ff::{ @@ -24,8 +24,11 @@ mod g2 { ec_bench!(G2, G2Affine); } -f_bench!(1, Fq2, Fq2, fq2); -f_bench!(2, Fq12, Fq12, fq12); f_bench!(Fq, Fq, FqRepr, FqRepr, fq); f_bench!(Fr, Fr, FrRepr, FrRepr, fr); -pairing_bench!(Bls12_377, Fq12, prepared_v); +f_bench!(extension, Fq2, Fq2, fq2); +f_bench!(target, Fq12, Fq12, fq12); + +pairing_bench!(Bls12_377, Fq12); + +criterion::criterion_main!(g1::group_ops, g2::group_ops, fq, fr, fq2, fq12, pairing); diff --git a/curve-benches/src/curves/bls12_381.rs b/curve-benches/benches/bls12_381.rs similarity index 71% rename from curve-benches/src/curves/bls12_381.rs rename to curve-benches/benches/bls12_381.rs index cd58eb4..07d2418 100644 --- a/curve-benches/src/curves/bls12_381.rs +++ b/curve-benches/benches/bls12_381.rs @@ -1,13 +1,13 @@ use rand::SeedableRng; use rand_xorshift::XorShiftRng; use std::ops::{AddAssign, MulAssign, SubAssign}; +use ark_curve_benches::*; use ark_bls12_381::{ fq::Fq, fq2::Fq2, fr::Fr, Bls12_381, Fq12, G1Affine, G1Projective as G1, G2Affine, - G2Projective as G2, Parameters, + G2Projective as G2, }; use ark_ec::{ - bls12::{G1Prepared, G2Prepared}, PairingEngine, ProjectiveCurve, }; use ark_ff::{ @@ -24,8 +24,11 @@ mod g2 { ec_bench!(G2, G2Affine); } -f_bench!(1, Fq2, Fq2, fq2); -f_bench!(2, Fq12, Fq12, fq12); f_bench!(Fq, Fq, FqRepr, FqRepr, fq); f_bench!(Fr, Fr, FrRepr, FrRepr, fr); -pairing_bench!(Bls12_381, Fq12, prepared_v); +f_bench!(extension, Fq2, Fq2, fq2); +f_bench!(target, Fq12, Fq12, fq12); + +pairing_bench!(Bls12_381, Fq12); + +criterion::criterion_main!(g1::group_ops, g2::group_ops, fq, fr, fq2, fq12, pairing); diff --git a/curve-benches/src/curves/bn254.rs b/curve-benches/benches/bn254.rs similarity index 71% rename from curve-benches/src/curves/bn254.rs rename to curve-benches/benches/bn254.rs index e430ffb..539ef2d 100644 --- a/curve-benches/src/curves/bn254.rs +++ b/curve-benches/benches/bn254.rs @@ -1,13 +1,13 @@ use rand::SeedableRng; use rand_xorshift::XorShiftRng; use std::ops::{AddAssign, MulAssign, SubAssign}; +use ark_curve_benches::*; use ark_bn254::{ fq::Fq, fq2::Fq2, fr::Fr, Bn254, Fq12, G1Affine, G1Projective as G1, G2Affine, - G2Projective as G2, Parameters, + G2Projective as G2, }; use ark_ec::{ - bn::{G1Prepared, G2Prepared}, PairingEngine, ProjectiveCurve, }; use ark_ff::{ @@ -24,8 +24,11 @@ mod g2 { ec_bench!(G2, G2Affine); } -f_bench!(1, Fq2, Fq2, fq2); -f_bench!(2, Fq12, Fq12, fq12); f_bench!(Fq, Fq, FqRepr, FqRepr, fq); f_bench!(Fr, Fr, FrRepr, FrRepr, fr); -pairing_bench!(Bn254, Fq12, prepared_v); +f_bench!(extension, Fq2, Fq2, fq2); +f_bench!(target, Fq12, Fq12, fq12); + +pairing_bench!(Bn254, Fq12); + +criterion::criterion_main!(g1::group_ops, g2::group_ops, fq, fr, fq2, fq12, pairing); diff --git a/curve-benches/src/curves/bw6_761.rs b/curve-benches/benches/bw6_761.rs similarity index 73% rename from curve-benches/src/curves/bw6_761.rs rename to curve-benches/benches/bw6_761.rs index e342500..15f5b06 100644 --- a/curve-benches/src/curves/bw6_761.rs +++ b/curve-benches/benches/bw6_761.rs @@ -1,13 +1,13 @@ use rand::SeedableRng; use rand_xorshift::XorShiftRng; use std::ops::{AddAssign, MulAssign, SubAssign}; +use ark_curve_benches::*; use ark_bw6_761::{ fq::Fq, fq3::Fq3, fr::Fr, Fq6, G1Affine, G1Projective as G1, G2Affine, G2Projective as G2, - Parameters, BW6_761, + BW6_761, }; use ark_ec::{ - bw6::{G1Prepared, G2Prepared}, PairingEngine, ProjectiveCurve, }; use ark_ff::{ @@ -24,8 +24,10 @@ mod g2 { ec_bench!(G2, G2Affine); } -f_bench!(1, Fq3, Fq3, fq3); -f_bench!(2, Fq6, Fq6, fq6); +f_bench!(extension, Fq3, Fq3, fq3); +f_bench!(target, Fq6, Fq6, fq6); f_bench!(Fq, Fq, FqRepr, FqRepr, fq); f_bench!(Fr, Fr, FrRepr, FrRepr, fr); -pairing_bench!(BW6_761, Fq6, prepared_v); +pairing_bench!(BW6_761, Fq6); + +criterion::criterion_main!(g1::group_ops, g2::group_ops, fq, fr, fq3, fq6, pairing); diff --git a/curve-benches/src/curves/cp6_782.rs b/curve-benches/benches/cp6_782.rs similarity index 74% rename from curve-benches/src/curves/cp6_782.rs rename to curve-benches/benches/cp6_782.rs index b343c57..dfa8891 100644 --- a/curve-benches/src/curves/cp6_782.rs +++ b/curve-benches/benches/cp6_782.rs @@ -1,6 +1,7 @@ use rand::SeedableRng; use rand_xorshift::XorShiftRng; use std::ops::{AddAssign, MulAssign, SubAssign}; +use ark_curve_benches::*; use ark_cp6_782::{ fq::Fq, fq3::Fq3, fr::Fr, Fq6, G1Affine, G1Projective as G1, G2Affine, G2Projective as G2, @@ -21,8 +22,10 @@ mod g2 { ec_bench!(G2, G2Affine); } -f_bench!(1, Fq3, Fq3, fq3); -f_bench!(2, Fq6, Fq6, fq6); +f_bench!(extension, Fq3, Fq3, fq3); +f_bench!(target, Fq6, Fq6, fq6); f_bench!(Fq, Fq, FqRepr, FqRepr, fq); f_bench!(Fr, Fr, FrRepr, FrRepr, fr); -pairing_bench!(CP6_782, Fq6, affine_v); +pairing_bench!(CP6_782, Fq6); + +criterion::criterion_main!(g1::group_ops, g2::group_ops, fq, fr, fq3, fq6, pairing); diff --git a/curve-benches/src/curves/ed_on_bls12_381.rs b/curve-benches/benches/ed_on_bls12_381.rs similarity index 86% rename from curve-benches/src/curves/ed_on_bls12_381.rs rename to curve-benches/benches/ed_on_bls12_381.rs index 6685b0c..d621bb0 100644 --- a/curve-benches/src/curves/ed_on_bls12_381.rs +++ b/curve-benches/benches/ed_on_bls12_381.rs @@ -1,6 +1,7 @@ use rand::SeedableRng; use rand_xorshift::XorShiftRng; use std::ops::{AddAssign, MulAssign, SubAssign}; +use ark_curve_benches::*; use ark_ec::ProjectiveCurve; use ark_ed_on_bls12_381::{fq::Fq, fr::Fr, EdwardsAffine as GAffine, EdwardsProjective as G}; @@ -16,3 +17,5 @@ mod g { f_bench!(Fq, Fq, FqRepr, FqRepr, fq); f_bench!(Fr, Fr, FrRepr, FrRepr, fr); + +criterion::criterion_main!(g::group_ops, fq, fr); diff --git a/curve-benches/src/curves/mnt4_298.rs b/curve-benches/benches/mnt4_298.rs similarity index 67% rename from curve-benches/src/curves/mnt4_298.rs rename to curve-benches/benches/mnt4_298.rs index cc0d027..c412e27 100644 --- a/curve-benches/src/curves/mnt4_298.rs +++ b/curve-benches/benches/mnt4_298.rs @@ -1,9 +1,9 @@ use rand::SeedableRng; use rand_xorshift::XorShiftRng; use std::ops::{AddAssign, MulAssign, SubAssign}; +use ark_curve_benches::*; use ark_ec::{ - mnt4::{G1Prepared, G2Prepared}, PairingEngine, ProjectiveCurve, }; use ark_ff::{ @@ -12,7 +12,7 @@ use ark_ff::{ }; use ark_mnt4_298::{ fq::Fq, fq2::Fq2, fr::Fr, Fq4, G1Affine, G1Projective as G1, G2Affine, G2Projective as G2, - Parameters, MNT4_298, + MNT4_298, }; mod g1 { @@ -24,7 +24,10 @@ mod g2 { ec_bench!(G2, G2Affine); } -f_bench!(1, Fq2, Fq2, fq2); -f_bench!(2, Fq4, Fq4, fq4); +f_bench!(extension, Fq2, Fq2, fq2); +f_bench!(target, Fq4, Fq4, fq4); f_bench!(Fq, Fq, FqRepr, FqRepr, fq); -pairing_bench!(MNT4_298, Fq4, prepared_v); +f_bench!(Fr, Fr, FqRepr, FqRepr, fr); +pairing_bench!(MNT4_298, Fq4); + +criterion::criterion_main!(g1::group_ops, g2::group_ops, fq, fr, fq2, fq4, pairing); diff --git a/curve-benches/src/curves/mnt4_753.rs b/curve-benches/benches/mnt4_753.rs similarity index 67% rename from curve-benches/src/curves/mnt4_753.rs rename to curve-benches/benches/mnt4_753.rs index f493018..77a7767 100644 --- a/curve-benches/src/curves/mnt4_753.rs +++ b/curve-benches/benches/mnt4_753.rs @@ -1,9 +1,9 @@ use rand::SeedableRng; use rand_xorshift::XorShiftRng; use std::ops::{AddAssign, MulAssign, SubAssign}; +use ark_curve_benches::*; use ark_ec::{ - mnt4::{G1Prepared, G2Prepared}, PairingEngine, ProjectiveCurve, }; use ark_ff::{ @@ -12,7 +12,7 @@ use ark_ff::{ }; use ark_mnt4_753::{ fq::Fq, fq2::Fq2, fr::Fr, Fq4, G1Affine, G1Projective as G1, G2Affine, G2Projective as G2, - Parameters, MNT4_753, + MNT4_753, }; mod g1 { @@ -24,7 +24,10 @@ mod g2 { ec_bench!(G2, G2Affine); } -f_bench!(1, Fq2, Fq2, fq2); -f_bench!(2, Fq4, Fq4, fq4); +f_bench!(extension, Fq2, Fq2, fq2); +f_bench!(target, Fq4, Fq4, fq4); f_bench!(Fq, Fq, FqRepr, FqRepr, fq); -pairing_bench!(MNT4_753, Fq4, prepared_v); +f_bench!(Fr, Fr, FqRepr, FqRepr, fr); +pairing_bench!(MNT4_753, Fq4); + +criterion::criterion_main!(g1::group_ops, g2::group_ops, fq, fr, fq2, fq4, pairing); diff --git a/curve-benches/src/curves/mnt6_298.rs b/curve-benches/benches/mnt6_298.rs similarity index 67% rename from curve-benches/src/curves/mnt6_298.rs rename to curve-benches/benches/mnt6_298.rs index 45c229f..278993f 100644 --- a/curve-benches/src/curves/mnt6_298.rs +++ b/curve-benches/benches/mnt6_298.rs @@ -1,9 +1,9 @@ use rand::SeedableRng; use rand_xorshift::XorShiftRng; use std::ops::{AddAssign, MulAssign, SubAssign}; +use ark_curve_benches::*; use ark_ec::{ - mnt6::{G1Prepared, G2Prepared}, PairingEngine, ProjectiveCurve, }; use ark_ff::{ @@ -12,7 +12,7 @@ use ark_ff::{ }; use ark_mnt6_298::{ fq::Fq, fq3::Fq3, fr::Fr, Fq6, G1Affine, G1Projective as G1, G2Affine, G2Projective as G2, - Parameters, MNT6_298, + MNT6_298, }; mod g1 { @@ -24,7 +24,10 @@ mod g2 { ec_bench!(G2, G2Affine); } -f_bench!(1, Fq3, Fq3, fq3); -f_bench!(2, Fq6, Fq6, fq6); +f_bench!(extension, Fq3, Fq3, fq3); +f_bench!(target, Fq6, Fq6, fq6); f_bench!(Fq, Fq, FqRepr, FqRepr, fq); -pairing_bench!(MNT6_298, Fq6, prepared_v); +f_bench!(Fr, Fr, FqRepr, FqRepr, fr); +pairing_bench!(MNT6_298, Fq6); + +criterion::criterion_main!(g1::group_ops, g2::group_ops, fq, fr, fq3, fq6, pairing); diff --git a/curve-benches/src/curves/mnt6_753.rs b/curve-benches/benches/mnt6_753.rs similarity index 67% rename from curve-benches/src/curves/mnt6_753.rs rename to curve-benches/benches/mnt6_753.rs index 87605e9..2e670ff 100644 --- a/curve-benches/src/curves/mnt6_753.rs +++ b/curve-benches/benches/mnt6_753.rs @@ -1,9 +1,9 @@ use rand::SeedableRng; use rand_xorshift::XorShiftRng; use std::ops::{AddAssign, MulAssign, SubAssign}; +use ark_curve_benches::*; use ark_ec::{ - mnt6::{G1Prepared, G2Prepared}, PairingEngine, ProjectiveCurve, }; use ark_ff::{ @@ -12,7 +12,7 @@ use ark_ff::{ }; use ark_mnt6_753::{ fq::Fq, fq3::Fq3, fr::Fr, Fq6, G1Affine, G1Projective as G1, G2Affine, G2Projective as G2, - Parameters, MNT6_753, + MNT6_753, }; mod g1 { @@ -24,7 +24,10 @@ mod g2 { ec_bench!(G2, G2Affine); } -f_bench!(1, Fq3, Fq3, fq3); -f_bench!(2, Fq6, Fq6, fq6); +f_bench!(extension, Fq3, Fq3, fq3); +f_bench!(target, Fq6, Fq6, fq6); f_bench!(Fq, Fq, FqRepr, FqRepr, fq); -pairing_bench!(MNT6_753, Fq6, prepared_v); +f_bench!(Fr, Fr, FqRepr, FqRepr, fr); +pairing_bench!(MNT6_753, Fq6); + +criterion::criterion_main!(g1::group_ops, g2::group_ops, fq, fr, fq3, fq6, pairing); diff --git a/curve-benches/src/curves/mod.rs b/curve-benches/src/curves/mod.rs deleted file mode 100644 index 48f3a4c..0000000 --- a/curve-benches/src/curves/mod.rs +++ /dev/null @@ -1,20 +0,0 @@ -#[cfg(feature = "ark-bls12-377")] -mod bls12_377; -#[cfg(feature = "ark-bls12-381")] -mod bls12_381; -#[cfg(feature = "ark-bn254")] -mod bn254; -#[cfg(feature = "ark-bw6-761")] -mod bw6_761; -#[cfg(feature = "ark-cp6-782")] -mod cp6_782; -#[cfg(feature = "ark-ed-on-bls12-381")] -mod ed_on_bls12_381; -#[cfg(feature = "ark-mnt4-298")] -mod mnt4_298; -#[cfg(feature = "ark-mnt4-753")] -mod mnt4_753; -#[cfg(feature = "ark-mnt6-298")] -mod mnt6_298; -#[cfg(feature = "ark-mnt6-753")] -mod mnt6_753; diff --git a/curve-benches/src/lib.rs b/curve-benches/src/lib.rs index 0a70245..2d763f7 100644 --- a/curve-benches/src/lib.rs +++ b/curve-benches/src/lib.rs @@ -1,12 +1,13 @@ -#![cfg_attr(nightly, feature(test))] #![allow(unused_macros, unused_imports)] - -#[cfg(nightly)] -extern crate test; - -#[cfg(all(nightly, test))] #[macro_use] pub mod macros; +pub use macros::*; -#[cfg(all(nightly, test))] -mod curves; +#[cfg(feature = "criterion")] +#[macro_use] +pub extern crate criterion; +pub use criterion::*; + +#[macro_use] +pub extern crate paste; +pub use paste::*; diff --git a/curve-benches/src/macros/ec.rs b/curve-benches/src/macros/ec.rs index eb6277c..38a3033 100644 --- a/curve-benches/src/macros/ec.rs +++ b/curve-benches/src/macros/ec.rs @@ -1,13 +1,26 @@ +#[macro_export] macro_rules! ec_bench { ($projective:ty, $affine:ty) => { - #[bench] - fn bench_rand(b: &mut ::test::Bencher) { + fn bench_curve(c: &mut $crate::criterion::Criterion) { + let mut group = c.benchmark_group(core::stringify!($projective)); + group.bench_function("Rand", rand); + group.bench_function("MulAssign", mul_assign); + group.bench_function("AddAssign", add_assign); + group.bench_function("AddAssignMixed", add_assign_mixed); + group.bench_function("Serialize w/ compression", ser); + group.bench_function("Deserialize w/ compression", deser); + group.bench_function("Serialize unchecked", ser_unchecked); + group.bench_function("Deserialize unchecked", deser_unchecked); + } + + + fn rand(b: &mut $crate::criterion::Bencher) { let mut rng = XorShiftRng::seed_from_u64(1231275789u64); b.iter(|| <$projective>::rand(&mut rng)); } - #[bench] - fn bench_mul_assign(b: &mut ::test::Bencher) { + + fn mul_assign(b: &mut $crate::criterion::Bencher) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::seed_from_u64(1231275789u64); @@ -25,8 +38,8 @@ macro_rules! ec_bench { }); } - #[bench] - fn bench_add_assign(b: &mut ::test::Bencher) { + + fn add_assign(b: &mut $crate::criterion::Bencher) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::seed_from_u64(1231275789u64); @@ -44,8 +57,8 @@ macro_rules! ec_bench { }); } - #[bench] - fn bench_add_assign_mixed(b: &mut ::test::Bencher) { + + fn add_assign_mixed(b: &mut $crate::criterion::Bencher) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::seed_from_u64(1231275789u64); @@ -68,8 +81,8 @@ macro_rules! ec_bench { }); } - #[bench] - fn bench_double(b: &mut ::test::Bencher) { + + fn double(b: &mut $crate::criterion::Bencher) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::seed_from_u64(1231275789u64); @@ -87,8 +100,8 @@ macro_rules! ec_bench { }); } - #[bench] - fn bench_deser(b: &mut ::test::Bencher) { + + fn deser(b: &mut $crate::criterion::Bencher) { use ark_ec::ProjectiveCurve; use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; const SAMPLES: usize = 1000; @@ -114,8 +127,8 @@ macro_rules! ec_bench { }); } - #[bench] - fn bench_ser(b: &mut ::test::Bencher) { + + fn ser(b: &mut $crate::criterion::Bencher) { use ark_ec::ProjectiveCurve; use ark_serialize::CanonicalSerialize; const SAMPLES: usize = 1000; @@ -137,8 +150,8 @@ macro_rules! ec_bench { }); } - #[bench] - fn bench_deser_unchecked(b: &mut ::test::Bencher) { + + fn deser_unchecked(b: &mut $crate::criterion::Bencher) { use ark_ec::ProjectiveCurve; use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; const SAMPLES: usize = 1000; @@ -164,8 +177,8 @@ macro_rules! ec_bench { }); } - #[bench] - fn bench_ser_unchecked(b: &mut ::test::Bencher) { + + fn ser_unchecked(b: &mut $crate::criterion::Bencher) { use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; const SAMPLES: usize = 1000; @@ -185,5 +198,7 @@ macro_rules! ec_bench { tmp.serialize_unchecked(&mut bytes) }); } + + $crate::criterion::criterion_group!(group_ops, bench_curve); }; } diff --git a/curve-benches/src/macros/field.rs b/curve-benches/src/macros/field.rs index ee3d176..b4a2128 100644 --- a/curve-benches/src/macros/field.rs +++ b/curve-benches/src/macros/field.rs @@ -1,405 +1,437 @@ +#[macro_export] macro_rules! f_bench { // Use this for base fields - ($f:ident, $f_type:ty, $f_repr:ident, $f_repr_type:ty, $field_ident:ident) => { - field_common!($f, $f_type, $field_ident); - sqrt!($f, $f_type, $field_ident); - field_base!($f, $f_type, $f_repr, $f_repr_type, $field_ident); + ($f:ident, $f_type:ty, $f_repr:ident, $f_repr_type:ty, $modname:ident) => { + $crate::paste!{ + pub mod [<_ $modname>] { + use super::*; + field_common!($f, $f_type); + sqrt!($f, $f_type); + prime_field!($f, $f_type, $f_repr, $f_repr_type); + } + $crate::criterion::criterion_group!($modname, [<_ $modname>]::bench_common_field_ops, [<_ $modname>]::bench_sqrt, [<_ $modname>]::bench_prime_field_ops); + } }; // use this for intermediate fields - (1, $f:ident, $f_type:ty, $field_ident:ident) => { - field_common!($f, $f_type, $field_ident); - sqrt!($f, $f_type, $field_ident); + (extension, $f:ident, $f_type:ty, $modname:ident) => { + $crate::paste!{ + mod [<_ $modname>] { + use super::*; + field_common!($f, $f_type); + sqrt!($f, $f_type); + } + $crate::criterion::criterion_group!($modname, [<_ $modname>]::bench_common_field_ops, [<_ $modname>]::bench_sqrt); + } }; // Use this for the full extension field Fqk - (2, $f:ident, $f_type:ty, $field_ident:ident) => { - field_common!($f, $f_type, $field_ident); + (target, $f:ident, $f_type:ty, $modname:ident) => { + crate::paste! { + mod [<_ $modname>] { + use super::*; + field_common!($f, $f_type); + } + $crate::criterion::criterion_group!($modname, [<_ $modname>]::bench_common_field_ops); + } }; } +#[macro_export] macro_rules! field_common { - ($f:ident, $f_type:ty, $field_ident:ident) => { - paste::item! { - #[bench] - fn [](b: &mut ::test::Bencher) { - const SAMPLES: usize = 1000; - - let mut rng = XorShiftRng::seed_from_u64(1231275789u64); - - let v: Vec<_> = (0..SAMPLES) - .map(|_| ($f::rand(&mut rng), $f::rand(&mut rng))) - .collect(); - - let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - n_fold!(tmp, v, add_assign, count); - count = (count + 1) % SAMPLES; - tmp - }); - } + ($f:ident, $f_type:ty) => { + pub fn bench_common_field_ops(c: &mut $crate::criterion::Criterion) { + let mut group = c.benchmark_group("Common field operations for ".to_string() + core::stringify!($f)); + group.bench_function("AddAssign", add_assign); + group.bench_function("SubAssign", sub_assign); + group.bench_function("Double", double); + group.bench_function("Negate", negate); + group.bench_function("MulAssign", mul_assign); + group.bench_function("Square", square); + group.bench_function("Inverse", inverse); + group.bench_function("Serialize w/ compression", ser); + group.bench_function("Deserialize w/ compression", deser); + group.bench_function("Serialize unchecked", ser_unchecked); + group.bench_function("Deserialize unchecked", deser_unchecked); + } - #[bench] - fn [](b: &mut ::test::Bencher) { - const SAMPLES: usize = 1000; + fn add_assign(b: &mut $crate::criterion::Bencher) { + const SAMPLES: usize = 1000; - let mut rng = XorShiftRng::seed_from_u64(1231275789u64); + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); - let v: Vec<_> = (0..SAMPLES) - .map(|_| ($f::rand(&mut rng), $f::rand(&mut rng))) - .collect(); + let v: Vec<_> = (0..SAMPLES) + .map(|_| ($f::rand(&mut rng), $f::rand(&mut rng))) + .collect(); - let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - n_fold!(tmp, v, sub_assign, count); - count = (count + 1) % SAMPLES; - tmp - }); - } + let mut count = 0; + b.iter(|| { + let mut tmp = v[count].0; + n_fold!(tmp, v, add_assign, count); + count = (count + 1) % SAMPLES; + tmp + }); + } - #[bench] - fn [](b: &mut ::test::Bencher) { - const SAMPLES: usize = 1000; - let mut rng = XorShiftRng::seed_from_u64(1231275789u64); + fn sub_assign(b: &mut $crate::criterion::Bencher) { + const SAMPLES: usize = 1000; - let v: Vec<_> = (0..SAMPLES) - .map(|_| ($f::rand(&mut rng), $f::rand(&mut rng))) - .collect(); + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); - let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - n_fold!(tmp, v, mul_assign, count); - count = (count + 1) % SAMPLES; - tmp - }); - } + let v: Vec<_> = (0..SAMPLES) + .map(|_| ($f::rand(&mut rng), $f::rand(&mut rng))) + .collect(); - #[bench] - fn [](b: &mut ::test::Bencher) { - const SAMPLES: usize = 1000; + let mut count = 0; + b.iter(|| { + let mut tmp = v[count].0; + n_fold!(tmp, v, sub_assign, count); + count = (count + 1) % SAMPLES; + tmp + }); + } - let mut rng = XorShiftRng::seed_from_u64(1231275789u64); + fn double(b: &mut $crate::criterion::Bencher) { + const SAMPLES: usize = 1000; - let v: Vec<$f_type> = (0..SAMPLES).map(|_| $f::rand(&mut rng)).collect(); + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); - let mut count = 0; - b.iter(|| { - let mut tmp = v[count]; - n_fold!(tmp, double_in_place); - count = (count + 1) % SAMPLES; - tmp - }); - } + let v: Vec<$f_type> = (0..SAMPLES).map(|_| $f::rand(&mut rng)).collect(); - #[bench] - fn [](b: &mut ::test::Bencher) { - const SAMPLES: usize = 1000; + let mut count = 0; + b.iter(|| { + let mut tmp = v[count]; + n_fold!(tmp, double_in_place); + count = (count + 1) % SAMPLES; + tmp + }); + } - let mut rng = XorShiftRng::seed_from_u64(1231275789u64); - let v: Vec<$f_type> = (0..SAMPLES).map(|_| $f::rand(&mut rng)).collect(); + fn negate(b: &mut $crate::criterion::Bencher) { + const SAMPLES: usize = 1000; - let mut count = 0; - b.iter(|| { - let mut tmp = v[count]; - n_fold!(tmp, square_in_place); - count = (count + 1) % SAMPLES; - tmp - }); - } + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); - #[bench] - fn [](b: &mut ::test::Bencher) { - const SAMPLES: usize = 1000; + let v: Vec<$f_type> = (0..SAMPLES).map(|_| $f::rand(&mut rng)).collect(); - let mut rng = XorShiftRng::seed_from_u64(1231275789u64); + let mut count = 0; + b.iter(|| { + let mut tmp = v[count]; + tmp = -tmp; + count = (count + 1) % SAMPLES; + tmp + }); + } - let v: Vec<$f_type> = (0..SAMPLES).map(|_| $f::rand(&mut rng)).collect(); + fn mul_assign(b: &mut $crate::criterion::Bencher) { + const SAMPLES: usize = 1000; - let mut count = 0; - b.iter(|| { - let tmp = v[count].inverse(); - count = (count + 1) % SAMPLES; - tmp - }); - } + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); - #[bench] - fn [](b: &mut ::test::Bencher) { - use ark_serialize::{CanonicalSerialize, CanonicalDeserialize}; - const SAMPLES: usize = 1000; - - let mut rng = XorShiftRng::seed_from_u64(1231275789u64); - - let mut num_bytes = 0; - let v: Vec<_> = (0..SAMPLES).flat_map(|_| { - let mut bytes = Vec::with_capacity(1000); - let tmp = $f::rand(&mut rng); - tmp.serialize(&mut bytes).unwrap(); - num_bytes = bytes.len(); - bytes - }).collect(); - - let mut count = 0; - b.iter(|| { - count = (count + 1) % SAMPLES; - let index = count * num_bytes; - $f_type::deserialize(&v[index..(index + num_bytes)]).unwrap() - }); - } + let v: Vec<_> = (0..SAMPLES) + .map(|_| ($f::rand(&mut rng), $f::rand(&mut rng))) + .collect(); + + let mut count = 0; + b.iter(|| { + let mut tmp = v[count].0; + n_fold!(tmp, v, mul_assign, count); + count = (count + 1) % SAMPLES; + tmp + }); + } + + fn square(b: &mut $crate::criterion::Bencher) { + const SAMPLES: usize = 1000; + + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); + + let v: Vec<$f_type> = (0..SAMPLES).map(|_| $f::rand(&mut rng)).collect(); + + let mut count = 0; + b.iter(|| { + let mut tmp = v[count]; + n_fold!(tmp, square_in_place); + count = (count + 1) % SAMPLES; + tmp + }); + } - #[bench] - fn [](b: &mut ::test::Bencher) { - use ark_serialize::CanonicalSerialize; - const SAMPLES: usize = 1000; - let mut rng = XorShiftRng::seed_from_u64(1231275789u64); + fn inverse(b: &mut $crate::criterion::Bencher) { + const SAMPLES: usize = 1000; - let v: Vec<$f_type> = (0..SAMPLES).map(|_| $f::rand(&mut rng)).collect(); + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); + + let v: Vec<$f_type> = (0..SAMPLES).map(|_| $f::rand(&mut rng)).collect(); + + let mut count = 0; + b.iter(|| { + let tmp = v[count].inverse(); + count = (count + 1) % SAMPLES; + tmp + }); + } + + + fn deser(b: &mut $crate::criterion::Bencher) { + use ark_serialize::{CanonicalSerialize, CanonicalDeserialize}; + const SAMPLES: usize = 1000; + + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); + + let mut num_bytes = 0; + let v: Vec<_> = (0..SAMPLES).flat_map(|_| { let mut bytes = Vec::with_capacity(1000); + let tmp = $f::rand(&mut rng); + tmp.serialize(&mut bytes).unwrap(); + num_bytes = bytes.len(); + bytes + }).collect(); + + let mut count = 0; + b.iter(|| { + count = (count + 1) % SAMPLES; + let index = count * num_bytes; + <$f_type>::deserialize(&v[index..(index + num_bytes)]).unwrap() + }); + } - let mut count = 0; - b.iter(|| { - let tmp = v[count]; - count = (count + 1) % SAMPLES; - bytes.clear(); - tmp.serialize(&mut bytes) - }); - } + fn ser(b: &mut $crate::criterion::Bencher) { + use ark_serialize::CanonicalSerialize; + const SAMPLES: usize = 1000; - #[bench] - fn [](b: &mut ::test::Bencher) { - use ark_serialize::{CanonicalSerialize, CanonicalDeserialize}; - const SAMPLES: usize = 1000; - - let mut rng = XorShiftRng::seed_from_u64(1231275789u64); - - let mut num_bytes = 0; - let v: Vec<_> = (0..SAMPLES).flat_map(|_| { - let mut bytes = Vec::with_capacity(1000); - let tmp = $f::rand(&mut rng); - tmp.serialize_unchecked(&mut bytes).unwrap(); - num_bytes = bytes.len(); - bytes - }).collect(); - - let mut count = 0; - b.iter(|| { - count = (count + 1) % SAMPLES; - let index = count * num_bytes; - $f_type::deserialize_unchecked(&v[index..(index + num_bytes)]).unwrap() - }); - } + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); - #[bench] - fn [](b: &mut ::test::Bencher) { - use ark_serialize::CanonicalSerialize; - const SAMPLES: usize = 1000; + let v: Vec<$f_type> = (0..SAMPLES).map(|_| $f::rand(&mut rng)).collect(); + let mut bytes = Vec::with_capacity(1000); - let mut rng = XorShiftRng::seed_from_u64(1231275789u64); + let mut count = 0; + b.iter(|| { + let tmp = v[count]; + count = (count + 1) % SAMPLES; + bytes.clear(); + tmp.serialize(&mut bytes) - let v: Vec<$f_type> = (0..SAMPLES).map(|_| $f::rand(&mut rng)).collect(); + }); + } + + + fn deser_unchecked(b: &mut $crate::criterion::Bencher) { + use ark_serialize::{CanonicalSerialize, CanonicalDeserialize}; + const SAMPLES: usize = 1000; + + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); + + let mut num_bytes = 0; + let v: Vec<_> = (0..SAMPLES).flat_map(|_| { let mut bytes = Vec::with_capacity(1000); + let tmp = $f::rand(&mut rng); + tmp.serialize_unchecked(&mut bytes).unwrap(); + num_bytes = bytes.len(); + bytes + }).collect(); + + let mut count = 0; + b.iter(|| { + count = (count + 1) % SAMPLES; + let index = count * num_bytes; + <$f_type>::deserialize_unchecked(&v[index..(index + num_bytes)]).unwrap() + }); + } - let mut count = 0; - b.iter(|| { - let tmp = v[count]; - count = (count + 1) % SAMPLES; - bytes.clear(); - tmp.serialize_unchecked(&mut bytes) - }); - } + fn ser_unchecked(b: &mut $crate::criterion::Bencher) { + use ark_serialize::CanonicalSerialize; + const SAMPLES: usize = 1000; + + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); + + let v: Vec<$f_type> = (0..SAMPLES).map(|_| $f::rand(&mut rng)).collect(); + let mut bytes = Vec::with_capacity(1000); + + let mut count = 0; + b.iter(|| { + let tmp = v[count]; + count = (count + 1) % SAMPLES; + bytes.clear(); + tmp.serialize_unchecked(&mut bytes) + + }); } - }; + } } +#[macro_export] macro_rules! sqrt { - ($f:ident, $f_type:ty, $field_ident:ident) => { - paste::item! { - #[bench] - fn [](b: &mut ::test::Bencher) { - const SAMPLES: usize = 1000; - - let mut rng = XorShiftRng::seed_from_u64(1231275789u64); - - let v: Vec<$f_type> = (0..SAMPLES) - .map(|_| { - let mut tmp = $f::rand(&mut rng); - tmp.square_in_place(); - tmp - }) - .collect(); - - let mut count = 0; - b.iter(|| { - count = (count + 1) % SAMPLES; - v[count].sqrt() - }); - } - } - }; -} + ($f:ident, $f_type:ty) => { + pub fn bench_sqrt(b: &mut $crate::criterion::Criterion) { + const SAMPLES: usize = 1000; -macro_rules! field_base { - ($f:ident, $f_type:ty, $f_repr:ident, $f_repr_type:ty, $field_ident:ident) => { - paste::item! { - #[bench] - fn [](b: &mut ::test::Bencher) { - const SAMPLES: usize = 1000; - - let mut rng = XorShiftRng::seed_from_u64(1231275789u64); - - let v: Vec<_> = (0..SAMPLES) - .map(|_| { - let mut tmp1 = $f_repr::rand(&mut rng); - let mut tmp2 = $f_repr::rand(&mut rng); - // Shave a few bits off to avoid overflow. - for _ in 0..3 { - tmp1.div2(); - tmp2.div2(); - } - (tmp1, tmp2) - }) - .collect(); - - let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - n_fold!(tmp, v, add_nocarry, count); - count = (count + 1) % SAMPLES; - tmp - }); - } + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); - #[bench] - fn [](b: &mut ::test::Bencher) { - const SAMPLES: usize = 1000; - - let mut rng = XorShiftRng::seed_from_u64(1231275789u64); - - let v: Vec<_> = (0..SAMPLES) - .map(|_| { - let tmp1 = $f_repr::rand(&mut rng); - let mut tmp2 = tmp1; - // Ensure tmp2 is smaller than tmp1. - for _ in 0..10 { - tmp2.div2(); - } - (tmp1, tmp2) - }) - .collect(); - - let mut count = 0; - b.iter(|| { - let mut tmp = v[count].0; - n_fold!(tmp, v, sub_noborrow, count); - count = (count + 1) % SAMPLES; + let v: Vec<$f_type> = (0..SAMPLES) + .map(|_| { + let mut tmp = $f::rand(&mut rng); + tmp.square_in_place(); tmp - }); - } - - #[bench] - fn [](b: &mut ::test::Bencher) { - const SAMPLES: usize = 1000; + }) + .collect(); + + let mut count = 0; + b.bench_function( + core::concat!("Square-roots for ", core::stringify!($f)), + |_| { + count = (count + 1) % SAMPLES; + let _ = v[count].sqrt(); + }); + } + } +} - let mut rng = XorShiftRng::seed_from_u64(1231275789u64); +#[macro_export] +macro_rules! prime_field { + ($f:ident, $f_type:ty, $f_repr:ident, $f_repr_type:ty) => { + pub fn bench_prime_field_ops(c: &mut $crate::criterion::Criterion) { + let mut group = c.benchmark_group("Prime field operations for ".to_string() + core::stringify!($f)); + group.bench_function("AddNoCarry for BigInteger", repr_add_nocarry); + group.bench_function("SubNoBorrow for BigInteger", repr_sub_noborrow); + group.bench_function("NumBits for BigInteger", repr_num_bits); + group.bench_function("MulBy2 for BigInteger", repr_mul2); + group.bench_function("DivBy2 for BigInteger", repr_div2); + group.bench_function("Into BigInteger", into_repr); + group.bench_function("From BigInteger", from_repr); + } - let v: Vec<$f_repr_type> = (0..SAMPLES).map(|_| $f_repr::rand(&mut rng)).collect(); + fn repr_add_nocarry(b: &mut $crate::criterion::Bencher) { + const SAMPLES: usize = 1000; + + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); + + let v: Vec<_> = (0..SAMPLES) + .map(|_| { + let mut tmp1 = $f_repr::rand(&mut rng); + let mut tmp2 = $f_repr::rand(&mut rng); + // Shave a few bits off to avoid overflow. + for _ in 0..3 { + tmp1.div2(); + tmp2.div2(); + } + (tmp1, tmp2) + }) + .collect(); + + let mut count = 0; + b.iter(|| { + let mut tmp = v[count].0; + n_fold!(tmp, v, add_nocarry, count); + count = (count + 1) % SAMPLES; + tmp + }); + } - let mut count = 0; - b.iter(|| { - let tmp = v[count].num_bits(); - count = (count + 1) % SAMPLES; - tmp - }); - } + fn repr_sub_noborrow(b: &mut $crate::criterion::Bencher) { + const SAMPLES: usize = 1000; + + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); + + let v: Vec<_> = (0..SAMPLES) + .map(|_| { + let tmp1 = $f_repr::rand(&mut rng); + let mut tmp2 = tmp1; + // Ensure tmp2 is smaller than tmp1. + for _ in 0..10 { + tmp2.div2(); + } + (tmp1, tmp2) + }) + .collect(); + + let mut count = 0; + b.iter(|| { + let mut tmp = v[count].0; + n_fold!(tmp, v, sub_noborrow, count); + count = (count + 1) % SAMPLES; + tmp; + }); + } - #[bench] - fn [](b: &mut ::test::Bencher) { - const SAMPLES: usize = 1000; + fn repr_num_bits(b: &mut $crate::criterion::Bencher) { + const SAMPLES: usize = 1000; - let mut rng = XorShiftRng::seed_from_u64(1231275789u64); + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); - let v: Vec<$f_repr_type> = (0..SAMPLES).map(|_| $f_repr::rand(&mut rng)).collect(); + let v: Vec<$f_repr_type> = (0..SAMPLES).map(|_| $f_repr::rand(&mut rng)).collect(); - let mut count = 0; - b.iter(|| { - let mut tmp = v[count]; - n_fold!(tmp, mul2); - count = (count + 1) % SAMPLES; - tmp - }); - } + let mut count = 0; + b.iter(|| { + let tmp = v[count].num_bits(); + count = (count + 1) % SAMPLES; + tmp; + }); + } - #[bench] - fn [](b: &mut ::test::Bencher) { - const SAMPLES: usize = 1000; + fn repr_mul2(b: &mut $crate::criterion::Bencher) { + const SAMPLES: usize = 1000; - let mut rng = XorShiftRng::seed_from_u64(1231275789u64); + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); - let v: Vec<$f_repr_type> = (0..SAMPLES).map(|_| $f_repr::rand(&mut rng)).collect(); + let v: Vec<$f_repr_type> = (0..SAMPLES).map(|_| $f_repr::rand(&mut rng)).collect(); - let mut count = 0; - b.iter(|| { - let mut tmp = v[count]; - n_fold!(tmp, div2); - count = (count + 1) % SAMPLES; - tmp - }); - } + let mut count = 0; + b.iter(|| { + let mut tmp = v[count]; + n_fold!(tmp, mul2); + count = (count + 1) % SAMPLES; + tmp; + }); + } - #[bench] - fn [](b: &mut ::test::Bencher) { - const SAMPLES: usize = 1000; + fn repr_div2(b: &mut $crate::criterion::Bencher) { + const SAMPLES: usize = 1000; - let mut rng = XorShiftRng::seed_from_u64(1231275789u64); + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); - let v: Vec<$f_type> = (0..SAMPLES).map(|_| $f::rand(&mut rng)).collect(); + let v: Vec<$f_repr_type> = (0..SAMPLES).map(|_| $f_repr::rand(&mut rng)).collect(); - let mut count = 0; - b.iter(|| { - let mut tmp = v[count]; - tmp = -tmp; - count = (count + 1) % SAMPLES; - tmp - }); - } + let mut count = 0; + b.iter(|| { + let mut tmp = v[count]; + n_fold!(tmp, div2); + count = (count + 1) % SAMPLES; + tmp; + }); + } - #[bench] - fn [](b: &mut ::test::Bencher) { - const SAMPLES: usize = 1000; + fn into_repr(b: &mut $crate::criterion::Bencher) { + const SAMPLES: usize = 1000; - let mut rng = XorShiftRng::seed_from_u64(1231275789u64); + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); - let v: Vec<$f_type> = (0..SAMPLES).map(|_| $f::rand(&mut rng)).collect(); + let v: Vec<$f_type> = (0..SAMPLES).map(|_| $f::rand(&mut rng)).collect(); - let mut count = 0; - b.iter(|| { - count = (count + 1) % SAMPLES; - v[count].into_repr() - }); - } + let mut count = 0; + b.iter(|| { + count = (count + 1) % SAMPLES; + v[count].into_repr(); + }); + } - #[bench] - fn [](b: &mut ::test::Bencher) { - const SAMPLES: usize = 1000; + fn from_repr(b: &mut $crate::criterion::Bencher) { + const SAMPLES: usize = 1000; - let mut rng = XorShiftRng::seed_from_u64(1231275789u64); + let mut rng = XorShiftRng::seed_from_u64(1231275789u64); - let v: Vec<$f_repr_type> = (0..SAMPLES) - .map(|_| $f::rand(&mut rng).into_repr()) - .collect(); + let v: Vec<$f_repr_type> = (0..SAMPLES) + .map(|_| $f::rand(&mut rng).into_repr()) + .collect(); - let mut count = 0; - b.iter(|| { - count = (count + 1) % SAMPLES; - $f::from(v[count]) - }); - } + let mut count = 0; + b.iter(|| { + count = (count + 1) % SAMPLES; + $f::from(v[count]); + }); } - }; + } } diff --git a/curve-benches/src/macros/mod.rs b/curve-benches/src/macros/mod.rs index 5c936a2..0785770 100644 --- a/curve-benches/src/macros/mod.rs +++ b/curve-benches/src/macros/mod.rs @@ -1,3 +1,6 @@ +#[macro_use] +mod utils; + #[macro_use] mod ec; @@ -6,6 +9,3 @@ mod field; #[macro_use] mod pairing; - -#[macro_use] -mod utils; diff --git a/curve-benches/src/macros/pairing.rs b/curve-benches/src/macros/pairing.rs index 117391a..bfad660 100644 --- a/curve-benches/src/macros/pairing.rs +++ b/curve-benches/src/macros/pairing.rs @@ -1,28 +1,43 @@ +#[macro_export] macro_rules! pairing_bench { - ($curve:ident, $pairing_field:ident, $pairing_type:ident) => { - #[bench] - fn bench_pairing_miller_loop(b: &mut ::test::Bencher) { + ($curve:ident, $pairing_field:ident) => { + + fn bench_pairing(c: &mut $crate::criterion::Criterion) { + let mut group = c.benchmark_group("Pairing for ".to_string() + core::stringify!($curve)); + group.bench_function("Miller Loop", miller_loop); + group.bench_function("Final Exponentiation", final_exponentiation); + group.bench_function("Full Pairing", full_pairing); + } + + fn miller_loop(b: &mut $crate::criterion::Bencher) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::seed_from_u64(1231275789u64); - $pairing_type!(v, rng); - + let g1s = (0..SAMPLES).map(|_| G1::rand(&mut rng)).collect::>(); + let g2s = (0..SAMPLES).map(|_| G2::rand(&mut rng)).collect::>(); + let g1s = G1::batch_normalization_into_affine(&g1s); + let g2s = G2::batch_normalization_into_affine(&g2s); + let prepared = g1s + .into_iter() + .zip(g2s) + .map(|(g1, g2)| (g1.into(), g2.into())) + .collect::::G1Prepared, <$curve as PairingEngine>::G2Prepared)>>(); let mut count = 0; b.iter(|| { - let tmp = $curve::miller_loop(&[(v[count].0.clone(), v[count].1.clone())]); + let tmp = $curve::miller_loop(&[(prepared[count].0.clone(), prepared[count].1.clone())]); count = (count + 1) % SAMPLES; tmp }); } - #[bench] - fn bench_pairing_final_exponentiation(b: &mut ::test::Bencher) { + + fn final_exponentiation(b: &mut $crate::criterion::Bencher) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::seed_from_u64(1231275789u64); - let v: Vec<$pairing_field> = (0..SAMPLES) + let v: Vec<_> = (0..SAMPLES) .map(|_| { ( G1Affine::from(G1::rand(&mut rng)).into(), @@ -40,8 +55,8 @@ macro_rules! pairing_bench { }); } - #[bench] - fn bench_pairing_full(b: &mut ::test::Bencher) { + + fn full_pairing(b: &mut $crate::criterion::Bencher) { const SAMPLES: usize = 1000; let mut rng = XorShiftRng::seed_from_u64(1231275789u64); @@ -57,5 +72,7 @@ macro_rules! pairing_bench { tmp }); } - }; + + $crate::criterion::criterion_group!(pairing, bench_pairing); + } } diff --git a/curve-benches/src/macros/utils.rs b/curve-benches/src/macros/utils.rs index 8d9881a..0caf7d4 100644 --- a/curve-benches/src/macros/utils.rs +++ b/curve-benches/src/macros/utils.rs @@ -1,49 +1,10 @@ +#[macro_export] macro_rules! n_fold { ($tmp:ident, $v:ident, $func:ident, $count:ident) => { - const ITERS: usize = 1000; - - #[cfg(not(feature = "n_fold"))] $tmp.$func(&$v[$count].1); - #[cfg(feature = "n_fold")] - for _ in 0..ITERS { - $tmp.$func(&$v[$count].1); - } }; ($tmp:ident, $func:ident) => { - const ITERS: usize = 1000; - - #[cfg(not(feature = "n_fold"))] $tmp.$func(); - #[cfg(feature = "n_fold")] - for _ in 0..ITERS { - $tmp.$func(); - } - }; -} - -macro_rules! prepared_v { - ($v:ident, $rng:ident) => { - let $v: Vec<(G1Prepared, G2Prepared)> = (0..SAMPLES) - .map(|_| { - ( - G1Affine::from(G1::rand(&mut $rng)).into(), - G2Affine::from(G2::rand(&mut $rng)).into(), - ) - }) - .collect(); - }; -} - -macro_rules! affine_v { - ($v:ident, $rng:ident) => { - let $v: Vec<(G1Affine, G2Affine)> = (0..SAMPLES) - .map(|_| { - ( - G1Affine::from(G1::rand(&mut $rng)).into(), - G2Affine::from(G2::rand(&mut $rng)).into(), - ) - }) - .collect(); }; }