bitwright 0.32.10

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
//! A native prover (feature `prove`): expressions are bit-blasted into an and-inverter graph,
//! encoded as clauses, and decided by bitwright's own CDCL SAT solver. A proof of validity comes
//! with a certificate, the clauses and a DRUP proof of their unsatisfiability, that an
//! independent checker ([`Certificate::check`]) verifies, and that any DRAT checker can read
//! ([`Certificate::dimacs`], [`Certificate::drat`]). A refutation comes with a counterexample:
//! a value for every symbol, checked by bitwright's evaluator before it is returned. A question
//! the search does not settle within its budget says why ([`Unknown`]), and a [`Question`]
//! goes on from where the search stopped, under a larger budget, without starting over.
//! For a small joint output superset above unresolved predicates, [`domain::analyze`]
//! preserves shared Boolean structure under explicit premises without solving reachability.
//!
//! No other solver is involved. Bit-vector operators are blasted with bitwright's total
//! semantics (SMT-LIB's); floating-point operators with IEEE 754's and bitwright's canonical
//! NaN ([`crate::fp`]); extension operations through their [`expand`](crate::ext::ExtOp::expand)
//! definition, if they have one (otherwise the question is not decided).
//!
//! ```
//! use bitwright::prove::{Config, Outcome, equal};
//! use bitwright::{Context, ParseOptions, Width};
//!
//! let mut cx = Context::new();
//! let o = ParseOptions::width(Width::W32);
//! let (a, b) = (cx.parse("(x ^ y) + 2 * (x & y)", &o)?, cx.parse("x + y", &o)?);
//! let Outcome::Proved(Some(cert)) = equal(&mut cx, a, b, &Config::default().with_certificate(true))? else {
//!     panic!("not proved")
//! };
//! assert!(cert.check().is_ok());
//! let c = cx.parse("x | y", &o)?;
//! assert!(matches!(equal(&mut cx, a, c, &Config::default())?, Outcome::Refuted(_)));
//! # Ok::<(), bitwright::Error>(())
//! ```

pub mod aig;
pub mod blast;
mod cuts;
pub mod domain;
pub mod drup;
mod exhaustion;
pub mod finite;
mod fp;
mod incremental;
mod lean;
pub mod lrat;
mod relations;
mod rule;
mod sample_words;
pub mod sat;
mod sums;
mod sweep;
#[cfg(test)]
mod tests;

use crate::error::Error;
use crate::expr::{Context, Expr, OpCode};
use crate::facts::Assumptions;
use crate::hash::IdMap;
use crate::{BitVec, SymbolKey, Width};

pub use exhaustion::Exhaustion;
/// What an [`Incremental`] answers.
pub use incremental::Answer as IncrementalAnswer;
pub use incremental::{Incremental, Prepared};
pub(crate) use lean::lean_equation;
pub use lean::lean_theorem;
pub use rule::{RuleOutcome, WidthReport, rule, rule_all_widths};

use aig::{Aig, Cnf, L};
use blast::Bits;
use sat::{Answer, Lit, Solver, Step};
pub use sat::{Backend, Limits};

/// How assignments are selected before SAT search. Sampling can find a concrete
/// counterexample. Complete ordered enumeration on the word path can also prove validity;
/// partial coverage and pseudorandom samples cannot.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub enum SampleMode {
    /// Seeded pseudorandom assignments, with all-zero and all-one unknown bits first.
    #[default]
    Random,
    /// Enumerate assignments from zero upwards over the circuit's unknown input bits,
    /// least significant input first. Useful for small integer witnesses in wide domains.
    /// Multiple symbols' unknown bits are concatenated in circuit input order; declared
    /// fixed bits retain their values. Higher unknown bits stay zero until reached.
    Small,
    /// Enumerate small values of reconstructed words: unknown bits of larger pure
    /// concatenations and standalone symbols are visited from low to high. A bounded
    /// portfolio varies words separately and together within the shared sample allowance.
    /// Shared bits keep one assignment. Uses the bounded word sampler when supported, falling
    /// back to [`Small`](Self::Small) circuit ordering for other graphs.
    Words,
}

/// How hard to try, and what to keep.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Config {
    /// The most conflicts the SAT solver may meet (then [`Unknown::Budget`]).
    pub max_conflicts: u64,
    /// The most propagations the SAT solver may do (then [`Unknown::Budget`]; `u64::MAX`, the
    /// default: no limit). A conflict costs more propagation on a larger circuit, so this bounds
    /// the time a question takes more closely than conflicts do, and as deterministically.
    pub max_propagations: u64,
    /// The most AIG nodes the question may take (then [`Unknown::TooLarge`]).
    pub max_nodes: usize,
    /// Keep a [`Certificate`] for a proof.
    pub certificate: bool,
    /// Simplify the question with bitwright's own engine before blasting it (the
    /// deobfuscation strategy, with the MBA service and its opaque-constant sweep when the `mba`
    /// feature is on): identities the engine settles at the word level (products of MBA, say,
    /// which bit-level SAT finds hard) are answered at once, and the rest is blasted smaller.
    /// Not with a certificate: a certificate is of the question as asked, with its symbols'
    /// declared known bits.
    pub simplify: bool,
    /// Simplify without the one pass of the engine that commits rewrites it has derived but
    /// neither proved nor matched against a proved rule: the limb-arithmetic pass
    /// ([`Phase::Words`](crate::engine::Phase), integer identities checked at sample points).
    /// An answer then rests only on proved rules, passes that are decision procedures or
    /// prove what they commit, and the search. Off by default: that pass is what decides
    /// limb multiplications no search finishes.
    pub strict: bool,
    /// Whether the simplifier's limb-arithmetic pass writes a result only once a circuit and
    /// a search have proved it ([`Strategy::words_proved`](crate::engine::Strategy)): between
    /// the default, which takes the pass's derivations as they are, and
    /// [`strict`](Self::strict), which leaves the pass out.
    pub proved_words: bool,
    /// The engine's budget for [`simplify`](Self::simplify): caps in the units of
    /// [`Budget`](crate::engine::Budget), counts of work and never time, so a question stops at
    /// the same place on every run. [`Budget::default`](crate::engine::Budget::default) (the
    /// default) is generous; [`with_simplify_fuel`](Self::with_simplify_fuel) scales every cap
    /// by one number. A simplification its budget stops keeps the rewrites it made, and the
    /// rest of the question is blasted as it stands; [`Stats::simplification`] says what was
    /// spent and which cap stopped it.
    pub simplify_budget: crate::engine::Budget,
    /// Assignments of the symbols to evaluate before the search, 64 at a time
    /// (in batches of 64, selected by [`sample_mode`](Self::sample_mode)): a refutation that a fraction of
    /// the values give is found without the search, which needs many conflicts for it on a
    /// large circuit. For deep integer questions with simplification and random sampling,
    /// one batch can probe comparison literals before simplifying; a miss is not a proof.
    /// With [`word_sampling`](Self::word_sampling), supported samples precede bit-blasting. 0: none.
    pub samples: u32,
    /// Assignment selection for [`samples`](Self::samples). Default: [`SampleMode::Random`].
    pub sample_mode: SampleMode,
    /// Add certified transitive bit-equality lemmas exposed by the asserted query. Cycle
    /// contradictions are detected regardless; additional lemmas are opt-in because their
    /// effect on arithmetic search depends on the workload.
    pub relational_lemmas: bool,
    /// Initially prefer the unknown bits of small shift counts as decisions. This specializes
    /// variable shifts during search without restricting which selector values are allowed.
    pub selector_branching: bool,
    /// Cancel a shared input through bounded XOR paths at every bitwise boundary. This can
    /// remove direct input dependencies through odd products, but changes arithmetic search
    /// behavior; opt-in pending workload-specific measurement.
    pub input_cancellation: bool,
    /// Factor ORs of XORs with a shared operand at one-valued equality target bits.
    /// Opt-in because the smaller encoding can alter longer arithmetic searches adversely.
    pub join_factoring: bool,
    /// Inline private intermediate XORs into three-input parity gates. This reduces encoding
    /// variables without changing the clause count, but its search performance is workload-dependent.
    pub xor3_encoding: bool,
    /// Compress modular multiplication columns before one final carry-propagating addition.
    /// Opt-in pending measurements on arithmetic search workloads.
    pub carry_save_multiplication: bool,
    /// Try samples directly on a bounded integer DAG of at most 64-bit words before
    /// bit-blasting. Ordered modes can prove after enumerating every legal input assignment;
    /// partial samples and unsupported DAGs use the circuit path. Certificates use that path
    /// even after complete enumeration.
    /// [`SampleMode::Words`] also enables this path.
    pub word_sampling: bool,
    /// Decide a question by evaluating it at every legal assignment when it has at most this
    /// many unknown input bits, before bit-blasting. It applies to bounded integer DAGs of at
    /// most 64-bit words, the same as [`word_sampling`](Self::word_sampling), and costs
    /// `2^bits` evaluations, in parallel across threads; it is not bounded by the search
    /// limits. A failing assignment is replayed on the original question. A proof asked for
    /// with a [`certificate`](Self::certificate) comes with an [`Exhaustion`], which checks the
    /// question's bit-blasted circuit at every assignment of its inputs, independently of the
    /// word-level decision. If that circuit has more inputs than this bound (it may keep bits
    /// the word program fixes from scoped assumptions as constrained inputs), the search
    /// decides the question instead. 0 (the default): never.
    pub exhaustive_inputs: u8,
    /// Write the sums of the question one way before blasting it: both sides read as linear
    /// forms over slices of their values (carries of limbs as the high digits they stand
    /// for), and built again in one order, so that what the two sides add up the same way is
    /// one circuit and what cancels is gone. An equality at every input; not for a
    /// [`certificate`](Self::certificate), which is of the question as asked. Opt-in: it
    /// helps where the two sides are one arithmetic written twice (a limb against its word),
    /// and changes the search elsewhere.
    pub shared_sums: bool,
    /// Read the divisions by constants that a compiler wrote as products by magic numbers
    /// (`trunc((zext(x) · M) >>u k)`, a multiply-high and a shift, the signed forms) as the
    /// divisions they are, before the question is blasted: the simplifier's division pass
    /// alone, for a question asked without the simplifier ([`simplify`](Self::simplify)
    /// reads them already). A multiplier's circuit against a divider's is beyond a search
    /// (the 32-bit quotient by 10 is undecided after 200,000 conflicts); read, both sides
    /// are one division. An equality at every input; not for a
    /// [`certificate`](Self::certificate). Opt-in.
    pub divisions: bool,
    /// Sweep the circuit before the first search: nodes that simulation shows equal (or
    /// complementary) are proved so one pair at a time, from the inputs up, by a solver of
    /// the circuit's clauses alone, and each equivalence is given to the search as clauses.
    /// Two circuits for one function often share most of their inner nodes (a prefix adder
    /// and `x + y` have the same carries), and a search over their miter alone rediscovers
    /// each of them inside one refutation: swept, the 64-bit prefix adder against `x + y`
    /// takes 0.29 M propagations instead of 0.54 M, the 32-bit SWAR population count against
    /// `popcnt` 24 k instead of 148 k, and a 64-bit MBA rewriting of `x - y` 0.56 M instead of
    /// 7.6 M.
    ///
    /// The circuit is simulated first, and only swept when the simulation shows enough of
    /// that sharing: classes of nodes that agree joining the two sides of an equality, at
    /// least 4 and one per 10 nodes private to a side (a question that is no equality: classes
    /// of two nodes or more, against all its nodes). Products, or the shift-and-xor addition
    /// loop against `x + y`, show little and are not swept; the simulation is all they cost.
    /// A sweep is part of preparing the question: it takes at most half of the configured
    /// limits ([`max_conflicts`](Self::max_conflicts), [`max_propagations`](Self::max_propagations))
    /// before any search, whatever the searches' own, so a question resumed in steps is swept
    /// as one asked at once; its work counts in the question's totals as work done before the
    /// first search; and with a [`certificate`](Self::certificate) its lemmas are logged so the
    /// certificate still checks. See [`Stats::sweeping`]. On by default.
    pub sweeping: bool,
}

impl Default for Config {
    fn default() -> Self {
        Config {
            max_conflicts: 1_000_000,
            max_propagations: u64::MAX,
            max_nodes: 4_000_000,
            certificate: false,
            simplify: true,
            strict: false,
            proved_words: false,
            simplify_budget: crate::engine::Budget::default(),
            samples: 256,
            sample_mode: SampleMode::Random,
            relational_lemmas: false,
            selector_branching: false,
            input_cancellation: false,
            join_factoring: false,
            xor3_encoding: false,
            carry_save_multiplication: false,
            word_sampling: false,
            exhaustive_inputs: 0,
            shared_sums: false,
            divisions: false,
            sweeping: true,
        }
    }
}

setters!(Config {
    with_max_conflicts: max_conflicts: u64,
    with_max_propagations: max_propagations: u64,
    with_max_nodes: max_nodes: usize,
    with_certificate: certificate: bool,
    with_simplify: simplify: bool,
    with_strict: strict: bool,
    with_proved_words: proved_words: bool,
    with_simplify_budget: simplify_budget: crate::engine::Budget,
    with_samples: samples: u32,
    with_sample_mode: sample_mode: SampleMode,
    with_relational_lemmas: relational_lemmas: bool,
    with_selector_branching: selector_branching: bool,
    with_input_cancellation: input_cancellation: bool,
    with_join_factoring: join_factoring: bool,
    with_xor3_encoding: xor3_encoding: bool,
    with_carry_save_multiplication: carry_save_multiplication: bool,
    with_word_sampling: word_sampling: bool,
    with_exhaustive_inputs: exhaustive_inputs: u8,
    with_shared_sums: shared_sums: bool,
    with_divisions: divisions: bool,
    with_sweeping: sweeping: bool,
});

impl Config {
    /// Sets [`simplify_budget`](Self::simplify_budget) to
    /// [`Budget::fuel`](crate::engine::Budget::fuel)`(fuel)`: `fuel` node visits of the engine
    /// and every other cap in proportion (the default is `1 << 22`).
    pub fn with_simplify_fuel(self, fuel: u64) -> Self {
        self.with_simplify_budget(crate::engine::Budget::fuel(fuel))
    }

    /// The search's limits: [`max_conflicts`](Self::max_conflicts) and
    /// [`max_propagations`](Self::max_propagations).
    pub fn limits(&self) -> Limits {
        Limits {
            conflicts: self.max_conflicts,
            propagations: self.max_propagations,
        }
    }
}

/// The engines that simplify questions (see [`Config::simplify`]), owned by whoever asks:
/// the deobfuscation strategy with the MBA service, built when first needed, and with them
/// the one thing they remember, the MBA solver's memo. The entry points of this module that
/// take none use one such set for the process; a caller that wants nothing shared with the
/// rest of the process (its statistics, its memory) makes its own and asks through
/// [`Question::valid_under_with`], [`valid_under_with`] or a [`batch::Session`](crate::batch),
/// which owns one. Answers are the same either way: the solver's memo returns what solving
/// again would, at the same cost.
pub struct Simplifiers {
    /// As asked for, strict, and with the limb pass proved.
    engines: [std::sync::OnceLock<crate::engine::Engine>; 3],
    divisions: std::sync::OnceLock<crate::engine::Engine>,
    /// The MBA phase, when not the prover's own.
    #[cfg(feature = "mba")]
    mba: Option<crate::mba::MbaConfig>,
    #[cfg(feature = "mba")]
    solver: Option<std::sync::Arc<dyn crate::mba::MbaSolver>>,
    /// [`Strategy::kept_steps`](crate::engine::Strategy::kept_steps) of the engines.
    kept_steps: u64,
    /// [`Strategy::basis_sums`](crate::engine::Strategy::basis_sums) of the engines, and of
    /// their solver where it is their own ([`NfOptions::basis_sums`](crate::mba::NfOptions)).
    basis_sums: bool,
}

impl core::fmt::Debug for Simplifiers {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Simplifiers").finish_non_exhaustive()
    }
}

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

impl Simplifiers {
    /// Engines of the prover's own configuration: the MBA service with its opaque-constant
    /// sweep, and a normal-form solver of their own.
    pub const fn new() -> Simplifiers {
        Simplifiers {
            engines: [
                std::sync::OnceLock::new(),
                std::sync::OnceLock::new(),
                std::sync::OnceLock::new(),
            ],
            divisions: std::sync::OnceLock::new(),
            #[cfg(feature = "mba")]
            mba: None,
            #[cfg(feature = "mba")]
            solver: None,
            kept_steps: crate::engine::KEPT_STEPS,
            basis_sums: true,
        }
    }

    /// Without sums over the basis of small bitwise functions in the engines' linear-MBA
    /// pass and, where the solver is their own, in its answers. A question built of one gives
    /// the search xors and ors where the conjunction form gives it ands: one of a
    /// devirtualizer's, proved in 0.13 s from the conjunction form at 20,000 propagations,
    /// was not proved at 10,000,000 from the basis form. Another host's questions (the
    /// same devirtualizer on another protector) prove as well with them: it is the host's
    /// choice, as in 0.32.1 they are on.
    pub fn with_basis_sums(mut self, on: bool) -> Self {
        self.basis_sums = on;
        self
    }

    /// With the MBA phase keeping a fragment with no simpler form from the questions above it
    /// from these solver steps ([`Strategy::kept_steps`](crate::engine::Strategy::kept_steps)),
    /// not the default 2^16. A host whose questions are large next to its fuel sets it lower:
    /// a devirtualizer's walk asked a third of its questions in the same time at 2^16, and as
    /// many as with 0.31 (and fewer that ran slow) at 2^12.
    pub fn with_kept_steps(mut self, steps: u64) -> Self {
        self.kept_steps = steps;
        self
    }

    /// With this configuration of the MBA phase instead of the prover's own (the default
    /// with the opaque-constant sweep). It changes what simplification makes of a question,
    /// so which questions are decided before the search.
    #[cfg(feature = "mba")]
    pub fn with_mba(mut self, mba: crate::mba::MbaConfig) -> Self {
        self.mba = Some(mba);
        self
    }

    /// With this MBA solver instead of a [`NormalFormSolver`](crate::mba::NormalFormSolver)
    /// of their own.
    #[cfg(feature = "mba")]
    pub fn with_mba_solver(mut self, solver: std::sync::Arc<dyn crate::mba::MbaSolver>) -> Self {
        self.solver = Some(solver);
        self
    }

    /// The engine for [`Config::strict`] and [`Config::proved_words`] as given: with `strict`
    /// without the pass that commits rewrites it has derived but neither proved nor matched
    /// against a proved rule ([`Phase::Words`](crate::engine::Phase)), or with `proved` with
    /// that pass writing only what a circuit and a search prove.
    pub fn engine(&self, strict: bool, proved: bool) -> &crate::engine::Engine {
        let which = if strict { 1 } else { 2 * usize::from(proved) };
        self.engines[which].get_or_init(|| {
            #[cfg(feature = "mba")]
            let mut strategy = crate::engine::Strategy::deobfuscate().with_mba(
                (self.mba)
                    .unwrap_or_else(|| crate::mba::MbaConfig::default().with_opaque_sweep(true)),
            );
            #[cfg(not(feature = "mba"))]
            let mut strategy = crate::engine::Strategy::deobfuscate();
            if strict {
                (strategy.phases).retain(|p| !matches!(p, crate::engine::Phase::Words));
            }
            strategy.words_proved = proved;
            strategy.basis_sums = self.basis_sums;
            strategy.kept_steps = self.kept_steps;
            let builder = crate::engine::Engine::builder()
                .builtin()
                .strategy(strategy);
            #[cfg(feature = "mba")]
            let builder = match &self.solver {
                Some(s) => builder.mba_solver(s.clone()),
                None if !self.basis_sums => {
                    builder.mba_solver(std::sync::Arc::new(crate::mba::NormalFormSolver::new(
                        crate::mba::NfOptions::default().with_basis_sums(false),
                    )))
                }
                None => builder,
            };
            builder
                .build()
                .unwrap_or_else(|_| crate::engine::Engine::standard())
        })
    }

    /// The division pass alone ([`Config::divisions`]).
    fn divisions_only(&self) -> &crate::engine::Engine {
        self.divisions.get_or_init(|| {
            let strategy =
                crate::engine::Strategy::new("divisions", vec![crate::engine::Phase::Division]);
            crate::engine::Engine::builder()
                .builtin()
                .strategy(strategy)
                .build()
                .unwrap_or_else(|_| crate::engine::Engine::standard())
        })
    }
}

/// The engines of the entry points that are given none.
static PROCESS: Simplifiers = Simplifiers::new();

/// What a question is built with besides its configuration.
#[derive(Copy, Clone)]
pub(crate) struct Env<'a> {
    /// The engines that simplify it.
    pub(crate) simplifiers: &'a Simplifiers,
    /// Stops the simplification, and has the question answer [`Unknown::Deadline`].
    pub(crate) deadline: Option<crate::engine::Deadline<'a>>,
}

impl Env<'static> {
    fn process() -> Self {
        Env {
            simplifiers: &PROCESS,
            deadline: None,
        }
    }
}

/// `p` as the prover's simplifier leaves it within [`Config::simplify_budget`]: an expression
/// equal to `p` at every value of the symbols, which is what the prover builds its circuit
/// of. Asked again of an expression a question was posed about, it costs a lookup.
pub fn simplified(cx: &mut Context, p: Expr, cfg: &Config) -> Result<Expr, Error> {
    let run = crate::engine::Run::default().with_per_call(cfg.simplify_budget);
    Ok(PROCESS
        .engine(cfg.strict, cfg.proved_words)
        .run(cx, &[p], run)?
        .roots[0]
        .expr)
}

/// Clauses and a DRUP proof that they are unsatisfiable, or, for a proof by complete
/// enumeration ([`Config::exhaustive_inputs`]), the circuit to enumerate.
#[derive(Clone, Debug)]
pub struct Certificate {
    /// The number of variables.
    pub vars: u32,
    /// The clauses.
    pub clauses: Vec<Vec<Lit>>,
    /// The proof.
    pub proof: Vec<Step>,
    /// The circuit of a proof by complete enumeration. Such a certificate has no clauses or
    /// DRUP proof, so its [`dimacs`](Self::dimacs) and [`drat`](Self::drat) export nothing a
    /// DRAT checker would accept.
    pub exhaustion: Option<Exhaustion>,
}

impl Certificate {
    /// Checks the proof with the independent checker: the DRUP proof against the clauses, or
    /// the circuit of an [`Exhaustion`] at every assignment of its inputs.
    pub fn check(&self) -> Result<(), String> {
        match &self.exhaustion {
            Some(exhaustion) => exhaustion.check(),
            None => {
                drup::check(&self.clauses, &self.proof)?;
                // In the tests, every proof checked is checked as LRAT too.
                #[cfg(test)]
                lrat::check(&self.clauses, &self.lrat()?)?;
                Ok(())
            }
        }
    }

    /// The clauses in DIMACS CNF.
    pub fn dimacs(&self) -> String {
        let mut s = format!("p cnf {} {}\n", self.vars, self.clauses.len());
        for c in &self.clauses {
            for l in c {
                s.push_str(&l.dimacs().to_string());
                s.push(' ');
            }
            s.push_str("0\n");
        }
        s
    }

    /// The proof as LRAT ([`lrat`]): every learned clause with the clauses it follows from,
    /// found by checking the DRUP proof, for a checker that does no search (the text is
    /// [`lrat::to_text`], beside [`dimacs`](Self::dimacs)). An error when the proof does not
    /// check, or is one by enumeration.
    pub fn lrat(&self) -> Result<Vec<lrat::Step>, String> {
        match &self.exhaustion {
            Some(_) => Err("a proof by enumeration has no clauses".into()),
            None => lrat::from_drup(&self.clauses, &self.proof),
        }
    }

    /// The proof in DRAT's text format (additions, and deletions prefixed `d`).
    pub fn drat(&self) -> String {
        let mut s = String::new();
        for step in &self.proof {
            let (pre, c) = match step {
                Step::Add(c) => ("", c),
                Step::Delete(c) => ("d ", c),
            };
            s.push_str(pre);
            for l in c {
                s.push_str(&l.dimacs().to_string());
                s.push(' ');
            }
            s.push_str("0\n");
        }
        s
    }
}

/// The answer to a question.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum Outcome {
    /// It holds for every value of the symbols (with a certificate, if asked for).
    Proved(Option<Certificate>),
    /// It fails at these values of the symbols (checked by evaluation).
    Refuted(Vec<(SymbolKey, BitVec)>),
    /// Not decided: why.
    Unknown(Unknown),
}

/// Why a question is not decided.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Unknown {
    /// The search reached a limit ([`Config::max_conflicts`] or
    /// [`Config::max_propagations`]) after this much work on the question: more may decide
    /// it, and a [`Question`] goes on from where it stopped.
    Budget {
        /// Conflicts met on the question.
        conflicts: u64,
        /// Propagations done on the question.
        propagations: u64,
    },
    /// The circuit passed [`Config::max_nodes`]: `nodes` had been built when blasting stopped.
    /// The same under any search budget with that cap.
    TooLarge {
        /// AIG nodes built.
        nodes: usize,
    },
    /// Something the prover does not reason about (an extension operation without an
    /// expansion, say). The same under any budget.
    Unsupported(String),
    /// A caller's deadline passed ([`crate::solve::Solver::check_within`]): more time may
    /// decide it. Unlike the budgets, this depends on timing.
    Deadline,
}

