saorsa-mls 0.3.7

Experimental Message Layer Security (MLS)-inspired library for P2P secure group communication
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
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
//! Cryptographic primitives for MLS using saorsa-pqc
//!
//! This module provides post-quantum cryptographic operations using
//! NIST-standardized algorithms from the saorsa-pqc library.
//!
//! We use saorsa-pqc as the single source of truth for all cryptographic
//! operations to ensure consistency and quantum-resistance.

use crate::{MlsError, Result};
use saorsa_pqc::api::{
    hpke::{HpkeConfig, HpkeContext as PqcHpkeContext, HpkeRecipient, HpkeSender},
    kdf::KdfAlgorithm,
    MlDsa, MlDsaPublicKey, MlDsaSecretKey, MlDsaSignature, MlDsaVariant, MlKem, MlKemCiphertext,
    MlKemPublicKey, MlKemSecretKey, MlKemSharedSecret, MlKemVariant, SlhDsa, SlhDsaPublicKey,
    SlhDsaSecretKey, SlhDsaSignature, SlhDsaVariant,
};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use zeroize::{Zeroize, ZeroizeOnDrop};

/// Registry identifier for Saorsa MLS PQC cipher suites (private-use values).
///
/// # Registry Ranges
///
/// - **0x0A01-0x0A04**: SPEC-PROD suites (includes hybrid X25519+MLKEM768)
/// - **0x0B01-0x0B04**: SPEC-2 PQC-only suites (no hybrids, production use)
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[repr(u16)]
pub enum CipherSuiteId {
    // SPEC-PROD registry (0x0A** - includes hybrid)
    /// MLS_128_MLKEM768_AES128GCM_SHA256_MLDSA65 (SPEC-PROD)
    #[deprecated(
        since = "0.3.0",
        note = "Use SPEC2_MLS_128_MLKEM768_AES128GCM_SHA256_MLDSA65 (0x0B01) for PQC-only"
    )]
    MLS_128_MLKEM768_AES128GCM_SHA256_MLDSA65 = 0x0A01,
    /// MLS_128_HYBRID_X25519+MLKEM768_AES128GCM_SHA256_MLDSA65 (SPEC-PROD hybrid)
    #[deprecated(
        since = "0.3.0",
        note = "Hybrid suites not allowed in SPEC-2 PQC-only mode"
    )]
    MLS_128_HYBRID_X25519_MLKEM768_AES128GCM_SHA256_MLDSA65 = 0x0A02,
    /// MLS_256_MLKEM1024_AES256GCM_SHA512_MLDSA87 (SPEC-PROD)
    #[deprecated(
        since = "0.3.0",
        note = "Use SPEC2_MLS_256_MLKEM1024_AES256GCM_SHA512_MLDSA87 (0x0B02) for PQC-only"
    )]
    MLS_256_MLKEM1024_AES256GCM_SHA512_MLDSA87 = 0x0A03,
    /// MLS_128_MLKEM768_CHACHA20POLY1305_SHA256_MLDSA65 (SPEC-PROD)
    #[deprecated(
        since = "0.3.0",
        note = "Use SPEC2_MLS_128_MLKEM768_CHACHA20POLY1305_SHA256_MLDSA65 (0x0B01) for PQC-only"
    )]
    MLS_128_MLKEM768_CHACHA20POLY1305_SHA256_MLDSA65 = 0x0A04,

    // SPEC-2 PQC-only registry (0x0B** - ChaCha20Poly1305 only, no hybrids, production)
    /// MLS_128_MLKEM768_CHACHA20POLY1305_SHA256_MLDSA65 (SPEC-2 PQC-only, default)
    SPEC2_MLS_128_MLKEM768_CHACHA20POLY1305_SHA256_MLDSA65 = 0x0B01,
    /// MLS_256_MLKEM1024_CHACHA20POLY1305_SHA512_MLDSA87 (SPEC-2 PQC-only, high-security)
    SPEC2_MLS_256_MLKEM1024_CHACHA20POLY1305_SHA512_MLDSA87 = 0x0B02,
    /// MLS_192_MLKEM1024_CHACHA20POLY1305_SHA384_SLHDSA192 (SPEC-2 PQC-only, optional SLH-DSA)
    #[allow(dead_code)] // Optional suite, may not be fully implemented yet
    SPEC2_MLS_192_MLKEM1024_CHACHA20POLY1305_SHA384_SLHDSA192 = 0x0B03,
}

impl CipherSuiteId {
    #[must_use]
    pub const fn as_u16(self) -> u16 {
        self as u16
    }
}

/// Supported post-quantum / hybrid KEM choices.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum MlsKem {
    MlKem512,
    MlKem768,
    MlKem1024,
    /// Hybrid X25519+MLKEM768 (SPEC-PROD only, not allowed in SPEC-2 PQC-only mode)
    #[deprecated(
        since = "0.3.0",
        note = "Hybrid KEMs not allowed in SPEC-2 PQC-only mode"
    )]
    HybridX25519MlKem768,
}

/// Supported signature schemes.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum MlsSignature {
    MlDsa44,
    MlDsa65,
    MlDsa87,
    SlhDsa128,
    SlhDsa192,
    SlhDsa256,
}

/// Supported AEAD algorithms.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum MlsAead {
    Aes128Gcm,
    Aes256Gcm,
    ChaCha20Poly1305,
}

/// Supported hash algorithms per ciphersuite definition.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum MlsHash {
    Sha256,
    Sha384,
    Sha512,
    Blake3,
    Sha3_256,
    Sha3_512,
}

/// Saorsa MLS cipher suite descriptor binding KEM, signature, AEAD, and hash choices.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CipherSuite {
    pub id: CipherSuiteId,
    pub kem: MlsKem,
    pub signature: MlsSignature,
    pub aead: MlsAead,
    pub hash: MlsHash,
}

impl CipherSuite {
    pub const fn new(
        id: CipherSuiteId,
        kem: MlsKem,
        signature: MlsSignature,
        aead: MlsAead,
        hash: MlsHash,
    ) -> Self {
        Self {
            id,
            kem,
            signature,
            aead,
            hash,
        }
    }

    #[must_use]
    pub const fn id(&self) -> CipherSuiteId {
        self.id
    }

    #[must_use]
    pub const fn kem(&self) -> MlsKem {
        self.kem
    }

    #[must_use]
    pub const fn signature(&self) -> MlsSignature {
        self.signature
    }

    #[must_use]
    pub const fn aead(&self) -> MlsAead {
        self.aead
    }

    #[must_use]
    pub const fn hash(&self) -> MlsHash {
        self.hash
    }

    #[must_use]
    pub fn from_id(id: CipherSuiteId) -> Option<Self> {
        REGISTRY.iter().copied().find(|suite| suite.id == id)
    }

