Skip to content

Commit 1ab049f

Browse files
authored
Limb: add initial benchmarks (#412)
Adds benchmarks for constant-time comparison operations, with the goal of optimizing them better.
1 parent 1aa32e9 commit 1ab049f

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ required-features = ["alloc"]
6060
name = "dyn_residue"
6161
harness = false
6262

63+
[[bench]]
64+
name = "limb"
65+
harness = false
66+
6367
[[bench]]
6468
name = "uint"
6569
harness = false

benches/limb.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use criterion::{
2+
black_box, criterion_group, criterion_main, measurement::Measurement, BatchSize,
3+
BenchmarkGroup, Criterion,
4+
};
5+
use crypto_bigint::{Limb, Random};
6+
use rand_core::OsRng;
7+
use subtle::{ConstantTimeEq, ConstantTimeGreater, ConstantTimeLess};
8+
9+
fn bench_cmp<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
10+
group.bench_function("ct_lt", |b| {
11+
b.iter_batched(
12+
|| {
13+
let x = Limb::random(&mut OsRng);
14+
let y = Limb::random(&mut OsRng);
15+
(x, y)
16+
},
17+
|(x, y)| black_box(x.ct_lt(&y)),
18+
BatchSize::SmallInput,
19+
)
20+
});
21+
22+
group.bench_function("ct_eq", |b| {
23+
b.iter_batched(
24+
|| {
25+
let x = Limb::random(&mut OsRng);
26+
let y = Limb::random(&mut OsRng);
27+
(x, y)
28+
},
29+
|(x, y)| black_box(x.ct_eq(&y)),
30+
BatchSize::SmallInput,
31+
)
32+
});
33+
34+
group.bench_function("ct_gt", |b| {
35+
b.iter_batched(
36+
|| {
37+
let x = Limb::random(&mut OsRng);
38+
let y = Limb::random(&mut OsRng);
39+
(x, y)
40+
},
41+
|(x, y)| black_box(x.ct_gt(&y)),
42+
BatchSize::SmallInput,
43+
)
44+
});
45+
}
46+
47+
fn bench_ops(c: &mut Criterion) {
48+
let mut group = c.benchmark_group("ops");
49+
bench_cmp(&mut group);
50+
group.finish();
51+
}
52+
53+
criterion_group!(benches, bench_ops);
54+
55+
criterion_main!(benches);

0 commit comments

Comments
 (0)