impl Unknown {
    /// Whether a larger search budget may decide the question ([`Unknown::Budget`]).
    pub fn is_budget(&self) -> bool {
        matches!(self, Unknown::Budget { .. })
    }
}

impl core::fmt::Display for Unknown {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Unknown::Budget {
                conflicts,
                propagations,
            } => write!(
                f,
                "no answer within {conflicts} conflicts ({propagations} propagations)"
            ),
            Unknown::Deadline => write!(f, "the deadline passed"),
            Unknown::TooLarge { nodes } => {
                write!(f, "the circuit is too large ({nodes} nodes built)")
            }
            Unknown::Unsupported(why) => f.write_str(why),
        }
    }
}

/// The work a question took, so far. Every count is of work done, not of time, and the same on
/// every run: a question asked the same way (in a context with the same history) reports the
/// same numbers, and the totals of one resumed in steps are those of one asked at once.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Stats {
    /// What simplifying the question took ([`Config::simplify`]).
    pub simplification: Simplification,
    /// What sweeping the circuit did ([`Config::sweeping`]); its conflicts, decisions,
    /// propagations and lemmas are also counted in the totals below.
    pub sweeping: Sweeping,
    /// AIG nodes of the circuit (inputs and gates).
    pub nodes: usize,
    /// Whether the circuit is of the question with its sums written again
    /// ([`Config::shared_sums`]), and not of the question as asked (where that changed
    /// nothing, or made a circuit past [`Config::max_nodes`]).
    pub rewritten: bool,
    /// Assignments evaluated before search, on words or the circuit ([`Config::samples`]).
    pub samples: u64,
    /// Variables of the clauses.
    pub vars: u32,
    /// Clauses of the question (not learned ones).
    pub clauses: usize,
    /// Learned clauses kept.
    pub learned: usize,
    /// Clauses learned in all, units included, whether kept or since deleted.
    pub lemmas: u64,
    /// Variables fixed for good: assigned with no decision made, by the clauses and the units
    /// learned so far. Of [`vars`](Self::vars), the part of the circuit the search has settled.
    pub fixed: usize,
    /// Conflicts.
    pub conflicts: u64,
    /// Decisions.
    pub decisions: u64,
    /// Propagations.
    pub propagations: u64,
    /// Searches run: [`Question::solve`] calls that searched (one that finds the question
    /// already decided, or that has a zero allowance, does not).
    pub searches: u32,
    /// The last search's conflicts: the marginal cost of its call. A search goes on from where
    /// the one before stopped, so these add up to [`conflicts`](Self::conflicts) and nothing
    /// is searched twice.
    pub last_conflicts: u64,
    /// The last search's decisions.
    pub last_decisions: u64,
    /// The last search's propagations (they add up to [`propagations`](Self::propagations)
    /// with the propagation of the clauses before any search).
    pub last_propagations: u64,
    /// Clauses the last search learned.
    pub last_lemmas: u64,
}

impl Stats {
    /// These counts without the last search's own ([`searches`](Self::searches) and the
    /// `last_*` fields cleared): the work done in all, which is the same for a question
    /// resumed in steps and one asked at once under their combined limits.
    pub fn totals(self) -> Stats {
        Stats {
            searches: 0,
            last_conflicts: 0,
            last_decisions: 0,
            last_propagations: 0,
            last_lemmas: 0,
            ..self
        }
    }
}

/// What sweeping a question's circuit did ([`Config::sweeping`]).
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Sweeping {
    /// Whether the circuit was swept: simulated, and its candidate pairs proved.
    pub ran: bool,
    /// Whether the circuit was simulated and judged not worth sweeping: too few classes of
    /// nodes that agree join the two sides of an equality (see [`Config::sweeping`]).
    pub skipped: bool,
    /// Classes of the first simulation that join the two sides of an equality (of a question
    /// that is not one: classes of two nodes or more).
    pub crossings: u64,
    /// Nodes proved equal to an earlier node, or its complement, or to a constant.
    pub merged: u64,
    /// Candidate pairs a counterexample told apart.
    pub separated: u64,
    /// Candidate pairs left undecided within their own conflict cap.
    pub undecided: u64,
    /// The sweep's conflicts.
    pub conflicts: u64,
    /// The sweep's propagations.
    pub propagations: u64,
}

/// What simplifying a question with bitwright's own engine took ([`Config::simplify`]).
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Simplification {
    /// Whether the engine ran on the question.
    pub ran: bool,
    /// The work it did, in the units of its budget ([`Config::simplify_budget`]): every
    /// field counted, capped or not. Work the engine finished on the same nodes earlier in the
    /// same context is remembered there and not done again.
    pub spent: crate::engine::Budget,
    /// The cap that stopped it, if one did. The rewrites it made until then are kept, and the
    /// rest of the question is blasted as it stands; a larger budget may settle more.
    pub stopped: Option<crate::engine::Exhausted>,
    /// Whether it decided the question (the simplified question is true).
    pub decided: bool,
}

impl Default for Simplification {
    /// The engine did not run.
    fn default() -> Self {
        Simplification {
            ran: false,
            spent: crate::engine::Budget::ZERO,
            stopped: None,
            decided: false,
        }
    }
}

/// The bits of expressions, blasted on demand.
pub(crate) struct Blaster<'c> {
    pub(crate) cx: &'c mut Context,
    pub(crate) g: Aig,
    bits: IdMap<u32, Bits>,
    /// Each symbol met, with its bits.
    pub(crate) symbols: Vec<(u32, Bits)>,
    selectors: Vec<L>,
    selector_hints: bool,
    input_cancellation: bool,
    join_factoring: bool,
    carry_save_multiplication: bool,
    hardwire_declared: bool,
    udivrem_cache: IdMap<(u32, u32), (Bits, Bits)>,
    max_nodes: usize,
    /// Blasting stopped at `max_nodes`.
    too_large: bool,
}

impl<'c> Blaster<'c> {
    pub(crate) fn new(cx: &'c mut Context, max_nodes: usize) -> Blaster<'c> {
        Blaster {
            cx,
            g: Aig::new(),
            bits: IdMap::default(),
            symbols: Vec::new(),
            selectors: Vec::new(),
            selector_hints: false,
            input_cancellation: false,
            join_factoring: false,
            carry_save_multiplication: false,
            hardwire_declared: true,
            udivrem_cache: IdMap::default(),
            max_nodes,
            too_large: false,
        }
    }

    /// A blaster that goes on from a circuit built before: its graph, the bits of the nodes
    /// blasted and the symbols met ([`into_parts`](Self::into_parts)). Symbol bits stay free
    /// inputs so [`Incremental`] can assume whatever known bits are declared at each check.
    pub(crate) fn resume(
        cx: &'c mut Context,
        g: Aig,
        bits: IdMap<u32, Bits>,
        symbols: Vec<(u32, Bits)>,
        max_nodes: usize,
    ) -> Blaster<'c> {
        Blaster {
            g,
            bits,
            symbols,
            hardwire_declared: false,
            ..Blaster::new(cx, max_nodes)
        }
    }

    /// The circuit, to [`resume`](Self::resume) from.
    pub(crate) fn into_parts(self) -> (Aig, IdMap<u32, Bits>, Vec<(u32, Bits)>) {
        (self.g, self.bits, self.symbols)
    }

    /// The bits of node `root`.
    pub(crate) fn blast(&mut self, root: u32) -> Result<Bits, Error> {
        let bits = &self.bits;
        let order = self
            .cx
            .post_order_ids_pruned(&[root], |e| bits.contains_key(&e.index()));
        for i in order {
            if self.bits.contains_key(&i) {
                continue;
            }
            if self.g.len() > self.max_nodes {
                self.too_large = true;
                return Err(Error::Unsupported("the circuit is too large".into()));
            }
            let b = self.node(i)?;
            self.bits.insert(i, b);
        }
        Ok(self.bits[&root].clone())
    }