    #[must_use]
    pub fn all() -> &'static [CipherSuite] {
        &REGISTRY
    }

    /// Get the ML-KEM variant for this cipher suite.
    #[must_use]
    #[allow(deprecated)] // Must handle deprecated HybridX25519MlKem768 for backward compatibility
    pub fn ml_kem_variant(&self) -> MlKemVariant {
        match self.kem {
            MlsKem::MlKem512 => MlKemVariant::MlKem512,
            MlsKem::MlKem768 | MlsKem::HybridX25519MlKem768 => MlKemVariant::MlKem768,
            MlsKem::MlKem1024 => MlKemVariant::MlKem1024,
        }
    }

    /// Get the ML-DSA variant for this cipher suite.
    /// Check if this cipher suite uses SLH-DSA signatures
    #[must_use]
    pub fn uses_slh_dsa(&self) -> bool {
        matches!(
            self.signature,
            MlsSignature::SlhDsa128 | MlsSignature::SlhDsa192 | MlsSignature::SlhDsa256
        )
    }

    /// Get the ML-DSA variant for this suite.
    ///
    /// # Errors
    ///
    /// Returns [`MlsError::CryptoError`] if the suite uses SLH-DSA signatures
    /// (call [`Self::slh_dsa_variant`] instead, or gate on
    /// [`Self::uses_slh_dsa`]).
    pub fn ml_dsa_variant(&self) -> Result<MlDsaVariant> {
        match self.signature {
            MlsSignature::MlDsa44 => Ok(MlDsaVariant::MlDsa44),
            MlsSignature::MlDsa65 => Ok(MlDsaVariant::MlDsa65),
            MlsSignature::MlDsa87 => Ok(MlDsaVariant::MlDsa87),
            MlsSignature::SlhDsa128 | MlsSignature::SlhDsa192 | MlsSignature::SlhDsa256 => Err(
                MlsError::CryptoError("ml_dsa_variant() called on an SLH-DSA suite".to_string()),
            ),
        }
    }

    /// Get the SLH-DSA variant for this suite.
    ///
    /// # Errors
    ///
    /// Returns [`MlsError::CryptoError`] if the suite uses ML-DSA signatures
    /// (call [`Self::ml_dsa_variant`] instead, or gate on
    /// [`Self::uses_slh_dsa`]).
    pub fn slh_dsa_variant(&self) -> Result<SlhDsaVariant> {
        match self.signature {
            // Use "fast" variants for now as "small" variants may not be available
            MlsSignature::SlhDsa128 => Ok(SlhDsaVariant::Sha2_128f),
            MlsSignature::SlhDsa192 => Ok(SlhDsaVariant::Sha2_128f), // 128f for 192-bit level
            MlsSignature::SlhDsa256 => Ok(SlhDsaVariant::Sha2_256f),
            MlsSignature::MlDsa44 | MlsSignature::MlDsa65 | MlsSignature::MlDsa87 => Err(
                MlsError::CryptoError("slh_dsa_variant() called on an ML-DSA suite".to_string()),
            ),
        }
    }

    /// Get the key size for symmetric encryption.
    #[must_use]
    pub fn key_size(&self) -> usize {
        match self.aead {
            MlsAead::Aes128Gcm => 16,
            MlsAead::Aes256Gcm | MlsAead::ChaCha20Poly1305 => 32,
        }
    }

    /// Get the nonce size for AEAD.
    #[must_use]
    pub fn nonce_size(&self) -> usize {
        12
    }

    /// Get the hash output size used for HKDF and transcript hashing.
    #[must_use]
    pub fn hash_size(&self) -> usize {
        match self.hash {
            MlsHash::Sha256 => 32,
            MlsHash::Sha384 => 48,
            MlsHash::Sha512 => 64,
            MlsHash::Blake3 => 32,
            MlsHash::Sha3_256 => 32,
            MlsHash::Sha3_512 => 64,
        }
    }

    /// Check if this is a PQC-only cipher suite (SPEC-2 compliant).
    ///
    /// Returns `true` for SPEC-2 suites (0x0B01-0x0B03) which are PQC-only with ChaCha20Poly1305.
    /// Returns `false` for SPEC-PROD suites (0x0A01-0x0A04) which may include hybrids.
    ///
    /// # SPEC-2 Policy (§8)
    ///
    /// SPEC-2 requires rejecting any non-PQC suite and uses only ChaCha20Poly1305 AEAD.
    #[must_use]
    #[allow(deprecated)] // Must check deprecated HybridX25519MlKem768 for backward compatibility
    pub fn is_pqc_only(&self) -> bool {
        // Check if using hybrid KEM (not allowed in SPEC-2)
        if matches!(self.kem, MlsKem::HybridX25519MlKem768) {
            return false;
        }

        // Check if suite ID is in SPEC-2 range (0x0B01-0x0B03)
        matches!(
            self.id,
            CipherSuiteId::SPEC2_MLS_128_MLKEM768_CHACHA20POLY1305_SHA256_MLDSA65
                | CipherSuiteId::SPEC2_MLS_256_MLKEM1024_CHACHA20POLY1305_SHA512_MLDSA87
                | CipherSuiteId::SPEC2_MLS_192_MLKEM1024_CHACHA20POLY1305_SHA384_SLHDSA192
        )
    }

    /// Check if this suite is in the SPEC-2 registry range (0x0B**).
    #[must_use]
    pub fn is_spec2(&self) -> bool {
        (self.id.as_u16() & 0xFF00) == 0x0B00
    }

    /// Check if this suite is deprecated (SPEC-PROD range 0x0A**).
    #[must_use]
    pub fn is_deprecated(&self) -> bool {
        (self.id.as_u16() & 0xFF00) == 0x0A00
    }
}

impl Default for CipherSuite {
    fn default() -> Self {
        // SPEC-2 default: MLS_128_MLKEM768_CHACHA20POLY1305_SHA256_MLDSA65 (0x0B01)
        // All SPEC-2 suites use ChaCha20Poly1305 AEAD
        CipherSuite::new(
            CipherSuiteId::SPEC2_MLS_128_MLKEM768_CHACHA20POLY1305_SHA256_MLDSA65,
            MlsKem::MlKem768,
            MlsSignature::MlDsa65,
            MlsAead::ChaCha20Poly1305,
            MlsHash::Sha256,
        )
    }
}

/// Cipher suite registry containing both SPEC-PROD (0x0A**) and SPEC-2 (0x0B**) suites.
///
/// SPEC-2 PQC-only suites (0x0B01-0x0B03) use ChaCha20Poly1305 and are preferred for production.
/// SPEC-PROD suites (0x0A01-0x0A04) are deprecated and include hybrid mode.
#[allow(deprecated)]
const REGISTRY: [CipherSuite; 7] = [
    // SPEC-PROD registry (0x0A** - deprecated, includes hybrid)
    CipherSuite::new(
        CipherSuiteId::MLS_128_MLKEM768_AES128GCM_SHA256_MLDSA65,
        MlsKem::MlKem768,
        MlsSignature::MlDsa65,
        MlsAead::Aes128Gcm,
        MlsHash::Sha256,
    ),
    CipherSuite::new(
        CipherSuiteId::MLS_128_HYBRID_X25519_MLKEM768_AES128GCM_SHA256_MLDSA65,
        MlsKem::HybridX25519MlKem768,
        MlsSignature::MlDsa65,
        MlsAead::Aes128Gcm,
        MlsHash::Sha256,
    ),
    CipherSuite::new(
        CipherSuiteId::MLS_256_MLKEM1024_AES256GCM_SHA512_MLDSA87,
        MlsKem::MlKem1024,
        MlsSignature::MlDsa87,
        MlsAead::Aes256Gcm,
        MlsHash::Sha512,
    ),
    CipherSuite::new(
        CipherSuiteId::MLS_128_MLKEM768_CHACHA20POLY1305_SHA256_MLDSA65,
        MlsKem::MlKem768,
        MlsSignature::MlDsa65,
        MlsAead::ChaCha20Poly1305,
        MlsHash::Sha256,
    ),
    // SPEC-2 PQC-only registry (0x0B** - ChaCha20Poly1305 only, no hybrids, production)
    CipherSuite::new(
        CipherSuiteId::SPEC2_MLS_128_MLKEM768_CHACHA20POLY1305_SHA256_MLDSA65,
        MlsKem::MlKem768,
        MlsSignature::MlDsa65,
        MlsAead::ChaCha20Poly1305,
        MlsHash::Sha256,
    ),
    CipherSuite::new(
        CipherSuiteId::SPEC2_MLS_256_MLKEM1024_CHACHA20POLY1305_SHA512_MLDSA87,
        MlsKem::MlKem1024,
        MlsSignature::MlDsa87,
        MlsAead::ChaCha20Poly1305,
        MlsHash::Sha512,
    ),
    // SPEC-2 optional SLH-DSA suite (0x0B03)
    CipherSuite::new(
        CipherSuiteId::SPEC2_MLS_192_MLKEM1024_CHACHA20POLY1305_SHA384_SLHDSA192,
        MlsKem::MlKem1024,
        MlsSignature::SlhDsa192,
        MlsAead::ChaCha20Poly1305,
        MlsHash::Sha384,
    ),
];

/// Cryptographic operations using saorsa-pqc as the single source of truth
pub struct Hash {
    pub suite: CipherSuite,
}

impl Hash {
    #[must_use]
    pub fn new(suite: CipherSuite) -> Self {
        Self { suite }
    }

    /// Compute hash of data using BLAKE3 from saorsa-pqc
    #[must_use]
    pub fn hash(&self, data: &[u8]) -> Vec<u8> {
        use saorsa_pqc::api::hash::Blake3Hasher;
        use saorsa_pqc::api::traits::Hash as HashTrait;

        let mut hasher = Blake3Hasher::new();
        hasher.update(data);
        let output = hasher.finalize();
        output.as_ref().to_vec()
    }

    /// Compute HMAC using saorsa-pqc's HMAC-SHA3-256
    ///
    /// # Errors
    ///
    /// Returns `MlsError::CryptoError` if the HMAC key is invalid or computation fails.
    pub fn hmac(&self, key: &[u8], data: &[u8]) -> Result<Vec<u8>> {
        use saorsa_pqc::api::hmac::HmacSha3_256;
        use saorsa_pqc::api::traits::Mac;

        let mut mac = HmacSha3_256::new(key)
            .map_err(|e| MlsError::CryptoError(format!("HMAC key error: {e:?}")))?;
        mac.update(data);
        let output = mac.finalize();
        Ok(output.as_ref().to_vec())
    }
}

/// Key derivation using HKDF
#[derive(Debug, Clone)]
pub struct KeySchedule {
    suite: CipherSuite,
}

impl KeySchedule {
    #[must_use]
    pub fn new(suite: CipherSuite) -> Self {
        Self { suite }
    }

