bitwright 0.32.9

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
//! Lowering from the arena into an [`MbaExpr`], and lifting back.

use super::expr::{MNode, MOp, MbaExpr};
use crate::engine::pass::Lease;
use crate::error::Error;
use crate::expr::{Context, Expr, OpCode};
use crate::hash::IdMap;
use crate::ops::{BinOp, UnOp};
use crate::{BitVec, Width};

/// Caps on what is lowered and asked of a solver.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct MbaLimits {
    /// The most variables.
    pub max_vars: u32,
    /// The most nodes.
    pub max_nodes: u32,
    /// The widest node.
    pub max_width: u16,
    /// Fewer nodes than this are not worth a solver call.
    pub min_nodes: u32,
}

impl Default for MbaLimits {
    fn default() -> Self {
        MbaLimits {
            max_vars: 8,
            max_nodes: 2048,
            max_width: 512,
            min_nodes: 4,
        }
    }
}

setters!(MbaLimits {
    with_max_vars: max_vars: u32,
    with_max_nodes: max_nodes: u32,
    with_max_width: max_width: u16,
    with_min_nodes: min_nodes: u32,
});

/// Why an expression was not lowered or not solved.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Refusal {
    /// More variables than the limit.
    TooManyVars,
    /// More nodes than the limit.
    TooLarge,
    /// A node wider than the limit.
    TooWide,
    /// Fewer nodes than worth asking about.
    TooSmall,
    /// Not a mix of arithmetic and bitwise operators.
    NotMixed,
    /// The solver does not handle this input (its reason).
    Unsupported(String),
}

/// The expressions the variables of a lowered [`MbaExpr`] stand for.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Bindings {
    pub(crate) atoms: Vec<u32>,
}

impl Bindings {
    /// The number of variables.
    pub fn len(&self) -> usize {
        self.atoms.len()
    }

    /// Whether there are none.
    pub fn is_empty(&self) -> bool {
        self.atoms.is_empty()
    }
}

/// Whether node `i` belongs to the fragment (otherwise it is an atom).
fn in_fragment(cx: &Context, i: u32) -> bool {
    let n = cx.node(i);
    match n.op {
        OpCode::Const
        | OpCode::Add
        | OpCode::Sub
        | OpCode::Mul
        | OpCode::Neg
        | OpCode::And
        | OpCode::Or
        | OpCode::Xor
        | OpCode::Not
        | OpCode::Zext
        | OpCode::Sext
        | OpCode::Concat => true,
        // any slice, as a right shift then a truncation (`extract<8, 8>(x)` is
        // `trunc<8>(x >> 8)`), so a byte of a value stays related to the value.
        OpCode::Extract => n.b + u32::from(n.width) <= u32::from(cx.width_of(n.a).bits()),
        OpCode::Shl | OpCode::LShr => cx
            .const_val(n.b)
            .and_then(|k| k.to_u64())
            .is_some_and(|k| k < u64::from(n.width)),
        _ => false,
    }
}

/// Whether a narrower reading's atoms `bindings` include a node the whole fragment reads
/// through (arithmetic under a bitwise operator, a value cut off): otherwise the reading is
/// the whole fragment's.
pub(crate) fn cuts(cx: &Context, bindings: &Bindings) -> bool {
    bindings.atoms.iter().any(|&a| in_fragment(cx, a))
}

/// Whether the fragment rooted at `root` is one [`lower_id`] takes (rooted at a fragment
/// operator, within `lim`): its interior nodes, operands first, when it is.
pub(crate) fn fits_nodes(cx: &Context, root: u32, lim: &MbaLimits) -> Result<Vec<u32>, Refusal> {
    let (_, order) = scan(cx, root, lim)?;
    Ok(order)
}

