[benchmarks-only] Updates the benchmarks

- removes usages fo the (deprecated) `bench_function_over_inputs`
- introduces a few benchmark groups.
This commit is contained in:
François Garillot 2021-03-26 09:18:18 -07:00
parent 1491f0db36
commit d130b5f17e
No known key found for this signature in database
GPG key ID: AC66C67853005854

View file

@ -7,8 +7,10 @@ use rand::thread_rng;
#[macro_use] #[macro_use]
extern crate criterion; extern crate criterion;
use criterion::measurement::Measurement;
use criterion::BatchSize; use criterion::BatchSize;
use criterion::Criterion; use criterion::Criterion;
use criterion::{BenchmarkGroup, BenchmarkId};
extern crate curve25519_dalek; extern crate curve25519_dalek;
@ -100,115 +102,136 @@ mod multiscalar_benches {
(construct_scalars(n), construct_points(n)) (construct_scalars(n), construct_points(n))
} }
fn consttime_multiscalar_mul(c: &mut Criterion) { fn consttime_multiscalar_mul<M: Measurement>(c: &mut BenchmarkGroup<M>) {
c.bench_function_over_inputs( for multiscalar_size in &MULTISCALAR_SIZES {
"Constant-time variable-base multiscalar multiplication", c.bench_with_input(
|b, &&size| { BenchmarkId::new(
let points = construct_points(size); "Constant-time variable-base multiscalar multiplication",
// This is supposed to be constant-time, but we might as well *multiscalar_size,
// rerandomize the scalars for every call just in case. ),
b.iter_batched( &multiscalar_size,
|| construct_scalars(size), |b, &&size| {
|scalars| EdwardsPoint::multiscalar_mul(&scalars, &points), let points = construct_points(size);
BatchSize::SmallInput, // This is supposed to be constant-time, but we might as well
); // rerandomize the scalars for every call just in case.
}, b.iter_batched(
&MULTISCALAR_SIZES, || construct_scalars(size),
); |scalars| EdwardsPoint::multiscalar_mul(&scalars, &points),
BatchSize::SmallInput,
);
},
);
}
} }
fn vartime_multiscalar_mul(c: &mut Criterion) { fn vartime_multiscalar_mul<M: Measurement>(c: &mut BenchmarkGroup<M>) {
c.bench_function_over_inputs( for multiscalar_size in &MULTISCALAR_SIZES {
"Variable-time variable-base multiscalar multiplication", c.bench_with_input(
|b, &&size| { BenchmarkId::new(
let points = construct_points(size); "Variable-time variable-base multiscalar multiplication",
// Rerandomize the scalars for every call to prevent *multiscalar_size,
// false timings from better caching (e.g., the CPU ),
// cache lifts exactly the right table entries for the &multiscalar_size,
// benchmark into the highest cache levels). |b, &&size| {
b.iter_batched( let points = construct_points(size);
|| construct_scalars(size), // Rerandomize the scalars for every call to prevent
|scalars| EdwardsPoint::vartime_multiscalar_mul(&scalars, &points), // false timings from better caching (e.g., the CPU
BatchSize::SmallInput, // cache lifts exactly the right table entries for the
); // benchmark into the highest cache levels).
}, b.iter_batched(
&MULTISCALAR_SIZES, || construct_scalars(size),
); |scalars| EdwardsPoint::vartime_multiscalar_mul(&scalars, &points),
BatchSize::SmallInput,
);
},
);
}
} }
fn vartime_precomputed_pure_static(c: &mut Criterion) { fn vartime_precomputed_pure_static<M: Measurement>(c: &mut BenchmarkGroup<M>) {
c.bench_function_over_inputs( for multiscalar_size in &MULTISCALAR_SIZES {
"Variable-time fixed-base multiscalar multiplication", c.bench_with_input(
move |b, &&total_size| { BenchmarkId::new(
let static_size = total_size; "Variable-time fixed-base multiscalar multiplication",
&multiscalar_size,
),
&multiscalar_size,
move |b, &&total_size| {
let static_size = total_size;
let static_points = construct_points(static_size); let static_points = construct_points(static_size);
let precomp = VartimeEdwardsPrecomputation::new(&static_points); let precomp = VartimeEdwardsPrecomputation::new(&static_points);
// Rerandomize the scalars for every call to prevent // Rerandomize the scalars for every call to prevent
// false timings from better caching (e.g., the CPU // false timings from better caching (e.g., the CPU
// cache lifts exactly the right table entries for the // cache lifts exactly the right table entries for the
// benchmark into the highest cache levels). // benchmark into the highest cache levels).
b.iter_batched( b.iter_batched(
|| construct_scalars(static_size), || construct_scalars(static_size),
|scalars| precomp.vartime_multiscalar_mul(&scalars), |scalars| precomp.vartime_multiscalar_mul(&scalars),
BatchSize::SmallInput, BatchSize::SmallInput,
); );
}, },
&MULTISCALAR_SIZES, );
); }
} }
fn vartime_precomputed_helper(c: &mut Criterion, dynamic_fraction: f64) { fn vartime_precomputed_helper<M: Measurement>(
let label = format!( c: &mut BenchmarkGroup<M>,
"Variable-time mixed-base multiscalar multiplication ({:.0}pct dyn)", dynamic_fraction: f64,
100.0 * dynamic_fraction, ) {
); for multiscalar_size in &MULTISCALAR_SIZES {
c.bench_function_over_inputs( c.bench_with_input(
&label, BenchmarkId::new(
move |b, &&total_size| { "Variable-time mixed-base multiscalar multiplication ({:.0}pct dyn)",
let dynamic_size = ((total_size as f64) * dynamic_fraction) as usize; format!("({:.0}pct dyn)", 100.0 * dynamic_fraction),
let static_size = total_size - dynamic_size; ),
&multiscalar_size,
move |b, &&total_size| {
let dynamic_size = ((total_size as f64) * dynamic_fraction) as usize;
let static_size = total_size - dynamic_size;
let static_points = construct_points(static_size); let static_points = construct_points(static_size);
let dynamic_points = construct_points(dynamic_size); let dynamic_points = construct_points(dynamic_size);
let precomp = VartimeEdwardsPrecomputation::new(&static_points); let precomp = VartimeEdwardsPrecomputation::new(&static_points);
// Rerandomize the scalars for every call to prevent // Rerandomize the scalars for every call to prevent
// false timings from better caching (e.g., the CPU // false timings from better caching (e.g., the CPU
// cache lifts exactly the right table entries for the // cache lifts exactly the right table entries for the
// benchmark into the highest cache levels). Timings // benchmark into the highest cache levels). Timings
// should be independent of points so we don't // should be independent of points so we don't
// randomize them. // randomize them.
b.iter_batched( b.iter_batched(
|| { || {
( (
construct_scalars(static_size), construct_scalars(static_size),
construct_scalars(dynamic_size), construct_scalars(dynamic_size),
) )
}, },
|(static_scalars, dynamic_scalars)| { |(static_scalars, dynamic_scalars)| {
precomp.vartime_mixed_multiscalar_mul( precomp.vartime_mixed_multiscalar_mul(
&static_scalars, &static_scalars,
&dynamic_scalars, &dynamic_scalars,
&dynamic_points, &dynamic_points,
) )
}, },
BatchSize::SmallInput, BatchSize::SmallInput,
); );
}, },
&MULTISCALAR_SIZES, );
); }
} }
fn vartime_precomputed_00_pct_dynamic(c: &mut Criterion) { fn multiscalar_multiplications(c: &mut Criterion) {
vartime_precomputed_helper(c, 0.0); let mut group: BenchmarkGroup<_> = c.benchmark_group("Multiscalar muls");
}
fn vartime_precomputed_20_pct_dynamic(c: &mut Criterion) { consttime_multiscalar_mul(&mut group);
vartime_precomputed_helper(c, 0.2); vartime_multiscalar_mul(&mut group);
} vartime_precomputed_pure_static(&mut group);
fn vartime_precomputed_50_pct_dynamic(c: &mut Criterion) { let dynamic_fracs = [0.0, 0.2, 0.5];
vartime_precomputed_helper(c, 0.5); for frac in dynamic_fracs.iter() {
vartime_precomputed_helper(&mut group, *frac);
}
group.finish();
} }
criterion_group! { criterion_group! {
@ -216,12 +239,7 @@ mod multiscalar_benches {
// Lower the sample size to run the benchmarks faster // Lower the sample size to run the benchmarks faster
config = Criterion::default().sample_size(15); config = Criterion::default().sample_size(15);
targets = targets =
consttime_multiscalar_mul, multiscalar_multiplications,
vartime_multiscalar_mul,
vartime_precomputed_pure_static,
vartime_precomputed_00_pct_dynamic,
vartime_precomputed_20_pct_dynamic,
vartime_precomputed_50_pct_dynamic,
} }
} }
@ -243,18 +261,26 @@ mod ristretto_benches {
}); });
} }
fn double_and_compress_batch(c: &mut Criterion) { fn double_and_compress_batch<M: Measurement>(c: &mut BenchmarkGroup<M>) {
c.bench_function_over_inputs( for batch_size in &BATCH_SIZES {
"Batch Ristretto double-and-encode", c.bench_with_input(
|b, &&size| { BenchmarkId::new("Batch Ristretto double-and-encode", *batch_size),
let mut rng = OsRng; &batch_size,
let points: Vec<RistrettoPoint> = (0..size) |b, &&size| {
.map(|_| RistrettoPoint::random(&mut rng)) let mut rng = OsRng;
.collect(); let points: Vec<RistrettoPoint> = (0..size)
b.iter(|| RistrettoPoint::double_and_compress_batch(&points)); .map(|_| RistrettoPoint::random(&mut rng))
}, .collect();
&BATCH_SIZES, b.iter(|| RistrettoPoint::double_and_compress_batch(&points));
); },
);
}
}
fn double_and_compress_group(c: &mut Criterion) {
let mut group: BenchmarkGroup<_> = c.benchmark_group("double & compress batched");
double_and_compress_batch(&mut group);
group.finish();
} }
criterion_group! { criterion_group! {
@ -263,7 +289,7 @@ mod ristretto_benches {
targets = targets =
compress, compress,
decompress, decompress,
double_and_compress_batch, double_and_compress_group,
} }
} }
@ -295,19 +321,28 @@ mod scalar_benches {
}); });
} }
fn batch_scalar_inversion(c: &mut Criterion) { fn batch_scalar_inversion<M: Measurement>(c: &mut BenchmarkGroup<M>) {
c.bench_function_over_inputs( for batch_size in &BATCH_SIZES {
"Batch scalar inversion", c.bench_with_input(
|b, &&size| { BenchmarkId::new("Batch scalar inversion", *batch_size),
let mut rng = OsRng; &batch_size,
let scalars: Vec<Scalar> = (0..size).map(|_| Scalar::random(&mut rng)).collect(); |b, &&size| {
b.iter(|| { let mut rng = OsRng;
let mut s = scalars.clone(); let scalars: Vec<Scalar> =
Scalar::batch_invert(&mut s); (0..size).map(|_| Scalar::random(&mut rng)).collect();
}); b.iter(|| {
}, let mut s = scalars.clone();
&BATCH_SIZES, Scalar::batch_invert(&mut s);
); });
},
);
}
}
fn batch_scalar_inversion_group(c: &mut Criterion) {
let mut group: BenchmarkGroup<_> = c.benchmark_group("batch scalar inversion");
batch_scalar_inversion(&mut group);
group.finish();
} }
criterion_group! { criterion_group! {
@ -315,7 +350,7 @@ mod scalar_benches {
config = Criterion::default(); config = Criterion::default();
targets = targets =
scalar_inversion, scalar_inversion,
batch_scalar_inversion, batch_scalar_inversion_group,
} }
} }