Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More benchmarks and ct #343

Merged
merged 5 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
264 changes: 122 additions & 142 deletions libcrux-ml-kem/benches/ml-kem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,160 +5,140 @@ use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use rand_core::OsRng;
use rand_core::RngCore;

use libcrux_ml_kem::mlkem768;
use libcrux_ml_kem::{mlkem1024, mlkem512, mlkem768};

macro_rules! init {
($version:path, $bench:expr, $c:expr) => {{
let mut group = $c.benchmark_group(format!("ML-KEM {} {}", stringify!($version), $bench));
group.measurement_time(Duration::from_secs(10));

use $version as version;
#[cfg(feature = "pre-verification")]
fun!("portable", version::portable, group);
#[cfg(all(feature = "simd128", feature = "pre-verification"))]
fun!("neon", version::neon, group);
#[cfg(all(feature = "simd256", feature = "pre-verification"))]
fun!("neon", version::avx2, group);
#[cfg(not(feature = "pre-verification"))]
fun!("verified", version, group);
}};
}

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

group.bench_function("libcrux portable (external random)", |b| {
let mut seed = [0; 64];
rng.fill_bytes(&mut seed);
b.iter(|| {
let _kp = mlkem768::generate_key_pair(seed);
})
});

// group.bench_function("libcrux portable (HACL-DRBG)", |b| {
// b.iter(|| {
// let (_secret_key, _public_key) =
// libcrux::kem::key_gen(Algorithm::MlKem768, &mut drbg).unwrap();
// })
// });

// group.bench_function("libcrux portable (OsRng)", |b| {
// b.iter(|| {
// let (_secret_key, _public_key) =
// libcrux::kem::key_gen(Algorithm::MlKem768, &mut rng).unwrap();
// })
// });

// group.bench_function("pqclean reference implementation", |b| {
// b.iter(|| {
// let (_public_key, _secret_key) = pqcrypto_kyber::kyber768::keypair();
// })
// });

macro_rules! fun {
($name:expr, $p:path, $group:expr) => {
$group.bench_function(format!("libcrux {} (external random)", $name), |b| {
use $p as p;

let mut seed = [0; 64];
rng.fill_bytes(&mut seed);
b.iter(|| {
let _kp = core::hint::black_box(p::generate_key_pair(seed));
})
});
};
}

init!(mlkem512, "Key Generation", c);
init!(mlkem768, "Key Generation", c);
init!(mlkem1024, "Key Generation", c);
}

