bitwright 0.29.0

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
//! The passes' reasoning for their rewrites, for [`Event::Explain`](super::Event::Explain).
//!
//! What a pass records follows how it works, which changes from release to release: the
//! variants and their fields are not covered by the crate's compatibility promises and may
//! change in any release.

use crate::BitVec;
use crate::expr::Expr;

/// A pass's reasoning for one rewrite.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Explanation {
    /// Fact folding: the node's known bits pin its value.
    Fold {
        /// The bits known to be 0.
        known_zero: BitVec,
        /// The bits known to be 1.
        known_one: BitVec,
        /// The value they pin.
        value: BitVec,
    },
    /// The linear pass read the node as `konst + Σ coeff·atom` over Z/2^W and wrote that form
    /// back.
    Linear {
        /// The constant.
        konst: BitVec,
        /// Each atom with its coefficient.
        terms: Vec<(Expr, BitVec)>,
    },
    /// The xor pass read the node as `konst ⊕ ⊕ (atom & mask)` and wrote that form back.
    Xor {
        /// The constant.
        konst: BitVec,
        /// Each atom with its mask.
        terms: Vec<(Expr, BitVec)>,
    },
    /// The bitwise pass read the node as a function of a few atoms and wrote the minimum form
    /// of its truth table.
    Bitwise {
        /// The atoms.
        atoms: Vec<Expr>,
        /// The truth table: bit `i` is the function's value where atom `k` is bit `k` of `i`
        /// (all ones or all zeros, for every bit position alike).
        table: u64,
    },
    /// The compares pass read the node as a set of the five relations two values can stand
    /// in, and wrote it as one comparison (or fewer).
    Relations {
        /// The first value.
        x: Expr,
        /// The second value.
        y: Expr,
        /// The relations, a bit each: 0 `x == y`; 1 `x <u y` and `x <s y`; 2 `x <u y` and
        /// `x >s y`; 3 `x >u y` and `x <s y`; 4 `x >u y` and `x >s y`.
        set: u8,
    },
    /// The compares pass read the node as a set of values of one operand and wrote it as one
    /// comparison or range check.
    Intervals {
        /// The operand.
        x: Expr,
        /// The values, as disjoint unsigned intervals `[lo, hi]`, ascending.
        set: Vec<(BitVec, BitVec)>,
    },
    /// The linear MBA pass read the node as a linear combination of bitwise functions of its
    /// atoms, determined by its values at the 2^t corners where each atom is all zeros or all
    /// ones.
    LinearMba {
        /// The atoms.
        atoms: Vec<Expr>,
        /// The value at each corner: entry `p` has atom `k` all ones where bit `k` of `p` is
        /// set.
        corners: Vec<BitVec>,
        /// The form it wrote: `affine`, `indicators` or `conjunctions`.
        form: &'static str,
    },
    /// The shuffle pass traced each bit of the node to the bit it comes from.
    Shuffle {
        /// Each bit's source, lowest bit first.
        bits: Vec<Source>,
    },
    /// The casts pass pushed a narrowing (`extract`, `trunc`) into the value it reads, or
    /// recognized a sign extension written with shifts.
    Casts {
        /// The value read.
        source: Expr,
        /// Its lowest bit read.
        lo: u16,
        /// How many bits are read.
        bits: u16,
        /// Whether the bits read are sign-extended back (`sext(trunc(x))` written as shifts).
        sign_extended: bool,
    },
    /// The demanded-bits pass simplified an operand under the bits its user observes.
    Demanded {
        /// The operand.
        operand: Expr,
        /// The bits of it observed.
        mask: BitVec,
    },
    /// The invert pass undid injective layers of an equality's side against a constant: from
    /// the side down to the value compared, each layer with the preimage of the constant under
    /// it.
    Invert {
        /// The layers, outermost first.
        layers: Vec<Peel>,
    },
    /// The division pass recognized a multiplication by a magic number as a division by a
    /// constant, proved exact for every value the dividend can take.
    Division {
        /// The dividend.
        dividend: Expr,
        /// The divisor.
        divisor: BitVec,
        /// Whether the division is signed.
        signed: bool,
    },
    /// The carries pass read a carry or borrow network as the affine form it computes.
    Carries {
        /// The constant.
        konst: BitVec,
        /// Each atom with its coefficient.
        terms: Vec<(Expr, BitVec)>,
        /// Whether the native prover proved the form equal (a chain of shifts of one atom is
        /// equal by construction, unproved).
        proved: bool,
    },
    /// The MBA service: the fragment was lowered over its atoms, simplified by the solver and
    /// lifted back, on this evidence.
    Mba {
        /// The atoms the fragment is a function of (the solver's variables, in order).
        atoms: Vec<Expr>,
        /// What vouched for the answer: `certificate` (bitwright's own), `prover`, `backend`
        /// (the solver's own claim), `sampled`, or `cache` (an answer found before).
        evidence: &'static str,
        /// Which test of bitwright's certificate decided, when it did (`signature`,
        /// `single_bit`, `sparse`, `grid`, `exhaustive`, `symbolic`, `carries`).
        test: Option<&'static str>,
        /// The points the certificate evaluated.
        points: u64,
    },
}