    /// Derive key using saorsa-pqc's HKDF-SHA3-256
    ///
    /// # Errors
    ///
    /// Returns `MlsError::CryptoError` if the HKDF key derivation fails.
    pub fn derive_secret(&self, secret: &[u8], label: &str, context: &[u8]) -> Result<Vec<u8>> {
        use saorsa_pqc::api::kdf::HkdfSha3_256;
        use saorsa_pqc::api::traits::Kdf;

        let info = Self::build_hkdf_label(label, context, self.suite.hash_size());
        let mut output = vec![0u8; self.suite.hash_size()];

        HkdfSha3_256::derive(secret, None, &info, &mut output)
            .map_err(|e| MlsError::CryptoError(format!("HKDF error: {e:?}")))?;
        Ok(output)
    }

    /// Derive multiple keys using saorsa-pqc's HKDF
    ///
    /// # Errors
    ///
    /// Returns `MlsError::CryptoError` if any key derivation fails.
    pub fn derive_keys(
        &self,
        salt: &[u8],
        secret: &[u8],
        labels: &[&str],
        lengths: &[usize],
    ) -> Result<Vec<Vec<u8>>> {
        let mut results = Vec::new();

        for (label, &length) in labels.iter().zip(lengths.iter()) {
            let key = self.derive_secret(secret, label, salt)?;
            results.push(key[..length].to_vec());
        }
        Ok(results)
    }

    /// Derive a single key with specific length using saorsa-pqc's HKDF
    ///
    /// # Errors
    ///
    /// Returns `MlsError::CryptoError` if the HKDF key derivation fails.
    pub fn derive_key(
        &self,
        salt: &[u8],
        secret: &[u8],
        info: &[u8],
        length: usize,
    ) -> Result<Vec<u8>> {
        use saorsa_pqc::api::kdf::HkdfSha3_256;
        use saorsa_pqc::api::traits::Kdf;

        let mut output = vec![0u8; length];
        HkdfSha3_256::derive(secret, Some(salt), info, &mut output)
            .map_err(|e| MlsError::CryptoError(format!("HKDF error: {e:?}")))?;
        Ok(output)
    }

    fn build_hkdf_label(label: &str, context: &[u8], length: usize) -> Vec<u8> {
        let mut info = Vec::new();
        info.extend_from_slice(&u16::try_from(length).unwrap_or(u16::MAX).to_be_bytes());
        info.push(
            u8::try_from(b"tls13 ".len()).unwrap_or(u8::MAX)
                + u8::try_from(label.len()).unwrap_or(u8::MAX),
        );
        info.extend_from_slice(b"tls13 ");
        info.extend_from_slice(label.as_bytes());
        info.push(u8::try_from(context.len()).unwrap_or(u8::MAX));
        info.extend_from_slice(context);
        info
    }

    /// Derive an exported secret per RFC 9420 §8.5
    ///
    /// This implements the MLS exporter interface which allows applications
    /// to derive secrets from the group's exporter secret.
    ///
    /// # Errors
    ///
    /// Returns `MlsError::CryptoError` if the key derivation fails.
    pub fn export_secret(
        &self,
        exporter_secret: &[u8],
        label: &str,
        context: &[u8],
        length: usize,
    ) -> Result<Vec<u8>> {
        use saorsa_pqc::api::kdf::HkdfSha3_256;
        use saorsa_pqc::api::traits::Kdf;

        // Build the exporter label per RFC 9420
        // The label is "mls exporter" || user_label
        let full_label = format!("mls exporter {}", label);
        let info = Self::build_hkdf_label(&full_label, context, length);

        let mut output = vec![0u8; length];
        HkdfSha3_256::derive(exporter_secret, None, &info, &mut output)
            .map_err(|e| MlsError::CryptoError(format!("Exporter derivation failed: {e:?}")))?;

        Ok(output)
    }
}

/// Signature supporting both ML-DSA and SLH-DSA
#[derive(Clone)]
pub enum Signature {
    /// ML-DSA signature
    MlDsa(MlDsaSignature),
    /// SLH-DSA signature
    SlhDsa(SlhDsaSignature),
}

impl Signature {
    /// Convert to bytes
    pub fn to_bytes(&self) -> Vec<u8> {
        match self {
            Signature::MlDsa(sig) => sig.to_bytes().to_vec(),
            Signature::SlhDsa(sig) => sig.to_bytes().to_vec(),
        }
    }
}

/// Debug wrapper for `Signature` to provide Debug, PartialEq, and Serialize traits
#[derive(Clone)]
pub struct DebugSignature(pub Signature);

impl std::fmt::Debug for DebugSignature {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.0 {
            Signature::MlDsa(sig) => write!(f, "MlDsaSignature(<{} bytes>)", sig.to_bytes().len()),
            Signature::SlhDsa(sig) => {
                write!(f, "SlhDsaSignature(<{} bytes>)", sig.to_bytes().len())
            }
        }
    }
}

impl PartialEq for DebugSignature {
    fn eq(&self, other: &Self) -> bool {
        self.0.to_bytes() == other.0.to_bytes()
    }
}

impl Eq for DebugSignature {}

impl serde::Serialize for DebugSignature {
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_bytes(&self.0.to_bytes())
    }
}

impl<'de> serde::Deserialize<'de> for DebugSignature {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let bytes = <Vec<u8>>::deserialize(deserializer)?;
        // Note: We cannot deserialize without knowing the variant, so this will need context
        // For now, assume ML-DSA-65 for backward compatibility
        let signature = MlDsaSignature::from_bytes(MlDsaVariant::MlDsa65, &bytes)
            .map_err(|e| serde::de::Error::custom(format!("Signature decode error: {e:?}")))?;
        Ok(DebugSignature(Signature::MlDsa(signature)))
    }
}

/// Signature key pair supporting both ML-DSA and SLH-DSA
#[derive(Clone)]
pub enum SignatureKey {
    /// ML-DSA signature keys
    MlDsa {
        /// Secret key for signing
        secret: MlDsaSecretKey,
        /// Public key for verification
        public: MlDsaPublicKey,
    },
    /// SLH-DSA signature keys
    SlhDsa {
        /// Secret key for signing
        secret: SlhDsaSecretKey,
        /// Public key for verification
        public: SlhDsaPublicKey,
    },
}

/// Post-quantum key pair for signing and key agreement
pub struct KeyPair {
    /// Signature key pair (ML-DSA or SLH-DSA)
    pub signature_key: SignatureKey,
    /// ML-KEM secret key for decapsulation
    pub kem_secret: MlKemSecretKey,
    /// ML-KEM public key for encapsulation
    pub kem_public: MlKemPublicKey,
    /// Cipher suite
    pub suite: CipherSuite,
}

impl std::fmt::Debug for KeyPair {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("KeyPair")
            .field("suite", &self.suite)
            .field("verifying_key", &"<hidden>")
            .field("kem_public", &"<hidden>")
            .finish_non_exhaustive()
    }
}

impl KeyPair {
    /// Generate a new key pair
    ///
    /// # Aborts
    ///
    /// Aborts the process if the OS CSPRNG-backed key generation fails — an
    /// unrecoverable security invariant, mirroring [`random_bytes`]. (This
    /// cannot happen in practice.)
    #[must_use]
    pub fn generate(suite: CipherSuite) -> Self {
        // Generate signature key pair based on suite type
        let signature_key = if suite.uses_slh_dsa() {
            // `uses_slh_dsa()` guarantees `slh_dsa_variant()` is Ok; abort on the
            // impossible internal-inconsistency case (matches keygen handling).
            let variant = suite
                .slh_dsa_variant()
                .unwrap_or_else(|_| std::process::abort());
            let slh_dsa = SlhDsa::new(variant);
            let (public, secret) = slh_dsa
                .generate_keypair()
                .unwrap_or_else(|_| std::process::abort());
            SignatureKey::SlhDsa { secret, public }
        } else {
            let variant = suite
                .ml_dsa_variant()
                .unwrap_or_else(|_| std::process::abort());
            let ml_dsa = MlDsa::new(variant);
            let (public, secret) = ml_dsa
                .generate_keypair()
                .unwrap_or_else(|_| std::process::abort());
            SignatureKey::MlDsa { secret, public }
        };

        // Generate ML-KEM key pair for key encapsulation
        let ml_kem = MlKem::new(suite.ml_kem_variant());
        let (kem_public, kem_secret) = ml_kem
            .generate_keypair()
            .unwrap_or_else(|_| std::process::abort());

        Self {
            signature_key,
            kem_secret,
            kem_public,
            suite,
        }
    }

