kyberlib 0.0.7

A robust, audit-friendly Rust implementation of FIPS 203 ML-KEM (the standardised CRYSTALS-Kyber post-quantum KEM).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
// Copyright © 2026 kyberlib. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! FIPS 203 ML-KEM type-state API.
//!
//! New surface introduced in v0.0.7 (#130). Parameter set is a *type*,
//! not a Cargo feature: each consumer picks [`MlKem512`], [`MlKem768`],
//! or [`MlKem1024`] at the call site. Secrets and public material
//! travel as distinct types with the right Drop / Zeroize semantics:
//!
//! ```text
//!     KemCore                       — trait, sealed
//!     ├── MlKem512        ✅ implemented
//!     ├── MlKem768        ✅ implemented (default)
//!     └── MlKem1024       ✅ implemented
//!
//!     EncapsulationKey<P>           — `Copy`-safe public bytes
//!     DecapsulationKey<P>           — `!Copy`, `ZeroizeOnDrop`, redacted `Debug`
//!     Ciphertext<P>                 — opaque, fixed-size byte wrapper
//!     SharedSecret                  — 32-byte `ZeroizeOnDrop` secret
//! ```
//!
//! As of Phase 3e of #130b (commit `7…` — search `git log --grep
//! "Phase 3e"`), all three parameter sets implement [`KemCore`]
//! unconditionally. The default build supports `MlKem512`,
//! `MlKem768`, and `MlKem1024` concurrently — pick the right one
//! at the call site:
//!
//! ```
//! # fn main() -> Result<(), kyberlib::KyberLibError> {
//! use kyberlib::{KemCore, MlKem512, MlKem768, MlKem1024};
//! let mut rng = rand::thread_rng();
//! let (dk_512, ek_512)  = MlKem512::generate(&mut rng)?;
//! let (dk_768, ek_768)  = MlKem768::generate(&mut rng)?;
//! let (dk_1024, ek_1024) = MlKem1024::generate(&mut rng)?;
//! # let _ = (dk_512, ek_512, dk_768, ek_768, dk_1024, ek_1024);
//! # Ok(()) }
//! ```
//!
//! ## Migration from the v0.0.6 surface
//!
//! The free functions [`keypair`](crate::keypair),
//! [`encapsulate`](crate::encapsulate), [`decapsulate`](crate::decapsulate)
//! and the [`Keypair`](crate::Keypair) struct from the old surface are
//! retained as `#[deprecated]` shims for `MlKem768` only. They'll be
//! removed in a future release.
//!
//! ## ACVP conformance
//!
//! `MlKem768` is byte-validated against 60/60 NIST ACVP test vectors
//! (see [`tests/test_acvp.rs`][acvp]). `MlKem512` and `MlKem1024`
//! conformance is verified structurally — the generic algorithm
//! pipeline is byte-identical to the cfg-gated reference under each
//! parameter set's feature (#130b commits a77b94b through 3819f7a).
//! The ACVP harness is wired to all three under #130c.
//!
//! [acvp]: ../../tests/test_acvp.rs

use crate::{
    error::KyberLibError, KYBER_CIPHERTEXT_BYTES,
    KYBER_PUBLIC_KEY_BYTES, KYBER_SECRET_KEY_BYTES,
    KYBER_SHARED_SECRET_BYTES,
};
use core::fmt;
use rand_core::{CryptoRng, RngCore};
use zeroize::{Zeroize, ZeroizeOnDrop};

mod sealed {
    /// Sealed trait — only this crate may implement [`super::KemCore`].
    pub trait Sealed {}
}

// ---------------------------------------------------------------- KemCore

/// FIPS 203 ML-KEM parameter-set marker trait.
///
/// Implemented by the zero-sized marker types [`MlKem512`], [`MlKem768`],
/// [`MlKem1024`]. Sealed — third-party crates cannot add new
/// implementations.
pub trait KemCore: sealed::Sealed + Sized {
    /// Encapsulation key (public side).
    type EncapsulationKey;
    /// Decapsulation key (secret side). Zeroized on drop.
    type DecapsulationKey;
    /// Ciphertext.
    type Ciphertext;

    /// Stable algorithm identifier per the IETF LAMPS draft —
    /// `"ML-KEM-512"`, `"ML-KEM-768"`, or `"ML-KEM-1024"`.
    const ALGORITHM_ID: &'static str;

    /// Dotted-decimal object identifier per
    /// `2.16.840.1.101.3.4.4.{1,2,3}`.
    const OID: &'static str;

    /// Generate a fresh decapsulation / encapsulation key pair.
    ///
    /// # Errors
    ///
    /// Returns [`KyberLibError::RandomBytesGeneration`] if the supplied
    /// RNG fails to produce randomness. All other errors are
    /// internal-invariant violations and should not occur with a
    /// well-behaved RNG.
    fn generate<R: RngCore + CryptoRng>(
        rng: &mut R,
    ) -> Result<
        (Self::DecapsulationKey, Self::EncapsulationKey),
        KyberLibError,
    >;
}

// ---------------------------------------------------------------- markers

/// ML-KEM-512 parameter set marker. NIST security category 1 (≈ AES-128).
///
/// Module rank `k = 2`. Public key 800 B, secret key 1632 B,
/// ciphertext 768 B, shared secret 32 B.
///
/// [`KemCore`] is implemented unconditionally. Works in any build —
/// no `--features kyber512` needed. The default build supports
/// `MlKem512`, [`MlKem768`], and [`MlKem1024`] concurrently (Phase 3e
/// of #130b — see `crates/kyberlib/src/paramsets.rs`).
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct MlKem512;
impl sealed::Sealed for MlKem512 {}

impl MlKem512 {
    /// Module rank.
    pub const K: usize = 2;
    /// Public encapsulation key length in bytes.
    pub const PUBLIC_KEY_BYTES: usize = 800;
    /// Private decapsulation key length in bytes.
    pub const SECRET_KEY_BYTES: usize = 1632;
    /// Ciphertext length in bytes.
    pub const CIPHERTEXT_BYTES: usize = 768;
    /// Shared-secret length in bytes.
    pub const SHARED_SECRET_BYTES: usize = 32;
    /// Stable algorithm identifier per the IETF LAMPS draft.
    pub const ALGORITHM_ID: &'static str = "ML-KEM-512";
    /// Object identifier — `2.16.840.1.101.3.4.4.1`.
    pub const OID: &'static str = crate::oid::ML_KEM_512;
}

/// ML-KEM-768 parameter set marker. NIST security category 3 (≈ AES-192).
/// Default for CNSA 2.0 hybrid TLS deployments.
///
/// Module rank `k = 3`. Public key 1184 B, secret key 2400 B,
/// ciphertext 1088 B, shared secret 32 B.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct MlKem768;
impl sealed::Sealed for MlKem768 {}

impl MlKem768 {
    /// Module rank.
    pub const K: usize = 3;
    /// Public encapsulation key length in bytes.
    pub const PUBLIC_KEY_BYTES: usize = 1184;
    /// Private decapsulation key length in bytes.
    pub const SECRET_KEY_BYTES: usize = 2400;
    /// Ciphertext length in bytes.
    pub const CIPHERTEXT_BYTES: usize = 1088;
    /// Shared-secret length in bytes.
    pub const SHARED_SECRET_BYTES: usize = 32;
    /// Stable algorithm identifier per the IETF LAMPS draft.
    pub const ALGORITHM_ID: &'static str = "ML-KEM-768";
    /// Object identifier — `2.16.840.1.101.3.4.4.2`.
    pub const OID: &'static str = crate::oid::ML_KEM_768;
}

/// ML-KEM-1024 parameter set marker. NIST security category 5 (≈ AES-256).
/// Required by CNSA 2.0 for NSS by 1 Jan 2027.
///
/// Module rank `k = 4`. Public key 1568 B, secret key 3168 B,
/// ciphertext 1568 B, shared secret 32 B.
///
/// [`KemCore`] is implemented unconditionally — works alongside
/// [`MlKem512`] and [`MlKem768`] in any build.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct MlKem1024;
impl sealed::Sealed for MlKem1024 {}

impl MlKem1024 {
    /// Module rank.
    pub const K: usize = 4;
    /// Public encapsulation key length in bytes.
    pub const PUBLIC_KEY_BYTES: usize = 1568;
    /// Private decapsulation key length in bytes.
    pub const SECRET_KEY_BYTES: usize = 3168;
    /// Ciphertext length in bytes.
    pub const CIPHERTEXT_BYTES: usize = 1568;
    /// Shared-secret length in bytes.
    pub const SHARED_SECRET_BYTES: usize = 32;
    /// Stable algorithm identifier per the IETF LAMPS draft.
    pub const ALGORITHM_ID: &'static str = "ML-KEM-1024";
    /// Object identifier — `2.16.840.1.101.3.4.4.3`.
    pub const OID: &'static str = crate::oid::ML_KEM_1024;
}

// ---------------------------------------------------------------- ML-KEM-512 + 1024 typed wrappers

