paseto-pq 0.1.2

Post-quantum PASETO tokens with RFC-compliant footer authentication using ML-DSA signatures
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
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
//! # PASETO-PQ: Post-Quantum PASETO Tokens
//!
//! A pure post-quantum implementation of PASETO-inspired tokens using ML-DSA (CRYSTALS-Dilithium)
//! for digital signatures. This crate provides quantum-safe authentication tokens that are
//! resistant to attacks by quantum computers.
//!
//! ## Design Principles
//!
//! - **Post-Quantum Only**: Uses ML-DSA-65 (NIST FIPS 204) for all signatures
//! - **PASETO-Inspired**: Follows PASETO's security model but with PQ algorithms
//! - **Greenfield**: No legacy compatibility, designed for quantum-safe future
//! - **Memory Safety**: Automatic zeroization of sensitive keys on drop
//! - **Cryptographic Hygiene**: Proper HKDF key derivation and secure random generation
//!
//! ## ⚠️ Non-Standard Token Format
//!
//! **IMPORTANT**: This crate uses a **non-standard** token versioning scheme that diverges
//! from the official PASETO specification. The tokens use `pq1` to clearly indicate
//! post-quantum algorithms and avoid confusion with standard PASETO versions.
//!
//! ### Token Format
//!
//! ```text
//! paseto.pq1.public.<base64url-encoded-payload>.<base64url-encoded-ml-dsa-signature>
//! paseto.pq1.local.<base64url-encoded-encrypted-payload>
//! ```
//!
//! ### Interoperability Warning
//!
//! These tokens are **NOT** compatible with standard PASETO libraries or tooling.
//! If you need interoperability with existing PASETO ecosystems, this crate is not suitable.
//! The `pq1` versioning scheme clearly indicates "post-quantum era" tokens, distinguishing
//! them from the classical algorithms defined in the PASETO specification.
//!
//! Consider this crate for:
//! - Greenfield applications requiring post-quantum security
//! - Internal systems where PASETO compatibility is not required
//! - Future migration paths when post-quantum PASETO standards emerge
//!
//! ## Example Usage
//!
//! ```rust,no_run
//! use paseto_pq::{PasetoPQ, Claims, KeyPair};
//! use time::OffsetDateTime;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Generate a new key pair
//! let mut rng = rand::thread_rng();
//! let keypair = KeyPair::generate(&mut rng);
//!
//! // Create claims
//! let mut claims = Claims::new();
//! claims.set_subject("user123")?;
//! claims.set_issuer("my-service")?;
//! claims.set_audience("api.example.com")?;
//! claims.set_expiration(OffsetDateTime::now_utc() + time::Duration::hours(1))?;
//! claims.add_custom("tenant_id", "org_abc123")?;
//! claims.add_custom("roles", &["user", "admin"])?;
//!
//! // Sign the token
//! let token = PasetoPQ::sign(keypair.signing_key(), &claims)?;
//!
//! // Verify the token
//! let verified = PasetoPQ::verify(keypair.verifying_key(), &token)?;
//! let verified_claims = verified.claims();
//! assert_eq!(verified_claims.subject(), Some("user123"));
//! # Ok(())
//! # }
//! ```

use std::collections::HashMap;
use std::fmt;

use anyhow::Result;
pub mod pae;

use base64::Engine;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
// Conditional compilation for ML-DSA parameter set selection
#[cfg(all(
    feature = "ml-dsa-44",
    not(any(feature = "ml-dsa-65", feature = "ml-dsa-87"))
))]
use ml_dsa::MlDsa44 as MlDsaParam;

#[cfg(all(
    feature = "ml-dsa-65",
    not(any(feature = "ml-dsa-44", feature = "ml-dsa-87"))
))]
use ml_dsa::MlDsa65 as MlDsaParam;

#[cfg(all(
    feature = "ml-dsa-87",
    not(any(feature = "ml-dsa-44", feature = "ml-dsa-65"))
))]
use ml_dsa::MlDsa87 as MlDsaParam;

// Compilation guards to ensure exactly one parameter set is selected
#[cfg(not(any(feature = "ml-dsa-44", feature = "ml-dsa-65", feature = "ml-dsa-87")))]
compile_error!(
    "Please enable exactly one of the features: `ml-dsa-44`, `ml-dsa-65`, or `ml-dsa-87`."
);

#[cfg(all(
    feature = "ml-dsa-44",
    any(feature = "ml-dsa-65", feature = "ml-dsa-87")
))]
compile_error!("Only one of `ml-dsa-44`, `ml-dsa-65`, or `ml-dsa-87` may be enabled.");

#[cfg(all(feature = "ml-dsa-65", feature = "ml-dsa-87"))]
compile_error!("Only one of `ml-dsa-44`, `ml-dsa-65`, or `ml-dsa-87` may be enabled.");

use ml_dsa::{
    KeyGen,
    signature::{SignatureEncoding, Signer, Verifier},
};
// ML-KEM imports for real implementation
use hkdf::Hkdf;
use ml_kem::{
    KemCore, MlKem768,
    kem::{Decapsulate, Encapsulate},
};
pub use rand_core::{CryptoRng, RngCore};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sha2::Sha256;
use zeroize::{Zeroize, ZeroizeOnDrop};

use time::OffsetDateTime;

// Symmetric encryption imports
use chacha20poly1305::{
    ChaCha20Poly1305, Nonce,
    aead::{AeadCore, AeadInPlace, KeyInit, OsRng as AeadOsRng},
};

#[cfg(feature = "logging")]
use tracing::{debug, instrument, warn};

/// Post-quantum PASETO implementation using ML-DSA-65
pub struct PasetoPQ;

// Re-export core PAE function for advanced users (added in v0.1.1)
// Internal functions like le64_encode remain private
pub use pae::pae_encode;

/// A post-quantum key pair for signing and verification
#[derive(Clone)]
pub struct KeyPair {
    signing_key: SigningKey,
    verifying_key: VerifyingKey,
}

/// A signing key for creating tokens
#[derive(Clone)]
pub struct SigningKey(ml_dsa::SigningKey<MlDsaParam>);

/// A verifying key for validating tokens
#[derive(Clone)]
pub struct VerifyingKey(ml_dsa::VerifyingKey<MlDsaParam>);

/// A symmetric key for local token encryption/decryption
#[derive(Clone, Zeroize, ZeroizeOnDrop)]
pub struct SymmetricKey([u8; 32]);

/// A post-quantum key encapsulation key pair for key exchange
#[derive(Clone)]
pub struct KemKeyPair {
    pub encapsulation_key: EncapsulationKey,
    pub decapsulation_key: DecapsulationKey,
}

/// An encapsulation key for ML-KEM key exchange
#[derive(Clone)]
pub struct EncapsulationKey(<MlKem768 as KemCore>::EncapsulationKey);

/// A decapsulation key for ML-KEM key exchange
#[derive(Clone)]
pub struct DecapsulationKey(<MlKem768 as KemCore>::DecapsulationKey);

/// Footer data for additional authenticated metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Footer {
    /// Key identifier for key rotation and selection
    #[serde(skip_serializing_if = "Option::is_none")]
    pub kid: Option<String>,

    /// Token version for compatibility tracking
    #[serde(skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,

    /// Issuer-specific metadata
    #[serde(skip_serializing_if = "Option::is_none")]
    pub issuer_meta: Option<String>,

    /// Additional custom metadata
    #[serde(flatten)]
    pub custom: HashMap<String, Value>,
}

impl Footer {
    /// Create a new empty footer
    pub fn new() -> Self {
        Self {
            kid: None,
            version: None,
            issuer_meta: None,
            custom: HashMap::new(),
        }
    }

    /// Set the key identifier
    pub fn set_kid(&mut self, kid: &str) -> Result<(), PqPasetoError> {
        self.kid = Some(kid.to_string());
        Ok(())
    }

    /// Set the version
    pub fn set_version(&mut self, version: &str) -> Result<(), PqPasetoError> {
        self.version = Some(version.to_string());
        Ok(())
    }

    /// Set issuer metadata
    pub fn set_issuer_meta(&mut self, issuer_meta: &str) -> Result<(), PqPasetoError> {
        self.issuer_meta = Some(issuer_meta.to_string());
        Ok(())
    }

    /// Add custom footer field
    pub fn add_custom<T: Serialize + ?Sized>(
        &mut self,
        key: &str,
        value: &T,
    ) -> Result<(), PqPasetoError> {
        let json_value = serde_json::to_value(value)?;
        self.custom.insert(key.to_string(), json_value);
        Ok(())
    }

    /// Get custom footer field
    pub fn get_custom(&self, key: &str) -> Option<&Value> {
        self.custom.get(key)
    }

    /// Get key identifier
    pub fn kid(&self) -> Option<&str> {
        self.kid.as_deref()
    }

    /// Get version
    pub fn version(&self) -> Option<&str> {
        self.version.as_deref()
    }

    /// Get issuer metadata
    pub fn issuer_meta(&self) -> Option<&str> {
        self.issuer_meta.as_deref()
    }

    /// Serialize footer to base64url-encoded JSON
    pub fn to_base64(&self) -> Result<String, PqPasetoError> {
        let json = serde_json::to_vec(self)?;
        Ok(URL_SAFE_NO_PAD.encode(&json))
    }

    /// Deserialize footer from base64url-encoded JSON
    pub(crate) fn from_base64(encoded: &str) -> Result<Self, PqPasetoError> {
        let bytes = URL_SAFE_NO_PAD.decode(encoded)?;
        let footer = serde_json::from_slice(&bytes)?;
        Ok(footer)
    }
}

impl Default for Footer {
    fn default() -> Self {
        Self::new()
    }
}

/// Claims contained within a token
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Claims {
    /// Token issuer
    #[serde(skip_serializing_if = "Option::is_none")]
    pub iss: Option<String>,

    /// Token subject
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sub: Option<String>,

    /// Token audience
    #[serde(skip_serializing_if = "Option::is_none")]
    pub aud: Option<String>,

    /// Token expiration time
    #[serde(
        skip_serializing_if = "Option::is_none",
        default,
        with = "time::serde::rfc3339::option"
    )]
    pub exp: Option<OffsetDateTime>,

    /// Token not-before time
    #[serde(
        skip_serializing_if = "Option::is_none",
        default,
        with = "time::serde::rfc3339::option"
    )]
    pub nbf: Option<OffsetDateTime>,

    /// Token issued-at time
    #[serde(
        skip_serializing_if = "Option::is_none",
        default,
        with = "time::serde::rfc3339::option"
    )]
    pub iat: Option<OffsetDateTime>,

    /// Token identifier (jti)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub jti: Option<String>,

    /// Key identifier
    #[serde(skip_serializing_if = "Option::is_none")]
    pub kid: Option<String>,

    /// Custom claims (Conflux-specific)
    #[serde(flatten)]
    pub custom: HashMap<String, Value>,
}

/// Verified token containing validated claims and optional footer
#[derive(Debug, Clone)]
pub struct VerifiedToken {
    claims: Claims,
    footer: Option<Footer>,
    raw_token: String,
}

/// Parsed token structure for inspection without cryptographic operations
///
/// This struct allows you to examine token metadata (purpose, version, footer)
/// without performing expensive cryptographic verification or decryption.
/// Useful for debugging, logging, middleware, and routing decisions.
///
/// # Example
///
/// ```rust,no_run
/// use paseto_pq::ParsedToken;
///
/// let token = "paseto.pq1.public.ABC123...";
/// let parsed = ParsedToken::parse(token)?;
///
/// println!("Purpose: {}", parsed.purpose()); // "public"
/// println!("Version: {}", parsed.version()); // "pq1"
/// println!("Has footer: {}", parsed.has_footer());
///
/// // Use for routing decisions
/// match parsed.purpose() {
///     "public" => println!("Public token - needs verification"),
///     "local" => println!("Local token - needs decryption"),
///     _ => println!("Unsupported token type"),
/// }
/// # Ok::<(), paseto_pq::PqPasetoError>(())
/// ```
#[derive(Debug, Clone)]
pub struct ParsedToken {
    purpose: String,
    version: String,
    payload: Vec<u8>,
    signature_or_tag: Option<Vec<u8>>, // For public tokens (signature) or local tokens (auth tag)
    footer: Option<Footer>,
    raw_token: String,
}

/// Token size breakdown showing individual components
///
/// This struct provides detailed information about how token size is distributed
/// across different components, useful for optimization and debugging.
#[derive(Debug, Clone)]
pub struct TokenSizeBreakdown {
    /// Size of the protocol prefix ("paseto.pq1.public." or "paseto.pq1.local.")
    pub prefix: usize,
    /// Size of the JSON payload after base64 encoding
    pub payload: usize,
    /// Size of signature (public tokens) or authentication tag (local tokens)
    pub signature_or_tag: usize,
    /// Size of footer if present
    pub footer: Option<usize>,
    /// Size of separator dots between parts
    pub separators: usize,
    /// Additional overhead from base64 encoding (~33% expansion)
    pub base64_overhead: usize,
}

/// Token size estimator for planning and optimization
///
/// This struct allows you to estimate token sizes before creation to avoid
/// runtime surprises with HTTP headers, cookies, or URL length limits.
///
/// # Example
///
/// ```rust,no_run
/// use paseto_pq::{Claims, TokenSizeEstimator};
///
/// let mut claims = Claims::new();
/// claims.set_subject("user123").unwrap();
/// claims.add_custom("role", "admin").unwrap();
///
/// let estimator = TokenSizeEstimator::public(&claims, true);
/// println!("Estimated size: {} bytes", estimator.total_bytes());
///
/// if !estimator.fits_in_cookie() {
///     println!("Token too large for browser cookies!");
/// }
/// # Ok::<(), paseto_pq::PqPasetoError>(())
/// ```
#[derive(Debug, Clone)]
pub struct TokenSizeEstimator {
    breakdown: TokenSizeBreakdown,
}

/// Errors that can occur during token operations
#[derive(Debug, thiserror::Error)]
pub enum PqPasetoError {
    #[error("Invalid token format: {0}")]
    InvalidFormat(String),

    #[error("Signature verification failed")]
    SignatureVerificationFailed,

    #[error("Token has expired")]
    TokenExpired,

    #[error("Token is not yet valid (nbf claim)")]
    TokenNotYetValid,

    #[error("Invalid audience: expected {expected}, got {actual}")]
    InvalidAudience { expected: String, actual: String },

    #[error("Invalid issuer: expected {expected}, got {actual}")]
    InvalidIssuer { expected: String, actual: String },

    #[error("JSON serialization error: {0}")]
    SerializationError(#[from] serde_json::Error),

    #[error("Base64 decoding error: {0}")]
    Base64Error(#[from] base64::DecodeError),

    #[error("Time parsing error: {0}")]
    TimeError(#[from] time::error::ComponentRange),

    #[error("Cryptographic error: {0}")]
    CryptoError(String),

    #[error("Encryption error: {0}")]
    EncryptionError(String),

    #[error("Decryption error: {0}")]
    DecryptionError(String),

    #[error("Token parsing error: {0}")]
    TokenParsingError(String),
}

// Constants for token formatting
//
// IMPORTANT: These prefixes use a non-standard versioning scheme!
// The "pq1" here indicates "post-quantum era" tokens, NOT the classical
// algorithms defined in the official PASETO specification.
//
// This creates intentional incompatibility with standard PASETO tooling
// to prevent accidental mixing of classical and post-quantum tokens.

/// Token prefix for public (signature-based) post-quantum tokens
///
/// Uses `pq1` versioning to clearly distinguish from standard PASETO tokens.
/// Standard PASETO v1 uses RSA signatures, while this uses ML-DSA post-quantum signatures.
pub const TOKEN_PREFIX_PUBLIC: &str = "paseto.pq1.public";

/// Token prefix for local (symmetric encryption) post-quantum tokens
///
/// Uses `pq1` versioning to clearly distinguish from standard PASETO tokens.
/// Standard PASETO v1 uses HMAC, while this uses ChaCha20-Poly1305 with ML-KEM key exchange.
pub const TOKEN_PREFIX_LOCAL: &str = "paseto.pq1.local";

const MAX_TOKEN_SIZE: usize = 1024 * 1024; // 1MB max token size
const SYMMETRIC_KEY_SIZE: usize = 32;
const NONCE_SIZE: usize = 12;

impl KeyPair {
    /// Generate a new post-quantum key pair
    #[cfg_attr(feature = "logging", instrument(skip(rng)))]
    pub fn generate<R: CryptoRng + RngCore>(rng: &mut R) -> Self {
        let keypair = MlDsaParam::key_gen(rng);

        #[cfg(feature = "logging")]
        debug!("Generated new ML-DSA key pair");

        Self {
            signing_key: SigningKey(keypair.signing_key().clone()),
            verifying_key: VerifyingKey(keypair.verifying_key().clone()),
        }
    }

    /// Get a reference to the signing key
    pub fn signing_key(&self) -> &SigningKey {
        &self.signing_key
    }

    /// Get a reference to the verifying key
    pub fn verifying_key(&self) -> &VerifyingKey {
        &self.verifying_key
    }

    /// Export the signing key as bytes
    pub fn signing_key_to_bytes(&self) -> Vec<u8> {
        let encoded = self.signing_key.0.encode();
        encoded.to_vec()
    }

    /// Import signing key from bytes
    pub fn signing_key_from_bytes(bytes: &[u8]) -> Result<SigningKey, PqPasetoError> {
        let encoded = ml_dsa::EncodedSigningKey::<MlDsaParam>::try_from(bytes)
            .map_err(|e| PqPasetoError::CryptoError(format!("Invalid key bytes: {:?}", e)))?;
        let key = ml_dsa::SigningKey::<MlDsaParam>::decode(&encoded);
        Ok(SigningKey(key))
    }

    /// Export the verifying key as bytes
    pub fn verifying_key_to_bytes(&self) -> Vec<u8> {
        let encoded = self.verifying_key.0.encode();
        encoded.to_vec()
    }

    /// Import verifying key from bytes
    pub fn verifying_key_from_bytes(bytes: &[u8]) -> Result<VerifyingKey, PqPasetoError> {
        let encoded = ml_dsa::EncodedVerifyingKey::<MlDsaParam>::try_from(bytes)
            .map_err(|e| PqPasetoError::CryptoError(format!("Invalid key bytes: {:?}", e)))?;
        let key = ml_dsa::VerifyingKey::<MlDsaParam>::decode(&encoded);
        Ok(VerifyingKey(key))
    }
}

impl SymmetricKey {
    /// Generate a new random symmetric key
    #[cfg_attr(feature = "logging", instrument(skip(rng)))]
    pub fn generate<R: CryptoRng + RngCore>(rng: &mut R) -> Self {
        let mut key_bytes = [0u8; SYMMETRIC_KEY_SIZE];
        rng.fill_bytes(&mut key_bytes);

        #[cfg(feature = "logging")]
        debug!("Generated new symmetric key");

        Self(key_bytes)
    }

    /// Create symmetric key from bytes
    pub fn from_bytes(bytes: &[u8]) -> Result<Self, PqPasetoError> {
        if bytes.len() != SYMMETRIC_KEY_SIZE {
            return Err(PqPasetoError::CryptoError(format!(
                "Invalid symmetric key length: expected {}, got {}",
                SYMMETRIC_KEY_SIZE,
                bytes.len()
            )));
        }
        let mut key_bytes = [0u8; SYMMETRIC_KEY_SIZE];
        key_bytes.copy_from_slice(bytes);
        Ok(Self(key_bytes))
    }

    /// Export symmetric key as bytes
    pub fn to_bytes(&self) -> [u8; SYMMETRIC_KEY_SIZE] {
        self.0
    }

    /// Derive a symmetric key from shared secret using proper HKDF-SHA256
    ///
    /// Uses RFC 5869 HKDF with SHA-256 for cryptographically sound key derivation.
    /// The salt is set to None for domain separation, following best practices
    /// for post-quantum key exchange scenarios.
    pub fn derive_from_shared_secret(shared_secret: &[u8], info: &[u8]) -> Self {
        // Use proper HKDF with SHA-256 (no salt - appropriate for PQ key exchange)
        let hk = Hkdf::<Sha256>::new(None, shared_secret);

        let mut key_bytes = [0u8; SYMMETRIC_KEY_SIZE];
        hk.expand(info, &mut key_bytes)
            .expect("SYMMETRIC_KEY_SIZE (32) is valid for SHA-256 HKDF output");

        Self(key_bytes)
    }
}

impl KemKeyPair {
    /// Generate a new post-quantum KEM key pair using ML-KEM-768
    #[cfg_attr(feature = "logging", instrument(skip(_rng)))]
    pub fn generate<R: CryptoRng + RngCore>(_rng: &mut R) -> Self {
        // Generate actual ML-KEM-768 key pair
        let (dk, ek) = MlKem768::generate(&mut chacha20poly1305::aead::OsRng);

        #[cfg(feature = "logging")]
        debug!("Generated new ML-KEM-768 key pair");

        Self {
            encapsulation_key: EncapsulationKey(ek),
            decapsulation_key: DecapsulationKey(dk),
        }
    }

    /// Export the encapsulation key as bytes
    pub fn encapsulation_key_to_bytes(&self) -> Vec<u8> {
        use ml_kem::EncodedSizeUser;
        self.encapsulation_key.0.as_bytes().to_vec()
    }

    /// Import encapsulation key from bytes
    pub fn encapsulation_key_from_bytes(bytes: &[u8]) -> Result<EncapsulationKey, PqPasetoError> {
        use ml_kem::{EncodedSizeUser, array::Array};
        if bytes.len() != 1184 {
            return Err(PqPasetoError::CryptoError(
                "Invalid encapsulation key length".to_string(),
            ));
        }
        let array: Array<u8, _> = Array::try_from(bytes)
            .map_err(|_| PqPasetoError::CryptoError("Invalid key format".to_string()))?;
        Ok(EncapsulationKey(
            <MlKem768 as KemCore>::EncapsulationKey::from_bytes(&array),
        ))
    }

    /// Export the decapsulation key as bytes
    pub fn decapsulation_key_to_bytes(&self) -> Vec<u8> {
        use ml_kem::EncodedSizeUser;
        self.decapsulation_key.0.as_bytes().to_vec()
    }

    /// Import decapsulation key from bytes
    pub fn decapsulation_key_from_bytes(bytes: &[u8]) -> Result<DecapsulationKey, PqPasetoError> {
        use ml_kem::{EncodedSizeUser, array::Array};
        if bytes.len() != 2400 {
            return Err(PqPasetoError::CryptoError(
                "Invalid decapsulation key length".to_string(),
            ));
        }
        let array: Array<u8, _> = Array::try_from(bytes)
            .map_err(|_| PqPasetoError::CryptoError("Invalid key format".to_string()))?;
        Ok(DecapsulationKey(
            <MlKem768 as KemCore>::DecapsulationKey::from_bytes(&array),
        ))
    }

    /// Perform key encapsulation (sender side) using ML-KEM-768
    pub fn encapsulate(&self) -> (SymmetricKey, Vec<u8>) {
        // Use real ML-KEM-768 encapsulation with OsRng for compatibility
        let (ciphertext, shared_secret) = self
            .encapsulation_key
            .0
            .encapsulate(&mut chacha20poly1305::aead::OsRng)
            .unwrap();

        let symmetric_key = SymmetricKey::derive_from_shared_secret(
            shared_secret.as_slice(),
            b"PASETO-PQ-LOCAL-pq1",
        );

        (symmetric_key, ciphertext.as_slice().to_vec())
    }

    /// Perform key decapsulation (receiver side) using ML-KEM-768
    pub fn decapsulate(&self, ciphertext: &[u8]) -> Result<SymmetricKey, PqPasetoError> {
        use ml_kem::array::Array;

        // Parse ciphertext into the correct type
        if ciphertext.len() != 1088 {
            return Err(PqPasetoError::CryptoError(
                "Invalid ciphertext length".to_string(),
            ));
        }

        let ct_array: Array<u8, _> = Array::try_from(ciphertext)
            .map_err(|_| PqPasetoError::CryptoError("Invalid ciphertext format".to_string()))?;
        let ct = ml_kem::Ciphertext::<MlKem768>::from(ct_array);

        // Use real ML-KEM-768 decapsulation
        let shared_secret = self.decapsulation_key.0.decapsulate(&ct).unwrap();

        Ok(SymmetricKey::derive_from_shared_secret(
            shared_secret.as_ref(),
            b"PASETO-PQ-LOCAL-pq1",
        ))
    }
}

impl Claims {
    /// Create a new empty claims set
    pub fn new() -> Self {
        Self {
            iss: None,
            sub: None,
            aud: None,
            exp: None,
            nbf: None,
            iat: None,
            jti: None,
            kid: None,
            custom: HashMap::new(),
        }
    }

    /// Set the issuer claim
    pub fn set_issuer(&mut self, issuer: impl Into<String>) -> Result<(), PqPasetoError> {
        self.iss = Some(issuer.into());
        Ok(())
    }

    /// Set the subject claim
    pub fn set_subject(&mut self, subject: impl Into<String>) -> Result<(), PqPasetoError> {
        self.sub = Some(subject.into());
        Ok(())
    }

    /// Set the audience claim
    pub fn set_audience(&mut self, audience: impl Into<String>) -> Result<(), PqPasetoError> {
        self.aud = Some(audience.into());
        Ok(())
    }

    /// Set the expiration time
    pub fn set_expiration(&mut self, exp: OffsetDateTime) -> Result<(), PqPasetoError> {
        self.exp = Some(exp);
        Ok(())
    }

    /// Set the not-before time
    pub fn set_not_before(&mut self, nbf: OffsetDateTime) -> Result<(), PqPasetoError> {
        self.nbf = Some(nbf);
        Ok(())
    }

    /// Set the issued-at time
    pub fn set_issued_at(&mut self, iat: OffsetDateTime) -> Result<(), PqPasetoError> {
        self.iat = Some(iat);
        Ok(())
    }

    /// Set the token identifier
    pub fn set_jti(&mut self, jti: impl Into<String>) -> Result<(), PqPasetoError> {
        self.jti = Some(jti.into());
        Ok(())
    }

    /// Set the key identifier
    pub fn set_kid(&mut self, kid: impl Into<String>) -> Result<(), PqPasetoError> {
        self.kid = Some(kid.into());
        Ok(())
    }

    /// Add a custom claim
    pub fn add_custom(
        &mut self,
        key: impl Into<String>,
        value: impl Serialize,
    ) -> Result<(), PqPasetoError> {
        let value = serde_json::to_value(value)?;
        self.custom.insert(key.into(), value);
        Ok(())
    }

    /// Get a custom claim
    pub fn get_custom(&self, key: &str) -> Option<&Value> {
        self.custom.get(key)
    }

    /// Validate time-based claims
    pub fn validate_time(
        &self,
        now: OffsetDateTime,
        clock_skew_tolerance: time::Duration,
    ) -> Result<(), PqPasetoError> {
        // Check expiration
        if let Some(exp) = self.exp {
            if now > exp + clock_skew_tolerance {
                return Err(PqPasetoError::TokenExpired);
            }
        }

        // Check not-before
        if let Some(nbf) = self.nbf {
            if now < nbf - clock_skew_tolerance {
                return Err(PqPasetoError::TokenNotYetValid);
            }
        }

        Ok(())
    }

    // Getters
    pub fn issuer(&self) -> Option<&str> {
        self.iss.as_deref()
    }
    pub fn subject(&self) -> Option<&str> {
        self.sub.as_deref()
    }
    pub fn audience(&self) -> Option<&str> {
        self.aud.as_deref()
    }
    pub fn expiration(&self) -> Option<OffsetDateTime> {
        self.exp
    }
    pub fn not_before(&self) -> Option<OffsetDateTime> {
        self.nbf
    }
    pub fn issued_at(&self) -> Option<OffsetDateTime> {
        self.iat
    }
    pub fn jti(&self) -> Option<&str> {
        self.jti.as_deref()
    }
    pub fn kid(&self) -> Option<&str> {
        self.kid.as_deref()
    }

    /// Convert claims to a JSON value
    ///
    /// This method provides easy integration with logging, databases, and tracing systems.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use paseto_pq::Claims;
    /// use serde_json::Value;
    ///
    /// let mut claims = Claims::new();
    /// claims.set_subject("user123").unwrap();
    /// claims.add_custom("role", "admin").unwrap();
    ///
    /// let json_value: Value = claims.to_json_value();
    /// println!("Claims as JSON: {}", json_value);
    /// ```
    pub fn to_json_value(&self) -> serde_json::Value {
        serde_json::Value::from(self.clone())
    }

    /// Convert claims to a JSON string
    ///
    /// This method provides easy integration with logging, databases, and tracing systems.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use paseto_pq::Claims;
    ///
    /// let mut claims = Claims::new();
    /// claims.set_subject("user123").unwrap();
    /// claims.add_custom("role", "admin").unwrap();
    ///
    /// let json_string = claims.to_json_string().unwrap();
    /// println!("User claims: {}", json_string);
    /// ```
    pub fn to_json_string(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string(self)
    }

    /// Convert claims to a pretty-printed JSON string
    ///
    /// Useful for debugging and development environments.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use paseto_pq::Claims;
    ///
    /// let mut claims = Claims::new();
    /// claims.set_subject("user123").unwrap();
    /// claims.add_custom("role", "admin").unwrap();
    ///
    /// let pretty_json = claims.to_json_string_pretty().unwrap();
    /// println!("Claims:\n{}", pretty_json);
    /// ```
    pub fn to_json_string_pretty(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(self)
    }
}

impl Default for Claims {
    fn default() -> Self {
        Self::new()
    }
}

/// Convert Claims to serde_json::Value for easy integration with logging, databases, and tracing
///
/// # Example
///
/// ```rust,no_run
/// use paseto_pq::Claims;
/// use serde_json::Value;
///
/// let mut claims = Claims::new();
/// claims.set_subject("user123").unwrap();
/// claims.add_custom("tenant_id", "org_abc123").unwrap();
///
/// // Direct conversion
/// let json_value: Value = claims.into();
///
/// // Use in logging
/// println!("User authenticated with claims: {}", json_value);
///
/// // Use in database operations
/// // db.insert_audit_log(json_value).await?;
/// ```
impl From<Claims> for serde_json::Value {
    fn from(claims: Claims) -> Self {
        // Use serde to convert the Claims to JSON Value
        // This leverages the existing Serialize implementation on Claims
        serde_json::to_value(claims).unwrap_or(serde_json::Value::Null)
    }
}

/// Convert &Claims to serde_json::Value (borrowed version)
impl From<&Claims> for serde_json::Value {
    fn from(claims: &Claims) -> Self {
        serde_json::to_value(claims).unwrap_or(serde_json::Value::Null)
    }
}

impl TokenSizeBreakdown {
    /// Get the total size from all components
    pub fn total(&self) -> usize {
        self.prefix
            + self.payload
            + self.signature_or_tag
            + self.footer.unwrap_or(0)
            + self.separators
            + self.base64_overhead
    }
}

impl TokenSizeEstimator {
    /// Estimate the size of a public token
    ///
    /// # Arguments
    ///
    /// * `claims` - The claims that will be included in the token
    /// * `has_footer` - Whether the token will include a footer
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use paseto_pq::{Claims, TokenSizeEstimator};
    ///
    /// let mut claims = Claims::new();
    /// claims.set_subject("user123").unwrap();
    ///
    /// let estimator = TokenSizeEstimator::public(&claims, false);
    /// println!("Public token will be ~{} bytes", estimator.total_bytes());
    /// # Ok::<(), paseto_pq::PqPasetoError>(())
    /// ```
    pub fn public(claims: &Claims, has_footer: bool) -> Self {
        // Serialize claims to get actual payload size
        let claims_json = serde_json::to_string(claims).unwrap_or_default();
        let claims_bytes = claims_json.len();

        // Calculate base64 encoded payload size
        let payload_b64_len = claims_bytes.div_ceil(3) * 4; // Base64 encoding

        // Constants for public tokens
        let prefix_len = TOKEN_PREFIX_PUBLIC.len() + 1; // +1 for trailing dot
        // Signature size varies by parameter set
        let signature_len = if cfg!(feature = "ml-dsa-44") {
            2800 // ML-DSA-44 signature is smaller
        } else if cfg!(feature = "ml-dsa-65") {
            4300 // ML-DSA-65 signature is ~2,420 bytes -> ~3,227 base64 -> actual ~4.3KB
        } else {
            5000 // ML-DSA-87 signature is largest
        };
        let footer_len = if has_footer { 150 } else { 0 }; // Estimated footer size
        let separators = if has_footer { 3 } else { 2 }; // Dots between parts
        let base64_overhead = (claims_bytes * 4).div_ceil(3) - claims_bytes; // More accurate base64 overhead

        let breakdown = TokenSizeBreakdown {
            prefix: prefix_len,
            payload: payload_b64_len,
            signature_or_tag: signature_len,
            footer: if has_footer { Some(footer_len) } else { None },
            separators,
            base64_overhead,
        };

        Self { breakdown }
    }

    /// Estimate the size of a local token
    ///
    /// # Arguments
    ///
    /// * `claims` - The claims that will be included in the token
    /// * `has_footer` - Whether the token will include a footer
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use paseto_pq::{Claims, TokenSizeEstimator};
    ///
    /// let mut claims = Claims::new();
    /// claims.set_subject("user123").unwrap();
    ///
    /// let estimator = TokenSizeEstimator::local(&claims, false);
    /// println!("Local token will be ~{} bytes", estimator.total_bytes());
    /// # Ok::<(), paseto_pq::PqPasetoError>(())
    /// ```
    pub fn local(claims: &Claims, has_footer: bool) -> Self {
        // Serialize claims to get actual payload size
        let claims_json = serde_json::to_string(claims).unwrap_or_default();
        let claims_bytes = claims_json.len();

        // Local tokens encrypt the payload, add nonce (12 bytes) and auth tag (16 bytes)
        let encrypted_payload_len = claims_bytes + 12 + 16; // nonce + tag
        let payload_b64_len = encrypted_payload_len.div_ceil(3) * 4; // Base64 encoding

        // Constants for local tokens
        let prefix_len = TOKEN_PREFIX_LOCAL.len() + 1; // +1 for trailing dot
        let footer_len = if has_footer { 150 } else { 0 }; // Estimated footer size
        let separators = if has_footer { 2 } else { 1 }; // Dots between parts
        let base64_overhead = (encrypted_payload_len * 4).div_ceil(3) - encrypted_payload_len; // More accurate base64 overhead

        let breakdown = TokenSizeBreakdown {
            prefix: prefix_len,
            payload: payload_b64_len,
            signature_or_tag: 0, // Local tokens don't have separate signature
            footer: if has_footer { Some(footer_len) } else { None },
            separators,
            base64_overhead,
        };

        Self { breakdown }
    }

    /// Get the estimated total size in bytes
    pub fn total_bytes(&self) -> usize {
        self.breakdown.total()
    }

    /// Check if the token fits within typical cookie size limits (4KB)
    pub fn fits_in_cookie(&self) -> bool {
        self.total_bytes() <= 4096
    }

    /// Check if the token fits within typical URL length limits (2KB)
    pub fn fits_in_url(&self) -> bool {
        self.total_bytes() <= 2048
    }

    /// Check if the token fits within typical HTTP header limits (8KB)
    pub fn fits_in_header(&self) -> bool {
        self.total_bytes() <= 8192
    }

    /// Get detailed breakdown of size components
    pub fn breakdown(&self) -> &TokenSizeBreakdown {
        &self.breakdown
    }

    /// Get optimization suggestions if the token is large
    pub fn optimization_suggestions(&self) -> Vec<String> {
        let mut suggestions = Vec::new();
        let total = self.total_bytes();

        if total > 4096 {
            suggestions.push("Token exceeds cookie size limit (4KB)".to_string());
            suggestions.push("Consider using shorter claim values".to_string());
            suggestions.push("Move large data to footer or external storage".to_string());
            suggestions.push("Use local tokens for internal services (smaller)".to_string());
        }

        if total > 2048 {
            suggestions.push("Token exceeds URL length limits".to_string());
            suggestions.push("Avoid passing token in query parameters".to_string());
        }

        if self.breakdown.payload > total / 2 {
            suggestions.push("Payload is majority of token size - reduce claim data".to_string());
        }

        if self.breakdown.footer.unwrap_or(0) > 200 {
            suggestions.push("Footer is large - consider minimal metadata only".to_string());
        }

        suggestions
    }

    /// Compare token size to typical JWT tokens
    pub fn compare_to_jwt(&self) -> String {
        let jwt_typical = 200; // Typical JWT size
        let ratio = self.total_bytes() as f64 / jwt_typical as f64;
        format!(
            "{:.1}x larger than typical JWT ({} bytes)",
            ratio, jwt_typical
        )
    }

    /// Get a human-readable size summary
    pub fn size_summary(&self) -> String {
        format!(
            "Token size: {} bytes (payload: {}, signature: {}, overhead: {})",
            self.total_bytes(),
            self.breakdown.payload,
            self.breakdown.signature_or_tag,
            self.breakdown.base64_overhead + self.breakdown.separators + self.breakdown.prefix
        )
    }
}

impl ParsedToken {
    /// Parse a PASETO token string to extract structural information
    ///
    /// This method performs **no cryptographic operations** - it only parses the token
    /// structure to extract metadata. Use this for debugging, logging, middleware,
    /// and routing decisions.
    ///
    /// # Arguments
    ///
    /// * `token` - The PASETO token string to parse
    ///
    /// # Returns
    ///
    /// Returns a `ParsedToken` containing the token's structural information,
    /// or an error if the token format is invalid.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use paseto_pq::ParsedToken;
    ///
    /// let token = "paseto.pq1.public.ABC123.DEF456.eyJraWQiOiJ0ZXN0In0";
    /// let parsed = ParsedToken::parse(token)?;
    ///
    /// assert_eq!(parsed.purpose(), "public");
    /// assert_eq!(parsed.version(), "pq1");
    /// assert!(parsed.has_footer());
    /// # Ok::<(), paseto_pq::PqPasetoError>(())
    /// ```
    pub fn parse(token: &str) -> Result<Self, PqPasetoError> {
        let parts: Vec<&str> = token.split('.').collect();

        // Validate minimum structure: paseto.pq1.purpose.payload
        if parts.len() < 4 {
            return Err(PqPasetoError::TokenParsingError(format!(
                "Invalid token format: expected at least 4 parts, got {}",
                parts.len()
            )));
        }

        // Validate protocol
        if parts[0] != "paseto" {
            return Err(PqPasetoError::TokenParsingError(format!(
                "Invalid protocol: expected 'paseto', got '{}'",
                parts[0]
            )));
        }

        // Extract version
        let version = parts[1].to_string();

        // Extract purpose
        let purpose = parts[2].to_string();

        // Validate known formats
        match (version.as_str(), purpose.as_str()) {
            ("pq1", "public") | ("pq1", "local") => {}
            _ => {
                return Err(PqPasetoError::TokenParsingError(format!(
                    "Unsupported token format: {}.{}.{}",
                    parts[0], parts[1], parts[2]
                )));
            }
        }

        // Decode payload
        let payload = URL_SAFE_NO_PAD.decode(parts[3]).map_err(|e| {
            PqPasetoError::TokenParsingError(format!("Invalid payload base64: {}", e))
        })?;

        let mut signature_or_tag = None;
        let mut footer = None;

        // Parse remaining parts based on token type
        match purpose.as_str() {
            "public" => {
                // Public tokens: paseto.pq1.public.payload.signature[.footer]
                if parts.len() > 6 {
                    return Err(PqPasetoError::TokenParsingError(
                        "Public token has too many parts".to_string(),
                    ));
                }
                if parts.len() >= 5 {
                    signature_or_tag = Some(URL_SAFE_NO_PAD.decode(parts[4]).map_err(|e| {
                        PqPasetoError::TokenParsingError(format!("Invalid signature base64: {}", e))
                    })?);
                }
                if parts.len() >= 6 {
                    footer = Some(Footer::from_base64(parts[5])?);
                }
            }
            "local" => {
                // Local tokens: paseto.pq1.local.payload[.footer]
                if parts.len() > 5 {
                    return Err(PqPasetoError::TokenParsingError(
                        "Local token has too many parts".to_string(),
                    ));
                }
                if parts.len() >= 5 {
                    footer = Some(Footer::from_base64(parts[4])?);
                }
            }
            _ => unreachable!(), // Already validated above
        }

        Ok(ParsedToken {
            purpose,
            version,
            payload,
            signature_or_tag,
            footer,
            raw_token: token.to_string(),
        })
    }

    /// Get the token purpose ("public" for public tokens, "local" for local tokens)
    pub fn purpose(&self) -> &str {
        &self.purpose
    }

    /// Get the token version (currently "pq1")
    pub fn version(&self) -> &str {
        &self.version
    }

    /// Check if the token has a footer
    pub fn has_footer(&self) -> bool {
        self.footer.is_some()
    }

    /// Get the footer, if present
    pub fn footer(&self) -> Option<&Footer> {
        self.footer.as_ref()
    }

    /// Get the raw payload bytes (base64-decoded)
    pub fn payload_bytes(&self) -> &[u8] {
        &self.payload
    }

    /// Get the signature or authentication tag bytes, if present
    ///
    /// For public tokens, this is the ML-DSA signature.
    /// For local tokens, this is None (auth tag is embedded in payload).
    pub fn signature_bytes(&self) -> Option<&[u8]> {
        self.signature_or_tag.as_deref()
    }

    /// Get the length of the payload in bytes
    pub fn payload_length(&self) -> usize {
        self.payload.len()
    }

    /// Get the total length of the token string
    pub fn total_length(&self) -> usize {
        self.raw_token.len()
    }

    /// Get the raw token string
    pub fn raw_token(&self) -> &str {
        &self.raw_token
    }

    /// Get footer as JSON string, if present
    pub fn footer_json(&self) -> Option<Result<String, serde_json::Error>> {
        self.footer.as_ref().map(serde_json::to_string)
    }

    /// Get footer as pretty-printed JSON string, if present
    pub fn footer_json_pretty(&self) -> Option<Result<String, serde_json::Error>> {
        self.footer.as_ref().map(serde_json::to_string_pretty)
    }

    /// Check if this is a public token (uses signatures)
    pub fn is_public(&self) -> bool {
        self.purpose == "public"
    }

    /// Check if this is a local token (uses symmetric encryption)
    pub fn is_local(&self) -> bool {
        self.purpose == "local"
    }

    /// Get token format summary for debugging
    pub fn format_summary(&self) -> String {
        format!(
            "paseto.{}.{} (payload: {} bytes, signature: {}, footer: {})",
            self.version,
            self.purpose,
            self.payload.len(),
            if self.signature_or_tag.is_some() {
                "present"
            } else {
                "none"
            },
            if self.footer.is_some() {
                "present"
            } else {
                "none"
            }
        )
    }
}

impl VerifiedToken {
    /// Get the claims from the verified token
    pub fn claims(&self) -> &Claims {
        &self.claims
    }

    /// Get the footer from the verified token, if present
    pub fn footer(&self) -> Option<&Footer> {
        self.footer.as_ref()
    }

    /// Get the raw token string
    pub fn raw_token(&self) -> &str {
        &self.raw_token
    }

    /// Consume the verified token and return the claims
    pub fn into_claims(self) -> Claims {
        self.claims
    }

    /// Consume the verified token and return both claims and footer
    pub fn into_parts(self) -> (Claims, Option<Footer>) {
        (self.claims, self.footer)
    }
}

impl PasetoPQ {
    /// Get the current token prefix used for public tokens
    ///
    /// Returns the prefix string used in public token generation.
    /// This allows applications to inspect the versioning scheme being used.
    pub fn public_token_prefix() -> &'static str {
        TOKEN_PREFIX_PUBLIC
    }

    /// Get the current token prefix used for local tokens
    ///
    /// Returns the prefix string used in local token generation.
    /// This allows applications to inspect the versioning scheme being used.
    pub fn local_token_prefix() -> &'static str {
        TOKEN_PREFIX_LOCAL
    }

    /// Check if this implementation uses standard PASETO versioning
    ///
    /// Returns `false` because this crate uses non-standard `pq1` versioning
    /// that is incompatible with the official PASETO specification.
    pub fn is_standard_paseto_compatible() -> bool {
        false
    }
    /// Parse a PASETO token for inspection without cryptographic operations
    ///
    /// This method allows you to examine token structure, purpose, version, and footer
    /// without performing expensive signature verification or decryption. Useful for
    /// debugging, logging, middleware, and routing decisions.
    ///
    /// # Arguments
    ///
    /// * `token` - The PASETO token string to parse
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use paseto_pq::PasetoPQ;
    ///
    /// let token = "paseto.pq1.public.ABC123...";
    /// let parsed = PasetoPQ::parse_token(token)?;
    ///
    /// println!("Token type: {}", parsed.purpose());
    /// println!("Has footer: {}", parsed.has_footer());
    ///
    /// // Route based on token type
    /// match parsed.purpose() {
    ///     "public" => println!("Public token - needs signature verification"),
    ///     "local" => println!("Local token - needs decryption"),
    ///     _ => println!("Unknown token type"),
    /// }
    /// # Ok::<(), paseto_pq::PqPasetoError>(())
    /// ```
    pub fn parse_token(token: &str) -> Result<ParsedToken, PqPasetoError> {
        ParsedToken::parse(token)
    }

    /// Estimate the size of a public token before creation
    ///
    /// This method helps you plan token usage and avoid size-related issues
    /// with HTTP headers, cookies, or URL length limits.
    ///
    /// # Arguments
    ///
    /// * `claims` - The claims that will be included in the token
    /// * `has_footer` - Whether the token will include a footer
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use paseto_pq::{PasetoPQ, Claims};
    ///
    /// let mut claims = Claims::new();
    /// claims.set_subject("user123").unwrap();
    /// claims.add_custom("role", "admin").unwrap();
    ///
    /// let estimator = PasetoPQ::estimate_public_size(&claims, false);
    /// println!("Token will be ~{} bytes", estimator.total_bytes());
    ///
    /// if !estimator.fits_in_cookie() {
    ///     println!("Warning: Token too large for cookies!");
    /// }
    /// # Ok::<(), paseto_pq::PqPasetoError>(())
    /// ```
    pub fn estimate_public_size(claims: &Claims, has_footer: bool) -> TokenSizeEstimator {
        TokenSizeEstimator::public(claims, has_footer)
    }

    /// Estimate the size of a local token before creation
    ///
    /// This method helps you plan token usage and avoid size-related issues
    /// with HTTP headers, cookies, or URL length limits.
    ///
    /// # Arguments
    ///
    /// * `claims` - The claims that will be included in the token
    /// * `has_footer` - Whether the token will include a footer
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use paseto_pq::{PasetoPQ, Claims};
    ///
    /// let mut claims = Claims::new();
    /// claims.set_subject("user123").unwrap();
    /// claims.add_custom("session_data", "confidential").unwrap();
    ///
    /// let estimator = PasetoPQ::estimate_local_size(&claims, true);
    /// println!("Token will be ~{} bytes", estimator.total_bytes());
    ///
    /// if estimator.fits_in_header() {
    ///     println!("Token fits in HTTP headers");
    /// }
    /// # Ok::<(), paseto_pq::PqPasetoError>(())
    /// ```
    pub fn estimate_local_size(claims: &Claims, has_footer: bool) -> TokenSizeEstimator {
        TokenSizeEstimator::local(claims, has_footer)
    }

    /// Sign claims to create a public token
    #[cfg_attr(feature = "logging", instrument(skip(signing_key)))]
    pub fn sign(signing_key: &SigningKey, claims: &Claims) -> Result<String, PqPasetoError> {
        Self::sign_with_footer(signing_key, claims, None)
    }

    /// Sign claims with optional footer to create a new public token
    #[cfg_attr(feature = "logging", instrument(skip(signing_key)))]
    pub fn sign_with_footer(
        signing_key: &SigningKey,
        claims: &Claims,
        footer: Option<&Footer>,
    ) -> Result<String, PqPasetoError> {
        // Serialize claims to JSON bytes (not base64 for PAE)
        let payload_bytes = serde_json::to_vec(claims)?;

        #[cfg(feature = "logging")]
        debug!("Serialized claims to {} bytes", payload_bytes.len());

        // Serialize footer to JSON bytes (empty if None)
        let footer_bytes = match footer {
            Some(f) => serde_json::to_vec(f)?,
            None => Vec::new(), // Empty bytes for no footer
        };

        // Create PAE message for signing (RFC Section 2.2.1)
        // PAE([header, payload_bytes, footer_bytes])
        let header = TOKEN_PREFIX_PUBLIC.as_bytes();
        let pae_message =
            crate::pae::pae_encode_public_token(header, &payload_bytes, &footer_bytes);

        #[cfg(feature = "logging")]
        debug!(
            "Created PAE message of {} bytes for signing",
            pae_message.len()
        );

        // Sign the PAE-encoded message with ML-DSA
        let signature = signing_key.0.sign(&pae_message);
        let signature_bytes = signature.to_bytes();

        // Base64url encode components for token construction
        let encoded_payload = URL_SAFE_NO_PAD.encode(&payload_bytes);
        let encoded_signature = URL_SAFE_NO_PAD.encode(signature_bytes);

        // Construct final token with optional footer
        let token = match footer {
            Some(f) => {
                let footer_b64 = f.to_base64()?;
                format!(
                    "{}.{}.{}.{}",
                    TOKEN_PREFIX_PUBLIC, encoded_payload, encoded_signature, footer_b64
                )
            }
            None => format!(
                "{}.{}.{}",
                TOKEN_PREFIX_PUBLIC, encoded_payload, encoded_signature
            ),
        };

        #[cfg(feature = "logging")]
        debug!(
            "Generated v0.1.1 token with {} byte signature and PAE footer authentication{}",
            signature_bytes.len(),
            if footer.is_some() { " with footer" } else { "" }
        );

        Ok(token)
    }

    /// Encrypt claims to create a new local token
    #[cfg_attr(feature = "logging", instrument(skip(symmetric_key)))]
    pub fn encrypt(symmetric_key: &SymmetricKey, claims: &Claims) -> Result<String, PqPasetoError> {
        Self::encrypt_with_footer(symmetric_key, claims, None)
    }

    /// Encrypt claims with optional footer to create a new local token
    #[cfg_attr(feature = "logging", instrument(skip(symmetric_key)))]
    pub fn encrypt_with_footer(
        symmetric_key: &SymmetricKey,
        claims: &Claims,
        footer: Option<&Footer>,
    ) -> Result<String, PqPasetoError> {
        // Serialize claims to JSON bytes
        let payload_bytes = serde_json::to_vec(claims)?;

        #[cfg(feature = "logging")]
        debug!("Serialized claims to {} bytes", payload_bytes.len());

        // Create cipher
        let cipher = ChaCha20Poly1305::new((&symmetric_key.0).into());

        // Generate random nonce
        let nonce = ChaCha20Poly1305::generate_nonce(&mut AeadOsRng);

        // Serialize footer to JSON bytes (empty if None)
        let footer_bytes = match footer {
            Some(f) => serde_json::to_vec(f)?,
            None => Vec::new(), // Empty bytes for no footer
        };

        // Create PAE-encoded AAD for footer authentication (RFC Section 2.2.1)
        // PAE([header, nonce_bytes, footer_bytes])
        let header = TOKEN_PREFIX_LOCAL.as_bytes();
        let nonce_bytes = nonce.as_slice();
        let aad = crate::pae::pae_encode_local_token(header, nonce_bytes, &footer_bytes);

        #[cfg(feature = "logging")]
        debug!(
            "Created PAE AAD of {} bytes for footer authentication",
            aad.len()
        );

        // Encrypt payload with PAE AAD (footer now authenticated by AEAD!)
        let mut buffer = payload_bytes.clone();
        let tag = cipher
            .encrypt_in_place_detached(&nonce, &aad, &mut buffer)
            .map_err(|e| PqPasetoError::EncryptionError(format!("Encryption failed: {}", e)))?;

        // Combine encrypted payload with authentication tag
        let mut ciphertext = buffer;
        ciphertext.extend_from_slice(&tag);

        // Combine nonce + ciphertext + tag
        let mut encrypted_data = Vec::new();
        encrypted_data.extend_from_slice(&nonce);
        encrypted_data.extend_from_slice(&ciphertext);

        // Base64url encode the encrypted data
        let encoded_payload = URL_SAFE_NO_PAD.encode(&encrypted_data);

        // Construct final token with optional footer
        let token = match footer {
            Some(f) => {
                let footer_b64 = f.to_base64()?;
                format!("{}.{}.{}", TOKEN_PREFIX_LOCAL, encoded_payload, footer_b64)
            }
            None => format!("{}.{}", TOKEN_PREFIX_LOCAL, encoded_payload),
        };

        #[cfg(feature = "logging")]
        debug!(
            "Generated v0.1.1 local token with {} byte payload and PAE footer authentication{}",
            encrypted_data.len(),
            if footer.is_some() { " with footer" } else { "" }
        );

        Ok(token)
    }

    /// Decrypt a local token and extract claims
    #[cfg_attr(feature = "logging", instrument(skip(symmetric_key)))]
    pub fn decrypt(
        symmetric_key: &SymmetricKey,
        token: &str,
    ) -> Result<VerifiedToken, PqPasetoError> {
        Self::decrypt_with_footer(symmetric_key, token)
    }

    /// Decrypt a local token with footer support and extract claims
    #[cfg_attr(feature = "logging", instrument(skip(symmetric_key)))]
    pub fn decrypt_with_footer(
        symmetric_key: &SymmetricKey,
        token: &str,
    ) -> Result<VerifiedToken, PqPasetoError> {
        // Basic size check
        if token.len() > MAX_TOKEN_SIZE {
            return Err(PqPasetoError::InvalidFormat("Token too large".into()));
        }

        // Split token into parts (4 parts without footer, 5 parts with footer)
        let parts: Vec<&str> = token.splitn(5, '.').collect();
        let (encoded_payload, footer) = if parts.len() == 5 {
            // Token with footer: paseto.pq1.local.payload.footer
            if parts[0] != "paseto" || parts[1] != "pq1" || parts[2] != "local" {
                return Err(PqPasetoError::InvalidFormat(
                    "Invalid token format - expected 'paseto.pq1.local'".into(),
                ));
            }
            let footer = Footer::from_base64(parts[4])?;
            (parts[3], Some(footer))
        } else if parts.len() == 4 {
            // Token without footer: paseto.pq1.local.payload
            if parts[0] != "paseto" || parts[1] != "pq1" || parts[2] != "local" {
                return Err(PqPasetoError::InvalidFormat(
                    "Invalid token format - expected 'paseto.pq1.local'".into(),
                ));
            }
            (parts[3], None)
        } else {
            return Err(PqPasetoError::InvalidFormat(
                "Expected 4 or 5 parts separated by '.' for local token".into(),
            ));
        };

        // Decode encrypted payload
        let encrypted_data = URL_SAFE_NO_PAD.decode(encoded_payload).map_err(|e| {
            PqPasetoError::InvalidFormat(format!("Invalid payload encoding: {}", e))
        })?;

        // Split nonce, ciphertext, and tag
        if encrypted_data.len() < NONCE_SIZE + 16 {
            return Err(PqPasetoError::DecryptionError(
                "Encrypted data too short for nonce and tag".into(),
            ));
        }

        let nonce = Nonce::from_slice(&encrypted_data[..NONCE_SIZE]);
        let ciphertext_with_tag = &encrypted_data[NONCE_SIZE..];

        // Split ciphertext and authentication tag (last 16 bytes)
        if ciphertext_with_tag.len() < 16 {
            return Err(PqPasetoError::DecryptionError(
                "Encrypted data too short for authentication tag".into(),
            ));
        }

        let tag_start = ciphertext_with_tag.len() - 16;
        let mut ciphertext = ciphertext_with_tag[..tag_start].to_vec();
        let tag = &ciphertext_with_tag[tag_start..];

        // Serialize footer to JSON bytes (empty if None)
        let footer_bytes = match &footer {
            Some(f) => serde_json::to_vec(f)?,
            None => Vec::new(), // Empty bytes for no footer
        };

        // Reconstruct PAE-encoded AAD for footer validation (v0.1.1 RFC-compliant)
        // PAE([header, nonce_bytes, footer_bytes])
        let header = TOKEN_PREFIX_LOCAL.as_bytes();
        let nonce_bytes = nonce.as_slice();
        let aad = crate::pae::pae_encode_local_token(header, nonce_bytes, &footer_bytes);

        #[cfg(feature = "logging")]
        debug!(
            "Reconstructed PAE AAD of {} bytes for footer validation",
            aad.len()
        );

        // Create cipher and decrypt with PAE AAD (footer tampering now detected!)
        let cipher = ChaCha20Poly1305::new((&symmetric_key.0).into());

        // Convert tag slice to GenericArray
        use chacha20poly1305::aead::generic_array::GenericArray;
        let tag_array = GenericArray::from_slice(tag);

        let payload_bytes = cipher
            .decrypt_in_place_detached(nonce, &aad, &mut ciphertext, tag_array)
            .map_err(|e| {
                PqPasetoError::DecryptionError(format!(
                    "Decryption failed (footer authentication failed): {}",
                    e
                ))
            })
            .map(|_| ciphertext)?;

        #[cfg(feature = "logging")]
        debug!("v0.1.1 PAE decryption successful with footer authentication");

        // Parse claims
        let claims: Claims = serde_json::from_slice(&payload_bytes)?;

        // Basic time validation (with default 30s clock skew tolerance)
        claims.validate_time(OffsetDateTime::now_utc(), time::Duration::seconds(30))?;

        Ok(VerifiedToken {
            claims,
            footer,
            raw_token: token.to_string(),
        })
    }

    /// Decrypt a local token with custom validation options
    pub fn decrypt_with_options(
        symmetric_key: &SymmetricKey,
        token: &str,
        expected_audience: Option<&str>,
        expected_issuer: Option<&str>,
        clock_skew_tolerance: time::Duration,
    ) -> Result<VerifiedToken, PqPasetoError> {
        let verified = Self::decrypt(symmetric_key, token)?;

        // Validate audience if specified
        if let Some(expected_aud) = expected_audience {
            match verified.claims.audience() {
                Some(actual_aud) if actual_aud == expected_aud => {}
                Some(actual_aud) => {
                    return Err(PqPasetoError::InvalidAudience {
                        expected: expected_aud.to_string(),
                        actual: actual_aud.to_string(),
                    });
                }
                None => {
                    return Err(PqPasetoError::InvalidAudience {
                        expected: expected_aud.to_string(),
                        actual: "none".to_string(),
                    });
                }
            }
        }

        // Validate issuer if specified
        if let Some(expected_iss) = expected_issuer {
            match verified.claims.issuer() {
                Some(actual_iss) if actual_iss == expected_iss => {}
                Some(actual_iss) => {
                    return Err(PqPasetoError::InvalidIssuer {
                        expected: expected_iss.to_string(),
                        actual: actual_iss.to_string(),
                    });
                }
                None => {
                    return Err(PqPasetoError::InvalidIssuer {
                        expected: expected_iss.to_string(),
                        actual: "none".to_string(),
                    });
                }
            }
        }

        // Re-validate time with custom tolerance
        verified
            .claims
            .validate_time(OffsetDateTime::now_utc(), clock_skew_tolerance)?;

        Ok(verified)
    }

    /// Verify a token and extract claims
    #[cfg_attr(feature = "logging", instrument(skip(verifying_key)))]
    pub fn verify(
        verifying_key: &VerifyingKey,
        token: &str,
    ) -> Result<VerifiedToken, PqPasetoError> {
        Self::verify_with_footer(verifying_key, token)
    }

    /// Verify a token with footer support and extract claims
    #[cfg_attr(feature = "logging", instrument(skip(verifying_key)))]
    pub fn verify_with_footer(
        verifying_key: &VerifyingKey,
        token: &str,
    ) -> Result<VerifiedToken, PqPasetoError> {
        // Basic size check
        if token.len() > MAX_TOKEN_SIZE {
            return Err(PqPasetoError::InvalidFormat("Token too large".into()));
        }

        // Split token into parts (5 parts without footer, 6 parts with footer)
        let parts: Vec<&str> = token.splitn(6, '.').collect();
        let (encoded_payload, encoded_signature, footer) = if parts.len() == 6 {
            // Token with footer: paseto.pq1.public.payload.signature.footer
            if parts[0] != "paseto" || parts[1] != "pq1" || parts[2] != "public" {
                return Err(PqPasetoError::InvalidFormat(
                    "Invalid token format - expected 'paseto.pq1.public'".into(),
                ));
            }
            let footer = Footer::from_base64(parts[5])?;
            (parts[3], parts[4], Some(footer))
        } else if parts.len() == 5 {
            // Token without footer: paseto.pq1.public.payload.signature
            if parts[0] != "paseto" || parts[1] != "pq1" || parts[2] != "public" {
                return Err(PqPasetoError::InvalidFormat(
                    "Invalid token format - expected 'paseto.pq1.public'".into(),
                ));
            }
            (parts[3], parts[4], None)
        } else {
            return Err(PqPasetoError::InvalidFormat(
                "Expected 5 or 6 parts separated by '.' for public token".into(),
            ));
        };

        // Decode payload bytes
        let payload_bytes = URL_SAFE_NO_PAD.decode(encoded_payload).map_err(|e| {
            PqPasetoError::InvalidFormat(format!("Invalid payload encoding: {}", e))
        })?;

        // Serialize footer to bytes for PAE (empty if None)
        let footer_bytes = match &footer {
            Some(f) => serde_json::to_vec(f)?,
            None => Vec::new(), // Empty bytes for no footer
        };

        // Reconstruct PAE message that was signed (v0.1.1 RFC-compliant)
        // PAE([header, payload_bytes, footer_bytes])
        let header = TOKEN_PREFIX_PUBLIC.as_bytes();
        let pae_message =
            crate::pae::pae_encode_public_token(header, &payload_bytes, &footer_bytes);

        #[cfg(feature = "logging")]
        debug!(
            "Reconstructed PAE message of {} bytes for verification",
            pae_message.len()
        );

        // Decode signature
        let signature_bytes = URL_SAFE_NO_PAD.decode(encoded_signature).map_err(|e| {
            PqPasetoError::InvalidFormat(format!("Invalid signature encoding: {}", e))
        })?;

        // Reconstruct signature
        let encoded_sig = ml_dsa::EncodedSignature::<MlDsaParam>::try_from(
            signature_bytes.as_slice(),
        )
        .map_err(|e| PqPasetoError::CryptoError(format!("Invalid signature bytes: {:?}", e)))?;
        let signature = ml_dsa::Signature::<MlDsaParam>::decode(&encoded_sig)
            .ok_or_else(|| PqPasetoError::CryptoError("Failed to decode signature".into()))?;

        // Verify signature against PAE message (footer tampering now detected!)
        verifying_key
            .0
            .verify(&pae_message, &signature)
            .map_err(|_| PqPasetoError::SignatureVerificationFailed)?;

        #[cfg(feature = "logging")]
        debug!("v0.1.1 PAE signature verification successful with footer authentication");

        // Parse claims
        let claims: Claims = serde_json::from_slice(&payload_bytes)?;

        // Basic time validation (with default 30s clock skew tolerance)
        claims.validate_time(OffsetDateTime::now_utc(), time::Duration::seconds(30))?;

        Ok(VerifiedToken {
            claims,
            footer,
            raw_token: token.to_string(),
        })
    }

    /// Verify a token with custom validation options
    pub fn verify_with_options(
        verifying_key: &VerifyingKey,
        token: &str,
        expected_audience: Option<&str>,
        expected_issuer: Option<&str>,
        clock_skew_tolerance: time::Duration,
    ) -> Result<VerifiedToken, PqPasetoError> {
        let verified = Self::verify_with_footer(verifying_key, token)?;

        // Validate audience if specified
        if let Some(expected_aud) = expected_audience {
            match verified.claims.audience() {
                Some(actual_aud) if actual_aud == expected_aud => {}
                Some(actual_aud) => {
                    return Err(PqPasetoError::InvalidAudience {
                        expected: expected_aud.to_string(),
                        actual: actual_aud.to_string(),
                    });
                }
                None => {
                    return Err(PqPasetoError::InvalidAudience {
                        expected: expected_aud.to_string(),
                        actual: "none".to_string(),
                    });
                }
            }
        }

        // Validate issuer if specified
        if let Some(expected_iss) = expected_issuer {
            match verified.claims.issuer() {
                Some(actual_iss) if actual_iss == expected_iss => {}
                Some(actual_iss) => {
                    return Err(PqPasetoError::InvalidIssuer {
                        expected: expected_iss.to_string(),
                        actual: actual_iss.to_string(),
                    });
                }
                None => {
                    return Err(PqPasetoError::InvalidIssuer {
                        expected: expected_iss.to_string(),
                        actual: "none".to_string(),
                    });
                }
            }
        }

        // Re-validate time with custom tolerance
        verified
            .claims
            .validate_time(OffsetDateTime::now_utc(), clock_skew_tolerance)?;

        Ok(verified)
    }
}

impl fmt::Debug for SigningKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SigningKey")
            .field("algorithm", &"ML-DSA-65")
            .finish_non_exhaustive()
    }
}