    /// Deterministically generate a combined signature + KEM key pair from
    /// `seed`. The same `(suite, seed)` always yields the same keypair, via
    /// `saorsa-pqc`'s FIPS-203/204 seeded keygen
    /// (`MlKem::generate_keypair_from_seed(d, z)`,
    /// `MlDsa::generate_keypair_from_seed(xi)`). `seed` may be any length; it is
    /// HKDF-expanded into independent, domain-separated sub-seeds.
    ///
    /// Note: only ML-DSA suites are deterministic — `saorsa-pqc` provides no
    /// seeded keygen for SLH-DSA, so SLH-DSA suites return an error.
    ///
    /// # Errors
    ///
    /// Returns [`MlsError::CryptoError`] for an SLH-DSA suite or if HKDF fails.
    pub fn generate_from_seed(suite: CipherSuite, seed: &[u8]) -> Result<Self> {
        use saorsa_pqc::api::kdf::HkdfSha3_256;
        use saorsa_pqc::api::traits::Kdf;

        if suite.uses_slh_dsa() {
            return Err(MlsError::CryptoError(
                "deterministic key generation is not supported for SLH-DSA suites".to_string(),
            ));
        }

        // Expand the seed into independent, domain-separated sub-seeds.
        let mut sig_xi = [0u8; 32];
        let mut kem_d = [0u8; 32];
        let mut kem_z = [0u8; 32];
        HkdfSha3_256::derive(seed, None, b"saorsa-mls keypair v1 ml-dsa xi", &mut sig_xi)
            .map_err(|e| MlsError::CryptoError(format!("HKDF error: {e:?}")))?;
        HkdfSha3_256::derive(seed, None, b"saorsa-mls keypair v1 ml-kem d", &mut kem_d)
            .map_err(|e| MlsError::CryptoError(format!("HKDF error: {e:?}")))?;
        HkdfSha3_256::derive(seed, None, b"saorsa-mls keypair v1 ml-kem z", &mut kem_z)
            .map_err(|e| MlsError::CryptoError(format!("HKDF error: {e:?}")))?;

        let ml_dsa = MlDsa::new(suite.ml_dsa_variant()?);
        let (public, secret) = ml_dsa.generate_keypair_from_seed(&sig_xi);
        let signature_key = SignatureKey::MlDsa { secret, public };

        let ml_kem = MlKem::new(suite.ml_kem_variant());
        let (kem_public, kem_secret) = ml_kem.generate_keypair_from_seed(&kem_d, &kem_z);

        sig_xi.zeroize();
        kem_d.zeroize();
        kem_z.zeroize();

        Ok(Self {
            signature_key,
            kem_secret,
            kem_public,
            suite,
        })
    }

    /// Get the public verification key bytes
    #[must_use]
    pub fn verifying_key_bytes(&self) -> Vec<u8> {
        match &self.signature_key {
            SignatureKey::MlDsa { public, .. } => public.to_bytes().to_vec(),
            SignatureKey::SlhDsa { public, .. } => public.to_bytes().to_vec(),
        }
    }

    /// Get the ML-DSA public key. Use [`Self::verifying_key_bytes`] for the
    /// suite-agnostic form.
    ///
    /// # Errors
    ///
    /// Returns [`MlsError::CryptoError`] if this is an SLH-DSA keypair.
    pub fn verifying_key(&self) -> Result<&MlDsaPublicKey> {
        match &self.signature_key {
            SignatureKey::MlDsa { public, .. } => Ok(public),
            SignatureKey::SlhDsa { .. } => Err(MlsError::CryptoError(
                "verifying_key() called on an SLH-DSA keypair".to_string(),
            )),
        }
    }

    /// Get the public KEM key
    #[must_use]
    pub fn public_key(&self) -> &MlKemPublicKey {
        &self.kem_public
    }

    /// Sign a message
    ///
    /// # Errors
    ///
    /// Returns `MlsError::CryptoError` if the signing operation fails.
    pub fn sign(&self, message: &[u8]) -> Result<Signature> {
        match &self.signature_key {
            SignatureKey::MlDsa { secret, .. } => {
                let ml_dsa = MlDsa::new(self.suite.ml_dsa_variant()?);
                ml_dsa
                    .sign(secret, message)
                    .map(Signature::MlDsa)
                    .map_err(|e| MlsError::CryptoError(format!("ML-DSA signing failed: {e:?}")))
            }
            SignatureKey::SlhDsa { secret, .. } => {
                let slh_dsa = SlhDsa::new(self.suite.slh_dsa_variant()?);
                slh_dsa
                    .sign(secret, message)
                    .map(Signature::SlhDsa)
                    .map_err(|e| MlsError::CryptoError(format!("SLH-DSA signing failed: {e:?}")))
            }
        }
    }

    /// Verify a signature
    ///
    /// Returns `true` if signature is valid, `false` otherwise.
    pub fn verify(&self, message: &[u8], signature: &Signature) -> bool {
        match (&self.signature_key, signature) {
            (SignatureKey::MlDsa { public, .. }, Signature::MlDsa(sig)) => {
                let Ok(variant) = self.suite.ml_dsa_variant() else {
                    return false;
                };
                let ml_dsa = MlDsa::new(variant);
                ml_dsa.verify(public, message, sig).unwrap_or(false)
            }
            (SignatureKey::SlhDsa { public, .. }, Signature::SlhDsa(sig)) => {
                let Ok(variant) = self.suite.slh_dsa_variant() else {
                    return false;
                };
                let slh_dsa = SlhDsa::new(variant);
                slh_dsa.verify(public, message, sig).unwrap_or(false)
            }
            // Signature type mismatch
            _ => false,
        }
    }

    /// Perform key encapsulation
    ///
    /// # Errors
    ///
    /// Returns `MlsError::CryptoError` if the encapsulation operation fails.
    pub fn encapsulate(
        &self,
        recipient_public: &MlKemPublicKey,
    ) -> Result<(MlKemCiphertext, MlKemSharedSecret)> {
        let ml_kem = MlKem::new(self.suite.ml_kem_variant());
        let (shared_secret, ciphertext) = ml_kem
            .encapsulate(recipient_public)
            .map_err(|e| MlsError::CryptoError(format!("Encapsulation failed: {e:?}")))?;
        Ok((ciphertext, shared_secret))
    }

    /// Perform key decapsulation (for FIPS KATs)
    ///
    /// # Errors
    ///
    /// Returns `MlsError::CryptoError` if the decapsulation operation fails.
    pub fn kem_decaps(&self, ciphertext: &[u8]) -> Result<Vec<u8>> {
        let ml_kem = MlKem::new(self.suite.ml_kem_variant());

        // Convert bytes to ciphertext
        let ct = MlKemCiphertext::from_bytes(self.suite.ml_kem_variant(), ciphertext)
            .map_err(|e| MlsError::CryptoError(format!("Invalid ciphertext: {e:?}")))?;

        let shared_secret = ml_kem
            .decapsulate(&self.kem_secret, &ct)
            .map_err(|e| MlsError::CryptoError(format!("Decapsulation failed: {e:?}")))?;

        Ok(shared_secret.to_bytes().to_vec())
    }

    /// Perform key decapsulation
    ///
    /// # Errors
    ///
    /// Returns `MlsError::CryptoError` if the decapsulation operation fails.
    pub fn decapsulate(&self, ciphertext: &MlKemCiphertext) -> Result<MlKemSharedSecret> {
        let ml_kem = MlKem::new(self.suite.ml_kem_variant());
        ml_kem
            .decapsulate(&self.kem_secret, ciphertext)
            .map_err(|e| MlsError::CryptoError(format!("Decapsulation failed: {e:?}")))
    }
}

/// AEAD encryption/decryption using saorsa-pqc's `ChaCha20Poly1305`
#[derive(Debug)]
pub struct AeadCipher {
    key: Vec<u8>,
    suite: CipherSuite,
}

impl AeadCipher {
    /// Create a new AEAD cipher from key material
    ///
    /// # Errors
    ///
    /// Returns `MlsError::CryptoError` if the key size is invalid.
    pub fn new(key: Vec<u8>, suite: CipherSuite) -> Result<Self> {
        if key.len() != suite.key_size() {
            return Err(MlsError::CryptoError(format!(
                "Invalid key size: expected {}, got {}",
                suite.key_size(),
                key.len()
            )));
        }

        Ok(Self { key, suite })
    }

