verif 0.1.0

In-code formal verification for Rust: an embedded bit-blasting SMT decision procedure, a symbolic DSL, contracts and a machine-checked proof kernel. SPARK-inspired, no_std, no external dependencies.
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
//! Shared IR for the differential oracles.
//!
//! This module is compiled into four separate example binaries: `kanidiff`, `verusdiff`,
//! `leandiff` and `coqdiff`. Each uses one of the four source generators
//! (`rust_word`/`rust_pred` for Kani, `verus_word`/`verus_pred` for Verus,
//! `lean_word`/`lean_pred` for Lean 4, `coq_word`/`coq_pred` for Coq) and sees the others as
//! unused, so `dead_code` is allowed here rather than at each call site.
//!
//! `z3diff` checks the *exporter* (does the printed query mean what the engine proved?).
//! The other harnesses check the *engine*, by turning each predicate the engine proved into
//! a program or a goal for an independent verifier -- Kani, Verus, Lean's `bv_decide`, Coq's
//! `vm_compute` -- and asking that verifier to establish the same property.
//!
//! The cases live here, in one place, for one reason: if each harness carried its own
//! corpus and generator, "the engine agrees with Kani" and "the engine agrees with Lean"
//! would be claims about two different sets of formulas, and neither would say much. One
//! IR, five backends (the engine, Kani, Verus, Lean, Coq) means a case that any backend
//! mishandles is visible as a disagreement rather than as a harness-specific blind spot.
//!
//! The four generators are deliberately *not* one parameterised printer. Kani and Verus
//! read Rust-shaped text, Lean reads `BitVec`, and Coq has no machine word at all: its
//! generator emits the engine's own arithmetic over `Z` and lets the harness enumerate the
//! 8-bit domain. Those are four different transcriptions of one tree, and that is the
//! point -- a transcription that quietly says something else shows up as a disagreement,
//! which is what the negative control in every harness is there to prove is detectable.
//!
//! The IR is also why the generated code describes the *same* property as the predicate:
//! the two are interpretations of one tree, not two independent generations. Generating
//! them separately would compare unrelated formulas and every disagreement would be noise.
#![allow(dead_code)]

use verif::smt::{self, Pred, Var};

/// Width of every word. Everything is `u8` so both sides stay well-sorted: the engine
/// refuses mixed-width comparisons, and a case it refuses makes no claim to confirm.
pub const W: u8 = 8;
/// The parameters every case is built over. Three reaches every binary operator and `ite`.
pub const PARAMS: [&str; 3] = ["a", "b", "c"];

/// A small linear-congruential generator, so the sweeps are reproducible from a seed.
pub struct Lcg(pub u64);
impl Lcg {
    pub fn next(&mut self) -> u64 {
        self.0 = self.0.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407);
        self.0 >> 33
    }
    pub fn below(&mut self, n: u64) -> u64 {
        if n == 0 { 0 } else { self.next() % n }
    }
}

/// Interpret a word tree as a Rust expression of type `u8`, for the Kani backend.
///
/// The translation is the whole content of the Kani harness, so it is written to be
/// auditable by eye: `Add` is `wrapping_add` because the engine's `Add` wraps, and `Mask`
/// is `& ((1<<k)-1)` because the engine's narrowing `truncate` keeps the low bits. A mistake
/// *here* shows up as a mismatch, which is the right outcome -- the harness reports its own
/// bug rather than silently vouching for the engine.
///
/// Shifts go through the emitted helpers `shl_eng`/`lshr_eng`/`ashr_eng`, and the reason is
/// a *real* divergence the first version of this harness tripped over. The engine defines a
/// shift by `k >= width` to be 0 (or the sign bit), the SMT-LIB rule its tests pin. Rust
/// does not: `x << 8` on a `u8` is an arithmetic-overflow *error* at compile time and panics
/// at run time. Emitting `x << k` would either fail to compile or check a different
/// operation, so the helpers spell the engine's rule out in Rust.
pub fn rust_word(e: &WExpr) -> String {
    match e {
        WExpr::Const(k) => format!("{k}u8"),
        WExpr::Param(i) => PARAMS[*i].to_string(),
        WExpr::Add(x, y) => format!("({}).wrapping_add({})", rust_word(x), rust_word(y)),
        WExpr::Sub(x, y) => format!("({}).wrapping_sub({})", rust_word(x), rust_word(y)),
        WExpr::Mul(x, y) => format!("({}).wrapping_mul({})", rust_word(x), rust_word(y)),
        WExpr::Band(x, y) => format!("(({}) & ({}))", rust_word(x), rust_word(y)),
        WExpr::Bor(x, y) => format!("(({}) | ({}))", rust_word(x), rust_word(y)),
        WExpr::Bxor(x, y) => format!("(({}) ^ ({}))", rust_word(x), rust_word(y)),
        WExpr::Shl(x, k) => format!("shl_eng({}, {k})", rust_word(x)),
        WExpr::Lshr(x, k) => format!("lshr_eng({}, {k})", rust_word(x)),
        WExpr::Ashr(x, k) => format!("ashr_eng({}, {k})", rust_word(x)),
        WExpr::Mask(x, k) => {
            if *k >= 8 {
                format!("({})", rust_word(x))
            } else {
                format!("(({}) & {}u8)", rust_word(x), (1u16 << k) - 1)
            }
        }
        WExpr::Ite(c, t, f) => {
            format!("(if {} {{ {} }} else {{ {} }})", rust_pred(c), rust_word(t), rust_word(f))
        }
    }
}

