|
| 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