| 1 |
use constant_time_eq::generic::{constant_time_eq, constant_time_eq_n}; |
| 2 |
use core::hint::black_box; |
| 3 |
use criterion::{ |
| 4 |
BenchmarkGroup, BenchmarkId, Criterion, Throughput, criterion_group, criterion_main, |
| 5 |
measurement::WallTime, |
| 6 |
}; |
| 7 |
|
| 8 |
fn bench_array(c: &mut Criterion) { |
| 9 |
let mut group = c.benchmark_group("generic::constant_time_eq_n"); |
| 10 |
|
| 11 |
fn bench_array_n<const N: usize>(group: &mut BenchmarkGroup<WallTime>) { |
| 12 |
let input = (&[1; N], &[2; N]); |
| 13 |
group.throughput(Throughput::Bytes(N as u64)); |
| 14 |
group.bench_with_input(BenchmarkId::from_parameter(N), &input, |b, &(x, y)| { |
| 15 |
b.iter(|| constant_time_eq_n(black_box(x), black_box(y))) |
| 16 |
}); |
| 17 |
} |
| 18 |
|
| 19 |
bench_array_n::<8>(&mut group); |
| 20 |
bench_array_n::<16>(&mut group); |
| 21 |
bench_array_n::<20>(&mut group); |
| 22 |
bench_array_n::<32>(&mut group); |
| 23 |
bench_array_n::<64>(&mut group); |
| 24 |
bench_array_n::<96>(&mut group); |
| 25 |
bench_array_n::<128>(&mut group); |
| 26 |
|
| 27 |
group.finish(); |
| 28 |
} |
| 29 |
|
| 30 |
fn bench_slice(c: &mut Criterion) { |
| 31 |
let mut group = c.benchmark_group("generic::constant_time_eq"); |
| 32 |
|
| 33 |
let input = (&[1; 65536], &[2; 65536]); |
| 34 |
for &size in &[8, 16, 20, 32, 64, 96, 128, 4 * 1024, 16 * 1024, 64 * 1024] { |
| 35 |
let input = (&input.0[..size], &input.1[..size]); |
| 36 |
group.throughput(Throughput::Bytes(size as u64)); |
| 37 |
group.bench_with_input(BenchmarkId::from_parameter(size), &input, |b, &(x, y)| { |
| 38 |
b.iter(|| constant_time_eq(black_box(x), black_box(y))) |
| 39 |
}); |
| 40 |
} |
| 41 |
|
| 42 |
group.finish(); |
| 43 |
} |
| 44 |
|
| 45 |
criterion_group!(benches, bench_array, bench_slice); |
| 46 |
criterion_main!(benches); |