/// Interpret a predicate tree as a Rust expression of type `bool`.
pub fn rust_pred(e: &PExpr) -> String {
    match e {
        PExpr::Eq(x, y) => format!("(({}) == ({}))", rust_word(x), rust_word(y)),
        PExpr::Ne(x, y) => format!("(({}) != ({}))", rust_word(x), rust_word(y)),
        PExpr::Ult(x, y) => format!("(({}) < ({}))", rust_word(x), rust_word(y)),
        PExpr::Ule(x, y) => format!("(({}) <= ({}))", rust_word(x), rust_word(y)),
        PExpr::And(x, y) => format!("(({}) && ({}))", rust_pred(x), rust_pred(y)),
        PExpr::Or(x, y) => format!("(({}) || ({}))", rust_pred(x), rust_pred(y)),
        // Boolean xor and iff are `!=` and `==` on `bool`, which Rust has.
        PExpr::Xor(x, y) => format!("(({}) != ({}))", rust_pred(x), rust_pred(y)),
        PExpr::Not(x) => format!("(!({}))", rust_pred(x)),
        PExpr::Iff(x, y) => format!("(({}) == ({}))", rust_pred(x), rust_pred(y)),
    }
}

/// Interpret a word tree as a Verus `spec fn` expression of type `u8`.
///
/// Deliberately a separate function from [`rust_word`] rather than a shared one: the two
/// targets need different spellings, and a shared printer would have to be parameterised in
/// ways that make the translation harder to audit -- which is the one property that matters
/// here. The semantics are identical; the two are cross-checked against each other by
/// agreeing with the same engine.
///
/// Arithmetic goes through the emitted spec helpers `add_eng`/`sub_eng`/`mul_eng`, which
/// wrap. That is not cosmetic: `#[verifier(bit_vector)]` rejects `wrapping_mul` when an
/// operand is a literal ("expected finite-width integer in `bit_vector` proof, got
/// Int(Nat)"), and a constant expression like `255u8 * 255u8` is a rustc const-eval overflow
/// *error* that aborts the whole run, so neither form can be written inline. In a spec fn the
/// operands are already lifted to `int`, so `(x * y) as u8` is the wrapping operation and
/// there is no const-eval to overflow.
pub fn verus_word(e: &WExpr) -> String {
    match e {
        WExpr::Const(k) => format!("{k}u8"),
        WExpr::Param(i) => PARAMS[*i].to_string(),
        WExpr::Add(x, y) => format!("add_eng({}, {})", verus_word(x), verus_word(y)),
        WExpr::Sub(x, y) => format!("sub_eng({}, {})", verus_word(x), verus_word(y)),
        WExpr::Mul(x, y) => format!("mul_eng({}, {})", verus_word(x), verus_word(y)),
        WExpr::Band(x, y) => format!("(({}) & ({}))", verus_word(x), verus_word(y)),
        WExpr::Bor(x, y) => format!("(({}) | ({}))", verus_word(x), verus_word(y)),
        WExpr::Bxor(x, y) => format!("(({}) ^ ({}))", verus_word(x), verus_word(y)),
        WExpr::Shl(x, k) => format!("shl_eng({}, {k})", verus_word(x)),
        WExpr::Lshr(x, k) => format!("lshr_eng({}, {k})", verus_word(x)),
        WExpr::Ashr(x, k) => format!("ashr_eng({}, {k})", verus_word(x)),
        WExpr::Mask(x, k) => {
            if *k >= 8 {
                format!("({})", verus_word(x))
            } else {
                format!("(({}) & {}u8)", verus_word(x), (1u16 << k) - 1)
            }
        }
        WExpr::Ite(c, t, f) => {
            format!("(if {} {{ {} }} else {{ {} }})", verus_pred(c), verus_word(t), verus_word(f))
        }
    }
}

/// Interpret a predicate tree as a Verus `spec` boolean expression.
pub fn verus_pred(e: &PExpr) -> String {
    match e {
        PExpr::Eq(x, y) => format!("(({}) == ({}))", verus_word(x), verus_word(y)),
        PExpr::Ne(x, y) => format!("(({}) != ({}))", verus_word(x), verus_word(y)),
        PExpr::Ult(x, y) => format!("(({}) < ({}))", verus_word(x), verus_word(y)),
        PExpr::Ule(x, y) => format!("(({}) <= ({}))", verus_word(x), verus_word(y)),
        PExpr::And(x, y) => format!("(({}) && ({}))", verus_pred(x), verus_pred(y)),
        PExpr::Or(x, y) => format!("(({}) || ({}))", verus_pred(x), verus_pred(y)),
        // Verus has `!=`/`==` on `bool`, same as Rust.
        PExpr::Xor(x, y) => format!("(({}) != ({}))", verus_pred(x), verus_pred(y)),
        PExpr::Not(x) => format!("(!({}))", verus_pred(x)),
        PExpr::Iff(x, y) => format!("(({}) == ({}))", verus_pred(x), verus_pred(y)),
    }
}

