bitwright 0.31.6

Hash-consed fixed-width bit-vector expressions: exact evaluation, bit-level facts, and verified simplification.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
//! `MbaExpr`: a small, self-contained DAG in the MBA fragment, the unit a solver sees.

use core::hash::{Hash, Hasher};

use crate::ops::{BinOp, UnOp};
use crate::{BitVec, Width};

/// An operator of the MBA fragment.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum MOp {
    /// A constant.
    Const(BitVec),
    /// Variable `i` (an atom of the original expression).
    Var(u32),
    /// `a + b`.
    Add,
    /// `a - b`.
    Sub,
    /// `a * b`.
    Mul,
    /// `-a`.
    Neg,
    /// `a & b`.
    And,
    /// `a | b`.
    Or,
    /// `a ^ b`.
    Xor,
    /// `~a`.
    Not,
    /// `a << k` for a constant `k` below the width.
    Shl(u16),
    /// `a >>u k` for a constant `k` below the width.
    LShr(u16),
    /// Zero extension to the node's width.
    Zext,
    /// Sign extension to the node's width.
    Sext,
    /// The low bits of `a`, at the node's width.
    Trunc,
}

impl MOp {
    /// The number of operands.
    pub fn arity(&self) -> usize {
        match self {
            MOp::Const(_) | MOp::Var(_) => 0,
            MOp::Neg
            | MOp::Not
            | MOp::Shl(_)
            | MOp::LShr(_)
            | MOp::Zext
            | MOp::Sext
            | MOp::Trunc => 1,
            _ => 2,
        }
    }

    fn arith(&self) -> bool {
        matches!(self, MOp::Add | MOp::Sub | MOp::Mul | MOp::Neg)
    }

    fn bitwise(&self) -> bool {
        matches!(self, MOp::And | MOp::Or | MOp::Xor)
    }

    /// The operator's number (its place among the operators), and a variable's index or a
    /// shift's amount.
    fn tag(&self) -> (u8, u32) {
        match *self {
            MOp::Const(_) => (0, 0),
            MOp::Var(i) => (1, i),
            MOp::Add => (2, 0),
            MOp::Sub => (3, 0),
            MOp::Mul => (4, 0),
            MOp::Neg => (5, 0),
            MOp::And => (6, 0),
            MOp::Or => (7, 0),
            MOp::Xor => (8, 0),
            MOp::Not => (9, 0),
            MOp::Shl(k) => (10, u32::from(k)),
            MOp::LShr(k) => (11, u32::from(k)),
            MOp::Zext => (12, 0),
            MOp::Sext => (13, 0),
            MOp::Trunc => (14, 0),
        }
    }

    /// Hashes the operator with `more` (under 2^24) in one word; a constant's width and limbs
    /// after it.
    fn hash_with<H: Hasher>(&self, more: u64, state: &mut H) {
        let (tag, extra) = self.tag();
        state.write_u64(u64::from(tag) | more << 8 | u64::from(extra) << 32);
        if let MOp::Const(v) = self {
            state.write_u16(v.width().bits());
            for &l in v.limbs() {
                state.write_u64(l);
            }
        }
    }
}

/// Hashed as one word (and a constant's limbs): operators and nodes key the maps that intern
/// them in every pass over an expression. This is not the content hash ([`MbaExpr::key`]).
impl Hash for MOp {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.hash_with(0, state);
    }
}

/// A node: operator, result width, operand indices (earlier nodes).
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct MNode {
    /// The operator.
    pub op: MOp,
    /// The result width.
    pub width: Width,
    /// Operand node indices (unused ones are 0).
    pub args: [u32; 2],
}

/// Hashed as two words (and a constant's limbs), see [`MOp`]'s.
impl Hash for MNode {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.op.hash_with(u64::from(self.width.bits()), state);
        state.write_u64(u64::from(self.args[0]) | u64::from(self.args[1]) << 32);
    }
}

/// Why a node could not be added.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum MbaError {
    /// An operand index is not an earlier node.
    BadOperand,
    /// Operand widths do not fit the operator.
    Width,
    /// A variable index is not declared.
    UnknownVar,
}

impl core::fmt::Display for MbaError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str(match self {
            MbaError::BadOperand => "an operand is not an earlier node",
            MbaError::Width => "operand widths do not fit the operator",
            MbaError::UnknownVar => "unknown variable",
        })
    }
}

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

