Skip to content

Commit

Permalink
Include raw KEM operations in benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
jschneider-bensch committed Jun 13, 2024
1 parent 0155caf commit e4d952c
Showing 1 changed file with 100 additions and 17 deletions.
117 changes: 100 additions & 17 deletions libcrux-psq/benches/psq.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use classic_mceliece_rust::{decapsulate_boxed, encapsulate_boxed};
use rand::thread_rng;
use std::time::Duration;

use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
pub fn comparisons_key_generation(c: &mut Criterion) {
pub fn comparisons_kem_key_generation(c: &mut Criterion) {
let mut rng = thread_rng();
let mut group = c.benchmark_group("PSK-PQ Key Generation");
group.measurement_time(Duration::from_secs(15));
Expand All @@ -25,16 +26,96 @@ pub fn comparisons_key_generation(c: &mut Criterion) {
});
}

pub fn comparisons_psk_generation(c: &mut Criterion) {
pub fn comparisons_kem_encaps(c: &mut Criterion) {
let mut rng = thread_rng();
let mut group = c.benchmark_group("PSK-PQ Pre-Shared Key Generation");
let mut group = c.benchmark_group("Raw KEM Encapsulation");
group.measurement_time(Duration::from_secs(15));

group.bench_function("libcrux ML-KEM-768", |b| {
b.iter_batched(
|| libcrux_kem::key_gen(libcrux_kem::Algorithm::MlKem768, &mut rng).unwrap(),
|(_sk, pk)| {
let _ = pk.encapsulate(&mut thread_rng());
},
BatchSize::SmallInput,
)
});

group.bench_function("libcrux X25519", |b| {
b.iter_batched(
|| libcrux_kem::key_gen(libcrux_kem::Algorithm::X25519, &mut rng).unwrap(),
|(_sk, pk)| {
let _ = pk.encapsulate(&mut thread_rng());
},
BatchSize::SmallInput,
)
});

group.bench_function("classic_mceliece_rust (mceliece460896f)", |b| {
b.iter_batched(
|| classic_mceliece_rust::keypair_boxed(&mut rng),
|(pk, _sk)| {
let _ = encapsulate_boxed(&pk, &mut thread_rng());
},
BatchSize::SmallInput,
)
});
}

pub fn comparisons_kem_decaps(c: &mut Criterion) {
let mut rng = thread_rng();
let mut group = c.benchmark_group("Raw KEM Decapsulation");
group.measurement_time(Duration::from_secs(15));

group.bench_function("libcrux ML-KEM-768", |b| {
b.iter_batched(
|| {
let (sk, pk) =
libcrux_kem::key_gen(libcrux_kem::Algorithm::MlKem768, &mut rng).unwrap();
let (_ss, enc) = pk.encapsulate(&mut rng).unwrap();
(sk, enc)
},
|(sk, enc)| enc.decapsulate(&sk),
BatchSize::SmallInput,
)
});

group.bench_function("libcrux X25519", |b| {
b.iter_batched(
|| {
let (sk, pk) =
libcrux_kem::key_gen(libcrux_kem::Algorithm::X25519, &mut rng).unwrap();
let (_ss, enc) = pk.encapsulate(&mut rng).unwrap();
(sk, enc)
},
|(sk, enc)| enc.decapsulate(&sk),
BatchSize::SmallInput,
)
});

group.bench_function("classic_mceliece_rust (mceliece460896f)", |b| {
b.iter_batched(
|| {
let (pk, sk) = classic_mceliece_rust::keypair_boxed(&mut rng);
let (enc, _ss) = encapsulate_boxed(&pk, &mut rng);
(sk, enc)
},
|(sk, enc)| decapsulate_boxed(&enc, &sk),
BatchSize::SmallInput,
)
});
}

pub fn comparisons_psk_send(c: &mut Criterion) {
let mut rng = thread_rng();
let mut group = c.benchmark_group("PSK-PQ Pre-Shared Key Send");
group.measurement_time(Duration::from_secs(15));

group.bench_function("libcrux ML-KEM-768", |b| {
b.iter_batched(
|| libcrux_psq::generate_key_pair(libcrux_psq::Algorithm::MlKem768, &mut rng).unwrap(),
|(_sk, pk)| {
let _ = pk.generate_psk(
let _ = pk.send_psk(
b"bench context",
chrono::Duration::hours(1),
&mut thread_rng(),
Expand All @@ -48,7 +129,7 @@ pub fn comparisons_psk_generation(c: &mut Criterion) {
b.iter_batched(
|| libcrux_psq::generate_key_pair(libcrux_psq::Algorithm::X25519, &mut rng).unwrap(),
|(_sk, pk)| {
let _ = pk.generate_psk(
let _ = pk.send_psk(
b"bench context",
chrono::Duration::hours(1),
&mut thread_rng(),
Expand All @@ -65,7 +146,7 @@ pub fn comparisons_psk_generation(c: &mut Criterion) {
.unwrap()
},
|(_sk, pk)| {
let _ = pk.generate_psk(
let _ = pk.send_psk(
b"bench context",
chrono::Duration::hours(1),
&mut thread_rng(),
Expand All @@ -76,9 +157,9 @@ pub fn comparisons_psk_generation(c: &mut Criterion) {
});
}

pub fn comparisons_psk_derivation(c: &mut Criterion) {
pub fn comparisons_psk_receive(c: &mut Criterion) {
let mut rng = thread_rng();
let mut group = c.benchmark_group("PSK-PQ Pre-Shared Key Derivation");
let mut group = c.benchmark_group("PSK-PQ Pre-Shared Key Receive");
group.measurement_time(Duration::from_secs(15));

group.bench_function("libcrux ML-KEM-768", |b| {
Expand All @@ -89,12 +170,12 @@ pub fn comparisons_psk_derivation(c: &mut Criterion) {
.unwrap();

let (_psk, message) = pk
.generate_psk(b"bench context", chrono::Duration::hours(1), &mut rng)
.send_psk(b"bench context", chrono::Duration::hours(1), &mut rng)
.unwrap();
(pk, sk, message)
},
|(pk, sk, message)| {
let _ = sk.derive_psk(&pk, &message, b"bench context");
let _ = sk.receive_psk(&pk, &message, b"bench context");
},
BatchSize::SmallInput,
)
Expand All @@ -108,12 +189,12 @@ pub fn comparisons_psk_derivation(c: &mut Criterion) {
.unwrap();

let (_psk, message) = pk
.generate_psk(b"bench context", chrono::Duration::hours(1), &mut rng)
.send_psk(b"bench context", chrono::Duration::hours(1), &mut rng)
.unwrap();
(pk, sk, message)
},
|(pk, sk, message)| {
let _ = sk.derive_psk(&pk, &message, b"bench context");
let _ = sk.receive_psk(&pk, &message, b"bench context");
},
BatchSize::SmallInput,
)
Expand All @@ -129,22 +210,24 @@ pub fn comparisons_psk_derivation(c: &mut Criterion) {
.unwrap();

let (_psk, message) = pk
.generate_psk(b"bench context", chrono::Duration::hours(1), &mut rng)
.send_psk(b"bench context", chrono::Duration::hours(1), &mut rng)
.unwrap();
(pk, sk, message)
},
|(pk, sk, message)| {
let _ = sk.derive_psk(&pk, &message, b"bench context");
let _ = sk.receive_psk(&pk, &message, b"bench context");
},
BatchSize::SmallInput,
)
});
}

pub fn comparisons(c: &mut Criterion) {
comparisons_key_generation(c);
comparisons_psk_generation(c);
comparisons_psk_derivation(c);
comparisons_kem_key_generation(c);
comparisons_kem_encaps(c);
comparisons_kem_decaps(c);
comparisons_psk_send(c);
comparisons_psk_receive(c);
}

criterion_group!(benches, comparisons);
Expand Down

0 comments on commit e4d952c

Please sign in to comment.