    /// Encrypt plaintext with associated data using saorsa-pqc's `ChaCha20Poly1305`
    ///
    /// # Errors
    ///
    /// Returns `MlsError::CryptoError` if the nonce size is invalid or encryption fails.
    pub fn encrypt(
        &self,
        nonce: &[u8],
        plaintext: &[u8],
        associated_data: &[u8],
    ) -> Result<Vec<u8>> {
        // Use saorsa-pqc's ChaCha20Poly1305
        use saorsa_pqc::api::symmetric::ChaCha20Poly1305 as PqcCipher;

        if nonce.len() != self.suite.nonce_size() {
            return Err(MlsError::CryptoError("Invalid nonce size".to_string()));
        }

        // Convert key to the format expected by saorsa-pqc
        let key_array: [u8; 32] = self
            .key
            .clone()
            .try_into()
            .map_err(|_| MlsError::CryptoError("Invalid key size".to_string()))?;
        let key = chacha20poly1305::Key::from(key_array);

        let cipher = PqcCipher::new(&key);

        // Convert nonce
        let nonce_array: [u8; 12] = nonce
            .try_into()
            .map_err(|_| MlsError::CryptoError("Invalid nonce size".to_string()))?;
        let nonce_obj = chacha20poly1305::Nonce::from(nonce_array);

        // Encrypt with AAD
        let ciphertext = cipher
            .encrypt_with_aad(&nonce_obj, plaintext, associated_data)
            .map_err(|e| MlsError::CryptoError(format!("Encryption failed: {e:?}")))?;

        // Return nonce + ciphertext for wire format
        let mut result = nonce.to_vec();
        result.extend_from_slice(&ciphertext);
        Ok(result)
    }

    /// Decrypt ciphertext with associated data using saorsa-pqc's `ChaCha20Poly1305`
    ///
    /// # Errors
    ///
    /// Returns `MlsError::CryptoError` if the nonce size is invalid, ciphertext is too short, or decryption fails.
    pub fn decrypt(
        &self,
        nonce: &[u8],
        ciphertext: &[u8],
        associated_data: &[u8],
    ) -> Result<Vec<u8>> {
        // Use saorsa-pqc's ChaCha20Poly1305
        use saorsa_pqc::api::symmetric::ChaCha20Poly1305 as PqcCipher;

        if nonce.len() != self.suite.nonce_size() {
            return Err(MlsError::CryptoError("Invalid nonce size".to_string()));
        }

        // The ciphertext includes the nonce at the beginning (12 bytes)
        // Skip it and use it for decryption
        if ciphertext.len() < 12 {
            return Err(MlsError::CryptoError("Ciphertext too short".to_string()));
        }

        let actual_nonce = &ciphertext[..12];
        let actual_ciphertext = &ciphertext[12..];

        // Convert key to the format expected by saorsa-pqc
        let key_array: [u8; 32] = self
            .key
            .clone()
            .try_into()
            .map_err(|_| MlsError::CryptoError("Invalid key size".to_string()))?;
        let key = chacha20poly1305::Key::from(key_array);

        let cipher = PqcCipher::new(&key);

        // Convert nonce
        let nonce_array: [u8; 12] = actual_nonce
            .try_into()
            .map_err(|_| MlsError::CryptoError("Invalid nonce size".to_string()))?;
        let nonce_obj = chacha20poly1305::Nonce::from(nonce_array);

        // Decrypt with AAD
        let plaintext = cipher
            .decrypt_with_aad(&nonce_obj, actual_ciphertext, associated_data)
            .map_err(|e| MlsError::CryptoError(format!("Decryption failed: {e:?}")))?;

        Ok(plaintext)
    }

    /// Get the key size for this cipher
    #[must_use]
    pub fn key_size(&self) -> usize {
        self.suite.key_size()
    }

    /// Get the nonce size for this cipher
    #[must_use]
    pub fn nonce_size(&self) -> usize {
        self.suite.nonce_size()
    }
}

/// Generate cryptographically secure random bytes via the OS CSPRNG.
///
/// # Panics
///
/// Aborts the process if the OS random number generator is unavailable.
/// This is an unrecoverable security invariant — cryptographic operations
/// cannot proceed safely without a functioning CSPRNG.
#[must_use]
pub fn random_bytes(len: usize) -> Vec<u8> {
    let mut bytes = vec![0u8; len];
    getrandom::fill(&mut bytes).unwrap_or_else(|_| std::process::abort());
    bytes
}

/// Constant-time comparison using subtle crate (already a dependency of saorsa-pqc)
#[must_use]
pub fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
    use subtle::ConstantTimeEq;
    a.ct_eq(b).into()
}

/// MLS-specific KDF labels
pub mod labels {
    pub const ENCRYPTION: &str = "encryption";
    pub const AUTHENTICATION: &str = "authentication";
    pub const EXPORTER: &str = "exporter";
    pub const EXTERNAL: &str = "external";
    pub const CONFIRM: &str = "confirm";
    pub const MEMBERSHIP: &str = "membership";
    pub const RESUMPTION: &str = "resumption";
    pub const INIT: &str = "init";
    pub const SENDER_DATA: &str = "sender data";
    pub const WELCOME: &str = "welcome";
    pub const HANDSHAKE: &str = "handshake";
    pub const APPLICATION: &str = "application";
    // Additional MLS labels
    pub const EPOCH_SECRET: &str = "epoch";
    pub const SENDER_DATA_SECRET: &str = "sender data secret";
    pub const HANDSHAKE_SECRET: &str = "handshake secret";
    pub const APPLICATION_SECRET: &str = "application secret";
    pub const EXPORTER_SECRET: &str = "exporter secret";
    pub const AUTHENTICATION_SECRET: &str = "authentication secret";
    pub const EXTERNAL_SECRET: &str = "external secret";
    pub const CONFIRMATION_KEY: &str = "confirmation key";
    pub const MEMBERSHIP_KEY: &str = "membership key";
    pub const RESUMPTION_PSK: &str = "resumption psk";
    pub const INIT_SECRET: &str = "init secret";
}

/// Secure bytes that are zeroed on drop
#[derive(Debug, Clone, Zeroize, ZeroizeOnDrop)]
pub struct SecretBytes {
    inner: Vec<u8>,
}

impl SecretBytes {
    #[must_use]
    pub fn new(bytes: Vec<u8>) -> Self {
        Self { inner: bytes }
    }

    #[must_use]
    pub fn as_bytes(&self) -> &[u8] {
        &self.inner
    }

    #[must_use]
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }
}

impl From<Vec<u8>> for SecretBytes {
    fn from(bytes: Vec<u8>) -> Self {
        Self::new(bytes)
    }
}

/// HPKE context for encryption/decryption operations
///
/// Wraps saorsa-pqc's HPKE context and provides MLS-specific interface
pub struct HpkeContext {
    inner: PqcHpkeContext,
}

impl HpkeContext {
    /// Export secret material for key derivation
    ///
    /// # Errors
    ///
    /// Returns `MlsError::CryptoError` if the export operation fails
    pub fn export(&mut self, context: &[u8], length: usize) -> Result<Vec<u8>> {
        self.inner
            .export(context, length)
            .map_err(|e| MlsError::CryptoError(format!("HPKE export failed: {e:?}")))
    }

    /// Seal (encrypt) plaintext with associated data
    ///
    /// # Errors
    ///
    /// Returns `MlsError::CryptoError` if encryption fails
    pub fn seal(&mut self, plaintext: &[u8], aad: &[u8]) -> Result<Vec<u8>> {
        self.inner
            .seal(plaintext, aad)
            .map_err(|e| MlsError::CryptoError(format!("HPKE seal failed: {e:?}")))
    }

    /// Open (decrypt) ciphertext with associated data
    ///
    /// # Errors
    ///
    /// Returns `MlsError::CryptoError` if decryption or authentication fails
    pub fn open(&mut self, ciphertext: &[u8], aad: &[u8]) -> Result<Vec<u8>> {
        self.inner
            .open(ciphertext, aad)
            .map_err(|e| MlsError::CryptoError(format!("HPKE open failed: {e:?}")))
    }
}

impl CipherSuite {
    /// Get HPKE configuration for this ciphersuite
    fn hpke_config(&self) -> HpkeConfig {
        HpkeConfig {
            kem: self.ml_kem_variant(),
            kdf: match self.hash {
                MlsHash::Sha256 | MlsHash::Sha3_256 | MlsHash::Blake3 => KdfAlgorithm::HkdfSha3_256,
                MlsHash::Sha384 | MlsHash::Sha512 | MlsHash::Sha3_512 => KdfAlgorithm::HkdfSha3_512,
            },
            aead: match self.aead {
                MlsAead::ChaCha20Poly1305 => saorsa_pqc::api::aead::AeadCipher::ChaCha20Poly1305,
                // Note: saorsa-pqc only has AES256GCM, using it for both AES128 and AES256
                MlsAead::Aes128Gcm | MlsAead::Aes256Gcm => {
                    saorsa_pqc::api::aead::AeadCipher::Aes256Gcm
                }
            },
        }
    }