    fn node(&mut self, i: u32) -> Result<Bits, Error> {
        use blast::*;
        let n = self.cx.node(i);
        let w = usize::from(n.width);
        let g = &mut self.g;
        Ok(match n.op {
            OpCode::Const => konst(&self.cx.const_val(i).unwrap_or(BitVec::zero(Width::W1))),
            OpCode::Sym => {
                // Declarations are part of a symbol's meaning, including in certificate
                // mode. Only its unknown bits are inputs of the circuit, unless the circuit
                // is kept across checks (`Incremental`), where declarations are assumed per
                // solve so changes or withdrawals stay sound.
                let known = self
                    .hardwire_declared
                    .then(|| self.cx.declared_at(i))
                    .flatten();
                let b: Bits = (0..w)
                    .map(|j| match known {
                        Some(k) if k.known_one().bit(j as u16) == Some(true) => aig::TRUE,
                        Some(k) if k.known_zero().bit(j as u16) == Some(true) => aig::FALSE,
                        _ => g.input(),
                    })
                    .collect();
                self.symbols.push((i, b.clone()));
                b
            }
            OpCode::Not => not(g, &self.bits[&n.a]),
            OpCode::Neg => neg(g, &self.bits[&n.a]),
            OpCode::Popcnt => popcnt(g, &self.bits[&n.a]),
            OpCode::Clz => clz(g, &self.bits[&n.a]),
            OpCode::Ctz => ctz(g, &self.bits[&n.a]),
            OpCode::Bswap => bswap(&self.bits[&n.a]),
            OpCode::BitRev => bitrev(&self.bits[&n.a]),
            OpCode::Zext | OpCode::Sext => {
                let low = &self.bits[&n.a];
                let fill = if n.op == OpCode::Sext { low[low.len() - 1] } else { aig::FALSE };
                let mut out = Vec::with_capacity(w);
                out.extend_from_slice(low);
                out.resize(w, fill);
                out
            }
            OpCode::Extract => {
                let a = &self.bits[&n.a];
                let lo = n.b as usize;
                a[lo..lo + w].to_vec()
            }
            OpCode::Concat => {
                let mut out = Vec::with_capacity(w);
                out.extend_from_slice(&self.bits[&n.b]);
                out.extend_from_slice(&self.bits[&n.a]);
                out
            }
            OpCode::Select => mux(g, self.bits[&n.a][0], &self.bits[&n.b], &self.bits[&n.c]),
            OpCode::Eq | OpCode::Ne | OpCode::Ult | OpCode::Ule | OpCode::Slt | OpCode::Sle => {
                let (a, b) = (&self.bits[&n.a], &self.bits[&n.b]);
                let r = match n.op {
                    OpCode::Eq if self.join_factoring => eq_factored(g, a, b),
                    OpCode::Ne if self.join_factoring => eq_factored(g, a, b) ^ 1,
                    OpCode::Eq => eq(g, a, b),
                    OpCode::Ne => eq(g, a, b) ^ 1,
                    OpCode::Ult => ult(g, a, b),
                    OpCode::Ule => ule(g, a, b),
                    OpCode::Slt => slt(g, a, b),
                    _ => sle(g, a, b),
                };
                vec![r]
            }
            op if op.as_bin().is_some() => {
                let (a, b) = (&self.bits[&n.a], &self.bits[&n.b]);
                if self.selector_hints && matches!(op, OpCode::Shl | OpCode::LShr | OpCode::AShr) {
                    let unknown: Vec<_> = b
                        .iter()
                        .copied()
                        .filter(|&l| l > aig::TRUE)
                        .take(5)
                        .collect();
                    if !unknown.is_empty() && unknown.len() <= 4 {
                        self.selectors.extend(unknown);
                    }
                }
                match op {
                    OpCode::Add => add(g, a, b),
                    OpCode::Sub => sub(g, a, b),
                    OpCode::Mul if self.carry_save_multiplication => mul_carry_save(g, a, b),
                    OpCode::Mul => mul(g, a, b),
                    OpCode::UMulHi if self.carry_save_multiplication => mulhi_carry_save(g, a, b, false),
                    OpCode::UMulHi => mulhi(g, a, b, false),
                    OpCode::SMulHi if self.carry_save_multiplication => mulhi_carry_save(g, a, b, true),
                    OpCode::SMulHi => mulhi(g, a, b, true),
                    OpCode::UDiv | OpCode::URem => {
                        let qr = self.udivrem_cache.entry((n.a, n.b)).or_insert_with(|| udivrem(g, a, b));
                        if op == OpCode::UDiv { qr.0.clone() } else { qr.1.clone() }
                    }
                    OpCode::SDiv => sdiv(g, a, b),
                    OpCode::SRem => srem(g, a, b),
                    OpCode::And => and(g, a, b),
                    OpCode::Or => or(g, a, b),
                    OpCode::Xor if self.input_cancellation => xor_cancel_input(g, a, b),
                    OpCode::Xor => xor(g, a, b),
                    OpCode::Shl => shift(g, a, b, true, aig::FALSE),
                    OpCode::LShr => shift(g, a, b, false, aig::FALSE),
                    OpCode::AShr => shift(g, a, b, false, a[a.len() - 1]),
                    OpCode::RotL => rotate(g, a, b, true),
                    OpCode::RotR => rotate(g, a, b, false),
                    OpCode::Pdep => pdep(g, a, b),
                    _ => pext(g, a, b),
                }
            }
            op if op.as_ext().is_some() => self.ext(i)?,
            _ => {
                let kids: Vec<Bits> = n.children().map(|c| self.bits[&c].clone()).collect();
                fp::blast(self, i, &kids)?
            }
        })
    }

    /// An extension output, through the operation's expansion.
    fn ext(&mut self, i: u32) -> Result<Bits, Error> {
        let n = self.cx.node(i);
        let Some((_, k)) = n.op.as_ext() else {
            return Err(Error::Unsupported("not an extension output".into()));
        };
        let args: Vec<Expr> = n.children().map(|c| self.cx.handle(c)).collect();
        let reg = self
            .cx
            .registry
            .clone()
            .ok_or_else(|| Error::Unsupported("an extension without its registry".into()))?;
        let op = reg
            .op_at(n.aux)
            .ok_or_else(|| Error::Unsupported("an extension without its operation".into()))?;
        let Some(ex) = op.expand(self.cx, &args) else {
            return Err(Error::Unsupported(format!(
                "`{}` has no expansion to reason about",
                op.name()
            )));
        };
        let outs = ex?;
        let e = self.cx.id(outs[k])?;
        self.blast(e)
    }

    /// Why blasting failed with `e`: [`Unknown::TooLarge`] past `max_nodes`, else the reason
    /// of an unsupported operation. Other errors are errors.
    fn unknown(&self, e: Error) -> Result<Unknown, Error> {
        match e {
            _ if self.too_large => Ok(Unknown::TooLarge {
                nodes: self.g.len(),
            }),
            Error::Unsupported(why) => Ok(Unknown::Unsupported(why)),
            e => Err(e),
        }
    }
}

/// Where a SAT-solved question stands.
struct Search {
    solver: Solver,
    cnf: Cnf,
    /// What the sweep cost ([`Config::sweeping`]), counted in the totals.
    swept: sweep::Work,
}

/// A question, blasted and encoded once, then decided in as many steps as its caller likes: a
/// search that ends on its limits stops where it is, keeping what it learned, and the next
/// [`solve`](Self::solve) goes on from there. Asking under 5,000 conflicts, then 15,000 more,
/// costs what asking under 20,000 once does, and answers the same.
///
/// ```
/// use bitwright::prove::{Config, Limits, Outcome, Question};
/// use bitwright::{Context, ParseOptions, Width};
///
/// let mut cx = Context::new();
/// let o = ParseOptions::width(Width::W16);
/// let p = cx.parse("x * x != 0x1234", &o)?;
/// // Without the engine's simplification first, which decides it by residues.
/// let cfg = Config::default().with_samples(0).with_simplify(false);
/// let mut q = Question::valid(&mut cx, p, &cfg)?;
/// let mut out = q.solve(&mut cx, Limits::conflicts(10))?;
/// while let Outcome::Unknown(why) = &out {
///     assert!(why.is_budget());
///     out = q.solve(&mut cx, Limits::conflicts(10))?;
/// }
/// assert!(matches!(out, Outcome::Proved(_))); // 0x1234 is not a square mod 2^16
/// assert!(q.stats().conflicts > 0);
/// # Ok::<(), bitwright::Error>(())
/// ```
pub struct Question {
    /// The question as asked (with the constraints, as `¬(c1 ∧ …) ∨ p`), checked by evaluation.
    root: Expr,
    /// Each symbol of the question with its bits (one the circuit does not read takes its
    /// declared ones, or 0 without a declaration).
    symbols: Vec<(SymbolKey, Width, Bits)>,
    search: Option<Box<Search>>,
    outcome: Option<Outcome>,
    stats: Stats,
}

impl core::fmt::Debug for Question {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Question")
            .field("outcome", &self.outcome)
            .field("stats", &self.stats)
            .finish_non_exhaustive()
    }
}

