Skip to main content

kyberlib/
ml_kem.rs

1// Copyright © 2026 kyberlib. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3
4//! FIPS 203 ML-KEM type-state API.
5//!
6//! New surface introduced in v0.0.7 (#130). Parameter set is a *type*,
7//! not a Cargo feature: each consumer picks [`MlKem512`], [`MlKem768`],
8//! or [`MlKem1024`] at the call site. Secrets and public material
9//! travel as distinct types with the right Drop / Zeroize semantics:
10//!
11//! ```text
12//!     KemCore                       — trait, sealed
13//!     ├── MlKem512        ✅ implemented
14//!     ├── MlKem768        ✅ implemented (default)
15//!     └── MlKem1024       ✅ implemented
16//!
17//!     EncapsulationKey<P>           — `Copy`-safe public bytes
18//!     DecapsulationKey<P>           — `!Copy`, `ZeroizeOnDrop`, redacted `Debug`
19//!     Ciphertext<P>                 — opaque, fixed-size byte wrapper
20//!     SharedSecret                  — 32-byte `ZeroizeOnDrop` secret
21//! ```
22//!
23//! As of Phase 3e of #130b (commit `7…` — search `git log --grep
24//! "Phase 3e"`), all three parameter sets implement [`KemCore`]
25//! unconditionally. The default build supports `MlKem512`,
26//! `MlKem768`, and `MlKem1024` concurrently — pick the right one
27//! at the call site:
28//!
29//! ```
30//! # fn main() -> Result<(), kyberlib::KyberLibError> {
31//! use kyberlib::{KemCore, MlKem512, MlKem768, MlKem1024};
32//! let mut rng = rand::thread_rng();
33//! let (dk_512, ek_512)  = MlKem512::generate(&mut rng)?;
34//! let (dk_768, ek_768)  = MlKem768::generate(&mut rng)?;
35//! let (dk_1024, ek_1024) = MlKem1024::generate(&mut rng)?;
36//! # let _ = (dk_512, ek_512, dk_768, ek_768, dk_1024, ek_1024);
37//! # Ok(()) }
38//! ```
39//!
40//! ## Migration from the v0.0.6 surface
41//!
42//! The free functions [`keypair`],
43//! [`encapsulate`], [`decapsulate`]
44//! and the [`Keypair`] struct from the old surface are
45//! retained as `#[deprecated]` shims for `MlKem768` only. They'll be
46//! removed in a future release.
47//!
48//! ## ACVP conformance
49//!
50//! `MlKem768` is byte-validated against 60/60 NIST ACVP test vectors
51//! (see [`tests/test_acvp.rs`][acvp]). `MlKem512` and `MlKem1024`
52//! conformance is verified structurally — the generic algorithm
53//! pipeline is byte-identical to the cfg-gated reference under each
54//! parameter set's feature (#130b commits a77b94b through 3819f7a).
55//! The ACVP harness is wired to all three under #130c.
56//!
57//! [acvp]: ../../tests/test_acvp.rs
58
59#[cfg(doc)]
60#[allow(deprecated)]
61use crate::{decapsulate, encapsulate, keypair, Keypair};
62use crate::{
63    error::KyberLibError, KYBER_CIPHERTEXT_BYTES,
64    KYBER_PUBLIC_KEY_BYTES, KYBER_SECRET_KEY_BYTES,
65    KYBER_SHARED_SECRET_BYTES,
66};
67use core::fmt;
68use rand_core::{CryptoRng, RngCore};
69use zeroize::{Zeroize, ZeroizeOnDrop};
70
71use crate::backend::{
72    backend_decapsulate, backend_encapsulate, backend_keygen,
73    keypair_from_seed,
74};
75
76mod sealed {
77    /// Sealed trait — only this crate may implement [`super::KemCore`].
78    pub trait Sealed {}
79}
80
81// ---------------------------------------------------------------- KemCore
82
83/// FIPS 203 ML-KEM parameter-set marker trait.
84///
85/// Implemented by the zero-sized marker types [`MlKem512`], [`MlKem768`],
86/// [`MlKem1024`]. Sealed — third-party crates cannot add new
87/// implementations.
88pub trait KemCore: sealed::Sealed + Sized {
89    /// Encapsulation key (public side).
90    type EncapsulationKey;
91    /// Decapsulation key (secret side). Zeroized on drop.
92    type DecapsulationKey;
93    /// Ciphertext.
94    type Ciphertext;
95
96    /// Stable algorithm identifier per the IETF LAMPS draft —
97    /// `"ML-KEM-512"`, `"ML-KEM-768"`, or `"ML-KEM-1024"`.
98    const ALGORITHM_ID: &'static str;
99
100    /// Dotted-decimal object identifier per
101    /// `2.16.840.1.101.3.4.4.{1,2,3}`.
102    const OID: &'static str;
103
104    /// Generate a fresh decapsulation / encapsulation key pair.
105    ///
106    /// # Errors
107    ///
108    /// Returns [`KyberLibError::RandomBytesGeneration`] if the supplied
109    /// RNG fails to produce randomness. All other errors are
110    /// internal-invariant violations and should not occur with a
111    /// well-behaved RNG.
112    fn generate<R: RngCore + CryptoRng>(
113        rng: &mut R,
114    ) -> Result<
115        (Self::DecapsulationKey, Self::EncapsulationKey),
116        KyberLibError,
117    >;
118
119    /// Derive a key pair from the seed `(d, z)`: FIPS 203 Algorithm 16,
120    /// `ML-KEM.KeyGen_internal(d, z)`.
121    ///
122    /// `d ‖ z` is the seed form of an ML-KEM private key: keeping these
123    /// 64 bytes is enough to rebuild the key pair (for example, X-Wing
124    /// stores a 32-byte seed and expands it into `d` and `z`). Both
125    /// halves must come from an approved RNG and stay secret.
126    ///
127    /// # Errors
128    ///
129    /// None in practice: the seeded path draws no randomness. The
130    /// `Result` matches [`KemCore::generate`].
131    fn generate_from_seed(
132        d: &[u8; 32],
133        z: &[u8; 32],
134    ) -> Result<
135        (Self::DecapsulationKey, Self::EncapsulationKey),
136        KyberLibError,
137    >;
138}
139
140// ---------------------------------------------------------------- markers
141
142/// ML-KEM-512 parameter set marker. NIST security category 1 (≈ AES-128).
143///
144/// Module rank `k = 2`. Public key 800 B, secret key 1632 B,
145/// ciphertext 768 B, shared secret 32 B.
146///
147/// [`KemCore`] is implemented unconditionally. Works in any build —
148/// no `--features kyber512` needed. The default build supports
149/// `MlKem512`, [`MlKem768`], and [`MlKem1024`] concurrently (Phase 3e
150/// of #130b — see `crates/kyberlib/src/paramsets.rs`).
151#[derive(Clone, Copy, Debug, Eq, PartialEq)]
152#[non_exhaustive]
153pub struct MlKem512;
154impl sealed::Sealed for MlKem512 {}
155
156impl MlKem512 {
157    /// Module rank.
158    pub const K: usize = 2;
159    /// Public encapsulation key length in bytes.
160    pub const PUBLIC_KEY_BYTES: usize = 800;
161    /// Private decapsulation key length in bytes.
162    pub const SECRET_KEY_BYTES: usize = 1632;
163    /// Ciphertext length in bytes.
164    pub const CIPHERTEXT_BYTES: usize = 768;
165    /// Shared-secret length in bytes.
166    pub const SHARED_SECRET_BYTES: usize = 32;
167    /// Stable algorithm identifier per the IETF LAMPS draft.
168    pub const ALGORITHM_ID: &'static str = "ML-KEM-512";
169    /// Object identifier — `2.16.840.1.101.3.4.4.1`.
170    pub const OID: &'static str = crate::oid::ML_KEM_512;
171}
172
173/// ML-KEM-768 parameter set marker. NIST security category 3 (≈ AES-192).
174/// Default for CNSA 2.0 hybrid TLS deployments.
175///
176/// Module rank `k = 3`. Public key 1184 B, secret key 2400 B,
177/// ciphertext 1088 B, shared secret 32 B.
178#[derive(Clone, Copy, Debug, Eq, PartialEq)]
179#[non_exhaustive]
180pub struct MlKem768;
181impl sealed::Sealed for MlKem768 {}
182
183impl MlKem768 {
184    /// Module rank.
185    pub const K: usize = 3;
186    /// Public encapsulation key length in bytes.
187    pub const PUBLIC_KEY_BYTES: usize = 1184;
188    /// Private decapsulation key length in bytes.
189    pub const SECRET_KEY_BYTES: usize = 2400;
190    /// Ciphertext length in bytes.
191    pub const CIPHERTEXT_BYTES: usize = 1088;
192    /// Shared-secret length in bytes.
193    pub const SHARED_SECRET_BYTES: usize = 32;
194    /// Stable algorithm identifier per the IETF LAMPS draft.
195    pub const ALGORITHM_ID: &'static str = "ML-KEM-768";
196    /// Object identifier — `2.16.840.1.101.3.4.4.2`.
197    pub const OID: &'static str = crate::oid::ML_KEM_768;
198}
199
200/// ML-KEM-1024 parameter set marker. NIST security category 5 (≈ AES-256).
201/// Required by CNSA 2.0 for NSS by 1 Jan 2027.
202///
203/// Module rank `k = 4`. Public key 1568 B, secret key 3168 B,
204/// ciphertext 1568 B, shared secret 32 B.
205///
206/// [`KemCore`] is implemented unconditionally — works alongside
207/// [`MlKem512`] and [`MlKem768`] in any build.
208#[derive(Clone, Copy, Debug, Eq, PartialEq)]
209#[non_exhaustive]
210pub struct MlKem1024;
211impl sealed::Sealed for MlKem1024 {}
212
213impl MlKem1024 {
214    /// Module rank.
215    pub const K: usize = 4;
216    /// Public encapsulation key length in bytes.
217    pub const PUBLIC_KEY_BYTES: usize = 1568;
218    /// Private decapsulation key length in bytes.
219    pub const SECRET_KEY_BYTES: usize = 3168;
220    /// Ciphertext length in bytes.
221    pub const CIPHERTEXT_BYTES: usize = 1568;
222    /// Shared-secret length in bytes.
223    pub const SHARED_SECRET_BYTES: usize = 32;
224    /// Stable algorithm identifier per the IETF LAMPS draft.
225    pub const ALGORITHM_ID: &'static str = "ML-KEM-1024";
226    /// Object identifier — `2.16.840.1.101.3.4.4.3`.
227    pub const OID: &'static str = crate::oid::ML_KEM_1024;
228}
229
230// ---------------------------------------------------------------- ML-KEM-512 + 1024 typed wrappers
231
232#[path = "ml_kem_sized.rs"]
233mod sized;
234pub use sized::*;
235
236// ---------------------------------------------------------------- shared secret
237
238/// 32-byte shared secret. Zeroized on drop.
239///
240/// Construct via [`KemCore::generate`] →
241/// `EncapsulationKey::encapsulate` → `DecapsulationKey::decapsulate`;
242/// never directly by the consumer.
243#[derive(Clone, Zeroize, ZeroizeOnDrop)]
244#[non_exhaustive]
245pub struct SharedSecret([u8; KYBER_SHARED_SECRET_BYTES]);
246
247impl SharedSecret {
248    /// Wrap bytes produced by a backend in `crate::backend`.
249    pub(crate) const fn from_backend(
250        bytes: [u8; KYBER_SHARED_SECRET_BYTES],
251    ) -> Self {
252        Self(bytes)
253    }
254
255    /// Borrow the raw 32 bytes. Use sparingly — every borrow expands
256    /// the surface that needs to handle the secret carefully.
257    pub fn as_bytes(&self) -> &[u8; KYBER_SHARED_SECRET_BYTES] {
258        &self.0
259    }
260}
261
262impl fmt::Debug for SharedSecret {
263    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
264        f.write_str("SharedSecret([REDACTED 32 bytes])")
265    }
266}
267
268impl PartialEq for SharedSecret {
269    fn eq(&self, other: &Self) -> bool {
270        // Constant-time-ish (memcmp loops in stdlib are fixed-length).
271        // For provable CT equality on secrets, use `subtle::ConstantTimeEq`.
272        self.0 == other.0
273    }
274}
275
276impl Eq for SharedSecret {}
277
278// ---------------------------------------------------------------- ML-KEM-768
279
280/// ML-KEM-768 encapsulation key (public). 1184 bytes.
281#[derive(Clone, Copy, Eq, PartialEq)]
282#[non_exhaustive]
283pub struct MlKem768EncapKey([u8; KYBER_PUBLIC_KEY_BYTES]);
284
285impl MlKem768EncapKey {
286    /// Construct from raw bytes — typically the receiving side of a
287    /// wire-format decode.
288    pub fn from_bytes(bytes: [u8; KYBER_PUBLIC_KEY_BYTES]) -> Self {
289        Self(bytes)
290    }
291
292    /// Construct from a borrowed slice, validating the length.
293    ///
294    /// # Errors
295    ///
296    /// Returns [`KyberLibError::InvalidLength`] if `bytes.len() !=
297    /// KYBER_PUBLIC_KEY_BYTES`.
298    pub fn try_from_slice(bytes: &[u8]) -> Result<Self, KyberLibError> {
299        if bytes.len() != KYBER_PUBLIC_KEY_BYTES {
300            return Err(KyberLibError::InvalidLength);
301        }
302        let mut buf = [0u8; KYBER_PUBLIC_KEY_BYTES];
303        buf.copy_from_slice(bytes);
304        Ok(Self(buf))
305    }
306}
307
308impl fmt::Debug for MlKem768EncapKey {
309    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
310        f.debug_tuple("MlKem768EncapKey")
311            .field(&format_args!(
312                "[{} bytes; not secret]",
313                KYBER_PUBLIC_KEY_BYTES
314            ))
315            .finish()
316    }
317}
318
319impl MlKem768EncapKey {
320    /// Encapsulate against this public key. Returns the ciphertext
321    /// and the resulting shared secret.
322    ///
323    /// # Errors
324    ///
325    /// Returns [`KyberLibError::InvalidKey`] if the key fails the
326    /// FIPS 203 §7.2 modulus check, or
327    /// [`KyberLibError::RandomBytesGeneration`] if the supplied RNG
328    /// fails.
329    pub fn encapsulate<R: RngCore + CryptoRng>(
330        &self,
331        rng: &mut R,
332    ) -> Result<(MlKem768Ciphertext, SharedSecret), KyberLibError> {
333        let (ct, ss) =
334            backend_encapsulate::<MlKem768, R, KYBER_CIPHERTEXT_BYTES>(
335                &self.0, rng,
336            )?;
337        Ok((MlKem768Ciphertext(ct), SharedSecret(ss)))
338    }
339
340    /// Borrow as raw bytes.
341    pub fn as_bytes(&self) -> &[u8; KYBER_PUBLIC_KEY_BYTES] {
342        &self.0
343    }
344}
345
346/// ML-KEM-768 decapsulation key (private). 2400 bytes. Zeroized on drop.
347#[derive(Clone, Zeroize, ZeroizeOnDrop, Eq, PartialEq)]
348#[non_exhaustive]
349pub struct MlKem768DecapKey(pub(crate) [u8; KYBER_SECRET_KEY_BYTES]);
350
351impl fmt::Debug for MlKem768DecapKey {
352    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
353        f.write_str("MlKem768DecapKey([REDACTED 2400 bytes])")
354    }
355}
356
357impl MlKem768DecapKey {
358    /// Decapsulate a ciphertext under this secret key. Implicit
359    /// rejection per FIPS 203 §6.3 — returns a pseudorandom shared
360    /// secret on invalid ciphertexts (never panics, never branches on
361    /// validity).
362    #[must_use]
363    pub fn decapsulate(&self, ct: &MlKem768Ciphertext) -> SharedSecret {
364        backend_decapsulate::<MlKem768>(&self.0, &ct.0)
365    }
366
367    /// Borrow as raw bytes. Sparingly — this exposes the entire 2400-
368    /// byte private state.
369    pub fn as_bytes(&self) -> &[u8; KYBER_SECRET_KEY_BYTES] {
370        &self.0
371    }
372}
373
374/// ML-KEM-768 ciphertext. 1088 bytes.
375#[derive(Clone, Copy, Eq, PartialEq)]
376#[non_exhaustive]
377pub struct MlKem768Ciphertext([u8; KYBER_CIPHERTEXT_BYTES]);
378
379impl MlKem768Ciphertext {
380    /// Construct from raw bytes — typically the receiving side of a
381    /// wire-format decode.
382    pub fn from_bytes(bytes: [u8; KYBER_CIPHERTEXT_BYTES]) -> Self {
383        Self(bytes)
384    }
385
386    /// Construct from a borrowed slice, validating the length.
387    ///
388    /// # Errors
389    ///
390    /// Returns [`KyberLibError::InvalidLength`] if `bytes.len() !=
391    /// KYBER_CIPHERTEXT_BYTES`.
392    pub fn try_from_slice(bytes: &[u8]) -> Result<Self, KyberLibError> {
393        if bytes.len() != KYBER_CIPHERTEXT_BYTES {
394            return Err(KyberLibError::InvalidLength);
395        }
396        let mut buf = [0u8; KYBER_CIPHERTEXT_BYTES];
397        buf.copy_from_slice(bytes);
398        Ok(Self(buf))
399    }
400}
401
402impl fmt::Debug for MlKem768Ciphertext {
403    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
404        f.debug_tuple("MlKem768Ciphertext")
405            .field(&format_args!(
406                "[{} bytes; opaque]",
407                KYBER_CIPHERTEXT_BYTES
408            ))
409            .finish()
410    }
411}
412
413impl MlKem768Ciphertext {
414    /// Borrow as raw bytes — for wire-format serialization.
415    pub fn as_bytes(&self) -> &[u8; KYBER_CIPHERTEXT_BYTES] {
416        &self.0
417    }
418}
419
420// ============================== KemCore impls (Phase 3e — generic-wired)
421//
422// All three impls coexist in a single build, routing through the
423// generic FIPS 203 pipeline (`crate::kem::kem_*_generic`). Each
424// `generate` / `encapsulate` / `decapsulate` allocates its own
425// fixed-size byte buffer and hands a `&mut [u8]` to the generic
426// function. The associated `PublicKeyBytes` / `SecretKeyBytes` /
427// `CiphertextBytes` types from `MlKemParams` guarantee the array
428// sizes match FIPS 203 §6 Table 2.
429//
430// The old `classic::*` shim path is preserved in `crate::api` for the
431// legacy free-function surface (`keypair` / `encapsulate` /
432// `decapsulate`); those still consume the cfg-gated reference. The
433// typed-state API below is FULLY generic and works across all three
434// parameter sets in any single build.
435
436impl KemCore for MlKem768 {
437    type EncapsulationKey = MlKem768EncapKey;
438    type DecapsulationKey = MlKem768DecapKey;
439    type Ciphertext = MlKem768Ciphertext;
440    const ALGORITHM_ID: &'static str = "ML-KEM-768";
441    const OID: &'static str = "2.16.840.1.101.3.4.4.2";
442
443    fn generate<R: RngCore + CryptoRng>(
444        rng: &mut R,
445    ) -> Result<
446        (Self::DecapsulationKey, Self::EncapsulationKey),
447        KyberLibError,
448    > {
449        let (sk, pk) = backend_keygen::<
450            MlKem768,
451            R,
452            KYBER_SECRET_KEY_BYTES,
453            KYBER_PUBLIC_KEY_BYTES,
454        >(rng)?;
455        Ok((MlKem768DecapKey(sk), MlKem768EncapKey(pk)))
456    }
457
458    fn generate_from_seed(
459        d: &[u8; 32],
460        z: &[u8; 32],
461    ) -> Result<
462        (Self::DecapsulationKey, Self::EncapsulationKey),
463        KyberLibError,
464    > {
465        let (sk, pk) = keypair_from_seed::<
466            MlKem768,
467            KYBER_PUBLIC_KEY_BYTES,
468            KYBER_SECRET_KEY_BYTES,
469        >(d, z)?;
470        Ok((MlKem768DecapKey(sk), MlKem768EncapKey(pk)))
471    }
472}
473
474impl MlKem512EncapKey {
475    /// Encapsulate against this public key under ML-KEM-512.
476    ///
477    /// # Errors
478    ///
479    /// [`KyberLibError::InvalidKey`] if the key fails the FIPS 203
480    /// §7.2 modulus check; [`KyberLibError::RandomBytesGeneration`] if
481    /// the RNG fails.
482    pub fn encapsulate<R: RngCore + CryptoRng>(
483        &self,
484        rng: &mut R,
485    ) -> Result<(MlKem512Ciphertext, SharedSecret), KyberLibError> {
486        let (ct, ss) =
487            backend_encapsulate::<MlKem512, R, 768>(&self.0, rng)?;
488        Ok((MlKem512Ciphertext(ct), SharedSecret(ss)))
489    }
490}
491
492impl MlKem512DecapKey {
493    /// Decapsulate a ciphertext under this ML-KEM-512 secret key.
494    /// Implicit-rejection per FIPS 203 §6.3 — never panics, never
495    /// branches on validity.
496    #[must_use]
497    pub fn decapsulate(&self, ct: &MlKem512Ciphertext) -> SharedSecret {
498        backend_decapsulate::<MlKem512>(&self.0, &ct.0)
499    }
500}
501
502impl KemCore for MlKem512 {
503    type EncapsulationKey = MlKem512EncapKey;
504    type DecapsulationKey = MlKem512DecapKey;
505    type Ciphertext = MlKem512Ciphertext;
506    const ALGORITHM_ID: &'static str = "ML-KEM-512";
507    const OID: &'static str = "2.16.840.1.101.3.4.4.1";
508
509    fn generate<R: RngCore + CryptoRng>(
510        rng: &mut R,
511    ) -> Result<
512        (Self::DecapsulationKey, Self::EncapsulationKey),
513        KyberLibError,
514    > {
515        let (sk, pk) = backend_keygen::<MlKem512, R, 1632, 800>(rng)?;
516        Ok((MlKem512DecapKey(sk), MlKem512EncapKey(pk)))
517    }
518
519    fn generate_from_seed(
520        d: &[u8; 32],
521        z: &[u8; 32],
522    ) -> Result<
523        (Self::DecapsulationKey, Self::EncapsulationKey),
524        KyberLibError,
525    > {
526        let (sk, pk) = keypair_from_seed::<MlKem512, 800, 1632>(d, z)?;
527        Ok((MlKem512DecapKey(sk), MlKem512EncapKey(pk)))
528    }
529}
530
531impl MlKem1024EncapKey {
532    /// Encapsulate against this public key under ML-KEM-1024.
533    ///
534    /// # Errors
535    ///
536    /// [`KyberLibError::InvalidKey`] if the key fails the FIPS 203
537    /// §7.2 modulus check; [`KyberLibError::RandomBytesGeneration`] if
538    /// the RNG fails.
539    pub fn encapsulate<R: RngCore + CryptoRng>(
540        &self,
541        rng: &mut R,
542    ) -> Result<(MlKem1024Ciphertext, SharedSecret), KyberLibError>
543    {
544        let (ct, ss) =
545            backend_encapsulate::<MlKem1024, R, 1568>(&self.0, rng)?;
546        Ok((MlKem1024Ciphertext(ct), SharedSecret(ss)))
547    }
548}
549
550impl MlKem1024DecapKey {
551    /// Decapsulate a ciphertext under this ML-KEM-1024 secret key.
552    /// Implicit-rejection per FIPS 203 §6.3 — never panics, never
553    /// branches on validity.
554    #[must_use]
555    pub fn decapsulate(
556        &self,
557        ct: &MlKem1024Ciphertext,
558    ) -> SharedSecret {
559        backend_decapsulate::<MlKem1024>(&self.0, &ct.0)
560    }
561}
562
563impl KemCore for MlKem1024 {
564    type EncapsulationKey = MlKem1024EncapKey;
565    type DecapsulationKey = MlKem1024DecapKey;
566    type Ciphertext = MlKem1024Ciphertext;
567    const ALGORITHM_ID: &'static str = "ML-KEM-1024";
568    const OID: &'static str = "2.16.840.1.101.3.4.4.3";
569
570    fn generate<R: RngCore + CryptoRng>(
571        rng: &mut R,
572    ) -> Result<
573        (Self::DecapsulationKey, Self::EncapsulationKey),
574        KyberLibError,
575    > {
576        let (sk, pk) = backend_keygen::<MlKem1024, R, 3168, 1568>(rng)?;
577        Ok((MlKem1024DecapKey(sk), MlKem1024EncapKey(pk)))
578    }
579
580    fn generate_from_seed(
581        d: &[u8; 32],
582        z: &[u8; 32],
583    ) -> Result<
584        (Self::DecapsulationKey, Self::EncapsulationKey),
585        KyberLibError,
586    > {
587        let (sk, pk) =
588            keypair_from_seed::<MlKem1024, 1568, 3168>(d, z)?;
589        Ok((MlKem1024DecapKey(sk), MlKem1024EncapKey(pk)))
590    }
591}
592
593#[cfg(test)]
594#[path = "ml_kem_tests.rs"]
595mod tests;