/// A DAG in the MBA fragment over declared variables. The last node is the root. Nodes refer
/// only to earlier nodes, so evaluation and every traversal are single forward passes.
#[derive(Clone, Debug)]
pub struct MbaExpr {
    vars: Vec<Width>,
    nodes: Vec<MNode>,
    /// [`MbaExpr::key`], computed once (adding a node clears it). Not part of
    /// the expression: equality and hashing read the variables and nodes alone.
    key: std::sync::OnceLock<[u64; 2]>,
}

impl PartialEq for MbaExpr {
    fn eq(&self, other: &Self) -> bool {
        self.vars == other.vars && self.nodes == other.nodes
    }
}

impl Eq for MbaExpr {}

impl Hash for MbaExpr {
    // The variables, then the nodes.
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.vars.hash(state);
        self.nodes.hash(state);
    }
}

/// The limbs of a value in the key's stream: every limb of the widest value.
const KEY_LIMBS: usize = Width::W512.bits() as usize / 64;

/// The two running words of [`MbaExpr::key`], each byte of the stream folded into both.
struct KeyHasher(u64, u64);

impl KeyHasher {
    fn new() -> KeyHasher {
        KeyHasher(0x6d62_615f_6578_7072, 0x6269_7477_7269_6768)
    }

    #[inline]
    fn bytes(&mut self, bytes: &[u8]) {
        for &b in bytes {
            self.0 = crate::hash::combine(self.0, u64::from(b));
            self.1 = crate::hash::combine(self.1 ^ 0x5555, u64::from(b).rotate_left(29));
        }
    }

    /// A length or an operator's number: a `usize` in the stream, as the derived hashes
    /// wrote it.
    #[inline]
    fn word(&mut self, x: u64) {
        self.bytes(&(x as usize).to_ne_bytes());
    }
}

/// Structural properties used to decide whether a solver is worth asking.
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Shape {
    /// Variables used.
    pub vars: u32,
    /// Nodes.
    pub nodes: u32,
    /// Longest operand chain.
    pub height: u32,
    /// Both arithmetic and bitwise operators occur.
    pub mixed: bool,
    /// The largest number of variables multiplied together in one product (1 for linear).
    pub degree: u32,
}

impl MbaExpr {
    /// An expression over variables of the given widths, with no nodes yet.
    pub fn new(vars: Vec<Width>) -> MbaExpr {
        MbaExpr {
            vars,
            nodes: Vec::new(),
            key: std::sync::OnceLock::new(),
        }
    }

    /// [`MbaExpr::new`] with room for `nodes` nodes.
    pub(crate) fn with_capacity(vars: Vec<Width>, nodes: usize) -> MbaExpr {
        MbaExpr {
            vars,
            nodes: Vec::with_capacity(nodes),
            key: std::sync::OnceLock::new(),
        }
    }

    /// The variables' widths.
    pub fn vars(&self) -> &[Width] {
        &self.vars
    }

    /// The nodes, operands before users; the last is the root.
    pub fn nodes(&self) -> &[MNode] {
        &self.nodes
    }

    /// The root's index, if there are nodes.
    pub fn root(&self) -> Option<u32> {
        self.nodes.len().checked_sub(1).map(|r| r as u32)
    }

    /// The root's width.
    pub fn width(&self) -> Option<Width> {
        self.nodes.last().map(|n| n.width)
    }

    /// Adds a node whose width follows from its operands (every operator but the casts).
    pub fn push(&mut self, op: MOp, args: &[u32]) -> Result<u32, MbaError> {
        let w = match op {
            MOp::Const(v) => v.width(),
            MOp::Var(i) => *self.vars.get(i as usize).ok_or(MbaError::UnknownVar)?,
            MOp::Zext | MOp::Sext | MOp::Trunc => return Err(MbaError::Width),
            _ => {
                let a = self.arg(args, 0)?;
                if op.arity() == 2 && self.arg(args, 1)? != a {
                    return Err(MbaError::Width);
                }
                if let MOp::Shl(k) | MOp::LShr(k) = op
                    && k >= a.bits()
                {
                    return Err(MbaError::Width);
                }
                a
            }
        };
        self.add(op, w, args)
    }

    /// Adds a cast (`Zext`, `Sext`, `Trunc`) to `width`.
    pub fn push_cast(&mut self, op: MOp, a: u32, width: Width) -> Result<u32, MbaError> {
        let wa = self.arg(&[a], 0)?;
        let ok = match op {
            MOp::Zext | MOp::Sext => width > wa,
            MOp::Trunc => width < wa,
            _ => false,
        };
        if !ok {
            return Err(MbaError::Width);
        }
        self.add(op, width, &[a])
    }