/// the narrower fragments [`lower_id_reading`] lowers.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) enum Reading {
    /// `+ − neg`, products and left shifts by a constant, and `& | ^ ~` with any constants, at
    /// one width: a semi-linear MBA.
    SemiLinear,
    /// The same, with the operands of `& | ^ ~` bitwise functions of atoms: arithmetic under a
    /// bitwise operator is an atom, as obfuscators build a semi-linear MBA (`Σ cᵢ·fᵢ(x)` over
    /// whatever values they take for `x`).
    Shallow,
    /// The same with `& | ^ ~` only over 0 and all-ones: a linear MBA, decided at the corners.
    Linear,
    /// Semi-linear, with this node (shared by several terms) an atom: `Σ cᵢ·fᵢ(a)` over a
    /// value every term reads.
    Cut(u32),
    /// The whole fragment, casts included, with this node an atom: a block over a rotated value
    /// and over bytes and halves of it (`trunc<32>(t)`, `zext<64>(trunc<8>(n))`), which stay
    /// related to `t` and `n`.
    CutWhole(u32),
}

/// whether node `i` is interior to `reading`'s fragment.
fn in_reading(cx: &Context, i: u32, reading: Reading) -> bool {
    let n = cx.node(i);
    let uniform = |k: u32| cx.const_val(k).is_none_or(|v| v.is_zero() || v.is_ones());
    match n.op {
        OpCode::Const | OpCode::Add | OpCode::Sub | OpCode::Neg => true,
        OpCode::Mul => cx.const_val(n.a).is_some() || cx.const_val(n.b).is_some(),
        OpCode::Shl => reading == Reading::SemiLinear && in_fragment(cx, i),
        OpCode::And | OpCode::Or | OpCode::Xor | OpCode::Not => {
            reading == Reading::SemiLinear || n.children().all(uniform)
        }
        _ => false,
    }
}

/// The fragment rooted at `root`: its atoms (in order of discovery) and its other nodes,
/// operands first.
fn scan(cx: &Context, root: u32, lim: &MbaLimits) -> Result<(Vec<u32>, Vec<u32>), Refusal> {
    scan_in(cx, root, lim, &in_fragment)
}

/// [`scan`] over the fragment `in_fragment` defines.
fn scan_in(
    cx: &Context,
    root: u32,
    lim: &MbaLimits,
    in_fragment: &dyn Fn(&Context, u32) -> bool,
) -> Result<(Vec<u32>, Vec<u32>), Refusal> {
    Dense::with(&SEEN, cx.len(), |seen| {
        // (Sized for the fragments most questions have: grown a push at a time, the three
        // were a quarter of what a scan cost.)
        let mut atoms: Vec<u32> = Vec::with_capacity(16);
        let mut order: Vec<u32> = Vec::with_capacity(64);
        let mut met = 0u32;
        let mut stack: Vec<(u32, bool)> = Vec::with_capacity(64);
        stack.push((root, false));
        while let Some((i, expanded)) = stack.pop() {
            if expanded {
                order.push(i);
                continue;
            }
            if seen.insert(i, 0) {
                continue;
            }
            met += 1;
            if cx.node(i).width > lim.max_width {
                return Err(Refusal::TooWide);
            }
            if met > lim.max_nodes {
                return Err(Refusal::TooLarge);
            }
            if !in_fragment(cx, i) {
                if i == root {
                    return Err(Refusal::Unsupported("not an MBA operator".into()));
                }
                atoms.push(i);
                if atoms.len() as u32 > lim.max_vars {
                    return Err(Refusal::TooManyVars);
                }
                continue;
            }
            stack.push((i, true));
            for c in cx.node(i).children() {
                stack.push((c, false));
            }
        }
        Ok((atoms, order))
    })
}

/// A map from arena indices to `u32` that is emptied in constant time: a slot holds its value
/// while its stamp is the current generation. The lowering walks one fragment per reading of
/// every candidate, and a hash map built and dropped for each was most of what a walk cost.
#[derive(Default)]
struct Dense {
    stamp: Vec<u32>,
    value: Vec<u32>,
    generation: u32,
}

thread_local! {
    static SEEN: std::cell::RefCell<Dense> = std::cell::RefCell::new(Dense::default());
    static AT: std::cell::RefCell<Dense> = std::cell::RefCell::new(Dense::default());
}

