rscrypto 0.1.1

Pure Rust cryptography, hardware-accelerated: BLAKE3, SHA-2/3, AES-GCM, ChaCha20-Poly1305, Ed25519, X25519, HMAC, HKDF, Argon2, CRC. no_std, WASM, ten CPU architectures.
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
//! Argon2 password hashing (RFC 9106).
//!
//! Ships all three variants:
//!
//! - [`Argon2d`] — data-dependent indexing, highest throughput, vulnerable to side-channel timing
//!   attacks. Useful for non-interactive, trusted- hardware settings (e.g. cryptocurrency
//!   proof-of-work).
//! - [`Argon2i`] — data-independent indexing, side-channel resistant. Useful when the adversary can
//!   observe memory-access patterns.
//! - [`Argon2id`] — hybrid: first half of the first pass runs Argon2i, the rest runs Argon2d.
//!   **Recommended default for password hashing** per RFC 9106 §4 and OWASP 2024 guidance.
//!
//! The implementation uses the BlaMka compression function from RFC 9106
//! §3.6 layered on top of [`crate::Blake2b`]. A runtime-cached [`KernelId`]
//! dispatcher picks the highest-throughput BlaMka kernel for the host: a
//! 4-way NEON kernel on aarch64 ships today, with per-arch x86_64 / VSX /
//! s390x / RVV / simd128 kernels rolling in behind the same contract.
//!
//! # Examples
//!
//! ```rust
//! use rscrypto::{Argon2Params, Argon2id};
//!
//! let params = Argon2Params::new()
//!   .memory_cost_kib(19 * 1024)
//!   .time_cost(2)
//!   .parallelism(1)
//!   .build()
//!   .expect("valid params");
//!
//! let password = b"correct horse battery staple";
//! let salt = b"random-salt-1234";
//!
//! let mut hash = [0u8; 32];
//! Argon2id::hash(&params, password, salt, &mut hash).expect("hash");
//!
//! assert!(Argon2id::verify(&params, password, salt, &hash).is_ok());
//! assert!(Argon2id::verify(&params, b"wrong", salt, &hash).is_err());
//! ```
//!
//! # Security
//!
//! - Salt length must be ≥ 8 bytes (RFC 9106 §3.1). 16 bytes recommended.
//! - Memory cost must satisfy `m ≥ 8 · p` (KiB).
//! - [`Argon2Params::validate`] enforces all RFC 9106 bounds; invalid inputs surface as
//!   [`Argon2Error`] at build time, not at hash time.
//! - The memory matrix is zeroized on drop.
//! - [`Argon2id::verify`] is constant-time with respect to the stored hash bytes.
//!
//! # Compliance
//!
//! Argon2 is **not FIPS 140-3 approved**. NIST SP 800-132 only covers
//! PBKDF2 for password-based key derivation; deployments under FIPS
//! policy should use [`crate::Pbkdf2Sha256`]. This module is suitable
//! for OWASP-aligned password hashing outside strict FIPS boundaries.
//!
//! Requires `alloc` — the memory matrix (`m_kib · 1024` bytes) cannot be
//! stack-allocated. Bare-metal / heap-less targets should select
//! [`crate::Pbkdf2Sha256`] (alloc-free).

#![allow(clippy::indexing_slicing)]
#![allow(clippy::unwrap_used)]
// unwraps here are on slice→array conversions whose lengths are fixed by construction.

use alloc::vec::Vec;
use core::fmt;

use crate::{
  hashes::crypto::blake2b::{Blake2b, Blake2b512},
  traits::{Digest as _, VerificationError, ct},
};

#[cfg(target_arch = "aarch64")]
mod aarch64;
mod dispatch;
mod kernels;
#[cfg(target_arch = "powerpc64")]
mod power;
#[cfg(target_arch = "riscv64")]
mod riscv64;
#[cfg(target_arch = "s390x")]
mod s390x;
#[cfg(target_arch = "wasm32")]
mod wasm;
#[cfg(target_arch = "x86_64")]
mod x86_64;

use dispatch::active_compress;
pub use dispatch::{ALL_KERNELS, KernelId, required_caps};
use kernels::CompressFn;

// ─── Constants ──────────────────────────────────────────────────────────────

/// Memory block size in bytes (RFC 9106 §3).
pub const BLOCK_SIZE: usize = 1024;

/// Memory block size in 64-bit words.
const BLOCK_WORDS: usize = BLOCK_SIZE / 8; // 128

/// Number of synchronisation points per pass (RFC 9106 §3.4).
const SYNC_POINTS: u32 = 4;

/// Minimum salt length in bytes (RFC 9106 §3.1).
pub const MIN_SALT_LEN: usize = 8;

/// Maximum encoded parameter byte length (bounds password/secret/AD/salt).
const MAX_VAR_BYTES: u64 = u32::MAX as u64;

/// Minimum output length in bytes (RFC 9106 §3.1).
pub const MIN_OUTPUT_LEN: usize = 4;

/// Default OWASP 2024 parameters (see [`Argon2Params::new`]).
const DEFAULT_MEMORY_KIB: u32 = 19 * 1024;
const DEFAULT_TIME_COST: u32 = 2;
const DEFAULT_PARALLELISM: u32 = 1;
const DEFAULT_OUTPUT_LEN: usize = 32;

/// BlaMka P permutation operates on 8×8 matrices of 16-byte "registers",
/// i.e. on 16-u64 slabs.
const P_LANE_WORDS: usize = 16;

#[repr(align(64))]
#[derive(Clone, Copy)]
struct MemoryBlock([u64; BLOCK_WORDS]);

impl MemoryBlock {
  #[inline(always)]
  const fn zero() -> Self {
    Self([0u64; BLOCK_WORDS])
  }
}

/// Volatile zeroisation for a slice of u64 words. The workspace `ct`
/// module only exports byte-slice zeroization; Argon2 holds most scratch
/// as `[u64; BLOCK_WORDS]`, so a word-oriented helper is cleaner than
/// repeated byte-slice transmutes.
#[inline]
fn zeroize_u64_slice_no_fence(words: &mut [u64]) {
  let mut chunks = words.chunks_exact_mut(8);
  for chunk in &mut chunks {
    // SAFETY: chunk has exactly 8 initialized u64 values and [u64; 8] has
    // the same alignment requirement as u64.
    unsafe { core::ptr::write_volatile(chunk.as_mut_ptr().cast::<[u64; 8]>(), [0u64; 8]) };
  }
  for w in chunks.into_remainder() {
    // SAFETY: w is a valid, aligned, dereferenceable pointer to initialized u64.
    unsafe { core::ptr::write_volatile(w, 0) };
  }
}

#[inline]
fn zeroize_u64_slice(words: &mut [u64]) {
  zeroize_u64_slice_no_fence(words);
  core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
}

// ─── Variant, Version ───────────────────────────────────────────────────────

/// Argon2 variant selector.
///
/// The variant controls reference-block indexing:
///
/// - `Argon2d` — data-dependent (Argon2d mode throughout).
/// - `Argon2i` — data-independent (Argon2i mode throughout).
/// - `Argon2id` — data-independent for the first half of the first pass, data-dependent afterwards
///   (RFC 9106 §3.4.1).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Argon2Variant {
  Argon2d,
  Argon2i,
  Argon2id,
}

impl Argon2Variant {
  /// Argon2 `y` constant encoded into `H0` per RFC 9106 §3.2.
  #[inline]
  const fn y(self) -> u32 {
    match self {
      Self::Argon2d => 0,
      Self::Argon2i => 1,
      Self::Argon2id => 2,
    }
  }
}

/// Argon2 algorithm version.
///
/// `V0x13` (1.3) is the RFC 9106 default and should be used for all new
/// deployments. `V0x10` (1.0) is supported only for decoding legacy PHC
/// strings.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum Argon2Version {
  V0x10,
  #[default]
  V0x13,
}

impl Argon2Version {
  #[inline]
  const fn as_u32(self) -> u32 {
    match self {
      Self::V0x10 => 0x10,
      Self::V0x13 => 0x13,
    }
  }
}

// ─── Error ──────────────────────────────────────────────────────────────────

/// Invalid Argon2 parameter or input.
///
/// Surfaced at `build` or `hash` time — never at `verify` time (a parameter
/// error during verification would leak whether the encoded hash was valid,
/// so verify collapses these into [`crate::VerificationError`]).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Argon2Error {
  /// `time_cost` must be ≥ 1.
  InvalidTimeCost,
  /// `memory_cost` must satisfy RFC 9106 bounds (`m ≥ 8 · p`, `m ≤ 2^32 − 1`).
  InvalidMemoryCost,
  /// `parallelism` must be in 1..=2^24-1.
  InvalidParallelism,
  /// `output_len` must be in 4..=2^32-1.
  InvalidOutputLen,
  /// Salt must be ≥ 8 bytes (RFC 9106 §3.1).
  SaltTooShort,
  /// Salt length exceeds 2^32-1 bytes.
  SaltTooLong,
  /// Password length exceeds 2^32-1 bytes.
  PasswordTooLong,
  /// Optional secret length exceeds 2^32-1 bytes.
  SecretTooLong,
  /// Optional associated-data length exceeds 2^32-1 bytes.
  AssociatedDataTooLong,
  /// The platform entropy source failed while generating a PHC salt.
  EntropyUnavailable,
}

impl fmt::Display for Argon2Error {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.write_str(match self {
      Self::InvalidTimeCost => "Argon2 time_cost must be at least 1",
      Self::InvalidMemoryCost => "Argon2 memory_cost is out of range (m >= 8 * p, m <= 2^32 - 1)",
      Self::InvalidParallelism => "Argon2 parallelism must be in 1..=2^24-1",
      Self::InvalidOutputLen => "Argon2 output length must be in 4..=2^32-1",
      Self::SaltTooShort => "Argon2 salt must be at least 8 bytes",
      Self::SaltTooLong => "Argon2 salt exceeds 2^32-1 bytes",
      Self::PasswordTooLong => "Argon2 password exceeds 2^32-1 bytes",
      Self::SecretTooLong => "Argon2 secret exceeds 2^32-1 bytes",
      Self::AssociatedDataTooLong => "Argon2 associated data exceeds 2^32-1 bytes",
      Self::EntropyUnavailable => "Argon2 entropy source unavailable",
    })
  }
}

impl core::error::Error for Argon2Error {}

// ─── Parameters ─────────────────────────────────────────────────────────────

/// Validated Argon2 cost-parameter set.
///
/// Constructed via [`Argon2Params::new`] and the setters; call
/// [`Argon2Params::build`] to validate and produce a `Result<Argon2Params,
/// Argon2Error>`. The output length is enforced by validators but is the
/// same for every hash produced with this parameter set.
///
/// # Examples
///
/// ```rust
/// use rscrypto::{Argon2Params, Argon2id};
///
/// let params = Argon2Params::new()
///   .time_cost(3)
///   .memory_cost_kib(65_536)
///   .parallelism(4)
///   .output_len(32)
///   .build()
///   .expect("valid params");
///
/// let mut out = [0u8; 32];
/// Argon2id::hash(&params, b"password", b"random-salt-1234", &mut out).unwrap();
/// ```
#[derive(Clone)]
pub struct Argon2Params {
  time_cost: u32,
  memory_cost_kib: u32,
  parallelism: u32,
  output_len: u32,
  version: Argon2Version,
  secret: Vec<u8>,
  associated_data: Vec<u8>,
}

impl fmt::Debug for Argon2Params {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    f.debug_struct("Argon2Params")
      .field("time_cost", &self.time_cost)
      .field("memory_cost_kib", &self.memory_cost_kib)
      .field("parallelism", &self.parallelism)
      .field("output_len", &self.output_len)
      .field("version", &self.version)
      .field("secret_len", &self.secret.len())
      .field("associated_data_len", &self.associated_data.len())
      .finish()
  }
}

impl Drop for Argon2Params {
  fn drop(&mut self) {
    if !self.secret.is_empty() {
      ct::zeroize(&mut self.secret);
    }
    if !self.associated_data.is_empty() {
      ct::zeroize(&mut self.associated_data);
    }
  }
}

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

impl Argon2Params {
  /// Create a new parameter builder pre-populated with OWASP 2024 defaults
  /// (m = 19 MiB, t = 2, p = 1, output = 32 bytes, v = 1.3). Call the setters
  /// to override, then [`Argon2Params::build`] to validate.
  #[must_use]
  pub fn new() -> Self {
    Self {
      time_cost: DEFAULT_TIME_COST,
      memory_cost_kib: DEFAULT_MEMORY_KIB,
      parallelism: DEFAULT_PARALLELISM,
      output_len: DEFAULT_OUTPUT_LEN as u32,
      version: Argon2Version::V0x13,
      secret: Vec::new(),
      associated_data: Vec::new(),
    }
  }

  /// Set the time cost (iterations). Must be ≥ 1.
  #[must_use]
  pub const fn time_cost(mut self, t: u32) -> Self {
    self.time_cost = t;
    self
  }

  /// Set the memory cost in KiB. Must satisfy `m ≥ 8 · p`.
  #[must_use]
  pub const fn memory_cost_kib(mut self, m: u32) -> Self {
    self.memory_cost_kib = m;
    self
  }

  /// Set the parallelism (number of lanes). Must be 1..=2^24-1.
  #[must_use]
  pub const fn parallelism(mut self, p: u32) -> Self {
    self.parallelism = p;
    self
  }

  /// Set the output tag length in bytes. Must be 4..=2^32-1.
  #[must_use]
  pub const fn output_len(mut self, t: u32) -> Self {
    self.output_len = t;
    self
  }

  /// Set the algorithm version.
  #[must_use]
  pub const fn version(mut self, v: Argon2Version) -> Self {
    self.version = v;
    self
  }

  /// Set the optional secret (pepper). Empty disables pepper.
  #[must_use]
  pub fn secret(mut self, secret: &[u8]) -> Self {
    ct::zeroize(&mut self.secret);
    self.secret.clear();
    self.secret.extend_from_slice(secret);
    self
  }

  /// Set the optional associated data. Empty means "none".
  #[must_use]
  pub fn associated_data(mut self, data: &[u8]) -> Self {
    ct::zeroize(&mut self.associated_data);
    self.associated_data.clear();
    self.associated_data.extend_from_slice(data);
    self
  }

  /// Validate every field against RFC 9106 bounds and return the finalised
  /// parameter set.
  pub fn build(self) -> Result<Self, Argon2Error> {
    self.validate()?;
    Ok(self)
  }

  /// Run validation without consuming the builder — returns the first error.
  pub fn validate(&self) -> Result<(), Argon2Error> {
    if self.time_cost < 1 {
      return Err(Argon2Error::InvalidTimeCost);
    }
    if self.parallelism < 1 || self.parallelism > (1 << 24) - 1 {
      return Err(Argon2Error::InvalidParallelism);
    }
    // RFC 9106 §3.1: m >= 8 * p, m <= 2^32 - 1.
    let min_memory = self.parallelism.checked_mul(8).ok_or(Argon2Error::InvalidMemoryCost)?;
    if self.memory_cost_kib < min_memory {
      return Err(Argon2Error::InvalidMemoryCost);
    }
    if (self.output_len as usize) < MIN_OUTPUT_LEN {
      return Err(Argon2Error::InvalidOutputLen);
    }
    if self.secret.len() as u64 > MAX_VAR_BYTES {
      return Err(Argon2Error::SecretTooLong);
    }
    if self.associated_data.len() as u64 > MAX_VAR_BYTES {
      return Err(Argon2Error::AssociatedDataTooLong);
    }
    Ok(())
  }

  /// Common length checks for `password` / `salt` shared by all three variants.
  fn check_inputs(&self, password: &[u8], salt: &[u8]) -> Result<(), Argon2Error> {
    if password.len() as u64 > MAX_VAR_BYTES {
      return Err(Argon2Error::PasswordTooLong);
    }
    if salt.len() < MIN_SALT_LEN {
      return Err(Argon2Error::SaltTooShort);
    }
    if salt.len() as u64 > MAX_VAR_BYTES {
      return Err(Argon2Error::SaltTooLong);
    }
    Ok(())
  }

  /// Time cost (iterations).
  #[must_use]
  pub const fn get_time_cost(&self) -> u32 {
    self.time_cost
  }

  /// Memory cost in KiB.
  #[must_use]
  pub const fn get_memory_cost_kib(&self) -> u32 {
    self.memory_cost_kib
  }

  /// Parallelism (lanes).
  #[must_use]
  pub const fn get_parallelism(&self) -> u32 {
    self.parallelism
  }

  /// Output tag length in bytes.
  #[must_use]
  pub const fn get_output_len(&self) -> u32 {
    self.output_len
  }

  /// Algorithm version.
  #[must_use]
  pub const fn get_version(&self) -> Argon2Version {
    self.version
  }
}

/// Operational limits for verifying Argon2 PHC strings from untrusted storage.
///
/// PHC strings encode their own memory, iteration, parallelism, and output
/// lengths. Use `Argon2id::verify_string_with_policy` (or the matching
/// variant method) when those encoded parameters can be controlled by another
/// tenant, database row, network peer, or migration input.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Argon2VerifyPolicy {
  /// Maximum encoded memory cost in KiB.
  pub max_memory_cost_kib: u32,
  /// Maximum encoded time cost.
  pub max_time_cost: u32,
  /// Maximum encoded parallelism.
  pub max_parallelism: u32,
  /// Maximum encoded output length in bytes.
  pub max_output_len: usize,
}

impl Argon2VerifyPolicy {
  /// Build a policy from explicit upper bounds.
  #[must_use]
  pub const fn new(max_memory_cost_kib: u32, max_time_cost: u32, max_parallelism: u32, max_output_len: usize) -> Self {
    Self {
      max_memory_cost_kib,
      max_time_cost,
      max_parallelism,
      max_output_len,
    }
  }

  /// Return `true` when `params` and `output_len` are within this policy.
  #[must_use]
  pub const fn allows(&self, params: &Argon2Params, output_len: usize) -> bool {
    params.memory_cost_kib <= self.max_memory_cost_kib
      && params.time_cost <= self.max_time_cost
      && params.parallelism <= self.max_parallelism
      && output_len <= self.max_output_len
  }
}

impl Default for Argon2VerifyPolicy {
  fn default() -> Self {
    Self::new(
      DEFAULT_MEMORY_KIB,
      DEFAULT_TIME_COST,
      DEFAULT_PARALLELISM,
      DEFAULT_OUTPUT_LEN,
    )
  }
}

// ─── Kernel dispatch ────────────────────────────────────────────────────────
//
// [`KernelId`], [`ALL_KERNELS`], [`required_caps`], and the private
// `active_compress` selector live in the [`dispatch`] submodule; the
// per-arch kernels live in sibling files (`kernels.rs` for portable,
// `aarch64.rs` for NEON, etc.). This module only *calls through* the
// resolved [`CompressFn`] — no inline BlaMka code below this point.

// ─── Diagnostic entry points for per-kernel tests ──────────────────────────

/// The kernel selected by the runtime dispatcher on the current host.
///
/// Useful for reporting and for tests that cross-check the active kernel
/// against the runtime dispatcher.
#[cfg(feature = "diag")]
#[must_use]
pub fn diag_active_kernel() -> KernelId {
  dispatch::active_kernel()
}

/// Hash via the kernel selected by the runtime dispatcher (default path).
///
/// # Errors
///
/// Returns [`Argon2Error`] for invalid parameters per
/// [`Argon2Params::validate`].
#[cfg(feature = "diag")]
pub fn diag_hash_active(
  params: &Argon2Params,
  password: &[u8],
  salt: &[u8],
  variant: Argon2Variant,
  out: &mut [u8],
) -> Result<(), Argon2Error> {
  argon2_hash(params, password, salt, variant, out)
}

/// Hash via the **portable** kernel, regardless of host capabilities.
///
/// # Errors
///
/// Returns [`Argon2Error`] for invalid parameters per
/// [`Argon2Params::validate`].
#[cfg(feature = "diag")]
pub fn diag_hash_portable(
  params: &Argon2Params,
  password: &[u8],
  salt: &[u8],
  variant: Argon2Variant,
  out: &mut [u8],
) -> Result<(), Argon2Error> {
  argon2_hash_with_kernel(
    params,
    password,
    salt,
    variant,
    out,
    dispatch::compress_fn_for(KernelId::Portable),
  )
}

/// Hash via the aarch64 NEON kernel.
///
/// # Errors
///
/// Returns [`Argon2Error`] for invalid parameters.
#[cfg(all(feature = "diag", target_arch = "aarch64"))]
pub fn diag_hash_aarch64_neon(
  params: &Argon2Params,
  password: &[u8],
  salt: &[u8],
  variant: Argon2Variant,
  out: &mut [u8],
) -> Result<(), Argon2Error> {
  argon2_hash_with_kernel(
    params,
    password,
    salt,
    variant,
    out,
    dispatch::compress_fn_for(KernelId::Aarch64Neon),
  )
}

/// Hash via the x86_64 AVX2 kernel.
///
/// # Errors
///
/// Returns [`Argon2Error`] for invalid parameters.
///
/// # Panics
///
/// Panics if the host does not support AVX2. The per-kernel tests
/// gate this call on `crate::platform::caps()` having the kernel's
/// required caps before invoking it.
#[cfg(all(feature = "diag", target_arch = "x86_64"))]
pub fn diag_hash_x86_avx2(
  params: &Argon2Params,
  password: &[u8],
  salt: &[u8],
  variant: Argon2Variant,
  out: &mut [u8],
) -> Result<(), Argon2Error> {
  assert!(
    crate::platform::caps().has(dispatch::required_caps(KernelId::X86Avx2)),
    "AVX2 not available on host"
  );
  argon2_hash_with_kernel(
    params,
    password,
    salt,
    variant,
    out,
    dispatch::compress_fn_for(KernelId::X86Avx2),
  )
}

/// Hash via the x86_64 AVX-512 kernel.
///
/// # Errors
///
/// Returns [`Argon2Error`] for invalid parameters.
///
/// # Panics
///
/// Panics if the host does not support AVX-512F + AVX-512VL.
#[cfg(all(feature = "diag", target_arch = "x86_64"))]
pub fn diag_hash_x86_avx512(
  params: &Argon2Params,
  password: &[u8],
  salt: &[u8],
  variant: Argon2Variant,
  out: &mut [u8],
) -> Result<(), Argon2Error> {
  assert!(
    crate::platform::caps().has(dispatch::required_caps(KernelId::X86Avx512)),
    "AVX-512F + AVX-512VL not available on host"
  );
  argon2_hash_with_kernel(
    params,
    password,
    salt,
    variant,
    out,
    dispatch::compress_fn_for(KernelId::X86Avx512),
  )
}

/// Hash via the powerpc64 VSX kernel.
///
/// # Errors
///
/// Returns [`Argon2Error`] for invalid parameters.
///
/// # Panics
///
/// Panics if the host does not support VSX.
#[cfg(all(feature = "diag", target_arch = "powerpc64"))]
pub fn diag_hash_power_vsx(
  params: &Argon2Params,
  password: &[u8],
  salt: &[u8],
  variant: Argon2Variant,
  out: &mut [u8],
) -> Result<(), Argon2Error> {
  assert!(
    crate::platform::caps().has(dispatch::required_caps(KernelId::PowerVsx)),
    "POWER VSX not available on host"
  );
  argon2_hash_with_kernel(
    params,
    password,
    salt,
    variant,
    out,
    dispatch::compress_fn_for(KernelId::PowerVsx),
  )
}

/// Hash via the s390x z/Vector kernel.
///
/// # Errors
///
/// Returns [`Argon2Error`] for invalid parameters.
///
/// # Panics
///
/// Panics if the host does not support the z13+ vector facility.
#[cfg(all(feature = "diag", target_arch = "s390x"))]
pub fn diag_hash_s390x_vector(
  params: &Argon2Params,
  password: &[u8],
  salt: &[u8],
  variant: Argon2Variant,
  out: &mut [u8],
) -> Result<(), Argon2Error> {
  assert!(
    crate::platform::caps().has(dispatch::required_caps(KernelId::S390xVector)),
    "s390x vector facility not available on host"
  );
  argon2_hash_with_kernel(
    params,
    password,
    salt,
    variant,
    out,
    dispatch::compress_fn_for(KernelId::S390xVector),
  )
}

/// Hash via the riscv64 RVV kernel.
///
/// # Errors
///
/// Returns [`Argon2Error`] for invalid parameters.
///
/// # Panics
///
/// Panics if the host does not support the RISC-V V extension.
#[cfg(all(feature = "diag", target_arch = "riscv64"))]
pub fn diag_hash_riscv64_v(
  params: &Argon2Params,
  password: &[u8],
  salt: &[u8],
  variant: Argon2Variant,
  out: &mut [u8],
) -> Result<(), Argon2Error> {
  assert!(
    crate::platform::caps().has(dispatch::required_caps(KernelId::Riscv64V)),
    "RISC-V V extension not available on host"
  );
  argon2_hash_with_kernel(
    params,
    password,
    salt,
    variant,
    out,
    dispatch::compress_fn_for(KernelId::Riscv64V),
  )
}

/// Hash via the wasm32 simd128 kernel.
///
/// # Errors
///
/// Returns [`Argon2Error`] for invalid parameters.
///
/// # Panics
///
/// Panics if the host does not support wasm SIMD128.
#[cfg(all(feature = "diag", target_arch = "wasm32"))]
pub fn diag_hash_wasm_simd128(
  params: &Argon2Params,
  password: &[u8],
  salt: &[u8],
  variant: Argon2Variant,
  out: &mut [u8],
) -> Result<(), Argon2Error> {
  assert!(
    crate::platform::caps().has(dispatch::required_caps(KernelId::WasmSimd128)),
    "wasm simd128 not available on host"
  );
  argon2_hash_with_kernel(
    params,
    password,
    salt,
    variant,
    out,
    dispatch::compress_fn_for(KernelId::WasmSimd128),
  )
}

/// Single-block compress via the portable kernel (diagnostic).
///
/// Runs one 1 KiB BlaMka compression, bypassing the full hash pipeline.
/// Used by kernel microbenches and cross-kernel differential tests.
#[cfg(feature = "diag")]
pub fn diag_compress_portable(
  dst: &mut [u64; BLOCK_WORDS],
  x: &[u64; BLOCK_WORDS],
  y: &[u64; BLOCK_WORDS],
  xor_into: bool,
) {
  // SAFETY: portable kernel has no unsafe preconditions.
  unsafe { kernels::compress_portable(dst, x, y, xor_into) }
}

/// Single-block compress via the aarch64 NEON kernel (diagnostic).
#[cfg(all(feature = "diag", target_arch = "aarch64"))]
pub fn diag_compress_aarch64_neon(
  dst: &mut [u64; BLOCK_WORDS],
  x: &[u64; BLOCK_WORDS],
  y: &[u64; BLOCK_WORDS],
  xor_into: bool,
) {
  // SAFETY: NEON is baseline on aarch64 — the module gate enforces the
  // target_arch check at compile time.
  unsafe { aarch64::compress_neon(dst, x, y, xor_into) }
}

/// Single-block compress via the x86_64 AVX2 kernel (diagnostic).
///
/// # Panics
///
/// Panics if the host does not support AVX2.
#[cfg(all(feature = "diag", target_arch = "x86_64"))]
pub fn diag_compress_x86_avx2(
  dst: &mut [u64; BLOCK_WORDS],
  x: &[u64; BLOCK_WORDS],
  y: &[u64; BLOCK_WORDS],
  xor_into: bool,
) {
  assert!(
    crate::platform::caps().has(dispatch::required_caps(KernelId::X86Avx2)),
    "AVX2 not available on host"
  );
  // SAFETY: the assertion above witnesses the AVX2 cap on the host before
  // the call, satisfying `compress_avx2`'s `#[target_feature]` precondition.
  unsafe { x86_64::compress_avx2(dst, x, y, xor_into) }
}

/// Single-block compress via the x86_64 AVX-512 kernel (diagnostic).
///
/// # Panics
///
/// Panics if the host does not support AVX-512F + AVX-512VL.
#[cfg(all(feature = "diag", target_arch = "x86_64"))]
pub fn diag_compress_x86_avx512(
  dst: &mut [u64; BLOCK_WORDS],
  x: &[u64; BLOCK_WORDS],
  y: &[u64; BLOCK_WORDS],
  xor_into: bool,
) {
  assert!(
    crate::platform::caps().has(dispatch::required_caps(KernelId::X86Avx512)),
    "AVX-512F + AVX-512VL not available on host"
  );
  // SAFETY: the assertion above witnesses both AVX-512F and AVX-512VL on
  // the host, satisfying `compress_avx512`'s `#[target_feature]`.
  unsafe { x86_64::compress_avx512(dst, x, y, xor_into) }
}

/// Single-block compress via the powerpc64 VSX kernel (diagnostic).
///
/// # Panics
///
/// Panics if the host does not support VSX.
#[cfg(all(feature = "diag", target_arch = "powerpc64"))]
pub fn diag_compress_power_vsx(
  dst: &mut [u64; BLOCK_WORDS],
  x: &[u64; BLOCK_WORDS],
  y: &[u64; BLOCK_WORDS],
  xor_into: bool,
) {
  assert!(
    crate::platform::caps().has(dispatch::required_caps(KernelId::PowerVsx)),
    "POWER VSX not available on host"
  );
  // SAFETY: assertion witnesses VSX on the host.
  unsafe { power::compress_vsx(dst, x, y, xor_into) }
}

/// Single-block compress via the s390x z/Vector kernel (diagnostic).
///
/// # Panics
///
/// Panics if the host does not support the z13+ vector facility.
#[cfg(all(feature = "diag", target_arch = "s390x"))]
pub fn diag_compress_s390x_vector(
  dst: &mut [u64; BLOCK_WORDS],
  x: &[u64; BLOCK_WORDS],
  y: &[u64; BLOCK_WORDS],
  xor_into: bool,
) {
  assert!(
    crate::platform::caps().has(dispatch::required_caps(KernelId::S390xVector)),
    "s390x vector facility not available on host"
  );
  // SAFETY: assertion witnesses the vector facility on the host.
  unsafe { s390x::compress_vector(dst, x, y, xor_into) }
}

/// Single-block compress via the riscv64 RVV kernel (diagnostic).
///
/// # Panics
///
/// Panics if the host does not support the RISC-V V extension.
#[cfg(all(feature = "diag", target_arch = "riscv64"))]
pub fn diag_compress_riscv64_v(
  dst: &mut [u64; BLOCK_WORDS],
  x: &[u64; BLOCK_WORDS],
  y: &[u64; BLOCK_WORDS],
  xor_into: bool,
) {
  assert!(
    crate::platform::caps().has(dispatch::required_caps(KernelId::Riscv64V)),
    "RISC-V V extension not available on host"
  );
  // SAFETY: assertion witnesses the V extension on the host.
  unsafe { riscv64::compress_rvv(dst, x, y, xor_into) }
}

/// Single-block compress via the wasm32 simd128 kernel (diagnostic).
///
/// # Panics
///
/// Panics if the host does not support wasm SIMD128.
#[cfg(all(feature = "diag", target_arch = "wasm32"))]
pub fn diag_compress_wasm_simd128(
  dst: &mut [u64; BLOCK_WORDS],
  x: &[u64; BLOCK_WORDS],
  y: &[u64; BLOCK_WORDS],
  xor_into: bool,
) {
  assert!(
    crate::platform::caps().has(dispatch::required_caps(KernelId::WasmSimd128)),
    "wasm simd128 not available on host"
  );
  // SAFETY: assertion witnesses simd128 availability on the host.
  unsafe { wasm::compress_simd128(dst, x, y, xor_into) }
}

/// Block-word count (128) — exposed for diagnostic kernel tests.
#[cfg(feature = "diag")]
pub const DIAG_BLOCK_WORDS: usize = BLOCK_WORDS;

// ─── H' variable-length Blake2b helper (RFC 9106 §3.3) ──────────────────────

/// Compute `H'(input, out.len())` per RFC 9106 §3.3 into `out`.
///
/// `out.len()` may be any positive value; for `≤ 64` this is a single
/// Blake2b call, and for larger outputs it becomes a chained 64→32-byte
/// extraction with a tail Blake2b of the remaining bytes.
fn h_prime(input_parts: &[&[u8]], out: &mut [u8]) {
  let out_len = out.len();
  assert!(out_len > 0, "H' output length must be positive");
  // Feed LE32(out_len) then the input parts into Blake2b. For out_len <= 64
  // the single-block output is the answer directly.
  let len_le = u32::try_from(out_len)
    .unwrap_or_else(|_| unreachable!("Argon2 H' out_len <= u32::MAX, validated by Argon2Params::validate"))
    .to_le_bytes();

  if out_len <= 64 {
    let mut hasher =
      Blake2b::new(u8::try_from(out_len).unwrap_or_else(|_| unreachable!("guarded by `out_len <= 64` above")));
    hasher.update(&len_le);
    for part in input_parts {
      hasher.update(part);
    }
    hasher.finalize_into(out);
    return;
  }

  // out_len > 64: chained 64-byte outputs, copying the first 32 bytes of
  // each V_i into `out`, with a final V_{r+1} of length (out_len − 32·r).
  // r = ceil(out_len/32) - 2
  let r = (out_len.strict_add(31) / 32).strict_sub(2);
  // V_1 = Blake2b(LE32(T) || X, 64)
  let mut v_prev: [u8; 64] = {
    let mut hasher = Blake2b512::new();
    hasher.update(&len_le);
    for part in input_parts {
      hasher.update(part);
    }
    hasher.finalize()
  };
  out[..32].copy_from_slice(&v_prev[..32]);

  // V_i = Blake2b(V_{i-1}, 64) for i = 2..=r
  for i in 1..r {
    let v_next = Blake2b512::digest(&v_prev);
    out[i.strict_mul(32)..i.strict_mul(32).strict_add(32)].copy_from_slice(&v_next[..32]);
    v_prev = v_next;
  }

  // V_{r+1} = Blake2b(V_r, out_len − 32·r)
  let tail_off = r.strict_mul(32);
  let tail_len = out_len.strict_sub(tail_off);
  let mut hasher = Blake2b::new(
    u8::try_from(tail_len)
      .unwrap_or_else(|_| unreachable!("tail_len ∈ (32, 64] by construction (out_len > 64, r = ⌈out_len/32⌉ - 2)")),
  );
  hasher.update(&v_prev);
  hasher.finalize_into(&mut out[tail_off..]);

  ct::zeroize(&mut v_prev);
}

// ─── H0 initialisation (RFC 9106 §3.2) ──────────────────────────────────────

/// Compute the 64-byte `H0` seed.
fn compute_h0(params: &Argon2Params, password: &[u8], salt: &[u8], variant: Argon2Variant) -> [u8; 64] {
  // All input lengths have been bounded ≤ MAX_VAR_BYTES (= u32::MAX) by
  // `check_inputs` before reaching here, so the `try_from` calls below are
  // infallible. We use `try_from + expect` rather than `as u32` to keep the
  // invariant readable at the call site.
  let len_u32 = |label: &'static str, len: usize| -> [u8; 4] {
    u32::try_from(len)
      .unwrap_or_else(|_| panic!("Argon2 H0: {label} length exceeded MAX_VAR_BYTES; check_inputs should have rejected"))
      .to_le_bytes()
  };

  let mut hasher = Blake2b512::new();
  hasher.update(&params.parallelism.to_le_bytes());
  hasher.update(&params.output_len.to_le_bytes());
  hasher.update(&params.memory_cost_kib.to_le_bytes());
  hasher.update(&params.time_cost.to_le_bytes());
  hasher.update(&params.version.as_u32().to_le_bytes());
  hasher.update(&variant.y().to_le_bytes());
  hasher.update(&len_u32("password", password.len()));
  hasher.update(password);
  hasher.update(&len_u32("salt", salt.len()));
  hasher.update(salt);
  hasher.update(&len_u32("secret", params.secret.len()));
  hasher.update(&params.secret);
  hasher.update(&len_u32("associated_data", params.associated_data.len()));
  hasher.update(&params.associated_data);
  hasher.finalize()
}

// ─── Block conversion ───────────────────────────────────────────────────────

#[inline(always)]
fn block_from_bytes(bytes: &[u8; BLOCK_SIZE]) -> MemoryBlock {
  let mut out = MemoryBlock::zero();
  for i in 0..BLOCK_WORDS {
    out.0[i] = u64::from_le_bytes(
      bytes[i.strict_mul(8)..i.strict_mul(8).strict_add(8)]
        .try_into()
        .unwrap(),
    );
  }
  out
}

