asupersync 0.3.4

Spec-first, cancel-correct, capability-secure async runtime for Rust.
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
//! RaptorQ decode proof artifact for explainable failures.
//!
//! This module provides a compact, deterministic artifact that explains
//! how a decode operation proceeded and why it succeeded or failed.
//!
//! # Design Goals
//!
//! 1. **Deterministic**: Same inputs produce identical artifacts
//! 2. **Bounded size**: Explicit caps on unbounded collections
//! 3. **Explainable**: Human-readable failure reasons
//! 4. **Replayable**: Sufficient info to reproduce decoder state transitions

use crate::raptorq::decoder::{DecodeError, InactivationDecoder, ReceivedSymbol};
use crate::raptorq::systematic::{SystematicEncoder, SystematicParamError, SystematicParams};
use crate::types::ObjectId;
use crate::util::DetHasher;
use sha2::{Digest, Sha256};
use std::collections::BinaryHeap;
use std::fmt;

/// Maximum number of pivot events to record before truncation.
pub const MAX_PIVOT_EVENTS: usize = 256;

/// Maximum number of received symbol IDs to record.
pub const MAX_RECEIVED_SYMBOLS: usize = 1024;

/// Version of the proof artifact schema.
pub const PROOF_SCHEMA_VERSION: u8 = 2;

/// Version of the proof-artifact distribution manifest schema.
pub const PROOF_ARTIFACT_DISTRIBUTION_SCHEMA_VERSION: u8 = 1;

// ============================================================================
// Cryptographic attestation hash
// ============================================================================

/// Cryptographic SHA-256 hash for proof attestation and forgery detection.
///
/// Replaces the previous 64-bit non-cryptographic hash to prevent proof
/// forgery attacks where an attacker could construct false proofs that
/// hash to the same value as legitimate proofs.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(
    feature = "test-internals",
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct ProofHash([u8; 32]);

impl ProofHash {
    /// Returns the raw 32-byte hash value.
    #[must_use]
    pub const fn as_bytes(&self) -> &[u8; 32] {
        &self.0
    }

    /// Returns the hash as a 64-character lowercase hex string.
    #[must_use]
    pub fn to_hex(&self) -> String {
        let mut out = String::with_capacity(64);
        use std::fmt::Write;
        for byte in &self.0 {
            let _ = write!(out, "{byte:02x}");
        }
        out
    }

    /// Create ProofHash from hex string (for testing/deserialization).
    #[cfg(test)]
    pub fn from_hex(hex: &str) -> Option<Self> {
        if hex.len() != 64 {
            return None;
        }
        let bytes = hex.as_bytes();
        let mut out = [0u8; 32];
        for (i, byte) in out.iter_mut().enumerate() {
            let pair = [*bytes.get(i * 2)?, *bytes.get(i * 2 + 1)?];
            let s = std::str::from_utf8(&pair).ok()?;
            *byte = u8::from_str_radix(s, 16).ok()?;
        }
        Some(Self(out))
    }
}

impl std::fmt::Debug for ProofHash {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "ProofHash({})", &self.to_hex()[..16]) // Truncated for readability
    }
}

impl std::fmt::Display for ProofHash {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.to_hex())
    }
}

// ============================================================================
// Proof artifact types
// ============================================================================

/// A proof-carrying decode artifact that explains the decode process.
///
/// This artifact is produced during decoding and captures:
/// - Configuration and inputs
/// - Key decision points (pivots, inactivation)
/// - Final outcome with explanation
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "test-internals", derive(serde::Serialize))]
pub struct DecodeProof {
    /// Schema version for forward compatibility.
    pub version: u8,
    /// Configuration used for decoding.
    pub config: DecodeConfig,
    /// Summary of received symbols.
    pub received: ReceivedSummary,
    /// Phase 1: Peeling events.
    pub peeling: PeelingTrace,
    /// Phase 2: Inactivation and elimination events.
    pub elimination: EliminationTrace,
    /// Final outcome.
    pub outcome: ProofOutcome,
}

impl DecodeProof {
    /// Create a new proof builder.
    #[must_use]
    #[inline]
    pub fn builder(config: DecodeConfig) -> DecodeProofBuilder {
        DecodeProofBuilder::new(config)
    }

    /// Compute a cryptographic SHA-256 hash for secure attestation.
    ///
    /// This hash provides integrity protection against proof forgery attacks.
    /// Unlike the previous 64-bit non-cryptographic hash, this 256-bit SHA-256
    /// hash makes it computationally infeasible for an attacker to construct
    /// a false proof that produces the same hash as a legitimate proof.
    #[must_use]
    pub fn content_hash(&self) -> ProofHash {
        let mut hasher = Sha256::new();

        // Hash schema version for forward compatibility
        hasher.update([self.version]);

        // Hash configuration (deterministic field order)
        hasher.update((self.config.k as u32).to_le_bytes());
        hasher.update((self.config.symbol_size as u32).to_le_bytes());
        hasher.update(self.config.seed.to_le_bytes());
        hasher.update(self.config.object_id.as_u128().to_le_bytes());
        hasher.update(self.config.sbn.to_le_bytes());

        // Hash received symbols summary
        hasher.update((self.received.total as u32).to_le_bytes());
        hasher.update((self.received.source_count as u32).to_le_bytes());
        hasher.update((self.received.repair_count as u32).to_le_bytes());
        hasher.update(self.received.esi_multiset_hash.to_le_bytes());
        hasher.update((self.received.esis.len() as u32).to_le_bytes());
        for esi in &self.received.esis {
            hasher.update(esi.to_le_bytes());
        }
        hasher.update([u8::from(self.received.truncated)]);

        // Hash peeling trace
        hasher.update((self.peeling.solved as u32).to_le_bytes());
        hasher.update((self.peeling.solved_indices.len() as u32).to_le_bytes());
        for index in &self.peeling.solved_indices {
            hasher.update((*index as u32).to_le_bytes());
        }
        hasher.update([u8::from(self.peeling.truncated)]);

        // Hash elimination trace
        hasher.update((self.elimination.pivots as u32).to_le_bytes());
        hasher.update((self.elimination.row_ops as u32).to_le_bytes());
        hasher.update((self.elimination.inactivated as u32).to_le_bytes());
        hash_inactivation_strategy(&mut hasher, self.elimination.strategy);
        hasher.update((self.elimination.inactive_cols.len() as u32).to_le_bytes());
        for col in &self.elimination.inactive_cols {
            hasher.update((*col as u32).to_le_bytes());
        }
        hasher.update([u8::from(self.elimination.inactive_cols_truncated)]);
        hasher.update((self.elimination.pivot_events.len() as u32).to_le_bytes());
        for pivot in &self.elimination.pivot_events {
            hasher.update((pivot.col as u32).to_le_bytes());
            hasher.update((pivot.row as u32).to_le_bytes());
        }
        hasher.update([u8::from(self.elimination.pivot_events_truncated)]);
        hasher.update((self.elimination.strategy_transitions.len() as u32).to_le_bytes());
        for transition in &self.elimination.strategy_transitions {
            hash_inactivation_strategy(&mut hasher, transition.from);
            hash_inactivation_strategy(&mut hasher, transition.to);
            hasher.update((transition.reason.len() as u32).to_le_bytes());
            hasher.update(transition.reason.as_bytes());
        }
        hasher.update([u8::from(self.elimination.strategy_transitions_truncated)]);

        // Hash outcome
        match &self.outcome {
            ProofOutcome::Success {
                symbols_recovered,
                source_payload_hash,
            } => {
                hasher.update([0u8]); // Success discriminant
                hasher.update((*symbols_recovered as u32).to_le_bytes());
                hasher.update(source_payload_hash.to_le_bytes());
            }
            ProofOutcome::Failure { reason } => {
                hasher.update([1u8]); // Failure discriminant

                // Hash FailureReason deterministically by variant
                match reason {
                    FailureReason::InsufficientSymbols { received, required } => {
                        hasher.update([0u8]); // InsufficientSymbols discriminant
                        hasher.update((*received as u32).to_le_bytes());
                        hasher.update((*required as u32).to_le_bytes());
                    }
                    FailureReason::SingularMatrix {
                        row,
                        attempted_cols,
                    } => {
                        hasher.update([1u8]); // SingularMatrix discriminant
                        hasher.update((*row as u32).to_le_bytes());
                        hasher.update((attempted_cols.len() as u32).to_le_bytes());
                        for col in attempted_cols {
                            hasher.update((*col as u32).to_le_bytes());
                        }
                    }
                    FailureReason::SymbolSizeMismatch { expected, actual } => {
                        hasher.update([2u8]); // SymbolSizeMismatch discriminant
                        hasher.update((*expected as u32).to_le_bytes());
                        hasher.update((*actual as u32).to_le_bytes());
                    }
                    FailureReason::SymbolEquationArityMismatch {
                        esi,
                        columns,
                        coefficients,
                    } => {
                        hasher.update([3u8]); // SymbolEquationArityMismatch discriminant
                        hasher.update(esi.to_le_bytes());
                        hasher.update((*columns as u32).to_le_bytes());
                        hasher.update((*coefficients as u32).to_le_bytes());
                    }
                    FailureReason::ColumnIndexOutOfRange {
                        esi,
                        column,
                        max_valid,
                    } => {
                        hasher.update([4u8]); // ColumnIndexOutOfRange discriminant
                        hasher.update(esi.to_le_bytes());
                        hasher.update((*column as u32).to_le_bytes());
                        hasher.update((*max_valid as u32).to_le_bytes());
                    }
                    FailureReason::SourceEsiOutOfRange { esi, max_valid } => {
                        hasher.update([5u8]); // SourceEsiOutOfRange discriminant
                        hasher.update(esi.to_le_bytes());
                        hasher.update((*max_valid as u32).to_le_bytes());
                    }
                    FailureReason::InvalidSourceSymbolEquation {
                        esi,
                        expected_column,
                    } => {
                        hasher.update([6u8]); // InvalidSourceSymbolEquation discriminant
                        hasher.update(esi.to_le_bytes());
                        hasher.update((*expected_column as u32).to_le_bytes());
                    }
                    FailureReason::CorruptDecodedOutput {
                        esi,
                        byte_index,
                        expected,
                        actual,
                    } => {
                        hasher.update([7u8]); // CorruptDecodedOutput discriminant
                        hasher.update(esi.to_le_bytes());
                        hasher.update((*byte_index as u32).to_le_bytes());
                        hasher.update([*expected]);
                        hasher.update([*actual]);
                    }
                    FailureReason::ComputeBudgetExhausted {
                        used,
                        requested,
                        max,
                    } => {
                        hasher.update([8u8]); // ComputeBudgetExhausted discriminant
                        hasher.update(used.to_le_bytes());
                        hasher.update(requested.to_le_bytes());
                        hasher.update(max.to_le_bytes());
                    }
                    FailureReason::EsiRateLimitExceeded {
                        esi,
                        column_count,
                        max_columns,
                    } => {
                        hasher.update([9u8]); // EsiRateLimitExceeded discriminant
                        hasher.update(esi.to_le_bytes());
                        hasher.update((*column_count as u32).to_le_bytes());
                        hasher.update((*max_columns as u32).to_le_bytes());
                    }
                }
            }
        }

        let digest: [u8; 32] = hasher.finalize().into();
        ProofHash(digest)
    }

    /// Replay the decode with the provided symbols and verify the proof trace matches.
    ///
    /// Returns a detailed [`ReplayError`] if any divergence is detected.
    pub fn replay_and_verify(&self, symbols: &[ReceivedSymbol]) -> Result<(), ReplayError> {
        let decoder =
            InactivationDecoder::new(self.config.k, self.config.symbol_size, self.config.seed);
        let actual =
            match decoder.decode_with_proof(symbols, self.config.object_id, self.config.sbn) {
                Ok(result) => result.proof,
                Err((_err, proof)) => proof,
            };
        compare_proofs(self, &actual)
    }
}

// ============================================================================
// Proof artifact distribution
// ============================================================================

/// Manifest describing a proof bundle distributed through RaptorQ shards.
///
/// The manifest is intentionally small and self-authenticating. Operators can
/// exchange it out-of-band, then verify every shard and recovered artifact
/// against the manifest before accepting the proof bundle.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "test-internals", derive(serde::Serialize))]
pub struct ProofArtifactManifest {
    /// Distribution schema version.
    pub version: u8,
    /// Object ID bound to the proof bundle.
    pub object_id: ObjectId,
    /// Source block number bound to the proof bundle.
    pub sbn: u8,
    /// Original proof-artifact byte length before symbol padding.
    pub artifact_len: usize,
    /// RaptorQ symbol size used for sharding.
    pub symbol_size: usize,
    /// Number of source symbols in the padded artifact.
    pub source_symbols: usize,
    /// Number of repair symbols emitted for redundancy.
    pub repair_symbols: usize,
    /// RFC 6330 `K'` value selected for `source_symbols`.
    pub k_prime: usize,
    /// Total intermediate-symbol count for the selected source block.
    pub l: usize,
    /// Seed used to derive deterministic repair symbols.
    pub seed: u64,
    /// SHA-256 hash of the original unpadded artifact bytes.
    pub source_payload_hash: ProofHash,
    /// SHA-256 hash of all manifest fields above.
    pub manifest_hash: ProofHash,
}

impl ProofArtifactManifest {
    /// Recomputes the manifest hash from all manifest fields except
    /// `manifest_hash`.
    #[must_use]
    pub fn recompute_hash(&self) -> ProofHash {
        hash_proof_artifact_manifest(self)
    }

    /// Returns true when `manifest_hash` still matches the manifest fields.
    #[must_use]
    pub fn hash_is_valid(&self) -> bool {
        self.manifest_hash == self.recompute_hash()
    }
}

/// One RaptorQ shard of a proof bundle.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "test-internals", derive(serde::Serialize))]
pub struct ProofArtifactShard {
    /// Manifest hash this shard belongs to.
    pub manifest_hash: ProofHash,
    /// Encoding Symbol ID.
    pub esi: u32,
    /// Whether this shard is a systematic source symbol.
    pub is_source: bool,
    /// RaptorQ symbol payload.
    pub data: Vec<u8>,
    /// SHA-256 hash of the shard payload.
    pub data_hash: ProofHash,
    /// Deterministic authentication tag over manifest hash, ESI, kind, and data hash.
    pub auth_tag: ProofHash,
}

/// Complete encoded distribution bundle.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "test-internals", derive(serde::Serialize))]
pub struct ProofArtifactDistribution {
    /// Self-authenticating manifest for the bundle.
    pub manifest: ProofArtifactManifest,
    /// Source and repair shards emitted in deterministic ESI order.
    pub shards: Vec<ProofArtifactShard>,
}

/// Successful proof-artifact recovery result.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProofArtifactRecovery {
    /// Recovered unpadded proof-artifact bytes.
    pub payload: Vec<u8>,
    /// Number of supplied shards that passed per-shard authentication.
    pub symbols_received: usize,
    /// Number of supplied shards beyond the manifest's source-symbol count.
    pub overhead_symbols: usize,
    /// True when all supplied shards matched their deterministic auth tags.
    pub authenticated: bool,
    /// Manifest hash that authenticated the recovered payload.
    pub manifest_hash: ProofHash,
}

