Repository: RustCrypto/AEADs (language: rust)

Issue:
Consider using cycles for ascon-aead instead of milliseconds 
All the other benchmarks in benches use cycles apart from ascon-aead. It will be great if it will also be able to measure it in cycles rather than in milliseconds. 


Discussion / hints:

Fix patch:
diff --git a/.github/workflows/benches.yml b/.github/workflows/benches.yml
index feac9db8..c05b57b5 100644
--- a/.github/workflows/benches.yml
+++ b/.github/workflows/benches.yml
@@ -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
diff --git a/benches/Cargo.toml b/benches/Cargo.toml
index 32a5f0d4..03564426 100644
--- a/benches/Cargo.toml
+++ b/benches/Cargo.toml
@@ -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/" }
@@ -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" }
diff --git a/benches/src/ascon-aead.rs b/benches/src/ascon-aead.rs
index cee21170..565ad056 100644
--- a/benches/src/ascon-aead.rs
+++ b/benches/src/ascon-aead.rs
@@ -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);


Test patch: