ML-KEM

This crate implements all three ML-KEM (FIPS 203) variants 512, 768, and 1024. For each algorithm, it inlcudes a portable Rust implementation, as well as SIMD implementations for Intel AVX2 and AArch64 Neon platforms.

Module structure

The modules

implement the three parameter sets for ML-KEM defined in FIPS 203.

Each module provides the following API:

Portable and Optimized Implementations

The crate provides portable, as well as AVX2- and NEON-optimized implementations of the above API. By default, the crate's build.rs will include the portable implementation and one of the optimized implementations in the build, according to the value of CARGO_CFG_TARGET_ARCH.

In addition, the above functions perform CPU feature detection at runtime to ensure the most efficient implementation for the given platform is selected.

It is recommended to rely on the automatic feature detection, but specific builds can be forced by setting environment variables, LIBCRUX_ENABLE_SIMD128=1 or LIBCRUX_ENABLE_SIMD256=1.

Usage example:

 #[cfg(feature = "mlkem768")]
 {
     use rand::{rngs::SysRng, TryRng};

     // 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 = SysRng;
         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);
 }

Unpacked APIs

The default KEM API described above operates on serialized keys, i.e. encapsulate will take a serialized ML-KEM public key as input and decapsulate will take a serialized ML-KEM private key as input, and these must be validated before use with validate_public_key and validate_private_key for FIPS 203 compliance.

In addition, in each parameter set module, (e.g. mlkem768) the crate provides an API for working with "unpacked" keys, which have already been deserialized. For some applications it may thus be advantageous to validate key material once, then deserialized into unpacked representation once, and to use the the already validated and deserialized form from then on.

The unpacked APIs are platform dependent, so they can be found in submodules mlkem768::portable::unpacked, mlkem768::avx2::unpacked, mlkem768::neon::unpacked, depending on which of these platform specific modules are part of the build in question.

PQ Code Package Common APIs

As part of the PQ code package project (PQCP), the crate also implements the common API on packed keys outlined in that project for each parameter set in the respective sub-module pqcp:

Additionally, the above API is available for each platform-dependent unpacked key type, with function names suffixed by _struct and the following functions are provided for working with unpacked keys:

The API is available through feature pqcp.

Crate Features

The crate provides the following features.

Default features:

Additional features:

Secret Independence

As outlined in the description of the check-secret-independence feature above, we leverage the Rust type system to ensure that secret values are not used in operations that are known to have data-dependent execution time. While the implementation of constant time operations is best-effort, as there are no guarantees from the compiler, we follow established constant-time patterns and validate constant-time code via inspection of the generated assembly instructions.

Verification

verified

The portable and AVX2 code for field arithmetic, NTT polynomial arithmetic, serialization, and the generic code for high-level algorithms is formally verified using hax and F*.

Please refer to this file for detail on the verification of this crate.