/// Error while packaging or recovering a distributed proof artifact.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "test-internals", derive(serde::Serialize))]
pub enum ProofArtifactDistributionError {
    /// Proof artifact bytes were empty.
    EmptyArtifact,
    /// Symbol size must be non-zero.
    InvalidSymbolSize,
    /// The artifact needs more source symbols than RFC 6330 supports.
    UnsupportedSourceBlock {
        /// Requested source-symbol count.
        requested: usize,
        /// Maximum supported source-symbol count.
        max_supported: usize,
    },
    /// RFC 6330 table data violates an invariant required for proof distribution.
    RfcTableInvariantViolation {
        /// Description of the violated invariant.
        invariant: &'static str,
        /// Problematic RFC table values or derived parameters.
        details: String,
    },
    /// The systematic encoder could not solve the source block.
    EncoderUnavailable,
    /// The manifest hash does not match the manifest fields.
    ManifestHashMismatch {
        /// Hash stored in the manifest.
        expected: ProofHash,
        /// Hash recomputed from manifest fields.
        actual: ProofHash,
    },
    /// RFC 6330 parameters in the manifest do not match the source block.
    ManifestParameterMismatch {
        /// Manifest field that did not match recomputed parameters.
        field: &'static str,
        /// Value stored in the manifest.
        expected: usize,
        /// Value recomputed from `source_symbols` and `symbol_size`.
        actual: usize,
    },
    /// A shard belongs to a different manifest.
    ManifestMismatch {
        /// Manifest hash expected by the recovery operation.
        expected: ProofHash,
        /// Manifest hash carried by the shard.
        actual: ProofHash,
    },
    /// A shard payload had the wrong symbol size.
    ShardSizeMismatch {
        /// ESI of the malformed shard.
        esi: u32,
        /// Expected symbol size from the manifest.
        expected: usize,
        /// Actual payload length.
        actual: usize,
    },
    /// A shard payload hash did not match its bytes.
    ShardPayloadHashMismatch {
        /// ESI of the corrupted shard.
        esi: u32,
    },
    /// A shard authentication tag did not match the manifest and payload hash.
    ShardAuthenticationFailed {
        /// ESI of the unauthenticated shard.
        esi: u32,
    },
    /// RaptorQ decode failed after all supplied shards were authenticated.
    DecodeFailed {
        /// Proof-compatible failure reason.
        reason: FailureReason,
    },
    /// Recovered bytes did not match the source-payload hash in the manifest.
    SourcePayloadHashMismatch {
        /// Hash stored in the manifest.
        expected: ProofHash,
        /// Hash computed from recovered bytes.
        actual: ProofHash,
    },
}

impl fmt::Display for ProofArtifactDistributionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::EmptyArtifact => f.write_str("proof artifact payload is empty"),
            Self::InvalidSymbolSize => f.write_str("proof artifact symbol size must be non-zero"),
            Self::UnsupportedSourceBlock {
                requested,
                max_supported,
            } => write!(
                f,
                "proof artifact needs {requested} source symbols; maximum supported is {max_supported}"
            ),
            Self::RfcTableInvariantViolation { invariant, details } => write!(
                f,
                "proof artifact RFC 6330 table invariant violation: {invariant}; {details}"
            ),
            Self::EncoderUnavailable => {
                f.write_str("systematic RaptorQ encoder could not solve proof artifact block")
            }
            Self::ManifestHashMismatch { expected, actual } => write!(
                f,
                "manifest hash mismatch: stored {expected}, recomputed {actual}"
            ),
            Self::ManifestParameterMismatch {
                field,
                expected,
                actual,
            } => write!(
                f,
                "manifest {field} mismatch: stored {expected}, recomputed {actual}"
            ),
            Self::ManifestMismatch { expected, actual } => {
                write!(
                    f,
                    "shard manifest mismatch: expected {expected}, got {actual}"
                )
            }
            Self::ShardSizeMismatch {
                esi,
                expected,
                actual,
            } => write!(
                f,
                "shard {esi} has {actual} bytes; expected {expected} bytes"
            ),
            Self::ShardPayloadHashMismatch { esi } => {
                write!(f, "shard {esi} payload hash mismatch")
            }
            Self::ShardAuthenticationFailed { esi } => {
                write!(f, "shard {esi} authentication tag mismatch")
            }
            Self::DecodeFailed { reason } => {
                write!(f, "proof artifact decode failed: {reason:?}")
            }
            Self::SourcePayloadHashMismatch { expected, actual } => write!(
                f,
                "recovered proof artifact hash mismatch: expected {expected}, got {actual}"
            ),
        }
    }
}

impl std::error::Error for ProofArtifactDistributionError {}

/// Package a proof artifact into authenticated RaptorQ shards for distribution.
///
/// # Errors
///
/// Returns an error if the payload is empty, `symbol_size` is zero, the source
/// block exceeds the RFC 6330 table, or the systematic encoder cannot solve
/// the source block.
pub fn package_proof_artifact_for_distribution(
    artifact: &[u8],
    symbol_size: usize,
    repair_symbols: usize,
    seed: u64,
    object_id: ObjectId,
    sbn: u8,
) -> Result<ProofArtifactDistribution, ProofArtifactDistributionError> {
    let source = proof_artifact_source_symbols(artifact, symbol_size)?;
    let source_symbols = source.len();
    let params = SystematicParams::try_for_source_block(source_symbols, symbol_size)
        .map_err(map_systematic_param_error)?;
    let source_payload_hash = hash_proof_artifact_payload(artifact);

    let mut manifest = ProofArtifactManifest {
        version: PROOF_ARTIFACT_DISTRIBUTION_SCHEMA_VERSION,
        object_id,
        sbn,
        artifact_len: artifact.len(),
        symbol_size,
        source_symbols,
        repair_symbols,
        k_prime: params.k_prime,
        l: params.l,
        seed,
        source_payload_hash,
        manifest_hash: ProofHash([0u8; 32]),
    };
    manifest.manifest_hash = manifest.recompute_hash();

    let mut encoder = SystematicEncoder::new(&source, symbol_size, seed)
        .ok_or(ProofArtifactDistributionError::EncoderUnavailable)?;
    let shards = encoder
        .emit_all(repair_symbols)
        .into_iter()
        .map(|symbol| {
            ProofArtifactShard::new(
                manifest.manifest_hash,
                symbol.esi,
                symbol.is_source,
                symbol.data,
            )
        })
        .collect();

    Ok(ProofArtifactDistribution { manifest, shards })
}

/// Recover a proof artifact from any sufficiently complete authenticated shard set.
///
/// # Errors
///
/// Returns an error if the manifest or any supplied shard fails authentication,
/// if there are not enough shards to decode, or if the recovered artifact hash
/// does not match the manifest.
pub fn recover_proof_artifact_from_shards(
    manifest: &ProofArtifactManifest,
    shards: &[ProofArtifactShard],
) -> Result<ProofArtifactRecovery, ProofArtifactDistributionError> {
    validate_manifest(manifest)?;

    let decoder =
        InactivationDecoder::new(manifest.source_symbols, manifest.symbol_size, manifest.seed);
    let mut received = decoder.constraint_symbols();

    for shard in shards {
        verify_shard(manifest, shard)?;
        if shard.is_source {
            received.push(ReceivedSymbol::source(shard.esi, shard.data.clone()));
        } else {
            let (columns, coefficients) = decoder.repair_equation(shard.esi).map_err(|_| {
                ProofArtifactDistributionError::DecodeFailed {
                    reason: FailureReason::SingularMatrix {
                        row: usize::try_from(shard.esi).unwrap_or(usize::MAX),
                        attempted_cols: Vec::new(),
                    },
                }
            })?;
            received.push(ReceivedSymbol::repair(
                shard.esi,
                columns,
                coefficients,
                shard.data.clone(),
            ));
        }
    }

    let decoded =
        decoder
            .decode(&received)
            .map_err(|err| ProofArtifactDistributionError::DecodeFailed {
                reason: FailureReason::from(&err),
            })?;
    let payload = flatten_source_payload(&decoded.source, manifest.artifact_len);
    let actual_hash = hash_proof_artifact_payload(&payload);
    if actual_hash != manifest.source_payload_hash {
        return Err(ProofArtifactDistributionError::SourcePayloadHashMismatch {
            expected: manifest.source_payload_hash,
            actual: actual_hash,
        });
    }

    Ok(ProofArtifactRecovery {
        payload,
        symbols_received: shards.len(),
        overhead_symbols: shards.len().saturating_sub(manifest.source_symbols),
        authenticated: true,
        manifest_hash: manifest.manifest_hash,
    })
}

impl ProofArtifactShard {
    fn new(manifest_hash: ProofHash, esi: u32, is_source: bool, data: Vec<u8>) -> Self {
        let data_hash = hash_proof_artifact_shard_payload(&data);
        let auth_tag = hash_proof_artifact_shard_auth(manifest_hash, esi, is_source, data_hash);
        Self {
            manifest_hash,
            esi,
            is_source,
            data,
            data_hash,
            auth_tag,
        }
    }
}

fn proof_artifact_source_symbols(
    artifact: &[u8],
    symbol_size: usize,
) -> Result<Vec<Vec<u8>>, ProofArtifactDistributionError> {
    if artifact.is_empty() {
        return Err(ProofArtifactDistributionError::EmptyArtifact);
    }
    if symbol_size == 0 {
        return Err(ProofArtifactDistributionError::InvalidSymbolSize);
    }

    let source_symbols = artifact.len().div_ceil(symbol_size);
    SystematicParams::try_for_source_block(source_symbols, symbol_size)
        .map_err(map_systematic_param_error)?;

    let mut source = Vec::with_capacity(source_symbols);
    for chunk in artifact.chunks(symbol_size) {
        let mut symbol = vec![0u8; symbol_size];
        symbol[..chunk.len()].copy_from_slice(chunk);
        source.push(symbol);
    }
    Ok(source)
}

fn validate_manifest(
    manifest: &ProofArtifactManifest,
) -> Result<(), ProofArtifactDistributionError> {
    if manifest.symbol_size == 0 {
        return Err(ProofArtifactDistributionError::InvalidSymbolSize);
    }
    let params =
        SystematicParams::try_for_source_block(manifest.source_symbols, manifest.symbol_size)
            .map_err(map_systematic_param_error)?;
    validate_manifest_parameter("k_prime", manifest.k_prime, params.k_prime)?;
    validate_manifest_parameter("l", manifest.l, params.l)?;
    let actual = manifest.recompute_hash();
    if actual != manifest.manifest_hash {
        return Err(ProofArtifactDistributionError::ManifestHashMismatch {
            expected: manifest.manifest_hash,
            actual,
        });
    }
    Ok(())
}

fn validate_manifest_parameter(
    field: &'static str,
    expected: usize,
    actual: usize,
) -> Result<(), ProofArtifactDistributionError> {
    if expected == actual {
        return Ok(());
    }
    Err(ProofArtifactDistributionError::ManifestParameterMismatch {
        field,
        expected,
        actual,
    })
}

fn verify_shard(
    manifest: &ProofArtifactManifest,
    shard: &ProofArtifactShard,
) -> Result<(), ProofArtifactDistributionError> {
    if shard.manifest_hash != manifest.manifest_hash {
        return Err(ProofArtifactDistributionError::ManifestMismatch {
            expected: manifest.manifest_hash,
            actual: shard.manifest_hash,
        });
    }
    if shard.data.len() != manifest.symbol_size {
        return Err(ProofArtifactDistributionError::ShardSizeMismatch {
            esi: shard.esi,
            expected: manifest.symbol_size,
            actual: shard.data.len(),
        });
    }
    if shard.data_hash != hash_proof_artifact_shard_payload(&shard.data) {
        return Err(ProofArtifactDistributionError::ShardPayloadHashMismatch { esi: shard.esi });
    }
    if shard.auth_tag
        != hash_proof_artifact_shard_auth(
            shard.manifest_hash,
            shard.esi,
            shard.is_source,
            shard.data_hash,
        )
    {
        return Err(ProofArtifactDistributionError::ShardAuthenticationFailed { esi: shard.esi });
    }
    Ok(())
}

fn map_systematic_param_error(err: SystematicParamError) -> ProofArtifactDistributionError {
    match err {
        SystematicParamError::UnsupportedSourceBlockSize {
            requested,
            max_supported,
        } => ProofArtifactDistributionError::UnsupportedSourceBlock {
            requested,
            max_supported,
        },
        SystematicParamError::KPrimeExceedsU32 { k_prime, max_u32 } => {
            ProofArtifactDistributionError::UnsupportedSourceBlock {
                requested: k_prime,
                max_supported: max_u32,
            }
        }
        SystematicParamError::RfcTableInvariantViolation { invariant, details } => {
            ProofArtifactDistributionError::RfcTableInvariantViolation { invariant, details }
        }
    }
}

fn flatten_source_payload(source: &[Vec<u8>], artifact_len: usize) -> Vec<u8> {
    source
        .iter()
        .flatten()
        .copied()
        .take(artifact_len)
        .collect()
}

fn hash_proof_artifact_manifest(manifest: &ProofArtifactManifest) -> ProofHash {
    let mut hasher = Sha256::new();
    hasher.update(b"RaptorQ::ProofArtifactManifest::v1");
    hasher.update([manifest.version]);
    hasher.update(manifest.object_id.as_u128().to_le_bytes());
    hasher.update([manifest.sbn]);
    hash_usize(&mut hasher, manifest.artifact_len);
    hash_usize(&mut hasher, manifest.symbol_size);
    hash_usize(&mut hasher, manifest.source_symbols);
    hash_usize(&mut hasher, manifest.repair_symbols);
    hash_usize(&mut hasher, manifest.k_prime);
    hash_usize(&mut hasher, manifest.l);
    hasher.update(manifest.seed.to_le_bytes());
    hasher.update(manifest.source_payload_hash.as_bytes());
    ProofHash(hasher.finalize().into())
}

fn hash_proof_artifact_payload(payload: &[u8]) -> ProofHash {
    let mut hasher = Sha256::new();
    hasher.update(b"RaptorQ::ProofArtifactPayload::v1");
    hash_usize(&mut hasher, payload.len());
    hasher.update(payload);
    ProofHash(hasher.finalize().into())
}

fn hash_proof_artifact_shard_payload(payload: &[u8]) -> ProofHash {
    let mut hasher = Sha256::new();
    hasher.update(b"RaptorQ::ProofArtifactShardPayload::v1");
    hash_usize(&mut hasher, payload.len());
    hasher.update(payload);
    ProofHash(hasher.finalize().into())
}

fn hash_proof_artifact_shard_auth(
    manifest_hash: ProofHash,
    esi: u32,
    is_source: bool,
    data_hash: ProofHash,
) -> ProofHash {
    let mut hasher = Sha256::new();
    hasher.update(b"RaptorQ::ProofArtifactShardAuth::v1");
    hasher.update(manifest_hash.as_bytes());
    hasher.update(esi.to_le_bytes());
    hasher.update([u8::from(is_source)]);
    hasher.update(data_hash.as_bytes());
    ProofHash(hasher.finalize().into())
}

fn hash_usize(hasher: &mut Sha256, value: usize) {
    hasher.update(u64::try_from(value).unwrap_or(u64::MAX).to_le_bytes());
}

#[inline]
fn hash_inactivation_strategy(hasher: &mut Sha256, strategy: InactivationStrategy) {
    match strategy {
        InactivationStrategy::AllAtOnce => hasher.update([0u8]),
        InactivationStrategy::HighSupportFirst => hasher.update([1u8]),
        InactivationStrategy::BlockSchurLowRank => hasher.update([2u8]),
    }
}

#[inline]
fn recovered_source_hash(source: &[Vec<u8>]) -> u64 {
    // Use SHA-256 for cryptographic integrity, then truncate to u64 for compatibility
    // with existing ProofOutcome::Success struct (which expects u64).
    // This provides cryptographic strength while maintaining wire format compatibility.
    let mut hasher = Sha256::new();

    // Add domain separator to prevent cross-protocol attacks
    hasher.update(b"RaptorQ::RecoveredSource");
    hasher.update((source.len() as u64).to_le_bytes());
    for row in source {
        hasher.update((row.len() as u64).to_le_bytes());
        hasher.update(row);
    }

    let digest: [u8; 32] = hasher.finalize().into();
    // Take first 8 bytes as little-endian u64 (collision resistance still strong)
    u64::from_le_bytes([
        digest[0], digest[1], digest[2], digest[3], digest[4], digest[5], digest[6], digest[7],
    ])
}

#[derive(Default)]
struct ReceivedEsiMultisetHashState {
    count: u64,
    sum: u64,
    sum_products: u64,
    mix: u64,
}

impl ReceivedEsiMultisetHashState {
    #[inline]
    fn observe(&mut self, esi: u32, is_source: bool) {
        use std::hash::{Hash, Hasher};

        let mut hasher = DetHasher::default();
        (esi, is_source).hash(&mut hasher);
        let digest = hasher.finish();

        self.count = self.count.wrapping_add(1);
        self.sum = self.sum.wrapping_add(digest);
        self.sum_products = self
            .sum_products
            .wrapping_add(digest.wrapping_mul(digest | 1));
        self.mix = self
            .mix
            .wrapping_add(digest.rotate_left(17) ^ digest.wrapping_mul(0x9E37_79B9_7F4A_7C15));
    }

    #[inline]
    fn finish(self) -> u64 {
        use std::hash::{Hash, Hasher};

        let mut hasher = DetHasher::default();
        self.count.hash(&mut hasher);
        self.sum.hash(&mut hasher);
        self.sum_products.hash(&mut hasher);
        self.mix.hash(&mut hasher);
        hasher.finish()
    }
}

