Skip to main content

Module paramsets

Module paramsets 

Source
Expand description

Parameter-pack trait unifying ML-KEM-512 / 768 / 1024 — foundation for the const-generic refactor tracked as #130b. Parameter-pack trait for the three FIPS 203 ML-KEM parameter sets.

§Motivation

The existing reference backend in crate::reference selects its parameter set at compile time via Cargo feature gates (kyber512, kyber768, kyber1024). That means a single build can support exactly one parameter set: an application that wants to negotiate between ML-KEM-512 and ML-KEM-1024 at runtime has to either pick one or link two kyberlib copies.

This module introduces MlKemParams — a sealed trait whose associated constants and associated types pin down everything a generic FIPS 203 algorithm needs:

  • Numeric parameters — module rank K, noise widths ETA1/ETA2, compression bit-widths DU/DV, byte-length constants for keys and ciphertexts.
  • Concrete buffer types — PublicKeyBytes, SecretKeyBytes, CiphertextBytes are associated types that each impl instantiates as the correct [u8; N] length for the spec. This sidesteps the stable-Rust restriction that prevents [u8; K * Self::POLY_BYTES] in generic contexts.

§Implementation strategy

Three marker types MlKem512, MlKem768, MlKem1024 implement MlKemParams. Algorithm code that takes P: MlKemParams then has access to P::K, P::ETA1, etc. as const values and uses P::PublicKeyBytes as the concrete byte array type for keys.

§Migration status (v0.0.7)

This module ships the trait + impls so downstream code can be written against the unified parameter surface, but the algorithm code in crate::reference has NOT yet been ported to consume the trait. The three KemCore impls remain Cargo-feature-gated for now. The mechanical port of the reference backend — indcpa_keypair, indcpa_enc, indcpa_dec, crypto_kem_keypair, crypto_kem_enc, crypto_kem_dec, plus the polynomial-arithmetic primitives in poly.rs, polyvec.rs, cbd.rs — is a multi-day focused PR tracked as #130b.

§Why the trait lands now

  1. Downstream code can begin writing generic algorithms targeting fn foo<P: MlKemParams>(...) immediately, with the understanding that the body currently has to specialise internally.
  2. It pins down the API contract for the parameter pack so the eventual algorithm refactor has a fixed target to land against.
  3. Const accessors — <MlKem768 as MlKemParams>::PUBLIC_KEY_BYTES is now resolvable at compile time, useful for downstream const fn size calculations.

Traits§

MlKemParams
FIPS 203 parameter pack — every constant and type the algorithm needs to specialise to one of the three ML-KEM variants.

Functions§

ciphertext_from_slice
Validate a borrowed byte slice and copy it into the parameter-set’s concrete CiphertextBytes buffer.
ciphertext_len
Length of the ciphertext for parameter set P.
public_key_from_slice
Validate a borrowed byte slice and copy it into the parameter-set’s concrete PublicKeyBytes buffer.
public_key_len
Length of the public encapsulation key for the parameter set P, as a const fn so it can be used in array dimensions inside const contexts.
secret_key_from_slice
Validate a borrowed byte slice and copy it into the parameter-set’s concrete SecretKeyBytes buffer.
secret_key_len
Length of the decapsulation secret key for parameter set P.
shared_secret_len
Length of the shared secret. Always 32 bytes per FIPS 203 §6, but exposed via P to keep call sites uniform.