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

Make verified ML-KEM available in libcrux-ml-kem #329

Merged
merged 62 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
7b5c5e4
Revert changes to Kyber (Round 3) for ML-KEM
jschneider-bensch Jun 18, 2024
5126d6a
Fix swapped private key decoding
jschneider-bensch Jun 18, 2024
11d7bb9
Pull out Kyber encaps
jschneider-bensch Jun 19, 2024
017ac5f
Make Kyber API available behind `kyber` feature
jschneider-bensch Jun 20, 2024
c7beed2
Format
jschneider-bensch Jun 20, 2024
0571d1f
Introduce `kyber` feature in `libcrux-kem`
jschneider-bensch Jun 20, 2024
283d99e
Format
jschneider-bensch Jun 20, 2024
4be326b
Include verified ML-KEM in `libcrux-ml-kem`
jschneider-bensch Jun 20, 2024
7f3e42e
WIP separation of verified API
jschneider-bensch Jun 20, 2024
66dca4d
Merge branch 'dev' into jonas/xyber768
franziskuskiefer Jun 21, 2024
e346e99
Integrating pre-verification and verified testing
jschneider-bensch Jun 21, 2024
cb11230
Integrating verified ML-KEM in `libcrux-kem`
jschneider-bensch Jun 21, 2024
688b7fb
Merge branch 'jonas/xyber768' into jonas/restore-verified-mlkem
jschneider-bensch Jun 21, 2024
cfaeac0
Format
jschneider-bensch Jun 21, 2024
d86006b
Test portable pre-verification ML-KEM 768
jschneider-bensch Jun 21, 2024
6b39b9e
Merge branch 'dev' into jonas/xyber768
franziskuskiefer Jun 24, 2024
8cdead9
fix C extraction
franziskuskiefer Jun 24, 2024
847136a
Fix typo
jschneider-bensch Jun 26, 2024
32af120
Give more accurate test names (`kyber...` to `mlkem...`)
jschneider-bensch Jun 26, 2024
11355da
Avoid unused warning when testing
jschneider-bensch Jun 26, 2024
0aa89eb
Use `Hasher` trait to select best KDF for Kyber
jschneider-bensch Jun 26, 2024
b4e4d13
Document differences between Kyber (Round 3) and ML-KEM
jschneider-bensch Jun 26, 2024
1d9e38d
Inline ML-KEM/Kyber variant differences
jschneider-bensch Jun 26, 2024
00e62c0
`kyber{512,768,1024}` top level modules
jschneider-bensch Jun 26, 2024
422bedb
Merge branch 'jonas/xyber768' of github.com:cryspen/libcrux into jona…
jschneider-bensch Jun 26, 2024
790eb06
Merge remote-tracking branch 'origin/jonas/xyber768-f' into jonas/xyb…
jschneider-bensch Jun 26, 2024
366c761
Fix `kyber` feature in `libcrux-kem`
jschneider-bensch Jun 26, 2024
d0740e3
Rename NIST KAT files for ML-KEM
jschneider-bensch Jun 26, 2024
2af99c3
Include NIST KATs for all Kyber parameter sets
jschneider-bensch Jun 26, 2024
e5b264c
Document single Kyber 768 KAT from boringssl
jschneider-bensch Jun 26, 2024
608fe19
Format
jschneider-bensch Jun 26, 2024
1233e95
Test Kyber on CI
jschneider-bensch Jun 26, 2024
7942c6f
Merge branch 'jonas/xyber768' into jonas/restore-verified-mlkem
jschneider-bensch Jun 26, 2024
f2f27e5
Fix insufficient feature gating
jschneider-bensch Jun 26, 2024
30cfb4c
Merge branch 'dev' into jonas/restore-verified-mlkem
jschneider-bensch Jun 26, 2024
a9e6489
Get rid of various warnings
jschneider-bensch Jun 26, 2024
956382d
Format
jschneider-bensch Jun 26, 2024
6d2fda3
Remove commented `use` declarations
jschneider-bensch Jun 26, 2024
936b0b9
Merge branch 'dev' into jonas/restore-verified-mlkem
jschneider-bensch Jun 27, 2024
906a298
Fix `unused_mut` warning
jschneider-bensch Jun 27, 2024
7f349f1
Feature gating helper macros
jschneider-bensch Jun 27, 2024
19040cd
Fix hax extraction
jschneider-bensch Jun 27, 2024
dbde61d
Feature flag documentation
jschneider-bensch Jun 27, 2024
24297ff
Fix mistaken feature guards
jschneider-bensch Jun 27, 2024
3877346
Remove commented code
jschneider-bensch Jun 27, 2024
5271f75
`size` -> `len`
jschneider-bensch Jun 27, 2024
96ab20d
Drop explicit dependency on `hax-lib-macros`
jschneider-bensch Jun 27, 2024
4b1d95a
Hide helper macro docs
jschneider-bensch Jun 27, 2024
bd14f8d
Formatting
jschneider-bensch Jun 27, 2024
c7ca638
Call out the `kyber` feature more prominently
jschneider-bensch Jun 27, 2024
12d7693
Feature gate tests etc so `--no-std-features` tests
jschneider-bensch Jun 27, 2024
c391c74
cleanup; make features work
franziskuskiefer Jun 28, 2024
fc84415
more cleanup and docs
franziskuskiefer Jun 28, 2024
193cc33
update hax extraction
franziskuskiefer Jun 28, 2024
d19b897
fixup x64 and hax extract
franziskuskiefer Jun 28, 2024
a3ce343
update C extraction
franziskuskiefer Jun 28, 2024
a576e70
update header only C code
franziskuskiefer Jun 28, 2024
9cd4bb5
Merge branch 'dev' into jonas/restore-verified-mlkem
franziskuskiefer Jun 28, 2024
65fd9fd
update C code
franziskuskiefer Jun 28, 2024
9c15eda
install cargo hack on ci
franziskuskiefer Jun 28, 2024
58c79cd
ci
franziskuskiefer Jun 28, 2024
43cab0a
more updates
franziskuskiefer Jun 28, 2024
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
1 change: 1 addition & 0 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-kem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ rand = { version = "0.8" }
[features]
tests = [] # Expose functions for testing.
kyber = ["libcrux-ml-kem/kyber"]
pre-verification = ["libcrux-ml-kem/pre-verification"]