    fn arg(&self, args: &[u32], k: usize) -> Result<Width, MbaError> {
        let i = *args.get(k).ok_or(MbaError::BadOperand)?;
        self.nodes
            .get(i as usize)
            .map(|n| n.width)
            .ok_or(MbaError::BadOperand)
    }

    fn add(&mut self, op: MOp, width: Width, args: &[u32]) -> Result<u32, MbaError> {
        let mut a = [0u32; 2];
        for (k, &x) in args.iter().take(op.arity()).enumerate() {
            if x as usize >= self.nodes.len() {
                return Err(MbaError::BadOperand);
            }
            a[k] = x;
        }
        self.nodes.push(MNode { op, width, args: a });
        self.key = std::sync::OnceLock::new();
        Ok(self.nodes.len() as u32 - 1)
    }

    /// The root's value for the given variable values (`None` on a width mismatch).
    pub fn eval(&self, vals: &[BitVec]) -> Option<BitVec> {
        let mut v: Vec<BitVec> = Vec::with_capacity(self.nodes.len());
        for n in &self.nodes {
            let a = |k: usize| v[n.args[k] as usize];
            let r = match n.op {
                MOp::Const(c) => c,
                MOp::Var(i) => {
                    let x = *vals.get(i as usize)?;
                    if x.width() != n.width {
                        return None;
                    }
                    x
                }
                MOp::Add => BitVec::bin_unchecked(BinOp::Add, &a(0), &a(1)),
                MOp::Sub => BitVec::bin_unchecked(BinOp::Sub, &a(0), &a(1)),
                MOp::Mul => BitVec::bin_unchecked(BinOp::Mul, &a(0), &a(1)),
                MOp::And => BitVec::bin_unchecked(BinOp::And, &a(0), &a(1)),
                MOp::Or => BitVec::bin_unchecked(BinOp::Or, &a(0), &a(1)),
                MOp::Xor => BitVec::bin_unchecked(BinOp::Xor, &a(0), &a(1)),
                MOp::Neg => BitVec::un_unchecked(UnOp::Neg, &a(0)),
                MOp::Not => BitVec::un_unchecked(UnOp::Not, &a(0)),
                MOp::Shl(k) | MOp::LShr(k) => {
                    let kv = BitVec::wrapping_from_u64(n.width, u64::from(k));
                    let op = if matches!(n.op, MOp::Shl(_)) {
                        BinOp::Shl
                    } else {
                        BinOp::LShr
                    };
                    BitVec::bin_unchecked(op, &a(0), &kv)
                }
                MOp::Zext => a(0).zext(n.width).ok()?,
                MOp::Sext => a(0).sext(n.width).ok()?,
                MOp::Trunc => a(0).trunc(n.width).ok()?,
            };
            v.push(r);
        }
        v.last().copied()
    }

    /// A 128-bit content hash (variables, nodes), stable across processes.
    pub fn key(&self) -> [u64; 2] {
        // hashed once per expression; the engine and its solver ask it of one
        // question several times.
        *self.key.get_or_init(|| self.key_uncached())
    }

    fn key_uncached(&self) -> [u64; 2] {
        // The key is a hash of a fixed stream of bytes: the one `#[derive(Hash)]` gave for the
        // variables and then the nodes when the key was defined (lengths and operator numbers
        // as `usize`, a constant's every limb), written out here so that the key does not
        // follow how nodes hash for maps. Keys seed samples and name cache entries: the
        // stream must not change.
        let mut h = KeyHasher::new();
        h.word(self.vars.len() as u64);
        for w in &self.vars {
            h.bytes(&w.bits().to_ne_bytes());
        }
        h.word(self.nodes.len() as u64);
        for n in &self.nodes {
            let (tag, extra) = n.op.tag();
            h.word(u64::from(tag));
            match n.op {
                MOp::Const(v) => {
                    h.bytes(&v.width().bits().to_ne_bytes());
                    h.word(KEY_LIMBS as u64);
                    let limbs = v.limbs();
                    for i in 0..KEY_LIMBS {
                        h.bytes(&limbs.get(i).copied().unwrap_or(0).to_ne_bytes());
                    }
                }
                MOp::Var(_) => h.bytes(&extra.to_ne_bytes()),
                MOp::Shl(_) | MOp::LShr(_) => h.bytes(&(extra as u16).to_ne_bytes()),
                _ => {}
            }
            h.bytes(&n.width.bits().to_ne_bytes());
            h.word(n.args.len() as u64);
            h.bytes(&n.args[0].to_ne_bytes());
            h.bytes(&n.args[1].to_ne_bytes());
        }
        [h.0, h.1]
    }

