-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benches: fix compilation errors (#645)
Also fix benches CI job and tweak ascon-aead benchmarks to make them more consistent with other benchmarks.
- Loading branch information
Showing
3 changed files
with
41 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,62 @@ | ||
// Copyright 2022 Sebastian Ramacher | ||
// SPDX-License-Identifier: MIT | ||
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput}; | ||
|
||
use ascon_aead::{ | ||
aead::{generic_array::typenum::Unsigned, Aead, AeadInPlace, KeyInit}, | ||
Ascon128, Ascon128a, Ascon80pq, | ||
}; | ||
use criterion::{ | ||
black_box, criterion_group, criterion_main, Bencher, BenchmarkId, Criterion, Throughput, | ||
}; | ||
use rand::{rngs::StdRng, RngCore, SeedableRng}; | ||
use ascon_aead::aead::{AeadInPlace, KeyInit}; | ||
use ascon_aead::{Ascon128, Ascon128a, Ascon80pq}; | ||
|
||
const KB: usize = 1024; | ||
|
||
fn bench_for_size<A: KeyInit + Aead>(b: &mut Bencher, rng: &mut dyn RngCore, size: usize) { | ||
let mut key = vec![0u8; A::KeySize::USIZE]; | ||
rng.fill_bytes(key.as_mut_slice()); | ||
let mut nonce = vec![0u8; A::NonceSize::USIZE]; | ||
rng.fill_bytes(nonce.as_mut_slice()); | ||
let mut plaintext = vec![0u8; size]; | ||
rng.fill_bytes(plaintext.as_mut_slice()); | ||
#[cfg(not(any(target_arch = "x86_64", target_arch = "x86")))] | ||
type Benchmarker = Criterion; | ||
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))] | ||
type Benchmarker = Criterion<criterion_cycles_per_byte::CyclesPerByte>; | ||
|
||
let cipher = A::new(key.as_slice().into()); | ||
let nonce = nonce.as_slice().into(); | ||
|
||
b.iter(|| black_box(cipher.encrypt(nonce, plaintext.as_slice()))); | ||
} | ||
|
||
fn bench_for_size_inplace<A: KeyInit + AeadInPlace>( | ||
b: &mut Bencher, | ||
rng: &mut dyn RngCore, | ||
size: usize, | ||
) { | ||
let mut key = vec![0u8; A::KeySize::USIZE]; | ||
rng.fill_bytes(key.as_mut_slice()); | ||
let mut nonce = vec![0u8; A::NonceSize::USIZE]; | ||
rng.fill_bytes(nonce.as_mut_slice()); | ||
let mut buffer = vec![0u8; size + 16]; | ||
rng.fill_bytes(buffer.as_mut_slice()); | ||
fn bench<A: AeadInPlace + KeyInit>(name: &str, c: &mut Benchmarker) { | ||
let mut group = c.benchmark_group(name); | ||
let nonce = black_box(Default::default()); | ||
let cipher = black_box(A::new(&Default::default())); | ||
|
||
let cipher = A::new(key.as_slice().into()); | ||
let nonce = nonce.as_slice().into(); | ||
let mut buf = vec![0u8; 16 * KB]; | ||
for size in [KB, 2 * KB, 4 * KB, 8 * KB, 16 * KB] { | ||
let buf = &mut buf[..size]; | ||
let tag = cipher.encrypt_in_place_detached(&nonce, b"", buf).unwrap(); | ||
|
||
b.iter(|| black_box(cipher.encrypt_in_place(nonce, b"", &mut buffer))); | ||
} | ||
group.throughput(Throughput::Bytes(size as u64)); | ||
|
||
fn criterion_benchmark<A: KeyInit + Aead>(c: &mut Criterion, name: &str) { | ||
let mut rng = StdRng::from_entropy(); | ||
let mut group = c.benchmark_group(name); | ||
for size in [KB, 2 * KB, 4 * KB, 8 * KB, 16 * KB, 32 * KB, 64 * KB].iter() { | ||
group.throughput(Throughput::Bytes(*size as u64)); | ||
group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, &size| { | ||
bench_for_size::<A>(b, &mut rng, size) | ||
group.bench_function(BenchmarkId::new("encrypt-128", size), |b| { | ||
b.iter(|| cipher.encrypt_in_place_detached(&nonce, b"", buf)) | ||
}); | ||
} | ||
group.finish(); | ||
} | ||
|
||
fn criterion_benchmark_inplace<A: KeyInit + AeadInPlace>(c: &mut Criterion, name: &str) { | ||
let mut rng = StdRng::from_entropy(); | ||
let mut group = c.benchmark_group(name); | ||
for size in [KB, 2 * KB, 4 * KB, 8 * KB, 16 * KB, 32 * KB, 64 * KB].iter() { | ||
group.throughput(Throughput::Bytes(*size as u64)); | ||
group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, &size| { | ||
bench_for_size_inplace::<A>(b, &mut rng, size) | ||
group.bench_function(BenchmarkId::new("decrypt-128", size), |b| { | ||
b.iter(|| cipher.decrypt_in_place_detached(&nonce, b"", buf, &tag)) | ||
}); | ||
} | ||
group.finish(); | ||
} | ||
|
||
fn criterion_bench_ascon128(c: &mut Criterion) { | ||
criterion_benchmark::<Ascon128>(c, "Ascon-128"); | ||
} | ||
|
||
fn criterion_bench_ascon128a(c: &mut Criterion) { | ||
criterion_benchmark::<Ascon128a>(c, "Ascon-128a"); | ||
} | ||
|
||
fn criterion_bench_ascon80pq(c: &mut Criterion) { | ||
criterion_benchmark::<Ascon80pq>(c, "Ascon-80pq"); | ||
group.finish(); | ||
} | ||
|
||
fn criterion_bench_ascon128_inplace(c: &mut Criterion) { | ||
criterion_benchmark_inplace::<Ascon128>(c, "Ascon-128 (inplace)"); | ||
fn bench_ascon128(c: &mut Benchmarker) { | ||
bench::<Ascon128>("ascon128", c); | ||
} | ||
|
||
fn criterion_bench_ascon128a_inplace(c: &mut Criterion) { | ||
criterion_benchmark_inplace::<Ascon128a>(c, "Ascon-128a (inplace)"); | ||
fn bench_ascon128a(c: &mut Benchmarker) { | ||
bench::<Ascon128a>("ascon128a", c); | ||
} | ||
|
||
fn criterion_bench_ascon80pq_inplace(c: &mut Criterion) { | ||
criterion_benchmark_inplace::<Ascon80pq>(c, "Ascon-80pq (inplace)"); | ||
fn bench_ascon80pq(c: &mut Benchmarker) { | ||
bench::<Ascon80pq>("ascon80pq", c); | ||
} | ||
|
||
#[cfg(not(any(target_arch = "x86_64", target_arch = "x86")))] | ||
criterion_group!( | ||
bench_ascon128, | ||
criterion_bench_ascon128, | ||
criterion_bench_ascon128_inplace, | ||
); | ||
criterion_group!( | ||
bench_ascon128a, | ||
criterion_bench_ascon128a, | ||
criterion_bench_ascon128a_inplace | ||
name = benches; | ||
config = Criterion::default(); | ||
targets = bench_ascon128, bench_ascon128a, bench_ascon80pq, | ||
); | ||
|
||
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))] | ||
criterion_group!( | ||
bench_ascon80pq, | ||
criterion_bench_ascon80pq, | ||
criterion_bench_ascon80pq_inplace | ||
name = benches; | ||
config = Criterion::default().with_measurement(criterion_cycles_per_byte::CyclesPerByte); | ||
targets = bench_ascon128, bench_ascon128a, bench_ascon80pq, | ||
); | ||
criterion_main!(bench_ascon128, bench_ascon128a, bench_ascon80pq); | ||
|
||
criterion_main!(benches); |