spine-crypto 1.0.0

Advanced cryptographic primitives for SPINE including transformer prediction and post-quantum key evolution
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
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
// Allow dead code for cryptographic API surface
#![allow(dead_code)]

//! # SPINE Crypto
//!
//! Advanced cryptographic primitives for the SPINE stack including:
//! - **Titans-based message prediction** for speculative decoding with Neural Long-Term Memory
//! - **MIRAS-adaptive prediction** with automatic variant switching
//! - Post-quantum key evolution using lattice-based cryptography concepts
//!
//! ## Titans Predictor (Neural Long-Term Memory)
//!
//! Uses the **Titans architecture** with test-time training for unbounded context
//! message prediction, enabling speculative decoding where receivers can pre-compute
//! responses. Unlike standard Transformers, Titans maintains persistent memory that
//! survives across sequences through surprise-gated updates.
//!
//! Key advantages over Transformers:
//! - **Unbounded context**: Memory persists indefinitely via consolidation
//! - **Test-time training**: Adapts to message patterns during inference
//! - **Surprise detection**: Identifies anomalous messages for security
//! - **Memory efficiency**: O(1) memory vs O(n²) for attention
//!
//! ## MIRAS-Adaptive Prediction
//!
//! Integrates the MIRAS framework for continual learning:
//! - **YAAD**: Yield-Adaptive Anomaly Detection for outlier-robust prediction
//! - **MONETA**: Memory-Optimized Network for stable long-running sessions
//! - **MEMORA**: Balanced updates for mixed traffic patterns
//!
//! The predictor automatically switches between variants based on surprise levels.
//!
//! ## Quantum-Resistant Key Evolution
//!
//! Implements NTRU-inspired lattice operations for key evolution that
//! resists quantum computing attacks (Shor's algorithm).

use rand::prelude::*;
use rand::rngs::StdRng;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use zeroize::{Zeroize, ZeroizeOnDrop};
use spine_neural::{
    Activation, DenseLayer, MirasNeuralEncoder, MirasVariant, MultiHeadAttention,
    NeuralEncoderConfig, TitansMemory,
};
use std::collections::VecDeque;
use subtle::ConstantTimeEq;

// ML-KEM (FIPS 203) post-quantum KEM
use ml_kem::kem::{Decapsulate, Encapsulate, EncapsulationKey, DecapsulationKey};
use ml_kem::{MlKem512, MlKem768, MlKem1024, KemCore, EncodedSizeUser, Encoded,
    MlKem512Params, MlKem768Params, MlKem1024Params};

// AES-256-GCM for authenticated encryption (replaces XOR)
use aes_gcm::aead::Aead;
use aes_gcm::{Aes256Gcm, KeyInit, Nonce};
// HKDF for proper key derivation
use hkdf::Hkdf;

// ============================================================================
// TITANS-BASED MESSAGE PREDICTOR (Neural Long-Term Memory)
// ============================================================================

/// Positional encoding for transformer sequence modeling
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PositionalEncoding {
    max_len: usize,
    embed_dim: usize,
    encodings: Vec<Vec<f32>>,
}

impl PositionalEncoding {
    pub fn new(max_len: usize, embed_dim: usize) -> Self {
        // Use enumerate patterns for better clippy compliance
        let encodings: Vec<Vec<f32>> = (0..max_len)
            .map(|pos| {
                (0..embed_dim)
                    .map(|i| {
                        let angle = pos as f32
                            / (10000.0_f32).powf(2.0 * (i / 2) as f32 / embed_dim as f32);
                        if i % 2 == 0 {
                            angle.sin()
                        } else {
                            angle.cos()
                        }
                    })
                    .collect()
            })
            .collect();

        Self {
            max_len,
            embed_dim,
            encodings,
        }
    }

    pub fn get(&self, position: usize) -> &[f32] {
        // Handle edge case: if max_len is 0, return empty slice (though this shouldn't happen)
        if self.encodings.is_empty() {
            return &[];
        }
        &self.encodings[position.min(self.max_len.saturating_sub(1))]
    }
}

/// Layer normalization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LayerNorm {
    dim: usize,
    gamma: Vec<f32>,
    beta: Vec<f32>,
    eps: f32,
}

impl LayerNorm {
    pub fn new(dim: usize) -> Self {
        Self {
            dim,
            gamma: vec![1.0; dim],
            beta: vec![0.0; dim],
            eps: 1e-5,
        }
    }

    pub fn forward(&self, x: &[f32]) -> Vec<f32> {
        // Handle empty input to prevent division by zero
        if x.is_empty() {
            return Vec::new();
        }
        let n = x.len() as f32;
        let mean: f32 = x.iter().sum::<f32>() / n;
        let var: f32 = x.iter().map(|&v| (v - mean).powi(2)).sum::<f32>() / n;
        let std = (var + self.eps).sqrt();

        x.iter()
            .enumerate()
            .map(|(i, &v)| self.gamma[i % self.dim] * (v - mean) / std + self.beta[i % self.dim])
            .collect()
    }
}

/// Feed-forward network in transformer
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FeedForward {
    linear1: DenseLayer,
    linear2: DenseLayer,
}

impl FeedForward {
    pub fn new(embed_dim: usize, ff_dim: usize, rng: &mut StdRng) -> Self {
        Self {
            linear1: DenseLayer::new(embed_dim, ff_dim, Activation::GELU, rng),
            linear2: DenseLayer::new(ff_dim, embed_dim, Activation::None, rng),
        }
    }

    pub fn forward(&mut self, x: &[f32]) -> Vec<f32> {
        let hidden = self.linear1.forward(x);
        self.linear2.forward(&hidden)
    }
}

/// Single Titans decoder block with Neural Long-Term Memory
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TitansBlock {
    /// Neural Long-Term Memory for persistent context
    memory: TitansMemory,
    /// Short-term attention for recent sequence
    attention: MultiHeadAttention,
    ff: FeedForward,
    norm1: LayerNorm,
    norm2: LayerNorm,
    norm3: LayerNorm,
    embed_dim: usize,
}

impl TitansBlock {
    pub fn new(
        embed_dim: usize,
        num_heads: usize,
        ff_dim: usize,
        memory_size: usize,
        rng: &mut StdRng,
    ) -> Self {
        Self {
            memory: TitansMemory::new(embed_dim, embed_dim, memory_size, rng),
            attention: MultiHeadAttention::new(embed_dim, num_heads, rng),
            ff: FeedForward::new(embed_dim, ff_dim, rng),
            norm1: LayerNorm::new(embed_dim),
            norm2: LayerNorm::new(embed_dim),
            norm3: LayerNorm::new(embed_dim),
            embed_dim,
        }
    }

    pub fn forward(&mut self, sequence: &[Vec<f32>]) -> Vec<f32> {
        if sequence.is_empty() {
            return vec![0.0; self.embed_dim];
        }

        let last = &sequence[sequence.len() - 1];

        // Step 1: Query Neural Long-Term Memory (persistent context)
        let memory_out = self.memory.forward(last);
        let residual1: Vec<f32> = memory_out
            .iter()
            .zip(last.iter())
            .map(|(m, l)| m + l)
            .collect();
        let normed1 = self.norm1.forward(&residual1);

        // Step 2: Short-term self-attention for recent sequence
        let attended = self.attention.forward(sequence);
        let residual2: Vec<f32> = attended
            .iter()
            .zip(normed1.iter())
            .map(|(a, n)| a + n)
            .collect();
        let normed2 = self.norm2.forward(&residual2);

        // Step 3: Feed-forward with residual
        let ff_out = self.ff.forward(&normed2);
        let residual3: Vec<f32> = ff_out
            .iter()
            .zip(normed2.iter())
            .map(|(f, n)| f + n)
            .collect();
        self.norm3.forward(&residual3)
    }

    /// Get current surprise level (for anomaly detection)
    pub fn get_surprise(&self) -> f32 {
        self.memory.get_surprise()
    }

    /// Reset memory state
    pub fn reset_memory(&mut self) {
        self.memory.reset_state();
    }
}

/// Byte-level tokenizer for message encoding
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ByteTokenizer {
    embed_dim: usize,
    embeddings: Vec<Vec<f32>>, // 256 byte embeddings
}

impl ByteTokenizer {
    pub fn new(embed_dim: usize, rng: &mut StdRng) -> Self {
        let scale = (1.0 / embed_dim as f32).sqrt();
        let embeddings: Vec<Vec<f32>> = (0..256)
            .map(|_| {
                (0..embed_dim)
                    .map(|_| rng.gen::<f32>() * 2.0 * scale - scale)
                    .collect()
            })
            .collect();

        Self {
            embed_dim,
            embeddings,
        }
    }

    pub fn encode(&self, byte: u8) -> &[f32] {
        &self.embeddings[byte as usize]
    }

    pub fn encode_sequence(&self, bytes: &[u8]) -> Vec<Vec<f32>> {
        bytes
            .iter()
            .map(|&b| self.embeddings[b as usize].clone())
            .collect()
    }
}

/// Output projection to predict next byte distribution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OutputProjection {
    weights: Vec<Vec<f32>>, // [256][embed_dim]
    temperature: f32,
}

impl OutputProjection {
    pub fn new(embed_dim: usize, rng: &mut StdRng) -> Self {
        let scale = (1.0 / embed_dim as f32).sqrt();
        let weights: Vec<Vec<f32>> = (0..256)
            .map(|_| {
                (0..embed_dim)
                    .map(|_| rng.gen::<f32>() * 2.0 * scale - scale)
                    .collect()
            })
            .collect();

        Self {
            weights,
            temperature: 1.0,
        }
    }

    pub fn set_temperature(&mut self, temp: f32) {
        self.temperature = temp.max(0.01);
    }

    pub fn forward(&self, hidden: &[f32]) -> Vec<f32> {
        let mut logits = vec![0.0; 256];
        for (i, w) in self.weights.iter().enumerate() {
            for (j, &h) in hidden.iter().enumerate() {
                logits[i] += w[j] * h;
            }
            logits[i] /= self.temperature;
        }

        // Softmax
        let max = logits.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
        let mut sum = 0.0;
        for l in &mut logits {
            *l = (*l - max).exp();
            sum += *l;
        }
        for l in &mut logits {
            *l /= sum;
        }

        logits
    }

    pub fn sample(&self, probs: &[f32], rng: &mut StdRng) -> u8 {
        let mut cumsum = 0.0;
        let r: f32 = rng.gen();
        for (i, &p) in probs.iter().enumerate() {
            cumsum += p;
            if r < cumsum {
                return i as u8;
            }
        }
        255
    }

    pub fn argmax(&self, probs: &[f32]) -> u8 {
        probs
            .iter()
            .enumerate()
            .max_by(|(_, a), (_, b)| {
                // Handle NaN: treat NaN as less than any number
                a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Less)
            })
            .map(|(i, _)| i as u8)
            .unwrap_or(0)
    }
}

/// Titans-based message predictor with Neural Long-Term Memory
///
/// Unlike standard Transformers with fixed context windows, Titans maintains
/// persistent memory that survives across sequences through surprise-gated
/// test-time training. This enables:
/// - **Unbounded context**: Memory consolidates patterns indefinitely
/// - **Anomaly detection**: High surprise indicates novel/malicious messages
/// - **Adaptive prediction**: Memory updates based on prediction errors
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TitansPredictor {
    tokenizer: ByteTokenizer,
    positional: PositionalEncoding,
    blocks: Vec<TitansBlock>,
    output: OutputProjection,
    embed_dim: usize,
    max_seq_len: usize,
    memory_size: usize,
    context_window: VecDeque<Vec<f32>>,
    /// Accumulated surprise for anomaly detection
    total_surprise: f32,
    #[serde(skip, default = "default_rng")]
    rng: StdRng,
}

/// MIRAS-adaptive message predictor with automatic variant switching
///
/// Extends TitansPredictor with MIRAS continual learning framework:
/// - **Adaptive encoding**: Uses MirasNeuralEncoder for latent projections
/// - **Variant switching**: Automatically selects optimal MIRAS variant
/// - **Outlier robustness**: YAAD for high-anomaly traffic
/// - **Long-term stability**: MONETA for extended sessions
#[derive(Debug, Clone)]
pub struct MirasTitansPredictor {
    /// Base Titans predictor
    base: TitansPredictor,
    /// MIRAS encoder for adaptive projections
    miras_encoder: Option<MirasNeuralEncoder>,
    /// Current MIRAS variant
    active_variant: MirasVariant,
    /// Surprise history for adaptive switching
    surprise_history: VecDeque<f32>,
    /// Threshold for variant switching
    anomaly_threshold: f32,
    /// Message counter for long-running detection
    message_count: u64,
    /// Predictions enhanced with MIRAS embeddings
    miras_enhanced_predictions: u64,
    /// Latent dimension for encoding
    latent_dim: usize,
}

impl MirasTitansPredictor {
    /// Create a new MIRAS-enhanced predictor
    pub fn new(config: TitansConfig) -> Self {
        let base = TitansPredictor::new(config.clone());

        // Create MIRAS encoder (embed_dim must be divisible by attention_heads)
        let encoder_config = NeuralEncoderConfig {
            input_dim: config.embed_dim,
            latent_dim: config.embed_dim,
            hidden_dims: vec![config.ff_dim, config.embed_dim],
            attention_heads: config.num_heads,
            seed: config.seed + 1,
            miras_variant: MirasVariant::Titans,
            memory_tokens: config.memory_size,
        };

        let miras_encoder = Some(MirasNeuralEncoder::new(&encoder_config));

        Self {
            base,
            miras_encoder,
            active_variant: MirasVariant::Titans,
            surprise_history: VecDeque::with_capacity(100),
            anomaly_threshold: 0.5,
            message_count: 0,
            miras_enhanced_predictions: 0,
            latent_dim: config.embed_dim,
        }
    }

    /// Create with specific MIRAS variant
    pub fn new_with_variant(config: TitansConfig, variant: MirasVariant) -> Self {
        let base = TitansPredictor::new(config.clone());

        // Recreate encoder with specified variant
        let encoder_config = NeuralEncoderConfig {
            input_dim: config.embed_dim,
            latent_dim: config.embed_dim,
            hidden_dims: vec![config.ff_dim, config.embed_dim],
            attention_heads: config.num_heads,
            seed: config.seed + 1,
            miras_variant: variant,
            memory_tokens: config.memory_size,
        };

        Self {
            base,
            miras_encoder: Some(MirasNeuralEncoder::new(&encoder_config)),
            active_variant: variant,
            surprise_history: VecDeque::with_capacity(100),
            anomaly_threshold: 0.5,
            message_count: 0,
            miras_enhanced_predictions: 0,
            latent_dim: config.embed_dim,
        }
    }

    /// Set anomaly threshold for variant switching
    pub fn set_anomaly_threshold(&mut self, threshold: f32) {
        self.anomaly_threshold = threshold;
    }

    /// Get current MIRAS variant
    pub fn variant(&self) -> &str {
        match self.active_variant {
            MirasVariant::Titans => "titans",
            MirasVariant::Yaad => "yaad",
            MirasVariant::Moneta { .. } => "moneta",
            MirasVariant::Memora => "memora",
        }
    }

    /// Get average anomaly level
    pub fn anomaly_level(&self) -> f32 {
        if self.surprise_history.is_empty() {
            0.0
        } else {
            self.surprise_history.iter().sum::<f32>() / self.surprise_history.len() as f32
        }
    }

    /// Adaptively switch MIRAS variant based on traffic patterns
    fn maybe_switch_variant(&mut self) {
        let anomaly = self.anomaly_level();

        let new_variant = if anomaly > self.anomaly_threshold * 2.0 {
            // High anomaly: use YAAD for outlier robustness
            MirasVariant::Yaad
        } else if anomaly > self.anomaly_threshold {
            // Moderate anomaly: use MEMORA for balanced updates
            MirasVariant::Memora
        } else if self.message_count > 10000 {
            // Long-running session: use MONETA for stability (p=2 is L2 norm)
            MirasVariant::Moneta { p: 2.0 }
        } else {
            // Normal: baseline Titans
            MirasVariant::Titans
        };

        // Check if variant changed (ignoring Moneta's p value for comparison)
        let variant_changed = !matches!(
            (&new_variant, &self.active_variant),
            (MirasVariant::Titans, MirasVariant::Titans)
                | (MirasVariant::Yaad, MirasVariant::Yaad)
                | (MirasVariant::Moneta { .. }, MirasVariant::Moneta { .. })
                | (MirasVariant::Memora, MirasVariant::Memora)
        );

        if variant_changed {
            self.active_variant = new_variant;
            // Note: In production, we'd rebuild the encoder here
            // For efficiency, we keep the same encoder but track the variant
        }
    }

    /// Observe a message with MIRAS-enhanced encoding
    pub fn observe(&mut self, message: &[u8]) {
        // Base observation
        self.base.observe(message);

        // Track surprise
        let surprise = self.base.get_surprise();
        self.surprise_history.push_back(surprise);
        if self.surprise_history.len() > 100 {
            self.surprise_history.pop_front();
        }

        // MIRAS encoding step (for enhanced pattern learning)
        if let Some(ref mut encoder) = self.miras_encoder {
            // Encode with MIRAS (triggers surprise tracking)
            let _latent = encoder.encode(message);
            self.miras_enhanced_predictions += 1;
        }

        self.message_count += 1;

        // Check if we should switch variants
        self.maybe_switch_variant();
    }

    /// Predict next byte (delegates to base)
    pub fn predict_next(&mut self) -> (u8, f32) {
        self.base.predict_next()
    }

    /// Predict sequence (delegates to base)
    pub fn predict_sequence(&mut self, length: usize, greedy: bool) -> Vec<u8> {
        self.base.predict_sequence(length, greedy)
    }

    /// Verify prediction (delegates to base)
    pub fn verify_prediction(&mut self, message: &[u8]) -> (bool, f32) {
        self.base.verify_prediction(message)
    }

    /// Get surprise from base predictor
    pub fn get_surprise(&self) -> f32 {
        self.base.get_surprise()
    }

    /// Check if anomalous
    pub fn is_anomalous(&self, threshold: f32) -> bool {
        self.base.is_anomalous(threshold)
    }

    /// Get MIRAS encoder surprise (if available)
    pub fn get_miras_surprise(&self) -> Option<f32> {
        self.miras_encoder.as_ref().map(|e| e.get_surprise())
    }

    /// Get combined surprise (Titans + MIRAS)
    pub fn get_combined_surprise(&self) -> f32 {
        let titans = self.base.get_surprise();
        let miras = self.get_miras_surprise().unwrap_or(0.0);
        (titans + miras) / 2.0
    }

    /// Reset context (preserves memory)
    pub fn reset(&mut self) {
        self.base.reset();
    }

    /// Full reset including MIRAS state
    pub fn reset_all(&mut self) {
        self.base.reset_all();
        self.surprise_history.clear();
        self.message_count = 0;
        if let Some(ref mut encoder) = self.miras_encoder {
            encoder.reset();
        }
    }

    /// Get statistics
    pub fn stats(&self) -> MirasPredictorStats {
        MirasPredictorStats {
            message_count: self.message_count,
            miras_enhanced_predictions: self.miras_enhanced_predictions,
            current_variant: self.variant().to_string(),
            anomaly_level: self.anomaly_level(),
            titans_surprise: self.base.get_surprise(),
            miras_surprise: self.get_miras_surprise(),
        }
    }
}

/// Statistics for MIRAS predictor
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MirasPredictorStats {
    pub message_count: u64,
    pub miras_enhanced_predictions: u64,
    pub current_variant: String,
    pub anomaly_level: f32,
    pub titans_surprise: f32,
    pub miras_surprise: Option<f32>,
}

fn default_rng() -> StdRng {
    StdRng::seed_from_u64(42)
}

impl TitansPredictor {
    pub fn new(config: TitansConfig) -> Self {
        let mut rng = StdRng::seed_from_u64(config.seed);

        let tokenizer = ByteTokenizer::new(config.embed_dim, &mut rng);
        let positional = PositionalEncoding::new(config.max_seq_len, config.embed_dim);

        let blocks: Vec<TitansBlock> = (0..config.num_layers)
            .map(|_| {
                TitansBlock::new(
                    config.embed_dim,
                    config.num_heads,
                    config.ff_dim,
                    config.memory_size,
                    &mut rng,
                )
            })
            .collect();

        let output = OutputProjection::new(config.embed_dim, &mut rng);

        Self {
            tokenizer,
            positional,
            blocks,
            output,
            embed_dim: config.embed_dim,
            max_seq_len: config.max_seq_len,
            memory_size: config.memory_size,
            context_window: VecDeque::with_capacity(config.max_seq_len),
            total_surprise: 0.0,
            rng,
        }
    }

    /// Add a message to the context for prediction (triggers test-time training)
    pub fn observe(&mut self, message: &[u8]) {
        for &byte in message {
            let mut embedding = self.tokenizer.encode(byte).to_vec();
            let pos = self.context_window.len();
            let pos_enc = self.positional.get(pos);
            for (e, p) in embedding.iter_mut().zip(pos_enc.iter()) {
                *e += *p;
            }

            self.context_window.push_back(embedding);
            if self.context_window.len() > self.max_seq_len {
                self.context_window.pop_front();
            }
        }

        // Accumulate surprise from all blocks (for anomaly detection)
        self.total_surprise = self.blocks.iter().map(|b| b.get_surprise()).sum::<f32>()
            / self.blocks.len().max(1) as f32;
    }

    /// Predict the next byte
    pub fn predict_next(&mut self) -> (u8, f32) {
        let sequence: Vec<Vec<f32>> = self.context_window.iter().cloned().collect();

        if sequence.is_empty() {
            return (0, 1.0 / 256.0);
        }

        // Forward through Titans blocks (with persistent memory)
        let mut hidden = self.blocks[0].forward(&sequence);
        for block in &mut self.blocks[1..] {
            let seq_with_hidden = vec![hidden.clone()];
            hidden = block.forward(&seq_with_hidden);
        }

        // Project to output
        let probs = self.output.forward(&hidden);
        let predicted = self.output.argmax(&probs);
        let confidence = probs[predicted as usize];

        (predicted, confidence)
    }

    /// Predict multiple bytes autoregressively
    pub fn predict_sequence(&mut self, length: usize, greedy: bool) -> Vec<u8> {
        let mut result = Vec::with_capacity(length);

        for _ in 0..length {
            let sequence: Vec<Vec<f32>> = self.context_window.iter().cloned().collect();

            if sequence.is_empty() {
                let byte = if greedy { 0 } else { self.rng.gen() };
                result.push(byte);
                continue;
            }

            // Forward through Titans blocks
            let mut hidden = self.blocks[0].forward(&sequence);
            for block in &mut self.blocks[1..] {
                let seq_with_hidden = vec![hidden.clone()];
                hidden = block.forward(&seq_with_hidden);
            }

            let probs = self.output.forward(&hidden);
            let byte = if greedy {
                self.output.argmax(&probs)
            } else {
                self.output.sample(&probs, &mut self.rng)
            };

            result.push(byte);

            // Add prediction to context for autoregressive generation
            let mut embedding = self.tokenizer.encode(byte).to_vec();
            let pos = self.context_window.len();
            let pos_enc = self.positional.get(pos);
            for (e, p) in embedding.iter_mut().zip(pos_enc.iter()) {
                *e += *p;
            }
            self.context_window.push_back(embedding);
            if self.context_window.len() > self.max_seq_len {
                self.context_window.pop_front();
            }
        }

        result
    }

    /// Check if a message matches prediction
    pub fn verify_prediction(&mut self, message: &[u8]) -> (bool, f32) {
        let predicted = self.predict_sequence(message.len(), true);
        let matches = predicted == message;

        let similarity = predicted
            .iter()
            .zip(message.iter())
            .filter(|(p, m)| p == m)
            .count() as f32
            / message.len().max(1) as f32;

        (matches, similarity)
    }

    /// Get accumulated surprise (anomaly score)
    /// High values indicate unexpected/novel message patterns
    pub fn get_surprise(&self) -> f32 {
        self.total_surprise
    }

    /// Check if current message pattern is anomalous
    pub fn is_anomalous(&self, threshold: f32) -> bool {
        self.total_surprise > threshold
    }

    /// Reset context window (but preserve long-term memory)
    pub fn reset(&mut self) {
        self.context_window.clear();
        self.total_surprise = 0.0;
    }

    /// Full reset including long-term memory
    pub fn reset_all(&mut self) {
        self.context_window.clear();
        self.total_surprise = 0.0;
        for block in &mut self.blocks {
            block.reset_memory();
        }
    }

    /// Set temperature for sampling
    pub fn set_temperature(&mut self, temp: f32) {
        self.output.set_temperature(temp);
    }
}

// Backwards compatibility aliases
pub type TransformerPredictor = TitansPredictor;
pub type TransformerConfig = TitansConfig;

/// Configuration for Titans predictor
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TitansConfig {
    pub embed_dim: usize,
    pub num_heads: usize,
    pub num_layers: usize,
    pub ff_dim: usize,
    pub max_seq_len: usize,
    /// Size of persistent memory (number of memory tokens)
    pub memory_size: usize,
    pub seed: u64,
}

impl Default for TitansConfig {
    fn default() -> Self {
        Self {
            embed_dim: 64,
            num_heads: 4,
            num_layers: 2,
            ff_dim: 128,
            max_seq_len: 256,
            memory_size: 64, // Persistent memory tokens
            seed: 42,
        }
    }
}

// ============================================================================
// QUANTUM-RESISTANT KEY EVOLUTION
// ============================================================================

/// Parameters for NTRU-like lattice operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LatticeParams {
    pub n: usize,   // Polynomial degree (power of 2)
    pub q: u64,     // Large modulus
    pub p: u64,     // Small modulus for message space
    pub sigma: f64, // Gaussian noise standard deviation
}

impl Default for LatticeParams {
    fn default() -> Self {
        Self {
            n: 1024,    // NIST Level 3 security (~192-bit classical)
            q: 12289,   // NTT-friendly prime for n=1024
            p: 3,       // Ternary message space
            sigma: 3.2, // Standard deviation per NIST recommendations
        }
    }
}

/// Polynomial ring element Z_q[X]/(X^n + 1).
///
/// Holds RLWE secret-key coefficients. Memory is zeroed on `Drop` so a
/// dropped `RingElement` cannot leak its secret via a core dump or
/// swap-to-disk event (NIST SP 800-171 § 3.13.10).
#[derive(Debug, Clone, Serialize, Deserialize, Zeroize, ZeroizeOnDrop)]
pub struct RingElement {
    coeffs: Vec<i64>,
    n: usize,
    q: u64,
}

impl RingElement {
    pub fn new(n: usize, q: u64) -> Self {
        Self {
            coeffs: vec![0; n],
            n,
            q,
        }
    }

    pub fn random(n: usize, q: u64, rng: &mut StdRng) -> Self {
        let coeffs: Vec<i64> = (0..n).map(|_| rng.gen_range(0..q as i64)).collect();
        Self { coeffs, n, q }
    }

    pub fn random_ternary(n: usize, q: u64, rng: &mut StdRng) -> Self {
        let coeffs: Vec<i64> = (0..n).map(|_| rng.gen_range(-1..=1)).collect();
        Self { coeffs, n, q }
    }

    pub fn random_gaussian(n: usize, q: u64, sigma: f64, rng: &mut StdRng) -> Self {
        // Box-Muller transform for Gaussian
        let coeffs: Vec<i64> = (0..n)
            .map(|_| {
                let u1: f64 = rng.gen::<f64>().max(1e-10);
                let u2: f64 = rng.gen();
                let z = (-2.0 * u1.ln()).sqrt() * (2.0 * std::f64::consts::PI * u2).cos();
                (z * sigma).round() as i64
            })
            .collect();
        Self { coeffs, n, q }
    }

    pub fn from_bytes(bytes: &[u8], n: usize, q: u64) -> Self {
        let mut coeffs = vec![0i64; n];
        for (i, chunk) in bytes.chunks(2).enumerate() {
            if i >= n {
                break;
            }
            let val = if chunk.len() == 2 {
                ((chunk[0] as u16) | ((chunk[1] as u16) << 8)) as i64
            } else {
                chunk[0] as i64
            };
            coeffs[i] = val % q as i64;
        }
        Self { coeffs, n, q }
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        let mut bytes = Vec::with_capacity(self.n * 2);
        for &c in &self.coeffs {
            let val = ((c % self.q as i64 + self.q as i64) % self.q as i64) as u16;
            bytes.push(val as u8);
            bytes.push((val >> 8) as u8);
        }
        bytes
    }

    fn reduce(&mut self) {
        for c in &mut self.coeffs {
            *c = ((*c % self.q as i64) + self.q as i64) % self.q as i64;
        }
    }

    /// Polynomial multiplication in R_q = Z_q[X]/(X^n + 1)
    pub fn mul(&self, other: &RingElement) -> RingElement {
        assert_eq!(self.n, other.n);
        let mut result = vec![0i64; self.n];

        for i in 0..self.n {
            for j in 0..self.n {
                let idx = i + j;
                let coeff = self.coeffs[i] * other.coeffs[j];
                if idx < self.n {
                    result[idx] += coeff;
                } else {
                    // X^n = -1 in the ring
                    result[idx - self.n] -= coeff;
                }
            }
        }

        let mut elem = RingElement {
            coeffs: result,
            n: self.n,
            q: self.q,
        };
        elem.reduce();
        elem
    }

    /// Polynomial addition
    pub fn add(&self, other: &RingElement) -> RingElement {
        assert_eq!(self.n, other.n);
        let coeffs: Vec<i64> = self
            .coeffs
            .iter()
            .zip(other.coeffs.iter())
            .map(|(a, b)| (a + b) % self.q as i64)
            .collect();
        let mut elem = RingElement {
            coeffs,
            n: self.n,
            q: self.q,
        };
        elem.reduce();
        elem
    }

    /// Polynomial subtraction
    pub fn sub(&self, other: &RingElement) -> RingElement {
        assert_eq!(self.n, other.n);
        let coeffs: Vec<i64> = self
            .coeffs
            .iter()
            .zip(other.coeffs.iter())
            .map(|(a, b)| (a - b) % self.q as i64)
            .collect();
        let mut elem = RingElement {
            coeffs,
            n: self.n,
            q: self.q,
        };
        elem.reduce();
        elem
    }

    /// Scale coefficients
    pub fn scale(&self, scalar: i64) -> RingElement {
        let coeffs: Vec<i64> = self
            .coeffs
            .iter()
            .map(|&c| (c * scalar) % self.q as i64)
            .collect();
        let mut elem = RingElement {
            coeffs,
            n: self.n,
            q: self.q,
        };
        elem.reduce();
        elem
    }
}

/// Key Encapsulation Mechanism algorithm selection
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Default)]
pub enum KemAlgorithm {
    /// Custom RLWE (existing implementation) — NIST Level 3 equivalent
    Rlwe,
    /// ML-KEM-512 (FIPS 203) — NIST Level 1
    MlKem512,
    /// ML-KEM-768 (FIPS 203) — NIST Level 3 (recommended)
    #[default]
    MlKem768,
    /// ML-KEM-1024 (FIPS 203) — NIST Level 5
    MlKem1024,
    /// Hybrid: RLWE + ML-KEM-768 (defense in depth)
    Hybrid,
}

/// ML-KEM key encapsulation result.
///
/// `dk_bytes` is the FIPS 203 decapsulation key — the private half of
/// the KEM. Zeroed on `Drop`; the public `ek_bytes` is zeroed too
/// because it's free to do so and keeps the Drop impl uniform.
#[derive(Debug, Clone, Zeroize, ZeroizeOnDrop)]
struct MlKemKeyPair {
    dk_bytes: Vec<u8>,  // Decapsulation key (private)
    ek_bytes: Vec<u8>,  // Encapsulation key (public)
    #[zeroize(skip)]
    algorithm: KemAlgorithm,
}

/// ML-KEM operations using FIPS 203
mod mlkem_ops {
    use super::*;

    pub fn generate_512(rng: &mut StdRng) -> MlKemKeyPair {
        let (dk, ek) = MlKem512::generate(rng);
        MlKemKeyPair {
            dk_bytes: dk.as_bytes().to_vec(),
            ek_bytes: ek.as_bytes().to_vec(),
            algorithm: KemAlgorithm::MlKem512,
        }
    }

    pub fn generate_768(rng: &mut StdRng) -> MlKemKeyPair {
        let (dk, ek) = MlKem768::generate(rng);
        MlKemKeyPair {
            dk_bytes: dk.as_bytes().to_vec(),
            ek_bytes: ek.as_bytes().to_vec(),
            algorithm: KemAlgorithm::MlKem768,
        }
    }

    pub fn generate_1024(rng: &mut StdRng) -> MlKemKeyPair {
        let (dk, ek) = MlKem1024::generate(rng);
        MlKemKeyPair {
            dk_bytes: dk.as_bytes().to_vec(),
            ek_bytes: ek.as_bytes().to_vec(),
            algorithm: KemAlgorithm::MlKem1024,
        }
    }

    pub fn encapsulate_512(ek_bytes: &[u8], rng: &mut StdRng) -> Option<(Vec<u8>, [u8; 32])> {
        let ek_encoded = <Encoded<EncapsulationKey<MlKem512Params>>>::try_from(ek_bytes).ok()?;
        let ek = EncapsulationKey::<MlKem512Params>::from_bytes(&ek_encoded);
        let (ct, ss) = ek.encapsulate(rng).ok()?;
        let mut shared = [0u8; 32];
        shared.copy_from_slice(ss.as_slice());
        Some((ct.to_vec(), shared))
    }

    pub fn encapsulate_768(ek_bytes: &[u8], rng: &mut StdRng) -> Option<(Vec<u8>, [u8; 32])> {
        let ek_encoded = <Encoded<EncapsulationKey<MlKem768Params>>>::try_from(ek_bytes).ok()?;
        let ek = EncapsulationKey::<MlKem768Params>::from_bytes(&ek_encoded);
        let (ct, ss) = ek.encapsulate(rng).ok()?;
        let mut shared = [0u8; 32];
        shared.copy_from_slice(ss.as_slice());
        Some((ct.to_vec(), shared))
    }

    pub fn encapsulate_1024(ek_bytes: &[u8], rng: &mut StdRng) -> Option<(Vec<u8>, [u8; 32])> {
        let ek_encoded = <Encoded<EncapsulationKey<MlKem1024Params>>>::try_from(ek_bytes).ok()?;
        let ek = EncapsulationKey::<MlKem1024Params>::from_bytes(&ek_encoded);
        let (ct, ss) = ek.encapsulate(rng).ok()?;
        let mut shared = [0u8; 32];
        shared.copy_from_slice(ss.as_slice());
        Some((ct.to_vec(), shared))
    }

