Maps to: Stanford CS 257 Introduction to Automated Reasoning (Winter 2026), Lecture 16 · Thu 02/26/2026 · Bit-vectors (guest lecture by Mathias Preiner). Reading: Kroening and Strichman, Decision Procedures, Chapter 6 (bit-vectors). Basis: The Stanford slides are on Canvas, and the public Iowa decks have no bit-vector lecture. This lecture is written from:
- Kroening and Strichman, Chapter 6: bit-flattening, incremental and abstraction-based approaches;
- the SMT-LIB QF_BV semantics;
- Brummayer and Biere’s Boolector papers;
- Niemetz, Preiner and Biere (2017), propagation-based local search;
- Niemetz and Preiner (CAV 2023), Bitwuzla, the solver the guest lecturer co-develops;
- Bryant et al. (TACAS 2007), abstraction for bit-vector arithmetic;
- Warren, Hacker’s Delight;
- de Moura’s Lecture 15 slides, for
bv_decide.These notes add one module,
bitvec.py, with:
- terms with exact semantics;
- a bit-blaster to CNF for Lecture 5’s CDCL;
- word-level rewriting with polynomial normalization;
- lazy multiplication;
- certificates for every answer.
The experiments were designed for these notes and do not reflect what was said in class. Code:
code/lecture16/(3 scripts + 1 module; Python standard library only; about 2 to 27 seconds of CPU time each) Reading time: 2.5 hours including running the code
0. How to read this lecture
0.1 Learning goals
After this lecture you should be able to:
- State the semantics of fixed-width bit-vectors: arithmetic modulo , unsigned vs signed readings, and the SMT-LIB conventions for division by zero and large shifts.
- Find the integer facts that fail on bit-vectors, and recognize classic overflow bugs.
- Bit-blast each operator into a circuit and its CNF, and estimate the circuit sizes: linear, and quadratic.
- Explain why multiplication is the bottleneck of bit-blasting, and why a faster SAT solver does not fix it.
- Use word-level rewriting, including polynomial normalization modulo , and explain why it is sound but incomplete.
- Describe abstraction refinement (lazy multiplication) and local search as alternatives to eager bit-blasting.
- Connect bit-blasting with certificates: DRUP here, and LRAT with a verified checker in Lean’s
bv_decide.
0.2 Prerequisites and scope
| Assumed | Introduced here | Developed later |
|---|---|---|
| CDCL, DRUP (Lecture 5); Tseitin (Lecture 2) | bit-blasting, AIGs, structural hashing | — |
| integer arithmetic (Lecture 12) | arithmetic modulo | theory combination with BV (Lecture 18) |
| lazy lemmas (Lectures 10, 13) | abstraction refinement for multiplication | — |
proof kernels, bv_decide (Lecture 15) | the bit-blasting pipeline in detail | — |
0.3 Map
§1 why bit-vectors: programs compute modulo 2^w
§2 semantics, and the facts that break ← code 01
§3 bit-blasting: gates, circuits, CNF ← code 02
§4 why multiplication is hard ← code 02
§5 word-level rewriting and polynomials modulo 2^w ← code 03
§6 lazy multiplication and other alternatives ← code 03
§7 certificates: from DRUP to verified bit-blasting
§8 summary §9 common pitfalls §10 exercises §11 further reading §12 next lecture
0.4 Notation
| Symbol | Meaning |
|---|---|
| bit width; values are | |
| arithmetic modulo | |
| , | unsigned and signed (two’s complement) comparison |
| , | logical and arithmetic right shift |
| , | bit ; the slice from bit down to bit |
| AIG | and-inverter graph: a circuit of AND gates and negations |
0.5 The code of this lecture
One module, bitvec.py:
- terms and semantics:
bv,const,ite, Python operators onBVandBool,evaluate,to_signed; - bit-blasting: the
Blastergates and circuits, andsimulate; - solving:
solveandprove, with checked models and DRUP proofs; - rewriting:
Rewriterandpoly; - abstraction refinement:
solve_lazy_mul; - benchmarks:
popcount_hd,popcount_loop,midpoint_naive,midpoint_safe,abs_branchy,is_power_of_two.
1. Why bit-vectors
Lecture 12’s integers are unbounded; a machine’s are not. A 32-bit int holds values, and + wraps around. Most software and all hardware compute on fixed-width words. The bugs that matter are exactly where words and integers disagree:
- overflow;
- signed vs unsigned confusion;
- shifts by too much.
The theory QF_BV (quantifier-free bit-vectors, SMT-LIB) is the theory of these words. Its consumers:
- symbolic execution of binaries (Lecture 14’s topic: KLEE and friends speak QF_BV);
- hardware verification (Lecture 9’s circuits are bit-vector transition systems);
- compiler verification (Alive checks LLVM peephole optimizations);
- cryptography and smart contracts.
Complexity. QF_BV with widths written in unary is NP-complete: bit-blasting is a polynomial reduction to SAT. SMT-LIB writes widths in binary, though, so a formula can mention -bit vectors in a few characters. Kovásznai, Fröhlich and Biere showed that with binary widths the problem is NEXPTIME-complete.
2. Semantics, and the facts that break
2.1 Two readings of every word
A word is a string of bits. Operations, not words, have signs:
- the unsigned reading of is ;
- the signed (two’s complement) reading subtracts when the top bit is 1.
Script 01, Part 1, on 8 bits:
| expression | unsigned | signed | bits |
|---|---|---|---|
| 200 + 100 | 44 | 44 | 00101100 |
| 127 + 1 | 128 | −128 | 10000000 |
| 0 − 1 | 255 | −1 | 11111111 |
| 16 · 16 | 0 | 0 | 00000000 |
| 200 ≫ 1 (logical) | 100 | 100 | 01100100 |
| 200 ≫ₛ 1 (arithmetic) | 228 | −28 | 11100100 |
| 7 / 0 (udiv) | 255 | −1 | 11111111 |
| 7 % 0 (urem) | 7 | 7 | 00000111 |
| 1 ≪ 9 | 0 | 0 | 00000000 |
What the table shows.
- Comparisons depend on the reading. is false, but is true.
- SMT-LIB makes division total. is all ones and , so a formula never has an “undefined” value.
- Large shifts give 0. Shifting by gives 0 (the arithmetic shift fills with the sign).
2.2 Integer facts that fail
Each claim below is valid over (Lecture 12). On words prove returns a counterexample, checked by evaluating the claim on it (script 01, Part 2):
| claim | 8 bits | 32 bits |
|---|---|---|
The counterexamples are the boundaries:
- the unsigned and signed maxima;
- , the most negative number, which is its own negation;
- zero divisors: , because is not a field.
2.3 Classic bugs, and tricks that work
Script 01, Part 3, at 8, 16 and 32 bits:
| claim | result (32 bits) | conflicts |
|---|---|---|
| midpoint , given | counterexample | 2 |
| valid | 1,993 | |
| counterexample | 0 | |
| is a power of two | counterexample | 0 |
| the xor swap exchanges and | valid | 286 |
| valid | 943 |
The bugs.
- The midpoint. The naive midpoint overflows when . That is the binary-search bug that sat in the Java library for about nine years (Bloch 2006; there with signed ints). The fix is proved to stay in range for all pairs of 32-bit inputs.
- abs. It fails on the most negative number.
- The power-of-two test. The textbook trick also accepts 0.
The tricks. The two bit tricks from Hacker’s Delight are proved outright.
Run it:
python3 01_machine_arithmetic.py(about 2 s).
3. Bit-blasting
3.1 The idea
Bit-blasting is also called bit-flattening (Kroening and Strichman). It builds the circuit that a processor would build and hands it to a SAT solver:
- every bit of every term becomes a propositional literal;
- every operator becomes a circuit of gates;
- every gate becomes its Tseitin clauses (Lecture 2): is .
Blaster builds an and-inverter graph (AIG) with XOR and MUX conveniences, using two tricks:
- constant propagation: and , with no clauses at all;
- structural hashing: building twice returns the same literal, so equal subcircuits are shared.
3.2 The circuits
| operator | circuit | size |
|---|---|---|
| , (and negation) | ripple-carry adder: one full adder per bit; | linear |
| the borrow of | linear | |
| flip both sign bits, then | linear | |
| conjunction of XNORs | linear | |
| , , | barrel shifter: layers of multiplexers, plus “amount ” | |
| shift-and-add: partial products of bits, additions | quadratic | |
| , | fresh quotient and remainder with (in bits) , and the SMT-LIB values for | quadratic, double width |
Script 02, Part 1, counts clauses:
| operator | ||||
|---|---|---|---|---|
| 59 | 127 | 263 | 535 | |
| 62 | 130 | 266 | 538 | |
| 77 | 239 | 645 | 1,611 | |
| 103 | 515 | 2,299 | 9,707 | |
| 446 | 1,554 | 5,690 | 21,642 |
3.3 Who checks the bit-blaster?
A bit-blaster is a compiler from words to gates, and compilers have bugs. Script 02, Part 2 compares every circuit with evaluate, the exact semantics:
- 18 operators;
- widths 1–4 (division 1–3);
- every pair of inputs.
That is 5,608 cases, all agreeing.
- How each case is checked. Gate-level simulation for most circuits; a SAT call per input for division, which introduces quotient and remainder bits that no gate defines.
- What it does not prove. Exhaustive checking at small widths is evidence, not a proof for 32 bits. Lean’s
bv_decidecloses that gap by proving its bit-blaster correct for every width (§7).
Answers are checked too.
- SAT. The model is evaluated on the original formula with the semantics.
- UNSAT. The CDCL run’s DRUP proof is checked by Lecture 5’s checker. That certifies the CNF, and the circuit checks connect the CNF to the formula.
4. Why multiplication is hard
Script 02, Part 3 proves three identities at growing widths:
| 3 | 0 | 13 | 31 |
| 4 | 0 | 91 | 144 |
| 5 | 0 | 403 | 432 |
| 6 | 0 | 1,828 | 1,733 |
| 7 | 0 | 8,636 | — |
(The figures are CDCL conflicts; see figures/02_multiplier_hardness.png. At 8 bits commutativity of multiplication took about 36,000 conflicts during development.)
Addition is free. Structural hashing builds the same gates for and , because AND and XOR are hashed with sorted inputs. The miter, which asserts that the two outputs differ, collapses to ⊥ before the search starts.
Multiplication is not. The two multipliers add their partial products in different orders, so their internal signals differ, and CDCL has no notion of “the same number”. The conflicts grow by a factor of about 4–5 per bit. Multiplier equivalence is a classic hard family for SAT solvers and for BDDs: Bryant showed that BDDs for multiplication are exponential in any variable order.
That is the bottleneck of bit-blasting. It is the standard answer and works astonishingly well for most bit-level reasoning, but nonlinear arithmetic on wide words defeats it. The cure is not a faster SAT solver but reasoning at the word level (§5).
A success story for comparison. Script 02, Part 4 proves the slides’ bv_decide example: Hacker’s Delight’s branch-free popcount equals the loop.
| variables | clauses | conflicts | |
|---|---|---|---|
| 8 | 131 | 421 | 174 |
| 16 | 419 | 1,389 | 2,505 |
| 32 | 1,419 | 4,805 | 15,227 |
All are certified by DRUP. The 32-bit proof takes tens of seconds with our Python CDCL; bv_decide hands the same kind of CNF to CaDiCaL, an industrial SAT solver.
Run it:
python3 02_bit_blasting.py(about 30 s), withfigures/02_multiplier_hardness.png.
5. Word-level rewriting
5.1 Rules
Before any bit exists, Rewriter simplifies terms bottom-up:
- constant folding: every operator applied to constants is evaluated;
- identities:
- , , , and become ;
- , , and become ;
- and become ;
- shifts by give 0;
- becomes ⊤;
- canonical order: the arguments of are sorted, so becomes ;
- polynomial normalization of equalities: both sides of are read as polynomials in their atoms, with coefficients modulo .
- The atoms are variables and non-arithmetic subterms.
- Equal polynomials give ⊤.
- A difference that is a non-zero constant gives ⊥.
5.2 What it buys
Script 03, Part 1:
| claim | 8 bits | 16 bits | 32 bits |
|---|---|---|---|
| valid, by rewriting | valid, by rewriting | valid, by rewriting | |
| valid, by rewriting | valid, by rewriting | valid, by rewriting | |
| valid, by rewriting | false, by rewriting | false, by rewriting | |
| valid, by rewriting | valid, by rewriting | valid, by rewriting | |
| valid, by SAT (40 conflicts) | valid, by SAT (80) | valid, by SAT (160) |
All with 0 conflicts where rewriting decides.
Reading the table.
- Commutativity. It needed 8,636 conflicts at 7 bits in §4, and here costs nothing at 32 bits.
- The middle claim is Lean’s grind slide: “BitVec 8 is a CommRing of characteristic 256 — no bitvector-specific theory needed”. On 8 bits, , since . On 16 and 32 bits the difference is the non-zero constant , so the claim is false, and the model confirms it.
- The xor claim is not polynomial, so the solver bit-blasts it.
5.3 What it cannot do
Rewriting is sound (every rule is an identity modulo ) but incomplete (script 03, Part 2):
- on 1 bit. It is valid: and . Yet and are different polynomials, and a difference in normal forms proves nothing, so the bit-blaster decides it.
- Bit tricks. The power-of-two test and popcount are not polynomial identities: they need the bits.
On 300 random 6-bit formulas (282 SAT, 18 UNSAT; script 03, Part 3):
- rewriting never changes an answer (300/300);
- it decides 12 formulas alone;
- it cuts the total conflicts from 1,293 to 197.
Every SAT answer is still checked on the original formula, so a wrong rewrite rule would be caught.
Real solvers rewrite much more. Boolector and Bitwuzla apply hundreds of rewrite rules, normalize sums and products, and eliminate variables from linear equations. Rewriting is where most of a modern bit-vector solver’s engineering goes.
6. Alternatives to eager bit-blasting
6.1 Lazy multiplication by abstraction refinement
The idea is CEGAR again (Lectures 9, 10 and 13; Bryant et al. 2007):
- Abstract every non-constant multiplication: its result bits are fresh and unconstrained, an over-approximation of the formula.
- If the abstraction is UNSAT, so is the formula.
- If it is SAT, evaluate every multiplication on the model’s operands. If the model’s result bits are the true product everywhere, it is a real model. Otherwise make the violated multiplications precise and solve again.
Script 03, Part 4:
| formula | eager clauses | eager conflicts | lazy clauses | rounds | precise |
|---|---|---|---|---|---|
| , a sum of 8 products (16 bits) | 20,220 | 0 | 1,836 | 1 | 0/8 |
| , (20 bits) | 2,073 | 47 | 2,073 | 2 | 1/1 |
| (6 bits) | 493 | 1,828 | 493 | 3 | 2/2 |
| grind slide: (16 bits) | 10,802 | 15 | 10,802 | 5 | 5/5 |
When it pays.
- It pays when the multiplications do not matter. holds whatever the products are: the first call is UNSAT with a tenth of the clauses.
- It costs rounds when they do. Factoring (the answer 17·19) and commutativity need the multipliers, so the loop ends with the full circuit plus extra rounds.
- Our measurements. CDCL also dispatches irrelevant multipliers by propagation (0 conflicts eager), so the saving is in size, not search.
What real solvers add.
- They under-approximate too (fix some bits, or bound the operands), to find models faster.
- They refine lemma by lemma, not by blasting whole multipliers: Boolector and Bitwuzla do this for arrays and other operators.
6.2 Local search
Bitwuzla, co-developed by this lecture’s guest, also has a propagation-based local search engine (Niemetz, Preiner and Biere 2017). It works directly on the words, with no CNF:
- start from an assignment of the inputs;
- if the root is false, walk down the term to a “responsible” input, choosing at each operator a value that would make its output right (an inverse value, computed by word-level operations);
- change that input, and repeat.
It is incomplete (it never proves UNSAT), but very effective on satisfiable instances with wide arithmetic, exactly where bit-blasting struggles. Exercise 10 asks you to build a tiny version.
6.3 Other word-level techniques (overview)
- Width reduction. Some formulas are equisatisfiable at smaller widths, which gives smaller circuits.
- Algebraic methods. Gröbner bases over , the ring solver of grind (Lecture 15), prove polynomial identities without bits. Recent multiplier verification combines algebra with SAT.
- Int-blasting. Translate to integer arithmetic with explicit modulo constraints. This sometimes beats bit-blasting on wide words, and Lean’s grind uses it through its ToInt embedding.
Run it:
python3 03_word_level.py(about 4 s).
7. Certificates: from DRUP to verified bit-blasting
Lecture 15 showed Lean’s bv_decide pipeline:
Simplify → Reflect → Bit-blast → AIG → CNF → SAT solver (CaDiCaL) → LRAT proof → verified checker
This lecture built the same pipeline in Python:
| stage | here | in bv_decide |
|---|---|---|
| simplify | Rewriter | simp with bit-vector lemmas |
| bit-blast to an AIG | Blaster (hashing, constant propagation) | a bit-blaster written and proved correct in Lean |
| CNF | Tseitin clauses | the same |
| SAT | Lecture 5’s CDCL | CaDiCaL |
| proof | DRUP | LRAT (DRUP with hints) |
| checker | Lecture 5’s check_drup (unverified) | an LRAT checker proved correct in Lean |
Where trust sits. Our pipeline trusts the bit-blaster and the DRUP checker. We test the blaster exhaustively at small widths and keep the checker independent of the solver. bv_decide trusts neither: the reflection theorem says “if the verified checker accepts this certificate for the verified bit-blasting of φ, then φ holds”, and the Lean kernel checks that theorem once. A buggy CaDiCaL can at worst fail to produce a certificate.
8. Summary
- Bit-vectors are words modulo with two readings. SMT-LIB makes every operation total (division by zero, large shifts). Integer facts fail at the boundaries and on zero divisors.
- Bit-blasting turns each operator into a circuit and each gate into Tseitin clauses, with constant propagation and structural hashing (an AIG). Adders and comparators are linear, shifters , and multipliers and dividers quadratic.
- The blaster itself must be checked: here it agrees with the semantics on 5,608 exhaustive cases. Every answer is checked, by evaluating the model or by DRUP.
- Multiplication is the bottleneck. Commutativity needs 0 conflicts for and 8,636 for at 7 bits, growing about 4–5× per bit.
- Word-level rewriting, with polynomial normalization modulo , proves such identities instantly at any width. It is sound but incomplete ( on 1 bit), so solvers rewrite first and blast the rest.
- Abstraction refinement blasts only the multipliers a model violates; it saves size when they are irrelevant and costs rounds when they matter. Local search works on words directly for satisfiable instances.
bv_decideis this pipeline with a verified bit-blaster and LRAT checker: trust moves from tools to proofs.
9. Common pitfalls
- Reasoning about words with integer intuitions. , and all fail.
- Forgetting which reading an operator uses. vs , logical vs arithmetic shift, zero vs sign extension.
- Undefined division. In SMT-LIB, is all ones, not an error; programs may differ, so model their semantics explicitly.
- Trusting the bit-blaster. It is a compiler; test it, or verify it.
- Expecting SAT to handle multiplication. Multiplier identities blow up; use word-level reasoning.
- Reading a rewriting failure as a counterexample. Different normal forms prove nothing; only the bit-level answer counts.
10. Exercises
1. (semantics) On 8 bits, compute as an unsigned value, , and to 16 bits.
Answer
- is unsigned.
- , i.e. again.
- : the bits 11001000 become 11110010, which is 242 (signed ).
- to 16 bits is (signed ).
2. (counterexamples) Find, by hand, an 8-bit counterexample to .
Answer
A counterexample. and any , say : then , and .
3. (circuits) How many full adders does the shift-add multiplier of Blaster.mul build for , before constant propagation? Why does constant propagation remove some?
Answer
The count. There are additions of -bit rows, so 16 full adders before simplification.
What constant propagation removes.
- The first row is added to an all-zero accumulator, so its adders collapse.
- Row has low constant-0 bits, so those positions only copy.
- The carry into bit 0 is the constant 0.
4. (comparators) Explain why flipping the sign bits turns into .
Answer
The offset. Flipping the top bit adds modulo , mapping signed values monotonically onto unsigned .
Why the order survives. A monotone, order-preserving bijection keeps the comparison.
5. (division) Why does the division circuit compute in bits? Give an 8-bit example that would go wrong in bits.
Answer
What goes wrong in bits. The constraint could be satisfied with a wrapped-around product.
An example. , , and : on 8 bits, . So would be accepted, although . In 16 bits, , which forces the true quotient.
6. (hardness) Why does structural hashing make trivial but not ?
Answer
Addition. Each bit of is a function of , and a carry, built from AND and XOR. These are hashed with sorted inputs, so both orders build identical gates.
Multiplication. adds the partial products row by row, and adds . The intermediate sums are different functions, and only the final sums agree, so there is nothing to share.
7. (polynomials) Show by polynomial normalization that holds at every width, and that holds at 8 bits but not at 16.
Answer
The first identity. The difference is , which holds in every ring.
The second. . At 8 bits, and . At 16 bits the difference is , which is not the zero polynomial; as a function it is non-zero at (16384), so the claim is false.
8. (abstraction) For , trace solve_lazy_mul. How many rounds?
Answer
One round. The product bits are fresh, but the formula forces them to 6. The check computes from the model’s operands, the abstraction was right, and the answer is SAT after one round.
A variant. With instead, round 1 sets the product bits to 7 and the check fails (). The multiplier is made precise, and round 2 is UNSAT. solve_lazy_mul reports exactly these round counts.
9. (programming) Add a Karatsuba-style multiplier to Blaster (split operands in halves) and compare clause counts and the commutativity conflicts with the shift-add multiplier.
Answer sketch
The construction. Split , and compute , and recursively, with sums in bits.
What to expect. Fewer partial-product gates for large , but more adders, and no gain for SAT: commutativity of two different multiplier architectures is typically even harder for CDCL.
10. (programming) Implement a tiny propagation-based local search for formulas ( built from , , over two variables). At each step, pick the operand to change and compute an inverse value:
- for , take ;
- for , take ;
- for , if is odd take modulo , otherwise try random values.
Measure its success rate on random satisfiable targets.
Answer sketch
The search. Evaluate the term; if wrong, descend from the root, choosing at each node the child whose value can be inverted. Odd numbers are invertible modulo ; compute the inverse by Newton iteration.
What to expect. Fast success on most SAT instances with odd multipliers; even multipliers need random moves. The procedure never proves UNSAT.
11. Further reading
- D. Kroening, O. Strichman, Decision Procedures, Chapter 6 (2nd ed.): bit-vector arithmetic, bit-flattening, incremental and abstraction-based approaches.
- C. Barrett, P. Fontaine, C. Tinelli, The SMT-LIB Standard (the FixedSizeBitVectors theory and QF_BV logic).
- R. Brummayer, A. Biere, “Boolector: An Efficient SMT Solver for Bit-Vectors and Arrays”, TACAS 2009.
- A. Niemetz, M. Preiner, “Bitwuzla”, CAV 2023.
- A. Niemetz, M. Preiner, A. Biere, “Propagation based local search for bit-precise reasoning”, FMSD 2017.
- R. E. Bryant, D. Kroening, J. Ouaknine, S. A. Seshia, O. Strichman, B. Brady, “Deciding Bit-Vector Arithmetic with Abstraction”, TACAS 2007.
- G. Kovásznai, A. Fröhlich, A. Biere, “Complexity of Fixed-Size Bit-Vector Logics”, Theory of Computing Systems, 2016.
- R. E. Bryant, “On the Complexity of VLSI Implementations and Graph Representations of Boolean Functions with Application to Integer Multiplication”, IEEE TC 1991.
- H. S. Warren, Hacker’s Delight, 2nd ed., 2012.
- J. Bloch, “Extra, Extra — Read All About It: Nearly All Binary Searches and Mergesorts are Broken”, Google Research blog, 2006.
12. Next lecture
Lecture 17: Herbrand’s theorem and quantifier instantiation.
- Beyond quantifier-free formulas. Herbrand’s theorem reduces first-order unsatisfiability to the unsatisfiability of finitely many ground instances. SMT solvers find those instances by E-matching (grind’s annotations in Lecture 15), model-based instantiation and enumeration.
See Lecture 17.