argon2-rust 0.0.3

Pure-Rust port of the reference Argon2 implementation (phc-winner-argon2), with runtime-dispatched SIMD backends
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
//! The Argon2 driver: pre-hashing, the fill loop, finalisation, and the public
//! [`Argon2`] entry points.
//!
//! Ported line by line from `phc-winner-argon2/src/core.c` (`initial_hash`,
//! `fill_first_blocks`, `initialize`, `index_alpha`, `fill_memory_blocks`,
//! `fill_memory_blocks_st`, `fill_memory_blocks_mt`, `finalize`) and
//! `phc-winner-argon2/src/argon2.c` (`argon2_ctx`, `argon2_hash`,
//! `argon2_verify`, `argon2_verify_ctx`, `argon2_compare`).

use alloc::string::String;
use alloc::vec::Vec;

use crate::blake2b::{Blake2b, blake2b_long};
use crate::block::{Block, Instance, Position};
use crate::error::Error;
use crate::fill_block::{Backend, FillSegmentFn};
use crate::memory::{Arena, Workspace, clear_internal_memory, clear_internal_memory_u64};
use crate::params::{
    Algorithm, BLOCK_SIZE, MAX_PWD_LENGTH, PREHASH_DIGEST_LENGTH, PREHASH_SEED_LENGTH, Params,
    SYNC_POINTS, Version,
};
#[cfg(test)]
use crate::params::{Memory, TagLen};

/// The KAT trace hook: `internal_kat(instance, pass)` from `src/genkat.c`.
///
/// Invoked after every pass with `(pass_index, whole_arena)`, at a point where
/// every helper is parked at the pass boundary and cannot touch the arena.
/// `tests/kat.rs` uses it to dump the arena the way `genkat` does.
pub type PassTrace<'a> = &'a mut dyn FnMut(u32, &[Block]);

// Structural regression hook for the zeroization boundary: stable hashing must
// never ask `hash_in_arena` to copy H0 out of the blockhash it already wipes.
// Thread-local keeps parallel libtest cases from charging one another.
#[cfg(all(test, feature = "std"))]
std::thread_local! {
    static H0_COPY_COUNT: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}

// ---------------------------------------------------------------------------
// index_alpha  (contract: called by every fill_block backend)
// ---------------------------------------------------------------------------

/// `index_alpha()` from `src/core.c`: the absolute index of the reference block.
///
/// (contract) The `fill_block` backends call this once per block.
///
/// # The two traps
///
/// **1. Deliberate wrapping subtraction.** The C writes
///
/// ```c
/// reference_area_size = position->slice * segment_length
///                     + ((position->index == 0) ? (-1) : 0);
/// ```
///
/// `-1` added to a `uint32_t` wraps, so when `slice * segment_length` is 0 the
/// result is `0xFFFFFFFF`, not a negative number. The same shape appears in the
/// `pass > 0` branch:
///
/// ```c
/// reference_area_size = lane_length - segment_length
///                     + ((position->index == 0) ? (-1) : 0);
/// ```
///
/// In Rust these **must** be [`u32::wrapping_sub`] / [`u32::wrapping_add`].
/// A plain `-` panics in debug and silently differs in release. This is the
/// single most common way an Argon2 port breaks, and it only shows up for
/// lane-crossing references at `index == 0` — which means the official
/// single-lane vectors will not catch it. The `p > 1` vectors and KATs will.
///
/// **2. Exact integer widths in the position mapping.** All of this is `u64`:
///
/// ```text
/// let mut rel = pseudo_rand as u64;          // the LOW u32 of the pseudo-random value
/// rel = (rel * rel) >> 32;
/// rel = (ras as u64) - 1 - (((ras as u64) * rel) >> 32);
/// abs = ((start_position as u64 + rel) % lane_length as u64) as u32;
/// ```
///
/// with one refinement the summary above glosses over and the C source settles:
/// `reference_area_size - 1` is evaluated in **`uint32_t`** (both operands are
/// 32-bit; the `int` literal converts to `unsigned int`) and only *then* widened
/// for the outer subtraction. The two readings differ exactly when
/// `reference_area_size == 0`: 32-bit-first gives `0x0000_0000_FFFF_FFFF`,
/// 64-bit-first gives `0xFFFF_FFFF_FFFF_FFFF`. This port does it the C way.
///
/// # Starting position
///
/// ```text
/// start_position = 0;
/// if pass != 0 {
///     start_position = if slice == SYNC_POINTS - 1 { 0 }
///                      else { (slice + 1) * segment_length };
/// }
/// ```
#[must_use]
pub fn index_alpha(
    instance: &Instance,
    position: &Position,
    pseudo_rand: u32,
    same_lane: bool,
) -> u32 {
    // The C ends with `% instance->lane_length`. For any instance built from
    // validated `Params`, `lane_length == segment_length * SYNC_POINTS >= 8`.
    // This guard exists only so a hand-built degenerate `Instance` cannot turn
    // that `%` into a division-by-zero panic; the crate must never panic.
    if instance.lane_length == 0 {
        return 0;
    }

    let reference_area_size: u32 = if position.pass == 0 {
        // First pass.
        if position.slice == 0 {
            // core.c:210-211 `reference_area_size = position->index - 1;`
            // `fill_segment` starts at index 2 on pass 0 / slice 0, so this is
            // >= 1; `wrapping_sub` only keeps the function panic-free.
            position.index.wrapping_sub(1)
        } else if same_lane {
            // core.c:215-217
            //   position->slice * instance->segment_length + position->index - 1
            position
                .slice
                .wrapping_mul(instance.segment_length)
                .wrapping_add(position.index)
                // core.c:217 `+ position->index - 1`, uint32_t arithmetic.
                .wrapping_sub(1)
        } else {
            // core.c:219-221
            //   position->slice * instance->segment_length
            //       + ((position->index == 0) ? (-1) : 0)
            //
            // Adding the `int` -1 to a uint32_t is a DELIBERATE wrapping
            // subtraction. `wrapping_sub(1)` is the same value as
            // `wrapping_add(0xFFFF_FFFF)`; a plain `-` would panic in debug.
            position
                .slice
                .wrapping_mul(instance.segment_length)
                .wrapping_sub(u32::from(position.index == 0))
        }
    } else if same_lane {
        // core.c:227-229
        //   instance->lane_length - instance->segment_length
        //       + position->index - 1
        instance
            .lane_length
            // core.c:227 `lane_length - segment_length`, uint32_t arithmetic.
            .wrapping_sub(instance.segment_length)
            .wrapping_add(position.index)
            // core.c:229 `+ position->index - 1`, uint32_t arithmetic.
            .wrapping_sub(1)
    } else {
        // core.c:231-233
        //   instance->lane_length - instance->segment_length
        //       + ((position->index == 0) ? (-1) : 0)
        instance
            .lane_length
            // core.c:231-232 `lane_length - segment_length`, uint32_t arithmetic.
            .wrapping_sub(instance.segment_length)
            // core.c:233, the same deliberate wrapping subtraction as above.
            .wrapping_sub(u32::from(position.index == 0))
    };

    // core.c:239-242. 1.2.4. Mapping pseudo_rand to 0..<reference_area_size-1>
    // and producing the relative position.
    //
    // Neither multiplication can overflow: `relative_position` is bounded by
    // `u32::MAX` on entry and by `2^32 - 1` after the shift, and both factors of
    // each product are therefore < 2^32.
    let mut relative_position = u64::from(pseudo_rand);
    relative_position = (relative_position * relative_position) >> 32;
    // core.c:241 `reference_area_size - 1` is uint32_t (see the doc comment),
    // hence the 32-bit `wrapping_sub` inside `u64::from(..)`.
    relative_position = u64::from(reference_area_size.wrapping_sub(1))
        // core.c:241-242, the outer subtraction is uint64_t. It cannot underflow
        // — `(ras * rel) >> 32 <= ras - 1` for every `rel < 2^32` — but
        // `wrapping_sub` keeps that a property rather than an assumption.
        .wrapping_sub((u64::from(reference_area_size) * relative_position) >> 32);

    // core.c:245-251. 1.2.5 Computing the starting position.
    let mut start_position: u32 = 0;
    if position.pass != 0 {
        start_position = if position.slice == SYNC_POINTS - 1 {
            0
        } else {
            position
                .slice
                .wrapping_add(1)
                .wrapping_mul(instance.segment_length)
        };
    }

    // core.c:254-255. 1.2.6. Computing the absolute position. `start_position`
    // is uint32_t and `relative_position` uint64_t, so the sum and the `%` are
    // evaluated in uint64_t and only the result is truncated back to uint32_t.
    ((u64::from(start_position).wrapping_add(relative_position)) % u64::from(instance.lane_length))
        as u32
}

// ---------------------------------------------------------------------------
// Pre-hashing and the fill loop
// ---------------------------------------------------------------------------

/// `initial_hash()` from `src/core.c`, returning the 72-byte `H0` buffer.
///
/// BLAKE2b-512 over, in this order, each `u32` little-endian:
/// `lanes`, `outlen`, `m_cost`, `t_cost`, `version`, `type`,
/// then `pwdlen` and `pwd`, `saltlen` and `salt`, `secretlen` and `secret`,
/// `adlen` and `ad`.
///
/// The first 64 bytes of the result are `H0`; the trailing 8 bytes are left
/// zero and are filled in by [`fill_first_blocks`] with the block index and the
/// lane index. (`initialize()` in the C zeroes them explicitly right after
/// calling `initial_hash`; here they are never written in the first place.)
///
/// The four buffer lengths are hashed as `u32`, exactly as the C hashes
/// `context->pwdlen` and friends. A caller that has run
/// [`Params::validate_for`] first — which every entry point in this module
/// does — cannot reach the truncating cast.
///
/// # Errors
///
/// Only a BLAKE2b parameter error, which cannot happen here: the digest length
/// is the constant 64.
pub fn initial_hash(
    algorithm: Algorithm,
    version: Version,
    params: &Params,
    pwd: &[u8],
    salt: &[u8],
    secret: &[u8],
    ad: &[u8],
) -> Result<[u8; PREHASH_SEED_LENGTH], Error> {
    let mut blockhash = [0u8; PREHASH_SEED_LENGTH];
    initial_hash_into(
        algorithm,
        version,
        params,
        pwd,
        salt,
        secret,
        ad,
        &mut blockhash,
    )?;
    Ok(blockhash)
}

/// [`initial_hash`] written directly into its caller's wipe-owned buffer.
///
/// The stable hash path uses this form so returning a 72-byte `Result` cannot
/// make the optimiser leave an intermediate H0 copy behind on the stack.
#[allow(clippy::too_many_arguments)]
fn initial_hash_into(
    algorithm: Algorithm,
    version: Version,
    params: &Params,
    pwd: &[u8],
    salt: &[u8],
    secret: &[u8],
    ad: &[u8],
    blockhash: &mut [u8; PREHASH_SEED_LENGTH],
) -> Result<(), Error> {
    /// `store32(&value, len); blake2b_update(&BlakeHash, &value, 4);`
    #[inline]
    fn le32(len: usize) -> [u8; 4] {
        (len as u32).to_le_bytes()
    }

    // core.c:547 `blake2b_init(&BlakeHash, ARGON2_PREHASH_DIGEST_LENGTH);`
    let mut state = Blake2b::new(PREHASH_DIGEST_LENGTH)?;

    // core.c:549-565. Six u32 parameters, in this exact order.
    state.update(&params.lanes().to_le_bytes());
    state.update(&le32(params.tag_len_bytes()));
    state.update(&params.memory_kib().to_le_bytes());
    state.update(&params.passes().to_le_bytes());
    state.update(&version.as_u32().to_le_bytes());
    state.update(&algorithm.as_u32().to_le_bytes());

    // core.c:567-607. Four length-prefixed buffers, in this exact order.
    // The C guards each `blake2b_update` with a NULL check; feeding an empty
    // slice is the same no-op. `ARGON2_FLAG_CLEAR_PASSWORD` /
    // `ARGON2_FLAG_CLEAR_SECRET` have no analogue here: this port never takes
    // ownership of the caller's buffers, so it cannot wipe them.
    state.update(&le32(pwd.len()));
    state.update(pwd);

    state.update(&le32(salt.len()));
    state.update(salt);

    state.update(&le32(secret.len()));
    state.update(secret);

    state.update(&le32(ad.len()));
    state.update(ad);

    // core.c:609 `blake2b_final(&BlakeHash, blockhash, ARGON2_PREHASH_DIGEST_LENGTH);`
    state.finalize(&mut blockhash[..PREHASH_DIGEST_LENGTH])?;
    Ok(())
}

/// `fill_first_blocks()` from `src/core.c`.
///
/// For each lane `l`, writes `LE32(0)` then `LE32(l)` at
/// `blockhash[64..72]`, expands the 72 bytes to 1024 with `blake2b_long`, and
/// loads that into block `l * lane_length + 0`; then repeats with `LE32(1)` for
/// block `l * lane_length + 1`.
///
/// # Errors
///
/// [`Error::IncorrectParameter`] if `arena` is too small for
/// `lanes * lane_length` blocks — unreachable from this module, which always
/// sizes the arena from the same [`Params`]. Otherwise only a BLAKE2b parameter
/// error, which cannot happen for the constant length 1024.
pub fn fill_first_blocks(
    blockhash: &mut [u8; PREHASH_SEED_LENGTH],
    arena: &mut [Block],
    lanes: u32,
    lane_length: u32,
) -> Result<(), Error> {
    let mut blockhash_bytes = [0u8; BLOCK_SIZE];

    // Unlike the C helper, this safe internal-api entry point reports a short
    // arena instead of indexing unchecked. Keep every fallible exit inside the
    // closure so the derived block bytes are wiped before the error escapes.
    let result = (|| {
        for lane in 0..lanes {
            // core.c:522-523
            //   store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH, 0);
            //   store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH + 4, l);
            blockhash[PREHASH_DIGEST_LENGTH..PREHASH_DIGEST_LENGTH + 4]
                .copy_from_slice(&0u32.to_le_bytes());
            blockhash[PREHASH_DIGEST_LENGTH + 4..PREHASH_SEED_LENGTH]
                .copy_from_slice(&lane.to_le_bytes());

            // core.c:524-525 `blake2b_long(blockhash_bytes, ARGON2_BLOCK_SIZE,
            //                              blockhash, ARGON2_PREHASH_SEED_LENGTH);`
            blake2b_long(&mut blockhash_bytes, blockhash)?;

            // core.c:526-527 `load_block(&instance->memory[l * lane_length + 0], ..)`
            let base = (lane as usize)
                .checked_mul(lane_length as usize)
                .ok_or(Error::IncorrectParameter)?;
            match arena.get_mut(base) {
                Some(block) => block.load_le(&blockhash_bytes),
                None => return Err(Error::IncorrectParameter),
            }

            // core.c:529 `store32(blockhash + ARGON2_PREHASH_DIGEST_LENGTH, 1);`
            blockhash[PREHASH_DIGEST_LENGTH..PREHASH_DIGEST_LENGTH + 4]
                .copy_from_slice(&1u32.to_le_bytes());
            blake2b_long(&mut blockhash_bytes, blockhash)?;

            // core.c:532-533 `load_block(&instance->memory[l * lane_length + 1], ..)`
            let second = base.checked_add(1).ok_or(Error::IncorrectParameter)?;
            match arena.get_mut(second) {
                Some(block) => block.load_le(&blockhash_bytes),
                None => return Err(Error::IncorrectParameter),
            }
        }
        Ok(())
    })();

    // core.c:535 `clear_internal_memory(blockhash_bytes, ARGON2_BLOCK_SIZE);`
    clear_internal_memory(&mut blockhash_bytes);
    result
}

/// `fill_memory_blocks()` from `src/core.c`, with the backend resolved from
/// runtime CPU detection.
///
/// Safe, and the reason it can be: it never lets a caller name the backend. The
/// value comes from [`crate::fill_block::backend`], which only ever returns a
/// backend whose instruction set this CPU was *detected* to have. Every entry
/// point that does take a [`Backend`] is `unsafe` — see
/// [`fill_memory_blocks_traced`].
///
/// # Errors
///
/// [`Error::IncorrectParameter`] if `instance.lanes == 0` (`core.c:377`).
pub fn fill_memory_blocks(instance: &Instance) -> Result<(), Error> {
    // SAFETY: `backend()` is the cached result of the `is_*_feature_detected!`
    // cascade in `fill_block::detect`, so this CPU can execute it. The other two
    // obligations — a valid arena and no concurrent writer — are `Instance`'s
    // own contract, discharged by whoever called `Instance::new`.
    unsafe { fill_memory_blocks_traced(instance, crate::fill_block::backend(), None) }
}

/// `fill_memory_blocks()` with an explicit [`Backend`] and a KAT trace hook.
///
/// The function pointer is resolved **once**, here, before any loop:
///
/// ```text
/// let fill = crate::fill_block::fill_segment_fn(backend);
/// for pass in 0..instance.passes {
///     for slice in 0..SYNC_POINTS {
///         for lane in 0..instance.lanes {
///             fill(instance, Position::new(pass, lane, slice, 0));
///         }
///     }
/// }
/// ```
///
/// Nothing detects or dispatches inside the per-block loop; the cost is one
/// indirect call per *segment*, which is `segment_length` blocks.
///
/// `trace` is `internal_kat()` from `src/genkat.c`: it is invoked after every
/// pass with `(pass_index, whole_arena)`, at a point where every helper is
/// parked at the barrier and cannot touch the arena.
///
/// With the `parallel` feature, `instance.threads > 1` and
/// `instance.lanes > 1`, one
/// [`std::thread::scope`] owns the helpers for the whole fill. They meet at an
/// atomic barrier after each slice, matching the C's sync points without
/// respawning. The single-threaded path holds no raw-pointer sharing at all, so
/// it stays checkable under Miri.
///
/// # Safety
///
/// `backend` selects a `fill_segment` carrying
/// `#[target_feature(enable = ...)]`. Calling one whose feature this CPU lacks
/// is undefined behaviour — in practice `SIGILL` — so the caller must guarantee
/// the CPU can execute it. Either of these discharges that:
///
/// * `backend.is_available()` returned `true`, or
/// * `backend` came from [`crate::fill_block::backend`] / `detect`.
///
/// The one other way to satisfy it is a host *measured* to execute the
/// instructions while hiding them from `cpuid`, which is what the deliberately
/// forced AVX2 tests rely on under Rosetta 2. That is a test-only affordance and
/// never a library path.
///
/// `instance` must also uphold [`Instance::new`]'s contract, and no other thread
/// may be filling this arena.
///
/// # Errors
///
/// [`Error::IncorrectParameter`] if `instance.lanes == 0`.
pub unsafe fn fill_memory_blocks_traced(
    instance: &Instance,
    backend: Backend,
    mut trace: Option<PassTrace<'_>>,
) -> Result<(), Error> {
    // core.c:377-379 `if (instance == NULL || instance->lanes == 0)`.
    if instance.lanes == 0 {
        return Err(Error::IncorrectParameter);
    }

    // Resolve the backend ONCE, before every loop. See `fill_block/mod.rs` for
    // why the `#[target_feature]` boundary sits on `fill_segment`.
    let fill = crate::fill_block::fill_segment_fn(backend);

    // Multi-lane: hand the whole pass/slice/lane nest to the worker pool, which
    // owns its threads for the entire fill instead of for one slice.
    #[cfg(feature = "parallel")]
    if instance.threads > 1 && instance.lanes > 1 {
        // SAFETY: forwarded verbatim from this function's own contract.
        unsafe { fill_pooled(instance, fill, trace) };
        return Ok(());
    }

    for pass in 0..instance.passes {
        for slice in 0..SYNC_POINTS {
            // SAFETY: `fill` is `fill_segment_fn(backend)`, and the caller
            // guarantees this CPU can execute `backend`; `instance` and the
            // absence of a concurrent filler are the caller's obligations too.
            unsafe { fill_slice_st(instance, fill, pass, slice) };
        }

        // genkat.c `internal_kat(instance, r)` — printed after each pass.
        if let Some(callback) = trace.as_mut() {
            // SAFETY: three obligations, and none of them is "the arena is zero".
            //
            //  1. Valid for `memory_len()` `Block`s: that is `Instance::new`'s
            //     own contract, discharged by whoever built `instance`.
            //  2. Every block is *initialised*, which is what makes a `&[Block]`
            //     over them a valid reference. `Arena` guarantees this for its
            //     whole capacity from birth (`alloc_zeroed`) and never gives it
            //     up — initialised memory stays initialised when a `Workspace`
            //     parks and re-lends it. NOTE for the next reader: this used to
            //     be justified by "the arena was zero-initialised by
            //     `Arena::new`". That premise is false for a pooled arena with
            //     `zeroize-memory` off, and it was never the load-bearing one;
            //     *initialised* is.
            //  3. No live `&mut Block`: the parallel path returned above, and
            //     every sequential `fill_slice_st` call has returned before
            //     this shared slice is formed.
            //
            // Separately from soundness, the callback can never observe a
            // previous tenant's bytes even on a reused arena: it fires only
            // after all four slices of a pass have completed, and pass 0 writes
            // every block of every lane exactly once before this point.
            let blocks = unsafe {
                core::slice::from_raw_parts(
                    instance.memory_ptr().cast_const(),
                    instance.memory_len(),
                )
            };
            callback(pass, blocks);
        }
    }

    Ok(())
}

/// `fill_memory_blocks_st()`'s innermost loop (`core.c:265-268`).
///
/// Deliberately free of any cross-thread pointer sharing: the only `unsafe` is
/// the call to `fill`, which every backend requires. That keeps the whole
/// single-threaded path checkable under Miri.
///
/// # Safety
///
/// As [`fill_memory_blocks_traced`]: this CPU must be able to execute whatever
/// instruction set `fill` needs.
unsafe fn fill_slice_st(instance: &Instance, fill: FillSegmentFn, pass: u32, slice: u32) {
    for lane in 0..instance.lanes {
        let position = Position::new(pass, lane, slice, 0);
        // SAFETY: three obligations from `FillSegmentFn`.
        //  1. `fill` came from `fill_segment_fn(backend)`, and this function's
        //     caller guarantees the CPU can execute that backend — an
        //     obligation that is now carried in the type system all the way out
        //     to `fill_memory_blocks_traced`, `hash_traced` and
        //     `hash_with_backend`, every one of which is an `unsafe fn`.
        //  2. `instance`'s arena is valid for `memory_len()` blocks — that is
        //     `Instance::new`'s own safety contract, discharged by the caller.
        //  3. `pass < passes`, `slice < SYNC_POINTS` and `lane < lanes` by
        //     construction, and this thread is the only one running, so nothing
        //     else can be writing this segment.
        unsafe { fill(instance, position) };
    }
}

// ---------------------------------------------------------------------------
// Parallel fill
// ---------------------------------------------------------------------------

/// A `&Instance` that may be handed to another thread.
///
/// `Instance` holds the arena as a `*mut Block`, which makes it neither `Send`
/// nor `Sync`, so sharing it across lanes needs this newtype and the safety
/// argument below.
#[cfg(feature = "parallel")]
#[derive(Clone, Copy)]
struct SharedInstance<'a>(&'a Instance);

// SAFETY: sending a `SharedInstance` hands another thread a `&Instance`, and
// through it the arena's `*mut Block`. That is sound for the one way
// `fill_slice_mt` uses it, and only that way:
//
//  * At each sync point, every worker in the whole-fill `std::thread::scope`
//    runs the SAME `(pass, slice)` and pairwise DISTINCT `lane`s: lanes are
//    claimed from a single `fetch_add` counter, so every index is handed out
//    exactly once.
//  * Within one slice, `fill_segment(instance, {pass, lane, slice, ..})`
//    WRITES only blocks `lane * lane_length + slice * segment_length + i` for
//    `i in 0..segment_length` — precisely the one segment that lane owns in
//    this slice. Two different lanes therefore never write the same block, so
//    no two `&mut Block` ever alias.
//  * It READS `prev_offset`, which always stays inside its own lane (the block
//    it just wrote, or the last block of the lane on the wrap-around), and
//    `ref_lane * lane_length + index_alpha(..)`. When `ref_lane != lane`,
//    `index_alpha`'s reference area is `slice * segment_length` blocks from the
//    start of the lane (pass 0) or the `lane_length - segment_length` blocks of
//    the *other three* slices (pass > 0, `start_position` skips the current
//    one). Either way it never lands in another lane's current segment, which
//    is the only region being written concurrently. So no read races a write.
//  * The scope spans the whole fill. At each slice boundary, every helper
//    publishes its writes with `Release` on `arrived`; the leader acquires them,
//    resets the work counters, then releases the next `generation`, which every
//    helper acquires before proceeding. Thus every write of slice `s`
//    happens-before every read in slice `s + 1`, mirroring the C's sync points.
//  * The `Instance` struct itself is only ever read; the interior mutability is
//    confined to the arena it points at, via `Instance::block_mut`.
//
// `Sync` is deliberately NOT implemented: the workers each take their own copy
// of this `Copy` newtype, so a shared reference to it never crosses a thread.
#[cfg(feature = "parallel")]
unsafe impl Send for SharedInstance<'_> {}

/// The sync point, and the state the workers share across one whole fill.
///
/// # Why this exists instead of one `thread::scope` per slice
///
/// The four sync points per pass are algorithmic and cannot be weakened: every
/// lane must finish slice `N` before any lane starts `N + 1`. What is *not*
/// algorithmic is destroying and recreating the worker set at each of them,
/// which is what `std::thread::scope` per slice did — `passes * 4 *
/// (workers - 1)` thread creations for one hash, each a `clone()` plus a stack
/// mapping. Measured on the target, four lanes over 4096-block segments:
///
/// ```text
///   scope + 3 spawns, per slice        63.8 us
///   live pool on std::sync::Barrier    20.7 us   (Mutex + Condvar: a syscall)
///   live pool on this barrier           0.6 us
/// ```
///
/// The threads now live for the whole fill and meet at a barrier instead. That
/// is a 12-to-3 reduction in thread creations at `t = 1, p = 4`, and 48-to-3 at
/// `t = 4`. The barrier is hand-rolled rather than `std::sync::Barrier` for the
/// 20 us in that table: `Barrier` is a `Mutex` + `Condvar`, so every one of the
/// `4 * passes` sync points is a pair of futex round trips.
///
/// # The barrier, and why the ordering is enough
///
/// Sense-reversing, with the *leader* — the thread that called into the hash —
/// always doing the release. Every helper, having finished its lanes:
///
/// ```text
///   arrived.fetch_add(1, Release)          publishes that helper's writes
///   spin until generation != mine (Acquire)
/// ```
///
/// and the leader, having finished its own lanes:
///
/// ```text
///   spin until arrived + lost >= helpers (Acquire)
///                                            acquires every live helper's writes
///   ... KAT trace here, if any: everyone is parked ...
///   next_lane = 0; arrived = 0
///   generation.store(next, Release)        publishes all of it to everyone
/// ```
///
/// The transitivity is the point. A helper's block writes happen-before its
/// `Release` on `arrived`; the leader's `Acquire` on `arrived` makes them
/// happen-before everything it does next, which includes its `Release` on
/// `generation`; and every *other* helper's `Acquire` on `generation` therefore
/// sees them. So lane 0's slice-`N` writes are visible to lane 3 in slice
/// `N + 1`, which is exactly the guarantee `thread::scope`'s join gave for free.
///
/// # Panics, and why `lost` exists
///
/// A spin barrier turns a worker that never arrives into a hang, and a hang is
/// a far worse failure than a panic. `Bail`'s `Drop` marks a helper lost on the
/// way out of an unwind; the leader stops waiting, sets `stop`, and returns, at
/// which point `thread::scope` joins and re-raises the original panic. Nothing
/// in `fill_segment` is supposed to panic — but "supposed to" is not a
/// scheduling primitive.
#[cfg(feature = "parallel")]
struct FillSync {
    /// Lanes handed out for the current slice. Reset by the leader.
    next_lane: core::sync::atomic::AtomicU32,
    /// Helpers that have finished the current slice.
    arrived: core::sync::atomic::AtomicU32,
    /// Bumped once per sync point; helpers wait for their own count to match.
    generation: core::sync::atomic::AtomicU32,
    /// Helpers that unwound out of the fill and will never arrive again.
    lost: core::sync::atomic::AtomicU32,
    /// Tells parked helpers to give up so the scope can join and propagate.
    stop: core::sync::atomic::AtomicBool,
    /// Helpers the OS actually gave us, which is what the leader waits for.
    helpers: u32,
}

/// Iterations of `pause` before falling back to `yield_now`.
///
/// A segment is thousands of blocks, so an arriving worker is normally a few
/// microseconds ahead of the last one and spinning wins outright. Past that the
/// box is oversubscribed — 4 vCPU here is 2 physical cores plus SMT — and
/// yielding the slot to the worker we are waiting for is strictly better than
/// stealing issue bandwidth from its SMT sibling.
#[cfg(feature = "parallel")]
const SPIN_LIMIT: u32 = 1024;

#[cfg(feature = "parallel")]
impl FillSync {
    /// Wait until `cond()` holds, spinning then yielding.
    #[inline]
    fn park_until(mut cond: impl FnMut() -> bool) {
        let mut spins = 0u32;
        while !cond() {
            if spins < SPIN_LIMIT {
                spins += 1;
                core::hint::spin_loop();
            } else {
                std::thread::yield_now();
            }
        }
    }
}

/// Fill every lane of one `(pass, slice)` this worker can claim.
///
/// # Safety
///
/// As [`fill_memory_blocks_traced`]: this CPU must be able to execute whatever
/// instruction set `fill` needs.
#[cfg(feature = "parallel")]
unsafe fn drain_lanes(
    shared: SharedInstance<'_>,
    sync: &FillSync,
    fill: FillSegmentFn,
    pass: u32,
    slice: u32,
    lanes: u32,
) {
    use core::sync::atomic::Ordering;

    loop {
        // Relaxed is enough: the counter only partitions work, and the
        // happens-before the *data* needs comes from the barrier below.
        let lane = sync.next_lane.fetch_add(1, Ordering::Relaxed);
        if lane >= lanes {
            return;
        }
        // SAFETY: as in `fill_slice_st`, plus the cross-lane argument written
        // out at `unsafe impl Send for SharedInstance`: this worker owns lane
        // `lane` of slice `slice` for the whole call, and no other worker was
        // handed the same index.
        unsafe { fill(shared.0, Position::new(pass, lane, slice, 0)) };
    }
}

/// The whole pass/slice/lane nest, on a worker pool that outlives every slice.
///
/// Replaces `fill_memory_blocks_mt()` (`core.c:311-357`), which spawns one
/// thread per lane *per slice* and caps concurrency by joining
/// `thread[l - threads]` before creating thread `l`. This spawns
/// `min(threads, lanes)` workers **once for the entire hash**; they pull lanes
/// off a counter and meet at [`FillSync`]'s barrier at each of the `4 * passes`
/// sync points. Same bound on live threads, same (absent) ordering requirement
/// between lanes of one slice, same sync points — and `threads` never affects
/// the tag, only `lanes` does, so the schedules are interchangeable.
///
/// # Safety
///
/// As [`fill_memory_blocks_traced`]: this CPU must be able to execute whatever
/// instruction set `fill` needs.
#[cfg(feature = "parallel")]
unsafe fn fill_pooled(instance: &Instance, fill: FillSegmentFn, mut trace: Option<PassTrace<'_>>) {
    use core::sync::atomic::{AtomicBool, AtomicU32, Ordering};

    let lanes = instance.lanes;
    let passes = instance.passes;
    // `Instance::new` already clamps to `min(threads, lanes)`; re-clamp so a
    // hand-built `Instance` cannot ask for more workers than there are lanes.
    let workers = instance.threads.min(lanes);

    let shared = SharedInstance(instance);
    let sync = FillSync {
        next_lane: AtomicU32::new(0),
        arrived: AtomicU32::new(0),
        generation: AtomicU32::new(0),
        lost: AtomicU32::new(0),
        stop: AtomicBool::new(false),
        // Optimistic: the shortfall from any thread the OS refuses is charged
        // to `lost` right after the spawn loop, which the leader's wait
        // condition already accounts for.
        helpers: workers.saturating_sub(1),
    };
    let sync = &sync;

    std::thread::scope(|scope| {
        /// Releases every parked helper when the leader leaves the scope,
        /// however it leaves.
        ///
        /// Without this the crate deadlocks on any leader unwind — a panicking
        /// KAT trace callback is enough to reach it. The helpers would be
        /// spinning inside `park_until` for a `generation` bump that is never
        /// coming, and `Scope`'s own `Drop` would block for ever trying to join
        /// them. `thread::scope` propagating a panic is only useful if the
        /// threads it is joining can still finish.
        ///
        /// Firing on the normal path too is harmless and is why this is a guard
        /// rather than a `catch_unwind`: by then every helper has completed its
        /// last slice and is on its way out, so `stop` only shortens a wait
        /// whose answer has already been decided.
        struct ReleaseHelpers<'a>(&'a FillSync);
        impl Drop for ReleaseHelpers<'_> {
            fn drop(&mut self) {
                self.0.stop.store(true, Ordering::Relaxed);
                self.0.generation.fetch_add(1, Ordering::Release);
            }
        }
        let _release = ReleaseHelpers(sync);

        let mut spawned = 0u32;

        for _ in 1..workers {
            // `Builder::spawn_scoped` returns an error where `scope.spawn`
            // would panic, and this crate must not panic. A refused thread just
            // means fewer workers: every lane is still claimed from `next_lane`
            // by whoever does exist, and the tag does not depend on the count.
            let handle = std::thread::Builder::new().spawn_scoped(scope, move || {
                // Marks this helper lost if it unwinds, so the leader stops
                // waiting for a barrier arrival that will never come.
                struct Bail<'a>(&'a AtomicU32, bool);
                impl Drop for Bail<'_> {
                    fn drop(&mut self) {
                        if self.1 {
                            self.0.fetch_add(1, Ordering::Release);
                        }
                    }
                }
                let mut bail = Bail(&sync.lost, true);

                let mut generation = 0u32;
                'outer: for pass in 0..passes {
                    for slice in 0..SYNC_POINTS {
                        // SAFETY: forwarded from this function's contract — the
                        // caller guarantees the CPU can execute `fill`. The
                        // cross-thread half is the `unsafe impl Send for
                        // SharedInstance` argument above.
                        unsafe { drain_lanes(shared, sync, fill, pass, slice, lanes) };

                        // Release: publishes this helper's block writes to the
                        // leader's acquire below.
                        sync.arrived.fetch_add(1, Ordering::Release);
                        generation += 1;
                        FillSync::park_until(|| {
                            sync.generation.load(Ordering::Acquire) == generation
                                || sync.stop.load(Ordering::Relaxed)
                        });
                        if sync.stop.load(Ordering::Relaxed) {
                            break 'outer;
                        }
                    }
                }
                bail.1 = false;
            });
            if handle.is_ok() {
                spawned += 1;
            }
        }

        // A thread the OS refused must not be waited for. `helpers` was set
        // optimistically; charge the shortfall to `lost`, which the leader's
        // wait condition already accounts for.
        sync.lost
            .fetch_add(sync.helpers - spawned, Ordering::Relaxed);

        let mut generation = 0u32;
        'outer: for pass in 0..passes {
            for slice in 0..SYNC_POINTS {
                // SAFETY: as in the spawned workers; the leader is just one
                // more of them.
                unsafe { drain_lanes(shared, sync, fill, pass, slice, lanes) };

                // Acquire: makes every helper's slice writes visible here, and
                // therefore — through the release on `generation` below — to
                // every other helper in the next slice.
                FillSync::park_until(|| {
                    sync.arrived.load(Ordering::Acquire) + sync.lost.load(Ordering::Acquire)
                        >= sync.helpers
                });
                if sync.lost.load(Ordering::Relaxed) > sync.helpers - spawned {
                    // A helper unwound. Stop cleanly so `thread::scope` can
                    // join it and re-raise the panic, rather than spinning for
                    // ever on an arrival that is never coming.
                    sync.stop.store(true, Ordering::Relaxed);
                    sync.generation.fetch_add(1, Ordering::Release);
                    break 'outer;
                }

                // genkat.c `internal_kat(instance, r)`, printed after each
                // pass. This is the one place it can go: every helper is parked
                // on `generation`, holding no reference into the arena, and
                // none of them can move until the release below.
                if slice == SYNC_POINTS - 1
                    && let Some(callback) = trace.as_mut()
                {
                    // SAFETY: three obligations, and none of them is "the arena
                    // is zero".
                    //
                    //  1. Valid for `memory_len()` `Block`s: that is
                    //     `Instance::new`'s own contract, discharged by whoever
                    //     built `instance`.
                    //  2. Every block is *initialised*, which is what makes a
                    //     `&[Block]` over them a valid reference. `Arena`
                    //     guarantees that for its whole capacity from birth —
                    //     `alloc_zeroed`, or a kernel-zeroed `MAP_ANONYMOUS`
                    //     mapping — and never gives it up.
                    //  3. No live `&mut Block`: every helper has arrived at the
                    //     barrier for the last slice of this pass and is parked
                    //     inside `park_until`, so every `&mut Block` any of
                    //     them formed is dead. This shared slice is the only
                    //     live reference into the arena.
                    let blocks = unsafe {
                        core::slice::from_raw_parts(
                            instance.memory_ptr().cast_const(),
                            instance.memory_len(),
                        )
                    };
                    callback(pass, blocks);
                }

                sync.next_lane.store(0, Ordering::Relaxed);
                sync.arrived.store(0, Ordering::Relaxed);
                generation += 1;
                // Release: hands every helper everything acquired above.
                sync.generation.store(generation, Ordering::Release);
            }
        }
    });
    // Leaving the scope joins every worker, and re-raises a helper's panic.
}

// ---------------------------------------------------------------------------
// finalize
// ---------------------------------------------------------------------------

/// `finalize()` from `src/core.c`.
///
/// XORs the last block of every lane together, stores it little-endian, and runs
/// `blake2b_long` over the 1024 bytes into `out`.
///
/// Freeing the arena — the `free_memory()` at the end of the C's `finalize` — is
/// [`Arena`]'s `Drop`, which wipes before it deallocates. On the pooled path
/// (`Hasher`) the same wipe happens in [`Workspace::release`] instead, and
/// only the `dealloc` is deferred; either way the arena is wiped by the time the
/// call that owned it returns.
///
/// # Errors
///
/// [`Error::IncorrectParameter`] for a degenerate instance whose last blocks are
/// out of bounds; otherwise only a BLAKE2b parameter error, which cannot happen
/// for a validated `outlen`.
pub fn finalize(instance: &Instance, out: &mut [u8]) -> Result<(), Error> {
    let lane_length = instance.lane_length as usize;
    if lane_length == 0 || instance.lanes == 0 {
        return Err(Error::IncorrectParameter);
    }

    // SAFETY: `Instance`'s contract guarantees `memory_ptr()` is valid for
    // `memory_len()` initialised `Block`s. `fill_memory_blocks` has returned, so
    // every worker is joined and no `&mut Block` into the arena is live; this
    // shared slice is the only live reference to it.
    let blocks = unsafe {
        core::slice::from_raw_parts(instance.memory_ptr().cast_const(), instance.memory_len())
    };

    // core.c:160 `copy_block(&blockhash, instance->memory + instance->lane_length - 1);`
    let Some(&last_of_lane_0) = blocks.get(lane_length - 1) else {
        return Err(Error::IncorrectParameter);
    };
    let mut blockhash = last_of_lane_0;

    // core.c:163-167. XOR the last block of every other lane.
    for lane in 1..instance.lanes {
        // `l * instance->lane_length + (instance->lane_length - 1)`
        let index = (lane as usize)
            .checked_mul(lane_length)
            .and_then(|base| base.checked_add(lane_length - 1))
            .ok_or(Error::IncorrectParameter)?;
        match blocks.get(index) {
            Some(block) => blockhash.xor_with(block),
            None => return Err(Error::IncorrectParameter),
        }
    }

    // core.c:171-174 `store_block(blockhash_bytes, &blockhash);`
    //                `blake2b_long(context->out, context->outlen,
    //                              blockhash_bytes, ARGON2_BLOCK_SIZE);`
    let mut blockhash_bytes = blockhash.to_le_bytes();
    let result = blake2b_long(out, &blockhash_bytes);

    // core.c:176-177, on every path.
    clear_internal_memory_u64(&mut blockhash.0);
    clear_internal_memory(&mut blockhash_bytes);

    result
}

/// `argon2_compare()` from `src/argon2.c`: a constant-time byte comparison.
///
/// Returns `false` immediately for differing lengths (the C never compares
/// mismatched lengths — `outlen` is fixed by the decoded string).
#[must_use]
pub fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
    if a.len() != b.len() {
        return false;
    }

    let mut d = 0u8;
    for (x, y) in a.iter().zip(b.iter()) {
        d |= x ^ y;
    }

    // The accumulator is laundered before it is tested. Nothing downstream may
    // learn that `d` is only ever compared against zero, because a compiler
    // that knows *that* is free to rewrite the loop above into a `bcmp` that
    // stops at the first differing byte — which is precisely the timing leak
    // this function exists to prevent.
    //
    // What this actually emits on aarch64 (`--emit=asm`, release):
    //
    //     LBB_2: ldrb w9,[x0],#1 ; ldrb w10,[x2],#1
    //            eor  w9,w10,w9  ; orr  w8,w9,w8
    //            subs x1,x1,#1   ; b.ne LBB_2      <- branch on the COUNTER
    //            strb w8,[sp,#15]; ldrb w8,[sp,#15] <- the black_box launder
    //            sub  w8,w8,#1   ; ubfx w0,w8,#8,#1 <- branchless verdict
    //
    // One branch, and it is the loop counter; no `bcmp`, no `memcmp`, nothing
    // that depends on the bytes. The launder costs two instructions once per
    // call.
    //
    // `black_box` rather than the `asm!` barrier in `memory::secure_wipe_raw`
    // because the thing being protected is a value in a register, not a store
    // to memory, and because it exists on every target — including wasm and
    // under Miri, where `asm!` does not.
    let d = core::hint::black_box(d);

    // argon2.c:246 `return (int)((1 & ((d - 1) >> 8)) - 1);` — 0 when `d == 0`,
    // -1 otherwise, computed without a branch. Written out rather than reduced
    // to `d == 0` so the constant-time property is on the page, not implied.
    let verdict = (1i32 & ((i32::from(d) - 1) >> 8)) - 1;
    verdict == 0
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Salt length used by the `*_with_random_salt` entry points, in bytes.
///
/// 16 is what RFC 9106 §4 recommends for password hashing, and is comfortably
/// above [`crate::params::MIN_SALT_LENGTH`]. It is a constant rather than an
/// argument because a caller who wants to choose the length also wants to
/// choose the bytes, and should call [`Argon2::hash_encoded`] with their own
/// salt instead.
#[cfg(feature = "std")]
pub const RANDOM_SALT_LEN: usize = 16;

/// Longest salt the `*_bounded` verify entry points will accept, in bytes.
///
/// [`Params`] carries no salt length, so the pre-decode size gate in
/// [`Argon2::verify_encoded_bounded`] needs one number from somewhere. This is
/// it: generous enough that no real producer is near it — RFC 9106 recommends
/// 16 and [`RANDOM_SALT_LEN`] uses that — and small enough that a hostile string
/// cannot turn the decode into an allocation worth caring about.
///
/// A legitimate string with a salt longer than this is rejected with
/// [`Error::DecodingLengthFail`]; use the unbounded
/// [`Argon2::verify_encoded`] if you genuinely have one.
pub const BOUNDED_MAX_SALT_LEN: u32 = 1024;