/// Where one bit of a value comes from.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Source {
    /// A constant 0.
    Zero,
    /// A constant 1.
    One,
    /// Bit `bit` of `of`.
    Bit {
        /// The value.
        of: Expr,
        /// The bit.
        bit: u16,
    },
}

/// One layer the invert pass undid.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Peel {
    /// The layer's top node.
    pub node: Expr,
    /// Its operand: the value the layer is a function of, the others fixed.
    pub inner: Expr,
    /// How it maps its operand (`v ^ k`, `v * k`, `rotl(v, k)`, `zext(v)`, `region`, …).
    pub map: &'static str,
    /// Whether it is onto (same width in and out), not only injective.
    pub bijective: bool,
    /// The value the operand must take for the layer to give the constant; `None` when no
    /// value does (then the equality is false).
    pub preimage: Option<BitVec>,
}

/// Calls `f` with every expression an explanation names.
pub(super) fn exprs(why: &Explanation, f: &mut dyn FnMut(Expr)) {
    match why {
        Explanation::Linear { terms, .. }
        | Explanation::Xor { terms, .. }
        | Explanation::Carries { terms, .. } => terms.iter().for_each(|(e, _)| f(*e)),
        Explanation::Bitwise { atoms, .. }
        | Explanation::LinearMba { atoms, .. }
        | Explanation::Mba { atoms, .. } => atoms.iter().for_each(|&e| f(e)),
        Explanation::Relations { x, y, .. } => {
            f(*x);
            f(*y);
        }
        Explanation::Intervals { x, .. } => f(*x),
        Explanation::Shuffle { bits } => {
            for b in bits {
                if let Source::Bit { of, .. } = b {
                    f(*of);
                }
            }
        }
        Explanation::Demanded { operand, .. } => f(*operand),
        Explanation::Casts { source, .. } => f(*source),
        Explanation::Invert { layers } => {
            for l in layers {
                f(l.node);
                f(l.inner);
            }
        }
        Explanation::Division { dividend, .. } => f(*dividend),
        Explanation::Fold { .. } => {}
    }
}