/// Interpret a word tree as a Lean 4 `BitVec 8` expression.
///
/// Lean is the strongest oracle here, and for a structural reason rather than a
/// performance one: `bv_decide` proves a bitvector goal by emitting CNF and handing it to
/// a SAT solver, and it *replays the solver's refutation as a Lean proof term* that the
/// kernel then checks. So the verdict is not "a solver said so", it is a machine-checked
/// theorem about the same 8-bit words the engine blasts. That is a genuinely independent
/// decision procedure, which is the property the `z3diff` header notes a printer-agreement
/// check does not have.
///
/// `BitVec` arithmetic is already wrapping, and Lean's shifts are defined at and beyond the
/// width (`a >>> 8 = 0`), so unlike [`rust_word`] there is no need to route through
/// helpers to reproduce the engine's shift rule -- the language's own rule is the engine's.
pub fn lean_word(e: &WExpr) -> String {
    match e {
        WExpr::Const(k) => format!("({k} : BitVec 8)"),
        WExpr::Param(i) => PARAMS[*i].to_string(),
        WExpr::Add(x, y) => format!("({} + {})", lean_word(x), lean_word(y)),
        WExpr::Sub(x, y) => format!("({} - {})", lean_word(x), lean_word(y)),
        WExpr::Mul(x, y) => format!("({} * {})", lean_word(x), lean_word(y)),
        WExpr::Band(x, y) => format!("({} &&& {})", lean_word(x), lean_word(y)),
        WExpr::Bor(x, y) => format!("({} ||| {})", lean_word(x), lean_word(y)),
        WExpr::Bxor(x, y) => format!("({} ^^^ {})", lean_word(x), lean_word(y)),
        WExpr::Shl(x, k) => format!("({} <<< {k})", lean_word(x)),
        WExpr::Lshr(x, k) => format!("({} >>> {k})", lean_word(x)),
        WExpr::Ashr(x, k) => format!("({}.sshiftRight {k})", lean_word(x)),
        WExpr::Mask(x, k) => {
            if *k >= 8 {
                lean_word(x)
            } else {
                // `.setWidth 8` is load-bearing, not decoration. Lean's `setWidth` returns a
                // `BitVec` of the *new* width, and `==` is homogeneous, so a bare
                // `(x).setWidth 4` cannot be compared against a `BitVec 8` at all -- the
                // first version of this generator emitted that and every masked case came
                // back as a `Type mismatch` rather than a verdict. Widening back to 8 zero
                // extends, which is exactly the engine's `truncate`-then-widen `Mask`.
                format!("((({}).setWidth {k}).setWidth 8)", lean_word(x))
            }
        }
        WExpr::Ite(c, t, f) => {
            format!("(if {} then {} else {})", lean_pred(c), lean_word(t), lean_word(f))
        }
    }
}

/// Interpret a predicate tree as a Lean 4 `Bool` expression, for `bv_decide`.
///
/// Lean's `==`/`!=`/`<`/`<=` on `BitVec` are *decidable propositions* (`DecidableEq`), not
/// `Bool`, and the two do not mix: `(a == b) && c` is a type error. Spelling the predicate
/// out in `Bool` and then asserting `= true` is the way to keep it composable -- and it is
/// the same shape [`rust_pred`] and [`verus_pred`] produce, so all three generators describe
/// one boolean tree rather than three.
pub fn lean_pred(e: &PExpr) -> String {
    match e {
        PExpr::Eq(x, y) => format!("({} == {})", lean_word(x), lean_word(y)),
        PExpr::Ne(x, y) => format!("({} != {})", lean_word(x), lean_word(y)),
        PExpr::Ult(x, y) => format!("({} < {})", lean_word(x), lean_word(y)),
        PExpr::Ule(x, y) => format!("({} <= {})", lean_word(x), lean_word(y)),
        PExpr::And(x, y) => format!("({} && {})", lean_pred(x), lean_pred(y)),
        PExpr::Or(x, y) => format!("({} || {})", lean_pred(x), lean_pred(y)),
        PExpr::Xor(x, y) => format!("({} != {})", lean_pred(x), lean_pred(y)),
        PExpr::Not(x) => format!("(!{})", lean_pred(x)),
        PExpr::Iff(x, y) => format!("({} == {})", lean_pred(x), lean_pred(y)),
    }
}