macro_rules! sized_wrapper_types {
    ($p:ident, $ek:ident, $dk:ident, $ct:ident, $pk_bytes:expr, $sk_bytes:expr, $ct_bytes:expr) => {
        /// Public encapsulation key.
        #[derive(Clone, Copy, Eq, PartialEq)]
        pub struct $ek([u8; $pk_bytes]);

        impl $ek {
            /// Construct from raw bytes.
            pub fn from_bytes(bytes: [u8; $pk_bytes]) -> Self {
                Self(bytes)
            }
            /// Construct from a borrowed slice, validating the length.
            ///
            /// # Errors
            ///
            /// [`KyberLibError::InvalidLength`] on length mismatch.
            pub fn try_from_slice(
                bytes: &[u8],
            ) -> Result<Self, KyberLibError> {
                if bytes.len() != $pk_bytes {
                    return Err(KyberLibError::InvalidLength);
                }
                let mut buf = [0u8; $pk_bytes];
                buf.copy_from_slice(bytes);
                Ok(Self(buf))
            }
            /// Borrow as raw bytes.
            pub fn as_bytes(&self) -> &[u8; $pk_bytes] {
                &self.0
            }
        }

        impl fmt::Debug for $ek {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.debug_tuple(stringify!($ek))
                    .field(&format_args!(
                        "[{} bytes; not secret]",
                        $pk_bytes
                    ))
                    .finish()
            }
        }

        /// Secret decapsulation key. Zeroized on drop.
        #[derive(Clone, Zeroize, ZeroizeOnDrop, Eq, PartialEq)]
        pub struct $dk([u8; $sk_bytes]);

        impl $dk {
            /// Construct from raw bytes.
            pub fn from_bytes(bytes: [u8; $sk_bytes]) -> Self {
                Self(bytes)
            }
            /// Construct from a borrowed slice, validating the length.
            ///
            /// # Errors
            ///
            /// [`KyberLibError::InvalidLength`] on length mismatch.
            pub fn try_from_slice(
                bytes: &[u8],
            ) -> Result<Self, KyberLibError> {
                if bytes.len() != $sk_bytes {
                    return Err(KyberLibError::InvalidLength);
                }
                let mut buf = [0u8; $sk_bytes];
                buf.copy_from_slice(bytes);
                Ok(Self(buf))
            }
            /// Borrow as raw bytes. Sparingly — the secret state is
            /// exposed.
            pub fn as_bytes(&self) -> &[u8; $sk_bytes] {
                &self.0
            }
        }

        impl fmt::Debug for $dk {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str(concat!(
                    stringify!($dk),
                    "([REDACTED ",
                    stringify!($sk_bytes),
                    " bytes])"
                ))
            }
        }

        /// Ciphertext.
        #[derive(Clone, Copy, Eq, PartialEq)]
        pub struct $ct([u8; $ct_bytes]);

        impl $ct {
            /// Construct from raw bytes.
            pub fn from_bytes(bytes: [u8; $ct_bytes]) -> Self {
                Self(bytes)
            }
            /// Construct from a borrowed slice, validating the length.
            ///
            /// # Errors
            ///
            /// [`KyberLibError::InvalidLength`] on length mismatch.
            pub fn try_from_slice(
                bytes: &[u8],
            ) -> Result<Self, KyberLibError> {
                if bytes.len() != $ct_bytes {
                    return Err(KyberLibError::InvalidLength);
                }
                let mut buf = [0u8; $ct_bytes];
                buf.copy_from_slice(bytes);
                Ok(Self(buf))
            }
            /// Borrow as raw bytes.
            pub fn as_bytes(&self) -> &[u8; $ct_bytes] {
                &self.0
            }
        }

        impl fmt::Debug for $ct {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.debug_tuple(stringify!($ct))
                    .field(&format_args!(
                        "[{} bytes; opaque]",
                        $ct_bytes
                    ))
                    .finish()
            }
        }
    };
}

sized_wrapper_types!(
    MlKem512,
    MlKem512EncapKey,
    MlKem512DecapKey,
    MlKem512Ciphertext,
    800,
    1632,
    768
);
sized_wrapper_types!(
    MlKem1024,
    MlKem1024EncapKey,
    MlKem1024DecapKey,
    MlKem1024Ciphertext,
    1568,
    3168,
    1568
);

// ---------------------------------------------------------------- shared secret

/// 32-byte shared secret. Zeroized on drop.
///
/// Construct via [`KemCore::generate`] →
/// `EncapsulationKey::encapsulate` → `DecapsulationKey::decapsulate`;
/// never directly by the consumer.
#[derive(Clone, Zeroize, ZeroizeOnDrop)]
#[non_exhaustive]
pub struct SharedSecret([u8; KYBER_SHARED_SECRET_BYTES]);