impl Question {
    /// Whether the 1-bit `p` is true for every value of its symbols agreeing with their
    /// declared known bits.
    pub fn valid(cx: &mut Context, p: Expr, cfg: &Config) -> Result<Question, Error> {
        Question::valid_under(cx, p, None, cfg)
    }

    /// Whether `a` and `b` (of one width) are equal for every value of their symbols agreeing
    /// with their declared known bits.
    pub fn equal(cx: &mut Context, a: Expr, b: Expr, cfg: &Config) -> Result<Question, Error> {
        let (ai, bi) = (cx.id(a)?, cx.id(b)?);
        if cx.wid(ai) != cx.wid(bi) {
            return Err(crate::WidthError::Mismatch {
                left: cx.wid(ai),
                right: cx.wid(bi),
            }
            .into());
        }
        let p = cx.c_cmp(crate::CmpOp::Eq, ai, bi)?;
        let p = cx.handle(p);
        Question::valid(cx, p, cfg)
    }

    /// Whether `p` is true wherever the constraints of `assumptions` hold. Complete original
    /// facts are retained, including partial masks, signed/unsigned
    /// intervals and unsigned strides; they remain in certificates and counterexample replay.
    /// The question is simplified (see [`Config::simplify`]), optionally sampled at the word level, then
    /// blasted, sampled (see [`Config::samples`]) and encoded here; each stage may decide it.
    pub fn valid_under(
        cx: &mut Context,
        p: Expr,
        assumptions: Option<&Assumptions>,
        cfg: &Config,
    ) -> Result<Question, Error> {
        Self::valid_under_with_pairs(cx, p, assumptions, &[], cfg)
    }

    /// Ask the original question after applying explicitly supplied, exhaustively checked
    /// finite-domain pair facts. Verification costs are separate from this query's budget.
    /// Models are replayed on the original predicate. Certificate requests retain the
    /// original bit-blast path and do not use these word-level facts.
    pub fn valid_with_pairs(
        cx: &mut Context,
        p: Expr,
        pairs: &[finite::CheckedPair],
        cfg: &Config,
    ) -> Result<Question, Error> {
        Self::valid_under_with_pairs(cx, p, None, pairs, cfg)
    }

    /// [`valid_with_pairs`](Self::valid_with_pairs) under explicit assumptions.
    pub fn valid_under_with_pairs(
        cx: &mut Context,
        p: Expr,
        assumptions: Option<&Assumptions>,
        pairs: &[finite::CheckedPair],
        cfg: &Config,
    ) -> Result<Question, Error> {
        Self::build(cx, p, assumptions, pairs, cfg, Env::process())
    }

    /// [`valid_under`](Self::valid_under), simplified by the caller's engines and not the
    /// process's: the same question, answer and statistics.
    pub fn valid_under_with(
        cx: &mut Context,
        p: Expr,
        assumptions: Option<&Assumptions>,
        cfg: &Config,
        simplifiers: &Simplifiers,
    ) -> Result<Question, Error> {
        let env = Env {
            simplifiers,
            deadline: None,
        };
        Self::build(cx, p, assumptions, &[], cfg, env)
    }

