Skip to content

Commit

Permalink
benches: fix compilation errors (#645)
Browse files Browse the repository at this point in the history
Also fix benches CI job and tweak ascon-aead benchmarks to make them
more consistent with other benchmarks.
  • Loading branch information
newpavlov authored Oct 28, 2024
1 parent a054ced commit 1da7daf
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 94 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/benches.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ jobs:
strategy:
matrix:
rust:
- 1.65.0 # MSRV
- 1.81.0 # MSRV
- stable
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
- run: cargo build --release
- run: cargo bench --no-run
7 changes: 1 addition & 6 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ rust-version = "1.56"
[dependencies]
criterion = "0.4.0"
rand = "0.8.5"
aes = "0.8.2"
aes = "=0.9.0-pre.2"
aes-gcm = { path = "../aes-gcm/" }
aes-gcm-siv = { path = "../aes-gcm-siv/" }
ascon-aead = { path = "../ascon-aead/" }
Expand Down Expand Up @@ -53,8 +53,3 @@ harness = false
name = "eax"
path = "src/eax.rs"
harness = false

[patch.crates-io]
chacha20 = { git = "https://github.com/RustCrypto/stream-ciphers.git" }
cmac = { git = "https://github.com/RustCrypto/MACs.git" }
ctr = { git = "https://github.com/RustCrypto/block-modes.git" }
124 changes: 38 additions & 86 deletions benches/src/ascon-aead.rs
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);

0 comments on commit 1da7daf

Please sign in to comment.