pub fn comparisons_pk_validation(c: &mut Criterion) {
pub fn pk_validation(c: &mut Criterion) {
let mut rng = OsRng;
let mut group = c.benchmark_group("Kyber768 PK Validation");
group.measurement_time(Duration::from_secs(10));

group.bench_function("libcrux portable", |b| {
let mut seed = [0; 64];
rng.fill_bytes(&mut seed);
b.iter_batched(
|| {
let keypair = mlkem768::generate_key_pair(seed);
keypair.public_key().as_slice().into()
},
|public_key| {
let _valid = black_box(mlkem768::validate_public_key(public_key));
},
BatchSize::SmallInput,
)
});

macro_rules! fun {
($name:expr, $p:path, $group:expr) => {
$group.bench_function(format!("libcrux {}", $name), |b| {
use $p as p;

let mut seed = [0; 64];
rng.fill_bytes(&mut seed);
b.iter_batched(
|| {
let keypair = p::generate_key_pair(seed);
keypair.public_key().as_slice().into()
},
|public_key| {
let _valid = black_box(p::validate_public_key(public_key));
},
BatchSize::SmallInput,
)
});
};
}

init!(mlkem512, "PK Validation", c);
init!(mlkem768, "PK Validation", c);
init!(mlkem1024, "PK Validation", c);
}

pub fn comparisons_encapsulation(c: &mut Criterion) {
let mut group = c.benchmark_group("Kyber768 Encapsulation");
group.measurement_time(Duration::from_secs(10));

group.bench_function("libcrux portable (external random)", |b| {
let mut seed1 = [0; 64];
OsRng.fill_bytes(&mut seed1);
let mut seed2 = [0; 32];
OsRng.fill_bytes(&mut seed2);
b.iter_batched(
|| mlkem768::generate_key_pair(seed1),
|keypair| {
let (_shared_secret, _ciphertext) =
mlkem768::encapsulate(keypair.public_key(), seed2);
},
BatchSize::SmallInput,
)
});

// group.bench_function("libcrux portable", |b| {
// b.iter_batched(
// || {
// let mut drbg = Drbg::new(digest::Algorithm::Sha256).unwrap();
// let (_secret_key, public_key) =
// libcrux::kem::key_gen(Algorithm::MlKem768, &mut drbg).unwrap();

// (drbg, public_key)
// },
// |(mut rng, public_key)| {
// let (_shared_secret, _ciphertext) = public_key.encapsulate(&mut rng).unwrap();
// },
// BatchSize::SmallInput,
// )
// });

// group.bench_function("pqclean reference implementation", |b| {
// b.iter_batched(
// || {
// let (public_key, _secret_key) = pqcrypto_kyber::kyber768::keypair();

// public_key
// },
// |public_key| {
// let (_shared_secret, _ciphertext) =
// pqcrypto_kyber::kyber768::encapsulate(&public_key);
// },
// BatchSize::SmallInput,
// )
// });
pub fn encapsulation(c: &mut Criterion) {
macro_rules! fun {
($name:expr, $p:path, $group:expr) => {
$group.bench_function(format!("libcrux {} (external random)", $name), |b| {
use $p as p;

let mut seed1 = [0; 64];
OsRng.fill_bytes(&mut seed1);
let mut seed2 = [0; 32];
OsRng.fill_bytes(&mut seed2);
b.iter_batched(
|| p::generate_key_pair(seed1),
|keypair| {
let (_shared_secret, _ciphertext) =
black_box(p::encapsulate(keypair.public_key(), seed2));
},
BatchSize::SmallInput,
)
});
};
}

init!(mlkem512, "Encapsulation", c);
init!(mlkem768, "Encapsulation", c);
init!(mlkem1024, "Encapsulation", c);
}

pub fn comparisons_decapsulation(c: &mut Criterion) {
let mut group = c.benchmark_group("Kyber768 Decapsulation");
group.measurement_time(Duration::from_secs(10));

group.bench_function("libcrux portable", |b| {
let mut seed1 = [0; 64];
OsRng.fill_bytes(&mut seed1);
let mut seed2 = [0; 32];
OsRng.fill_bytes(&mut seed2);
b.iter_batched(
|| {
let keypair = mlkem768::generate_key_pair(seed1);
let (ciphertext, _shared_secret) =
mlkem768::encapsulate(keypair.public_key(), seed2);
(keypair, ciphertext)
},
|(keypair, ciphertext)| {
let _shared_secret = mlkem768::decapsulate(keypair.private_key(), &ciphertext);
},
BatchSize::SmallInput,
)
});

// group.bench_function("pqclean reference implementation", |b| {
// b.iter_batched(
// || {
// let (public_key, secret_key) = pqcrypto_kyber::kyber768::keypair();
// let (_shared_secret, ciphertext) =
// pqcrypto_kyber::kyber768::encapsulate(&public_key);

// (ciphertext, secret_key)
// },
// |(ciphertext, secret_key)| {
// let _shared_secret =
// pqcrypto_kyber::kyber768::decapsulate(&ciphertext, &secret_key);
// },
// BatchSize::SmallInput,
// )
// });
pub fn decapsulation(c: &mut Criterion) {
macro_rules! fun {
($name:expr, $p:path, $group:expr) => {
$group.bench_function(format!("libcrux {}", $name), |b| {
use $p as p;

let mut seed1 = [0; 64];
OsRng.fill_bytes(&mut seed1);
let mut seed2 = [0; 32];
OsRng.fill_bytes(&mut seed2);
b.iter_batched(
|| {
let keypair = p::generate_key_pair(seed1);
let (ciphertext, _shared_secret) =
p::encapsulate(keypair.public_key(), seed2);
(keypair, ciphertext)
},
|(keypair, ciphertext)| {
let _shared_secret =
black_box(p::decapsulate(keypair.private_key(), &ciphertext));
},
BatchSize::SmallInput,
)
});
};
}

init!(mlkem512, "Decapsulation", c);
init!(mlkem768, "Decapsulation", c);
init!(mlkem1024, "Decapsulation", c);
}

pub fn comparisons(c: &mut Criterion) {
comparisons_pk_validation(c);
comparisons_key_generation(c);
comparisons_encapsulation(c);
comparisons_decapsulation(c);
pk_validation(c);
key_generation(c);
encapsulation(c);
decapsulation(c);
}

criterion_group!(benches, comparisons);
Expand Down
4 changes: 2 additions & 2 deletions libcrux-ml-kem/c/code_gen.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
This code was generated with the following tools:
Charon: 23f20c184e51015582b7918ea4f1eb063b28daba
Eurydice: 30fdb50add4dabaee90051878c166bac8c5ac26a
Charon: aeeae1d46704810bf498db552a75dff15aa3abcc
Eurydice: ffeb01ce4cf0646e5cadec836bc042f98b8a16a8
Karamel: 42a431696cd32d41155d7e484720eb71fd5dc7b1
F*: a32b316e521fa4f239b610ec8f1d15e78d62cbe8-dirty
19 changes: 5 additions & 14 deletions libcrux-ml-kem/c/internal/libcrux_core.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
This file was generated by KaRaMeL <https://github.com/FStarLang/karamel>
KaRaMeL invocation: /home/franziskus/eurydice//eurydice --config ../c.yaml
../../libcrux_ml_kem.llbc ../../libcrux_sha3.llbc F* version: <unknown>
KaRaMeL version: 42a43169
-funroll-loops 16 ../../libcrux_ml_kem.llbc ../../libcrux_sha3.llbc F*
version: <unknown> KaRaMeL version: 42a43169
*/

#ifndef __internal_libcrux_core_H
Expand All @@ -19,6 +19,9 @@ extern "C" {

static inline uint32_t core_num__u8_6__count_ones(uint8_t x0);

uint8_t libcrux_ml_kem_constant_time_ops_compare_ciphertexts_in_constant_time(
Eurydice_slice lhs, Eurydice_slice rhs);

#define LIBCRUX_ML_KEM_CONSTANTS_SHARED_SECRET_SIZE ((size_t)32U)

void libcrux_ml_kem_constant_time_ops_select_shared_secret_in_constant_time(
Expand Down Expand Up @@ -74,10 +77,6 @@ uint8_t *
libcrux_ml_kem_types__libcrux_ml_kem__types__MlKemPublicKey_SIZE__18__as_slice___1568size_t(
libcrux_ml_kem_types_MlKemPublicKey____1568size_t *self);

uint8_t
libcrux_ml_kem_constant_time_ops_compare_ciphertexts_in_constant_time___1568size_t(
Eurydice_slice lhs, Eurydice_slice rhs);

Eurydice_slice
libcrux_ml_kem_types___core__convert__AsRef__Slice_u8___for_libcrux_ml_kem__types__MlKemCiphertext_SIZE___1__as_ref___1568size_t(
libcrux_ml_kem_mlkem1024_MlKem1024Ciphertext *self);
Expand Down Expand Up @@ -106,10 +105,6 @@ uint8_t *
libcrux_ml_kem_types__libcrux_ml_kem__types__MlKemPublicKey_SIZE__18__as_slice___1184size_t(
libcrux_ml_kem_types_MlKemPublicKey____1184size_t *self);

uint8_t
libcrux_ml_kem_constant_time_ops_compare_ciphertexts_in_constant_time___1088size_t(
Eurydice_slice lhs, Eurydice_slice rhs);

Eurydice_slice
libcrux_ml_kem_types___core__convert__AsRef__Slice_u8___for_libcrux_ml_kem__types__MlKemCiphertext_SIZE___1__as_ref___1088size_t(
libcrux_ml_kem_mlkem768_MlKem768Ciphertext *self);
Expand Down Expand Up @@ -138,10 +133,6 @@ uint8_t *
libcrux_ml_kem_types__libcrux_ml_kem__types__MlKemPublicKey_SIZE__18__as_slice___800size_t(
libcrux_ml_kem_types_MlKemPublicKey____800size_t *self);

uint8_t
libcrux_ml_kem_constant_time_ops_compare_ciphertexts_in_constant_time___768size_t(
Eurydice_slice lhs, Eurydice_slice rhs);

void libcrux_ml_kem_utils_into_padded_array___33size_t(Eurydice_slice slice,
uint8_t ret[33U]);

Expand Down
4 changes: 2 additions & 2 deletions libcrux-ml-kem/c/internal/libcrux_mlkem_avx2.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
This file was generated by KaRaMeL <https://github.com/FStarLang/karamel>
KaRaMeL invocation: /home/franziskus/eurydice//eurydice --config ../c.yaml
../../libcrux_ml_kem.llbc ../../libcrux_sha3.llbc F* version: <unknown>
KaRaMeL version: 42a43169
-funroll-loops 16 ../../libcrux_ml_kem.llbc ../../libcrux_sha3.llbc F*
version: <unknown> KaRaMeL version: 42a43169
*/

#ifndef __internal_libcrux_mlkem_avx2_H
Expand Down
4 changes: 2 additions & 2 deletions libcrux-ml-kem/c/internal/libcrux_mlkem_portable.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
This file was generated by KaRaMeL <https://github.com/FStarLang/karamel>
KaRaMeL invocation: /home/franziskus/eurydice//eurydice --config ../c.yaml
../../libcrux_ml_kem.llbc ../../libcrux_sha3.llbc F* version: <unknown>
KaRaMeL version: 42a43169
-funroll-loops 16 ../../libcrux_ml_kem.llbc ../../libcrux_sha3.llbc F*
version: <unknown> KaRaMeL version: 42a43169
*/

#ifndef __internal_libcrux_mlkem_portable_H
Expand Down
4 changes: 2 additions & 2 deletions libcrux-ml-kem/c/internal/libcrux_sha3_avx2.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
This file was generated by KaRaMeL <https://github.com/FStarLang/karamel>
KaRaMeL invocation: /home/franziskus/eurydice//eurydice --config ../c.yaml
../../libcrux_ml_kem.llbc ../../libcrux_sha3.llbc F* version: <unknown>
KaRaMeL version: 42a43169
-funroll-loops 16 ../../libcrux_ml_kem.llbc ../../libcrux_sha3.llbc F*
version: <unknown> KaRaMeL version: 42a43169
*/

#ifndef __internal_libcrux_sha3_avx2_H
Expand Down
4 changes: 2 additions & 2 deletions libcrux-ml-kem/c/internal/libcrux_sha3_internal.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
This file was generated by KaRaMeL <https://github.com/FStarLang/karamel>
KaRaMeL invocation: /home/franziskus/eurydice//eurydice --config ../c.yaml
../../libcrux_ml_kem.llbc ../../libcrux_sha3.llbc F* version: <unknown>
KaRaMeL version: 42a43169
-funroll-loops 16 ../../libcrux_ml_kem.llbc ../../libcrux_sha3.llbc F*
version: <unknown> KaRaMeL version: 42a43169
*/

#ifndef __internal_libcrux_sha3_internal_H
Expand Down
Loading
Loading