impl Dense {
    /// Runs `f` with the thread's map `key`, empty and sized for `len` indices.
    fn with<R>(
        key: &'static std::thread::LocalKey<std::cell::RefCell<Dense>>,
        len: usize,
        f: impl FnOnce(&mut Dense) -> R,
    ) -> R {
        let mut d = key.with(|c| std::mem::take(&mut *c.borrow_mut()));
        if d.generation == u32::MAX {
            d.stamp.clear();
            d.generation = 0;
        }
        d.generation += 1;
        if d.stamp.len() < len {
            d.stamp.resize(len, 0);
            d.value.resize(len, 0);
        }
        let out = f(&mut d);
        key.with(|c| *c.borrow_mut() = d);
        out
    }

    /// Sets `i` to `v`; whether it had a value.
    fn insert(&mut self, i: u32, v: u32) -> bool {
        let had = self.stamp[i as usize] == self.generation;
        self.stamp[i as usize] = self.generation;
        self.value[i as usize] = v;
        had
    }

    fn get(&self, i: u32) -> Option<u32> {
        (self.stamp[i as usize] == self.generation).then(|| self.value[i as usize])
    }
}

/// Lowers the fragment rooted at `root` (arena index); non-fragment subterms become variables.
pub(crate) fn lower_id(
    cx: &Context,
    root: u32,
    lim: &MbaLimits,
) -> Result<(MbaExpr, Bindings), Refusal> {
    let (atoms, order) = scan(cx, root, lim)?;
    build(cx, atoms, order, &[])
}

/// [`lower_id`] over `reading`'s narrower fragment: every other subterm (a cast,
/// a right shift, a product of two non-constants, for [`Reading::Linear`] a bitwise operation
/// with another constant) is a variable. An identity of the result holds for independent
/// values of those, so for theirs.
pub(crate) fn lower_id_reading(
    cx: &Context,
    root: u32,
    lim: &MbaLimits,
    reading: Reading,
) -> Result<(MbaExpr, Bindings), Refusal> {
    lower_id_over(cx, root, lim, Some(reading), &IdMap::default())
}

/// [`lower_id`] (no reading) or [`lower_id_reading`] with every node of `opaque` below `root`
/// an atom whatever its operator: the fragment ends there. An identity of the result holds for
/// any values of those nodes, so for theirs.
pub(crate) fn lower_id_over(
    cx: &Context,
    root: u32,
    lim: &MbaLimits,
    reading: Option<Reading>,
    opaque: &IdMap<u32, ()>,
) -> Result<(MbaExpr, Bindings), Refusal> {
    let free = |i: u32| i == root || !opaque.contains_key(&i);
    let Some(reading) = reading else {
        let (atoms, order) = scan_in(cx, root, lim, &|cx, i| free(i) && in_fragment(cx, i))?;
        return build(cx, atoms, order, &[]);
    };
    let inside = |cx: &Context, i: u32, under: Option<&Lease>| {
        free(i)
            && match reading {
                Reading::Shallow => {
                    in_reading(cx, i, Reading::SemiLinear)
                        && (bitwise_or_const(cx, i) || under.is_none_or(|u| !is_under(u, i)))
                }
                Reading::Cut(cut) => i != cut && in_reading(cx, i, Reading::SemiLinear),
                Reading::CutWhole(cut) => i != cut && in_fragment(cx, i),
                _ => in_reading(cx, i, reading),
            }
    };
    let under = match reading {
        Reading::Shallow => Some(under_bitwise(cx, root, lim, opaque)?),
        _ => None,
    };
    let (atoms, order) = scan_in(cx, root, lim, &|cx, i| inside(cx, i, under.as_ref()))?;
    // An atom that is its operand and a constant added (`a = t + k`, `a = k − t`), where the
    // fragment reads `t` too: `t` is `a − k` (`k − a`), a sum over the atom, and nothing below
    // `t` is read. The terms linear in `t` are then terms of `a`, which the bitwise operators
    // read: one variable, where `t`'s own atoms beside `a` were two values no test relates.
    let candidates: &[u32] = match reading {
        Reading::Shallow => &atoms,
        Reading::Cut(cut) | Reading::CutWhole(cut) if atoms.contains(&cut) => &[cut],
        _ => &[],
    };
    let mut solved: Vec<Solved> = Vec::new();
    for &a in candidates {
        let Some((t, k, negated)) = offset(cx, a) else {
            continue;
        };
        if t != root
            && (order.contains(&t) || atoms.contains(&t))
            && solved
                .iter()
                .all(|s| s.operand != t && s.atom != t && s.operand != a)
        {
            solved.push(Solved {
                operand: t,
                atom: a,
                constant: k,
                negated,
            });
        }
    }
    if solved.is_empty() {
        return build(cx, atoms, order, &[]);
    }
    let (mut atoms, order) = scan_in(cx, root, lim, &|cx, i| {
        solved.iter().all(|s| s.operand != i) && inside(cx, i, under.as_ref())
    })?;
    // An operand read only below what another one cut off is not read; an atom it left
    // unread is no variable.
    solved.retain(|s| atoms.contains(&s.operand) && atoms.contains(&s.atom));
    atoms.retain(|a| solved.iter().all(|s| s.operand != *a));
    build(cx, atoms, order, &solved)
}