#[inline(always)]
fn block_to_bytes(block: &[u64; BLOCK_WORDS]) -> [u8; BLOCK_SIZE] {
  let mut out = [0u8; BLOCK_SIZE];
  for i in 0..BLOCK_WORDS {
    out[i.strict_mul(8)..i.strict_mul(8).strict_add(8)].copy_from_slice(&block[i].to_le_bytes());
  }
  out
}

// ─── Argon2i pseudo-random address stream (RFC 9106 §3.4.2) ────────────────

/// Buffer of 128 `J1||J2` word pairs used for a single segment of Argon2i
/// (or Argon2id's data-independent slices).
#[derive(Clone)]
struct AddressBlock {
  words: MemoryBlock,
}

impl AddressBlock {
  fn zeros() -> Self {
    Self {
      words: MemoryBlock::zero(),
    }
  }

  /// Generate a fresh address block keyed by
  /// `(pass, lane, slice, blocks, total_passes, variant_y, counter)`.
  #[allow(clippy::too_many_arguments)] // RFC 9106 §3.4.2 fixes this list; wrapping it in a struct is empty ceremony.
  fn refresh(
    &mut self,
    compress: CompressFn,
    pass: u32,
    lane: u32,
    slice: u32,
    blocks: u32,
    total_passes: u32,
    variant_y: u32,
    counter: u64,
  ) {
    let mut input = MemoryBlock::zero();
    input.0[0] = pass as u64;
    input.0[1] = lane as u64;
    input.0[2] = slice as u64;
    input.0[3] = blocks as u64;
    input.0[4] = total_passes as u64;
    input.0[5] = variant_y as u64;
    input.0[6] = counter;

    let mut zero = MemoryBlock::zero();
    let mut intermediate = MemoryBlock::zero();
    // SAFETY: the CompressFn came from `active_compress()` (or
    // `compress_fn_for` in per-kernel tests), which only returns a
    // kernel whose `required_caps` are a subset of the host's caps.
    unsafe {
      compress(&mut intermediate.0, &zero.0, &input.0, /* xor_into = */ false);
      compress(&mut self.words.0, &zero.0, &intermediate.0, /* xor_into = */ false);
    }
    zeroize_u64_slice_no_fence(&mut zero.0);
    zeroize_u64_slice_no_fence(&mut intermediate.0);
    zeroize_u64_slice_no_fence(&mut input.0);
    core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
  }
}

// ─── Fill engine ───────────────────────────────────────────────────────────

/// Layout helper. A fresh Argon2 hash call allocates `m' × p` contiguous
/// u64 blocks; rows correspond to lanes, columns to positions within a lane.
struct Matrix {
  blocks: Vec<MemoryBlock>,
  lane_len: u32,
  lanes: u32,
  segment_len: u32,
}

impl Matrix {
  fn new(mem_kib: u32, lanes: u32) -> Result<Self, Argon2Error> {
    // m' = 4 · p · ⌊m / (4p)⌋
    let four_p = lanes.strict_mul(SYNC_POINTS);
    let m_prime = mem_kib / four_p * four_p;
    let lane_len = m_prime / lanes;
    let segment_len = lane_len / SYNC_POINTS;
    let total = m_prime as usize;
    let mut blocks = Vec::new();
    blocks
      .try_reserve_exact(total)
      .map_err(|_| Argon2Error::InvalidMemoryCost)?;
    blocks.resize(total, MemoryBlock::zero());
    Ok(Self {
      blocks,
      lane_len,
      lanes,
      segment_len,
    })
  }

  #[inline(always)]
  fn len(&self) -> usize {
    self.blocks.len()
  }

  #[inline(always)]
  fn index(&self, lane: u32, col: u32) -> usize {
    (lane as usize)
      .strict_mul(self.lane_len as usize)
      .strict_add(col as usize)
  }

  #[inline(always)]
  fn get(&self, lane: u32, col: u32) -> &[u64; BLOCK_WORDS] {
    let idx = self.index(lane, col);
    self.get_index(idx)
  }

  #[inline(always)]
  fn get_index(&self, idx: usize) -> &[u64; BLOCK_WORDS] {
    &self.blocks[idx].0
  }

  #[inline(always)]
  fn set(&mut self, lane: u32, col: u32, block: MemoryBlock) {
    let idx = self.index(lane, col);
    self.blocks[idx] = block;
  }
}

impl Drop for Matrix {
  fn drop(&mut self) {
    for block in &mut self.blocks {
      zeroize_u64_slice_no_fence(&mut block.0);
    }
    core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst);
  }
}

/// Pointer-based view of the memory matrix shared between sequential and
/// parallel fill paths.
///
/// Carries no Rust-level borrow of the underlying [`Matrix`]; ownership /
/// aliasing discipline is enforced by the callers:
///
/// - **Sequential path** (`fill_segment`): the view is constructed under an exclusive `&mut Matrix`
///   borrow, the matrix is single-threaded for the call's duration, and the view is dropped before
///   control returns to `argon2_hash`. Exclusivity is trivial.
/// - **Parallel path** (`fill_slice_parallel`): the view is shared across `rayon::scope` tasks.
///   Disjointness across tasks is upheld by the Argon2 reference-index function (RFC 9106 §3.4),
///   which guarantees that within a single slice every lane writes only to its own segment range
///   and reads only from blocks in already-completed slices (or strictly earlier columns of the
///   same segment, which are this task's own writes).
///
/// `Send + Sync` are required to cross rayon thread boundaries; both are
/// `unsafe impl`d on the assumption that callers honour the discipline
/// above. The methods on this view are themselves `unsafe` for the same
/// reason — the runtime, not the type system, owns the soundness proof.
#[derive(Clone, Copy)]
struct MatrixView {
  ptr: *mut MemoryBlock,
  total_len: usize,
}

#[cfg(feature = "parallel")]
// SAFETY: `MatrixView` is `Send + Sync` only because the parallel filler
// (`fill_slice_parallel`) honours the RFC 9106 §3.4 disjointness discipline:
//   1. Within one slice, lane `i` writes exclusively to its own segment and reads only from (a)
//      blocks in already-completed slices, or (b) earlier columns of the in-progress segment which
//      the same task wrote.
//   2. `rayon::scope` synchronises all per-lane tasks before advancing to the next slice — no
//      read-write race can cross slice boundaries.
//   3. Sequential callers (`fill_segment`) hold an exclusive `&mut Matrix` borrow for the duration
//      of the call and never split the view.
// All `block`/`block_mut` methods are `unsafe`; the type system does not
// enforce the proof, the runtime does.
unsafe impl Send for MatrixView {}
#[cfg(feature = "parallel")]
// SAFETY: see the Send impl above — same disjointness argument.
unsafe impl Sync for MatrixView {}

impl MatrixView {
  #[inline(always)]
  fn from_blocks(blocks: &mut [MemoryBlock]) -> Self {
    Self {
      ptr: blocks.as_mut_ptr(),
      total_len: blocks.len(),
    }
  }

  /// Borrow the block at flat index `idx` immutably.
  ///
  /// # Safety
  ///
  /// `idx < self.total_len`, and no concurrent task may be mutating the
  /// block at `idx` while the returned reference is in use. The lifetime
  /// `'a` is unconstrained at the type level — the caller is responsible
  /// for keeping the underlying `Matrix` alive and aliasing-free.
  #[inline(always)]
  unsafe fn block<'a>(self, idx: usize) -> &'a [u64; BLOCK_WORDS] {
    debug_assert!(idx < self.total_len);
    // SAFETY: caller guarantees `idx` is in bounds, no concurrent write,
    // and that the `Matrix` outlives the returned borrow.
    unsafe { &(*self.ptr.add(idx)).0 }
  }

  /// Borrow the block at flat index `idx` mutably.
  ///
  /// # Safety
  ///
  /// `idx < self.total_len`, and the block at `idx` must be exclusively
  /// owned by the calling task — no other task (or this task) may hold a
  /// concurrent immutable or mutable reference to the same index.
  #[inline(always)]
  unsafe fn block_mut<'a>(self, idx: usize) -> &'a mut [u64; BLOCK_WORDS] {
    debug_assert!(idx < self.total_len);
    // SAFETY: caller guarantees `idx` is in bounds, exclusive access, and
    // that the `Matrix` outlives the returned borrow.
    unsafe { &mut (*self.ptr.add(idx)).0 }
  }
}

/// Compute the reference block `(ref_lane, ref_index)` for a position
/// `(lane, col)` in pass `pass`, using `j1`/`j2` pseudo-random words.
#[allow(clippy::too_many_arguments)] // RFC 9106 §3.4 ties eight fields together; a ctx struct would just forward them all.
#[inline(always)]
fn reference_index(
  pass: u32,
  lane: u32,
  slice: u32,
  col: u32,
  j1: u32,
  j2: u32,
  lanes: u32,
  segment_len: u32,
  lane_len: u32,
) -> (u32, u32) {
  // Reference lane
  let ref_lane = if pass == 0 && slice == 0 { lane } else { j2 % lanes };

  // Reference area size
  let same_lane = ref_lane == lane;
  let position_in_segment = col.wrapping_sub(slice.wrapping_mul(segment_len));
  // We know position_in_segment < segment_len (by construction).
  let area_size: u32 = if pass == 0 {
    // First pass: previous slices in the current lane are available.
    if same_lane {
      // Blocks 0..col − 1 available (col excludes position itself).
      // col = slice·segment_len + position_in_segment; position > 0 or col > 0.
      // Reference area = col − 1
      col.wrapping_sub(1)
    } else {
      // Other lane: blocks in slices 0..slice completed, minus 1 if position
      // in current segment == 0 (prevents self-reference in racing lane).
      let completed_slices = slice.wrapping_mul(segment_len);
      if position_in_segment == 0 {
        completed_slices.wrapping_sub(1)
      } else {
        completed_slices
      }
    }
  } else {
    // Subsequent passes: wrap the full lane, excluding current segment's
    // already-computed portion for same-lane references.
    if same_lane {
      // lane_len − segment_len + position_in_segment − 1
      lane_len
        .wrapping_sub(segment_len)
        .wrapping_add(position_in_segment)
        .wrapping_sub(1)
    } else {
      // lane_len − segment_len, minus 1 if position_in_segment == 0
      let base = lane_len.wrapping_sub(segment_len);
      if position_in_segment == 0 {
        base.wrapping_sub(1)
      } else {
        base
      }
    }
  };

  // Map j1 to a reference slot using the φ function (RFC 9106 §3.4.1.2).
  let j1_u64 = j1 as u64;
  let relative_position = {
    let x = (j1_u64.wrapping_mul(j1_u64)) >> 32;
    let y = (area_size as u64).wrapping_mul(x) >> 32;
    (area_size as u64).wrapping_sub(1).wrapping_sub(y) as u32
  };

  // Absolute start position of the reference area (wraps across the lane).
  let start_position = if pass == 0 || slice == (SYNC_POINTS - 1) {
    0
  } else {
    (slice.wrapping_add(1)).wrapping_mul(segment_len)
  };

  let ref_index = (start_position.wrapping_add(relative_position)) % lane_len;

  (ref_lane, ref_index)
}

/// Fill a single segment (lane × slice) of the matrix.
///
/// Safe wrapper: takes an exclusive `&mut Matrix`, constructs a
/// [`MatrixView`] from the underlying block storage, and dispatches into
/// [`fill_segment_inner`]. The exclusive borrow makes the inner kernel's
/// safety contract trivially satisfied for single-threaded callers.
#[allow(clippy::too_many_arguments)] // RFC 9106 §3.4 fixes this list; struct wrapper is empty ceremony.
fn fill_segment(
  matrix: &mut Matrix,
  compress: CompressFn,
  pass: u32,
  lane: u32,
  slice: u32,
  variant: Argon2Variant,
  version: Argon2Version,
  time_cost: u32,
) {
  let lanes = matrix.lanes;
  let segment_len = matrix.segment_len;
  let lane_len = matrix.lane_len;
  let total_blocks = matrix.len() as u32;
  let view = MatrixView::from_blocks(&mut matrix.blocks);
  // SAFETY: `&mut matrix` is held exclusively for the duration of the
  // call. The view is the only handle to the matrix's storage during
  // `fill_segment_inner`; aliasing and concurrency contracts are
  // trivially upheld.
  unsafe {
    fill_segment_inner(
      view,
      compress,
      pass,
      lane,
      slice,
      lanes,
      segment_len,
      lane_len,
      total_blocks,
      variant,
      version,
      time_cost,
    );
  }
}

