|
| 1 | +use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion}; |
| 2 | +use crypto_bigint::BoxedUint; |
| 3 | +use num_bigint::BigUint; |
| 4 | +use rand_core::OsRng; |
| 5 | + |
| 6 | +/// Size of `BoxedUint` to use in benchmark. |
| 7 | +const UINT_BITS: u32 = 4096; |
| 8 | + |
| 9 | +fn bench_shifts(c: &mut Criterion) { |
| 10 | + let mut group = c.benchmark_group("bit shifts"); |
| 11 | + |
| 12 | + group.bench_function("shl_vartime", |b| { |
| 13 | + b.iter_batched( |
| 14 | + || BoxedUint::random(&mut OsRng, UINT_BITS), |
| 15 | + |x| black_box(x.shl_vartime(UINT_BITS / 2 + 10)), |
| 16 | + BatchSize::SmallInput, |
| 17 | + ) |
| 18 | + }); |
| 19 | + |
| 20 | + group.bench_function("shl", |b| { |
| 21 | + b.iter_batched( |
| 22 | + || BoxedUint::random(&mut OsRng, UINT_BITS), |
| 23 | + |x| x.shl(UINT_BITS / 2 + 10), |
| 24 | + BatchSize::SmallInput, |
| 25 | + ) |
| 26 | + }); |
| 27 | + |
| 28 | + group.bench_function("shr_vartime", |b| { |
| 29 | + b.iter_batched( |
| 30 | + || BoxedUint::random(&mut OsRng, UINT_BITS), |
| 31 | + |x| black_box(x.shr_vartime(UINT_BITS / 2 + 10)), |
| 32 | + BatchSize::SmallInput, |
| 33 | + ) |
| 34 | + }); |
| 35 | + |
| 36 | + group.bench_function("shr", |b| { |
| 37 | + b.iter_batched( |
| 38 | + || BoxedUint::random(&mut OsRng, UINT_BITS), |
| 39 | + |x| x.shr(UINT_BITS / 2 + 10), |
| 40 | + BatchSize::SmallInput, |
| 41 | + ) |
| 42 | + }); |
| 43 | + |
| 44 | + group.finish(); |
| 45 | +} |
| 46 | + |
| 47 | +criterion_group!(benches, bench_shifts); |
| 48 | + |
| 49 | +criterion_main!(benches); |
0 commit comments