Add chacha20_poly1305 Criterion AEAD benchmarks
What changed, and why it matters
This commit only adds new performance benchmark tests for the ChaCha20-Poly1305 encryption code. It does not change any production code, fix any bug, or alter any security behavior. There is no security issue here.
No action required. This is a benign benchmark-only change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff adds a Criterion benchmark target (chacha20poly1305) in benches/Cargo.toml and a new benchmark file that exercises ChaCha20Poly1305::encrypt and decrypt with various payload sizes, with and without AAD. The code under test is the existing AEAD implementation; no library logic is modified. The benchmark uses fixed all-zero keys/nonces and dummy AAD, which is normal and safe for benchmarking because the inputs are not secret and the results are discarded.
Changed components
benches/chacha20_poly1305/chacha20poly1305.rsbenches/Cargo.tomlInspect captured patch +63 / −0
diff --git a/benches/Cargo.toml b/benches/Cargo.toml
index d2dcaa23..aacc2f20 100644
--- a/benches/Cargo.toml
+++ b/benches/Cargo.toml
@@ -29,6 +29,11 @@ path = "bitcoin/witness.rs"
harness = false
+[[bench]]
+name = "chacha20poly1305"
+path = "chacha20_poly1305/chacha20poly1305.rs"
+harness = false
+
[[bench]]
name = "chacha20"
path = "chacha20_poly1305/chacha20.rs"
diff --git a/benches/chacha20_poly1305/chacha20poly1305.rs b/benches/chacha20_poly1305/chacha20poly1305.rs
new file mode 100644
index 00000000..cc87e4c6
--- /dev/null
+++ b/benches/chacha20_poly1305/chacha20poly1305.rs
@@ -0,0 +1,58 @@
+// 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::{ChaCha20Poly1305, Key, Nonce};
+
+fn bench_chacha20poly1305(c: &mut Criterion) {
+ let mut g = c.benchmark_group("chacha20poly1305");
+ g.measurement_time(Duration::from_secs(5)).warm_up_time(Duration::from_secs(2));
+
+ for &size in &[128usize, 1024, 16 * 1024, 64 * 1024] {
+ let key = Key::new([0u8; 32]);
+ let nonce = Nonce::new([0u8; 12]);
+
+ let pt = vec![0u8; size];
+ let aad: &[u8] = b"dummy_aad";
+
+ g.throughput(Throughput::Bytes(size as u64));
+
+ g.bench_function(BenchmarkId::new("encrypt_no_aad", size), |b| {
+ b.iter(|| {
+ let mut buf = pt.clone();
+ let cipher = ChaCha20Poly1305::new(key, nonce);
+ let tag = cipher.encrypt(black_box(&mut buf), None);
+ black_box(tag);
+ });
+ });
+
+ g.bench_function(BenchmarkId::new("encrypt_with_aad", size), |b| {
+ b.iter(|| {
+ let mut buf = pt.clone();
+ let cipher = ChaCha20Poly1305::new(key, nonce);
+ let tag = cipher.encrypt(black_box(&mut buf), Some(aad));
+ black_box(tag);
+ });
+ });
+
+ let mut ct = pt.clone();
+ let tag = ChaCha20Poly1305::new(key, nonce).encrypt(&mut ct, Some(aad));
+
+ g.bench_function(BenchmarkId::new("decrypt_ok", size), |b| {
+ b.iter(|| {
+ let mut buf = ct.clone();
+ let cipher = ChaCha20Poly1305::new(key, nonce);
+ let res = cipher.decrypt(black_box(&mut buf), tag, Some(aad));
+ black_box(res.unwrap());
+ });
+ });
+ }
+
+ g.finish();
+}
+
+criterion_group!(benches, bench_chacha20poly1305);
+criterion_main!(benches);
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.