/// Fill a single segment via a [`MatrixView`].
///
/// Common kernel for the sequential and parallel fill paths. The body is
/// identical to the legacy `fill_segment(&mut Matrix, ...)`; the only
/// change is that block reads/writes go through [`MatrixView::block`] /
/// [`MatrixView::block_mut`] rather than through `&mut Matrix`.
///
/// # Safety
///
/// Callers must guarantee that, for the indices touched by this call:
///
/// - The current segment range `[lane * lane_len + slice * segment_len .. lane * lane_len + (slice
///   + 1) * segment_len]` is exclusively writeable by this task (no other task may read or write
///   within this range while this call runs).
/// - All read indices are stable: blocks at any flat index outside this task's segment range must
///   not be mutated by any other task while this call runs.
///
/// Both conditions are upheld by:
/// - The sequential path's exclusive `&mut Matrix` borrow, OR
/// - The Argon2 reference-index function (RFC 9106 §3.4) when called from `fill_slice_parallel` —
///   see the doc-comment on [`MatrixView`] for the disjointness argument.
#[allow(clippy::too_many_arguments, clippy::doc_lazy_continuation)]
unsafe fn fill_segment_inner(
  view: MatrixView,
  compress: CompressFn,
  pass: u32,
  lane: u32,
  slice: u32,
  lanes: u32,
  segment_len: u32,
  lane_len: u32,
  total_blocks: u32,
  variant: Argon2Variant,
  version: Argon2Version,
  time_cost: u32,
) {
  let variant_y = variant.y();
  let lane_len_usize = lane_len as usize;
  let lane_base = (lane as usize).strict_mul(lane_len_usize);

  // Determine if this segment is data-independent:
  //   Argon2i: always
  //   Argon2id: pass == 0 and slice in 0..2
  //   Argon2d: never
  let is_independent = match variant {
    Argon2Variant::Argon2i => true,
    Argon2Variant::Argon2id => pass == 0 && slice < 2,
    Argon2Variant::Argon2d => false,
  };

  let mut address_block = AddressBlock::zeros();
  let mut address_counter: u64 = 0;
  if is_independent {
    address_counter = 1; // RFC 9106: counter starts at 1 for first block.
    address_block.refresh(
      compress,
      pass,
      lane,
      slice,
      total_blocks,
      time_cost,
      variant_y,
      address_counter,
    );
  }

  // Starting column for this segment. Skip first two blocks of lane 0 on
  // pass 0 — they're computed during initialisation.
  let starting_col: u32 = if pass == 0 && slice == 0 { 2 } else { 0 };
  let segment_start = slice.strict_mul(segment_len);
  let first_col = segment_start.strict_add(starting_col);

  let mut prev_idx = if first_col == 0 {
    lane_base.strict_add(lane_len_usize.strict_sub(1))
  } else {
    lane_base.strict_add(first_col.strict_sub(1) as usize)
  };

  for seg_col in starting_col..segment_len {
    let col = segment_start.strict_add(seg_col);
    let cur_idx = lane_base.strict_add(col as usize);

    // Determine J1, J2
    let (j1, j2) = if is_independent {
      let addr_pos = (seg_col as usize) % BLOCK_WORDS;
      if addr_pos == 0 && seg_col != 0 {
        address_counter = address_counter.strict_add(1);
        address_block.refresh(
          compress,
          pass,
          lane,
          slice,
          total_blocks,
          time_cost,
          variant_y,
          address_counter,
        );
      }
      let word = address_block.words.0[addr_pos];
      ((word & 0xFFFF_FFFFu64) as u32, (word >> 32) as u32)
    } else {
      // Data-dependent: J1, J2 from the previous block.
      // SAFETY: `prev_idx` is either `lane_base + first_col - 1` or the
      // previous iteration's `cur_idx`. Both are within this task's lane
      // (so within either the segment range or this lane's already-
      // completed columns); no concurrent writer touches them.
      let prev_block = unsafe { view.block(prev_idx) };
      let word = prev_block[0];
      ((word & 0xFFFF_FFFFu64) as u32, (word >> 32) as u32)
    };

    let (ref_lane, ref_index) = reference_index(pass, lane, slice, col, j1, j2, lanes, segment_len, lane_len);

    // Compute new block: G(B[lane][prev], B[ref_lane][ref_index]), optionally
    // XOR-accumulated into existing block (v1.3, pass > 0).
    let xor_into = pass > 0 && version == Argon2Version::V0x13;
    let ref_idx = (ref_lane as usize)
      .strict_mul(lane_len_usize)
      .strict_add(ref_index as usize);
    // SAFETY:
    // - `prev_idx` is either the previous iteration's `cur_idx` or (on the segment boundary) `lane_base
    //   + lane_len - 1` — both within this task's own lane, never concurrently written.
    // - `(ref_lane, ref_index)` per RFC 9106 §3.4 lives in a completed region, never within the current
    //   slice's still-being-written range. Not concurrently written.
    // - `cur_idx = lane_base + col` is in this task's exclusive segment write range per
    //   `fill_segment_inner`'s safety contract.
    // - `prev_idx` and `cur_idx` differ by design (`cur_idx` is the block being written this iteration,
    //   `prev_idx` is one position earlier or wrapped around to the lane's last block). `ref_idx` is in
    //   a completed region outside the current segment, never the write target. The three block
    //   locations are pairwise disjoint.
    // - `compress` was resolved by `active_compress`, so its `required_caps` are a subset of the host's
    //   caps.
    unsafe {
      let prev_block = view.block(prev_idx);
      let ref_block = view.block(ref_idx);
      let dst = view.block_mut(cur_idx);
      compress(dst, prev_block, ref_block, xor_into);
    }

    prev_idx = cur_idx;
  }
}

// ─── Slice driver (sequential / parallel) ──────────────────────────────────

/// Drive one slice's worth of segment fills.
///
/// Sequential: one segment per lane, in order. Always available.
#[inline]
fn fill_slice_sequential(
  matrix: &mut Matrix,
  compress: CompressFn,
  pass: u32,
  slice: u32,
  variant: Argon2Variant,
  version: Argon2Version,
  time_cost: u32,
) {
  let lanes = matrix.lanes;
  for lane in 0..lanes {
    fill_segment(matrix, compress, pass, lane, slice, variant, version, time_cost);
  }
}

/// Drive one slice's segment fills in parallel: one rayon task per lane.
///
/// Within a slice, every lane writes only to its own segment range and
/// reads only from already-completed regions (RFC 9106 §3.4). The shared
/// [`MatrixView`] therefore carries no aliasing hazard across the
/// `rayon::scope` tasks — see the `MatrixView` and `fill_segment_inner`
/// doc-comments for the full safety argument.
///
/// `rayon::scope` joins all spawned tasks before returning, so the
/// `&mut Matrix` borrow is released only after every lane has finished
/// the slice. The next slice (or finalisation) sees a fully-completed
/// snapshot.
#[cfg(feature = "parallel")]
fn fill_slice_parallel(
  matrix: &mut Matrix,
  compress: CompressFn,
  pass: u32,
  slice: u32,
  variant: Argon2Variant,
  version: Argon2Version,
  time_cost: u32,
) {
  let lanes = matrix.lanes;
  let segment_len = matrix.segment_len;
  let lane_len = matrix.lane_len;
  let total_blocks = matrix.len() as u32;
  let view = MatrixView::from_blocks(&mut matrix.blocks);

  // Spawn lanes 1..lanes onto rayon workers; run lane 0 inline on the
  // calling thread. This is the standard Blake3 / parallel-tree-reduction
  // pattern: it saves one task dispatch per slice (n=8 slices × t passes
  // × 1 task ≈ measurable when each fill_segment is short), and ensures
  // the calling thread participates as a worker rather than purely
  // blocking on `scope()`.
  rayon::scope(|s| {
    for lane in 1..lanes {
      s.spawn(move |_| {
        // SAFETY: `view` is shared across all spawned tasks. Each task
        // is responsible for exactly one lane's segment within the
        // current slice. Per RFC 9106 §3.4:
        //
        // - Lane `lane`'s write range is `[lane*lane_len + slice*segment_len .. lane*lane_len +
        //   (slice+1)*segment_len]`. This range is pairwise disjoint across lanes, so concurrent tasks
        //   never write the same index.
        // - Cross-lane reads (the `(ref_lane, ref_index)` fetch in `fill_segment_inner`) only target blocks
        //   in completed slices — never the current slice's still-being-written range. Same-lane reads of
        //   the previous column are within this task's own write range, accessed sequentially.
        //
        // No two tasks ever access the same block when at least one is
        // a writer. The Send/Sync impls on `MatrixView` are sound under
        // this discipline; see the `MatrixView` doc-comment.
        unsafe {
          fill_segment_inner(
            view,
            compress,
            pass,
            lane,
            slice,
            lanes,
            segment_len,
            lane_len,
            total_blocks,
            variant,
            version,
            time_cost,
          );
        }
      });
    }

    // SAFETY: lane 0's segment range is exclusively writeable by this
    // call (no other spawned task touches lane 0). The same disjointness
    // argument used for the spawned tasks applies — see above.
    unsafe {
      fill_segment_inner(
        view,
        compress,
        pass,
        0,
        slice,
        lanes,
        segment_len,
        lane_len,
        total_blocks,
        variant,
        version,
        time_cost,
      );
    }
  });
}

/// Dispatch one slice fill: parallel when `parallel` is on and `lanes > 1`,
/// sequential otherwise. The `lanes == 1` fast-path skips rayon entirely
/// — no thread dispatch, no `Send`/`Sync` machinery — which keeps the
/// single-lane (OWASP-default) configuration free of any parallel
/// overhead.
#[inline]
fn fill_slice(
  matrix: &mut Matrix,
  compress: CompressFn,
  pass: u32,
  slice: u32,
  variant: Argon2Variant,
  version: Argon2Version,
  time_cost: u32,
) {
  #[cfg(feature = "parallel")]
  if matrix.lanes > 1 {
    fill_slice_parallel(matrix, compress, pass, slice, variant, version, time_cost);
    return;
  }
  fill_slice_sequential(matrix, compress, pass, slice, variant, version, time_cost);
}

// ─── Full Argon2 hash function ─────────────────────────────────────────────

fn argon2_hash(
  params: &Argon2Params,
  password: &[u8],
  salt: &[u8],
  variant: Argon2Variant,
  out: &mut [u8],
) -> Result<(), Argon2Error> {
  argon2_hash_with_kernel(params, password, salt, variant, out, active_compress())
}

fn argon2_hash_with_kernel(
  params: &Argon2Params,
  password: &[u8],
  salt: &[u8],
  variant: Argon2Variant,
  out: &mut [u8],
  compress: CompressFn,
) -> Result<(), Argon2Error> {
  params.validate()?;
  params.check_inputs(password, salt)?;
  if out.len() < MIN_OUTPUT_LEN || out.len() as u64 > MAX_VAR_BYTES || out.len() != params.output_len as usize {
    return Err(Argon2Error::InvalidOutputLen);
  }

  // Compute H0.
  let mut h0 = compute_h0(params, password, salt, variant);

  // Allocate the memory matrix.
  let mut matrix = Matrix::new(params.memory_cost_kib, params.parallelism)?;
  let lane_len = matrix.lane_len;
  let lanes = matrix.lanes;

  // Initialise first two blocks per lane.
  for lane in 0..lanes {
    let mut buf = [0u8; BLOCK_SIZE];
    // B[lane][0] = H'(H0 || LE32(0) || LE32(lane), BLOCK_SIZE)
    let lane_le = lane.to_le_bytes();
    h_prime(&[&h0, &0u32.to_le_bytes(), &lane_le], &mut buf);
    matrix.set(lane, 0, block_from_bytes(&buf));

    // B[lane][1] = H'(H0 || LE32(1) || LE32(lane), BLOCK_SIZE)
    h_prime(&[&h0, &1u32.to_le_bytes(), &lane_le], &mut buf);
    matrix.set(lane, 1, block_from_bytes(&buf));
    ct::zeroize(&mut buf);
  }

  // Main fill loop: pass × slice × (lanes synchronised at slice boundaries).
  // Lane parallelism within a slice is gated on the `parallel` feature and
  // skipped when `parallelism == 1` to avoid rayon overhead.
  for pass in 0..params.time_cost {
    for slice in 0..SYNC_POINTS {
      fill_slice(
        &mut matrix,
        compress,
        pass,
        slice,
        variant,
        params.version,
        params.time_cost,
      );
    }
  }

  // Finalisation: C = XOR of last block of each lane; output = H'(C, T).
  let mut acc = *matrix.get(0, lane_len - 1);
  for lane in 1..lanes {
    let blk = matrix.get(lane, lane_len - 1);
    for i in 0..BLOCK_WORDS {
      acc[i] ^= blk[i];
    }
  }
  let mut acc_bytes = block_to_bytes(&acc);
  h_prime(&[&acc_bytes], out);

  // Wipe scratch
  ct::zeroize(&mut h0);
  zeroize_u64_slice(&mut acc);
  ct::zeroize(&mut acc_bytes);
  // Matrix zeroized via Drop
  Ok(())
}