// ============================================================================
// Replay verification
// ============================================================================

/// Detailed error for proof replay verification.
#[derive(Debug)]
pub enum ReplayError {
    /// Generic mismatch for scalar fields.
    Mismatch {
        /// Name of the mismatched field.
        field: &'static str,
        /// Expected value (formatted).
        expected: String,
        /// Actual value (formatted).
        actual: String,
    },
    /// Sequence mismatch at a specific index.
    SequenceMismatch {
        /// Name of the sequence being compared.
        label: &'static str,
        /// Index of the first mismatch.
        index: usize,
        /// Expected value at the mismatch (formatted).
        expected: String,
        /// Actual value at the mismatch (formatted).
        actual: String,
    },
}

impl fmt::Display for ReplayError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Mismatch {
                field,
                expected,
                actual,
            } => write!(f, "mismatch for {field}: expected {expected}, got {actual}"),
            Self::SequenceMismatch {
                label,
                index,
                expected,
                actual,
            } => write!(
                f,
                "sequence mismatch for {label} at index {index}: expected {expected}, got {actual}"
            ),
        }
    }
}

impl std::error::Error for ReplayError {}

#[inline]
fn mismatch<T: fmt::Debug>(field: &'static str, expected: T, actual: T) -> ReplayError {
    ReplayError::Mismatch {
        field,
        expected: format!("{expected:?}"),
        actual: format!("{actual:?}"),
    }
}

#[inline]
fn sequence_mismatch(
    label: &'static str,
    index: usize,
    expected: String,
    actual: String,
) -> ReplayError {
    ReplayError::SequenceMismatch {
        label,
        index,
        expected,
        actual,
    }
}

fn compare_prefix<T: PartialEq + fmt::Debug>(
    label: &'static str,
    expected: &[T],
    actual: &[T],
    truncated: bool,
) -> Result<(), ReplayError> {
    if actual.len() != expected.len() {
        let idx = expected.len().min(actual.len());
        let (expected_item, actual_item) = if actual.len() < expected.len() {
            (
                format!("{:?}", expected.get(actual.len())),
                "missing".to_string(),
            )
        } else if truncated {
            (
                format!("len {}", expected.len()),
                format!("len {}", actual.len()),
            )
        } else {
            (
                "missing".to_string(),
                format!("{:?}", actual.get(expected.len())),
            )
        };
        return Err(sequence_mismatch(label, idx, expected_item, actual_item));
    }
    for (idx, (exp, act)) in expected.iter().zip(actual.iter()).enumerate() {
        if exp != act {
            return Err(sequence_mismatch(
                label,
                idx,
                format!("{exp:?}"),
                format!("{act:?}"),
            ));
        }
    }
    Ok(())
}

#[allow(clippy::too_many_lines)]
fn compare_proofs(expected: &DecodeProof, actual: &DecodeProof) -> Result<(), ReplayError> {
    if expected.version != actual.version {
        return Err(mismatch("version", expected.version, actual.version));
    }
    if expected.config != actual.config {
        return Err(mismatch("config", &expected.config, &actual.config));
    }

    let exp_recv = &expected.received;
    let act_recv = &actual.received;
    if exp_recv.total != act_recv.total {
        return Err(mismatch("received.total", exp_recv.total, act_recv.total));
    }
    if exp_recv.source_count != act_recv.source_count {
        return Err(mismatch(
            "received.source_count",
            exp_recv.source_count,
            act_recv.source_count,
        ));
    }
    if exp_recv.repair_count != act_recv.repair_count {
        return Err(mismatch(
            "received.repair_count",
            exp_recv.repair_count,
            act_recv.repair_count,
        ));
    }
    if exp_recv.esi_multiset_hash != act_recv.esi_multiset_hash {
        return Err(mismatch(
            "received.esi_multiset_hash",
            exp_recv.esi_multiset_hash,
            act_recv.esi_multiset_hash,
        ));
    }
    if exp_recv.truncated != act_recv.truncated {
        return Err(mismatch(
            "received.truncated",
            exp_recv.truncated,
            act_recv.truncated,
        ));
    }
    compare_prefix(
        "received.esis",
        &exp_recv.esis,
        &act_recv.esis,
        exp_recv.truncated,
    )?;

    let exp_peel = &expected.peeling;
    let act_peel = &actual.peeling;
    if exp_peel.solved != act_peel.solved {
        return Err(mismatch("peeling.solved", exp_peel.solved, act_peel.solved));
    }
    if exp_peel.truncated != act_peel.truncated {
        return Err(mismatch(
            "peeling.truncated",
            exp_peel.truncated,
            act_peel.truncated,
        ));
    }
    compare_prefix(
        "peeling.solved_indices",
        &exp_peel.solved_indices,
        &act_peel.solved_indices,
        exp_peel.truncated,
    )?;

    let exp_elim = &expected.elimination;
    let act_elim = &actual.elimination;
    if exp_elim.inactivated != act_elim.inactivated {
        return Err(mismatch(
            "elimination.inactivated",
            exp_elim.inactivated,
            act_elim.inactivated,
        ));
    }
    if exp_elim.pivots != act_elim.pivots {
        return Err(mismatch(
            "elimination.pivots",
            exp_elim.pivots,
            act_elim.pivots,
        ));
    }
    if exp_elim.row_ops != act_elim.row_ops {
        return Err(mismatch(
            "elimination.row_ops",
            exp_elim.row_ops,
            act_elim.row_ops,
        ));
    }
    if exp_elim.inactive_cols_truncated != act_elim.inactive_cols_truncated {
        return Err(mismatch(
            "elimination.inactive_cols_truncated",
            exp_elim.inactive_cols_truncated,
            act_elim.inactive_cols_truncated,
        ));
    }
    if exp_elim.pivot_events_truncated != act_elim.pivot_events_truncated {
        return Err(mismatch(
            "elimination.pivot_events_truncated",
            exp_elim.pivot_events_truncated,
            act_elim.pivot_events_truncated,
        ));
    }
    if exp_elim.strategy_transitions_truncated != act_elim.strategy_transitions_truncated {
        return Err(mismatch(
            "elimination.strategy_transitions_truncated",
            exp_elim.strategy_transitions_truncated,
            act_elim.strategy_transitions_truncated,
        ));
    }
    if exp_elim.strategy != act_elim.strategy {
        return Err(mismatch(
            "elimination.strategy",
            exp_elim.strategy,
            act_elim.strategy,
        ));
    }
    compare_prefix(
        "elimination.inactive_cols",
        &exp_elim.inactive_cols,
        &act_elim.inactive_cols,
        exp_elim.inactive_cols_truncated,
    )?;
    compare_prefix(
        "elimination.pivot_events",
        &exp_elim.pivot_events,
        &act_elim.pivot_events,
        exp_elim.pivot_events_truncated,
    )?;
    compare_prefix(
        "elimination.strategy_transitions",
        &exp_elim.strategy_transitions,
        &act_elim.strategy_transitions,
        exp_elim.strategy_transitions_truncated,
    )?;

    if expected.outcome != actual.outcome {
        return Err(mismatch("outcome", &expected.outcome, &actual.outcome));
    }

    Ok(())
}

/// Decode configuration captured in the proof.
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "test-internals", derive(serde::Serialize))]
pub struct DecodeConfig {
    /// Object ID being decoded.
    pub object_id: ObjectId,
    /// Source block number.
    pub sbn: u8,
    /// Number of source symbols (K).
    pub k: usize,
    /// Number of LDPC symbols (S).
    pub s: usize,
    /// Number of HDPC symbols (H).
    pub h: usize,
    /// Total intermediate symbols (L = K + S + H).
    pub l: usize,
    /// Symbol size in bytes.
    pub symbol_size: usize,
    /// Seed used for encoding.
    pub seed: u64,
}

/// Summary of received symbols.
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "test-internals", derive(serde::Serialize))]
pub struct ReceivedSummary {
    /// Total symbols received.
    pub total: usize,
    /// Number of source symbols received.
    pub source_count: usize,
    /// Number of repair symbols received.
    pub repair_count: usize,
    /// Deterministic hash of the full received ESI/source multiset.
    ///
    /// This binds replay verification to entries that fall outside the bounded
    /// preview list once `esis` is truncated.
    pub esi_multiset_hash: u64,
    /// ESIs of received symbols (sorted, truncated to MAX_RECEIVED_SYMBOLS).
    pub esis: Vec<u32>,
    /// True if ESI list was truncated.
    pub truncated: bool,
}

impl ReceivedSummary {
    /// Create from a list of (ESI, is_source) pairs.
    ///
    /// ESIs are recorded in deterministic ascending order and truncated
    /// to the smallest MAX_RECEIVED_SYMBOLS entries.
    #[must_use]
    pub fn from_received(symbols: impl Iterator<Item = (u32, bool)>) -> Self {
        let mut source_count = 0;
        let mut repair_count = 0;
        let mut total = 0usize;
        let mut esis_heap: BinaryHeap<u32> = BinaryHeap::new();
        let mut hash_state = ReceivedEsiMultisetHashState::default();

        for (esi, is_source) in symbols {
            total += 1;
            if is_source {
                source_count += 1;
            } else {
                repair_count += 1;
            }
            hash_state.observe(esi, is_source);
            if esis_heap.len() < MAX_RECEIVED_SYMBOLS {
                esis_heap.push(esi);
                continue;
            }
            if let Some(&max) = esis_heap.peek() {
                if esi < max {
                    esis_heap.pop();
                    esis_heap.push(esi);
                }
            }
        }

        let truncated = total > MAX_RECEIVED_SYMBOLS;
        let esi_multiset_hash = hash_state.finish();
        let mut esis = esis_heap.into_vec();
        esis.sort_unstable();
        Self {
            total,
            source_count,
            repair_count,
            esi_multiset_hash,
            esis,
            truncated,
        }
    }
}

/// Trace of peeling (belief propagation) phase.
#[derive(Debug, Clone, Default, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "test-internals", derive(serde::Serialize))]
pub struct PeelingTrace {
    /// Number of symbols solved via peeling.
    pub solved: usize,
    /// Intermediate symbol indices solved during peeling.
    pub solved_indices: Vec<usize>,
    /// True if solved_indices was truncated.
    pub truncated: bool,
}

impl PeelingTrace {
    /// Record a solved symbol index.
    pub fn record_solved(&mut self, col: usize) {
        self.solved += 1;
        if self.solved_indices.len() < MAX_PIVOT_EVENTS {
            self.solved_indices.push(col);
        } else {
            self.truncated = true;
        }
    }
}

/// Trace of inactivation and Gaussian elimination phase.
#[derive(Debug, Clone, Default, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "test-internals", derive(serde::Serialize))]
pub struct EliminationTrace {
    /// Inactivation strategy selected for this decode.
    pub strategy: InactivationStrategy,
    /// Number of columns marked as inactive.
    pub inactivated: usize,
    /// Column indices that were inactivated.
    pub inactive_cols: Vec<usize>,
    /// Number of pivot selections.
    pub pivots: usize,
    /// Pivot events: (column, pivot_row) pairs.
    pub pivot_events: Vec<PivotEvent>,
    /// True if inactive_cols was truncated.
    pub inactive_cols_truncated: bool,
    /// True if pivot_events was truncated.
    pub pivot_events_truncated: bool,
    /// Number of row operations performed.
    pub row_ops: usize,
    /// Strategy transitions recorded during decode.
    pub strategy_transitions: Vec<StrategyTransition>,
    /// True if strategy_transitions was truncated.
    pub strategy_transitions_truncated: bool,
}

impl EliminationTrace {
    /// Set the strategy used by the decoder.
    pub fn set_strategy(&mut self, strategy: InactivationStrategy) {
        self.strategy = strategy;
    }

    /// Record a strategy transition.
    pub fn record_strategy_transition(
        &mut self,
        from: InactivationStrategy,
        to: InactivationStrategy,
        reason: &'static str,
    ) {
        if from == to {
            self.strategy = to;
            return;
        }
        if self.strategy_transitions.len() < MAX_PIVOT_EVENTS {
            self.strategy_transitions
                .push(StrategyTransition { from, to, reason });
        } else {
            self.strategy_transitions_truncated = true;
        }
        self.strategy = to;
    }

    /// Record an inactivated column.
    pub fn record_inactivation(&mut self, col: usize) {
        self.inactivated += 1;
        if self.inactive_cols.len() < MAX_PIVOT_EVENTS {
            self.inactive_cols.push(col);
        } else {
            self.inactive_cols_truncated = true;
        }
    }

    /// Record a pivot selection.
    pub fn record_pivot(&mut self, col: usize, row: usize) {
        self.pivots += 1;
        if self.pivot_events.len() < MAX_PIVOT_EVENTS {
            self.pivot_events.push(PivotEvent { col, row });
        } else {
            self.pivot_events_truncated = true;
        }
    }

    /// Record a row operation.
    pub fn record_row_op(&mut self) {
        self.row_ops += 1;
    }
}

/// Inactivation strategy used by the decoder.
#[derive(Debug, Clone, Copy, Default, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "test-internals", derive(serde::Serialize))]
pub enum InactivationStrategy {
    /// Legacy behavior: inactivate all remaining unsolved columns in their natural order.
    #[default]
    AllAtOnce,
    /// Hard-regime behavior: inactivate columns ordered by descending equation support.
    HighSupportFirst,
    /// Accelerated hard-regime behavior: deterministic block-Schur partitioning with
    /// conservative fallback to high-support ordering when assumptions break.
    BlockSchurLowRank,
}

/// A single strategy transition event.
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "test-internals", derive(serde::Serialize))]
pub struct StrategyTransition {
    /// Previous strategy.
    pub from: InactivationStrategy,
    /// New strategy.
    pub to: InactivationStrategy,
    /// Deterministic reason for the transition.
    pub reason: &'static str,
}

/// A single pivot selection event.
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "test-internals", derive(serde::Serialize))]
pub struct PivotEvent {
    /// Column being eliminated.
    pub col: usize,
    /// Row selected as pivot.
    pub row: usize,
}

/// Final decode outcome.
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "test-internals", derive(serde::Serialize))]
pub enum ProofOutcome {
    /// Decode succeeded.
    Success {
        /// Total source symbols recovered.
        symbols_recovered: usize,
        /// Deterministic hash of the recovered source payload.
        source_payload_hash: u64,
    },
    /// Decode failed with a specific reason.
    Failure {
        /// The error that occurred.
        reason: FailureReason,
    },
}

/// Detailed failure reason for proof artifact.
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "test-internals", derive(serde::Serialize))]
pub enum FailureReason {
    /// Not enough symbols received.
    InsufficientSymbols {
        /// Symbols received.
        received: usize,
        /// Symbols required.
        required: usize,
    },
    /// Matrix became singular during elimination.
    SingularMatrix {
        /// Row that couldn't find a pivot.
        row: usize,
        /// Columns that were attempted.
        attempted_cols: Vec<usize>,
    },
    /// Symbol size mismatch.
    SymbolSizeMismatch {
        /// Expected size.
        expected: usize,
        /// Actual size.
        actual: usize,
    },
    /// Received symbol has mismatched equation vectors.
    SymbolEquationArityMismatch {
        /// ESI of malformed symbol.
        esi: u32,
        /// Number of column indices.
        columns: usize,
        /// Number of coefficients.
        coefficients: usize,
    },
    /// Received symbol references an invalid column outside [0, L).
    ColumnIndexOutOfRange {
        /// ESI of malformed symbol.
        esi: u32,
        /// Offending column index.
        column: usize,
        /// Exclusive upper bound for valid columns.
        max_valid: usize,
    },
    /// A source symbol used an ESI outside the systematic source domain [0, K).
    SourceEsiOutOfRange {
        /// ESI of malformed source symbol.
        esi: u32,
        /// Exclusive upper bound for valid source ESIs.
        max_valid: usize,
    },
    /// A source symbol did not use the required identity equation `C[esi] = data`.
    InvalidSourceSymbolEquation {
        /// ESI of malformed source symbol.
        esi: u32,
        /// Required intermediate column for that source symbol.
        expected_column: usize,
    },
    /// Decoder produced output that failed equation verification.
    CorruptDecodedOutput {
        /// ESI of mismatched equation row.
        esi: u32,
        /// First mismatching byte index.
        byte_index: usize,
        /// Reconstructed byte from decoded intermediate symbols.
        expected: u8,
        /// Received RHS byte from input symbol.
        actual: u8,
    },
    /// Decoder compute budget was exhausted.
    ComputeBudgetExhausted {
        /// Budget consumed before this operation.
        used: u64,
        /// Additional budget requested by the operation.
        requested: u64,
        /// Maximum allowed budget.
        max: u64,
    },
    /// ESI expansion exceeded the configured per-symbol rate limit.
    EsiRateLimitExceeded {
        /// ESI that exceeded the limit.
        esi: u32,
        /// Columns that would have been generated.
        column_count: usize,
        /// Maximum allowed columns.
        max_columns: usize,
    },
}