    pub fn decapsulate_512(dk_bytes: &[u8], ct_bytes: &[u8]) -> Option<[u8; 32]> {
        let dk_encoded = <Encoded<DecapsulationKey<MlKem512Params>>>::try_from(dk_bytes).ok()?;
        let dk = DecapsulationKey::<MlKem512Params>::from_bytes(&dk_encoded);
        let ct = <ml_kem::Ciphertext<MlKem512>>::try_from(ct_bytes).ok()?;
        let ss = dk.decapsulate(&ct).ok()?;
        let mut shared = [0u8; 32];
        shared.copy_from_slice(ss.as_slice());
        Some(shared)
    }

    pub fn decapsulate_768(dk_bytes: &[u8], ct_bytes: &[u8]) -> Option<[u8; 32]> {
        let dk_encoded = <Encoded<DecapsulationKey<MlKem768Params>>>::try_from(dk_bytes).ok()?;
        let dk = DecapsulationKey::<MlKem768Params>::from_bytes(&dk_encoded);
        let ct = <ml_kem::Ciphertext<MlKem768>>::try_from(ct_bytes).ok()?;
        let ss = dk.decapsulate(&ct).ok()?;
        let mut shared = [0u8; 32];
        shared.copy_from_slice(ss.as_slice());
        Some(shared)
    }

    pub fn decapsulate_1024(dk_bytes: &[u8], ct_bytes: &[u8]) -> Option<[u8; 32]> {
        let dk_encoded = <Encoded<DecapsulationKey<MlKem1024Params>>>::try_from(dk_bytes).ok()?;
        let dk = DecapsulationKey::<MlKem1024Params>::from_bytes(&dk_encoded);
        let ct = <ml_kem::Ciphertext<MlKem1024>>::try_from(ct_bytes).ok()?;
        let ss = dk.decapsulate(&ct).ok()?;
        let mut shared = [0u8; 32];
        shared.copy_from_slice(ss.as_slice());
        Some(shared)
    }
}

/// Quantum-resistant key pair.
///
/// `secret_key` is the RLWE secret coefficient vector. All three
/// `RingElement` fields zeroize on drop via their own derived
/// `ZeroizeOnDrop`. `params` is plaintext metadata (n, q, sigma) so it
/// is intentionally skipped.
#[derive(Debug, Clone, Serialize, Deserialize, Zeroize, ZeroizeOnDrop)]
pub struct QuantumKeyPair {
    /// Public parameter `a` — must be stored for correct KEM encaps/decaps
    pub a: RingElement,
    pub public_key: RingElement,
    secret_key: RingElement,
    #[zeroize(skip)]
    params: LatticeParams,
}

/// Quantum-resistant key evolution system.
///
/// `Drop` is implemented manually below because `VecDeque<[u8; 32]>`
/// and `StdRng` don't impl `Zeroize` in the derive form. The wrapped
/// `QuantumKeyPair` and `MlKemKeyPair` zero themselves on their own
/// drops; we only need to scrub the history buffer here.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuantumKeyEvolution {
    params: LatticeParams,
    current_key: QuantumKeyPair,
    evolution_counter: u64,
    key_history: VecDeque<[u8; 32]>, // Hashes of past keys for forward secrecy
    max_history: usize,
    #[serde(skip, default = "default_rng")]
    rng: StdRng,
    /// KEM algorithm in use
    algorithm: KemAlgorithm,
    /// ML-KEM keypair (when using FIPS 203 algorithms)
    #[serde(skip)]
    mlkem_keypair: Option<MlKemKeyPair>,
}

impl Drop for QuantumKeyEvolution {
    fn drop(&mut self) {
        // The wrapped key structs zero themselves on their own drops.
        // We only need to scrub the rolling history buffer here — each
        // entry is a SHA-256 over a past secret and is treated as
        // sensitive even though it is not the secret itself.
        for h in self.key_history.iter_mut() {
            h.zeroize();
        }
        self.key_history.clear();
    }
}

impl QuantumKeyEvolution {
    pub fn new(params: LatticeParams, seed: u64) -> Self {
        let mut rng = StdRng::seed_from_u64(seed);
        let current_key = Self::generate_keypair(&params, &mut rng);

        Self {
            params,
            current_key,
            evolution_counter: 0,
            key_history: VecDeque::new(),
            max_history: 100,
            rng,
            algorithm: KemAlgorithm::Rlwe,
            mlkem_keypair: None,
        }
    }

    /// Create with a specific KEM algorithm
    pub fn new_with_algorithm(params: LatticeParams, seed: u64, algorithm: KemAlgorithm) -> Self {
        let mut rng = StdRng::seed_from_u64(seed);
        let current_key = Self::generate_keypair(&params, &mut rng);
        let mlkem_keypair = match algorithm {
            KemAlgorithm::MlKem512 => Some(mlkem_ops::generate_512(&mut rng)),
            KemAlgorithm::MlKem768 => Some(mlkem_ops::generate_768(&mut rng)),
            KemAlgorithm::MlKem1024 => Some(mlkem_ops::generate_1024(&mut rng)),
            KemAlgorithm::Hybrid => Some(mlkem_ops::generate_768(&mut rng)),
            KemAlgorithm::Rlwe => None,
        };

        Self {
            params,
            current_key,
            evolution_counter: 0,
            key_history: VecDeque::new(),
            max_history: 100,
            rng,
            algorithm,
            mlkem_keypair,
        }
    }

    fn generate_keypair(params: &LatticeParams, rng: &mut StdRng) -> QuantumKeyPair {
        // RLWE-style key generation
        let a = RingElement::random(params.n, params.q, rng);
        let s = RingElement::random_ternary(params.n, params.q, rng);
        let e = RingElement::random_gaussian(params.n, params.q, params.sigma, rng);

        // Public key: b = a*s + e
        let b = a.mul(&s).add(&e);

        QuantumKeyPair {
            a, // Store `a` for correct KEM operation
            public_key: b,
            secret_key: s,
            params: params.clone(),
        }
    }

    /// Evolve the key forward (one-way function)
    ///
    /// Uses HKDF to derive a new seed from the current key material,
    /// then generates a fresh RLWE keypair that maintains the b=a*s+e invariant.
    pub fn evolve(&mut self) -> [u8; 32] {
        // Hash current key material (public key + secret key + counter)
        let mut hasher = Sha256::new();
        hasher.update(self.current_key.public_key.to_bytes());
        hasher.update(self.current_key.secret_key.to_bytes());
        hasher.update(self.evolution_counter.to_le_bytes());
        let hash: [u8; 32] = hasher.finalize().into();

        // Store in history
        self.key_history.push_back(hash);
        if self.key_history.len() > self.max_history {
            self.key_history.pop_front();
        }

        // Use HKDF to derive new seed (mixes entropy from current key + counter)
        let hk = Hkdf::<Sha256>::new(Some(&self.evolution_counter.to_le_bytes()), &hash);
        let mut okm = [0u8; 32];
        hk.expand(b"spine-key-evolution", &mut okm)
            .expect("HKDF expand failed");
        let new_seed = u64::from_le_bytes(okm[0..8].try_into().unwrap());
        let mut new_rng = StdRng::seed_from_u64(new_seed);

        // Generate a proper RLWE keypair that maintains the b=a*s+e invariant
        self.current_key = Self::generate_keypair(&self.params, &mut new_rng);

        // Also evolve ML-KEM keypair if in use
        if self.algorithm != KemAlgorithm::Rlwe {
            self.mlkem_keypair = match self.algorithm {
                KemAlgorithm::MlKem512 => Some(mlkem_ops::generate_512(&mut new_rng)),
                KemAlgorithm::MlKem768 | KemAlgorithm::Hybrid => Some(mlkem_ops::generate_768(&mut new_rng)),
                KemAlgorithm::MlKem1024 => Some(mlkem_ops::generate_1024(&mut new_rng)),
                KemAlgorithm::Rlwe => None,
            };
        }

        self.evolution_counter += 1;

        hash
    }

    /// Encapsulate a shared secret using the current KEM algorithm
    pub fn encapsulate(&mut self) -> (Vec<u8>, [u8; 32]) {
        match self.algorithm {
            KemAlgorithm::Rlwe => self.encapsulate_rlwe(),
            KemAlgorithm::MlKem512 => self.encapsulate_mlkem(KemAlgorithm::MlKem512),
            KemAlgorithm::MlKem768 => self.encapsulate_mlkem(KemAlgorithm::MlKem768),
            KemAlgorithm::MlKem1024 => self.encapsulate_mlkem(KemAlgorithm::MlKem1024),
            KemAlgorithm::Hybrid => self.encapsulate_hybrid(),
        }
    }

    /// Encapsulate using ML-KEM (FIPS 203)
    fn encapsulate_mlkem(&mut self, alg: KemAlgorithm) -> (Vec<u8>, [u8; 32]) {
        let kp = self.mlkem_keypair.as_ref().expect("ML-KEM keypair required");
        let result = match alg {
            KemAlgorithm::MlKem512 => mlkem_ops::encapsulate_512(&kp.ek_bytes, &mut self.rng),
            KemAlgorithm::MlKem768 => mlkem_ops::encapsulate_768(&kp.ek_bytes, &mut self.rng),
            KemAlgorithm::MlKem1024 => mlkem_ops::encapsulate_1024(&kp.ek_bytes, &mut self.rng),
            _ => unreachable!(),
        };
        result.unwrap_or_else(|| {
            // Fallback to RLWE if ML-KEM fails
            self.encapsulate_rlwe()
        })
    }

    /// Encapsulate using hybrid RLWE + ML-KEM-768 (defense in depth)
    fn encapsulate_hybrid(&mut self) -> (Vec<u8>, [u8; 32]) {
        // Get shared secrets from both algorithms
        let (rlwe_ct, rlwe_ss) = self.encapsulate_rlwe();
        let (mlkem_ct, mlkem_ss) = self.encapsulate_mlkem(KemAlgorithm::MlKem768);

        // Combine shared secrets via HKDF
        let mut combined_ikm = [0u8; 64];
        combined_ikm[..32].copy_from_slice(&rlwe_ss);
        combined_ikm[32..].copy_from_slice(&mlkem_ss);
        let hk = Hkdf::<Sha256>::new(None, &combined_ikm);
        let mut hybrid_ss = [0u8; 32];
        hk.expand(b"spine-hybrid-kem", &mut hybrid_ss).expect("HKDF expand");

        // Concatenate ciphertexts with length prefix for RLWE part
        let rlwe_len = (rlwe_ct.len() as u32).to_le_bytes();
        let mut hybrid_ct = Vec::with_capacity(4 + rlwe_ct.len() + mlkem_ct.len());
        hybrid_ct.extend_from_slice(&rlwe_len);
        hybrid_ct.extend_from_slice(&rlwe_ct);
        hybrid_ct.extend_from_slice(&mlkem_ct);

        (hybrid_ct, hybrid_ss)
    }

    /// Encapsulate a shared secret using the public key (RLWE KEM)
    ///
    /// Uses the stored `a` from keygen to ensure sender and receiver
    /// derive the same shared secret. Encodes a random message `m` into
    /// the high bits and recovers it via rounding on decapsulation.
    fn encapsulate_rlwe(&mut self) -> (Vec<u8>, [u8; 32]) {
        // Use the SAME `a` from keygen — critical for correctness
        let a = &self.current_key.a;
        let r = RingElement::random_ternary(self.params.n, self.params.q, &mut self.rng);
        let e1 = RingElement::random_gaussian(
            self.params.n,
            self.params.q,
            self.params.sigma,
            &mut self.rng,
        );
        let e2 = RingElement::random_gaussian(
            self.params.n,
            self.params.q,
            self.params.sigma,
            &mut self.rng,
        );

        // Generate random message m ∈ {0, 1}^n for KEM
        let m: Vec<i64> = (0..self.params.n)
            .map(|_| self.rng.gen_range(0..2i64))
            .collect();

        // u = a*r + e1
        let u = a.mul(&r).add(&e1);

        // v = b*r + e2 + ⌊q/2⌋·m (encode message in high bits)
        let half_q = (self.params.q / 2) as i64;
        let encoded_m = RingElement {
            coeffs: m.iter().map(|&mi| mi * half_q).collect(),
            n: self.params.n,
            q: self.params.q,
        };
        let v = self.current_key.public_key.mul(&r).add(&e2).add(&encoded_m);

        // Ciphertext = (u, v)
        let mut ciphertext = u.to_bytes();
        ciphertext.extend(v.to_bytes());

        // Shared secret = H(m) — both sides derive from the same m
        let mut hasher = Sha256::new();
        for &mi in &m {
            hasher.update(mi.to_le_bytes());
        }
        let shared_secret: [u8; 32] = hasher.finalize().into();

        (ciphertext, shared_secret)
    }

    /// Decapsulate to recover shared secret using the current KEM algorithm
    pub fn decapsulate(&self, ciphertext: &[u8]) -> Option<[u8; 32]> {
        match self.algorithm {
            KemAlgorithm::Rlwe => self.decapsulate_rlwe(ciphertext),
            KemAlgorithm::MlKem512 => self.decapsulate_mlkem(ciphertext, KemAlgorithm::MlKem512),
            KemAlgorithm::MlKem768 => self.decapsulate_mlkem(ciphertext, KemAlgorithm::MlKem768),
            KemAlgorithm::MlKem1024 => self.decapsulate_mlkem(ciphertext, KemAlgorithm::MlKem1024),
            KemAlgorithm::Hybrid => self.decapsulate_hybrid(ciphertext),
        }
    }

    /// Decapsulate using ML-KEM (FIPS 203)
    fn decapsulate_mlkem(&self, ciphertext: &[u8], alg: KemAlgorithm) -> Option<[u8; 32]> {
        let kp = self.mlkem_keypair.as_ref()?;
        match alg {
            KemAlgorithm::MlKem512 => mlkem_ops::decapsulate_512(&kp.dk_bytes, ciphertext),
            KemAlgorithm::MlKem768 => mlkem_ops::decapsulate_768(&kp.dk_bytes, ciphertext),
            KemAlgorithm::MlKem1024 => mlkem_ops::decapsulate_1024(&kp.dk_bytes, ciphertext),
            _ => None,
        }
    }

    /// Decapsulate using hybrid RLWE + ML-KEM-768
    fn decapsulate_hybrid(&self, ciphertext: &[u8]) -> Option<[u8; 32]> {
        if ciphertext.len() < 4 { return None; }
        let rlwe_len = u32::from_le_bytes(ciphertext[..4].try_into().ok()?) as usize;
        if ciphertext.len() < 4 + rlwe_len { return None; }

        let rlwe_ct = &ciphertext[4..4+rlwe_len];
        let mlkem_ct = &ciphertext[4+rlwe_len..];

        let rlwe_ss = self.decapsulate_rlwe(rlwe_ct)?;
        let mlkem_ss = self.decapsulate_mlkem(mlkem_ct, KemAlgorithm::MlKem768)?;

        let mut combined_ikm = [0u8; 64];
        combined_ikm[..32].copy_from_slice(&rlwe_ss);
        combined_ikm[32..].copy_from_slice(&mlkem_ss);
        let hk = Hkdf::<Sha256>::new(None, &combined_ikm);
        let mut hybrid_ss = [0u8; 32];
        hk.expand(b"spine-hybrid-kem", &mut hybrid_ss).expect("HKDF expand");

        Some(hybrid_ss)
    }

    /// Decapsulate to recover shared secret (RLWE KEM)
    ///
    /// Recovers the encoded message by computing v - u·s, then rounding
    /// each coefficient to 0 or 1 to recover the original message m.
    /// The shared secret is H(m), matching the encapsulator.
    fn decapsulate_rlwe(&self, ciphertext: &[u8]) -> Option<[u8; 32]> {
        let half = ciphertext.len() / 2;
        if half < self.params.n * 2 {
            return None;
        }

        let u = RingElement::from_bytes(&ciphertext[..half], self.params.n, self.params.q);
        let v = RingElement::from_bytes(&ciphertext[half..], self.params.n, self.params.q);

        // recovered = v - u·s ≈ ⌊q/2⌋·m + (small noise)
        let recovered = v.sub(&u.mul(&self.current_key.secret_key));

        // Round each coefficient: if closer to ⌊q/2⌋ → 1, else → 0
        let half_q = self.params.q as i64 / 2;
        let quarter_q = self.params.q as i64 / 4;
        let m: Vec<i64> = recovered
            .coeffs
            .iter()
            .map(|&c| {
                // Normalize to [0, q)
                let c_pos =
                    ((c % self.params.q as i64) + self.params.q as i64) % self.params.q as i64;
                // If |c - q/2| < q/4, round to 1; otherwise 0
                if (c_pos - half_q).abs() < quarter_q {
                    1i64
                } else {
                    0i64
                }
            })
            .collect();

        // Shared secret = H(m) — matches encapsulate
        let mut hasher = Sha256::new();
        for &mi in &m {
            hasher.update(mi.to_le_bytes());
        }
        Some(hasher.finalize().into())
    }

    /// Get current key hash for synchronization
    pub fn get_key_hash(&self) -> [u8; 32] {
        let mut hasher = Sha256::new();
        hasher.update(self.current_key.public_key.to_bytes());
        hasher.finalize().into()
    }

    /// Verify key chain integrity (constant-time comparison)
    pub fn verify_evolution(&self, expected_hash: &[u8; 32]) -> bool {
        self.key_history
            .iter()
            .any(|h| h.ct_eq(expected_hash).into())
    }

    /// Get evolution counter for synchronization
    pub fn get_evolution_counter(&self) -> u64 {
        self.evolution_counter
    }

    /// Export public key for key exchange
    pub fn export_public_key(&self) -> Vec<u8> {
        self.current_key.public_key.to_bytes()
    }
}

/// Combined quantum-resistant speculative protocol
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuantumSpeculativeProtocol {
    predictor: TransformerPredictor,
    key_evolution: QuantumKeyEvolution,
    prediction_threshold: f32,
    evolution_interval: u64,
    message_count: u64,
}

impl QuantumSpeculativeProtocol {
    pub fn new(
        transformer_config: TransformerConfig,
        lattice_params: LatticeParams,
        seed: u64,
    ) -> Self {
        Self {
            predictor: TransformerPredictor::new(transformer_config),
            key_evolution: QuantumKeyEvolution::new(lattice_params, seed),
            prediction_threshold: 0.8,
            evolution_interval: 10,
            message_count: 0,
        }
    }

    /// Create with a specific KEM algorithm
    pub fn new_with_algorithm(
        transformer_config: TransformerConfig,
        lattice_params: LatticeParams,
        seed: u64,
        algorithm: KemAlgorithm,
    ) -> Self {
        Self {
            predictor: TransformerPredictor::new(transformer_config),
            key_evolution: QuantumKeyEvolution::new_with_algorithm(lattice_params, seed, algorithm),
            prediction_threshold: 0.8,
            evolution_interval: 10,
            message_count: 0,
        }
    }

    /// Get the KEM algorithm in use
    pub fn algorithm(&self) -> KemAlgorithm {
        self.key_evolution.algorithm
    }

    /// Process an outgoing message with prediction and encryption
    pub fn send(&mut self, message: &[u8]) -> QuantumMessage {
        // Check if receiver could predict this
        let (matches, similarity) = self.predictor.verify_prediction(message);

        let payload = if matches && similarity >= self.prediction_threshold {
            // Send confirmation only
            MessagePayload::Confirmation {
                hash: Self::hash_message(message),
                length: message.len(),
            }
        } else {
            // Encapsulate shared secret via RLWE KEM
            let (ciphertext, shared_secret) = self.key_evolution.encapsulate();

            // Derive AES-256-GCM key from KEM shared secret via HKDF
            let hk = Hkdf::<Sha256>::new(None, &shared_secret);
            let mut aes_key = [0u8; 32];
            hk.expand(b"spine-aead-key", &mut aes_key)
                .expect("HKDF expand failed");

            // Create nonce from message count (unique per message)
            let mut nonce_bytes = [0u8; 12];
            nonce_bytes[..8].copy_from_slice(&self.message_count.to_le_bytes());
            let nonce = Nonce::from_slice(&nonce_bytes);

            // Encrypt with AES-256-GCM (authenticated encryption)
            let cipher = Aes256Gcm::new_from_slice(&aes_key).expect("AES key length");
            let encrypted = cipher.encrypt(nonce, message).expect("AES-GCM encrypt");

            // Prepend 12-byte nonce to ciphertext for receiver
            let mut encrypted_message = nonce_bytes.to_vec();
            encrypted_message.extend(encrypted);

            MessagePayload::Full {
                ciphertext,
                encrypted_message,
            }
        };

        // Evolve key periodically
        self.message_count += 1;
        let key_evolution = if self.message_count.is_multiple_of(self.evolution_interval) {
            Some(self.key_evolution.evolve())
        } else {
            None
        };

        QuantumMessage {
            payload,
            evolution_counter: self.key_evolution.get_evolution_counter(),
            key_evolution,
        }
    }

    /// Get a seed for protocol morphing based on current quantum key state
    pub fn get_morph_seed(&self) -> u64 {
        let key_hash = self.key_evolution.get_key_hash();
        u64::from_le_bytes(key_hash[0..8].try_into().unwrap())
    }

    /// Process an incoming message
    pub fn receive(&mut self, quantum_msg: &QuantumMessage) -> Option<Vec<u8>> {
        // Sync key evolution if needed
        while self.key_evolution.get_evolution_counter() < quantum_msg.evolution_counter {
            self.key_evolution.evolve();
        }

        let message = match &quantum_msg.payload {
            MessagePayload::Confirmation { hash, length } => {
                // Use prediction
                let predicted = self.predictor.predict_sequence(*length, true);

                // Verify hash
                let predicted_hash = Self::hash_message(&predicted);
                if &predicted_hash == hash {
                    Some(predicted)
                } else {
                    None // Prediction mismatch, need retransmission
                }
            }
            MessagePayload::Full {
                ciphertext,
                encrypted_message,
            } => {
                // Decapsulate KEM ciphertext to recover shared secret
                let shared_secret = self.key_evolution.decapsulate(ciphertext)?;

                // Derive AES-256-GCM key from KEM shared secret via HKDF
                let hk = Hkdf::<Sha256>::new(None, &shared_secret);
                let mut aes_key = [0u8; 32];
                hk.expand(b"spine-aead-key", &mut aes_key)
                    .expect("HKDF expand failed");

                // Extract nonce (first 12 bytes) and ciphertext
                if encrypted_message.len() < 12 {
                    return None;
                }
                let nonce = Nonce::from_slice(&encrypted_message[..12]);
                let ciphertext_data = &encrypted_message[12..];

                // Decrypt with AES-256-GCM (authenticated — rejects tampered data)
                let cipher = Aes256Gcm::new_from_slice(&aes_key).expect("AES key length");
                cipher.decrypt(nonce, ciphertext_data).ok()
            }
        };

        // Update predictor
        if let Some(ref msg) = message {
            self.predictor.observe(msg);
        }

        message
    }

    fn hash_message(message: &[u8]) -> [u8; 32] {
        let mut hasher = Sha256::new();
        hasher.update(message);
        hasher.finalize().into()
    }

    /// Set prediction confidence threshold
    pub fn set_threshold(&mut self, threshold: f32) {
        self.prediction_threshold = threshold.clamp(0.0, 1.0);
    }

    /// Set key evolution interval
    pub fn set_evolution_interval(&mut self, interval: u64) {
        self.evolution_interval = interval.max(1);
    }

    /// Reset protocol state
    pub fn reset(&mut self) {
        self.predictor.reset();
        self.message_count = 0;
    }
}

/// Wire format for quantum-protected messages
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuantumMessage {
    pub payload: MessagePayload,
    pub evolution_counter: u64,
    pub key_evolution: Option<[u8; 32]>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MessagePayload {
    /// Prediction matched - send only confirmation
    Confirmation { hash: [u8; 32], length: usize },
    /// Full encrypted message
    Full {
        ciphertext: Vec<u8>,
        encrypted_message: Vec<u8>,
    },
}

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

    // -----------------------------------------------------------------
    // Zeroization regression guards (NIST SP 800-171 § 3.13.10).
    // -----------------------------------------------------------------

    /// Compile-time proof that the type implements `ZeroizeOnDrop`.
    /// If the derive ever falls off the struct definition, this stops
    /// compiling — a louder failure than a silently-leaking secret.
    fn assert_zeroize_on_drop<T: ZeroizeOnDrop>() {}

    #[test]
    fn ringelement_implements_zeroize_on_drop() {
        assert_zeroize_on_drop::<RingElement>();
    }

    #[test]
    fn mlkemkeypair_implements_zeroize_on_drop() {
        assert_zeroize_on_drop::<MlKemKeyPair>();
    }

    #[test]
    fn quantumkeypair_implements_zeroize_on_drop() {
        assert_zeroize_on_drop::<QuantumKeyPair>();
    }

    /// Runtime verification: filling a `RingElement` with non-zero
    /// coefficients and then calling `zeroize()` (the same path the
    /// `ZeroizeOnDrop` derive takes on drop) leaves every coefficient
    /// at exactly 0. If this ever fails, the `Zeroize` derive is no
    /// longer covering `coeffs`.
    #[test]
    fn ringelement_zeroize_clears_all_coefficients() {
        let mut rng = StdRng::seed_from_u64(0xAB_CD);
        let mut r = RingElement::random(64, 8_192, &mut rng);
        assert!(
            r.coeffs.iter().any(|&c| c != 0),
            "test precondition: random RingElement should have non-zero coeffs"
        );
        r.zeroize();
        assert!(
            r.coeffs.iter().all(|&c| c == 0),
            "RingElement::zeroize did not clear every coefficient"
        );
    }

    #[test]
    fn mlkemkeypair_zeroize_clears_dk_bytes() {
        let mut rng = StdRng::seed_from_u64(0x12_34);
        let mut kp = mlkem_ops::generate_768(&mut rng);
        assert!(
            kp.dk_bytes.iter().any(|&b| b != 0),
            "test precondition: fresh ML-KEM dk should be non-zero"
        );
        kp.zeroize();
        // Vec is zeroed (length becomes 0 OR bytes are 0 in place
        // — zeroize 1.8 does an in-place clear and then keeps the
        // allocation). Either is fine; the contract is "no surviving
        // secret".
        assert!(
            kp.dk_bytes.iter().all(|&b| b == 0),
            "MlKemKeyPair::zeroize left non-zero bytes in dk_bytes"
        );
    }

    #[test]
    fn quantumkeyevolution_drop_clears_key_history() {
        let mut ev = QuantumKeyEvolution::new(LatticeParams::default(), 0xCAFE);
        // Push two fake history entries so we have something to scrub.
        ev.key_history.push_back([0x11u8; 32]);
        ev.key_history.push_back([0x22u8; 32]);
        assert_eq!(ev.key_history.len(), 2);
        // Manually trigger Drop semantics by replacing with a fresh
        // instance — the old ev is dropped, which runs our impl.
        // After drop, we can't observe the old buffer's bytes safely,
        // but we CAN verify the impl exists by calling drop() directly
        // on a still-borrowable target via a helper.
        ev.key_history.iter_mut().for_each(|h| h.zeroize());
        assert!(
            ev.key_history.iter().all(|h| h.iter().all(|&b| b == 0)),
            "QuantumKeyEvolution key_history not zeroed"
        );
    }

    #[test]
    fn test_positional_encoding() {
        let pe = PositionalEncoding::new(100, 64);
        let enc0 = pe.get(0);
        let enc50 = pe.get(50);
        assert_eq!(enc0.len(), 64);
        assert_ne!(enc0, enc50);
    }

    #[test]
    fn test_layer_norm() {
        let ln = LayerNorm::new(8);
        let input = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
        let output = ln.forward(&input);
        assert_eq!(output.len(), 8);

        // Mean should be ~0
        let mean: f32 = output.iter().sum::<f32>() / output.len() as f32;
        assert!(mean.abs() < 0.01);
    }

    #[test]
    fn test_titans_predictor() {
        let config = TitansConfig {
            embed_dim: 32,
            num_heads: 2,
            num_layers: 1,
            ff_dim: 64,
            max_seq_len: 64,
            memory_size: 16,
            seed: 42,
        };
        let mut predictor = TitansPredictor::new(config);

        // Observe some data
        predictor.observe(b"Hello ");
        predictor.observe(b"World");

        // Predict next
        let (next, conf) = predictor.predict_next();
        assert!(conf > 0.0 && conf <= 1.0);
        // next is u8, already valid in range 0-255
        let _ = next;

        // Check surprise is tracked
        let surprise = predictor.get_surprise();
        assert!(surprise >= 0.0);
    }

    #[test]
    fn test_titans_anomaly_detection() {
        let config = TitansConfig {
            embed_dim: 32,
            num_heads: 2,
            num_layers: 1,
            ff_dim: 64,
            max_seq_len: 64,
            memory_size: 16,
            seed: 42,
        };
        let mut predictor = TitansPredictor::new(config);

        // Train on normal pattern
        for _ in 0..10 {
            predictor.observe(b"GET /api/status\n");
        }
        let _normal_surprise = predictor.get_surprise();

        // Introduce anomalous pattern
        predictor.observe(b"MALICIOUS_PAYLOAD_XYZ!!!");
        let anomaly_surprise = predictor.get_surprise();

        // Anomaly should have higher surprise (or at least be detected)
        assert!(anomaly_surprise >= 0.0);
    }

    #[test]
    fn test_ring_operations() {
        let mut rng = StdRng::seed_from_u64(42);
        let params = LatticeParams {
            n: 16,
            q: 97,
            p: 3,
            sigma: 2.0,
        };

        let a = RingElement::random(params.n, params.q, &mut rng);
        let b = RingElement::random(params.n, params.q, &mut rng);

        let sum = a.add(&b);
        let product = a.mul(&b);

        assert_eq!(sum.coeffs.len(), params.n);
        assert_eq!(product.coeffs.len(), params.n);

        // Check coefficients are in range
        for &c in &sum.coeffs {
            assert!(c >= 0 && c < params.q as i64);
        }
    }

    #[test]
    fn test_key_evolution() {
        let params = LatticeParams {
            n: 32,
            q: 257,
            p: 3,
            sigma: 2.0,
        };
        let mut ke = QuantumKeyEvolution::new(params, 42);

        let hash1 = ke.get_key_hash();
        ke.evolve();
        let hash2 = ke.get_key_hash();

        // Keys should be different after evolution
        assert_ne!(hash1, hash2);

        // Evolution should be tracked
        assert_eq!(ke.get_evolution_counter(), 1);
    }

    #[test]
    fn test_encapsulation() {
        let params = LatticeParams {
            n: 32,
            q: 257,
            p: 3,
            sigma: 2.0,
        };
        let mut ke = QuantumKeyEvolution::new(params, 42);

        let (ciphertext, _shared_secret1) = ke.encapsulate();
        assert!(!ciphertext.is_empty());

        let shared_secret2 = ke.decapsulate(&ciphertext);
        assert!(shared_secret2.is_some());

        // Note: Due to noise, shared secrets may not match exactly in RLWE
        // This is a simplified demo - production would use error correction
    }

    #[test]
    fn test_quantum_speculative_protocol() {
        let config = TitansConfig {
            embed_dim: 16,
            num_heads: 2,
            num_layers: 1,
            ff_dim: 32,
            max_seq_len: 32,
            memory_size: 8,
            seed: 42,
        };
        let params = LatticeParams {
            n: 16,
            q: 97,
            p: 3,
            sigma: 2.0,
        };

        let mut alice = QuantumSpeculativeProtocol::new(config.clone(), params.clone(), 42);
        let mut bob = QuantumSpeculativeProtocol::new(config, params, 42);

        // Alice sends to Bob
        let msg = b"Hello Bob!";
        let quantum_msg = alice.send(msg);

        // Bob receives
        let received = bob.receive(&quantum_msg);
        assert!(received.is_some());
        assert_eq!(received.unwrap(), msg.to_vec());
    }

    #[test]
    fn test_prediction_efficiency() {
        let config = TransformerConfig::default();
        let params = LatticeParams::default();

        let mut sender = QuantumSpeculativeProtocol::new(config.clone(), params.clone(), 42);
        let mut receiver = QuantumSpeculativeProtocol::new(config, params, 42);

        // Send same pattern multiple times to train predictor
        for _ in 0..5 {
            let msg1 = sender.send(b"GET /api/status");
            receiver.receive(&msg1);

            let msg2 = sender.send(b"200 OK");
            receiver.receive(&msg2);
        }

        // After training, check if prediction kicks in
        let msg = sender.send(b"GET /api/status");

        // Even if not confirmed (training takes longer), protocol should work
        let received = receiver.receive(&msg);
        assert!(received.is_some());
    }

    // =========================================================================
    // MIRAS-ADAPTIVE PREDICTOR TESTS
    // =========================================================================

    #[test]
    fn test_miras_predictor_basic() {
        let config = TitansConfig {
            embed_dim: 32,
            num_heads: 2,
            num_layers: 1,
            ff_dim: 64,
            max_seq_len: 64,
            memory_size: 16,
            seed: 42,
        };
        let mut predictor = MirasTitansPredictor::new(config);

        // Observe data
        predictor.observe(b"Hello World");

        // Check variant starts as Titans
        assert_eq!(predictor.variant(), "titans");

        // Predict should work
        let (next, conf) = predictor.predict_next();
        assert!(conf > 0.0 && conf <= 1.0);
        // next is u8, already valid in range 0-255
        let _ = next;

        // Stats should be populated
        let stats = predictor.stats();
        assert_eq!(stats.message_count, 1);
        assert!(stats.miras_enhanced_predictions > 0);
    }

    #[test]
    fn test_miras_predictor_variants() {
        let config = TitansConfig {
            embed_dim: 32,
            num_heads: 2,
            num_layers: 1,
            ff_dim: 64,
            max_seq_len: 64,
            memory_size: 16,
            seed: 42,
        };

        // Test each variant - check initial state before observe
        for variant in [
            MirasVariant::Titans,
            MirasVariant::Yaad,
            MirasVariant::Moneta { p: 2.0 },
            MirasVariant::Memora,
        ] {
            let predictor = MirasTitansPredictor::new_with_variant(config.clone(), variant);

            // Check variant matches what was requested (before any adaptive switching)
            match variant {
                MirasVariant::Titans => assert_eq!(predictor.variant(), "titans"),
                MirasVariant::Yaad => assert_eq!(predictor.variant(), "yaad"),
                MirasVariant::Moneta { .. } => assert_eq!(predictor.variant(), "moneta"),
                MirasVariant::Memora => assert_eq!(predictor.variant(), "memora"),
            }
        }

        // Test that variants can be used after observation (adaptive switching may occur)
        let mut predictor =
            MirasTitansPredictor::new_with_variant(config.clone(), MirasVariant::Yaad);
        assert_eq!(predictor.variant(), "yaad");

        // After observe, low anomaly may switch to Titans (adaptive behavior)
        predictor.observe(b"test");
        // Variant may have changed due to adaptive switching - this is expected behavior
    }

    #[test]
    fn test_miras_predictor_combined_surprise() {
        let config = TitansConfig {
            embed_dim: 32,
            num_heads: 2,
            num_layers: 1,
            ff_dim: 64,
            max_seq_len: 64,
            memory_size: 16,
            seed: 42,
        };
        let mut predictor = MirasTitansPredictor::new(config);

        // Train on normal pattern
        for _ in 0..5 {
            predictor.observe(b"normal message pattern");
        }

        // Get combined surprise
        let combined = predictor.get_combined_surprise();
        assert!(combined >= 0.0);

        // Get individual surprises
        let titans_surprise = predictor.get_surprise();
        let miras_surprise = predictor.get_miras_surprise();

        assert!(titans_surprise >= 0.0);
        assert!(miras_surprise.is_some());
    }

    #[test]
    fn test_miras_predictor_anomaly_level() {
        let config = TitansConfig {
            embed_dim: 32,
            num_heads: 2,
            num_layers: 1,
            ff_dim: 64,
            max_seq_len: 64,
            memory_size: 16,
            seed: 42,
        };
        let mut predictor = MirasTitansPredictor::new(config);

        // Initially no anomaly
        assert_eq!(predictor.anomaly_level(), 0.0);

        // After observation, anomaly level is tracked
        predictor.observe(b"test");
        let level = predictor.anomaly_level();
        assert!(level >= 0.0); // Some level is tracked
    }

    #[test]
    fn test_miras_predictor_reset() {
        let config = TitansConfig {
            embed_dim: 32,
            num_heads: 2,
            num_layers: 1,
            ff_dim: 64,
            max_seq_len: 64,
            memory_size: 16,
            seed: 42,
        };
        let mut predictor = MirasTitansPredictor::new(config);

        // Add some state
        for _ in 0..10 {
            predictor.observe(b"data");
        }
        assert!(predictor.stats().message_count > 0);

        // Reset
        predictor.reset_all();
        let stats = predictor.stats();
        assert_eq!(stats.message_count, 0);
    }

    // =========================================================================
    // COMPREHENSIVE SECURITY TESTS (W3: Security Validation)
    // =========================================================================

    #[test]
    fn test_rlwe_ring_arithmetic_correctness() {
        // Test ring arithmetic properties: associativity, commutativity, distributivity
        let mut rng = StdRng::seed_from_u64(12345);
        let params = LatticeParams {
            n: 32,
            q: 257,
            p: 3,
            sigma: 2.0,
        };

        let a = RingElement::random(params.n, params.q, &mut rng);
        let b = RingElement::random(params.n, params.q, &mut rng);
        let c = RingElement::random(params.n, params.q, &mut rng);

        // Commutativity of addition: a + b = b + a
        let ab = a.add(&b);
        let ba = b.add(&a);
        assert_eq!(ab.coeffs, ba.coeffs, "Addition should be commutative");

        // Associativity of addition: (a + b) + c = a + (b + c)
        let ab_c = a.add(&b).add(&c);
        let a_bc = a.add(&b.add(&c));
        assert_eq!(ab_c.coeffs, a_bc.coeffs, "Addition should be associative");

        // Distributivity: a * (b + c) = a*b + a*c
        let a_times_bplusc = a.mul(&b.add(&c));
        let ab_plus_ac = a.mul(&b).add(&a.mul(&c));
        assert_eq!(
            a_times_bplusc.coeffs, ab_plus_ac.coeffs,
            "Multiplication should distribute over addition"
        );
    }

    #[test]
    fn test_rlwe_gaussian_distribution() {
        // Verify Gaussian noise has expected statistical properties
        let mut rng = StdRng::seed_from_u64(54321);
        let params = LatticeParams {
            n: 1024,
            q: 12289, // NIST-like parameter
            p: 3,
            sigma: 3.2,
        };

        let e = RingElement::random_gaussian(params.n, params.q, params.sigma, &mut rng);

        // Calculate mean and variance
        let mean: f64 = e.coeffs.iter().map(|&c| c as f64).sum::<f64>() / params.n as f64;
        let variance: f64 = e
            .coeffs
            .iter()
            .map(|&c| (c as f64 - mean).powi(2))
            .sum::<f64>()
            / params.n as f64;

        // Mean should be close to 0 (centered)
        assert!(
            mean.abs() < params.sigma,
            "Gaussian mean should be near 0, got {}",
            mean
        );

        // Variance should be close to sigma^2
        let expected_variance = params.sigma * params.sigma;
        assert!(
            (variance - expected_variance).abs() < expected_variance * 0.5,
            "Variance {} should be close to sigma^2 = {}",
            variance,
            expected_variance
        );
    }

    #[test]
    fn test_rlwe_ternary_distribution() {
        // Verify ternary noise is in {-1, 0, 1}
        let mut rng = StdRng::seed_from_u64(98765);
        let params = LatticeParams {
            n: 256,
            q: 257,
            p: 3,
            sigma: 2.0,
        };

        let s = RingElement::random_ternary(params.n, params.q, &mut rng);

        // Ternary coefficients are in {-1, 0, 1} before reduction
        for &coeff in &s.coeffs {
            assert!(
                coeff == 0 || coeff == 1 || coeff == -1,
                "Ternary coefficient should be -1, 0, or 1, got {}",
                coeff
            );
        }

        // Check roughly uniform distribution among {-1, 0, 1}
        let count_zero = s.coeffs.iter().filter(|&&c| c == 0).count();
        let count_one = s.coeffs.iter().filter(|&&c| c == 1).count();
        let count_neg = s.coeffs.iter().filter(|&&c| c == -1).count();

        // Each should be roughly 1/3 of total
        let expected = params.n / 3;
        let tolerance = params.n / 4; // Allow 25% deviation
        assert!(
            (count_zero as isize - expected as isize).unsigned_abs() < tolerance,
            "Ternary distribution unbalanced: zeros={}, ones={}, neg={}",
            count_zero,
            count_one,
            count_neg
        );
    }

    #[test]
    fn test_key_evolution_forward_secrecy() {
        // Test that key evolution provides forward secrecy
        let params = LatticeParams {
            n: 64,
            q: 257,
            p: 3,
            sigma: 2.0,
        };

        let mut ke1 = QuantumKeyEvolution::new(params.clone(), 42);
        let mut ke2 = QuantumKeyEvolution::new(params, 42);

        // Both start with same state
        assert_eq!(ke1.get_key_hash(), ke2.get_key_hash());

        // Evolve ke1 multiple times
        for _ in 0..5 {
            ke1.evolve();
        }

        // Keys should now be different
        assert_ne!(ke1.get_key_hash(), ke2.get_key_hash());

        // Counter should track evolutions
        assert_eq!(ke1.get_evolution_counter(), 5);
        assert_eq!(ke2.get_evolution_counter(), 0);

        // Sync ke2 to same state
        for _ in 0..5 {
            ke2.evolve();
        }

        // Now should match again (deterministic evolution)
        assert_eq!(ke1.get_key_hash(), ke2.get_key_hash());
    }

    #[test]
    fn test_key_evolution_history_integrity() {
        let params = LatticeParams {
            n: 32,
            q: 257,
            p: 3,
            sigma: 2.0,
        };

        let mut ke = QuantumKeyEvolution::new(params, 42);

        // Collect hashes during evolution
        let mut hashes = Vec::new();
        for _ in 0..10 {
            let hash = ke.evolve();
            hashes.push(hash);
        }

        // All hashes should be unique (no cycles)
        let unique_count = hashes
            .iter()
            .collect::<std::collections::HashSet<_>>()
            .len();
        assert_eq!(unique_count, 10, "All evolution hashes should be unique");

        // History verification should work for recent keys
        for hash in &hashes {
            assert!(
                ke.verify_evolution(hash),
                "Recent evolution should be verifiable"
            );
        }
    }

    #[test]
    fn test_quantum_protocol_message_integrity() {
        // Test that messages are correctly encrypted and decrypted
        let config = TitansConfig {
            embed_dim: 16,
            num_heads: 2,
            num_layers: 1,
            ff_dim: 32,
            max_seq_len: 32,
            memory_size: 8,
            seed: 42,
        };
        let params = LatticeParams {
            n: 32,
            q: 257,
            p: 3,
            sigma: 2.0,
        };

        let mut alice = QuantumSpeculativeProtocol::new(config.clone(), params.clone(), 42);
        let mut bob = QuantumSpeculativeProtocol::new(config, params, 42);

        // Test multiple different message sizes
        let test_messages = [
            b"A".to_vec(),
            b"Short".to_vec(),
            b"Medium length message".to_vec(),
            b"This is a longer message to test variable length handling properly".to_vec(),
        ];

        for msg in &test_messages {
            let quantum_msg = alice.send(msg);
            let received = bob.receive(&quantum_msg);
            assert!(received.is_some(), "Should receive message");
            assert_eq!(
                &received.unwrap(),
                msg,
                "Received message should match original"
            );
        }
    }

    #[test]
    fn test_tampered_ciphertext_detection() {
        let params = LatticeParams {
            n: 32,
            q: 257,
            p: 3,
            sigma: 2.0,
        };

        let mut ke = QuantumKeyEvolution::new(params, 42);

        let (mut ciphertext, original_secret) = ke.encapsulate();

        // Tamper with ciphertext
        if !ciphertext.is_empty() {
            ciphertext[0] ^= 0xFF;
        }

        // Decapsulation with tampered ciphertext should produce different result
        let tampered_secret = ke.decapsulate(&ciphertext);

        if let Some(tampered) = tampered_secret {
            // Due to noise characteristics, tampered ciphertext produces different secret
            // In production, we'd add MAC for integrity checking
            assert_ne!(
                tampered, original_secret,
                "Tampered ciphertext should produce different secret"
            );
        }
    }

    #[test]
    fn test_lattice_params_security_levels() {
        // Test different security parameter sets
        let toy_params = LatticeParams {
            n: 16,
            q: 97,
            p: 3,
            sigma: 2.0,
        };
        let medium_params = LatticeParams {
            n: 256,
            q: 7681,
            p: 3,
            sigma: 3.19,
        };
        let _high_params = LatticeParams {
            n: 1024,
            q: 12289,
            p: 3,
            sigma: 3.19,
        };

        // Verify params are valid (n is power of 2, q is prime)
        assert!(
            toy_params.n.is_power_of_two(),
            "n should be power of 2 for NTT"
        );
        assert!(
            medium_params.n.is_power_of_two(),
            "n should be power of 2 for NTT"
        );

        // Key generation should work for all param sets
        let mut ke_toy = QuantumKeyEvolution::new(toy_params, 1);
        let mut ke_med = QuantumKeyEvolution::new(medium_params, 1);

        // Both should be able to encapsulate/decapsulate
        let (ct_toy, _) = ke_toy.encapsulate();
        let (ct_med, _) = ke_med.encapsulate();

        assert!(!ct_toy.is_empty());
        assert!(!ct_med.is_empty());

        // Medium params should produce larger ciphertext
        assert!(
            ct_med.len() > ct_toy.len(),
            "Higher security params should produce larger ciphertext"
        );
    }

    #[test]
    fn test_titans_predictor_statistical_properties() {
        let config = TitansConfig {
            embed_dim: 32,
            num_heads: 2,
            num_layers: 1,
            ff_dim: 64,
            max_seq_len: 64,
            memory_size: 16,
            seed: 42,
        };
        let mut predictor = TitansPredictor::new(config);

        // Train on repetitive pattern
        let pattern = b"ABCABC";
        for _ in 0..20 {
            predictor.observe(pattern);
        }

        // Predictions should have reasonable confidence
        let (_, confidence) = predictor.predict_next();
        assert!(
            (0.0..=1.0).contains(&confidence),
            "Confidence should be normalized"
        );

        // Surprise should be tracked
        let surprise = predictor.get_surprise();
        assert!(surprise >= 0.0, "Surprise should be non-negative");
    }

    #[test]
    fn test_kem_shared_secret_match() {
        // Verify that encapsulate and decapsulate produce matching shared secrets
        let params = LatticeParams {
            n: 64,
            q: 257,
            p: 3,
            sigma: 1.5,
        };
        let mut ke = QuantumKeyEvolution::new(params, 12345);

        let (ciphertext, shared_secret_enc) = ke.encapsulate();
        let shared_secret_dec = ke.decapsulate(&ciphertext).unwrap();

        assert_eq!(
            shared_secret_enc, shared_secret_dec,
            "KEM shared secrets must match between encapsulate and decapsulate"
        );
    }

    #[test]
    fn test_aead_tampered_ciphertext_rejected() {
        // Verify that AES-256-GCM rejects tampered ciphertext
        let config = TransformerConfig::default();
        let params = LatticeParams {
            n: 32,
            q: 257,
            p: 3,
            sigma: 2.0,
        };

        let mut alice = QuantumSpeculativeProtocol::new(config.clone(), params.clone(), 42);
        let mut bob = QuantumSpeculativeProtocol::new(config, params, 42);

        let msg = b"Secret message";
        let mut quantum_msg = alice.send(msg);

        // Tamper with the encrypted message
        if let MessagePayload::Full {
            ref mut encrypted_message,
            ..
        } = quantum_msg.payload
        {
            if let Some(byte) = encrypted_message.last_mut() {
                *byte ^= 0xFF; // Flip bits
            }
        }

        // Bob should reject tampered message (AES-GCM authentication failure)
        let received = bob.receive(&quantum_msg);
        assert!(
            received.is_none(),
            "Tampered ciphertext must be rejected by AEAD"
        );
    }

    #[test]
    fn test_key_evolution_maintains_kem_invariant() {
        // Verify that key evolution produces valid keypairs (b = a*s + e)
        let params = LatticeParams {
            n: 32,
            q: 257,
            p: 3,
            sigma: 2.0,
        };
        let mut ke = QuantumKeyEvolution::new(params, 99);

        for _ in 0..5 {
            ke.evolve();
            // After evolution, encaps/decaps should still work
            let (ct, ss_enc) = ke.encapsulate();
            let ss_dec = ke.decapsulate(&ct).unwrap();
            assert_eq!(ss_enc, ss_dec, "KEM must work after key evolution");
        }
    }

    #[test]
    fn test_key_evolution_deterministic_hkdf() {
        // Verify that two instances with the same seed evolve identically
        let params = LatticeParams::default();
        let mut ke1 = QuantumKeyEvolution::new(params.clone(), 7777);
        let mut ke2 = QuantumKeyEvolution::new(params, 7777);

        for _ in 0..5 {
            let h1 = ke1.evolve();
            let h2 = ke2.evolve();
            assert_eq!(
                h1, h2,
                "Deterministic evolution must produce identical hashes"
            );
        }
        assert_eq!(ke1.get_key_hash(), ke2.get_key_hash());
    }

    #[test]
    fn test_aes_gcm_round_trip() {
        // Full send/receive round-trip with AES-256-GCM
        let config = TransformerConfig::default();
        let params = LatticeParams {
            n: 64,
            q: 257,
            p: 3,
            sigma: 1.5,
        };

        let mut alice = QuantumSpeculativeProtocol::new(config.clone(), params.clone(), 100);
        let mut bob = QuantumSpeculativeProtocol::new(config, params, 100);

        // Send multiple messages
        for i in 0..5 {
            let msg = format!("Message number {}", i);
            let quantum_msg = alice.send(msg.as_bytes());
            let received = bob.receive(&quantum_msg);
            assert!(received.is_some(), "Message {} should decrypt", i);
            assert_eq!(
                received.unwrap(),
                msg.as_bytes(),
                "Message {} content mismatch",
                i
            );
        }
    }

    // ── Phase 24: ML-KEM (FIPS 203) tests ──────────────────────────────

    #[test]
    fn test_mlkem_512_round_trip() {
        let mut rng = StdRng::seed_from_u64(1);
        let kp = mlkem_ops::generate_512(&mut rng);
        assert_eq!(kp.algorithm, KemAlgorithm::MlKem512);

        let (ct, ss_enc) = mlkem_ops::encapsulate_512(&kp.ek_bytes, &mut rng).unwrap();
        let ss_dec = mlkem_ops::decapsulate_512(&kp.dk_bytes, &ct).unwrap();
        assert_eq!(ss_enc.len(), 32);
        assert_eq!(ss_enc, ss_dec, "ML-KEM-512 shared secret mismatch");
    }

    #[test]
    fn test_mlkem_768_round_trip() {
        let mut rng = StdRng::seed_from_u64(2);
        let kp = mlkem_ops::generate_768(&mut rng);
        assert_eq!(kp.algorithm, KemAlgorithm::MlKem768);

        let (ct, ss_enc) = mlkem_ops::encapsulate_768(&kp.ek_bytes, &mut rng).unwrap();
        let ss_dec = mlkem_ops::decapsulate_768(&kp.dk_bytes, &ct).unwrap();
        assert_eq!(ss_enc.len(), 32);
        assert_eq!(ss_enc, ss_dec, "ML-KEM-768 shared secret mismatch");
    }

    #[test]
    fn test_mlkem_1024_round_trip() {
        let mut rng = StdRng::seed_from_u64(3);
        let kp = mlkem_ops::generate_1024(&mut rng);
        assert_eq!(kp.algorithm, KemAlgorithm::MlKem1024);

        let (ct, ss_enc) = mlkem_ops::encapsulate_1024(&kp.ek_bytes, &mut rng).unwrap();
        let ss_dec = mlkem_ops::decapsulate_1024(&kp.dk_bytes, &ct).unwrap();
        assert_eq!(ss_enc.len(), 32);
        assert_eq!(ss_enc, ss_dec, "ML-KEM-1024 shared secret mismatch");
    }

    #[test]
    fn test_mlkem_different_keypairs_produce_different_secrets() {
        let mut rng = StdRng::seed_from_u64(4);
        let kp1 = mlkem_ops::generate_768(&mut rng);
        let kp2 = mlkem_ops::generate_768(&mut rng);

        let (_, ss1) = mlkem_ops::encapsulate_768(&kp1.ek_bytes, &mut rng).unwrap();
        let (_, ss2) = mlkem_ops::encapsulate_768(&kp2.ek_bytes, &mut rng).unwrap();

        // Overwhelmingly likely to differ (2^-256 collision probability)
        assert_ne!(ss1, ss2, "Different keypairs should yield different secrets");
    }

    #[test]
    fn test_mlkem_wrong_key_decapsulation_fails() {
        let mut rng = StdRng::seed_from_u64(5);
        let kp1 = mlkem_ops::generate_768(&mut rng);
        let kp2 = mlkem_ops::generate_768(&mut rng);

        let (ct, ss_enc) = mlkem_ops::encapsulate_768(&kp1.ek_bytes, &mut rng).unwrap();
        // Decapsulating with wrong key should yield a different (implicit reject) secret
        let ss_wrong = mlkem_ops::decapsulate_768(&kp2.dk_bytes, &ct).unwrap();
        assert_ne!(
            ss_enc, ss_wrong,
            "Wrong DK must produce different shared secret (implicit reject)"
        );
    }

    #[test]
    fn test_kem_algorithm_default() {
        assert_eq!(KemAlgorithm::default(), KemAlgorithm::MlKem768);
    }

    #[test]
    fn test_quantum_key_evolution_with_mlkem() {
        let params = LatticeParams {
            n: 32,
            q: 257,
            p: 3,
            sigma: 2.0,
        };
        let mut ke = QuantumKeyEvolution::new_with_algorithm(params, 42, KemAlgorithm::MlKem768);

        // Encapsulate/decapsulate should work with ML-KEM
        let (ct, ss_enc) = ke.encapsulate();
        let ss_dec = ke.decapsulate(&ct).unwrap();
        assert_eq!(ss_enc, ss_dec, "ML-KEM encaps/decaps via QuantumKeyEvolution");
        assert!(!ct.is_empty());
    }

    #[test]
    fn test_quantum_key_evolution_hybrid_kem() {
        let params = LatticeParams {
            n: 32,
            q: 257,
            p: 3,
            sigma: 2.0,
        };
        let mut ke = QuantumKeyEvolution::new_with_algorithm(params, 42, KemAlgorithm::Hybrid);

        let (ct, ss_enc) = ke.encapsulate();
        let ss_dec = ke.decapsulate(&ct).unwrap();
        assert_eq!(ss_enc, ss_dec, "Hybrid RLWE+ML-KEM shared secret mismatch");
        assert_eq!(ss_enc.len(), 32, "Hybrid shared secret should be 32 bytes");
        // Hybrid ciphertext is larger (RLWE + ML-KEM-768 concatenated)
        assert!(ct.len() > 100, "Hybrid ciphertext should be large");
    }

    #[test]
    fn test_mlkem_key_evolution_maintains_invariant() {
        let params = LatticeParams {
            n: 32,
            q: 257,
            p: 3,
            sigma: 2.0,
        };
        let mut ke = QuantumKeyEvolution::new_with_algorithm(params, 55, KemAlgorithm::MlKem768);

        for i in 0..5 {
            ke.evolve();
            let (ct, ss_enc) = ke.encapsulate();
            let ss_dec = ke.decapsulate(&ct).unwrap();
            assert_eq!(ss_enc, ss_dec, "ML-KEM must work after evolution step {}", i);
        }
    }

    #[test]
    fn test_quantum_speculative_protocol_with_mlkem() {
        let config = TransformerConfig::default();
        let params = LatticeParams {
            n: 32,
            q: 257,
            p: 3,
            sigma: 2.0,
        };

        let mut alice = QuantumSpeculativeProtocol::new_with_algorithm(
            config.clone(),
            params.clone(),
            42,
            KemAlgorithm::MlKem768,
        );
        let mut bob = QuantumSpeculativeProtocol::new_with_algorithm(
            config,
            params,
            42,
            KemAlgorithm::MlKem768,
        );

        assert_eq!(alice.algorithm(), KemAlgorithm::MlKem768);
        assert_eq!(bob.algorithm(), KemAlgorithm::MlKem768);

        let msg = b"ML-KEM secured message";
        let quantum_msg = alice.send(msg);
        let received = bob.receive(&quantum_msg);
        assert!(received.is_some());
        assert_eq!(received.unwrap(), msg);
    }

    #[test]
    fn test_mlkem_ciphertext_sizes() {
        let mut rng = StdRng::seed_from_u64(6);
        let kp512 = mlkem_ops::generate_512(&mut rng);
        let kp768 = mlkem_ops::generate_768(&mut rng);
        let kp1024 = mlkem_ops::generate_1024(&mut rng);

        let (ct512, _) = mlkem_ops::encapsulate_512(&kp512.ek_bytes, &mut rng).unwrap();
        let (ct768, _) = mlkem_ops::encapsulate_768(&kp768.ek_bytes, &mut rng).unwrap();
        let (ct1024, _) = mlkem_ops::encapsulate_1024(&kp1024.ek_bytes, &mut rng).unwrap();

        assert_eq!(ct512.len(), 768, "ML-KEM-512 ciphertext should be 768 bytes");
        assert_eq!(ct768.len(), 1088, "ML-KEM-768 ciphertext should be 1088 bytes");
        assert_eq!(ct1024.len(), 1568, "ML-KEM-1024 ciphertext should be 1568 bytes");

        // Monotonically increasing with security level
        assert!(ct512.len() < ct768.len());
        assert!(ct768.len() < ct1024.len());
    }
}