/// A configured Argon2 hasher.
///
/// Bundles the algorithm, version and validated [`Params`]. The secret (key) and
/// associated data are passed per call rather than stored, which keeps `Argon2`
/// free of lifetime parameters.
///
/// # Examples
///
/// ```
/// use argon2_rust::{Algorithm, Argon2, Params, Version};
///
/// // m=19456 KiB, t=2, 1 lane, 32-byte tag: `Params::default()`, which is
/// // what a password store should start from. About 8 ms per hash in release,
/// // so this runs as a real doctest rather than only being compiled.
/// let params = Params::default();
/// let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
/// let mut tag = [0u8; 32];
/// argon2.hash_into(b"password", b"somesalt", &mut tag)?;
/// assert_eq!(argon2.verify(b"password", b"somesalt", &tag), Ok(()));
/// # Ok::<(), argon2_rust::Error>(())
/// ```
///
/// # Two spellings
///
/// Three entry points carry a password-flavoured alias, and only three:
/// [`Argon2::hash_password_into`] for [`Argon2::hash_into`],
/// [`Argon2::hash_password`] for [`Argon2::hash_encoded`], and
/// [`Argon2::verify_password`] for [`Argon2::verify_encoded`]. Each of those
/// three is a pure delegation, same function and same bytes.
///
/// Six other entry points have a base name and nothing else: [`Argon2::hash`],
/// [`Argon2::verify`], [`Argon2::hash_into_with_ad`],
/// [`Argon2::verify_encoded_with_ad`], [`Argon2::verify_encoded_bounded`] and
/// [`Argon2::verify_encoded_bounded_with_ad`]. One runs the other way:
/// `Argon2::hash_password_with_random_salt` has a password name with no base
/// twin, and it is not a delegation either. It draws a fresh salt from the OS
/// before calling [`Argon2::hash_encoded`], so two calls with one password do
/// not return the same string.
///
/// Where the alias does exist, the two families do not spell the *output
/// format* the same way:
///
/// ```text
///                      raw -> caller buffer   raw -> Vec   PHC -> String
///   base:              hash_into              hash         hash_encoded
///   password:          hash_password_into     (none)       hash_password
///                                   ^ raw                  ^ PHC
/// ```
///
/// [`Argon2::hash_password_into`] writes a **raw** tag into `out`, byte for
/// byte what [`Argon2::hash_into`] writes. [`Argon2::hash_password`] returns a
/// **PHC string**, character for character what [`Argon2::hash_encoded`]
/// returns. The only difference between those two names is `_into`, which reads
/// as a destination and not as a format; in the base family the word `encoded`
/// carries that distinction in the name, and in the password family nothing
/// does. Verification is the same shape: [`Argon2::verify_password`] takes a
/// PHC string, like [`Argon2::verify_encoded`], not the raw expected tag that
/// [`Argon2::verify`] takes.
///
/// There is no raw-`Vec` password spelling, which is the empty cell above; for
/// that shape the only name is [`Argon2::hash`].
///
/// ```
/// use argon2_rust::{Algorithm, Argon2, Params, Version, params::Memory};
///
/// let params = Params::builder().memory(Memory::kib(1 << 8)).passes(1).build()?;
/// let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
///
/// // `_into` picks the destination, and with it the raw format.
/// let mut raw = [0u8; 32];
/// argon2.hash_password_into(b"password", b"somesalt", &mut raw)?;
///
/// // No suffix at all, and the format changes to PHC.
/// let phc = argon2.hash_password(b"password", b"somesalt")?;
/// assert!(phc.starts_with("$argon2id$v=19$m=256,t=1,p=1$c29tZXNhbHQ$"));
///
/// // One tag underneath both: `raw` is the bytes the string base64s.
/// assert_eq!(argon2.hash(b"password", b"somesalt")?, raw);
/// # Ok::<(), argon2_rust::Error>(())
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Argon2 {
    algorithm: Algorithm,
    version: Version,
    params: Params,
}

impl Argon2 {
    /// Build a hasher. `params` is already validated, so this cannot fail.
    #[inline]
    #[must_use]
    pub const fn new(algorithm: Algorithm, version: Version, params: Params) -> Argon2 {
        Argon2 {
            algorithm,
            version,
            params,
        }
    }

    /// The configured algorithm.
    #[inline]
    #[must_use]
    pub const fn algorithm(&self) -> Algorithm {
        self.algorithm
    }

    /// The configured version.
    #[inline]
    #[must_use]
    pub const fn version(&self) -> Version {
        self.version
    }

    /// The configured parameters.
    #[inline]
    #[must_use]
    pub const fn params(&self) -> &Params {
        &self.params
    }

    /// A `Hasher`: this configuration plus scratch memory it keeps between
    /// calls.
    ///
    /// Allocates nothing — the first hash allocates the arena, and every hash
    /// after that reuses it. Use this when one thread hashes repeatedly;
    /// keep using [`Argon2::hash_into`] and friends when it does not.
    ///
    /// ```
    /// use argon2_rust::{Algorithm, Argon2, Params, Version, params::Memory};
    ///
    /// let params = Params::builder().memory(Memory::kib(8)).passes(1).build()?;
    /// let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
    ///
    /// let mut hasher = argon2.hasher();
    /// let mut tag = [0u8; 32];
    /// for salt in [&b"somesalt"[..], &b"othersaltx"[..]] {
    ///     hasher.hash_into(b"password", salt, &mut tag)?;
    /// }
    ///
    /// // Same answer as the one-shot API, every time.
    /// let mut once = [0u8; 32];
    /// argon2.hash_into(b"password", b"somesalt", &mut once)?;
    /// hasher.hash_into(b"password", b"somesalt", &mut tag)?;
    /// assert_eq!(tag, once);
    /// # Ok::<(), argon2_rust::Error>(())
    /// ```
    #[inline]
    #[must_use]
    pub fn hasher(&self) -> Hasher {
        Hasher {
            argon2: *self,
            workspace: Workspace::new(),
        }
    }

    /// Derive a tag into `out`.
    ///
    /// `out.len()` must equal [`Params::tag_len_bytes`].
    ///
    /// ```
    /// use argon2_rust::{Algorithm, Argon2, Error, Params, Version, params::Memory};
    ///
    /// let params = Params::builder().memory(Memory::kib(64)).passes(1).build()?;
    /// let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
    ///
    /// let mut tag = [0u8; 32];
    /// argon2.hash_into(b"password", b"somesalt", &mut tag)?;
    ///
    /// // Those 32 bytes are what the PHC string base64s, so pinning the string
    /// // pins the tag without spelling out an array of hex.
    /// assert_eq!(
    ///     argon2.hash_encoded(b"password", b"somesalt")?,
    ///     "$argon2id$v=19$m=64,t=1,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70",
    /// );
    ///
    /// // `out.len()` is checked against `Params::tag_len_bytes`, never used to
    /// // size the tag: a buffer of the wrong length is an error, not a
    /// // truncated hash.
    /// let mut too_short = [0u8; 16];
    /// assert_eq!(
    ///     argon2.hash_into(b"password", b"somesalt", &mut too_short),
    ///     Err(Error::OutPtrMismatch),
    /// );
    /// # Ok::<(), argon2_rust::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// Whatever [`Params::validate_for`] returns, [`Error::OutPtrMismatch`] if
    /// `out.len()` disagrees with `params.tag_len_bytes()`, or
    /// [`Error::MemoryAllocationError`].
    pub fn hash_into(&self, pwd: &[u8], salt: &[u8], out: &mut [u8]) -> Result<(), Error> {
        self.hash_into_with_ad(pwd, salt, &[], &[], out)
    }

    /// Derive a tag into `out`, with a secret key and associated data.
    ///
    /// No PHC-emitting entry point accepts either; see the "Secret and
    /// associated data" section of [`Argon2::hash_encoded`].
    ///
    /// # Errors
    ///
    /// As [`Argon2::hash_into`].
    pub fn hash_into_with_ad(
        &self,
        pwd: &[u8],
        salt: &[u8],
        secret: &[u8],
        ad: &[u8],
        out: &mut [u8],
    ) -> Result<(), Error> {
        // SAFETY: the only `Backend` the public API ever names is
        // `fill_block::backend()`, the cached result of the
        // `is_*_feature_detected!` cascade, so this CPU can execute it by
        // construction. That is what keeps this — and every other public entry
        // point — safe while `hash_inner` is not.
        unsafe {
            hash_inner(
                crate::fill_block::backend(),
                self.algorithm,
                self.version,
                &self.params,
                pwd,
                salt,
                secret,
                ad,
                out,
            )
        }
    }

    /// Derive a tag of [`Params::tag_len_bytes`] bytes.
    ///
    /// ```
    /// use argon2_rust::{Algorithm, Argon2, Params, Version, params::Memory};
    ///
    /// let params = Params::builder().memory(Memory::kib(64)).passes(1).build()?;
    /// let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
    ///
    /// // The `Vec` is sized from the parameters, so there is no buffer to get
    /// // wrong and no `Error::OutPtrMismatch` to handle.
    /// let tag = argon2.hash(b"password", b"somesalt")?;
    /// assert_eq!(tag.len(), argon2.params().tag_len_bytes());
    ///
    /// // Byte for byte what `hash_into` writes into a buffer you own; this is
    /// // the same function with the allocation moved inside.
    /// let mut into = [0u8; 32];
    /// argon2.hash_into(b"password", b"somesalt", &mut into)?;
    /// assert_eq!(tag, into);
    /// # Ok::<(), argon2_rust::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// As [`Argon2::hash_into`].
    pub fn hash(&self, pwd: &[u8], salt: &[u8]) -> Result<Vec<u8>, Error> {
        let mut out = try_zeroed_vec(self.params.tag_len_bytes())?;
        self.hash_into(pwd, salt, &mut out)?;
        Ok(out)
    }

    /// Derive a tag and format it as a PHC string.
    ///
    /// Always emits `$v=`, exactly as `encode_string()` in the C does, even for
    /// [`Version::V0x10`].
    ///
    /// # Secret and associated data
    ///
    /// No PHC-emitting entry point takes a `secret` (pepper) or `ad`, because a
    /// PHC string has a field for neither: `argon2_hash()` (`argon2.h:322`)
    /// hardcodes `context.secret = NULL; context.ad = NULL` (`argon2.c:139-142`)
    /// and `encode_string` emits only `$type$v=$m=,t=,p=$salt$hash`. A peppered
    /// deployment must call [`Argon2::hash_into_with_ad`] and encode the tag
    /// itself — `mod encoding` is private, so that means the PHC layout and
    /// unpadded Base64 by hand. Its string is indistinguishable from an
    /// unpeppered one, so [`Argon2::verify_encoded`] on it answers
    /// [`Error::VerifyMismatch`] rather than any "missing pepper" signal.
    ///
    /// # Errors
    ///
    /// As [`Argon2::hash_into`], plus [`Error::EncodingFail`].
    pub fn hash_encoded(&self, pwd: &[u8], salt: &[u8]) -> Result<String, Error> {
        let mut tag = self.hash(pwd, salt)?;
        let encoded = crate::encoding::encode_string_alloc(
            self.algorithm,
            self.version,
            &self.params,
            salt,
            &tag,
        );
        // argon2.c:173 `clear_internal_memory(out, hashlen);`
        clear_internal_memory(&mut tag);
        encoded
    }

    /// Recompute the tag and compare it with `expected` in constant time.
    ///
    /// A length mismatch is a [`Error::VerifyMismatch`], not a separate error:
    /// the C cannot reach that case, because `decode_string` sets
    /// `context->outlen` from the tag it just decoded.
    ///
    /// ```
    /// use argon2_rust::{Algorithm, Argon2, Error, Params, Version, params::Memory};
    ///
    /// let params = Params::builder().memory(Memory::kib(64)).passes(1).build()?;
    /// let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
    ///
    /// // A raw tag stored earlier, alongside the salt that produced it. The
    /// // parameters are yours to remember too, which is what the PHC string
    /// // from `hash_encoded` saves you.
    /// let expected = argon2.hash(b"password", b"somesalt")?;
    /// assert_eq!(argon2.verify(b"password", b"somesalt", &expected), Ok(()));
    ///
    /// // Wrong password.
    /// assert_eq!(
    ///     argon2.verify(b"wrong", b"somesalt", &expected),
    ///     Err(Error::VerifyMismatch),
    /// );
    /// // Wrong salt: the tag is a function of both.
    /// assert_eq!(
    ///     argon2.verify(b"password", b"othersalt", &expected),
    ///     Err(Error::VerifyMismatch),
    /// );
    /// // A truncated `expected` is that same error and not a length error,
    /// // exactly as the paragraph above says.
    /// assert_eq!(
    ///     argon2.verify(b"password", b"somesalt", &expected[..16]),
    ///     Err(Error::VerifyMismatch),
    /// );
    /// # Ok::<(), argon2_rust::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// As [`Argon2::hash_into`], or [`Error::VerifyMismatch`].
    pub fn verify(&self, pwd: &[u8], salt: &[u8], expected: &[u8]) -> Result<(), Error> {
        let mut computed = try_zeroed_vec(self.params.tag_len_bytes())?;
        let result = self.hash_into(pwd, salt, &mut computed);
        // argon2.c:349 `argon2_compare(hash, context->out, context->outlen)`.
        let matched = result.is_ok() && constant_time_eq(&computed, expected);
        clear_internal_memory(&mut computed);

        result?;
        if matched {
            Ok(())
        } else {
            Err(Error::VerifyMismatch)
        }
    }

    /// `argon2_verify()`: decode a PHC string and check `pwd` against it.
    ///
    /// # Errors
    ///
    /// [`Error::DecodingFail`] for a malformed string, [`Error::VerifyMismatch`]
    /// if the password is wrong, or any hashing error.
    pub fn verify_encoded(encoded: &str, pwd: &[u8], algorithm: Algorithm) -> Result<(), Error> {
        // argon2.c:260-262 `if (pwdlen > ARGON2_MAX_PWD_LENGTH)`.
        if pwd.len() > MAX_PWD_LENGTH as usize {
            return Err(Error::PwdTooLong);
        }

        // argon2.c:289 `decode_string(&ctx, encoded, type)`.
        let decoded = crate::encoding::decode_string(encoded, algorithm)?;

        // argon2.c:302 `argon2_verify_ctx(&ctx, desired_result, type)`.
        Argon2::new(decoded.algorithm, decoded.version, decoded.params).verify(
            pwd,
            &decoded.salt,
            &decoded.hash,
        )
    }

    /// `argon2_verify_ctx()`: decode a PHC string and check `pwd` against it,
    /// with a secret key and associated data.
    ///
    /// # Errors
    ///
    /// As [`Argon2::verify_encoded`], plus the secret/ad validation errors of
    /// [`Argon2::hash_into_with_ad`].
    pub fn verify_encoded_with_ad(
        encoded: &str,
        pwd: &[u8],
        secret: &[u8],
        ad: &[u8],
        algorithm: Algorithm,
    ) -> Result<(), Error> {
        // argon2.c:260-262 `if (pwdlen > ARGON2_MAX_PWD_LENGTH)`.
        if pwd.len() > MAX_PWD_LENGTH as usize {
            return Err(Error::PwdTooLong);
        }

        // argon2.c:289 `decode_string(&ctx, encoded, type)`.
        let decoded = crate::encoding::decode_string(encoded, algorithm)?;

        // argon2.c:302 `argon2_verify_ctx(&ctx, desired_result, type)`.
        let argon2 = Argon2::new(decoded.algorithm, decoded.version, decoded.params);
        let mut computed = try_zeroed_vec(argon2.params.tag_len_bytes())?;
        let result =
            argon2.hash_into_with_ad(pwd, &decoded.salt, secret, ad, &mut computed);
        let matched = result.is_ok() && constant_time_eq(&computed, &decoded.hash);
        clear_internal_memory(&mut computed);

        result?;
        if matched {
            Ok(())
        } else {
            Err(Error::VerifyMismatch)
        }
    }

    // -----------------------------------------------------------------
    // Password-flavoured spellings of the three entry points above
    // -----------------------------------------------------------------
    //
    // Same functions, the names the C's three public entry points suggest:
    // `argon2_hash` with a raw output buffer, `argon2_hash` with an encoded
    // output buffer, and `argon2_verify`. They exist so a caller can read the
    // API as "hash a password" rather than "hash some bytes"; the shorter
    // spellings stay because that is what this crate's own tests and benches
    // already call.
    //
    // That last half is an internal reason. The user-facing one is that these
    // are the names a C caller already knows: the per-algorithm wrappers it
    // links against are `argon2id_hash_raw` (argon2.c:230),
    // `argon2id_hash_encoded` (argon2.c:219) and `argon2id_verify`
    // (argon2.c:325), each a single `return` into `argon2_hash`/`argon2_verify`
    // and nothing else in the body. Only `argon2id_verify` fits on one line
    // (argon2.c:327); the two hash wrappers each spend three on the argument
    // list alone (argon2.c:225-227 and argon2.c:234-236), which is line
    // wrapping and not work. The raw/encoded choice is made entirely by which
    // out-pointer those wrappers pass as non-NULL (argon2.c:160 `if (hash)`,
    // argon2.c:165 `if (encoded && encodedlen)`).
    //
    // Note what the C's names do that these do not: they carry the output
    // format, `raw` against `encoded`. Here the only difference between
    // `hash_password_into` and `hash_password` is `_into`, which names a
    // destination, not a format. So the format asymmetry is stated on the type
    // (`Argon2`'s `# Two spellings`) and again on the first line of each method
    // below, where a reader scanning the method list will actually see it.

    /// Derive a **raw** tag into `out`, not a PHC string.
    ///
    /// `argon2_hash()` with `hash != NULL` (`argon2.c:160`). The same function
    /// as [`Argon2::hash_into`]: `_into` picks the destination, and the format
    /// that comes with it is bytes. `out.len()` must equal
    /// [`Params::tag_len_bytes`]. For the PHC string, [`Argon2::hash_password`].
    ///
    /// # Errors
    ///
    /// As [`Argon2::hash_into`].
    #[inline]
    pub fn hash_password_into(&self, pwd: &[u8], salt: &[u8], out: &mut [u8]) -> Result<(), Error> {
        self.hash_into(pwd, salt, out)
    }

    /// Derive a tag and return the **PHC string** for it, not the raw bytes.
    ///
    /// `argon2_hash()` with `encoded != NULL` (`argon2.c:165`). The same
    /// function as [`Argon2::hash_encoded`]; for the raw tag, its sibling
    /// [`Argon2::hash_password_into`] or [`Argon2::hash`].
    ///
    /// Always emits `$v=`, just like `encode_string()` in the C, even for
    /// [`Version::V0x10`] — the `v=0x10` reference strings in `src/test.c`
    /// predate that field, so they have no `$v=` and are one field shorter than
    /// what this returns. Both forms decode, see [`Argon2::verify_password`].
    ///
    /// # Errors
    ///
    /// As [`Argon2::hash_into`], plus [`Error::EncodingFail`].
    #[inline]
    pub fn hash_password(&self, pwd: &[u8], salt: &[u8]) -> Result<String, Error> {
        self.hash_encoded(pwd, salt)
    }

    /// Derive a PHC string with a fresh salt from the OS entropy source.
    ///
    /// Convenience for the common case where the caller does not manage its own
    /// salt. The salt is [`RANDOM_SALT_LEN`] bytes — the length RFC 9106 §4
    /// recommends — and lands in the returned string, so verification needs
    /// nothing else kept alongside it.
    ///
    /// The randomness comes straight from the OS, with the entry point chosen
    /// per platform (`getrandom(2)`, `getentropy`, `CCRandomGenerateBytes`,
    /// `ProcessPrng`, WASI `random_get`, or `/dev/urandom`) and declared by
    /// hand, so this costs the crate no dependency. Callers who already run
    /// their own CSPRNG should keep passing their own salt to
    /// [`Argon2::hash_encoded`].
    ///
    /// Hashing many passwords? [`Hasher::hash_password_with_random_salt`] does
    /// this over a pooled arena.
    ///
    /// # Errors
    ///
    /// [`Error::OsRandom`] if every OS entropy source for this platform fails,
    /// plus the errors of [`Argon2::hash_encoded`].
    #[cfg(feature = "std")]
    pub fn hash_password_with_random_salt(&self, pwd: &[u8]) -> Result<String, Error> {
        // Not wiped on the way out, deliberately, and unlike every other
        // buffer in this file: the salt is *published* in the returned string,
        // so scrubbing the stack copy protects nothing that is not already in
        // the caller's hands. `clear_internal_memory` is for secret-derived
        // material; a salt is not that.
        let mut salt = [0u8; RANDOM_SALT_LEN];
        crate::random::os_random(&mut salt)?;
        self.hash_encoded(pwd, &salt)
    }

    /// Check `pwd` against a **PHC string**, not against a raw tag.
    ///
    /// `argon2_verify()` (`argon2.c:249`): decode `encoded`, then recompute and
    /// compare. The same function as [`Argon2::verify_encoded`]. The parameters
    /// come out of the string, so nothing on `self` is consulted, which is why
    /// this is an associated function. To check a raw expected tag with these
    /// parameters instead, [`Argon2::verify`].
    ///
    /// # Errors
    ///
    /// [`Error::DecodingFail`] for a malformed string, [`Error::VerifyMismatch`]
    /// if the password is wrong, or any hashing error.
    #[inline]
    pub fn verify_password(encoded: &str, pwd: &[u8], algorithm: Algorithm) -> Result<(), Error> {
        Argon2::verify_encoded(encoded, pwd, algorithm)
    }

    /// [`Argon2::verify_encoded`], refusing costs above `ceiling` **before**
    /// allocating anything.
    ///
    /// # Why this exists
    ///
    /// `m_cost` in a PHC string is up to ten decimal digits, and the decoder
    /// accepts everything the C accepts — up to
    /// [`MAX_MEMORY`](crate::params::MAX_MEMORY) KiB, which is 4 TiB. Nothing in
    /// [`Argon2::verify_encoded`] sits between that number and the allocation,
    /// because nothing does in `argon2_verify` either; on a login endpoint,
    /// where the string is whatever a database row (or a request) contained,
    /// that is a one-line denial of service. `t_cost` is the same story in CPU
    /// time rather than bytes.
    ///
    /// The plain entry points keep exact C parity and are the right choice when
    /// the string is trusted — a config file, a fixture, your own output. This
    /// one is for when it is not.
    ///
    /// ```
    /// use argon2_rust::{Algorithm, Argon2, Params, Version, params::Memory};
    ///
    /// let hostile = "$argon2id$v=19$m=4294967295,t=1,p=1$c29tZXNhbHQ$\
    ///                CTFhFdXPJO1aFaMaO6Mm5c8y7cJHAph8ArZWb2GRPPc";
    /// // 64 MiB, 8 passes, 4 lanes is far more than any sane stored hash.
    /// let ceiling = Params::builder().memory(Memory::mib(64)).passes(8).lanes(4).build()?;
    ///
    /// let err = Argon2::verify_encoded_bounded(
    ///     hostile, b"password", Algorithm::Argon2id, &ceiling,
    /// ).unwrap_err();
    /// // Rejected on the parameters, without ever asking for 4 TiB.
    /// assert_eq!(err, argon2_rust::Error::MemoryTooMuch);
    /// # Ok::<(), argon2_rust::Error>(())
    /// ```
    ///
    /// # What is bounded
    ///
    /// Both the cost *and* the allocation. The length of `encoded` is checked
    /// against the longest string `ceiling` could have produced — with
    /// [`BOUNDED_MAX_SALT_LEN`] allowed for the salt — **before** the decoder
    /// runs, because the decoder sizes its salt and tag buffers from the input.
    /// Then the decoded parameters are held to all four of the ceiling's
    /// numbers.
    ///
    /// # Worker threads
    ///
    /// `ceiling.threads()` bounds them, and it is a *fifth*, independent knob —
    /// none of the four checks above implies it. Decoding sets `threads = lanes`
    /// (C parity), so the string's own `p` would otherwise choose how many OS
    /// threads this call spawns. A ceiling that leaves
    /// [`ParamsBuilder::threads`](crate::params::ParamsBuilder::threads) unset
    /// has `threads == lanes` and so bounds them together; set it to allow wide
    /// strings without spawning wide:
    ///
    /// ```
    /// use argon2_rust::{Params, params::Memory};
    /// // Accept up to 256 lanes, but never run more than 2 workers.
    /// let ceiling = Params::builder()
    ///     .memory(Memory::mib(64))
    ///     .passes(8)
    ///     .lanes(256)
    ///     .threads(2)
    ///     .build()?;
    /// # Ok::<(), argon2_rust::Error>(())
    /// ```
    ///
    /// Clamping is always safe: `threads` is a scheduling knob that cannot
    /// change the tag — only `lanes` can — so a bounded verify accepts exactly
    /// the same strings whatever the budget.
    ///
    /// # Errors
    ///
    /// The errors of [`Argon2::verify_encoded`], plus — checked in this order,
    /// and reusing the C's own codes rather than inventing new ones —
    /// [`Error::DecodingLengthFail`] if `encoded` is longer than `ceiling` could
    /// have produced, [`Error::OutputTooLong`] if the decoded tag is longer than
    /// `ceiling.tag_len_bytes()`, [`Error::MemoryTooMuch`] if the decoded `m_cost`
    /// exceeds `ceiling.memory_kib()`, [`Error::TimeTooLarge`] if `t_cost` exceeds
    /// `ceiling.passes()`, and [`Error::LanesTooMany`] if `lanes` exceeds
    /// `ceiling.lanes()`.
    pub fn verify_encoded_bounded(
        encoded: &str,
        pwd: &[u8],
        algorithm: Algorithm,
        ceiling: &Params,
    ) -> Result<(), Error> {
        // argon2.c:260-262 `if (pwdlen > ARGON2_MAX_PWD_LENGTH)`.
        if pwd.len() > MAX_PWD_LENGTH as usize {
            return Err(Error::PwdTooLong);
        }

        let decoded = decode_bounded(encoded, algorithm, ceiling)?;

        Argon2::new(decoded.algorithm, decoded.version, decoded.params).verify(
            pwd,
            &decoded.salt,
            &decoded.hash,
        )
    }

    /// [`Argon2::verify_encoded_with_ad`] with the cost ceiling of
    /// [`Argon2::verify_encoded_bounded`].
    ///
    /// A keyed deployment is *more* likely to be the one parsing untrusted
    /// strings, not less, so the bounded form exists for both.
    ///
    /// # Errors
    ///
    /// As [`Argon2::verify_encoded_bounded`], plus the secret/ad validation
    /// errors of [`Argon2::hash_into_with_ad`].
    pub fn verify_encoded_bounded_with_ad(
        encoded: &str,
        pwd: &[u8],
        secret: &[u8],
        ad: &[u8],
        algorithm: Algorithm,
        ceiling: &Params,
    ) -> Result<(), Error> {
        // argon2.c:260-262 `if (pwdlen > ARGON2_MAX_PWD_LENGTH)`.
        if pwd.len() > MAX_PWD_LENGTH as usize {
            return Err(Error::PwdTooLong);
        }

        let decoded = decode_bounded(encoded, algorithm, ceiling)?;

        let argon2 = Argon2::new(decoded.algorithm, decoded.version, decoded.params);
        let mut computed = try_zeroed_vec(argon2.params.tag_len_bytes())?;
        let result = argon2.hash_into_with_ad(pwd, &decoded.salt, secret, ad, &mut computed);
        let matched = result.is_ok() && constant_time_eq(&computed, &decoded.hash);
        clear_internal_memory(&mut computed);

        result?;
        if matched {
            Ok(())
        } else {
            Err(Error::VerifyMismatch)
        }
    }
}

/// Decode `encoded` and hold it to `ceiling`, for the `*_bounded` entry points.
///
/// # Why the length gate comes first
///
/// Checking the ceiling *after* decoding is not enough, and an earlier revision
/// of this function got that wrong. [`crate::encoding::decode_string`] sizes its
/// salt and tag buffers from the input string, so the decode itself is an
/// attacker-controlled allocation before any ceiling is consulted. Measured with
/// an allocator spy against the previous version: a well-formed string with
/// `m=8,t=1,p=1` and a 16 MiB Base64 tag peaked at **36 MiB** of live
/// allocation, then ran a full Argon2 and a 12 MiB comparison — under a ceiling
/// whose tag length was 32 bytes. Every cost was inside the ceiling; the tag was
/// never looked at.
///
/// So the size of the string is checked against what the ceiling could
/// legitimately produce *before* anything is parsed, and the decoded tag length
/// is then checked against `ceiling.tag_len_bytes()` as well. A ceiling is four
/// numbers, and all four now mean something.
fn decode_bounded(
    encoded: &str,
    algorithm: Algorithm,
    ceiling: &Params,
) -> Result<crate::encoding::Decoded, Error> {
    // The longest string the ceiling could have produced. `num_len` is monotone
    // in its argument and the costs are themselves capped below, so taking the
    // ceiling's own values gives a true upper bound. `encoded_len` counts the
    // C's NUL, so this is permissive by exactly one byte.
    let max_encoded = crate::encoding::encoded_len(
        algorithm,
        ceiling.passes(),
        ceiling.memory_kib(),
        ceiling.lanes(),
        BOUNDED_MAX_SALT_LEN,
        // The tag length is bounded by MAX_OUTLEN, so this cast cannot truncate.
        ceiling.tag_len_bytes() as u32,
    );
    if encoded.len() > max_encoded {
        // ARGON2_DECODING_LENGTH_FAIL: "Some of encoded parameters are too long
        // or too short". The C defines it for exactly this and never returns it;
        // it is the right code and it costs no new error variant.
        return Err(Error::DecodingLengthFail);
    }

    let mut decoded = crate::encoding::decode_string(encoded, algorithm)?;

    if decoded.params.tag_len_bytes() > ceiling.tag_len_bytes() {
        return Err(Error::OutputTooLong);
    }
    if decoded.params.memory_kib() > ceiling.memory_kib() {
        return Err(Error::MemoryTooMuch);
    }
    if decoded.params.passes() > ceiling.passes() {
        return Err(Error::TimeTooLarge);
    }
    if decoded.params.lanes() > ceiling.lanes() {
        return Err(Error::LanesTooMany);
    }

    // The four checks above do **not** imply a worker-thread bound, and the
    // ceiling's `threads` is a separate field precisely so a caller can say
    // "allow wide strings, but never spawn wide". `decode_string` sets
    // `threads = lanes` (C parity, `argon2.c`), and `fill_pooled` spawns
    // `min(threads, lanes) - 1` helpers — so without this clamp a `p=256`
    // string inside a `lanes` ceiling of 256 spawns 255 OS threads even when
    // the ceiling asked for one worker. That is attacker-chosen concurrency on
    // an authentication path.
    //
    // Lowering `threads` is free: it is a pure scheduling knob that cannot
    // change the tag (only `lanes` can), which `threads_do_not_change_the_tag`
    // pins across both versions and all three algorithms.
    //
    // `min` with `lanes` keeps the value meaningful rather than merely legal —
    // workers above the lane count have nothing to claim — and cannot underflow
    // the `MIN_THREADS = 1` floor, because a validated ceiling has
    // `threads >= 1` and a decoded string has `lanes >= 1`.
    let threads = ceiling.threads().min(decoded.params.lanes());
    if threads != decoded.params.threads() {
        // `to_builder()` carries the four cost values and the tag length across
        // unchanged, so only the one field that actually moves is named here.
        // Re-listing all five through `Params::builder()` would invite exactly
        // the drift this clamp exists to prevent.
        decoded.params = decoded.params.to_builder().threads(threads).build()?;
    }
    Ok(decoded)
}

// ---------------------------------------------------------------------------
// Hasher — the same API, over memory that survives the call
// ---------------------------------------------------------------------------

/// An [`Argon2`] that keeps its block arena between calls.
///
/// Build one with [`Argon2::hasher`]. Every method mirrors the [`Argon2`]
/// method of the same name and returns the same bytes; the only difference is
/// that the arena is borrowed from a pool instead of allocated and freed each
/// time. Nothing else about the computation changes — same backend dispatch,
/// same threading, same wipe.
///
/// ```
/// use argon2_rust::{Algorithm, Argon2, Params, Version, params::Memory};
///
/// let params = Params::builder().memory(Memory::kib(1 << 8)).passes(1).build()?;
/// let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
/// let mut hasher = argon2.hasher();
///
/// let encoded = hasher.hash_encoded(b"password", b"somesalt")?;
/// assert!(hasher.verify_encoded(&encoded, b"password", Algorithm::Argon2id).is_ok());
/// # Ok::<(), argon2_rust::Error>(())
/// ```
///
/// # What it is worth, measured
///
/// Reuse skips the `mmap`, the first-touch page faults over the whole arena,
/// and the `munmap`. Interleaved A/B against [`Argon2::hash_into`], 15 paired
/// rounds on Linux/x86-64 (Sapphire Rapids, AVX-512):
///
/// ```text
///   m_cost   t   p |  one-shot |    pooled |  delta
///  ---------|-----|-----------|-----------|--------
///     8 KiB   1   1 |  20.4 us |   20.3 us |  -0.7%
///    64 KiB   1   1 |  27.9 us |   26.5 us |  -5.3%
///     1 MiB   1   1 |  212 us  |   185 us  | -11.7%
///     4 MiB   1   1 |  989 us  |   786 us  | -19.9%
///     4 MiB   1   4 |  806 us  |   592 us  | -26.9%
///    64 MiB   1   1 |  25.89 ms|  19.43 ms | -24.9%
///    64 MiB   1   4 |  11.65 ms|   8.40 ms | -34.0%
///   256 MiB   1   1 | 111.74 ms|  86.17 ms | -23.3%
///   256 MiB   1   4 |  46.14 ms|  35.09 ms | -24.0%
///   256 MiB   3   4 | 109.85 ms|  99.26 ms |  -9.7%
/// ```
///
/// The `t = 3` rows are smaller for the obvious reason: the same one-time
/// acquisition is spread over three passes of filling.
///
/// It does **not** remove allocator calls — there was only ever one per hash,
/// 1.7 us out of 306 ms at `m_cost = 1 GiB`.
///
/// # Wiping
///
/// Unchanged from the one-shot API. The arena is wiped when the call that
/// borrowed it returns — success, `?` error or unwind alike — so the window in
/// which a password's derived material is resident is exactly as long as it was
/// before. What reuse changes is that the wipe now doubles as the *next* call's
/// zeroing, instead of being followed by a fresh `alloc_zeroed` that zeroes
/// again.
///
/// Dropping the `Hasher` releases the arena to the allocator, wiped.
///
/// # Threading
///
/// One `Hasher` per thread. It is [`Send`], so it can move to whichever worker
/// picks up a request, and deliberately **not** [`Sync`]: two threads hashing
/// through one `Hasher` would be two hashes sharing one arena. The multi-lane
/// fill inside a single hash is unaffected — one [`std::thread::scope`] owns
/// its helper pool for the whole fill, over the arena this `Hasher` lent it for
/// the duration of that one call.
///
/// ```compile_fail
/// # use argon2_rust::{Algorithm, Argon2, Params, Version, params::Memory};
/// # let params = Params::builder().memory(Memory::kib(8)).passes(1).build().unwrap();
/// # let hasher = Argon2::new(Algorithm::Argon2id, Version::V0x13, params).hasher();
/// fn needs_sync<T: Sync>(_: &T) {}
/// needs_sync(&hasher);
/// ```
///
/// # Two spellings
///
/// Every alias mirrors [`Argon2`], trap included: [`Hasher::hash_password_into`]
/// writes a **raw** tag while [`Hasher::hash_password`] returns a **PHC
/// string**, because `_into` names a destination and not a format. See
/// [`Argon2`'s section of the same name](Argon2#two-spellings) for the table.
///
/// ```
/// use argon2_rust::{Algorithm, Argon2, Params, Version, params::Memory};
///
/// let params = Params::builder().memory(Memory::kib(1 << 8)).passes(1).build()?;
/// let mut hasher = Argon2::new(Algorithm::Argon2id, Version::V0x13, params).hasher();
///
/// // Same prefix, same arena, different return type and different format.
/// let mut raw = [0u8; 32];
/// hasher.hash_password_into(b"password", b"somesalt", &mut raw)?;
/// let phc = hasher.hash_password(b"password", b"somesalt")?;
///
/// assert!(phc.starts_with("$argon2id$v=19$m=256,t=1,p=1$c29tZXNhbHQ$"));
/// assert_eq!(hasher.hash(b"password", b"somesalt")?, raw);
/// # Ok::<(), argon2_rust::Error>(())
/// ```
pub struct Hasher {
    argon2: Argon2,
    workspace: Workspace,
}

impl Hasher {
    /// The configuration this hasher applies.
    #[inline]
    #[must_use]
    pub const fn argon2(&self) -> &Argon2 {
        &self.argon2
    }

    /// Point the hasher at a different configuration, keeping the memory.
    ///
    /// For a process that has to hash at more than one parameter set — a
    /// password migration, say. The arena grows if the new `m_cost` needs more
    /// blocks and is kept as-is if it needs fewer, so the steady state is one
    /// allocation sized to the largest configuration seen.
    #[inline]
    pub fn set_argon2(&mut self, argon2: Argon2) {
        self.argon2 = argon2;
    }

    /// The configured algorithm.
    #[inline]
    #[must_use]
    pub const fn algorithm(&self) -> Algorithm {
        self.argon2.algorithm
    }

    /// The configured version.
    #[inline]
    #[must_use]
    pub const fn version(&self) -> Version {
        self.argon2.version
    }

    /// The configured parameters.
    #[inline]
    #[must_use]
    pub const fn params(&self) -> &Params {
        &self.argon2.params
    }

    /// Allocate the arena now instead of during the first hash.
    ///
    /// Only moves the cost; it does not remove it. Worth doing when the first
    /// request must not be the slow one, or to find out at start-up rather than
    /// under load that `m_cost` does not fit in memory.
    ///
    /// # Errors
    ///
    /// [`Error::MemoryAllocationError`].
    pub fn reserve(&mut self) -> Result<(), Error> {
        self.workspace.reserve(self.argon2.params.memory_blocks() as usize)
    }

    /// Blocks of arena the hasher is holding on to. 1 KiB each.
    ///
    /// 0 before the first hash, or after [`clear`](Hasher::clear). Diagnostic:
    /// it is how a test proves that reuse is actually happening.
    #[inline]
    #[must_use]
    pub fn reserved_blocks(&self) -> usize {
        self.workspace.capacity()
    }

    /// Give the arena back to the allocator, wiped, and keep the configuration.
    ///
    /// For a worker going idle that would rather not sit on `m_cost` KiB. The
    /// next hash allocates again.
    pub fn clear(&mut self) {
        self.workspace.clear();
    }

    /// Derive a tag into `out`. [`Argon2::hash_into`], reusing the arena.
    ///
    /// ```
    /// use argon2_rust::{Algorithm, Argon2, Params, Version, params::Memory};
    ///
    /// let params = Params::builder().memory(Memory::kib(64)).passes(1).build()?;
    /// let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
    /// let mut hasher = argon2.hasher();
    ///
    /// // `Argon2::hasher` allocates nothing; the first hash sizes the arena.
    /// assert_eq!(hasher.reserved_blocks(), 0);
    ///
    /// let mut tags = Vec::new();
    /// for pwd in [&b"first"[..], &b"second"[..]] {
    ///     let mut tag = [0u8; 32];
    ///     hasher.hash_into(pwd, b"somesalt", &mut tag)?;
    ///     tags.push(tag);
    /// }
    ///
    /// // Two hashes, one arena: 64 blocks of 1 KiB, the `m_cost` above. The
    /// // second call neither allocated nor grew it.
    /// assert_eq!(hasher.reserved_blocks(), 64);
    /// assert_ne!(tags[0], tags[1]);
    ///
    /// // Reuse changes where the memory came from and nothing else.
    /// assert_eq!(argon2.hash(b"second", b"somesalt")?, tags[1]);
    /// # Ok::<(), argon2_rust::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// As [`Argon2::hash_into`].
    #[inline]
    pub fn hash_into(&mut self, pwd: &[u8], salt: &[u8], out: &mut [u8]) -> Result<(), Error> {
        self.hash_into_with_ad(pwd, salt, &[], &[], out)
    }

    /// [`Argon2::hash_into_with_ad`], reusing the arena.
    ///
    /// # Errors
    ///
    /// As [`Argon2::hash_into`].
    pub fn hash_into_with_ad(
        &mut self,
        pwd: &[u8],
        salt: &[u8],
        secret: &[u8],
        ad: &[u8],
        out: &mut [u8],
    ) -> Result<(), Error> {
        let argon2 = self.argon2;
        self.hash_into_using(&argon2, pwd, salt, secret, ad, out)
    }

    /// [`Argon2::hash`], reusing the arena.
    ///
    /// # Errors
    ///
    /// As [`Argon2::hash_into`].
    pub fn hash(&mut self, pwd: &[u8], salt: &[u8]) -> Result<Vec<u8>, Error> {
        let mut out = try_zeroed_vec(self.argon2.params.tag_len_bytes())?;
        self.hash_into(pwd, salt, &mut out)?;
        Ok(out)
    }

    /// [`Argon2::hash_encoded`], reusing the arena.
    ///
    /// # Errors
    ///
    /// As [`Argon2::hash_into`], plus [`Error::EncodingFail`].
    pub fn hash_encoded(&mut self, pwd: &[u8], salt: &[u8]) -> Result<String, Error> {
        let argon2 = self.argon2;
        let mut tag = self.hash(pwd, salt)?;
        let encoded = crate::encoding::encode_string_alloc(
            argon2.algorithm,
            argon2.version,
            &argon2.params,
            salt,
            &tag,
        );
        // argon2.c:173 `clear_internal_memory(out, hashlen);`
        clear_internal_memory(&mut tag);
        encoded
    }

    /// [`Argon2::verify`], reusing the arena.
    ///
    /// # Errors
    ///
    /// As [`Argon2::hash_into`], or [`Error::VerifyMismatch`].
    pub fn verify(&mut self, pwd: &[u8], salt: &[u8], expected: &[u8]) -> Result<(), Error> {
        let argon2 = self.argon2;
        self.verify_using(&argon2, pwd, salt, expected)
    }

    /// [`Argon2::verify_encoded`], reusing the arena.
    ///
    /// The parameters come from `encoded`, **not** from this hasher — that is
    /// what verifying a stored PHC string means, and it is what lets one hasher
    /// check strings written at several different `m_cost`s.
    ///
    /// ```
    /// use argon2_rust::{Algorithm, Argon2, Error, Params, Version, params::Memory};
    ///
    /// let params = Params::builder().memory(Memory::kib(64)).passes(1).build()?;
    /// let mut hasher = Argon2::new(Algorithm::Argon2id, Version::V0x13, params).hasher();
    ///
    /// // Registration: one string, carrying the salt and the parameters.
    /// let stored = hasher.hash_encoded(b"password", b"somesalt")?;
    /// assert_eq!(
    ///     stored,
    ///     "$argon2id$v=19$m=64,t=1,p=1$c29tZXNhbHQ$cpx6VEQbwTVZvcpxNIxOVUWZ5xnAipUmAe1cg2GMG70",
    /// );
    ///
    /// // Two logins, over the arena the registration already paid for.
    /// assert_eq!(
    ///     hasher.verify_encoded(&stored, b"password", Algorithm::Argon2id),
    ///     Ok(()),
    /// );
    /// assert_eq!(
    ///     hasher.verify_encoded(&stored, b"wrong", Algorithm::Argon2id),
    ///     Err(Error::VerifyMismatch),
    /// );
    ///
    /// // The string's `m=64` is not above what this hasher already holds, so
    /// // the pool served both verifies and did not grow. See below for what
    /// // happens when a decoded `m_cost` is larger.
    /// assert_eq!(hasher.reserved_blocks(), 64);
    /// # Ok::<(), argon2_rust::Error>(())
    /// ```
    ///
    /// # The string cannot grow this hasher — but it can still be huge
    ///
    /// Read this one first: what follows bounds what an untrusted `m_cost` can
    /// **retain**, and nothing at all about what it can **allocate**. A decoded
    /// `m_cost` of `0xFFFFFFFF` still asks for a 4 TiB arena here, exactly as it
    /// does in [`Argon2::verify_encoded`] and exactly as it does in the C. If
    /// `encoded` comes from anywhere an attacker can write, bound it first —
    /// [`Hasher::verify_encoded_bounded`] does that — or the process dies on the
    /// allocation regardless of everything below.
    ///
    /// `encoded` is untrusted input: on a login endpoint it is whatever the
    /// database row said, and a `m_cost` field is four bytes of decimal that can
    /// ask for 4 TiB. A pooled arena is *retained*, so if a decoded `m_cost`
    /// were allowed to size it, one string would set a permanent high-water mark
    /// on a long-lived per-worker hasher — memory the process never gives back,
    /// chosen by the caller rather than by this hasher's owner.
    ///
    /// So it is not allowed to. A decoded `m_cost` that fits in memory this
    /// hasher already holds — [`reserved_blocks`](Hasher::reserved_blocks), or
    /// the [`params`](Hasher::params) it is configured for — is served from the
    /// pool as usual. One that would have to *grow* the pool gets a private
    /// arena instead, allocated, wiped and freed inside this call exactly as
    /// [`Argon2::verify_encoded`] does. Verifying still works at any `m_cost`
    /// the decoder accepts — including ones that will not fit in this machine.
    /// It just cannot leave anything behind.
    ///
    /// That mirrors the C, where `finalize()` ends every `argon2_ctx` with
    /// `free_memory(...)` (`core.c:184`), so `argon2_verify` never retains an
    /// arena sized by the string it was handed.
    ///
    /// To verify *and* keep the memory — a migration that re-hashes upward, say
    /// — call [`set_argon2`](Hasher::set_argon2) first. Then the size is the
    /// owner's choice, which is the whole distinction being drawn here.
    ///
    /// # Errors
    ///
    /// As [`Argon2::verify_encoded`].
    pub fn verify_encoded(
        &mut self,
        encoded: &str,
        pwd: &[u8],
        algorithm: Algorithm,
    ) -> Result<(), Error> {
        // argon2.c:260-262 `if (pwdlen > ARGON2_MAX_PWD_LENGTH)`.
        if pwd.len() > MAX_PWD_LENGTH as usize {
            return Err(Error::PwdTooLong);
        }

        // argon2.c:289 `decode_string(&ctx, encoded, type)`.
        let decoded = crate::encoding::decode_string(encoded, algorithm)?;

        // argon2.c:302 `argon2_verify_ctx(&ctx, desired_result, type)`.
        let argon2 = Argon2::new(decoded.algorithm, decoded.version, decoded.params);

        if decoded.params.memory_blocks() as usize > self.pooled_ceiling() {
            // Bigger than any arena this hasher's *owner* asked for. Run it on a
            // private arena that is freed on the way out, so an attacker-chosen
            // `m_cost` cannot pin memory to a worker for the rest of its life.
            return argon2.verify(pwd, &decoded.salt, &decoded.hash);
        }
        self.verify_using(&argon2, pwd, &decoded.salt, &decoded.hash)
    }

    /// [`Argon2::verify_encoded_with_ad`], reusing the arena.
    ///
    /// # Errors
    ///
    /// As [`Argon2::verify_encoded_with_ad`].
    pub fn verify_encoded_with_ad(
        &mut self,
        encoded: &str,
        pwd: &[u8],
        secret: &[u8],
        ad: &[u8],
        algorithm: Algorithm,
    ) -> Result<(), Error> {
        // argon2.c:260-262 `if (pwdlen > ARGON2_MAX_PWD_LENGTH)`.
        if pwd.len() > MAX_PWD_LENGTH as usize {
            return Err(Error::PwdTooLong);
        }

        // argon2.c:289 `decode_string(&ctx, encoded, type)`.
        let decoded = crate::encoding::decode_string(encoded, algorithm)?;

        // argon2.c:302 `argon2_verify_ctx(&ctx, desired_result, type)`.
        let argon2 = Argon2::new(decoded.algorithm, decoded.version, decoded.params);

        if decoded.params.memory_blocks() as usize > self.pooled_ceiling() {
            // As `verify_encoded`: keep an attacker-chosen `m_cost` off the
            // pooled arena by running on a one-shot arena instead.
            let mut computed = try_zeroed_vec(argon2.params.tag_len_bytes())?;
            let result =
                argon2.hash_into_with_ad(pwd, &decoded.salt, secret, ad, &mut computed);
            let matched = result.is_ok() && constant_time_eq(&computed, &decoded.hash);
            clear_internal_memory(&mut computed);
            result?;
            return if matched {
                Ok(())
            } else {
                Err(Error::VerifyMismatch)
            };
        }
        self.verify_using_ad(&argon2, pwd, &decoded.salt, secret, ad, &decoded.hash)
    }

    // -----------------------------------------------------------------
    // Password-flavoured spellings, matching `Argon2`'s
    // -----------------------------------------------------------------

    /// Derive a **raw** tag into `out`, not a PHC string, reusing the arena.
    ///
    /// [`Argon2::hash_password_into`] over pooled memory, which is the same
    /// function as [`Hasher::hash_into`]. `out.len()` must equal
    /// [`Params::tag_len_bytes`]. For the PHC string, [`Hasher::hash_password`].
    ///
    /// # Errors
    ///
    /// As [`Argon2::hash_into`].
    #[inline]
    pub fn hash_password_into(
        &mut self,
        pwd: &[u8],
        salt: &[u8],
        out: &mut [u8],
    ) -> Result<(), Error> {
        self.hash_into(pwd, salt, out)
    }

    /// Derive a tag and return the **PHC string** for it, reusing the arena.
    ///
    /// [`Argon2::hash_password`] over pooled memory, which is the same function
    /// as [`Hasher::hash_encoded`]. For the raw tag instead, its sibling
    /// [`Hasher::hash_password_into`] or [`Hasher::hash`].
    ///
    /// # Errors
    ///
    /// As [`Argon2::hash_into`], plus [`Error::EncodingFail`].
    #[inline]
    pub fn hash_password(&mut self, pwd: &[u8], salt: &[u8]) -> Result<String, Error> {
        self.hash_encoded(pwd, salt)
    }

    /// Derive a **PHC string** with a fresh salt from the OS entropy source,
    /// reusing the arena.
    ///
    /// [`Argon2::hash_password_with_random_salt`] over pooled memory, which is
    /// [`Hasher::hash_encoded`] with a [`RANDOM_SALT_LEN`]-byte salt drawn for
    /// you and carried in the returned string. There is no raw-tag counterpart:
    /// a caller who keeps the tag has to keep the salt too, and then generating
    /// it here saves nothing.
    ///
    /// This is the spelling that matters for the case the type exists to serve:
    /// a long-lived per-worker hasher registering many users, where every hash
    /// wants both the pooled arena *and* a fresh salt.
    ///
    /// # Errors
    ///
    /// [`Error::OsRandom`] if every OS entropy source fails, plus the errors of
    /// [`Hasher::hash_encoded`].
    #[cfg(feature = "std")]
    pub fn hash_password_with_random_salt(&mut self, pwd: &[u8]) -> Result<String, Error> {
        // Not wiped on the way out, deliberately, and unlike every other
        // buffer in this file: the salt is *published* in the returned string,
        // so scrubbing the stack copy protects nothing that is not already in
        // the caller's hands. `clear_internal_memory` is for secret-derived
        // material; a salt is not that.
        let mut salt = [0u8; RANDOM_SALT_LEN];
        crate::random::os_random(&mut salt)?;
        self.hash_encoded(pwd, &salt)
    }

    /// Check `pwd` against a **PHC string**, not a raw tag, reusing the arena.
    ///
    /// [`Argon2::verify_password`] over pooled memory, which is the same
    /// function as [`Hasher::verify_encoded`] and inherits its pooled-arena
    /// rule: a decoded `m_cost` above this hasher's high-water mark runs on a
    /// private arena that is freed on the way out, so the string cannot grow
    /// the pool. To check a raw expected tag instead, [`Hasher::verify`].
    ///
    /// # Errors
    ///
    /// As [`Argon2::verify_encoded`].
    #[inline]
    pub fn verify_password(
        &mut self,
        encoded: &str,
        pwd: &[u8],
        algorithm: Algorithm,
    ) -> Result<(), Error> {
        self.verify_encoded(encoded, pwd, algorithm)
    }

    /// [`Argon2::verify_encoded_bounded`], reusing the arena.
    ///
    /// The ceiling is checked before anything is allocated, so it bounds the
    /// *allocation* — which is the half [`Hasher::verify_encoded`] does not
    /// address. Note that the pooled-arena rule still applies underneath: a
    /// decoded `m_cost` within `ceiling` but above this hasher's own high-water
    /// mark runs on a private arena, so passing a generous `ceiling` cannot
    /// enlarge the pool either.
    ///
    /// `ceiling.threads()` bounds the worker threads exactly as it does on
    /// [`Argon2::verify_encoded_bounded`] — worth knowing here in particular,
    /// since a `Hasher` is what a server holds while verifying strings it did
    /// not write, and the arena it reuses is not the only resource a wide `p`
    /// can spend.
    ///
    /// # Errors
    ///
    /// As [`Argon2::verify_encoded_bounded`].
    pub fn verify_encoded_bounded(
        &mut self,
        encoded: &str,
        pwd: &[u8],
        algorithm: Algorithm,
        ceiling: &Params,
    ) -> Result<(), Error> {
        // argon2.c:260-262 `if (pwdlen > ARGON2_MAX_PWD_LENGTH)`.
        if pwd.len() > MAX_PWD_LENGTH as usize {
            return Err(Error::PwdTooLong);
        }

        let decoded = decode_bounded(encoded, algorithm, ceiling)?;

        let argon2 = Argon2::new(decoded.algorithm, decoded.version, decoded.params);
        if decoded.params.memory_blocks() as usize > self.pooled_ceiling() {
            // As `verify_encoded`: keep an m_cost this hasher's owner never
            // asked for off the retained arena.
            return argon2.verify(pwd, &decoded.salt, &decoded.hash);
        }
        self.verify_using(&argon2, pwd, &decoded.salt, &decoded.hash)
    }

    /// [`Argon2::verify_encoded_bounded_with_ad`], reusing the arena.
    ///
    /// # Errors
    ///
    /// As [`Argon2::verify_encoded_bounded_with_ad`].
    pub fn verify_encoded_bounded_with_ad(
        &mut self,
        encoded: &str,
        pwd: &[u8],
        secret: &[u8],
        ad: &[u8],
        algorithm: Algorithm,
        ceiling: &Params,
    ) -> Result<(), Error> {
        // argon2.c:260-262 `if (pwdlen > ARGON2_MAX_PWD_LENGTH)`.
        if pwd.len() > MAX_PWD_LENGTH as usize {
            return Err(Error::PwdTooLong);
        }

        let decoded = decode_bounded(encoded, algorithm, ceiling)?;

        let argon2 = Argon2::new(decoded.algorithm, decoded.version, decoded.params);
        if decoded.params.memory_blocks() as usize > self.pooled_ceiling() {
            // As `verify_encoded_with_ad`: an m_cost this hasher's owner never
            // asked for runs on a one-shot arena.
            let mut computed = try_zeroed_vec(argon2.params.tag_len_bytes())?;
            let result = argon2.hash_into_with_ad(pwd, &decoded.salt, secret, ad, &mut computed);
            let matched = result.is_ok() && constant_time_eq(&computed, &decoded.hash);
            clear_internal_memory(&mut computed);
            result?;
            return if matched {
                Ok(())
            } else {
                Err(Error::VerifyMismatch)
            };
        }
        self.verify_using_ad(&argon2, pwd, &decoded.salt, secret, ad, &decoded.hash)
    }

    // -----------------------------------------------------------------
    // The two private workers every public method above funnels through
    // -----------------------------------------------------------------

    /// The largest arena an *untrusted* `m_cost` may borrow from the pool.
    ///
    /// Two sources, both chosen by whoever owns this hasher, never by an input:
    /// the configuration it was built or [`set_argon2`](Hasher::set_argon2)'d
    /// with, and whatever the workspace already holds (which
    /// [`reserve`](Hasher::reserve) or an earlier, larger configuration may have
    /// made bigger than the current one).
    ///
    /// The guarantee is a ceiling, not a freeze: a decoded `m_cost` under this
    /// bound may still be the thing that allocates the arena, on a hasher whose
    /// owner has not hashed yet. What it cannot do is push the retained arena
    /// past a size the owner has already asked for — so the worst an input can
    /// cost is memory the very next `hash_into` was going to take anyway, and
    /// there is no ratchet.
    ///
    /// The one caller is [`verify_encoded`](Hasher::verify_encoded), because it
    /// is the only method whose `m_cost` does not come from `self`.
    #[inline]
    fn pooled_ceiling(&self) -> usize {
        core::cmp::max(
            self.workspace.capacity(),
            self.argon2.params.memory_blocks() as usize,
        )
    }

    /// `argon2_ctx()` with `argon2`'s configuration and this hasher's memory.
    ///
    /// `argon2` is passed explicitly rather than read from `self` so that
    /// [`verify_encoded`](Hasher::verify_encoded) can use the parameters it
    /// decoded from the string. It is [`Copy`], so callers hand in a copy and
    /// the borrow checker never has to reconcile it with `&mut self.workspace`.
    fn hash_into_using(
        &mut self,
        argon2: &Argon2,
        pwd: &[u8],
        salt: &[u8],
        secret: &[u8],
        ad: &[u8],
        out: &mut [u8],
    ) -> Result<(), Error> {
        // SAFETY: the same argument that makes `Argon2::hash_into_with_ad`
        // safe — the only `Backend` this crate's safe API ever names is
        // `fill_block::backend()`, the cached result of the
        // `is_*_feature_detected!` cascade, so this CPU can execute it by
        // construction.
        unsafe {
            hash_in_workspace(
                &mut self.workspace,
                crate::fill_block::backend(),
                argon2.algorithm,
                argon2.version,
                &argon2.params,
                pwd,
                salt,
                secret,
                ad,
                out,
                None,
                None,
            )
        }
    }

    /// [`Argon2::verify`]'s body, over this hasher's memory.
    fn verify_using(
        &mut self,
        argon2: &Argon2,
        pwd: &[u8],
        salt: &[u8],
        expected: &[u8],
    ) -> Result<(), Error> {
        self.verify_using_ad(argon2, pwd, salt, &[], &[], expected)
    }

    fn verify_using_ad(
        &mut self,
        argon2: &Argon2,
        pwd: &[u8],
        salt: &[u8],
        secret: &[u8],
        ad: &[u8],
        expected: &[u8],
    ) -> Result<(), Error> {
        let mut computed = try_zeroed_vec(argon2.params.tag_len_bytes())?;
        let result = self.hash_into_using(argon2, pwd, salt, secret, ad, &mut computed);
        // argon2.c:349 `argon2_compare(hash, context->out, context->outlen)`.
        let matched = result.is_ok() && constant_time_eq(&computed, expected);
        clear_internal_memory(&mut computed);

        result?;
        if matched {
            Ok(())
        } else {
            Err(Error::VerifyMismatch)
        }
    }
}

impl core::fmt::Debug for Hasher {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Hasher")
            .field("argon2", &self.argon2)
            .field("reserved_blocks", &self.reserved_blocks())
            .finish()
    }
}

/// A zeroed `Vec<u8>` of `len` bytes, without the abort-on-OOM of
/// `Vec::with_capacity`.
fn try_zeroed_vec(len: usize) -> Result<Vec<u8>, Error> {
    let mut v = Vec::new();
    v.try_reserve(len)
        .map_err(|_| Error::MemoryAllocationError)?;
    // Cannot reallocate: the capacity was just reserved.
    v.resize(len, 0);
    Ok(v)
}

/// `argon2_ctx()`: validate, size the arena, initialise, fill, finalise.
///
/// The one place the whole computation lives; every public entry point funnels
/// through here. `backend` is resolved by the caller so the forced-backend test
/// hook and the normal path share this body.
///
/// # Safety
///
/// As [`fill_memory_blocks_traced`]: this CPU must be able to execute `backend`.
// One parameter per `argon2_context` field this port needs; collapsing them into
// a struct would just move the same list somewhere else.
#[allow(clippy::too_many_arguments)]
unsafe fn hash_inner(
    backend: Backend,
    algorithm: Algorithm,
    version: Version,
    params: &Params,
    pwd: &[u8],
    salt: &[u8],
    secret: &[u8],
    ad: &[u8],
    out: &mut [u8],
) -> Result<(), Error> {
    // SAFETY: forwarded verbatim from this function's own contract.
    unsafe {
        hash_owned(
            backend, algorithm, version, params, pwd, salt, secret, ad, out, None, None,
        )
    }
}

/// One-shot hashing over a freshly allocated arena.
///
/// `h0_out` is `None` on every stable API path. That distinction is
/// security-relevant: normal hashing must not materialise a second copy of H0
/// merely to throw it away after the computation. The unstable KAT hook passes
/// a destination because H0 is one of its requested outputs.
///
/// # Safety
///
/// As [`fill_memory_blocks_traced`]: this CPU must be able to execute `backend`.
#[allow(clippy::too_many_arguments)]
unsafe fn hash_owned(
    backend: Backend,
    algorithm: Algorithm,
    version: Version,
    params: &Params,
    pwd: &[u8],
    salt: &[u8],
    secret: &[u8],
    ad: &[u8],
    out: &mut [u8],
    trace: Option<PassTrace<'_>>,
    h0_out: Option<&mut [u8; PREHASH_DIGEST_LENGTH]>,
) -> Result<(), Error> {
    let memory_blocks = validate_and_size(params, pwd, salt, secret, ad, out)?;

    // core.c:621 "1. Memory allocation". A fresh allocation every call, freed
    // on the way out. `Hasher` runs the same computation over an arena borrowed
    // from a `Workspace`.
    let mut arena = Arena::new(memory_blocks)?;

    // SAFETY: `backend` is forwarded verbatim from this function's own
    // contract. `arena` was just sized from the same `params`, and it lives
    // until the end of this function, i.e. past every use inside.
    unsafe {
        hash_in_arena(
            &mut arena, backend, algorithm, version, params, pwd, salt, secret, ad, out, trace,
            h0_out,
        )
    }
    // `arena` drops here: `Arena::drop` wipes it (`zeroize-memory`) and frees
    // it, which is core.c:184's `free_memory(...)`. It drops on `Ok`, `Err` and
    // unwind alike. The two `?`s above fire before the arena exists.
}

/// `argon2_ctx()` with the two hooks `src/genkat.c` needs.
///
/// Returns the 64-byte pre-hashing digest `H0` that `initial_kat()` prints, and
/// invokes `trace(pass, whole_arena)` after each pass, which is what
/// `internal_kat()` prints. `tests/kat.rs` reaches this through `__internal`.
///
/// # Safety
///
/// As [`fill_memory_blocks_traced`]: this CPU must be able to execute `backend`,
/// which `backend.is_available()` or [`crate::fill_block::backend`] establishes.
/// Nothing else here is unsafe — validation, allocation and finalisation are all
/// ordinary safe code — but a `Backend` this CPU lacks makes the fill loop jump
/// into a `#[target_feature]` function it cannot run.
///
/// # Errors
///
/// As [`Argon2::hash_into`].
#[allow(clippy::too_many_arguments)]
pub unsafe fn hash_traced(
    backend: Backend,
    algorithm: Algorithm,
    version: Version,
    params: &Params,
    pwd: &[u8],
    salt: &[u8],
    secret: &[u8],
    ad: &[u8],
    out: &mut [u8],
    trace: Option<PassTrace<'_>>,
) -> Result<[u8; PREHASH_DIGEST_LENGTH], Error> {
    let mut h0 = [0u8; PREHASH_DIGEST_LENGTH];
    // SAFETY: forwarded verbatim from this function's own contract.
    let result = unsafe {
        hash_owned(
            backend,
            algorithm,
            version,
            params,
            pwd,
            salt,
            secret,
            ad,
            out,
            trace,
            Some(&mut h0),
        )
    };
    if let Err(error) = result {
        // H0 was requested as output, but an error means it will not leave this
        // function. Do not turn that failed internal trace into stack residue.
        clear_internal_memory(&mut h0);
        return Err(error);
    }
    Ok(h0)
}

/// Steps 1 and 2 of `argon2_ctx()`: validate every input, then align the memory
/// size. Returns the block count the arena must have.
///
/// Split out so that both arena sources — [`hash_traced`]'s one-shot
/// [`Arena::new`] and [`Hasher`]'s pooled [`Workspace`] — reject bad input
/// *before* anything is allocated, and reject it identically.
///
/// # Errors
///
/// Whatever [`Params::validate_for`] returns, or [`Error::OutPtrMismatch`].
fn validate_and_size(
    params: &Params,
    pwd: &[u8],
    salt: &[u8],
    secret: &[u8],
    ad: &[u8],
    out: &[u8],
) -> Result<usize, Error> {
    // argon2.c:41 "1. Validate all inputs".
    params.validate_for(pwd.len(), salt.len(), secret.len(), ad.len())?;

    // argon2.c:49-51 `ARGON2_INCORRECT_TYPE` cannot fire: `Algorithm` is a
    // closed enum, so there is no "no such version of Argon2".
    //
    // Rust-only check. The C's `context->out` and `context->outlen` are one
    // object; here the buffer and the configured length are separate, so they
    // can disagree. `ARGON2_OUT_PTR_MISMATCH` is defined in `argon2.h` but
    // never returned by the C, which makes it exactly the right code for this.
    if out.len() != params.tag_len_bytes() {
        return Err(Error::OutPtrMismatch);
    }

    // argon2.c:55-70 "2. Align memory size". See `Params::memory_layout`.
    Ok(params.memory_layout().0 as usize)
}