/// [`lower_id_over`] with no reading, and whether the fragment is semi-linear as it is (every
/// node of it one [`Reading::SemiLinear`] takes): the semi-linear reading is then this one, and
/// each cut of the whole fragment is the semi-linear cut at the same node, so a caller that
/// has this need not lower those.
pub(crate) fn lower_id_whole(
    cx: &Context,
    root: u32,
    lim: &MbaLimits,
    opaque: &IdMap<u32, ()>,
) -> Result<(MbaExpr, Bindings, bool), Refusal> {
    let free = |i: u32| i == root || !opaque.contains_key(&i);
    let (atoms, order) = scan_in(cx, root, lim, &|cx, i| free(i) && in_fragment(cx, i))?;
    let semi = order
        .iter()
        .all(|&i| in_reading(cx, i, Reading::SemiLinear));
    let (m, bindings) = build(cx, atoms, order, &[])?;
    Ok((m, bindings, semi))
}

/// Node `a` as `t + k` or (`true`) `k − t`, `k` a constant: `t`, `k`, and which. The negated
/// operand may be written `−t` or `t · −1`.
fn offset(cx: &Context, a: u32) -> Option<(u32, BitVec, bool)> {
    // Operands are read only once the operator says they are nodes.
    let n = cx.node(a);
    if !matches!(n.op, OpCode::Add | OpCode::Sub) {
        return None;
    }
    let (t, k) = match (n.op, cx.const_val(n.a), cx.const_val(n.b)) {
        (OpCode::Add, None, Some(k)) => (n.a, k),
        (OpCode::Add, Some(k), None) => (n.b, k),
        (OpCode::Sub, Some(k), None) => return Some((n.b, k, true)),
        _ => return None,
    };
    let m = cx.node(t);
    match m.op {
        OpCode::Neg => Some((m.a, k, true)),
        OpCode::Mul => match (cx.const_val(m.a), cx.const_val(m.b)) {
            (None, Some(c)) if c.is_ones() => Some((m.a, k, true)),
            (Some(c), None) if c.is_ones() => Some((m.b, k, true)),
            _ => Some((t, k, false)),
        },
        _ => Some((t, k, false)),
    }
}

/// An atom's operand written over the atom: `atom − constant`, or `constant − atom` (`negated`).
struct Solved {
    operand: u32,
    atom: u32,
    constant: BitVec,
    negated: bool,
}

/// the non-constant interior nodes of the semi-linear fragment below `root` that
/// two or more of its nodes read, most read first (at most `n`): where a [`Reading::Cut`] may
/// cut. The fragment ends at the nodes of `opaque` below `root` (see [`lower_id_over`]).
pub(crate) fn shared_nodes(
    cx: &Context,
    root: u32,
    lim: &MbaLimits,
    n: usize,
    opaque: &IdMap<u32, ()>,
) -> Vec<u32> {
    shared_in(cx, root, lim, n, opaque, false)
}

/// [`shared_nodes`] of the whole fragment below `root`, casts and slices included: where a
/// [`Reading::CutWhole`] may cut (a value read once as it is and once through a truncation or
/// an extension is shared here and not there).
pub(crate) fn shared_nodes_whole(cx: &Context, root: u32, lim: &MbaLimits, n: usize) -> Vec<u32> {
    shared_in(cx, root, lim, n, &IdMap::default(), true)
}

fn shared_in(
    cx: &Context,
    root: u32,
    lim: &MbaLimits,
    n: usize,
    opaque: &IdMap<u32, ()>,
    whole: bool,
) -> Vec<u32> {
    let inside = |i: u32| {
        (i == root || !opaque.contains_key(&i))
            && match whole {
                true => in_fragment(cx, i),
                false => in_reading(cx, i, Reading::SemiLinear),
            }
    };
    // (Leased tables: this is asked of every candidate, and hash maps grown from empty for
    // each were a quarter of a large expression's run. The order the users are listed in is
    // of no account: they are sorted, and no two are equal.)
    let mut users = Lease::new(cx.len());
    let mut used: Vec<u32> = Vec::new();
    let mut seen = Lease::new(cx.len());
    let mut met = 0u32;
    let mut stack = vec![root];
    while let Some(i) = stack.pop() {
        if seen.insert(i, 0) {
            continue;
        }
        met += 1;
        if met > lim.max_nodes {
            continue;
        }
        if !inside(i) {
            continue;
        }
        for c in cx.node(i).children() {
            if !users.bump(c) {
                used.push(c);
            }
            stack.push(c);
        }
    }
    let mut v: Vec<(u32, u32)> = used
        .into_iter()
        .map(|i| (i, users.get(i).unwrap_or(0)))
        .filter(|&(i, k)| k >= 2 && cx.const_val(i).is_none() && inside(i))
        .filter(|&(i, _)| cx.node(i).op != OpCode::Not)
        .map(|(i, k)| (k, i))
        .collect();
    v.sort_by(|a, b| b.cmp(a));
    v.into_iter().take(n).map(|(_, i)| i).collect()
}

/// Whether node `i` is a constant or `& | ^ ~`.
fn bitwise_or_const(cx: &Context, i: u32) -> bool {
    matches!(
        cx.node(i).op,
        OpCode::Const | OpCode::And | OpCode::Or | OpCode::Xor | OpCode::Not
    )
}

/// The nodes of the semi-linear fragment below `root` that a bitwise operator of it reads
/// (directly, or through other bitwise operators); the fragment ends at the nodes of `opaque`.
///
/// A node's number in the table says how it was met: 1 as it is, 2 under a bitwise operator
/// ([`is_under`]), 3 both ways.
fn under_bitwise(
    cx: &Context,
    root: u32,
    lim: &MbaLimits,
    opaque: &IdMap<u32, ()>,
) -> Result<Lease, Refusal> {
    let mut seen = Lease::new(cx.len());
    let mut met = 0u32;
    let mut stack: Vec<(u32, bool)> = vec![(root, false)];
    while let Some((i, bw)) = stack.pop() {
        let (was, way) = (seen.get(i).unwrap_or(0), 1 << u32::from(bw));
        if was & way != 0 {
            continue;
        }
        seen.insert(i, was | way);
        met += 1;
        if met > lim.max_nodes.saturating_mul(2) {
            return Err(Refusal::TooLarge);
        }
        let bitwise = bitwise_or_const(cx, i);
        // Arithmetic read by a bitwise operator is an atom: nothing below it is read here.
        if (bw && !bitwise)
            || (i != root && opaque.contains_key(&i))
            || !in_reading(cx, i, Reading::SemiLinear)
        {
            continue;
        }
        for c in cx.node(i).children() {
            stack.push((c, bitwise));
        }
    }
    Ok(seen)
}

/// Whether [`under_bitwise`] met `i` under a bitwise operator.
fn is_under(under: &Lease, i: u32) -> bool {
    under.get(i).is_some_and(|way| way & 2 != 0)
}

/// The expression over `atoms` that the fragment's nodes `order` (operands first) compute,
/// each operand in `solved` written over its atom.
fn build(
    cx: &Context,
    atoms: Vec<u32>,
    order: Vec<u32>,
    solved: &[Solved],
) -> Result<(MbaExpr, Bindings), Refusal> {
    Dense::with(&AT, cx.len(), |at| build_in(cx, atoms, order, solved, at))
}

