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 widthsETA1/ETA2, compression bit-widthsDU/DV, byte-length constants for keys and ciphertexts. - Concrete buffer types —
PublicKeyBytes,SecretKeyBytes,CiphertextBytesare 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
- Downstream code can begin writing generic algorithms
targeting
fn foo<P: MlKemParams>(...)immediately, with the understanding that the body currently has to specialise internally. - It pins down the API contract for the parameter pack so the eventual algorithm refactor has a fixed target to land against.
- Const accessors —
<MlKem768 as MlKemParams>::PUBLIC_KEY_BYTESis now resolvable at compile time, useful for downstreamconst fnsize calculations.
Traits§
- MlKem
Params - 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
CiphertextBytesbuffer. - 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
PublicKeyBytesbuffer. - public_
key_ len - Length of the public encapsulation key for the parameter set
P, as aconst fnso it can be used in array dimensions insideconstcontexts. - secret_
key_ from_ slice - Validate a borrowed byte slice and copy it into the parameter-set’s
concrete
SecretKeyBytesbuffer. - 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
Pto keep call sites uniform.