Skip to content

Commit

Permalink
benchmarks.
Browse files Browse the repository at this point in the history
  • Loading branch information
xvzcf committed Jun 25, 2024
1 parent 16a3293 commit 5ae8c58
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions libcrux-ml-dsa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ rand = { version = "0.8" }
hex = { version = "0.4.3", features = ["serde"] }
serde_json = { version = "1.0" }
serde = { version = "1.0", features = ["derive"] }
criterion = "0.5"
pqcrypto-dilithium = "0.5.0"

[[bench]]
name = "ml-dsa"
harness = false
94 changes: 94 additions & 0 deletions libcrux-ml-dsa/benches/ml-dsa.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use std::time::Duration;

use criterion::{criterion_group, criterion_main, Criterion};
use rand::{rngs::OsRng, RngCore};

use libcrux_ml_dsa::ml_dsa_65;

pub fn comparisons_key_generation(c: &mut Criterion) {
let mut rng = OsRng;
let mut group = c.benchmark_group("ML-DSA-65 Key Generation");
group.measurement_time(Duration::from_secs(10));

group.bench_function("libcrux portable (external random)", |b| {
let mut randomness = [0; 32];
rng.fill_bytes(&mut randomness);
b.iter(|| {
let _ = ml_dsa_65::generate_key_pair(randomness);
})
});

group.bench_function("pqclean reference implementation (internal random)", |b| {
b.iter(|| {
let (_, _) = pqcrypto_dilithium::dilithium3::keypair();
})
});
}

pub fn comparisons_signing(c: &mut Criterion) {
let mut rng = OsRng;
let mut group = c.benchmark_group("ML-DSA-65 Signing");
group.measurement_time(Duration::from_secs(10));

let mut message = [0u8; 511];
rng.fill_bytes(&mut message);

group.bench_function("libcrux portable (external random)", |b| {
let mut randomness = [0; 32];
rng.fill_bytes(&mut randomness);
let keypair = ml_dsa_65::generate_key_pair(randomness);

rng.fill_bytes(&mut randomness);
b.iter(|| {
let _ = ml_dsa_65::sign(keypair.signing_key, &message, randomness);
})
});

group.bench_function("pqclean reference implementation (internal random)", |b| {
let (_, sk) = pqcrypto_dilithium::dilithium3::keypair();
b.iter(|| {
let _ = pqcrypto_dilithium::dilithium3::detached_sign(&message, &sk);
})
});
}

pub fn comparisons_verification(c: &mut Criterion) {
let mut rng = OsRng;
let mut group = c.benchmark_group("ML-DSA-65 Verification");
group.measurement_time(Duration::from_secs(10));

let mut message = [0u8; 511];
rng.fill_bytes(&mut message);

group.bench_function("libcrux portable (external random)", |b| {
let mut randomness = [0; 32];
rng.fill_bytes(&mut randomness);
let keypair = ml_dsa_65::generate_key_pair(randomness);

rng.fill_bytes(&mut randomness);
let signature = ml_dsa_65::sign(keypair.signing_key, &message, randomness);
b.iter(|| {
let _ = ml_dsa_65::verify(keypair.verification_key, &message, signature).unwrap();
})
});

group.bench_function("pqclean reference implementation (internal random)", |b| {
let (vk, sk) = pqcrypto_dilithium::dilithium3::keypair();
let signature = pqcrypto_dilithium::dilithium3::detached_sign(&message, &sk);
b.iter(|| {
let _ = pqcrypto_dilithium::dilithium3::verify_detached_signature(
&signature, &message, &vk,
)
.unwrap();
})
});
}

pub fn comparisons(c: &mut Criterion) {
comparisons_key_generation(c);
comparisons_signing(c);
comparisons_verification(c);
}

criterion_group!(benches, comparisons);
criterion_main!(benches);
1 change: 1 addition & 0 deletions libcrux-ml-dsa/src/ml_dsa_65.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub struct MLDSA65KeyPair {
pub verification_key: MLDSA65VerificationKey,
}

#[derive(Clone, Copy)]
pub struct MLDSA65Signature(pub [u8; SIGNATURE_SIZE]);

/// Generate an ML-DSA-65 Key Pair
Expand Down
2 changes: 1 addition & 1 deletion libcrux-ml-dsa/tests/wycheproof/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The JSON files were taken from `https://github.com/C2SP/wycheproof/pull/112`, and
The JSON files were taken from `https://github.com/C2SP/wycheproof/pull/112`.

Both `sign_schema.rs` and `verify_schema.rs` were generated with
[quicktype](https://github.com/glideapps/quicktype), using the commands:
Expand Down

0 comments on commit 5ae8c58

Please sign in to comment.