    /// Single-shot HPKE seal operation
    ///
    /// # Errors
    ///
    /// Returns `MlsError::CryptoError` if HPKE setup or encryption fails
    pub fn hpke_seal(
        &self,
        recipient_public_key: &MlKemPublicKey,
        plaintext: &[u8],
        aad: &[u8],
        info: &[u8],
    ) -> Result<(Vec<u8>, Vec<u8>)> {
        let config = self.hpke_config();
        let sender = HpkeSender::new(config);

        // Convert public key to bytes
        let pk_bytes = recipient_public_key.to_bytes();

        // Setup and get context
        let (encapped_key, mut ctx) = sender
            .setup_base(&pk_bytes, info)
            .map_err(|e| MlsError::CryptoError(format!("HPKE setup failed: {e:?}")))?;

        // Seal the plaintext
        let ciphertext = ctx
            .seal(plaintext, aad)
            .map_err(|e| MlsError::CryptoError(format!("HPKE seal failed: {e:?}")))?;

        Ok((encapped_key, ciphertext))
    }

    /// KEM encapsulation with seed (for FIPS KATs)
    ///
    /// Note: Current implementation uses standard encapsulation.
    /// Deterministic encapsulation from seed requires RNG seeding support
    /// in saorsa-pqc which is pending upstream.
    ///
    /// # Errors
    ///
    /// Returns `MlsError::CryptoError` if encapsulation fails
    pub fn kem_encaps_with_seed(
        &self,
        recipient_public_key: &MlKemPublicKey,
        _seed: &[u8],
    ) -> Result<(Vec<u8>, Vec<u8>)> {
        // TODO: Use seed when saorsa-pqc provides RNG seeding APIs
        // For now, use standard encapsulation to validate algorithm correctness
        let ml_kem = MlKem::new(self.ml_kem_variant());
        let (shared_secret, ciphertext) = ml_kem
            .encapsulate(recipient_public_key)
            .map_err(|e| MlsError::CryptoError(format!("Encapsulation failed: {e:?}")))?;

        Ok((ciphertext.to_bytes(), shared_secret.to_bytes().to_vec()))
    }

    /// Setup HPKE sender context
    ///
    /// Returns encapsulated key and sender context for multiple encryptions
    ///
    /// # Errors
    ///
    /// Returns `MlsError::CryptoError` if HPKE setup fails
    pub fn hpke_setup_sender(
        &self,
        recipient_public_key: &MlKemPublicKey,
        info: &[u8],
    ) -> Result<(Vec<u8>, HpkeContext)> {
        let config = self.hpke_config();
        let sender = HpkeSender::new(config);

        let pk_bytes = recipient_public_key.to_bytes();

        let (encapped_key, ctx) = sender
            .setup_base(&pk_bytes, info)
            .map_err(|e| MlsError::CryptoError(format!("HPKE sender setup failed: {e:?}")))?;

        Ok((encapped_key, HpkeContext { inner: ctx }))
    }
}

impl KeyPair {
    /// Single-shot HPKE open operation
    ///
    /// # Errors
    ///
    /// Returns `MlsError::CryptoError` if HPKE setup or decryption fails
    pub fn hpke_open(
        &self,
        encapped_key: &[u8],
        ciphertext: &[u8],
        aad: &[u8],
        info: &[u8],
    ) -> Result<Vec<u8>> {
        let mut ctx = self.hpke_setup_receiver(encapped_key, info)?;
        ctx.open(ciphertext, aad)
    }