/// Interpret a word tree as a Coq `Z` expression, masked to 8 bits.
///
/// Coq has no machine word in its standard library, so this generator does the arithmetic
/// the engine does -- over unbounded `Z`, with every operation masked back to `[0, 256)` --
/// and the harness enumerates all 256 values per variable with `vm_compute`. That makes Coq
/// a *different kind* of oracle from the other three: it does not decide the formula, it
/// evaluates it on every point of the domain, which is exactly what
/// `verif-arithmetic`'s own enumeration proofs do but in a kernel that was not written
/// by this crate. Agreement is therefore evidence about the engine's *transcription* into
/// `Z` as much as about its verdicts.
///
/// Every operator is spelled `Z.land`/`Z.shiftl`/... explicitly, and every result is masked,
/// so the generated file is auditable against the engine's documented semantics line by
/// line. The helpers are emitted by [`coq_preamble`].
pub fn coq_word(e: &WExpr) -> String {
    match e {
        WExpr::Const(k) => format!("{k}"),
        WExpr::Param(i) => PARAMS[*i].to_string(),
        // Coq applies by juxtaposition, not by comma: `add_eng a b`, not `add_eng(a, b)`.
        // The first version of this generator emitted the Rust/C style and every case failed
        // to compile with "The term add_eng has type Z -> Z -> Z while it is expected to have
        // type Z", which the harness correctly refused to read as a verdict.
        WExpr::Add(x, y) => format!("(add_eng {} {})", coq_word(x), coq_word(y)),
        WExpr::Sub(x, y) => format!("(sub_eng {} {})", coq_word(x), coq_word(y)),
        WExpr::Mul(x, y) => format!("(mul_eng {} {})", coq_word(x), coq_word(y)),
        WExpr::Band(x, y) => format!("(band_eng {} {})", coq_word(x), coq_word(y)),
        WExpr::Bor(x, y) => format!("(bor_eng {} {})", coq_word(x), coq_word(y)),
        WExpr::Bxor(x, y) => format!("(bxor_eng {} {})", coq_word(x), coq_word(y)),
        WExpr::Shl(x, k) => format!("(shl_eng {} {k})", coq_word(x)),
        WExpr::Lshr(x, k) => format!("(lshr_eng {} {k})", coq_word(x)),
        WExpr::Ashr(x, k) => format!("(ashr_eng {} {k})", coq_word(x)),
        WExpr::Mask(x, k) => {
            if *k >= 8 {
                coq_word(x)
            } else {
                format!("(band_eng {} {})", coq_word(x), (1u16 << k) - 1)
            }
        }
        WExpr::Ite(c, t, f) => {
            format!("(if {} then {} else {})", coq_pred(c), coq_word(t), coq_word(f))
        }
    }
}

/// Interpret a predicate tree as a Coq `bool` expression.
pub fn coq_pred(e: &PExpr) -> String {
    match e {
        PExpr::Eq(x, y) => format!("(Z.eqb {} {})", coq_word(x), coq_word(y)),
        PExpr::Ne(x, y) => format!("(negb (Z.eqb {} {}))", coq_word(x), coq_word(y)),
        PExpr::Ult(x, y) => format!("(Z.ltb {} {})", coq_word(x), coq_word(y)),
        PExpr::Ule(x, y) => format!("(Z.leb {} {})", coq_word(x), coq_word(y)),
        PExpr::And(x, y) => format!("(andb {} {})", coq_pred(x), coq_pred(y)),
        PExpr::Or(x, y) => format!("(orb {} {})", coq_pred(x), coq_pred(y)),
        PExpr::Xor(x, y) => format!("(xorb {} {})", coq_pred(x), coq_pred(y)),
        PExpr::Not(x) => format!("(negb {})", coq_pred(x)),
        PExpr::Iff(x, y) => format!("(Bool.eqb {} {})", coq_pred(x), coq_pred(y)),
    }
}

/// The Coq preamble every generated file needs: the enumeration domain and one helper per
/// operator, each spelling out the engine's rule.
///
/// `coq_word`/`coq_pred` emit calls to these, so this is where the transcription actually
/// lives and it is written to be read against the engine's own documentation. The two
/// interesting rules are the shift-at-or-past-width rule (`shl_eng`/`lshr_eng`/`ashr_eng`
/// return 0 or the sign fill, where Rust would not even define the operation) and `ashr_eng`
/// itself, which needs the sign bit of the *8-bit* value, not of the unbounded `Z`.
pub fn coq_preamble() -> &'static str {
    "Require Import ZArith List Bool.\n\
     Open Scope Z_scope.\n\
     \n\
     Definition r256 : list Z := List.map Z.of_nat (List.seq 0 256).\n\
     Definition forall256 (f : Z -> bool) : bool := List.forallb f r256.\n\
     \n\
     Definition add_eng (x y : Z) : Z := Z.land (Z.add x y) 255.\n\
     Definition sub_eng (x y : Z) : Z := Z.land (Z.sub x y) 255.\n\
     Definition mul_eng (x y : Z) : Z := Z.land (Z.mul x y) 255.\n\
     Definition band_eng (x y : Z) : Z := Z.land x y.\n\
     Definition bor_eng (x y : Z) : Z := Z.lor x y.\n\
     Definition bxor_eng (x y : Z) : Z := Z.lxor x y.\n\
     Definition shl_eng (x k : Z) : Z := if Z.leb 8 k then 0 else Z.land (Z.shiftl x k) 255.\n\
     Definition lshr_eng (x k : Z) : Z := if Z.leb 8 k then 0 else Z.shiftr x k.\n\
     Definition ashr_eng (x k : Z) : Z :=\n\
     \x20 if Z.leb 8 k then (if Z.leb 128 x then 255 else 0)\n\
     \x20 else (if Z.leb 128 x then Z.land (Z.lor (Z.shiftr x k) (Z.land (Z.shiftl (-1) (8 - k)) 255)) 255\n\
     \x20 else Z.shiftr x k).\n"
}