impl fmt::Debug for VerifyingKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("VerifyingKey")
            .field("algorithm", &"ML-DSA-65")
            .finish_non_exhaustive()
    }
}

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

impl fmt::Debug for SymmetricKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SymmetricKey")
            .field("algorithm", &"ChaCha20-Poly1305")
            .finish_non_exhaustive()
    }
}

impl fmt::Debug for EncapsulationKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("EncapsulationKey")
            .field("algorithm", &"ML-KEM-768")
            .finish_non_exhaustive()
    }
}

impl fmt::Debug for DecapsulationKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("DecapsulationKey")
            .field("algorithm", &"ML-KEM-768")
            .finish_non_exhaustive()
    }
}

impl fmt::Debug for KemKeyPair {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("KemKeyPair")
            .field("encapsulation_key", &"<encapsulation_key>")
            .field("decapsulation_key", &"<decapsulation_key>")
            .finish()
    }
}

// Zeroization implementations for sensitive key material
// Note: ML-DSA and ML-KEM keys are opaque types that may handle their own zeroization internally.
// We implement Drop for best-effort cleanup, but rely on the underlying libraries for complete zeroization.

impl Drop for SigningKey {
    fn drop(&mut self) {
        // ML-DSA SigningKey is opaque - rely on underlying library for zeroization
        // The ml-dsa crate is compiled with zeroize feature enabled
    }
}

impl Drop for VerifyingKey {
    fn drop(&mut self) {
        // ML-DSA VerifyingKey is opaque - rely on underlying library for zeroization
        // The ml-dsa crate is compiled with zeroize feature enabled
    }
}

impl Drop for KeyPair {
    fn drop(&mut self) {
        // Drop implementations for individual keys will handle cleanup
    }
}

impl Drop for EncapsulationKey {
    fn drop(&mut self) {
        // ML-KEM EncapsulationKey is opaque - rely on underlying library for zeroization
        // The ml-kem crate is compiled with zeroize feature enabled
    }
}

impl Drop for DecapsulationKey {
    fn drop(&mut self) {
        // ML-KEM DecapsulationKey is opaque - rely on underlying library for zeroization
        // The ml-kem crate is compiled with zeroize feature enabled
    }
}

impl Drop for KemKeyPair {
    fn drop(&mut self) {
        // Drop implementations for individual keys will handle cleanup
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rand::rng;
    use std::thread;
    use time::Duration;

    #[test]
    fn test_keypair_generation() {
        thread::Builder::new()
            .name("keypair-generation-smoke".to_string())
            .stack_size(16 * 1024 * 1024)
            .spawn(|| {
                let mut rng = rng();
                let keypair = KeyPair::generate(&mut rng);

                // Test bytes export/import
                let signing_bytes = keypair.signing_key_to_bytes();
                let verifying_bytes = keypair.verifying_key_to_bytes();

                assert!(!signing_bytes.is_empty());
                assert!(!verifying_bytes.is_empty());

                let imported_signing = KeyPair::signing_key_from_bytes(&signing_bytes).unwrap();
                let imported_verifying =
                    KeyPair::verifying_key_from_bytes(&verifying_bytes).unwrap();

                // Keys should be functionally equivalent (test by signing/verifying)
                let mut claims = Claims::new();
                claims.set_subject("test").unwrap();

                let token1 = PasetoPQ::sign(keypair.signing_key(), &claims).unwrap();
                let token2 = PasetoPQ::sign(&imported_signing, &claims).unwrap();

                // Both should verify with either key
                PasetoPQ::verify(keypair.verifying_key(), &token1).unwrap();
                PasetoPQ::verify(&imported_verifying, &token1).unwrap();
                PasetoPQ::verify(keypair.verifying_key(), &token2).unwrap();
                PasetoPQ::verify(&imported_verifying, &token2).unwrap();
            })
            .unwrap()
            .join()
            .unwrap();
    }

    #[test]
    fn test_basic_sign_and_verify() {
        let mut rng = rng();
        let keypair = KeyPair::generate(&mut rng);

        let mut claims = Claims::new();
        claims.set_subject("user123").unwrap();
        claims.set_issuer("conflux-auth").unwrap();
        claims.set_audience("conflux-network").unwrap();
        claims.set_jti("token-id-123").unwrap();
        claims.add_custom("tenant_id", "org_abc123").unwrap();
        claims.add_custom("roles", ["user", "admin"]).unwrap();

        let token = PasetoPQ::sign(keypair.signing_key(), &claims).unwrap();
        assert!(token.starts_with("paseto.pq1.public."));

        let verified = PasetoPQ::verify(keypair.verifying_key(), &token).unwrap();
        let verified_claims = verified.claims();

        assert_eq!(verified_claims.subject(), Some("user123"));
        assert_eq!(verified_claims.issuer(), Some("conflux-auth"));
        assert_eq!(verified_claims.audience(), Some("conflux-network"));
        assert_eq!(verified_claims.jti(), Some("token-id-123"));

        // Check custom claims
        assert_eq!(
            verified_claims.get_custom("tenant_id").unwrap().as_str(),
            Some("org_abc123")
        );
        let roles: Vec<String> =
            serde_json::from_value(verified_claims.get_custom("roles").unwrap().clone()).unwrap();
        assert_eq!(roles, vec!["user", "admin"]);
    }

    #[test]
    fn test_time_validation() {
        let mut rng = rng();
        let keypair = KeyPair::generate(&mut rng);

        let now = OffsetDateTime::now_utc();

        // Test expired token
        let mut expired_claims = Claims::new();
        expired_claims.set_subject("user").unwrap();
        expired_claims
            .set_expiration(now - Duration::hours(1))
            .unwrap();

        let expired_token = PasetoPQ::sign(keypair.signing_key(), &expired_claims).unwrap();
        let result = PasetoPQ::verify(keypair.verifying_key(), &expired_token);
        assert!(matches!(result.unwrap_err(), PqPasetoError::TokenExpired));

        // Test not-yet-valid token
        let mut future_claims = Claims::new();
        future_claims.set_subject("user").unwrap();
        future_claims
            .set_not_before(now + Duration::hours(1))
            .unwrap();

        let future_token = PasetoPQ::sign(keypair.signing_key(), &future_claims).unwrap();
        let result = PasetoPQ::verify(keypair.verifying_key(), &future_token);
        assert!(matches!(
            result.unwrap_err(),
            PqPasetoError::TokenNotYetValid
        ));

        // Test valid token
        let mut valid_claims = Claims::new();
        valid_claims.set_subject("user").unwrap();
        valid_claims
            .set_not_before(now - Duration::minutes(5))
            .unwrap();
        valid_claims
            .set_expiration(now + Duration::hours(1))
            .unwrap();

        let valid_token = PasetoPQ::sign(keypair.signing_key(), &valid_claims).unwrap();
        let verified = PasetoPQ::verify(keypair.verifying_key(), &valid_token).unwrap();
        assert_eq!(verified.claims().subject(), Some("user"));
    }

    #[test]
    fn test_audience_and_issuer_validation() {
        let mut rng = rng();
        let keypair = KeyPair::generate(&mut rng);

        let mut claims = Claims::new();
        claims.set_subject("user").unwrap();
        claims.set_audience("api.example.com").unwrap();
        claims.set_issuer("my-service").unwrap();

        let token = PasetoPQ::sign(keypair.signing_key(), &claims).unwrap();

        // Valid audience and issuer
        let verified = PasetoPQ::verify_with_options(
            keypair.verifying_key(),
            &token,
            Some("api.example.com"),
            Some("my-service"),
            Duration::seconds(30),
        )
        .unwrap();
        assert_eq!(verified.claims().subject(), Some("user"));

        // Invalid audience
        let result = PasetoPQ::verify_with_options(
            keypair.verifying_key(),
            &token,
            Some("wrong-audience"),
            Some("conflux-auth"),
            Duration::seconds(30),
        );
        assert!(matches!(
            result.unwrap_err(),
            PqPasetoError::InvalidAudience { .. }
        ));

        // Invalid issuer
        let result = PasetoPQ::verify_with_options(
            keypair.verifying_key(),
            &token,
            Some("api.example.com"),
            Some("wrong-service"),
            Duration::seconds(30),
        );
        assert!(matches!(
            result.unwrap_err(),
            PqPasetoError::InvalidIssuer { .. }
        ));
    }

    #[test]
    fn test_signature_verification_failure() {
        let mut rng = rng();
        let keypair1 = KeyPair::generate(&mut rng);
        let keypair2 = KeyPair::generate(&mut rng);

        let mut claims = Claims::new();
        claims.set_subject("user").unwrap();

        let token = PasetoPQ::sign(&keypair1.signing_key, &claims).unwrap();

        // Try to verify with wrong key
        let result = PasetoPQ::verify(&keypair2.verifying_key, &token);
        assert!(matches!(
            result.unwrap_err(),
            PqPasetoError::SignatureVerificationFailed
        ));
    }

    #[test]
    fn test_malformed_tokens() {
        let mut rng = rng();
        let keypair = KeyPair::generate(&mut rng);

        // Too few parts
        let result = PasetoPQ::verify(keypair.verifying_key(), "paseto.pq1");
        assert!(matches!(
            result.unwrap_err(),
            PqPasetoError::InvalidFormat(_)
        ));

        // Wrong prefix
        let result = PasetoPQ::verify(keypair.verifying_key(), "wrong.pq1.pq.payload.sig");
        assert!(matches!(
            result.unwrap_err(),
            PqPasetoError::InvalidFormat(_)
        ));

        // Invalid base64 in payload
        let result = PasetoPQ::verify(keypair.verifying_key(), "paseto.pq1.public.invalid!!!.sig");
        assert!(matches!(
            result.unwrap_err(),
            PqPasetoError::InvalidFormat(_)
        ));

        // Invalid signature bytes
        let result = PasetoPQ::verify(
            keypair.verifying_key(),
            "paseto.pq1.public.dGVzdA.invalid_sig",
        );
        assert!(matches!(result.unwrap_err(), PqPasetoError::CryptoError(_)));
    }

    #[test]
    fn test_symmetric_key_generation() {
        let mut rng = rng();
        let key = SymmetricKey::generate(&mut rng);

        // Test bytes export/import
        let key_bytes = key.to_bytes();
        assert_eq!(key_bytes.len(), SYMMETRIC_KEY_SIZE);

        let imported_key = SymmetricKey::from_bytes(&key_bytes).unwrap();
        assert_eq!(key.to_bytes(), imported_key.to_bytes());
    }

    #[test]
    fn test_kem_keypair_generation() {
        let mut rng = rng();
        let keypair = KemKeyPair::generate(&mut rng);

        // Test bytes export/import
        let enc_bytes = keypair.encapsulation_key_to_bytes();
        let dec_bytes = keypair.decapsulation_key_to_bytes();

        assert!(!enc_bytes.is_empty());
        assert!(!dec_bytes.is_empty());

        let _imported_enc = KemKeyPair::encapsulation_key_from_bytes(&enc_bytes).unwrap();
        let _imported_dec = KemKeyPair::decapsulation_key_from_bytes(&dec_bytes).unwrap();

        // Test key encapsulation/decapsulation with real ML-KEM implementation
        let (sender_key, ciphertext) = keypair.encapsulate();
        let receiver_key = keypair.decapsulate(&ciphertext).unwrap();

        // Real ML-KEM implementation should produce identical shared secrets
        assert_eq!(sender_key.to_bytes(), receiver_key.to_bytes());
        assert_ne!(sender_key.to_bytes(), [0u8; 32]); // Should not be all zeros
        assert_eq!(ciphertext.len(), 1088); // ML-KEM-768 ciphertext size
    }

    #[test]
    fn test_basic_encrypt_and_decrypt() {
        let mut rng = rng();
        let key = SymmetricKey::generate(&mut rng);

        let mut claims = Claims::new();
        claims.set_subject("user123").unwrap();
        claims.set_issuer("conflux-auth").unwrap();
        claims.set_audience("conflux-network").unwrap();
        claims.set_jti("token-id-123").unwrap();
        claims.add_custom("tenant_id", "org_abc123").unwrap();
        claims.add_custom("roles", ["user", "admin"]).unwrap();

        let token = PasetoPQ::encrypt(&key, &claims).unwrap();
        assert!(token.starts_with("paseto.pq1.local."));

        let verified = PasetoPQ::decrypt(&key, &token).unwrap();
        let verified_claims = verified.claims();

        assert_eq!(verified_claims.subject(), Some("user123"));
        assert_eq!(verified_claims.issuer(), Some("conflux-auth"));
        assert_eq!(verified_claims.audience(), Some("conflux-network"));
        assert_eq!(verified_claims.jti(), Some("token-id-123"));

        // Check custom claims
        assert_eq!(
            verified_claims.get_custom("tenant_id").unwrap().as_str(),
            Some("org_abc123")
        );
        let roles: Vec<String> =
            serde_json::from_value(verified_claims.get_custom("roles").unwrap().clone()).unwrap();
        assert_eq!(roles, vec!["user", "admin"]);
    }

    #[test]
    fn test_local_token_time_validation() {
        let mut rng = rng();
        let key = SymmetricKey::generate(&mut rng);
        let now = OffsetDateTime::now_utc();

        // Test expired token
        let mut expired_claims = Claims::new();
        expired_claims.set_subject("user").unwrap();
        expired_claims
            .set_expiration(now - Duration::hours(1))
            .unwrap();

        let expired_token = PasetoPQ::encrypt(&key, &expired_claims).unwrap();
        let result = PasetoPQ::decrypt(&key, &expired_token);
        assert!(matches!(result.unwrap_err(), PqPasetoError::TokenExpired));

        // Test not-yet-valid token
        let mut future_claims = Claims::new();
        future_claims.set_subject("user").unwrap();
        future_claims
            .set_not_before(now + Duration::hours(1))
            .unwrap();

        let future_token = PasetoPQ::encrypt(&key, &future_claims).unwrap();
        let result = PasetoPQ::decrypt(&key, &future_token);
        assert!(matches!(
            result.unwrap_err(),
            PqPasetoError::TokenNotYetValid
        ));

        // Test valid token
        let mut valid_claims = Claims::new();
        valid_claims.set_subject("user").unwrap();
        valid_claims
            .set_not_before(now - Duration::minutes(5))
            .unwrap();
        valid_claims
            .set_expiration(now + Duration::hours(1))
            .unwrap();

        let valid_token = PasetoPQ::encrypt(&key, &valid_claims).unwrap();
        let verified = PasetoPQ::decrypt(&key, &valid_token).unwrap();
        assert_eq!(verified.claims().subject(), Some("user"));
    }

    #[test]
    fn test_local_token_audience_and_issuer_validation() {
        let mut rng = rng();
        let key = SymmetricKey::generate(&mut rng);

        let mut claims = Claims::new();
        claims.set_subject("user123").unwrap();
        claims.set_issuer("test-issuer").unwrap();
        claims.set_audience("test-audience").unwrap();

        let token = PasetoPQ::encrypt(&key, &claims).unwrap();

        // Valid audience and issuer
        let verified = PasetoPQ::decrypt_with_options(
            &key,
            &token,
            Some("test-audience"),
            Some("test-issuer"),
            Duration::seconds(30),
        )
        .unwrap();
        assert_eq!(verified.claims().subject(), Some("user123"));

        // Wrong audience
        let result = PasetoPQ::decrypt_with_options(
            &key,
            &token,
            Some("wrong-audience"),
            Some("test-issuer"),
            Duration::seconds(30),
        );
        assert!(matches!(
            result.unwrap_err(),
            PqPasetoError::InvalidAudience { .. }
        ));

        // Wrong issuer
        let result = PasetoPQ::decrypt_with_options(
            &key,
            &token,
            Some("test-audience"),
            Some("wrong-issuer"),
            Duration::seconds(30),
        );
        assert!(matches!(
            result.unwrap_err(),
            PqPasetoError::InvalidIssuer { .. }
        ));
    }

    #[test]
    fn test_local_token_tamper_detection() {
        let mut rng = rng();
        let key = SymmetricKey::generate(&mut rng);

        let mut claims = Claims::new();
        claims.set_subject("user123").unwrap();

        let token = PasetoPQ::encrypt(&key, &claims).unwrap();

        // Tamper with the token
        let mut tampered_token = token.clone();
        tampered_token.push('x'); // Append extra character

        let result = PasetoPQ::decrypt(&key, &tampered_token);
        assert!(result.is_err());

        // Try with wrong key
        let wrong_key = SymmetricKey::generate(&mut rng);
        let result = PasetoPQ::decrypt(&wrong_key, &token);
        assert!(matches!(
            result.unwrap_err(),
            PqPasetoError::DecryptionError(_)
        ));
    }

    #[test]
    fn test_malformed_local_tokens() {
        let mut rng = rng();
        let key = SymmetricKey::generate(&mut rng);

        // Wrong prefix
        let result = PasetoPQ::decrypt(&key, "wrong.pq1.local.payload");
        assert!(matches!(
            result.unwrap_err(),
            PqPasetoError::InvalidFormat(_)
        ));

        // Invalid base64 in payload
        let result = PasetoPQ::decrypt(&key, "paseto.pq1.local.invalid!!!");
        assert!(matches!(
            result.unwrap_err(),
            PqPasetoError::InvalidFormat(_)
        ));

        // Too short payload (no nonce)
        let result = PasetoPQ::decrypt(&key, "paseto.pq1.local.dGVzdA");
        assert!(matches!(
            result.unwrap_err(),
            PqPasetoError::DecryptionError(_)
        ));
    }

    #[test]
    fn test_mixed_token_types() {
        let mut rng = rng();
        let asymmetric_keypair = KeyPair::generate(&mut rng);
        let symmetric_key = SymmetricKey::generate(&mut rng);

        let mut claims = Claims::new();
        claims.set_subject("user123").unwrap();

        // Create both types of tokens
        let public_token = PasetoPQ::sign(asymmetric_keypair.signing_key(), &claims).unwrap();
        let local_token = PasetoPQ::encrypt(&symmetric_key, &claims).unwrap();

        assert!(public_token.starts_with("paseto.pq1.public."));
        assert!(local_token.starts_with("paseto.pq1.local."));

        // Verify each with correct method
        let verified_public =
            PasetoPQ::verify(asymmetric_keypair.verifying_key(), &public_token).unwrap();
        let verified_local = PasetoPQ::decrypt(&symmetric_key, &local_token).unwrap();

        assert_eq!(verified_public.claims().subject(), Some("user123"));
        assert_eq!(verified_local.claims().subject(), Some("user123"));

        // Cross-verification should fail
        let result = PasetoPQ::decrypt(&symmetric_key, &public_token);
        assert!(result.is_err());

        let result = PasetoPQ::verify(asymmetric_keypair.verifying_key(), &local_token);
        assert!(result.is_err());
    }

    #[test]
    fn test_footer_basic_functionality() {
        let mut footer = Footer::new();
        footer.set_kid("test-key-123").unwrap();
        footer.set_version("1.0.0").unwrap();
        footer.add_custom("env", "production").unwrap();

        assert_eq!(footer.kid(), Some("test-key-123"));
        assert_eq!(footer.version(), Some("1.0.0"));
        assert_eq!(
            footer.get_custom("env").unwrap().as_str(),
            Some("production")
        );
    }

    #[test]
    fn test_public_token_with_footer() {
        let mut rng = rng();
        let keypair = KeyPair::generate(&mut rng);

        let mut claims = Claims::new();
        claims.set_subject("user123").unwrap();

        let mut footer = Footer::new();
        footer.set_kid("signing-key-2024").unwrap();
        footer.add_custom("deployment", "us-east-1").unwrap();

        // Token with footer
        let token =
            PasetoPQ::sign_with_footer(keypair.signing_key(), &claims, Some(&footer)).unwrap();
        assert!(token.starts_with("paseto.pq1.public."));
        assert_eq!(token.split('.').count(), 6); // paseto.pq1.public.payload.signature.footer

        let verified = PasetoPQ::verify_with_footer(keypair.verifying_key(), &token).unwrap();
        assert_eq!(verified.claims().subject(), Some("user123"));

        let verified_footer = verified.footer().unwrap();
        assert_eq!(verified_footer.kid(), Some("signing-key-2024"));
        assert_eq!(
            verified_footer.get_custom("deployment").unwrap().as_str(),
            Some("us-east-1")
        );

        // Token without footer should still work
        let token_no_footer = PasetoPQ::sign(keypair.signing_key(), &claims).unwrap();
        assert_eq!(token_no_footer.split('.').count(), 5);

        let verified_no_footer =
            PasetoPQ::verify(keypair.verifying_key(), &token_no_footer).unwrap();
        assert_eq!(verified_no_footer.claims().subject(), Some("user123"));
        assert!(verified_no_footer.footer().is_none());
    }

    #[test]
    fn test_local_token_with_footer() {
        let mut rng = rng();
        let key = SymmetricKey::generate(&mut rng);

        let mut claims = Claims::new();
        claims.set_subject("user123").unwrap();
        claims.add_custom("session_data", "confidential").unwrap();

        let mut footer = Footer::new();
        footer.set_kid("encryption-key-2024").unwrap();
        footer.add_custom("session_type", "secure").unwrap();

        // Token with footer
        let token = PasetoPQ::encrypt_with_footer(&key, &claims, Some(&footer)).unwrap();
        assert!(token.starts_with("paseto.pq1.local."));
        assert_eq!(token.split('.').count(), 5); // paseto.pq1.local.payload.footer

        let verified = PasetoPQ::decrypt_with_footer(&key, &token).unwrap();
        assert_eq!(verified.claims().subject(), Some("user123"));
        assert_eq!(
            verified
                .claims()
                .get_custom("session_data")
                .unwrap()
                .as_str(),
            Some("confidential")
        );

        let verified_footer = verified.footer().unwrap();
        assert_eq!(verified_footer.kid(), Some("encryption-key-2024"));
        assert_eq!(
            verified_footer.get_custom("session_type").unwrap().as_str(),
            Some("secure")
        );

        // Token without footer should still work
        let token_no_footer = PasetoPQ::encrypt(&key, &claims).unwrap();
        assert_eq!(token_no_footer.split('.').count(), 4);

        let verified_no_footer = PasetoPQ::decrypt(&key, &token_no_footer).unwrap();
        assert_eq!(verified_no_footer.claims().subject(), Some("user123"));
        assert!(verified_no_footer.footer().is_none());
    }

    #[test]
    fn test_footer_serialization() {
        let mut footer = Footer::new();
        footer.set_kid("test-key").unwrap();
        footer.set_version("1.0.0").unwrap();
        footer.add_custom("custom_field", "custom_value").unwrap();

        let encoded = footer.to_base64().unwrap();
        let decoded = Footer::from_base64(&encoded).unwrap();

        assert_eq!(footer.kid(), decoded.kid());
        assert_eq!(footer.version(), decoded.version());
        assert_eq!(
            footer.get_custom("custom_field"),
            decoded.get_custom("custom_field")
        );
    }

    #[test]
    fn test_footer_tamper_detection() {
        let mut rng = rng();
        let keypair = KeyPair::generate(&mut rng);

        let mut claims = Claims::new();
        claims.set_subject("user123").unwrap();

        let mut footer = Footer::new();
        footer.set_kid("test-key").unwrap();

        let token =
            PasetoPQ::sign_with_footer(keypair.signing_key(), &claims, Some(&footer)).unwrap();

        // Tamper with footer
        let mut tampered_token = token.clone();
        tampered_token.push('x');

        let result = PasetoPQ::verify_with_footer(keypair.verifying_key(), &tampered_token);
        assert!(result.is_err()); // Tampered footer should fail verification
    }

    #[test]
    fn test_backward_compatibility() {
        let mut rng = rng();
        let keypair = KeyPair::generate(&mut rng);
        let symmetric_key = SymmetricKey::generate(&mut rng);

        let mut claims = Claims::new();
        claims.set_subject("user123").unwrap();

        // Old format tokens (without footer) should work with new methods
        let public_token = PasetoPQ::sign(keypair.signing_key(), &claims).unwrap();
        let local_token = PasetoPQ::encrypt(&symmetric_key, &claims).unwrap();

        let verified_public =
            PasetoPQ::verify_with_footer(keypair.verifying_key(), &public_token).unwrap();
        let verified_local = PasetoPQ::decrypt_with_footer(&symmetric_key, &local_token).unwrap();

        assert_eq!(verified_public.claims().subject(), Some("user123"));
        assert_eq!(verified_local.claims().subject(), Some("user123"));
        assert!(verified_public.footer().is_none());
        assert!(verified_local.footer().is_none());
    }

    #[test]
    fn test_claims_json_conversion() {
        use serde_json::Value;

        let mut claims = Claims::new();
        claims.set_subject("user123").unwrap();
        claims.set_issuer("test-service").unwrap();
        claims.set_audience("api.example.com").unwrap();
        claims.add_custom("role", "admin").unwrap();
        claims.add_custom("tenant_id", "org_abc123").unwrap();
        claims
            .add_custom("permissions", ["read", "write", "delete"])
            .unwrap();

        // Test From<Claims> for serde_json::Value
        let json_value: Value = claims.clone().into();
        assert!(json_value.is_object());
        assert_eq!(json_value["sub"], "user123");
        assert_eq!(json_value["iss"], "test-service");
        assert_eq!(json_value["aud"], "api.example.com");
        assert_eq!(json_value["role"], "admin");
        assert_eq!(json_value["tenant_id"], "org_abc123");
        assert_eq!(json_value["permissions"][0], "read");

        // Test From<&Claims> for serde_json::Value
        let json_value_ref: Value = (&claims).into();
        assert_eq!(json_value, json_value_ref);

        // Test to_json_value method
        let json_value_method = claims.to_json_value();
        assert_eq!(json_value, json_value_method);

        // Test to_json_string method
        let json_string = claims.to_json_string().unwrap();
        assert!(json_string.contains("\"sub\":\"user123\""));
        assert!(json_string.contains("\"role\":\"admin\""));

        // Test to_json_string_pretty method
        let pretty_json = claims.to_json_string_pretty().unwrap();
        assert!(pretty_json.contains("\"sub\": \"user123\""));
        assert!(pretty_json.contains("\"role\": \"admin\""));
        assert!(pretty_json.len() > json_string.len()); // Pretty format should be longer

        // Test that optional fields are skipped when None
        let minimal_claims = Claims::new();
        let minimal_json: Value = minimal_claims.into();
        assert!(minimal_json.is_object());
        assert!(minimal_json.as_object().unwrap().is_empty());
    }

    #[test]
    fn test_claims_json_with_time_fields() {
        use serde_json::Value;
        use time::OffsetDateTime;

        let mut claims = Claims::new();
        let now = OffsetDateTime::now_utc();
        let exp_time = now + time::Duration::hours(1);
        let nbf_time = now - time::Duration::minutes(5);

        claims.set_subject("user456").unwrap();
        claims.set_expiration(exp_time).unwrap();
        claims.set_not_before(nbf_time).unwrap();
        claims.set_issued_at(now).unwrap();

        let json_value: Value = claims.into();

        // Verify time fields are present and properly formatted as RFC3339 strings
        assert!(json_value["exp"].is_string());
        assert!(json_value["nbf"].is_string());
        assert!(json_value["iat"].is_string());

        // Verify the time strings can be parsed back
        let exp_str = json_value["exp"].as_str().unwrap();
        let parsed_exp =
            OffsetDateTime::parse(exp_str, &time::format_description::well_known::Rfc3339).unwrap();
        assert_eq!(parsed_exp.unix_timestamp(), exp_time.unix_timestamp());
    }

    #[test]
    fn test_claims_json_integration_example() {
        use serde_json::Value;

        // Simulate a real-world use case
        let mut claims = Claims::new();
        claims.set_subject("user789").unwrap();
        claims.set_issuer("auth-service").unwrap();
        claims.set_audience("api.conflux.dev").unwrap();
        claims
            .add_custom("session_id", "sess_abc123def456")
            .unwrap();
        claims.add_custom("user_type", "premium").unwrap();
        claims
            .add_custom("scopes", ["profile", "email", "admin"])
            .unwrap();

        // Test integration with logging (simulated)
        let json_string = claims.to_json_string().unwrap();
        assert!(!json_string.is_empty());

        // Test integration with database storage (simulated)
        let json_value: Value = claims.clone().into();
        let serialized_for_db = serde_json::to_vec(&json_value).unwrap();
        assert!(!serialized_for_db.is_empty());

        // Test round-trip conversion
        let deserialized_value: Value = serde_json::from_slice(&serialized_for_db).unwrap();
        assert_eq!(json_value, deserialized_value);

        // Verify specific fields for logging/tracing integration
        assert_eq!(deserialized_value["sub"], "user789");
        assert_eq!(deserialized_value["session_id"], "sess_abc123def456");
        assert_eq!(deserialized_value["scopes"].as_array().unwrap().len(), 3);
    }

    #[test]
    fn test_token_parsing_public_tokens() {
        let mut rng = rng();
        let keypair = KeyPair::generate(&mut rng);

        // Create a public token without footer
        let mut claims = Claims::new();
        claims.set_subject("test-user").unwrap();
        claims.add_custom("role", "admin").unwrap();

        let token = PasetoPQ::sign(keypair.signing_key(), &claims).unwrap();
        let parsed = ParsedToken::parse(&token).unwrap();

        // Verify basic properties
        assert_eq!(parsed.purpose(), "public");
        assert_eq!(parsed.version(), "pq1");
        assert!(!parsed.has_footer());
        assert!(parsed.is_public());
        assert!(!parsed.is_local());
        assert!(parsed.signature_bytes().is_some());
        assert_eq!(parsed.raw_token(), &token);

        // Test alternative API
        let parsed_alt = PasetoPQ::parse_token(&token).unwrap();
        assert_eq!(parsed.purpose(), parsed_alt.purpose());
    }

    #[test]
    fn test_token_parsing_public_tokens_with_footer() {
        let mut rng = rng();
        let keypair = KeyPair::generate(&mut rng);

        // Create a public token with footer
        let mut claims = Claims::new();
        claims.set_subject("test-user").unwrap();

        let mut footer = Footer::new();
        footer.set_kid("test-key-123").unwrap();
        footer.add_custom("tenant", "org_abc").unwrap();

        let token =
            PasetoPQ::sign_with_footer(keypair.signing_key(), &claims, Some(&footer)).unwrap();
        let parsed = ParsedToken::parse(&token).unwrap();

        // Verify footer parsing
        assert!(parsed.has_footer());
        let parsed_footer = parsed.footer().unwrap();
        assert_eq!(parsed_footer.kid(), Some("test-key-123"));
        assert_eq!(
            parsed_footer.get_custom("tenant"),
            Some(&serde_json::json!("org_abc"))
        );

        // Test JSON footer methods
        let footer_json = parsed.footer_json().unwrap().unwrap();
        assert!(footer_json.contains("test-key-123"));
        assert!(footer_json.contains("org_abc"));

        let footer_pretty = parsed.footer_json_pretty().unwrap().unwrap();
        assert!(footer_pretty.len() > footer_json.len()); // Pretty should be longer
    }

    #[test]
    fn test_token_parsing_local_tokens() {
        let mut rng = rng();
        let key = SymmetricKey::generate(&mut rng);

        // Create a local token without footer
        let mut claims = Claims::new();
        claims.set_subject("local-user").unwrap();
        claims.add_custom("session_type", "confidential").unwrap();

        let token = PasetoPQ::encrypt(&key, &claims).unwrap();
        let parsed = ParsedToken::parse(&token).unwrap();

        // Verify basic properties
        assert_eq!(parsed.purpose(), "local");
        assert_eq!(parsed.version(), "pq1");
        assert!(!parsed.has_footer());
        assert!(!parsed.is_public());
        assert!(parsed.is_local());
        assert!(parsed.signature_bytes().is_none()); // Local tokens don't have separate signatures
        assert!(parsed.payload_length() > 0);
    }

    #[test]
    fn test_token_parsing_local_tokens_with_footer() {
        let mut rng = rng();
        let key = SymmetricKey::generate(&mut rng);

        // Create a local token with footer
        let mut claims = Claims::new();
        claims.set_subject("local-user").unwrap();

        let mut footer = Footer::new();
        footer.set_kid("encryption-key-456").unwrap();
        footer.set_version("v2.1").unwrap();

        let token = PasetoPQ::encrypt_with_footer(&key, &claims, Some(&footer)).unwrap();
        let parsed = ParsedToken::parse(&token).unwrap();

        // Verify footer parsing
        assert!(parsed.has_footer());
        let parsed_footer = parsed.footer().unwrap();
        assert_eq!(parsed_footer.kid(), Some("encryption-key-456"));
        assert_eq!(parsed_footer.version(), Some("v2.1"));

        // Test format summary
        let summary = parsed.format_summary();
        assert!(summary.contains("paseto.pq1.local"));
        assert!(summary.contains("footer: present"));
    }

    #[test]
    fn test_token_parsing_error_cases() {
        // Test various malformed tokens
        let error_cases = vec![
            ("", "expected at least 4 parts"),
            ("not.a.token", "expected at least 4 parts"),
            ("wrong.pq1.public.payload", "Invalid protocol"),
            ("paseto.v2.public.payload", "Unsupported token format"),
            ("paseto.pq1.unknown.payload", "Unsupported token format"),
            ("paseto.pq1.public.invalid_base64", "Invalid payload base64"),
            (
                "paseto.pq1.public.dGVzdA.invalid!!!base64",
                "Invalid signature base64",
            ),
            (
                "paseto.pq1.public.dGVzdA.dGVzdA.dGVzdA.extra.parts",
                "too many parts",
            ),
            ("paseto.pq1.local.dGVzdA.dGVzdA.extra", "too many parts"),
        ];

        for (token, expected_error) in error_cases {
            let result = ParsedToken::parse(token);
            assert!(result.is_err(), "Expected error for token: {}", token);
            let error_msg = result.unwrap_err().to_string();
            assert!(
                error_msg.contains(expected_error),
                "Expected '{}' in error '{}' for token '{}'",
                expected_error,
                error_msg,
                token
            );
        }
    }

    #[test]
    fn test_token_size_estimation_public_tokens() {
        // Test basic public token estimation
        let mut claims = Claims::new();
        claims.set_subject("user123").unwrap();
        claims.set_issuer("test-service").unwrap();

        let estimator = TokenSizeEstimator::public(&claims, false);

        // Public token size expectations vary by ML-DSA parameter set
        let (expected_min_size, expected_max_size) = if cfg!(feature = "ml-dsa-44") {
            (2800, 3200) // ML-DSA-44: ~2.8KB signature + payload + overhead
        } else if cfg!(feature = "ml-dsa-65") {
            (4200, 4800) // ML-DSA-65: ~4.3KB signature + payload + overhead
        } else {
            (5000, 5500) // ML-DSA-87: ~5KB signature + payload + overhead
        };

        assert!(estimator.total_bytes() >= expected_min_size);
        assert!(estimator.total_bytes() < expected_max_size);

        // Check size limit methods - expectations vary by parameter set
        if cfg!(feature = "ml-dsa-44") {
            // ML-DSA-44 tokens (~2.8KB) fit in cookies but not URLs
            assert!(estimator.fits_in_cookie()); // 2885 < 4096
            assert!(!estimator.fits_in_url()); // 2885 > 2048
        } else {
            // ML-DSA-65 and ML-DSA-87 tokens are too large for both
            assert!(!estimator.fits_in_cookie()); // > 4096
            assert!(!estimator.fits_in_url()); // > 2048
        }
        assert!(estimator.fits_in_header()); // All should fit in headers

        // Test breakdown components
        let breakdown = estimator.breakdown();
        assert!(breakdown.prefix > 0);
        assert!(breakdown.payload > 0);

        // Signature size expectations based on parameter set
        let expected_sig_size = if cfg!(feature = "ml-dsa-44") {
            2800
        } else if cfg!(feature = "ml-dsa-65") {
            4300
        } else {
            5000
        };
        assert_eq!(breakdown.signature_or_tag, expected_sig_size);
        assert_eq!(breakdown.footer, None);
        assert!(breakdown.separators > 0);
        assert!(breakdown.base64_overhead > 0);
    }

    #[test]
    fn test_token_size_estimation_local_tokens() {
        // Test basic local token estimation
        let mut claims = Claims::new();
        claims.set_subject("user123").unwrap();
        claims.set_issuer("test-service").unwrap();

        let estimator = TokenSizeEstimator::local(&claims, false);

        // Local tokens should be much smaller than public tokens
        assert!(estimator.total_bytes() > 80);
        assert!(estimator.total_bytes() < 300); // Should be reasonably small

        // Check size limit methods
        assert!(estimator.fits_in_cookie());
        assert!(estimator.fits_in_url());
        assert!(estimator.fits_in_header());

        // Test breakdown components
        let breakdown = estimator.breakdown();
        assert!(breakdown.prefix > 0);
        assert!(breakdown.payload > 0);
        assert_eq!(breakdown.signature_or_tag, 0); // Local tokens don't have separate signature
        assert_eq!(breakdown.footer, None);
        assert!(breakdown.separators > 0);
        assert!(breakdown.base64_overhead > 0);
    }

    #[test]
    fn test_token_size_estimation_with_footer() {
        let mut claims = Claims::new();
        claims.set_subject("user123").unwrap();

        // Test public token with footer
        let estimator_public = TokenSizeEstimator::public(&claims, true);
        let estimator_public_no_footer = TokenSizeEstimator::public(&claims, false);

        assert!(estimator_public.total_bytes() > estimator_public_no_footer.total_bytes());
        assert!(estimator_public.breakdown().footer.is_some());
        assert!(estimator_public_no_footer.breakdown().footer.is_none());

        // Test local token with footer
        let estimator_local = TokenSizeEstimator::local(&claims, true);
        let estimator_local_no_footer = TokenSizeEstimator::local(&claims, false);

        assert!(estimator_local.total_bytes() > estimator_local_no_footer.total_bytes());
        assert!(estimator_local.breakdown().footer.is_some());
        assert!(estimator_local_no_footer.breakdown().footer.is_none());
    }

    #[test]
    fn test_token_size_estimation_convenience_methods() {
        let mut claims = Claims::new();
        claims.set_subject("user123").unwrap();

        // Test PasetoPQ convenience methods
        let public_estimator = PasetoPQ::estimate_public_size(&claims, false);
        let local_estimator = PasetoPQ::estimate_local_size(&claims, true);

        // Should work the same as direct construction
        let direct_public = TokenSizeEstimator::public(&claims, false);
        let direct_local = TokenSizeEstimator::local(&claims, true);

        assert_eq!(public_estimator.total_bytes(), direct_public.total_bytes());
        assert_eq!(local_estimator.total_bytes(), direct_local.total_bytes());
    }

    #[test]
    fn test_token_size_estimation_optimization_suggestions() {
        // Test with small token - should have few suggestions
        let small_claims = Claims::new();
        let small_estimator = TokenSizeEstimator::local(&small_claims, false);
        let small_suggestions = small_estimator.optimization_suggestions();
        // Small local tokens should have no suggestions
        assert!(small_suggestions.is_empty() || small_estimator.total_bytes() < 1000);

        // Test with large token - should have many suggestions
        let mut large_claims = Claims::new();
        large_claims
            .add_custom("huge_data", "x".repeat(5000))
            .unwrap();
        let large_estimator = TokenSizeEstimator::public(&large_claims, false);
        let large_suggestions = large_estimator.optimization_suggestions();

        assert!(!large_suggestions.is_empty());
        assert!(large_suggestions.iter().any(|s| s.contains("cookie")));
        assert!(
            large_suggestions
                .iter()
                .any(|s| s.contains("shorter claim"))
        );
    }

    #[test]
    fn test_token_size_breakdown_total() {
        let breakdown = TokenSizeBreakdown {
            prefix: 10,
            payload: 200,
            signature_or_tag: 3000,
            footer: Some(50),
            separators: 3,
            base64_overhead: 100,
        };

        let expected_total = 10 + 200 + 3000 + 50 + 3 + 100;
        assert_eq!(breakdown.total(), expected_total);

        // Test without footer
        let breakdown_no_footer = TokenSizeBreakdown {
            prefix: 10,
            payload: 200,
            signature_or_tag: 3000,
            footer: None,
            separators: 2,
            base64_overhead: 100,
        };

        let expected_total_no_footer = 10 + 200 + 3000 + 2 + 100;
        assert_eq!(breakdown_no_footer.total(), expected_total_no_footer);
    }

    #[test]
    fn test_token_parsing_debugging_methods() {
        let mut rng = rng();
        let keypair = KeyPair::generate(&mut rng);

        let mut claims = Claims::new();
        claims.set_subject("debug-user").unwrap();
        claims.add_custom("large_data", "x".repeat(500)).unwrap(); // Make it somewhat large

        let mut footer = Footer::new();
        footer.set_kid("debug-key").unwrap();

        let token =
            PasetoPQ::sign_with_footer(keypair.signing_key(), &claims, Some(&footer)).unwrap();
        let parsed = ParsedToken::parse(&token).unwrap();

        // Test debugging methods
        assert!(parsed.payload_length() > 100); // Should have substantial payload
        assert!(parsed.total_length() > parsed.payload_length()); // Total includes overhead
        assert!(!parsed.payload_bytes().is_empty());

        let summary = parsed.format_summary();
        assert!(summary.contains("paseto.pq1.public"));
        assert!(summary.contains("signature: present"));
        assert!(summary.contains("footer: present"));
        assert!(summary.contains(&format!("{} bytes", parsed.payload_length())));
    }

    #[test]
    fn test_token_parsing_middleware_scenarios() {
        let mut rng = rng();
        let keypair = KeyPair::generate(&mut rng);
        let symmetric_key = SymmetricKey::generate(&mut rng);

        // Create different token types
        let mut claims = Claims::new();
        claims.set_subject("middleware-test").unwrap();

        let public_token = PasetoPQ::sign(keypair.signing_key(), &claims).unwrap();
        let local_token = PasetoPQ::encrypt(&symmetric_key, &claims).unwrap();

        // Simulate middleware routing logic
        let tokens = vec![
            (public_token, "public", true), // (token, expected_purpose, is_public)
            (local_token, "local", false),
        ];

        for (token, expected_purpose, should_be_public) in tokens {
            let parsed = ParsedToken::parse(&token).unwrap();

            // Routing decisions
            assert_eq!(parsed.purpose(), expected_purpose);
            assert_eq!(parsed.is_public(), should_be_public);
            assert_eq!(parsed.is_local(), !should_be_public);

            // Logging/metrics simulation
            let purpose = parsed.purpose();
            let version = parsed.version();
            let size = parsed.total_length();

            assert!(!purpose.is_empty());
            assert_eq!(version, "pq1");
            assert!(size > 0);

            // Simulate size-based alerts
            if size > 2048 {
                // Would trigger monitoring alert
                println!("Large token detected: {} bytes", size);
            }
        }
    }

    #[test]
    fn test_symmetric_key_zeroization() {
        // Test SymmetricKey zeroization - this is the only key type we fully control
        {
            let mut key = SymmetricKey([0x42u8; 32]);

            // Verify key contains expected data
            assert_eq!(key.0[0], 0x42);

            // Zeroize the key
            key.zeroize();

            // Verify key is zeroed
            assert_eq!(key.0, [0u8; 32]);
        }

        // Test that SymmetricKey is automatically zeroized on drop (ZeroizeOnDrop)
        {
            let key = SymmetricKey([0x55u8; 32]);
            assert_eq!(key.0[0], 0x55);
            // Key will be automatically zeroized when it goes out of scope
        }
    }

    #[test]
    fn test_key_operations_with_drop_cleanup() {
        // Test that cryptographic operations work correctly with Drop implementations
        let mut rng = rng();

        // Test ML-DSA keypair operations
        {
            let keypair = KeyPair::generate(&mut rng);
            let test_data = b"test message";
            let signature = keypair.signing_key().0.sign(test_data);
            assert!(
                keypair
                    .verifying_key()
                    .0
                    .verify(test_data, &signature)
                    .is_ok()
            );
            // keypair will be dropped and cleaned up automatically
        }

        // Test ML-KEM operations
        {
            let kem_keypair = KemKeyPair::generate(&mut rng);
            let (key1, ciphertext) = kem_keypair.encapsulate();
            let key2 = kem_keypair.decapsulate(&ciphertext).unwrap();
            assert_eq!(key1.to_bytes(), key2.to_bytes());
            // All keys will be dropped and cleaned up automatically
        }

        // Test symmetric key operations
        {
            let key = SymmetricKey::generate(&mut rng);
            let key_bytes = key.to_bytes();
            assert_eq!(key_bytes.len(), 32);
            // key will be automatically zeroized on drop
        }
    }

    #[test]
    fn test_token_versioning_configuration() {
        // Test that the prefix constants use pq1 versioning
        assert_eq!(TOKEN_PREFIX_PUBLIC, "paseto.pq1.public");
        assert_eq!(TOKEN_PREFIX_LOCAL, "paseto.pq1.local");

        // Test that we always report non-standard compatibility
        assert!(!PasetoPQ::is_standard_paseto_compatible());

        // Test prefix accessor methods
        assert_eq!(PasetoPQ::public_token_prefix(), TOKEN_PREFIX_PUBLIC);
        assert_eq!(PasetoPQ::local_token_prefix(), TOKEN_PREFIX_LOCAL);
    }

    #[test]
    fn test_actual_token_contains_correct_prefix() {
        let mut rng = rng();
        let keypair = KeyPair::generate(&mut rng);
        let symmetric_key = SymmetricKey::generate(&mut rng);

        let claims = Claims::new();

        // Test public token uses correct prefix
        let public_token = PasetoPQ::sign(keypair.signing_key(), &claims).unwrap();
        assert!(public_token.starts_with(TOKEN_PREFIX_PUBLIC));

        // Test local token uses correct prefix
        let local_token = PasetoPQ::encrypt(&symmetric_key, &claims).unwrap();
        assert!(local_token.starts_with(TOKEN_PREFIX_LOCAL));

        // Verify tokens can be parsed with the correct prefix expectations
        let parsed_public = ParsedToken::parse(&public_token).unwrap();
        let parsed_local = ParsedToken::parse(&local_token).unwrap();

        assert_eq!(parsed_public.version(), "pq1");
        assert_eq!(parsed_local.version(), "pq1");

        assert_eq!(parsed_public.purpose(), "public");
        assert_eq!(parsed_local.purpose(), "local");
    }

    #[test]
    fn test_hkdf_implementation() {
        // Test that proper HKDF produces different outputs for different inputs
        let shared_secret1 = b"shared_secret_1";
        let shared_secret2 = b"shared_secret_2";
        let info = b"PASETO-PQ-LOCAL-pq1";

        let key1 = SymmetricKey::derive_from_shared_secret(shared_secret1, info);
        let key2 = SymmetricKey::derive_from_shared_secret(shared_secret2, info);

        // Different secrets should produce different keys
        assert_ne!(key1.to_bytes(), key2.to_bytes());

        // Same inputs should produce same outputs (deterministic)
        let key1_repeat = SymmetricKey::derive_from_shared_secret(shared_secret1, info);
        assert_eq!(key1.to_bytes(), key1_repeat.to_bytes());

        // Different info should produce different keys with same secret
        let info2 = b"DIFFERENT-INFO";
        let key_diff_info = SymmetricKey::derive_from_shared_secret(shared_secret1, info2);
        assert_ne!(key1.to_bytes(), key_diff_info.to_bytes());

        // Verify we get full 32 bytes
        assert_eq!(key1.to_bytes().len(), 32);
        assert_eq!(key2.to_bytes().len(), 32);
    }

    #[test]
    fn test_footer_authentication_security_v0_1_1() {
        // Test that footer tampering is properly detected in v0.1.1
        let mut rng = rng();
        let keypair = KeyPair::generate(&mut rng);
        let symmetric_key = SymmetricKey::generate(&mut rng);

        let mut claims = Claims::new();
        claims.set_subject("test-user".to_string()).unwrap();
        claims.set_issuer("test-issuer".to_string()).unwrap();

        let mut footer = Footer::new();
        footer.set_kid("test-key-id").unwrap();
        footer.set_version("1.0").unwrap();

        // Test public token footer authentication
        let public_token =
            PasetoPQ::sign_with_footer(keypair.signing_key(), &claims, Some(&footer)).unwrap();

        // Valid token should verify successfully
        let verified =
            PasetoPQ::verify_with_footer(keypair.verifying_key(), &public_token).unwrap();
        assert_eq!(verified.claims().subject().unwrap(), "test-user");
        assert_eq!(verified.footer().unwrap().kid().unwrap(), "test-key-id");

        // Tamper with footer in public token - should fail verification
        let mut token_parts: Vec<&str> = public_token.split('.').collect();
        // Create a valid but different footer JSON
        let tampered_footer = Footer::new();
        let tampered_footer_b64 = tampered_footer.to_base64().unwrap();
        token_parts[5] = &tampered_footer_b64;
        let tampered_public = token_parts.join(".");

        let result = PasetoPQ::verify_with_footer(keypair.verifying_key(), &tampered_public);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            PqPasetoError::SignatureVerificationFailed
        ));

        // Test local token footer authentication
        let local_token =
            PasetoPQ::encrypt_with_footer(&symmetric_key, &claims, Some(&footer)).unwrap();

        // Valid token should decrypt successfully
        let decrypted = PasetoPQ::decrypt_with_footer(&symmetric_key, &local_token).unwrap();
        assert_eq!(decrypted.claims().subject().unwrap(), "test-user");
        assert_eq!(decrypted.footer().unwrap().kid().unwrap(), "test-key-id");

        // Tamper with footer in local token - should fail decryption
        let mut token_parts: Vec<&str> = local_token.split('.').collect();
        // Create a valid but different footer JSON
        let tampered_footer = Footer::new();
        let tampered_footer_b64 = tampered_footer.to_base64().unwrap();
        token_parts[4] = &tampered_footer_b64;
        let tampered_local = token_parts.join(".");

        let result = PasetoPQ::decrypt_with_footer(&symmetric_key, &tampered_local);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            PqPasetoError::DecryptionError(_)
        ));
    }

    #[test]
    fn test_pae_integration_v0_1_1() {
        // Test that PAE encoding is working correctly in token operations
        let mut rng = rng();
        let keypair = KeyPair::generate(&mut rng);

        let mut claims = Claims::new();
        claims.set_subject("pae-test".to_string()).unwrap();
        let mut footer = Footer::new();
        footer.set_kid("pae-key").unwrap();

        // Create token with footer
        let token_with_footer =
            PasetoPQ::sign_with_footer(keypair.signing_key(), &claims, Some(&footer)).unwrap();

        // Create token without footer
        let token_without_footer =
            PasetoPQ::sign_with_footer(keypair.signing_key(), &claims, None).unwrap();

        // Both should verify successfully
        let verified_with =
            PasetoPQ::verify_with_footer(keypair.verifying_key(), &token_with_footer).unwrap();
        let verified_without =
            PasetoPQ::verify_with_footer(keypair.verifying_key(), &token_without_footer).unwrap();

        assert_eq!(verified_with.claims().subject().unwrap(), "pae-test");
        assert_eq!(verified_without.claims().subject().unwrap(), "pae-test");
        assert!(verified_with.footer().is_some());
        assert!(verified_without.footer().is_none());

        // Test that empty footer is handled correctly (should authenticate empty bytes)
        let claims_json = serde_json::to_vec(&claims).unwrap();
        let empty_footer_bytes = Vec::new();
        let header = "paseto.pq1.public".as_bytes();

        let pae_message =
            crate::pae::pae_encode_public_token(header, &claims_json, &empty_footer_bytes);

        // PAE message should contain the empty footer bytes (length 0)
        assert!(!pae_message.is_empty());
        // This proves empty footers are still authenticated via PAE
    }

    #[test]
    fn test_v0_1_1_security_improvements() {
        // Comprehensive test demonstrating v0.1.1 security improvements
        let mut rng = rng();
        let keypair = KeyPair::generate(&mut rng);
        let symmetric_key = SymmetricKey::generate(&mut rng);

        let mut claims = Claims::new();
        claims.set_subject("security-test".to_string()).unwrap();
        claims.set_audience("api.example.com".to_string()).unwrap();

        // Test various footer content types
        let mut footer1 = Footer::new();
        footer1.set_kid("key-1").unwrap();

        let mut footer2 = Footer::new();
        footer2.set_version("2.0").unwrap();
        footer2.set_kid("key-2").unwrap();

        let mut footer3 = Footer::new();
        let admin_value = "admin";
        footer3.add_custom("role", &admin_value).unwrap();

        let footers = [footer1, footer2, footer3];

        for (i, footer) in footers.iter().enumerate() {
            // Test public tokens
            let public_token =
                PasetoPQ::sign_with_footer(keypair.signing_key(), &claims, Some(footer)).unwrap();

            let verified =
                PasetoPQ::verify_with_footer(keypair.verifying_key(), &public_token).unwrap();
            assert_eq!(verified.claims().subject().unwrap(), "security-test");

            // Test local tokens
            let local_token =
                PasetoPQ::encrypt_with_footer(&symmetric_key, &claims, Some(footer)).unwrap();

            let decrypted = PasetoPQ::decrypt_with_footer(&symmetric_key, &local_token).unwrap();
            assert_eq!(decrypted.claims().subject().unwrap(), "security-test");

            // Verify footer content is preserved
            match i {
                0 => assert_eq!(verified.footer().unwrap().kid().unwrap(), "key-1"),
                1 => {
                    assert_eq!(verified.footer().unwrap().version().unwrap(), "2.0");
                    assert_eq!(verified.footer().unwrap().kid().unwrap(), "key-2");
                }
                2 => {
                    let custom = verified.footer().unwrap().get_custom("role").unwrap();
                    assert_eq!(custom.as_str().unwrap(), "admin");
                }
                _ => unreachable!(),
            }
        }
    }

    #[test]
    fn test_hkdf_vs_simple_hash_difference() {
        // Verify that proper HKDF produces different results than simple hash
        let shared_secret = b"test_shared_secret";
        let info = b"PASETO-PQ-LOCAL-pq1";

        // Get HKDF result
        let hkdf_key = SymmetricKey::derive_from_shared_secret(shared_secret, info);

        // Simulate old simple hash approach for comparison
        use sha2::{Digest, Sha256};
        let mut hasher = Sha256::new();
        hasher.update(shared_secret);
        hasher.update(info);
        let simple_hash = hasher.finalize();

        // They should be different (proving we're using proper HKDF)
        assert_ne!(hkdf_key.to_bytes(), simple_hash.as_slice());
    }
}