impl From<&DecodeError> for FailureReason {
    fn from(err: &DecodeError) -> Self {
        match err {
            DecodeError::InsufficientSymbols { received, required } => Self::InsufficientSymbols {
                received: *received,
                required: *required,
            },
            DecodeError::SingularMatrix { row } => Self::SingularMatrix {
                row: *row,
                attempted_cols: Vec::new(), // Filled in by caller if available
            },
            DecodeError::SymbolSizeMismatch { expected, actual } => Self::SymbolSizeMismatch {
                expected: *expected,
                actual: *actual,
            },
            DecodeError::SymbolEquationArityMismatch {
                esi,
                columns,
                coefficients,
            } => Self::SymbolEquationArityMismatch {
                esi: *esi,
                columns: *columns,
                coefficients: *coefficients,
            },
            DecodeError::ColumnIndexOutOfRange {
                esi,
                column,
                max_valid,
            } => Self::ColumnIndexOutOfRange {
                esi: *esi,
                column: *column,
                max_valid: *max_valid,
            },
            DecodeError::SourceEsiOutOfRange { esi, max_valid } => Self::SourceEsiOutOfRange {
                esi: *esi,
                max_valid: *max_valid,
            },
            DecodeError::InvalidSourceSymbolEquation {
                esi,
                expected_column,
            } => Self::InvalidSourceSymbolEquation {
                esi: *esi,
                expected_column: *expected_column,
            },
            DecodeError::CorruptDecodedOutput {
                esi,
                byte_index,
                expected,
                actual,
            } => Self::CorruptDecodedOutput {
                esi: *esi,
                byte_index: *byte_index,
                expected: *expected,
                actual: *actual,
            },
            DecodeError::ComputeBudgetExhausted {
                used,
                requested,
                max,
            } => Self::ComputeBudgetExhausted {
                used: *used,
                requested: *requested,
                max: *max,
            },
            DecodeError::EsiRateLimitExceeded {
                esi,
                column_count,
                max_columns,
            } => Self::EsiRateLimitExceeded {
                esi: *esi,
                column_count: *column_count,
                max_columns: *max_columns,
            },
        }
    }
}

// ============================================================================
// Builder for incremental construction
// ============================================================================

/// Builder for constructing a decode proof incrementally.
#[derive(Debug)]
pub struct DecodeProofBuilder {
    config: DecodeConfig,
    received: Option<ReceivedSummary>,
    peeling: PeelingTrace,
    elimination: EliminationTrace,
    outcome: Option<ProofOutcome>,
}

impl DecodeProofBuilder {
    /// Create a new builder with the given configuration.
    #[must_use]
    pub fn new(config: DecodeConfig) -> Self {
        Self {
            config,
            received: None,
            peeling: PeelingTrace::default(),
            elimination: EliminationTrace::default(),
            outcome: None,
        }
    }

    /// Set the received symbols summary.
    pub fn set_received(&mut self, received: ReceivedSummary) {
        self.received = Some(received);
    }

    /// Get mutable access to the peeling trace.
    pub fn peeling_mut(&mut self) -> &mut PeelingTrace {
        &mut self.peeling
    }

    /// Get mutable access to the elimination trace.
    pub fn elimination_mut(&mut self) -> &mut EliminationTrace {
        &mut self.elimination
    }

    /// Mark decode as successful.
    ///
    /// br-asupersync-gvxrxv: panics if the outcome was already set. Callers
    /// must drive a builder through exactly one terminal transition (success
    /// XOR failure) — silently overwriting an earlier outcome would let an
    /// internal-ordering bug bind source_payload_hash to garbage from a
    /// failed decode and attest success for a failed run, bypassing the
    /// s2jxu0 hash-binding protection entirely.
    ///
    /// # Panics
    ///
    /// Panics if the proof outcome was already set by a prior call to
    /// `set_success` or `set_failure`.
    pub fn set_success(&mut self, recovered_source: &[Vec<u8>]) {
        assert!(
            self.outcome.is_none(),
            "ProofBuilder::set_success called after outcome already set \
             (br-asupersync-gvxrxv): existing outcome = {:?}",
            self.outcome
        );
        self.outcome = Some(ProofOutcome::Success {
            symbols_recovered: recovered_source.len(),
            source_payload_hash: recovered_source_hash(recovered_source),
        });
    }

    /// Mark decode as failed.
    ///
    /// br-asupersync-gvxrxv: panics if the outcome was already set. See
    /// [`set_success`](Self::set_success) for the rationale.
    ///
    /// # Panics
    ///
    /// Panics if the proof outcome was already set by a prior call to
    /// `set_success` or `set_failure`.
    pub fn set_failure(&mut self, reason: FailureReason) {
        assert!(
            self.outcome.is_none(),
            "ProofBuilder::set_failure called after outcome already set \
             (br-asupersync-gvxrxv): existing outcome = {:?}",
            self.outcome
        );
        self.outcome = Some(ProofOutcome::Failure { reason });
    }

    /// Build the final proof artifact.
    ///
    /// # Panics
    ///
    /// Panics if received or outcome hasn't been set.
    #[must_use]
    pub fn build(self) -> DecodeProof {
        DecodeProof {
            version: PROOF_SCHEMA_VERSION,
            config: self.config,
            received: self.received.expect("received must be set before build"),
            peeling: self.peeling,
            elimination: self.elimination,
            outcome: self.outcome.expect("outcome must be set before build"),
        }
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    #![allow(
        clippy::pedantic,
        clippy::nursery,
        clippy::expect_fun_call,
        clippy::map_unwrap_or,
        clippy::cast_possible_wrap,
        clippy::future_not_send
    )]
    use super::*;
    use crate::raptorq::decoder::{InactivationDecoder, ReceivedSymbol};
    use crate::raptorq::systematic::SystematicEncoder;
    use serde_json::json;

    fn make_test_config() -> DecodeConfig {
        DecodeConfig {
            object_id: ObjectId::new(0, 1),
            sbn: 0,
            k: 10,
            s: 3,
            h: 2,
            l: 15,
            symbol_size: 64,
            seed: 42,
        }
    }

    fn make_test_recovered(config: &DecodeConfig) -> Vec<Vec<u8>> {
        vec![vec![0u8; config.symbol_size]; config.k]
    }

    fn deterministic_artifact_payload(len: usize) -> Vec<u8> {
        (0..len)
            .map(|idx| (idx.wrapping_mul(37).wrapping_add(11) % 251) as u8)
            .collect()
    }

    fn scrub_failure_reason_for_snapshot_test(reason: &FailureReason) -> serde_json::Value {
        match reason {
            FailureReason::InsufficientSymbols { received, required } => json!({
                "kind": "InsufficientSymbols",
                "received": received,
                "required": required,
            }),
            FailureReason::SingularMatrix {
                row,
                attempted_cols,
            } => json!({
                "kind": "SingularMatrix",
                "row": row,
                "attempted_cols": attempted_cols,
            }),
            FailureReason::SymbolSizeMismatch { expected, actual } => json!({
                "kind": "SymbolSizeMismatch",
                "expected": expected,
                "actual": actual,
            }),
            FailureReason::SymbolEquationArityMismatch {
                esi,
                columns,
                coefficients,
            } => json!({
                "kind": "SymbolEquationArityMismatch",
                "esi": esi,
                "columns": columns,
                "coefficients": coefficients,
            }),
            FailureReason::ColumnIndexOutOfRange {
                esi,
                column,
                max_valid,
            } => json!({
                "kind": "ColumnIndexOutOfRange",
                "esi": esi,
                "column": column,
                "max_valid": max_valid,
            }),
            FailureReason::SourceEsiOutOfRange { esi, max_valid } => json!({
                "kind": "SourceEsiOutOfRange",
                "esi": esi,
                "max_valid": max_valid,
            }),
            FailureReason::InvalidSourceSymbolEquation {
                esi,
                expected_column,
            } => json!({
                "kind": "InvalidSourceSymbolEquation",
                "esi": esi,
                "expected_column": expected_column,
            }),
            FailureReason::CorruptDecodedOutput {
                esi,
                byte_index,
                expected,
                actual,
            } => json!({
                "kind": "CorruptDecodedOutput",
                "esi": esi,
                "byte_index": byte_index,
                "expected": expected,
                "actual": actual,
            }),
            FailureReason::ComputeBudgetExhausted {
                used,
                requested,
                max,
            } => json!({
                "kind": "ComputeBudgetExhausted",
                "used": used,
                "requested": requested,
                "max": max,
            }),
            FailureReason::EsiRateLimitExceeded {
                esi,
                column_count,
                max_columns,
            } => json!({
                "kind": "EsiRateLimitExceeded",
                "esi": esi,
                "column_count": column_count,
                "max_columns": max_columns,
            }),
        }
    }

    fn scrub_decode_proof_for_snapshot_test(proof: &DecodeProof) -> serde_json::Value {
        json!({
            "version": proof.version,
            "content_hash": proof.content_hash(),
            "config": {
                "object_id": "[object_id]",
                "sbn": proof.config.sbn,
                "k": proof.config.k,
                "s": proof.config.s,
                "h": proof.config.h,
                "l": proof.config.l,
                "symbol_size": proof.config.symbol_size,
                "seed": "[seed]",
            },
            "received": {
                "total": proof.received.total,
                "source_count": proof.received.source_count,
                "repair_count": proof.received.repair_count,
                "esi_multiset_hash": proof.received.esi_multiset_hash,
                "esis": proof.received.esis,
                "truncated": proof.received.truncated,
            },
            "peeling": {
                "solved": proof.peeling.solved,
                "solved_indices": proof.peeling.solved_indices,
                "truncated": proof.peeling.truncated,
            },
            "elimination": {
                "strategy": format!("{:?}", proof.elimination.strategy),
                "inactivated": proof.elimination.inactivated,
                "inactive_cols": proof.elimination.inactive_cols,
                "inactive_cols_truncated": proof.elimination.inactive_cols_truncated,
                "pivots": proof.elimination.pivots,
                "pivot_events": proof
                    .elimination
                    .pivot_events
                    .iter()
                    .map(|event| json!({"col": event.col, "row": event.row}))
                    .collect::<Vec<_>>(),
                "pivot_events_truncated": proof.elimination.pivot_events_truncated,
                "row_ops": proof.elimination.row_ops,
                "strategy_transitions": proof
                    .elimination
                    .strategy_transitions
                    .iter()
                    .map(|transition| {
                        json!({
                            "from": format!("{:?}", transition.from),
                            "to": format!("{:?}", transition.to),
                            "reason": transition.reason,
                        })
                    })
                    .collect::<Vec<_>>(),
                "strategy_transitions_truncated": proof.elimination.strategy_transitions_truncated,
            },
            "outcome": match &proof.outcome {
                ProofOutcome::Success {
                    symbols_recovered,
                    source_payload_hash,
                } => json!({
                    "kind": "Success",
                    "symbols_recovered": symbols_recovered,
                    "source_payload_hash": source_payload_hash,
                }),
                ProofOutcome::Failure { reason } => json!({
                    "kind": "Failure",
                    "reason": scrub_failure_reason_for_snapshot_test(reason),
                }),
            },
        })
    }

    fn serialize_decode_proof_bytes_for_snapshot_test(proof: &DecodeProof) -> String {
        use std::fmt::Write as _;

        let bytes = serde_json::to_vec(proof).expect("serialize DecodeProof to JSON bytes");
        let mut rendered = String::new();
        let _ = writeln!(&mut rendered, "len={}", bytes.len());
        for (line_index, chunk) in bytes.chunks(16).enumerate() {
            if line_index > 0 {
                rendered.push('\n');
            }
            for (index, byte) in chunk.iter().enumerate() {
                if index > 0 {
                    rendered.push(' ');
                }
                let _ = write!(&mut rendered, "{byte:02x}");
            }
        }
        rendered
    }

    fn make_success_proof_for_snapshot_test() -> DecodeProof {
        let config = make_test_config();
        let recovered = make_test_recovered(&config);
        let mut builder = DecodeProof::builder(config);

        builder.set_received(ReceivedSummary::from_received(
            (0..15).map(|esi| (esi, esi < 10)),
        ));
        builder.peeling_mut().record_solved(0);
        builder.peeling_mut().record_solved(4);
        builder.peeling_mut().record_solved(7);

        let elimination = builder.elimination_mut();
        elimination.set_strategy(InactivationStrategy::AllAtOnce);
        elimination.record_strategy_transition(
            InactivationStrategy::AllAtOnce,
            InactivationStrategy::HighSupportFirst,
            "dense_repair_mix",
        );
        elimination.record_inactivation(11);
        elimination.record_pivot(11, 2);
        elimination.record_row_op();
        elimination.record_pivot(13, 5);
        elimination.record_row_op();

        builder.set_success(&recovered);
        builder.build()
    }

    fn make_degraded_singular_proof_for_snapshot_test() -> DecodeProof {
        let mut config = make_test_config();
        config.seed = 1337;
        let mut builder = DecodeProof::builder(config);

        builder.set_received(ReceivedSummary::from_received(
            [(0, true), (1, true), (3, true), (10, false), (11, false)].into_iter(),
        ));
        builder.peeling_mut().record_solved(0);

        let elimination = builder.elimination_mut();
        elimination.set_strategy(InactivationStrategy::HighSupportFirst);
        elimination.record_inactivation(8);
        elimination.record_inactivation(9);
        elimination.record_pivot(8, 1);
        elimination.record_row_op();
        elimination.record_strategy_transition(
            InactivationStrategy::HighSupportFirst,
            InactivationStrategy::BlockSchurLowRank,
            "rank_drop_detected",
        );

        builder.set_failure(FailureReason::SingularMatrix {
            row: 6,
            attempted_cols: vec![8, 9, 12],
        });
        builder.build()
    }

    fn make_degraded_insufficient_proof_for_snapshot_test() -> DecodeProof {
        let mut config = make_test_config();
        config.seed = 7;
        let required = config.l;
        let mut builder = DecodeProof::builder(config);

        builder.set_received(ReceivedSummary::from_received(
            (0..6).map(|esi| (esi, true)),
        ));
        builder.peeling_mut().record_solved(1);
        builder.peeling_mut().record_solved(2);
        builder
            .elimination_mut()
            .set_strategy(InactivationStrategy::AllAtOnce);

        builder.set_failure(FailureReason::InsufficientSymbols {
            received: 6,
            required,
        });
        builder.build()
    }

    fn make_source_block_payload_for_snapshot_test(
        k: usize,
        symbol_size: usize,
        salt: u8,
    ) -> Vec<Vec<u8>> {
        (0..k)
            .map(|i| {
                (0..symbol_size)
                    .map(|j| ((i * 37 + j * 19 + usize::from(salt)) % 256) as u8)
                    .collect()
            })
            .collect()
    }

    fn make_known_good_source_block_proof_for_snapshot_test() -> DecodeProof {
        let k = 8;
        let symbol_size = 32;
        let seed = 2026;
        let source = make_source_block_payload_for_snapshot_test(k, symbol_size, 0x21);
        let decoder = InactivationDecoder::new(k, symbol_size, seed);
        let mut received = decoder.constraint_symbols();

        for (esi, data) in source.iter().enumerate() {
            received.push(ReceivedSymbol::source(esi as u32, data.clone()));
        }

        let proof = decoder
            .decode_with_proof(&received, ObjectId::new_for_test(2026), 0)
            .expect("known-good source block should decode")
            .proof;
        assert!(
            matches!(proof.outcome, ProofOutcome::Success { .. }),
            "known-good snapshot scenario must produce a success certificate"
        );
        proof
    }

    fn make_known_bad_source_block_proof_for_snapshot_test() -> DecodeProof {
        let k = 8;
        let symbol_size = 32;
        let seed = 2026;
        let source = make_source_block_payload_for_snapshot_test(k, symbol_size, 0x4D);
        let decoder = InactivationDecoder::new(k, symbol_size, seed);
        let mut received = decoder.constraint_symbols();

        for (esi, data) in source.iter().enumerate().take(4) {
            received.push(ReceivedSymbol::source(esi as u32, data.clone()));
        }

        let (_err, proof) = decoder
            .decode_with_proof(&received, ObjectId::new_for_test(3030), 0)
            .expect_err("known-bad source block should fail decode");
        assert!(
            matches!(proof.outcome, ProofOutcome::Failure { .. }),
            "known-bad snapshot scenario must produce a failure certificate"
        );
        proof
    }

    #[test]
    fn proof_builder_success() {
        let config = make_test_config();
        let recovered = make_test_recovered(&config);
        let mut builder = DecodeProof::builder(config);

        builder.set_received(ReceivedSummary {
            total: 15,
            source_count: 10,
            repair_count: 5,
            esi_multiset_hash: 123,
            esis: (0..15).collect(),
            truncated: false,
        });

        builder.peeling_mut().record_solved(0);
        builder.peeling_mut().record_solved(1);

        builder.elimination_mut().record_inactivation(2);
        builder.elimination_mut().record_pivot(2, 0);
        builder.elimination_mut().record_row_op();

        builder.set_success(&recovered);

        let proof = builder.build();

        assert_eq!(proof.version, PROOF_SCHEMA_VERSION);
        assert_eq!(proof.peeling.solved, 2);
        assert_eq!(proof.elimination.pivots, 1);
        assert!(matches!(proof.outcome, ProofOutcome::Success { .. }));
    }

    #[test]
    fn proof_builder_failure() {
        let config = make_test_config();
        let mut builder = DecodeProof::builder(config);

        builder.set_received(ReceivedSummary {
            total: 5,
            source_count: 5,
            repair_count: 0,
            esi_multiset_hash: 456,
            esis: (0..5).collect(),
            truncated: false,
        });

        builder.set_failure(FailureReason::InsufficientSymbols {
            received: 5,
            required: 15,
        });

        let proof = builder.build();

        assert!(matches!(
            proof.outcome,
            ProofOutcome::Failure {
                reason: FailureReason::InsufficientSymbols { .. }
            }
        ));
    }

    #[test]
    fn decode_proof_certificate_scrubbed() {
        let success = make_success_proof_for_snapshot_test();
        let degraded_singular = make_degraded_singular_proof_for_snapshot_test();
        let degraded_insufficient = make_degraded_insufficient_proof_for_snapshot_test();
        let source_block_success = make_known_good_source_block_proof_for_snapshot_test();
        let source_block_failure = make_known_bad_source_block_proof_for_snapshot_test();

        insta::assert_json_snapshot!(
            "decode_proof_certificate_scrubbed",
            json!({
                "success": scrub_decode_proof_for_snapshot_test(&success),
                "degraded_singular": scrub_decode_proof_for_snapshot_test(&degraded_singular),
                "degraded_insufficient": scrub_decode_proof_for_snapshot_test(&degraded_insufficient),
                "source_block_success": scrub_decode_proof_for_snapshot_test(&source_block_success),
                "source_block_failure": scrub_decode_proof_for_snapshot_test(&source_block_failure),
            })
        );
    }

    #[test]
    fn decode_proof_byte_serialization() {
        let proof = make_success_proof_for_snapshot_test();

        insta::assert_snapshot!(
            "decode_proof_byte_serialization",
            serialize_decode_proof_bytes_for_snapshot_test(&proof)
        );
    }

    #[test]
    fn source_block_decode_proof_hashes_are_stable() {
        let success1 = make_known_good_source_block_proof_for_snapshot_test();
        let success2 = make_known_good_source_block_proof_for_snapshot_test();
        let failure1 = make_known_bad_source_block_proof_for_snapshot_test();
        let failure2 = make_known_bad_source_block_proof_for_snapshot_test();

        assert_eq!(success1.content_hash(), success2.content_hash());
        assert_eq!(failure1.content_hash(), failure2.content_hash());
    }

    /// Pin the JSON shape of every `FailureReason` variant individually so that
    /// adding/renaming/reshaping a variant trips the golden. The constructed
    /// values use distinct, non-default field values per variant to avoid
    /// accidental equality if two variants are confused during refactoring.
    #[test]
    fn failure_reason_variants_scrubbed() {
        let variants: Vec<(&'static str, FailureReason)> = vec![
            (
                "insufficient_symbols",
                FailureReason::InsufficientSymbols {
                    received: 7,
                    required: 15,
                },
            ),
            (
                "singular_matrix",
                FailureReason::SingularMatrix {
                    row: 11,
                    attempted_cols: vec![3, 8, 12, 14],
                },
            ),
            (
                "symbol_size_mismatch",
                FailureReason::SymbolSizeMismatch {
                    expected: 64,
                    actual: 48,
                },
            ),
            (
                "symbol_equation_arity_mismatch",
                FailureReason::SymbolEquationArityMismatch {
                    esi: 21,
                    columns: 4,
                    coefficients: 3,
                },
            ),
            (
                "column_index_out_of_range",
                FailureReason::ColumnIndexOutOfRange {
                    esi: 33,
                    column: 99,
                    max_valid: 15,
                },
            ),
            (
                "source_esi_out_of_range",
                FailureReason::SourceEsiOutOfRange {
                    esi: 42,
                    max_valid: 10,
                },
            ),
            (
                "invalid_source_symbol_equation",
                FailureReason::InvalidSourceSymbolEquation {
                    esi: 5,
                    expected_column: 5,
                },
            ),
            (
                "corrupt_decoded_output",
                FailureReason::CorruptDecodedOutput {
                    esi: 9,
                    byte_index: 17,
                    expected: 0xAB,
                    actual: 0x37,
                },
            ),
        ];

        let mut catalog = serde_json::Map::with_capacity(variants.len());
        for (key, reason) in &variants {
            catalog.insert(
                (*key).to_string(),
                scrub_failure_reason_for_snapshot_test(reason),
            );
        }

        insta::assert_json_snapshot!(
            "failure_reason_variants_scrubbed",
            serde_json::Value::Object(catalog)
        );
    }

    /// Pin the JSON differential between `ProofOutcome::Success` and
    /// `ProofOutcome::Failure` so a refactor that drops `symbols_recovered`
    /// or `source_payload_hash` from the success arm — or that promotes a
    /// failure into a success without re-running the hash — trips the golden.
    #[test]
    fn proof_outcome_success_vs_failure_scrubbed() {
        let success_proof = make_success_proof_for_snapshot_test();
        let failure_proof = make_degraded_singular_proof_for_snapshot_test();

        let success_outcome = match &success_proof.outcome {
            ProofOutcome::Success {
                symbols_recovered,
                source_payload_hash,
            } => json!({
                "kind": "Success",
                "symbols_recovered": symbols_recovered,
                "source_payload_hash": source_payload_hash,
            }),
            ProofOutcome::Failure { .. } => panic!("success fixture must be Success"),
        };
        let failure_outcome = match &failure_proof.outcome {
            ProofOutcome::Failure { reason } => json!({
                "kind": "Failure",
                "reason": scrub_failure_reason_for_snapshot_test(reason),
            }),
            ProofOutcome::Success { .. } => panic!("failure fixture must be Failure"),
        };

        insta::assert_json_snapshot!(
            "proof_outcome_success_vs_failure_scrubbed",
            json!({
                "success": success_outcome,
                "failure": failure_outcome,
            })
        );
    }

    /// Pin the determinism of `recovered_source_hash` across a fixed table of
    /// shapes so that any change to the hash function (algorithm choice, salt,
    /// length-prefix encoding, byte order) trips the golden. Catches s2jxu0
    /// hash-binding regressions that would otherwise let a divergent decoder
    /// silently bind a stale hash to a successful decode certificate.
    #[test]
    fn recovered_source_hash_golden_table() {
        let cases: Vec<(&'static str, Vec<Vec<u8>>)> = vec![
            ("empty", Vec::new()),
            ("single_zero_symbol_size_64", vec![vec![0u8; 64]]),
            (
                "two_symbols_distinct_pattern",
                vec![
                    (0..32).map(|i| i as u8).collect(),
                    (0..32).map(|i| (255 - i) as u8).collect(),
                ],
            ),
            (
                "k10_symbol_size_64_deterministic_fill",
                (0..10)
                    .map(|i| {
                        (0..64)
                            .map(|j| ((i * 37 + j * 19 + 0x21) % 256) as u8)
                            .collect()
                    })
                    .collect(),
            ),
            (
                "irregular_symbol_lengths",
                vec![
                    vec![0xDE, 0xAD],
                    vec![0xBE, 0xEF, 0x00, 0x01],
                    vec![0xFE; 8],
                ],
            ),
            (
                "empty_symbol_then_full_symbol",
                vec![Vec::new(), vec![0xFFu8; 16]],
            ),
        ];

        let mut table = serde_json::Map::with_capacity(cases.len());
        for (label, source) in &cases {
            let hash = recovered_source_hash(source);
            table.insert(
                (*label).to_string(),
                json!({
                    "shape": source.iter().map(Vec::len).collect::<Vec<_>>(),
                    "hash": hash,
                }),
            );
        }

        insta::assert_json_snapshot!(
            "recovered_source_hash_golden_table",
            serde_json::Value::Object(table)
        );
    }

    #[test]
    fn received_summary_truncation() {
        let symbols = (0..2000).map(|i| (i, i < 1000));
        let summary = ReceivedSummary::from_received(symbols);

        assert_eq!(summary.total, 2000);
        assert_eq!(summary.source_count, 1000);
        assert_eq!(summary.repair_count, 1000);
        assert_eq!(summary.esis.len(), MAX_RECEIVED_SYMBOLS);
        assert!(summary.truncated);
    }

    #[test]
    fn received_summary_hash_changes_when_high_esis_change_beyond_preview() {
        let total = MAX_RECEIVED_SYMBOLS as u32 + 8;
        let original = ReceivedSummary::from_received((0..total).map(|esi| (esi, esi < 8)));
        let mutated = ReceivedSummary::from_received(
            (0..(total - 1))
                .map(|esi| (esi, esi < 8))
                .chain(std::iter::once((u32::MAX - 7, false))),
        );

        assert_eq!(original.total, mutated.total);
        assert_eq!(original.source_count, mutated.source_count);
        assert_eq!(original.repair_count, mutated.repair_count);
        assert_eq!(
            original.esis, mutated.esis,
            "preview ESIs should stay identical when only higher truncated ESIs differ"
        );
        assert!(original.truncated);
        assert!(mutated.truncated);
        assert_ne!(
            original.esi_multiset_hash, mutated.esi_multiset_hash,
            "full multiset hash must distinguish divergence beyond the preview window"
        );
    }

    #[test]
    fn received_summary_hash_is_order_independent_for_same_multiset() {
        let ordered = [
            (9, false),
            (1, true),
            (7, false),
            (1, true),
            (4, false),
            (2, true),
        ];
        let permuted = [
            (2, true),
            (4, false),
            (1, true),
            (9, false),
            (1, true),
            (7, false),
        ];

        let ordered_summary = ReceivedSummary::from_received(ordered.into_iter());
        let permuted_summary = ReceivedSummary::from_received(permuted.into_iter());

        assert_eq!(ordered_summary.total, permuted_summary.total);
        assert_eq!(ordered_summary.source_count, permuted_summary.source_count);
        assert_eq!(ordered_summary.repair_count, permuted_summary.repair_count);
        assert_eq!(ordered_summary.esis, permuted_summary.esis);
        assert_eq!(
            ordered_summary.esi_multiset_hash, permuted_summary.esi_multiset_hash,
            "multiset hash must remain stable across input orderings"
        );
    }

    #[test]
    fn content_hash_deterministic() {
        let config = make_test_config();
        let recovered = make_test_recovered(&config);
        let mut builder1 = DecodeProof::builder(config.clone());
        let mut builder2 = DecodeProof::builder(config);

        for builder in [&mut builder1, &mut builder2] {
            builder.set_received(ReceivedSummary {
                total: 15,
                source_count: 10,
                repair_count: 5,
                esi_multiset_hash: 999,
                esis: (0..15).collect(),
                truncated: false,
            });
            builder.set_success(&recovered);
        }

        let proof1 = builder1.build();
        let proof2 = builder2.build();

        assert_eq!(proof1.content_hash(), proof2.content_hash());
    }

    #[test]
    fn recovered_source_hash_binds_symbol_boundaries() {
        let split_symbols = vec![vec![0x10, 0x20], vec![0x30, 0x40]];
        let merged_suffix = vec![vec![0x10], vec![0x20, 0x30, 0x40]];

        assert_eq!(
            split_symbols.concat(),
            merged_suffix.concat(),
            "test setup should keep the flattened payload identical"
        );
        assert_ne!(
            recovered_source_hash(&split_symbols),
            recovered_source_hash(&merged_suffix),
            "proof success hash must bind per-symbol boundaries, not just flattened bytes"
        );
    }

    #[test]
    fn recovered_source_hash_binds_symbol_order() {
        let ordered = vec![vec![0xAA, 0x01], vec![0xBB, 0x02], vec![0xCC, 0x03]];
        let reordered = vec![vec![0xCC, 0x03], vec![0xBB, 0x02], vec![0xAA, 0x01]];

        assert_ne!(
            recovered_source_hash(&ordered),
            recovered_source_hash(&reordered),
            "proof success hash must distinguish reordered recovered source symbols"
        );
    }

    #[test]
    fn replay_verification_roundtrip() {
        let k = 8;
        let symbol_size = 32;
        let seed = 99u64;

        let source: Vec<Vec<u8>> = (0..k)
            .map(|i| {
                (0..symbol_size)
                    .map(|j| ((i * 53 + j * 19 + 3) % 256) as u8)
                    .collect()
            })
            .collect();

        let encoder = SystematicEncoder::new(&source, symbol_size, seed).unwrap();
        let decoder = InactivationDecoder::new(k, symbol_size, seed);
        let l = decoder.params().l;

        // Start with constraint symbols (LDPC + HDPC with zero data)
        let mut received = decoder.constraint_symbols();

        // Add source symbols
        for (i, data) in source.iter().enumerate() {
            received.push(ReceivedSymbol::source(i as u32, data.clone()));
        }

        // Add repair symbols
        for esi in (k as u32)..(l as u32) {
            let (cols, coefs) = decoder
                .repair_equation(esi)
                .expect("repair equation should succeed with valid test parameters");
            let repair_data = encoder.repair_symbol(esi);
            received.push(ReceivedSymbol::repair(esi, cols, coefs, repair_data));
        }

        let object_id = ObjectId::new_for_test(777);
        let proof = decoder
            .decode_with_proof(&received, object_id, 0)
            .expect("decode should succeed")
            .proof;

        proof
            .replay_and_verify(&received)
            .expect("replay verification should succeed");
    }

    // Pure data-type tests (wave 18 – CyanBarn)

    #[test]
    fn decode_config_debug_clone_hash_eq() {
        let cfg = make_test_config();
        let cfg2 = cfg.clone();
        assert_eq!(cfg, cfg2);
        assert!(format!("{cfg:?}").contains("DecodeConfig"));
    }

    #[test]
    fn received_summary_debug_clone_hash_eq() {
        let summary = ReceivedSummary {
            total: 10,
            source_count: 7,
            repair_count: 3,
            esi_multiset_hash: 789,
            esis: vec![0, 1, 2],
            truncated: false,
        };
        let summary2 = summary.clone();
        assert_eq!(summary, summary2);
        assert!(format!("{summary:?}").contains("ReceivedSummary"));
    }

    #[test]
    fn received_summary_from_received_empty() {
        let summary = ReceivedSummary::from_received(std::iter::empty());
        assert_eq!(summary.total, 0);
        assert_eq!(summary.source_count, 0);
        assert_eq!(summary.repair_count, 0);
        assert!(summary.esis.is_empty());
        assert!(!summary.truncated);
    }

    #[test]
    fn peeling_trace_debug_clone_default_hash_eq() {
        let trace = PeelingTrace::default();
        let trace2 = trace.clone();
        assert_eq!(trace, trace2);
        assert_eq!(trace.solved, 0);
        assert!(format!("{trace:?}").contains("PeelingTrace"));
    }

    #[test]
    fn peeling_trace_record_solved() {
        let mut trace = PeelingTrace::default();
        trace.record_solved(5);
        trace.record_solved(10);
        assert_eq!(trace.solved, 2);
        assert_eq!(trace.solved_indices, vec![5, 10]);
    }

    #[test]
    fn elimination_trace_debug_clone_default_hash_eq() {
        let trace = EliminationTrace::default();
        let trace2 = trace.clone();
        assert_eq!(trace, trace2);
        assert!(format!("{trace:?}").contains("EliminationTrace"));
    }

    #[test]
    fn elimination_trace_record_operations() {
        let mut trace = EliminationTrace::default();
        trace.record_inactivation(3);
        trace.record_pivot(3, 0);
        trace.record_row_op();
        assert_eq!(trace.inactivated, 1);
        assert_eq!(trace.pivots, 1);
        assert_eq!(trace.row_ops, 1);
        assert_eq!(trace.pivot_events.len(), 1);
        assert!(!trace.inactive_cols_truncated);
        assert!(!trace.pivot_events_truncated);
        assert!(!trace.strategy_transitions_truncated);
    }

    #[test]
    fn replay_verification_keeps_non_truncated_elimination_subtraces_strict() {
        let config = make_test_config();
        let recovered = make_test_recovered(&config);
        let mut expected_builder = DecodeProof::builder(config);
        expected_builder.set_received(ReceivedSummary {
            total: 10,
            source_count: 10,
            repair_count: 0,
            esi_multiset_hash: 321,
            esis: (0..10).collect(),
            truncated: false,
        });
        expected_builder.set_success(&recovered);

        let elimination = expected_builder.elimination_mut();
        for col in 0..=MAX_PIVOT_EVENTS {
            elimination.record_inactivation(col);
        }
        elimination.record_pivot(3, 0);
        elimination.record_strategy_transition(
            InactivationStrategy::AllAtOnce,
            InactivationStrategy::HighSupportFirst,
            "dense_or_near_square",
        );

        let expected = expected_builder.build();
        assert!(expected.elimination.inactive_cols_truncated);
        assert!(!expected.elimination.pivot_events_truncated);
        assert!(!expected.elimination.strategy_transitions_truncated);

        let mut actual = expected.clone();
        actual
            .elimination
            .pivot_events
            .push(PivotEvent { col: 9, row: 1 });

        let err = compare_proofs(&expected, &actual)
            .expect_err("extra non-truncated pivot events must fail replay verification");
        assert!(
            err.to_string().contains("elimination.pivot_events"),
            "mismatch should point directly at the non-truncated elimination sub-trace"
        );
    }

    #[test]
    fn replay_verification_rejects_extra_entries_in_truncated_subtraces() {
        let config = make_test_config();
        let recovered = make_test_recovered(&config);
        let mut expected_builder = DecodeProof::builder(config);
        expected_builder.set_received(ReceivedSummary {
            total: 10,
            source_count: 10,
            repair_count: 0,
            esi_multiset_hash: 321,
            esis: (0..10).collect(),
            truncated: false,
        });
        expected_builder.set_success(&recovered);

        let elimination = expected_builder.elimination_mut();
        for col in 0..=MAX_PIVOT_EVENTS {
            elimination.record_inactivation(col);
        }

        let expected = expected_builder.build();
        assert!(expected.elimination.inactive_cols_truncated);

        let mut actual = expected.clone();
        actual.elimination.inactive_cols.push(MAX_PIVOT_EVENTS + 99);

        let err = compare_proofs(&expected, &actual)
            .expect_err("truncated previews must still reject extra recorded entries");
        assert!(
            err.to_string().contains("elimination.inactive_cols"),
            "mismatch should point directly at the truncated elimination preview"
        );
    }

    #[test]
    fn inactivation_strategy_debug_clone_copy_default_hash_eq() {
        let s = InactivationStrategy::default();
        assert_eq!(s, InactivationStrategy::AllAtOnce);
        let s2 = s;
        assert_eq!(s, s2);
        assert!(format!("{s:?}").contains("AllAtOnce"));
    }

    #[test]
    fn inactivation_strategy_all_variants() {
        let variants = [
            InactivationStrategy::AllAtOnce,
            InactivationStrategy::HighSupportFirst,
            InactivationStrategy::BlockSchurLowRank,
        ];
        for (i, v) in variants.iter().enumerate() {
            for (j, v2) in variants.iter().enumerate() {
                if i == j {
                    assert_eq!(v, v2);
                } else {
                    assert_ne!(v, v2);
                }
            }
        }
    }

    #[test]
    fn strategy_transition_debug_clone_hash_eq() {
        let t = StrategyTransition {
            from: InactivationStrategy::AllAtOnce,
            to: InactivationStrategy::HighSupportFirst,
            reason: "escalation",
        };
        let t2 = t.clone();
        assert_eq!(t, t2);
        assert!(format!("{t:?}").contains("StrategyTransition"));
    }

    #[test]
    fn pivot_event_debug_clone_hash_eq() {
        let p = PivotEvent { col: 3, row: 7 };
        let p2 = p.clone();
        assert_eq!(p, p2);
        assert!(format!("{p:?}").contains("PivotEvent"));
    }

    #[test]
    fn proof_outcome_debug_clone_hash_eq() {
        let success = ProofOutcome::Success {
            symbols_recovered: 10,
            source_payload_hash: 123,
        };
        let success2 = success.clone();
        assert_eq!(success, success2);
        assert!(format!("{success:?}").contains("Success"));

        let fail = ProofOutcome::Failure {
            reason: FailureReason::InsufficientSymbols {
                received: 5,
                required: 10,
            },
        };
        assert_ne!(success, fail);
    }

    #[test]
    fn failure_reason_all_variants() {
        let variants: Vec<FailureReason> = vec![
            FailureReason::InsufficientSymbols {
                received: 1,
                required: 2,
            },
            FailureReason::SingularMatrix {
                row: 0,
                attempted_cols: vec![1, 2],
            },
            FailureReason::SymbolSizeMismatch {
                expected: 64,
                actual: 32,
            },
            FailureReason::SymbolEquationArityMismatch {
                esi: 5,
                columns: 3,
                coefficients: 4,
            },
            FailureReason::ColumnIndexOutOfRange {
                esi: 1,
                column: 99,
                max_valid: 15,
            },
            FailureReason::CorruptDecodedOutput {
                esi: 0,
                byte_index: 7,
                expected: 0xAA,
                actual: 0xBB,
            },
        ];
        for v in &variants {
            assert!(!format!("{v:?}").is_empty());
        }
    }

    #[test]
    fn replay_error_display_mismatch() {
        let err = ReplayError::Mismatch {
            field: "version",
            expected: "1".into(),
            actual: "2".into(),
        };
        let s = err.to_string();
        assert!(s.contains("version"));
        assert!(s.contains("expected"));
        assert!(format!("{err:?}").contains("Mismatch"));
    }

    #[test]
    fn replay_error_display_sequence() {
        let err = ReplayError::SequenceMismatch {
            label: "esis",
            index: 5,
            expected: "10".into(),
            actual: "20".into(),
        };
        let s = err.to_string();
        assert!(s.contains("esis"));
        assert!(s.contains("index 5"));
    }

    #[test]
    fn replay_error_trait() {
        let err: Box<dyn std::error::Error> = Box::new(ReplayError::Mismatch {
            field: "test",
            expected: "a".into(),
            actual: "b".into(),
        });
        assert!(!err.to_string().is_empty());
    }

    #[test]
    fn decode_proof_debug_clone_eq() {
        let config = make_test_config();
        let recovered = make_test_recovered(&config);
        let mut builder = DecodeProof::builder(config);
        builder.set_received(ReceivedSummary {
            total: 10,
            source_count: 10,
            repair_count: 0,
            esi_multiset_hash: 321,
            esis: (0..10).collect(),
            truncated: false,
        });
        builder.set_success(&recovered);
        let proof = builder.build();
        let proof2 = proof.clone();
        assert_eq!(proof, proof2);
        assert!(format!("{proof:?}").contains("DecodeProof"));
    }

    #[test]
    fn decode_proof_builder_debug() {
        let builder = DecodeProof::builder(make_test_config());
        assert!(format!("{builder:?}").contains("DecodeProofBuilder"));
    }

    /// br-asupersync-gvxrxv: ProofBuilder must reject double-set of
    /// outcome. Internal call-ordering bugs that re-call set_success or
    /// set_failure after an earlier outcome was already recorded must
    /// panic, NOT silently overwrite — silent overwrite would let a
    /// failed-then-succeeded path bind source_payload_hash to garbage and
    /// attest success for a failed decode (bypasses s2jxu0 hash binding).
    #[test]
    #[should_panic(expected = "set_success called after outcome already set")]
    fn gvxrxv_set_success_after_set_success_panics() {
        let mut builder = DecodeProof::builder(make_test_config());
        let recovered: Vec<Vec<u8>> = vec![vec![1, 2, 3]; 4];
        builder.set_success(&recovered);
        builder.set_success(&recovered); // must panic
    }

    #[test]
    #[should_panic(expected = "set_failure called after outcome already set")]
    fn gvxrxv_set_failure_after_set_failure_panics() {
        let mut builder = DecodeProof::builder(make_test_config());
        builder.set_failure(FailureReason::SingularMatrix {
            row: 0,
            attempted_cols: vec![0, 1],
        });
        builder.set_failure(FailureReason::SingularMatrix {
            row: 0,
            attempted_cols: vec![0, 1],
        }); // must panic
    }

    #[test]
    #[should_panic(expected = "set_failure called after outcome already set")]
    fn gvxrxv_set_failure_after_set_success_panics() {
        let mut builder = DecodeProof::builder(make_test_config());
        let recovered: Vec<Vec<u8>> = vec![vec![1, 2, 3]; 4];
        builder.set_success(&recovered);
        builder.set_failure(FailureReason::SingularMatrix {
            row: 0,
            attempted_cols: vec![0, 1],
        }); // must panic — would otherwise corrupt the certified outcome
    }

    #[test]
    #[should_panic(expected = "set_success called after outcome already set")]
    fn gvxrxv_set_success_after_set_failure_panics() {
        let mut builder = DecodeProof::builder(make_test_config());
        builder.set_failure(FailureReason::SingularMatrix {
            row: 0,
            attempted_cols: vec![0, 1],
        });
        let recovered: Vec<Vec<u8>> = vec![vec![1, 2, 3]; 4];
        builder.set_success(&recovered); // must panic — most dangerous case (bypasses s2jxu0 hash binding)
    }

    #[test]
    fn elimination_trace_strategy_transition_same_is_noop() {
        let mut trace = EliminationTrace::default();
        trace.record_strategy_transition(
            InactivationStrategy::AllAtOnce,
            InactivationStrategy::AllAtOnce,
            "noop",
        );
        assert!(trace.strategy_transitions.is_empty());
        assert_eq!(trace.strategy, InactivationStrategy::AllAtOnce);
    }

    #[test]
    fn elimination_trace_strategy_transition_records() {
        let mut trace = EliminationTrace::default();
        trace.record_strategy_transition(
            InactivationStrategy::AllAtOnce,
            InactivationStrategy::HighSupportFirst,
            "escalation",
        );
        assert_eq!(trace.strategy_transitions.len(), 1);
        assert_eq!(trace.strategy, InactivationStrategy::HighSupportFirst);
    }

    #[test]
    fn replay_verification_detects_mismatch() {
        let k = 6;
        let symbol_size = 24;
        let seed = 17u64;

        let source: Vec<Vec<u8>> = (0..k)
            .map(|i| {
                (0..symbol_size)
                    .map(|j| ((i * 41 + j * 11 + 5) % 256) as u8)
                    .collect()
            })
            .collect();

        let encoder = SystematicEncoder::new(&source, symbol_size, seed).unwrap();
        let decoder = InactivationDecoder::new(k, symbol_size, seed);
        let l = decoder.params().l;

        // Start with constraint symbols (LDPC + HDPC with zero data)
        let mut received = decoder.constraint_symbols();

        // Add source symbols
        for (i, data) in source.iter().enumerate() {
            received.push(ReceivedSymbol::source(i as u32, data.clone()));
        }

        // Add repair symbols
        for esi in (k as u32)..(l as u32) {
            let (cols, coefs) = decoder
                .repair_equation(esi)
                .expect("repair equation should succeed with valid test parameters");
            let repair_data = encoder.repair_symbol(esi);
            received.push(ReceivedSymbol::repair(esi, cols, coefs, repair_data));
        }

        let object_id = ObjectId::new_for_test(42);
        let mut proof = decoder
            .decode_with_proof(&received, object_id, 0)
            .expect("decode should succeed")
            .proof;

        proof.elimination.row_ops = proof.elimination.row_ops.saturating_add(1);

        let err = proof
            .replay_and_verify(&received)
            .expect_err("replay should detect mismatch");
        assert!(err.to_string().contains("row_ops"));
    }

    #[test]
    fn replay_verification_rejects_payload_divergent_success() {
        let k = 8;
        let symbol_size = 32;
        let seed = 123u64;

        let make_source = |salt: u8| -> Vec<Vec<u8>> {
            (0..k)
                .map(|i| {
                    (0..symbol_size)
                        .map(|j| ((i * 53 + j * 19 + usize::from(salt)) % 256) as u8)
                        .collect()
                })
                .collect()
        };
        let make_received =
            |decoder: &InactivationDecoder, source: &[Vec<u8>]| -> Vec<ReceivedSymbol> {
                let encoder = SystematicEncoder::new(source, symbol_size, seed).unwrap();
                let l = decoder.params().l;
                let mut received = decoder.constraint_symbols();
                for (i, data) in source.iter().enumerate() {
                    received.push(ReceivedSymbol::source(i as u32, data.clone()));
                }
                for esi in (k as u32)..(l as u32) {
                    let (cols, coefs) = decoder
                        .repair_equation(esi)
                        .expect("repair equation should succeed with valid test parameters");
                    let repair_data = encoder.repair_symbol(esi);
                    received.push(ReceivedSymbol::repair(esi, cols, coefs, repair_data));
                }
                received
            };

        let source = make_source(3);
        let mutated_source = make_source(11);
        let decoder = InactivationDecoder::new(k, symbol_size, seed);
        let original_received = make_received(&decoder, &source);
        let mutated_received = make_received(&decoder, &mutated_source);
        let object_id = ObjectId::new_for_test(8080);

        let proof = decoder
            .decode_with_proof(&original_received, object_id, 0)
            .expect("original decode should succeed")
            .proof;
        let mutated_result = decoder
            .decode_with_proof(&mutated_received, object_id, 0)
            .expect("mutated decode should still succeed");
        assert_eq!(mutated_result.result.source, mutated_source);
        assert_ne!(mutated_result.result.source, source);

        let err = proof
            .replay_and_verify(&mutated_received)
            .expect_err("payload-divergent replay must fail verification");
        assert!(err.to_string().contains("source_payload_hash"));
    }

    #[test]
    fn replay_verification_rejects_high_esi_divergence_when_received_preview_truncates() {
        let k = 8;
        let symbol_size = 32;
        let seed = 321u64;
        let repair_count = MAX_RECEIVED_SYMBOLS as u32 + 32;

        let source: Vec<Vec<u8>> = (0..k)
            .map(|i| {
                (0..symbol_size)
                    .map(|j| ((i * 37 + j * 17 + 9) % 256) as u8)
                    .collect()
            })
            .collect();
        let encoder = SystematicEncoder::new(&source, symbol_size, seed).unwrap();
        let decoder = InactivationDecoder::new(k, symbol_size, seed);

        let mut original_received = decoder.constraint_symbols();
        for (i, data) in source.iter().enumerate() {
            original_received.push(ReceivedSymbol::source(i as u32, data.clone()));
        }
        for offset in 0..repair_count {
            let esi = k as u32 + offset;
            let (cols, coefs) = decoder
                .repair_equation(esi)
                .expect("repair equation should succeed with valid test parameters");
            let repair_data = encoder.repair_symbol(esi);
            original_received.push(ReceivedSymbol::repair(esi, cols, coefs, repair_data));
        }

        let mut mutated_received = original_received.clone();
        let replaced_esi = k as u32 + repair_count - 1;
        let replacement_esi = replaced_esi + 10_000;
        let (replacement_cols, replacement_coefs) = decoder
            .repair_equation(replacement_esi)
            .expect("repair equation should succeed with valid test parameters");
        let replacement_data = encoder.repair_symbol(replacement_esi);
        let replacement = ReceivedSymbol::repair(
            replacement_esi,
            replacement_cols,
            replacement_coefs,
            replacement_data,
        );
        let replaced_symbol = mutated_received
            .last_mut()
            .expect("repair-heavy test input must contain a trailing repair symbol");
        assert_eq!(replaced_symbol.esi, replaced_esi);
        *replaced_symbol = replacement;

        let object_id = ObjectId::new_for_test(9090);
        let proof = decoder
            .decode_with_proof(&original_received, object_id, 0)
            .expect("original decode should succeed")
            .proof;
        let mutated_result = decoder
            .decode_with_proof(&mutated_received, object_id, 0)
            .expect("mutated decode should still succeed with enough symbols");
        assert_eq!(mutated_result.result.source, source);

        let original_summary =
            ReceivedSummary::from_received(original_received.iter().map(|s| (s.esi, s.is_source)));
        let mutated_summary =
            ReceivedSummary::from_received(mutated_received.iter().map(|s| (s.esi, s.is_source)));
        assert_eq!(
            original_summary.esis, mutated_summary.esis,
            "preview ESIs should not expose the high-ESI divergence"
        );
        assert_ne!(
            original_summary.esi_multiset_hash, mutated_summary.esi_multiset_hash,
            "full multiset binding must distinguish the mutated higher ESI"
        );

        let err = proof
            .replay_and_verify(&mutated_received)
            .expect_err("truncated received-summary replay must reject higher-ESI divergence");
        assert!(err.to_string().contains("received.esi_multiset_hash"));
    }

    // ============================================================================
    // br-asupersync-x5roo0: Cryptographic attestation security tests
    // ============================================================================

    #[test]
    fn content_hash_produces_256_bit_cryptographic_hash() {
        // br-asupersync-x5roo0: Verify that content_hash now returns a 256-bit
        // cryptographic hash instead of a 64-bit non-cryptographic hash.

        let config = make_test_config();
        let mut builder = DecodeProof::builder(config);
        builder.set_received(ReceivedSummary::from_received(std::iter::empty()));
        let proof = builder.build();

        let hash = proof.content_hash();

        // Verify it's 256 bits (32 bytes)
        assert_eq!(hash.as_bytes().len(), 32);

        // Verify hex encoding works and produces 64 hex characters
        let hex = hash.to_hex();
        assert_eq!(hex.len(), 64);
        assert!(
            hex.chars()
                .all(|c| c.is_ascii_hexdigit() && !c.is_ascii_uppercase())
        );
    }

    #[test]
    fn content_hash_deterministic_across_identical_proofs() {
        // Verify that identical proofs produce identical hashes (deterministic).

        let config = make_test_config();
        let proof1 = DecodeProof::builder(config.clone()).build();
        let proof2 = DecodeProof::builder(config).build();

        assert_eq!(
            proof1.content_hash().as_bytes(),
            proof2.content_hash().as_bytes()
        );
    }

    #[test]
    fn content_hash_differs_for_different_proofs() {
        // Verify that different proofs produce different hashes (collision resistance).

        let config1 = make_test_config();
        let mut config2 = make_test_config();
        config2.k = config1.k + 1; // Different configuration

        let proof1 = DecodeProof::builder(config1).build();
        let proof2 = DecodeProof::builder(config2).build();

        assert_ne!(
            proof1.content_hash().as_bytes(),
            proof2.content_hash().as_bytes()
        );
    }

    #[test]
    fn forged_proof_rejected_by_hash_mismatch() {
        // br-asupersync-x5roo0: Regression test ensuring that forged proofs
        // with tampered fields are rejected due to hash mismatch.

        let config = make_test_config();
        let mut original_builder = DecodeProof::builder(config.clone());
        let original_received =
            ReceivedSummary::from_received([(0, true), (1, true), (2, true)].iter().copied());
        original_builder.set_received(original_received);
        let original_source = vec![vec![0x12, 0x34, 0x56, 0x78]; 8];
        original_builder.set_success(&original_source);
        let original_proof = original_builder.build();

        // Create a forged proof with different outcome data but claim it matches
        let mut forged_builder = DecodeProof::builder(config);
        let forged_received =
            ReceivedSummary::from_received([(0, true), (1, true), (2, true)].iter().copied());
        forged_builder.set_received(forged_received);
        // Forge the success outcome with different data
        let forged_source = vec![vec![0xDE, 0xAD, 0xBE, 0xEF]; 10]; // Different from original
        forged_builder.set_success(&forged_source);
        let forged_proof = forged_builder.build();

        // Verify the hashes are different (forgery detected)
        assert_ne!(
            original_proof.content_hash().as_bytes(),
            forged_proof.content_hash().as_bytes(),
            "Forged proof must produce different hash than original"
        );

        // Verify that the hash difference is significant (not just 1-2 bits)
        let original_hash = original_proof.content_hash();
        let forged_hash = forged_proof.content_hash();

        let differing_bytes = original_hash
            .as_bytes()
            .iter()
            .zip(forged_hash.as_bytes())
            .filter(|(a, b)| a != b)
            .count();

        assert!(
            differing_bytes > 16,
            "Cryptographic hash should produce avalanche effect with many differing bytes, got {differing_bytes}"
        );
    }

    #[test]
    fn forged_proof_configuration_tampering_detected() {
        // Test that tampering with configuration fields is detected.

        let config1 = make_test_config();
        let mut config2 = config1.clone();
        config2.seed = config1.seed.wrapping_add(1); // Minimal change

        let proof1 = DecodeProof::builder(config1).build();
        let proof2 = DecodeProof::builder(config2).build();

        assert_ne!(
            proof1.content_hash().as_bytes(),
            proof2.content_hash().as_bytes(),
            "Even minimal configuration changes must be detected"
        );
    }

    #[test]
    fn forged_proof_received_summary_tampering_detected() {
        // Test that tampering with received symbol summary is detected.

        let config = make_test_config();

        let mut builder1 = DecodeProof::builder(config.clone());
        let received1 =
            ReceivedSummary::from_received([(0, true), (1, true), (2, true)].iter().copied());
        builder1.set_received(received1);
        let proof1 = builder1.build();

        let mut builder2 = DecodeProof::builder(config);
        let mut received2 =
            ReceivedSummary::from_received([(0, true), (1, true), (2, true)].iter().copied());
        received2.esi_multiset_hash = received2.esi_multiset_hash.wrapping_add(1); // Forge the hash
        builder2.set_received(received2);
        let proof2 = builder2.build();

        assert_ne!(
            proof1.content_hash().as_bytes(),
            proof2.content_hash().as_bytes(),
            "Tampering with received symbol hash must be detected"
        );
    }

    #[test]
    fn forged_proof_elimination_strategy_tampering_detected() {
        // Test that changing the elimination strategy is detected.

        let config = make_test_config();

        let mut builder1 = DecodeProof::builder(config.clone());
        let received1 = ReceivedSummary::from_received([(0, true), (1, true)].iter().copied());
        builder1.set_received(received1);
        builder1
            .elimination_mut()
            .set_strategy(InactivationStrategy::AllAtOnce);
        let proof1 = builder1.build();

        let mut builder2 = DecodeProof::builder(config);
        let received2 = ReceivedSummary::from_received([(0, true), (1, true)].iter().copied());
        builder2.set_received(received2);
        builder2
            .elimination_mut()
            .set_strategy(InactivationStrategy::HighSupportFirst);
        let proof2 = builder2.build();

        assert_ne!(
            proof1.content_hash().as_bytes(),
            proof2.content_hash().as_bytes(),
            "Different elimination strategies must produce different hashes"
        );
    }

    #[test]
    fn forged_proof_elimination_pivot_trace_tampering_detected() {
        // RFC 6330 Section 6 recommends integrity checks before accepting a
        // decoded artifact. A forged proof must not be able to rewrite the
        // elimination trace while preserving the attested hash.

        let config = make_test_config();
        let received = ReceivedSummary::from_received([(0, true), (1, true)].iter().copied());

        let mut builder1 = DecodeProof::builder(config.clone());
        builder1.set_received(received.clone());
        builder1.elimination_mut().record_pivot(3, 0);
        let proof1 = builder1.build();

        let mut builder2 = DecodeProof::builder(config);
        builder2.set_received(received);
        builder2.elimination_mut().record_pivot(7, 1);
        let proof2 = builder2.build();

        assert_eq!(proof1.elimination.pivots, proof2.elimination.pivots);
        assert_ne!(
            proof1.elimination.pivot_events,
            proof2.elimination.pivot_events
        );
        assert_ne!(
            proof1.content_hash().as_bytes(),
            proof2.content_hash().as_bytes(),
            "Forged pivot traces must change the attested proof hash"
        );
    }

    #[test]
    fn recovered_source_hash_upgraded_to_cryptographic() {
        // br-asupersync-x5roo0: Verify that recovered_source_hash now uses
        // SHA-256 internally for cryptographic strength.

        let source1 = vec![vec![1, 2, 3, 4], vec![5, 6, 7, 8]];
        let source2 = vec![vec![1, 2, 3, 5], vec![5, 6, 7, 8]]; // Minimal change

        let hash1 = recovered_source_hash(&source1);
        let hash2 = recovered_source_hash(&source2);

        assert_ne!(
            hash1, hash2,
            "Minimal source changes should produce different hashes"
        );

        // Verify deterministic behavior
        let hash1_repeat = recovered_source_hash(&source1);
        assert_eq!(hash1, hash1_repeat, "Hash should be deterministic");
    }

    #[test]
    fn proof_hash_hex_roundtrip() {
        // Test that ProofHash hex encoding/decoding works correctly.

        let config = make_test_config();
        let mut builder = DecodeProof::builder(config);
        builder.set_received(ReceivedSummary::from_received(std::iter::empty()));
        let proof = builder.build();
        let original_hash = proof.content_hash();

        let hex = original_hash.to_hex();
        let decoded_hash = ProofHash::from_hex(&hex).expect("Valid hex should decode successfully");

        assert_eq!(original_hash.as_bytes(), decoded_hash.as_bytes());
    }

    #[test]
    fn proof_hash_hex_encoding_invalid_input_rejected() {
        // Test that invalid hex inputs are properly rejected.

        assert!(ProofHash::from_hex("invalid hex").is_none());
        assert!(ProofHash::from_hex("").is_none());
        assert!(ProofHash::from_hex("01234567890abcdef").is_none()); // Too short

        // Test wrong length (63 chars instead of 64)
        let short_hex = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde";
        assert!(ProofHash::from_hex(short_hex).is_none());

        // Test wrong length (65 chars)
        let long_hex = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0";
        assert!(ProofHash::from_hex(long_hex).is_none());
    }

    #[test]
    fn source_payload_hash_mismatch_fails_verification() {
        // Test that hash mismatches are properly detected in fail-closed manner
        let config = make_test_config();

        // Create original proof with one source payload
        let source1 = vec![vec![1, 2, 3, 4], vec![5, 6, 7, 8]];
        let mut builder1 = DecodeProof::builder(config.clone());
        builder1.set_received(ReceivedSummary::from_received(std::iter::empty()));
        builder1.set_success(&source1);
        let proof1 = builder1.build();

        // Create proof with different source payload (different hash)
        let source2 = vec![vec![1, 2, 3, 5], vec![5, 6, 7, 8]]; // Minimal change
        let mut builder2 = DecodeProof::builder(config);
        builder2.set_received(ReceivedSummary::from_received(std::iter::empty()));
        builder2.set_success(&source2);
        let proof2 = builder2.build();

        // Verify hashes are different
        let hash1 = if let ProofOutcome::Success {
            source_payload_hash,
            ..
        } = &proof1.outcome
        {
            *source_payload_hash
        } else {
            panic!("Expected Success outcome");
        };
        let hash2 = if let ProofOutcome::Success {
            source_payload_hash,
            ..
        } = &proof2.outcome
        {
            *source_payload_hash
        } else {
            panic!("Expected Success outcome");
        };
        assert_ne!(
            hash1, hash2,
            "Different sources must produce different hashes"
        );

        // Verify verification fails when comparing proofs with different hashes
        let comparison_result = compare_proofs(&proof1, &proof2);
        assert!(
            comparison_result.is_err(),
            "Hash mismatch should cause verification failure"
        );

        // Verify the error specifically mentions outcome mismatch
        let err = comparison_result.unwrap_err();
        if let ReplayError::Mismatch { field, .. } = err {
            assert_eq!(
                field, "outcome",
                "Error should identify outcome field as mismatched"
            );
        } else {
            panic!("Expected Mismatch error, got: {:?}", err);
        }
    }

    #[test]
    fn proof_artifact_manifest_hash_binds_distribution_metadata() {
        let payload = deterministic_artifact_payload(512);
        let object_id = ObjectId::new_for_test(0xA11CE);

        let first = package_proof_artifact_for_distribution(&payload, 64, 8, 0x5150, object_id, 3)
            .expect("proof artifact should package");
        let second = package_proof_artifact_for_distribution(&payload, 64, 9, 0x5150, object_id, 3)
            .expect("proof artifact should package with different repair count");

        assert!(first.manifest.hash_is_valid());
        assert!(second.manifest.hash_is_valid());
        assert_eq!(
            first.manifest.source_payload_hash, second.manifest.source_payload_hash,
            "same artifact bytes keep the same source-payload hash"
        );
        assert_ne!(
            first.manifest.manifest_hash, second.manifest.manifest_hash,
            "manifest hash must bind repair-symbol distribution metadata"
        );
        assert_eq!(
            first.shards.len(),
            first
                .manifest
                .source_symbols
                .saturating_add(first.manifest.repair_symbols)
        );
    }

    #[test]
    fn proof_artifact_distribution_recovers_after_source_shard_loss() {
        let payload = deterministic_artifact_payload(1024);
        let distribution = package_proof_artifact_for_distribution(
            &payload,
            64,
            20,
            0xD157_71B7_u64,
            ObjectId::new_for_test(0xD151),
            7,
        )
        .expect("proof artifact should package");

        let dropped_source_esis = [1u32, 4, 9, 13];
        let partial_shards: Vec<_> = distribution
            .shards
            .iter()
            .filter(|shard| !(shard.is_source && dropped_source_esis.contains(&shard.esi)))
            .cloned()
            .collect();

        let recovery = recover_proof_artifact_from_shards(&distribution.manifest, &partial_shards)
            .expect("repair shards should recover the missing source shards");

        assert_eq!(recovery.payload, payload);
        assert_eq!(recovery.symbols_received, partial_shards.len());
        assert_eq!(
            recovery.overhead_symbols,
            partial_shards
                .len()
                .saturating_sub(distribution.manifest.source_symbols)
        );
        assert!(recovery.authenticated);
        assert_eq!(recovery.manifest_hash, distribution.manifest.manifest_hash);
    }

    #[test]
    fn proof_artifact_distribution_rejects_corrupted_shard_payload() {
        let payload = deterministic_artifact_payload(384);
        let distribution = package_proof_artifact_for_distribution(
            &payload,
            64,
            10,
            0x0BAD_5EED,
            ObjectId::new_for_test(0xC0DE),
            2,
        )
        .expect("proof artifact should package");

        let mut shards = distribution.shards.clone();
        shards[0].data[0] ^= 0xA5;

        let err = recover_proof_artifact_from_shards(&distribution.manifest, &shards)
            .expect_err("corrupted shard bytes must fail before decode");

        assert!(
            matches!(
                err,
                ProofArtifactDistributionError::ShardPayloadHashMismatch { esi: 0 }
            ),
            "unexpected corruption error: {err:?}"
        );
    }

    #[test]
    fn proof_artifact_distribution_rejects_false_manifest_parameters() {
        let payload = deterministic_artifact_payload(384);
        let distribution = package_proof_artifact_for_distribution(
            &payload,
            64,
            10,
            0xF015_EC0D,
            ObjectId::new_for_test(0xF015),
            4,
        )
        .expect("proof artifact should package");

        let mut forged_manifest = distribution.manifest.clone();
        forged_manifest.k_prime = forged_manifest.k_prime.saturating_add(1);
        forged_manifest.manifest_hash = forged_manifest.recompute_hash();
        assert!(
            forged_manifest.hash_is_valid(),
            "test must cover a self-consistent but semantically false manifest"
        );

        let err = recover_proof_artifact_from_shards(&forged_manifest, &distribution.shards)
            .expect_err("false RFC 6330 metadata must fail before decode");

        assert!(
            matches!(
                err,
                ProofArtifactDistributionError::ManifestParameterMismatch {
                    field: "k_prime",
                    ..
                }
            ),
            "unexpected manifest validation error: {err:?}"
        );
    }

    #[test]
    fn empty_source_payload_produces_valid_nonzero_hash() {
        // Verify that empty source produces a valid hash (not zero due to domain separator)
        let empty_source: Vec<Vec<u8>> = Vec::new();
        let hash = recovered_source_hash(&empty_source);
        assert_ne!(
            hash, 0,
            "Empty source should produce non-zero hash due to domain separator"
        );

        // Verify different empty-like sources produce different hashes
        let empty_symbol = vec![Vec::new()]; // One empty symbol
        let empty_symbol_hash = recovered_source_hash(&empty_symbol);
        assert_ne!(
            hash, empty_symbol_hash,
            "Different empty payloads should produce different hashes"
        );
    }

    // ============================================================================
    // ATP-N15: Comprehensive RaptorQ repair proof unit test harness
    // ============================================================================

    /// Generate deterministic test data for a given size.
    fn generate_atp_test_data(size: usize, offset: u8) -> Vec<u8> {
        (0..size)
            .map(|i| ((i + offset as usize) % 256) as u8)
            .collect()
    }

    /// Create test received symbols with deterministic ESIs.
    fn create_atp_test_received_symbols(k: usize, repair_count: usize) -> Vec<(u32, bool)> {
        let mut symbols = Vec::new();

        // Add source symbols
        for i in 0..k {
            symbols.push((i as u32, true));
        }

        // Add repair symbols starting from K
        for i in 0..repair_count {
            symbols.push(((k + i) as u32, false));
        }

        symbols
    }

    #[test]
    fn test_atp_received_summary_truncation_boundary() {
        let large_symbol_count = MAX_RECEIVED_SYMBOLS + 100;
        let symbols: Vec<(u32, bool)> = (0..large_symbol_count)
            .map(|i| (i as u32, i < large_symbol_count / 2))
            .collect();

        let summary = ReceivedSummary::from_received(symbols.into_iter());

        assert_eq!(summary.total, large_symbol_count);
        assert_eq!(summary.esis.len(), MAX_RECEIVED_SYMBOLS);
        assert!(summary.truncated);

        // Should keep the smallest ESIs due to sorting
        for i in 0..MAX_RECEIVED_SYMBOLS {
            assert_eq!(summary.esis[i], i as u32);
        }
    }

    #[test]
    fn test_atp_received_summary_duplicate_tracking() {
        let symbols = vec![
            (0, true),
            (1, true),
            (1, true), // Duplicate
            (2, false),
            (2, false), // Duplicate
        ];

        let summary = ReceivedSummary::from_received(symbols.into_iter());

        assert_eq!(summary.total, 5); // Includes duplicates in count
        assert_eq!(summary.source_count, 3); // 2 unique + 1 duplicate
        assert_eq!(summary.repair_count, 2); // 1 unique + 1 duplicate
        assert_eq!(summary.esis, vec![0, 1, 1, 2, 2]); // Preserves duplicates
    }

    #[test]
    fn test_atp_received_summary_esi_multiset_hash_stability() {
        let symbols1 = vec![(0, true), (1, true), (2, false)];
        let symbols2 = vec![(0, true), (1, true), (2, false)];
        let symbols3 = vec![(0, true), (2, false), (1, true)]; // Different order

        let summary1 = ReceivedSummary::from_received(symbols1.into_iter());
        let summary2 = ReceivedSummary::from_received(symbols2.into_iter());
        let summary3 = ReceivedSummary::from_received(symbols3.into_iter());

        // Same symbols should produce same hash
        assert_eq!(summary1.esi_multiset_hash, summary2.esi_multiset_hash);
        // Same symbols in different order should produce same hash
        assert_eq!(summary1.esi_multiset_hash, summary3.esi_multiset_hash);
    }

    #[test]
    fn test_atp_proof_hash_hex_roundtrip() {
        let test_bytes = [
            0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54,
            0x32, 0x10, 0x0f, 0x1e, 0x2d, 0x3c, 0x4b, 0x5a, 0x69, 0x78, 0x87, 0x96, 0xa5, 0xb4,
            0xc3, 0xd2, 0xe1, 0xf0,
        ];
        let hash = ProofHash(test_bytes);

        let hex = hash.to_hex();
        assert_eq!(hex.len(), 64);
        assert_eq!(&hex[..16], "0123456789abcdef");

        #[cfg(test)]
        {
            let recovered = ProofHash::from_hex(&hex).expect("should parse valid hex");
            assert_eq!(recovered, hash);
        }
    }

    #[cfg(test)]
    #[test]
    fn test_atp_proof_hash_hex_validation_boundary() {
        // Valid hex
        let valid_hex = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
        assert!(ProofHash::from_hex(valid_hex).is_some());

        // Wrong length
        assert!(ProofHash::from_hex("0123456789abcdef").is_none());
        assert!(ProofHash::from_hex(&format!("{valid_hex}00")).is_none());

        // Invalid hex characters
        assert!(
            ProofHash::from_hex("g123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
                .is_none()
        );
    }

    #[test]
    fn test_atp_decode_proof_content_hash_determinism() {
        let config = make_test_config();
        let symbols = create_atp_test_received_symbols(8, 2);
        let recovered_data = generate_atp_test_data(8 * config.symbol_size, 0);

        let mut builder1 = DecodeProof::builder(config.clone());
        builder1.set_received(ReceivedSummary::from_received(symbols.clone().into_iter()));
        builder1.set_success(std::slice::from_ref(&recovered_data));
        let proof1 = builder1.build();

        let mut builder2 = DecodeProof::builder(config);
        builder2.set_received(ReceivedSummary::from_received(symbols.into_iter()));
        builder2.set_success(&[recovered_data]);
        let proof2 = builder2.build();

        let hash1 = proof1.content_hash();
        let hash2 = proof2.content_hash();

        assert_eq!(hash1, hash2);
    }

    #[test]
    fn test_atp_decode_proof_content_hash_sensitivity() {
        let config = make_test_config();
        let symbols = create_atp_test_received_symbols(8, 2);
        let recovered_data1 = generate_atp_test_data(8 * config.symbol_size, 0);
        let recovered_data2 = generate_atp_test_data(8 * config.symbol_size, 1); // Different data

        let mut builder1 = DecodeProof::builder(config.clone());
        builder1.set_received(ReceivedSummary::from_received(symbols.clone().into_iter()));
        builder1.set_success(&[recovered_data1]);
        let proof1 = builder1.build();

        let mut builder2 = DecodeProof::builder(config);
        builder2.set_received(ReceivedSummary::from_received(symbols.into_iter()));
        builder2.set_success(&[recovered_data2]);
        let proof2 = builder2.build();

        let hash1 = proof1.content_hash();
        let hash2 = proof2.content_hash();

        assert_ne!(hash1, hash2);
    }

    #[test]
    fn test_atp_proof_artifact_manifest_hash_validation_cycle() {
        let test_object_id = ObjectId::new(0x1234, 0x5678);
        let manifest = ProofArtifactManifest {
            version: PROOF_ARTIFACT_DISTRIBUTION_SCHEMA_VERSION,
            object_id: test_object_id,
            sbn: 42,
            artifact_len: 1024,
            symbol_size: 64,
            source_symbols: 16,
            repair_symbols: 4,
            k_prime: 20,
            l: 24,
            seed: 0xdeadbeef,
            source_payload_hash: ProofHash([0; 32]),
            manifest_hash: ProofHash([0; 32]), // Will be wrong
        };

        // Hash should not validate with zero manifest_hash
        assert!(!manifest.hash_is_valid());

        // Recompute and verify
        let correct_hash = manifest.recompute_hash();
        let corrected_manifest = ProofArtifactManifest {
            manifest_hash: correct_hash,
            ..manifest
        };

        assert!(corrected_manifest.hash_is_valid());
    }

    #[test]
    fn test_atp_elimination_trace_pivot_truncation() {
        let config = make_test_config();
        let symbols = create_atp_test_received_symbols(8, 2);

        let mut builder = DecodeProof::builder(config);
        builder.set_received(ReceivedSummary::from_received(symbols.into_iter()));

        // Add many pivot events to test truncation
        for i in 0..(MAX_PIVOT_EVENTS + 50) {
            builder.elimination_mut().record_pivot(i % 100, i % 50); // Arbitrary col, row
        }

        builder.set_success(&[generate_atp_test_data(8 * 64, 0)]);
        let proof = builder.build();

        assert_eq!(proof.elimination.pivot_events.len(), MAX_PIVOT_EVENTS);
        assert!(proof.elimination.pivot_events_truncated);
    }

    #[test]
    fn test_atp_peeling_trace_bounds_checking() {
        let config = make_test_config();
        let symbols = create_atp_test_received_symbols(8, 0);

        let mut builder = DecodeProof::builder(config);
        builder.set_received(ReceivedSummary::from_received(symbols.into_iter()));

        // Add solved indices within bounds
        for i in 0..8 {
            builder.peeling_mut().record_solved(i);
        }

        builder.set_success(&[generate_atp_test_data(8 * 64, 0)]);
        let proof = builder.build();

        assert_eq!(proof.peeling.solved, 8);
        assert_eq!(proof.peeling.solved_indices.len(), 8);
        assert!(!proof.peeling.truncated);
    }

    #[test]
    fn proof_artifact_table_invariant_error_preserves_corruption_evidence() {
        let err = map_systematic_param_error(SystematicParamError::RfcTableInvariantViolation {
            invariant: "K' >= K",
            details: "K=10 K'=9 from RFC systematic index table".to_string(),
        });

        assert_eq!(
            err,
            ProofArtifactDistributionError::RfcTableInvariantViolation {
                invariant: "K' >= K",
                details: "K=10 K'=9 from RFC systematic index table".to_string(),
            }
        );
        assert!(
            !matches!(
                &err,
                ProofArtifactDistributionError::UnsupportedSourceBlock { .. }
            ),
            "table corruption must not be reported as an unsupported source block"
        );

        let display = err.to_string();
        assert!(display.contains("RFC 6330 table invariant violation"));
        assert!(display.contains("K' >= K"));
        assert!(display.contains("K=10 K'=9"));
        let old_sentinel_text = ["maximum supported is ", "0"].concat();
        assert!(!display.contains(&old_sentinel_text));
    }

    #[cfg(feature = "test-internals")]
    #[test]
    fn proof_artifact_table_invariant_error_serializes_as_explicit_corruption() {
        let err = ProofArtifactDistributionError::RfcTableInvariantViolation {
            invariant: "L = K' + S + H",
            details: "K'=20 S=4 H=2 L=21".to_string(),
        };

        let value =
            serde_json::to_value(&err).expect("distribution errors should serialize for proofs");

        assert_eq!(
            value,
            json!({
                "RfcTableInvariantViolation": {
                    "invariant": "L = K' + S + H",
                    "details": "K'=20 S=4 H=2 L=21",
                }
            })
        );
    }

    #[test]
    fn test_atp_proof_distribution_error_coverage() {
        let errors = vec![
            ProofArtifactDistributionError::EmptyArtifact,
            ProofArtifactDistributionError::InvalidSymbolSize,
            ProofArtifactDistributionError::UnsupportedSourceBlock {
                requested: 100000,
                max_supported: 56403,
            },
            ProofArtifactDistributionError::RfcTableInvariantViolation {
                invariant: "P = L - W",
                details: "L=8 W=13 P underflow".to_string(),
            },
            ProofArtifactDistributionError::EncoderUnavailable,
            ProofArtifactDistributionError::ManifestHashMismatch {
                expected: ProofHash([1; 32]),
                actual: ProofHash([2; 32]),
            },
            ProofArtifactDistributionError::ManifestParameterMismatch {
                field: "k_prime",
                expected: 20,
                actual: 24,
            },
            ProofArtifactDistributionError::ShardSizeMismatch {
                esi: 42,
                expected: 64,
                actual: 32,
            },
            ProofArtifactDistributionError::ShardAuthenticationFailed { esi: 99 },
        ];

        for error in errors {
            let display = format!("{error}");
            assert!(!display.is_empty());
            assert!(!display.contains("Debug")); // Should use Display, not Debug
        }
    }

    /// Integration test for ATP gates conformance validation.
    #[test]
    fn test_atp_raptorq_conformance_integration() {
        let config = make_test_config();
        let symbols = create_atp_test_received_symbols(16, 4);
        let recovered_data = generate_atp_test_data(16 * 64, 0);

        let mut builder = DecodeProof::builder(config.clone());
        builder.set_received(ReceivedSummary::from_received(symbols.into_iter()));
        builder.set_success(&[recovered_data]);

        let proof = builder.build();
        let hash = proof.content_hash();

        // Verify proof structure for ATP integration
        assert_eq!(proof.config.k, config.k);
        assert!(matches!(proof.outcome, ProofOutcome::Success { .. }));
        assert!(!hash.to_hex().is_empty());

        // Verify hash determinism for ATP replay
        let hash2 = proof.content_hash();
        assert_eq!(hash, hash2);
    }

    /// Hard-regime telemetry integration test for ATP gates.
    #[test]
    fn test_atp_hard_regime_telemetry_integration() {
        let config = make_test_config();
        let symbols = create_atp_test_received_symbols(12, 6); // High repair overhead

        let mut builder = DecodeProof::builder(config);
        builder.set_received(ReceivedSummary::from_received(symbols.into_iter()));
        builder.set_success(&[generate_atp_test_data(12 * 64, 0)]);
        let proof = builder.build();

        // Verify telemetry data is captured in proof
        assert_eq!(proof.received.repair_count, 6);
        assert!(proof.received.repair_count > proof.config.k / 2); // High overhead condition

        // Proof should be valid for ATP integration
        let hash = proof.content_hash();
        assert_eq!(hash.to_hex().len(), 64);
    }

    /// Boundary condition testing for ATP gate integration.
    #[test]
    fn test_atp_boundary_conditions_for_gates() {
        // Test edge cases that ATP gates need to handle

        // Zero source symbols (minimal config)
        let minimal_config = DecodeConfig {
            object_id: ObjectId::new(0x1234, 0x5678),
            sbn: 0,
            k: 1, // Minimal valid K
            s: 0,
            h: 0,
            l: 1,
            symbol_size: 64,
            seed: 0xdeadbeef,
        };

        let mut builder = DecodeProof::builder(minimal_config);
        builder.set_received(ReceivedSummary::from_received(vec![(0, true)].into_iter()));
        builder.set_success(&[generate_atp_test_data(64, 0)]);
        let proof = builder.build();

        assert!(matches!(proof.outcome, ProofOutcome::Success { .. }));

        // Large symbols (boundary testing)
        let large_config = make_test_config(); // Use existing test config
        let large_symbols = create_atp_test_received_symbols(large_config.k, large_config.k / 4);

        let mut large_builder = DecodeProof::builder(large_config.clone());
        large_builder.set_received(ReceivedSummary::from_received(large_symbols.into_iter()));
        large_builder.set_success(&[generate_atp_test_data(large_config.k * 64, 0)]);
        let large_proof = large_builder.build();

        assert!(matches!(large_proof.outcome, ProofOutcome::Success { .. }));
        assert_eq!(large_proof.config.k, large_config.k);
    }

    #[test]
    fn test_atp_proof_replay_consistency() {
        // Test that proofs maintain consistency across rebuilds for ATP replay
        let config = make_test_config();
        let symbols = create_atp_test_received_symbols(config.k, 2);
        let recovered = make_test_recovered(&config);

        let proof1 = {
            let mut builder = DecodeProof::builder(config.clone());
            builder.set_received(ReceivedSummary::from_received(symbols.clone().into_iter()));
            builder.peeling_mut().record_solved(0);
            builder.peeling_mut().record_solved(4);
            builder.elimination_mut().record_pivot(8, 3);
            builder.elimination_mut().record_pivot(9, 7);
            builder.set_success(&recovered);
            builder.build()
        };

        let proof2 = {
            let mut builder = DecodeProof::builder(config);
            builder.set_received(ReceivedSummary::from_received(symbols.into_iter()));
            builder.peeling_mut().record_solved(0);
            builder.peeling_mut().record_solved(4);
            builder.elimination_mut().record_pivot(8, 3);
            builder.elimination_mut().record_pivot(9, 7);
            builder.set_success(&recovered);
            builder.build()
        };

        // Proofs should be identical for ATP replay consistency
        assert_eq!(proof1.content_hash(), proof2.content_hash());
        assert_eq!(proof1.peeling, proof2.peeling);
        assert_eq!(proof1.elimination.pivots, proof2.elimination.pivots);
    }
}