/// Which of [`PARAMS`] a predicate actually mentions, in order.
///
/// The Coq harness enumerates the domain, and enumerating a variable a case never reads
/// multiplies the work by 256 for no verdict. `mask_8_identity` mentions only `a`, so it is
/// 256 evaluations rather than 16.7M, and the result is the same. This matters because it
/// is the difference between the Coq oracle running in a gate and not: three-variable cases
/// are minutes, two-variable ones are under a second.
pub fn params_used(e: &PExpr) -> Vec<usize> {
    fn word(e: &WExpr, seen: &mut Vec<usize>) {
        match e {
            WExpr::Const(_) => {}
            WExpr::Param(i) => {
                if !seen.contains(i) {
                    seen.push(*i);
                }
            }
            WExpr::Add(x, y)
            | WExpr::Sub(x, y)
            | WExpr::Mul(x, y)
            | WExpr::Band(x, y)
            | WExpr::Bor(x, y)
            | WExpr::Bxor(x, y) => {
                word(x, seen);
                word(y, seen);
            }
            WExpr::Shl(x, _) | WExpr::Lshr(x, _) | WExpr::Ashr(x, _) | WExpr::Mask(x, _) => {
                word(x, seen)
            }
            WExpr::Ite(c, t, f) => {
                pred(c, seen);
                word(t, seen);
                word(f, seen);
            }
        }
    }
    fn pred(e: &PExpr, seen: &mut Vec<usize>) {
        match e {
            PExpr::Eq(x, y) | PExpr::Ne(x, y) | PExpr::Ult(x, y) | PExpr::Ule(x, y) => {
                word(x, seen);
                word(y, seen);
            }
            PExpr::And(x, y)
            | PExpr::Or(x, y)
            | PExpr::Xor(x, y)
            | PExpr::Iff(x, y) => {
                pred(x, seen);
                pred(y, seen);
            }
            PExpr::Not(x) => pred(x, seen),
        }
    }
    let mut seen = Vec::new();
    pred(e, &mut seen);
    seen
}

/// Word expressions. One tree, interpreted into the engine ([`to_word`]) and into each
/// backend's source ([`rust_word`] for Kani, [`verus_word`] for Verus, [`lean_word`] for
/// Lean, [`coq_word`] for Coq).
pub enum WExpr {
    Const(u64),
    Param(usize),
    Add(Box<WExpr>, Box<WExpr>),
    Sub(Box<WExpr>, Box<WExpr>),
    Mul(Box<WExpr>, Box<WExpr>),
    Band(Box<WExpr>, Box<WExpr>),
    Bor(Box<WExpr>, Box<WExpr>),
    Bxor(Box<WExpr>, Box<WExpr>),
    Shl(Box<WExpr>, u32),
    Lshr(Box<WExpr>, u32),
    Ashr(Box<WExpr>, u32),
    /// Narrow to `k` bits then widen back: a mask, through the one width-changing operation.
    /// The pairing is deliberate -- a bare narrowing leaves a width-`k` term every comparison
    /// refuses, so the case would be `Unknown` rather than checked.
    Mask(Box<WExpr>, u8),
    Ite(Box<PExpr>, Box<WExpr>, Box<WExpr>),
}

/// Predicate expressions.
pub enum PExpr {
    Eq(Box<WExpr>, Box<WExpr>),
    Ne(Box<WExpr>, Box<WExpr>),
    Ult(Box<WExpr>, Box<WExpr>),
    Ule(Box<WExpr>, Box<WExpr>),
    And(Box<PExpr>, Box<PExpr>),
    Or(Box<PExpr>, Box<PExpr>),
    Xor(Box<PExpr>, Box<PExpr>),
    Not(Box<PExpr>),
    Iff(Box<PExpr>, Box<PExpr>),
}

/// Interpret a word tree as an engine term.
pub fn to_word(e: &WExpr, vars: &[Var]) -> Var {
    match e {
        WExpr::Const(k) => smt::word(*k, W),
        WExpr::Param(i) => vars[*i],
        WExpr::Add(x, y) => smt::add(to_word(x, vars), to_word(y, vars)),
        WExpr::Sub(x, y) => smt::sub(to_word(x, vars), to_word(y, vars)),
        WExpr::Mul(x, y) => smt::mul(to_word(x, vars), to_word(y, vars)),
        WExpr::Band(x, y) => smt::band(to_word(x, vars), to_word(y, vars)),
        WExpr::Bor(x, y) => smt::bor(to_word(x, vars), to_word(y, vars)),
        WExpr::Bxor(x, y) => smt::bxor(to_word(x, vars), to_word(y, vars)),
        WExpr::Shl(x, k) => smt::shl(to_word(x, vars), *k),
        WExpr::Lshr(x, k) => smt::lshr(to_word(x, vars), *k),
        WExpr::Ashr(x, k) => smt::ashr(to_word(x, vars), *k),
        WExpr::Mask(x, k) => smt::truncate(smt::truncate(to_word(x, vars), *k), W),
        WExpr::Ite(c, t, f) => smt::ite(to_pred(c, vars), to_word(t, vars), to_word(f, vars)),
    }
}

