package mlkem

  1. Overview
  2. Docs

Module Mlkem.Mlkem1024Source

ML-KEM-1024, as standardized by FIPS 203.

Keys, ciphertexts, and shared secrets have distinct abstract types. All parsing functions reject inputs of the wrong length. Encapsulation-key parsing also performs the FIPS 203 modulus check.

The caller supplies randomness so that this library stays portable across Unix, MirageOS unikernels, and js_of_ocaml. The callback must return exactly the requested number of cryptographically secure random bytes.

Sourcetype error =
  1. | Invalid_length of {
    1. what : string;
    2. expected : int;
    3. actual : int;
    }
  2. | Invalid_encoding of string
Sourceval pp_error : Format.formatter -> error -> unit

pp_error formats a parsing error for humans.

Sourcetype encapsulation_key
Sourcetype decapsulation_key
Sourcetype ciphertext
Sourcetype shared_secret
Sourceval seed_size : int

The size of a seed-encoded decapsulation key: 64 bytes (d || z).

Sourceval encapsulation_key_size : int

The size of an encoded encapsulation key: 1568 bytes.

Sourceval ciphertext_size : int

The size of a ciphertext: 1568 bytes.

Sourceval shared_secret_size : int

The size of a shared secret: 32 bytes.

Sourceval generate : random:(int -> string) -> unit -> decapsulation_key * encapsulation_key

generate ~random () creates a key pair from 64 bytes obtained from random.

Sourceval decapsulation_key_of_seed : string -> (decapsulation_key, error) result

decapsulation_key_of_seed seed expands the canonical 64-byte d || z representation.

Sourceval decapsulation_key_to_seed : decapsulation_key -> string

decapsulation_key_to_seed key returns a fresh 64-byte seed encoding. The result is secret key material.

Sourceval encapsulation_key_of_decapsulation_key : decapsulation_key -> encapsulation_key
Sourceval encapsulation_key_of_octets : string -> (encapsulation_key, error) result
Sourceval encapsulation_key_to_octets : encapsulation_key -> string
Sourceval ciphertext_of_octets : string -> (ciphertext, error) result
Sourceval ciphertext_to_octets : ciphertext -> string
Sourceval shared_secret_to_octets : shared_secret -> string

shared_secret_to_octets secret returns a fresh 32-byte string.

Sourceval encapsulate : random:(int -> string) -> encapsulation_key -> ciphertext * shared_secret

encapsulate ~random key obtains 32 bytes from random, then produces a ciphertext and shared secret.

decapsulate key ciphertext always returns a shared secret. Invalid same-length ciphertexts are handled with FIPS 203 implicit rejection.