Skip to content

Commit

Permalink
Merge pull request #546 from cryspen/franziskus/ml-kem-rand
Browse files Browse the repository at this point in the history
Add randomised APIs to ml-kem
  • Loading branch information
franziskuskiefer authored Sep 3, 2024
2 parents 9680093 + 08491c7 commit 482620d
Show file tree
Hide file tree
Showing 16 changed files with 234 additions and 88 deletions.
63 changes: 31 additions & 32 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions libcrux-ecdh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ hex = { version = "0.4.3", features = ["serde"] }
serde_json = { version = "1.0" }
serde = { version = "1.0", features = ["derive"] }
pretty_env_logger = "0.5"

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(adx)', 'cfg(bmi2)'] }
63 changes: 40 additions & 23 deletions libcrux-kem/src/kem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,14 @@ pub struct X25519MlKem768Draft00PublicKey {
impl X25519MlKem768Draft00PublicKey {
pub fn decode(bytes: &[u8]) -> Result<Self, Error> {
Ok(Self {
mlkem: MlKem768PublicKey::try_from(&bytes[32..])
.ok()
.and_then(mlkem768::validate_public_key)
.ok_or(Error::InvalidPublicKey)?,
mlkem: {
let key = MlKem768PublicKey::try_from(&bytes[32..])
.map_err(|_| Error::InvalidPublicKey)?;
if !mlkem768::validate_public_key(&key) {
return Err(Error::InvalidPublicKey);
}
key
},
x25519: bytes[0..32]
.try_into()
.map_err(|_| Error::InvalidPublicKey)?,
Expand All @@ -241,10 +245,14 @@ pub struct XWingKemDraft02PublicKey {
impl XWingKemDraft02PublicKey {
pub fn decode(bytes: &[u8]) -> Result<Self, Error> {
Ok(Self {
pk_m: MlKem768PublicKey::try_from(&bytes[0..1184])
.ok()
.and_then(mlkem768::validate_public_key)
.ok_or(Error::InvalidPublicKey)?,
pk_m: {
let key = MlKem768PublicKey::try_from(&bytes[0..1184])
.map_err(|_| Error::InvalidPublicKey)?;
if !mlkem768::validate_public_key(&key) {
return Err(Error::InvalidPublicKey);
}
key
},
pk_x: bytes[1184..]
.try_into()
.map_err(|_| Error::InvalidPublicKey)?,
Expand Down Expand Up @@ -654,16 +662,22 @@ impl PublicKey {
.try_into()
.map(Self::P256)
.map_err(|_| Error::InvalidPublicKey),
Algorithm::MlKem512 => MlKem512PublicKey::try_from(bytes)
.ok()
.and_then(mlkem512::validate_public_key)
.map(Self::MlKem512)
.ok_or(Error::InvalidPublicKey),
Algorithm::MlKem768 => MlKem768PublicKey::try_from(bytes)
.ok()
.and_then(mlkem768::validate_public_key)
.map(Self::MlKem768)
.ok_or(Error::InvalidPublicKey),
Algorithm::MlKem512 => {
let key =
MlKem512PublicKey::try_from(bytes).map_err(|_| Error::InvalidPublicKey)?;
if !mlkem512::validate_public_key(&key) {
return Err(Error::InvalidPublicKey);
}
Ok(Self::MlKem512(key))
}
Algorithm::MlKem768 => {
let key =
MlKem768PublicKey::try_from(bytes).map_err(|_| Error::InvalidPublicKey)?;
if !mlkem768::validate_public_key(&key) {
return Err(Error::InvalidPublicKey);
}
Ok(Self::MlKem768(key))
}
Algorithm::X25519MlKem768Draft00 => {
X25519MlKem768Draft00PublicKey::decode(bytes).map(Self::X25519MlKem768Draft00)
}
Expand All @@ -678,11 +692,14 @@ impl PublicKey {
Algorithm::XWingKyberDraft02 => {
XWingKemDraft02PublicKey::decode(bytes).map(Self::XWingKyberDraft02)
}
Algorithm::MlKem1024 => MlKem1024PublicKey::try_from(bytes)
.ok()
.and_then(mlkem1024::validate_public_key)
.map(Self::MlKem1024)
.ok_or(Error::InvalidPublicKey),
Algorithm::MlKem1024 => {
let key =
MlKem1024PublicKey::try_from(bytes).map_err(|_| Error::InvalidPublicKey)?;
if !mlkem1024::validate_public_key(&key) {
return Err(Error::InvalidPublicKey);
}
Ok(Self::MlKem1024(key))
}
_ => Err(Error::UnsupportedAlgorithm),
}
}
Expand Down
2 changes: 1 addition & 1 deletion libcrux-kem/tests/ml_kem_wycheproof_early.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ macro_rules! impl_known_answer_test {
assert_eq!(my_shared_secret.as_ref(), expected_shared_secret);
} else {
if comment == "Public key not reduced" {
assert!($validate_pk(<$pk>::from(pk)).is_none());
assert!(!$validate_pk(&<$pk>::from(pk)));
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions libcrux-ml-kem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ exclude = [
bench = false # so libtest doesn't eat the arguments to criterion

[dependencies]
rand_core = { version = "0.6" }
rand = { version = "0.8", optional = true }
libcrux-platform = { version = "0.0.2-alpha.3", path = "../sys/platform" }
libcrux-sha3 = { version = "0.0.2-alpha.3", path = "../libcrux-sha3" }
libcrux-intrinsics = { version = "0.0.2-alpha.3", path = "../libcrux-intrinsics" }
Expand All @@ -34,7 +34,7 @@ hax-lib = { version = "0.1.0-alpha.1", git = "https://github.com/hacspec/hax/" }

[features]
# By default all variants and std are enabled.
default = ["std", "mlkem512", "mlkem768", "mlkem1024"]
default = ["std", "mlkem512", "mlkem768", "mlkem1024", "rand"]

# Hardware features can be force enabled.
# It is not recommended to use these. This crate performs CPU feature detection
Expand All @@ -56,6 +56,10 @@ kyber = []
# Code that is not yet verified
pre-verification = []

# APIs that sample their own randomness
rand = ["dep:rand"]

# std support
std = []

[dev-dependencies]
Expand Down
3 changes: 1 addition & 2 deletions libcrux-ml-kem/benches/ml-kem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use std::hint::black_box;
use std::time::Duration;

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

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

Expand Down
1 change: 1 addition & 0 deletions libcrux-ml-kem/src/hash_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ pub(crate) mod portable {
}

/// A SIMD256 implementation of [`Hash`] for AVX2
#[cfg(feature = "simd256")]
pub(crate) mod avx2 {
use super::*;
use libcrux_sha3::{
Expand Down
2 changes: 1 addition & 1 deletion libcrux-ml-kem/src/ind_cca/multiplexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use instantiations::avx2::{
#[cfg(feature = "simd128")]
use instantiations::neon::{
decapsulate as decapsulate_neon, encapsulate as encapsulate_neon,
generate_keypair as generate_keypair_neon, validate_public_key as validate_public_key_neon,
generate_keypair as generate_keypair_neon,
};

#[cfg(not(feature = "simd256"))]
Expand Down
Loading

0 comments on commit 482620d

Please sign in to comment.