Skip to content

Instantly share code, notes, and snippets.

@elichai
Created November 19, 2024 18:47
Show Gist options
  • Save elichai/271019f1a6cf24c8f4c50aba2f1a5609 to your computer and use it in GitHub Desktop.
Save elichai/271019f1a6cf24c8f4c50aba2f1a5609 to your computer and use it in GitHub Desktop.
Behncmark aes\chacha
[package]
name = "bench_rng"
version = "0.1.0"
edition = "2021"
[dependencies]
aes-prng = "0.2.1"
blake3 = "1.5.4"
criterion = "0.5.1"
rand = "0.8.5"
rand_aes = "0.3.1"
rand_chacha = "0.3.1"
[[bench]]
name = "my_benchmark"
harness = false
[profile.release]
lto = true
debug = true
strip = false
use aes_prng::AesRng;
use rand::{thread_rng, Rng, RngCore, SeedableRng};
use rand_aes::{Aes128Ctr64, seeds::Aes128Ctr64Seed};
use rand_chacha::{ChaCha20Rng, ChaCha8Rng};
pub type Buffer = [u8; 1 << 21];
pub const fn zerored_buffer() -> Buffer {
[0; 1 << 21]
}
#[inline(never)]
#[no_mangle]
pub fn encrypt_aes_ctr(rng: &mut Aes128Ctr64, buf: &mut Buffer) {
rng.fill_bytes(buf);
}
#[inline(never)]
#[no_mangle]
pub fn encrypt_aes_prng(rng: &mut AesRng, buf: &mut Buffer) {
rng.fill_bytes(buf);
}
#[inline(never)]
#[no_mangle]
pub fn encrypt_chacha8(rng: &mut ChaCha8Rng, buf: &mut Buffer) {
rng.fill_bytes(buf);
}
#[inline(never)]
#[no_mangle]
pub fn encrypt_chacha20(rng: &mut ChaCha20Rng, buf: &mut Buffer) {
rng.fill_bytes(buf);
}
fn main() {
let mut chacha8 = ChaCha8Rng::from_seed([0u8; 32]);
let mut chacha20 = ChaCha20Rng::from_seed([0; 32]);
let mut aes_ctr = Aes128Ctr64::from_seed(Aes128Ctr64Seed::new([0; 16], [0; 8], 0));
let mut aes_prng = AesRng::from_seed([0; 16],);
let mut buf = zerored_buffer();
encrypt_chacha8(&mut chacha8, &mut buf);
encrypt_chacha20(&mut chacha20, &mut buf);
encrypt_aes_ctr(&mut aes_ctr, &mut buf);
encrypt_aes_prng(&mut aes_prng, &mut buf);
println!("{:?}", buf);
}
use aes_prng::AesRng;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rand::{thread_rng, Rng, RngCore, SeedableRng};
use rand_aes::{Aes128Ctr64, seeds::Aes128Ctr64Seed};
use rand_chacha::{ChaCha20Rng, ChaCha8Rng};
use bench_rng::*;
fn bench_rng(c: &mut Criterion) {
let seed = thread_rng().gen();
let mut chacha8 = ChaCha8Rng::from_seed(seed);
let mut chacha20 = ChaCha20Rng::from_seed(seed);
let mut aes_ctr = Aes128Ctr64::from_seed(Aes128Ctr64Seed::new(seed[0..16].try_into().unwrap(), [0; 8], 0));
let mut aes_prng = AesRng::from_seed(seed[16..].try_into().unwrap());
c.bench_function("chacha8", |b| {
let mut buf = zerored_buffer();
b.iter(|| encrypt_chacha8(&mut chacha8, &mut buf));
});
c.bench_function("chacha20", |b| {
let mut buf = zerored_buffer();
b.iter(|| encrypt_chacha20(&mut chacha20, &mut buf));
});
c.bench_function("aes_ctr", |b| {
let mut buf = zerored_buffer();
b.iter(|| encrypt_aes_ctr(&mut aes_ctr, &mut buf));
});
c.bench_function("aes_prng", |b| {
let mut buf = zerored_buffer();
b.iter(|| encrypt_aes_prng(&mut aes_prng, &mut buf));
});
}
criterion_group!(benches, bench_rng);
criterion_main!(benches);
use aes_prng::AesRng;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rand::{thread_rng, Rng, RngCore, SeedableRng};
use rand_aes::{Aes128Ctr64, seeds::Aes128Ctr64Seed};
use rand_chacha::{ChaCha20Rng, ChaCha8Rng};
use bench_rng::*;
fn bench_rng(c: &mut Criterion) {
let seed = thread_rng().gen();
let mut chacha8 = ChaCha8Rng::from_seed(seed);
let mut chacha20 = ChaCha20Rng::from_seed(seed);
let mut aes_ctr = Aes128Ctr64::from_seed(Aes128Ctr64Seed::new(seed[0..16].try_into().unwrap(), [0; 8], 0));
let mut aes_prng = AesRng::from_seed(seed[16..].try_into().unwrap());
c.bench_function("chacha8", |b| {
let mut buf = zerored_buffer();
b.iter(|| encrypt_chacha8(&mut chacha8, &mut buf));
});
c.bench_function("chacha20", |b| {
let mut buf = zerored_buffer();
b.iter(|| encrypt_chacha20(&mut chacha20, &mut buf));
});
c.bench_function("aes_ctr", |b| {
let mut buf = zerored_buffer();
b.iter(|| encrypt_aes_ctr(&mut aes_ctr, &mut buf));
});
c.bench_function("aes_prng", |b| {
let mut buf = zerored_buffer();
b.iter(|| encrypt_aes_prng(&mut aes_prng, &mut buf));
});
}
criterion_group!(benches, bench_rng);
criterion_main!(benches);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment