Crate libcrux_ml_kem

Source
Expand description

§ML-KEM

This crate implements all three ML-KEM variants 512, 768, and 1024. It is formally verified using hax and F*.

Functions in this crate use CPU feature detection to pick the most efficient version on each platform. To use a specific version with your own feature detection use e.g. one of the following

  • mlkem768::avx2::generate_key_pair,
  • mlkem768::neon::generate_key_pair,
  • mlkem768::portable::generate_key_pair,

analogously for encapsulation and decapsulation.“

 use rand::{rngs::OsRng, TryRngCore};

 // Ensure you use good randomness.
 // It is not recommended to use OsRng directly!
 // Instead it is highly encouraged to use RNGs like NISTs DRBG to account for
 // bad system entropy.
 fn random_array<const L: usize>() -> [u8; L] {
     let mut rng = OsRng;
     let mut seed = [0; L];
     rng.try_fill_bytes(&mut seed).unwrap();
     seed
 }

 use libcrux_ml_kem::*;

 // This example uses ML-KEM 768. The other variants can be used the same way.

 // Generate a key pair.
 let key_pair = {
    let randomness = random_array();
    mlkem768::generate_key_pair(randomness)
 };

 // Encapsulating a shared secret to a public key.
 let (ciphertext, shared_secret) = {
    let randomness = random_array();
    mlkem768::encapsulate(key_pair.public_key(), randomness)
 };

 // Decapsulating a shared secret with a private key.
 let shared_secret_decapsulated = mlkem768::decapsulate(key_pair.private_key(), &ciphertext);

 assert_eq!(shared_secret_decapsulated, shared_secret);

§Features

By default, all ML-KEM parameter sets are enabled. If required, they are available individually under feature flags mlkem512, mlkem768, mlkem1024.

§Kyber Round 3

The kyber flag also gives access to an, as yet, unverified implementation of Kyber as submitted in Round 3 of the NIST PQ competition.

Modules§

kyber512kyber and mlkem512
Kyber 512 (NIST PQC Round 3)
kyber768kyber and mlkem768
Kyber 768 (NIST PQC Round 3)
kyber1024kyber and mlkem1024
Kyber 1024 (NIST PQC Round 3)
mlkem512mlkem512
ML-KEM 512
mlkem768mlkem768
ML-KEM 768
mlkem1024mlkem1024
ML-KEM 1024

Structs§

MlKemCiphertext
An ML-KEM Ciphertext
MlKemKeyPair
An ML-KEM key pair
MlKemPrivateKey
An ML-KEM Private key
MlKemPublicKey
An ML-KEM Public key

Constants§

ENCAPS_SEED_SIZE
Seed size for encapsulation
KEY_GENERATION_SEED_SIZE
Seed size for key generation
SHARED_SECRET_SIZE
The size of an ML-KEM shared secret.

Type Aliases§

MlKemSharedSecret
An ML-KEM shared secret.