impl SharedSecret {
    /// Borrow the raw 32 bytes. Use sparingly — every borrow expands
    /// the surface that needs to handle the secret carefully.
    pub fn as_bytes(&self) -> &[u8; KYBER_SHARED_SECRET_BYTES] {
        &self.0
    }
}

impl fmt::Debug for SharedSecret {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("SharedSecret([REDACTED 32 bytes])")
    }
}

impl PartialEq for SharedSecret {
    fn eq(&self, other: &Self) -> bool {
        // Constant-time-ish (memcmp loops in stdlib are fixed-length).
        // For provable CT equality on secrets, use `subtle::ConstantTimeEq`.
        self.0 == other.0
    }
}

impl Eq for SharedSecret {}

// ---------------------------------------------------------------- ML-KEM-768

/// ML-KEM-768 encapsulation key (public). 1184 bytes.
#[derive(Clone, Copy, Eq, PartialEq)]
pub struct MlKem768EncapKey([u8; KYBER_PUBLIC_KEY_BYTES]);

impl MlKem768EncapKey {
    /// Construct from raw bytes — typically the receiving side of a
    /// wire-format decode.
    pub fn from_bytes(bytes: [u8; KYBER_PUBLIC_KEY_BYTES]) -> Self {
        Self(bytes)
    }

    /// Construct from a borrowed slice, validating the length.
    ///
    /// # Errors
    ///
    /// Returns [`KyberLibError::InvalidLength`] if `bytes.len() !=
    /// KYBER_PUBLIC_KEY_BYTES`.
    pub fn try_from_slice(bytes: &[u8]) -> Result<Self, KyberLibError> {
        if bytes.len() != KYBER_PUBLIC_KEY_BYTES {
            return Err(KyberLibError::InvalidLength);
        }
        let mut buf = [0u8; KYBER_PUBLIC_KEY_BYTES];
        buf.copy_from_slice(bytes);
        Ok(Self(buf))
    }
}

impl fmt::Debug for MlKem768EncapKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("MlKem768EncapKey")
            .field(&format_args!(
                "[{} bytes; not secret]",
                KYBER_PUBLIC_KEY_BYTES
            ))
            .finish()
    }
}

impl MlKem768EncapKey {
    /// Encapsulate against this public key. Returns the ciphertext
    /// and the resulting shared secret.
    ///
    /// # Errors
    ///
    /// Returns [`KyberLibError::RandomBytesGeneration`] if the supplied
    /// RNG fails.
    pub fn encapsulate<R: RngCore + CryptoRng>(
        &self,
        rng: &mut R,
    ) -> Result<(MlKem768Ciphertext, SharedSecret), KyberLibError> {
        let mut ct = [0u8; KYBER_CIPHERTEXT_BYTES];
        let mut ss = [0u8; KYBER_SHARED_SECRET_BYTES];
        crate::kem::kem_enc_generic::<MlKem768, R>(
            &mut ct, &mut ss, &self.0, rng, None,
        )?;
        Ok((MlKem768Ciphertext(ct), SharedSecret(ss)))
    }

    /// Borrow as raw bytes.
    pub fn as_bytes(&self) -> &[u8; KYBER_PUBLIC_KEY_BYTES] {
        &self.0
    }
}

/// ML-KEM-768 decapsulation key (private). 2400 bytes. Zeroized on drop.
#[derive(Clone, Zeroize, ZeroizeOnDrop, Eq, PartialEq)]
#[non_exhaustive]
pub struct MlKem768DecapKey([u8; KYBER_SECRET_KEY_BYTES]);

impl fmt::Debug for MlKem768DecapKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("MlKem768DecapKey([REDACTED 2400 bytes])")
    }
}

impl MlKem768DecapKey {
    /// Decapsulate a ciphertext under this secret key. Implicit
    /// rejection per FIPS 203 §6.3 — returns a pseudorandom shared
    /// secret on invalid ciphertexts (never panics, never branches on
    /// validity).
    #[must_use]
    pub fn decapsulate(&self, ct: &MlKem768Ciphertext) -> SharedSecret {
        let mut ss = [0u8; KYBER_SHARED_SECRET_BYTES];
        crate::kem::kem_dec_generic::<MlKem768>(
            &mut ss, &ct.0, &self.0,
        );
        SharedSecret(ss)
    }

    /// Borrow as raw bytes. Sparingly — this exposes the entire 2400-
    /// byte private state.
    pub fn as_bytes(&self) -> &[u8; KYBER_SECRET_KEY_BYTES] {
        &self.0
    }
}

/// ML-KEM-768 ciphertext. 1088 bytes.
#[derive(Clone, Copy, Eq, PartialEq)]
pub struct MlKem768Ciphertext([u8; KYBER_CIPHERTEXT_BYTES]);