/// Writes an explanation as a JSON object with its `kind`, naming expressions by `id`.
pub(super) fn write(out: &mut String, why: &Explanation, id: &dyn Fn(Expr) -> u64) {
    use super::trace::{Obj, jstr, num};
    let hex = |v: &BitVec| format!("{v:#x}");
    let pairs = |o: &mut Obj<'_>, k: &str, terms: &[(Expr, BitVec)]| {
        o.key(k);
        o.out.push('[');
        for (j, (e, v)) in terms.iter().enumerate() {
            if j > 0 {
                o.out.push(',');
            }
            o.out.push('[');
            num(o.out, id(*e));
            o.out.push(',');
            jstr(o.out, &hex(v));
            o.out.push(']');
        }
        o.out.push(']');
    };
    let ids = |o: &mut Obj<'_>, k: &str, es: &[Expr]| {
        o.key(k);
        o.out.push('[');
        for (j, e) in es.iter().enumerate() {
            if j > 0 {
                o.out.push(',');
            }
            num(o.out, id(*e));
        }
        o.out.push(']');
    };
    let mut o = Obj::new(out);
    match why {
        Explanation::Fold {
            known_zero,
            known_one,
            value,
        } => {
            o.str("kind", "fold");
            o.str("zero", &hex(known_zero));
            o.str("one", &hex(known_one));
            o.str("value", &hex(value));
        }
        Explanation::Linear { konst, terms } => {
            o.str("kind", "linear");
            o.str("konst", &hex(konst));
            pairs(&mut o, "terms", terms);
        }
        Explanation::Xor { konst, terms } => {
            o.str("kind", "xor");
            o.str("konst", &hex(konst));
            pairs(&mut o, "terms", terms);
        }
        Explanation::Bitwise { atoms, table } => {
            o.str("kind", "bitwise");
            ids(&mut o, "atoms", atoms);
            o.str("table", &format!("{table:#x}"));
        }
        Explanation::Relations { x, y, set } => {
            o.str("kind", "relations");
            o.num("x", id(*x));
            o.num("y", id(*y));
            o.num("set", u64::from(*set));
        }
        Explanation::Intervals { x, set } => {
            o.str("kind", "intervals");
            o.num("x", id(*x));
            o.key("set");
            o.out.push('[');
            for (j, (lo, hi)) in set.iter().enumerate() {
                if j > 0 {
                    o.out.push(',');
                }
                o.out.push('[');
                jstr(o.out, &hex(lo));
                o.out.push(',');
                jstr(o.out, &hex(hi));
                o.out.push(']');
            }
            o.out.push(']');
        }
        Explanation::LinearMba {
            atoms,
            corners,
            form,
        } => {
            o.str("kind", "linear_mba");
            ids(&mut o, "atoms", atoms);
            o.key("corners");
            o.out.push('[');
            for (j, v) in corners.iter().enumerate() {
                if j > 0 {
                    o.out.push(',');
                }
                jstr(o.out, &hex(v));
            }
            o.out.push(']');
            o.str("form", form);
        }
        Explanation::Shuffle { bits } => {
            o.str("kind", "shuffle");
            o.key("bits");
            o.out.push('[');
            for (j, b) in bits.iter().enumerate() {
                if j > 0 {
                    o.out.push(',');
                }
                match b {
                    Source::Zero => o.out.push('0'),
                    Source::One => o.out.push('1'),
                    Source::Bit { of, bit } => {
                        o.out.push('[');
                        num(o.out, id(*of));
                        o.out.push(',');
                        num(o.out, u64::from(*bit));
                        o.out.push(']');
                    }
                }
            }
            o.out.push(']');
        }
        Explanation::Casts {
            source,
            lo,
            bits,
            sign_extended,
        } => {
            o.str("kind", "casts");
            o.num("source", id(*source));
            o.num("lo", u64::from(*lo));
            o.num("bits", u64::from(*bits));
            o.bool("sign_extended", *sign_extended);
        }
        Explanation::Demanded { operand, mask } => {
            o.str("kind", "demanded");
            o.num("operand", id(*operand));
            o.str("mask", &hex(mask));
        }
        Explanation::Invert { layers } => {
            o.str("kind", "invert");
            o.key("layers");
            o.out.push('[');
            for (k, l) in layers.iter().enumerate() {
                if k > 0 {
                    o.out.push(',');
                }
                let mut p = Obj::new(o.out);
                p.num("node", id(l.node));
                p.num("inner", id(l.inner));
                p.str("map", l.map);
                p.bool("bijective", l.bijective);
                p.key("preimage");
                match &l.preimage {
                    Some(v) => jstr(p.out, &hex(v)),
                    None => p.out.push_str("null"),
                }
                p.end();
            }
            o.out.push(']');
        }
        Explanation::Division {
            dividend,
            divisor,
            signed,
        } => {
            o.str("kind", "division");
            o.num("dividend", id(*dividend));
            o.str("divisor", &hex(divisor));
            o.bool("signed", *signed);
        }
        Explanation::Carries {
            konst,
            terms,
            proved,
        } => {
            o.str("kind", "carries");
            o.str("konst", &hex(konst));
            pairs(&mut o, "terms", terms);
            o.bool("proved", *proved);
        }
        Explanation::Mba {
            atoms,
            evidence,
            test,
            points,
        } => {
            o.str("kind", "mba");
            ids(&mut o, "atoms", atoms);
            o.str("evidence", evidence);
            if let Some(t) = test {
                o.str("test", t);
            }
            o.num("points", *points);
        }
    }
    o.end();
}