    /// Setup HPKE receiver context
    ///
    /// Returns receiver context for multiple decryptions
    ///
    /// # Errors
    ///
    /// Returns `MlsError::CryptoError` if HPKE setup fails
    pub fn hpke_setup_receiver(&self, encapped_key: &[u8], info: &[u8]) -> Result<HpkeContext> {
        let config = HpkeConfig {
            kem: self.suite.ml_kem_variant(),
            kdf: match self.suite.hash {
                MlsHash::Sha256 | MlsHash::Sha3_256 | MlsHash::Blake3 => KdfAlgorithm::HkdfSha3_256,
                MlsHash::Sha384 | MlsHash::Sha512 | MlsHash::Sha3_512 => KdfAlgorithm::HkdfSha3_512,
            },
            aead: match self.suite.aead {
                MlsAead::ChaCha20Poly1305 => saorsa_pqc::api::aead::AeadCipher::ChaCha20Poly1305,
                // Note: saorsa-pqc only has AES256GCM, using it for both AES128 and AES256
                MlsAead::Aes128Gcm | MlsAead::Aes256Gcm => {
                    saorsa_pqc::api::aead::AeadCipher::Aes256Gcm
                }
            },
        };

        let recipient = HpkeRecipient::new(config);

        // Convert secret key to bytes
        let sk_bytes = self.kem_secret.to_bytes();

        let ctx = recipient
            .setup_base(encapped_key, &sk_bytes, info)
            .map_err(|e| MlsError::CryptoError(format!("HPKE recipient setup failed: {e:?}")))?;

        Ok(HpkeContext { inner: ctx })
    }
}

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

    #[test]
    fn test_cipher_suite_defaults() {
        let suite = CipherSuite::default();
        // SPEC-2: Default is ChaCha20Poly1305 (0x0B01)
        assert_eq!(
            suite.id(),
            CipherSuiteId::SPEC2_MLS_128_MLKEM768_CHACHA20POLY1305_SHA256_MLDSA65
        );
        assert_eq!(suite.key_size(), 32); // ChaCha20 = 32 bytes
        assert_eq!(suite.nonce_size(), 12);
        assert!(suite.is_pqc_only());
        assert!(suite.is_spec2());
    }

    #[test]
    fn test_hash_operations() {
        let hash = Hash::new(CipherSuite::default());
        let data = b"test data";
        let result = hash.hash(data);
        assert_eq!(result.len(), 32);

        let key = b"test key";
        let hmac_result = hash.hmac(key, data).unwrap();
        assert!(!hmac_result.is_empty());
    }

    #[test]
    fn test_key_generation() {
        let kp1 = KeyPair::generate(CipherSuite::default());
        let kp2 = KeyPair::generate(CipherSuite::default());

        // Keys should be different
        assert_ne!(kp1.verifying_key_bytes(), kp2.verifying_key_bytes());
    }

    #[test]
    fn test_signing_and_verification() {
        let kp = KeyPair::generate(CipherSuite::default());
        let message = b"test message";

        let signature = kp.sign(message).unwrap();

        // Test with correct message
        assert!(kp.verify(message, &signature));

        // Test with wrong message - should NOT verify
        let wrong_message = b"wrong message";
        assert!(
            !kp.verify(wrong_message, &signature),
            "Signature should not verify with wrong message"
        );
    }

    #[test]
    fn test_key_encapsulation() {
        let kp1 = KeyPair::generate(CipherSuite::default());
        let kp2 = KeyPair::generate(CipherSuite::default());

        // Encapsulate for kp2
        let (ciphertext, shared_secret1) = kp1.encapsulate(&kp2.kem_public).unwrap();

        // Decapsulate with kp2's secret
        let shared_secret2 = kp2.decapsulate(&ciphertext).unwrap();

        // Shared secrets should match
        assert_eq!(shared_secret1.to_bytes(), shared_secret2.to_bytes());
    }

    #[test]
    fn test_aead_encryption() {
        // Use ChaCha20Poly1305 suite since AEAD implementation currently only supports that
        let suite = CipherSuite::from_id(
            CipherSuiteId::SPEC2_MLS_128_MLKEM768_CHACHA20POLY1305_SHA256_MLDSA65,
        )
        .expect("ChaCha20 suite should exist");
        let key = random_bytes(suite.key_size()); // Use correct key size for suite
        let cipher = AeadCipher::new(key, suite).unwrap();
        let nonce = random_bytes(12);
        let plaintext = b"secret message";
        let aad = b"associated data";

        let ciphertext = cipher.encrypt(&nonce, plaintext, aad).unwrap();
        let decrypted = cipher.decrypt(&nonce, &ciphertext, aad).unwrap();

        assert_eq!(decrypted, plaintext);
    }

    #[test]
    fn test_key_derivation() {
        let ks = KeySchedule::new(CipherSuite::default());
        let secret = random_bytes(32);
        let derived = ks.derive_secret(&secret, "test", b"context").unwrap();
        assert_eq!(derived.len(), 32);
    }

    #[test]
    fn test_secret_bytes_zeroize() {
        let data = vec![1, 2, 3, 4, 5];
        let secret = SecretBytes::new(data.clone());
        assert_eq!(secret.as_bytes(), &data);
        assert_eq!(secret.len(), 5);
        assert!(!secret.is_empty());
        // SecretBytes will be zeroed when dropped
    }

    // SPEC-2 PQC-only suite tests

    #[test]
    fn test_spec2_default_suite() {
        let suite = CipherSuite::default();
        // SPEC-2 default is 0x0B01 (ChaCha20Poly1305)
        assert_eq!(
            suite.id(),
            CipherSuiteId::SPEC2_MLS_128_MLKEM768_CHACHA20POLY1305_SHA256_MLDSA65,
            "Default should be SPEC-2 PQC-only suite 0x0B01"
        );
        assert_eq!(suite.kem(), MlsKem::MlKem768);
        assert_eq!(suite.signature(), MlsSignature::MlDsa65);
        assert_eq!(suite.aead(), MlsAead::ChaCha20Poly1305);
        assert_eq!(suite.hash(), MlsHash::Sha256);
        assert!(suite.is_pqc_only(), "Default suite must be PQC-only");
        assert!(suite.is_spec2(), "Default suite must be SPEC-2");
    }

    #[test]
    fn test_spec2_suite_0xb01_chacha_sha256() {
        let suite = CipherSuite::from_id(
            CipherSuiteId::SPEC2_MLS_128_MLKEM768_CHACHA20POLY1305_SHA256_MLDSA65,
        )
        .expect("Suite 0x0B01 should exist");
        assert_eq!(suite.key_size(), 32, "ChaCha20 key size");
        assert_eq!(suite.hash_size(), 32, "SHA256 hash size");
        assert_eq!(suite.aead(), MlsAead::ChaCha20Poly1305);
        assert!(suite.is_pqc_only());
        assert!(suite.is_spec2());
        assert!(!suite.is_deprecated());
    }

    #[test]
    fn test_spec2_suite_0xb02_chacha_sha512() {
        let suite = CipherSuite::from_id(
            CipherSuiteId::SPEC2_MLS_256_MLKEM1024_CHACHA20POLY1305_SHA512_MLDSA87,
        )
        .expect("Suite 0x0B02 should exist");
        assert_eq!(suite.kem(), MlsKem::MlKem1024, "High-security ML-KEM-1024");
        assert_eq!(
            suite.signature(),
            MlsSignature::MlDsa87,
            "High-security ML-DSA-87"
        );
        assert_eq!(suite.aead(), MlsAead::ChaCha20Poly1305);
        assert_eq!(suite.hash(), MlsHash::Sha512);
        assert_eq!(suite.key_size(), 32, "ChaCha20 key size");
        assert_eq!(suite.hash_size(), 64, "SHA512 hash size");
        assert!(suite.is_pqc_only());
    }

    #[test]
    #[allow(deprecated)]
    fn test_hybrid_suite_not_pqc_only() {
        let suite = CipherSuite::from_id(
            CipherSuiteId::MLS_128_HYBRID_X25519_MLKEM768_AES128GCM_SHA256_MLDSA65,
        )
        .expect("Hybrid suite should exist for backwards compat");
        assert!(!suite.is_pqc_only(), "Hybrid suite must NOT be PQC-only");
        assert!(!suite.is_spec2(), "Hybrid suite is SPEC-PROD");
        assert!(suite.is_deprecated(), "Hybrid suite should be deprecated");
    }

    #[test]
    #[allow(deprecated)]
    fn test_deprecated_suites_not_pqc_only() {
        let deprecated_ids = [
            CipherSuiteId::MLS_128_MLKEM768_AES128GCM_SHA256_MLDSA65,
            CipherSuiteId::MLS_256_MLKEM1024_AES256GCM_SHA512_MLDSA87,
            CipherSuiteId::MLS_128_MLKEM768_CHACHA20POLY1305_SHA256_MLDSA65,
        ];

        for id in &deprecated_ids {
            let suite =
                CipherSuite::from_id(*id).expect("Deprecated suite should still be in registry");
            assert!(suite.is_deprecated(), "Suite {:?} should be deprecated", id);
            assert!(!suite.is_spec2(), "Suite {:?} is not SPEC-2", id);
            // Note: Deprecated PQC-only suites (0x0A01, 0x0A03, 0x0A04) are actually PQC-only,
            // but we encourage migration to SPEC-2 range (0x0B**)
        }
    }

    #[test]
    fn test_pqc_only_policy_enforcement() {
        // SPEC-2 suites must be PQC-only with ChaCha20Poly1305
        let spec2_ids = [
            CipherSuiteId::SPEC2_MLS_128_MLKEM768_CHACHA20POLY1305_SHA256_MLDSA65,
            CipherSuiteId::SPEC2_MLS_256_MLKEM1024_CHACHA20POLY1305_SHA512_MLDSA87,
        ];

        for id in &spec2_ids {
            let suite = CipherSuite::from_id(*id).expect("SPEC-2 suite should exist");
            assert!(
                suite.is_pqc_only(),
                "SPEC-2 suite {:?} must be PQC-only",
                id
            );
            assert_eq!(
                suite.aead(),
                MlsAead::ChaCha20Poly1305,
                "SPEC-2 suite {:?} must use ChaCha20Poly1305",
                id
            );
        }
    }

    #[test]
    fn test_sha384_hash_size() {
        // Test the new SHA384 hash variant with ChaCha20Poly1305
        let suite = CipherSuite::new(
            CipherSuiteId::SPEC2_MLS_192_MLKEM1024_CHACHA20POLY1305_SHA384_SLHDSA192,
            MlsKem::MlKem1024,
            MlsSignature::SlhDsa192,
            MlsAead::ChaCha20Poly1305,
            MlsHash::Sha384,
        );
        assert_eq!(suite.hash_size(), 48, "SHA384 produces 48-byte output");
    }

    #[test]
    fn test_registry_contains_seven_suites() {
        let all_suites = CipherSuite::all();
        assert_eq!(
            all_suites.len(),
            7,
            "Registry should contain 4 SPEC-PROD + 3 SPEC-2 suites (including optional SLH-DSA)"
        );
    }

    #[test]
    fn test_suite_id_to_u16() {
        assert_eq!(
            CipherSuiteId::SPEC2_MLS_128_MLKEM768_CHACHA20POLY1305_SHA256_MLDSA65.as_u16(),
            0x0B01
        );
        assert_eq!(
            CipherSuiteId::SPEC2_MLS_256_MLKEM1024_CHACHA20POLY1305_SHA512_MLDSA87.as_u16(),
            0x0B02
        );
        assert_eq!(
            CipherSuiteId::SPEC2_MLS_192_MLKEM1024_CHACHA20POLY1305_SHA384_SLHDSA192.as_u16(),
            0x0B03
        );
    }

    #[test]
    fn test_all_spec2_suites_functional() {
        // Verify all SPEC-2 suites can perform basic crypto operations
        let spec2_ids = [
            CipherSuiteId::SPEC2_MLS_128_MLKEM768_CHACHA20POLY1305_SHA256_MLDSA65,
            CipherSuiteId::SPEC2_MLS_256_MLKEM1024_CHACHA20POLY1305_SHA512_MLDSA87,
        ];

        for id in &spec2_ids {
            let suite = CipherSuite::from_id(*id).expect("Suite should exist");

            // Test key generation
            let kp = KeyPair::generate(suite);
            assert!(!kp.verifying_key_bytes().is_empty());

            // Test signing
            let message = b"test message for SPEC-2";
            let signature = kp.sign(message).expect("Signing should succeed");
            assert!(
                kp.verify(message, &signature),
                "Verification should succeed for suite {:?}",
                id
            );
        }
    }
}

/// Debug wrapper for `MlDsaSignature` to work around missing Debug impl
#[derive(Clone)]
pub struct DebugMlDsaSignature(pub MlDsaSignature);

impl std::fmt::Debug for DebugMlDsaSignature {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "MlDsaSignature(<{} bytes>)", self.0.to_bytes().len())
    }
}

impl PartialEq for DebugMlDsaSignature {
    fn eq(&self, other: &Self) -> bool {
        self.0.to_bytes() == other.0.to_bytes()
    }
}

impl Eq for DebugMlDsaSignature {}

impl Serialize for DebugMlDsaSignature {
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serde_wrappers::serialize_ml_dsa_signature(&self.0, serializer)
    }
}

impl<'de> Deserialize<'de> for DebugMlDsaSignature {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let signature = serde_wrappers::deserialize_ml_dsa_signature(deserializer)?;
        Ok(DebugMlDsaSignature(signature))
    }
}

/// Debug wrapper for `MlDsaPublicKey` to work around missing Debug impl
#[derive(Clone)]
pub struct DebugMlDsaPublicKey(pub MlDsaPublicKey);

impl std::fmt::Debug for DebugMlDsaPublicKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "MlDsaPublicKey(<{} bytes>)", self.0.to_bytes().len())
    }
}

impl PartialEq for DebugMlDsaPublicKey {
    fn eq(&self, other: &Self) -> bool {
        self.0.to_bytes() == other.0.to_bytes()
    }
}

impl Eq for DebugMlDsaPublicKey {}

impl Serialize for DebugMlDsaPublicKey {
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serde_wrappers::serialize_ml_dsa_public_key(&self.0, serializer)
    }
}

impl<'de> Deserialize<'de> for DebugMlDsaPublicKey {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let key = serde_wrappers::deserialize_ml_dsa_public_key(deserializer)?;
        Ok(DebugMlDsaPublicKey(key))
    }
}