    /// The question, built with `env`.
    pub(crate) fn build(
        cx: &mut Context,
        p: Expr,
        assumptions: Option<&Assumptions>,
        pairs: &[finite::CheckedPair],
        cfg: &Config,
        env: Env<'_>,
    ) -> Result<Question, Error> {
        let pi = cx.id(p)?;
        if cx.wid(pi) != 1 {
            return Err(crate::WidthError::Mismatch {
                left: 1,
                right: cx.wid(pi),
            }
            .into());
        }
        let mut q = Question {
            root: p,
            symbols: Vec::new(),
            search: None,
            outcome: None,
            stats: Stats::default(),
        };
        let decided = |mut q: Question, o: Outcome| {
            q.outcome = Some(o);
            Ok(q)
        };
        // Exact original constraints, including masks, signed/unsigned bounds and strides.
        let mut cons: Vec<u32> = Vec::new();
        if let Some(a) = assumptions {
            for predicate in a.predicates(cx)? {
                cons.push(cx.id(predicate)?);
            }
        }
        // The root checked by evaluation: `p` where the constraints hold, as `¬(c1 ∧ …) ∨ p`.
        let mut root = pi;
        for &c in &cons {
            let nc = cx.c_un(crate::UnOp::Not, c)?;
            root = cx.c_bin(crate::BinOp::Or, nc, root)?;
        }
        q.root = cx.handle(root);
        // The question simplified (not for a certificate, which must be of the question itself).
        let mut goal_node = pi;
        if !cfg.certificate {
            for pair in pairs {
                let rewritten = pair.simplify(cx, cx.handle(goal_node))?;
                goal_node = cx.id(rewritten)?;
            }
            if cx
                .const_val(goal_node)
                .is_some_and(|value| !value.is_zero())
            {
                return decided(q, Outcome::Proved(None));
            }
        }
        // Reserve at least one ordinary circuit batch. Sample the *original implication*
        // so even constraints hidden inside the predicate participate in witness validation.
        if cfg.simplify && !cfg.certificate && !cfg.word_sampling
            && cfg.sample_mode == SampleMode::Random && cfg.samples >= 128
            && env.deadline.is_none()
            && let Some((tested, model)) = sample_words::guard_samples(cx, root)
        {
            q.stats.samples = tested;
            if let Some(model) = model {
                let outcome = q.refuted(cx, model)?;
                return decided(q, outcome);
            }
        }
        let remaining_batches = cfg.samples.div_ceil(64) - (q.stats.samples / 64) as u32;
        if cfg.simplify && !cfg.certificate {
            let mut spent = crate::engine::Allowance::new(crate::engine::Budget::UNLIMITED);
            let run = crate::engine::Run::default()
                .with_per_call(cfg.simplify_budget)
                .with_allowance(&mut spent);
            let run = match assumptions {
                Some(a) => run.with_assumptions(a),
                None => run,
            };
            let run = match env.deadline {
                Some(d) => run.with_deadline(d),
                None => run,
            };
            let goal = cx.handle(goal_node);
            let out = (env.simplifiers)
                .engine(cfg.strict, cfg.proved_words)
                .run(cx, &[goal], run)?
                .roots[0];
            let s = cx.id(out.expr)?;
            let proved = cx.const_val(s).is_some_and(|v| !v.is_zero());
            q.stats.simplification = Simplification {
                ran: true,
                spent: spent.spent(),
                stopped: match out.end {
                    crate::engine::End::BudgetTerminated(why) => Some(why),
                    _ => None,
                },
                decided: proved,
            };
            if proved {
                return decided(q, Outcome::Proved(None));
            }
            // Stopped by its caller: what is left would be built and searched for nobody.
            if out.end == crate::engine::End::BudgetTerminated(crate::engine::Exhausted::Deadline) {
                return decided(q, Outcome::Unknown(Unknown::Deadline));
            }
            // A result that relies on constraints holds where they do, which is all a proof
            // under them needs.
            goal_node = s;
        }
        if cfg.divisions && !cfg.simplify && !cfg.certificate {
            let run = crate::engine::Run::default().with_per_call(cfg.simplify_budget);
            let run = match assumptions {
                Some(a) => run.with_assumptions(a),
                None => run,
            };
            let goal = cx.handle(goal_node);
            let out = env
                .simplifiers
                .divisions_only()
                .run(cx, &[goal], run)?
                .roots[0];
            goal_node = cx.id(out.expr)?;
            if cx.const_val(goal_node).is_some_and(|v| !v.is_zero()) {
                q.stats.rewritten = true;
                return decided(q, Outcome::Proved(None));
            }
        }
        let mut word_sampled = false;
        if (cfg.word_sampling || cfg.sample_mode == SampleMode::Words)
            && cfg.samples != 0
            && let Some(trial) = sample_words::try_sample(cx, goal_node, &cons, assumptions, cfg)
        {
            q.stats.samples = trial.samples;
            word_sampled = true;
            if let Some(mut model) = trial.model {
                complete_model(cx, q.root, &mut model)?;
                let outcome = q.refuted(cx, model)?;
                return decided(q, outcome);
            }
            if trial.complete && !cfg.certificate {
                return decided(q, Outcome::Proved(None));
            }
        }
        // Every legal assignment, when there are few enough. A proof that asks for a
        // certificate takes it from the circuit below, which is checked independently.
        let mut exhausted = false;
        if cfg.exhaustive_inputs != 0
            && let Some((failure, evaluated)) =
                sample_words::try_exhaust(cx, goal_node, &cons, assumptions, cfg.exhaustive_inputs)
        {
            q.stats.samples += evaluated;
            match failure {
                Some(mut model) => {
                    complete_model(cx, q.root, &mut model)?;
                    let outcome = q.refuted(cx, model)?;
                    return decided(q, outcome);
                }
                None if !cfg.certificate => return decided(q, Outcome::Proved(None)),
                None => exhausted = true,
            }
        }
        // The question as asked, where writing its sums again changes it.
        let asked = goal_node;
        if cfg.shared_sums && !cfg.certificate {
            goal_node = sums::shared(cx, goal_node)?;
            if cx
                .const_val(goal_node)
                .is_some_and(|value| !value.is_zero())
            {
                q.stats.rewritten = true;
                return decided(q, Outcome::Proved(None));
            }
        }
        let (mut b, goal, extra) = loop {
            let constant_goal = cuts::constant_true(cx, goal_node);
            let mut b = Blaster::new(cx, cfg.max_nodes);
            b.selector_hints = cfg.selector_branching;
            b.input_cancellation = cfg.input_cancellation;
            b.join_factoring = cfg.join_factoring;
            b.carry_save_multiplication = cfg.carry_save_multiplication;
            if constant_goal {
                b.bits.insert(goal_node, vec![aig::TRUE]);
            }
            let mut blasted = || -> Result<(L, Vec<L>), Error> {
                let goal = b.blast(goal_node)?[0];
                let mut extra = Vec::new();
                for &c in &cons {
                    extra.push(b.blast(c)?[0]);
                }
                Ok((goal, extra))
            };
            match blasted() {
                Ok((goal, extra)) if b.g.len() <= cfg.max_nodes => break (b, goal, extra),
                // (Written again the circuit is too large: the question as asked, then.)
                _ if goal_node != asked => goal_node = asked,
                Ok(_) => {
                    let why = Unknown::TooLarge { nodes: b.g.len() };
                    return decided(q, Outcome::Unknown(why));
                }
                Err(e) => {
                    let why = b.unknown(e)?;
                    return decided(q, Outcome::Unknown(why));
                }
            }
        };
        q.stats.nodes = b.g.len();
        q.stats.rewritten = goal_node != asked;
        // Every symbol of the question, with its bits if the circuit reads it. (Marked by
        // symbol index: a search of the list per symbol took 12 s on a question of 64 000
        // symbols that simplification had already decided.)
        let mut listed = vec![false; b.cx.symbol_count()];
        for (s, bits) in &b.symbols {
            let h = b.cx.handle(*s);
            let id =
                b.cx.symbol_id(h)?
                    .ok_or_else(|| Error::Contract("a symbol without its key".into()))?;
            let key =
                b.cx.symbol_key(id)
                    .cloned()
                    .ok_or_else(|| Error::Contract("a symbol without its key".into()))?;
            listed[id.index() as usize] = true;
            q.symbols.push((key, b.cx.width_of(*s), bits.clone()));
        }
        for id in b.cx.symbols_in(&[q.root])? {
            if !core::mem::replace(&mut listed[id.index() as usize], true)
                && let (Some(k), Some(w)) = (b.cx.symbol_key(id).cloned(), b.cx.symbol_width(id))
            {
                let bits =
                    b.cx.find_symbol(&k)
                        .and_then(|e| b.cx.declared_at(e.index()))
                        .map(|known| blast::konst(&known.known_one()))
                        .unwrap_or_default();
                q.symbols.push((k, w, bits));
            }
        }
        if exhausted {
            let exhaustion = Exhaustion::of(&b.g, goal, &extra);
            if exhaustion.inputs() <= u32::from(cfg.exhaustive_inputs.min(63)) {
                let cert = Certificate {
                    vars: 0,
                    clauses: Vec::new(),
                    proof: Vec::new(),
                    exhaustion: Some(exhaustion),
                };
                return decided(q, Outcome::Proved(Some(cert)));
            }
        }
        // Sampled: a lane where the constraints hold and the goal does not refutes it.
        let batches = if word_sampled {
            0
        } else {
            remaining_batches
        };
        for batch in 0..batches {
            let vals = b.g.eval_words(|k| match cfg.sample_mode {
                SampleMode::Random => sample(batch, k),
                SampleMode::Small | SampleMode::Words => small_sample(batch, k),
            });
            q.stats.samples += 64;
            let mut bad = !Aig::word(&vals, goal);
            for &e in &extra {
                bad &= Aig::word(&vals, e);
            }
            if bad != 0 {
                let lane = bad.trailing_zeros();
                let ce = Question::counterexample(&q.symbols, |l| {
                    (Aig::word(&vals, l) >> lane) & 1 == 1
                });
                let o = q.refuted(b.cx, ce)?;
                return decided(q, o);
            }
        }
        let mut solver = Solver::new();
        if cfg.certificate {
            solver.log_proof();
        }
        let mut roots = vec![goal];
        roots.extend_from_slice(&extra);
        let mut cnf = Cnf::encode_with_xor3(
            &b.g,
            &roots,
            &mut solver,
            cfg.certificate,
            cfg.xor3_encoding,
        );
        for &e in &extra {
            cnf.assert(e, &mut solver);
        }
        cnf.assert(goal ^ 1, &mut solver);
        let mut asserted = vec![goal ^ 1];
        asserted.extend_from_slice(&extra);
        relations::strengthen(&b.g, &asserted, &cnf, &mut solver, cfg.relational_lemmas);
        if cfg.selector_branching {
            for &l in &b.selectors {
                if let Some(l) = cnf.mapped_lit(l) {
                    solver.prefer_selector(l);
                }
            }
        }
        // Swept once, as part of preparing the question: within half of the configured limits,
        // whatever the searches' own, so a question resumed in steps is swept as one asked at
        // once.
        let mut swept = sweep::Work::default();
        if cfg.sweeping {
            // The two sides of an equality, whose inner nodes a sweep can merge across.
            let sides = match b.cx.node(goal_node) {
                n if n.op == OpCode::Eq => match (b.bits.get(&n.a), b.bits.get(&n.b)) {
                    (Some(x), Some(y)) => Some((x.clone(), y.clone())),
                    _ => None,
                },
                _ => None,
            };
            let pending = sweep::Pending {
                sides,
                words: b.symbols.iter().map(|(_, bits)| bits.clone()).collect(),
                aig: core::mem::take(&mut b.g),
                roots,
                xor3: cfg.xor3_encoding,
                certificate: cfg.certificate,
            };
            let share = Limits {
                conflicts: cfg.max_conflicts / 2,
                propagations: cfg.max_propagations / 2,
            };
            swept = sweep::sweep(&pending, &cnf, &mut solver, share);
            q.stats.sweeping = Sweeping {
                ran: swept.ran,
                skipped: swept.skipped,
                crossings: swept.crossings,
                merged: swept.merged,
                separated: swept.separated,
                undecided: swept.undecided,
                conflicts: swept.conflicts,
                propagations: swept.propagations,
            };
        }
        q.stats.vars = solver.num_vars();
        q.stats.clauses = cnf.emitted();
        q.stats.learned = solver.num_learnts();
        q.stats.lemmas = solver.lemmas + swept.lemmas;
        q.stats.fixed = solver.num_fixed();
        q.stats.conflicts = swept.conflicts;
        q.stats.decisions = swept.decisions;
        q.stats.propagations = solver.propagations + swept.propagations;
        q.search = Some(Box::new(Search { solver, cnf, swept }));
        Ok(q)
    }

    /// Searches within `limits` more work (counted from this call): the answer, or
    /// [`Unknown::Budget`] with the work done on the question so far, and the next call goes on
    /// from there. Once decided (here or when built), the answer is kept and returned again.
    /// `cx` is the context the question was built in.
    pub fn solve(&mut self, cx: &mut Context, limits: Limits) -> Result<Outcome, Error> {
        if let Some(o) = &self.outcome {
            return Ok(o.clone());
        }
        let Some(s) = self.search.as_mut() else {
            return Err(Error::Contract(
                "a question neither decided nor open".into(),
            ));
        };
        // The question's totals: its solver's and its sweep's.
        let totals = |s: &Search| {
            (
                s.solver.conflicts + s.swept.conflicts,
                s.solver.decisions + s.swept.decisions,
                s.solver.propagations + s.swept.propagations,
                s.solver.lemmas + s.swept.lemmas,
            )
        };
        let before = totals(s);
        // A zero allowance pauses the question without searching.
        let searching = limits.conflicts != 0 && limits.propagations != 0;
        let answer = s.solver.solve_within(limits);
        let after = totals(s);
        self.stats.learned = s.solver.num_learnts();
        self.stats.lemmas = after.3;
        self.stats.fixed = s.solver.num_fixed();
        self.stats.conflicts = after.0;
        self.stats.decisions = after.1;
        self.stats.propagations = after.2;
        if searching {
            self.stats.searches += 1;
            self.stats.last_conflicts = after.0 - before.0;
            self.stats.last_decisions = after.1 - before.1;
            self.stats.last_propagations = after.2 - before.2;
            self.stats.last_lemmas = after.3 - before.3;
        }
        let o = match answer {
            Answer::Unknown => {
                return Ok(Outcome::Unknown(Unknown::Budget {
                    conflicts: after.0,
                    propagations: after.2,
                }));
            }
            Answer::Unsat => {
                let cert = s.solver.take_proof().map(|proof| Certificate {
                    vars: s.solver.num_vars(),
                    clauses: core::mem::take(&mut s.cnf.clauses),
                    proof,
                    exhaustion: None,
                });
                Outcome::Proved(cert)
            }
            Answer::Sat(model) => {
                let cnf = &s.cnf;
                let ce = Question::counterexample(&self.symbols, |l| cnf.value(l, &model));
                self.refuted(cx, ce)?
            }
        };
        self.search = None;
        self.outcome = Some(o.clone());
        Ok(o)
    }

