Merge pull request #349 from huitseeker/modernize_benchmarks

[benchmarks-only] Updates the benchmarks
This commit is contained in:
isis agora lovecruft 2021-04-13 22:55:59 +00:00 committed by GitHub
commit 35d4eab431
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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,9 +102,14 @@ 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 {
c.bench_with_input(
BenchmarkId::new(
"Constant-time variable-base multiscalar multiplication", "Constant-time variable-base multiscalar multiplication",
*multiscalar_size,
),
&multiscalar_size,
|b, &&size| { |b, &&size| {
let points = construct_points(size); let points = construct_points(size);
// This is supposed to be constant-time, but we might as well // This is supposed to be constant-time, but we might as well
@ -113,13 +120,18 @@ mod multiscalar_benches {
BatchSize::SmallInput, BatchSize::SmallInput,
); );
}, },
&MULTISCALAR_SIZES,
); );
} }
}
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 {
c.bench_with_input(
BenchmarkId::new(
"Variable-time variable-base multiscalar multiplication", "Variable-time variable-base multiscalar multiplication",
*multiscalar_size,
),
&multiscalar_size,
|b, &&size| { |b, &&size| {
let points = construct_points(size); let points = construct_points(size);
// Rerandomize the scalars for every call to prevent // Rerandomize the scalars for every call to prevent
@ -132,13 +144,18 @@ mod multiscalar_benches {
BatchSize::SmallInput, BatchSize::SmallInput,
); );
}, },
&MULTISCALAR_SIZES,
); );
} }
}
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 {
c.bench_with_input(
BenchmarkId::new(
"Variable-time fixed-base multiscalar multiplication", "Variable-time fixed-base multiscalar multiplication",
&multiscalar_size,
),
&multiscalar_size,
move |b, &&total_size| { move |b, &&total_size| {
let static_size = total_size; let static_size = total_size;
@ -154,17 +171,21 @@ mod multiscalar_benches {
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>,
dynamic_fraction: f64,
) {
for multiscalar_size in &MULTISCALAR_SIZES {
c.bench_with_input(
BenchmarkId::new(
"Variable-time mixed-base multiscalar multiplication ({:.0}pct dyn)", "Variable-time mixed-base multiscalar multiplication ({:.0}pct dyn)",
100.0 * dynamic_fraction, format!("({:.0}pct dyn)", 100.0 * dynamic_fraction),
); ),
c.bench_function_over_inputs( &multiscalar_size,
&label,
move |b, &&total_size| { move |b, &&total_size| {
let dynamic_size = ((total_size as f64) * dynamic_fraction) as usize; let dynamic_size = ((total_size as f64) * dynamic_fraction) as usize;
let static_size = total_size - dynamic_size; let static_size = total_size - dynamic_size;
@ -195,20 +216,22 @@ mod multiscalar_benches {
BatchSize::SmallInput, BatchSize::SmallInput,
); );
}, },
&MULTISCALAR_SIZES,
); );
} }
fn vartime_precomputed_00_pct_dynamic(c: &mut Criterion) {
vartime_precomputed_helper(c, 0.0);
} }
fn vartime_precomputed_20_pct_dynamic(c: &mut Criterion) { fn multiscalar_multiplications(c: &mut Criterion) {
vartime_precomputed_helper(c, 0.2); let mut group: BenchmarkGroup<_> = c.benchmark_group("Multiscalar muls");
}
fn vartime_precomputed_50_pct_dynamic(c: &mut Criterion) { consttime_multiscalar_mul(&mut group);
vartime_precomputed_helper(c, 0.5); vartime_multiscalar_mul(&mut group);
vartime_precomputed_pure_static(&mut group);
let dynamic_fracs = [0.0, 0.2, 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,9 +261,11 @@ 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(
BenchmarkId::new("Batch Ristretto double-and-encode", *batch_size),
&batch_size,
|b, &&size| { |b, &&size| {
let mut rng = OsRng; let mut rng = OsRng;
let points: Vec<RistrettoPoint> = (0..size) let points: Vec<RistrettoPoint> = (0..size)
@ -253,9 +273,15 @@ mod ristretto_benches {
.collect(); .collect();
b.iter(|| RistrettoPoint::double_and_compress_batch(&points)); b.iter(|| RistrettoPoint::double_and_compress_batch(&points));
}, },
&BATCH_SIZES,
); );
} }
}
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! {
name = ristretto_benches; name = ristretto_benches;
@ -263,7 +289,7 @@ mod ristretto_benches {
targets = targets =
compress, compress,
decompress, decompress,
double_and_compress_batch, double_and_compress_group,
} }
} }
@ -295,27 +321,36 @@ 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(
BenchmarkId::new("Batch scalar inversion", *batch_size),
&batch_size,
|b, &&size| { |b, &&size| {
let mut rng = OsRng; let mut rng = OsRng;
let scalars: Vec<Scalar> = (0..size).map(|_| Scalar::random(&mut rng)).collect(); let scalars: Vec<Scalar> =
(0..size).map(|_| Scalar::random(&mut rng)).collect();
b.iter(|| { b.iter(|| {
let mut s = scalars.clone(); let mut s = scalars.clone();
Scalar::batch_invert(&mut s); Scalar::batch_invert(&mut s);
}); });
}, },
&BATCH_SIZES,
); );
} }
}
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! {
name = scalar_benches; name = scalar_benches;
config = Criterion::default(); config = Criterion::default();
targets = targets =
scalar_inversion, scalar_inversion,
batch_scalar_inversion, batch_scalar_inversion_group,
} }
} }