// ─── Public typed hashers ───────────────────────────────────────────────────

macro_rules! define_argon2_variant {
  (
    $(#[$meta:meta])*
    $name:ident { variant: $variant:expr, phc_algorithm: $phc_alg:literal }
  ) => {
    $(#[$meta])*
    #[derive(Debug, Clone, Copy, Default)]
    pub struct $name;

    impl $name {
      /// Algorithm identifier used in PHC strings and diagnostics (e.g.
      /// `"argon2d"`, `"argon2i"`, `"argon2id"`).
      pub const ALGORITHM: &'static str = $phc_alg;

      /// Hash `password` with `salt` into `out`.
      ///
      /// # Errors
      ///
      /// Returns [`Argon2Error`] if parameters are out of range, the salt is
      /// too short, or `out.len()` does not match `params.output_len`.
      pub fn hash(
        params: &Argon2Params,
        password: &[u8],
        salt: &[u8],
        out: &mut [u8],
      ) -> Result<(), Argon2Error> {
        argon2_hash(params, password, salt, $variant, out)
      }

      /// Hash `password` with `salt` into a fixed-size array.
      ///
      /// # Errors
      ///
      /// Returns [`Argon2Error`] if `N != params.output_len`.
      pub fn hash_array<const N: usize>(
        params: &Argon2Params,
        password: &[u8],
        salt: &[u8],
      ) -> Result<[u8; N], Argon2Error> {
        let mut out = [0u8; N];
        Self::hash(params, password, salt, &mut out)?;
        Ok(out)
      }

      /// Verify `expected` against a freshly-computed hash in constant time.
      ///
      /// The Argon2 hash always runs, even when `expected.len()` does not
      /// match `params.output_len`. The length check is folded into the
      /// final boolean, not an early return, so wall-clock cost does not
      /// depend on whether the supplied tag has the right length —
      /// `params.output_len` is not leaked through timing.
      ///
      /// # Errors
      ///
      /// Returns an opaque [`VerificationError`] on any mismatch, malformed
      /// input, or parameter error.
      #[must_use = "password verification must be checked; a dropped Result silently accepts the wrong password"]
      pub fn verify(
        params: &Argon2Params,
        password: &[u8],
        salt: &[u8],
        expected: &[u8],
      ) -> Result<(), VerificationError> {
        // Always allocate and hash to `params.output_len` regardless of
        // `expected.len()`. The dominant cost (Argon2 fill) is constant; the
        // residual byte-compare cost difference (≤ tens of ns) is dwarfed by
        // the hash itself (≥ milliseconds at any realistic cost knobs).
        let actual_len = params.output_len as usize;
        let mut actual = alloc::vec![0u8; actual_len];
        let hash_failed = Self::hash(params, password, salt, &mut actual).is_err();

        // `constant_time_eq` returns false for length mismatches; combined
        // with the always-run hash above, length-vs-bytes cases collapse
        // into a single failure path.
        let bytes_match = ct::constant_time_eq(&actual, expected);
        ct::zeroize(&mut actual);

        let success = !hash_failed & bytes_match;
        if core::hint::black_box(success) {
          Ok(())
        } else {
          Err(VerificationError::new())
        }
      }

      /// Hash `password` with `salt` and encode the result as a PHC string.
      ///
      /// Emits `$argon2{d|i|id}$v=<ver>$m=<m>,t=<t>,p=<p>$<salt>$<hash>`
      /// (standard RFC 4648 base64, no padding). `params.secret` and
      /// `params.associated_data` are honoured during hashing but are **not**
      /// embedded in the PHC string, per the PHC spec — callers holding
      /// secrets must re-supply them when verifying.
      ///
      /// # Errors
      ///
      /// Propagates any [`Argon2Error`] from parameter validation or input
      /// length checks.
      #[cfg(feature = "phc-strings")]
      pub fn hash_string_with_salt(
        params: &Argon2Params,
        password: &[u8],
        salt: &[u8],
      ) -> Result<alloc::string::String, Argon2Error> {
        let mut hash = alloc::vec![0u8; params.output_len as usize];
        Self::hash(params, password, salt, &mut hash)?;
        let encoded = phc_integration::encode_string(Self::ALGORITHM, params, salt, &hash);
        ct::zeroize(&mut hash);
        Ok(encoded)
      }

      /// Hash `password` with a fresh 16-byte salt from the operating
      /// system CSPRNG and encode the result as a PHC string.
      ///
      /// # Errors
      ///
      /// Propagates any [`Argon2Error`] from parameter validation or input
      /// length checks. Returns [`Argon2Error::EntropyUnavailable`] if the
      /// platform entropy source fails.
      #[cfg(all(feature = "phc-strings", feature = "getrandom"))]
      pub fn hash_string(
        params: &Argon2Params,
        password: &[u8],
      ) -> Result<alloc::string::String, Argon2Error> {
        let mut salt = [0u8; 16];
        getrandom::fill(&mut salt).map_err(|_| Argon2Error::EntropyUnavailable)?;
        Self::hash_string_with_salt(params, password, &salt)
      }

      /// Verify `password` against a PHC-encoded hash in constant time.
      ///
      /// Parses the encoded string, rebuilds the cost parameters, re-hashes
      /// `password` with the embedded salt, and compares in constant time.
      /// Works only when the encoded string's algorithm matches `Self` — a
      /// PHC string emitted by another variant is rejected opaquely.
      ///
      /// # Errors
      ///
      /// Returns [`VerificationError`] on any mismatch, malformed string,
      /// variant mismatch, or parameter error. Errors are intentionally
      /// opaque — callers needing to distinguish parse failures should use
      /// the lower-level `decode_string` helper.
      #[cfg(feature = "phc-strings")]
      #[must_use = "password verification must be checked; a dropped Result silently accepts the wrong password"]
      pub fn verify_string(
        password: &[u8],
        encoded: &str,
      ) -> Result<(), VerificationError> {
        Self::verify_string_with_context(password, encoded, &[], &[])
      }

      /// Verify `password` against a PHC string after enforcing operational
      /// bounds on its encoded cost parameters.
      ///
      /// # Errors
      ///
      /// Returns [`VerificationError`] on any mismatch, malformed string,
      /// variant mismatch, parameter error, or policy violation.
      #[cfg(feature = "phc-strings")]
      #[must_use = "password verification must be checked; a dropped Result silently accepts the wrong password"]
      pub fn verify_string_with_policy(
        password: &[u8],
        encoded: &str,
        policy: &Argon2VerifyPolicy,
      ) -> Result<(), VerificationError> {
        Self::verify_string_with_context_and_policy(password, encoded, &[], &[], policy)
      }

      /// Verify `password` against a PHC-encoded hash using an external
      /// pepper and/or associated data.
      ///
      /// PHC strings carry the algorithm, cost parameters, salt, and hash.
      /// They do not carry secret pepper material. This helper is the
      /// verification counterpart for PHC strings produced from
      /// [`Argon2Params::secret`] or [`Argon2Params::associated_data`].
      ///
      /// # Errors
      ///
      /// Returns [`VerificationError`] on any mismatch, malformed string,
      /// variant mismatch, or parameter error.
      #[cfg(feature = "phc-strings")]
      #[must_use = "password verification must be checked; a dropped Result silently accepts the wrong password"]
      pub fn verify_string_with_context(
        password: &[u8],
        encoded: &str,
        secret: &[u8],
        associated_data: &[u8],
      ) -> Result<(), VerificationError> {
        Self::verify_string_with_context_and_policy(
          password,
          encoded,
          secret,
          associated_data,
          &Argon2VerifyPolicy::new(u32::MAX, u32::MAX, u32::MAX, usize::MAX),
        )
      }

      /// Verify `password` against a PHC string with external context and
      /// explicit operational bounds.
      ///
      /// # Errors
      ///
      /// Returns [`VerificationError`] on any mismatch, malformed string,
      /// variant mismatch, parameter error, or policy violation.
      #[cfg(feature = "phc-strings")]
      #[must_use = "password verification must be checked; a dropped Result silently accepts the wrong password"]
      pub fn verify_string_with_context_and_policy(
        password: &[u8],
        encoded: &str,
        secret: &[u8],
        associated_data: &[u8],
        policy: &Argon2VerifyPolicy,
      ) -> Result<(), VerificationError> {
        let parsed = phc_integration::decode_string(encoded, Self::ALGORITHM)
          .map_err(|_| VerificationError::new())?;
        let mut params = parsed.params;
        if !secret.is_empty() {
          params = params.secret(secret);
        }
        if !associated_data.is_empty() {
          params = params.associated_data(associated_data);
        }
        params = params.build().map_err(|_| VerificationError::new())?;
        if !policy.allows(&params, parsed.hash.len()) {
          return Err(VerificationError::new());
        }
        let mut actual = alloc::vec![0u8; parsed.hash.len()];
        if Self::hash(&params, password, &parsed.salt, &mut actual).is_err() {
          ct::zeroize(&mut actual);
          return Err(VerificationError::new());
        }
        let ok = ct::constant_time_eq(&actual, &parsed.hash);
        ct::zeroize(&mut actual);
        if core::hint::black_box(ok) {
          Ok(())
        } else {
          Err(VerificationError::new())
        }
      }

      /// Decode a PHC string without re-hashing.
      ///
      /// Returns the parsed cost parameters, salt, and reference hash. Use
      /// when you need programmatic access to the encoded components —
      /// e.g. to re-hash with a new salt or compare multiple candidates.
      ///
      /// # Errors
      ///
      /// Returns [`crate::auth::phc::PhcError`] on any parse failure, or if
      /// the encoded algorithm does not match `Self::ALGORITHM`.
      #[cfg(feature = "phc-strings")]
      pub fn decode_string(
        encoded: &str,
      ) -> Result<(Argon2Params, alloc::vec::Vec<u8>, alloc::vec::Vec<u8>), crate::auth::phc::PhcError> {
        let parsed = phc_integration::decode_string(encoded, Self::ALGORITHM)?;
        Ok((parsed.params, parsed.salt, parsed.hash))
      }
    }
  };
}

define_argon2_variant! {
  /// Argon2d — data-dependent indexing variant of Argon2 (RFC 9106).
  ///
  /// Fastest of the three variants but vulnerable to side-channel timing
  /// attacks if an adversary can observe memory accesses. Use only in
  /// trusted-hardware or non-interactive settings (e.g. cryptocurrency PoW).
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rscrypto::{Argon2Params, Argon2d};
  /// let params = Argon2Params::new().memory_cost_kib(32).time_cost(3).parallelism(4).output_len(32).build().unwrap();
  /// let mut h = [0u8; 32];
  /// Argon2d::hash(&params, &[0x01; 32], &[0x02; 16], &mut h).unwrap();
  /// ```
  Argon2d { variant: Argon2Variant::Argon2d, phc_algorithm: "argon2d" }
}

define_argon2_variant! {
  /// Argon2i — data-independent indexing variant of Argon2 (RFC 9106).
  ///
  /// Side-channel resistant. Slower than Argon2d and Argon2id; prefer
  /// Argon2id for password hashing unless you specifically need data-
  /// independent access patterns throughout.
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rscrypto::{Argon2Params, Argon2i};
  /// let params = Argon2Params::new().memory_cost_kib(32).time_cost(3).parallelism(4).output_len(32).build().unwrap();
  /// let mut h = [0u8; 32];
  /// Argon2i::hash(&params, &[0x01; 32], &[0x02; 16], &mut h).unwrap();
  /// ```
  Argon2i { variant: Argon2Variant::Argon2i, phc_algorithm: "argon2i" }
}

define_argon2_variant! {
  /// Argon2id — hybrid variant of Argon2 (RFC 9106). **Recommended default.**
  ///
  /// Runs Argon2i (data-independent) for the first half of the first pass
  /// and Argon2d (data-dependent) for the rest. OWASP 2024 recommends this
  /// variant for password hashing.
  ///
  /// # Examples
  ///
  /// ```rust
  /// use rscrypto::{Argon2Params, Argon2id};
  /// let params = Argon2Params::new().memory_cost_kib(32).time_cost(3).parallelism(4).output_len(32).build().unwrap();
  /// let mut h = [0u8; 32];
  /// Argon2id::hash(&params, &[0x01; 32], &[0x02; 16], &mut h).unwrap();
  /// ```
  Argon2id { variant: Argon2Variant::Argon2id, phc_algorithm: "argon2id" }
}

// ─── PHC string integration (feature: phc-strings) ─────────────────────────

#[cfg(feature = "phc-strings")]
mod phc_integration {
  use alloc::{string::String, vec::Vec};

  use super::{Argon2Params, Argon2Version};
  use crate::auth::phc::{self, PhcError};

  /// Parsed PHC components reconstituted into rscrypto types.
  pub(super) struct ParsedPhc {
    pub params: Argon2Params,
    pub salt: Vec<u8>,
    pub hash: Vec<u8>,
  }

  /// Build `$argon2{d|i|id}$v=<ver>$m=<m>,t=<t>,p=<p>$<salt>$<hash>`.
  pub(super) fn encode_string(algorithm: &str, params: &Argon2Params, salt: &[u8], hash: &[u8]) -> String {
    let mut out = String::new();
    out.push('$');
    out.push_str(algorithm);
    out.push_str("$v=");
    phc::push_u32_decimal(&mut out, params.get_version().as_u32());
    out.push_str("$m=");
    phc::push_u32_decimal(&mut out, params.get_memory_cost_kib());
    out.push_str(",t=");
    phc::push_u32_decimal(&mut out, params.get_time_cost());
    out.push_str(",p=");
    phc::push_u32_decimal(&mut out, params.get_parallelism());
    out.push('$');
    phc::base64_encode_into(salt, &mut out);
    out.push('$');
    phc::base64_encode_into(hash, &mut out);
    out
  }

  /// Parse a PHC string and reconstitute `(params, salt, hash)`.
  pub(super) fn decode_string(encoded: &str, expected_algorithm: &str) -> Result<ParsedPhc, PhcError> {
    let parts = phc::parse(encoded)?;
    if parts.algorithm != expected_algorithm {
      return Err(PhcError::AlgorithmMismatch);
    }

    // Parse version. Required for Argon2 (RFC 9106 recommends v=19).
    let version = match parts.version {
      Some(v) => match phc::parse_param_u32(v)? {
        0x10 => Argon2Version::V0x10,
        0x13 => Argon2Version::V0x13,
        _ => return Err(PhcError::UnsupportedVersion),
      },
      None => Argon2Version::V0x13, // Permissive default: some encoders omit v=.
    };

    // Parse the params segment: exactly {m, t, p}, each exactly once, in any order.
    let mut memory_kib: Option<u32> = None;
    let mut time_cost: Option<u32> = None;
    let mut parallelism: Option<u32> = None;
    for pair in phc::PhcParamIter::new(parts.parameters) {
      let (k, v) = pair?;
      let value = phc::parse_param_u32(v)?;
      match k {
        "m" => {
          if memory_kib.replace(value).is_some() {
            return Err(PhcError::DuplicateParam);
          }
        }
        "t" => {
          if time_cost.replace(value).is_some() {
            return Err(PhcError::DuplicateParam);
          }
        }
        "p" => {
          if parallelism.replace(value).is_some() {
            return Err(PhcError::DuplicateParam);
          }
        }
        _ => return Err(PhcError::UnknownParam),
      }
    }
    let m = memory_kib.ok_or(PhcError::MissingParam)?;
    let t = time_cost.ok_or(PhcError::MissingParam)?;
    let p = parallelism.ok_or(PhcError::MissingParam)?;

    // Decode salt and hash.
    let salt = phc::decode_base64_to_vec(parts.salt_b64)?;
    let hash = phc::decode_base64_to_vec(parts.hash_b64)?;

    if salt.len() < super::MIN_SALT_LEN || hash.len() < super::MIN_OUTPUT_LEN {
      return Err(PhcError::InvalidLength);
    }

    // Build params with the extracted cost knobs; output_len equals the
    // decoded hash length so verify's length check passes.
    let params = Argon2Params::new()
      .memory_cost_kib(m)
      .time_cost(t)
      .parallelism(p)
      .output_len(hash.len() as u32)
      .version(version);
    let params = params.build().map_err(|_| PhcError::ParamOutOfRange)?;

    Ok(ParsedPhc { params, salt, hash })
  }
}

// ─── Tests ──────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
  #[cfg(not(miri))]
  use alloc::vec;

  use super::*;

  // RFC 9106 Appendix A test vectors (version 0x13, m=32, t=3, p=4, T=32).
  #[cfg(not(miri))]
  const PASSWORD: &[u8] = &[0x01u8; 32];
  #[cfg(not(miri))]
  const SALT: &[u8] = &[0x02u8; 16];
  #[cfg(not(miri))]
  const SECRET: &[u8] = &[0x03u8; 8];
  #[cfg(not(miri))]
  const AD: &[u8] = &[0x04u8; 12];

  #[cfg(not(miri))]
  fn canon_params() -> Argon2Params {
    Argon2Params::new()
      .memory_cost_kib(32)
      .time_cost(3)
      .parallelism(4)
      .output_len(32)
      .version(Argon2Version::V0x13)
      .secret(SECRET)
      .associated_data(AD)
      .build()
      .unwrap()
  }

  // ── RFC 9106 Appendix A ────────────────────────────────────────────────

  #[test]
  #[cfg(not(miri))]
  fn argon2d_rfc_appendix_a_vector() {
    let expected: [u8; 32] = [
      0x51, 0x2b, 0x39, 0x1b, 0x6f, 0x11, 0x62, 0x97, 0x53, 0x71, 0xd3, 0x09, 0x19, 0x73, 0x42, 0x94, 0xf8, 0x68, 0xe3,
      0xbe, 0x39, 0x84, 0xf3, 0xc1, 0xa1, 0x3a, 0x4d, 0xb9, 0xfa, 0xbe, 0x4a, 0xcb,
    ];
    let mut out = [0u8; 32];
    Argon2d::hash(&canon_params(), PASSWORD, SALT, &mut out).unwrap();
    assert_eq!(out, expected);
  }

  #[test]
  #[cfg(not(miri))]
  fn argon2i_rfc_appendix_a_vector() {
    let expected: [u8; 32] = [
      0xc8, 0x14, 0xd9, 0xd1, 0xdc, 0x7f, 0x37, 0xaa, 0x13, 0xf0, 0xd7, 0x7f, 0x24, 0x94, 0xbd, 0xa1, 0xc8, 0xde, 0x6b,
      0x01, 0x6d, 0xd3, 0x88, 0xd2, 0x99, 0x52, 0xa4, 0xc4, 0x67, 0x2b, 0x6c, 0xe8,
    ];
    let mut out = [0u8; 32];
    Argon2i::hash(&canon_params(), PASSWORD, SALT, &mut out).unwrap();
    assert_eq!(out, expected);
  }

  #[test]
  #[cfg(not(miri))]
  fn argon2id_rfc_appendix_a_vector() {
    let expected: [u8; 32] = [
      0x0d, 0x64, 0x0d, 0xf5, 0x8d, 0x78, 0x76, 0x6c, 0x08, 0xc0, 0x37, 0xa3, 0x4a, 0x8b, 0x53, 0xc9, 0xd0, 0x1e, 0xf0,
      0x45, 0x2d, 0x75, 0xb6, 0x5e, 0xb5, 0x25, 0x20, 0xe9, 0x6b, 0x01, 0xe6, 0x59,
    ];
    let mut out = [0u8; 32];
    Argon2id::hash(&canon_params(), PASSWORD, SALT, &mut out).unwrap();
    assert_eq!(out, expected);
  }

  // ── Verify ─────────────────────────────────────────────────────────────

  #[test]
  #[cfg(not(miri))]
  fn argon2id_verify_accepts_correct() {
    let params = canon_params();
    let h = Argon2id::hash_array::<32>(&params, PASSWORD, SALT).unwrap();
    assert!(Argon2id::verify(&params, PASSWORD, SALT, &h).is_ok());
  }

  #[test]
  #[cfg(not(miri))]
  fn argon2id_verify_rejects_wrong_password() {
    let params = canon_params();
    let h = Argon2id::hash_array::<32>(&params, PASSWORD, SALT).unwrap();
    assert!(Argon2id::verify(&params, b"wrong_password_wrong_wrong!!wrong", SALT, &h).is_err());
  }

  #[test]
  #[cfg(not(miri))]
  fn argon2id_verify_rejects_wrong_salt() {
    let params = canon_params();
    let h = Argon2id::hash_array::<32>(&params, PASSWORD, SALT).unwrap();
    let wrong_salt = [0xffu8; 16];
    assert!(Argon2id::verify(&params, PASSWORD, &wrong_salt, &h).is_err());
  }

  #[cfg(all(feature = "phc-strings", feature = "getrandom"))]
  #[test]
  #[cfg(not(miri))]
  fn hash_string_uses_random_salt_and_verifies() {
    let params = Argon2Params::new()
      .memory_cost_kib(32)
      .time_cost(1)
      .parallelism(1)
      .output_len(32)
      .build()
      .unwrap();

    let encoded = Argon2id::hash_string(&params, b"password").unwrap();
    assert!(Argon2id::verify_string(b"password", &encoded).is_ok());
    assert!(Argon2id::verify_string(b"wrong-password", &encoded).is_err());
  }

  // ── Errors ─────────────────────────────────────────────────────────────

  #[test]
  fn invalid_params_fail_build() {
    assert_eq!(
      Argon2Params::new().time_cost(0).build().unwrap_err(),
      Argon2Error::InvalidTimeCost
    );
    assert_eq!(
      Argon2Params::new().parallelism(0).build().unwrap_err(),
      Argon2Error::InvalidParallelism
    );
    assert_eq!(
      Argon2Params::new()
        .parallelism(4)
        .memory_cost_kib(16) // < 8 * p
        .build()
        .unwrap_err(),
      Argon2Error::InvalidMemoryCost
    );
    assert_eq!(
      Argon2Params::new().output_len(3).build().unwrap_err(),
      Argon2Error::InvalidOutputLen
    );
  }

  #[test]
  fn short_salt_rejected() {
    let params = Argon2Params::new().memory_cost_kib(32).parallelism(4).build().unwrap();
    let mut out = [0u8; 32];
    assert_eq!(
      Argon2id::hash(&params, b"pw", &[0u8; 7], &mut out).unwrap_err(),
      Argon2Error::SaltTooShort
    );
  }

  #[test]
  fn output_len_mismatch_rejected() {
    let params = Argon2Params::new().memory_cost_kib(32).parallelism(4).build().unwrap();
    let mut out = [0u8; 16]; // params.output_len is 32
    assert_eq!(
      Argon2id::hash(&params, b"pw", &[0u8; 16], &mut out).unwrap_err(),
      Argon2Error::InvalidOutputLen
    );
  }

  #[test]
  fn error_traits() {
    fn assert_copy<T: Copy>() {}
    fn assert_err<T: core::error::Error>() {}
    assert_copy::<Argon2Error>();
    assert_err::<Argon2Error>();
  }

  // ── Differential: rscrypto vs rustcrypto `argon2` oracle ──────────────

  #[cfg(not(miri))]
  fn oracle_hash(
    algo: argon2::Algorithm,
    password: &[u8],
    salt: &[u8],
    m_kib: u32,
    t: u32,
    p: u32,
    out_len: usize,
  ) -> vec::Vec<u8> {
    let params = argon2::Params::new(m_kib, t, p, Some(out_len)).unwrap();
    let ctx = argon2::Argon2::new(algo, argon2::Version::V0x13, params);
    let mut out = alloc::vec![0u8; out_len];
    ctx.hash_password_into(password, salt, &mut out).unwrap();
    out
  }

  #[test]
  #[cfg(not(miri))]
  fn argon2d_matches_oracle_small_params() {
    let cases: &[(u32, u32, u32, usize)] = &[(8, 1, 1, 16), (16, 2, 1, 32), (32, 3, 2, 32)];
    for &(m, t, p, out_len) in cases {
      let params = Argon2Params::new()
        .memory_cost_kib(m)
        .time_cost(t)
        .parallelism(p)
        .output_len(out_len as u32)
        .build()
        .unwrap();
      let mut actual = alloc::vec![0u8; out_len];
      Argon2d::hash(&params, b"password", &[0u8; 16], &mut actual).unwrap();
      let expected = oracle_hash(argon2::Algorithm::Argon2d, b"password", &[0u8; 16], m, t, p, out_len);
      assert_eq!(actual, expected, "argon2d mismatch m={m} t={t} p={p} T={out_len}");
    }
  }

  #[test]
  #[cfg(not(miri))]
  fn argon2i_matches_oracle_small_params() {
    let cases: &[(u32, u32, u32, usize)] = &[(8, 1, 1, 16), (16, 2, 1, 32), (32, 3, 2, 32)];
    for &(m, t, p, out_len) in cases {
      let params = Argon2Params::new()
        .memory_cost_kib(m)
        .time_cost(t)
        .parallelism(p)
        .output_len(out_len as u32)
        .build()
        .unwrap();
      let mut actual = alloc::vec![0u8; out_len];
      Argon2i::hash(&params, b"password", &[0u8; 16], &mut actual).unwrap();
      let expected = oracle_hash(argon2::Algorithm::Argon2i, b"password", &[0u8; 16], m, t, p, out_len);
      assert_eq!(actual, expected, "argon2i mismatch m={m} t={t} p={p} T={out_len}");
    }
  }

  #[test]
  #[cfg(not(miri))]
  fn argon2id_matches_oracle_small_params() {
    let cases: &[(u32, u32, u32, usize)] = &[(8, 1, 1, 16), (16, 2, 1, 32), (32, 3, 2, 32), (32, 3, 4, 64)];
    for &(m, t, p, out_len) in cases {
      let params = Argon2Params::new()
        .memory_cost_kib(m)
        .time_cost(t)
        .parallelism(p)
        .output_len(out_len as u32)
        .build()
        .unwrap();
      let mut actual = alloc::vec![0u8; out_len];
      Argon2id::hash(&params, b"password", &[0u8; 16], &mut actual).unwrap();
      let expected = oracle_hash(argon2::Algorithm::Argon2id, b"password", &[0u8; 16], m, t, p, out_len);
      assert_eq!(actual, expected, "argon2id mismatch m={m} t={t} p={p} T={out_len}");
    }
  }

  // ── Kernel dispatch plumbing ─────────────────────────────────────────

  #[test]
  fn kernel_id_stringifies() {
    assert_eq!(KernelId::Portable.as_str(), "portable");
  }

  #[test]
  fn portable_kernel_has_no_required_caps() {
    assert!(required_caps(KernelId::Portable).is_empty());
  }

  // ── PHC string integration ───────────────────────────────────────────

  #[cfg(all(feature = "phc-strings", not(miri)))]
  mod phc_tests {
    use alloc::{string::String, vec};

    use super::*;
    use crate::auth::phc::PhcError;

    fn small_params() -> Argon2Params {
      Argon2Params::new()
        .memory_cost_kib(32)
        .time_cost(2)
        .parallelism(1)
        .output_len(32)
        .build()
        .unwrap()
    }

    #[test]
    fn hash_string_with_salt_round_trip_id() {
      let params = small_params();
      let salt = [0xAAu8; 16];
      let encoded = Argon2id::hash_string_with_salt(&params, b"password", &salt).unwrap();
      assert!(encoded.starts_with("$argon2id$v=19$m=32,t=2,p=1$"));
      assert!(Argon2id::verify_string(b"password", &encoded).is_ok());
      assert!(Argon2id::verify_string(b"wrongpassword", &encoded).is_err());
    }

    #[test]
    fn verify_string_with_policy_enforces_argon2_bounds() {
      let params = small_params();
      let salt = [0xA1u8; 16];
      let encoded = Argon2id::hash_string_with_salt(&params, b"password", &salt).unwrap();

      let allowed = Argon2VerifyPolicy::new(32, 2, 1, 32);
      assert!(Argon2id::verify_string_with_policy(b"password", &encoded, &allowed).is_ok());

      let low_memory = Argon2VerifyPolicy::new(31, 2, 1, 32);
      assert!(Argon2id::verify_string_with_policy(b"password", &encoded, &low_memory).is_err());

      let short_output = Argon2VerifyPolicy::new(32, 2, 1, 31);
      assert!(Argon2id::verify_string_with_policy(b"password", &encoded, &short_output).is_err());
    }

    #[test]
    fn verify_string_with_context_and_policy_enforces_argon2_bounds() {
      let params = small_params().secret(b"pepper").build().unwrap();
      let salt = [0xA2u8; 16];
      let encoded = Argon2id::hash_string_with_salt(&params, b"password", &salt).unwrap();

      let allowed = Argon2VerifyPolicy::new(32, 2, 1, 32);
      assert!(Argon2id::verify_string_with_context_and_policy(b"password", &encoded, b"pepper", &[], &allowed).is_ok());

      let low_time = Argon2VerifyPolicy::new(32, 1, 1, 32);
      assert!(
        Argon2id::verify_string_with_context_and_policy(b"password", &encoded, b"pepper", &[], &low_time).is_err()
      );
    }

    #[test]
    fn hash_string_round_trip_d_and_i() {
      let params = small_params();
      let salt = [0xBBu8; 16];
      let encoded_d = Argon2d::hash_string_with_salt(&params, b"pw", &salt).unwrap();
      let encoded_i = Argon2i::hash_string_with_salt(&params, b"pw", &salt).unwrap();
      assert!(encoded_d.starts_with("$argon2d$"));
      assert!(encoded_i.starts_with("$argon2i$"));
      assert!(Argon2d::verify_string(b"pw", &encoded_d).is_ok());
      assert!(Argon2i::verify_string(b"pw", &encoded_i).is_ok());
    }

    #[test]
    fn verify_string_rejects_variant_mismatch() {
      let params = small_params();
      let salt = [0xCCu8; 16];
      let encoded_d = Argon2d::hash_string_with_salt(&params, b"pw", &salt).unwrap();
      // Cross-feed: Argon2id expects its own algorithm label.
      assert!(Argon2id::verify_string(b"pw", &encoded_d).is_err());
      assert!(Argon2i::verify_string(b"pw", &encoded_d).is_err());
    }

    #[test]
    fn decode_string_extracts_params_salt_hash() {
      let params = small_params();
      let salt = vec![0xDDu8; 16];
      let encoded = Argon2id::hash_string_with_salt(&params, b"pw", &salt).unwrap();

      let (decoded_params, decoded_salt, decoded_hash) = Argon2id::decode_string(&encoded).unwrap();
      assert_eq!(decoded_params.get_memory_cost_kib(), 32);
      assert_eq!(decoded_params.get_time_cost(), 2);
      assert_eq!(decoded_params.get_parallelism(), 1);
      assert_eq!(decoded_params.get_output_len(), 32);
      assert_eq!(decoded_params.get_version(), Argon2Version::V0x13);
      assert_eq!(decoded_salt, salt);
      assert_eq!(decoded_hash.len(), 32);

      // Re-hashing with decoded params should give the same hash.
      let mut rehashed = [0u8; 32];
      Argon2id::hash(&decoded_params, b"pw", &decoded_salt, &mut rehashed).unwrap();
      assert_eq!(rehashed.as_slice(), decoded_hash.as_slice());
    }

    #[test]
    fn decode_string_rejects_malformed() {
      assert_eq!(
        Argon2id::decode_string("not a phc string").unwrap_err(),
        PhcError::MalformedInput
      );
      // Short salt (2 bytes of "aa") should fail InvalidLength (< MIN_SALT_LEN = 8).
      assert_eq!(
        Argon2id::decode_string("$argon2id$m=32,t=2,p=1$YWE$aGFzaA").unwrap_err(),
        PhcError::InvalidLength
      );
      // Hash segment with 2 bytes ("aa" → base64 "YWE") is below MIN_OUTPUT_LEN.
      assert_eq!(
        Argon2id::decode_string("$argon2id$m=32,t=2,p=1$QUFBQUFBQUFBQUFBQUFBQQ$YWE").unwrap_err(),
        PhcError::InvalidLength
      );
    }

    #[test]
    fn verify_string_rejects_tampered_hash() {
      let params = small_params();
      let salt = [0xEEu8; 16];
      let encoded = Argon2id::hash_string_with_salt(&params, b"pw", &salt).unwrap();
      // Flip a byte in the hash segment (last character).
      let mut tampered: String = encoded.chars().collect();
      let len = tampered.len();
      tampered.replace_range(len - 1..len, "A"); // the original value was "A" or not — either way the test verifies rejection on corruption
      // The tampering may coincidentally match; try multiple flips.
      let mut matched_tamper = encoded.clone();
      let variants = ["A", "B", "C"];
      let last_char = encoded.chars().last().unwrap();
      for v in variants {
        if v.chars().next().unwrap() != last_char {
          let mut t = encoded.clone();
          let len = t.len();
          t.replace_range(len - 1..len, v);
          matched_tamper = t;
          break;
        }
      }
      assert!(Argon2id::verify_string(b"pw", &matched_tamper).is_err());
    }

    #[test]
    fn decode_string_rejects_duplicate_params() {
      // Encode a hash and munge the params segment to include a duplicate `m=`.
      let params = small_params();
      let encoded = Argon2id::hash_string_with_salt(&params, b"pw", &[0xFFu8; 16]).unwrap();
      let broken = encoded.replace("t=2", "m=99");
      assert_eq!(Argon2id::decode_string(&broken).unwrap_err(), PhcError::DuplicateParam);
    }

    #[test]
    fn decode_string_accepts_missing_version() {
      // Omitting the `$v=19` segment is permissive and defaults to V0x13.
      let params = small_params();
      let salt = [0x77u8; 16];
      let encoded = Argon2id::hash_string_with_salt(&params, b"pw", &salt).unwrap();
      let no_v = encoded.replace("$v=19", "");
      let (p, s, h) = Argon2id::decode_string(&no_v).unwrap();
      assert_eq!(p.get_version(), Argon2Version::V0x13);
      assert_eq!(s, salt);
      assert_eq!(h.len(), 32);
    }

    #[test]
    fn verify_string_with_context_handles_secret_and_associated_data() {
      let params = small_params()
        .secret(b"pepper")
        .associated_data(b"context")
        .build()
        .unwrap();
      let salt = [0x99u8; 16];
      let encoded = Argon2id::hash_string_with_salt(&params, b"pw", &salt).unwrap();

      assert!(Argon2id::verify_string(b"pw", &encoded).is_err());
      assert!(Argon2id::verify_string_with_context(b"pw", &encoded, b"pepper", b"context").is_ok());
      assert!(Argon2id::verify_string_with_context(b"pw", &encoded, b"wrong", b"context").is_err());
      assert!(Argon2id::verify_string_with_context(b"pw", &encoded, b"pepper", b"wrong").is_err());
    }

    #[test]
    fn encoded_format_exact_for_known_vector() {
      // Verify exact string shape with fixed params + salt.
      let params = Argon2Params::new()
        .memory_cost_kib(8)
        .time_cost(1)
        .parallelism(1)
        .output_len(16)
        .version(Argon2Version::V0x13)
        .build()
        .unwrap();
      let salt = b"exampleSALTvalue"; // 16 bytes
      let encoded = Argon2id::hash_string_with_salt(&params, b"password", salt).unwrap();
      // Shape check: exactly 5 segments after leading $.
      let segments: alloc::vec::Vec<&str> = encoded.split('$').collect();
      assert_eq!(segments[0], "");
      assert_eq!(segments[1], "argon2id");
      assert_eq!(segments[2], "v=19");
      assert_eq!(segments[3], "m=8,t=1,p=1");
      // segments[4] is base64 salt (16 bytes → 22 chars)
      assert_eq!(segments[4].len(), 22);
      // segments[5] is base64 hash (16 bytes → 22 chars)
      assert_eq!(segments[5].len(), 22);
      assert_eq!(segments.len(), 6);
    }
  }
}