impl MlKem768Ciphertext {
    /// Construct from raw bytes — typically the receiving side of a
    /// wire-format decode.
    pub fn from_bytes(bytes: [u8; KYBER_CIPHERTEXT_BYTES]) -> Self {
        Self(bytes)
    }

    /// Construct from a borrowed slice, validating the length.
    ///
    /// # Errors
    ///
    /// Returns [`KyberLibError::InvalidLength`] if `bytes.len() !=
    /// KYBER_CIPHERTEXT_BYTES`.
    pub fn try_from_slice(bytes: &[u8]) -> Result<Self, KyberLibError> {
        if bytes.len() != KYBER_CIPHERTEXT_BYTES {
            return Err(KyberLibError::InvalidLength);
        }
        let mut buf = [0u8; KYBER_CIPHERTEXT_BYTES];
        buf.copy_from_slice(bytes);
        Ok(Self(buf))
    }
}

impl fmt::Debug for MlKem768Ciphertext {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("MlKem768Ciphertext")
            .field(&format_args!(
                "[{} bytes; opaque]",
                KYBER_CIPHERTEXT_BYTES
            ))
            .finish()
    }
}

impl MlKem768Ciphertext {
    /// Borrow as raw bytes — for wire-format serialization.
    pub fn as_bytes(&self) -> &[u8; KYBER_CIPHERTEXT_BYTES] {
        &self.0
    }
}

// ============================== KemCore impls (Phase 3e — generic-wired)
//
// All three impls coexist in a single build, routing through the
// generic FIPS 203 pipeline (`crate::kem::kem_*_generic`). Each
// `generate` / `encapsulate` / `decapsulate` allocates its own
// fixed-size byte buffer and hands a `&mut [u8]` to the generic
// function. The associated `PublicKeyBytes` / `SecretKeyBytes` /
// `CiphertextBytes` types from `MlKemParams` guarantee the array
// sizes match FIPS 203 §6 Table 2.
//
// The old `classic::*` shim path is preserved in `crate::api` for the
// legacy free-function surface (`keypair` / `encapsulate` /
// `decapsulate`); those still consume the cfg-gated reference. The
// typed-state API below is FULLY generic and works across all three
// parameter sets in any single build.

impl KemCore for MlKem768 {
    type EncapsulationKey = MlKem768EncapKey;
    type DecapsulationKey = MlKem768DecapKey;
    type Ciphertext = MlKem768Ciphertext;
    const ALGORITHM_ID: &'static str = "ML-KEM-768";
    const OID: &'static str = "2.16.840.1.101.3.4.4.2";

    fn generate<R: RngCore + CryptoRng>(
        rng: &mut R,
    ) -> Result<
        (Self::DecapsulationKey, Self::EncapsulationKey),
        KyberLibError,
    > {
        let mut pk = [0u8; KYBER_PUBLIC_KEY_BYTES];
        let mut sk = [0u8; KYBER_SECRET_KEY_BYTES];
        crate::kem::kem_keypair_generic::<MlKem768, R>(
            &mut pk, &mut sk, rng, None,
        )?;
        Ok((MlKem768DecapKey(sk), MlKem768EncapKey(pk)))
    }
}

impl MlKem512EncapKey {
    /// Encapsulate against this public key under ML-KEM-512.
    ///
    /// # Errors
    ///
    /// [`KyberLibError::RandomBytesGeneration`] if the RNG fails.
    pub fn encapsulate<R: RngCore + CryptoRng>(
        &self,
        rng: &mut R,
    ) -> Result<(MlKem512Ciphertext, SharedSecret), KyberLibError> {
        let mut ct = [0u8; 768];
        let mut ss = [0u8; KYBER_SHARED_SECRET_BYTES];
        crate::kem::kem_enc_generic::<MlKem512, R>(
            &mut ct, &mut ss, &self.0, rng, None,
        )?;
        Ok((MlKem512Ciphertext(ct), SharedSecret(ss)))
    }
}

impl MlKem512DecapKey {
    /// Decapsulate a ciphertext under this ML-KEM-512 secret key.
    /// Implicit-rejection per FIPS 203 §6.3 — never panics, never
    /// branches on validity.
    #[must_use]
    pub fn decapsulate(&self, ct: &MlKem512Ciphertext) -> SharedSecret {
        let mut ss = [0u8; KYBER_SHARED_SECRET_BYTES];
        crate::kem::kem_dec_generic::<MlKem512>(
            &mut ss, &ct.0, &self.0,
        );
        SharedSecret(ss)
    }
}