    /// The answer, once decided.
    pub fn outcome(&self) -> Option<&Outcome> {
        self.outcome.as_ref()
    }

    /// The work done on the question so far.
    pub fn stats(&self) -> Stats {
        self.stats
    }

    /// The counterexample an assignment of the circuit's inputs gives.
    fn counterexample(symbols: &[(SymbolKey, Width, Bits)], bit: impl Fn(L) -> bool) -> Model {
        symbols
            .iter()
            .map(|(k, w, bits)| {
                let mut limbs = [0u64; 8];
                for (j, &b) in bits.iter().enumerate() {
                    if bit(b) {
                        limbs[j / 64] |= 1 << (j % 64);
                    }
                }
                (k.clone(), BitVec::wrapping_from_limbs(*w, &limbs))
            })
            .collect()
    }

    /// `Refuted(ce)`, checked by bitwright's evaluator: a solver or blaster bug cannot produce a
    /// false refutation unnoticed.
    fn refuted(&self, cx: &mut Context, ce: Model) -> Result<Outcome, Error> {
        // (By key, the first value given for one: a model lists every symbol of the question.)
        let mut env: std::collections::HashMap<SymbolKey, BitVec> =
            std::collections::HashMap::with_capacity(ce.len());
        for (k, v) in &ce {
            env.entry(k.clone()).or_insert(*v);
        }
        let v = cx.eval(&[self.root], &env)?[0];
        if v.is_zero() {
            Ok(Outcome::Refuted(ce))
        } else {
            Err(Error::Contract(format!(
                "the prover's counterexample does not refute {}",
                cx.display(self.root)
            )))
        }
    }
}

/// Simplification can omit original symbols: supply values consistent with their declarations
/// before replaying the original constrained predicate.
fn complete_model(cx: &mut Context, root: Expr, model: &mut Model) -> Result<(), Error> {
    let mut given: std::collections::HashSet<SymbolKey> =
        model.iter().map(|(k, _)| k.clone()).collect();
    for id in cx.symbols_in(&[root])? {
        let key = cx.symbol_key(id).unwrap().clone();
        if given.insert(key.clone()) {
            let symbol = cx.find_symbol(&key).unwrap();
            let value = cx.declared_known(symbol)?.map_or_else(
                || BitVec::zero(cx.symbol_width(id).unwrap()),
                |k| k.known_one(),
            );
            model.push((key, value));
        }
    }
    Ok(())
}

/// The 64 consecutive assignments `64*batch .. 64*batch+64`, transposed into input words.
fn small_sample(batch: u32, k: u32) -> u64 {
    const LOW: [u64; 6] = [
        0xaaaa_aaaa_aaaa_aaaa,
        0xcccc_cccc_cccc_cccc,
        0xf0f0_f0f0_f0f0_f0f0,
        0xff00_ff00_ff00_ff00,
        0xffff_0000_ffff_0000,
        0xffff_ffff_0000_0000,
    ];
    if k < 6 {
        LOW[k as usize]
    } else if k - 6 < 32 && (batch >> (k - 6)) & 1 == 1 {
        u64::MAX
    } else {
        0
    }
}

/// Input `k`'s word in sample batch `batch` (splitmix64 of the pair); batch 0's lanes 0 and 1
/// are all zeros and all ones.
fn sample(batch: u32, k: u32) -> u64 {
    let mut z = (u64::from(batch) << 32 | u64::from(k)).wrapping_add(0x9e37_79b9_7f4a_7c15);
    z = (z ^ (z >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9);
    z = (z ^ (z >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb);
    z ^= z >> 31;
    if batch == 0 { (z & !0b11) | 0b10 } else { z }
}

/// Whether the 1-bit `p` is true for every value of its symbols agreeing with their declared
/// known bits.
pub fn valid(cx: &mut Context, p: Expr, cfg: &Config) -> Result<Outcome, Error> {
    valid_under(cx, p, None, cfg)
}

/// Whether `p` is true wherever the constraints of `assumptions` and the symbols' declared
/// known bits hold.
pub fn valid_under(
    cx: &mut Context,
    p: Expr,
    assumptions: Option<&Assumptions>,
    cfg: &Config,
) -> Result<Outcome, Error> {
    Question::valid_under(cx, p, assumptions, cfg)?.solve(cx, cfg.limits())
}

/// [`valid_under`], simplified by the caller's engines and not the process's
/// ([`Simplifiers`]): the same answer.
pub fn valid_under_with(
    cx: &mut Context,
    p: Expr,
    assumptions: Option<&Assumptions>,
    cfg: &Config,
    simplifiers: &Simplifiers,
) -> Result<Outcome, Error> {
    Question::valid_under_with(cx, p, assumptions, cfg, simplifiers)?.solve(cx, cfg.limits())
}

/// Whether `a` and `b` (of one width) are equal for every value of their symbols agreeing with
/// their declared known bits.
pub fn equal(cx: &mut Context, a: Expr, b: Expr, cfg: &Config) -> Result<Outcome, Error> {
    Question::equal(cx, a, b, cfg)?.solve(cx, cfg.limits())
}

/// Values of symbols, as a counterexample or a model gives them.
pub type Model = Vec<(SymbolKey, BitVec)>;

/// A value of the symbols that makes the 1-bit `p` true, if there is one: `Refuted` of `¬p`
/// read back as a model (`Ok(Some)`), `Ok(None)` when `p` is unsatisfiable, and `Err` with the
/// reason when not decided.
pub fn satisfy(
    cx: &mut Context,
    p: Expr,
    cfg: &Config,
) -> Result<Result<Option<Model>, Unknown>, Error> {
    let np = cx.un(crate::UnOp::Not, p)?;
    Ok(match valid(cx, np, cfg)? {
        Outcome::Proved(_) => Ok(None),
        Outcome::Refuted(m) => Ok(Some(m)),
        Outcome::Unknown(why) => Err(why),
    })
}

/// The prover the engine's passes are given ([`crate::engine::oracle`]): the circuit and the
/// search alone, without the simplifier (which is the engine that asks).
pub(crate) fn circuit_equality(
    cx: &mut Context,
    a: Expr,
    b: Expr,
    caps: &crate::engine::oracle::EqualityCaps,
) -> Result<crate::engine::oracle::EqualityAnswer, Error> {
    use crate::engine::oracle::{EqualityAnswer, EqualityVerdict};
    let cfg = Config::default()
        .with_simplify(false)
        .with_certificate(false)
        .with_samples(caps.samples)
        .with_max_nodes(caps.max_nodes)
        .with_max_conflicts(u64::MAX)
        .with_max_propagations(caps.max_propagations);
    let mut q = Question::equal(cx, a, b, &cfg)?;
    let out = q.solve(cx, cfg.limits())?;
    let stats = q.stats();
    let (verdict, nodes) = match out {
        Outcome::Proved(_) => (EqualityVerdict::Proved, stats.nodes),
        Outcome::Refuted(_) => (EqualityVerdict::Refuted, stats.nodes),
        Outcome::Unknown(Unknown::Budget { .. }) => (EqualityVerdict::Budget, stats.nodes),
        Outcome::Unknown(Unknown::TooLarge { nodes }) => (EqualityVerdict::Unknown, nodes),
        Outcome::Unknown(_) => (EqualityVerdict::Unknown, stats.nodes),
    };
    Ok(EqualityAnswer {
        verdict,
        nodes,
        propagations: stats.propagations,
    })
}