Skip to content

Commit

Permalink
argon2: add benchmarks using criterion (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc3 committed Jul 5, 2023
1 parent 3571f10 commit 795c424
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/target/
/.readme/target/
/benches/target/
**/Cargo.lock

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
"scrypt",
"sha-crypt"
]
exclude = ["benches"]

[profile.dev]
opt-level = 2
16 changes: 16 additions & 0 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "benches"
version = "0.0.0"
authors = ["RustCrypto Developers"]
license = "Apache-2.0 OR MIT"
edition = "2021"
publish = false

[dev-dependencies]
argon2 = { path = "../argon2" }
criterion = { version = "0.4", features = ["html_reports"] }

[[bench]]
name = "argon2"
path = "src/argon2.rs"
harness = false
89 changes: 89 additions & 0 deletions benches/src/argon2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use argon2::*;
use criterion::{black_box, criterion_group, criterion_main, Criterion};

const BENCH_PASSWORD: &[u8] = b"hunter2";
const BENCH_SALT: &[u8] = b"pepper42";

fn bench_default_params(c: &mut Criterion) {
for algorithm in [Algorithm::Argon2i, Algorithm::Argon2d, Algorithm::Argon2id] {
for version in [Version::V0x10, Version::V0x13] {
let test_name = format!("{algorithm} {version:?}");
c.bench_function(&test_name, |b| {
let mut out = [0u8; 32];
let argon2 = Argon2::new(algorithm, version, Params::default());
b.iter(|| {
argon2
.hash_password_into(
black_box(BENCH_PASSWORD),
black_box(BENCH_SALT),
&mut out,
)
.unwrap()
})
});
}
}
}

fn bench_vary_m(c: &mut Criterion) {
let t_cost = 4;
let p_cost = 4;
for m_cost in [2 * 1024, 16 * 1024, 64 * 1024, 256 * 1024] {
let test_name = format!("argon2id V0x13 m={m_cost} t={t_cost} p={p_cost}");
c.bench_function(&test_name, |b| {
let mut out = [0u8; 32];
let params = Params::new(m_cost, t_cost, p_cost, Some(32)).unwrap();
let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
b.iter(|| {
argon2
.hash_password_into(black_box(BENCH_PASSWORD), black_box(BENCH_SALT), &mut out)
.unwrap()
})
});
}
}

fn bench_vary_t(c: &mut Criterion) {
let m_cost = 32 * 1024;
let p_cost = 4;
for t_cost in [2, 8, 16, 24] {
let test_name = format!("argon2id V0x13 m={m_cost} t={t_cost} p={p_cost}");
c.bench_function(&test_name, |b| {
let mut out = [0u8; 32];
let params = Params::new(m_cost, t_cost, p_cost, Some(32)).unwrap();
let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
b.iter(|| {
argon2
.hash_password_into(black_box(BENCH_PASSWORD), black_box(BENCH_SALT), &mut out)
.unwrap()
})
});
}
}

fn bench_vary_p(c: &mut Criterion) {
let m_cost = 32 * 1024;
let t_cost = 4;
for p_cost in [2, 8, 16, 64] {
let test_name = format!("argon2id V0x13 m={m_cost} t={t_cost} p={p_cost}");
c.bench_function(&test_name, |b| {
let mut out = [0u8; 32];
let params = Params::new(m_cost, t_cost, p_cost, Some(32)).unwrap();
let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
b.iter(|| {
argon2
.hash_password_into(black_box(BENCH_PASSWORD), black_box(BENCH_SALT), &mut out)
.unwrap()
})
});
}
}

criterion_group!(
benches,
bench_default_params,
bench_vary_m,
bench_vary_t,
bench_vary_p,
);
criterion_main!(benches);
1 change: 1 addition & 0 deletions benches/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit 795c424

Please sign in to comment.