impl KemCore for MlKem512 {
    type EncapsulationKey = MlKem512EncapKey;
    type DecapsulationKey = MlKem512DecapKey;
    type Ciphertext = MlKem512Ciphertext;
    const ALGORITHM_ID: &'static str = "ML-KEM-512";
    const OID: &'static str = "2.16.840.1.101.3.4.4.1";

    fn generate<R: RngCore + CryptoRng>(
        rng: &mut R,
    ) -> Result<
        (Self::DecapsulationKey, Self::EncapsulationKey),
        KyberLibError,
    > {
        let mut pk = [0u8; 800];
        let mut sk = [0u8; 1632];
        crate::kem::kem_keypair_generic::<MlKem512, R>(
            &mut pk, &mut sk, rng, None,
        )?;
        Ok((MlKem512DecapKey(sk), MlKem512EncapKey(pk)))
    }
}

impl MlKem1024EncapKey {
    /// Encapsulate against this public key under ML-KEM-1024.
    ///
    /// # Errors
    ///
    /// [`KyberLibError::RandomBytesGeneration`] if the RNG fails.
    pub fn encapsulate<R: RngCore + CryptoRng>(
        &self,
        rng: &mut R,
    ) -> Result<(MlKem1024Ciphertext, SharedSecret), KyberLibError>
    {
        let mut ct = [0u8; 1568];
        let mut ss = [0u8; KYBER_SHARED_SECRET_BYTES];
        crate::kem::kem_enc_generic::<MlKem1024, R>(
            &mut ct, &mut ss, &self.0, rng, None,
        )?;
        Ok((MlKem1024Ciphertext(ct), SharedSecret(ss)))
    }
}

impl MlKem1024DecapKey {
    /// Decapsulate a ciphertext under this ML-KEM-1024 secret key.
    /// Implicit-rejection per FIPS 203 §6.3 — never panics, never
    /// branches on validity.
    #[must_use]
    pub fn decapsulate(
        &self,
        ct: &MlKem1024Ciphertext,
    ) -> SharedSecret {
        let mut ss = [0u8; KYBER_SHARED_SECRET_BYTES];
        crate::kem::kem_dec_generic::<MlKem1024>(
            &mut ss, &ct.0, &self.0,
        );
        SharedSecret(ss)
    }
}

impl KemCore for MlKem1024 {
    type EncapsulationKey = MlKem1024EncapKey;
    type DecapsulationKey = MlKem1024DecapKey;
    type Ciphertext = MlKem1024Ciphertext;
    const ALGORITHM_ID: &'static str = "ML-KEM-1024";
    const OID: &'static str = "2.16.840.1.101.3.4.4.3";

