bitwright 0.32.6

Hash-consed fixed-width bit-vector expressions: exact evaluation, bit-level facts, and verified simplification.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
//! Pass tests: soundness (exhaustive at small widths, sampled wide), canonical results on
//! masking fixtures, idempotence, caps, and the commit rule.

use crate::engine::tests::{equivalent, generator};
use crate::engine::{Budget, End, Engine, Hooks, Phase, Run, Strategy, Verify};
use crate::testutil::{Gen, Rng};
use crate::{
    Assumptions, BinOp, BitVec, CmpOpExt, Context, Expr, FnEnv, ParseOptions, UnOp, Width,
    fp::FpFormat,
};

/// An engine running `phases` with the default verification: the tests' own equivalence
/// checks are the oracle (strict verification would reject a wrong rewrite before any test
/// could see it).
fn engine(phases: Vec<Phase>) -> Engine {
    Engine::builder()
        .builtin()
        .strategy(Strategy::new("t", phases))
        .build()
        .unwrap()
}

/// A random expression in the linear fragment over a few atoms (symbols, and non-linear
/// compounds of them), with constants.
fn linear_expr(g: &mut Gen, cx: &mut Context, w: u16, depth: u32) -> Expr {
    let width = Width::new(w).unwrap();
    if depth == 0 || g.rng.chance(1, 4) {
        return match g.rng.below(5) {
            0 => {
                let v = g.constant(w);
                cx.constant(&v).unwrap()
            }
            1 => {
                let a = cx.symbol("a", width).unwrap();
                let b = cx.symbol("b", width).unwrap();
                cx.bin(BinOp::Mul, a, b).unwrap()
            }
            2 => {
                let a = cx.symbol("a", width).unwrap();
                let c = cx.symbol("c", width).unwrap();
                cx.bin(BinOp::And, a, c).unwrap()
            }
            k => cx.symbol(["a", "b", "c"][k as usize - 2], width).unwrap(),
        };
    }
    let d = depth - 1;
    match g.rng.below(8) {
        0 | 1 => {
            let (x, y) = (linear_expr(g, cx, w, d), linear_expr(g, cx, w, d));
            cx.bin(BinOp::Add, x, y).unwrap()
        }
        2 => {
            let (x, y) = (linear_expr(g, cx, w, d), linear_expr(g, cx, w, d));
            cx.bin(BinOp::Sub, x, y).unwrap()
        }
        3 => {
            let x = linear_expr(g, cx, w, d);
            cx.un(
                if g.rng.chance(1, 2) {
                    UnOp::Neg
                } else {
                    UnOp::Not
                },
                x,
            )
            .unwrap()
        }
        4 => {
            let x = linear_expr(g, cx, w, d);
            let k = g.constant(w);
            let k = cx.constant(&k).unwrap();
            cx.bin(BinOp::Mul, x, k).unwrap()
        }
        5 => {
            let x = linear_expr(g, cx, w, d);
            let k = cx
                .constant_u64(width, g.rng.below(u64::from(w) + 1))
                .unwrap();
            cx.bin(BinOp::Shl, x, k).unwrap()
        }
        _ => {
            // Disjoint or/xor: masks with no common bit.
            let (x, y) = (linear_expr(g, cx, w, d), linear_expr(g, cx, w, d));
            let m = g.constant(w);
            let mk = cx.constant(&m).unwrap();
            let nm = cx.un(UnOp::Not, mk).unwrap();
            let (x, y) = (
                cx.bin(BinOp::And, x, mk).unwrap(),
                cx.bin(BinOp::And, y, nm).unwrap(),
            );
            cx.bin(
                if g.rng.chance(1, 2) {
                    BinOp::Or
                } else {
                    BinOp::Xor
                },
                x,
                y,
            )
            .unwrap()
        }
    }
}

#[test]
fn linear_is_sound_and_idempotent() {
    let eng = engine(vec![Phase::Linear]);
    let mut g = generator(0x11ea_4001);
    let mut rng = Rng(9);
    let mut changed = 0;
    for i in 0..1500 {
        let mut cx = Context::new();
        let w = if i % 10 == 0 {
            [16, 64, 128, 257, 512][g.rng.below(5) as usize]
        } else {
            1 + g.rng.below(6) as u16
        };
        let e = match i % 4 {
            0 => g.expr(&mut cx, w.min(6), 4).0,
            // `(P | Q) - Q` and `(P ^ Q) - Q` with overlapping operands: cancels only if the
            // or/xor is (wrongly) read as a sum.
            1 => {
                let p = linear_expr(&mut g, &mut cx, w, 3);
                let q = linear_expr(&mut g, &mut cx, w, 3);
                let op = if g.rng.chance(1, 2) {
                    BinOp::Or
                } else {
                    BinOp::Xor
                };
                let o = cx.bin(op, p, q).unwrap();
                cx.bin(BinOp::Sub, o, q).unwrap()
            }
            _ => linear_expr(&mut g, &mut cx, w, 5),
        };
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        let r = out.roots[0];
        assert_eq!(r.end, End::Completed);
        assert_eq!(out.stats.rejected, 0);
        assert!(
            equivalent(&mut cx, e, r.expr, &mut rng),
            "{} vs {}",
            cx.display(e),
            cx.display(r.expr)
        );
        changed += u64::from(r.changed);
        // Idempotent (recomputed, not answered from the memo).
        cx.memo.clear();
        let out2 = eng.run(&mut cx, &[r.expr], Run::default()).unwrap();
        assert!(
            !out2.roots[0].changed,
            "not idempotent: {} -> {}",
            cx.display(r.expr),
            cx.display(out2.roots[0].expr)
        );
    }
    assert!(changed > 200, "{changed}");
}

fn simplify(eng: &Engine, src: &str, w: u16) -> String {
    let mut cx = Context::new();
    let e = cx
        .parse(src, &ParseOptions::width(Width::new(w).unwrap()))
        .unwrap();
    let out = eng.simplify(&mut cx, e).unwrap();
    cx.display(out.expr).to_string()
}

#[test]
fn linear_cancels_additive_masking() {
    let eng = engine(vec![Phase::Linear]);
    for (src, want) in [
        ("(x + r) - r", "x"),
        ("(x - r) + (r + 5) - 5", "x"),
        ("~x + x", "-1:8"),
        ("x + x + x - 3 * x", "0:8"),
        ("(x << 2) - 4 * x", "0:8"),
        ("-(~x) - x", "1:8"),
        (
            "((x & 0xf0) | ((y * 2) & 0x0f)) - (y * 2 & 0x0f)",
            "x & -16",
        ),
        ("(x * y + 3) - (x * y + 1)", "2:8"),
        ("((x & 0xf0) ^ (y & 0x0f)) - (y & 0x0f)", "x & -16"),
        // Overlapping or/xor are not sums.
        ("(x | y) - y", "(x | y) - y"),
        ("(x ^ y) - y", "(x ^ y) - y"),
    ] {
        assert_eq!(simplify(&eng, src, 8), want, "{src}");
    }
    // Wide widths.
    assert_eq!(simplify(&eng, "(x + r) - r", 512), "x");
    assert_eq!(simplify(&eng, "~x + x + 1", 129), "0:129");
    // Equal-size rewrites are not made: a disjoint or stays an or.
    let mut cx = Context::new();
    let e = cx
        .parse("(x & 0xf0) | (y & 0x0f)", &ParseOptions::width(Width::W8))
        .unwrap();
    assert!(!eng.simplify(&mut cx, e).unwrap().changed);
}

#[test]
fn linear_preserves_wrap_and_fixed_residue_of_high_fields() {
    let eng = engine(vec![Phase::Linear, Phase::Bitwise, Phase::Linear]);
    // The low bits are fixed, but nonzero; dropping their correction would
    // turn both identities into an off-by-three result.
    for body in [
        "rotl(a, 6) * 12 - a * 3 + 9",
        "concat(7:6 - extract<2, 6>(a), 1:2) + a - 32",
    ] {
        assert_eq!(simplify(&eng, &format!("let a = x:8 * 4 + 3; {body}"), 8), "0:8");
    }
    // Neither unrestricted low bits nor an odd scaling coefficient admits
    // the cancellation. Check all assignments, not just the extremal words.
    for src in ["rotl(x:8, 6) * 12 - x * 3", "let a = x:8 & 252; rotl(a, 6) * 3 - a"] {
        let mut cx = Context::new();
        let e = cx.parse(src, &ParseOptions::width(Width::W8)).unwrap();
        let r = eng.simplify(&mut cx, e).unwrap().expr;
        let mut seen = [false; 256];
        for x in 0..=255 {
            let vals = cx.eval(&[e, r], &crate::FnEnv(|_: &crate::SymbolKey, w| BitVec::from_u64(w, x).ok())).unwrap();
            assert_eq!(vals[0], vals[1]);
            seen[vals[1].to_u64().unwrap() as usize] = true;
        }
        assert!(seen.iter().filter(|&&s| s).count() > 1);
    }
}

#[cfg(feature = "prove")]
#[test]
fn linear_cancels_sums_of_shifted_selects() {
    use crate::prove::{self, Config, Outcome};

    let eng = engine(vec![Phase::Linear]);
    for (last, identity) in [("y << 5", true), ("y << 4", false)] {
        // Independent guards: even the inner false arm must agree.
        let sum = format!(
            "let %0 = select(p:1, 0xffffcffe00000fd8, select(q:1, x:64, y:64)); \
             ~%0 * 223 + %0 * 259 \
             - select(p, 0xffff3ff800003f60, select(q, x << 2, y << 2)) \
             - select(p, 0xfff9ffc00001fb00, select(q, x << 5, {last}))"
        );
        if identity {
            check_to(&eng, &sum, "-223:64", 64);
        }
        let mut cx = Context::new();
        let p = cx
            .parse(&format!("{sum} == -223"), &ParseOptions::width(Width::W64))
            .unwrap();
        let cfg = Config::default().with_samples(0);
        match prove::valid(&mut cx, p, &cfg).unwrap() {
            Outcome::Proved(_) if identity => {}
            Outcome::Refuted(model) if !identity => {
                assert!(cx.eval(&[p], &model[..]).unwrap()[0].is_zero());
            }
            other => panic!("unexpected outcome: {other:?}"),
        }
    }
}

/// `n` symbols `s0`, `s1`, … of width `w`.
fn symbols(cx: &mut Context, n: usize, w: Width) -> Vec<Expr> {
    (0..n)
        .map(|i| cx.symbol(format!("s{i}").as_str(), w).unwrap())
        .collect()
}

/// `terms` joined by `op`, left to right.
fn chain(cx: &mut Context, op: BinOp, terms: &[Expr]) -> Expr {
    terms[1..]
        .iter()
        .fold(terms[0], |e, &t| cx.bin(op, e, t).unwrap())
}

/// The symbols under `e`, and its distinct nodes.
fn size(cx: &mut Context, e: Expr) -> (usize, u32) {
    let nodes = match cx.dag_size(&[e], u32::MAX).unwrap() {
        crate::Bounded::Exact(n) | crate::Bounded::AtLeast(n) => n,
    };
    (cx.symbols_in(&[e]).unwrap().len(), nodes)
}

/// A sum over a pass's cap on terms is an atom of the forms over it, and terms still cancel
/// through it: in the linear pass's sums and in the xor pass's xors, at a width whose forms
/// are kept as words and at one over 64 bits.
#[test]
fn terms_cancel_through_sums_over_the_cap() {
    for (phase, name, add, sub, bits) in [
        (Phase::Linear, "linear", BinOp::Add, BinOp::Sub, 16),
        (Phase::Xor, "xor", BinOp::Xor, BinOp::Xor, 16),
        (Phase::Linear, "linear", BinOp::Add, BinOp::Sub, 200),
        (Phase::Xor, "xor", BinOp::Xor, BinOp::Xor, 200),
    ] {
        let eng = engine(vec![phase]);
        let w = Width::new(bits).unwrap();
        let mut rng = Rng(1);
        // One of 74 terms: the other 73 are left, as one sum.
        let mut cx = Context::new();
        let s = symbols(&mut cx, 74, w);
        let all = chain(&mut cx, add, &s);
        let e = cx.bin(sub, all, s[0]).unwrap();
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        let r = out.roots[0].expr;
        assert!(out.stats.passes[name].atomized > 0);
        assert_eq!(size(&mut cx, r), (73, 73 + 72), "{name}");
        assert!(equivalent(&mut cx, e, r, &mut rng));
        cx.memo.clear();
        let again = eng.run(&mut cx, &[r], Run::default()).unwrap();
        assert!(!again.roots[0].changed, "{name}: not idempotent");
        // The same 70 terms in the other order: nothing is left.
        let mut cx = Context::new();
        let s = symbols(&mut cx, 70, w);
        let a = chain(&mut cx, add, &s);
        let back: Vec<Expr> = s.iter().rev().copied().collect();
        let b = chain(&mut cx, add, &back);
        let e = cx.bin(sub, a, b).unwrap();
        let zero = cx.constant_u64(w, 0).unwrap();
        assert_eq!(eng.simplify(&mut cx, e).unwrap().expr, zero, "{name}");
        // 100 of 200 terms, through three sums over the cap, one inside the other.
        let mut cx = Context::new();
        let s = symbols(&mut cx, 200, w);
        let a = chain(&mut cx, add, &s);
        let even: Vec<Expr> = s.iter().step_by(2).copied().collect();
        let b = chain(&mut cx, add, &even);
        let e = cx.bin(sub, a, b).unwrap();
        let r = eng.simplify(&mut cx, e).unwrap().expr;
        assert_eq!(size(&mut cx, r), (100, 100 + 99), "{name}");
        assert!(equivalent(&mut cx, e, r, &mut rng));
        // A sum with another use is not taken apart: its terms would be built a second time.
        let mut cx = Context::new();
        let s = symbols(&mut cx, 70, w);
        let a = chain(&mut cx, add, &s);
        let d = cx.bin(sub, a, s[0]).unwrap();
        let e = cx.bin(BinOp::Mul, d, a).unwrap();
        assert!(!eng.simplify(&mut cx, e).unwrap().changed, "{name}");
        // Over `MAX_WHOLE_TERMS` terms a sum keeps no whole form: it is an atom and no more.
        let mut cx = Context::new();
        let s = symbols(&mut cx, super::forms::MAX_WHOLE_TERMS + 76, w);
        let a = chain(&mut cx, add, &s);
        let e = cx.bin(sub, a, s[0]).unwrap();
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        assert!(!out.roots[0].changed, "{name}");
        assert_eq!(out.roots[0].end, End::Completed);
    }
}

/// Random sums and xors of hundreds of terms over a pool of symbols, built in pieces that
/// repeat terms (sums over the cap as operands of sums, scaled, masked, negated and
/// complemented): the results are equivalent, and a second run changes nothing.
#[test]
fn long_sums_are_sound_and_idempotent() {
    for (phase, linear) in [(Phase::Linear, true), (Phase::Xor, false)] {
        let eng = engine(vec![phase]);
        let mut g = generator(0x10_9500);
        let mut rng = Rng(5);
        let add = if linear { BinOp::Add } else { BinOp::Xor };
        let mut changed = 0;
        for i in 0..32 {
            let mut cx = Context::new();
            let bits = [8, 16, 64, 100][i % 4];
            let pool = symbols(
                &mut cx,
                40 + g.rng.below(200) as usize,
                Width::new(bits).unwrap(),
            );
            let mut e: Option<Expr> = None;
            for _ in 0..2 + g.rng.below(5) {
                let terms: Vec<Expr> = (0..1 + g.rng.below(150))
                    .map(|_| pool[g.rng.below(pool.len() as u64) as usize])
                    .collect();
                let mut piece = chain(&mut cx, add, &terms);
                match g.rng.below(4) {
                    0 => {
                        let k = g.constant(bits);
                        let k = cx.constant(&k).unwrap();
                        let op = if linear { BinOp::Mul } else { BinOp::And };
                        piece = cx.bin(op, piece, k).unwrap();
                    }
                    1 => {
                        let op = if linear { UnOp::Neg } else { UnOp::Not };
                        piece = cx.un(op, piece).unwrap();
                    }
                    _ => {}
                }
                e = Some(match e {
                    None => piece,
                    Some(e) if linear && g.rng.chance(1, 2) => {
                        cx.bin(BinOp::Sub, e, piece).unwrap()
                    }
                    Some(e) => cx.bin(add, e, piece).unwrap(),
                });
            }
            let e = e.unwrap();
            let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
            let r = out.roots[0];
            assert_eq!(r.end, End::Completed);
            assert_eq!(out.stats.rejected, 0);
            assert!(equivalent(&mut cx, e, r.expr, &mut rng), "case {i}");
            changed += u64::from(r.changed);
            cx.memo.clear();
            let again = eng.run(&mut cx, &[r.expr], Run::default()).unwrap();
            assert!(!again.roots[0].changed, "case {i}: not idempotent");
        }
        assert!(changed > 8, "{changed}");
    }
}

/// Whole forms are read only where terms meet: a long sum of distinct terms costs what it did,
/// 36 units a node (taking each sum over the cap apart at every node above it would be 16 more).
#[test]
fn a_long_sum_of_distinct_terms_costs_what_it_did() {
    for (phase, add) in [(Phase::Linear, BinOp::Add), (Phase::Xor, BinOp::Xor)] {
        let eng = engine(vec![phase]);
        let mut cx = Context::new();
        let s = symbols(&mut cx, 4000, Width::W32);
        let e = chain(&mut cx, add, &s);
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        assert!(!out.roots[0].changed);
        assert!(out.stats.pass_work < 40 * 4000, "{}", out.stats.pass_work);
    }
}

/// A chain of `·`, `&` or `|` is one node however it was ordered and parenthesized, also over
/// more operands than the bitwise tables read: two writings of one chain cancel.
#[test]
fn chains_are_written_one_way() {
    for strategy in [Strategy::standard(), Strategy::deobfuscate()] {
        let eng = Engine::builder()
            .builtin()
            .strategy(strategy)
            .build()
            .unwrap();
        for (op, minus) in [("&", "^"), ("|", "^"), ("*", "-")] {
            for n in [3, 7, 9, 40] {
                let v: Vec<String> = (0..n).map(|i| format!("v{i}")).collect();
                let join = |v: &[String]| v.join(&format!(" {op} "));
                let back: Vec<String> = v.iter().rev().cloned().collect();
                // `v0 op (v1 op (…))`.
                let nested = back[1..]
                    .iter()
                    .fold(back[0].clone(), |e, x| format!("{x} {op} ({e})"));
                for other in [join(&back), nested] {
                    let src = format!("({}) {minus} ({other})", join(&v));
                    assert_eq!(simplify(&eng, &src, 32), "0:32", "{op} of {n}");
                }
            }
        }
        // An operand of `&` or `|` counts once, and beside its complement it decides the chain.
        for (src, want) in [
            (
                "(a & b & c & d & e & f & g & a) ^ (g & f & e & d & c & b & a)",
                "0:32",
            ),
            ("a & b & c & d & e & f & g & ~d", "0:32"),
            ("a | b | c | d | e | f | g | ~d", "-1:32"),
        ] {
            assert_eq!(simplify(&eng, src, 32), want, "{src}");
        }
        // Over `MAX_CHAIN` operands a chain is as written above the ones put in order: the
        // result is still the chain's value, and stable.
        let mut rng = Rng(7);
        for op in [BinOp::And, BinOp::Or, BinOp::Mul] {
            let mut cx = Context::new();
            let s = symbols(&mut cx, super::MAX_CHAIN + 44, Width::W32);
            let back: Vec<Expr> = s.iter().rev().copied().collect();
            let e = chain(&mut cx, op, &back);
            let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
            let r = out.roots[0].expr;
            assert_eq!(out.roots[0].end, End::Completed);
            assert!(equivalent(&mut cx, e, r, &mut rng));
            cx.memo.clear();
            let again = eng.run(&mut cx, &[r], Run::default()).unwrap();
            assert!(!again.roots[0].changed, "{op:?}: not stable");
        }
    }
}

/// A node rebuilt over a rewrite below it will be used by every user of the node it replaced,
/// though the walk has rebuilt only some of them yet: a rewrite at one user that would leave
/// it to the others frees nothing. Here `y` is `p`; the pass that finds it then meets the chain
/// `x op (p op z)` over the rebuilt `p op z`, and putting that chain in order would build
/// `p op x` beside the `p op z` that `popcnt` keeps.
#[test]
fn a_rebuilt_node_counts_the_users_still_to_be_rebuilt() {
    for strategy in [Strategy::standard(), Strategy::deobfuscate()] {
        let eng = Engine::builder()
            .builtin()
            .strategy(strategy)
            .build()
            .unwrap();
        for op in ["&", "|"] {
            let mut cx = Context::new();
            let o = ParseOptions::width(Width::W32);
            let written =
                format!("let y = (p & q) | (p & ~q); let i = y {op} z; (x {op} i) ^ popcnt(i)");
            let reduced = format!("let i = p {op} z; (x {op} i) ^ popcnt(i)");
            let a = cx.parse(&written, &o).unwrap();
            let b = cx.parse(&reduced, &o).unwrap();
            let (_, nodes) = size(&mut cx, b);
            let got = eng.simplify(&mut cx, a).unwrap().expr;
            assert_eq!(size(&mut cx, got).1, nodes, "{op}: {}", cx.display(got));
        }
    }
}

/// Factoring reads a sum's terms and a term's factors from the top, and no more of them than
/// it may factor: a sum of 100 000 terms, and a product of as many factors under a sum, are
/// simplified on a stack of the size a thread gets by default. Read by recursion to the bottom
/// of the chain, each overflowed it.
#[test]
fn factoring_reads_long_sums_and_products_from_the_top() {
    let run = || {
        let eng = engine(vec![Phase::Linear]);
        for op in [BinOp::Add, BinOp::Mul] {
            let mut cx = Context::new();
            let s = symbols(&mut cx, 100_000, Width::W64);
            let mut e = chain(&mut cx, op, &s);
            if op == BinOp::Mul {
                let x = cx.symbol("x", Width::W64).unwrap();
                e = cx.bin(BinOp::Add, e, x).unwrap();
            }
            let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
            assert_eq!(out.roots[0].end, End::Completed, "{op:?}");
        }
    };
    std::thread::Builder::new()
        .stack_size(2 << 20)
        .spawn(run)
        .unwrap()
        .join()
        .unwrap();
}

/// Terms that read one condition are merged over the same spelling of it however the nodes
/// were built. A form's terms are in the order of their nodes, `Context::import` builds
/// operands right to left and the parser left to right; `zext(c) - zext(~c)` is
/// `2·zext(c) - 1` or `1 - 2·zext(~c)`, and over a `1` the sum already reads only the second
/// is smaller than it. Merged over the first term, the sum was left as written or not by the
/// order its nodes were built in, so a result changed again once imported.
#[test]
fn conditions_merge_whatever_the_order_of_construction() {
    for strategy in [Strategy::standard(), Strategy::deobfuscate()] {
        let eng = Engine::builder()
            .builtin()
            .strategy(strategy)
            .build()
            .unwrap();
        for src in [
            "let a = udiv(1, x); zext<8>(x <=s a) - zext<8>(a <s x)",
            "let a = udiv(1, x); zext<8>(a <s x) - zext<8>(x <=s a)",
            "let a = udiv(1, x); zext<8>(x <u a) - zext<8>(a <=u x)",
            "let a = udiv(1, x); zext<8>(x != a) - zext<8>(x == a)",
        ] {
            let mut parsed = Context::new();
            let e = parsed.parse(src, &ParseOptions::width(Width::W8)).unwrap();
            let mut imported = Context::new();
            let i = imported.import(&parsed, &[e]).unwrap()[0];
            let (e, i) = (
                eng.simplify(&mut parsed, e).unwrap().expr,
                eng.simplify(&mut imported, i).unwrap().expr,
            );
            assert_eq!(
                parsed.display(e).to_string(),
                imported.display(i).to_string(),
                "{src}"
            );
        }
    }
}

/// A pass tries several candidates at a node, and one declined only because a part of the node
/// has another user is declined for now: the node's result is not final, whichever candidate
/// it was. The linear pass tries a case split on a condition mask first; of these two inputs
/// it declined the split of `sext(b) + 2^31 ^ 2^31` and of `concat(a, 7) - (sext(c) << 2)`
/// while the sum had another user, then remembered the node as final by its last candidate
/// alone, and the other user went. So the result changed again in a context without that
/// memory.
#[test]
fn a_candidate_declined_for_sharing_is_decided_again() {
    // (A symbol's width at its first use: `b:1`.)
    let cases = [
        (
            32,
            "let %0 = sext<32>(b:1); let %1 = %0 + 0x80000000; \
             concat(trunc<17>(~(sext<32>(a:4) ^ sext<32>(extract<25, 1>(select((%1 ^ 0x80000000) \
             & (%1 ^ %0) <s 0, select(%0 <s 0, 0x80000000:32, 0x7fffffff), %1))))), \
             extract<7, 15>(zext<32>(b) * -1))",
        ),
        (
            7,
            "let %0 = sext<7>(a:3); let %1 = sext<7>(udiv(%0, 7) * %0 != %0 << 4) << 2; \
             let %2 = concat(a, 7:4); let %3 = %2 - %1; \
             let %4 = smulhi((concat(-1:4, a) << 4) - select((%1 ^ %2) & (%3 ^ %2) <s 0, \
             select(%2 <s 0, -64:7, 63), %3), concat(a, -1:4)); \
             zext<7>((%0 - %4 ^ %0) & (%4 ^ %0) <s 0)",
        ),
    ];
    for strategy in [
        Strategy::standard(),
        Strategy::decompile(),
        Strategy::deobfuscate(),
    ] {
        let eng = Engine::builder()
            .builtin()
            .strategy(strategy)
            .build()
            .unwrap();
        for (width, src) in cases {
            let mut cx = Context::new();
            let e = cx
                .parse(src, &ParseOptions::width(Width::new(width).unwrap()))
                .unwrap();
            let r = eng.simplify(&mut cx, e).unwrap().expr;
            let mut fresh = Context::new();
            let again = fresh.import(&cx, &[r]).unwrap()[0];
            let out = eng.run(&mut fresh, &[again], Run::default()).unwrap();
            assert!(
                !out.roots[0].changed,
                "{} became {}",
                cx.display(r),
                fresh.display(out.roots[0].expr)
            );
        }
    }
}

/// Under `(e << k) | s` nested thousands of times only the last levels reach the result: the
/// ones a shift has moved out of the word are dropped. The walk is operands first, and each of
/// those levels was simplified under the mask of the level above before the walk reached the
/// top, a window of levels rebuilt at every one (3000 levels: 6 s and 126 655 nodes built).
/// The node at the top is asked first, and the levels out of its reach are not walked.
#[test]
fn a_deep_chain_of_shifts_is_asked_at_its_top() {
    for strategy in [Strategy::standard(), Strategy::deobfuscate()] {
        let eng = Engine::builder()
            .builtin()
            .strategy(strategy)
            .build()
            .unwrap();
        let mut cx = Context::new();
        let n = 3000;
        let s = symbols(&mut cx, n, Width::W64);
        let mut e = s[0];
        for (i, &t) in s.iter().enumerate().skip(1) {
            let k = cx.constant_u64(Width::W64, 1 + i as u64 % 3).unwrap();
            let shifted = cx.bin(BinOp::Shl, e, k).unwrap();
            e = cx.bin(BinOp::Or, shifted, t).unwrap();
        }
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        let r = out.roots[0];
        assert_eq!(r.end, End::Completed);
        for k in 0..8u64 {
            let env = FnEnv(|key: &crate::SymbolKey, w| {
                let h = format!("{key:?}")
                    .bytes()
                    .fold(k, |h, c| (h ^ u64::from(c)).wrapping_mul(0x100_0000_01b3));
                Some(BitVec::wrapping_from_u64(w, h))
            });
            let v = cx.eval(&[e, r.expr], &env).unwrap();
            assert_eq!(v[0], v[1]);
        }
        // 64 bits at one to three a level: 33 levels at most are read.
        let (read, nodes) = size(&mut cx, r.expr);
        assert!(read <= 34 && nodes < 110, "{read} symbols, {nodes} nodes");
        let built = out.stats.new_nodes;
        assert!(built < 10_000, "{built} nodes built");
    }
}

#[test]
fn linear_charges_pass_work() {
    let eng = engine(vec![Phase::Linear]);
    let mut cx = Context::new();
    let e = cx
        .parse("(x + r) - r + (y + q) - q", &ParseOptions::width(Width::W8))
        .unwrap();
    let out = eng
        .run(
            &mut cx,
            &[e],
            Run {
                per_call: Budget {
                    pass_work: 3,
                    ..Budget::UNLIMITED
                },
                ..Run::default()
            },
        )
        .unwrap();
    assert!(matches!(out.roots[0].end, End::BudgetTerminated(_)));
    assert!(out.stats.passes["linear"].calls > 0);
}

#[test]
fn fact_fold_folds_known_nodes_and_obeys_the_hook() {
    let eng = engine(vec![Phase::FactFold]);
    assert_eq!(simplify(&eng, "(x | 0x0f) & 0x0f", 8), "15:8");
    assert_eq!(simplify(&eng, "popcnt(x & 1) <=u 1", 8), "1:1");
    struct NoFold;
    impl Hooks for NoFold {
        fn fold_known(&self, _: &Context, _: Expr) -> bool {
            false
        }
    }
    let mut cx = Context::new();
    let e = cx
        .parse("(x | 0x0f) & 0x0f", &ParseOptions::width(Width::W8))
        .unwrap();
    let out = eng
        .run(
            &mut cx,
            &[e],
            Run {
                hooks: Some(&NoFold),
                ..Run::default()
            },
        )
        .unwrap();
    assert!(!out.roots[0].changed);
}

#[test]
fn passes_and_rules_compose() {
    let eng = engine(vec![
        Phase::FactFold,
        Phase::Local {
            groups: vec![
                "core.bitwise".into(),
                "core.arith".into(),
                "core.compare".into(),
                "core.casts".into(),
                "core.shift".into(),
            ],
        },
        Phase::Linear,
    ]);
    let mut g = generator(0xc0_4905);
    let mut rng = Rng(3);
    for _ in 0..500 {
        let mut cx = Context::new();
        let w = 1 + g.rng.below(6) as u16;
        let e = linear_expr(&mut g, &mut cx, w, 5);
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        assert!(equivalent(&mut cx, e, out.roots[0].expr, &mut rng));
        assert_eq!(out.stats.rejected, 0);
    }
}

// ----- bitwise ------------------------------------------------------------------------------------

#[test]
fn bitwise_table_is_exact_and_sized() {
    use super::bitwise::{T, min_forms};
    let forms = &min_forms().forms;
    assert_eq!(forms.len(), 256);
    let mut max = 0;
    for (tt, (tmpl, size)) in forms.iter().enumerate() {
        let mut vals: Vec<u8> = Vec::new();
        let mut ops = 0;
        for t in tmpl {
            let v = match *t {
                T::Var(k) => [0xAAu8, 0xCC, 0xF0][k as usize],
                T::Zero => 0,
                T::Ones => 0xFF,
                T::Not(a) => {
                    ops += 1;
                    !vals[a as usize]
                }
                T::Bin(op, a, b) => {
                    ops += 1;
                    let (a, b) = (vals[a as usize], vals[b as usize]);
                    match op {
                        BinOp::And => a & b,
                        BinOp::Or => a | b,
                        BinOp::Xor => a ^ b,
                        _ => panic!("not a bitwise op"),
                    }
                }
            };
            vals.push(v);
        }
        assert_eq!(*vals.last().unwrap() as usize, tt, "template for {tt:#04x}");
        assert_eq!(ops, *size as usize, "{tt:#04x}");
        max = max.max(*size);
    }
    // Every 3-input function needs at most a handful of operators; a regression in the search
    // would show here.
    assert!(max <= 7, "{max}");
    // The single-operator functions are single operators.
    assert_eq!(forms[0xAA & 0xCC].1, 1);
    assert_eq!(forms[0xAA ^ 0xCC ^ 0xF0].1, 2);
}

/// A random bitwise expression over up to six atoms (so some regions are over the caps).
fn bitwise_expr(g: &mut Gen, cx: &mut Context, w: u16, depth: u32, atoms: u64) -> Expr {
    let width = Width::new(w).unwrap();
    if depth == 0 || g.rng.chance(1, 5) {
        return match g.rng.below(atoms + 2) {
            0 => cx.constant(&crate::BitVec::zero(width)).unwrap(),
            1 => cx.constant(&crate::BitVec::ones(width)).unwrap(),
            k => {
                let name = ["a", "b", "c", "d", "e", "f"][(k - 2) as usize];
                let s = cx.symbol(name, width).unwrap();
                if name == "d" {
                    // A non-bitwise atom.
                    let a = cx.symbol("a", width).unwrap();
                    cx.bin(BinOp::Add, s, a).unwrap()
                } else {
                    s
                }
            }
        };
    }
    let d = depth - 1;
    match g.rng.below(4) {
        0 => {
            let x = bitwise_expr(g, cx, w, d, atoms);
            cx.un(UnOp::Not, x).unwrap()
        }
        k => {
            let x = bitwise_expr(g, cx, w, d, atoms);
            let y = bitwise_expr(g, cx, w, d, atoms);
            cx.bin([BinOp::And, BinOp::Or, BinOp::Xor][(k - 1) as usize], x, y)
                .unwrap()
        }
    }
}

#[test]
fn bitwise_is_sound_minimal_and_idempotent() {
    let eng = engine(vec![Phase::Bitwise]);
    let mut g = generator(0xb175e);
    let mut rng = Rng(21);
    let mut changed = 0;
    for i in 0..1500 {
        let mut cx = Context::new();
        let w = if i % 10 == 0 {
            [8, 64, 129, 512][g.rng.below(4) as usize]
        } else {
            1 + g.rng.below(6) as u16
        };
        let atoms = 2 + g.rng.below(5);
        let e = bitwise_expr(&mut g, &mut cx, w, 5, atoms);
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        let r = out.roots[0];
        assert_eq!(out.stats.rejected, 0);
        assert!(
            equivalent(&mut cx, e, r.expr, &mut rng),
            "{} vs {}",
            cx.display(e),
            cx.display(r.expr)
        );
        changed += u64::from(r.changed);
        cx.memo.clear();
        let again = eng.run(&mut cx, &[r.expr], Run::default()).unwrap();
        assert!(
            !again.roots[0].changed,
            "not idempotent: {}",
            cx.display(r.expr)
        );
    }
    assert!(changed > 300, "{changed}");
}

#[test]
fn bitwise_fixtures() {
    let eng = engine(vec![Phase::Bitwise]);
    let same = |src: &str, want: &str| {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::W8);
        let e = cx.parse(src, &o).unwrap();
        let w = cx.parse(want, &o).unwrap();
        let out = eng.simplify(&mut cx, e).unwrap();
        assert_eq!(out.expr, w, "{src}: got {}", cx.display(out.expr));
    };
    same("(x & y) | (x & ~y)", "x");
    same("(x | y) & ~(x & y)", "x ^ y");
    same("~(~x & ~y)", "x | y");
    same("(x & ~y) | (~x & y)", "x ^ y");
    same("((x ^ y) & z) | (~(x ^ y) & z)", "z");
    same("(x & y & z) | (x & y & ~z)", "x & y");
    // A rewrite that drops an atom (`c` below) leaves no stale atom behind: the region above
    // has three atoms again and still reaches its minimum form.
    same("(d & ~b & ~a & ((b & c) | b)) | ~(d | a)", "~(a | d)");
    // Atoms may be arbitrary subterms.
    same("((p + q) & r) | ((p + q) & ~r)", "p + q");
}

#[test]
fn bitwise_reads_masking_over_six_atoms() {
    for eng in [engine(vec![Phase::Bitwise]), Engine::standard()] {
        let same = |src: &str, want: &str| {
            let mut cx = Context::new();
            let o = ParseOptions::width(Width::W32);
            let e = cx.parse(src, &o).unwrap();
            let w = cx.parse(want, &o).unwrap();
            let out = eng.simplify(&mut cx, e).unwrap();
            assert_eq!(out.expr, w, "{src}: got {}", cx.display(out.expr));
        };
        // Boolean shares of `a & b` (masks `m`, `n`), and of `a & b & c` (six atoms).
        let share = "((a ^ m) & (b ^ n)) ^ ((a ^ m) & n) ^ (m & (b ^ n)) ^ (m & n)";
        same(share, "a & b");
        same(&format!("{share} ^ (a & b)"), "0");
        same(
            "((a ^ m) & (b ^ n) & (c ^ o)) ^ ((a ^ m) & (b ^ n) & o) ^ ((a ^ m) & n & (c ^ o)) \
             ^ ((a ^ m) & n & o) ^ (m & (b ^ n) & (c ^ o)) ^ (m & (b ^ n) & o) \
             ^ (m & n & (c ^ o)) ^ (m & n & o)",
            "a & b & c",
        );
        // Atoms may be subterms or constants.
        same(
            "(((p + q) ^ m) & (b ^ n)) ^ (((p + q) ^ m) & n) ^ (m & (b ^ n)) ^ (m & n)",
            "(p + q) & b",
        );
        same(
            "((a ^ 0x5a) & (b ^ n)) ^ ((a ^ 0x5a) & n) ^ (0x5a & (b ^ n)) ^ (0x5a & n)",
            "a & b",
        );
        // Read flat, this depends on four atoms; the three-atom reading still writes it over
        // the region of four as one atom.
        same(
            "((a ^ b ^ c ^ d) & x) | ((a ^ b ^ c ^ d) & ~x)",
            "a ^ b ^ c ^ d",
        );
    }
}

#[test]
fn bitwise_reads_constants_by_classes_before_reading_wide() {
    let eng = engine(vec![Phase::Bitwise]);
    let o = ParseOptions::width(Width::W8);
    // Four atoms with the constants: by classes, the constants fold (`~(0x36 ^ 0xc5)` is 12);
    // read wide, they would be atoms of the form.
    let mut cx = Context::new();
    let e = cx.parse("~(b ^ 0x36 ^ 0xc5) ^ (b ^ d)", &o).unwrap();
    let want = cx.parse("d ^ 12", &o).unwrap();
    assert_eq!(eng.simplify(&mut cx, e).unwrap().expr, want);
    // Over more than six atoms the wide reading is the node itself: nothing to write.
    let mut cx = Context::new();
    let e = cx.parse("(a ^ b ^ c ^ d) | (e ^ f ^ g ^ h)", &o).unwrap();
    let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
    assert!(!out.roots[0].changed);
    assert_eq!(out.stats.passes["bitwise"].noop, 0);
}

#[test]
fn passes_are_independent_of_construction_order() {
    for phase in [
        Phase::Linear,
        Phase::Bitwise,
        Phase::Xor,
        Phase::Compares,
        Phase::LinearMba,
        Phase::FactFold,
    ] {
        let eng = engine(vec![phase.clone()]);
        let mut g = generator(0x0de7);
        for _ in 0..300 {
            // The same expression in two contexts whose symbols were created in opposite
            // orders (so node indices differ).
            let mut c1 = Context::new();
            let mut c2 = Context::new();
            let w = Width::new(1 + g.rng.below(8) as u16).unwrap();
            for name in ["a", "b", "c", "d"] {
                c1.symbol(name, w).unwrap();
            }
            for name in ["d", "c", "b", "a"] {
                c2.symbol(name, w).unwrap();
            }
            for name in ["x", "y", "z"] {
                c1.symbol(name, w).unwrap();
            }
            for name in ["z", "y", "x"] {
                c2.symbol(name, w).unwrap();
            }
            let e1 = match g.rng.below(5) {
                4 => linear_mba_expr(&mut g, &mut c1, w.bits()),
                0 => bitwise_expr(&mut g, &mut c1, w.bits(), 5, 3),
                1 => xor_expr(&mut g, &mut c1, w.bits(), 5),
                2 => {
                    let mode = g.rng.below(3);
                    compare_expr(&mut g, &mut c1, w.bits(), 4, mode)
                }
                _ => linear_expr(&mut g, &mut c1, w.bits(), 4),
            };
            let text = c1.display(e1).to_string();
            let o = ParseOptions::width(w);
            let e2 = c2.parse(&text, &o).unwrap();
            let r1 = eng.simplify(&mut c1, e1).unwrap().expr;
            let r2 = eng.simplify(&mut c2, e2).unwrap().expr;
            assert_eq!(
                c1.display(r1).to_string(),
                c2.display(r2).to_string(),
                "{phase:?} on {text}"
            );
        }
    }
}

// ----- xor ----------------------------------------------------------------------------------------

/// A random expression in the xor fragment over up to six atoms.
fn xor_expr(g: &mut Gen, cx: &mut Context, w: u16, depth: u32) -> Expr {
    let width = Width::new(w).unwrap();
    if depth == 0 || g.rng.chance(1, 5) {
        return match g.rng.below(8) {
            0 => {
                let v = g.constant(w);
                cx.constant(&v).unwrap()
            }
            1 => {
                let a = cx.symbol("a", width).unwrap();
                let b = cx.symbol("b", width).unwrap();
                cx.bin(BinOp::Add, a, b).unwrap()
            }
            k => cx
                .symbol(["a", "b", "c", "d", "e", "f"][(k - 2) as usize], width)
                .unwrap(),
        };
    }
    let d = depth - 1;
    match g.rng.below(6) {
        0 | 1 => {
            let (x, y) = (xor_expr(g, cx, w, d), xor_expr(g, cx, w, d));
            cx.bin(BinOp::Xor, x, y).unwrap()
        }
        2 => {
            let x = xor_expr(g, cx, w, d);
            cx.un(UnOp::Not, x).unwrap()
        }
        3 => {
            let x = xor_expr(g, cx, w, d);
            let m = g.constant(w);
            let m = cx.constant(&m).unwrap();
            cx.bin(
                if g.rng.chance(1, 2) {
                    BinOp::And
                } else {
                    BinOp::Or
                },
                x,
                m,
            )
            .unwrap()
        }
        4 => {
            // Disjoint or.
            let (x, y) = (xor_expr(g, cx, w, d), xor_expr(g, cx, w, d));
            let m = g.constant(w);
            let mk = cx.constant(&m).unwrap();
            let nm = cx.un(UnOp::Not, mk).unwrap();
            let (x, y) = (
                cx.bin(BinOp::And, x, mk).unwrap(),
                cx.bin(BinOp::And, y, nm).unwrap(),
            );
            cx.bin(BinOp::Or, x, y).unwrap()
        }
        _ => {
            // Overlapping or: never a sum.
            let (x, y) = (xor_expr(g, cx, w, d), xor_expr(g, cx, w, d));
            let o = cx.bin(BinOp::Or, x, y).unwrap();
            cx.bin(BinOp::Xor, o, y).unwrap()
        }
    }
}

#[test]
fn xor_is_sound_and_idempotent() {
    let eng = engine(vec![Phase::Xor]);
    let mut g = generator(0x0a0f);
    let mut rng = Rng(33);
    let mut changed = 0;
    for i in 0..1500 {
        let mut cx = Context::new();
        let w = if i % 10 == 0 {
            [8, 64, 128, 511][g.rng.below(4) as usize]
        } else {
            1 + g.rng.below(6) as u16
        };
        let e = if i % 4 == 0 {
            g.expr(&mut cx, w.min(6), 4).0
        } else {
            xor_expr(&mut g, &mut cx, w, 5)
        };
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        let r = out.roots[0];
        assert_eq!(out.stats.rejected, 0);
        assert!(
            equivalent(&mut cx, e, r.expr, &mut rng),
            "{} vs {}",
            cx.display(e),
            cx.display(r.expr)
        );
        changed += u64::from(r.changed);
        cx.memo.clear();
        let again = eng.run(&mut cx, &[r.expr], Run::default()).unwrap();
        assert!(
            !again.roots[0].changed,
            "not idempotent: {}",
            cx.display(r.expr)
        );
    }
    assert!(changed > 300, "{changed}");
}

/// Linear maps over GF(2) of one atom (xors of shifted, rotated, masked copies): the xor
/// pass's diagonal form is equivalent, exhaustively at small widths, and idempotent.
#[test]
fn gf2_forms_are_sound_and_idempotent() {
    let eng = engine(vec![Phase::Xor]);
    let mut g = generator(0x6f2f);
    let mut rng = Rng(51);
    let mut changed = 0;
    for i in 0..2000 {
        let mut cx = Context::new();
        let w = if i % 10 == 0 {
            64
        } else {
            2 + g.rng.below(7) as u16
        };
        let width = Width::new(w).unwrap();
        let x = cx.symbol("x", width).unwrap();
        let mut e = x;
        for _ in 0..1 + g.rng.below(5) {
            let k = BitVec::wrapping_from_u64(width, g.rng.below(u64::from(w)));
            let k = cx.constant(&k).unwrap();
            let t = match g.rng.below(5) {
                0 => cx.bin(BinOp::RotL, e, k).unwrap(),
                1 => cx.bin(BinOp::Shl, e, k).unwrap(),
                2 => cx.bin(BinOp::LShr, e, k).unwrap(),
                3 => {
                    let m = g.constant(w);
                    let m = cx.constant(&m).unwrap();
                    cx.bin(BinOp::And, e, m).unwrap()
                }
                _ => cx.un(UnOp::Not, e).unwrap(),
            };
            e = if g.rng.chance(1, 2) {
                cx.bin(BinOp::Xor, e, t).unwrap()
            } else {
                cx.bin(BinOp::Xor, t, x).unwrap()
            };
        }
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        let r = out.roots[0];
        assert_eq!(out.stats.rejected, 0);
        assert!(
            equivalent(&mut cx, e, r.expr, &mut rng),
            "W={w}: {} vs {}",
            cx.display(e),
            cx.display(r.expr)
        );
        changed += u64::from(r.changed);
        cx.memo.clear();
        let again = eng.run(&mut cx, &[r.expr], Run::default()).unwrap();
        assert!(
            !again.roots[0].changed,
            "not idempotent: {}",
            cx.display(r.expr)
        );
    }
    assert!(changed > 100, "{changed}");
}

#[test]
fn xor_cancels_boolean_masking() {
    let eng = engine(vec![Phase::Xor]);
    let same = |src: &str, want: &str| {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::W8);
        let e = cx.parse(src, &o).unwrap();
        let w = cx.parse(want, &o).unwrap();
        let out = eng.simplify(&mut cx, e).unwrap();
        assert_eq!(out.expr, w, "{src}: got {}", cx.display(out.expr));
    };
    same("(x ^ r) ^ r", "x");
    same("((x ^ k) & 0xf0) ^ (k & 0xf0)", "x & 0xf0");
    same("~(x ^ y) ^ y", "~x");
    same("(x | 0x0f) ^ 0x0f", "x & 0xf0");
    same("a ^ b ^ c ^ d ^ e ^ (b ^ d)", "a ^ c ^ e");
    same("((a ^ m) ^ (b ^ m)) ^ ((c + d) ^ (a ^ b))", "c + d");
    // Overlapping or is not an xor.
    same("(x | y) ^ y", "(x | y) ^ y");
    // `x | c` is read as `(x & ~c) ^ c` and comes back as itself, after a cancellation too;
    // a constant that misses every mask widens them (here x's mask to all-ones).
    same("x | 3", "x | 3");
    same("((x | 3) ^ v) ^ v", "x | 3");
    same("(((x | 3) ^ (y & 0xf0)) ^ v) ^ v", "(x ^ (y & 0xf3)) | 3");
    same("(x & 0xf0) ^ 1", "(x & 0xf0) ^ 1");
}

// ----- compares -----------------------------------------------------------------------------------

/// A random boolean combination of comparisons: of one operand pair, of one operand against
/// constants, or mixed.
fn compare_expr(g: &mut Gen, cx: &mut Context, w: u16, depth: u32, mode: u64) -> Expr {
    use crate::CmpOpExt;
    let width = Width::new(w).unwrap();
    if depth == 0 || g.rng.chance(1, 4) {
        let op = CmpOpExt::ALL[g.rng.below(10) as usize];
        let x = cx.symbol("x", width).unwrap();
        let other = match mode {
            0 => cx.symbol("y", width).unwrap(),
            1 => {
                let v = g.constant(w);
                cx.constant(&v).unwrap()
            }
            _ => match g.rng.below(3) {
                0 => cx.symbol("y", width).unwrap(),
                1 => cx.symbol("z", width).unwrap(),
                _ => {
                    let v = g.constant(w);
                    cx.constant(&v).unwrap()
                }
            },
        };
        let (a, b) = if g.rng.chance(1, 2) {
            (x, other)
        } else {
            (other, x)
        };
        if g.rng.chance(1, 12) {
            return cx
                .constant(&crate::BitVec::from_bool(g.rng.chance(1, 2)))
                .unwrap();
        }
        return cx.cmp(op, a, b).unwrap();
    }
    let d = depth - 1;
    match g.rng.below(4) {
        0 => {
            let x = compare_expr(g, cx, w, d, mode);
            cx.un(UnOp::Not, x).unwrap()
        }
        k => {
            let x = compare_expr(g, cx, w, d, mode);
            let y = compare_expr(g, cx, w, d, mode);
            cx.bin([BinOp::And, BinOp::Or, BinOp::Xor][(k - 1) as usize], x, y)
                .unwrap()
        }
    }
}

#[test]
fn compares_is_sound_and_idempotent() {
    let eng = engine(vec![Phase::Compares]);
    let mut g = generator(0xc0_3e);
    let mut rng = Rng(44);
    let mut changed = 0;
    for i in 0..3000 {
        let mut cx = Context::new();
        let w = if i % 10 == 0 {
            [8, 32, 64, 128, 512][g.rng.below(5) as usize]
        } else {
            1 + g.rng.below(6) as u16
        };
        let mode = g.rng.below(3);
        let e = compare_expr(&mut g, &mut cx, w, 4, mode);
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        let r = out.roots[0];
        assert_eq!(out.stats.rejected, 0);
        assert!(
            equivalent(&mut cx, e, r.expr, &mut rng),
            "W={w}: {} vs {}",
            cx.display(e),
            cx.display(r.expr)
        );
        changed += u64::from(r.changed);
        cx.memo.clear();
        let again = eng.run(&mut cx, &[r.expr], Run::default()).unwrap();
        assert!(
            !again.roots[0].changed,
            "not idempotent: {}",
            cx.display(r.expr)
        );
    }
    assert!(changed > 800, "{changed}");
}

#[test]
fn compares_fixtures() {
    let eng = engine(vec![Phase::Compares]);
    let same = |src: &str, want: &str, w: u16| {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::new(w).unwrap());
        let e = cx.parse(src, &o).unwrap();
        let want = cx.parse(want, &o).unwrap();
        let out = eng.simplify(&mut cx, e).unwrap();
        assert_eq!(out.expr, want, "{src}: got {}", cx.display(out.expr));
    };
    same("(x <u y) | (x == y)", "x <=u y", 8);
    same("(x <u y) & (y <u x)", "false", 8);
    same("~(x <=s y)", "y <s x", 8);
    same("(x <=u y) & (x >=u y)", "x == y", 8);
    same("(x <u 10) & (x >u 3)", "x - 4 <=u 5", 8);
    same("(x == 5) | (x == 6)", "x - 5 <=u 1", 8);
    same("(x <u 3) | (x >u 250)", "x - 251 <=u 7", 8);
    same("(x <s 0) ^ (x <u 128)", "true", 8);
    same("(x <u 200) & (x != 7) & (x <u 8)", "x <=u 6", 8);
    same("(x <s 5) & (x >=s -3)", "x + 3 <=u 7", 8);
    // At W = 1 only three relations occur: `<u` and `<s` never hold together.
    same("(x <u y) & (x <s y)", "false", 1);
    same("(x <u y) ^ (x <s y)", "x != y", 1);
    // Different pairs are left alone.
    same("(x <u y) & (x <u z)", "(x <u y) & (x <u z)", 8);
    // Comparisons of functions of x combine on x.
    same("(x - 4 <=u 5) | (x == 10)", "x - 4 <=u 6", 8);
    same("(x + 3 <u 5) & (x == 1)", "x == 1", 8);
    same("((x & 0x7f) == 0) & (x <u 0x80)", "x == 0", 8);
    same("(~x <u 10) | (x <u 10)", "x - 246 <=u 19", 8);
    // A truncation's values are the value's, a copy for each value of the bits dropped.
    same("(3 <u trunc<7>(x)) & (x <u 100)", "x - 4 <=u 95", 8);
    same("(trunc<7>(x) == 5) & (x <s 0)", "x == -123", 8);
    same("((x ^ 0x80) <u 0x10) | (x - 0x90 <u 0x10)", "x <=s 0x9f", 8);
    same("(-x == 3) & (x == 5)", "false", 8);
    same("((x | 0x80) == 0x85) ^ (x == 5)", "x == 0x85", 8);
    same("(x + 1 == 0) | (x + 1 == 1)", "x - 255 <=u 1", 8);
}

/// A term for the order reading: a symbol, a constant, an operation bounded by or monotone in
/// its operands, or a select between terms on a comparison (a minimum or maximum).
fn order_term(g: &mut Gen, cx: &mut Context, w: u16, depth: u32, signed: bool) -> Expr {
    let width = Width::new(w).unwrap();
    let sym = |cx: &mut Context, g: &mut Gen| {
        cx.symbol(["x", "y", "z"][g.rng.below(3) as usize], width)
            .unwrap()
    };
    if depth == 0 || g.rng.chance(1, 3) {
        return match g.rng.below(8) {
            0 => {
                let v = g.constant(w);
                cx.constant(&v).unwrap()
            }
            1 => {
                let (a, b) = (sym(cx, g), sym(cx, g));
                cx.bin(BinOp::And, a, b).unwrap()
            }
            2 => {
                let (a, b) = (sym(cx, g), sym(cx, g));
                cx.bin(BinOp::Or, a, b).unwrap()
            }
            3 => {
                let a = sym(cx, g);
                let k = BitVec::wrapping_from_u64(width, g.rng.below(3));
                let k = cx.constant(&k).unwrap();
                let op = if signed { BinOp::AShr } else { BinOp::LShr };
                cx.bin(op, a, k).unwrap()
            }
            4 => {
                let a = sym(cx, g);
                let k = BitVec::wrapping_from_u64(width, 1 + g.rng.below(3));
                let k = cx.constant(&k).unwrap();
                cx.bin(BinOp::UDiv, a, k).unwrap()
            }
            _ => sym(cx, g),
        };
    }
    let c = order_formula(g, cx, w, depth - 1, signed);
    let (a, b) = (
        order_term(g, cx, w, depth - 1, signed),
        order_term(g, cx, w, depth - 1, signed),
    );
    cx.select(c, a, b).unwrap()
}

/// A boolean combination of comparisons between order terms.
fn order_formula(g: &mut Gen, cx: &mut Context, w: u16, depth: u32, signed: bool) -> Expr {
    use crate::CmpOpExt;
    if depth == 0 || g.rng.chance(1, 3) {
        let ops: &[CmpOpExt] = if signed {
            &[
                CmpOpExt::Slt,
                CmpOpExt::Sle,
                CmpOpExt::Sgt,
                CmpOpExt::Sge,
                CmpOpExt::Eq,
                CmpOpExt::Ne,
            ]
        } else {
            &[
                CmpOpExt::Ult,
                CmpOpExt::Ule,
                CmpOpExt::Ugt,
                CmpOpExt::Uge,
                CmpOpExt::Eq,
                CmpOpExt::Ne,
            ]
        };
        let op = ops[g.rng.below(ops.len() as u64) as usize];
        let a = order_term(g, cx, w, depth.min(1), signed);
        let b = order_term(g, cx, w, depth.min(1), signed);
        return cx.cmp(op, a, b).unwrap();
    }
    let d = depth - 1;
    match g.rng.below(4) {
        0 => {
            let x = order_formula(g, cx, w, d, signed);
            cx.un(UnOp::Not, x).unwrap()
        }
        k => {
            let x = order_formula(g, cx, w, d, signed);
            let y = order_formula(g, cx, w, d, signed);
            cx.bin([BinOp::And, BinOp::Or, BinOp::Xor][(k - 1) as usize], x, y)
                .unwrap()
        }
    }
}

/// The order reading (in the compares pass) is sound: every result is equivalent,
/// exhaustively at small widths, and a second run changes nothing.
#[test]
fn order_is_sound_and_idempotent() {
    let eng = engine(vec![Phase::Compares]);
    let mut g = generator(0x0de5);
    let mut rng = Rng(46);
    let mut changed = 0;
    for i in 0..3000 {
        let mut cx = Context::new();
        let w = if i % 10 == 0 {
            [8, 32, 64][g.rng.below(3) as usize]
        } else {
            1 + g.rng.below(4) as u16
        };
        let signed = g.rng.chance(1, 3);
        let e = if g.rng.chance(1, 3) {
            order_term(&mut g, &mut cx, w, 3, signed)
        } else {
            order_formula(&mut g, &mut cx, w, 3, signed)
        };
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        let r = out.roots[0];
        assert_eq!(out.stats.rejected, 0);
        assert!(
            equivalent(&mut cx, e, r.expr, &mut rng),
            "W={w}: {} vs {}",
            cx.display(e),
            cx.display(r.expr)
        );
        changed += u64::from(r.changed);
        cx.memo.clear();
        let again = eng.run(&mut cx, &[r.expr], Run::default()).unwrap();
        assert!(
            !again.roots[0].changed,
            "not idempotent: {} became {}",
            cx.display(r.expr),
            cx.display(again.roots[0].expr)
        );
    }
    assert!(changed > 300, "{changed}");
}

#[test]
fn compares_handles_deep_boolean_chains() {
    let eng = engine(vec![Phase::Compares]);
    let mut cx = Context::new();
    let mut root = cx.symbol("p", Width::W1).unwrap();
    for k in 0..10_000 {
        let leaf = cx.symbol(format!("q{k}"), Width::W1).unwrap();
        let op = if k % 2 == 0 { BinOp::And } else { BinOp::Or };
        root = cx.bin(op, root, leaf).unwrap();
    }
    assert_eq!(cx.height(root).unwrap(), 10_000);
    // This formula has too many Boolean leaves for the order model. Declining it must be
    // bounded even when its entire left spine is visited before the first leaf is completed.
    assert_eq!(eng.simplify(&mut cx, root).unwrap().expr, root);
}

#[test]
fn order_fixtures() {
    let eng = engine(vec![Phase::Compares]);
    let same = |src: &str, want: &str| {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::W8);
        let e = cx.parse(src, &o).unwrap();
        let want = cx.parse(want, &o).unwrap();
        let out = eng.simplify(&mut cx, e).unwrap();
        assert_eq!(out.expr, want, "{src}: got {}", cx.display(out.expr));
    };
    // Laws of the order: transitivity, asymmetry, trichotomy, totality.
    same("(x <u y) & (y <u z) & (z <=u x)", "false");
    same("(x <=s y) & (y <=s z) & (z <s x)", "false");
    same("(x <u y) | (x == y) | (y <u x)", "true");
    same("~(x <u y) & ~(y <u x)", "x == y");
    same("(x <=u y) & (y <=u z) & (x <=u z)", "(x <=u y) & (y <=u z)");
    // Minimum and maximum, however they are written.
    same("select(x <u y, x, y) == select(y <u x, y, x)", "true");
    same("select(x <u y, x, y) <=u select(x <u y, y, x)", "true");
    same(
        "select(x <u select(x <u y, y, x), x, select(x <u y, y, x))",
        "x",
    );
    same(
        "select(x <u select(y <u z, y, z), x, select(y <u z, y, z)) \
         == select(select(x <u y, x, y) <u z, select(x <u y, x, y), z)",
        "true",
    );
    same("select(x <s y, x, y) == select(y <s x, y, x)", "true");
    // Bounded and monotone operations.
    same("(x & y) <=u x", "true");
    same("x <=u (x | y)", "true");
    same("~(x <=u y) | ((x >>u 2) <=u (y >>u 2))", "true");
    same("~(x <=u y) | (udiv(x, 3) <=u udiv(y, 3))", "true");
    same("~(x <=s y) | ((x >>s 2) <=s (y >>s 2))", "true");
    // Constants and ranges order atoms too.
    same("select(x <u 0xff, x, 0xff)", "x");
    same("((x & 0x0f) <u 0x20) & (y <=u x)", "y <=u x");
    // Other pairs stay.
    same("(x <u y) & (x <u z)", "(x <u y) & (x <u z)");
}

/// Comparisons of `x`, of functions of it that the pass sees through, and of extensions of a
/// narrower `v`, combined.
fn peeled_expr(g: &mut Gen, cx: &mut Context, w: u16, depth: u32) -> Expr {
    use crate::CmpOpExt;
    let width = Width::new(w).unwrap();
    if depth == 0 || g.rng.chance(1, 4) {
        let x = cx.symbol("x", width).unwrap();
        let k = g.constant(w);
        let k = cx.constant(&k).unwrap();
        let low = BitVec::wrapping_from_u64(width, (1 << g.rng.below(u64::from(w.min(63)))) - 1);
        let low = cx.constant(&low).unwrap();
        let smin = cx.constant(&BitVec::smin(width)).unwrap();
        let v = || Width::new(w - 1).unwrap();
        // A truncation of `x`, compared at its own width.
        if w > 1 && g.rng.chance(1, 6) {
            let j = 1 + g.rng.below(u64::from(w) - 1) as u16;
            let t = cx.trunc(x, Width::new(j).unwrap()).unwrap();
            let op = CmpOpExt::ALL[g.rng.below(10) as usize];
            let c = g.constant(j);
            let c = cx.constant(&c).unwrap();
            return if g.rng.chance(1, 2) {
                cx.cmp(op, t, c).unwrap()
            } else {
                cx.cmp(op, c, t).unwrap()
            };
        }
        let t = match g.rng.below(11) {
            0 => x,
            1 => cx.bin(BinOp::Add, x, k).unwrap(),
            2 => cx.bin(BinOp::Sub, k, x).unwrap(),
            3 => cx.un(UnOp::Neg, x).unwrap(),
            4 => cx.un(UnOp::Not, x).unwrap(),
            5 => cx.bin(BinOp::Xor, x, smin).unwrap(),
            6 => cx.bin(BinOp::And, x, low).unwrap(),
            7 => cx.bin(BinOp::Or, x, smin).unwrap(),
            8 if w > 1 => {
                let v = cx.symbol("v", v()).unwrap();
                cx.zext(v, width).unwrap()
            }
            9 if w > 1 => {
                let v = cx.symbol("v", v()).unwrap();
                cx.sext(v, width).unwrap()
            }
            _ => {
                let s = cx.bin(BinOp::Sub, x, k).unwrap();
                cx.bin(BinOp::And, s, low).unwrap()
            }
        };
        let op = CmpOpExt::ALL[g.rng.below(10) as usize];
        let c = g.constant(w);
        let c = cx.constant(&c).unwrap();
        return if g.rng.chance(1, 2) {
            cx.cmp(op, t, c).unwrap()
        } else {
            cx.cmp(op, c, t).unwrap()
        };
    }
    let d = depth - 1;
    match g.rng.below(4) {
        0 => {
            let x = peeled_expr(g, cx, w, d);
            cx.un(UnOp::Not, x).unwrap()
        }
        k => {
            let x = peeled_expr(g, cx, w, d);
            let y = peeled_expr(g, cx, w, d);
            cx.bin([BinOp::And, BinOp::Or, BinOp::Xor][(k - 1) as usize], x, y)
                .unwrap()
        }
    }
}

#[test]
fn compares_through_functions_is_sound_and_idempotent() {
    let eng = engine(vec![Phase::Compares]);
    let mut g = generator(0xc0_3f);
    let mut rng = Rng(45);
    let mut changed = 0;
    for i in 0..3000 {
        let mut cx = Context::new();
        let w = if i % 10 == 0 {
            [8, 32, 64, 128][g.rng.below(4) as usize]
        } else {
            2 + g.rng.below(6) as u16
        };
        let e = peeled_expr(&mut g, &mut cx, w, 3);
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        let r = out.roots[0];
        assert_eq!(out.stats.rejected, 0);
        assert!(
            equivalent(&mut cx, e, r.expr, &mut rng),
            "W={w}: {} vs {}",
            cx.display(e),
            cx.display(r.expr)
        );
        changed += u64::from(r.changed);
        cx.memo.clear();
        let again = eng.run(&mut cx, &[r.expr], Run::default()).unwrap();
        assert!(
            !again.roots[0].changed,
            "not idempotent: {} to {}",
            cx.display(r.expr),
            cx.display(again.roots[0].expr)
        );
    }
    assert!(changed > 600, "{changed}");
}

/// A random boolean combination of float comparisons (of `x`, `y` and constants), float tests
/// and integer comparisons of `x`, in format `f`.
fn float_compare_expr(g: &mut Gen, cx: &mut Context, f: FpFormat, depth: u32) -> Expr {
    use crate::fp::{FpCmpOp, FpTest};
    let w = f.width();
    if depth == 0 || g.rng.chance(1, 4) {
        let operand = |g: &mut Gen, cx: &mut Context| match g.rng.below(5) {
            0 | 1 => cx.symbol("x", w).unwrap(),
            2 => cx.symbol("y", w).unwrap(),
            _ => {
                let v = match g.rng.below(6) {
                    0 => f.zero(g.rng.chance(1, 2)),
                    1 => f.inf(g.rng.chance(1, 2)),
                    2 => f.nan(),
                    _ => g.constant(w.bits()),
                };
                cx.constant(&v).unwrap()
            }
        };
        return match g.rng.below(6) {
            0..=2 => {
                let op = [
                    FpCmpOp::Eq,
                    FpCmpOp::Lt,
                    FpCmpOp::Le,
                    FpCmpOp::Gt,
                    FpCmpOp::Ge,
                ][g.rng.below(5) as usize];
                let a = operand(g, cx);
                let b = operand(g, cx);
                cx.fp_cmp(f, op, a, b).unwrap()
            }
            3 | 4 => {
                let t = [
                    FpTest::Nan,
                    FpTest::Infinite,
                    FpTest::Zero,
                    FpTest::Subnormal,
                    FpTest::Normal,
                    FpTest::Negative,
                    FpTest::Positive,
                ][g.rng.below(7) as usize];
                let a = if g.rng.chance(3, 4) { "x" } else { "y" };
                let a = cx.symbol(a, w).unwrap();
                cx.fp_test(f, t, a).unwrap()
            }
            _ => {
                let op = CmpOpExt::ALL[g.rng.below(10) as usize];
                let x = cx.symbol("x", w).unwrap();
                let c = g.constant(w.bits());
                let c = cx.constant(&c).unwrap();
                cx.cmp(op, x, c).unwrap()
            }
        };
    }
    let d = depth - 1;
    match g.rng.below(4) {
        0 => {
            let x = float_compare_expr(g, cx, f, d);
            cx.un(UnOp::Not, x).unwrap()
        }
        k => {
            let x = float_compare_expr(g, cx, f, d);
            let y = float_compare_expr(g, cx, f, d);
            cx.bin([BinOp::And, BinOp::Or, BinOp::Xor][(k - 1) as usize], x, y)
                .unwrap()
        }
    }
}

#[test]
fn compares_of_floats_is_sound_and_idempotent() {
    compares_of_floats_is_sound_and_idempotent_cases(1500);
}

/// [`compares_of_floats_is_sound_and_idempotent`] over 4000 random cases.
#[test]
#[ignore = "heavy: run with --release -- --ignored"]
fn compares_of_floats_is_sound_and_idempotent_heavy() {
    compares_of_floats_is_sound_and_idempotent_cases(4000);
}

fn compares_of_floats_is_sound_and_idempotent_cases(cases: usize) {
    let eng = engine(vec![Phase::Compares]);
    let mut g = generator(0xf1_0a7);
    let mut rng = Rng(46);
    let formats = [
        (2, 2),
        (2, 3),
        (3, 2),
        (2, 4),
        (3, 3),
        (4, 2),
        (3, 4),
        (5, 2),
    ];
    let mut changed = 0;
    for i in 0..cases {
        let mut cx = Context::new();
        let f = if i % 10 == 0 {
            [FpFormat::F16, FpFormat::BF16, FpFormat::F32][g.rng.below(3) as usize]
        } else {
            let (eb, sb) = formats[g.rng.below(formats.len() as u64) as usize];
            FpFormat::new(eb, sb).unwrap()
        };
        let e = float_compare_expr(&mut g, &mut cx, f, 3);
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        let r = out.roots[0];
        assert_eq!(out.stats.rejected, 0);
        assert!(
            equivalent(&mut cx, e, r.expr, &mut rng),
            "{f:?}: {} vs {}",
            cx.display(e),
            cx.display(r.expr)
        );
        changed += u64::from(r.changed);
        cx.memo.clear();
        let again = eng.run(&mut cx, &[r.expr], Run::default()).unwrap();
        assert!(
            !again.roots[0].changed,
            "not idempotent: {} to {}",
            cx.display(r.expr),
            cx.display(again.roots[0].expr)
        );
    }
    assert!(changed > 800 * cases as u64 / 4000, "{changed}");
}

#[test]
fn compares_of_floats_fixtures() {
    let eng = engine(vec![Phase::Compares]);
    let same = |src: &str, want: &str| {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::W32);
        let e = cx.parse(src, &o).unwrap();
        let want = cx.parse(want, &o).unwrap();
        let out = eng.simplify(&mut cx, e).unwrap();
        assert_eq!(out.expr, want, "{src}: got {}", cx.display(out.expr));
    };
    // The classes and the signs cover every encoding; a zero is not normal.
    same(
        "fp.isnan.f32(x) | fp.isinf.f32(x) | fp.iszero.f32(x) | fp.isnormal.f32(x) \
         | fp.issubnormal.f32(x)",
        "true",
    );
    same(
        "fp.isnan.f32(x) | fp.isneg.f32(x) | fp.ispos.f32(x)",
        "true",
    );
    same("fp.iszero.f32(x) & fp.isnormal.f32(x)", "false");
    same(
        "fp.iszero.f32(x) | fp.issubnormal.f32(x)",
        "x & 0x7fffffff <=u 0x7fffff",
    );
    same("fp.isnan.f32(x) | fp.isneg.f32(x)", "0x7f800001 <=u x");
    // Two floats.
    same("fp.lt.f32(x, y) & fp.lt.f32(y, x)", "false");
    same("fp.lt.f32(x, y) | fp.eq.f32(x, y)", "fp.le.f32(x, y)");
    same("fp.le.f32(x, y) & fp.ge.f32(x, y)", "fp.eq.f32(x, y)");
    same("~fp.lt.f32(x, y) & ~fp.eq.f32(x, y)", "~fp.le.f32(x, y)");
    // x86's flags after `ucomiss x, y`: ZF, PF, CF are equal, unordered, less, each or
    // unordered; `ja`, `jae`, `jb`, `jbe` test them.
    let unordered = "(fp.isnan.f32(x) | fp.isnan.f32(y))";
    let cf = format!("(fp.lt.f32(x, y) | {unordered})");
    let zf = format!("(fp.eq.f32(x, y) | {unordered})");
    same(&format!("~{cf} & ~{zf}"), "fp.lt.f32(y, x)");
    same(&format!("~{cf}"), "fp.le.f32(y, x)");
    same(&cf, "~fp.le.f32(y, x)");
    same(&format!("{cf} | {zf}"), "~fp.lt.f32(y, x)");
    // A float against constants: 1.0 is 0x3f800000.
    same(
        "fp.lt.f32(x, 0x3f800000) | fp.eq.f32(x, 0x3f800000)",
        "fp.le.f32(x, 0x3f800000)",
    );
    same(
        "fp.isnan.f32(x) | fp.le.f32(x, 0x3f800000)",
        "~fp.lt.f32(0x3f800000, x)",
    );
    same(
        "fp.lt.f32(x, 0x3f800000) & ~fp.isnan.f32(x)",
        "fp.lt.f32(x, 0x3f800000)",
    );
    same(
        "fp.le.f32(x, 0x3f7fffff) & ~fp.isnan.f32(x)",
        "fp.lt.f32(x, 0x3f800000)",
    );
    same("fp.lt.f32(x, 0) | fp.iszero.f32(x)", "fp.le.f32(x, 0)");
    same("fp.lt.f32(0, x) | fp.iszero.f32(x)", "fp.le.f32(0, x)");
    same(
        "fp.lt.f32(x, 0x3f800000) & fp.lt.f32(0x40000000, x)",
        "false",
    );
    same("fp.isinf.f32(x) & fp.lt.f32(x, 0)", "x == 0xff800000");
}

// ----- casts --------------------------------------------------------------------------------------

#[test]
fn casts_is_sound_and_idempotent() {
    let eng = engine(vec![Phase::Casts]);
    let mut g = generator(0xca57);
    g.max_w = 8;
    let mut rng = Rng(55);
    let mut changed = 0;
    for _ in 0..2000 {
        let mut cx = Context::new();
        let wide = 2 + g.rng.below(7) as u16;
        let (x, _) = g.expr(&mut cx, wide, 4);
        // Extract some bits of it (the pass acts on extracts).
        let n = 1 + g.rng.below(u64::from(wide)) as u16;
        let lo = g.rng.below(u64::from(wide - n) + 1) as u16;
        let e = cx.extract(x, lo, Width::new(n).unwrap()).unwrap();
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        let r = out.roots[0];
        assert_eq!(out.stats.rejected, 0);
        assert!(
            equivalent(&mut cx, e, r.expr, &mut rng),
            "{} vs {}",
            cx.display(e),
            cx.display(r.expr)
        );
        changed += u64::from(r.changed);
        cx.memo.clear();
        let again = eng.run(&mut cx, &[r.expr], Run::default()).unwrap();
        assert!(
            !again.roots[0].changed,
            "not idempotent: {} -> {} (from {})",
            cx.display(r.expr),
            cx.display(again.roots[0].expr),
            cx.display(e)
        );
    }
    assert!(changed > 100, "{changed}");
}

#[test]
fn casts_fixtures() {
    let eng = engine(vec![Phase::Casts]);
    let same = |src: &str, want: &str| {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::W8);
        let e = cx.parse(src, &o).unwrap();
        let want = cx.parse(want, &o).unwrap();
        let out = eng.simplify(&mut cx, e).unwrap();
        assert_eq!(out.expr, want, "{src}: got {}", cx.display(out.expr));
    };
    same("trunc<8>(zext<16>(x) + zext<16>(y))", "x + y");
    same("extract<8, 8>(zext<16>(x) << 8)", "x");
    same("trunc<8>(sext<32>(x) * sext<32>(y))", "x * y");
    same("extract<4, 4>(concat(a, b))", "extract<4, 4>(b)");
    same("trunc<4>(x >>u 4)", "extract<4, 4>(x)");
    same("trunc<8>(~(zext<16>(x) ^ zext<16>(y)))", "~(x ^ y)");
    same("extract<8, 8>(zext<16>(x))", "0:8");
    // Opaque operands: narrowing would only add nodes.
    same("trunc<8>(a:16 + b:16)", "trunc<8>(a:16 + b:16)");
    // Equal size: no change (`~trunc<4>(x)` is no smaller).
    same("trunc<4>(~x)", "trunc<4>(~x)");
}

// ----- demanded -----------------------------------------------------------------------------------

#[test]
fn demanded_is_sound_and_idempotent() {
    let eng = engine(vec![Phase::Demanded]);
    let mut g = generator(0xde_3a);
    g.max_w = 8;
    let mut rng = Rng(66);
    let mut changed = 0;
    for i in 0..2500 {
        let mut cx = Context::new();
        let w = if i % 10 == 0 {
            [16, 64, 130][g.rng.below(3) as usize]
        } else {
            1 + g.rng.below(7) as u16
        };
        let width = Width::new(w).unwrap();
        let (x, _) = if i % 3 == 0 {
            (xor_expr(&mut g, &mut cx, w, 4), ())
        } else if i % 3 == 1 {
            (linear_expr(&mut g, &mut cx, w, 4), ())
        } else {
            let (e, _) = g.expr(&mut cx, w.min(8), 4);
            (e, ())
        };
        let width = cx.width(x).unwrap_or(width);
        // Observe it through some bits.
        let e = match g.rng.below(4) {
            0 => {
                let m = g.constant(width.bits());
                let m = cx.constant(&m).unwrap();
                cx.bin(BinOp::And, x, m).unwrap()
            }
            1 => {
                let m = g.constant(width.bits());
                let m = cx.constant(&m).unwrap();
                cx.bin(BinOp::Or, x, m).unwrap()
            }
            2 => {
                let k = cx
                    .constant_u64(width, g.rng.below(u64::from(width.bits())))
                    .unwrap();
                let op = if g.rng.chance(1, 2) {
                    BinOp::Shl
                } else {
                    BinOp::LShr
                };
                cx.bin(op, x, k).unwrap()
            }
            _ => {
                let n = 1 + g.rng.below(u64::from(width.bits())) as u16;
                let lo = g.rng.below(u64::from(width.bits() - n) + 1) as u16;
                cx.extract(x, lo, Width::new(n).unwrap()).unwrap()
            }
        };
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        let r = out.roots[0];
        assert_eq!(out.stats.rejected, 0);
        assert!(
            equivalent(&mut cx, e, r.expr, &mut rng),
            "{} vs {}",
            cx.display(e),
            cx.display(r.expr)
        );
        changed += u64::from(r.changed);
        cx.memo.clear();
        let again = eng.run(&mut cx, &[r.expr], Run::default()).unwrap();
        assert!(
            !again.roots[0].changed,
            "not idempotent: {} -> {} (from {})",
            cx.display(r.expr),
            cx.display(again.roots[0].expr),
            cx.display(e)
        );
    }
    assert!(changed > 200, "{changed}");
}

/// A polynomial over one or two symbols, with bitwise operations and constants: the
/// fragment residues read.
fn poly_expr(g: &mut Gen, cx: &mut Context, w: u16, depth: u32) -> Expr {
    let width = Width::new(w).unwrap();
    if depth == 0 || g.rng.chance(1, 4) {
        return match g.rng.below(4) {
            0 => {
                let v = g.constant(w);
                cx.constant(&v).unwrap()
            }
            1 => cx.symbol("y", width).unwrap(),
            _ => cx.symbol("x", width).unwrap(),
        };
    }
    let d = depth - 1;
    let ops = [
        BinOp::Mul,
        BinOp::Mul,
        BinOp::Add,
        BinOp::Sub,
        BinOp::And,
        BinOp::Or,
        BinOp::Xor,
    ];
    let op = ops[g.rng.below(ops.len() as u64) as usize];
    let (a, b) = (poly_expr(g, cx, w, d), poly_expr(g, cx, w, d));
    cx.bin(op, a, b).unwrap()
}

/// Residues: the low bits of polynomials (masked, compared with constants, shifted up) are
/// decided soundly, exhaustively at small widths.
#[test]
fn residues_are_sound_and_idempotent() {
    let eng = engine(vec![Phase::Demanded, Phase::Compares]);
    let mut g = generator(0x7e51);
    let mut rng = Rng(47);
    let mut changed = 0;
    for i in 0..2000 {
        let mut cx = Context::new();
        let w = if i % 10 == 0 {
            64
        } else {
            1 + g.rng.below(7) as u16
        };
        let width = Width::new(w).unwrap();
        let p = poly_expr(&mut g, &mut cx, w, 3);
        let e = match g.rng.below(3) {
            0 => {
                let m = BitVec::wrapping_from_u64(width, g.rng.below(16));
                let m = cx.constant(&m).unwrap();
                cx.bin(BinOp::And, p, m).unwrap()
            }
            1 => {
                let c = BitVec::wrapping_from_u64(width, g.rng.below(16));
                let c = cx.constant(&c).unwrap();
                cx.cmp(CmpOpExt::Eq, p, c).unwrap()
            }
            _ => {
                let s = cx.constant_u64(width, g.rng.below(u64::from(w))).unwrap();
                cx.bin(BinOp::Shl, p, s).unwrap()
            }
        };
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        let r = out.roots[0];
        assert_eq!(out.stats.rejected, 0);
        assert!(
            equivalent(&mut cx, e, r.expr, &mut rng),
            "W={w}: {} vs {}",
            cx.display(e),
            cx.display(r.expr)
        );
        changed += u64::from(r.changed);
        cx.memo.clear();
        let again = eng.run(&mut cx, &[r.expr], Run::default()).unwrap();
        assert!(
            !again.roots[0].changed,
            "not idempotent: {}",
            cx.display(r.expr)
        );
    }
    assert!(changed > 200, "{changed}");
}

/// Polynomial equality: comparisons of random polynomials (with each other, and with a
/// rearrangement) are decided soundly, exhaustively at small widths.
#[test]
fn polynomial_equality_is_sound() {
    let eng = engine(vec![Phase::Compares]);
    let mut g = generator(0x9017);
    let mut rng = Rng(50);
    let mut decided = 0;
    for i in 0..1500 {
        let mut cx = Context::new();
        let w = if i % 10 == 0 {
            64
        } else {
            1 + g.rng.below(6) as u16
        };
        let a = poly_expr(&mut g, &mut cx, w, 3);
        let b = if g.rng.chance(1, 2) {
            poly_expr(&mut g, &mut cx, w, 3)
        } else {
            // a·(1 + k) − a·k, which expands to a.
            let k = g.constant(w);
            let k = cx.constant(&k).unwrap();
            let one = cx.constant_u64(Width::new(w).unwrap(), 1).unwrap();
            let k1 = cx.bin(BinOp::Add, one, k).unwrap();
            let p = cx.bin(BinOp::Mul, a, k1).unwrap();
            let q = cx.bin(BinOp::Mul, a, k).unwrap();
            cx.bin(BinOp::Sub, p, q).unwrap()
        };
        let op = if g.rng.chance(1, 2) {
            CmpOpExt::Eq
        } else {
            CmpOpExt::Ne
        };
        let e = cx.cmp(op, a, b).unwrap();
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        let r = out.roots[0];
        assert!(
            equivalent(&mut cx, e, r.expr, &mut rng),
            "W={w}: {} vs {}",
            cx.display(e),
            cx.display(r.expr)
        );
        decided += u64::from(cx.const_val(cx.id(r.expr).unwrap()).is_some());
    }
    assert!(decided > 300, "{decided}");
}

#[test]
fn residue_fixtures() {
    let eng = Engine::standard();
    let same = |src: &str, want: &str| {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::W8);
        let e = cx.parse(src, &o).unwrap();
        let want = cx.parse(want, &o).unwrap();
        let out = eng.simplify(&mut cx, e).unwrap();
        assert_eq!(out.expr, want, "{src}: got {}", cx.display(out.expr));
    };
    same("(x * x) & 1", "x & 1");
    same("(x * x * x) & 1", "x & 1");
    same("(x * x + x) & 1", "0");
    same("(x * (x + 1) * (x + 2)) & 1", "0");
    same("((x | 1) * (x | 1)) & 7", "1");
    same("(((x | 1) - 1) * ((x | 1) + 1)) & 7", "0");
    same("((x * x) & 3) == 3", "false");
    same("((x * x) & 7) == 5", "false");
    same("x * x == 2", "false");
    same("(x * x) & 2", "0");
    same("(x * x + x) * 0x80", "0");
    same("(x * (x + 1) * ((x + 2) * (x + 3))) * 0x20", "0");
    // A product of two values has no such law.
    same("(x * y) & 1", "(x * y) & 1");
}

/// A term of width `w` for the predicate tests: squares and products of sums, multiples, masks,
/// shifts, remainders and quotients by small constants (unsigned and signed), and narrow values
/// extended.
fn predicate_term(g: &mut Gen, cx: &mut Context, w: u16, depth: u32) -> Expr {
    let width = Width::new(w).unwrap();
    if depth == 0 || g.rng.chance(1, 4) {
        return match g.rng.below(7) {
            0 => {
                let v = BitVec::wrapping_from_u64(width, g.rng.below(16));
                cx.constant(&v).unwrap()
            }
            1 => cx.symbol("y", width).unwrap(),
            2 if w > 2 => {
                let a = cx.symbol("a", Width::new(2).unwrap()).unwrap();
                cx.zext(a, width).unwrap()
            }
            3 if w > 2 => {
                let b = cx.symbol("b", Width::new(2).unwrap()).unwrap();
                cx.sext(b, width).unwrap()
            }
            _ => cx.symbol("x", width).unwrap(),
        };
    }
    let d = depth - 1;
    let small = |g: &mut Gen, cx: &mut Context, n: u64| {
        let v = BitVec::wrapping_from_u64(width, g.rng.below(n));
        cx.constant(&v).unwrap()
    };
    match g.rng.below(11) {
        0 | 1 => {
            let t = predicate_term(g, cx, w, d);
            cx.bin(BinOp::Mul, t, t).unwrap()
        }
        2 => {
            let (a, b) = (predicate_term(g, cx, w, d), predicate_term(g, cx, w, d));
            cx.bin(BinOp::Mul, a, b).unwrap()
        }
        3 => {
            let (a, b) = (predicate_term(g, cx, w, d), predicate_term(g, cx, w, d));
            cx.bin(BinOp::Add, a, b).unwrap()
        }
        4 => {
            let (a, b) = (predicate_term(g, cx, w, d), predicate_term(g, cx, w, d));
            cx.bin(BinOp::Sub, a, b).unwrap()
        }
        5 => {
            let (t, k) = (predicate_term(g, cx, w, d), small(g, cx, 16));
            cx.bin(BinOp::Mul, t, k).unwrap()
        }
        6 => {
            let t = predicate_term(g, cx, w, d);
            let m = cx
                .constant_u64(width, (1u64 << g.rng.below(u64::from(w) + 1)) - 1)
                .unwrap();
            cx.bin(BinOp::And, t, m).unwrap()
        }
        7 => {
            let (t, k) = (predicate_term(g, cx, w, d), small(g, cx, 4));
            cx.bin(BinOp::Or, t, k).unwrap()
        }
        8 => {
            let t = predicate_term(g, cx, w, d);
            let k = BitVec::wrapping_from_u64(width, 2 + g.rng.below(6));
            let k = cx.constant(&k).unwrap();
            let op = [
                BinOp::URem,
                BinOp::URem,
                BinOp::SRem,
                BinOp::SRem,
                BinOp::UDiv,
                BinOp::SDiv,
            ][g.rng.below(6) as usize];
            cx.bin(op, t, k).unwrap()
        }
        9 => {
            let t = predicate_term(g, cx, w, d);
            let s = cx.constant_u64(width, g.rng.below(u64::from(w))).unwrap();
            cx.bin(BinOp::Shl, t, s).unwrap()
        }
        _ => {
            let (c, a, b) = (
                predicate(g, cx, w, 0),
                predicate_term(g, cx, w, d),
                predicate_term(g, cx, w, d),
            );
            cx.select(c, a, b).unwrap()
        }
    }
}

/// A 1-bit predicate over [`predicate_term`]s: comparisons of two terms or of a term with a
/// constant, under `& | ^ ~`.
fn predicate(g: &mut Gen, cx: &mut Context, w: u16, depth: u32) -> Expr {
    if depth == 0 || g.rng.chance(1, 3) {
        let a = predicate_term(g, cx, w, 3);
        let b = if g.rng.chance(1, 3) {
            let v = BitVec::wrapping_from_u64(Width::new(w).unwrap(), g.rng.below(16));
            cx.constant(&v).unwrap()
        } else {
            predicate_term(g, cx, w, 3)
        };
        let op = [
            CmpOpExt::Eq,
            CmpOpExt::Ne,
            CmpOpExt::Eq,
            CmpOpExt::Ne,
            CmpOpExt::Ult,
            CmpOpExt::Slt,
        ][g.rng.below(6) as usize];
        return cx.cmp(op, a, b).unwrap();
    }
    let p = predicate(g, cx, w, depth - 1);
    if g.rng.chance(1, 4) {
        return cx.un(UnOp::Not, p).unwrap();
    }
    let q = predicate(g, cx, w, depth - 1);
    let op = [BinOp::And, BinOp::Or, BinOp::Xor][g.rng.below(3) as usize];
    cx.bin(op, p, q).unwrap()
}

/// Predicates decided by residues: random predicates over squares, products, masks and
/// remainders are folded only when constant (exhaustively checked), and many are.
#[test]
fn residue_predicates_are_sound() {
    residue_predicates_are_sound_cases(1000);
}

/// [`residue_predicates_are_sound`] over 3000 random cases.
#[test]
#[ignore = "heavy: run with --release -- --ignored"]
fn residue_predicates_are_sound_heavy() {
    residue_predicates_are_sound_cases(3000);
}

fn residue_predicates_are_sound_cases(cases: usize) {
    let eng = engine(vec![Phase::Compares]);
    let mut g = generator(0x0b5c_7e11);
    let mut rng = Rng(53);
    let mut decided = 0;
    for _ in 0..cases {
        let mut cx = Context::new();
        let w = 1 + g.rng.below(6) as u16;
        let p = predicate(&mut g, &mut cx, w, 2);
        let out = eng.run(&mut cx, &[p], Run::default()).unwrap();
        let r = out.roots[0];
        assert!(
            equivalent(&mut cx, p, r.expr, &mut rng),
            "W={w}: {} vs {}",
            cx.display(p),
            cx.display(r.expr)
        );
        decided += u64::from(r.changed && cx.const_val(cx.id(r.expr).unwrap()).is_some());
    }
    assert!(decided > 300 * cases as u64 / 3000, "{decided}");
}

/// The decision itself, on its own: a predicate it calls constant is constant (exhaustively
/// checked), and it finds many of the constant ones the generator makes (those without a
/// product of a value with itself, or with comparisons only full values decide, it leaves).
#[test]
fn residue_decision_is_sound() {
    let mut g = generator(0x5e4c_0de1);
    let mut rng = Rng(54);
    let (mut constant, mut decided) = (0, 0);
    for _ in 0..4000 {
        let mut cx = Context::new();
        let w = 1 + g.rng.below(6) as u16;
        let p = predicate(&mut g, &mut cx, w, 2);
        let id = cx.id(p).unwrap();
        let (got, _) = super::predicate::constant(&cx, super::predicate::Pred::Node(id));
        let t = cx.bool(true).unwrap();
        let f = cx.bool(false).unwrap();
        let truth = if equivalent(&mut cx, p, t, &mut rng) {
            Some(true)
        } else if equivalent(&mut cx, p, f, &mut rng) {
            Some(false)
        } else {
            None
        };
        if let Some(v) = got {
            assert_eq!(truth, Some(v), "W={w}: {} is not {v}", cx.display(p));
            decided += 1;
        }
        constant += u32::from(truth.is_some() && cx.const_val(id).is_none());
    }
    assert!(decided * 2 > constant, "{decided} of {constant}");
}

/// The opaque predicates of number theory, as obfuscators write them, fold at every width; the
/// ones that are not constant on machine integers (a product that wraps) stay.
#[test]
fn opaque_predicate_fixtures() {
    let eng = Engine::standard();
    for w in [8, 32, 64, 128] {
        for (src, want) in [
            ("x * x != 7 * y * y - 1", "1:1"),
            ("7 * y * y - 1 == x * x", "0:1"),
            ("x * x + 1 != 7 * y * y", "1:1"),
            ("(x * x ^ (7 * y * y - 1)) == 0", "0:1"),
            ("x * x + x == 2 * y + 1", "0:1"),
            ("x * (x + 1) == 2 * y + 1", "0:1"),
            ("x * x == 8 * y + 5", "0:1"),
            ("x * x + y * y == 4 * z + 3", "0:1"),
            ("x * x - y * y != 4 * z + 2", "1:1"),
            ("x * x + y * y + z * z != 7", "1:1"),
            ("x * x * x != 4 * y + 2", "1:1"),
            ("(x * x | 1) != 7 * y * y - 1", "1:1"),
            ("((x & 1) == 0) | ((x * x & 7) == 1)", "1:1"),
            ("(urem(x * (x - 1), 2) == 0) | (y <s 10)", "1:1"),
            ("(x * x == 7 * y * y - 1) | (x == 3)", "x == 3"),
            ("(x * x != 7 * y * y - 1) & (x == 3)", "x == 3"),
            // Disguised: the sum `x·x + t` and the difference `x·x − (7·y·y − 1)` as MBA.
            ("(x * x ^ t) + 2 * (x * x & t) == 7 * y * y - 1 + t", "0:1"),
            (
                "((x * x | (7 * y * y - 1)) - (x * x & (7 * y * y - 1))) != 0",
                "1:1",
            ),
            ("select(x * x != 7 * y * y - 1, z + 1, z * 3)", "z + 1"),
            // Not constant: 2 is a difference of these modulo every power of two.
            ("x * x != 7 * y * y + 2", "y * 7 * y + 2 != x * x"),
        ] {
            assert_eq!(simplify(&eng, src, w), want, "{src} at {w} bits");
        }
    }
    // Signed remainders keep the low bits of their dividend; over values that do not wrap
    // (zero- or sign-extended, unsigned or signed), residues modulo other numbers decide: no
    // square is 3 modulo 7 or 2 modulo 3, `x² + 1` is never a multiple of 3, `x³ − x` always
    // one of 6, a square is not negative. `None`: not a constant (a product that wraps).
    for (src, want) in [
        ("srem(x * (x + 1), 2) == 0", Some("1:1")),
        ("srem(x * x, 4) != 2", Some("1:1")),
        ("srem((2 * x + 1) * (2 * x + 1), 8) != 3", Some("1:1")),
        ("urem(zext<32>(a:8) * zext<32>(a:8), 7) != 3", Some("1:1")),
        (
            "urem(zext<64>(c:16) * zext<64>(c:16) + 1, 3) != 0",
            Some("1:1"),
        ),
        ("urem(zext<64>(d:32) * zext<64>(d:32), 7) != 3", Some("1:1")),
        ("urem(zext<64>(d:32) * zext<64>(d:32), 3) == 2", Some("0:1")),
        (
            "urem(zext<64>(d:32) * (zext<64>(d:32) + 1), 6) != 1",
            Some("1:1"),
        ),
        // A divisibility test: `d² + 1` is never a multiple of 3.
        (
            "udiv(zext<64>(d:32) * zext<64>(d:32) + 1, 3) * 3 == zext<64>(d:32) * zext<64>(d:32) + 1",
            Some("0:1"),
        ),
        (
            "zext<64>(d:32) * zext<64>(d:32) + 1 != 3 * zext<64>(e:32)",
            Some("1:1"),
        ),
        (
            "urem(zext<32>(a:8) * (zext<32>(a:8) + 1) * (zext<32>(a:8) + 2), 3) == 0",
            Some("1:1"),
        ),
        (
            "srem(sext<64>(c:16) * sext<64>(c:16) + 1, 3) != 0",
            Some("1:1"),
        ),
        (
            "srem(sext<64>(c:16) * sext<64>(c:16) * sext<64>(c:16) - sext<64>(c:16), 6) == 0",
            Some("1:1"),
        ),
        ("sext<32>(a:8) * sext<32>(a:8) >=s 0", Some("1:1")),
        ("sext<64>(d:32) * sext<64>(d:32) >=s 0", Some("1:1")),
        ("x * x >=s 0", None),
        // Wraps: x = 7 at 8 bits, 336 mod 256 = 80.
        ("urem(x * x * x - x, 3) == 0", None),
        ("urem(x * x, 7) != 3", None),
    ] {
        let mut cx = Context::new();
        let e = cx.parse(src, &ParseOptions::width(Width::W8)).unwrap();
        let out = eng.simplify(&mut cx, e).unwrap();
        let got = cx.display(out.expr).to_string();
        match want {
            Some(want) => assert_eq!(got, want, "{src}"),
            None => assert!(
                cx.const_val(cx.id(out.expr).unwrap()).is_none(),
                "{src}: {got}"
            ),
        }
    }
}

/// A predicate over one value that only its low bits decide is recovered as the test of those
/// bits: parity and alignment tests in disguise.
#[test]
fn residue_tests_are_recovered() {
    let eng = Engine::standard();
    for w in [8, 32, 64] {
        for (src, want) in [
            ("((x * x) & 7) == 1", "trunc<1>(x)"),
            ("((x * x) & 7) != 1", "~trunc<1>(x)"),
            ("((x * x) & 3) == 0", "~trunc<1>(x)"),
            ("((x * x) & 15) == 0", "trunc<2>(x) == 0"),
            ("((x * x * x) & 7) == 1", "trunc<3>(x) == 1"),
            // 1 or 2 modulo 4.
            ("((x * (x + 1)) & 3) == 2", "trunc<2>(x) - 1 <=u 1"),
        ] {
            assert_eq!(simplify(&eng, src, w), want, "{src} at {w} bits");
        }
    }
}

/// `Context::prove` answers what residues decide, about expressions it does not build: the
/// equality, the order, the test against zero and the bit.
#[test]
fn prove_decides_by_residues() {
    use crate::{Query, Truth};
    for w in [8, 64] {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::new(w).unwrap());
        let sq = cx.parse("x * x", &o).unwrap();
        let rhs = cx.parse("7 * y * y - 1", &o).unwrap();
        let d = cx.bin(BinOp::Sub, sq, rhs).unwrap();
        // `x·(x + 1)` is even: bit 0 is 0.
        let e = cx.parse("x * (x + 1) + y * y * 4", &o).unwrap();
        let open = cx.parse("x * x != 7 * y * y + 2", &o).unwrap();
        let before = cx.len();
        assert_eq!(cx.prove(Query::Eq(sq, rhs)).unwrap(), Truth::False);
        assert_eq!(
            cx.prove(Query::Cmp(CmpOpExt::Ne, rhs, sq)).unwrap(),
            Truth::True
        );
        assert_eq!(cx.prove(Query::IsZero(d)).unwrap(), Truth::False);
        assert_eq!(cx.prove(Query::IsNonZero(d)).unwrap(), Truth::True);
        let bit = |value| Query::Bit { e, bit: 0, value };
        assert_eq!(cx.prove(bit(false)).unwrap(), Truth::True);
        assert_eq!(cx.prove(bit(true)).unwrap(), Truth::False);
        assert_eq!(cx.prove(Query::IsNonZero(open)).unwrap(), Truth::Unknown);
        // Nothing was built for the questions.
        assert_eq!(cx.len(), before);
    }
}

/// The residues compute with values of at most 128 bits, so an extension or a concatenation
/// wider than that is a leaf to them, whatever it reads: cut to 128 bits, `sext<257>(x)` of a
/// negative `x` was a large positive value, and `sext<257>(t·t) <=s sext<257>(x)` true.
#[test]
fn residues_read_no_value_over_128_bits() {
    use crate::{Query, Truth};
    let mut cx = Context::new();
    let o = ParseOptions::width(Width::new(3).unwrap());
    for (a, b) in [
        ("let t = sext<33>(x); sext<257>(t * t)", "sext<257>(x)"),
        ("let t = sext<33>(x); zext<129>(t * t)", "sext<129>(x)"),
        ("let t = sext<33>(x); sext<512>(t * t)", "zext<512>(x)"),
        (
            "let t = sext<100>(x); concat(t * t, t)",
            "concat(sext<100>(x), zext<100>(x))",
        ),
    ] {
        let (a, b) = (cx.parse(a, &o).unwrap(), cx.parse(b, &o).unwrap());
        // Every value of `x`: what a proof says must hold at all of them.
        let values: Vec<Vec<BitVec>> = (0..8u64)
            .map(|v| {
                let x = BitVec::wrapping_from_u64(Width::new(3).unwrap(), v);
                cx.eval(&[a, b], &FnEnv(|_: &crate::SymbolKey, _| Some(x)))
                    .unwrap()
            })
            .collect();
        for op in CmpOpExt::ALL {
            let truth = cx.prove(Query::Cmp(op, a, b)).unwrap();
            if truth == Truth::Unknown {
                continue;
            }
            for v in &values {
                assert_eq!(
                    BitVec::apply_cmp(op, &v[0], &v[1]).unwrap(),
                    truth == Truth::True,
                    "`{}` {op:?} `{}` proved {truth:?}",
                    cx.display(a),
                    cx.display(b)
                );
            }
        }
    }
}

/// Sign extension spelled with shifts or with xor and a subtraction, and bit tests through a
/// mask: equivalent at every shift and mask at small widths, and recognized.
#[test]
fn sign_extension_and_bit_test_idioms() {
    let eng = Engine::standard();
    let mut rng = Rng(48);
    for w in 2..=8u16 {
        let width = Width::new(w).unwrap();
        let o = ParseOptions::width(width);
        for c in 1..w {
            let mut cx = Context::new();
            let e = cx.parse(&format!("(x << {c}) >>s {c}"), &o).unwrap();
            let out = eng.simplify(&mut cx, e).unwrap();
            assert!(equivalent(&mut cx, e, out.expr, &mut rng));
            assert_eq!(
                cx.display(out.expr).to_string(),
                format!("sext<{w}>(trunc<{}>(x))", w - c)
            );
            let (m, h) = ((1u64 << c) - 1, 1u64 << (c - 1));
            let e = cx.parse(&format!("((x & {m}) ^ {h}) - {h}"), &o).unwrap();
            let out = eng.simplify(&mut cx, e).unwrap();
            assert!(
                equivalent(&mut cx, e, out.expr, &mut rng),
                "{}",
                cx.display(e)
            );
            if c > 1 {
                assert_eq!(
                    cx.display(out.expr).to_string(),
                    format!("sext<{w}>(trunc<{c}>(x))")
                );
            }
        }
        for k in 0..w {
            let mut cx = Context::new();
            for (src, set) in [
                (format!("(x & {}) != 0", 1u64 << k), true),
                (format!("(x & {}) == 0", 1u64 << k), false),
            ] {
                let e = cx.parse(&src, &o).unwrap();
                let out = eng.simplify(&mut cx, e).unwrap();
                assert!(equivalent(&mut cx, e, out.expr, &mut rng), "{src}");
                let s = cx.display(out.expr).to_string();
                if k + 1 < w {
                    let bit = if k == 0 {
                        "trunc<1>(x)".to_string()
                    } else {
                        format!("extract<{k}, 1>(x)")
                    };
                    assert_eq!(s, if set { bit } else { format!("~{bit}") }, "{src}");
                }
            }
        }
    }
}

/// `context::join` writes a select of two expressions with the choice further down: equal to
/// the select for random pairs, and one skeleton over the choice of what differs where the
/// sides are that skeleton.
#[cfg(feature = "mba")]
#[test]
fn joined_cases_are_the_select() {
    let mut g = generator(0x7015);
    let mut rng = Rng(77);
    let mut merged = 0;
    for i in 0..600 {
        let mut cx = Context::new();
        let w = [8u16, 16, 32, 64][i % 4];
        let c = cx.symbol("p", Width::W1).unwrap();
        let (t, _) = g.expr(&mut cx, w, 3);
        // The other side: an unrelated expression, or the first with one of its symbols
        // complemented (one skeleton, the choice at that symbol).
        let first = cx.symbols_in(&[t]).unwrap().first().map(|&s| {
            let (key, width) = (
                cx.symbol_key(s).unwrap().clone(),
                cx.symbol_width(s).unwrap(),
            );
            cx.symbol(key, width).unwrap()
        });
        let f = match (i % 3, first) {
            (0, _) | (_, None) => g.expr(&mut cx, w, 3).0,
            (_, Some(sym)) => {
                let not = cx.un(UnOp::Not, sym).unwrap();
                let mut repl: crate::hash::IdMap<u32, u32> = Default::default();
                repl.insert(cx.id(sym).unwrap(), cx.id(not).unwrap());
                let ti = cx.id(t).unwrap();
                let fi = cx.substitute_ids(&[ti], &repl).unwrap()[0];
                cx.handle(fi)
            }
        };
        let want = cx.select(c, t, f).unwrap();
        let (ci, ti, fi) = (cx.id(c).unwrap(), cx.id(t).unwrap(), cx.id(f).unwrap());
        let j = super::context::join(&mut cx, ci, ti, fi).unwrap();
        let j = cx.handle(j);
        assert!(
            equivalent(&mut cx, want, j, &mut rng),
            "W={w}: {} vs {}",
            cx.display(want),
            cx.display(j)
        );
        merged += u64::from(j != want);
    }
    assert!(merged > 200, "{merged}");
    // One skeleton over two constants: the choice is between the constants.
    let mut cx = Context::new();
    let c = cx.symbol("p", Width::W1).unwrap();
    let a = cx.symbol("a", Width::W64).unwrap();
    let b = cx.symbol("b", Width::W64).unwrap();
    let side = |cx: &mut Context, k: u64| {
        let k = cx.constant_u64(Width::W64, k).unwrap();
        let m = cx.bin(BinOp::Mul, a, k).unwrap();
        let r = cx.bin(BinOp::RotL, m, k).unwrap();
        cx.bin(BinOp::Add, r, b).unwrap()
    };
    let (t, f) = (side(&mut cx, 3), side(&mut cx, 5));
    let (ci, ti, fi) = (cx.id(c).unwrap(), cx.id(t).unwrap(), cx.id(f).unwrap());
    let j = super::context::join(&mut cx, ci, ti, fi).unwrap();
    // The sum is written once; a rotation keeps its amount, so two amounts stay two
    // rotations under the choice.
    assert_eq!(
        cx.display(cx.handle(j)).to_string(),
        "select(p, rotl(a * 3, 3), rotl(a * 5, 5)) + b"
    );
}

/// Case splits on condition masks: random linear and bitwise expressions reading `sext(c)`,
/// `-zext(c)` or a sign mask are equivalent after the split, exhaustively at small widths.
#[test]
fn case_splits_are_sound() {
    let eng = engine(vec![Phase::Linear]);
    let mut g = generator(0xca5e);
    let mut rng = Rng(49);
    let mut changed = 0;
    for i in 0..1500 {
        let mut cx = Context::new();
        let w = if i % 10 == 0 {
            32
        } else {
            2 + g.rng.below(4) as u16
        };
        let width = Width::new(w).unwrap();
        let c = cx.symbol("p", Width::W1).unwrap();
        let x = cx.symbol("x", width).unwrap();
        let m = match g.rng.below(3) {
            0 => cx.sext(c, width).unwrap(),
            1 => {
                let z = cx.zext(c, width).unwrap();
                cx.un(UnOp::Neg, z).unwrap()
            }
            _ => {
                let k = cx.constant_u64(width, u64::from(w) - 1).unwrap();
                cx.bin(BinOp::AShr, x, k).unwrap()
            }
        };
        let other = linear_expr(&mut g, &mut cx, w, 2);
        let ops = [BinOp::Add, BinOp::Sub, BinOp::Xor, BinOp::Or, BinOp::And];
        let op1 = ops[g.rng.below(5) as usize];
        let op2 = ops[g.rng.below(5) as usize];
        let inner = cx.bin(op1, other, m).unwrap();
        let e = cx.bin(op2, inner, m).unwrap();
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        let r = out.roots[0];
        assert_eq!(out.stats.rejected, 0);
        assert!(
            equivalent(&mut cx, e, r.expr, &mut rng),
            "W={w}: {} vs {}",
            cx.display(e),
            cx.display(r.expr)
        );
        changed += u64::from(r.changed);
    }
    assert!(changed > 100, "{changed}");
    // The conditional negation.
    let mut cx = Context::new();
    let e = cx
        .parse(
            "(x ^ sext<8>(c:1)) - sext<8>(c:1)",
            &ParseOptions::width(Width::W8),
        )
        .unwrap();
    let out = Engine::standard().simplify(&mut cx, e).unwrap();
    assert_eq!(cx.display(out.expr).to_string(), "select(c, -x, x)");
}

#[test]
fn demanded_fixtures() {
    let eng = engine(vec![Phase::Demanded]);
    let same = |src: &str, want: &str, w: u16| {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::new(w).unwrap());
        let e = cx.parse(src, &o).unwrap();
        let want = cx.parse(want, &o).unwrap();
        let out = eng.simplify(&mut cx, e).unwrap();
        assert_eq!(out.expr, want, "{src}: got {}", cx.display(out.expr));
    };
    same("(x | 0xff00) & 0x00ff", "x & 0xff", 16);
    same("((x & 0xf0) + y) & 0x0f", "y & 0x0f", 8);
    same("trunc<8>((x << 8) | y)", "trunc<8>(y)", 16);
    same("(x & 0xffff0000) >>u 16", "x >>u 16", 32);
    same("((x ^ 0xff) | 0xf0) & 0x0f", "~x & 15", 8);
    same(
        "(zext<16>(a:8) + (b << 8)) & 0xff",
        "zext<16>(a:8) & 0xff",
        16,
    );
    same(
        "concat(a:8 ^ b:8, l:8) & 0x00ff",
        "zext<16>(l:8) & 0xff",
        16,
    );
}

// ----- regressions from the M5 review --------------------------------------------------------------

struct VetoAll;

impl Hooks for VetoAll {
    fn admit(&self, _: &Context, _: Expr, _: Expr, _: crate::engine::By<'_>) -> bool {
        false
    }
}

#[test]
fn passes_obey_the_host_veto_and_report_events() {
    let o = ParseOptions::width(Width::W8);
    let mut cx = Context::new();
    let e = cx.parse("((x + r) - r) & ((y ^ k) ^ k)", &o).unwrap();
    let out = Engine::standard()
        .run(
            &mut cx,
            &[e],
            Run {
                hooks: Some(&VetoAll),
                ..Run::default()
            },
        )
        .unwrap();
    assert!(!out.roots[0].changed, "{}", cx.display(out.roots[0].expr));
    assert!(out.stats.hook_vetoes > 0);
    // Applied events name the pass, and agree with the counters.
    struct Passes(u64);
    impl crate::engine::Observer for Passes {
        fn event(&mut self, e: crate::engine::Event<'_>) {
            if let crate::engine::Event::Applied {
                by: crate::engine::By::Pass(_),
                ..
            } = e
            {
                self.0 += 1;
            }
        }
    }
    let mut obs = Passes(0);
    let mut cx = Context::new();
    let e = cx.parse("((x + r) - r) & ((y ^ k) ^ k)", &o).unwrap();
    let out = Engine::standard()
        .run(
            &mut cx,
            &[e],
            Run {
                observer: Some(&mut obs),
                ..Run::default()
            },
        )
        .unwrap();
    let changed: u64 = out.stats.passes.values().map(|c| c.changed).sum();
    assert!(changed > 0);
    assert_eq!(obs.0, changed);
    assert!(out.stats.pass_work > 0);
}

#[test]
fn demanded_obeys_fold_known() {
    struct NoFold;
    impl Hooks for NoFold {
        fn fold_known(&self, _: &Context, _: Expr) -> bool {
            false
        }
    }
    let eng = engine(vec![Phase::Demanded]);
    let mut cx = Context::new();
    let e = cx
        .parse("(x * 16) & 0x0f", &ParseOptions::width(Width::W8))
        .unwrap();
    let out = eng
        .run(
            &mut cx,
            &[e],
            Run {
                hooks: Some(&NoFold),
                ..Run::default()
            },
        )
        .unwrap();
    assert!(!out.roots[0].changed, "{}", cx.display(out.roots[0].expr));
    // Without the hook it folds.
    assert_eq!(simplify(&eng, "(x * 16) & 0x0f", 8), "0:8");
}

#[test]
fn demanded_keeps_each_leaf_once_across_a_phase_run() {
    // A ripple chain read through `<< 1`: every level shares its operands' leaves, and the
    // leaves a step keeps for the next ones held each level's operands' copies, so they
    // doubled at every level (2^48 at 48 bits, past any memory). Kept once each, the run
    // takes a moment.
    let mut src = String::from("let a0 = x; let b0 = y; ");
    for k in 0..48 {
        src += &format!(
            "let a{n} = a{k} ^ b{k}; let b{n} = (a{k} & b{k}) << 1; ",
            n = k + 1
        );
    }
    src += "a48 << 1";
    let eng = engine(vec![Phase::Demanded]);
    let mut cx = Context::new();
    let e = cx
        .parse(&src, &ParseOptions::width(Width::new(48).unwrap()))
        .unwrap();
    let before = dag(&mut cx, &[e]);
    let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
    let r = out.roots[0].expr;
    assert!(dag(&mut cx, &[r]) < before);
    assert!(equivalent(&mut cx, e, r, &mut Rng(48)));
}

#[test]
fn demanded_reduces_constants_to_their_residues() {
    let eng = engine(vec![Phase::Demanded]);
    let same = |src: &str, want: &str| {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::W64);
        let e = cx.parse(src, &o).unwrap();
        let want = cx.parse(want, &o).unwrap();
        let out = eng.simplify(&mut cx, e).unwrap();
        assert_eq!(out.expr, want, "{src}: got {}", cx.display(out.expr));
    };
    // `43 · 0xbe83 = 0x200001`: an inverse modulo 2^17 that is none at 64 bits.
    same("(x * 0x200001) & 0x1ffff", "x & 0x1ffff");
    same("((x * 0x200001) + y) & 0x1ffff", "(x + y) & 0x1ffff");
    same("trunc<17>(x * 0x200001)", "trunc<17>(x)");
    same("(x * 0x100000001) & 0xffffffff", "x & 0xffffffff");
    // The residue extended with its sign when that is the smaller: `0x3ffff` is −1 there.
    same("(x * 0x3ffff) & 0x1ffff", "-x & 0x1ffff");
    // Read at every bit, or already a residue: unchanged.
    same("x * 0x200001", "x * 0x200001");
    same("(x + 0x1ffff) & 0x1ffff", "(x + 0x1ffff) & 0x1ffff");
}

fn dag(cx: &mut Context, roots: &[Expr]) -> u32 {
    match cx.dag_size(roots, u32::MAX).unwrap() {
        crate::Bounded::Exact(n) | crate::Bounded::AtLeast(n) => n,
    }
}

#[test]
fn shared_regions_never_grow_the_dag() {
    for phases in [
        vec![Phase::Linear],
        vec![Phase::Xor],
        Strategy::standard().phases,
    ] {
        let eng = engine(phases);
        let w = Width::W32;
        for op in [BinOp::Add, BinOp::Xor] {
            // (S - a0) * S with S a 60-term chain: S survives, so re-emitting S - a0 grows.
            let mut cx = Context::new();
            let mut s = cx.symbol("a0", w).unwrap();
            let mut atoms = vec![s];
            for i in 1..60 {
                let a = cx.symbol(format!("a{i}").as_str(), w).unwrap();
                atoms.push(a);
                s = cx.bin(op, s, a).unwrap();
            }
            let inv = if op == BinOp::Add {
                BinOp::Sub
            } else {
                BinOp::Xor
            };
            let d = cx.bin(inv, s, atoms[0]).unwrap();
            let root = cx.bin(BinOp::Mul, d, s).unwrap();
            let before = dag(&mut cx, &[root]);
            let out = eng.run(&mut cx, &[root], Run::default()).unwrap();
            let after = dag(&mut cx, &[out.roots[0].expr]);
            assert!(after <= before, "{op:?}: {before} -> {after}");
            // Many roots sharing S.
            let roots: Vec<Expr> = atoms.iter().map(|&a| cx.bin(inv, s, a).unwrap()).collect();
            let before = dag(&mut cx, &roots);
            let out = eng.run(&mut cx, &roots, Run::default()).unwrap();
            let results: Vec<Expr> = out.roots.iter().map(|r| r.expr).collect();
            let after = dag(&mut cx, &results);
            assert!(after <= before, "{op:?}, 60 roots: {before} -> {after}");
        }
    }
}

#[test]
fn long_chains_build_no_garbage() {
    let eng = engine(vec![Phase::Linear, Phase::Xor]);
    let mut cx = Context::new();
    let w = Width::W32;
    // A chain in an order unrelated to the canonical one, so every re-emission would differ.
    let mut rng = Rng(77);
    let mut names: Vec<u32> = (0..20_000).collect();
    for i in (1..names.len()).rev() {
        let j = rng.below(i as u64 + 1) as usize;
        names.swap(i, j);
    }
    let mut e = cx.symbol(format!("s{}", names[0]).as_str(), w).unwrap();
    for &k in &names[1..] {
        let a = cx.symbol(format!("s{k}").as_str(), w).unwrap();
        e = cx.bin(BinOp::Add, e, a).unwrap();
    }
    let nodes = cx.len() as u64;
    let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
    assert_eq!(out.roots[0].end, End::Completed);
    assert!(
        out.stats.new_nodes < nodes / 10,
        "{} new nodes for {nodes}",
        out.stats.new_nodes
    );
}

// ----- linear MBA ---------------------------------------------------------------------------------

/// A random linear MBA: a random integer combination of random bitwise functions of up to four
/// atoms (symbols, or a non-linear compound), plus a constant.
fn linear_mba_expr(g: &mut Gen, cx: &mut Context, w: u16) -> Expr {
    let width = Width::new(w).unwrap();
    let natoms = 1 + g.rng.below(4);
    let atoms: Vec<Expr> = (0..natoms)
        .map(|k| {
            if k == 3 {
                let a = cx.symbol("a", width).unwrap();
                let b = cx.symbol("b", width).unwrap();
                cx.bin(BinOp::Mul, a, b).unwrap()
            } else {
                cx.symbol(["a", "b", "c"][k as usize], width).unwrap()
            }
        })
        .collect();
    let terms = 1 + g.rng.below(4);
    let v = g.constant(w);
    let mut e = cx.constant(&v).unwrap();
    for _ in 0..terms {
        // A random bitwise function over the atoms.
        let mut f = atoms[g.rng.below(natoms) as usize];
        for _ in 0..g.rng.below(4) {
            let other = atoms[g.rng.below(natoms) as usize];
            f = match g.rng.below(4) {
                0 => cx.un(UnOp::Not, f).unwrap(),
                1 => cx.bin(BinOp::And, f, other).unwrap(),
                2 => cx.bin(BinOp::Or, f, other).unwrap(),
                _ => cx.bin(BinOp::Xor, f, other).unwrap(),
            };
        }
        // Sometimes under a non-uniform mask (then no longer a bitwise function of the atoms
        // alone: an atom of the fragment, and the pass must not read the mask as uniform).
        if g.rng.chance(1, 4) {
            let m = g.constant(w);
            let m = cx.constant(&m).unwrap();
            f = cx.bin(BinOp::And, f, m).unwrap();
        }
        let k = g.constant(w);
        let k = cx.constant(&k).unwrap();
        let t = cx.bin(BinOp::Mul, f, k).unwrap();
        e = cx
            .bin(
                if g.rng.chance(1, 3) {
                    BinOp::Sub
                } else {
                    BinOp::Add
                },
                e,
                t,
            )
            .unwrap();
    }
    e
}

#[test]
fn linear_mba_is_sound_and_idempotent() {
    let eng = engine(vec![Phase::LinearMba]);
    let mut g = generator(0x3ba);
    let mut rng = Rng(88);
    let mut changed = 0;
    for i in 0..2000 {
        let mut cx = Context::new();
        let w = if i % 10 == 0 {
            [16, 64, 128, 257, 512][g.rng.below(5) as usize]
        } else {
            1 + g.rng.below(6) as u16
        };
        let e = if i % 4 == 0 {
            g.expr(&mut cx, w.min(6), 4).0
        } else {
            linear_mba_expr(&mut g, &mut cx, w)
        };
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        let r = out.roots[0];
        assert_eq!(out.stats.rejected, 0);
        assert!(
            equivalent(&mut cx, e, r.expr, &mut rng),
            "W={w}: {} vs {}",
            cx.display(e),
            cx.display(r.expr)
        );
        changed += u64::from(r.changed);
        cx.memo.clear();
        let again = eng.run(&mut cx, &[r.expr], Run::default()).unwrap();
        assert!(
            !again.roots[0].changed,
            "not idempotent: {} -> {} (from {})",
            cx.display(r.expr),
            cx.display(again.roots[0].expr),
            cx.display(e)
        );
    }
    assert!(changed > 200, "{changed}");
}

#[test]
fn linear_mba_identities() {
    let eng = engine(vec![Phase::LinearMba]);
    let same = |src: &str, want: &str, w: u16| {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::new(w).unwrap());
        let e = cx.parse(src, &o).unwrap();
        let want = cx.parse(want, &o).unwrap();
        let out = eng.simplify(&mut cx, e).unwrap();
        assert_eq!(out.expr, want, "{src}: got {}", cx.display(out.expr));
    };
    for w in [8, 32, 64, 128] {
        same("(x ^ y) + 2 * (x & y)", "x + y", w);
        same("(x | y) - (x & ~y) - (~x & y)", "x & y", w);
        same("(x + y) - 2 * (x & y)", "x ^ y", w);
        same("2 * (x | y) - (x ^ y)", "x + y", w);
        same("(x ^ y) - 2 * (~x & y)", "x - y", w);
        same("(x & y) + (x | y)", "x + y", w);
        same("x + y - (x | y)", "x & y", w);
        same("~x + (x & y) + (x & ~y) + 1", "0", w);
        // Non-uniform masks are not bitwise functions of the atoms.
        same("(x & 0xf0) + (y & 0x0f)", "(x & 0xf0) + (y & 0x0f)", w);
        // Atoms may be compound: (p * q) plays the role of x.
        same("((p * q) ^ r) + 2 * ((p * q) & r)", "p * q + r", w);
    }
}

/// Fragments through extensions of narrow values (see the module notes of `linear_mba`): the
/// function under a cast is read over the atoms lifted by that cast.
#[test]
fn linear_mba_reads_through_extensions() {
    let eng = engine(vec![Phase::LinearMba]);
    let same = |src: &str, want: &str| {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::W64);
        let e = cx.parse(src, &o).unwrap();
        let want = cx.parse(want, &o).unwrap();
        let out = eng.simplify(&mut cx, e).unwrap();
        assert_eq!(out.expr, want, "{src}: got {}", cx.display(out.expr));
    };
    // Casts of the atoms themselves (as the input spells them)...
    same(
        "(zext<64>(a:32) ^ zext<64>(b:32)) + 2 * (zext<64>(a:32) & zext<64>(b:32))",
        "zext<64>(a) + zext<64>(b)",
    );
    same(
        "(sext<64>(a:32) ^ sext<64>(b:32)) + 2 * (sext<64>(a:32) & sext<64>(b:32))",
        "sext<64>(a) + sext<64>(b)",
    );
    // ...and casts of bitwise functions of them (as the rules spell them).
    same(
        "zext<64>(a:32 ^ b:32) + 2 * zext<64>(a:32 & b:32)",
        "zext<64>(a) + zext<64>(b)",
    );
    same(
        "sext<64>(a:32 ^ b:32) + 2 * sext<64>(a:32 & b:32)",
        "sext<64>(a) + sext<64>(b)",
    );
    same(
        "sext<64>(a:32 ^ b:32) - 2 * sext<64>(~a:32 & b:32)",
        "sext<64>(a) - sext<64>(b)",
    );
    // (What the pass leaves of a sum is linear: the linear pass finishes it.)
    {
        let eng = engine(vec![Phase::LinearMba, Phase::Linear]);
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::W64);
        let src = "zext<64>(a:32 | b:32) + zext<64>(a:32 & b:32) - zext<64>(a:32) - zext<64>(b:32)";
        let e = cx.parse(src, &o).unwrap();
        let want = cx.parse("0", &o).unwrap();
        let out = eng.simplify(&mut cx, e).unwrap();
        assert_eq!(out.expr, want, "{src}: got {}", cx.display(out.expr));
    }
    // A zero extension of a function that is all ones at 0 is the function plus 2^W.
    same("zext<64>(~a:32) + zext<64>(a:32)", "0xffffffff");
    // (`~a & b` is 0 at 0: no `2^W` here.)
    same(
        "zext<64>(a:32 ^ b:32) - 2 * zext<64>(~a:32 & b:32)",
        "zext<64>(a) - zext<64>(b)",
    );
    same(
        "zext<64>(~a:32 | b:32) + zext<64>(a:32 & ~b:32)",
        "0xffffffff",
    );
    // A value read under casts of two kinds: the second cast is an atom (no identity holds
    // over independent lifted atoms), and nothing is rewritten.
    same(
        "zext<64>(a:32 & b:32) + sext<64>(a:32 & b:32)",
        "zext<64>(a & b) + sext<64>(a & b)",
    );
}

/// A tree of `& | ^ ~` over extensions of one kind and width, with constants, is the extension
/// of the tree over the operands (see `bitwise::hoist_cast`).
#[test]
fn bitwise_hoists_a_common_extension() {
    let eng = engine(vec![Phase::Bitwise]);
    let same = |src: &str, want: &str| {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::W64);
        let e = cx.parse(src, &o).unwrap();
        let want = cx.parse(want, &o).unwrap();
        let out = eng.simplify(&mut cx, e).unwrap();
        assert_eq!(out.expr, want, "{src}: got {}", cx.display(out.expr));
    };
    same("zext<64>(a:32) & ~zext<64>(b:32)", "zext<64>(a & ~b)");
    same("~zext<64>(a:32) & 0xffffffff", "zext<64>(~a)");
    same(
        "~(zext<64>(a:32) | zext<64>(b:32)) & 0xffffffff",
        "zext<64>(~(a | b))",
    );
    same("sext<64>(a:32) & ~sext<64>(b:32)", "sext<64>(a & ~b)");
    // The or below hoists; with one extension left and a constant, hoisting the xor would
    // trade the constant for a narrower one, no smaller (a sign extension of its low bits or
    // not).
    same(
        "(sext<64>(a:32) | sext<64>(b:32)) ^ 0xffffffff80000001",
        "sext<64>(a | b) ^ 0xffffffff80000001",
    );
    same(
        "(sext<64>(a:32) | sext<64>(b:32)) ^ 0xffffffff00000001",
        "sext<64>(a | b) ^ 0xffffffff00000001",
    );
    same(
        "(sext<64>(a:32) | sext<64>(b:32)) & 0xffffffff80000001 & sext<64>(c:32)",
        "sext<64>((a | b) & 0x80000001 & c)",
    );
    // All ones above the width: the complement of a zero extension of the complement, where
    // that is smaller (a constant absorbed); `~zext(a) | zext(b)` and `~zext(a & ~b)` are the
    // same size, and so are the forms once the trees below are hoisted.
    same("~zext<64>(a:32) | 0xffffffff00000000", "~zext<64>(a)");
    same(
        "~(zext<64>(a:32) | zext<64>(b:32)) | (zext<64>(c:32) & zext<64>(d:32))",
        "~zext<64>(a | b) | zext<64>(c & d)",
    );
    same(
        "~zext<64>(a:32) | zext<64>(b:32)",
        "~zext<64>(a) | zext<64>(b)",
    );
    // A constant that is no sign extension of its low bits stops the hoist (the xor below
    // still hoists), and so do extensions of two widths.
    same(
        "(sext<64>(a:32) ^ sext<64>(b:32)) | 0xffffffff00000000",
        "sext<64>(a ^ b) | 0xffffffff00000000",
    );
    same(
        "zext<64>(a:32) & zext<64>(c:16)",
        "zext<64>(a) & zext<64>(c)",
    );
}

/// `zext(~t)` is `lowmask − zext(t)` and `sext(~t)` is `−1 − sext(t)` to the linear pass,
/// where the extension of `t` exists.
#[test]
fn linear_reads_extended_complements() {
    let eng = engine(vec![Phase::Linear]);
    let same = |src: &str, want: &str| {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::W64);
        let e = cx.parse(src, &o).unwrap();
        let want = cx.parse(want, &o).unwrap();
        let out = eng.simplify(&mut cx, e).unwrap();
        assert_eq!(out.expr, want, "{src}: got {}", cx.display(out.expr));
    };
    same("sext<64>(a:32) + sext<64>(~a:32)", "-1");
    same("zext<64>(a:32) + zext<64>(~a:32)", "0xffffffff");
    same("zext<64>(a:32) * 3 + zext<64>(~a:32) * 3", "0x2fffffffd");
    same(
        "sext<64>(a:32) * 2 + sext<64>(~a:32) * 2 + sext<64>(b:32)",
        "sext<64>(b) - 2",
    );
    // A lone complement stays: building its extension would make the sum look cheaper.
    same(
        "zext<64>(~a:32) + zext<64>(b:32)",
        "zext<64>(~a) + zext<64>(b)",
    );
}

/// A lone comparison with a constant is re-expressed on the deepest term it is a function
/// of where that is smaller (see `compares::step`): extensions, offsets and complements come
/// off.
#[test]
fn compares_reexpress_on_the_deeper_term() {
    let eng = engine(vec![Phase::Compares]);
    let same = |src: &str, want: &str| {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::W64);
        let e = cx.parse(src, &o).unwrap();
        let want = cx.parse(want, &o).unwrap();
        let out = eng.simplify(&mut cx, e).unwrap();
        assert_eq!(out.expr, want, "{src}: got {}", cx.display(out.expr));
    };
    same("sext<64>(a:32) <s 0", "a <=s -1");
    same("sext<64>(a:32) <s 5", "a <=s 4");
    same("zext<64>(a:32) <u 5", "a <=u 4");
    same("5 <u zext<64>(a:32)", "6 <=u a");
    same("sext<64>(a:32) + 3 <s 10", "a <=s 6");
    same("sext<64>(trunc<8>(x)) <s 0", "trunc<8>(x) <=s -1");
    // A range check on the narrow term beats one comparison on the wide one...
    same("zext<64>(a:32) - 3 <u 10", "a - 3 <=u 9");
    // ...but not one on the same term.
    same("(x + 3) <u 10", "x + 3 <u 10");
    // Nothing deeper: as it is.
    same("x <s 0", "x <s 0");
}

/// Extracts push through more (see the `casts` module notes): a doubled value, a sum or
/// product with an operand whose low bits are zero, straddled left shifts, arithmetic shifts.
#[test]
fn casts_push_through_doubling_zero_lows_and_arithmetic_shifts() {
    let eng = engine(vec![Phase::Casts]);
    let same = |src: &str, want: &str| {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::W64);
        let e = cx.parse(src, &o).unwrap();
        let want = cx.parse(want, &o).unwrap();
        let out = eng.simplify(&mut cx, e).unwrap();
        assert_eq!(out.expr, want, "{src}: got {}", cx.display(out.expr));
    };
    same("extract<1, 31>(x + x)", "trunc<31>(x)");
    same("extract<1, 63>(x + x)", "trunc<63>(x)");
    same("extract<8, 8>((x << 8) + (y & 0xff))", "trunc<8>(x)");
    same(
        "extract<8, 8>((x << 8) - (y & 0xff))",
        "extract<8, 8>((x << 8) - (y & 0xff))",
    );
    same("extract<8, 8>((y & 0xff) - (x << 8))", "-trunc<8>(x)");
    same(
        "extract<4, 8>((x & 0xfffffff0) * y)",
        "trunc<8>(y) * extract<4, 8>(x)",
    );
    same("trunc<32>(x >>s 8)", "extract<8, 32>(x)");
    same("extract<8, 8>(x >>s 60)", "sext<8>(extract<63, 1>(x))");
    same("extract<8, 8>(x >>s 52)", "sext<8>(extract<60, 4>(x))");
}

#[test]
fn casts_split_sums_where_the_carry_is_decided() {
    let eng = engine(vec![Phase::Casts]);
    let same = |src: &str, want: &str| {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::W64);
        let e = cx.parse(src, &o).unwrap();
        let want = cx.parse(want, &o).unwrap();
        let out = eng.simplify(&mut cx, e).unwrap();
        assert_eq!(out.expr, want, "{src}: got {}", cx.display(out.expr));
    };
    // Three 20-bit lanes, guard bits 20, 41 and 62 clear in both: nothing carries into a lane.
    same(
        "extract<21, 20>((x & 0x3ffffdffffefffff) + (y & 0x3ffffdffffefffff))",
        "extract<21, 20>(x) + extract<21, 20>(y)",
    );
    // `a − b` is `a + ~b + 1`: a guard bit set in `a` and clear in `b` borrows nothing, one
    // clear in `a` and set in `b` always borrows.
    same(
        "extract<21, 20>((x | 0x100000) - (y & ~0x100000))",
        "extract<21, 20>(x) - extract<21, 20>(y)",
    );
    same(
        "extract<21, 20>((x & ~0x100000) - (y | 0x100000))",
        "extract<21, 20>(x) - extract<21, 20>(y) - 1",
    );
    // Undecided: unchanged.
    same("extract<21, 20>(x + y)", "extract<21, 20>(x + y)");
    // A shift straddling the slice: the operand's low bits, shifted at the narrow width.
    same("trunc<8>(zext<16>(a:8) << 3)", "a:8 << 3");
}

// ----- shuffle ------------------------------------------------------------------------------------

/// A random value assembled from slices: masks, shifts, rotations, extensions, extracts,
/// concats, byte swaps, and | ^ + of pieces.
fn shuffle_expr(g: &mut Gen, cx: &mut Context, w: u16, depth: u32) -> Expr {
    let width = Width::new(w).unwrap();
    if depth == 0 || g.rng.chance(1, 5) {
        return match g.rng.below(4) {
            0 => {
                let v = g.constant(w);
                cx.constant(&v).unwrap()
            }
            k => {
                let name = format!("{}{w}", ["x", "y", "z"][(k - 1) as usize]);
                cx.symbol(name.as_str(), width).unwrap()
            }
        };
    }
    let d = depth - 1;
    loop {
        match g.rng.below(9) {
            0 => {
                let x = shuffle_expr(g, cx, w, d);
                let m = g.constant(w);
                let m = cx.constant(&m).unwrap();
                return cx.bin(BinOp::And, x, m).unwrap();
            }
            1 => {
                let (x, y) = (shuffle_expr(g, cx, w, d), shuffle_expr(g, cx, w, d));
                let op = [BinOp::Or, BinOp::Xor, BinOp::Add][g.rng.below(3) as usize];
                return cx.bin(op, x, y).unwrap();
            }
            2 => {
                let x = shuffle_expr(g, cx, w, d);
                let kv = crate::BitVec::wrapping_from_u64(width, g.rng.below(u64::from(w) + 2));
                let k = cx.constant(&kv).unwrap();
                let op = [
                    BinOp::Shl,
                    BinOp::LShr,
                    BinOp::AShr,
                    BinOp::RotL,
                    BinOp::RotR,
                ][g.rng.below(5) as usize];
                return cx.bin(op, x, k).unwrap();
            }
            3 if w > 1 => {
                let from = 1 + g.rng.below(u64::from(w - 1)) as u16;
                let x = shuffle_expr(g, cx, from, d);
                return if g.rng.chance(1, 2) {
                    cx.zext(x, width).unwrap()
                } else {
                    cx.sext(x, width).unwrap()
                };
            }
            4 if w < 16 => {
                let from = w + 1 + g.rng.below(u64::from(16 - w)) as u16;
                let lo = g.rng.below(u64::from(from - w) + 1) as u16;
                let x = shuffle_expr(g, cx, from, d);
                return cx.extract(x, lo, width).unwrap();
            }
            5 if w > 1 => {
                let hw = 1 + g.rng.below(u64::from(w - 1)) as u16;
                let h = shuffle_expr(g, cx, hw, d);
                let l = shuffle_expr(g, cx, w - hw, d);
                return cx.concat(h, l).unwrap();
            }
            6 if w.is_multiple_of(8) => {
                let x = shuffle_expr(g, cx, w, d);
                return cx.un(UnOp::Bswap, x).unwrap();
            }
            7 | 8 => {
                // A split-and-recombine of one value: (x >> k) << k | x & lowmask(k).
                let x = shuffle_expr(g, cx, w, d);
                let k = g.rng.below(u64::from(w));
                let kc = cx.constant_u64(width, k).unwrap();
                let hi = cx.bin(BinOp::LShr, x, kc).unwrap();
                let hi = cx.bin(BinOp::Shl, hi, kc).unwrap();
                let m = crate::facts::known::low_mask(width, k as u32);
                let m = cx.constant(&m).unwrap();
                let lo = cx.bin(BinOp::And, x, m).unwrap();
                let op = [BinOp::Or, BinOp::Xor, BinOp::Add][g.rng.below(3) as usize];
                return cx.bin(op, hi, lo).unwrap();
            }
            _ => {}
        }
    }
}

#[test]
fn shuffle_is_sound_and_idempotent() {
    let eng = engine(vec![Phase::Shuffle]);
    let mut g = generator(0x5f1e);
    let mut rng = Rng(99);
    let mut changed = 0;
    for i in 0..2000 {
        let mut cx = Context::new();
        let w = if i % 10 == 0 {
            [16, 32, 64, 128][g.rng.below(4) as usize]
        } else {
            1 + g.rng.below(8) as u16
        };
        let e = shuffle_expr(&mut g, &mut cx, w, 4);
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        let r = out.roots[0];
        assert_eq!(out.stats.rejected, 0);
        assert!(
            equivalent(&mut cx, e, r.expr, &mut rng),
            "W={w}: {} vs {}",
            cx.display(e),
            cx.display(r.expr)
        );
        changed += u64::from(r.changed);
        cx.memo.clear();
        let again = eng.run(&mut cx, &[r.expr], Run::default()).unwrap();
        assert!(
            !again.roots[0].changed,
            "not idempotent: {} -> {} (from {})",
            cx.display(r.expr),
            cx.display(again.roots[0].expr),
            cx.display(e)
        );
    }
    assert!(changed > 200, "{changed}");
}

#[test]
fn shuffle_fixtures() {
    let eng = engine(vec![Phase::Shuffle]);
    let same = |src: &str, want: &str, w: u16| {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::new(w).unwrap());
        let e = cx.parse(src, &o).unwrap();
        let want = cx.parse(want, &o).unwrap();
        let out = eng.simplify(&mut cx, e).unwrap();
        assert_eq!(out.expr, want, "{src}: got {}", cx.display(out.expr));
    };
    // Limb recomposition.
    same(
        "(zext<64>(extract<32, 32>(x:64)) << 32) | zext<64>(trunc<32>(x:64))",
        "x:64",
        64,
    );
    same("((x >>u 8) << 8) | (x & 0xff)", "x", 32);
    // Rotation spelled with shifts and a disjoint or.
    same("(x << 8) | (x >>u 24)", "rotl(x, 8)", 32);
    // Byte swap spelled with masks and shifts.
    same(
        "(x << 24) | ((x & 0xff00) << 8) | ((x >>u 8) & 0xff00) | (x >>u 24)",
        "bswap(x)",
        32,
    );
    // Overlapping pieces are not traced.
    same("(x << 1) | x", "(x << 1) | x", 8);
    // One source in place with constant bits: a mask and an or, not a concat.
    same("((x >>u 2) << 2) | 3", "x | 3", 8);
    same("((x >>u 4) << 4) | 5", "(x & 0xf0) | 5", 8);
    same("(x & 0xf0) | 0x0f", "x | 0x0f", 8);
    same("(x & -4) ^ 3", "x | 3", 8);
}

#[test]
fn deobfuscate_strategy_undoes_layered_masking() {
    let eng = Engine::builder()
        .builtin()
        .strategy(Strategy::deobfuscate())
        .verify(Verify::strict())
        .build()
        .unwrap();
    let mut rng = Rng(123);
    for (src, want) in [
        // Linear MBA of x + y, then additive masking, then an xor mask.
        ("(((x ^ y) + 2 * (x & y)) + r - r) ^ k ^ k", "x + y"),
        // A byte-shuffled value recomposed and masked.
        (
            "((((x << 24) | ((x & 0xff00) << 8) | ((x >>u 8) & 0xff00) | (x >>u 24)) + m) - m)",
            "bswap(x)",
        ),
        // Nested: an MBA over a recomposed limb.
        (
            "(((x >>u 8) << 8) | (x & 0xff)) - y + 2 * (y & ~(((x >>u 8) << 8) | (x & 0xff))) - ((((x >>u 8) << 8) | (x & 0xff)) ^ y)",
            "0",
        ),
    ] {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::W32);
        let e = cx.parse(src, &o).unwrap();
        let want = cx.parse(want, &o).unwrap();
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        assert_eq!(out.stats.rejected, 0);
        assert!(equivalent(&mut cx, e, out.roots[0].expr, &mut rng));
        assert_eq!(
            out.roots[0].expr,
            want,
            "{src}: got {}",
            cx.display(out.roots[0].expr)
        );
    }
}

// ----- the MBA service ----------------------------------------------------------------------------

#[cfg(feature = "mba")]
mod mba_service {
    use super::*;
    use crate::mba::{
        Claim, EquivalenceProver, MOp, MbaAnswer, MbaBudget, MbaConfig, MbaExpr, MbaSolver,
        MbaTrust, MemoryCache, Verdict,
    };
    use std::sync::Arc;
    use std::sync::atomic::{AtomicU64, Ordering};

    fn mba_engine(solver: Arc<dyn MbaSolver>, trust: MbaTrust) -> Engine {
        let cfg = MbaConfig {
            trust,
            ..MbaConfig::default()
        };
        Engine::builder()
            .builtin()
            .strategy(Strategy::new("mba", vec![Phase::Mba(cfg)]))
            .mba_solver(solver)
            .build()
            .unwrap()
    }

    /// Answers the questions it was scripted with (by content), and no others.
    struct Scripted(Vec<(MbaExpr, MbaExpr)>);

    impl MbaSolver for Scripted {
        fn id(&self) -> &str {
            "scripted"
        }
        fn solve(&self, p: &MbaExpr, _: &MbaBudget) -> MbaAnswer {
            match self.0.iter().find(|(q, _)| q == p) {
                Some((_, a)) => MbaAnswer::Simplified {
                    expr: a.clone(),
                    claim: Claim::Proved,
                },
                None => MbaAnswer::NoSimpler,
            }
        }
    }

    #[test]
    fn repeated_candidate_reuses_only_the_charged_refutation_sample() {
        struct Ticks(std::cell::Cell<u64>);
        impl crate::engine::Clock for Ticks {
            fn now_ticks(&self) -> u64 {
                let t = self.0.get();
                self.0.set(t + 1);
                t
            }
        }

        let options = ParseOptions::width(Width::W32);
        let roots = |cx: &mut Context| {
            ["(x - 3) ^ 17", "(y - 3) ^ 17"].map(|text| cx.parse(text, &options).unwrap())
        };
        let mut cx = Context::new();
        let inputs = roots(&mut cx);
        // (p | x) + (p & x) - x is p, but still needs p and extra nodes.
        let big = cx
            .parse("((((x - 3) ^ 17) | x) + (((x - 3) ^ 17) & x)) - x", &options)
            .unwrap();
        let lower = |e| crate::mba::lower(&cx, e, &Default::default()).unwrap().0;
        let q = lower(inputs[0]);
        assert_eq!(q, lower(inputs[1]));
        let answer = lower(big);
        let nodes = q.nodes().len() + answer.nodes().len();
        let sample_work = nodes as u64 * crate::mba::certify::SAMPLE_POINTS as u64;
        let eng = mba_engine(Arc::new(Scripted(vec![(q, answer)])), MbaTrust::default());
        let clock = Ticks(std::cell::Cell::new(0));
        let mut profile = crate::engine::MbaRejections::default();
        let out = eng
            .run(
                &mut cx,
                &inputs,
                Run::default().with_mba_rejections(&clock, &mut profile),
            )
            .unwrap();
        assert!(out.roots.iter().all(|r| !r.changed));
        assert_eq!(profile.fresh_whole.count, 1);
        assert_eq!(profile.fresh_whole.sample_hits, 0);
        assert!(profile.fresh_whole.solve > 0);
        assert!(profile.cached_whole.count > 0);
        assert_eq!(profile.cached_whole.count, profile.cached_whole.sample_hits);
        assert_eq!(profile.cached_whole.solve, 0);
        assert!(profile.cached_whole.sample > 0 && profile.cached_whole.shrink > 0);

        // Insufficient pass work for two samples, including one answered from memory.
        let mut cx = Context::new();
        let inputs = roots(&mut cx);
        let out = eng
            .run(
                &mut cx,
                &inputs,
                Run::default().with_per_call(Budget::UNLIMITED.with_pass_work(2 * sample_work - 1)),
            )
            .unwrap();
        assert_eq!(out.stats.mba.not_smaller, 1);
        assert_eq!(
            out.roots[1].end,
            End::BudgetTerminated(crate::engine::Exhausted::PassWork)
        );
    }

    #[test]
    fn a_rewrite_cycle_is_cut() {
        // `c = (x & 1)·(x & -2)` alone is smaller as `(x & 1)·(x ^ 1)`, and the context remembers
        // that; next to `(x & -2) ^ 5`, which keeps `x & -2` alive, it is the other way round. So
        // the answer for the sum brings back `c`, whose remembered answer rebuilds the sum, which
        // is still being worked out. (Not next to `(x & -2)²`: the two products then share a
        // factor, and the sum with it taken out is smaller than either spelling.)
        let o = ParseOptions::width(Width::W8);
        let mut cx = Context::new();
        let n = cx
            .parse("((x & 1) * (x ^ 1)) + ((x & -2) ^ 5)", &o)
            .unwrap();
        let r = cx
            .parse("((x & 1) * (x & -2)) + ((x & -2) ^ 5)", &o)
            .unwrap();
        let c = cx.parse("(x & 1) * (x & -2)", &o).unwrap();
        let c1 = cx.parse("(x & 1) * (x ^ 1)", &o).unwrap();
        let lim = crate::mba::MbaLimits::default().with_min_nodes(0);
        let q = |cx: &Context, e: Expr| crate::mba::lower(cx, e, &lim).unwrap().0;
        let script = vec![(q(&cx, n), q(&cx, r)), (q(&cx, c), q(&cx, c1))];
        let trusted = MbaTrust::default().with_backend_certificates(true);
        let eng = mba_engine(Arc::new(Scripted(script)), trusted);
        let alone = eng.run(&mut cx, &[c], Run::default()).unwrap();
        assert_eq!(alone.roots[0].expr, c1);
        let out = eng.run(&mut cx, &[n], Run::default()).unwrap();
        assert_eq!(out.roots[0].end, End::Completed);
        assert!(out.stats.cycles_cut >= 1, "{:?}", out.stats);
        // Settled in the first round (the second only confirms it): the rewrite is made once.
        assert_eq!((out.stats.rounds, out.stats.mba.simplified), (2, 1));
        assert_eq!(out.roots[0].expr, r, "{}", cx.display(out.roots[0].expr));
        let mut rng = Rng(3);
        assert!(equivalent(&mut cx, n, out.roots[0].expr, &mut rng));
    }

    #[test]
    fn signature_solver_through_the_engine_is_sound() {
        let eng = mba_engine(Arc::new(crate::mba::SignatureSolver), MbaTrust::default());
        let mut g = generator(0x3bab);
        let mut rng = Rng(5);
        let mut changed = 0;
        for i in 0..800 {
            let mut cx = Context::new();
            let w = if i % 10 == 0 {
                64
            } else {
                1 + g.rng.below(6) as u16
            };
            let e = if i % 3 == 0 {
                g.expr(&mut cx, w.min(6), 4).0
            } else {
                linear_mba_expr(&mut g, &mut cx, w)
            };
            let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
            assert!(equivalent(&mut cx, e, out.roots[0].expr, &mut rng));
            changed += u64::from(out.roots[0].changed);
        }
        assert!(changed > 50, "{changed}");
    }

    #[test]
    fn normal_form_solver_through_the_engine_is_sound() {
        let solver = Arc::new(crate::mba::NormalFormSolver::default());
        let no_trust = MbaTrust {
            backend_certificates: false,
            sampled: false,
        };
        let eng = mba_engine(solver.clone(), no_trust);
        let mut g = generator(0x9f5b);
        let mut rng = Rng(7);
        let mut changed = 0;
        for i in 0..800 {
            let mut cx = Context::new();
            let w = if i % 10 == 0 {
                64
            } else {
                1 + g.rng.below(6) as u16
            };
            let e = if i % 3 == 0 {
                g.expr(&mut cx, w.min(6), 4).0
            } else {
                linear_mba_expr(&mut g, &mut cx, w)
            };
            let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
            assert!(equivalent(&mut cx, e, out.roots[0].expr, &mut rng));
            changed += u64::from(out.roots[0].changed);
            // Every answer it accepted was proved by the gate's own certificates; a candidate
            // the stronger solver exposes may still exceed a certificate's local budget.
            assert_eq!(
                out.stats.mba.simplified, out.stats.mba.certificates.proved,
                "iteration {i}: {:#?}",
                out.stats.mba
            );
        }
        assert!(changed > 50, "{changed}");
        let s = solver.stats();
        assert_eq!(s.declined_internal, 0);
        assert!(s.simplified > 50, "{s:?}");
    }

    /// Answers `x ⊕ ... ` wrongly but claims a certificate.
    struct Liar;
    impl MbaSolver for Liar {
        fn id(&self) -> &str {
            "test.liar"
        }
        fn solve(&self, p: &MbaExpr, _: &MbaBudget) -> MbaAnswer {
            let mut m = MbaExpr::new(p.vars().to_vec());
            m.push(MOp::Var(0), &[]).unwrap();
            MbaAnswer::Simplified {
                expr: m,
                claim: Claim::Certified,
            }
        }
    }

    /// Right except where no sample reaches: `x + y + [x·K₁ + y = K₂]`, a relation between the
    /// variables that no constant of either side (nor a neighbour of one) satisfies. Not over
    /// `x` alone: `[x·K₁ = K₂]` is one point, which the certificates reach through a change of
    /// variable to `x·K₁`, where it is the constant `K₂`.
    struct Subtle;
    impl MbaSolver for Subtle {
        fn id(&self) -> &str {
            "test.subtle"
        }
        fn solve(&self, p: &MbaExpr, _: &MbaBudget) -> MbaAnswer {
            if p.vars().len() != 2 {
                return MbaAnswer::Unsupported("two variables only".into());
            }
            let w = p.width().unwrap();
            let c = |m: &mut MbaExpr, v: u64| {
                m.push(MOp::Const(crate::BitVec::wrapping_from_u64(w, v)), &[])
                    .unwrap()
            };
            let mut m = MbaExpr::new(p.vars().to_vec());
            let x = m.push(MOp::Var(0), &[]).unwrap();
            let y = m.push(MOp::Var(1), &[]).unwrap();
            let s = m.push(MOp::Add, &[x, y]).unwrap();
            let k1 = c(&mut m, 0x9e37_79b9_7f4a_7c15);
            let k2 = c(
                &mut m,
                0x9e37_79b9_7f4a_7c15u64.wrapping_mul(0x1234_5678_9abc_def1),
            );
            let xk = m.push(MOp::Mul, &[x, k1]).unwrap();
            let xky = m.push(MOp::Add, &[xk, y]).unwrap();
            let d = m.push(MOp::Xor, &[xky, k2]).unwrap();
            let nd = m.push(MOp::Neg, &[d]).unwrap();
            let o = m.push(MOp::Or, &[d, nd]).unwrap();
            let nz = m.push(MOp::LShr(w.bits() - 1), &[o]).unwrap();
            let one = c(&mut m, 1);
            let hit = m.push(MOp::Sub, &[one, nz]).unwrap();
            m.push(MOp::Add, &[s, hit]).unwrap();
            MbaAnswer::Simplified {
                expr: m,
                claim: Claim::Certified,
            }
        }
    }

    /// The right answer `x + y`, with no evidence.
    struct Unproven;
    impl MbaSolver for Unproven {
        fn id(&self) -> &str {
            "test.unproven"
        }
        fn solve(&self, p: &MbaExpr, _: &MbaBudget) -> MbaAnswer {
            if p.vars().len() != 2 {
                return MbaAnswer::Unsupported("two variables only".into());
            }
            let mut m = MbaExpr::new(p.vars().to_vec());
            let x = m.push(MOp::Var(0), &[]).unwrap();
            let y = m.push(MOp::Var(1), &[]).unwrap();
            m.push(MOp::Add, &[x, y]).unwrap();
            MbaAnswer::Simplified {
                expr: m,
                claim: Claim::Unverified,
            }
        }
    }

    /// `x + y`, spelled with a degree-2 MBA identity (`x·y = (x&y)(x|y) + (x&~y)(~x&y)`): the
    /// linear signature and exhaustive evaluation (128 variable bits) cannot prove it, the
    /// degree-2 certificate can.
    const NONLINEAR: &str =
        "(x ^ y) + 2 * (x & y) + x * y - (x & y) * (x | y) - (x & ~y) * (~x & y)";

    /// `x + y`, through a relation between `(x + y) * 2 >> 1` and `x + y` that no native
    /// certificate sees (a right shift of arithmetic is an atom, independent of its operand).
    /// a right shift of a bitwise function is not an atom (`(x >> 1) * 2 + (x & 1)`
    /// is proved `x`), so the relation goes through a sum; and not through a shift of the sum
    /// itself beside a mask of it, which the phase reads as two slices of one value
    /// (`((x + y) >> 1) * 2 + ((x + y) & 1)` is proved `x + y` that way).
    const NOT_NATIVE: &str = "(((x + y) * 2) >>u 1) + ((x + y) & (-1 ^ (-1 >>u 1)))";

    fn run(eng: &Engine, src: &str) -> (Context, crate::engine::Outcome, Expr) {
        let mut cx = Context::new();
        let e = cx.parse(src, &ParseOptions::width(Width::W64)).unwrap();
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        (cx, out, e)
    }

    #[test]
    fn the_gate_refutes_liars_and_honours_trust() {
        // Trusting the backend: a certified lie is refuted by the always-on sampled check.
        let trusted = MbaTrust::default().with_backend_certificates(true);
        let (_, out, _) = run(
            &mba_engine(Arc::new(Liar), trusted),
            "(x ^ y) + 2 * (x & y)",
        );
        assert!(!out.roots[0].changed);
        assert!(out.stats.mba.refuted > 0);
        // An answer wrong at one unsampled point is accepted only on the backend's word, so
        // without trust in certificates (the default) it is rejected.
        let no_trust = MbaTrust::default();
        assert!(!no_trust.backend_certificates && !no_trust.sampled);
        let (mut cx, out, _) = run(&mba_engine(Arc::new(Subtle), no_trust), NONLINEAR);
        let wrong = cx.parse("x + y", &ParseOptions::width(Width::W64)).unwrap();
        assert_ne!(out.roots[0].expr, wrong);
        assert!(out.stats.mba.proof_unknown > 0);
        // A right answer bitwright can prove itself is taken without any trust: here as the
        // same polynomial over symbols, tried ahead of the degree-2 certificate's 18,337 points.
        let (mut cx, out, _) = run(&mba_engine(Arc::new(Unproven), no_trust), NONLINEAR);
        let want = cx.parse("x + y", &ParseOptions::width(Width::W64)).unwrap();
        assert_eq!(out.roots[0].expr, want);
        assert!(out.stats.mba.certificates.symbolic > 0);
        // A right but unproven answer no native certificate reaches: rejected by default,
        // accepted when sampling is trusted, and accepted by default when a prover proves it.
        let (mut cx, out, _) = run(
            &mba_engine(Arc::new(Unproven), MbaTrust::default()),
            NOT_NATIVE,
        );
        let want = cx.parse("x + y", &ParseOptions::width(Width::W64)).unwrap();
        assert_ne!(out.roots[0].expr, want);
        assert!(out.stats.mba.proof_unknown > 0);
        let sampled = MbaTrust {
            sampled: true,
            ..MbaTrust::default()
        };
        let (mut cx, out, _) = run(&mba_engine(Arc::new(Unproven), sampled), NOT_NATIVE);
        let want = cx.parse("x + y", &ParseOptions::width(Width::W64)).unwrap();
        assert_eq!(out.roots[0].expr, want);
        struct Yes;
        impl EquivalenceProver for Yes {
            fn id(&self) -> &str {
                "test.yes"
            }
            fn prove_equal(&self, _: &MbaExpr, _: &MbaExpr, _: &MbaBudget) -> Verdict {
                Verdict::Proved
            }
        }
        let eng = Engine::builder()
            .builtin()
            .strategy(Strategy::new("mba", vec![Phase::Mba(MbaConfig::default())]))
            .mba_solver(Arc::new(Unproven))
            .mba_prover(Arc::new(Yes))
            .build()
            .unwrap();
        let (mut cx, out, _) = run(&eng, NOT_NATIVE);
        let want = cx.parse("x + y", &ParseOptions::width(Width::W64)).unwrap();
        assert_eq!(out.roots[0].expr, want);
    }

    /// Counts calls and answers "no simpler".
    struct Counting(AtomicU64);
    impl MbaSolver for Counting {
        fn id(&self) -> &str {
            "test.counting"
        }
        fn solve(&self, _: &MbaExpr, _: &MbaBudget) -> MbaAnswer {
            self.0.fetch_add(1, Ordering::Relaxed);
            MbaAnswer::NoSimpler
        }
    }

    #[test]
    fn answers_are_cached_and_calls_budgeted() {
        // (A solver that counts its steps: one charged its whole limit has its fragments kept
        // from the questions above them in the run that asked, which a cached answer does
        // not say.)
        let solver = Arc::new(Steady(1000, AtomicU64::new(0)));
        let cache = Arc::new(MemoryCache::new(1024));
        let eng = Engine::builder()
            .builtin()
            .strategy(Strategy::new("mba", vec![Phase::Mba(MbaConfig::default())]))
            .mba_solver(solver.clone())
            .mba_cache(cache.clone())
            .build()
            .unwrap();
        let (_, out, _) = run(&eng, NONLINEAR);
        let first = solver.1.load(Ordering::Relaxed);
        assert!(first > 0 && out.stats.mba.no_simpler > 0);
        // A fresh context: answered from the cache.
        let (_, out, _) = run(&eng, NONLINEAR);
        assert_eq!(solver.1.load(Ordering::Relaxed), first);
        assert!(out.stats.mba.cache_hits > 0);
        // The call budget.
        let eng = mba_engine(Arc::new(Counting(AtomicU64::new(0))), MbaTrust::default());
        let mut cx = Context::new();
        let e = cx
            .parse(NONLINEAR, &ParseOptions::width(Width::W64))
            .unwrap();
        let out = eng
            .run(
                &mut cx,
                &[e],
                Run {
                    per_call: Budget {
                        mba_calls: 0,
                        ..Budget::UNLIMITED
                    },
                    ..Run::default()
                },
            )
            .unwrap();
        assert_eq!(
            out.roots[0].end,
            End::BudgetTerminated(crate::engine::Exhausted::MbaCalls)
        );
    }

    /// Always runs out.
    struct Tired;
    impl MbaSolver for Tired {
        fn id(&self) -> &str {
            "test.tired"
        }
        fn solve(&self, _: &MbaExpr, _: &MbaBudget) -> MbaAnswer {
            MbaAnswer::Exhausted
        }
    }

    #[test]
    fn exhausted_answers_are_not_final() {
        let eng = mba_engine(Arc::new(Tired), MbaTrust::default());
        let mut cx = Context::new();
        let e = cx
            .parse(NONLINEAR, &ParseOptions::width(Width::W64))
            .unwrap();
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        assert!(matches!(out.roots[0].end, End::BudgetTerminated(_)));
        let again = eng.run(&mut cx, &[e], Run::default()).unwrap();
        assert!(
            again.stats.mba.calls > 0,
            "an exhausted answer was memoized"
        );
    }

    /// Answers "no simpler" for a fixed number of steps (at most its limit), counting calls.
    struct Steady(u64, AtomicU64);
    impl MbaSolver for Steady {
        fn id(&self) -> &str {
            "test.steady"
        }
        fn solve(&self, p: &MbaExpr, budget: &MbaBudget) -> MbaAnswer {
            self.solve_counted(p, budget).0
        }
        fn solve_counted(&self, _: &MbaExpr, budget: &MbaBudget) -> (MbaAnswer, u64) {
            self.1.fetch_add(1, Ordering::Relaxed);
            (MbaAnswer::NoSimpler, self.0.min(budget.steps))
        }
    }

    /// Runs out of steps with fewer than the first field; the second is the most it was given.
    struct Hungry(u64, AtomicU64);
    impl MbaSolver for Hungry {
        fn id(&self) -> &str {
            "test.hungry"
        }
        fn solve(&self, p: &MbaExpr, budget: &MbaBudget) -> MbaAnswer {
            self.solve_counted(p, budget).0
        }
        fn solve_counted(&self, _: &MbaExpr, budget: &MbaBudget) -> (MbaAnswer, u64) {
            self.1.fetch_max(budget.steps, Ordering::Relaxed);
            match budget.steps < self.0 {
                true => (MbaAnswer::Exhausted, budget.steps),
                false => (MbaAnswer::NoSimpler, self.0),
            }
        }
    }

    /// Several MBA questions, one per summand.
    const MANY: &str = "((x ^ y) + 2 * (x & y)) * ((x | z) - (x & z)) \
                        + ((y ^ z) + 2 * (y & z)) * ((y | x) + (y & x)) \
                        + ((z ^ x) + 2 * (z & x)) * ((z | y) - (z & y))";

    #[test]
    fn the_solvers_steps_are_an_account_of_the_run() {
        assert_eq!(Budget::fuel(1000).mba_steps, 4000);
        assert_eq!(Budget::default().mba_steps, 1 << 24);
        let ask = |solver: Arc<dyn MbaSolver>, steps: u64| {
            let eng = mba_engine(solver, MbaTrust::default());
            let mut cx = Context::new();
            let e = cx.parse(MANY, &ParseOptions::width(Width::W64)).unwrap();
            let run = Run::default().with_per_call(Budget::UNLIMITED.with_mba_steps(steps));
            eng.run(&mut cx, &[e], run).unwrap()
        };
        // Unlimited: every question asked, each charged what it spent.
        let s = Arc::new(Steady(1000, AtomicU64::new(0)));
        let out = ask(s.clone(), u64::MAX);
        let calls = s.1.load(Ordering::Relaxed);
        assert!(calls >= 3, "{calls}");
        assert_eq!(out.stats.mba.calls, calls);
        assert_eq!(out.stats.mba.steps, 1000 * calls);
        assert_eq!(out.roots[0].end, End::Completed);
        // An account for one question: the next ones are not asked, and the run says why.
        let s = Arc::new(Steady(1000, AtomicU64::new(0)));
        let out = ask(s.clone(), 1000);
        assert_eq!(
            (s.1.load(Ordering::Relaxed), out.stats.mba.steps),
            (1, 1000)
        );
        assert_eq!(
            out.roots[0].end,
            End::BudgetTerminated(crate::engine::Exhausted::MbaSteps)
        );
        // Nothing in it: nothing asked.
        let s = Arc::new(Steady(1000, AtomicU64::new(0)));
        let out = ask(s.clone(), 0);
        assert_eq!(s.1.load(Ordering::Relaxed), 0);
        assert_eq!(
            out.roots[0].end,
            End::BudgetTerminated(crate::engine::Exhausted::MbaSteps)
        );
        // A last question gets what is left, never more.
        let s = Arc::new(Steady(1000, AtomicU64::new(0)));
        let out = ask(s.clone(), 1000 * calls - 1);
        assert_eq!(out.stats.mba.steps, 1000 * calls - 1);
        // A solver that does not count is charged its whole limit: what a question takes
        // while the rounds have others to ask (65,536 steps).
        let limit = MbaConfig::default().budget.steps;
        let c = Arc::new(Counting(AtomicU64::new(0)));
        let out = ask(c.clone(), 2 * limit);
        let asked = c.0.load(Ordering::Relaxed);
        assert!(asked >= 3 && asked << 16 < 2 * limit, "{asked}");
        assert_eq!(out.stats.mba.steps, asked << 16);
        // A question that ran out of those is asked again once a round changes nothing, and
        // its limit then is a quarter of the account where that is less than the solver's
        // own.
        let h = Arc::new(Hungry(300_000, AtomicU64::new(0)));
        let out = ask(h.clone(), 2 * limit);
        assert_eq!(h.1.load(Ordering::Relaxed), limit / 2);
        assert_eq!(out.roots[0].end, End::Completed);
        // One that runs out of that too: the run says so.
        let h = Arc::new(Hungry(limit, AtomicU64::new(0)));
        let out = ask(h.clone(), 2 * limit);
        assert_eq!(h.1.load(Ordering::Relaxed), limit / 2);
        assert_eq!(
            out.roots[0].end,
            End::BudgetTerminated(crate::engine::Exhausted::MbaSteps)
        );
        // The normal-form solver counts its steps, and a remembered answer is charged what
        // finding it cost: the same run in a fresh context, answered from the solver's memory,
        // spends the same and gives the same.
        let nf: Arc<dyn MbaSolver> = Arc::new(crate::mba::NormalFormSolver::default());
        let eng = mba_engine(nf, MbaTrust::default());
        let once = |eng: &Engine| {
            let mut cx = Context::new();
            let e = cx.parse(MANY, &ParseOptions::width(Width::W64)).unwrap();
            let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
            (cx.display(out.roots[0].expr).to_string(), out.stats.mba)
        };
        let (first, a) = once(&eng);
        let (again, b) = once(&eng);
        assert_eq!(first, again);
        assert!(a.calls > 0 && a.steps > 0 && a.steps < a.calls * limit);
        assert_eq!((a.calls, a.steps), (b.calls, b.steps));
    }

    #[test]
    fn the_signature_solver_counts_its_corners() {
        let s = crate::mba::SignatureSolver;
        let w = Width::W64;
        let mut p = MbaExpr::new(vec![w, w]);
        let x = p.push(MOp::Var(0), &[]).unwrap();
        let y = p.push(MOp::Var(1), &[]).unwrap();
        let and = p.push(MOp::And, &[x, y]).unwrap();
        let xor = p.push(MOp::Xor, &[x, y]).unwrap();
        let two = p
            .push(MOp::Const(BitVec::wrapping_from_u64(w, 2)), &[])
            .unwrap();
        let dbl = p.push(MOp::Mul, &[and, two]).unwrap();
        p.push(MOp::Add, &[xor, dbl]).unwrap();
        // Six nodes at four corners: 24 evaluations, one step.
        let (a, steps) = s.solve_counted(&p, &MbaBudget::default());
        assert!(matches!(a, MbaAnswer::Simplified { .. }));
        assert_eq!(steps, 1);
        assert_eq!(s.solve(&p, &MbaBudget::default()), a);
        // No step to spend: not computed.
        let (a, steps) = s.solve_counted(&p, &MbaBudget::default().with_steps(0));
        assert_eq!((a, steps), (MbaAnswer::Exhausted, 0));
        // Through the deadline wrapper, the same count.
        let t = crate::mba::ThreadedSolver::new(s, std::time::Duration::from_secs(30), 2);
        assert_eq!(t.solve_counted(&p, &MbaBudget::default()).1, 1);
    }

    // ----- M6 review regressions ------------------------------------------------------------

    #[test]
    fn cached_answers_are_keyed_by_trust() {
        // A lax engine takes `Subtle`'s wrong answer on sampling; a strict engine sharing the
        // cache must not take it from there.
        let cache = Arc::new(MemoryCache::new(1024));
        let with = |trust| {
            Engine::builder()
                .builtin()
                .strategy(Strategy::new(
                    "mba",
                    vec![Phase::Mba(MbaConfig {
                        trust,
                        ..MbaConfig::default()
                    })],
                ))
                .mba_solver(Arc::new(Subtle))
                .mba_cache(cache.clone())
                .build()
                .unwrap()
        };
        let lax = MbaTrust {
            backend_certificates: false,
            sampled: true,
        };
        let strict = MbaTrust {
            backend_certificates: false,
            sampled: false,
        };
        let (_, out, _) = run(&with(lax), NONLINEAR);
        assert!(out.roots[0].changed && out.stats.mba.simplified > 0);
        // (The strict engine still takes the factor two of the terms share out of them: no
        // answer of the solver's.)
        let (cx, out, _) = run(&with(strict), NONLINEAR);
        assert_eq!(
            out.stats.mba.simplified, 0,
            "a laxly accepted answer was reused"
        );
        let text = cx.display(out.roots[0].expr).to_string();
        assert!(!text.contains("9e3779b97f4a7c15"), "{text}");
    }

    struct Panicky;
    impl MbaSolver for Panicky {
        fn id(&self) -> &str {
            "test.panicky"
        }
        fn solve(&self, _: &MbaExpr, _: &MbaBudget) -> MbaAnswer {
            panic!("a solver bug")
        }
    }

    #[test]
    fn a_panicking_solver_does_not_disable_the_deadline_wrapper() {
        let s = crate::mba::ThreadedSolver::new(Panicky, std::time::Duration::from_secs(5), 2);
        let m = MbaExpr::new(vec![]);
        for _ in 0..4 {
            assert!(matches!(
                s.solve(&m, &MbaBudget::default()),
                MbaAnswer::Unsupported(_)
            ));
        }
        assert_eq!(s.abandoned(), 0);
    }

    #[test]
    fn exact_evidence_is_charged_and_budgeted() {
        // At W = 6 the two variables have 12 bits: `Unproven`'s right answer, outside the
        // polynomial fragment, is proved by evaluating all 2^12 points, and that work is
        // charged.
        let eng = mba_engine(Arc::new(Unproven), MbaTrust::default());
        let o = ParseOptions::width(Width::new(6).unwrap());
        let mut cx = Context::new();
        let e = cx.parse(NOT_NATIVE, &o).unwrap();
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        assert_eq!(
            out.roots[0].expr,
            cx.parse("x + y", &o).unwrap(),
            "{}\n{:#?}",
            cx.display(out.roots[0].expr),
            out.stats
        );
        assert!(out.stats.pass_work >= 1 << 12, "{}", out.stats.pass_work);
        assert_eq!(out.stats.mba.certificates.exhaustive, 1);
        assert_eq!(out.stats.mba.certificates.points, 1 << 12);
        // A pass-work budget stops it: the certificate is not started, and the node is not
        // final (more budget may prove it).
        let mut cx = Context::new();
        let e = cx.parse(NOT_NATIVE, &o).unwrap();
        let out = eng
            .run(
                &mut cx,
                &[e],
                Run {
                    per_call: Budget {
                        pass_work: 1 << 12,
                        ..Budget::UNLIMITED
                    },
                    ..Run::default()
                },
            )
            .unwrap();
        assert_eq!(
            out.roots[0].end,
            End::BudgetTerminated(crate::engine::Exhausted::PassWork)
        );
    }
}

// ----- regressions from the M6 review --------------------------------------------------------------

#[test]
fn linear_mba_never_reads_linear_terms_as_bitwise_operands() {
    linear_mba_never_reads_linear_terms_as_bitwise_operands_cases(1000);
}

/// [`linear_mba_never_reads_linear_terms_as_bitwise_operands`] over 3000 random cases.
#[test]
#[ignore = "heavy: run with --release -- --ignored"]
fn linear_mba_never_reads_linear_terms_as_bitwise_operands_heavy() {
    linear_mba_never_reads_linear_terms_as_bitwise_operands_cases(3000);
}

fn linear_mba_never_reads_linear_terms_as_bitwise_operands_cases(cases: usize) {
    for phases in [vec![Phase::LinearMba], Strategy::deobfuscate().phases] {
        let eng = engine(phases);
        let mut rng = Rng(0x6b);
        for w in [8u16, 32] {
            for src in [
                "(x + y) & (x | y)",
                "(x + y) | (x & y)",
                "(2 * x) & (x | y)",
                "((x << 1) & y) + (x & y)",
                "(x - y) & ~(x & y)",
                "~(x + y) & y",
            ] {
                let mut cx = Context::new();
                let e = cx
                    .parse(src, &ParseOptions::width(Width::new(w).unwrap()))
                    .unwrap();
                let out = eng.simplify(&mut cx, e).unwrap();
                assert!(
                    equivalent(&mut cx, e, out.expr, &mut rng),
                    "W={w}: {src} became {}",
                    cx.display(out.expr)
                );
            }
        }
        // Random DAGs over + - & | ^ of two symbols, linear terms under bitwise operators.
        let mut g = generator(0x6c);
        for _ in 0..cases {
            let mut cx = Context::new();
            let w = Width::new(1 + g.rng.below(8) as u16).unwrap();
            let mut pool = vec![cx.symbol("x", w).unwrap(), cx.symbol("y", w).unwrap()];
            for _ in 0..6 {
                let a = pool[g.rng.below(pool.len() as u64) as usize];
                let b = pool[g.rng.below(pool.len() as u64) as usize];
                let op = [BinOp::Add, BinOp::Sub, BinOp::And, BinOp::Or, BinOp::Xor]
                    [g.rng.below(5) as usize];
                pool.push(cx.bin(op, a, b).unwrap());
            }
            let e = *pool.last().unwrap();
            let out = eng.simplify(&mut cx, e).unwrap();
            assert!(
                equivalent(&mut cx, e, out.expr, &mut rng),
                "{} became {}",
                cx.display(e),
                cx.display(out.expr)
            );
        }
    }
}

#[test]
fn linear_mba_is_independent_of_construction_order() {
    let eng = engine(vec![Phase::LinearMba]);
    let mut g = generator(0x1234);
    for _ in 0..5000 {
        let mut c1 = Context::new();
        let mut c2 = Context::new();
        let w = Width::new(1 + g.rng.below(8) as u16).unwrap();
        for name in ["a", "b", "c", "d", "x", "y", "z"] {
            c1.symbol(name, w).unwrap();
        }
        for name in ["z", "y", "x", "d", "c", "b", "a"] {
            c2.symbol(name, w).unwrap();
        }
        let e1 = linear_mba_expr(&mut g, &mut c1, w.bits());
        let text = c1.display(e1).to_string();
        let e2 = c2.parse(&text, &ParseOptions::width(w)).unwrap();
        let r1 = eng.simplify(&mut c1, e1).unwrap().expr;
        let r2 = eng.simplify(&mut c2, e2).unwrap().expr;
        assert_eq!(
            c1.display(r1).to_string(),
            c2.display(r2).to_string(),
            "{text}"
        );
    }
}

#[test]
fn linear_mba_emits_conjunctions_for_any_number_of_atoms() {
    let eng = engine(vec![Phase::LinearMba]);
    for (src, w, want) in [
        // The conjunction form is smaller than the indicator form here.
        (
            "3 * (x & ~y) + 2 * (~x & y) + 5 * (x & y) - (x | y)",
            64,
            "(x & y) + (x << 1) + y",
        ),
        // Four atoms: the indicator form needs three or fewer, and a conjunction remains.
        ("(a | b) - (a ^ b) + (c & ~d) + (c & d)", 32, "(a & b) + c"),
    ] {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::new(w).unwrap());
        let e = cx.parse(src, &o).unwrap();
        let out = eng.simplify(&mut cx, e).unwrap();
        assert_eq!(cx.display(out.expr).to_string(), want, "{src}");
    }
    // Random linear MBAs over four to six symbols, exhaustively at two bits a symbol.
    let mut g = generator(0xc0_4d);
    let mut rng = Rng(0xc0_4e);
    let mut changed = 0;
    for _ in 0..1500 {
        let mut cx = Context::new();
        let w = Width::new(2).unwrap();
        let n = 4 + g.rng.below(3) as usize;
        let vars: Vec<Expr> = (0..n)
            .map(|k| cx.symbol(format!("v{k}").as_str(), w).unwrap())
            .collect();
        let mut sum = cx.constant_u64(w, g.rng.below(4)).unwrap();
        for _ in 0..(2 + g.rng.below(4)) {
            let mut f = vars[g.rng.below(n as u64) as usize];
            for _ in 0..(1 + g.rng.below(3)) {
                let o = vars[g.rng.below(n as u64) as usize];
                let o = if g.rng.chance(1, 3) {
                    cx.un(UnOp::Not, o).unwrap()
                } else {
                    o
                };
                let op = [BinOp::And, BinOp::Or, BinOp::Xor][g.rng.below(3) as usize];
                f = cx.bin(op, f, o).unwrap();
            }
            let k = cx.constant_u64(w, g.rng.below(4)).unwrap();
            let term = cx.bin(BinOp::Mul, f, k).unwrap();
            sum = cx.bin(BinOp::Add, sum, term).unwrap();
        }
        let out = eng.simplify(&mut cx, sum).unwrap();
        assert!(
            equivalent(&mut cx, sum, out.expr, &mut rng),
            "{} became {}",
            cx.display(sum),
            cx.display(out.expr)
        );
        changed += u32::from(out.changed);
    }
    assert!(changed > 300, "{changed}");
}

// ----- the invert pass --------------------------------------------------------------------------

/// A random chain of layers over `x`: invertible ones (with constant or symbol parameters, and
/// triangular `v ⊙ g(v)`) and non-invertible ones (masks, shifts, feeding `x` back in).
fn layered(g: &mut Gen, cx: &mut Context, x: Expr, w: u16, depth: u32) -> Expr {
    let wd = Width::new(w).unwrap();
    let mut e = x;
    for _ in 0..depth {
        let k = if g.rng.chance(1, 4) {
            cx.symbol("p", wd).unwrap()
        } else {
            let v = g.constant(w);
            cx.constant(&v).unwrap()
        };
        let sh = cx.constant_u64(wd, 1 + g.rng.below(u64::from(w))).unwrap();
        e = match g.rng.below(12) {
            0 => cx.bin(BinOp::Add, e, k).unwrap(),
            1 => cx.bin(BinOp::Xor, e, k).unwrap(),
            2 => cx.bin(BinOp::Mul, e, k).unwrap(),
            3 => cx.bin(BinOp::Sub, k, e).unwrap(),
            4 => cx.bin(BinOp::RotL, e, k).unwrap(),
            5 => cx
                .un(
                    [UnOp::Not, UnOp::Neg, UnOp::BitRev][g.rng.below(3) as usize],
                    e,
                )
                .unwrap(),
            6 => {
                let t = cx.bin(BinOp::LShr, e, sh).unwrap();
                cx.bin(BinOp::Xor, e, t).unwrap()
            }
            7 => {
                let t = cx.bin(BinOp::Shl, e, sh).unwrap();
                let t = cx.bin(BinOp::And, t, k).unwrap();
                cx.bin(BinOp::Sub, e, t).unwrap()
            }
            8 => {
                // The mixer's involution, scaled to the width.
                let a = cx.constant_u64(wd, u64::from(w / 2)).unwrap();
                let b = cx.constant_u64(wd, u64::from(w - w / 4 - 1)).unwrap();
                let (s1, s2) = (
                    cx.bin(BinOp::LShr, e, a).unwrap(),
                    cx.bin(BinOp::LShr, e, b).unwrap(),
                );
                let t = cx.bin(BinOp::LShr, s1, s2).unwrap();
                cx.bin(BinOp::Xor, e, t).unwrap()
            }
            9 => cx.bin(BinOp::And, e, k).unwrap(),
            10 => cx.bin(BinOp::LShr, e, sh).unwrap(),
            _ => cx.bin(BinOp::Xor, e, x).unwrap(),
        };
    }
    e
}

#[test]
fn invert_is_sound_and_idempotent() {
    let eng = engine(vec![Phase::Invert]);
    let std = Engine::standard();
    // Strict verification samples every rewrite and checks both sides' facts meet.
    let strict = Engine::builder()
        .builtin()
        .strategy(Strategy::standard())
        .verify(Verify::strict())
        .build()
        .unwrap();
    let mut g = generator(0x1a7e_0001);
    let mut rng = Rng(11);
    let mut changed = 0;
    for i in 0..1500 {
        let mut cx = Context::new();
        let w = if i % 10 == 0 {
            [16, 64, 128, 257][g.rng.below(4) as usize]
        } else {
            1 + g.rng.below(4) as u16
        };
        let wd = Width::new(w).unwrap();
        let (x, y) = (cx.symbol("x", wd).unwrap(), cx.symbol("y", wd).unwrap());
        let depth = 1 + g.rng.below(4) as u32;
        let f = layered(&mut g, &mut cx, x, w, depth);
        // Sometimes widened by an injective map that is not onto: then a constant may have no
        // preimage at all.
        let widen = match g.rng.below(8) {
            _ if w >= 256 => None,
            0 => Some((0, w + 1 + g.rng.below(3) as u16)),
            1 => Some((1, w + 1 + g.rng.below(3) as u16)),
            2 => Some((2, w + 1 + g.rng.below(3) as u16)),
            _ => None,
        };
        let lo = widen.map(|(_, w2)| g.constant(w2 - w));
        let wide = |cx: &mut Context, e: Expr| -> Expr {
            let Some((how, w2)) = widen else {
                return e;
            };
            let wd2 = Width::new(w2).unwrap();
            match how {
                0 => cx.zext(e, wd2).unwrap(),
                1 => cx.sext(e, wd2).unwrap(),
                _ => {
                    let k = cx.constant(&lo.unwrap()).unwrap();
                    cx.concat(e, k).unwrap()
                }
            }
        };
        let fw = wide(&mut cx, f);
        let wc = cx.width(fw).unwrap();
        let op = [
            CmpOpExt::Eq,
            CmpOpExt::Ne,
            CmpOpExt::Ult,
            CmpOpExt::Ule,
            CmpOpExt::Slt,
            CmpOpExt::Sle,
        ][g.rng.below(6) as usize];
        let e = match i % 5 {
            // Against a constant: often an actual value of the chain, so it is solvable.
            0 | 1 => {
                let c = if g.rng.chance(1, 2) {
                    let xv = g.constant(w);
                    let env = FnEnv(|k: &crate::SymbolKey, _| {
                        Some(if *k == crate::SymbolKey::from("x") {
                            xv
                        } else {
                            BitVec::zero(wd)
                        })
                    });
                    cx.eval(&[fw], &env).unwrap()[0]
                } else {
                    g.constant(wc.bits())
                };
                let c = cx.constant(&c).unwrap();
                cx.cmp(op, fw, c).unwrap()
            }
            // The same chain over another value, or a different one.
            2 => {
                let fy = cx.substitute(&[fw], &[(x, y)]).unwrap()[0];
                cx.cmp(op, fw, fy).unwrap()
            }
            3 => {
                let depth = 1 + g.rng.below(3) as u32;
                let h = layered(&mut g, &mut cx, y, w, depth);
                let h = wide(&mut cx, h);
                cx.cmp(op, fw, h).unwrap()
            }
            // Or-trees against zero, over one value or two.
            _ => {
                let other = if g.rng.chance(1, 2) { x } else { y };
                let depth = 1 + g.rng.below(3) as u32;
                let h = layered(&mut g, &mut cx, other, w, depth);
                let h = wide(&mut cx, h);
                let o = cx.bin(BinOp::Or, fw, h).unwrap();
                let z = cx.constant(&BitVec::zero(wc)).unwrap();
                cx.cmp(op, o, z).unwrap()
            }
        };
        for engine in [&eng, &std, &strict] {
            let out = engine.run(&mut cx, &[e], Run::default()).unwrap();
            let r = out.roots[0];
            assert_eq!(r.end, End::Completed);
            assert_eq!(out.stats.rejected, 0, "{}", cx.display(e));
            assert!(
                equivalent(&mut cx, e, r.expr, &mut rng),
                "{} vs {}",
                cx.display(e),
                cx.display(r.expr)
            );
            changed += u64::from(r.changed);
            cx.memo.clear();
            let again = engine.run(&mut cx, &[r.expr], Run::default()).unwrap();
            assert!(
                !again.roots[0].changed,
                "not idempotent: {} -> {}",
                cx.display(r.expr),
                cx.display(again.roots[0].expr)
            );
        }
    }
    assert!(changed > 800, "{changed}");
}

/// Checks `src` simplifies to `want` (both parsed at `w` bits) under `eng`.
fn check_to(eng: &Engine, src: &str, want: &str, w: u16) {
    let mut cx = Context::new();
    let o = ParseOptions::width(Width::new(w).unwrap());
    let e = cx.parse(src, &o).unwrap();
    let out = eng.simplify(&mut cx, e).unwrap();
    let want = cx
        .parse(want, &ParseOptions::width(cx.width(e).unwrap()))
        .unwrap();
    assert_eq!(out.expr, want, "{src} gave {}", cx.display(out.expr));
}

#[test]
fn invert_fixtures() {
    let eng = Engine::standard();
    // Cancelling one layer on both sides, repeatedly.
    check_to(&eng, "x + 7 == y + 7", "x == y", 8);
    check_to(&eng, "(x ^ z) * 3 != (y ^ z) * 3", "x != y", 8);
    check_to(&eng, "rotl(x, z) == rotl(y, z)", "x == y", 8);
    check_to(&eng, "z - x == z - y", "x == y", 8);
    check_to(&eng, "zext<16>(x) == zext<16>(y)", "x == y", 8);
    // ... never through a map that is not injective.
    check_to(&eng, "x * 6 == y * 6", "x * 6 == y * 6", 8);
    check_to(&eng, "(x & z) == (y & z)", "(x & z) == (y & z)", 8);
    check_to(
        &eng,
        "(x ^ z) * w == (y ^ z) * w",
        "(x ^ z) * w == (y ^ z) * w",
        8,
    );
    // Moving a constant across: 5 * 3⁻¹ = 5 * 171 = 0x57 (mod 2^8).
    check_to(&eng, "x * 3 == 5", "x == 0x57", 8);
    check_to(&eng, "rotl(x, 3) != 5", "x != 0xa0", 8);
    check_to(&eng, "~(x - 9) == 0", "x == 8", 8);
    // A triangular map: the high nibble first, then the low one.
    check_to(&eng, "(x ^ (x >>u 4)) == 0x5a", "x == 0x5f", 8);
    check_to(&eng, "(x ^ (x >>u 4)) == (y ^ (y >>u 4))", "x == y", 8);
    // No preimage at all.
    check_to(&eng, "zext<16>(x) == 0x1ff", "false", 8);
    check_to(&eng, "concat(x, 1:4) == 0x12:12", "false", 8);
    check_to(&eng, "concat(x, 2:4) != 0x12:12", "x != 1", 8);
    // Zero or-trees are solved leaf by leaf.
    check_to(&eng, "((x ^ 1) | (x ^ 2)) == 0", "false", 8);
    check_to(
        &eng,
        "((x + 1) | (y * 3)) == 0",
        "(x == 0xff) & (y == 0)",
        8,
    );
    check_to(
        &eng,
        "((x + 1) | (y * 3)) != 0",
        "(x != 0xff) | (y != 0)",
        8,
    );
    // Orderings never go through a bijection that is not monotone.
    check_to(&eng, "x + 7 <u y + 7", "x + 7 <u y + 7", 8);
    check_to(&eng, "x * 3 <u 5", "x * 3 <u 5", 8);
    // The pass alone (fact folding would decide some of these first): no preimage.
    let only = engine(vec![Phase::Invert]);
    check_to(&only, "zext<16>(x + 1) != 0x1ff", "true", 8);
    check_to(&only, "sext<16>(x ^ 3) == 0x80", "false", 8);
    check_to(&only, "sext<16>(x ^ 3) == 0xff80", "x == 0x83", 8);
    check_to(&only, "(concat(x, 1:4) | y:12) == 0", "false", 8);
    check_to(&only, "(concat(x, 1:4) | y:12) != 0", "true", 8);
    check_to(
        &only,
        "(zext<16>(x) | concat(y, 0:8)) == 0",
        "(x == 0) & (y == 0)",
        8,
    );
    // `zext<16>(x)` is never `0xffff`; `sext<16>(x)` is, at one value.
    check_to(
        &only,
        "(zext<16>(x) & concat(y, 0xff:8)) == 0xffff",
        "false",
        8,
    );
    check_to(
        &only,
        "(sext<16>(x) & concat(y, 0xff:8)) == 0xffff",
        "(x == 0xff) & (y == 0xff)",
        8,
    );
}

/// Checks `src` simplifies to `want` at `w` bits under `eng`, assuming each of `under` true.
fn check_under(eng: &Engine, under: &[&str], src: &str, want: &str, w: u16) {
    let mut cx = Context::new();
    let o = ParseOptions::width(Width::new(w).unwrap());
    let e = cx.parse(src, &o).unwrap();
    let mut a = Assumptions::new();
    for u in under {
        let u = cx.parse(u, &o).unwrap();
        a.assume_true(&mut cx, u).unwrap();
    }
    let out = eng
        .run(&mut cx, &[e], Run::default().with_assumptions(&a))
        .unwrap()
        .roots[0];
    let want = cx
        .parse(want, &ParseOptions::width(cx.width(e).unwrap()))
        .unwrap();
    assert_eq!(
        out.expr,
        want,
        "{src} under {under:?} gave {}",
        cx.display(out.expr)
    );
}

#[test]
fn monotone_fixtures() {
    let eng = Engine::standard();
    let only = engine(vec![Phase::Invert]);
    // Layers monotone at every input, on both sides: the order they are monotone from.
    check_to(&eng, "zext<16>(x) <u zext<16>(y)", "x <u y", 8);
    check_to(&eng, "zext<16>(x) <s zext<16>(y)", "x <u y", 8);
    check_to(&eng, "sext<16>(x) <s sext<16>(y)", "x <s y", 8);
    check_to(&eng, "sext<16>(x) <u sext<16>(y)", "x <u y", 8);
    check_to(&eng, "concat(x, 3:4) <=u concat(y, 3:4)", "x <=u y", 8);
    check_to(&eng, "concat(x, 3:4) <s concat(y, 3:4)", "x <s y", 8);
    check_to(&eng, "concat(3:4, x) <s concat(3:4, y)", "x <u y", 8);
    check_to(&eng, "concat(9:4, x) <u concat(9:4, y)", "x <u y", 8);
    // Decreasing: the sides swap. Between orders: `x ^ smin`.
    check_to(&only, "~x <u ~y", "y <u x", 8);
    check_to(&only, "~x <=s ~y", "y <=s x", 8);
    check_to(&only, "(x ^ 0x80) <u (y ^ 0x80)", "x <s y", 8);
    check_to(&only, "(x ^ 0x80) <s (y ^ 0x80)", "x <u y", 8);
    check_to(&only, "(x + 0x80) <u (y + 0x80)", "x <s y", 8);
    // Chains, mixing orders along the way.
    check_to(&only, "zext<16>(~x) <s zext<16>(~y)", "y <u x", 8);
    check_to(&only, "~zext<16>(x) <u ~zext<16>(y)", "y <u x", 8);
    check_to(
        &only,
        "sext<16>(x ^ 0x80) <s sext<16>(y ^ 0x80)",
        "x <u y",
        8,
    );
    // Not without facts: a sum, a product or a shift may wrap.
    check_to(&only, "x + 7 <u y + 7", "x + 7 <u y + 7", 8);
    check_to(&only, "x * 3 <s y * 3", "x * 3 <s y * 3", 8);
    check_to(&only, "(x << 1) <u (y << 1)", "(x << 1) <u (y << 1)", 8);
    check_to(&only, "-x <u -y", "-x <u -y", 8);
    // ... and with them, when both ranges keep to one piece.
    check_under(
        &eng,
        &["x <u 100", "y <u 100"],
        "x + 7 <u y + 7",
        "x <u y",
        8,
    );
    check_under(
        &eng,
        &["x <u 100", "y <u 100", "k <u 10"],
        "x + k <u y + k",
        "x <u y",
        8,
    );
    check_under(
        &eng,
        &["x <u 100", "y <u 100"],
        "x - 7 <u y - 7",
        "x - 7 <u y - 7",
        8,
    );
    check_under(&eng, &["7 <=u x", "7 <=u y"], "x - 7 <u y - 7", "x <u y", 8);
    check_under(
        &eng,
        &["x <=u 50", "y <=u 50"],
        "50 - x <u 50 - y",
        "y <u x",
        8,
    );
    check_under(&eng, &["0 <u x", "0 <u y"], "-x <u -y", "y <u x", 8);
    check_under(
        &eng,
        &["x <s 50", "-50 <s x", "y <s 50", "-50 <s y"],
        "x + 7 <s y + 7",
        "x <s y",
        8,
    );
    check_under(
        &eng,
        &["x <s 40", "-40 <s x", "y <s 40", "-40 <s y"],
        "x * 3 <s y * 3",
        "x <s y",
        8,
    );
    check_under(
        &eng,
        &["x <s 40", "-40 <s x", "y <s 40", "-40 <s y"],
        "x * -3 <s y * -3",
        "y <s x",
        8,
    );
    check_under(&eng, &["x <u 80", "y <u 80"], "x * 3 <u y * 3", "x <u y", 8);
    check_under(
        &eng,
        &["x <u 60", "y <u 60"],
        "(x << 2) <u (y << 2)",
        "x <u y",
        8,
    );
    check_under(
        &eng,
        &["x <u 30", "y <u 30"],
        "(x << 2) <s (y << 2)",
        "x <s y",
        8,
    );
    check_under(
        &eng,
        &["x <u 60", "y <u 60"],
        "(x << 2) <s (y << 2)",
        "(x << 2) <s (y << 2)",
        8,
    );
    // Both on the wrapping piece is fine. One on each is not, unsigned; but read from the
    // signed order, `v + 10` is increasing on all of `[-6, 9]`.
    check_under(
        &eng,
        &["200 <=u x", "200 <=u y"],
        "x + 100 <u y + 100",
        "x <u y",
        8,
    );
    check_under(
        &only,
        &["x <u 10", "250 <=u y"],
        "x + 10 <u y + 10",
        "x <s y",
        8,
    );
    check_under(
        &only,
        &["200 <=u x", "x <u 210", "250 <=u y"],
        "x + 10 <u y + 10",
        "x + 10 <u y + 10",
        8,
    );
    // Against a constant: a threshold, or decided.
    check_to(&only, "zext<16>(x) <u 300", "true", 8);
    check_to(&only, "zext<16>(x) <u 100", "x <u 100", 8);
    check_to(&only, "zext<16>(x) <s 0xffff", "false", 8);
    check_to(&only, "sext<16>(x) <s 5", "x <s 5", 8);
    check_to(&only, "sext<16>(x) <=s 0x7f", "true", 8);
    check_to(&only, "sext<16>(x) <s 0xff80", "false", 8);
    check_to(&only, "~x <u 5", "0xfb <=u x", 8);
    check_to(&only, "5 <u ~x", "x <u 0xfa", 8);
    check_to(&only, "(x ^ 0x80) <u 0x90", "x <s 0x10", 8);
    check_to(&only, "0x90 <=u (x ^ 0x80)", "0x10 <=s x", 8);
    check_to(&only, "concat(x, 3:4) <u 0x5a3", "x <u 0x5a", 8);
    check_to(&only, "concat(x, 3:4) <u 0x5a4", "x <u 0x5b", 8);
    check_to(&only, "concat(x, 3:4) <u 0x5a2", "x <u 0x5a", 8);
    check_to(&only, "(x << 1) <u 10", "(x << 1) <u 10", 8);
    check_under(&eng, &["x <u 100"], "x + 7 <u 50", "x <u 43", 8);
    check_under(&eng, &["x <u 100"], "x + 7 <u 200", "true", 8);
    check_under(&eng, &["x <u 60"], "(x << 2) <u 100", "x <u 25", 8);
    check_under(
        &eng,
        &["x <s 40", "-40 <s x"],
        "x * 3 <s -30",
        "x <s -10",
        8,
    );
}

#[test]
fn monotone_trees() {
    let eng = Engine::standard();
    let only = engine(vec![Phase::Invert]);
    // A select passes its order to its arms: the leaves of both sides are peeled together.
    check_to(
        &only,
        "select(c, zext<16>(x), zext<16>(y)) <u zext<16>(z)",
        "select(c, x, y) <u z",
        8,
    );
    check_to(
        &eng,
        "select(zext<16>(x) <u zext<16>(y), zext<16>(x), zext<16>(y)) <u zext<16>(z)",
        "select(x <u y, x, y) <u z",
        8,
    );
    check_to(&only, "select(c, ~x, ~y) <u ~z", "z <u select(c, x, y)", 8);
    check_to(
        &only,
        "select(c, select(d, ~x, ~y), ~w) <=s ~z",
        "z <=s select(c, select(d, x, y), w)",
        8,
    );
    check_to(
        &only,
        "select(c, x ^ 0x80, y ^ 0x80) <s (z ^ 0x80)",
        "select(c, x, y) <u z",
        8,
    );
    // A constant leaf stands for its exact preimage, or stops the peeling.
    check_to(
        &only,
        "select(c, zext<16>(x), 5) <u zext<16>(z)",
        "select(c, x, 5) <u z",
        8,
    );
    check_to(
        &only,
        "select(c, zext<16>(x), 300) <u zext<16>(z)",
        "select(c, zext<16>(x), 300) <u zext<16>(z)",
        8,
    );
    check_to(
        &only,
        "select(c, ~x, 5) <u ~z",
        "z <u select(c, x, 0xfa)",
        8,
    );
    // ... but not through a layer monotone only on the facts' range.
    check_under(
        &only,
        &["x <u 10", "z <u 10"],
        "select(c, x + 1, 5) <u z + 1",
        "select(c, x + 1, 5) <u z + 1",
        8,
    );
    check_under(
        &only,
        &["x <u 10", "y <u 10", "z <u 10"],
        "select(c, x + 1, y + 1) <u z + 1",
        "select(c, x, y) <u z",
        8,
    );
    // Different layers, or one leaf that is not a layer: nothing.
    check_to(
        &only,
        "select(c, zext<16>(x), sext<16>(y)) <u zext<16>(z)",
        "select(c, zext<16>(x), sext<16>(y)) <u zext<16>(z)",
        8,
    );
    check_to(
        &only,
        "select(c, zext<16>(x), w) <u zext<16>(z)",
        "select(c, zext<16>(x), w) <u zext<16>(z)",
        8,
    );
    // Equalities through selects: an injective layer, constants by preimage.
    check_to(
        &only,
        "select(c, x + 1, y + 1) == z + 1",
        "select(c, x, y) == z",
        8,
    );
    check_to(
        &only,
        "select(c, x * 3, 7) != y * 3",
        "select(c, x, 0xad) != y",
        8,
    );
    check_to(
        &only,
        "select(c, zext<16>(x), 300) == zext<16>(z)",
        "select(c, zext<16>(x), 300) == zext<16>(z)",
        8,
    );
    check_to(
        &only,
        "select(c, x ^ (x >>u 4), y ^ (y >>u 4)) == z ^ (z >>u 4)",
        "select(c, x, y) == z",
        8,
    );
}

/// Random chains of layers monotone where the facts allow (sums, differences, products and
/// shifts by a constant or a shared symbol, complements, negations, `^ smin`, casts and
/// concatenations), compared through every ordering on both sides or against a constant, under
/// random range assumptions on the symbols: the result agrees with the input on every
/// assignment the assumptions admit.
#[test]
fn monotone_is_sound_under_ranges() {
    let eng = Engine::standard();
    let only = engine(vec![Phase::Invert]);
    let mut g = generator(0x3030_0001);
    let (mut changed, mut by_pass) = (0, 0);
    for _ in 0..800 {
        let mut cx = Context::new();
        let w = 3 + g.rng.below(3) as u16;
        let wd = Width::new(w).unwrap();
        let (x, y, k) = (
            cx.symbol("x", wd).unwrap(),
            cx.symbol("y", wd).unwrap(),
            cx.symbol("k", wd).unwrap(),
        );
        let (mut e, mut ew) = (x, w);
        for _ in 0..1 + g.rng.below(3) {
            let ewd = Width::new(ew).unwrap();
            let p = if ew == w && g.rng.chance(1, 3) {
                k
            } else {
                let v = g.constant(ew);
                cx.constant(&v).unwrap()
            };
            e = match g.rng.below(10) {
                0 => cx.bin(BinOp::Add, e, p).unwrap(),
                1 => cx.bin(BinOp::Sub, e, p).unwrap(),
                2 => cx.bin(BinOp::Sub, p, e).unwrap(),
                3 => cx.bin(BinOp::Mul, e, p).unwrap(),
                4 => {
                    let s = cx.constant_u64(ewd, g.rng.below(3)).unwrap();
                    cx.bin(BinOp::Shl, e, s).unwrap()
                }
                5 => cx.un(UnOp::Not, e).unwrap(),
                6 => cx.un(UnOp::Neg, e).unwrap(),
                7 => {
                    let m = cx.constant(&BitVec::smin(ewd)).unwrap();
                    cx.bin(BinOp::Xor, e, m).unwrap()
                }
                _ if ew >= 7 => e,
                _ => {
                    let to = Width::new(ew + 1 + g.rng.below(2) as u16).unwrap();
                    let r = match g.rng.below(4) {
                        0 => cx.zext(e, to).unwrap(),
                        1 => cx.sext(e, to).unwrap(),
                        2 => {
                            let c = cx.constant(&g.constant(to.bits() - ew)).unwrap();
                            cx.concat(e, c).unwrap()
                        }
                        _ => {
                            let c = cx.constant(&g.constant(to.bits() - ew)).unwrap();
                            cx.concat(c, e).unwrap()
                        }
                    };
                    ew = to.bits();
                    r
                }
            };
        }
        let op =
            [CmpOpExt::Ult, CmpOpExt::Ule, CmpOpExt::Slt, CmpOpExt::Sle][g.rng.below(4) as usize];
        let root = match g.rng.below(6) {
            0 => {
                let fy = cx.substitute(&[e], &[(x, y)]).unwrap()[0];
                cx.cmp(op, e, fy).unwrap()
            }
            // Select trees over the chain at `x`, `y`, `k` and constants, compared by an
            // ordering or an equality.
            4 | 5 => {
                let fy = cx.substitute(&[e], &[(x, y)]).unwrap()[0];
                let fk = cx.substitute(&[e], &[(x, k)]).unwrap()[0];
                let ewd = Width::new(ew).unwrap();
                let leaf = |cx: &mut Context, g: &mut Gen| match g.rng.below(4) {
                    0 => e,
                    1 => fy,
                    2 => fk,
                    _ => {
                        let v = g.constant(ewd.bits());
                        cx.constant(&v).unwrap()
                    }
                };
                let cond = |cx: &mut Context, g: &mut Gen| {
                    let a = [x, y, k][g.rng.below(3) as usize];
                    let b = [x, y, k][g.rng.below(3) as usize];
                    cx.cmp(CmpOpExt::Ult, a, b).unwrap()
                };
                let c1 = cond(&mut cx, &mut g);
                let (l1, l2) = (leaf(&mut cx, &mut g), leaf(&mut cx, &mut g));
                let a = cx.select(c1, l1, l2).unwrap();
                let b = if g.rng.chance(1, 2) {
                    leaf(&mut cx, &mut g)
                } else {
                    let c2 = cond(&mut cx, &mut g);
                    let (l3, l4) = (leaf(&mut cx, &mut g), leaf(&mut cx, &mut g));
                    cx.select(c2, l3, l4).unwrap()
                };
                let op = if g.rng.chance(1, 3) {
                    [CmpOpExt::Eq, CmpOpExt::Ne][g.rng.below(2) as usize]
                } else {
                    op
                };
                cx.cmp(op, a, b).unwrap()
            }
            1 => {
                let c = cx.constant(&g.constant(ew)).unwrap();
                cx.cmp(op, c, e).unwrap()
            }
            _ => {
                let c = cx.constant(&g.constant(ew)).unwrap();
                cx.cmp(op, e, c).unwrap()
            }
        };
        // Range assumptions: each symbol bounded on either side, in either order, or not.
        let mut preds = Vec::new();
        for s in [x, y, k] {
            for _ in 0..g.rng.below(3) {
                let c = cx.constant(&g.constant(w)).unwrap();
                let op = [CmpOpExt::Ult, CmpOpExt::Ule, CmpOpExt::Slt, CmpOpExt::Sle]
                    [g.rng.below(4) as usize];
                preds.push(if g.rng.chance(1, 2) {
                    cx.cmp(op, s, c).unwrap()
                } else {
                    cx.cmp(op, c, s).unwrap()
                });
            }
        }
        let mut a = Assumptions::new();
        for &p in &preds {
            a.assume_true(&mut cx, p).unwrap();
        }
        if a.is_infeasible() {
            continue;
        }
        for engine in [&eng, &only] {
            let out = engine
                .run(&mut cx, &[root], Run::default().with_assumptions(&a))
                .unwrap();
            let r = out.roots[0];
            assert_eq!(out.stats.rejected, 0, "{}", cx.display(root));
            if !r.changed {
                continue;
            }
            changed += 1;
            by_pass += u64::from(core::ptr::eq(engine, &only));
            for case in 0..1u64 << (3 * w) {
                let vals = [
                    ("x", case & ((1 << w) - 1)),
                    ("y", (case >> w) & ((1 << w) - 1)),
                    ("k", case >> (2 * w)),
                ];
                let env = FnEnv(|key: &crate::SymbolKey, _| {
                    vals.iter()
                        .find(|(n, _)| *key == crate::SymbolKey::from(*n))
                        .map(|(_, v)| BitVec::wrapping_from_u64(wd, *v))
                });
                let pv = cx.eval(&preds, &env).unwrap();
                if pv.iter().any(BitVec::is_zero) {
                    continue;
                }
                let v = cx.eval(&[root, r.expr], &env).unwrap();
                assert_eq!(
                    v[0],
                    v[1],
                    "{} -> {} under {:?} at {vals:?}",
                    cx.display(root),
                    cx.display(r.expr),
                    preds
                        .iter()
                        .map(|&p| cx.display(p).to_string())
                        .collect::<Vec<_>>()
                );
            }
        }
    }
    assert!(changed > 300 && by_pass > 100, "{changed} {by_pass}");
}

#[test]
fn invert_cancels_and_solves_permutation_polynomials() {
    let eng = Engine::standard();
    // `2x² + x`, `x·(2x + 1)` and `4x³ + 2x² + 3x` are bijections (bit `k` is `x_k` flipped
    // by lower bits); `x² + x` is not (it is always even).
    check_to(&eng, "2 * x * x + x == 2 * y * y + y", "x == y", 17);
    check_to(&eng, "x * (2 * x + 1) == y * (2 * y + 1)", "x == y", 17);
    check_to(
        &eng,
        "4 * x * x * x + 2 * x * x + 3 * x == 4 * y * y * y + 2 * y * y + 3 * y",
        "x == y",
        64,
    );
    check_to(&eng, "2 * x * x + x == 2 * y * y + y", "x == y", 64);
    check_to(&eng, "x * x + x == y * y + y", "x * x + x == y * y + y", 17);
    // Solved: 2·72115² + 72115 = 5, and 2·0x26c89aff803319b3² + … = 5, modulo the width.
    check_to(&eng, "2 * x * x + x == 5", "x == 72115", 17);
    check_to(&eng, "2 * x * x + x == 5", "x == 0x26c89aff803319b3", 64);
    // A truncation of a map that is a bijection only at the truncated width: compared there.
    check_to(
        &eng,
        "trunc<17>(2 * x * x + x) == trunc<17>(2 * y * y + y)",
        "trunc<17>(x) == trunc<17>(y)",
        64,
    );
    check_to(
        &eng,
        "trunc<17>(x * 0x1234567 + 9) == 77",
        "trunc<17>(x) == 110364",
        64,
    );
    check_to(
        &eng,
        "trunc<17>(x * x + x) == trunc<17>(y * y + y)",
        "trunc<17>(x * x + x) == trunc<17>(y * y + y)",
        64,
    );
}

#[test]
fn invert_commits_like_a_rule_through_sharing() {
    // The hash stays live as another root; its comparison is solved anyway.
    let eng = Engine::standard();
    let mut cx = Context::new();
    let o = ParseOptions::width(Width::W64);
    let h = cx
        .parse(
            "let m = (x ^ 0x1234) * 0x87c37b91114253d5; m ^ (m >>u 29)",
            &o,
        )
        .unwrap();
    let c = cx.constant_u64(Width::W64, 0xfeed).unwrap();
    let e = cx.cmp(CmpOpExt::Eq, h, c).unwrap();
    let out = eng.run(&mut cx, &[h, e], Run::default()).unwrap();
    assert!(!out.roots[0].changed);
    let r = out.roots[1].expr;
    let crate::View::Cmp(crate::CmpOp::Eq, a, k) = cx.view(r).unwrap() else {
        panic!("{}", cx.display(r));
    };
    assert_eq!(cx.display(a).to_string(), "x");
    // And the constant is the preimage.
    let v = cx.as_const(k).unwrap().unwrap();
    let env = FnEnv(|_: &crate::SymbolKey, _| Some(v));
    assert_eq!(cx.eval(&[h], &env).unwrap()[0].to_u64(), Some(0xfeed));
}

#[test]
fn invert_relies_on_the_constraints_it_uses() {
    let eng = Engine::standard();
    let mut cx = Context::new();
    let o = ParseOptions::width(Width::W32);
    let e = cx.parse("x * k == y * k", &o).unwrap();
    // `k` is not known to be odd: nothing to cancel.
    assert!(!eng.simplify(&mut cx, e).unwrap().changed);
    let odd = cx.parse("(k & 1) == 1", &o).unwrap();
    let other = cx.parse("x <u 100", &o).unwrap();
    let mut a = Assumptions::new();
    let u = a.assume_true(&mut cx, other).unwrap();
    let id = a.assume_true(&mut cx, odd).unwrap();
    let out = eng
        .run(&mut cx, &[e], Run::default().with_assumptions(&a))
        .unwrap()
        .roots[0];
    assert_eq!(cx.display(out.expr).to_string(), "x == y");
    assert!(
        out.relies_on.may_use(id) && !out.relies_on.may_use(u),
        "{:?}",
        out.relies_on
    );
    // So does the proof query.
    let (x, p) = (cx.parse("x", &o).unwrap(), cx.parse("x * k", &o).unwrap());
    let q = crate::Query::Bijective { e: p, of: x };
    assert_eq!(cx.prove(q).unwrap(), crate::Truth::Unknown);
    let proof = cx.prove_under(q, &a).unwrap();
    assert_eq!(proof.truth, crate::Truth::True);
    assert!(proof.relies_on.may_use(id) && !proof.relies_on.may_use(u));
}

#[test]
fn invert_obeys_budgets_and_the_host_veto() {
    let mut cx = Context::new();
    let o = ParseOptions::width(Width::W64);
    let src = "let m = (x ^ 0x1234) * 0x87c37b91114253d5; \
               let s = m ^ ((m >>u 32) >>u (m >>u 60)); s * 0x87c37b91114253d5 == 0x42";
    let e = cx.parse(src, &o).unwrap();
    let eng = engine(vec![Phase::Invert]);
    // Every budget from nothing up: a sound result, never a panic.
    let mut rng = Rng(5);
    let mut done = false;
    for work in (0..3000).step_by(61) {
        cx.memo.clear();
        let run = Run::default().with_per_call(Budget::default().with_pass_work(work));
        let r = eng.run(&mut cx, &[e], run).unwrap().roots[0];
        assert!(equivalent(&mut cx, e, r.expr, &mut rng));
        done |= r.end == End::Completed && r.changed;
    }
    assert!(done);
    // The host can refuse the rewrite.
    struct No;
    impl Hooks for No {
        fn admit(&self, _: &Context, _: Expr, _: Expr, by: crate::engine::By<'_>) -> bool {
            !matches!(by, crate::engine::By::Pass("invert"))
        }
    }
    cx.memo.clear();
    let r = eng
        .run(&mut cx, &[e], Run::default().with_hooks(&No))
        .unwrap();
    assert!(!r.roots[0].changed);
    assert!(r.stats.hook_vetoes > 0);
}

/// The linear pass takes a constant into a term (`k·a + k = −k·~a`) when that is smaller,
/// doubles as `a << 1`, starts a sum of subtractions from its constant or a product by a
/// negative coefficient, reads `concat(trunc(x), c)` as `x·2^|c| + c`, and a constant always
/// replaces what it folds.
#[test]
fn linear_emissions() {
    let eng = engine(vec![Phase::Linear]);
    for (src, want) in [
        ("-x - 1", "~x"),
        ("(y << 2) + 4", "~y * -4"),
        ("-2 * y - 2", "y * -2 - 2"),
        ("2 * x + y", "x * 2 + y"),
        ("5 - 3 * x", "5 - x * 3"),
        ("-(y * 3) - z * 5", "y * -3 - z * 5"),
        ("concat(trunc<62>(y), 3:2) - 6 * y - 5", "~y << 1"),
    ] {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::W64);
        let e = cx.parse(src, &o).unwrap();
        let out = eng.simplify(&mut cx, e).unwrap();
        assert_eq!(cx.display(out.expr).to_string(), want, "{src}");
    }
    // A constant replaces the node it is, even beside a shared term: `~x + x` is −1.
    let eng = Engine::builder()
        .builtin()
        .strategy(Strategy::deobfuscate())
        .build()
        .unwrap();
    let mut cx = Context::new();
    let o = ParseOptions::width(Width::W64);
    let e = cx.parse("(~x ^ (x + ~x)) + y", &o).unwrap();
    let out = eng.simplify(&mut cx, e).unwrap();
    assert_eq!(cx.display(out.expr).to_string(), "x + y");
}

/// `a + a` is `a << 1` for the facts: its low bit is known.
#[test]
fn doubling_knows_its_low_bit() {
    let mut cx = Context::new();
    let o = ParseOptions::width(Width::W64);
    let e = cx.parse("x + x", &o).unwrap();
    let f = cx.facts(e).unwrap();
    assert_eq!(f.known().known_zero().bit(0), Some(true));
}

/// The MBA service with the native solver, on bitwright's own evidence (the `bw-nf` setup).
#[cfg(feature = "mba")]
mod native_mba {
    use super::*;
    use crate::Bounded;
    use crate::mba::{MbaConfig, MbaTrust, NormalFormSolver};
    use std::sync::Arc;

    fn nf_engine() -> Engine {
        let trust = MbaTrust::default().with_backend_certificates(false);
        Engine::builder()
            .builtin()
            .strategy(Strategy::deobfuscate().with_mba(MbaConfig::default().with_trust(trust)))
            .mba_solver(Arc::new(NormalFormSolver::default()))
            .build()
            .unwrap()
    }

    fn size(cx: &mut Context, e: Expr) -> u32 {
        match cx.dag_size(&[e], u32::MAX).unwrap() {
            Bounded::Exact(n) => n,
            _ => u32::MAX,
        }
    }

    /// A sum of sixty null products (`2^57·c·x(x − 1)⋯(x − 7)·y^j` is 0 at 64 bits: 8! has
    /// the other seven factors of two) and `3·x`: the question is asked at the top of the sum
    /// first, and never at every link, so it is answered within the call budget.
    #[test]
    fn chains_are_asked_at_their_tops() {
        let mut src = String::from("x * 3");
        for i in 0..60u64 {
            let c = (2 * i + 1) << 57;
            src.push_str(&format!(" + {c} * x"));
            for d in 1..8 {
                src.push_str(&format!(" * (x - {d})"));
            }
            for _ in 0..i % 3 {
                src.push_str(" * y");
            }
        }
        let mut cx = Context::new();
        let e = cx.parse(&src, &ParseOptions::width(Width::W64)).unwrap();
        let out = nf_engine().run(&mut cx, &[e], Run::default()).unwrap();
        let r = out.roots[0];
        assert_eq!(r.end, End::Completed);
        // `x * 3` or `x + x + x`: three nodes either way.
        assert_eq!(size(&mut cx, r.expr), 3, "{}", cx.display(r.expr));
        assert!(out.stats.mba.calls < 400, "{}", out.stats.mba.calls);
    }

    /// A link of a chain is asked when its user is not: a shift by a variable is no MBA
    /// operator, and a sum over more variables than the limits allow is refused.
    #[test]
    fn links_are_asked_under_users_that_are_not() {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::W64);
        let e = cx
            .parse("((x & y) * (x | y) + (x & ~y) * (~x & y)) << z", &o)
            .unwrap();
        let out = nf_engine().simplify(&mut cx, e).unwrap();
        assert_eq!(cx.display(out.expr).to_string(), "x * y << z");
        let lim = crate::mba::MbaLimits::default().with_max_vars(2);
        let trust = MbaTrust::default().with_backend_certificates(false);
        let cfg = MbaConfig::default().with_trust(trust).with_limits(lim);
        let eng = Engine::builder()
            .builtin()
            .strategy(Strategy::deobfuscate().with_mba(cfg))
            .mba_solver(Arc::new(NormalFormSolver::default()))
            .build()
            .unwrap();
        let mut cx = Context::new();
        let e = cx
            .parse("(x & y) * (x | y) + (x & ~y) * (~x & y) + z", &o)
            .unwrap();
        let out = eng.simplify(&mut cx, e).unwrap();
        assert_eq!(cx.display(out.expr).to_string(), "x * y + z");
    }

    /// Accurate use counts: a replaced node (and what only it used) no longer counts as a user,
    /// so a smaller answer is committed (10 nodes for 13).
    #[test]
    fn replaced_nodes_do_not_count_as_users() {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::W64);
        let e = cx
            .parse("(~x & y) * -15 - ((~y & x) << 3) - 7", &o)
            .unwrap();
        let out = nf_engine().simplify(&mut cx, e).unwrap();
        assert!(size(&mut cx, out.expr) <= 10, "{}", cx.display(out.expr));
    }
}

// ----- bit subsets, masked bitwise functions, and both readings of a condition -----------------

/// A term of width `w` (at most 6) over `x`, `y` of 6 bits: their extractions, constants,
/// bitwise operations, and extractions, extensions and concatenations of other terms.
fn subset_term(g: &mut Gen, cx: &mut Context, w: u16, depth: u32) -> Expr {
    let width = Width::new(w).unwrap();
    if depth == 0 || g.rng.chance(1, 4) {
        if g.rng.chance(1, 4) {
            let v = g.constant(w);
            return cx.constant(&v).unwrap();
        }
        let s = cx
            .symbol(["x", "y"][g.rng.below(2) as usize], Width::new(6).unwrap())
            .unwrap();
        let lo = g.rng.below(u64::from(6 - w) + 1) as u16;
        return cx.extract(s, lo, width).unwrap();
    }
    let d = depth - 1;
    match g.rng.below(9) {
        0 => {
            let a = subset_term(g, cx, w, d);
            cx.un(UnOp::Not, a).unwrap()
        }
        1..=4 => {
            let a = subset_term(g, cx, w, d);
            let b = subset_term(g, cx, w, d);
            let op = [BinOp::And, BinOp::And, BinOp::Or, BinOp::Xor][g.rng.below(4) as usize];
            cx.bin(op, a, b).unwrap()
        }
        5 if w > 1 => {
            let from = 1 + g.rng.below(u64::from(w) - 1) as u16;
            let a = subset_term(g, cx, from, d);
            cx.zext(a, width).unwrap()
        }
        6 if w < 6 => {
            let from = w + 1 + g.rng.below(u64::from(6 - w)) as u16;
            let lo = g.rng.below(u64::from(from - w) + 1) as u16;
            let a = subset_term(g, cx, from, d);
            cx.extract(a, lo, width).unwrap()
        }
        7 if w > 1 => {
            let h = 1 + g.rng.below(u64::from(w) - 1) as u16;
            let a = subset_term(g, cx, h, d);
            let b = subset_term(g, cx, w - h, d);
            cx.concat(a, b).unwrap()
        }
        _ => subset_term(g, cx, w, d),
    }
}

/// Every value of `e` over `x`, `y` of 6 bits.
fn values_6x6(cx: &mut Context, e: Expr) -> Vec<BitVec> {
    let w6 = Width::new(6).unwrap();
    (0..1u64 << 12)
        .map(|i| {
            let (xv, yv) = (
                BitVec::wrapping_from_u64(w6, i & 63),
                BitVec::wrapping_from_u64(w6, i >> 6),
            );
            let env = FnEnv(|k: &crate::SymbolKey, _| match k.to_string().as_str() {
                "x" => Some(xv),
                "y" => Some(yv),
                _ => None,
            });
            cx.eval(&[e], &env).unwrap()[0]
        })
        .collect()
}

/// What the order reading takes as bit subsets is: whenever `submask(b, a)` holds, no value of
/// `b` has a bit `a`'s lacks (exhaustively), and `zero_bits` likewise; and both hold often.
#[test]
fn bit_subsets_are_sound() {
    bit_subsets_are_sound_cases(1000);
}

/// [`bit_subsets_are_sound`] over 3000 random cases.
#[test]
#[ignore = "heavy: run with --release -- --ignored"]
fn bit_subsets_are_sound_heavy() {
    bit_subsets_are_sound_cases(3000);
}

fn bit_subsets_are_sound_cases(cases: usize) {
    let mut g = generator(0x0005_b5e7);
    let (mut subsets, mut zeros) = (0, 0);
    for _ in 0..cases {
        let mut cx = Context::new();
        let w = 1 + g.rng.below(6) as u16;
        let width = Width::new(w).unwrap();
        let a = subset_term(&mut g, &mut cx, w, 3);
        // Often a term built from the other, so that the relation is there to find.
        let b = match g.rng.below(4) {
            0 => {
                let t = subset_term(&mut g, &mut cx, w, 2);
                cx.bin(BinOp::And, a, t).unwrap()
            }
            1 => {
                let wide = subset_term(&mut g, &mut cx, w, 2);
                let t = cx.bin(BinOp::And, a, wide).unwrap();
                let t = cx.zext(t, Width::new(6).unwrap()).unwrap();
                let u = subset_term(&mut g, &mut cx, 6, 1);
                let t = cx.bin(BinOp::And, t, u).unwrap();
                cx.extract(t, 0, width).unwrap()
            }
            _ => subset_term(&mut g, &mut cx, w, 3),
        };
        let (ai, bi) = (cx.id(a).unwrap(), cx.id(b).unwrap());
        let mut left = super::order::SUBMASK_STEPS;
        if super::order::submask(&cx, bi, 0, ai, 0, w, &mut left) {
            let (va, vb) = (values_6x6(&mut cx, a), values_6x6(&mut cx, b));
            for (x, y) in vb.iter().zip(&va) {
                let extra =
                    BitVec::bin_unchecked(BinOp::And, x, &BitVec::un_unchecked(UnOp::Not, y));
                assert!(
                    extra.is_zero(),
                    "W={w}: {} not within {}",
                    cx.display(b),
                    cx.display(a)
                );
            }
            subsets += 1;
        }
        let mut left = super::order::SUBMASK_STEPS;
        let lo = g.rng.below(u64::from(w)) as u16;
        let len = 1 + g.rng.below(u64::from(w - lo)) as u16;
        if super::order::zero_bits(&cx, bi, lo, len, &mut left) {
            for v in values_6x6(&mut cx, b) {
                let part = v.extract(lo, Width::new(len).unwrap()).unwrap();
                assert!(
                    part.is_zero(),
                    "W={w}: bits {lo}+{len} of {}",
                    cx.display(b)
                );
            }
            zeros += 1;
        }
    }
    assert!(
        subsets > 600 * cases / 3000 && zeros > 300 * cases / 3000,
        "{subsets} subsets, {zeros} zero windows"
    );
}

/// Comparisons and selects of bit-subset terms through the order reading: sound
/// (exhaustively), idempotent, and many decided.
#[test]
fn order_through_bit_subsets_is_sound_and_idempotent() {
    use crate::CmpOpExt;
    let eng = engine(vec![Phase::Compares]);
    let mut g = generator(0x0de6);
    let mut rng = Rng(47);
    let mut changed = 0;
    for _ in 0..2000 {
        let mut cx = Context::new();
        let w = 1 + g.rng.below(6) as u16;
        let a = subset_term(&mut g, &mut cx, w, 3);
        let b = if g.rng.chance(1, 2) {
            let t = subset_term(&mut g, &mut cx, w, 2);
            let t = cx.bin(BinOp::And, a, t).unwrap();
            let s = cx.symbol("x", Width::new(6).unwrap()).unwrap();
            let u = cx.zext(t, Width::new(6).unwrap()).unwrap();
            let u = cx.bin(BinOp::And, u, s).unwrap();
            cx.trunc(u, Width::new(w).unwrap()).unwrap()
        } else {
            subset_term(&mut g, &mut cx, w, 3)
        };
        let ops = [
            CmpOpExt::Ult,
            CmpOpExt::Ule,
            CmpOpExt::Slt,
            CmpOpExt::Sle,
            CmpOpExt::Eq,
        ];
        let op = ops[g.rng.below(ops.len() as u64) as usize];
        let (p, q) = if g.rng.chance(1, 2) { (a, b) } else { (b, a) };
        let c = cx.cmp(op, p, q).unwrap();
        let e = if g.rng.chance(1, 3) {
            cx.select(c, p, q).unwrap()
        } else {
            c
        };
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        let r = out.roots[0];
        assert!(
            equivalent(&mut cx, e, r.expr, &mut rng),
            "W={w}: {} vs {}",
            cx.display(e),
            cx.display(r.expr)
        );
        changed += u64::from(r.changed);
        cx.memo.clear();
        let again = eng.run(&mut cx, &[r.expr], Run::default()).unwrap();
        assert!(
            !again.roots[0].changed,
            "not idempotent: {}",
            cx.display(r.expr)
        );
    }
    assert!(changed > 400, "{changed}");
}

/// A random bitwise expression over `a`, `b`, `c` and constants from a small pool.
fn masked_expr(g: &mut Gen, cx: &mut Context, w: u16, depth: u32, pool: &[BitVec]) -> Expr {
    let width = Width::new(w).unwrap();
    if depth == 0 || g.rng.chance(1, 5) {
        return match g.rng.below(5) {
            0 | 1 => {
                let v = pool[g.rng.below(pool.len() as u64) as usize];
                cx.constant(&v).unwrap()
            }
            k => cx.symbol(["a", "b", "c"][(k - 2) as usize], width).unwrap(),
        };
    }
    let d = depth - 1;
    match g.rng.below(4) {
        0 => {
            let x = masked_expr(g, cx, w, d, pool);
            cx.un(UnOp::Not, x).unwrap()
        }
        k => {
            let x = masked_expr(g, cx, w, d, pool);
            let y = masked_expr(g, cx, w, d, pool);
            cx.bin([BinOp::And, BinOp::Or, BinOp::Xor][(k - 1) as usize], x, y)
                .unwrap()
        }
    }
}

/// Bitwise functions with constants, by classes of bit positions: sound (exhaustively at small
/// widths), idempotent, and often smaller.
#[test]
fn masked_bitwise_is_sound_and_idempotent() {
    let eng = engine(vec![Phase::Bitwise]);
    let mut g = generator(0xb17_3a5c);
    let mut rng = Rng(22);
    let mut changed = 0;
    for i in 0..2000 {
        let mut cx = Context::new();
        let w = if i % 10 == 0 {
            [8, 64, 129][g.rng.below(3) as usize]
        } else {
            1 + g.rng.below(4) as u16
        };
        let pool: Vec<BitVec> = (0..1 + g.rng.below(4)).map(|_| g.constant(w)).collect();
        let e = masked_expr(&mut g, &mut cx, w, 5, &pool);
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        let r = out.roots[0];
        assert_eq!(out.stats.rejected, 0);
        assert!(
            equivalent(&mut cx, e, r.expr, &mut rng),
            "W={w}: {} vs {}",
            cx.display(e),
            cx.display(r.expr)
        );
        changed += u64::from(r.changed);
        cx.memo.clear();
        let again = eng.run(&mut cx, &[r.expr], Run::default()).unwrap();
        assert!(
            !again.roots[0].changed,
            "not idempotent: {}",
            cx.display(r.expr)
        );
    }
    assert!(changed > 500, "{changed}");
}

/// `p == q` of bitwise functions with constants is decided by their tables: soundly, and for
/// every pair that is the same function written twice.
#[test]
fn masked_equalities_are_decided_soundly() {
    use crate::CmpOpExt;
    let eng = engine(vec![Phase::Compares]);
    let rewrite = engine(vec![Phase::Bitwise]);
    let mut g = generator(0xe9_3a5c);
    let mut rng = Rng(23);
    let mut decided = 0;
    for _ in 0..2000 {
        let mut cx = Context::new();
        let w = 2 + g.rng.below(3) as u16;
        let pool: Vec<BitVec> = (0..1 + g.rng.below(3)).map(|_| g.constant(w)).collect();
        let p = masked_expr(&mut g, &mut cx, w, 4, &pool);
        // The same function in another form, or another function.
        let q = if g.rng.chance(1, 2) {
            let r = rewrite.run(&mut cx, &[p], Run::default()).unwrap().roots[0].expr;
            let x = cx.symbol("b", Width::new(w).unwrap()).unwrap();
            let y = cx.bin(BinOp::Xor, r, x).unwrap();
            cx.bin(BinOp::Xor, y, x).unwrap()
        } else {
            masked_expr(&mut g, &mut cx, w, 4, &pool)
        };
        let op = if g.rng.chance(1, 2) {
            CmpOpExt::Eq
        } else {
            CmpOpExt::Ne
        };
        let e = cx.cmp(op, p, q).unwrap();
        let r = eng.run(&mut cx, &[e], Run::default()).unwrap().roots[0];
        assert!(
            equivalent(&mut cx, e, r.expr, &mut rng),
            "W={w}: {} vs {}",
            cx.display(e),
            cx.display(r.expr)
        );
        decided += u32::from(cx.const_val(cx.id(r.expr).unwrap()).is_some());
    }
    assert!(decided > 900, "{decided}");
}

#[test]
fn masked_bitwise_fixtures() {
    let eng = Engine::standard();
    for w in [8, 32, 64] {
        let (zero, f) = (format!("0:{w}"), "0:1".to_string());
        for (src, want) in [
            // Four atoms, one of them a mask: a function of three per class.
            ("~a & (~b & (y & 5))", "~(a | b) & y & 5"),
            ("(~a & (~b & (y & 5))) ^ (~(a | b) & y & 5)", &zero),
            // The same function written two ways is one value.
            ("~y & 37 != (y & 37) ^ 37", &f),
            ("~a & (~b & 37) & y != ~a & (~b & (y & 37))", &f),
            ("(x & y ^ x) & 37 != ~y & (x & 37)", &f),
            // Complementary on the mask's bits: never equal.
            ("(y & 5) == (~y & 5) | 2", &f),
        ] {
            assert_eq!(simplify(&eng, src, w), want, "{src} at {w} bits");
        }
        // Different functions stay.
        assert!(!simplify(&eng, "x & 5 == x & 6", w).contains(':'));
    }
    // Each two-class form, by the bitwise pass alone.
    let eng = engine(vec![Phase::Bitwise]);
    for (src, want) in [
        ("(y & a & 5) | (y & 5 & ~a)", "y & 5"),
        ("(~(y & a) & 5) | (y & a & 250)", "a & y ^ 5"),
        ("(y & a) | 5", "a & y | 5"),
        ("(~(y & a) & 5) | (a & 250)", "(~a | y) & 5 ^ a"),
    ] {
        assert_eq!(simplify(&eng, src, 8), want, "{src}");
    }
}

/// The borrow of an XOR or an OR with a constant done in limbs (`x ^ c` written `x + c − 2·(x
/// & c)`, its low limb compared with the sum's): the two low limbs differ by the conjunction,
/// which stays below the limb's top bit, so the high digit of the sum less the borrow is the
/// high digit of `x`.
#[cfg(feature = "prove")]
#[test]
fn limb_borrows_of_constant_masks() {
    let eng = deobfuscator();
    for (src, want) in [
        (
            "extract<18, 14>(x:32 + 0x19766) \
             + sext<14>(x + 0x19766 & 0x3ffff <u (x ^ 0x19766) & 0x3ffff)",
            "extract<18, 14>(x)",
        ),
        (
            "extract<18, 14>(x:32 + 0x19766) \
             + sext<14>(x + 0x19766 & 0x3ffff <u (x | 0x19766) & 0x3ffff)",
            "extract<18, 14>(x)",
        ),
    ] {
        assert_eq!(simplify(&eng, src, 32), want, "{src}");
    }
}

/// Limb arithmetic: a sum or difference computed digit by digit, the carry between the digits
/// written as a comparison of the low ones, is the sum or difference.
#[cfg(feature = "prove")]
#[test]
fn limb_carry_fixtures() {
    let eng = deobfuscator();
    for (src, want) in [
        // The high digits and the carry into them are the high digit of the sum.
        (
            "zext<64>(extract<3, 5>(x) + extract<3, 5>(y) \
             + zext<5>(trunc<3>(x + y) <u trunc<3>(x)))",
            "zext<64>(extract<3, 5>(x + y))",
        ),
        // The halves joined with a shift, not a `concat`.
        (
            "((x >>u 21) + (y >>u 21) + zext<64>((x + y & 0x1fffff) <u (x & 0x1fffff)) << 21) \
             | (x + y & 0x1fffff)",
            "x + y",
        ),
        (
            "((x >>u 21) - (y >>u 21) - zext<64>((x & 0x1fffff) <u (y & 0x1fffff)) << 21) \
             | (x - y & 0x1fffff)",
            "x - y",
        ),
    ] {
        assert_eq!(simplify(&eng, src, 64), want, "{src}");
    }
}

/// Limb arithmetic: a carry whose digits meet no term of its own sum (the sum has as many
/// terms with the carry read, or more) is read at the root once the rounds have changed
/// nothing, where the root so written needs no more nodes: the digits are slices of words
/// that are there, or what reads the sum reads them.
#[cfg(feature = "prove")]
#[test]
fn limb_late_carry_fixtures() {
    let eng = deobfuscator();
    let carry = "zext<12>(trunc<20>(x:32 + y:32) <u trunc<20>(x))";
    for (src, w, want) in [
        // The borrow of `x - y` beside one of its three high digits: the two others.
        (
            "zext<12>(trunc<20>(x:32) <u trunc<20>(y:32)) + extract<20, 12>(y)".to_string(),
            12,
            "extract<20, 12>(x) - extract<20, 12>(x - y)",
        ),
        // The carry of `x + y` beside the high digit of `x`: the high digit of the word,
        // which the `^` reads as well, less that of `y`.
        (
            format!("(extract<20, 12>(x:32) + {carry}) ^ extract<20, 12>(x + y)"),
            12,
            "let %0 = extract<20, 12>(x + y);\n%0 - extract<20, 12>(y) ^ %0",
        ),
        // What reads the sum reads the digit left over.
        (
            format!("(extract<20, 12>(x:32) + {carry}) * extract<20, 12>(y)"),
            12,
            "let %0 = extract<20, 12>(y);\n(extract<20, 12>(x + y) - %0) * %0",
        ),
        // The word's low limb beside its high one.
        (
            format!("zext<32>(extract<20, 12>(x:32) + {carry}) * (x + y & 0xfffff)"),
            32,
            "let %0 = x + y;\n\
             (%0 & 0xfffff) * zext<32>(extract<20, 12>(%0) - extract<20, 12>(y))",
        ),
        // Two borrows, each way.
        (
            "zext<12>(trunc<20>(x:32) <u trunc<20>(y:32)) \
             + zext<12>(trunc<20>(y) <u trunc<20>(x)) + extract<20, 12>(x * y)"
                .to_string(),
            12,
            "extract<20, 12>(x * y) - extract<20, 12>(y - x) - extract<20, 12>(x - y)",
        ),
    ] {
        assert_eq!(simplify(&eng, &src, w), want, "{src}");
    }
}

/// A carry whose digits meet nothing, of words that are no nodes: read, it is three high
/// digits for one comparison, and the root with them needs more nodes than it frees. The
/// late stage leaves it (and ends: it rewrites nothing).
#[cfg(feature = "prove")]
#[test]
fn limb_late_carries_do_not_grow() {
    let eng = deobfuscator();
    for (src, w) in [
        (
            "zext<32>(trunc<13>(x:32 * y:32) <u trunc<13>(z:32)) + v:32",
            32,
        ),
        (
            "zext<12>(trunc<20>(x:32 * y:32) <u trunc<20>(z:32 * v:32)) + u:12",
            12,
        ),
        (
            "zext<12>(trunc<20>(x:32 * y:32) <u trunc<20>(z:32)) + extract<20, 12>(z ^ v:32)",
            12,
        ),
    ] {
        let mut cx = Context::new();
        let e = cx
            .parse(src, &ParseOptions::width(Width::new(w).unwrap()))
            .unwrap();
        let before = dag(&mut cx, &[e]);
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        assert_eq!(out.roots[0].end, End::Completed, "{src}");
        let after = dag(&mut cx, &[out.roots[0].expr]);
        assert!(
            after <= before,
            "{src}: {before} -> {after}: {}",
            cx.display(out.roots[0].expr)
        );
        assert_eq!(
            out.roots[0].expr, e,
            "{src}: the carry stays as it is written"
        );
    }
}

/// Limb arithmetic: sums of carries and terms their digits may or may not meet, under a
/// root that reads the sum with something else, stay equal to themselves: exhaustively.
/// (Such a sum is read in the late stage of the rounds, whatever terms it is left.)
#[cfg(feature = "prove")]
#[test]
fn limb_late_carries_are_sound() {
    let eng = deobfuscator();
    let mut g = Rng(0x11b5_0031);
    let mut rng = Rng(97);
    let mut changed = 0;
    for i in 0..900 {
        let (w, syms): (u64, &[&str]) = if i % 3 == 0 {
            (4, &["a", "b", "c"])
        } else {
            (6, &["a", "b"])
        };
        let sym = |g: &mut Rng| format!("{}:{w}", syms[g.below(syms.len() as u64) as usize]);
        let value = |g: &mut Rng| {
            let (x, y) = (sym(g), sym(g));
            match g.below(5) {
                0 => format!("({x} + {y})"),
                1 => format!("({x} - {y})"),
                2 => format!("({x} * {})", g.below(1 << w)),
                3 => format!("({x} ^ {y})"),
                _ => x,
            }
        };
        let k = 1 + g.below(w - 1);
        let mask = (1u64 << k) - 1;
        // One carry or two, of sides that are related or not, and a term that is a high
        // digit of one of the words, of another value, or none.
        let mut s = String::new();
        let mut words = Vec::new();
        for _ in 0..1 + g.below(2) {
            let p = value(&mut g);
            let q = if g.chance(1, 2) {
                value(&mut g)
            } else {
                format!("({p} + {})", value(&mut g))
            };
            let sign = if g.chance(1, 3) { '-' } else { '+' };
            s += &format!(" {sign} zext<{w}>(({p} & {mask}) <u ({q} & {mask}))");
            words.extend([p, q]);
        }
        let own = match g.below(4) {
            0 => format!("({} >>u {k})", words[g.below(words.len() as u64) as usize]),
            1 => format!("({} >>u {k})", value(&mut g)),
            2 => value(&mut g),
            _ => "0".to_string(),
        };
        let s = format!("({own}{s})");
        let j = 1 + g.below(w - k);
        let low = format!("zext<{w}>(trunc<{j}>({s}))");
        let v = value(&mut g);
        let src = match g.below(5) {
            0 => low,
            1 => format!("{low} * {v}"),
            2 => format!("{low} ^ ({} >>u {k})", words[0]),
            3 => format!("({low} << {k}) | ({} & {mask})", words[0]),
            _ => format!("select(t:1, {low}, {v})"),
        };
        let mut cx = Context::new();
        let e = cx
            .parse(&src, &ParseOptions::width(Width::new(w as u16).unwrap()))
            .unwrap_or_else(|err| panic!("{src}: {err}"));
        let out = checked(&eng, &mut cx, e);
        assert!(
            equivalent(&mut cx, e, out.expr, &mut rng),
            "{src} vs {}",
            cx.display(out.expr)
        );
        changed += u64::from(out.changed);
    }
    assert!(changed > 300, "{changed}");
}

/// Low digits a handler computes apart from the word.
#[cfg(feature = "prove")]
#[test]
fn limb_anchor_fixtures() {
    let eng = deobfuscator();
    for (src, want) in [
        // The low digit at its own width, the multiplier reduced to it (0x2d4b1 is 177 modulo
        // 2^9): the word is the one the high digit is of.
        (
            "let %0 = (x:32 ^ y:32) * 0x2d4b1; \
             let %1 = (trunc<9>(x) ^ trunc<9>(y)) * 177; \
             zext<9>(%1 + 301 <u %1) + extract<9, 9>(%0)",
            "extract<9, 9>((x ^ y) * 0x2d4b1 + 301)",
        ),
        // The high digit of `-(b - a)`: the borrow of a negation is `low != 0`.
        (
            "let %0 = b:32 - a:32; let %1 = a - b & 4095; let %2 = a - b + c:32 & 4095; \
             trunc<20>(%2 | (c >>u 12) - (%0 >>u 12) - zext<32>(%1 != 0) + zext<32>(%2 <u %1) << 12)",
            "trunc<20>(a - b + c)",
        ),
        // A carry out of a middle digit: a constant added limb by limb, the digit of the
        // constant (868 of 0xd9345) added to the word's own.
        (
            "zext<12>(extract<10, 10>(x:32 + 837) - 156 <u extract<10, 10>(x)) \
             + extract<20, 12>(x)",
            "extract<20, 12>(x + 0xd9345)",
        ),
        // Not where the digit of the difference may be all ones (the carry into the digit
        // then makes the two equal): the comparison stays, the constant beside its slice
        // taken into the slice's sum.
        (
            "zext<12>(extract<10, 10>(x:32 + 837) - 1 <u extract<10, 10>(x)) \
             + extract<20, 12>(x)",
            "zext<12>(extract<10, 10>(x + 0xfff45) <u extract<10, 10>(x)) + extract<20, 12>(x)",
        ),
        // The middle limb of `x - y`, a 64-bit word in limbs of 26 bits, its low limb
        // computed in a 32-bit register: the digits are the 64-bit word's.
        (
            "let %4 = -trunc<32>(y:64); let %5 = %4 & 0x3ffffff; let %11 = %4 + trunc<32>(x:64); \
             zext<32>(trunc<26>(-zext<32>(%5 != 0) + (-extract<26, 32>(y) + extract<26, 32>(x)) \
             + zext<32>(%11 & 0x3ffffff <u %5)))",
            "zext<32>(extract<26, 26>(x - y))",
        ),
        // The high limb of `(a ^ b) ^ c` written `x + c - 2·(x & c)`, the AND's digit read
        // one bit lower and of a slice of `a ^ b` where the borrow's own digit is of the
        // slices of `a` and `b`: one value, and it cancels.
        (
            "let %0 = a:32 ^ b:32; \
             zext<32>(trunc<13>(zext<32>(extract<18, 13>(%0 + c:32)) \
             - (c >>u 17 & zext<32>(extract<17, 13>(%0))) \
             + sext<32>(%0 + c & 0x3ffff <u (%0 ^ c) & 0x3ffff)))",
            "zext<32>(extract<18, 13>(a ^ b ^ c))",
        ),
    ] {
        assert_eq!(simplify(&eng, src, 32), want, "{src}");
    }
}

/// `x <u y` as a VM's flags spell it.
#[cfg(feature = "prove")]
fn below(x: &str, y: &str) -> String {
    format!("~((-{x} + {y} == 0) | ({y} <u {x}))")
}

/// The high limb of the 32-bit symbols of `ops` XORed one handler at a time, each in limbs of
/// 25 and 7 bits: an XOR is written `x + o - 2·(x & o)`, the low limb at the full width and
/// masked, the high limb from the digits, the carry of the sum and the borrow of the
/// difference (comparisons of the low limbs), and the next handler reads `x >> 24` back from
/// the two limbs.
#[cfg(feature = "prove")]
fn xor_limbs(ops: &[&str]) -> String {
    let m = "0x1ffffff";
    let (a, b) = (ops[0], ops[1]);
    // The word so far, its low limb and its high limb (the low 7 bits of a 32-bit value).
    let mut lets = vec![
        format!("{a}:32 & {b}:32"),
        format!("{a} + {b}"),
        "-(%0 << 1) + %1".to_string(),
        format!("%1 & {m}"),
        format!("%2 & {m}"),
    ];
    lets.push(format!(
        "({a} >>u 25) + ({b} >>u 25) - (%0 >>u 24) + sext<32>({}) + zext<32>({})",
        below("%3", "%4"),
        below("%3", &format!("({a} & {m})"))
    ));
    let (mut word, mut low, mut high) = (2, 4, 5);
    for o in &ops[2..] {
        let at = lets.len();
        lets.push(format!("%{word} + {o}:32"));
        lets.push(format!("%{at} + (%{word} & {o}) * -2"));
        lets.push(format!("%{at} & {m}"));
        lets.push(format!("%{} & {m}", at + 1));
        lets.push(format!(
            "-({o} >>u 24 & trunc<32>((zext<64>(%{high}) << 1) + zext<64>(%{low} >>u 24))) \
             + (%{high} + ({o} >>u 25) + zext<32>({})) + sext<32>({})",
            below(&format!("%{}", at + 2), &format!("%{low}")),
            below(&format!("%{}", at + 2), &format!("%{}", at + 3))
        ));
        (word, low, high) = (at + 1, at + 3, at + 4);
    }
    let mut src = String::new();
    for (k, e) in lets.iter().enumerate() {
        src.push_str(&format!("let %{k} = {e}; "));
    }
    format!("{src}trunc<7>(%{high})")
}

/// Limb arithmetic as a VM's handlers write it: a limb is the slice of the word its handler
/// computes at the full width for the lowest limb.
#[cfg(feature = "prove")]
#[test]
fn limb_slice_fixtures() {
    let eng = deobfuscator();
    // The top limb of `(w + k) ^ c` in limbs of 12, 12 and 8 bits: `x = w + k` with its
    // carries, `x + c` with its own, `- 2·(x & c)` with its borrows.
    let three = format!(
        "let %0 = w:32 + 0x2c51a7e3; \
         let %1 = trunc<16>(%0 & 4095); \
         let %2 = (w >>u 12) + zext<32>(%1 <u trunc<16>(w & 4095)); \
         let %3 = %2 + 0x2c51a; \
         let %4 = trunc<16>(%3 & 4095); \
         let %5 = zext<8>(%4 <u trunc<16>((w >>u 12) & 4095)); \
         let %6 = w + 0x7e15514f; \
         let %7 = trunc<16>(%6 & 4095); \
         let %8 = %2 + zext<32>({c0}) + 0x7e154; \
         let %9 = trunc<16>(%8 & 4095); \
         let %10 = trunc<16>(%6 + (%0 & 0x51c3a96c) * -2 & 4095); \
         let %11 = trunc<16>(%8 - (trunc<32>((zext<64>(%3) << 1) \
         + zext<64>((%0 & 4095) >>u 11)) & 0xa3875) + sext<32>({b0}) & 4095); \
         %5 + trunc<8>(zext<64>(w >>u 24) + 125) + zext<8>({c1}) \
         - (((trunc<8>(w >>u 24) << 1) + (%5 << 1) + 88 | extract<11, 8>(%3 & 4095)) & 163) \
         + sext<8>({b1})",
        c0 = below("%7", "%1"),
        b0 = below("%7", "%10"),
        c1 = below("%9", "%4"),
        b1 = below("%9", "%11"),
    );
    for (src, w, want) in [
        // One handler after another: each high limb is a slice of that handler's word, and
        // the words are XORs once nothing reads their sums.
        (xor_limbs(&["a", "b", "c"]), 32, "extract<25, 7>(b ^ c ^ a)"),
        // Three limbs: the carry into the third is no comparison of two low digits, and the
        // limb is found as the slice of the word and proved. The handler spells its XOR with
        // a constant `x + c - 2·(x & c)`, the constants folded: the words pass reads it as
        // the XOR.
        (three, 32, "extract<24, 8>(w + 0x2c51a7e3 ^ 0x51c3a96c)"),
        // A digit that is no truncation of the sum's operand: a value below `2^k`, and a mask
        // under the sum.
        (
            "extract<8, 8>(a) + extract<8, 8>(b) + zext<8>((a + b & 255) <u (a & 255))".into(),
            16,
            "extract<8, 8>(a + b)",
        ),
        (
            "extract<8, 8>(a) + zext<8>((a + (b & 15) & 255) <u (b & 15))".into(),
            16,
            "extract<8, 8>((b & 15) + a)",
        ),
        // Two neighbouring slices of one bitwise value, the upper one pushed into it.
        (
            "concat(extract<8, 8>(a) | extract<8, 8>(b), trunc<8>(a | b))".into(),
            32,
            "trunc<16>(a | b)",
        ),
        // A slice doubled beside the bit below it is the slice one lower.
        (
            "extract<25, 7>(x) + extract<25, 7>(x) | zext<7>(extract<24, 1>(x))".into(),
            32,
            "extract<24, 7>(x)",
        ),
        // An XOR written as a sum whose parts something else reads.
        (
            "(x + y - 2 * (x & y)) * ((x + y) ^ (x & y))".into(),
            32,
            "(x ^ y) * (x + y ^ x & y)",
        ),
        // A limb held at the word's width: what of it is not known to be 0 is a slice of the
        // word (the carry's digit here is of `x`, not of the sum's operand `x & 0xffff`).
        (
            "let %0 = (x & 0xffff) + (y & 0xffff); \
             zext<32>(trunc<8>(%0) <u trunc<8>(x)) + ((x & 0xffff) >>u 8) + ((y & 0xffff) >>u 8)"
                .into(),
            32,
            "zext<32>(extract<8, 9>((y & 65535) + (x & 65535)))",
        ),
        // The high limb shifted into its word beside the low one: an XOR in limbs of 18 and
        // 14 bits, its low limb already an XOR, is the XOR.
        (
            "let %0 = (a ^ b) & 0x3ffff; \
             let %1 = a + b & 0x3ffff; \
             %0 | (zext<32>(%1 <u (a & 0x3ffff)) + (a >>u 18) + (b >>u 18) \
             - zext<32>(%1 <u %0) - ((a & b) >>u 17) << 18)"
                .into(),
            32,
            "a ^ b",
        ),
        // The sum of two digits carries: its high digit is not 0, and this is no slice of
        // `x` (it was taken for one). It is the slice less that carry (the words pass).
        (
            "extract<14, 16>(x - zext<32>(a:14) - zext<32>(b:14)) \
             + zext<16>(trunc<14>(x) <u (a:14 + b:14))"
                .into(),
            32,
            "extract<14, 16>(x) - zext<16>(extract<14, 1>(zext<15>(b) + zext<15>(a)))",
        ),
    ] {
        assert_eq!(simplify(&eng, &src, w), want, "{src}");
    }
    // A longer chain, with the MBA phase beside these passes: an XOR of more than three
    // values written as a sum is that phase's to recognize.
    #[cfg(feature = "mba")]
    {
        use crate::mba::{MbaConfig, MbaTrust, NormalFormSolver};
        let trust = MbaTrust::default().with_backend_certificates(false);
        let eng = Engine::builder()
            .builtin()
            .strategy(Strategy::deobfuscate().with_mba(MbaConfig::default().with_trust(trust)))
            .mba_solver(std::sync::Arc::new(NormalFormSolver::default()))
            .build()
            .unwrap();
        assert_eq!(
            simplify(&eng, &xor_limbs(&["a", "b", "c", "d", "e"]), 32),
            "extract<25, 7>(b ^ c ^ a ^ d ^ e)"
        );
    }
}

/// Limbs whose word is no node: a handler that adds a constant adds it limb by limb, so the
/// node the low limb is masked from has only the constant's low digit; and a limb's raw sum
/// read wider than the limb holds the carry out of its register.
#[cfg(feature = "prove")]
#[test]
fn limb_made_word_fixtures() {
    let eng = deobfuscator();
    // `x * 12123 + 46789` in limbs of 9 and 7 bits (46789 is 91 above 197): the high limb is
    // its own digit of the product, the carry of the low limb's 197 and 91.
    let high = "(p >>u 9) + zext<16>((p + 197 & 0x1ff) <u 197) + 91";
    for (src, want) in [
        (
            format!("let p = x * 0x2f5b; trunc<7>({high})"),
            "extract<9, 7>(x * 12123 - 18747)",
        ),
        // Read one bit wider, the sum has the carry out of the 16 bits: a slice of the word
        // zero-extended.
        (
            format!("let p = x * 0x2f5b; trunc<8>({high})"),
            "extract<9, 8>(zext<32>(x * 12123) + 46789)",
        ),
        // The same of the sum of two words.
        (
            "trunc<8>((a >>u 9) + (b >>u 9) + zext<16>((a + b & 0x1ff) <u (a & 0x1ff)))".into(),
            "extract<9, 8>(zext<32>(a) + zext<32>(b))",
        ),
    ] {
        assert_eq!(simplify(&eng, &src, 16), want, "{src}");
    }
}

/// Limbs of a word plus a constant, the constant added limb by limb (sometimes with the wrong
/// low digit in the carry), read on any number of bits up to the register's: whatever word
/// they are read as a slice of, the result is equal at every assignment.
#[cfg(feature = "prove")]
#[test]
fn limb_made_words_are_sound() {
    let eng = deobfuscator();
    let mut g = Rng(0x11b5_0020);
    let mut rng = Rng(90);
    let mut changed = 0;
    let w = 7u64;
    for _ in 0..400 {
        let word = match g.below(3) {
            0 => format!("(a:{w} * {})", g.below(1 << w) | 1),
            1 => format!("(a:{w} + b:{w})"),
            _ => format!("a:{w}"),
        };
        let k = 1 + g.below(w - 2);
        let mask = (1u64 << k) - 1;
        let c = g.below(1 << w);
        let (c0, c1) = (c & mask, c >> k);
        let seen = if g.chance(1, 4) {
            g.below(mask + 1)
        } else {
            c0
        };
        let j = 1 + g.below(w);
        let carry = format!("zext<{w}>((({word} + {c0}) & {mask}) <u {seen})");
        let src = format!("trunc<{j}>(({word} >>u {k}) + {carry} + {c1})");
        let mut cx = Context::new();
        let e = cx
            .parse(&src, &ParseOptions::width(Width::new(w as u16).unwrap()))
            .unwrap_or_else(|err| panic!("{src}: {err}"));
        let out = checked(&eng, &mut cx, e);
        assert!(
            equivalent(&mut cx, e, out.expr, &mut rng),
            "{src} vs {}",
            cx.display(out.expr)
        );
        changed += u64::from(out.changed);
    }
    assert!(changed > 200, "{changed}");
}

/// The top limb of `(w + k) ^ c` in limbs of 12, 12 and 8 bits over the word `w` (an
/// expression of 32 bits), as [`limb_slice_fixtures`] spells it.
#[cfg(feature = "prove")]
fn three_limbs(w: &str) -> String {
    format!(
        "let %20 = {w}; \
         let %0 = %20 + 0x2c51a7e3; \
         let %1 = trunc<16>(%0 & 4095); \
         let %2 = (%20 >>u 12) + zext<32>(%1 <u trunc<16>(%20 & 4095)); \
         let %3 = %2 + 0x2c51a; \
         let %4 = trunc<16>(%3 & 4095); \
         let %5 = zext<8>(%4 <u trunc<16>((%20 >>u 12) & 4095)); \
         let %6 = %20 + 0x7e15514f; \
         let %7 = trunc<16>(%6 & 4095); \
         let %8 = %2 + zext<32>({c0}) + 0x7e154; \
         let %9 = trunc<16>(%8 & 4095); \
         let %10 = trunc<16>(%6 + (%0 & 0x51c3a96c) * -2 & 4095); \
         let %11 = trunc<16>(%8 - (trunc<32>((zext<64>(%3) << 1) \
         + zext<64>((%0 & 4095) >>u 11)) & 0xa3875) + sext<32>({b0}) & 4095); \
         %5 + trunc<8>(zext<64>(%20 >>u 24) + 125) + zext<8>({c1}) \
         - (((trunc<8>(%20 >>u 24) << 1) + (%5 << 1) + 88 | extract<11, 8>(%3 & 4095)) & 163) \
         + sext<8>({b1})",
        c0 = below("%7", "%1"),
        b0 = below("%7", "%10"),
        c1 = below("%9", "%4"),
        b1 = below("%9", "%11"),
    )
}

/// A limb is read back whatever computes its handler's operand: over a product of two
/// variables as over a symbol (in a slice proof's copy the product is a fresh symbol).
#[cfg(feature = "prove")]
#[test]
fn limb_slice_proofs_take_products_for_symbols() {
    let eng = deobfuscator();
    let run = |src: &str| {
        let mut cx = Context::new();
        let e = cx.parse(src, &ParseOptions::width(Width::W8)).unwrap();
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        assert_eq!(out.roots[0].end, End::Completed);
        cx.display(out.roots[0].expr).to_string()
    };
    assert_eq!(
        run(&three_limbs("w:32")),
        "extract<24, 8>(w + 0x2c51a7e3 ^ 0x51c3a96c)"
    );
    assert_eq!(
        run(&three_limbs("(a:32 * b:32)")),
        "extract<24, 8>(a * b + 0x2c51a7e3 ^ 0x51c3a96c)"
    );
}

/// A limb of two products by constants written digit by digit, joined to its word's low
/// limb: the proof's two sides are one sum over the slices of the operands once their sums
/// are written one way (the search alone does not decide it within the caps), and the limbs
/// are the word.
#[cfg(feature = "prove")]
#[test]
fn limb_products_in_digits_are_read_back() {
    let eng = deobfuscator();
    let (k1, k2, m) = ("0x511bff07", "0x2c51a7e3", "0x3ffff");
    let src = format!(
        "let %0 = a:32 * {k1}; let %1 = b:32 * {k2}; let %2 = %0 + %1; let %3 = %2 + c:32; \
         let %4 = extract<18, 14>((a & {m}) * {k1}) + trunc<14>((a >>u 18) * {k1}); \
         let %5 = extract<18, 14>((b & {m}) * {k2}) + trunc<14>((b >>u 18) * {k2}); \
         let %6 = %4 + %5 + zext<14>((%2 & {m}) <u (%0 & {m})) + extract<18, 14>(c) \
         + zext<14>((%3 & {m}) <u (c & {m})); \
         concat(%6, trunc<18>(%3))"
    );
    let mut cx = Context::new();
    let e = cx.parse(&src, &ParseOptions::width(Width::W32)).unwrap();
    let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
    assert_eq!(out.roots[0].end, End::Completed);
    assert!(equivalent(&mut cx, e, out.roots[0].expr, &mut Rng(81)));
    assert_eq!(
        cx.display(out.roots[0].expr).to_string(),
        "b * 0x2c51a7e3 + a * 0x511bff07 + c"
    );
}

/// Limb arithmetic: a word several limbs read stays one node for them all.
#[cfg(feature = "prove")]
#[test]
fn limb_word_fixtures() {
    let eng = deobfuscator();
    for (src, want) in [
        // Read under 7 bits and under 14: written once, under 14 (a copy for each reader, its
        // constants reduced to that reader's bits, is `x * 7 - 59` beside `x * -249 + 4933`).
        (
            "let %0 = x * 0x511bff07 + (y << 14) + (z << 20) + 0x764d9345; \
             trunc<8>((%0 & 127) * a + ((%0 & 16383) >>u 7) * b)",
            "let %0 = x * -249 + 4933;\n\
             trunc<8>(zext<32>(extract<7, 7>(%0)) * b + (%0 & 127) * a)",
        ),
        // The truncation of a sum reaches a value whose truncation something else reads: it
        // is used, where narrowing the value again would build its `select` a second time
        // (and the sum would stay 64 bits wide, no XOR).
        (
            "let %0 = select(c:1, p:64 >>u 32, q:64 >>u 32); \
             let %1 = trunc<32>(%0); \
             let %2 = a & %1; \
             trunc<32>(zext<64>(a) + %0) - (%2 + %2)",
            "extract<32, 32>(select(c, p, q)) ^ a",
        ),
        // A register read limb by limb, each limb through its own chain of `select`s, is the
        // limbs of the one chain over the registers.
        (
            "let %0 = select(c:1, a, select(d:1, b, e)); \
             (%0 >>u s) + zext<32>(select(c, trunc<16>(a & 16383), \
             select(d, trunc<16>(b & 16383), trunc<16>(e & 16383)))) \
             * select(c, a >>u 14, select(d, b >>u 14, e >>u 14))",
            "let %0 = select(c, a, select(d, b, e));\n\
             (%0 & 16383) * (%0 >>u 14) + (%0 >>u s)",
        ),
        // Without the chain over the registers: it is built, one for the two.
        (
            "zext<32>(select(c:1, trunc<16>(a & 16383), trunc<16>(b & 16383))) \
             * select(c, a >>u 14, b >>u 14)",
            "let %0 = select(c, a, b);\n\
             (%0 & 16383) * (%0 >>u 14)",
        ),
        // An arm that masks the low bits beside one that zero-extends as many: one value.
        (
            "select(c:1, x & 255, zext<32>(v:8)) + zext<32>(select(c, trunc<8>(x), v)) * z",
            "let %0 = zext<32>(select(c, trunc<8>(x), v));\n\
             %0 * z + %0",
        ),
        // A word XORed with itself shifted by a count taken from it, each part selected
        // apart: the arms are the same operations over one pair of words and the same slice
        // of both, so they are done once over the selected word.
        (
            "let %1 = trunc<32>(p:64); \
             let %2 = extract<32, 32>(q:64); \
             let %3 = select(c:1, %1, %2); \
             let %4 = %1 >>u concat(2:29, extract<29, 3>(p)); \
             let %5 = %2 >>u concat(2:29, extract<61, 3>(q)); \
             select(c, %4, %5) + %3 - (select(c, %4 & %1, %5 & %2) << 1)",
            "let %0 = select(c, trunc<32>(p), extract<32, 32>(q));\n\
             %0 >>u concat(2:29, extract<29, 3>(%0)) ^ %0",
        ),
        // A `select` that exists is used though the other arm's operation is shared (it costs
        // nothing: the shift is done once more, on it, and once less, on an arm).
        (
            "let %5 = b >>u 14; \
             let %6 = select(c:1, a >>u 14, %5); \
             -select(c, a, b) * %6 + %5",
            "let %0 = select(c, a, b);\n\
             -%0 * (%0 >>u 14) + (b >>u 14)",
        ),
        // A limb of a register whose half is selected whole elsewhere is that limb of the
        // half.
        (
            "zext<32>(select(c:1, extract<28, 4>(p:64), extract<60, 4>(q:64))) \
             * select(c, trunc<32>(p), extract<32, 32>(q))",
            "let %0 = select(c, trunc<32>(p), extract<32, 32>(q));\n\
             zext<32>(extract<28, 4>(%0)) * %0",
        ),
        // A slice across the seam of the words the arms of a `select` join: the limbs on
        // either side, and the low limbs nothing reads are gone.
        (
            "zext<32>(extract<22, 10>(select(c:1, concat(a:6, concat(b:11, x:15)), \
             select(d:1, concat(e:6, concat(f:11, y:15)), g))))",
            "zext<32>(select(c, concat(a, extract<7, 4>(b)), \
             select(d, concat(e, extract<7, 4>(f)), extract<22, 10>(g))))",
        ),
    ] {
        assert_eq!(simplify(&eng, src, 32), want, "{src}");
    }
}

/// Limb arithmetic: one word, however its readers spell it. A carry whose sides are another
/// spelling of the word the sum holds the high digit of (its constant reduced to the limb's
/// bits, or the product computed in digits) is read over that word; a carry under `~`; the
/// top limb of `v ^ (v >>u s)` written as that of `v`; a constant added to a slice; and two
/// sums below a root that are one word on the bits read of them.
#[cfg(feature = "prove")]
#[test]
fn limb_one_word_fixtures() {
    let eng = deobfuscator();
    for (src, w, want) in [
        // The low limb with the constant reduced to it, beside the word's high digit.
        (
            "zext<8>(trunc<8>(x:16 * 0x35 + 93) <u trunc<8>(x * 0x35)) \
             + extract<8, 8>(x * 0x6b35)",
            8,
            "extract<8, 8>(x * 27445 + 93)",
        ),
        // The low limb as a product in 6-bit digits of a 12-bit limb.
        (
            "let %0 = (x:16 & 0xfff) * 53 + (x & 63) * 2816; \
             zext<8>(trunc<8>(%0 + 93) <u trunc<8>(%0)) + extract<8, 8>(x * 0x6b35)",
            8,
            "extract<8, 8>(x * 27445 + 93)",
        ),
        // One side written over the word already, the other over its reduced spelling: the
        // difference is taken over the word.
        (
            "let %0 = (x:16 & 63) * 53; \
             zext<4>(trunc<6>(x * 0x6b35 + y:16 * 64 + 29) <u trunc<6>(%0)) \
             + extract<6, 4>(x * 0x6b35) - extract<6, 4>(y * 64 + 29)",
            4,
            "extract<6, 4>(x * 27445 + 29) - trunc<4>(y)",
        ),
        // The carry out of a middle limb under `~` (`a − ~c` is `a + c + 1`), and the
        // constant taken into the word.
        (
            "let %0 = x:16 * 0x6b35; \
             extract<12, 4>(%0) - ~zext<4>(extract<8, 4>(%0 + 0x35d) <u extract<8, 4>(%0))",
            4,
            "extract<12, 4>(x * 27445 + 4957)",
        ),
        // The top limb of a word XORed with itself shifted is the top limb of the word.
        (
            "((v:16 ^ (v >>u 9)) & 1023) * 0x6b35 + (v >>u 10) * 0xd400",
            16,
            "(v >>u 9 ^ v) * 27445",
        ),
        // A constant added to a slice of a sum is the sum's.
        (
            "extract<8, 8>(x:16 * 0x6b35 + 93) + 7",
            8,
            "extract<8, 8>(x * 27445 + 1885)",
        ),
        // Two readers, each with the constant reduced to its bits: one word.
        (
            "zext<8>(extract<4, 4>(x:16 * 0x35 + 5)) + extract<8, 8>(x * 0x6b35 + 5)",
            8,
            "let %0 = x * 27445 + 5;\n\
             zext<8>(extract<4, 4>(%0)) + extract<8, 8>(%0)",
        ),
        // The product in digits, read below its limb's top, beside the product of the word.
        (
            "let %0 = (x:16 & 0xfff) * 53 + (x & 63) * 2816; \
             zext<8>(extract<3, 5>(%0)) + extract<8, 8>(x * 0x6b35) * y:8",
            8,
            "let %0 = x * 27445;\n\
             extract<8, 8>(%0) * y + zext<8>(extract<3, 5>(%0))",
        ),
    ] {
        assert_eq!(simplify(&eng, src, w), want, "{src}");
    }
}

/// Limb arithmetic: two sums written over other words at the root in one step, where what
/// one is written as reads the other. `lo·K` is read on its low digit and is `lo·k0` there;
/// `x·k0 + lo·(k1 << s) + c` is read on two digits and is `lo·K + hi·(k0 << d) + c` there,
/// which reads `lo·K` on both: the second has to keep the first as it was, not as the first
/// is written for its own readers. Equal at every value of `x`.
#[cfg(feature = "prove")]
#[test]
fn limb_one_word_keeps_what_it_reads() {
    let eng = deobfuscator();
    let mut g = Rng(0x0b17_5eed);
    let mut rng = Rng(53);
    for i in 0..240 {
        let (w, d) = match i % 3 {
            0 => (8u64, 3u64),
            1 => (8, 2),
            _ => (12, 3 + g.below(2)),
        };
        let k0 = g.below(1 << d) | 1;
        let k1 = 1 + g.below(3);
        let c = g.below(1 << d);
        let s = d + 1;
        let lo = format!("(x:{w} & {})", (1u64 << d) - 1);
        let hi = format!("zext<{w}>(extract<{d}, {d}>(x))");
        let sum = format!("(x * {k0} + {lo} * {} + {c})", k1 << s);
        let wide = 2 * w;
        let src = format!(
            "zext<{w}>(extract<{d}, {d}>({sum})) ^ (zext<{w}>(trunc<{d}>({lo} * {})) << {d}) \
             ^ extract<{}, {w}>(zext<{wide}>({hi} * {}) + zext<{wide}>({lo} * {}) \
             + zext<{wide}>({lo} * {k0}))",
            k0 + (k1 << s),
            2 * d,
            k0 << d,
            k1 << s
        );
        let mut cx = Context::new();
        let e = cx
            .parse(&src, &ParseOptions::width(Width::new(w as u16).unwrap()))
            .unwrap_or_else(|err| panic!("{src}: {err}"));
        let out = checked(&eng, &mut cx, e);
        assert!(
            equivalent(&mut cx, e, out.expr, &mut rng),
            "{src} vs {}",
            cx.display(out.expr)
        );
    }
}

/// Limb arithmetic: a product `x·k` in several spellings (its constant with other bits
/// above some bit, or two limbs each under a constant of its own), a carry between two of
/// them beside the high digit of a third, slices of them added and constants added to the
/// slices, and the limbs of `a ^ (a >>u s)` with the top one written over `a`: each stays
/// equal to itself at every assignment. The spellings agree on their low bits only, and
/// the top limbs only for a shift that clears them, so a reading that took one for another
/// on more bits would be caught.
#[cfg(feature = "prove")]
#[test]
fn limb_one_word_readings_are_sound() {
    let eng = deobfuscator();
    let mut g = Rng(0x11b5_0031);
    let mut rng = Rng(97);
    let mut changed = 0;
    let w: u64 = 7;
    let mask = (1u64 << w) - 1;
    for i in 0..900 {
        let s = 1 + g.below(w - 1);
        let x = match g.below(3) {
            0 => "a:7".to_string(),
            1 => format!("(a:7 ^ (a >>u {s}))"),
            _ => "(a:7 + b:7)".to_string(),
        };
        let k = g.below(1 << w) | 1;
        // `x·k` right on its low `m` bits: other bits above them in the constant, and the
        // product as it is or in two limbs.
        let spell = |g: &mut Rng, m: u64| {
            let other = (k ^ (g.below(1 << w) << m)) & mask;
            let h = m.div_ceil(2);
            if m < 2 || g.chance(1, 2) {
                format!("({x} * {other})")
            } else {
                let (lo, hi) = (other & ((1 << h) - 1), other >> h << h);
                format!(
                    "(({x} & {}) * {lo} + ({x} & {}) * {hi})",
                    (1u64 << m) - 1,
                    (1u64 << h) - 1
                )
            }
        };
        let (src, j) = match i % 3 {
            // A carry between two spellings beside the high digit of the word.
            0 => {
                let at = 1 + g.below(w - 2);
                let j = 1 + g.below(w - at);
                let word = spell(&mut g, w);
                let m = at + g.below(w - at + 1);
                let q = spell(&mut g, m);
                let d = if g.chance(1, 2) {
                    format!("{}:7", g.below(1 << w))
                } else {
                    "b:7".to_string()
                };
                let p = match g.below(3) {
                    0 => format!("({q} + {d})"),
                    1 => format!("({word} + {d})"),
                    _ => format!("({} + {d})", spell(&mut g, at)),
                };
                let (x, y) = if at > 1 && g.chance(1, 3) {
                    let o = 1 + g.below(at - 1);
                    let side = |v: &str| format!("extract<{o}, {}>({v})", at - o);
                    (side(&p), side(&q))
                } else {
                    (format!("trunc<{at}>({p})"), format!("trunc<{at}>({q})"))
                };
                let (x, y) = if g.chance(1, 4) { (y, x) } else { (x, y) };
                let high = |v: &str| format!("extract<{at}, {j}>({v})");
                let carry = match g.below(4) {
                    0 => format!("- ~zext<{j}>({x} <u {y})"),
                    1 => format!("+ sext<{j}>({x} <u {y})"),
                    _ => format!("+ zext<{j}>({x} <u {y})"),
                };
                let sign = if g.chance(1, 4) { '-' } else { '+' };
                let mut src = format!("{} {carry}", high(&word));
                if g.chance(1, 3) {
                    src.push_str(&format!(" {sign} {}", high(&d)));
                }
                if g.chance(1, 3) {
                    src.push_str(&format!(" + {}", g.below(1 << j)));
                }
                (src, j)
            }
            // Slices of two spellings, a constant added to each word and to each slice.
            1 => {
                let slice = |g: &mut Rng| {
                    let lo = 1 + g.below(w - 2);
                    let n = 1 + g.below(w - lo);
                    let v = spell(g, lo + n);
                    let c = g.below(1 << w);
                    let e = format!("zext<7>(extract<{lo}, {n}>({v} + {c}))");
                    if g.chance(1, 2) {
                        format!("({e} + {})", g.below(1 << n))
                    } else {
                        e
                    }
                };
                let (t, u) = (slice(&mut g), slice(&mut g));
                let src = match g.below(3) {
                    0 => format!("{t} + {u}"),
                    1 => format!("{t} * b:7 + {u}"),
                    _ => format!("{t} + ({} >>u {})", spell(&mut g, w), g.below(w)),
                };
                (src, w)
            }
            // The limbs of `a ^ (a >>u s)`, the top one written over `a`: the word only
            // where the shift clears the top limb.
            _ => {
                let m = 1 + g.below(w - 1);
                let top = (k << m) & mask;
                let src = format!(
                    "((a:7 ^ (a >>u {s})) & {}) * {k} + (a >>u {m}) * {top} + b:7",
                    (1u64 << m) - 1
                );
                (src, w)
            }
        };
        let mut cx = Context::new();
        let e = cx
            .parse(&src, &ParseOptions::width(Width::new(j as u16).unwrap()))
            .unwrap_or_else(|err| panic!("{src}: {err}"));
        let out = checked(&eng, &mut cx, e);
        assert!(
            equivalent(&mut cx, e, out.expr, &mut rng),
            "{src} vs {}",
            cx.display(out.expr)
        );
        changed += u64::from(out.changed);
    }
    assert!(changed > 300, "{changed}");
}

/// Limb arithmetic: the step `T ^ (T << s)` in limbs, and one word in several spellings. The
/// low limb of `T << s` is written `(T & 1) << s` and the carry of `T + (T << s)` compares
/// with it: read over `T << s`, whose digit the sum holds in two pieces of `T`. The same
/// with a piece cut from the word at another width, its constant reduced to the limb's
/// bits; the high limb of a product written over the slices of the operands of an `^`; and
/// the borrow of an XOR whose operand is a truncated product.
#[cfg(feature = "prove")]
#[test]
fn limb_shifted_word_fixtures() {
    let eng = deobfuscator();
    for (src, w, want) in [
        // The high limb of `T + (T << 5) − 2·(T & (T << 5))` at 6 + 10 bits.
        (
            "let %0 = x:16 * 0x6b35; \
             zext<10>(extract<1, 5>(%0)) + extract<6, 10>(%0) * 33 \
             + zext<10>(trunc<6>(%0 + (%0 << 5)) <u trunc<6>((%0 & 1) << 5)) \
             - trunc<10>(%0 & (%0 >>u 5))",
            10,
            "let %0 = x * 27445;\n\
             extract<6, 10>(x * -11819) - trunc<10>(%0 & %0 >>u 5)",
        ),
        // The piece below the limb's top cut from a 32-bit product over the sign extension.
        (
            "let %0 = x:16 * 0x6b35 + 9; \
             zext<10>(extract<1, 5>(sext<32>(x) * 53 + 9)) + extract<6, 10>(%0) * 33 \
             + zext<10>(trunc<6>(%0 + (%0 << 5)) <u trunc<6>((%0 & 1) << 5)) \
             - trunc<10>(%0 & (%0 >>u 5))",
            10,
            "let %0 = x * 27445 + 9;\n\
             extract<6, 10>(%0 + (%0 << 5)) - trunc<10>(%0 & %0 >>u 5)",
        ),
        // The high limb of `a ^ b` written over the slices of `a` and `b`.
        (
            "zext<16>(extract<6, 10>(a:16) ^ extract<6, 10>(b:16)) * 0xcd40 \
             + ((a ^ b) & 63) * 0x6b35",
            16,
            "(a ^ b) * 27445",
        ),
        // The carry and the borrow of an XOR in limbs, one operand a truncated product.
        (
            "let %0 = trunc<16>(y:32 * 0x12345); \
             extract<6, 10>(%0) + extract<6, 10>(b:16) - extract<5, 10>(%0 & b) \
             + zext<10>(trunc<6>(%0 + b) <u trunc<6>(%0)) \
             - zext<10>(trunc<6>(%0 + b) <u trunc<6>(%0 ^ b))",
            10,
            "extract<6, 10>(trunc<16>(y * 0x12345) ^ b)",
        ),
    ] {
        assert_eq!(simplify(&eng, src, w), want, "{src}");
    }
}

/// Limb arithmetic: the readings of a word shifted left and of one word at several widths
/// stay equal to their inputs at every assignment. A carry whose side is `(T & m) << s`,
/// with masks that have the digit's bits and masks that lack one (the side is then no
/// digit of `T << s`); pieces of `T` cut from a wider product whose constants agree with
/// the word's on some low bits only; a bitwise function of two slices cut at the same and at
/// different bits beside the function of the words; and the carries of an XOR and an OR in
/// limbs over a truncated product.
#[cfg(feature = "prove")]
#[test]
fn limb_shifted_word_readings_are_sound() {
    let eng = deobfuscator();
    let mut g = Rng(0x5eed_0b17);
    let mut rng = Rng(131);
    let mut changed = 0;
    let w: u64 = 7;
    let mask = (1u64 << w) - 1;
    for i in 0..800 {
        let k = g.below(1 << w) | 1;
        let c = g.below(1 << w);
        let t = match g.below(3) {
            0 => "a:7".to_string(),
            1 => format!("(a:7 * {k} + {c})"),
            _ => "(a:7 + b:7)".to_string(),
        };
        let at = 2 + g.below(w - 3);
        let j = w - at;
        let (src, bits) = match i % 4 {
            // The carry of `T + (T << s)` against the masked low digit of `T << s`.
            0 => {
                let s = 1 + g.below(at - 1);
                let digit = (1u64 << (at - s)) - 1;
                let m = match g.below(4) {
                    0 => digit,
                    1 => digit | (g.below(1 << w) << (at - s)) & mask,
                    2 => digit & !(1 << g.below(at - s)),
                    _ => g.below(1 << w),
                };
                let d = match g.below(3) {
                    0 => String::new(),
                    1 => format!(" + {}", g.below(1 << w)),
                    _ => " + b:7".to_string(),
                };
                let carry = format!(
                    "zext<{j}>(trunc<{at}>({t} + ({t} << {s}){d}) <u trunc<{at}>(({t} & {m}) << {s}))"
                );
                let n = s.min(j);
                let mut src = format!(
                    "zext<{j}>(extract<{}, {n}>({t})) + extract<{at}, {j}>({t}) * {} + {carry}",
                    at - s,
                    (1 + (1u64 << s)) & ((1 << j) - 1)
                );
                if g.chance(1, 2) {
                    src.push_str(&format!(" - trunc<{j}>({t} & ({t} >>u {s}))"));
                }
                (src, j)
            }
            // Pieces of one word at two widths: a wider product right on `m` low bits.
            1 => {
                let m = 1 + g.below(w);
                let high = |g: &mut Rng, v: u64| (v ^ (g.below(1 << 5) << m)) & 0xfff;
                let (kw, cw) = (high(&mut g, k), high(&mut g, c));
                let ext = if g.chance(1, 2) { "sext" } else { "zext" };
                let wide = format!("({ext}<12>(a:7) * {kw} + {cw})");
                let word = format!("(a:7 * {k} + {c})");
                let lo = g.below(at);
                let carry = format!(
                    "zext<{j}>(trunc<{at}>({word} + b:7) <u trunc<{at}>({}))",
                    if g.chance(1, 2) { &wide } else { &word }
                );
                let src = format!(
                    "extract<{at}, {j}>({word}) + {carry} \
                     + zext<{j}>(extract<{lo}, {}>({wide})) * {} - zext<{j}>(extract<{lo}, {}>({word}))",
                    (at - lo).min(j),
                    1 + g.below(3),
                    (at - lo).min(j)
                );
                (src, j)
            }
            // A bitwise function of two slices beside the function of the words.
            2 => {
                let op = ["^", "|", "&"][g.below(3) as usize];
                let lo = at + g.below(2) - g.below(2);
                let n = j - 1;
                let src = format!(
                    "zext<7>(extract<{at}, {n}>(a:7) {op} extract<{lo}, {n}>(b:7)) * {} \
                     + ((a {op} b) & {}) * {k} + {c}",
                    (k << at) & mask,
                    (1u64 << at) - 1
                );
                (src, w)
            }
            // The carry and the borrow of an XOR or an OR in limbs over a truncated product.
            _ => {
                let p = format!("trunc<7>(zext<10>(a:7) * {})", k | (g.below(8) << w));
                let (f, and, by) = if g.chance(1, 2) {
                    ("^", format!("extract<{}, {j}>({p} & b:7)", at - 1), 1)
                } else {
                    ("|", format!("extract<{at}, {j}>({p} & b:7)"), 0)
                };
                let q = if g.chance(1, 4) {
                    "b:7".to_string()
                } else {
                    p.clone()
                };
                let src = format!(
                    "extract<{at}, {j}>({p}) + extract<{at}, {j}>(b:7) - {and} \
                     + zext<{j}>(trunc<{at}>({p} + b) <u trunc<{at}>({q})) \
                     - zext<{j}>(trunc<{at}>({p} + b) <u trunc<{at}>({p} {f} b)) + {}",
                    g.below(2) * by
                );
                (src, j)
            }
        };
        let mut cx = Context::new();
        let e = cx
            .parse(&src, &ParseOptions::width(Width::new(bits as u16).unwrap()))
            .unwrap_or_else(|err| panic!("{src}: {err}"));
        let out = eng.simplify(&mut cx, e).unwrap();
        assert!(
            equivalent(&mut cx, e, out.expr, &mut rng),
            "{src} vs {}",
            cx.display(out.expr)
        );
        changed += u64::from(out.changed);
    }
    assert!(changed > 400, "{changed}");
}

/// Limb arithmetic: sums of high digits and of carries written as comparisons of low digits,
/// each digit spelled every way the borrows pass reads one (a truncation, a mask, either under
/// a narrower truncation or a zero extension, a narrowed sum, a value that is small), stay
/// equal to themselves: exhaustively, at widths where a digit's reading can go wrong (a
/// narrowed sum that carries, a digit read at more bits than it has).
#[cfg(feature = "prove")]
#[test]
fn limb_carries_are_sound() {
    let eng = Engine::builder()
        .builtin()
        .strategy(Strategy::deobfuscate())
        .build()
        .unwrap();
    let mut g = Rng(0x11b5_0001);
    let mut rng = Rng(77);
    let mut changed = 0;
    for i in 0..900 {
        // Two symbols of 7 bits, or three of 4: every assignment is checked.
        let (w, syms): (u64, &[&str]) = if i % 3 == 0 {
            (4, &["a", "b", "c"])
        } else {
            (7, &["a", "b"])
        };
        let sym = |g: &mut Rng| format!("{}:{w}", syms[g.below(syms.len() as u64) as usize]);
        // In a third of the cases every value has high bits known zero: their sums do not
        // wrap, and a carry is read at any width.
        let bounded = g.chance(1, 3);
        let value = |g: &mut Rng| {
            let (x, y) = (sym(g), sym(g));
            let c = g.below(1 << w);
            let v = match g.below(9) {
                0 => x,
                1 => format!("({x} + {y})"),
                2 => format!("({x} - {y})"),
                3 => format!("({x} + {c})"),
                4 => format!("({x} ^ {y})"),
                5 => format!("({x} & {y})"),
                6 => format!("({x} * {c})"),
                7 => format!("({x} >>u {})", g.below(w)),
                _ => format!("({x} + ({y} << {}))", g.below(w)),
            };
            match g.below(2) {
                _ if !bounded => v,
                0 => format!("({v} >>u {})", 1 + g.below(w - 1)),
                _ => format!("({v} & {})", g.below(1 << (w - 1))),
            }
        };
        let k = 1 + g.below(w - 1);
        let mask = (1u64 << k) - 1;
        // `Q`, and with a narrowed side its low `k` bits as a sum of `k`-bit values (the
        // truncation pushed into `Q`, slices of other values beside it).
        let narrow = g.chance(1, 4);
        let (q, narrowed) = if narrow {
            let leaf = |g: &mut Rng| {
                let v = value(g);
                if g.chance(1, 2) {
                    (format!("trunc<{k}>({v})"), v)
                } else {
                    let n = format!("extract<{}, {k}>({v})", g.below(w - k + 1));
                    (n.clone(), format!("zext<{w}>({n})"))
                }
            };
            let ((n1, v1), (n2, v2)) = (leaf(&mut g), leaf(&mut g));
            let op = ["+", "-", "^"][g.below(3) as usize];
            (format!("({v1} {op} {v2})"), format!("({n1} {op} {n2})"))
        } else {
            (value(&mut g), String::new())
        };
        // `P = Q + D`, or unrelated values.
        let d = value(&mut g);
        let p = if g.chance(2, 3) {
            format!("({q} + {d})")
        } else {
            value(&mut g)
        };
        // The comparison's width, and a side: the low `k` bits of `v` there.
        let m = match g.below(3) {
            _ if narrow => k,
            0 => k,
            1 => w,
            _ => k + g.below(w - k + 1),
        };
        let digit = |g: &mut Rng, v: &str| {
            let small = 1 + g.below(k);
            match g.below(5) {
                // A value below `2^k`, which is its own low digit.
                0 => format!("zext<{m}>(trunc<{small}>({v}))"),
                1 if m == w => format!("({v} & {mask})"),
                2 if m > k => format!("(trunc<{m}>({v}) & {mask})"),
                3 if m < w => format!("trunc<{m}>({v} & {mask})"),
                _ => format!("zext<{m}>(trunc<{k}>({v}))"),
            }
        };
        // The sum's width, and a high digit there.
        let j = match g.below(3) {
            0 => w - k,
            1 => w,
            _ => 1 + g.below(w),
        };
        let high = |v: &str| {
            if j == w {
                format!("({v} >>u {k})")
            } else if k + j <= w {
                format!("extract<{k}, {j}>({v})")
            } else {
                format!("trunc<{j}>({v} >>u {k})")
            }
        };
        let mut src = String::new();
        let mut term = |g: &mut Rng, t: String| {
            let sign = if g.chance(1, 4) { '-' } else { '+' };
            src.push_str(&format!(" {sign} {t}"));
        };
        let ext = if g.chance(1, 4) { "sext" } else { "zext" };
        let x = digit(&mut g, &p);
        let y = if narrow { narrowed } else { digit(&mut g, &q) };
        let (x, y) = if g.chance(1, 4) { (y, x) } else { (x, y) };
        term(&mut g, format!("{ext}<{j}>({x} <u {y})"));
        for v in [p.clone(), q.clone(), d.clone(), format!("({p} - {q})")] {
            if g.chance(1, 2) {
                term(&mut g, high(&v));
            }
        }
        if g.chance(1, 3) {
            let (x, y) = (digit(&mut g, &q), digit(&mut g, &d));
            term(&mut g, format!("zext<{j}>({x} <u {y})"));
        }
        if g.chance(1, 3) {
            let v = value(&mut g);
            term(&mut g, format!("trunc<{j}>({v})"));
        }
        let src = format!("0:{j}{src}");
        let src = match g.below(3) {
            0 => src,
            1 => format!("trunc<{}>({src})", 1 + g.below(j)),
            _ => format!("({src}) & {}", g.below(1 << j)),
        };
        let mut cx = Context::new();
        let e = cx
            .parse(&src, &ParseOptions::width(Width::new(w as u16).unwrap()))
            .unwrap_or_else(|err| panic!("{src}: {err}"));
        let out = checked(&eng, &mut cx, e);
        assert!(
            equivalent(&mut cx, e, out.expr, &mut rng),
            "{src} vs {}",
            cx.display(out.expr)
        );
        changed += u64::from(out.changed);
    }
    assert!(changed > 600, "{changed}");
}

/// Carries of digits computed in a register narrower than the word: the low digits from
/// truncations of 7-bit values to 4 or 5 bits, the sum's high digits slices of the values
/// themselves that reach past the register's width, the sum read on more bits than the
/// register has above the digit. Checked at every assignment.
#[cfg(feature = "prove")]
#[test]
fn limb_lifted_digits_are_sound() {
    let eng = deobfuscator();
    let mut g = Rng(0x11b5_0005);
    let mut rng = Rng(81);
    let mut changed = 0;
    for _ in 0..700 {
        let w = 7u64;
        let reg = 4 + g.below(2);
        let k = 1 + g.below(reg - 1);
        let mask = (1u64 << k) - 1;
        let sym = |g: &mut Rng| ["a:7", "b:7"][g.below(2) as usize].to_string();
        let value = |g: &mut Rng| {
            let (x, y) = (sym(g), sym(g));
            let c = g.below(1 << w);
            match g.below(6) {
                0 | 1 => x,
                2 => format!("({x} + {y})"),
                3 => format!("({x} - {y})"),
                4 => format!("({x} ^ {c})"),
                _ => format!("({x} + {c})"),
            }
        };
        // A register's value and the word it is the low bits of.
        let low = |g: &mut Rng, v: &str| match g.below(3) {
            0 => (format!("trunc<{reg}>({v})"), v.to_string()),
            1 => (format!("-trunc<{reg}>({v})"), format!("-({v})")),
            _ => {
                let u = value(g);
                (
                    format!("(trunc<{reg}>({v}) + trunc<{reg}>({u}))"),
                    format!("({v} + {u})"),
                )
            }
        };
        let v = value(&mut g);
        let (qr, q) = low(&mut g, &v);
        let v = value(&mut g);
        let (dr, d) = low(&mut g, &v);
        let (pr, p) = if g.chance(3, 4) {
            (format!("({qr} + {dr})"), format!("({q} + {d})"))
        } else {
            let v = value(&mut g);
            low(&mut g, &v)
        };
        // The sum's width (the digits reach past the register), and what is read of it.
        let m = (reg - k + 1 + g.below(w - reg)).min(w - k);
        let j = 1 + g.below(m);
        let high = |v: &str| format!("extract<{k}, {m}>({v})");
        let mut src = String::new();
        let mut term = |g: &mut Rng, t: String| {
            let sign = if g.chance(1, 4) { '-' } else { '+' };
            src.push_str(&format!(" {sign} {t}"));
        };
        let carry = if g.chance(1, 5) {
            format!("zext<{m}>(({qr} & {mask}) != 0)")
        } else {
            format!("zext<{m}>(({pr} & {mask}) <u ({qr} & {mask}))")
        };
        term(&mut g, carry);
        for v in [p.clone(), q.clone(), d.clone()] {
            if g.chance(3, 4) {
                term(&mut g, high(&v));
            }
        }
        let src = format!("trunc<{j}>(0:{m}{src})");
        let mut cx = Context::new();
        let e = cx
            .parse(&src, &ParseOptions::width(Width::new(w as u16).unwrap()))
            .unwrap_or_else(|err| panic!("{src}: {err}"));
        let out = checked(&eng, &mut cx, e);
        assert!(
            equivalent(&mut cx, e, out.expr, &mut rng),
            "{src} vs {}",
            cx.display(out.expr)
        );
        changed += u64::from(out.changed);
    }
    assert!(changed > 350, "{changed}");
}

/// The reading of a carry by the sum of its sides with its carry out, at every assignment:
/// with `P = Q + D` of `W` bits and `S = zext(Q) + zext(D)` at `W' > W` bits, `lo_k(P) <u
/// lo_k(Q)` is `hi_k(S) − hi_k(Q) − hi_k(D)` on any `j` bits, every digit zero-extended. A
/// borrow against that word, `lo_k(P) <u lo_k(Q ^ D)` (or `Q | D`), is `hi_k(S) − hi_k(Q ^
/// D) − hi_k(2·zext(Q & D))` (or of `zext(Q & D)`) modulo `2^j` where `W' ≥ k + j`.
#[cfg(feature = "prove")]
#[test]
fn limb_wide_carry_identity() {
    let mut rng = Rng(91);
    for w in 2..=6u64 {
        for k in 1..w {
            for j in 1..=9u64 {
                let mask = (1u64 << k) - 1;
                // Digits of the values themselves, zero-extended to `j` bits.
                let high = |v: &str| {
                    let digit = format!("extract<{k}, {}>({v})", w - k);
                    match j.cmp(&(w - k)) {
                        core::cmp::Ordering::Less => format!("trunc<{j}>({digit})"),
                        core::cmp::Ordering::Equal => digit,
                        core::cmp::Ordering::Greater => format!("zext<{j}>({digit})"),
                    }
                };
                for wide in [w + 1, k + j, 2 * w] {
                    if wide <= w {
                        continue;
                    }
                    // A digit of a `wide`-bit value at `j` bits.
                    let digit = |v: &str| {
                        if k + j <= wide {
                            format!("extract<{k}, {j}>({v})")
                        } else {
                            format!("zext<{j}>(extract<{k}, {}>({v}))", wide - k)
                        }
                    };
                    let (q, d) = (format!("q:{w}"), format!("d:{w}"));
                    let s = format!("(zext<{wide}>({q}) + zext<{wide}>({d}))");
                    let mut pairs = vec![(
                        format!("zext<{j}>(({q} + {d} & {mask}) <u ({q} & {mask}))"),
                        format!("{} - {} - {}", digit(&s), high(&q), high(&d)),
                    )];
                    if wide >= k + j {
                        for (op, twice) in [("^", " * 2"), ("|", "")] {
                            let and = format!("(zext<{wide}>({q} & {d}){twice})");
                            pairs.push((
                                format!(
                                    "zext<{j}>(({q} + {d} & {mask}) <u (({q} {op} {d}) & {mask}))"
                                ),
                                format!(
                                    "{} - {} - {}",
                                    digit(&s),
                                    high(&format!("({q} {op} {d})")),
                                    digit(&and)
                                ),
                            ));
                        }
                    }
                    for (carry, reading) in pairs {
                        let mut cx = Context::new();
                        let opts = ParseOptions::width(Width::new(j as u16).unwrap());
                        let a = cx
                            .parse(&carry, &opts)
                            .unwrap_or_else(|e| panic!("{carry}: {e}"));
                        let b = cx
                            .parse(&reading, &opts)
                            .unwrap_or_else(|e| panic!("{reading}: {e}"));
                        assert!(equivalent(&mut cx, a, b, &mut rng), "{carry} vs {reading}");
                    }
                }
            }
        }
    }
}

/// A sum that reads more bits of a carry than its sides have above the digit: the carry is
/// read off the sum of the sides with its carry out.
#[cfg(feature = "prove")]
#[test]
fn limb_wide_carry_fixtures() {
    let eng = deobfuscator();
    // Two 16-bit registers added in limbs of 10 bits, the high sum kept at 16 bits: 7 bits
    // of the sum of the registers with its carry out.
    let high = "((x:16 >>u 10) + (y:16 >>u 10) + zext<16>((x + y & 1023) <u (x & 1023)))";
    // The high limb of `x ^ y` written `x + y − 2·(x & y)`: its carry and its borrow.
    let xor = "((x:16 >>u 10) + (y:16 >>u 10) - ((x & y) >>u 9) \
               + zext<16>((x + y & 1023) <u (x & 1023)) \
               - zext<16>((x + y & 1023) <u (x + y - (x & y) * 2 & 1023)))";
    for (src, w, want) in [
        (
            high.to_string(),
            16,
            "extract<10, 16>(zext<32>(x) + zext<32>(y))",
        ),
        // A mask later, of more bits than a register has above the limb.
        (
            format!("trunc<7>({high})"),
            16,
            "extract<10, 7>(zext<32>(x) + zext<32>(y))",
        ),
        // The limbs joined: the sum itself.
        (
            format!("(zext<32>({high}) << 10) | zext<32>(x + y & 1023)"),
            32,
            "zext<32>(x) + zext<32>(y)",
        ),
        // The carry and the borrow of one word: the word goes, the limb stays.
        (xor.to_string(), 16, "zext<16>(extract<10, 6>(x ^ y))"),
        // A constant added in limbs of 14 and 18 bits, the high sum read on 19.
        (
            "trunc<19>((x:32 * 77 >>u 14) + zext<32>((x * 77 + 567 & 16383) <u 567))".to_string(),
            32,
            "extract<14, 19>(zext<64>(x * 77) + 567)",
        ),
    ] {
        assert_eq!(simplify(&eng, &src, w), want, "{src}");
    }
}

/// Sums that read a carry on more bits than its sides have above the digit, checked at every
/// assignment: the high digits of two registers and the carry between them at up to twice
/// the registers' width, with the borrow of an XOR or OR written as a sum and its and-term,
/// a second carry out of the same or another word, read whole, truncated or masked.
#[cfg(feature = "prove")]
#[test]
fn limb_wide_carries_are_sound() {
    let eng = deobfuscator();
    let mut g = Rng(0x11b5_0021);
    let mut rng = Rng(92);
    let mut changed = 0;
    for i in 0..700 {
        let (w, syms): (u64, &[&str]) = if i % 3 == 0 {
            (4, &["a", "b", "c"])
        } else {
            (7, &["a", "b"])
        };
        let sym = |g: &mut Rng| format!("{}:{w}", syms[g.below(syms.len() as u64) as usize]);
        let value = |g: &mut Rng| {
            let (x, y) = (sym(g), sym(g));
            let c = g.below(1 << w);
            match g.below(7) {
                0..=2 => x,
                3 => format!("({x} ^ {y})"),
                4 => format!("({x} + {c})"),
                5 => format!("({x} * {c})"),
                _ => format!("{c}:{w}"),
            }
        };
        let k = 1 + g.below(w - 1);
        let mask = (1u64 << k) - 1;
        // The sum's width: more than the registers have above the digit.
        let m = w - k + 1 + g.below(w + k);
        let high = |v: &str| {
            if m <= w - k {
                format!("extract<{k}, {m}>({v})")
            } else {
                format!("zext<{m}>(extract<{k}, {}>({v}))", w - k)
            }
        };
        let (q, d) = (value(&mut g), value(&mut g));
        let p = format!("({q} + {d})");
        let mut src = String::new();
        // (`often`: in how many cases of four the term is there.)
        let mut term = |g: &mut Rng, t: String, often: u64| {
            if g.chance(often, 4) {
                let sign = if g.chance(1, 6) { '-' } else { '+' };
                src.push_str(&format!(" {sign} {t}"));
            }
        };
        term(&mut g, high(&q), 3);
        term(&mut g, high(&d), 3);
        term(
            &mut g,
            format!("zext<{m}>(({p} & {mask}) <u ({q} & {mask}))"),
            4,
        );
        // The borrow of `q ^ d` or `q | d` out of the same word, and its and-term.
        if g.chance(1, 2) {
            let (r, and) = match g.below(4) {
                0 => (format!("({p} - ({q} & {d}) * 2)"), 1),
                1 => (format!("({q} ^ {d})"), 1),
                2 => (format!("({p} - ({q} & {d}))"), 0),
                _ => (format!("({q} | {d})"), 0),
            };
            let ext = if g.chance(3, 4) { "sext" } else { "zext" };
            term(
                &mut g,
                format!("{ext}<{m}>(({p} & {mask}) <u ({r} & {mask}))"),
                4,
            );
            let digit = format!("extract<{}, {}>({q} & {d})", k - and, w - k + and);
            let digit = if m == w - k + and {
                digit
            } else {
                format!("zext<{m}>({digit})")
            };
            term(&mut g, format!("(0:{m} - {digit})"), 3);
            // The next handler's carry, out of the word made of this one's result.
            if g.chance(1, 2) {
                let e = value(&mut g);
                term(&mut g, high(&e), 3);
                term(
                    &mut g,
                    format!("zext<{m}>(({r} + {e} & {mask}) <u ({e} & {mask}))"),
                    4,
                );
            }
        } else if g.chance(1, 3) {
            // Another comparison of the same word, against anything.
            let v = value(&mut g);
            term(
                &mut g,
                format!("zext<{m}>(({p} & {mask}) <u ({v} & {mask}))"),
                4,
            );
        }
        let src = format!("0:{m}{src}");
        let src = match g.below(4) {
            0 | 1 => src,
            2 => format!("trunc<{}>({src})", 1 + g.below(m)),
            _ => format!("({src}) & {}", g.below(1 << m.min(16))),
        };
        let mut cx = Context::new();
        let e = cx
            .parse(&src, &ParseOptions::width(Width::new(w as u16).unwrap()))
            .unwrap_or_else(|err| panic!("{src}: {err}"));
        let out = checked(&eng, &mut cx, e);
        assert!(
            equivalent(&mut cx, e, out.expr, &mut rng),
            "{src} vs {}",
            cx.display(out.expr)
        );
        changed += u64::from(out.changed);
    }
    assert!(changed > 350, "{changed}");
}

/// Carries of middle digits: comparisons of one slice from above bit 0 of two values, a
/// constant added to either, in sums with the high digits above them, checked at every
/// assignment. The reading holds only where the difference's digit is not all ones, so half
/// the differences are constants and the rest any value.
#[cfg(feature = "prove")]
#[test]
fn limb_middle_digits_are_sound() {
    let eng = deobfuscator();
    let mut g = Rng(0x11b5_0004);
    let mut rng = Rng(80);
    let mut changed = 0;
    for i in 0..900 {
        let (w, syms): (u64, &[&str]) = if i % 3 == 0 {
            (5, &["a", "b"])
        } else {
            (7, &["a", "b"])
        };
        let sym = |g: &mut Rng| format!("{}:{w}", syms[g.below(syms.len() as u64) as usize]);
        let value = |g: &mut Rng| {
            let (x, y) = (sym(g), sym(g));
            let c = g.below(1 << w);
            match g.below(7) {
                0 | 1 => x,
                2 => format!("({x} + {y})"),
                3 => format!("({x} ^ {y})"),
                4 => format!("({x} * {c})"),
                5 => format!("({x} & {c})"),
                _ => format!("({x} + {c})"),
            }
        };
        // The digit `[o, o + n)`, with bits above it (or, one time in eight, none).
        let o = 1 + g.below(w - 2);
        let n = if g.chance(1, 8) {
            w - o
        } else {
            1 + g.below(w - o - 1)
        };
        let at = o + n;
        let q = value(&mut g);
        let d = if g.chance(1, 2) {
            format!("{}:{w}", g.below(1 << w))
        } else {
            value(&mut g)
        };
        let p = if g.chance(3, 4) {
            format!("({q} + {d})")
        } else {
            value(&mut g)
        };
        let side = |g: &mut Rng, v: &str| {
            let s = format!("extract<{o}, {n}>({v})");
            if g.chance(1, 3) {
                format!("({s} + {})", g.below(1 << n))
            } else {
                s
            }
        };
        let j = if at < w && g.chance(2, 3) {
            w - at
        } else {
            1 + g.below(w)
        };
        let high = |v: &str| {
            if at >= w {
                format!("trunc<{j}>({v})")
            } else if at + j <= w {
                format!("extract<{at}, {j}>({v})")
            } else {
                format!("zext<{j}>(extract<{at}, {}>({v}))", w - at)
            }
        };
        let mut src = String::new();
        let mut term = |g: &mut Rng, t: String| {
            let sign = if g.chance(1, 4) { '-' } else { '+' };
            src.push_str(&format!(" {sign} {t}"));
        };
        let ext = if g.chance(1, 4) { "sext" } else { "zext" };
        let (x, y) = (side(&mut g, &p), side(&mut g, &q));
        let (x, y) = if g.chance(1, 4) { (y, x) } else { (x, y) };
        term(&mut g, format!("{ext}<{j}>({x} <u {y})"));
        for v in [p.clone(), q.clone(), d.clone()] {
            if g.chance(2, 3) {
                term(&mut g, high(&v));
            }
        }
        let src = format!("0:{j}{src}");
        let src = match g.below(3) {
            0 => src,
            1 => format!("trunc<{}>({src})", 1 + g.below(j)),
            _ => format!("({src}) & {}", g.below(1 << j)),
        };
        let mut cx = Context::new();
        let e = cx
            .parse(&src, &ParseOptions::width(Width::new(w as u16).unwrap()))
            .unwrap_or_else(|err| panic!("{src}: {err}"));
        let out = checked(&eng, &mut cx, e);
        assert!(
            equivalent(&mut cx, e, out.expr, &mut rng),
            "{src} vs {}",
            cx.display(out.expr)
        );
        changed += u64::from(out.changed);
    }
    assert!(changed > 450, "{changed}");
}

/// Limb arithmetic: a narrow value added into the middle of a wide one. The identity first,
/// at every offset and width in a 7-bit word and every assignment: the carry out of the
/// digit `[o, o + n)` of `a` plus an `n`-bit `t`, written against `t` or against the digit,
/// is the high digit of `a + (zext(t) << o)` less that of `a`, and the borrow of the digit
/// less `t` the high digit of `a` less that of `a − (zext(t) << o)`. Then generated sums:
/// such a comparison (a constant or other terms beside the slice, the sides swapped, a
/// subtraction, a term on one side only, a constant added to the word; the sides in a
/// register wider than the digit) with the high digits above the digit, simplified and
/// checked at every assignment.
#[cfg(feature = "prove")]
#[test]
fn limb_shifted_digits_are_sound() {
    let mut rng = Rng(91);
    let w = 7u64;
    for o in 1..w - 1 {
        for n in 1..w - o {
            let (at, j) = (o + n, w - o - n);
            let s = format!("extract<{o}, {n}>(a:{w})");
            let q = format!("(zext<{w}>(t:{n}) << {o})");
            let high = |v: &str| format!("extract<{at}, {j}>({v})");
            let carry = format!("{} - {}", high(&format!("(a:{w} + {q})")), high("a"));
            let borrow = format!("{} - {}", high("a"), high(&format!("(a:{w} - {q})")));
            for (test, digits) in [
                (format!("{s} + t:{n} <u t:{n}"), &carry),
                (format!("{s} + t:{n} <u {s}"), &carry),
                (format!("{s} <u t:{n}"), &borrow),
            ] {
                let mut cx = Context::new();
                let opts = ParseOptions::width(Width::new(w as u16).unwrap());
                let a = cx.parse(&format!("zext<{j}>({test})"), &opts).unwrap();
                let b = cx.parse(digits, &opts).unwrap();
                assert!(equivalent(&mut cx, a, b, &mut rng), "{test} vs {digits}");
            }
        }
    }
    let eng = deobfuscator();
    let mut g = Rng(0x11b5_0021);
    let mut changed = 0;
    for i in 0..900 {
        let w: u64 = if i % 3 == 0 { 6 } else { 7 };
        // The digit `[o, o + n)`, with bits above it (or, one time in eight, none).
        let o = 1 + g.below(w - 2);
        let n = if g.chance(1, 8) {
            w - o
        } else {
            1 + g.below(w - o - 1)
        };
        let at = o + n;
        let word = match g.below(4) {
            0 | 1 => format!("a:{w}"),
            2 => format!("(a:{w} + {})", g.below(1 << w)),
            _ => format!("(a:{w} * 3)"),
        };
        // An `n`-bit value that is no slice: of a 3-bit symbol, a constant, a product, a
        // flag or another symbol beside it.
        let fit = |v: &str, bits: u64| match bits.cmp(&n) {
            core::cmp::Ordering::Less => format!("zext<{n}>({v})"),
            core::cmp::Ordering::Equal => v.to_string(),
            core::cmp::Ordering::Greater => format!("trunc<{n}>({v})"),
        };
        let narrow = |g: &mut Rng| {
            let b = fit("b:3", 3);
            match g.below(7) {
                0..=2 => b,
                3 => format!("({b} + {})", g.below(1 << n)),
                4 => format!("({b} * {})", g.below(1 << n) | 1),
                5 => fit(&format!("(c:4 <u {})", g.below(16)), 1),
                _ => format!("({b} ^ {})", fit("c:4", 4)),
            }
        };
        let s = format!("extract<{o}, {n}>({word})");
        let (t, u) = (narrow(&mut g), narrow(&mut g));
        let k = g.below(1 << n);
        let (x, y) = match g.below(12) {
            0 | 1 => (format!("{s} + {t}"), t.clone()),
            2 => (format!("{s} + {t} + {k}"), t.clone()),
            3 | 4 => (format!("{t} + {s}"), s.clone()),
            5 => (format!("{s} + {t} + {k}"), format!("{s} + {u}")),
            6 => (s.clone(), t.clone()),
            7 => (format!("{s} + {k}"), t.clone()),
            8 => (s.clone(), format!("{s} - {t}")),
            9 => (format!("{s} + {t}"), u.clone()),
            10 => (format!("{s} - {t}"), s.clone()),
            _ => (
                format!("extract<{o}, {n}>(({word} + {})) + {k}", g.below(1 << w)),
                s.clone(),
            ),
        };
        let (x, y) = if g.chance(1, 5) { (y, x) } else { (x, y) };
        // One time in four in a register wider than the digit: the sides zero-extended, the
        // word's slice also taken longer from under a mask (or, without one, with the bits
        // above the digit: another comparison).
        let (x, y) = if g.chance(1, 4) {
            let m = n + 1 + g.below(2);
            let mut held = |v: &str| {
                if *v == s && o + m <= w && g.chance(1, 2) {
                    if g.chance(3, 4) {
                        format!("extract<{o}, {m}>({word} & {})", (1u64 << at) - 1)
                    } else {
                        format!("extract<{o}, {m}>({word})")
                    }
                } else {
                    format!("zext<{m}>({v})")
                }
            };
            (held(&x), held(&y))
        } else {
            (x, y)
        };
        let j = if at < w && g.chance(2, 3) {
            w - at
        } else {
            1 + g.below(w)
        };
        let high = |v: &str| {
            if at >= w {
                format!("trunc<{j}>({v})")
            } else if at + j <= w {
                format!("extract<{at}, {j}>({v})")
            } else {
                format!("zext<{j}>(extract<{at}, {}>({v}))", w - at)
            }
        };
        let mut src = String::new();
        let mut term = |g: &mut Rng, t: String| {
            let sign = if g.chance(1, 4) { '-' } else { '+' };
            src.push_str(&format!(" {sign} {t}"));
        };
        let ext = if g.chance(1, 4) { "sext" } else { "zext" };
        let test = format!("{ext}<{j}>({x} <u {y})");
        let shifted = format!("(zext<{w}>({t}) << {o})");
        if g.chance(1, 2) {
            // As the limb code writes it: the carry beside the word's high digit, or the
            // high digit less the borrow.
            let sign = if g.chance(1, 4) { '-' } else { '+' };
            src.push_str(&format!(" + {} {sign} {test}", high(&word)));
        } else {
            term(&mut g, test);
            for v in [
                word.clone(),
                format!("({word} + {shifted})"),
                format!("({word} - {shifted})"),
            ] {
                if g.chance(1, 2) {
                    term(&mut g, high(&v));
                }
            }
        }
        let src = format!("0:{j}{src}");
        let src = match g.below(3) {
            0 => src,
            1 => format!("trunc<{}>({src})", 1 + g.below(j)),
            _ => format!("({src}) & {}", g.below(1 << j)),
        };
        let mut cx = Context::new();
        let e = cx
            .parse(&src, &ParseOptions::width(Width::new(w as u16).unwrap()))
            .unwrap_or_else(|err| panic!("{src}: {err}"));
        let out = checked(&eng, &mut cx, e);
        assert!(
            equivalent(&mut cx, e, out.expr, &mut rng),
            "{src} vs {}",
            cx.display(out.expr)
        );
        changed += u64::from(out.changed);
    }
    assert!(changed > 450, "{changed}");
}

/// Limb arithmetic: a narrow value added into the middle of a wide one, the carry out of the
/// limb written against the value or against the word's digit, is a slice of the word plus
/// the value shifted; the limb and its carry out are one slice.
#[cfg(feature = "prove")]
#[test]
fn limb_shifted_fixtures() {
    let eng = deobfuscator();
    for (src, w, want) in [
        // The carry out of the limb `[24, 36)` against the value added.
        (
            "let q = zext<12>(b:9); \
             zext<12>(q + extract<24, 12>(a:64) <u q) + extract<36, 12>(a)",
            12,
            "extract<12, 12>(zext<24>(b) + extract<24, 24>(a))",
        ),
        // Against the word's digit.
        (
            "let s = extract<20, 17>(a:64); \
             zext<9>(zext<17>(b:10) + s <u s) + extract<37, 9>(a)",
            9,
            "extract<17, 9>(zext<26>(b) + extract<20, 26>(a))",
        ),
        // The borrow of the digit less the value.
        (
            "extract<22, 10>(x:32) - zext<10>(extract<10, 12>(x) <u d:12)",
            10,
            "extract<12, 10>(extract<10, 22>(x) - zext<22>(d))",
        ),
        // The limb and its carry out, joined by a concatenation and by a shift.
        (
            "let q = zext<12>(b:9); let m = q + extract<24, 12>(a:64); \
             concat(zext<12>(m <u q) + extract<36, 12>(a), m)",
            24,
            "zext<24>(b) + extract<24, 24>(a)",
        ),
        (
            "let m = d:12 + extract<10, 12>(x:32); \
             zext<32>(m) | (zext<32>(zext<10>(m <u d) + extract<22, 10>(x)) << 12)",
            32,
            "zext<32>(zext<22>(d) + extract<10, 22>(x))",
        ),
        // The digits in a 16-bit register: the value's zero-extended, the word's a longer
        // slice from under a mask.
        (
            "zext<10>(zext<16>(extract<11, 11>(x:32) + e:11) <u extract<11, 16>(x & 0x3fffff)) \
             + extract<22, 10>(x)",
            10,
            "extract<11, 10>(zext<21>(e) + extract<11, 21>(x))",
        ),
        // A constant added limb by limb (11, 11 and 10 bits; 837 and 434 are its digits),
        // the middle limb's carry out compared in such a register.
        (
            "let m = x:32 * 0x2c51a7e3; \
             zext<10>(zext<16>(extract<11, 11>(m + 837) + 434) <u extract<11, 16>(m & 0x3fffff)) \
             + extract<22, 10>(m)",
            10,
            "extract<22, 10>(x * 0x2c51a7e3 + 0xd9345)",
        ),
    ] {
        assert_eq!(simplify(&eng, src, w), want, "{src}");
    }
}

/// Limb arithmetic: the carry out of a middle limb written with the carry into it (the other
/// operand's digit is all ones and a carry comes in, or the limb's sum wrapped) is the carry
/// at the limb's upper boundary, and the top limb the high digit of the word.
#[cfg(feature = "prove")]
#[test]
fn limb_detector_fixtures() {
    let eng = deobfuscator();
    for (src, want) in [
        // A constant added limb by limb (13, 13 and 6 bits): 421 and 2860 are its digits.
        (
            "let m = x * 0x2c51a7e3; let h = m >>u 13; let c = (m + 421 & 8191) <u 421; \
             let d = h + zext<32>(c) + 2860 & 8191; \
             trunc<6>(zext<8>(extract<26, 6>(m)) \
             + zext<8>((h + 1 & 8191 == 0) & c | (d <u 2860)))",
            "extract<26, 6>(x * 0x2c51a7e3 + 0x16581a5)",
        ),
        // A negation in limbs of 15, 15 and 2 bits: its borrows are `!= 0`.
        (
            "let h = x >>u 15; let b = -x & 32767 != 0; \
             let d = -(h + zext<32>(b)) & 32767; \
             trunc<2>(sext<8>((~h & 32767 == 0) & b | (d != 0)) - trunc<8>(x >>u 30))",
            "extract<30, 2>(-x)",
        ),
        // The top limb of `x + y` in limbs of 11, 11 and 10 bits, beside a third digit (no
        // slice of one word).
        (
            "let s = x + y; let c = (s & 2047) <u (x & 2047); \
             let m = (x >>u 11) + (y >>u 11) + zext<32>(c) & 2047; \
             trunc<10>((x >>u 22) + (y >>u 22) + (z >>u 22) \
             + zext<32>(((y >>u 11) + 1 & 2047 == 0) & c | (m <u (x >>u 11 & 2047))))",
            "extract<22, 10>(x + y) + extract<22, 10>(z)",
        ),
    ] {
        assert_eq!(simplify(&eng, src, 32), want, "{src}");
    }
}

/// Carry detectors with a carry in, generated: the words `q` and `d` and their sum, the limb
/// `[o, o + n)`, the carry into it and the test of a digit in several spellings, some of
/// them of another value (no detector then); every result is checked at every assignment.
#[cfg(feature = "prove")]
#[test]
fn limb_detectors_are_sound() {
    let eng = deobfuscator();
    let mut g = Rng(0x11b5_0007);
    let mut rng = Rng(83);
    let (mut changed, mut read) = (0, 0);
    for i in 0..1200 {
        let w: u64 = if i % 3 == 0 { 6 } else { 7 };
        let sym = |g: &mut Rng| format!("{}:{w}", ["a", "b"][g.below(2) as usize]);
        let value = |g: &mut Rng| {
            let (x, y) = (sym(g), sym(g));
            let c = g.below(1 << w);
            match g.below(6) {
                0 | 1 => x,
                2 => format!("({x} + {y})"),
                3 => format!("({x} ^ {y})"),
                4 => format!("({x} * {c})"),
                _ => format!("({x} + {c})"),
            }
        };
        // The limbs `[0, o)` and `[o, o + n)`, with bits above them.
        let o = 1 + g.below(w - 2);
        let n = 1 + g.below(w - o - 1);
        let at = o + n;
        let (mo, mn) = ((1u64 << o) - 1, (1u64 << n) - 1);
        let q = value(&mut g);
        let d = if g.chance(1, 2) {
            format!("{}:{w}", g.below(1 << at))
        } else {
            value(&mut g)
        };
        let p = format!("({q} + {d})");
        // (One time in ten another value: the detector of no word.)
        let wrong = |g: &mut Rng, v: &str| {
            if g.chance(1, 10) {
                value(g)
            } else {
                v.to_string()
            }
        };
        let low = |g: &mut Rng, v: &str| {
            if g.chance(1, 2) {
                format!("({v} & {mo})")
            } else {
                format!("zext<{w}>(trunc<{o}>({v}))")
            }
        };
        let hi = |v: &str| format!("({v} >>u {o})");
        // The carry into the limb.
        let other = if g.chance(1, 2) { q.clone() } else { d.clone() };
        let (cp, cq) = (wrong(&mut g, &p), wrong(&mut g, &other));
        let cin = format!("({} <u {})", low(&mut g, &cp), low(&mut g, &cq));
        // The digit of `p` against that of one operand, the other's tested.
        let (y, z) = if g.chance(1, 2) { (&q, &d) } else { (&d, &q) };
        let tested = wrong(&mut g, z);
        let dz = format!("extract<{o}, {n}>({tested})");
        let test = match g.below(4) {
            0 => format!("({dz} == {mn})"),
            1 => format!("(((zext<{w}>({dz}) + 1) & {mn}) == 0)"),
            2 => format!("((~{} & {mn}) == 0)", hi(&tested)),
            _ => format!("((({} + 1) & {mn}) == 0)", hi(&tested)),
        };
        let x = match g.below(3) {
            0 => format!("({} & {mn})", hi(&p)),
            1 => format!("zext<{w}>(extract<{o}, {n}>({p}))"),
            _ => format!("(({} + {} + zext<{w}>({cin})) & {mn})", hi(&q), hi(&d)),
        };
        let cmp = format!("({x} <u ({} & {mn}))", hi(y));
        let det = if g.chance(1, 2) {
            format!("(({test} & {cin}) | {cmp})")
        } else {
            format!("({cmp} | ({cin} & {test}))")
        };
        let j = if g.chance(2, 3) {
            w - at
        } else {
            1 + g.below(w)
        };
        let high = |v: &str| {
            if at + j <= w {
                format!("extract<{at}, {j}>({v})")
            } else {
                format!("zext<{j}>(extract<{at}, {}>({v}))", w - at)
            }
        };
        let mut src = String::new();
        let mut term = |g: &mut Rng, t: String| {
            let sign = if g.chance(1, 4) { '-' } else { '+' };
            src.push_str(&format!(" {sign} {t}"));
        };
        let ext = if g.chance(1, 4) { "sext" } else { "zext" };
        term(&mut g, format!("{ext}<{j}>({det})"));
        for v in [&p, &q, &d] {
            if g.chance(2, 3) {
                term(&mut g, high(v));
            }
        }
        let src = format!("0:{j}{src}");
        let src = match g.below(3) {
            0 => src,
            1 => format!("trunc<{}>({src})", 1 + g.below(j)),
            _ => format!("({src}) & {}", g.below(1 << j)),
        };
        let mut cx = Context::new();
        let e = cx
            .parse(&src, &ParseOptions::width(Width::new(w as u16).unwrap()))
            .unwrap_or_else(|err| panic!("{src}: {err}"));
        let out = checked(&eng, &mut cx, e);
        assert!(
            equivalent(&mut cx, e, out.expr, &mut rng),
            "{src} vs {}",
            cx.display(out.expr)
        );
        changed += u64::from(out.changed);
        read += u64::from(!cx.display(out.expr).to_string().contains("<u"));
    }
    // (614 are read without the detectors.)
    assert!(changed > 600 && read > 630, "{changed} {read}");
}

/// Limb arithmetic: a limb that is no slice of one word (a third value's digit added in it)
/// read for its carry-out detector as the digit of the word with that digit in its place; a
/// top limb in a register wider than the word, beside the digit of a wider value with the
/// word's low bits; a carry written `c <=u d` (of adding `2^n − c` to a digit) and one
/// written `x & M != M` (the borrow of subtracting `M`).
#[cfg(feature = "prove")]
#[test]
fn limb_detector_word_fixtures() {
    let eng = deobfuscator();
    for (src, want) in [
        // `x + y + (e << 11)` in limbs of 11, 11 and 10 bits, `e` a digit of a third value.
        (
            "let s = x + y; let c = (s & 2047) <u (x & 2047); let e = z & 2047; \
             let m = (x >>u 11) + (y >>u 11) + e + zext<32>(c) & 2047; \
             trunc<10>((x >>u 22) + (y + (e << 11) >>u 22) \
             + zext<32>(((y >>u 11) + e + 1 & 2047 == 0) & c | (m <u (x >>u 11 & 2047))))",
            "extract<22, 10>(((z & 2047) << 11) + x + y)",
        ),
        // A constant added in limbs of 13, 13 and 6 bits, the top limb in an 8-bit register
        // from the 64-bit product.
        (
            "let m = x * 0x2c51a7e3; let h = m >>u 13; let c = (m + 421 & 8191) <u 421; \
             let d = h + zext<32>(c) + 2860 & 8191; \
             zext<8>((h + 1 & 8191 == 0) & c | (d <u 2860)) \
             + extract<26, 8>(zext<64>(x) * 0x2c51a7e3)",
            "extract<26, 8>(zext<64>(x) * 0x2c51a7e3 + 0x16581a5)",
        ),
        // The borrow of subtracting 8191 from a low digit.
        (
            "let m = x * 0x2c51a7e3 + y; \
             trunc<19>((m >>u 13) + (z >>u 13) - zext<32>((m & 8191) != 8191))",
            "extract<13, 19>(z) + extract<13, 19>(x * 0x2c51a7e3 + y - 8191)",
        ),
    ] {
        assert_eq!(simplify(&eng, src, 32), want, "{src}");
    }
}

/// Carry detectors over limbs that are no slice of one word (a third value's digit beside
/// the operands'), the digit's test in three spellings, the top limb in a register wider than
/// the words (the digit of a wider value with the same low bits beside the carry), and
/// carries spelled `c <=u d` and `x != M`: generated, every result checked at every
/// assignment.
#[cfg(feature = "prove")]
#[test]
fn limb_detector_words_are_sound() {
    let eng = deobfuscator();
    let mut g = Rng(0x11b5_0019);
    let mut rng = Rng(89);
    let (mut changed, mut read) = (0, 0);
    for i in 0..1500 {
        let w: u64 = if i % 3 == 0 { 6 } else { 7 };
        // A register wider than the words.
        let ww = w + 1 + g.below(3);
        let name = |g: &mut Rng| ["a", "b"][g.below(2) as usize];
        // A value at the words' width, and the same at the register's (its low bits).
        let value = |g: &mut Rng| {
            let (x, y, c, kind) = (name(g), name(g), g.below(1 << w), g.below(6));
            let at = |b: u64| {
                let s = |v: &str| {
                    if b == w {
                        format!("{v}:{w}")
                    } else {
                        format!("zext<{b}>({v}:{w})")
                    }
                };
                match kind {
                    0 | 1 => s(x),
                    2 => format!("({} + {})", s(x), s(y)),
                    3 => format!("({} ^ {})", s(x), s(y)),
                    4 => format!("({} * {c})", s(x)),
                    _ => format!("({} + {c})", s(x)),
                }
            };
            (at(w), at(ww))
        };
        let o = 1 + g.below(w - 2);
        let n = 1 + g.below(w - o - 1);
        let at = o + n;
        let (mo, mn) = ((1u64 << o) - 1, (1u64 << n) - 1);
        let j = if g.chance(2, 3) {
            ww - at
        } else {
            1 + g.below(ww - at)
        };
        // The high digit from bit `k` of a value, of the word or of the register.
        let high = |g: &mut Rng, v: &(String, String), k: u64| {
            if g.chance(1, 2) {
                format!("extract<{k}, {j}>({})", v.1)
            } else if k + j <= w {
                format!("extract<{k}, {j}>({})", v.0)
            } else {
                format!("zext<{j}>(extract<{k}, {}>({}))", w - k, v.0)
            }
        };
        let mut src = String::new();
        let mut term = |g: &mut Rng, t: String| {
            let sign = if g.chance(1, 4) { '-' } else { '+' };
            src.push_str(&format!(" {sign} {t}"));
        };
        let ext = if g.chance(1, 4) { "sext" } else { "zext" };
        let q = value(&mut g);
        if i % 4 == 3 {
            // A carry in another spelling, beside the digits it is of.
            let wide = |v: &(String, String), k: u64| {
                (format!("({} + {k})", v.0), format!("({} + {k})", v.1))
            };
            let (carry, k, other) = match g.below(3) {
                // `c <=u lo(q)`: the carry of adding `2^at − c`.
                0 => {
                    let c = 1 + g.below((1 << at) - 1);
                    let carry = format!("({c}:{at} <=u trunc<{at}>({}))", q.0);
                    (carry, at, wide(&q, (1 << at) - c))
                }
                // The same of a middle digit: nothing carries into it.
                1 => {
                    let c = 1 + g.below(mn);
                    let carry = format!("({c}:{n} <=u extract<{o}, {n}>({}))", q.0);
                    (carry, at, wide(&q, ((1 << n) - c) << o))
                }
                // `lo(q) != M`: the borrow of subtracting `M`.
                _ => {
                    let low = if g.chance(1, 2) {
                        format!("({} & {mo})", q.0)
                    } else {
                        format!("zext<{w}>(trunc<{o}>({}))", q.0)
                    };
                    let m = (1u64 << w) - mo;
                    (format!("({low} != {mo})"), o, wide(&q, m))
                }
            };
            term(&mut g, format!("{ext}<{j}>({carry})"));
            for v in [&q, &other] {
                if g.chance(2, 3) {
                    let t = high(&mut g, v, k);
                    term(&mut g, t);
                }
            }
        } else {
            let d = if g.chance(1, 2) {
                let c = g.below(1 << at);
                (format!("{c}:{w}"), format!("{c}:{ww}"))
            } else {
                value(&mut g)
            };
            let p = (
                format!("({} + {})", q.0, d.0),
                format!("({} + {})", q.1, d.1),
            );
            // A third value's digit beside the limb's own, one time in two.
            let third = g.chance(1, 2);
            let e = if third {
                format!("({}:{w} & {mn})", name(&mut g))
            } else {
                format!("0:{w}")
            };
            // (One time in ten another value: the detector of no word.)
            let wrong = |g: &mut Rng, v: &str| {
                if g.chance(1, 10) {
                    value(g).0
                } else {
                    v.to_string()
                }
            };
            let low = |v: &str| format!("({v} & {mo})");
            let hi = |v: &str| format!("({v} >>u {o})");
            let other = if g.chance(1, 2) { &q.0 } else { &d.0 };
            let (cp, cq) = (wrong(&mut g, &p.0), wrong(&mut g, other));
            let cin = format!("({} <u {})", low(&cp), low(&cq));
            let (y, z) = if g.chance(1, 2) {
                (&q.0, &d.0)
            } else {
                (&d.0, &q.0)
            };
            let tested = wrong(&mut g, z);
            // The digit of the other operand plus the third's is all ones.
            let test = match g.below(3) {
                0 => format!("((({} + {e} + 1) & {mn}) == 0)", hi(&tested)),
                1 => format!("(extract<{o}, {n}>({tested}) == ~trunc<{n}>({e}))"),
                _ => format!("((({} + {e}) & {mn}) == {mn})", hi(&tested)),
            };
            let x = if g.chance(1, 2) {
                format!("(({} + {e}) & {mn})", hi(&p.0))
            } else {
                format!(
                    "(({} + {} + {e} + zext<{w}>({cin})) & {mn})",
                    hi(&q.0),
                    hi(&d.0)
                )
            };
            let cmp = format!("({x} <u ({} & {mn}))", hi(y));
            let det = if g.chance(1, 2) {
                format!("(({test} & {cin}) | {cmp})")
            } else {
                format!("({cmp} | ({cin} & {test}))")
            };
            term(&mut g, format!("{ext}<{j}>({det})"));
            // The word the limbs are of: with the third digit in its place.
            let whole = if third {
                (
                    format!("({} + ({e} << {o}))", p.0),
                    format!("({} + (zext<{ww}>({e}) << {o}))", p.1),
                )
            } else {
                p
            };
            for v in [&whole, &q, &d] {
                if g.chance(2, 3) {
                    let t = high(&mut g, v, at);
                    term(&mut g, t);
                }
            }
        }
        let src = format!("0:{j}{src}");
        let src = match g.below(3) {
            0 => src,
            1 => format!("trunc<{}>({src})", 1 + g.below(j)),
            _ => format!("({src}) & {}", g.below(1 << j)),
        };
        let mut cx = Context::new();
        let e = cx
            .parse(&src, &ParseOptions::width(Width::new(w as u16).unwrap()))
            .unwrap_or_else(|err| panic!("{src}: {err}"));
        let out = checked(&eng, &mut cx, e);
        assert!(
            equivalent(&mut cx, e, out.expr, &mut rng),
            "{src} vs {}",
            cx.display(out.expr)
        );
        changed += u64::from(out.changed);
        let text = cx.display(out.expr).to_string();
        read += u64::from(!(text.contains("<u") || text.contains("<=u") || text.contains("!=")));
    }
    // (481 are read without the limbs' words, the wider words and the spellings.)
    assert!(changed > 1100 && read > 500, "{changed} {read}");
}

/// Limb arithmetic: a carry whose difference the sum holds the high digit of in another
/// spelling (the partial product a schoolbook product adds, shifted into place, beside the
/// product of the word that a carry's side has become), and a carry between sides lifted
/// above their digit beside the word's high digit (the difference is read modulo the digit).
#[cfg(feature = "prove")]
#[test]
fn limb_difference_word_fixtures() {
    let eng = deobfuscator();
    for (src, w, want) in [
        // `x * 9653` in digits of 5 bits (21, 13 and 9), its limb from bit 10: the sum of
        // the low limbs is written as a product of the word right on 10 bits only, and the
        // sum holds the partial products from bit 5 on.
        (
            "let %0 = x:16 & 31; let %1 = (x >>u 5) & 31; let %2 = %0 * 21 + (%0 * 13 << 5); \
             trunc<5>(%0 * 9 + %1 * 13 + (x >>u 10) * 21 + (%0 * 13 >>u 5) + (%1 * 21 >>u 5) \
             + zext<16>(x * 38325 & 1023 <u %2 & 1023) + zext<16>(%2 & 1023 <u %0 * 21))",
            5,
            "extract<10, 5>(x * 9653)",
        ),
        // The low limb at its own width, the constant added to it written as a difference
        // (715 is -1333 modulo 2^11), beside the high digit of the word.
        (
            "zext<5>(77:11 - trunc<11>(x:16) * 715 <u trunc<11>(x) * 1333) \
             + extract<11, 5>(x * 27957)",
            5,
            "extract<11, 5>(x * 27957 + 77)",
        ),
    ] {
        assert_eq!(simplify(&eng, src, w), want, "{src}");
    }
}

/// Limb arithmetic: carries beside a high digit of a word that has the low bits of the
/// carry's difference, shifted into place, of one of its sides, or of neither (a reading
/// that took the word for the difference on more bits than the carry reads, or where it is
/// no spelling of it, would be caught); chains of two carries; and carries between sides at
/// the digit's own width beside the high digit of the word. Each stays equal to itself at
/// every assignment.
#[cfg(feature = "prove")]
#[test]
fn limb_difference_words_are_sound() {
    let eng = deobfuscator();
    let mut g = Rng(0x11b5_0041);
    let mut rng = Rng(101);
    let mut changed = 0;
    let w: u64 = 7;
    let mask = (1u64 << w) - 1;
    for i in 0..900 {
        let at = 2 + g.below(w - 3);
        let j = 1 + g.below(w - at);
        let x = match g.below(3) {
            0 => "a:7".to_string(),
            1 => format!("(a:7 ^ (a >>u {}))", 1 + g.below(w - 1)),
            _ => "(a:7 + b:7)".to_string(),
        };
        // Anything above bit `at`, or (one time in six) a bit below it: no spelling then.
        let noise = |g: &mut Rng| {
            if g.chance(1, 6) {
                1u64 << g.below(at)
            } else {
                (g.below(1 << w) << at) & mask
            }
        };
        let carry = |g: &mut Rng, p: &str, q: &str| {
            let (p, q) = (format!("trunc<{at}>({p})"), format!("trunc<{at}>({q})"));
            match g.below(4) {
                0 => format!("- sext<{j}>({p} <u {q})"),
                1 => format!("- ~zext<{j}>({p} <u {q})"),
                _ => format!("+ zext<{j}>({p} <u {q})"),
            }
        };
        let src = if i % 3 < 2 {
            // `P = Q + (A << s) + noise` in one spelling or another, the sum holding the
            // digit of `A` from bit `at − s`.
            let o = 1 + g.below(at);
            let s = at - o;
            let ka = g.below(1 << w) | 1;
            let a = match g.below(3) {
                0 => format!("({x} * {ka})"),
                1 => format!("(({x} & {}) * {ka})", (1u64 << o) - 1),
                _ => "b:7".to_string(),
            };
            let kq = g.below(1 << w);
            let q = match g.below(3) {
                0 => format!("({x} * {kq})"),
                1 => format!("{kq}:7"),
                _ => format!("(({x} & {}) * {kq})", (1u64 << at) - 1),
            };
            let up = (ka << s) & mask;
            let p = match g.below(3) {
                0 => format!("({q} + ({a} << {s}) + {x} * {})", noise(&mut g)),
                1 => format!("({x} * {} + {q})", (up + noise(&mut g)) & mask),
                _ => format!("({q} + ({a} << {s}))"),
            };
            let digit = if g.chance(1, 2) {
                format!("extract<{o}, {j}>({a})")
            } else {
                format!("trunc<{j}>({a} >>u {o})")
            };
            let mut src = format!("{digit} {}", carry(&mut g, &p, &q));
            // The sum before, with its own carry and its own digit.
            if g.chance(1, 2) {
                let kb = g.below(1 << w);
                let r = format!("({q} - b:7 * {kb} + {x} * {})", noise(&mut g));
                src.push_str(&format!(" {}", carry(&mut g, &q, &r)));
                if g.chance(1, 2) {
                    src.push_str(&format!(" + extract<{at}, {j}>(b * {kb})"));
                }
            }
            if g.chance(1, 3) {
                src.push_str(&format!(" + extract<{at}, {j}>({q})"));
            }
            src
        } else {
            // Sides at the digit's width, their difference a constant there (or not), beside
            // the high digit of the word (or of another).
            let kq = g.below(1 << at) | 1;
            let kn = if g.chance(1, 5) {
                g.below(1 << at)
            } else {
                (1u64 << at) - kq
            };
            let k = if g.chance(1, 5) {
                g.below(1 << w)
            } else {
                kq | (g.below(1 << w) << at) & mask
            };
            let c = g.below(1 << at);
            let t = format!("trunc<{at}>({x})");
            format!(
                "zext<{j}>({c}:{at} - {t} * {kn} <u {t} * {kq}) + extract<{at}, {j}>({x} * {k})"
            )
        };
        let mut cx = Context::new();
        let e = cx
            .parse(&src, &ParseOptions::width(Width::new(j as u16).unwrap()))
            .unwrap_or_else(|err| panic!("{src}: {err}"));
        let out = eng.simplify(&mut cx, e).unwrap();
        assert!(
            equivalent(&mut cx, e, out.expr, &mut rng),
            "{src} vs {}",
            cx.display(out.expr)
        );
        changed += u64::from(out.changed);
    }
    assert!(changed > 300, "{changed}");
}

/// Limb arithmetic: the limbs of one word, each under the coefficient of the one below shifted
/// by that limb's width, are the word (a product by a constant computed limb by limb is the
/// product of the word), in every spelling of a limb, modulo the bits read.
#[cfg(feature = "prove")]
#[test]
fn limb_join_fixtures() {
    let eng = deobfuscator();
    for (src, w, want) in [
        // Two limbs of 18 and 14 bits; the high one as a shift, as a slice, and as a slice of
        // the sign extension (whose high bits the coefficient's trailing zeros discard).
        (
            "(w & 0x3ffff) * 0x2f6b8d35 + (w >>u 18) * 0x34d40000",
            32,
            "w * 0x2f6b8d35",
        ),
        (
            "(w & 0x3ffff) * 0x2f6b8d35 + zext<32>(extract<18, 14>(w)) * 0x34d40000",
            32,
            "w * 0x2f6b8d35",
        ),
        (
            "(w & 0x3ffff) * 0x2f6b8d35 + extract<18, 32>(sext<64>(w)) * 0x34d40000",
            32,
            "w * 0x2f6b8d35",
        ),
        // Four digits, one of them a digit of a limb.
        (
            "(w & 127) * 11 + (w >>u 7 & 127) * 1408 + ((w >>u 14 & 0x3fff) & 127) * 180224 \
             + zext<32>(extract<21, 11>(w)) * 0x1600000",
            32,
            "w * 11",
        ),
        // The word less its low limb is its high limb, in place.
        ("w - (w & 0x1fff)", 32, "w & -8192"),
        // A product of the word (its high part of the constant) beside the limbs' products.
        (
            "w * 0x2f6b8000 + (w & 0x1fff) * 3381 + (w >>u 13) * 0x1a6a000",
            32,
            "w * 0x2f6b8d35",
        ),
        // The word joined by a `concat`, a limb put together again from its pieces.
        (
            "concat(h:6, concat(m:13, l:13)) * 0x2f6b8000 + concat(zext<19>(h), m) * 0x1a6a000 \
             + zext<32>(l) * 3381",
            32,
            "concat(h, concat(m, l)) * 0x2f6b8d35",
        ),
        // Modulo the bits read: the limbs of a 64-bit register's sum of which 32 are read.
        (
            "extract<18, 14>(zext<64>(x:32 & 0x3ffff) * 0x38d35 + sext<64>(x) * 0x2f680000 \
             + zext<64>(extract<18, 32>(sext<64>(x)) & 0x3ffff) * 0xe34d40000 + zext<64>(y:32))",
            14,
            "extract<18, 14>(x * 0x2f6b8d35 + y)",
        ),
    ] {
        assert_eq!(simplify(&eng, src, w), want, "{src}");
    }
}

/// Limb arithmetic: a limb's sum in a register wider than the limb, read by several users
/// each through a truncation of its own that is wider than the limb, is read on the bits that
/// reach the result, where its carry completes the high digit; every reader shares the limb.
#[cfg(feature = "prove")]
#[test]
fn limb_reader_fixtures() {
    let eng = deobfuscator();
    let limb = "((x >>u 18) + (y >>u 18) + zext<32>((x + y & 0x3ffff) <u (x & 0x3ffff)))";
    for (src, w, want) in [
        (
            format!(
                "trunc<14>(zext<32>(trunc<19>({limb})) * 3 + (zext<32>(trunc<31>({limb})) & v))"
            ),
            32,
            "let %0 = extract<18, 14>(x + y);\n%0 * 3 + (trunc<14>(v) & %0)",
        ),
        // The readers of a handler's and-term: twice the high limb and a bit of the low one
        // against the next digit, held up as a high limb is.
        (
            format!(
                "(concat(trunc<31>({limb}), extract<17, 1>(x + y)) + 5 \
                 & concat(trunc<18>({limb} + 77), extract<4, 14>(x + y))) << 18"
            ),
            32,
            "let %0 = x + y;\n\
             (zext<32>(extract<17, 14>(%0)) + 5 & zext<32>(extract<4, 14>(%0))) << 18",
        ),
    ] {
        assert_eq!(simplify(&eng, &src, w), want, "{src}");
    }
}

/// Limb arithmetic: sums of the limbs of a word, each limb spelled every way the limbs pass
/// reads one (a mask, a shift, a slice, a digit of a limb, a slice of the sign extension, the
/// pieces of a `concat`), under coefficients that double from limb to limb or do not, read
/// whole or under a truncation, a mask or a slice, stay equal to themselves: exhaustively.
#[cfg(feature = "prove")]
#[test]
fn limb_joins_are_sound() {
    let eng = deobfuscator();
    let mut g = Rng(0x11b5_0002);
    let mut rng = Rng(78);
    let mut changed = 0;
    for i in 0..1500 {
        // Two symbols of 7 bits, or three of 4: every assignment is checked.
        let (w, syms): (u64, &[&str]) = if i % 3 == 0 {
            (4, &["a", "b", "c"])
        } else {
            (7, &["a", "b"])
        };
        let full = (1u64 << w) - 1;
        let sym = |g: &mut Rng| format!("{}:{w}", syms[g.below(syms.len() as u64) as usize]);
        // The word: a symbol, a sum, or two slices put together by a `concat`.
        let x = match g.below(5) {
            0 => {
                let (p, q) = (sym(&mut g), sym(&mut g));
                format!("({p} + {q})")
            }
            1 => {
                let k = 1 + g.below(w - 1);
                let (p, q) = (sym(&mut g), sym(&mut g));
                let lo = g.below(k + 1);
                format!("concat(extract<{lo}, {}>({p}), trunc<{k}>({q}))", w - k)
            }
            _ => sym(&mut g),
        };
        // Its limbs, between cuts.
        let mut cuts = vec![0, w];
        for _ in 0..1 + g.below(3) {
            cuts.push(1 + g.below(w - 1));
        }
        cuts.sort_unstable();
        cuts.dedup();
        let k0 = g.below(1 << w) | u64::from(g.chance(2, 3));
        let mut src = format!("0:{w}");
        for c in cuts.windows(2) {
            let (lo, len) = (c[0], c[1] - c[0]);
            // (A limb left out now and then.)
            if g.chance(1, 8) {
                continue;
            }
            let mask = (1u64 << len) - 1;
            let limb = match g.below(6) {
                0 if lo == 0 => format!("({x} & {mask})"),
                1 => format!("zext<{w}>(extract<{lo}, {len}>({x}))"),
                // Right as a limb only under a coefficient whose trailing zeros discard the
                // sign's copies.
                2 => format!("extract<{lo}, {w}>(sext<{}>({x}))", 2 * w),
                3 => format!("(({x} & {}) >>u {lo})", (1u64 << (lo + len)) - 1),
                4 => format!("zext<{w}>(trunc<{len}>({x} >>u {lo}))"),
                _ => format!("(({x} >>u {lo}) & {mask})"),
            };
            // The coefficient of the lowest limb shifted to this one, mostly.
            let k = if g.chance(4, 5) {
                (k0 << lo) & full
            } else {
                g.below(1 << w)
            };
            src.push_str(&format!(" + {limb} * {k}"));
        }
        // The word itself, another value, a constant.
        if g.chance(1, 3) {
            src.push_str(&format!(" + {x} * {}", g.below(1 << w)));
        }
        if g.chance(1, 3) {
            src.push_str(&format!(" - {}", sym(&mut g)));
        }
        if g.chance(1, 3) {
            src.push_str(&format!(" + {}", g.below(1 << w)));
        }
        let src = match g.below(5) {
            0 | 1 => src,
            2 => format!("trunc<{}>({src})", 1 + g.below(w)),
            3 => format!("({src}) & {}", g.below(1 << w)),
            _ => {
                let lo = g.below(w);
                format!("extract<{lo}, {}>({src})", 1 + g.below(w - lo))
            }
        };
        let mut cx = Context::new();
        let e = cx
            .parse(&src, &ParseOptions::width(Width::new(w as u16).unwrap()))
            .unwrap_or_else(|err| panic!("{src}: {err}"));
        let out = checked(&eng, &mut cx, e);
        assert!(
            equivalent(&mut cx, e, out.expr, &mut rng),
            "{src} vs {}",
            cx.display(out.expr)
        );
        changed += u64::from(out.changed);
    }
    assert!(changed > 1200, "{changed}");
}

/// Limb arithmetic: a sum of high digits and a carry, read by two users each through a mask,
/// a truncation or a shift of its own, under a result that reads a few bits of them, stays
/// equal to itself: exhaustively. (The sum is simplified on the bits its users read between
/// them, where its carry is read as high digits.)
#[cfg(feature = "prove")]
#[test]
fn limb_readers_are_sound() {
    let eng = deobfuscator();
    let mut g = Rng(0x11b5_0003);
    let mut rng = Rng(79);
    let mut changed = 0;
    for i in 0..900 {
        let (w, syms): (u64, &[&str]) = if i % 3 == 0 {
            (4, &["a", "b", "c"])
        } else {
            (7, &["a", "b"])
        };
        let sym = |g: &mut Rng| format!("{}:{w}", syms[g.below(syms.len() as u64) as usize]);
        let value = |g: &mut Rng| {
            let (x, y) = (sym(g), sym(g));
            match g.below(4) {
                0 => format!("({x} + {y})"),
                1 => format!("({x} * {})", g.below(1 << w)),
                2 => format!("({x} ^ {y})"),
                _ => x,
            }
        };
        let k = 1 + g.below(w - 1);
        let mask = (1u64 << k) - 1;
        let (p, q) = (value(&mut g), value(&mut g));
        // The high limb of `p + q` as a register holds it, or a sum that only looks like it.
        let (cp, cq) = if g.chance(3, 4) {
            (format!("({p} + {q})"), p.clone())
        } else {
            (value(&mut g), value(&mut g))
        };
        let sign = if g.chance(1, 4) { '-' } else { '+' };
        let s = format!(
            "(({p} >>u {k}) + ({q} >>u {k}) {sign} zext<{w}>(({cp} & {mask}) <u ({cq} & {mask})))"
        );
        let reader = |g: &mut Rng| match g.below(5) {
            0 => format!("zext<{w}>(trunc<{}>({s}))", 1 + g.below(w)),
            1 => format!("({s} & {})", g.below(1 << w)),
            2 => format!("({s} << {})", g.below(w)),
            3 => format!("({s} + {})", g.below(1 << w)),
            _ => format!(
                "concat(trunc<{}>({s}), extract<{}, 1>({p}))",
                w - 1,
                g.below(w)
            ),
        };
        let (r1, r2) = (reader(&mut g), reader(&mut g));
        let v = sym(&mut g);
        let body = match g.below(4) {
            0 => format!("{r1} + {r2}"),
            1 => format!("({r1} & {r2})"),
            2 => format!("{r1} * 3 + ({r2} & {v})"),
            _ => format!("{r1} - ({r2} ^ {v})"),
        };
        let src = match g.below(5) {
            0 => body,
            1 | 2 => format!("trunc<{}>({body})", 1 + g.below(w - k)),
            3 => format!("({body}) & {}", g.below(1 << w)),
            _ => format!("({body}) << {}", 1 + g.below(w - 1)),
        };
        let mut cx = Context::new();
        let e = cx
            .parse(&src, &ParseOptions::width(Width::new(w as u16).unwrap()))
            .unwrap_or_else(|err| panic!("{src}: {err}"));
        let out = checked(&eng, &mut cx, e);
        assert!(
            equivalent(&mut cx, e, out.expr, &mut rng),
            "{src} vs {}",
            cx.display(out.expr)
        );
        changed += u64::from(out.changed);
    }
    assert!(changed > 600, "{changed}");
}

/// Limb arithmetic: an XOR or an OR with a constant written as a sum (`a + c − 2·(a & c)`,
/// `a + c − (a & c)`, the constant folded into the sum around it) is read as what it spells,
/// and meets the XOR itself in a carry's difference.
#[cfg(feature = "prove")]
#[test]
fn limb_mask_fixtures() {
    let eng = deobfuscator();
    for (src, w, want) in [
        (
            "x:32 * 77 + 0x5234 - 2 * (x * 77 + 0x4000 & 0x1234)",
            32,
            "x * 77 + 16384 ^ 4660",
        ),
        (
            "x:32 * 77 + 0x5234 - (x * 77 + 0x4000 & 0x1234)",
            32,
            "x * 77 + 16384 | 4660",
        ),
        ("x:32 + 0x2d5 - 2 * (x & 0x2d5)", 32, "x ^ 725"),
        (
            "(x:32 + y:32 ^ 0x1234) + 2 * (x + y & 0x1234)",
            32,
            "x + y + 4660",
        ),
        // Two carries of one handler: the first against the XOR, the second against a
        // constant, whose difference is the first's word less `2^26`.
        (
            "let s = x:32 + y:32 - 2 * (x & 0x2d5c1) + z:32; \
             zext<6>((s + 0x2d5c1 & 0x3ffffff) <u ((x ^ 0x2d5c1) & 0x3ffffff)) \
             + extract<26, 6>(y + z) + zext<6>((s + 9 & 0x3ffffff) <u 0x3fd2a48) - 1",
            6,
            "extract<26, 6>(y + z + (x ^ 0x2d5c1) + 0xfffd2a48) - extract<26, 6>(x)",
        ),
    ] {
        assert_eq!(simplify(&eng, src, w), want, "{src}");
    }
}

/// Limb arithmetic: the high digits of a product by a constant done digit by digit. A
/// partial product shifted into place (`a·(c << 11)`) meets the product's own high digit
/// (`a·c >> 11`), and the high limb of the schoolbook sum (the cross products modulo the
/// limb, the partial products' high digits and the two carries of the low limb) is the high
/// limb of the product of the word.
#[cfg(feature = "prove")]
#[test]
fn limb_digit_fixtures() {
    let eng = deobfuscator();
    let (k0, k1, k2) = (0x2b7u64, 0x1c35u64, 0x19u64);
    let high = format!(
        "let d0 = x:32 & 8191; let d1 = zext<32>(extract<13, 13>(x)); \
         let p = (x & 0x3ffffff) * {k0}; let w = p + d0 * {}; let l = p & 0x3ffffff; \
         trunc<6>(d1 * {} + d0 * {} + (x >>u 26) * {k0} + zext<32>(w & 0x3ffffff <u l) \
         + (d1 * {k0} >>u 13) + (d0 * {k1} >>u 13) + zext<32>(l <u d0 * {k0}))",
        k1 << 13,
        k1 + (77 << 6),
        k2 + (5 << 6),
    );
    for (src, w, want) in [
        (
            "let a = x:32 & 2047; let b = zext<32>(extract<11, 11>(x)); let p = a * 1021; \
             let q = p + (a * 333 << 11) & 0x3fffff; \
             trunc<10>((b * 333 + a * 97) + zext<32>(q <u p) + (b * 1021 >>u 11) + y:32 \
             + (a * 333 >>u 11) + zext<32>(z:32 & 0x3fffff <u q))"
                .to_string(),
            10,
            // (With the words pass: the word of the two low digits' product is read first.)
            "let %0 = extract<11, 21>(x);\n\
             extract<11, 10>(%0 * 1021) + extract<22, 10>(x * 0x184a6bfd \
             + zext<32>(%0) * 0xffe01800 + 0x3fffff - (z & 0x3fffff)) \
             + extract<22, 10>(x) * 3 + trunc<10>(y)",
        ),
        (high, 6, "extract<26, 6>(x * 0x6786a2b7)"),
    ] {
        assert_eq!(simplify(&eng, &src, w), want, "{src}");
    }
}

/// Limb arithmetic: sums over `a & c`, `a ^ c` and `a | c` for a constant `c`, beside the
/// terms of `a` and a constant, under the coefficients that make them one another or not,
/// stay equal to themselves: exhaustively.
#[cfg(feature = "prove")]
#[test]
fn limb_masks_are_sound() {
    let eng = deobfuscator();
    let mut g = Rng(0x11b5_0004);
    let mut rng = Rng(80);
    let mut changed = 0;
    for i in 0..1200 {
        let (w, syms): (u64, &[&str]) = if i % 3 == 0 {
            (4, &["a", "b", "c"])
        } else {
            (7, &["a", "b"])
        };
        let full = (1u64 << w) - 1;
        let sym = |g: &mut Rng| format!("{}:{w}", syms[g.below(syms.len() as u64) as usize]);
        // The value under the mask and its terms as a sum would spell them.
        let (p, q) = (sym(&mut g), sym(&mut g));
        let (k1, k2) = (g.below(1 << w), g.below(1 << w));
        let (a, spelled): (String, Vec<(String, u64)>) = match g.below(5) {
            0 => (format!("({p} + {q})"), vec![(p, 1), (q, 1)]),
            1 => (
                format!("({p} + {k1})"),
                vec![(p, 1), (format!("1:{w}"), k1)],
            ),
            2 => (format!("({p} * {k1})"), vec![(p, k1)]),
            3 => (
                format!("({p} - {q} + {k2})"),
                vec![(p, 1), (q, full), (format!("1:{w}"), k2)],
            ),
            _ => (p.clone(), vec![(p, 1)]),
        };
        let c = g.below(1 << w);
        // `k·(a ^ c)` or `k·(a | c)` as a sum over `a & c`, mostly; a term changed or left
        // out now and then.
        let k = 1 + g.below(full);
        let xor = g.chance(1, 2);
        let near = |g: &mut Rng, v: u64| if g.chance(5, 6) { v } else { g.below(1 << w) };
        let on_and = if xor { k.wrapping_mul(2) } else { k }.wrapping_neg() & full;
        let mut src = format!("({a} & {c}) * {}", near(&mut g, on_and));
        for (t, kt) in &spelled {
            if g.chance(7, 8) {
                let kt = near(&mut g, k.wrapping_mul(*kt) & full);
                src.push_str(&format!(" + {t} * {kt}"));
            }
        }
        if g.chance(5, 6) {
            src.push_str(&format!(" + {}", near(&mut g, k.wrapping_mul(c) & full)));
        }
        // The XOR, the OR and the value themselves beside it.
        for op in ["^", "|"] {
            if g.chance(1, 3) {
                let kt = near(&mut g, k.wrapping_neg() & full);
                src.push_str(&format!(" + ({a} {op} {c}) * {kt}"));
            }
        }
        if g.chance(1, 4) {
            src.push_str(&format!(" - {}", sym(&mut g)));
        }
        let src = match g.below(4) {
            0 | 1 => src,
            2 => format!("trunc<{}>({src})", 1 + g.below(w)),
            _ => {
                let lo = g.below(w);
                format!("extract<{lo}, {}>({src})", 1 + g.below(w - lo))
            }
        };
        let mut cx = Context::new();
        let e = cx
            .parse(&src, &ParseOptions::width(Width::new(w as u16).unwrap()))
            .unwrap_or_else(|err| panic!("{src}: {err}"));
        let out = checked(&eng, &mut cx, e);
        assert!(
            equivalent(&mut cx, e, out.expr, &mut rng),
            "{src} vs {}",
            cx.display(out.expr)
        );
        changed += u64::from(out.changed);
    }
    assert!(changed > 900, "{changed}");
}

/// Limb arithmetic: the high limb of a product by a constant done in digits (cross products,
/// the partial products' high digits, the carries of the low limb), and high digits of sums
/// whose constants differ by a multiple of the digit or whose coefficients are shifted into
/// place, with a term changed or left out now and then, stay equal to themselves:
/// exhaustively.
#[cfg(feature = "prove")]
#[test]
fn limb_digits_are_sound() {
    let eng = deobfuscator();
    let mut g = Rng(0x11b5_0005);
    let mut rng = Rng(81);
    let mut changed = 0;
    for i in 0..1200 {
        // A word of 8 bits in digits of 2 or 3, or two of 6 in digits of 2.
        let (w, syms): (u64, &[&str]) = if i % 3 == 0 {
            (6, &["a", "b"])
        } else {
            (8, &["a"])
        };
        let full = (1u64 << w) - 1;
        let sym = |g: &mut Rng| format!("{}:{w}", syms[g.below(syms.len() as u64) as usize]);
        let k = if w == 8 { 2 + g.below(2) } else { 2 };
        let (mask, mask2) = ((1u64 << k) - 1, (1u64 << (2 * k)) - 1);
        let near = |g: &mut Rng, v: u64| if g.chance(7, 8) { v } else { g.below(1 << w) };
        let x = sym(&mut g);
        let j = 1 + g.below(w - 2 * k);
        let src = if g.chance(2, 3) {
            // The schoolbook high limb of `x·(k0 + k1·2^k + k2·4^k)`.
            let (k0, k1, k2) = (g.below(mask + 1) | 1, g.below(mask + 1), g.below(mask + 1));
            let d0 = format!("({x} & {mask})");
            let d1 = match g.below(3) {
                0 => format!("zext<{w}>(extract<{k}, {k}>({x}))"),
                1 => format!("(({x} >>u {k}) & {mask})"),
                _ => format!("(({x} & {mask2}) >>u {k})"),
            };
            let d2 = if g.chance(1, 2) {
                format!("({x} >>u {})", 2 * k)
            } else {
                format!("extract<{}, {w}>(sext<{}>({x}))", 2 * k, 2 * w)
            };
            let p = format!("(({x} & {mask2}) * {})", near(&mut g, k0));
            let cross = if g.chance(1, 2) {
                format!("{d0} * {}", near(&mut g, (k1 << k) & full))
            } else {
                format!("({d0} * {k1} << {k})")
            };
            let (word, low) = (format!("({p} + {cross})"), format!("({p} & {mask2})"));
            // Any multiple of the limb's size on a cross product's constant.
            let above = |g: &mut Rng, v: u64| (v + (g.below(4) << j)) & full;
            let mut terms = vec![
                format!("{d1} * {}", above(&mut g, k1)),
                format!("{d0} * {}", above(&mut g, k2)),
                format!("{d2} * {}", near(&mut g, k0)),
                format!("zext<{w}>(({word} & {mask2}) <u {low})"),
                format!("(({d1} * {k0}) >>u {k})"),
                format!("(({d0} * {}) >>u {k})", near(&mut g, k1)),
                format!("zext<{w}>({low} <u ({d0} * {k0}))"),
            ];
            if g.chance(1, 6) {
                terms.remove(g.below(terms.len() as u64) as usize);
            }
            if g.chance(1, 4) {
                terms.push(sym(&mut g));
            }
            format!("trunc<{j}>({})", terms.join(" + "))
        } else {
            // A carry against a constant, beside high digits of the same sum under
            // constants that differ above the digit, and of a product shifted into place.
            let at = 2 * k;
            let y = sym(&mut g);
            let (c1, c2, t) = (g.below(1 << w), g.below(1 << at), 1 + g.below(at));
            let v = match g.below(3) {
                0 => format!("({x} + {y})"),
                1 => format!("({x} * {})", g.below(1 << w) | 1),
                _ => format!("({x} * {} << {t})", g.below(1 << (w - t))),
            };
            let high = |e: &str| format!("extract<{at}, {j}>({e})");
            let d = c1.wrapping_sub(c2) & full;
            let mut src = format!(
                "zext<{j}>(({v} + {c1} & {mask2}) <u {c2}) + {}",
                high(&format!("({v} + {})", near(&mut g, d)))
            );
            if g.chance(1, 2) {
                let up = (d + (g.below(4) << at)) & full;
                src.push_str(&format!(" - {}", high(&format!("({v} + {up})"))));
            }
            if g.chance(1, 2) {
                let c = g.below(1 << (w - t));
                let e = format!("({y} * {} + {})", c << t, g.below(1 << t));
                src.push_str(&format!(" + {}", high(&e)));
                if at - t + j <= w {
                    src.push_str(&format!(
                        " - extract<{}, {j}>({y} * {})",
                        at - t,
                        near(&mut g, c)
                    ));
                }
            }
            src
        };
        let mut cx = Context::new();
        let e = cx
            .parse(&src, &ParseOptions::width(Width::new(w as u16).unwrap()))
            .unwrap_or_else(|err| panic!("{src}: {err}"));
        let out = checked(&eng, &mut cx, e);
        assert!(
            equivalent(&mut cx, e, out.expr, &mut rng),
            "{src} vs {}",
            cx.display(out.expr)
        );
        changed += u64::from(out.changed);
    }
    assert!(changed > 900, "{changed}");
}

/// Limb arithmetic: a limb's sum read by users that have no observer in common below the root
/// (a product and a sum, the arms of a `select`), each through a truncation of its own. No
/// user's rewrite pays while the others hold the sum; at the root the sum is read on the bits
/// that reach the result, by every user at once (see `demanded::whole`).
#[cfg(feature = "prove")]
#[test]
fn limb_root_fixtures() {
    let eng = deobfuscator();
    // The high limb of `x + y` with a third digit: no slice of one word.
    let limb = "((x >>u 18) + (y >>u 18) + (z >>u 18) \
                + zext<32>((x + y & 0x3ffff) <u (x & 0x3ffff)))";
    // The carry of a product's digits beside a product nothing else reads narrowed: the
    // reading keeps the sum's own terms as they are.
    let (d, e) = ("(x & 0x1fff)", "((y >>u 13) & 0x1fff)");
    let (p, q) = (format!("({d} * (y & 0x1fff))"), format!("({d} * {e})"));
    let high = format!(
        "(({q} >>u 13) + zext<32>((({p} + ({q} << 13)) & 0x3ffffff) <u {p}) \
         + (x >>u 7) * (y >>u 9))"
    );
    for (src, w, want) in [
        // Read under 14 bits and under 7, by a product each.
        (
            format!("trunc<14>({limb}) * v + zext<14>(trunc<7>({limb})) * u"),
            14,
            "let %0 = extract<18, 14>(z) + extract<18, 14>(x + y);\n%0 * v + (%0 & 127) * u",
        ),
        // By the arms of a `select`.
        (
            format!("select(c:1, trunc<14>({limb}), zext<14>(trunc<7>({limb})))"),
            14,
            "let %0 = extract<18, 14>(z) + extract<18, 14>(x + y);\nselect(c, %0, %0 & 127)",
        ),
        // At the register's width, under 6 bits and under 3.
        (
            format!("zext<32>(trunc<6>({high})) * v + (zext<32>(trunc<3>({high})) ^ u)"),
            32,
            "let %0 = x & 8191;\n\
             let %1 = (x >>u 7) * (y >>u 9) \
             + zext<32>(extract<26, 6>(%0 * (y & 8191) + (%0 * zext<32>(extract<13, 13>(y)) << 13)));\n\
             (%1 & 63) * v + (%1 & 7 ^ u)",
        ),
    ] {
        assert_eq!(simplify(&eng, &src, w), want, "{src}");
    }
}

/// Limb arithmetic: a sum of high digits, a carry and sometimes a term of its own, read by
/// two users through a truncation, a mask or a shift each, under a root that is no observer
/// (a sum, a product, a `select`, a comparison of them), stays equal to itself: exhaustively.
/// (The sum is read at the root on the bits that reach the result.)
#[cfg(feature = "prove")]
#[test]
fn limb_roots_are_sound() {
    let eng = deobfuscator();
    let mut g = Rng(0x11b5_0005);
    let mut rng = Rng(81);
    let mut changed = 0;
    for i in 0..1200 {
        let (w, syms): (u64, &[&str]) = if i % 3 == 0 {
            (4, &["a", "b", "c"])
        } else {
            (6, &["a", "b"])
        };
        let sym = |g: &mut Rng| format!("{}:{w}", syms[g.below(syms.len() as u64) as usize]);
        let value = |g: &mut Rng| {
            let (x, y) = (sym(g), sym(g));
            match g.below(4) {
                0 => format!("({x} + {y})"),
                1 => format!("({x} * {})", g.below(1 << w)),
                2 => format!("({x} ^ {y})"),
                _ => x,
            }
        };
        let k = 1 + g.below(w - 1);
        let mask = (1u64 << k) - 1;
        let (p, q) = (value(&mut g), value(&mut g));
        let (cp, cq) = if g.chance(3, 4) {
            (format!("({p} + {q})"), p.clone())
        } else {
            (value(&mut g), value(&mut g))
        };
        let sign = if g.chance(1, 4) { '-' } else { '+' };
        // A term the carry's digits meet nothing of.
        let own = match g.below(4) {
            0 => format!(" + {} * {}", value(&mut g), value(&mut g)),
            1 => format!(" - ({} >>u {})", value(&mut g), g.below(w)),
            _ => String::new(),
        };
        let s = format!(
            "(({p} >>u {k}) + ({q} >>u {k}) {sign} \
             zext<{w}>(({cp} & {mask}) <u ({cq} & {mask})){own})"
        );
        let reader = |g: &mut Rng| match g.below(6) {
            0 => format!("zext<{w}>(trunc<{}>({s}))", 1 + g.below(w)),
            1 => format!("({s} & {})", g.below(1 << w)),
            2 => format!("({s} << {})", g.below(w)),
            3 => format!("({s} + {})", g.below(1 << w)),
            4 => format!("zext<{w}>(extract<{}, 1>({s}))", g.below(w)),
            _ => format!(
                "concat(trunc<{}>({s}), extract<{}, 1>({p}))",
                w - 1,
                g.below(w)
            ),
        };
        let (r1, r2) = (reader(&mut g), reader(&mut g));
        let v = sym(&mut g);
        let src = match g.below(7) {
            0 => format!("{r1} * {v} + {r2}"),
            1 => format!("select(t:1, {r1}, {r2})"),
            2 => format!("select({r1} <u {v}, {r2}, {v})"),
            3 => format!("{r1} - ({r2} ^ {v})"),
            4 => format!("({r1} << 1) + ({r2} >>u 1)"),
            5 => format!("zext<{w}>({r1} == {r2}) + {r1}"),
            _ => format!("select(t:1, {r1}, {v}) * {r2}"),
        };
        let mut cx = Context::new();
        let e = cx
            .parse(&src, &ParseOptions::width(Width::new(w as u16).unwrap()))
            .unwrap_or_else(|err| panic!("{src}: {err}"));
        let out = checked(&eng, &mut cx, e);
        assert!(
            equivalent(&mut cx, e, out.expr, &mut rng),
            "{src} vs {}",
            cx.display(out.expr)
        );
        changed += u64::from(out.changed);
    }
    assert!(changed > 800, "{changed}");
}

/// Limb arithmetic: comparisons of digits held in wider registers, and their borrow tests.
#[test]
fn limb_compare_fixtures() {
    let eng = Engine::standard();
    for (src, want) in [
        // Both sides fit in 21 bits: compared there, the masks gone.
        (
            "(x & 0x1fffff) <u (y & 0x1fffff)",
            "trunc<21>(x) <u trunc<21>(y)",
        ),
        (
            "(x & 0x1fffff) == (y & 0x1fffff)",
            "trunc<21>(y) == trunc<21>(x)",
        ),
        // A signed order of values whose sign bits are known zero is the unsigned one.
        (
            "(x & 0x1fffff) <s (y & 0x1fffff)",
            "trunc<21>(x) <u trunc<21>(y)",
        ),
        // A digit shared by two comparisons holds neither back.
        (
            "zext<32>((trunc<32>(y) & 0x3fff) <u (trunc<32>(z) & 0x3fff)) \
             + zext<32>((trunc<32>(y) & 0x3fff) <u (trunc<32>(x) & 0x3fff))",
            "let %0 = trunc<14>(y);\nzext<32>(%0 <u trunc<14>(z)) + zext<32>(%0 <u trunc<14>(x))",
        ),
        // The borrow test `~((b - a == 0) | (b <u a))` is `a <u b`, also where the difference
        // is spelled with a negation, and where that negation is used again.
        (
            "sext<32>(~((-(trunc<32>(x) & 0x3fff) + (trunc<32>(y) & 0x3fff) == 0) \
             | ((trunc<32>(y) & 0x3fff) <u (trunc<32>(x) & 0x3fff))))",
            "sext<32>(trunc<14>(x) <u trunc<14>(y))",
        ),
        (
            "zext<64>(~((-(x & 0x3fff) + (y & 0x3fff) == 0) | ((y & 0x3fff) <u (x & 0x3fff)))) \
             + -(x & 0x3fff)",
            "let %0 = x & 16383;\nzext<64>(%0 <u y & 16383) - %0",
        ),
        ("-x + y == 0", "x == y"),
        ("-x + y != 0", "x != y"),
        ("trunc<14>(x - y) == 0", "trunc<14>(x) == trunc<14>(y)"),
        ("trunc<14>(x - y) != 0", "trunc<14>(x) != trunc<14>(y)"),
    ] {
        assert_eq!(simplify(&eng, src, 64), want, "{src}");
    }
}

#[test]
fn order_bit_subset_fixtures() {
    let eng = Engine::standard();
    for (src, want) in [
        // Through truncations and extractions of a meet.
        ("trunc<16>(a) & 5 <u trunc<16>(~b & (a & 5))", "0:1"),
        (
            "extract<16, 16>(y) & 16389 <u extract<16, 16>(~x & (y & 0x140050000))",
            "0:1",
        ),
        (
            "zext<16>(extract<32, 1>(y)) <u extract<32, 16>(~x & (y & 0x140050000))",
            "0:1",
        ),
        ("trunc<16>(x & y) <=u trunc<16>(x)", "1:1"),
        // A lane minimum of a value and its meet with another is the meet.
        (
            "select(trunc<16>(x) <u trunc<16>(x & y), trunc<16>(x), trunc<16>(x & y))",
            "trunc<16>(x & y)",
        ),
        // Undecided where a bit of the mask escapes the window, or equality is possible (here
        // compared at the two bits both sides fit in).
        (
            "zext<16>(extract<32, 1>(y)) <u extract<32, 16>(~x & (y & 0x340050000))",
            "zext<2>(extract<32, 1>(y)) <u ~extract<32, 2>(x) & extract<32, 2>(y)",
        ),
        (
            "trunc<16>(x) <u trunc<16>(x | y)",
            "trunc<16>(x) <u trunc<16>(x | y)",
        ),
    ] {
        assert_eq!(simplify(&eng, src, 64), want, "{src}");
    }
}

/// `sext(c)` and `zext(c)` of a 1-bit `c` in one sum are one atom: sound (exhaustively), and
/// `zext(c) + sext(c)` cancels. (Not checked for idempotence: the linear pass alone was not
/// idempotent on these sums before either; the engine's rounds settle them.)
#[test]
fn linear_merges_both_readings_of_a_condition() {
    let eng = engine(vec![Phase::Linear]);
    let mut g = generator(0x5e_c71);
    let mut rng = Rng(24);
    let mut changed = 0;
    for _ in 0..1500 {
        let mut cx = Context::new();
        let w = 2 + g.rng.below(4) as u16;
        let width = Width::new(w).unwrap();
        let conds: Vec<Expr> = (0..2)
            .map(|k| cx.symbol(["p", "q"][k], Width::W1).unwrap())
            .collect();
        let mut e = cx.symbol("x", width).unwrap();
        for _ in 0..1 + g.rng.below(4) {
            let c = conds[g.rng.below(2) as usize];
            let t = if g.rng.chance(1, 2) {
                cx.zext(c, width).unwrap()
            } else {
                cx.sext(c, width).unwrap()
            };
            let k = cx.constant(&g.constant(w)).unwrap();
            let t = if g.rng.chance(1, 2) {
                cx.bin(BinOp::Mul, t, k).unwrap()
            } else {
                t
            };
            let op = [BinOp::Add, BinOp::Sub][g.rng.below(2) as usize];
            e = cx.bin(op, e, t).unwrap();
        }
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        let r = out.roots[0];
        assert!(
            equivalent(&mut cx, e, r.expr, &mut rng),
            "{} vs {}",
            cx.display(e),
            cx.display(r.expr)
        );
        changed += u64::from(r.changed);
    }
    assert!(changed > 300, "{changed}");
    let std = Engine::standard();
    let mut cx = Context::new();
    let c = cx.symbol("c", Width::W1).unwrap();
    let (z, s) = (
        cx.zext(c, Width::W64).unwrap(),
        cx.sext(c, Width::W64).unwrap(),
    );
    let e = cx.bin(BinOp::Add, z, s).unwrap();
    let r = std.simplify(&mut cx, e).unwrap().expr;
    assert_eq!(cx.display(r).to_string(), "0:64");
    assert_eq!(
        simplify(
            &std,
            "~x + zext<64>(trunc<16>(x) != -1) + sext<64>(trunc<16>(x) != -1)",
            64
        ),
        "~x"
    );
    // A lone `sext` stays: rewriting it to `−zext` would not be smaller.
    let p = cx.sext(c, Width::W64).unwrap();
    let five = cx
        .constant(&BitVec::wrapping_from_u64(Width::W64, 5))
        .unwrap();
    let e = cx.bin(BinOp::Add, p, five).unwrap();
    assert_eq!(std.simplify(&mut cx, e).unwrap().expr, e);
}

/// A parallel-prefix adder (or, with `sub`, subtractor) of `x` and `y` with its first `levels`
/// levels (shifts 1, 2, 4, …; a network of `w` bits needs `log2(w)`, see [`full_levels`]);
/// `shift` overrides some levels' shifts and `extra` is xor-ed into the result.
#[cfg(feature = "prove")]
fn prefix_network(sub: bool, levels: usize, shift: &[(usize, u32)], extra: &str) -> String {
    let mut s = String::from("let s = x ^ y; ");
    s += if sub {
        "let p0 = ~s; let g0 = ~x & y; "
    } else {
        "let p0 = s; let g0 = x & y; "
    };
    for k in 0..levels {
        let sh = shift
            .iter()
            .find(|&&(l, _)| l == k)
            .map_or(1u32 << k, |&(_, v)| v);
        s += &format!(
            "let g{n} = g{k} | (p{k} & (g{k} << {sh})); let p{n} = p{k} & (p{k} << {sh}); ",
            n = k + 1
        );
    }
    s += &format!("s ^ (g{levels} << 1){extra}");
    s
}

#[cfg(feature = "prove")]
fn full_levels(w: u16) -> usize {
    (u16::BITS - (w - 1).leading_zeros()) as usize
}

#[cfg(feature = "prove")]
fn deobfuscator() -> Engine {
    Engine::builder()
        .builtin()
        .strategy(Strategy::deobfuscate())
        .verify(Verify::strict())
        .build()
        .unwrap()
}

/// `e` simplified by `eng`, which verifies strictly: no candidate may have been rejected. A
/// pass that builds a candidate unequal to its node is caught by the verifier, the result
/// stays equal, and a test that compared results alone would take the pass for sound.
#[cfg(feature = "prove")]
fn checked(eng: &Engine, cx: &mut Context, e: Expr) -> crate::engine::RootOutcome {
    let out = eng.run(cx, &[e], Run::default()).unwrap();
    assert_eq!(out.stats.rejected, 0, "{}", cx.display(e));
    out.roots[0]
}

#[cfg(feature = "prove")]
#[test]
fn carries_recovers_prefix_adders_and_subtractors() {
    let eng = deobfuscator();
    for w in [8u16, 13, 16, 32, 33, 64] {
        for (sub, want) in [(false, "x + y"), (true, "x - y")] {
            let mut cx = Context::new();
            let src = prefix_network(sub, full_levels(w), &[], "");
            let e = cx
                .parse(&src, &ParseOptions::width(Width::new(w).unwrap()))
                .unwrap();
            let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
            assert_eq!(
                cx.display(out.roots[0].expr).to_string(),
                want,
                "{w} bits: {src}"
            );
            assert_eq!(out.roots[0].end, End::Completed);
            let c = out.stats.passes["carries"];
            assert_eq!((c.proved, c.changed), (1, 1), "{w} bits: {c:?}");
        }
    }
}

/// A ripple chain adding `a` and `b` (texts) at `w` bits: `w` half additions, each sum
/// `a ^ b` and carry `(a & b) << 1`.
#[cfg(feature = "prove")]
fn ripple_sum(a: &str, b: &str, w: u16) -> String {
    let mut s = format!("let a0 = {a}; let b0 = {b}; ");
    for k in 0..w {
        s += &format!(
            "let a{n} = a{k} ^ b{k}; let b{n} = (a{k} & b{k}) << 1; ",
            n = k + 1
        );
    }
    s + &format!("a{w}")
}

#[cfg(feature = "prove")]
#[test]
fn carries_recovers_ripple_chains_constants_and_three_atoms() {
    let eng = deobfuscator();
    let same = |src: &str, want: &str, w: u16| -> crate::engine::PassCounts {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::new(w).unwrap());
        let e = cx.parse(src, &o).unwrap();
        let want = cx.parse(want, &o).unwrap();
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        assert_eq!(
            out.roots[0].expr,
            want,
            "{w} bits: {}",
            cx.display(out.roots[0].expr)
        );
        out.stats.passes["carries"]
    };
    same(&ripple_sum("x", "y", 32), "x + y", 32);
    // With a constant, long carries happen only near `−K`: points there keep the partial sums
    // from the prover.
    let c = same(&ripple_sum("x", "0x55555555", 32), "x + 0x55555555", 32);
    assert!(c.refuted <= 1, "{c:?}");
    // Three atoms: a carry-save step, then a prefix adder of its sum and carries.
    let mut s = String::from(
        "let u = x ^ y ^ z; let v = ((x & y) | (x & z) | (y & z)) << 1; \
         let s = u ^ v; let p0 = s; let g0 = u & v; ",
    );
    for k in 0..4 {
        s += &format!(
            "let g{n} = g{k} | (p{k} & (g{k} << {sh})); let p{n} = p{k} & (p{k} << {sh}); ",
            n = k + 1,
            sh = 1 << k
        );
    }
    s += "s ^ (g4 << 1)";
    same(&s, "x + y + z", 16);
}

#[cfg(feature = "prove")]
#[test]
#[ignore = "heavy: a proof to the pass's caps; run with --release -- --ignored"]
fn carries_asks_a_region_past_its_caps_once() {
    // A 48-bit ripple chain is past the proof's caps; the region holding it, its double, is
    // larger still, and is not asked.
    let o = ParseOptions::width(Width::new(48).unwrap());
    let src = format!("{} << 1", ripple_sum("x", "y", 48));
    let mut rng = Rng(48);
    let mut cx = Context::new();
    let e = cx.parse(&src, &o).unwrap();
    let out = engine(vec![Phase::Carries])
        .run(&mut cx, &[e], Run::default())
        .unwrap();
    assert_eq!(out.roots[0].expr, e);
    let c = out.stats.passes["carries"];
    assert_eq!((c.proved, c.unknown), (0, 1), "{c:?}");
    // With the other passes, the demanded-bits pass rewrites the chain the shift reads only
    // the low 47 bits of (146 nodes to 143): a chain of its own, past the caps too, asked once.
    let mut cx = Context::new();
    let e = cx.parse(&src, &o).unwrap();
    let out = deobfuscator().run(&mut cx, &[e], Run::default()).unwrap();
    assert!(equivalent(&mut cx, e, out.roots[0].expr, &mut rng));
    assert_eq!(out.stats.passes["demanded"].changed, 1);
    let c = out.stats.passes["carries"];
    assert_eq!((c.proved, c.unknown), (0, 2), "{c:?}");
}

#[cfg(feature = "prove")]
#[test]
fn carries_leaves_a_prefix_tree_missing_its_last_level() {
    let eng = deobfuscator();
    let mut rng = Rng(7);
    for w in [8u16, 16, 32, 64] {
        for sub in [false, true] {
            let mut cx = Context::new();
            let o = ParseOptions::width(Width::new(w).unwrap());
            let e = cx
                .parse(&prefix_network(sub, full_levels(w) - 1, &[], ""), &o)
                .unwrap();
            let sum = cx.parse(if sub { "x - y" } else { "x + y" }, &o).unwrap();
            // Truncated, it is not the sum.
            assert!(matches!(
                crate::prove::equal(&mut cx, e, sum, &crate::prove::Config::default()).unwrap(),
                crate::prove::Outcome::Refuted(_)
            ));
            let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
            let r = out.roots[0].expr;
            assert_ne!(r, sum, "{w} bits");
            assert!(equivalent(&mut cx, e, r, &mut rng), "{w} bits");
            let c = out.stats.passes["carries"];
            assert_eq!((c.proved, c.changed), (0, 0), "{w} bits: {c:?}");
        }
    }
}

#[cfg(feature = "prove")]
#[test]
fn carries_refutes_a_network_wrong_only_at_rare_inputs() {
    // The adder xor a term that is nonzero only where `x` has 16 alternating bits under a set
    // bit of `y`: none of the pass's points has that, so only the proof tells.
    let rare: String = (0..16)
        .map(|k| match (k, k % 2) {
            (0, _) => "x".to_string(),
            (_, 0) => format!(" & (x << {k})"),
            _ => format!(" & ~(x << {k})"),
        })
        .collect();
    let eng = deobfuscator();
    let mut rng = Rng(11);
    for w in [32u16, 64] {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::new(w).unwrap());
        let src = prefix_network(false, full_levels(w), &[], &format!(" ^ ({rare} & y)"));
        let e = cx.parse(&src, &o).unwrap();
        let sum = cx.parse("x + y", &o).unwrap();
        assert!(matches!(
            crate::prove::equal(&mut cx, e, sum, &crate::prove::Config::default()).unwrap(),
            crate::prove::Outcome::Refuted(_)
        ));
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        let r = out.roots[0].expr;
        assert_ne!(r, sum, "{w} bits");
        assert!(equivalent(&mut cx, e, r, &mut rng), "{w} bits");
        let c = out.stats.passes["carries"];
        // The adder inside may become `x + y`; the whole is refuted.
        assert!(c.refuted >= 1, "{w} bits: {c:?}");
    }
}

#[cfg(feature = "prove")]
#[test]
fn carries_proves_within_what_is_left() {
    let eng = deobfuscator();
    let o = ParseOptions::width(Width::W32);
    let src = prefix_network(false, 5, &[], "");
    let nodes = super::carries::MAX_AIG_NODES as u64;
    // Room for a few hundred propagations: the proof (about 100,000) stops short, and the root
    // is not final.
    let mut cx = Context::new();
    let e = cx.parse(&src, &o).unwrap();
    let short = Budget::default().with_pass_work(nodes + 4_000);
    let out = eng
        .run(&mut cx, &[e], Run::default().with_per_call(short))
        .unwrap();
    assert_ne!(cx.display(out.roots[0].expr).to_string(), "x + y");
    assert_eq!(
        out.roots[0].end,
        End::BudgetTerminated(crate::engine::Exhausted::PassWork)
    );
    assert_eq!(out.stats.passes["carries"].proved, 0);
    // Less than a capped proof's whole cost is enough for this one.
    let mut cx = Context::new();
    let e = cx.parse(&src, &o).unwrap();
    let enough = Budget::default().with_pass_work(nodes + (1 << 20));
    let out = eng
        .run(&mut cx, &[e], Run::default().with_per_call(enough))
        .unwrap();
    assert_eq!(cx.display(out.roots[0].expr).to_string(), "x + y");
}

#[cfg(feature = "prove")]
#[test]
fn carries_is_sound_on_mutated_networks() {
    // 6-bit networks (shifts 1, 2, 4), some miswired: every result is checked at every input.
    let eng = engine(vec![Phase::Carries]);
    let mut rng = Rng(0xca44);
    let o = ParseOptions::width(Width::new(6).unwrap());
    let (mut changed, mut refuted) = (0, 0);
    for _ in 0..200 {
        let sub = rng.chance(1, 2);
        let levels = 1 + rng.below(3) as usize;
        let mut shift = Vec::new();
        if rng.chance(1, 2) {
            shift.push((rng.below(levels as u64) as usize, 1 + rng.below(5) as u32));
        }
        let extra = match rng.below(4) {
            0 => " ^ (x & (x << 1) & y)",
            1 => " ^ (y << 5)",
            _ => "",
        };
        let src = prefix_network(sub, levels, &shift, extra);
        let mut cx = Context::new();
        let e = cx.parse(&src, &o).unwrap();
        let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
        assert!(
            equivalent(&mut cx, e, out.roots[0].expr, &mut rng),
            "{src} became {}",
            cx.display(out.roots[0].expr)
        );
        let c = out.stats.passes["carries"];
        changed += c.changed;
        refuted += c.refuted;
    }
    assert!(changed > 0 && refuted > 0, "{changed} {refuted}");
}

#[cfg(feature = "prove")]
#[test]
fn carries_reads_shifts_of_one_atom_exactly() {
    // `a << k` is `2^k·a`, and 0 once the shifts reach the width: no proof needed, so none is
    // taken, and every result is equal to its input. (Inner shifts by more than 1 are their
    // own form, so the chain is read whole; `a << 1` alone is left as it is.)
    let eng = engine(vec![Phase::Carries]);
    let mut rng = Rng(0x5e1f);
    for w in [8u16, 64] {
        let o = ParseOptions::width(Width::new(w).unwrap());
        for (src, want) in [
            ("(x << 2) << 3".to_string(), "x << 5".to_string()),
            ("((x << 3) << 2) << 1".into(), "x << 6".into()),
            (format!("(x << {}) << 3", w - 2), format!("0:{w}")),
            ("x << 1".into(), "x << 1".into()),
        ] {
            let mut cx = Context::new();
            let e = cx.parse(&src, &o).unwrap();
            let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
            let r = out.roots[0].expr;
            assert_eq!(cx.display(r).to_string(), want, "{w} bits: {src}");
            assert!(equivalent(&mut cx, e, r, &mut rng), "{w} bits: {src}");
            assert_eq!(out.stats.passes["carries"].proofs, 0, "{w} bits: {src}");
        }
    }
}

/// The SWAR population count of `v` at `w` bits (Hacker's Delight), with the pair mask `m1`.
#[cfg(feature = "prove")]
fn swar(w: u16, v: &str, m1: u64) -> String {
    let ones = u64::MAX >> (64 - w);
    format!(
        "let a = {v} - (({v} >>u 1) & {m1:#x}); let b = (a & {:#x}) + ((a >>u 2) & {:#x}); \
         let c = (b + (b >>u 4)) & {:#x}; (c * {:#x}) >>u {}",
        ones / 5,
        ones / 5,
        ones / 17,
        ones / 255,
        w - 8
    )
}

#[cfg(feature = "prove")]
#[test]
fn carries_recognizes_bit_counts() {
    let eng = engine(vec![Phase::Carries]);
    let mut rng = Rng(0xb17c);
    for w in [16u16, 32, 64] {
        let o = ParseOptions::width(Width::new(w).unwrap());
        let ones = u64::MAX >> (64 - w);
        // Ones above the highest set bit: the smear, counted, is the leading zeros.
        let mut smear = String::from("let s0 = x; ");
        let mut k = 0;
        while 1u32 << k < u32::from(w) {
            smear += &format!(
                "let s{n} = s{k} | (s{k} >>u {sh}); ",
                n = k + 1,
                sh = 1u32 << k
            );
            k += 1;
        }
        let clz = format!("{smear}let v = ~s{k}; {}", swar(w, "v", ones / 3));
        let ctz = format!("let v = (x & -x) - 1; {}", swar(w, "v", ones / 3));
        for (src, want) in [
            (swar(w, "x", ones / 3), "popcnt(x)"),
            (clz, "clz(x)"),
            (ctz, "ctz(x)"),
            // A wrong pair mask: no count, and left as it was.
            (swar(w, "x", ones / 3 - 4), ""),
        ] {
            let mut cx = Context::new();
            let e = cx.parse(&src, &o).unwrap();
            let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
            let r = out.roots[0].expr;
            assert!(equivalent(&mut cx, e, r, &mut rng), "{w} bits: {src}");
            if want.is_empty() {
                assert_eq!(r, e, "{w} bits: {src} became {}", cx.display(r));
            } else {
                let want = cx.parse(want, &o).unwrap();
                assert_eq!(r, want, "{w} bits: {src} became {}", cx.display(r));
            }
        }
    }
}

/// A `w`-bit sum (or difference) split at bit `k`: the low halves added at `k + 1` bits, the
/// high ones apart, plus the carry read at bit `carry` of the low sum (`k` is right; `None`
/// drops it).
#[cfg(feature = "prove")]
fn split_sum(w: u16, k: u16, sub: bool, carry: Option<u16>) -> String {
    let (h, op) = (w - k, if sub { "-" } else { "+" });
    let c = carry.map_or(String::new(), |c| {
        format!(" {op} zext<{h}>(extract<{c}, 1>(s))")
    });
    format!(
        "let s = zext<{k1}>(trunc<{k}>(x)) {op} zext<{k1}>(trunc<{k}>(y)); \
         concat(extract<{k}, {h}>(x) {op} extract<{k}, {h}>(y){c}, trunc<{k}>(s))",
        k1 = k + 1
    )
}

#[cfg(feature = "prove")]
#[test]
fn carries_recombines_sums_split_at_any_boundary() {
    let eng = engine(vec![Phase::Carries]);
    let same = |src: &str, want: &str| {
        let mut cx = Context::new();
        let o = ParseOptions::width(Width::W64);
        let e = cx.parse(src, &o).unwrap();
        let want = cx.parse(want, &o).unwrap();
        let out = eng.simplify(&mut cx, e).unwrap();
        assert_eq!(out.expr, want, "{src}: got {}", cx.display(out.expr));
    };
    same(&split_sum(64, 17, false, Some(17)), "x + y");
    same(&split_sum(64, 32, false, Some(32)), "x + y");
    same(&split_sum(64, 17, true, Some(17)), "x - y");
    // The carry as the low sum's unsigned overflow.
    same(
        "let s = trunc<23>(x) + trunc<23>(y); \
         concat(extract<23, 41>(x) + extract<23, 41>(y) + zext<41>(s <u trunc<23>(x)), s)",
        "x + y",
    );
    // Without the carry, or with it read a bit too low, it is not the sum: unchanged.
    for wrong in [
        split_sum(64, 17, false, None),
        split_sum(64, 17, false, Some(16)),
    ] {
        same(&wrong, &wrong);
    }
}

#[cfg(feature = "prove")]
#[test]
fn carries_is_sound_on_split_sums() {
    // Every split point of every width from 4 to 10 bits, with the carry right, missing, or
    // read a bit too low: every result is checked, and the right ones are the sum.
    let eng = engine(vec![Phase::Carries]);
    let mut rng = Rng(0x5711);
    let mut recombined = 0;
    for w in 4u16..=10 {
        let o = ParseOptions::width(Width::new(w).unwrap());
        for k in 1..w {
            for sub in [false, true] {
                for carry in [Some(k), None, Some(k - 1)] {
                    let src = split_sum(w, k, sub, carry);
                    let mut cx = Context::new();
                    let e = cx.parse(&src, &o).unwrap();
                    let out = eng.run(&mut cx, &[e], Run::default()).unwrap();
                    let r = out.roots[0].expr;
                    assert!(
                        equivalent(&mut cx, e, r, &mut rng),
                        "{src} became {}",
                        cx.display(r)
                    );
                    if carry == Some(k) {
                        let want = cx.parse(if sub { "x - y" } else { "x + y" }, &o).unwrap();
                        assert_eq!(r, want, "{src}: got {}", cx.display(r));
                        recombined += 1;
                    }
                }
            }
        }
    }
    assert_eq!(recombined, 2 * (3 + 4 + 5 + 6 + 7 + 8 + 9));
}

// ----- opaque constants (`mba::sweep`) -------------------------------------------

#[cfg(feature = "mba")]
mod opaque_constants {
    use super::*;
    use crate::mba::MbaConfig;

    fn simplified(src: &str, fuel: u64) -> (Context, crate::engine::Outcome) {
        let eng = Engine::builder()
            .builtin()
            .strategy(
                Strategy::deobfuscate().with_mba(MbaConfig::default().with_opaque_sweep(true)),
            )
            .build()
            .unwrap();
        let mut cx = Context::new();
        let e = cx.parse(src, &ParseOptions::width(Width::W64)).unwrap();
        let out = eng
            .run(
                &mut cx,
                &[e],
                Run::default().with_per_call(Budget::fuel(fuel)),
            )
            .unwrap();
        (cx, out)
    }

    #[test]
    fn selected_word_bounds_use_only_closed_guard_domains() {
        for (guard, bias, counterexample) in [
            ("(a == 4) | (a == 12)", "0xffffffffffffffe0", None),
            // The first two equalities are not a closed domain any more.
            ("(a == 4) | (a == 12) | (a == 0)", "0xffffffffffffffe0", Some((0, 0))),
            // Same closed guard, but the selected unsigned sum can wrap below its bias.
            ("(a == 4) | (a == 12)", "0xfffffffffffffff8", Some((12, 15))),
        ] {
            let src = format!(
                "let w = select({guard}, a * a + 4, b & 3); \
                 let x = {bias} + (z & 15); \
                 0xffffffffffffffdf <=u select(extract<2, 1>(w), x * zext<64>(extract<4, 1>(w)), x)"
            );
            let (mut cx, out) = simplified(&src, 1 << 14);
            let result = out.roots[0].expr;
            if let Some((a, z)) = counterexample {
                let env = FnEnv(|key: &crate::SymbolKey, w| {
                    let v = match key.to_string().as_str() {
                        "a" => a,
                        "z" => z,
                        _ => 0,
                    };
                    Some(BitVec::wrapping_from_u64(w, v))
                });
                assert!(cx.eval(&[result], &env).unwrap()[0].is_zero(), "{src}");
            } else {
                assert_eq!(cx.as_const(result).unwrap(), Some(BitVec::from_bool(true)));
                assert!(out.stats.mba.opaque_proved > 0);
            }
        }
    }

    /// A constant hidden over three values that cancel (the FNV-1a prime), then read as a hash
    /// multiplier, through a high byte and through a cast.
    const PRIME: &str = "let p4 = ~b & ~(~c & a); let p6 = ~b & ~a; let p8 = ~p6 & c; \
         let p9 = ~(~b & c) & ~a; let p10 = ~(~(~a & b) & c); let p11 = p6 & c; \
         let p12 = ~(~b & ~(~c & ~a)); let p13 = ~(a & b) & ~p6; \
         let p14 = ~(p13 & c) & ~(~c & ~p13); let p15 = ~b & a; \
         let p16 = ~(p15 & c) & ~(~c & ~p15); \
         let p = p4 * 0x666665ffffffff52 + p8 * 0x999998fffffffefb + p9 * 0x99999a00000000ae \
         + p10 * 0xcccccbfffffffea4 + p11 * 0x666665ffffffff52 + p12 * 0x333332ffffffffa9 \
         + p14 * 0x666665ffffffff52 + p16 * 0xcccccd0000000057;";

    #[test]
    fn a_chain_of_hidden_constants_folds_operands_first() {
        let src = format!(
            "{PRIME} let h = (0xcbf29ce484222325 ^ (p >>u 40)) * p; \
             let g = zext<64>(extract<8, 8>(h)) * p; \
             g + (x ^ zext<64>(trunc<32>(h) & 39))"
        );
        let (mut cx, out) = simplified(&src, 1 << 14);
        let h: u64 =
            (0xcbf2_9ce4_8422_2325u64 ^ (0x100_0000_01b3u64 >> 40)).wrapping_mul(0x100_0000_01b3);
        let g = ((h >> 8) & 0xff).wrapping_mul(0x100_0000_01b3);
        let want = cx
            .parse(
                &format!("{g:#x} + (x ^ {:#x})", h & 39),
                &ParseOptions::width(Width::W64),
            )
            .unwrap();
        assert_eq!(out.roots[0].expr, want, "{}", cx.display(out.roots[0].expr));
        assert!(out.stats.mba.opaque_proved > 0);
    }

    /// Roots that share a hidden constant ask about it once: the sweep of the second reads what
    /// the sweep of the first found, so the two together ask no more than the first alone.
    #[test]
    fn roots_share_what_a_sweep_found() {
        let eng = Engine::builder()
            .builtin()
            .strategy(
                Strategy::deobfuscate().with_mba(MbaConfig::default().with_opaque_sweep(true)),
            )
            .build()
            .unwrap();
        let srcs = [
            format!("{PRIME} (0xcbf29ce484222325 ^ (p >>u 40)) * p + x"),
            format!("{PRIME} (p >>u 40) + (y ^ p)"),
        ];
        let run = |which: &[usize]| {
            let mut cx = Context::new();
            let roots: Vec<_> = which
                .iter()
                .map(|&i| {
                    cx.parse(&srcs[i], &ParseOptions::width(Width::W64))
                        .unwrap()
                })
                .collect();
            let out = eng
                .run(
                    &mut cx,
                    &roots,
                    Run::default().with_per_call(Budget::fuel(1 << 16)),
                )
                .unwrap();
            let shown: Vec<_> = out
                .roots
                .iter()
                .map(|r| cx.display(r.expr).to_string())
                .collect();
            (shown, out.stats.mba.opaque_asked)
        };
        let (first, second, both) = (run(&[0]), run(&[1]), run(&[0, 1]));
        assert_eq!(both.0, [first.0.clone(), second.0.clone()].concat());
        // `(y ^ p) + 1`, `p >> 40` being 1.
        assert_eq!(second.0, ["-(y ^ 0xfffffefffffffe4c)"]);
        assert!(first.1 > 0 && second.1 > 0);
        assert_eq!(both.1, first.1, "{first:?} {second:?} {both:?}");
    }

    #[test]
    fn a_guard_that_reads_constant_at_the_samples_is_not_folded() {
        // `x == k` is false at every sample point: the sum reads as 5, and is not.
        let (mut cx, out) = simplified("zext<64>(x == 0x1234567) + 5 + (y & (z | y)) - y", 1 << 14);
        let r = out.roots[0].expr;
        assert!(cx.as_const(r).unwrap().is_none(), "{}", cx.display(r));
        let want = cx
            .parse(
                "zext<64>(x == 0x1234567) + 5",
                &ParseOptions::width(Width::W64),
            )
            .unwrap();
        assert_eq!(r, want, "{}", cx.display(r));
    }

    /// What the sweep takes from facts that hold only under the run's assumptions, the result
    /// relies on them for: the guard above is false wherever `x` is assumed not to be `k`.
    #[test]
    fn what_the_sweep_takes_from_an_assumption_is_relied_on() {
        let eng = Engine::builder()
            .builtin()
            .strategy(
                Strategy::deobfuscate().with_mba(MbaConfig::default().with_opaque_sweep(true)),
            )
            .build()
            .unwrap();
        let o = ParseOptions::width(Width::W64);
        let mut cx = Context::new();
        let e = cx
            .parse("zext<64>(x == 0x1234567) + 5 + (y & (z | y)) - y", &o)
            .unwrap();
        let p = cx.parse("x != 0x1234567", &o).unwrap();
        let mut a = crate::Assumptions::new();
        let id = a.assume_true(&mut cx, p).unwrap();
        let out = eng
            .run(&mut cx, &[e], Run::default().with_assumptions(&a))
            .unwrap();
        let r = out.roots[0];
        assert_eq!(cx.display(r.expr).to_string(), "5:64");
        assert!(r.relies_on.may_use(id), "{:?}", r.relies_on);
        // With no assumption the guard stays, and nothing is relied on.
        let free = eng.run(&mut cx, &[e], Run::default()).unwrap().roots[0];
        assert!(cx.as_const(free.expr).unwrap().is_none() && free.relies_on.is_none());
    }
}

/// Two linear MBAs that are one function are equal, also where each side is as small as it
/// gets and neither is rewritten into the other: their difference has a zero signature.
#[cfg(feature = "prove")]
#[test]
fn an_equality_of_linear_mbas_is_decided_by_the_signature_of_their_difference() {
    for eng in [Engine::standard(), deobfuscator()] {
        for (src, want) in [
            // Disjoint terms: a sum is an or.
            ("(a^b^c^d) + (a&b&c&d) == ((a^b^c^d) | (a&b&c&d))", "1:1"),
            ("(a^b^c^d) + (a&b&c&d) != ((a^b^c^d) | (a&b&c&d))", "0:1"),
            (
                "(a&b&c) + (a|b|c|d) + (a^d) == (a|b|c|d) + (a|d) - (a&d) + (a&b&c)",
                "1:1",
            ),
            // Atoms that are not symbols.
            ("(x*y ^ z>>u 3) + 2*(x*y & z>>u 3) == x*y + (z>>u 3)", "1:1"),
        ] {
            check_to(&eng, src, want, 32);
            check_to(&eng, src, want, 64);
        }
        // Not the same function: left as it is (a corner where they differ decides nothing
        // about atoms that may never take it).
        let e = simplify(&eng, "(a^b^c^d) + (a&b&c) == ((a^b^c^d) | (a&b&c))", 32);
        assert!(e.contains("=="), "{e}");
    }
}