/// Interpret a predicate tree as an engine predicate.
pub fn to_pred(e: &PExpr, vars: &[Var]) -> Pred {
    match e {
        PExpr::Eq(x, y) => smt::eq(to_word(x, vars), to_word(y, vars)),
        PExpr::Ne(x, y) => smt::not(smt::eq(to_word(x, vars), to_word(y, vars))),
        PExpr::Ult(x, y) => smt::ult(to_word(x, vars), to_word(y, vars)),
        PExpr::Ule(x, y) => smt::ule(to_word(x, vars), to_word(y, vars)),
        PExpr::And(x, y) => smt::and(to_pred(x, vars), to_pred(y, vars)),
        PExpr::Or(x, y) => smt::or(to_pred(x, vars), to_pred(y, vars)),
        PExpr::Xor(x, y) => smt::xor(to_pred(x, vars), to_pred(y, vars)),
        PExpr::Not(x) => smt::not(to_pred(x, vars)),
        PExpr::Iff(x, y) => smt::iff(to_pred(x, vars), to_pred(y, vars)),
    }
}

pub fn rand_word(r: &mut Lcg, depth: u32) -> WExpr {
    if depth == 0 || r.below(3) == 0 {
        return if r.below(3) == 0 {
            WExpr::Const(r.below(256))
        } else {
            WExpr::Param(r.below(PARAMS.len() as u64) as usize)
        };
    }
    let tag = r.below(12);
    match tag {
        0 => bin(r, depth, WExpr::Add),
        1 => bin(r, depth, WExpr::Sub),
        2 => bin(r, depth, WExpr::Mul),
        3 => bin(r, depth, WExpr::Band),
        4 => bin(r, depth, WExpr::Bor),
        5 => bin(r, depth, WExpr::Bxor),
        // Shift amounts run to `W + 1` so the "shift everything out" edge is reachable,
        // which is where a `bit - sh` underflow would live.
        6 => {
            let a = rand_word(r, depth - 1);
            let k = r.below(W as u64 + 2) as u32;
            WExpr::Shl(Box::new(a), k)
        }
        7 => {
            let a = rand_word(r, depth - 1);
            let k = r.below(W as u64 + 2) as u32;
            WExpr::Lshr(Box::new(a), k)
        }
        8 => {
            let a = rand_word(r, depth - 1);
            let k = r.below(W as u64 + 2) as u32;
            WExpr::Ashr(Box::new(a), k)
        }
        9 => {
            let a = rand_word(r, depth - 1);
            let k = 1 + r.below(W as u64) as u8;
            WExpr::Mask(Box::new(a), k)
        }
        10 => {
            let c = rand_pred(r, depth - 1);
            let a = rand_word(r, depth - 1);
            let b = rand_word(r, depth - 1);
            WExpr::Ite(Box::new(c), Box::new(a), Box::new(b))
        }
        _ => bin(r, depth, WExpr::Add),
    }
}

fn bin(r: &mut Lcg, depth: u32, f: fn(Box<WExpr>, Box<WExpr>) -> WExpr) -> WExpr {
    let a = rand_word(r, depth - 1);
    let b = rand_word(r, depth - 1);
    f(Box::new(a), Box::new(b))
}

pub fn rand_pred(r: &mut Lcg, depth: u32) -> PExpr {
    if depth == 0 || r.below(4) == 0 {
        let tag = r.below(4);
        let a = rand_word(r, 0);
        let b = rand_word(r, 0);
        return match tag {
            0 => PExpr::Eq(Box::new(a), Box::new(b)),
            1 => PExpr::Ne(Box::new(a), Box::new(b)),
            2 => PExpr::Ult(Box::new(a), Box::new(b)),
            _ => PExpr::Ule(Box::new(a), Box::new(b)),
        };
    }
    let tag = r.below(6);
    match tag {
        0 => pbin(r, depth, PExpr::And),
        1 => pbin(r, depth, PExpr::Or),
        2 => pbin(r, depth, PExpr::Xor),
        3 => {
            let a = rand_pred(r, depth - 1);
            PExpr::Not(Box::new(a))
        }
        4 => pbin(r, depth, PExpr::Iff),
        _ => {
            let a = rand_word(r, depth - 1);
            let b = rand_word(r, depth - 1);
            PExpr::Eq(Box::new(a), Box::new(b))
        }
    }
}

fn pbin(r: &mut Lcg, depth: u32, f: fn(Box<PExpr>, Box<PExpr>) -> PExpr) -> PExpr {
    let a = rand_pred(r, depth - 1);
    let b = rand_pred(r, depth - 1);
    f(Box::new(a), Box::new(b))
}

/// A generated case: its name, the engine's verdict, and the IR both backends compile.
pub struct Case {
    pub name: String,
    pub ir: PExpr,
    pub ours: smt::Verdict,
}

