use std::hint::black_box;
use std::time::Duration;
use bcrypt_rust::__internal::{Backend, bcrypt_many_with_backend};
use bcrypt_rust::{Version, bcrypt, bcrypt_many, hash, hash_with_salt, verify};
use criterion::measurement::WallTime;
use criterion::{
BenchmarkGroup, BenchmarkId, Criterion, SamplingMode, Throughput, criterion_group,
criterion_main,
};
const PWD: &[u8] = b"correct horse battery staple";
const SALT: [u8; 16] = *b"bcrypt-bench-sal";
const BATCH: usize = 16;
struct SplitMix64(u64);
impl SplitMix64 {
fn new(seed: u64) -> Self {
Self(seed)
}
fn next_u64(&mut self) -> u64 {
self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15);
let mut z = self.0;
z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
z ^ (z >> 31)
}
fn next_below(&mut self, n: usize) -> usize {
(self.next_u64() % n as u64) as usize
}
fn fill_bytes(&mut self, buf: &mut [u8]) {
for chunk in buf.chunks_mut(8) {
let bytes = self.next_u64().to_le_bytes();
chunk.copy_from_slice(&bytes[..chunk.len()]);
}
}
}
fn batch_corpus() -> (Vec<Vec<u8>>, Vec<[u8; 16]>) {
let mut rng = SplitMix64::new(0xBC79_7BE0_0000_0002);
let store: Vec<Vec<u8>> = (0..BATCH)
.map(|_| {
let mut password = vec![0u8; 8 + rng.next_below(41)]; rng.fill_bytes(&mut password);
password
})
.collect();
let salts: Vec<[u8; 16]> = (0..BATCH)
.map(|_| {
let mut salt = [0u8; 16];
rng.fill_bytes(&mut salt);
salt
})
.collect();
(store, salts)
}
fn runnable_backends() -> Vec<Backend> {
Backend::ALL
.iter()
.copied()
.filter(|b| b.is_available())
.collect()
}
fn tune(group: &mut BenchmarkGroup<'_, WallTime>, cost: u32) {
if cost >= 12 {
group
.sample_size(10)
.sampling_mode(SamplingMode::Flat)
.warm_up_time(Duration::from_millis(500))
.measurement_time(Duration::from_secs(5));
} else {
group
.sample_size(50)
.warm_up_time(Duration::from_millis(500))
.measurement_time(Duration::from_secs(2));
}
}
fn bench_single_hash(c: &mut Criterion) {
let mut group = c.benchmark_group("single_hash");
for cost in [4u32, 10, 12] {
tune(&mut group, cost);
let label = format!("cost{cost}");
group.bench_function(BenchmarkId::new(label.as_str(), "raw_core"), |b| {
b.iter(|| black_box(bcrypt(black_box(cost), black_box(SALT), black_box(PWD))));
});
group.bench_function(BenchmarkId::new(label.as_str(), "public_hash"), |b| {
b.iter(|| black_box(hash(black_box(PWD), black_box(cost)).expect("hash")));
});
}
group.finish();
}
fn bench_batch(c: &mut Criterion) {
let backends = runnable_backends();
let (store, salts) = batch_corpus();
let passwords: Vec<&[u8]> = store.iter().map(Vec::as_slice).collect();
let mut group = c.benchmark_group("batch");
group.throughput(Throughput::Elements(BATCH as u64));
for cost in [4u32, 12] {
tune(&mut group, cost);
let label = format!("cost{cost}_n{BATCH}");
for &backend in &backends {
assert!(backend.is_available());
group.bench_function(BenchmarkId::new(backend.name(), &label), |b| {
b.iter(|| {
black_box(
unsafe {
bcrypt_many_with_backend(
black_box(backend),
cost,
black_box(&passwords),
black_box(&salts),
)
}
.expect("batch"),
);
});
});
}
group.bench_function(BenchmarkId::new("default", &label), |b| {
b.iter(|| {
black_box(
bcrypt_many(cost, black_box(&passwords), black_box(&salts)).expect("batch"),
);
});
});
}
group.finish();
}
fn bench_verify(c: &mut Criterion) {
let parts = hash_with_salt(PWD, 4, SALT).expect("valid cost");
let hash_string = parts.format_for_version(Version::TwoB);
assert_eq!(verify(PWD, &hash_string), Ok(true));
let mut group = c.benchmark_group("verify");
group.bench_function("cost4", |b| {
b.iter(|| {
black_box(verify(black_box(PWD), black_box(hash_string.as_str())).expect("verify"));
});
});
group.finish();
}
criterion_group!(benches, bench_single_hash, bench_batch, bench_verify);
criterion_main!(benches);