[dev-dependencies]
libcrux-kem = { version = "0.0.2-pre.2", path = "./", features = ["tests"] }
Expand Down
28 changes: 24 additions & 4 deletions libcrux-kem/src/kem.rs
franziskuskiefer marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ impl PrivateKey {
.map_err(|_| Error::InvalidPrivateKey)
.map(Self::MlKem768),
Algorithm::X25519MlKem768Draft00 => {
let key: [u8; MlKem768PrivateKey::len() + 32] =
let key: [u8; MlKem768PrivateKey::size() + 32] =
bytes.try_into().map_err(|_| Error::InvalidPrivateKey)?;
let (xsk, ksk) = key.split_at(32);
Ok(Self::X25519MlKem768Draft00(
Expand Down Expand Up @@ -790,7 +790,7 @@ impl Ct {
.map_err(|_| Error::InvalidCiphertext)
.map(Self::MlKem768),
Algorithm::X25519MlKem768Draft00 => {
let key: [u8; MlKem768Ciphertext::len() + 32] =
let key: [u8; MlKem768Ciphertext::size() + 32] =
bytes.try_into().map_err(|_| Error::InvalidCiphertext)?;
let (xct, kct) = key.split_at(32);
Ok(Self::X25519MlKem768Draft00(
Expand All @@ -799,9 +799,9 @@ impl Ct {
))
}
Algorithm::XWingKemDraft02 => {
let key: [u8; MlKem768Ciphertext::len() + 32] =
let key: [u8; MlKem768Ciphertext::size() + 32] =
bytes.try_into().map_err(|_| Error::InvalidCiphertext)?;
let (ct_m, ct_x) = key.split_at(MlKem768Ciphertext::len());
let (ct_m, ct_x) = key.split_at(MlKem768Ciphertext::size());
Ok(Self::XWingKemDraft02(
ct_m.try_into().map_err(|_| Error::InvalidCiphertext)?,
ct_x.try_into().map_err(|_| Error::InvalidCiphertext)?,
Expand All @@ -818,6 +818,26 @@ impl Ct {
))
}
#[cfg(feature = "kyber")]
Algorithm::XWingKyberDraft02 => {
let key: [u8; MlKem768Ciphertext::len() + 32] =
bytes.try_into().map_err(|_| Error::InvalidCiphertext)?;
let (ct_m, ct_x) = key.split_at(MlKem768Ciphertext::size());
Ok(Self::XWingKyberDraft02(
ct_m.try_into().map_err(|_| Error::InvalidCiphertext)?,
ct_x.try_into().map_err(|_| Error::InvalidCiphertext)?,
))
}
#[cfg(feature = "kyber")]
Algorithm::X25519Kyber768Draft00 => {
let key: [u8; MlKem768Ciphertext::len() + 32] =
bytes.try_into().map_err(|_| Error::InvalidCiphertext)?;
let (xct, kct) = key.split_at(32);
Ok(Self::X25519Kyber768Draft00(
kct.try_into().map_err(|_| Error::InvalidCiphertext)?,
xct.try_into().map_err(|_| Error::InvalidCiphertext)?,
))
}
#[cfg(feature = "kyber")]
Algorithm::XWingKyberDraft02 => {
let key: [u8; MlKem768Ciphertext::len() + 32] =
bytes.try_into().map_err(|_| Error::InvalidCiphertext)?;
Expand Down
2 changes: 2 additions & 0 deletions libcrux-ml-kem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ libcrux-intrinsics = { version = "0.0.2-pre.2", path = "../libcrux-intrinsics" }
# The hax config is set by the hax toolchain.
[target.'cfg(hax)'.dependencies]
hax-lib = { version = "0.1.0-pre.1", git = "https://github.com/hacspec/hax/" }
hax-lib-macros = { version = "0.1.0-pre.1", git = "https://github.com/hacspec/hax/" }
jschneider-bensch marked this conversation as resolved.
Show resolved Hide resolved

[features]
default = ["std", "mlkem512", "mlkem768", "mlkem1024"]
Expand All @@ -37,6 +38,7 @@ mlkem768 = []
mlkem1024 = []
std = []
kyber = []
pre-verification = []

[dev-dependencies]
rand = { version = "0.8" }
Expand Down
30 changes: 30 additions & 0 deletions libcrux-ml-kem/src/cfg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// Macro to simplify feature gating of verified code
macro_rules! cfg_verified {
($($item:item)*) => {
$(
#[cfg(not(feature = "pre-verification"))]
$item
)*
}
}

/// Macro to simplify `pre-verification` feature gating
macro_rules! cfg_pre_verification {
($($item:item)*) => {
$(
#[cfg(feature = "pre-verification")]
$item
)*
}
}

/// Macro to simplify `kyber` feature gating
#[cfg(feature = "pre-verification")]
macro_rules! cfg_kyber {
($($item:item)*) => {
$(
#[cfg(feature = "kyber")]
$item
)*
}
}
37 changes: 37 additions & 0 deletions libcrux-ml-kem/src/kem.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// hacspec code: don't let clippy touch it.
#[allow(clippy::all)]
pub mod kyber;

// TODO: These functions are currently exposed simply in order to make NIST KAT
// testing possible without an implementation of the NIST AES-CTR DRBG. Remove them
// (and change the visibility of the exported functions to pub(crate)) the
// moment we have an implementation of one. This is tracked by:
// https://github.com/cryspen/libcrux/issues/36
#[cfg(feature = "tests")]
pub mod deterministic {
pub use super::kyber::kyber1024::decapsulate as kyber1024_decapsulate_derand;
pub use super::kyber::kyber1024::encapsulate as kyber1024_encapsulate_derand;
pub use super::kyber::kyber1024::generate_key_pair as kyber1024_generate_keypair_derand;
pub use super::kyber::kyber512::decapsulate as kyber512_decapsulate_derand;
pub use super::kyber::kyber512::encapsulate as kyber512_encapsulate_derand;
pub use super::kyber::kyber512::generate_key_pair as kyber512_generate_keypair_derand;
pub use super::kyber::kyber768::decapsulate as kyber768_decapsulate_derand;
pub use super::kyber::kyber768::encapsulate as kyber768_encapsulate_derand;
pub use super::kyber::kyber768::generate_key_pair as kyber768_generate_keypair_derand;
}

// use self::kyber::MlKemSharedSecret;
jschneider-bensch marked this conversation as resolved.
Show resolved Hide resolved
// use self::kyber::{kyber1024, kyber512, kyber768};
// pub use kyber::{
// kyber1024::{MlKem1024Ciphertext, MlKem1024PrivateKey, MlKem1024PublicKey},
// kyber512::{MlKem512Ciphertext, MlKem512PrivateKey, MlKem512PublicKey},
// kyber768::{MlKem768Ciphertext, MlKem768PrivateKey, MlKem768PublicKey},
// MlKemCiphertext, MlKemKeyPair,
// };

#[cfg(feature = "tests")]
pub use kyber::{
kyber1024::validate_public_key as ml_kem1024_validate_public_key,
kyber512::validate_public_key as ml_kem512_validate_public_key,
kyber768::validate_public_key as ml_kem768_validate_public_key,
};
Loading
Loading