/// Run one predicate through the engine in its own fresh arena.
///
/// One arena per case, not one shared arena: sharing it exhausted the node budget after
/// about a dozen queries and every later query came back `Unknown`, which the harness then
/// reported as the engine being weak when the *harness* had run the arena dry. That is the
/// "summary outrunning its evidence" shape this crate keeps finding, and it was found by
/// the harness failing, not by reading.
pub fn engine_verdict(ir: &PExpr) -> smt::Verdict {
    smt::with_fresh(|| {
        let vars: Vec<Var> = PARAMS.iter().map(|_| smt::var(W)).collect();
        smt::prove(to_pred(ir, &vars))
    })
}

/// Identities that are true by construction, one or more per operator.
///
/// The random sweep is mostly refutable formulas and it compares only proved/refuted, so a
/// wrong-but-plausible blasting arm can survive it: if the engine blasted `sub` as `add`, a
/// random `a - b == a + b` is still refuted, so both sides agree. This corpus is the other
/// half -- true identities each backend must *prove* -- so a wrong arm turns a proved
/// identity into a refuted one and the disagreement is visible.
pub fn identity_corpus() -> Vec<(&'static str, PExpr)> {
    use PExpr::*;
    use WExpr::*;
    let p = |i: usize| Box::new(Param(i));
    let c = |k: u64| Box::new(Const(k));
    let w = |e: WExpr| Box::new(e);
    let pp = |e: PExpr| Box::new(e);
    vec![
        ("add_comm", Eq(w(Add(p(0), p(1))), w(Add(p(1), p(0))))),
        ("add_assoc",
         Eq(w(Add(w(Add(p(0), p(1))), p(2))), w(Add(p(0), w(Add(p(1), p(2))))))),
        ("sub_self", Eq(w(Sub(p(0), p(0))), c(0))),
        ("add_sub_cancel", Eq(w(Sub(w(Add(p(0), p(1))), p(1))), p(0))),
        ("sub_zero", Eq(w(Sub(p(0), c(0))), p(0))),
        ("mul_comm", Eq(w(Mul(p(0), p(1))), w(Mul(p(1), p(0))))),
        ("mul_zero", Eq(w(Mul(p(0), c(0))), c(0))),
        ("mul_one", Eq(w(Mul(p(0), c(1))), p(0))),
        ("band_comm", Eq(w(Band(p(0), p(1))), w(Band(p(1), p(0))))),
        ("band_self", Eq(w(Band(p(0), p(0))), p(0))),
        ("bor_self", Eq(w(Bor(p(0), p(0))), p(0))),
        ("bxor_self", Eq(w(Bxor(p(0), p(0))), c(0))),
        ("band_all_ones", Eq(w(Band(p(0), c(0xff))), p(0))),
        ("bor_zero", Eq(w(Bor(p(0), c(0))), p(0))),
        // De Morgan as an equivalence. A bare `Not(And(..))` is *not* a tautology (it fails
        // at `a=0,b=1,c=2`), which the first corpus got wrong; the engine correctly refuted
        // it and the harness blamed the engine. The law needs both sides.
        ("demorgan",
         Iff(
            pp(Not(pp(And(pp(Ult(p(0), p(1))), pp(Ult(p(1), p(2))))))),
            pp(Or(pp(Not(pp(Ult(p(0), p(1))))), pp(Not(pp(Ult(p(1), p(2))))))),
         )),
        ("shl_zero", Eq(w(Shl(p(0), 0)), p(0))),
        ("lshr_zero", Eq(w(Lshr(p(0), 0)), p(0))),
        ("ashr_zero", Eq(w(Ashr(p(0), 0)), p(0))),
        ("shl_eight", Eq(w(Shl(p(0), 8)), c(0))),
        ("lshr_eight", Eq(w(Lshr(p(0), 8)), c(0))),
        ("lshr_seven_80", Eq(w(Lshr(c(0x80), 7)), c(0x01))),
        ("ashr_sign_80", Eq(w(Ashr(c(0x80), 7)), c(0xff))),
        ("ashr_top_0x40", Eq(w(Ashr(c(0x40), 7)), c(0x00))),
        ("shl_one_80", Eq(w(Shl(c(0x80), 1)), c(0x00))),
        ("ult_irrefl", Not(pp(Ult(p(0), p(0))))),
        ("ule_refl", Ule(p(0), p(0))),
        ("ult_antisym", Not(pp(And(pp(Ult(p(0), p(1))), pp(Ult(p(1), p(0))))))),
        ("ult_total_ish", Or(pp(Ult(p(0), p(1))), pp(Ule(p(1), p(0))))),
        ("mask_4_upper_clear", Eq(w(Mask(p(0), 4)), w(Band(p(0), c(0x0f))))),
        ("mask_8_identity", Eq(w(Mask(p(0), 8)), p(0))),
        // `ite` selects the branch the guard picks -- as an *implication*, not an
        // equivalence. `guard <=> ite(guard,a,b)==a` is false at `a==b`, the second corpus
        // mistake the engine correctly refuted.
        ("ite_guard",
         Or(
            pp(Not(pp(Ult(p(0), p(1))))),
            pp(Eq(w(Ite(pp(Ult(p(0), p(1))), p(0), p(1))), p(0))),
         )),
        ("add_band_distrib_lo",
         Eq(w(Band(w(Add(p(0), p(1))), c(1))), w(Band(w(Bxor(p(0), p(1))), c(1))))),
    ]
}

