Skip to main content

MlKem

Struct MlKem 

Source
pub struct MlKem<P: Params>(/* private fields */);
Expand description

Main ML-KEM interface, generic over a Params parameter set.

This is the primary API for ML-KEM key encapsulation. It wraps the lower-level functions in kem (hazmat tier) and provides a convenient, type-safe interface parameterized by security level.

§Type parameters

§Example

use quantica::ml_kem::*;

let mut rng = OsRng;
let (ek, dk) = MlKem::<MlKem768>::keygen(&mut rng).unwrap();
let (ss, ct) = MlKem::<MlKem768>::encaps(&ek, &mut rng).unwrap();
let ss2 = MlKem::<MlKem768>::decaps(&dk, &ct, &mut rng).unwrap();
assert_eq!(ss, ss2);

Implementations§

Source§

impl<P: Params> MlKem<P>

Source

pub fn keygen( rng: &mut impl CryptoRng, ) -> Result<(EncapsulationKey<P>, DecapsulationKey<P>), MlKemError>

Generate an ML-KEM key pair.

Produces an encapsulation key (public) and a decapsulation key (private) using randomness from the provided RNG. Implements Algorithm 19 of FIPS 203.

The decapsulation key includes an integrity hash H(ek) that is verified during decapsulation to detect storage corruption (DFA protection).

§Arguments
  • rng - A cryptographic random number generator implementing CryptoRng.
§Returns

A tuple (encapsulation_key, decapsulation_key) of typed wrappers. The decapsulation key auto-zeroizes on Drop.

§Errors

Returns MlKemError::RngFailure if the RNG fails to produce bytes.

§Example
use quantica::ml_kem::*;
let mut rng = OsRng;
let (ek, dk) = MlKem::<MlKem768>::keygen(&mut rng).unwrap();
assert_eq!(ek.len(), MlKem768::EK_LEN);
assert_eq!(dk.len(), MlKem768::DK_LEN);
Source

pub fn encaps( ek: &EncapsulationKey<P>, rng: &mut impl CryptoRng, ) -> Result<(SharedSecret, Ciphertext<P>), MlKemError>

Encapsulate a shared secret against an encapsulation key.

Given a public encapsulation key, generates a fresh 32-byte shared secret and the corresponding ciphertext. Implements Algorithm 20 of FIPS 203 with input validation (length and modulus checks).

§Arguments
  • ek - The encapsulation (public) key, exactly Params::EK_LEN bytes.
  • rng - A cryptographic random number generator implementing CryptoRng.
§Returns

A tuple (shared_secret, ciphertext) where the shared secret is 32 bytes.

§Errors
§Example
use quantica::ml_kem::*;
let mut rng = OsRng;
let (ek, _dk) = MlKem::<MlKem768>::keygen(&mut rng).unwrap();
let (shared_secret, ciphertext) = MlKem::<MlKem768>::encaps(&ek, &mut rng).unwrap();
Source

pub fn decaps( dk: &DecapsulationKey<P>, ct: &Ciphertext<P>, rng: &mut impl CryptoRng, ) -> Result<SharedSecret, MlKemError>

Decapsulate a ciphertext with full DFA protection.

Recovers the 32-byte shared secret from a ciphertext using the decapsulation (private) key. Implements Algorithm 21 of FIPS 203 with two additional DFA countermeasures:

  1. dk integrity check – verifies H(ek) stored in dk to detect fault injection on key material in memory.
  2. Double computation – runs the internal decapsulation twice and compares results. A single-fault attack can only corrupt one execution, so divergent results indicate fault injection.

Recommended for embedded and high-security contexts where physical fault attacks are in the threat model.

§Arguments
  • dk - The decapsulation (private) key, exactly Params::DK_LEN bytes.
  • ct - The ciphertext, exactly Params::CT_LEN bytes.
  • rng - A cryptographic random number generator (reserved for future use).
§Returns

The 32-byte shared secret.

§Errors
§Example
use quantica::ml_kem::*;
let mut rng = OsRng;
let (ek, dk) = MlKem::<MlKem768>::keygen(&mut rng).unwrap();
let (ss, ct) = MlKem::<MlKem768>::encaps(&ek, &mut rng).unwrap();
let ss2 = MlKem::<MlKem768>::decaps(&dk, &ct, &mut rng).unwrap();
assert_eq!(ss, ss2);
Source

pub fn decaps_fast( dk: &DecapsulationKey<P>, ct: &Ciphertext<P>, ) -> Result<SharedSecret, MlKemError>

Decapsulate a ciphertext without double computation (faster variant).

Same as MlKem::decaps but omits the double-computation DFA countermeasure, making it roughly twice as fast. The H(ek) integrity check on the decapsulation key is still performed.

Use this for software-only contexts where physical fault injection is not part of the threat model.

§Arguments
§Returns

The 32-byte shared secret.

§Errors

Auto Trait Implementations§

§

impl<P> Freeze for MlKem<P>

§

impl<P> RefUnwindSafe for MlKem<P>
where P: RefUnwindSafe,

§

impl<P> Send for MlKem<P>
where P: Send,

§

impl<P> Sync for MlKem<P>
where P: Sync,

§

impl<P> Unpin for MlKem<P>
where P: Unpin,

§

impl<P> UnsafeUnpin for MlKem<P>

§

impl<P> UnwindSafe for MlKem<P>
where P: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.