/// Steps 3 to 5 of `argon2_ctx()` over an arena the caller already sized.
///
/// The whole computation lives here — pre-hash, first blocks, fill, finalise —
/// so the one-shot and pooled paths cannot drift apart. Everything they do not
/// share is on either side of this call: where the arena came from, and what
/// happens to it afterwards.
///
/// Deliberately does **not** zero the arena. Argon2 does not need it (pass 0
/// writes every block before anything reads one) and [`Arena`] already
/// guarantees the only property that matters for soundness, which is that every
/// block is *initialised*. See the module docs on [`crate::memory`].
///
/// # Safety
///
/// As [`fill_memory_blocks_traced`]: this CPU must be able to execute `backend`.
///
/// # Errors
///
/// [`Error::MemoryAllocationError`] if `arena.len()` disagrees with
/// `params.memory_layout()`, plus whatever [`initial_hash`],
/// [`fill_first_blocks`], [`fill_memory_blocks_traced`] and [`finalize`] return.
#[allow(clippy::too_many_arguments)]
unsafe fn hash_in_arena(
    arena: &mut Arena,
    backend: Backend,
    algorithm: Algorithm,
    version: Version,
    params: &Params,
    pwd: &[u8],
    salt: &[u8],
    secret: &[u8],
    ad: &[u8],
    out: &mut [u8],
    trace: Option<PassTrace<'_>>,
    h0_out: Option<&mut [u8; PREHASH_DIGEST_LENGTH]>,
) -> Result<(), Error> {
    let (memory_blocks, _segment_length, lane_length) = params.memory_layout();

    // `Instance::new`'s safety contract is `memory_len == memory_blocks`, and
    // below it is handed `arena.len()`. Both callers size the arena from this
    // same `params`, so this can only fire if someone wires up a third one
    // wrongly — at which point it must be an error, not undefined behaviour.
    // A pooled arena whose *capacity* is larger is fine and expected; it is the
    // visible `len()` that has to match.
    if arena.len() != memory_blocks as usize {
        return Err(Error::MemoryAllocationError);
    }

    // The release wipe may use as many threads as the caller sanctioned. It
    // cannot affect the tag, so `threads()` — the OS-thread budget — is the
    // right number here rather than `effective_threads()`, which is
    // `min(threads, lanes)` and describes the *algorithmic* parallelism.
    arena.set_workers(params.threads());

    // core.c:631 "2. Initial hashing". The 8 bytes after `H0` are already zero,
    // which is what core.c:633 achieves with `clear_internal_memory`.
    let mut blockhash = [0u8; PREHASH_SEED_LENGTH];
    if let Err(error) = initial_hash_into(
        algorithm,
        version,
        params,
        pwd,
        salt,
        secret,
        ad,
        &mut blockhash,
    ) {
        clear_internal_memory(&mut blockhash);
        return Err(error);
    }
    if let Some(h0) = h0_out {
        #[cfg(all(test, feature = "std"))]
        H0_COPY_COUNT.with(|count| count.set(count.get() + 1));
        h0.copy_from_slice(&blockhash[..PREHASH_DIGEST_LENGTH]);
    }

    // core.c:643 "3. Creating first blocks".
    let fill_first = fill_first_blocks(
        &mut blockhash,
        arena.as_mut_slice(),
        params.lanes(),
        lane_length,
    );
    // core.c:645 `clear_internal_memory(blockhash, ARGON2_PREHASH_SEED_LENGTH);`
    clear_internal_memory(&mut blockhash);
    fill_first?;

    // SAFETY: `arena` is borrowed for the whole of this function and `instance`
    // does not escape it, so the arena outlives every use of the pointer. It
    // owns `arena.len()` initialised, `ARENA_ALIGN`-aligned `Block`s — that is
    // `Arena`'s invariant 1, and it holds for a pooled arena exactly as it does
    // for a fresh one, since neither reuse nor the release wipe can
    // de-initialise memory. `arena.len() == memory_blocks` was just checked,
    // which is `Instance::new`'s remaining requirement.
    let instance =
        unsafe { Instance::new(arena.as_mut_ptr(), arena.len(), algorithm, version, params) };

    // argon2.c:89 "4. Filling memory".
    // SAFETY: the CPU's ability to execute `backend` is forwarded verbatim from
    // this function's own contract. `instance` was just built from an `Arena`
    // that outlives it, and the arena is uniquely borrowed (`&mut Arena`), so no
    // other thread holds a handle on it.
    unsafe { fill_memory_blocks_traced(&instance, backend, trace) }?;

    // argon2.c:95 "5. Finalization". Wiping and releasing the arena is the
    // caller's job, and it happens on this function's error paths too because
    // both callers do it in a `Drop`.
    finalize(&instance, out)?;

    Ok(())
}

/// [`hash_traced`] over an arena borrowed from `workspace` instead of a fresh
/// one. The engine behind every [`Hasher`] method.
///
/// # Safety
///
/// As [`fill_memory_blocks_traced`]: this CPU must be able to execute `backend`.
///
/// # Errors
///
/// As [`hash_traced`].
#[allow(clippy::too_many_arguments)]
unsafe fn hash_in_workspace(
    workspace: &mut Workspace,
    backend: Backend,
    algorithm: Algorithm,
    version: Version,
    params: &Params,
    pwd: &[u8],
    salt: &[u8],
    secret: &[u8],
    ad: &[u8],
    out: &mut [u8],
    trace: Option<PassTrace<'_>>,
    h0_out: Option<&mut [u8; PREHASH_DIGEST_LENGTH]>,
) -> Result<(), Error> {
    let memory_blocks = validate_and_size(params, pwd, salt, secret, ad, out)?;

    // The whole point: no allocator call and no zeroing memset when the parked
    // arena is already big enough. `acquire` only reallocates when it has to
    // grow, and the previous release left the blocks zeroed.
    let mut arena = workspace.acquire(memory_blocks)?;

    // SAFETY: `backend` is forwarded verbatim from this function's own
    // contract, and `arena` was just sized from the same `params`.
    unsafe {
        hash_in_arena(
            &mut arena, backend, algorithm, version, params, pwd, salt, secret, ad, out, trace,
            h0_out,
        )
    }
    // The `ArenaGuard` drops here and hands the arena back to `workspace` after
    // a `clear_internal_memory_blocks` over exactly the blocks this hash could
    // reach. It drops whether the call above returned `Ok` or `Err`, and on
    // unwind — that is the reason to take a guard rather than an owned `Arena`.
    // Same wipe as `Arena::drop`, same `zeroize-memory` gate, just before the
    // free instead of together with it. The next acquisition therefore starts
    // from a zeroed arena without a second memset, and that saved memset is the
    // entire measured win. The two `?`s above fire before the guard exists.
}

