From e41653d18838e0d6e157db475b7b034b2456c924 Mon Sep 17 00:00:00 2001 From: jeremiah Date: Mon, 17 Feb 2025 21:00:40 -0500 Subject: [PATCH] add chacha20 benchmarks --- chacha20_poly1305/Cargo.toml | 3 +++ chacha20_poly1305/src/benches.rs | 45 ++++++++++++++++++++++++++++++++ chacha20_poly1305/src/lib.rs | 5 ++++ 3 files changed, 53 insertions(+) create mode 100644 chacha20_poly1305/src/benches.rs diff --git a/chacha20_poly1305/Cargo.toml b/chacha20_poly1305/Cargo.toml index 7e4e45358..da5242bf9 100644 --- a/chacha20_poly1305/Cargo.toml +++ b/chacha20_poly1305/Cargo.toml @@ -22,3 +22,6 @@ hex = { package = "hex-conservative", version = "0.3.0", default-features = fals [package.metadata.docs.rs] all-features = true rustdoc-args = ["--cfg", "docsrs"] + +[lints.rust] +unexpected_cfgs = { level = "deny", check-cfg = ['cfg(bench)'] } diff --git a/chacha20_poly1305/src/benches.rs b/chacha20_poly1305/src/benches.rs new file mode 100644 index 000000000..5bfb6572c --- /dev/null +++ b/chacha20_poly1305/src/benches.rs @@ -0,0 +1,45 @@ +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 3058913ae..42b2df3af 100644 --- a/chacha20_poly1305/src/lib.rs +++ b/chacha20_poly1305/src/lib.rs @@ -5,6 +5,7 @@ #![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)] @@ -18,6 +19,10 @@ extern crate alloc; #[cfg(feature = "std")] extern crate std; +#[cfg(bench)] +mod benches; +#[cfg(bench)] +extern crate test; pub mod chacha20; pub mod poly1305;