/// Serde wrappers for saorsa-pqc types
pub mod serde_wrappers {
    use super::{
        DebugMlDsaPublicKey, DebugMlDsaSignature, MlDsaPublicKey, MlDsaSignature, MlKemCiphertext,
    };
    use saorsa_pqc::{MlDsaVariant, MlKemVariant};
    use serde::{Deserialize, Deserializer, Serializer};

    /// Serialize `MlKemCiphertext`
    ///
    /// # Errors
    ///
    /// Returns a serialization error if the ciphertext cannot be serialized.
    pub fn serialize_ml_kem_ciphertext<S>(
        ciphertext: &MlKemCiphertext,
        serializer: S,
    ) -> std::result::Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let bytes = ciphertext.to_bytes();
        let variant = ciphertext.variant();
        serializer.serialize_str(&format!("{}:{}", variant as u8, hex::encode(&bytes)))
    }

    /// Deserialize `MlKemCiphertext`
    ///
    /// # Errors
    ///
    /// Returns a deserialization error if the ciphertext cannot be deserialized.
    pub fn deserialize_ml_kem_ciphertext<'de, D>(
        deserializer: D,
    ) -> std::result::Result<MlKemCiphertext, D::Error>
    where
        D: Deserializer<'de>,
    {
        use serde::de::Error;
        let s = String::deserialize(deserializer)?;
        let parts: Vec<&str> = s.split(':').collect();
        if parts.len() != 2 {
            return Err(D::Error::custom("Invalid MlKemCiphertext format"));
        }

        let variant = match parts[0] {
            "0" => MlKemVariant::MlKem512,
            "1" => MlKemVariant::MlKem768,
            "2" => MlKemVariant::MlKem1024,
            _ => return Err(D::Error::custom("Invalid MlKemVariant")),
        };

        let bytes = hex::decode(parts[1])
            .map_err(|e| D::Error::custom(format!("Hex decode error: {e}")))?;

        MlKemCiphertext::from_bytes(variant, &bytes)
            .map_err(|e| D::Error::custom(format!("MlKemCiphertext decode error: {e:?}")))
    }

    /// Serialize `MlDsaSignature`
    ///
    /// # Errors
    ///
    /// Returns a serialization error if the signature cannot be serialized.
    pub fn serialize_ml_dsa_signature<S>(
        signature: &MlDsaSignature,
        serializer: S,
    ) -> std::result::Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let bytes = signature.to_bytes();
        serializer.serialize_str(&hex::encode(bytes))
    }

    /// Deserialize `MlDsaSignature`
    ///
    /// # Errors
    ///
    /// Returns a deserialization error if the signature cannot be deserialized.
    pub fn deserialize_ml_dsa_signature<'de, D>(
        deserializer: D,
    ) -> std::result::Result<MlDsaSignature, D::Error>
    where
        D: Deserializer<'de>,
    {
        use serde::de::Error;
        let s = String::deserialize(deserializer)?;
        let bytes =
            hex::decode(&s).map_err(|e| D::Error::custom(format!("Hex decode error: {e}")))?;

        // Create from bytes - we'll use MlDsa65 as default
        if bytes.len() != 3309 {
            // ML-DSA-65 signature size
            return Err(D::Error::custom("Invalid MlDsaSignature size"));
        }

        let array: [u8; 3309] = bytes
            .try_into()
            .map_err(|_| D::Error::custom("Failed to convert to array"))?;

        MlDsaSignature::from_bytes(MlDsaVariant::MlDsa65, &array)
            .map_err(|e| D::Error::custom(format!("MlDsaSignature decode error: {e:?}")))
    }

    /// Serialize `MlDsaPublicKey`
    ///
    /// # Errors
    ///
    /// Returns a serialization error if the public key cannot be serialized.
    pub fn serialize_ml_dsa_public_key<S>(
        key: &MlDsaPublicKey,
        serializer: S,
    ) -> std::result::Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let bytes = key.to_bytes();
        serializer.serialize_str(&hex::encode(bytes))
    }

    /// Deserialize `MlDsaPublicKey`
    ///
    /// # Errors
    ///
    /// Returns a deserialization error if the public key cannot be deserialized.
    pub fn deserialize_ml_dsa_public_key<'de, D>(
        deserializer: D,
    ) -> std::result::Result<MlDsaPublicKey, D::Error>
    where
        D: Deserializer<'de>,
    {
        use serde::de::Error;
        let s = String::deserialize(deserializer)?;
        let bytes =
            hex::decode(&s).map_err(|e| D::Error::custom(format!("Hex decode error: {e}")))?;

        // Create from bytes - we'll use MlDsa65 as default
        if bytes.len() != 1952 {
            // ML-DSA-65 public key size
            return Err(D::Error::custom("Invalid MlDsaPublicKey size"));
        }

        let array: [u8; 1952] = bytes
            .try_into()
            .map_err(|_| D::Error::custom("Failed to convert to array"))?;

        MlDsaPublicKey::from_bytes(MlDsaVariant::MlDsa65, &array)
            .map_err(|e| D::Error::custom(format!("MlDsaPublicKey decode error: {e:?}")))
    }

    /// Serialize `DebugMlDsaSignature` wrapper
    ///
    /// # Errors
    ///
    /// Returns a serialization error if the signature wrapper cannot be serialized.
    pub fn serialize_debug_ml_dsa_signature<S>(
        signature: &DebugMlDsaSignature,
        serializer: S,
    ) -> std::result::Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serialize_ml_dsa_signature(&signature.0, serializer)
    }

    /// Deserialize `DebugMlDsaSignature` wrapper
    ///
    /// # Errors
    ///
    /// Returns a deserialization error if the signature wrapper cannot be deserialized.
    pub fn deserialize_debug_ml_dsa_signature<'de, D>(
        deserializer: D,
    ) -> std::result::Result<DebugMlDsaSignature, D::Error>
    where
        D: Deserializer<'de>,
    {
        let signature = deserialize_ml_dsa_signature(deserializer)?;
        Ok(DebugMlDsaSignature(signature))
    }

    /// Serialize `DebugMlDsaPublicKey` wrapper
    ///
    /// # Errors
    ///
    /// Returns a serialization error if the public key wrapper cannot be serialized.
    pub fn serialize_debug_ml_dsa_public_key<S>(
        key: &DebugMlDsaPublicKey,
        serializer: S,
    ) -> std::result::Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serialize_ml_dsa_public_key(&key.0, serializer)
    }

    /// Deserialize `DebugMlDsaPublicKey` wrapper
    ///
    /// # Errors
    ///
    /// Returns a deserialization error if the public key wrapper cannot be deserialized.
    pub fn deserialize_debug_ml_dsa_public_key<'de, D>(
        deserializer: D,
    ) -> std::result::Result<DebugMlDsaPublicKey, D::Error>
    where
        D: Deserializer<'de>,
    {
        let key = deserialize_ml_dsa_public_key(deserializer)?;
        Ok(DebugMlDsaPublicKey(key))
    }
}

// Display implementations for test assertions
impl std::fmt::Display for MlsAead {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MlsAead::Aes128Gcm => write!(f, "AES-128-GCM"),
            MlsAead::Aes256Gcm => write!(f, "AES-256-GCM"),
            MlsAead::ChaCha20Poly1305 => write!(f, "ChaCha20Poly1305"),
        }
    }
}

impl std::fmt::Display for MlsHash {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MlsHash::Sha256 => write!(f, "SHA256"),
            MlsHash::Sha384 => write!(f, "SHA384"),
            MlsHash::Sha512 => write!(f, "SHA512"),
            MlsHash::Blake3 => write!(f, "BLAKE3"),
            MlsHash::Sha3_256 => write!(f, "SHA3-256"),
            MlsHash::Sha3_512 => write!(f, "SHA3-512"),
        }
    }
}

impl std::fmt::Display for MlsKem {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MlsKem::MlKem512 => write!(f, "ML-KEM-512"),
            MlsKem::MlKem768 => write!(f, "ML-KEM-768"),
            MlsKem::MlKem1024 => write!(f, "ML-KEM-1024"),
            #[allow(deprecated)]
            MlsKem::HybridX25519MlKem768 => write!(f, "Hybrid-X25519-ML-KEM-768"),
        }
    }
}

impl std::fmt::Display for MlsSignature {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MlsSignature::MlDsa44 => write!(f, "ML-DSA-44"),
            MlsSignature::MlDsa65 => write!(f, "ML-DSA-65"),
            MlsSignature::MlDsa87 => write!(f, "ML-DSA-87"),
            MlsSignature::SlhDsa128 => write!(f, "SLH-DSA-128"),
            MlsSignature::SlhDsa192 => write!(f, "SLH-DSA-192"),
            MlsSignature::SlhDsa256 => write!(f, "SLH-DSA-256"),
        }
    }
}