/// Run a hash with a specific [`Backend`], bypassing runtime detection.
///
/// Test and bench hook: lets the suite exercise every backend the host can
/// execute, not just the fastest one.
///
/// # Safety
///
/// This bypasses detection, so **the caller** must establish what detection
/// otherwise would: that this CPU can execute `backend`. `backend.is_available()`
/// is the portable way to do it. See [`fill_memory_blocks_traced`] for the full
/// contract.
///
/// Guarded, and therefore fine:
///
/// ```
/// # use argon2_rust::{Algorithm, Backend, Params, Version, params::Memory};
/// # use argon2_rust::__internal::hash_with_backend;
/// # let params = Params::builder().memory(Memory::kib(8)).passes(1).build().unwrap();
/// # let mut out = [0u8; 32];
/// for &backend in Backend::ALL {
///     if !backend.is_available() {
///         continue; // this CPU would SIGILL
///     }
///     // SAFETY: `is_available()` just said this CPU can execute `backend`.
///     unsafe {
///         hash_with_backend(
///             backend, Algorithm::Argon2id, Version::V0x13, &params,
///             b"password", b"somesaltsomesalt", &[], &[], &mut out,
///         ).unwrap();
///     }
/// }
/// ```
///
/// The **same snippet with the `unsafe` block deleted** must not compile, which
/// is the whole point: safe code cannot reach a `#[target_feature]` function
/// whose feature was never detected. Keep these two in sync — the pair is the
/// regression test, and the runnable one above is what proves the failing one
/// below fails for the right reason rather than through some unrelated typo:
///
/// ```compile_fail
/// # use argon2_rust::{Algorithm, Backend, Params, Version, params::Memory};
/// # use argon2_rust::__internal::hash_with_backend;
/// # let params = Params::builder().memory(Memory::kib(8)).passes(1).build().unwrap();
/// # let mut out = [0u8; 32];
/// for &backend in Backend::ALL {
///     if !backend.is_available() {
///         continue; // this CPU would SIGILL
///     }
///     hash_with_backend(
///         backend, Algorithm::Argon2id, Version::V0x13, &params,
///         b"password", b"somesaltsomesalt", &[], &[], &mut out,
///     ).unwrap();
/// }
/// ```
///
/// # Errors
///
/// As [`Argon2::hash_into`].
///
/// # Panics
///
/// Never.
#[cfg(feature = "internal-api")]
#[allow(clippy::too_many_arguments)]
pub unsafe fn hash_with_backend(
    backend: Backend,
    algorithm: Algorithm,
    version: Version,
    params: &Params,
    pwd: &[u8],
    salt: &[u8],
    secret: &[u8],
    ad: &[u8],
    out: &mut [u8],
) -> Result<(), Error> {
    // SAFETY: forwarded verbatim from this function's own contract.
    unsafe {
        hash_inner(
            backend, algorithm, version, params, pwd, salt, secret, ad, out,
        )
    }
}

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

    // ------------------------------------------------------------------
    // decode_bounded — the worker-thread clamp
    // ------------------------------------------------------------------

    /// The ceiling's `threads` is an OS-thread budget that none of the four
    /// magnitude checks implies.
    ///
    /// Decoding sets `threads = lanes`, and `fill_pooled` spawns
    /// `min(threads, lanes) - 1` helpers, so a string whose `p` is *within* the
    /// `lanes` ceiling used to hand an attacker that many OS threads on an
    /// authentication path. Measured before the clamp: a `p=256` string against
    /// a ceiling of `threads = 1` really did spawn 255 helpers.
    ///
    /// Asserted here rather than by sampling the live thread count, because the
    /// hash is over in milliseconds and a sampler misses the peak — which is
    /// exactly how this was nearly written off as unreproducible.
    #[test]
    fn decode_bounded_clamps_workers_to_the_ceilings_thread_budget() {
        const LANES: u32 = 256;
        let params = Params::builder()
            .memory(Memory::kib(u64::from(8 * LANES)))
            .passes(1)
            .lanes(LANES)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
        let encoded = argon2.hash_encoded(b"pw", b"somesalt").expect("encode");

        // "Strings this wide are allowed; spawning this wide is not."
        let ceiling = Params::builder()
            .memory(Memory::kib(u64::from(8 * LANES)))
            .passes(1)
            .lanes(LANES)
            .threads(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("ceiling");
        let decoded =
            decode_bounded(&encoded, Algorithm::Argon2id, &ceiling).expect("within the ceiling");

        assert_eq!(decoded.params.lanes(), LANES, "lanes must survive: it picks the tag");
        assert_eq!(decoded.params.threads(), 1, "workers must obey the ceiling");
        assert_eq!(decoded.params.effective_threads(), 1);
    }

    /// The clamp only ever lowers. A ceiling that permits more workers than the
    /// string needs must leave the decoded value alone, so the ordinary ceiling
    /// with `.threads()` left unset (where `threads == lanes`) keeps full
    /// parallelism.
    #[test]
    fn decode_bounded_leaves_workers_alone_when_the_ceiling_is_generous() {
        let params = Params::builder()
            .memory(Memory::kib(1 << 10))
            .passes(1)
            .lanes(4)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
        let encoded = argon2.hash_encoded(b"pw", b"somesalt").expect("encode");

        // `.threads()` unset means threads = lanes = 8, more than the string's 4.
        let ceiling = Params::builder()
            .memory(Memory::kib(1 << 16))
            .passes(8)
            .lanes(8)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("ceiling");
        let decoded =
            decode_bounded(&encoded, Algorithm::Argon2id, &ceiling).expect("within the ceiling");

        assert_eq!(decoded.params.lanes(), 4);
        assert_eq!(decoded.params.threads(), 4, "clamped to lanes, not raised to 8");
    }

    // ------------------------------------------------------------------
    // index_alpha
    // ------------------------------------------------------------------

    fn instance_for(params: &Params, algorithm: Algorithm, arena: &mut [Block]) -> Instance {
        // SAFETY: `arena` outlives the returned `Instance` at every call site
        // below, and none of these tests index into it.
        unsafe {
            Instance::new(
                arena.as_mut_ptr(),
                arena.len(),
                algorithm,
                Version::V0x13,
                params,
            )
        }
    }

    #[test]
    fn index_alpha_pass0_slice0_is_all_but_the_previous() {
        let params = Params::builder()
            .memory(Memory::kib(1 << 12))
            .passes(1)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let mut arena = [Block::ZERO; 2];
        let inst = instance_for(&params, Algorithm::Argon2i, &mut arena);

        // reference_area_size = index - 1, start_position = 0, so the result is
        // always < index: index_alpha never returns the block being written.
        for index in 2..64u32 {
            for pseudo in [0u32, 1, 0x7FFF_FFFF, 0x8000_0000, u32::MAX] {
                let pos = Position::new(0, 0, 0, index);
                let alpha = index_alpha(&inst, &pos, pseudo, true);
                assert!(alpha < index, "index={index} pseudo={pseudo} -> {alpha}");
            }
        }
    }

    #[test]
    fn index_alpha_never_selects_the_current_or_a_concurrent_block() {
        // This is the property the parallel safety argument rests on.
        let params = Params::builder()
            .memory(Memory::kib(1024))
            .passes(3)
            .lanes(4)
            .threads(4)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let mut arena = [Block::ZERO; 2];
        let inst = instance_for(&params, Algorithm::Argon2d, &mut arena);
        let seg = inst.segment_length;

        for pass in 0..3u32 {
            for slice in 0..SYNC_POINTS {
                for index in 0..seg {
                    if pass == 0 && slice == 0 && index < 2 {
                        continue;
                    }
                    let pos = Position::new(pass, 1, slice, index);
                    for pseudo in [0u32, 1, 12345, 0x8000_0000, u32::MAX] {
                        // Cross-lane: must land outside the current slice.
                        // `fill_segment` pins `ref_lane = position.lane` on
                        // pass 0 / slice 0, so `same_lane == false` is not
                        // reachable there and the C's answer (block 0, the only
                        // candidate) is a same-lane reference anyway.
                        if !(pass == 0 && slice == 0) {
                            let alpha = index_alpha(&inst, &pos, pseudo, false);
                            let alpha_slice = alpha / seg;
                            assert_ne!(
                                alpha_slice, slice,
                                "cross-lane reference into the live slice: \
                                 pass={pass} slice={slice} index={index} pseudo={pseudo}"
                            );
                        }

                        // Same lane: may be in this slice, but strictly before
                        // the block being written.
                        let alpha = index_alpha(&inst, &pos, pseudo, true);
                        if alpha / seg == slice {
                            assert!(
                                alpha % seg < index,
                                "same-lane reference at or past the current block: \
                                 pass={pass} slice={slice} index={index} -> {alpha}"
                            );
                        }
                    }
                }
            }
        }
    }

    #[test]
    fn index_alpha_wraps_at_index_zero_across_lanes() {
        // The `((index == 0) ? (-1) : 0)` branch. With slice = 1 and
        // segment_length = 2 the C computes reference_area_size = 2 - 1 = 1,
        // so the only legal answer is block 0.
        let params = Params::builder()
            .memory(Memory::kib(8))
            .passes(1)
            .lanes(1)
            .threads(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let mut arena = [Block::ZERO; 2];
        let inst = instance_for(&params, Algorithm::Argon2i, &mut arena);
        assert_eq!(inst.segment_length, 2);

        let pos = Position::new(0, 0, 1, 0);
        for pseudo in [0u32, 1, 0x1234_5678, u32::MAX] {
            assert_eq!(index_alpha(&inst, &pos, pseudo, false), 0);
        }
    }

    #[test]
    fn index_alpha_start_position_skips_the_current_slice() {
        // pass > 0: start_position = (slice + 1) * segment_length, except for
        // the last slice where it is 0.
        let params = Params::builder()
            .memory(Memory::kib(1024))
            .passes(2)
            .lanes(4)
            .threads(4)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let mut arena = [Block::ZERO; 2];
        let inst = instance_for(&params, Algorithm::Argon2d, &mut arena);
        let seg = inst.segment_length;

        // pseudo_rand = 0 makes relative_position = ras - 1, the far end of the
        // window, so the answer is (start_position + ras - 1) % lane_length.
        for slice in 0..SYNC_POINTS {
            let pos = Position::new(1, 0, slice, 5);
            let ras = inst.lane_length - seg + 5 - 1;
            let start = if slice == SYNC_POINTS - 1 {
                0
            } else {
                (slice + 1) * seg
            };
            assert_eq!(
                index_alpha(&inst, &pos, 0, true),
                (start + ras - 1) % inst.lane_length
            );
        }
    }

    /// `reference_area_size - 1` is evaluated in **`uint32_t`**, not `uint64_t`.
    ///
    /// This is the one place the task brief's summary and `core.c` disagree, and
    /// it is invisible to every other test in this repository — mutating
    /// `u64::from(ras.wrapping_sub(1))` into `u64::from(ras).wrapping_sub(1)`
    /// leaves the whole suite green, including all 26 official vectors, the
    /// KATs and a 95 040-case differential against the C. So it is pinned here
    /// directly, against values dumped from the real `index_alpha`.
    ///
    /// The two readings differ only when `reference_area_size == 0`, where the
    /// C gives `relative_position = 0x0000_0000_FFFF_FFFF` and the 64-bit-first
    /// reading gives `0xFFFF_FFFF_FFFF_FFFF`. Both then go through
    /// `% lane_length`, which hides the difference whenever `lane_length`
    /// divides `2^64 - 2^32 = 2^32 * (2^32 - 1)`. Since
    /// `2^32 - 1 = 3 * 5 * 17 * 257 * 65537`, that is true for every power of
    /// two and for `lane_length` 12 and 20 — which is why a grid of "nice"
    /// segment lengths cannot see it. `segment_length` 7, 11, 13, 100 and 341
    /// can.
    ///
    /// `reference_area_size == 0` needs `pass = 0`, `slice = 0`, `index = 1`,
    /// which `fill_segment` never produces (it starts at `index = 2` there), so
    /// this is unreachable through the public API — but it is still what the C
    /// computes, and the next person to "simplify" this line needs a test that
    /// stops them.
    #[test]
    fn index_alpha_reference_area_size_zero_uses_32_bit_arithmetic() {
        // Dumped from the C, `index_alpha(&inst, &{0,0,0,1}, r, 1)`:
        //   seg=2   lane_length=8    -> 7      (does NOT discriminate)
        //   seg=3   lane_length=12   -> 3      (does NOT discriminate)
        //   seg=5   lane_length=20   -> 15     (does NOT discriminate)
        //   seg=7   lane_length=28   -> 3      (64-bit-first would give 15)
        //   seg=11  lane_length=44   -> 3      (64-bit-first would give 15)
        //   seg=13  lane_length=52   -> 47     (64-bit-first would give 15)
        //   seg=100 lane_length=400  -> 95     (64-bit-first would give 15)
        //   seg=341 lane_length=1364 -> 3      (64-bit-first would give 15)
        const CASES: [(u32, u32); 8] = [
            (2, 7),
            (3, 3),
            (5, 15),
            (7, 3),
            (11, 3),
            (13, 47),
            (100, 95),
            (341, 3),
        ];

        let params = Params::builder()
            .memory(Memory::kib(1 << 12))
            .passes(1)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let mut arena = [Block::ZERO; 2];
        let mut inst = instance_for(&params, Algorithm::Argon2i, &mut arena);

        for (segment_length, expected) in CASES {
            inst.segment_length = segment_length;
            inst.lane_length = segment_length * SYNC_POINTS;
            // pass 0, slice 0, index 1  =>  reference_area_size = 1 - 1 = 0.
            let pos = Position::new(0, 0, 0, 1);
            for pseudo in [0u32, 1, 0x7FFF_FFFF, 0x8000_0000, u32::MAX, 0xDEAD_BEEF] {
                // `reference_area_size == 0` makes `(ras * rel) >> 32` zero for
                // every `pseudo_rand`, so the answer does not depend on it.
                assert_eq!(
                    index_alpha(&inst, &pos, pseudo, true),
                    expected,
                    "segment_length={segment_length} pseudo={pseudo:#010x}"
                );
                assert_eq!(index_alpha(&inst, &pos, pseudo, false), expected);
            }
        }
    }

    #[test]
    fn index_alpha_degenerate_instance_does_not_panic() {
        // lane_length == 0 would divide by zero in the C.
        let params = Params::builder()
            .memory(Memory::kib(8))
            .passes(1)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let mut arena = [Block::ZERO; 2];
        let mut inst = instance_for(&params, Algorithm::Argon2i, &mut arena);
        inst.lane_length = 0;
        inst.segment_length = 0;
        assert_eq!(index_alpha(&inst, &Position::new(0, 0, 0, 0), 7, true), 0);
    }

    // ------------------------------------------------------------------
    // constant_time_eq
    // ------------------------------------------------------------------

    #[test]
    fn constant_time_eq_matches_argon2_compare() {
        assert!(constant_time_eq(b"", b""));
        assert!(constant_time_eq(b"abc", b"abc"));
        assert!(!constant_time_eq(b"abc", b"abd"));
        assert!(!constant_time_eq(b"abc", b"abcd"));
        assert!(!constant_time_eq(b"", b"a"));
        // A single differing bit in the last byte.
        assert!(!constant_time_eq(&[0u8; 32], &{
            let mut b = [0u8; 32];
            b[31] = 1;
            b
        }));
        // 0x80 in the high bit: the C's `d - 1` must not sign-extend wrongly.
        assert!(!constant_time_eq(&[0u8; 4], &[0, 0, 0, 0x80]));
    }

    /// Structural guard for the two stable call sites: neither may request the
    /// optional H0 output copy from `hash_in_arena`. This observes that API
    /// choice, not stack contents; the traced call below proves the counter is
    /// live and reserves the copy for the unstable KAT API that returns H0.
    #[cfg(feature = "std")]
    #[test]
    fn stable_hashes_do_not_request_an_h0_output_copy() {
        H0_COPY_COUNT.with(|count| count.set(0));

        let params = Params::builder()
            .memory(Memory::kib(32))
            .passes(1)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
        let mut tag = [0u8; 32];
        argon2
            .hash_into(b"password", b"somesalt", &mut tag)
            .expect("one-shot hash");
        let mut hasher = argon2.hasher();
        hasher
            .hash_into(b"password", b"somesalt", &mut tag)
            .expect("pooled hash");
        H0_COPY_COUNT.with(|count| assert_eq!(count.get(), 0, "stable paths copied H0"));

        // The hook itself must be live or the zero above would prove nothing.
        // SAFETY: the scalar backend is available on every CPU.
        let mut h0 = unsafe {
            hash_traced(
                Backend::Scalar,
                argon2.algorithm,
                argon2.version,
                &argon2.params,
                b"password",
                b"somesalt",
                &[],
                &[],
                &mut tag,
                None,
            )
        }
        .expect("traced hash");
        H0_COPY_COUNT.with(|count| assert_eq!(count.get(), 1, "trace did not copy H0"));
        clear_internal_memory(&mut h0);
    }

    #[test]
    fn fill_first_blocks_rejects_a_short_internal_arena() {
        let mut blockhash = [0xA5; PREHASH_SEED_LENGTH];
        let mut arena = [];
        assert_eq!(
            fill_first_blocks(&mut blockhash, &mut arena, 1, 8),
            Err(Error::IncorrectParameter)
        );
    }

    // ------------------------------------------------------------------
    // initial_hash
    // ------------------------------------------------------------------

    #[test]
    fn initial_hash_matches_the_genkat_pre_hashing_digest() {
        // `phc-winner-argon2/kats/argon2id`, first "Pre-hashing digest" line:
        //   t_cost 3, m_cost 32, lanes 4, outlen 32,
        //   pwd 32 x 0x01, salt 16 x 0x02, secret 8 x 0x03, ad 12 x 0x04.
        let params = Params::builder()
            .memory(Memory::kib(32))
            .passes(3)
            .lanes(4)
            .threads(4)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let h = initial_hash(
            Algorithm::Argon2id,
            Version::V0x13,
            &params,
            &[1u8; 32],
            &[2u8; 16],
            &[3u8; 8],
            &[4u8; 12],
        )
        .expect("initial_hash");

        let expected = "2889de487eb42ae500c0007ed9252f1069eadec40d5765b485de6dc2437a67b8\
                        546a2f0acc1a0882db8fcf74714b472e94df421a5da1112ffa11434370a1e997";
        let mut hex = String::new();
        for byte in &h[..PREHASH_DIGEST_LENGTH] {
            hex.push_str(&alloc::format!("{byte:02x}"));
        }
        assert_eq!(hex, expected);
        // The 8 trailing bytes must be zero before `fill_first_blocks` fills them.
        assert_eq!(&h[PREHASH_DIGEST_LENGTH..], &[0u8; 8]);
    }

    #[test]
    fn initial_hash_field_order_is_load_bearing() {
        // Swapping any two parameters must change H0. Compare `lanes` against
        // `outlen`: both are 4, so a transposition would be invisible unless the
        // values differ.
        let a = Params::builder()
            .memory(Memory::kib(64))
            .passes(1)
            .lanes(2)
            .threads(2)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let b = Params::builder()
            .memory(Memory::kib(64))
            .passes(1)
            .lanes(4)
            .threads(4)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let ha = initial_hash(
            Algorithm::Argon2i,
            Version::V0x13,
            &a,
            b"p",
            b"salt",
            &[],
            &[],
        )
        .expect("h");
        let hb = initial_hash(
            Algorithm::Argon2i,
            Version::V0x13,
            &b,
            b"p",
            b"salt",
            &[],
            &[],
        )
        .expect("h");
        assert_ne!(ha, hb);

        // Version and type are hashed separately.
        let h10 = initial_hash(
            Algorithm::Argon2i,
            Version::V0x10,
            &a,
            b"p",
            b"salt",
            &[],
            &[],
        )
        .expect("h");
        assert_ne!(ha, h10);
        let hid = initial_hash(
            Algorithm::Argon2id,
            Version::V0x13,
            &a,
            b"p",
            b"salt",
            &[],
            &[],
        )
        .expect("h");
        assert_ne!(ha, hid);

        // The length prefixes make "ab" || "" different from "a" || "b".
        let h1 = initial_hash(
            Algorithm::Argon2i,
            Version::V0x13,
            &a,
            b"ab",
            b"saltsalt",
            &[],
            &[],
        )
        .expect("h");
        let h2 = initial_hash(
            Algorithm::Argon2i,
            Version::V0x13,
            &a,
            b"a",
            b"bsaltsalt",
            &[],
            &[],
        )
        .expect("h");
        assert_ne!(h1, h2);
    }

    // ------------------------------------------------------------------
    // The whole pipeline
    // ------------------------------------------------------------------

    fn hex(bytes: &[u8]) -> String {
        let mut s = String::new();
        for byte in bytes {
            s.push_str(&alloc::format!("{byte:02x}"));
        }
        s
    }

    #[test]
    fn one_official_vector_end_to_end() {
        // test.c: Argon2i v=19 t=2 m=1<<16 p=1 "password" / "somesalt".
        let params = Params::builder()
            .memory(Memory::kib(1 << 16))
            .passes(2)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let argon2 = Argon2::new(Algorithm::Argon2i, Version::V0x13, params);
        let tag = argon2.hash(b"password", b"somesalt").expect("hash");
        assert_eq!(
            hex(&tag),
            "c1628832147d9720c5bd1cfd61367078729f6dfb6f8fea9ff98158e0d7816ed0"
        );
    }

    #[test]
    fn genkat_tag_matches_for_all_three_types() {
        // The final "Tag:" line of each `phc-winner-argon2/kats/*` file:
        // t_cost 3, m_cost 32, lanes 4, outlen 32, pwd 32 x 0x01,
        // salt 16 x 0x02, secret 8 x 0x03, ad 12 x 0x04.
        let params = Params::builder()
            .memory(Memory::kib(32))
            .passes(3)
            .lanes(4)
            .threads(4)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        for (algorithm, version, expected) in [
            (
                Algorithm::Argon2d,
                Version::V0x13,
                "512b391b6f1162975371d30919734294f868e3be3984f3c1a13a4db9fabe4acb",
            ),
            (
                Algorithm::Argon2i,
                Version::V0x13,
                "c814d9d1dc7f37aa13f0d77f2494bda1c8de6b016dd388d29952a4c4672b6ce8",
            ),
            (
                Algorithm::Argon2id,
                Version::V0x13,
                "0d640df58d78766c08c037a34a8b53c9d01ef0452d75b65eb52520e96b01e659",
            ),
            (
                Algorithm::Argon2d,
                Version::V0x10,
                "96a9d4e5a1734092c85e29f410a45914a5dd1f5cbf08b2670da68a0285abf32b",
            ),
            (
                Algorithm::Argon2i,
                Version::V0x10,
                "87aeedd6517ab830cd9765cd8231abb2e647a5dee08f7c05e02fcb763335d0fd",
            ),
            (
                Algorithm::Argon2id,
                Version::V0x10,
                "b64615f07789b66b645b67ee9ed3b377ae350b6bfcbb0fc95141ea8f322613c0",
            ),
        ] {
            let argon2 = Argon2::new(algorithm, version, params);
            let mut tag = [0u8; 32];
            argon2
                .hash_into_with_ad(&[1u8; 32], &[2u8; 16], &[3u8; 8], &[4u8; 12], &mut tag)
                .expect("hash");
            assert_eq!(hex(&tag), expected, "{algorithm:?} {version:?}");
        }
    }

    /// The whole single-threaded pipeline on the smallest legal instance.
    ///
    /// `m_cost = MIN_MEMORY = 8` blocks, one lane, so the arena is 8 KiB and one
    /// pass is 4 slices of `segment_length = 2`. Small enough that
    /// `cargo +nightly miri test --lib tiny_` can run allocate → `initial_hash`
    /// → `fill_first_blocks` → `fill_segment` → `finalize` → wipe → free end to
    /// end. Ground truth from the C reference:
    ///
    /// ```text
    /// printf password | ./argon2 somesalt -{i,d,id} -t 1 -m 3 -p 1 -l 32 -r
    /// ```
    #[test]
    fn tiny_single_threaded_hash_matches_the_c_reference() {
        let params = Params::builder()
            .memory(Memory::kib(8))
            .passes(1)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        assert_eq!(params.memory_layout(), (8, 2, 8));
        for (algorithm, expected) in [
            (
                Algorithm::Argon2i,
                "cbf2bce47e6d23999626143fabc5db69164743ee000ddd3f8895a6f82cfb9a6e",
            ),
            (
                Algorithm::Argon2d,
                "c519e603ac603ec1aeb5b71ec44a6179e3f3975b14c0c97e3914c79e6363e178",
            ),
            (
                Algorithm::Argon2id,
                "f137f8e186a403a679ccd0606e5ab5dcdafe43c1640855ac8c6e33e9bd63eeb3",
            ),
        ] {
            let mut tag = [0u8; 32];
            Argon2::new(algorithm, Version::V0x13, params)
                .hash_into(b"password", b"somesalt", &mut tag)
                .expect("hash");
            assert_eq!(hex(&tag), expected, "{algorithm:?}");
        }
    }

    /// The same, two lanes and two passes, so the multi-threaded path and the
    /// cross-lane `index_alpha` branches are exercised under Miri too.
    ///
    /// ```text
    /// printf password | ./argon2 somesalt -{i,d,id} -t 2 -m 4 -p 2 -l 32 -r
    /// ```
    #[test]
    fn tiny_two_lane_hash_matches_the_c_reference() {
        let params = Params::builder()
            .memory(Memory::kib(16))
            .passes(2)
            .lanes(2)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        assert_eq!(params.memory_layout(), (16, 2, 8));
        for (algorithm, expected) in [
            (
                Algorithm::Argon2i,
                "7fbb85db7e9636115f2fd0f29ea4214baaada18b39fffed7875eeb9fa9b308c5",
            ),
            (
                Algorithm::Argon2d,
                "59f20a66a4c31bf0438a2f494867c32120409a91380f0687aefee984ba86bda8",
            ),
            (
                Algorithm::Argon2id,
                "747d7631b182faf749d7efc31aec31df4ecfe3b57c792f53800ac2c9978b4888",
            ),
        ] {
            let mut tag = [0u8; 32];
            Argon2::new(algorithm, Version::V0x13, params)
                .hash_into(b"password", b"somesalt", &mut tag)
                .expect("hash");
            assert_eq!(hex(&tag), expected, "{algorithm:?} (threads = lanes = 2)");
        }
    }

    #[test]
    fn out_length_mismatch_is_out_ptr_mismatch() {
        let params = Params::builder()
            .memory(Memory::kib(1 << 8))
            .passes(1)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
        let mut out = [0u8; 16];
        assert_eq!(
            argon2.hash_into(b"password", b"somesalt", &mut out),
            Err(Error::OutPtrMismatch)
        );
    }

    #[test]
    fn trace_fires_once_per_pass_with_the_whole_arena() {
        let params = Params::builder()
            .memory(Memory::kib(1 << 8))
            .passes(3)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let mut passes = alloc::vec::Vec::new();
        let mut out = [0u8; 32];
        let mut trace = |pass: u32, blocks: &[Block]| {
            passes.push((pass, blocks.len()));
        };
        // SAFETY: `backend()` is what runtime detection picked for this CPU.
        let h0 = unsafe {
            hash_traced(
                crate::fill_block::backend(),
                Algorithm::Argon2id,
                Version::V0x13,
                &params,
                b"password",
                b"somesalt",
                &[],
                &[],
                &mut out,
                Some(&mut trace),
            )
        }
        .expect("hash_traced");

        assert_eq!(passes, alloc::vec![(0, 256), (1, 256), (2, 256)]);
        assert_eq!(h0.len(), PREHASH_DIGEST_LENGTH);
    }

    /// A panic on the leader must propagate, not deadlock the pool.
    ///
    /// The worker pool spans the whole fill and its helpers park on a spin
    /// barrier between slices. If the leader unwinds out of `thread::scope`
    /// without releasing them, `Scope`'s `Drop` blocks for ever joining threads
    /// that are waiting for a `generation` bump that is never coming — the
    /// crate hangs instead of failing. This test reaches that path through the
    /// one leader-side callback that exists, and it is the reason
    /// `ReleaseHelpers` is a `Drop` guard rather than a line at the end of the
    /// loop.
    ///
    /// If this regresses, it does not fail — it hangs. That is the point.
    #[test]
    #[cfg(feature = "parallel")]
    // wasip1 is panic=abort: there is no unwinding to test there.
    #[cfg_attr(target_arch = "wasm32", ignore = "no unwinding on wasi (panic=abort)")]
    fn a_panicking_trace_callback_unwinds_instead_of_deadlocking_the_pool() {
        // 4 lanes and 4 threads, so there really are helpers parked on the
        // barrier when the callback runs.
        let params = Params::builder()
            .memory(Memory::kib(64))
            .passes(2)
            .lanes(4)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let mut arena = Arena::new(params.memory_blocks() as usize).expect("arena");
        let mut blockhash = initial_hash(
            Algorithm::Argon2id,
            Version::V0x13,
            &params,
            b"password",
            b"somesaltsomesalt",
            &[],
            &[],
        )
        .expect("H0");
        let (_, _, lane_length) = params.memory_layout();
        fill_first_blocks(
            &mut blockhash,
            arena.as_mut_slice(),
            params.lanes(),
            lane_length,
        )
        .expect("first blocks");

        let caught = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            // SAFETY: `arena` was sized from `params` and outlives `instance`.
            let instance = unsafe {
                Instance::new(
                    arena.as_mut_ptr(),
                    arena.len(),
                    Algorithm::Argon2id,
                    Version::V0x13,
                    &params,
                )
            };
            let mut boom = |_pass: u32, _blocks: &[Block]| panic!("trace exploded");
            // SAFETY: `Backend::Scalar` runs anywhere, and `instance` is valid.
            unsafe {
                fill_memory_blocks_traced(&instance, Backend::Scalar, Some(&mut boom)).expect("fill")
            };
        }));

        assert!(caught.is_err(), "the callback's panic must reach the caller");
    }

    #[test]
    fn threads_do_not_change_the_tag() {
        // Spec item (12): only `lanes` affects the tag.
        for lanes in [2u32, 4] {
            let single = Params::builder()
                .memory(Memory::kib(1 << 10))
                .passes(2)
                .lanes(lanes)
                .threads(1)
                .tag_len(TagLen::bytes(32))
                .build()
                .expect("params");
            let multi = Params::builder()
                .memory(Memory::kib(1 << 10))
                .passes(2)
                .lanes(lanes)
                .threads(lanes)
                .tag_len(TagLen::bytes(32))
                .build()
                .expect("params");
            let a = Argon2::new(Algorithm::Argon2id, Version::V0x13, single)
                .hash(b"password", b"somesalt")
                .expect("st");
            let b = Argon2::new(Algorithm::Argon2id, Version::V0x13, multi)
                .hash(b"password", b"somesalt")
                .expect("mt");
            assert_eq!(a, b, "lanes={lanes}");
        }
    }

    #[test]
    fn verify_round_trips_and_rejects() {
        let params = Params::builder()
            .memory(Memory::kib(1 << 8))
            .passes(2)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
        let encoded = argon2.hash_encoded(b"password", b"somesalt").expect("enc");
        assert!(encoded.starts_with("$argon2id$v=19$m=256,t=2,p=1$c29tZXNhbHQ$"));

        assert_eq!(
            Argon2::verify_encoded(&encoded, b"password", Algorithm::Argon2id),
            Ok(())
        );
        assert_eq!(
            Argon2::verify_encoded(&encoded, b"passwore", Algorithm::Argon2id),
            Err(Error::VerifyMismatch)
        );
        assert_eq!(
            Argon2::verify_encoded(&encoded, b"password", Algorithm::Argon2i),
            Err(Error::DecodingFail)
        );

        let tag = argon2.hash(b"password", b"somesalt").expect("hash");
        assert_eq!(argon2.verify(b"password", b"somesalt", &tag), Ok(()));
        assert_eq!(
            argon2.verify(b"password", b"somesalt", &tag[..16]),
            Err(Error::VerifyMismatch)
        );
    }

    #[test]
    fn password_flavoured_names_are_the_same_functions() {
        let params = Params::builder()
            .memory(Memory::kib(1 << 8))
            .passes(2)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);

        let mut a = [0u8; 32];
        let mut b = [0u8; 32];
        argon2
            .hash_into(b"password", b"somesalt", &mut a)
            .expect("hash_into");
        argon2
            .hash_password_into(b"password", b"somesalt", &mut b)
            .expect("hash_password_into");
        assert_eq!(a, b);

        let encoded = argon2.hash_password(b"password", b"somesalt").expect("enc");
        assert_eq!(
            encoded,
            argon2.hash_encoded(b"password", b"somesalt").expect("enc")
        );
        assert!(encoded.starts_with("$argon2id$v=19$m=256,t=2,p=1$c29tZXNhbHQ$"));

        assert_eq!(
            Argon2::verify_password(&encoded, b"password", Algorithm::Argon2id),
            Ok(())
        );
        assert_eq!(
            Argon2::verify_password(&encoded, b"passwore", Algorithm::Argon2id),
            Err(Error::VerifyMismatch)
        );
    }

    // ------------------------------------------------------------------
    // Hasher — the pooled arena
    // ------------------------------------------------------------------

    /// Everything one hash can be observed to produce: the pre-hashing digest,
    /// the whole arena after every pass, and the tag.
    ///
    /// The arena dumps are the point. A tag comparison would prove the two
    /// paths agree; a word-by-word arena comparison proves they agree *for the
    /// same reason*, and says exactly which block diverged when they do not.
    /// This is `genkat.c`'s `internal_kat` output in memory instead of on
    /// stdout — the same evidence `tests/kat.rs` checks against the golden
    /// files, applied to the one axis those files cannot see: where the arena
    /// came from.
    type Dump = (
        alloc::vec::Vec<(u32, alloc::vec::Vec<Block>)>,
        [u8; PREHASH_DIGEST_LENGTH],
        [u8; 32],
    );

    /// One hash down the one-shot path (`Arena::new` .. `Arena::drop`).
    ///
    /// # Safety
    ///
    /// This CPU must be able to execute `backend`.
    unsafe fn dump_one_shot(backend: Backend, argon2: &Argon2, pwd: &[u8], salt: &[u8]) -> Dump {
        let mut tag = [0u8; 32];
        let mut passes: alloc::vec::Vec<(u32, alloc::vec::Vec<Block>)> = alloc::vec::Vec::new();
        let mut trace = |pass: u32, blocks: &[Block]| passes.push((pass, blocks.to_vec()));
        // SAFETY: forwarded verbatim from this function's own contract.
        let h0 = unsafe {
            hash_traced(
                backend,
                argon2.algorithm,
                argon2.version,
                &argon2.params,
                pwd,
                salt,
                &[3u8; 8],
                &[4u8; 12],
                &mut tag,
                Some(&mut trace),
            )
        }
        .expect("one-shot hash");
        (passes, h0, tag)
    }

    /// The same hash over an arena borrowed from `workspace`.
    ///
    /// # Safety
    ///
    /// This CPU must be able to execute `backend`.
    unsafe fn dump_pooled(
        workspace: &mut Workspace,
        backend: Backend,
        argon2: &Argon2,
        pwd: &[u8],
        salt: &[u8],
    ) -> Dump {
        let mut tag = [0u8; 32];
        let mut h0 = [0u8; PREHASH_DIGEST_LENGTH];
        let mut passes: alloc::vec::Vec<(u32, alloc::vec::Vec<Block>)> = alloc::vec::Vec::new();
        let mut trace = |pass: u32, blocks: &[Block]| passes.push((pass, blocks.to_vec()));
        // SAFETY: forwarded verbatim from this function's own contract.
        unsafe {
            hash_in_workspace(
                workspace,
                backend,
                argon2.algorithm,
                argon2.version,
                &argon2.params,
                pwd,
                salt,
                &[3u8; 8],
                &[4u8; 12],
                &mut tag,
                Some(&mut trace),
                Some(&mut h0),
            )
        }
        .expect("pooled hash");
        (passes, h0, tag)
    }

    /// Report the *first* divergence, not a 96 KiB `assert_eq!` diff.
    fn assert_same_dump(what: &str, expected: &Dump, actual: &Dump) {
        assert_eq!(actual.1, expected.1, "{what}: H0 differs");
        assert_eq!(actual.0.len(), expected.0.len(), "{what}: pass count");

        for (want, got) in expected.0.iter().zip(actual.0.iter()) {
            assert_eq!(got.0, want.0, "{what}: pass index");
            assert_eq!(
                got.1.len(),
                want.1.len(),
                "{what}: arena length after pass {}",
                want.0
            );
            for (block, (wb, gb)) in want.1.iter().zip(got.1.iter()).enumerate() {
                for (word, (w, g)) in wb.0.iter().zip(gb.0.iter()).enumerate() {
                    assert_eq!(
                        g, w,
                        "{what}: pass {}, block {block}, word {word}",
                        want.0
                    );
                }
            }
        }
        assert_eq!(actual.2, expected.2, "{what}: tag differs");
    }

    /// The headline correctness claim, checked at the strongest granularity
    /// available: a pooled hash must produce a **byte-identical arena** at every
    /// pass boundary, not merely an identical tag.
    ///
    /// Rounds 1 and 2 are the ones that matter — round 0 runs on a
    /// freshly-allocated arena, so only a later round can catch a reused arena
    /// leaking a previous tenant's bytes into the computation. `genkat.c`'s
    /// parameters are used because they are the ones the golden files pin, and
    /// `lanes = threads = 4` puts the `std::thread::scope` path under the same
    /// check as the single-threaded one.
    #[test]
    fn a_pooled_hash_reproduces_the_one_shot_arena_word_for_word() {
        let params = Params::builder()
            .memory(Memory::kib(32))
            .passes(3)
            .lanes(4)
            .threads(4)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");

        for algorithm in [Algorithm::Argon2d, Algorithm::Argon2i, Algorithm::Argon2id] {
            for version in [Version::V0x10, Version::V0x13] {
                let argon2 = Argon2::new(algorithm, version, params);

                for &backend in Backend::ALL {
                    if !backend.is_available() {
                        continue; // this CPU would SIGILL
                    }
                    // SAFETY: guarded by `is_available()` immediately above.
                    let expected =
                        unsafe { dump_one_shot(backend, &argon2, &[1u8; 32], &[2u8; 16]) };

                    let mut workspace = Workspace::new();
                    for round in 0..3 {
                        // SAFETY: as above.
                        let actual = unsafe {
                            dump_pooled(&mut workspace, backend, &argon2, &[1u8; 32], &[2u8; 16])
                        };
                        assert_same_dump(
                            &alloc::format!("{algorithm:?} {version:?} {backend} round {round}"),
                            &expected,
                            &actual,
                        );
                    }
                }
            }
        }
    }

    /// The `Hasher` API itself, against the one-shot API, over enough parameter
    /// shapes to cover single-threaded, multi-lane threaded, and multi-pass.
    #[test]
    fn hasher_agrees_with_the_one_shot_api() {
        let configs = [
            Params::builder()
                .memory(Memory::kib(8))
                .passes(1)
                .lanes(1)
                .tag_len(TagLen::bytes(32))
                .build()
                .expect("minimum"),
            Params::builder()
                .memory(Memory::kib(1 << 8))
                .passes(2)
                .lanes(1)
                .tag_len(TagLen::bytes(32))
                .build()
                .expect("st"),
            Params::builder()
                .memory(Memory::kib(1 << 9))
                .passes(2)
                .lanes(4)
                .threads(4)
                .tag_len(TagLen::bytes(32))
                .build()
                .expect("mt"),
            Params::builder()
                .memory(Memory::kib(64))
                .passes(3)
                .lanes(2)
                .threads(2)
                .tag_len(TagLen::bytes(24))
                .build()
                .expect("odd outlen"),
        ];

        for params in configs {
            for algorithm in [Algorithm::Argon2d, Algorithm::Argon2i, Algorithm::Argon2id] {
                let argon2 = Argon2::new(algorithm, Version::V0x13, params);
                let mut hasher = argon2.hasher();

                for round in 0..4u8 {
                    let pwd = [round; 7];
                    let mut want = alloc::vec![0u8; params.tag_len_bytes()];
                    let mut got = alloc::vec![0u8; params.tag_len_bytes()];

                    argon2.hash_into(&pwd, b"somesalt", &mut want).expect("one");
                    hasher.hash_into(&pwd, b"somesalt", &mut got).expect("pool");
                    assert_eq!(got, want, "{algorithm:?} round {round}");

                    // ...and with a secret and associated data.
                    argon2
                        .hash_into_with_ad(&pwd, b"somesalt", &[3u8; 8], &[4u8; 12], &mut want)
                        .expect("one ad");
                    hasher
                        .hash_into_with_ad(&pwd, b"somesalt", &[3u8; 8], &[4u8; 12], &mut got)
                        .expect("pool ad");
                    assert_eq!(got, want, "{algorithm:?} round {round} with ad");
                }
            }
        }
    }

    /// The pooled counterparts of `tiny_single_threaded_hash_matches_the_c_reference`
    /// and `tiny_two_lane_hash_matches_the_c_reference`, against the same ground
    /// truth from the C reference.
    ///
    /// Sized so that `cargo +nightly miri test --lib tiny_` can run the whole
    /// new path end to end: acquire → hash → release-and-wipe → **re**-acquire →
    /// hash. The two-lane half puts the `std::thread::scope` raw-pointer sharing
    /// over an arena that has already been used once, which is the one piece of
    /// unsafe territory reuse actually changes. The growth step at the end makes
    /// Miri watch the old allocation being freed while the new one is filled.
    #[test]
    fn tiny_pooled_hashes_match_the_c_reference() {
        // `printf password | ./argon2 somesalt -id -t 1 -m 3 -p 1 -l 32 -r`
        let one_lane = Params::builder()
            .memory(Memory::kib(8))
            .passes(1)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        // `printf password | ./argon2 somesalt -id -t 2 -m 4 -p 2 -l 32 -r`
        let two_lane = Params::builder()
            .memory(Memory::kib(16))
            .passes(2)
            .lanes(2)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");

        let mut hasher = Argon2::new(Algorithm::Argon2id, Version::V0x13, one_lane).hasher();
        let mut tag = [0u8; 32];

        for round in 0..2 {
            hasher
                .hash_into(b"password", b"somesalt", &mut tag)
                .expect("single lane");
            assert_eq!(
                hex(&tag),
                "f137f8e186a403a679ccd0606e5ab5dcdafe43c1640855ac8c6e33e9bd63eeb3",
                "single lane, round {round}"
            );
        }

        // Same hasher, wider configuration: the arena grows once, then reuses.
        hasher.set_argon2(Argon2::new(
            Algorithm::Argon2id,
            Version::V0x13,
            two_lane,
        ));
        for round in 0..2 {
            hasher
                .hash_into(b"password", b"somesalt", &mut tag)
                .expect("two lanes");
            assert_eq!(
                hex(&tag),
                "747d7631b182faf749d7efc31aec31df4ecfe3b57c792f53800ac2c9978b4888",
                "two lanes, round {round}"
            );
        }

        // And back down: the big arena is kept and re-lent as a narrow window.
        hasher.set_argon2(Argon2::new(
            Algorithm::Argon2id,
            Version::V0x13,
            one_lane,
        ));
        hasher
            .hash_into(b"password", b"somesalt", &mut tag)
            .expect("single lane again");
        assert_eq!(
            hex(&tag),
            "f137f8e186a403a679ccd0606e5ab5dcdafe43c1640855ac8c6e33e9bd63eeb3"
        );
        assert_eq!(hasher.reserved_blocks(), two_lane.memory_blocks() as usize);
    }

    /// Reuse is not a claim, it is an address: every hash after the first must
    /// land on the same allocation.
    #[test]
    fn reuse_lands_on_one_allocation() {
        let params = Params::builder()
            .memory(Memory::kib(1 << 8))
            .passes(2)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let blocks = params.memory_blocks() as usize;
        let mut hasher = Argon2::new(Algorithm::Argon2id, Version::V0x13, params).hasher();

        assert_eq!(hasher.reserved_blocks(), 0, "nothing allocated up front");

        let mut tag = [0u8; 32];
        hasher.hash_into(b"password", b"somesalt", &mut tag).expect("first");
        assert_eq!(hasher.reserved_blocks(), blocks);

        // Peek at the parked arena the way the next hash would. The guard drops
        // at the end of the statement, handing it straight back.
        let first = hasher.workspace.acquire(blocks).expect("peek").as_ptr();
        for round in 0..8 {
            hasher.hash_into(b"password", b"somesalt", &mut tag).expect("again");
            assert_eq!(
                hasher.workspace.acquire(blocks).expect("peek").as_ptr(),
                first,
                "round {round} reallocated"
            );
        }
        assert_eq!(hasher.reserved_blocks(), blocks);
    }

    /// The control for the wipe test below, and a fact worth pinning in its own
    /// right: a finished hash leaves the **whole** arena full of material
    /// derived from that password. There is something real to wipe.
    ///
    /// Without this, `the_arena_a_hash_borrowed_comes_back_wiped` would be
    /// worthless — an arena that was never written would also read as all-zero.
    #[test]
    fn a_finished_hash_leaves_the_whole_arena_full_of_derived_material() {
        let params = Params::builder()
            .memory(Memory::kib(1 << 8))
            .passes(2)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let mut arena = Arena::new(params.memory_blocks() as usize).expect("arena");
        let mut out = [0u8; 32];

        // SAFETY: `Backend::Scalar` is available on every CPU.
        unsafe {
            hash_in_arena(
                &mut arena,
                Backend::Scalar,
                Algorithm::Argon2id,
                Version::V0x13,
                &params,
                b"password",
                b"somesalt",
                &[],
                &[],
                &mut out,
                None,
                None,
            )
        }
        .expect("hash");

        let dirty = arena.as_slice().iter().filter(|b| **b != Block::ZERO).count();
        assert_eq!(
            dirty,
            arena.len(),
            "every block should still hold derived material before the wipe"
        );
    }

    /// The security property reuse must not weaken: the arena is wiped when the
    /// call that borrowed it returns, so what is parked between calls is zero,
    /// not the last password's derived material.
    ///
    /// Its control is
    /// [`a_finished_hash_leaves_the_whole_arena_full_of_derived_material`],
    /// which proves the bytes this test demands be gone were there to begin
    /// with.
    #[test]
    #[cfg(feature = "zeroize-memory")]
    fn the_arena_a_hash_borrowed_comes_back_wiped() {
        let params = Params::builder()
            .memory(Memory::kib(1 << 8))
            .passes(2)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let blocks = params.memory_blocks() as usize;
        let mut hasher = Argon2::new(Algorithm::Argon2id, Version::V0x13, params).hasher();

        let mut tag = [0u8; 32];
        for round in 0..3 {
            hasher.hash_into(b"password", b"somesalt", &mut tag).expect("hash");
            let parked = hasher.workspace.acquire(blocks).expect("peek");
            assert!(
                parked.as_slice().iter().all(|b| *b == Block::ZERO),
                "round {round}: the arena still holds derived material"
            );
        }
    }

    /// A hash that fails validation must leave the hasher exactly as it was —
    /// no half-released arena, no lost capacity, no wrong answer afterwards.
    #[test]
    fn an_error_does_not_disturb_reuse() {
        let params = Params::builder()
            .memory(Memory::kib(1 << 8))
            .passes(2)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let blocks = params.memory_blocks() as usize;
        let mut hasher = Argon2::new(Algorithm::Argon2id, Version::V0x13, params).hasher();

        let mut tag = [0u8; 32];
        hasher.hash_into(b"password", b"somesalt", &mut tag).expect("warm up");
        let before = hasher.workspace.acquire(blocks).expect("peek").as_ptr();

        // Wrong output length: rejected before anything is allocated.
        let mut short = [0u8; 16];
        assert_eq!(
            hasher.hash_into(b"password", b"somesalt", &mut short),
            Err(Error::OutPtrMismatch)
        );
        // Salt too short: rejected by `validate_for`.
        assert!(hasher.hash_into(b"password", b"salt", &mut tag).is_err());

        assert_eq!(hasher.reserved_blocks(), blocks, "capacity survived");
        assert_eq!(
            hasher.workspace.acquire(blocks).expect("peek").as_ptr(),
            before,
            "and it is the same allocation"
        );

        let mut after = [0u8; 32];
        hasher.hash_into(b"password", b"somesalt", &mut after).expect("still works");
        assert_eq!(after, tag);
    }

    /// One hasher, several configurations. Growth reallocates once; shrinking
    /// keeps the big arena; every answer still matches the one-shot API.
    #[test]
    fn changing_the_configuration_keeps_the_memory_and_the_answers() {
        let small = Params::builder()
            .memory(Memory::kib(1 << 8))
            .passes(1)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("small");
        let large = Params::builder()
            .memory(Memory::kib(1 << 10))
            .passes(1)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("large");
        let mut hasher = Argon2::new(Algorithm::Argon2id, Version::V0x13, small).hasher();

        let mut tag = [0u8; 32];
        let mut want = [0u8; 32];

        for (params, label) in [(small, "small"), (large, "large"), (small, "small again")] {
            let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
            hasher.set_argon2(argon2);
            assert_eq!(hasher.params().memory_kib(), params.memory_kib(), "{label}");
            assert_eq!(hasher.algorithm(), Algorithm::Argon2id);
            assert_eq!(hasher.version(), Version::V0x13);
            assert_eq!(hasher.argon2(), &argon2);

            hasher.hash_into(b"password", b"somesalt", &mut tag).expect(label);
            argon2.hash_into(b"password", b"somesalt", &mut want).expect(label);
            assert_eq!(tag, want, "{label}");
        }

        assert_eq!(
            hasher.reserved_blocks(),
            large.memory_blocks() as usize,
            "a smaller configuration must not shrink the arena"
        );
    }

    /// `reserve` front-loads the allocation; `clear` gives it back. Neither
    /// changes an answer.
    #[test]
    fn reserve_and_clear_move_the_allocation_around() {
        let params = Params::builder()
            .memory(Memory::kib(1 << 8))
            .passes(1)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
        let mut hasher = argon2.hasher();

        hasher.reserve().expect("reserve");
        assert_eq!(hasher.reserved_blocks(), params.memory_blocks() as usize);
        let reserved = hasher
            .workspace
            .acquire(params.memory_blocks() as usize)
            .expect("peek")
            .as_ptr();

        let mut tag = [0u8; 32];
        hasher.hash_into(b"password", b"somesalt", &mut tag).expect("hash");
        assert_eq!(
            hasher
                .workspace
                .acquire(params.memory_blocks() as usize)
                .expect("peek")
                .as_ptr(),
            reserved,
            "the first hash must use the reserved arena, not a new one"
        );

        hasher.clear();
        assert_eq!(hasher.reserved_blocks(), 0);

        let mut again = [0u8; 32];
        hasher.hash_into(b"password", b"somesalt", &mut again).expect("after clear");
        assert_eq!(again, tag);
        assert_eq!(hasher.reserved_blocks(), params.memory_blocks() as usize);
    }

    /// The encoded and verifying halves of the API, including the one method
    /// that takes its parameters from the string rather than from the hasher.
    #[test]
    fn hasher_encodes_and_verifies_like_argon2() {
        let params = Params::builder()
            .memory(Memory::kib(1 << 8))
            .passes(2)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
        let mut hasher = argon2.hasher();

        let encoded = hasher.hash_encoded(b"password", b"somesalt").expect("enc");
        assert_eq!(
            encoded,
            argon2.hash_encoded(b"password", b"somesalt").expect("enc")
        );
        assert_eq!(
            encoded,
            hasher.hash_password(b"password", b"somesalt").expect("enc")
        );

        assert_eq!(
            hasher.verify_encoded(&encoded, b"password", Algorithm::Argon2id),
            Ok(())
        );
        assert_eq!(
            hasher.verify_password(&encoded, b"passwore", Algorithm::Argon2id),
            Err(Error::VerifyMismatch)
        );
        assert_eq!(
            hasher.verify_encoded(&encoded, b"password", Algorithm::Argon2i),
            Err(Error::DecodingFail)
        );

        let tag = hasher.hash(b"password", b"somesalt").expect("hash");
        assert_eq!(tag, argon2.hash(b"password", b"somesalt").expect("hash"));
        assert_eq!(hasher.verify(b"password", b"somesalt", &tag), Ok(()));
        assert_eq!(
            hasher.verify(b"password", b"somesalt", &tag[..16]),
            Err(Error::VerifyMismatch)
        );

        let mut into = [0u8; 32];
        hasher
            .hash_password_into(b"password", b"somesalt", &mut into)
            .expect("hash_password_into");
        assert_eq!(&into[..], &tag[..]);
    }

    /// `verify_encoded` reads `m_cost` out of the string, so one hasher can be
    /// pointed at strings written at different costs. All of them must verify —
    /// and none of them may leave the hasher any bigger than its *owner* made
    /// it, because the string is untrusted input and a pooled arena is retained.
    #[test]
    fn verifying_a_mix_of_costs_never_lets_a_string_grow_the_arena() {
        let small = Params::builder()
            .memory(Memory::kib(1 << 8))
            .passes(1)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("small");
        let large = Params::builder()
            .memory(Memory::kib(1 << 10))
            .passes(1)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("large");

        let encoded_small = Argon2::new(Algorithm::Argon2id, Version::V0x13, small)
            .hash_encoded(b"password", b"somesalt")
            .expect("enc small");
        let encoded_large = Argon2::new(Algorithm::Argon2id, Version::V0x13, large)
            .hash_encoded(b"password", b"somesalt")
            .expect("enc large");

        // Deliberately configured for neither algorithm nor version: those
        // `verify_encoded` does take from the string. The *size* it does not.
        let mut hasher = Argon2::new(Algorithm::Argon2i, Version::V0x10, small).hasher();

        for round in 0..3 {
            assert_eq!(
                hasher.verify_encoded(&encoded_large, b"password", Algorithm::Argon2id),
                Ok(()),
                "round {round} large"
            );
            assert_eq!(
                hasher.verify_encoded(&encoded_small, b"password", Algorithm::Argon2id),
                Ok(()),
                "round {round} small"
            );
            assert_eq!(
                hasher.reserved_blocks(),
                small.memory_blocks() as usize,
                "round {round}: the encoded string set the high-water mark"
            );
        }

        // The owner raising the configuration is a different matter: that is a
        // deliberate choice, so it pools as normal, and a string of that size
        // may then use the arena it paid for.
        hasher.set_argon2(Argon2::new(Algorithm::Argon2id, Version::V0x13, large));
        assert_eq!(
            hasher.verify_encoded(&encoded_large, b"password", Algorithm::Argon2id),
            Ok(())
        );
        assert_eq!(hasher.reserved_blocks(), large.memory_blocks() as usize);
    }

    /// The rule `verify_encoded` enforces is a *ceiling*, and the ceiling is the
    /// owner's configuration — not "whatever is already allocated", which would
    /// be zero on a hasher that has not hashed yet and would therefore send
    /// every verify down the un-pooled path.
    ///
    /// So: a decoded cost below the configured one pools even as the very first
    /// call, and the arena it leaves behind is never larger than the arena the
    /// owner's own next `hash_into` would have taken.
    #[test]
    fn a_decoded_cost_under_the_configured_one_pools_from_the_very_first_call() {
        let tiny = Params::builder()
            .memory(Memory::kib(1 << 7))
            .passes(1)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("tiny");
        let configured = Params::builder()
            .memory(Memory::kib(1 << 10))
            .passes(1)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("configured");

        let encoded_tiny = Argon2::new(Algorithm::Argon2id, Version::V0x13, tiny)
            .hash_encoded(b"password", b"somesalt")
            .expect("enc tiny");

        // Nothing allocated yet, and the first thing this hasher ever does is
        // verify somebody else's string.
        let mut hasher = Argon2::new(Algorithm::Argon2id, Version::V0x13, configured).hasher();
        assert_eq!(hasher.reserved_blocks(), 0);

        assert_eq!(
            hasher.verify_encoded(&encoded_tiny, b"password", Algorithm::Argon2id),
            Ok(())
        );
        assert_eq!(
            hasher.reserved_blocks(),
            tiny.memory_blocks() as usize,
            "a cost under the ceiling should still use the pool"
        );
        assert!(
            hasher.reserved_blocks() <= configured.memory_blocks() as usize,
            "an input must never push the pool past the owner's configuration"
        );

        // And the owner's own hashing still grows it to the configured size.
        let mut tag = [0u8; 32];
        hasher
            .hash_into(b"password", b"somesalt", &mut tag)
            .expect("hash");
        assert_eq!(
            hasher.reserved_blocks(),
            configured.memory_blocks() as usize
        );
    }

    /// A `Hasher` must be movable to whichever worker picks up a request. The
    /// matching negative — that it is not `Sync` — is the `compile_fail`
    /// doctest on [`Hasher`], which is what stops two threads sharing one arena.
    #[test]
    fn a_hasher_is_send() {
        const fn assert_send<T: Send>() {}
        assert_send::<Hasher>();
    }

    /// `hash_in_arena` is the one place an arena of the wrong size could reach
    /// `Instance::new`, whose safety contract is `memory_len == memory_blocks`.
    /// It must be an error, never undefined behaviour.
    #[test]
    fn a_wrongly_sized_arena_is_an_error_not_undefined_behaviour() {
        let params = Params::builder()
            .memory(Memory::kib(1 << 8))
            .passes(1)
            .lanes(1)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        assert_eq!(params.memory_blocks(), 256);
        let mut arena = Arena::new(64).expect("64 blocks");
        let mut out = [0u8; 32];

        // SAFETY: `Backend::Scalar` is available on every CPU.
        let result = unsafe {
            hash_in_arena(
                &mut arena,
                Backend::Scalar,
                Algorithm::Argon2id,
                Version::V0x13,
                &params,
                b"password",
                b"somesalt",
                &[],
                &[],
                &mut out,
                None,
                None,
            )
        };
        assert_eq!(result.err(), Some(Error::MemoryAllocationError));
        assert_eq!(out, [0u8; 32], "nothing was written");
    }

    #[test]
    fn every_available_backend_agrees_with_scalar() {
        let params = Params::builder()
            .memory(Memory::kib(1 << 9))
            .passes(2)
            .lanes(2)
            .threads(2)
            .tag_len(TagLen::bytes(32))
            .build()
            .expect("params");
        let mut reference = [0u8; 32];
        // SAFETY: `Backend::Scalar` is available on every CPU.
        unsafe {
            hash_inner(
                Backend::Scalar,
                Algorithm::Argon2id,
                Version::V0x13,
                &params,
                b"password",
                b"somesalt",
                &[],
                &[],
                &mut reference,
            )
        }
        .expect("scalar");

        for &backend in Backend::ALL {
            if !backend.is_available() {
                continue;
            }
            let mut out = [0u8; 32];
            // SAFETY: guarded by `is_available()` immediately above.
            unsafe {
                hash_inner(
                    backend,
                    Algorithm::Argon2id,
                    Version::V0x13,
                    &params,
                    b"password",
                    b"somesalt",
                    &[],
                    &[],
                    &mut out,
                )
            }
            .expect("backend");
            assert_eq!(out, reference, "{backend}");
        }
    }
}