fn build_in(
    cx: &Context,
    atoms: Vec<u32>,
    order: Vec<u32>,
    solved: &[Solved],
    at: &mut Dense,
) -> Result<(MbaExpr, Bindings), Refusal> {
    // An operand of a fragment's node is one of its atoms or an earlier node of it.
    let unmet = || Refusal::Unsupported("an operand outside the fragment".into());
    // (Room for a node per atom and per node of the fragment, two per operand written over an
    // atom, and the few a slice or a shift adds.)
    let room = atoms.len() + 2 * solved.len() + order.len() + 8;
    let mut m = MbaExpr::with_capacity(atoms.iter().map(|&a| cx.width_of(a)).collect(), room);
    for (&a, k) in atoms.iter().zip(0u32..) {
        let v = m
            .push(MOp::Var(k), &[])
            .map_err(|e| Refusal::Unsupported(e.to_string()))?;
        at.insert(a, v);
    }
    for s in solved {
        let bad = |e: super::expr::MbaError| Refusal::Unsupported(e.to_string());
        let a = at.get(s.atom).ok_or_else(unmet)?;
        let k = m.push(MOp::Const(s.constant), &[]).map_err(bad)?;
        let args = if s.negated { [k, a] } else { [a, k] };
        let v = m.push(MOp::Sub, &args).map_err(bad)?;
        at.insert(s.operand, v);
    }
    for i in order {
        let n = cx.node(i);
        let arg = |k: u32| at.get(k).unwrap_or(u32::MAX);
        let bad = |e: super::expr::MbaError| Refusal::Unsupported(e.to_string());
        let v = match n.op {
            OpCode::Const => m.push(
                MOp::Const(cx.const_val(i).unwrap_or(BitVec::zero(Width::W1))),
                &[],
            ),
            OpCode::Add => m.push(MOp::Add, &[arg(n.a), arg(n.b)]),
            OpCode::Sub => m.push(MOp::Sub, &[arg(n.a), arg(n.b)]),
            OpCode::Mul => m.push(MOp::Mul, &[arg(n.a), arg(n.b)]),
            OpCode::And => m.push(MOp::And, &[arg(n.a), arg(n.b)]),
            OpCode::Or => m.push(MOp::Or, &[arg(n.a), arg(n.b)]),
            OpCode::Xor => m.push(MOp::Xor, &[arg(n.a), arg(n.b)]),
            OpCode::Neg => m.push(MOp::Neg, &[arg(n.a)]),
            OpCode::Not => m.push(MOp::Not, &[arg(n.a)]),
            OpCode::Shl | OpCode::LShr => {
                let k = cx.const_val(n.b).and_then(|k| k.to_u64()).unwrap_or(0) as u16;
                let op = if n.op == OpCode::Shl {
                    MOp::Shl(k)
                } else {
                    MOp::LShr(k)
                };
                m.push(op, &[arg(n.a)])
            }
            OpCode::Zext => m.push_cast(MOp::Zext, arg(n.a), cx.width_of(i)),
            OpCode::Sext => m.push_cast(MOp::Sext, arg(n.a), cx.width_of(i)),
            OpCode::Extract if n.b == 0 => m.push_cast(MOp::Trunc, arg(n.a), cx.width_of(i)),
            OpCode::Extract => (|| {
                let s = m.push(MOp::LShr(n.b as u16), &[arg(n.a)])?;
                if cx.width_of(i) == cx.width_of(n.a) {
                    return Ok(s);
                }
                m.push_cast(MOp::Trunc, s, cx.width_of(i))
            })(),
            // `concat(hi, lo)` is `(hi << |lo|) + lo`, both extended (their bits are disjoint);
            // a high part that is the low bits of a value of the full width is that value, the
            // rest shifting out (`concat(trunc<62>(y), 3)` is `(y << 2) + 3`).
            OpCode::Concat => (|| {
                let w = cx.width_of(i);
                let lw = cx.width_of(n.b);
                let hn = cx.node(n.a);
                let hi = if hn.op == OpCode::Extract && hn.b == 0 && cx.width_of(hn.a) == w {
                    arg(hn.a)
                } else {
                    m.push_cast(MOp::Zext, arg(n.a), w)?
                };
                let hi = m.push(MOp::Shl(lw.bits()), &[hi])?;
                let lo = match cx.const_val(n.b) {
                    Some(c) => m.push(MOp::Const(c.zext(w).unwrap_or(BitVec::zero(w))), &[])?,
                    None => m.push_cast(MOp::Zext, arg(n.b), w)?,
                };
                m.push(MOp::Add, &[hi, lo])
            })(),
            _ => return Err(Refusal::Unsupported("not an MBA operator".into())),
        }
        .map_err(bad)?;
        at.insert(i, v);
    }
    Ok((m, Bindings { atoms }))
}