    /// Structural properties.
    pub fn shape(&self) -> Shape {
        let mut height = vec![0u32; self.nodes.len()];
        // Per node: the most variables multiplied together below it.
        let mut degree = vec![0u32; self.nodes.len()];
        let mut vars_used = vec![false; self.vars.len()];
        let (mut arith, mut bit) = (false, false);
        for (i, n) in self.nodes.iter().enumerate() {
            let kids = &n.args[..n.op.arity()];
            height[i] = kids
                .iter()
                .map(|&k| height[k as usize] + 1)
                .max()
                .unwrap_or(0);
            arith |= n.op.arith();
            bit |= n.op.bitwise();
            degree[i] = match n.op {
                MOp::Const(_) => 0,
                MOp::Var(v) => {
                    if let Some(u) = vars_used.get_mut(v as usize) {
                        *u = true;
                    }
                    1
                }
                MOp::Mul => degree[n.args[0] as usize] + degree[n.args[1] as usize],
                _ => kids.iter().map(|&k| degree[k as usize]).max().unwrap_or(0),
            };
        }
        Shape {
            vars: vars_used.iter().filter(|&&u| u).count() as u32,
            nodes: self.nodes.len() as u32,
            height: height.last().copied().unwrap_or(0),
            mixed: arith && bit,
            degree: degree.last().copied().unwrap_or(0),
        }
    }

    /// Whether this is a linear MBA: arithmetic over bitwise functions of the variables, with
    /// no multiplication of two non-constants, no shift or cast, and only 0 and all-ones
    /// constants inside bitwise parts. Such an expression is determined by its values at the
    /// corners where every variable is 0 or all-ones.
    pub fn is_linear(&self) -> bool {
        // Per node: 0 = constant, 1 = uniform (bitwise over variables, or 0/all-ones), 2 = linear.
        let mut class = vec![0u8; self.nodes.len()];
        let mut uniform_const = vec![false; self.nodes.len()];
        for (i, n) in self.nodes.iter().enumerate() {
            let c = |k: usize| class[n.args[k] as usize];
            let u = |k: usize| uniform_const[n.args[k] as usize];
            class[i] = match n.op {
                MOp::Const(v) => {
                    uniform_const[i] = v.is_zero() || v.is_ones();
                    0
                }
                MOp::Var(_) => 1,
                MOp::And | MOp::Or | MOp::Xor => {
                    // Bitwise of uniform parts (constants must be 0 or all-ones).
                    let ok = (0..2).all(|k| c(k) == 1 || (c(k) == 0 && u(k)));
                    if !ok {
                        return false;
                    }
                    1
                }
                MOp::Not => {
                    // `~c` of a constant is a constant (uniform only when `c` is); `~` of a
                    // bitwise function is one; `~` of a linear term is linear (`−t − 1`).
                    if c(0) == 0 {
                        uniform_const[i] = u(0);
                    }
                    c(0)
                }
                MOp::Add | MOp::Sub => {
                    if c(0) == 0 && c(1) == 0 {
                        0
                    } else {
                        2
                    }
                }
                MOp::Neg => c(0).max(if c(0) == 0 { 0 } else { 2 }),
                MOp::Mul => {
                    if c(0) != 0 && c(1) != 0 {
                        return false;
                    }
                    if c(0) == 0 && c(1) == 0 { 0 } else { 2 }
                }
                _ => return false,
            };
        }
        true
    }

