Move chacha20_poly1305 benches to Criterion
What changed, and why it matters
This commit is a routine refactoring of benchmark code. It moves ChaCha20 performance tests out of the source crate and into a separate Criterion-based benchmark suite. There is no change to the actual ChaCha20/Poly1305 library code, no bug fix, and no security-related change.
No security action needed. This is a benign build/test infrastructure change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch removes the old nightly-only #[bench] tests from chacha20_poly1305/src/benches.rs and the feature(test) / extern crate test plumbing from chacha20_poly1305/src/lib.rs. It adds a new Criterion benchmark file at benches/chacha20_poly1305/chacha20.rs and registers it in benches/Cargo.toml. The benchmark logic is functionally equivalent (same key/nonce/counter, same buffer sizes). No cryptographic implementation is modified.
Changed components
benches/Cargo.tomlbenches/chacha20_poly1305/chacha20.rschacha20_poly1305/src/benches.rschacha20_poly1305/src/lib.rsInspect captured patch +41 / −49
diff --git a/benches/Cargo.toml b/benches/Cargo.toml
index 955587e2..d2dcaa23 100644
--- a/benches/Cargo.toml
+++ b/benches/Cargo.toml
@@ -8,6 +8,8 @@ edition = "2021"
[dependencies]
bitcoin = { path = "../bitcoin", default-features = false, features = ["std"] }
bitcoin_hashes = { path = "../hashes" }
+chacha20-poly1305 = { path = "../chacha20_poly1305" }
+
criterion = "0.7"
hex_lit = "0.1.1"
@@ -26,6 +28,13 @@ name = "witness"
path = "bitcoin/witness.rs"
harness = false
+
+[[bench]]
+name = "chacha20"
+path = "chacha20_poly1305/chacha20.rs"
+harness = false
+
+
[[bench]]
name = "cmp"
path = "hashes/cmp.rs"
diff --git a/benches/chacha20_poly1305/chacha20.rs b/benches/chacha20_poly1305/chacha20.rs
new file mode 100644
index 00000000..95859295
--- /dev/null
+++ b/benches/chacha20_poly1305/chacha20.rs
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: CC0-1.0
+
+use std::hint::black_box;
+
+use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
+use std::time::Duration;
+
+use chacha20_poly1305::{chacha20::ChaCha20, Key, Nonce};
+
+fn bench_chacha20(c: &mut Criterion) {
+ let mut g = c.benchmark_group("chacha20");
+ g.measurement_time(Duration::from_secs(5)).warm_up_time(Duration::from_secs(2));
+
+ for &size in &[10usize, 1024, 65536] {
+ let key = Key::new([0u8; 32]);
+ let nonce = Nonce::new([0u8; 12]);
+ let count = 1u32;
+ let mut cipher = ChaCha20::new(key, nonce, count);
+ let mut buf = vec![0u8; size];
+ g.throughput(Throughput::Bytes(size as u64));
+ g.bench_function(BenchmarkId::new("apply_keystream", size), |b| {
+ b.iter(|| {
+ cipher.apply_keystream(black_box(&mut buf));
+ });
+ });
+ }
+
+ g.finish();
+}
+
+criterion_group!(benches, bench_chacha20);
+criterion_main!(benches);
diff --git a/chacha20_poly1305/src/benches.rs b/chacha20_poly1305/src/benches.rs
deleted file mode 100644
index ebe086c3..00000000
--- a/chacha20_poly1305/src/benches.rs
+++ /dev/null
@@ -1,42 +0,0 @@
-use test::Bencher;
-
-use crate::{ChaCha20, Key, Nonce};
-
-#[bench]
-pub fn chacha20_10(bh: &mut Bencher) {
- let key = Key::new([0u8; 32]);
- let nonce = Nonce::new([0u8; 12]);
- let count = 1;
- let mut chacha = ChaCha20::new(key, nonce, count);
- let mut bytes = [0u8; 10];
- bh.iter(|| {
- chacha.apply_keystream(&mut bytes[..]);
- });
- bh.bytes = bytes.len() as u64;
-}
-
-#[bench]
-pub fn chacha20_1k(bh: &mut Bencher) {
- let key = Key::new([0u8; 32]);
- let nonce = Nonce::new([0u8; 12]);
- let count = 1;
- let mut chacha = ChaCha20::new(key, nonce, count);
- let mut bytes = [0u8; 1024];
- bh.iter(|| {
- chacha.apply_keystream(&mut bytes[..]);
- });
- bh.bytes = bytes.len() as u64;
-}
-
-#[bench]
-pub fn chacha20_64k(bh: &mut Bencher) {
- let key = Key::new([0u8; 32]);
- let nonce = Nonce::new([0u8; 12]);
- let count = 1;
- let mut chacha = ChaCha20::new(key, nonce, count);
- let mut bytes = [0u8; 65536];
- bh.iter(|| {
- chacha.apply_keystream(&mut bytes[..]);
- });
- bh.bytes = bytes.len() as u64;
-}
diff --git a/chacha20_poly1305/src/lib.rs b/chacha20_poly1305/src/lib.rs
index 388bb23a..02f05ccb 100644
--- a/chacha20_poly1305/src/lib.rs
+++ b/chacha20_poly1305/src/lib.rs
@@ -8,7 +8,6 @@
#![no_std]
// Experimental features we need.
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
-#![cfg_attr(bench, feature(test))]
// Coding conventions.
#![warn(missing_docs)]
#![warn(deprecated_in_future)]
@@ -23,12 +22,6 @@ extern crate alloc;
#[cfg(feature = "std")]
extern crate std;
-#[cfg(bench)]
-extern crate test;
-
-#[cfg(bench)]
-mod benches;
-
pub mod chacha20;
pub mod poly1305;
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.