/// Lowers the fragment rooted at `e` into an [`MbaExpr`]: `+ − * neg & | ^ ~`, shifts by
/// constants below the width, extensions and truncation; every other subterm becomes a variable,
/// bound in the returned [`Bindings`]. Fails with [`Error::Unsupported`] (the refusal) when `e` is
/// not in the fragment or is over a limit.
pub fn lower(cx: &Context, e: Expr, lim: &MbaLimits) -> Result<(MbaExpr, Bindings), Error> {
    let i = cx.id(e)?;
    lower_id(cx, i, lim).map_err(|r| Error::Unsupported(format!("{r:?}")))
}

/// Builds `m` in the arena through the canonicalizing constructors, with each variable bound to
/// its atom.
pub(crate) fn lift_id(cx: &mut Context, m: &MbaExpr, b: &Bindings) -> Result<u32, Error> {
    let mut v: Vec<u32> = Vec::with_capacity(m.nodes().len());
    for n in m.nodes() {
        let a = |k: usize| v[n.args[k] as usize];
        let r = match n.op {
            MOp::Const(c) => cx.mk_const(&c)?,
            MOp::Var(i) => *b
                .atoms
                .get(i as usize)
                .ok_or(Error::Unsupported("unbound MBA variable".into()))?,
            MOp::Add => cx.c_bin(BinOp::Add, a(0), a(1))?,
            MOp::Sub => cx.c_bin(BinOp::Sub, a(0), a(1))?,
            MOp::Mul => cx.c_bin(BinOp::Mul, a(0), a(1))?,
            MOp::And => cx.c_bin(BinOp::And, a(0), a(1))?,
            MOp::Or => cx.c_bin(BinOp::Or, a(0), a(1))?,
            MOp::Xor => cx.c_bin(BinOp::Xor, a(0), a(1))?,
            MOp::Neg => cx.c_un(UnOp::Neg, a(0))?,
            MOp::Not => cx.c_un(UnOp::Not, a(0))?,
            MOp::Shl(k) | MOp::LShr(k) => {
                let kc = cx.mk_const(&BitVec::wrapping_from_u64(n.width, u64::from(k)))?;
                let op = if matches!(n.op, MOp::Shl(_)) {
                    BinOp::Shl
                } else {
                    BinOp::LShr
                };
                cx.c_bin(op, a(0), kc)?
            }
            MOp::Zext => cx.c_zext(a(0), n.width.bits())?,
            MOp::Sext => cx.c_sext(a(0), n.width.bits())?,
            // a truncated right shift is the slice it was lowered from.
            MOp::Trunc => match m.nodes()[n.args[0] as usize] {
                MNode {
                    op: MOp::LShr(k),
                    width,
                    args,
                } if k + n.width.bits() <= width.bits() => {
                    cx.c_extract(v[args[0] as usize], k, n.width.bits())?
                }
                _ => cx.c_extract(a(0), 0, n.width.bits())?,
            },
        };
        v.push(r);
    }
    v.last()
        .copied()
        .ok_or(Error::Unsupported("empty MBA expression".into()))
}

/// Builds `m` in the arena through the canonicalizing constructors, each variable bound to the
/// subterm it stands for.
pub fn lift(cx: &mut Context, m: &MbaExpr, b: &Bindings) -> Result<Expr, Error> {
    let i = lift_id(cx, m, b)?;
    Ok(cx.handle(i))
}