    /// The values at the 2^t corners (variable `j` all-ones when bit `j` of the corner is set),
    /// or `None` over 16 variables or on a width mismatch between variables.
    pub(crate) fn corners(&self) -> Option<Vec<BitVec>> {
        let t = self.vars.len();
        if t > 16 {
            return None;
        }
        (0..1usize << t)
            .map(|p| {
                let vals: Vec<BitVec> = self
                    .vars
                    .iter()
                    .enumerate()
                    .map(|(j, &w)| {
                        if p >> j & 1 == 1 {
                            BitVec::ones(w)
                        } else {
                            BitVec::zero(w)
                        }
                    })
                    .collect();
                self.eval(&vals)
            })
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The operators and a node as they were when the key was defined, with the derived
    /// hashes whose stream the key is.
    #[allow(dead_code)]
    #[derive(Hash)]
    enum KeyOp {
        Const(KeyValue),
        Var(u32),
        Add,
        Sub,
        Mul,
        Neg,
        And,
        Or,
        Xor,
        Not,
        Shl(u16),
        LShr(u16),
        Zext,
        Sext,
        Trunc,
    }

    /// A value as it was hashed then: its width and every limb of the widest (a `BitVec`
    /// hashes its active limbs now).
    #[derive(Hash)]
    struct KeyValue {
        width: Width,
        limbs: [u64; KEY_LIMBS],
    }

    #[derive(Hash)]
    struct KeyNode {
        op: KeyOp,
        width: Width,
        args: [u32; 2],
    }

    impl Hasher for KeyHasher {
        fn finish(&self) -> u64 {
            self.0
        }
        fn write(&mut self, bytes: &[u8]) {
            self.bytes(bytes);
        }
    }

    fn derived_key(m: &MbaExpr) -> [u64; 2] {
        let nodes: Vec<KeyNode> = m
            .nodes()
            .iter()
            .map(|n| KeyNode {
                op: match n.op {
                    MOp::Const(v) => {
                        let mut limbs = [0u64; KEY_LIMBS];
                        limbs[..v.limbs().len()].copy_from_slice(v.limbs());
                        KeyOp::Const(KeyValue {
                            width: v.width(),
                            limbs,
                        })
                    }
                    MOp::Var(i) => KeyOp::Var(i),
                    MOp::Add => KeyOp::Add,
                    MOp::Sub => KeyOp::Sub,
                    MOp::Mul => KeyOp::Mul,
                    MOp::Neg => KeyOp::Neg,
                    MOp::And => KeyOp::And,
                    MOp::Or => KeyOp::Or,
                    MOp::Xor => KeyOp::Xor,
                    MOp::Not => KeyOp::Not,
                    MOp::Shl(k) => KeyOp::Shl(k),
                    MOp::LShr(k) => KeyOp::LShr(k),
                    MOp::Zext => KeyOp::Zext,
                    MOp::Sext => KeyOp::Sext,
                    MOp::Trunc => KeyOp::Trunc,
                },
                width: n.width,
                args: n.args,
            })
            .collect();
        let mut h = KeyHasher::new();
        m.vars().to_vec().hash(&mut h);
        nodes.hash(&mut h);
        [h.0, h.1]
    }

    #[test]
    fn the_key_is_the_derived_hash_of_variables_and_nodes() {
        for bits in [1u16, 7, 8, 32, 64, 65, 128, 200, 512] {
            let w = Width::new(bits).unwrap();
            let narrow = Width::new(bits.div_ceil(2)).unwrap();
            let mut m = MbaExpr::new(vec![w, narrow, w]);
            assert_eq!(m.key(), derived_key(&m));
            let x = m.push(MOp::Var(0), &[]).unwrap();
            let z = m.push(MOp::Var(2), &[]).unwrap();
            let k = m.push(MOp::Const(BitVec::ones(w)), &[]).unwrap();
            let one = m.push(MOp::Const(BitVec::one(w)), &[]).unwrap();
            let mut last = x;
            for op in [MOp::Add, MOp::Sub, MOp::Mul, MOp::And, MOp::Or, MOp::Xor] {
                last = m.push(op, &[last, z]).unwrap();
                last = m.push(op, &[k, last]).unwrap();
                assert_eq!(m.key(), derived_key(&m));
            }
            for op in [MOp::Neg, MOp::Not, MOp::Shl(0), MOp::LShr(bits - 1)] {
                last = m.push(op, &[last]).unwrap();
            }
            last = m.push(MOp::Add, &[last, one]).unwrap();
            if narrow != w {
                let y = m.push(MOp::Var(1), &[]).unwrap();
                let t = m.push_cast(MOp::Trunc, last, narrow).unwrap();
                let u = m.push(MOp::Xor, &[t, y]).unwrap();
                let s = m.push_cast(MOp::Sext, u, w).unwrap();
                let e = m.push_cast(MOp::Zext, y, w).unwrap();
                m.push(MOp::Or, &[s, e]).unwrap();
            }
            assert_eq!(m.key(), derived_key(&m), "at {bits} bits");
        }
        // Equal nodes hash alike for maps, whatever the map's hasher.
        let w = Width::W64;
        let mut m = MbaExpr::new(vec![w]);
        let x = m.push(MOp::Var(0), &[]).unwrap();
        let a = m.push(MOp::Shl(3), &[x]).unwrap();
        let b = m.push(MOp::Shl(3), &[x]).unwrap();
        let hash = |n: &MNode| {
            let mut h = KeyHasher::new();
            n.hash(&mut h);
            [h.0, h.1]
        };
        assert_eq!(hash(&m.nodes()[a as usize]), hash(&m.nodes()[b as usize]));
        assert_ne!(hash(&m.nodes()[a as usize]), hash(&m.nodes()[x as usize]));
    }
}