Skip to content

Commit f3f7feb

Browse files
authored
elliptic-curve: add dev::bench_projective! macro (#2177)
Writes `criterion` benchmarks for `ProjectivePoint` using a macro which has been tested with both `criterion` v0.7 and v0.8, which means we can release on the former (for MSRV 1.85 compat) and then later upgrade to to the latter in a non-breaking manner
1 parent 5e83836 commit f3f7feb

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

elliptic-curve/src/dev.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,49 @@ pub mod mock_curve;
1010
note = "import these types from the `dev::mock_curves` module"
1111
)]
1212
pub use mock_curve::*;
13+
14+
/// Write a series of `criterion`-based benchmarks for arithmetic on a projective curve point.
15+
#[macro_export]
16+
macro_rules! bench_projective {
17+
($name:ident, $desc:expr, $point_a:expr, $point_b:expr, $scalar:expr) => {
18+
fn bench_add<M: ::criterion::measurement::Measurement>(
19+
group: &mut ::criterion::BenchmarkGroup<'_, M>,
20+
) {
21+
let x = core::hint::black_box($point_a);
22+
let y = core::hint::black_box($point_b);
23+
group.bench_function("add", |b| b.iter(|| x + y));
24+
}
25+
26+
fn bench_sub<M: ::criterion::measurement::Measurement>(
27+
group: &mut ::criterion::BenchmarkGroup<'_, M>,
28+
) {
29+
let x = core::hint::black_box($point_a);
30+
let y = core::hint::black_box($point_b);
31+
group.bench_function("sub", |b| b.iter(|| x - y));
32+
}
33+
34+
fn bench_neg<M: ::criterion::measurement::Measurement>(
35+
group: &mut ::criterion::BenchmarkGroup<'_, M>,
36+
) {
37+
let x = core::hint::black_box($point_a);
38+
group.bench_function("neg", |b| b.iter(|| -x));
39+
}
40+
41+
fn bench_scalar_mul<M: ::criterion::measurement::Measurement>(
42+
group: &mut ::criterion::BenchmarkGroup<'_, M>,
43+
) {
44+
let p = core::hint::black_box($point_a);
45+
let s = core::hint::black_box($scalar);
46+
group.bench_function("scalar mul", |b| b.iter(|| p * s));
47+
}
48+
49+
pub fn $name(c: &mut ::criterion::Criterion) {
50+
let mut group = c.benchmark_group($desc);
51+
bench_add(&mut group);
52+
bench_sub(&mut group);
53+
bench_neg(&mut group);
54+
bench_scalar_mul(&mut group);
55+
group.finish();
56+
}
57+
};
58+
}

0 commit comments

Comments
 (0)