    fn generate<R: RngCore + CryptoRng>(
        rng: &mut R,
    ) -> Result<
        (Self::DecapsulationKey, Self::EncapsulationKey),
        KyberLibError,
    > {
        let mut pk = [0u8; 1568];
        let mut sk = [0u8; 3168];
        crate::kem::kem_keypair_generic::<MlKem1024, R>(
            &mut pk, &mut sk, rng, None,
        )?;
        Ok((MlKem1024DecapKey(sk), MlKem1024EncapKey(pk)))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// **THE public-API multi-param headline test.**
    ///
    /// Downstream consumers can now call `MlKem512::generate`,
    /// `MlKem768::generate`, and `MlKem1024::generate` from the same
    /// function under DEFAULT features. Each produces the correct
    /// FIPS 203 §6 byte sizes, each round-trips through encap → decap
    /// to a matching 32-byte shared secret.
    ///
    /// This is the user-visible delivery of #130b: the typed `KemCore`
    /// surface is fully multi-parameter-set in one build.
    #[test]
    fn public_api_all_three_kem_cores_in_one_build() {
        let mut rng = rand::thread_rng();

        // ML-KEM-512
        let (dk_512, ek_512) = MlKem512::generate(&mut rng).unwrap();
        let (ct_512, ss_a_512) = ek_512.encapsulate(&mut rng).unwrap();
        let ss_b_512 = dk_512.decapsulate(&ct_512);
        assert_eq!(ss_a_512, ss_b_512);

        // ML-KEM-768
        let (dk_768, ek_768) = MlKem768::generate(&mut rng).unwrap();
        let (ct_768, ss_a_768) = ek_768.encapsulate(&mut rng).unwrap();
        let ss_b_768 = dk_768.decapsulate(&ct_768);
        assert_eq!(ss_a_768, ss_b_768);

        // ML-KEM-1024
        let (dk_1024, ek_1024) = MlKem1024::generate(&mut rng).unwrap();
        let (ct_1024, ss_a_1024) =
            ek_1024.encapsulate(&mut rng).unwrap();
        let ss_b_1024 = dk_1024.decapsulate(&ct_1024);
        assert_eq!(ss_a_1024, ss_b_1024);

        // Byte sizes match FIPS 203 §6 Table 2.
        assert_eq!(ek_512.as_bytes().len(), 800);
        assert_eq!(ek_768.as_bytes().len(), 1184);
        assert_eq!(ek_1024.as_bytes().len(), 1568);
        assert_eq!(dk_512.as_bytes().len(), 1632);
        assert_eq!(dk_768.as_bytes().len(), 2400);
        assert_eq!(dk_1024.as_bytes().len(), 3168);
        assert_eq!(ct_512.as_bytes().len(), 768);
        assert_eq!(ct_768.as_bytes().len(), 1088);
        assert_eq!(ct_1024.as_bytes().len(), 1568);

        // Three independent secrets.
        assert_ne!(ss_a_512, ss_a_768);
        assert_ne!(ss_a_768, ss_a_1024);
        assert_ne!(ss_a_512, ss_a_1024);
    }

    #[test]
    fn ml_kem_768_round_trip() {
        let mut rng = rand::thread_rng();
        let (dk, ek) = MlKem768::generate(&mut rng).expect("keygen");
        let (ct, ss_a) = ek.encapsulate(&mut rng).expect("encap");
        let ss_b = dk.decapsulate(&ct);
        assert_eq!(ss_a, ss_b);
    }

    #[test]
    fn ml_kem_512_round_trip() {
        let mut rng = rand::thread_rng();
        let (dk, ek) = MlKem512::generate(&mut rng).expect("keygen");
        let (ct, ss_a) = ek.encapsulate(&mut rng).expect("encap");
        let ss_b = dk.decapsulate(&ct);
        assert_eq!(ss_a, ss_b);
    }

    #[test]
    fn ml_kem_1024_round_trip() {
        let mut rng = rand::thread_rng();
        let (dk, ek) = MlKem1024::generate(&mut rng).expect("keygen");
        let (ct, ss_a) = ek.encapsulate(&mut rng).expect("encap");
        let ss_b = dk.decapsulate(&ct);
        assert_eq!(ss_a, ss_b);
    }

    #[test]
    fn ml_kem_768_oid() {
        assert_eq!(MlKem768::OID, "2.16.840.1.101.3.4.4.2");
        assert_eq!(MlKem768::ALGORITHM_ID, "ML-KEM-768");
    }

    // ------- Coverage tests for the sized_wrapper_types! macro --------
    //
    // The MlKem512 and MlKem1024 typed wrappers exist under all feature
    // combinations (the macro that generates them is unconditional);
    // only KemCore impls are feature-gated. The tests below exercise
    // the constructor / accessor / Debug surface across all three
    // parameter sets so the macro expansion lines stay covered.

    #[test]
    fn ml_kem_512_encap_key_constructors() {
        let bytes = [0u8; 800];
        let ek = MlKem512EncapKey::from_bytes(bytes);
        assert_eq!(ek.as_bytes(), &bytes);
        let ek2 = MlKem512EncapKey::try_from_slice(&bytes).unwrap();
        assert_eq!(ek.as_bytes(), ek2.as_bytes());
        assert!(MlKem512EncapKey::try_from_slice(&[0u8; 100]).is_err());
        // Debug should NOT print raw bytes — "[bytes; not secret]" marker.
        #[cfg(feature = "std")]
        assert!(format!("{ek:?}").contains("not secret"));
    }

    #[test]
    fn ml_kem_512_decap_key_constructors() {
        let bytes = [0u8; 1632];
        let dk = MlKem512DecapKey::from_bytes(bytes);
        assert_eq!(dk.as_bytes(), &bytes);
        assert!(MlKem512DecapKey::try_from_slice(&bytes).is_ok());
        assert!(MlKem512DecapKey::try_from_slice(&[0u8; 100]).is_err());
        #[cfg(feature = "std")]
        assert!(format!("{dk:?}").contains("REDACTED"));
    }

    #[test]
    fn ml_kem_512_ciphertext_constructors() {
        let bytes = [0u8; 768];
        let ct = MlKem512Ciphertext::from_bytes(bytes);
        assert_eq!(ct.as_bytes(), &bytes);
        assert!(MlKem512Ciphertext::try_from_slice(&bytes).is_ok());
        assert!(
            MlKem512Ciphertext::try_from_slice(&[0u8; 100]).is_err()
        );
        #[cfg(feature = "std")]
        assert!(format!("{ct:?}").contains("opaque"));
    }

    #[test]
    fn ml_kem_1024_encap_key_constructors() {
        let bytes = [0u8; 1568];
        let ek = MlKem1024EncapKey::from_bytes(bytes);
        assert_eq!(ek.as_bytes(), &bytes);
        assert!(MlKem1024EncapKey::try_from_slice(&bytes).is_ok());
        assert!(MlKem1024EncapKey::try_from_slice(&[0u8; 100]).is_err());
    }

    #[test]
    fn ml_kem_1024_decap_key_constructors() {
        let bytes = [0u8; 3168];
        let dk = MlKem1024DecapKey::from_bytes(bytes);
        assert_eq!(dk.as_bytes(), &bytes);
        assert!(MlKem1024DecapKey::try_from_slice(&bytes).is_ok());
        assert!(MlKem1024DecapKey::try_from_slice(&[0u8; 100]).is_err());
    }

    #[test]
    fn ml_kem_1024_ciphertext_constructors() {
        let bytes = [0u8; 1568];
        let ct = MlKem1024Ciphertext::from_bytes(bytes);
        assert_eq!(ct.as_bytes(), &bytes);
        assert!(MlKem1024Ciphertext::try_from_slice(&bytes).is_ok());
        assert!(
            MlKem1024Ciphertext::try_from_slice(&[0u8; 100]).is_err()
        );
    }

    #[test]
    fn ml_kem_768_encap_key_try_from_slice_wrong_length() {
        assert!(MlKem768EncapKey::try_from_slice(&[0u8; 100]).is_err());
        assert!(MlKem768EncapKey::try_from_slice(&[0u8; 1184]).is_ok());
    }

    #[test]
    fn ml_kem_768_ciphertext_try_from_slice_wrong_length() {
        assert!(
            MlKem768Ciphertext::try_from_slice(&[0u8; 100]).is_err()
        );
        assert!(
            MlKem768Ciphertext::try_from_slice(&[0u8; 1088]).is_ok()
        );
    }

    #[test]
    fn shared_secret_as_bytes() {
        let ss = SharedSecret([0xAB; KYBER_SHARED_SECRET_BYTES]);
        assert_eq!(ss.as_bytes(), &[0xAB; KYBER_SHARED_SECRET_BYTES]);
        let ss2 = ss.clone();
        assert_eq!(ss, ss2);
    }

    #[test]
    fn marker_constants_match_spec() {
        // FIPS 203 sizes, NIST IETF LAMPS OIDs.
        assert_eq!(MlKem512::K, 2);
        assert_eq!(MlKem512::PUBLIC_KEY_BYTES, 800);
        assert_eq!(MlKem512::SECRET_KEY_BYTES, 1632);
        assert_eq!(MlKem512::CIPHERTEXT_BYTES, 768);
        assert_eq!(MlKem512::ALGORITHM_ID, "ML-KEM-512");

        assert_eq!(MlKem768::K, 3);
        assert_eq!(MlKem768::PUBLIC_KEY_BYTES, 1184);
        assert_eq!(MlKem768::SECRET_KEY_BYTES, 2400);
        assert_eq!(MlKem768::CIPHERTEXT_BYTES, 1088);
        assert_eq!(MlKem768::ALGORITHM_ID, "ML-KEM-768");

        assert_eq!(MlKem1024::K, 4);
        assert_eq!(MlKem1024::PUBLIC_KEY_BYTES, 1568);
        assert_eq!(MlKem1024::SECRET_KEY_BYTES, 3168);
        assert_eq!(MlKem1024::CIPHERTEXT_BYTES, 1568);
        assert_eq!(MlKem1024::ALGORITHM_ID, "ML-KEM-1024");
    }

    #[test]
    fn marker_types_are_zst() {
        // The marker types should be zero-sized — they exist only at
        // the type level.
        assert_eq!(size_of::<MlKem512>(), 0);
        assert_eq!(size_of::<MlKem768>(), 0);
        assert_eq!(size_of::<MlKem1024>(), 0);
    }

    #[test]
    #[cfg(feature = "std")]
    fn shared_secret_debug_is_redacted() {
        let ss = SharedSecret([0xAA; KYBER_SHARED_SECRET_BYTES]);
        assert_eq!(
            format!("{ss:?}"),
            "SharedSecret([REDACTED 32 bytes])"
        );
    }

    #[test]
    #[cfg(feature = "std")]
    fn decap_key_debug_is_redacted() {
        let dk = MlKem768DecapKey([0xBB; KYBER_SECRET_KEY_BYTES]);
        assert_eq!(
            format!("{dk:?}"),
            "MlKem768DecapKey([REDACTED 2400 bytes])"
        );
    }
}