/// A deliberately false identity, to prove a harness can report a disagreement.
///
/// If every case agreed, "agreement" would be indistinguishable from a harness that never
/// asks a real question. `a - b == b - a` is false at `a != b`, so both sides must refute
/// it; each harness fails its run if either side *proves* it.
pub fn negative_control() -> PExpr {
    use PExpr::*;
    use WExpr::*;
    Eq(
        Box::new(Sub(Box::new(Param(0)), Box::new(Param(1)))),
        Box::new(Sub(Box::new(Param(1)), Box::new(Param(0)))),
    )
}

/// Build the full case list: corpus, negative control, then `n` random predicates.
pub fn build_cases(n: u32) -> Vec<Case> {
    let mut cases = Vec::new();
    for (name, ir) in identity_corpus() {
        let ours = engine_verdict(&ir);
        cases.push(Case { name: name.to_string(), ir, ours });
    }
    let control = negative_control();
    let ours = engine_verdict(&control);
    cases.push(Case { name: "NEGATIVE_CONTROL_sub_comm".to_string(), ir: control, ours });

    let mut r = Lcg(0x5eed_1234_9abc_def0);
    for i in 0..n {
        let ir = rand_pred(&mut r, 3);
        let ours = engine_verdict(&ir);
        cases.push(Case { name: format!("rand_{i}"), ir, ours });
    }
    cases
}

/// The identity-corpus check every harness shares: a true identity the engine *refutes* is
/// a soundness failure; one it leaves `Unknown` is the engine being conservative, which is
/// allowed and only reported. Collapsing the two is what let a mutated engine pass.
pub struct IdentityTally {
    pub refuted: usize,
    pub undecided: usize,
}

pub fn tally_identities(cases: &[Case], label: &str) -> IdentityTally {
    let mut t = IdentityTally { refuted: 0, undecided: 0 };
    for case in cases {
        let is_identity = !case.name.starts_with("rand_") && !case.name.starts_with("NEGATIVE");
        if !is_identity {
            continue;
        }
        match case.ours {
            smt::Verdict::Valid => {}
            smt::Verdict::Unknown => {
                t.undecided += 1;
                eprintln!(
                    "[{label}] IDENTITY UNDECIDED {} (engine conservative; not a soundness bug)",
                    case.name
                );
            }
            _ => {
                t.refuted += 1;
                eprintln!(
                    "[{label}] IDENTITY REFUTED {} ours={:?} -- a true identity the engine rejected",
                    case.name, case.ours
                );
            }
        }
    }
    t
}

/// The three-way outcome of one backend's verdict on one case, compared with the engine's.
///
/// The soundness matrix matters: only the engine's `Unknown` is "no claim". Every other
/// engine verdict is a definite claim the backend must not contradict, and the first version
/// of this comparison lumped all "engine did not prove it" together -- so a real blast bug
/// produced `mismatch=0` and was caught only by the curated corpus. See `tally_backend`.
pub struct BackendTally {
    pub agree: usize,
    pub mismatch: usize,
    pub missing: usize,
}

/// Compare one backend's per-case verdicts against the engine's, filling a [`BackendTally`].
///
/// `proved` is the backend's verdict: `Some(true)` it proved the case, `Some(false)` it
/// refuted it, `None` it produced no verdict (which is a failure, not a pass).
pub fn tally_backend(
    cases: &[Case],
    proved: impl Fn(&Case) -> Option<bool>,
    label: &str,
) -> BackendTally {
    let mut t = BackendTally { agree: 0, mismatch: 0, missing: 0 };
    for case in cases {
        match proved(case) {
            Some(true) => match case.ours {
                smt::Verdict::Valid => t.agree += 1,
                smt::Verdict::Unknown => {
                    // The engine made no claim; the backend proving it is the engine being
                    // conservative, which is allowed.
                    t.agree += 1;
                    if !case.name.starts_with("rand_") {
                        eprintln!(
                            "[{label}] NOTE {}: engine=Unknown backend=PROVED (engine incomplete)",
                            case.name
                        );
                    }
                }
                _ => {
                    // Engine said "refuted", backend says "always true": a definite
                    // disagreement, the soundness bug the random sweep exists to find.
                    t.mismatch += 1;
                    eprintln!(
                        "[{label}] MISMATCH {}: engine={:?} backend=PROVED (engine refuted a truth)",
                        case.name, case.ours
                    );
                }
            },
            Some(false) => {
                if case.ours.is_proved() {
                    t.mismatch += 1;
                    eprintln!(
                        "[{label}] MISMATCH {}: engine=Valid backend=REFUTED",
                        case.name
                    );
                } else {
                    t.agree += 1;
                }
            }
            None => {
                t.missing += 1;
                eprintln!("[{label}] MISSING {}: backend produced no verdict", case.name);
            }
        }
    }
    t
}