Lectures 8 and 9 verified functions: an input goes in, an output comes out, and the theorem relates the two. A lock manager, a network protocol or a GPU kernel is different. It runs step after step, several parts of it act at once, and the property you care about must hold at every moment of every run, not only at the end. Real machines also compute with words of a fixed width, where adding 1 to the largest 8-bit number gives 0.
This lecture treats both. The first half models a small protocol as a state machine and
proves a safety property by finding an inductive invariant. The second half proves facts
about 64-bit words with bv_decide, a decision procedure that ships with Lean and calls a
SAT solver, and looks at exactly what that proof trusts. The lecture ends with recent Lean
projects that verify hardware and software, each with what was proved and what was assumed.
Notebook and cheatsheet
Notebook 10 runs every experiment on this page and compares
bv_decidewith brute-force testing in Python. Cheatsheet: State machines and invariants, BitVec and bv_decide, decide, native_decide and kernel reduction.
A protocol as a state machine
Definition 10.1 (State machine).A state machine is a type of states, an initial state, and a step relation that says which state may follow which. A run starts in the initial state and takes one step at a time. A state is reachable if some run arrives at it.
Our example has two processes, A and B, that share one lock. Each process is idle, waiting,
or in its critical section: code, such as an update to shared memory, that must never run in
both processes at once. The state records where each process is (its program counter, PC)
and whether the lock is taken.
import Std.Tactic.BVDecide
inductive PC where
| idle | wait | crit
deriving DecidableEq, Repr
structure MState where
a : PC
b : PC
lock : Bool
deriving DecidableEq, ReprThe step relation is an inductive predicate with one constructor per action. A process may ask for the lock, enter when the lock is free (taking it in the same step), and leave (releasing it). Nothing says which process moves next, so the relation allows every interleaving, and a theorem about it covers every schedule.
inductive Step : MState → MState → Prop
| requestA (b : PC) (l : Bool) : Step ⟨.idle, b, l⟩ ⟨.wait, b, l⟩
| enterA (b : PC) : Step ⟨.wait, b, false⟩ ⟨.crit, b, true⟩
| leaveA (b : PC) (l : Bool) : Step ⟨.crit, b, l⟩ ⟨.idle, b, false⟩
| requestB (a : PC) (l : Bool) : Step ⟨a, .idle, l⟩ ⟨a, .wait, l⟩
| enterB (a : PC) : Step ⟨a, .wait, false⟩ ⟨a, .crit, true⟩
| leaveB (a : PC) (l : Bool) : Step ⟨a, .crit, l⟩ ⟨a, .idle, false⟩
def start : MState := ⟨.idle, .idle, false⟩
inductive Reachable : MState → Prop
| start : Reachable start
| step {s t : MState} : Reachable s → Step s t → Reachable tThe pattern ⟨.wait, b, false⟩ in enterA means that A can enter only when the lock is
false. Testing and setting the lock happen in one step, as with a hardware test-and-set
instruction. In the terms of Definition 10.1, start is the initial state, Step is the
step relation, and Reachable picks out the reachable states: the start state, and anything
one step from a reachable state. (Reachable s says the same as Star Step start s, with the
closure Star of Lecture 4.)
Safety, and why it is not enough for induction
The property we want is mutual exclusion: never both processes in the critical section.
def Safe (s : MState) : Prop := ¬ (s.a = .crit ∧ s.b = .crit)The natural plan is induction on Reachable. The step case gives Safe s and Step s t and
asks for Safe t, for any state s that is safe, reachable or not. That step fails. In the
state where A is in the critical section, B is waiting and the lock is free, only A is
inside, so the state is safe; but B may now enter:
theorem safe_not_inductive :
¬ ∀ s t : MState, Safe s → Step s t → Safe t := by
intro h
have hs : Safe ⟨.crit, .wait, false⟩ := by simp [Safe]
have ht := h _ _ hs (.enterB .crit)
simp [Safe] at htThat state cannot be reached, because A took the lock on the way in. But Safe does not
mention the lock, so the induction hypothesis cannot rule the state out. This is the problem
of Lecture 9’s compiler proof again: a true statement that is too weak to serve as its own
induction hypothesis. The cure is the same: prove something stronger.
Definition 10.2 (Inductive invariant).A predicate
Ion states is an inductive invariant if it holds in the start state and is preserved by every step:I sandStep s tgiveI t, for everys, reachable or not. By induction onReachable, an inductive invariant holds in every reachable state.
Here the extra fact is that whoever is in the critical section holds the lock:
def MutexInv (s : MState) : Prop :=
Safe s ∧ (s.a = .crit → s.lock = true) ∧ (s.b = .crit → s.lock = true)
theorem inv_start : MutexInv start := by
simp [MutexInv, Safe, start]
theorem inv_step {s t : MState} (h : MutexInv s) (hs : Step s t) :
MutexInv t := by
cases hs <;> simp_all [MutexInv, Safe]cases hs produces six goals, one per action, with s and t replaced by that
constructor’s patterns. simp_all unfolds the definitions and closes each goal. Take
enterB a: the old state has the lock free, so by the invariant A is not in the critical
section, and the new state, with B inside and the lock taken, is fine. Had one action broken
the invariant, its goal would have stayed open and shown the offending state.
Theorem 10.1 (Mutual exclusion).In every reachable state, at most one process is in the critical section.
theorem inv_reachable {s : MState} (h : Reachable s) : MutexInv s := by
induction h with
| start => exact inv_start
| step _ hst ih => exact inv_step ih hst
theorem mutual_exclusion {s : MState} (h : Reachable s) : Safe s :=
(inv_reachable h).1
#print axioms mutual_exclusionLean prints 'mutual_exclusion' depends on axioms: [propext]. The proof of Theorem 10.1 has
the shape of every invariant proof: the reachable states lie inside the invariant
(Definition 10.2), which lies inside the safe states. Finding the invariant is the
creative step; checking it is mechanical. This machine has only 18 states, so a model
checker could simply visit all of them. Invariants matter when the state space has no fixed
size: any number of processes, or messages in flight.
Refinement, in plain words
A specification can itself be a state machine, simpler than the system. Here the abstract state is “who holds the critical section, if anyone”, and the abstract steps are acquire and release. The system refines the specification if a translation from concrete to abstract states, called an abstraction function, maps every concrete step to an abstract step or to no change at all (a stuttering step, like a request). Every run of the system then looks, through the translation, like a run of the specification, so whatever holds of all runs of the specification holds of the system.
inductive Who where
| A | B
deriving DecidableEq, Repr
inductive SpecStep : Option Who → Option Who → Prop
| acquire (p : Who) : SpecStep none (some p)
| release (p : Who) : SpecStep (some p) none
def holder (s : MState) : Option Who :=
if s.a = .crit then some .A else if s.b = .crit then some .B else none
theorem refines {s t : MState} (h : MutexInv s) (hs : Step s t) :
SpecStep (holder s) (holder t) ∨ holder s = holder t := by
cases hs <;> simp_all [MutexInv, Safe, holder, SpecStep.acquire,
SpecStep.release]The hypothesis h is needed: from the unreachable state with both processes inside, A
leaving would change the holder from some .A to some .B, which is no abstract step. So
refinement proofs rest on invariants. Large verified systems stack several refinements, from
a short specification down to code. For distributed protocols,
Veil (CAV 2025) builds this workflow into Lean: it
combines automatic proofs by SMT solvers with interactive Lean proofs where automation fails.
Remark 10.1 (The theorem is about the model).
Stepwas written by hand. If real code reads the lock and writes it in two separate steps, Theorem 10.1 says nothing about that code. Systems projects close this gap by proving that the code refines the model, or by generating the code from the model.
The first exercise in Try this asks you to make that change in the model and watch the invariant proof fail (Remark 10.1).
Fixed-width words: BitVec
BitVec w is the type of words of w bits, with arithmetic modulo 255#8 is the number 255 as an 8-bit word, and &&&, |||, ^^^, <<<,
>>> are bitwise and, or, exclusive or, and shifts.
#eval 255#8 + 1#8
#eval 0b10110100#8 &&& (0b10110100#8 - 1)
#eval 0b10110100#8 &&& -0b10110100#8This page loads only Std.Tactic.BVDecide, not Mathlib, so Lean prints the words in
hexadecimal: 0x00#8, 0xb0#8 and 0x04#8, that is 0, 176 and 4. (With Mathlib loaded,
#eval prints them in decimal: 0#8, 176#8, 4#8.) The first is overflow.
The second is a classic trick: subtracting 1 turns the lowest set bit into 0 and the zeros
below it into ones, so x &&& (x - 1) clears the lowest set bit (180 is 1011 0100 in
binary, and 176 is 1011 0000). The third isolates that bit, because -x in two’s
complement is the bitwise complement plus 1.
bv_decide decides such statements for every value at once. A BitVec 64 has bv_decide proves both of these:
theorem clear_lowest (x : BitVec 64) : x &&& (x - 1) = x - (x &&& -x) := by
bv_decide
theorem clear_lowest_lt (x : BitVec 64) (h : x ≠ 0) : x &&& (x - 1) < x := by
bv_decideOn a false statement it reports a counterexample, an assignment that breaks it. Here is the average of two 8-bit words computed the obvious way, compared with the exact average, computed in 9 bits where the sum cannot overflow:
theorem avg_naive (x y : BitVec 8) :
(x + y) >>> 1 = ((x.setWidth 9 + y.setWidth 9) >>> 1).setWidth 8 := by
bv_decideLean reports an error:
The prover found a counterexample, consider the following assignment:
x = 255#8
y = 255#8The sum (low + high) / 2 overflows once an array has about a billion elements, and the
bug had gone unnoticed for about nine years
(Google Research blog).
One fix he gives computes lo + (hi - lo) / 2, which is correct when lo ≤ hi:
theorem mid_ok (lo hi : BitVec 64) (h : lo ≤ hi) :
lo + ((hi - lo) >>> 1) =
((lo.setWidth 65 + hi.setWidth 65) >>> 1).setWidth 64 := by
bv_decideHow bv_decide works, and what it trusts
bv_decide is described in the OOPSLA 2025 paper by Böving and others
(paper page). It proves a
goal by showing that its negation has no solution, in five stages:
- It rewrites the goal into a normal form and turns it into data, a syntax tree
bvof the formula. This step produces an ordinary proof, checked by the kernel. - It bit-blasts
bv: each 64-bit word becomes 64 Boolean variables and each operation a circuit (addition becomes a chain of adders), written as clauses, the input format of a SAT solver. - The SAT solver CaDiCaL, which ships with Lean, searches for an assignment that satisfies the negated goal. If it finds one, that is the counterexample above.
- If no assignment exists, CaDiCaL writes a certificate in the LRAT format: a list of derived clauses, each with pointers to the earlier clauses that justify it, ending in the empty clause (Cruz-Filipe et al., CADE 2017).
- Lean runs the function
verifyBVExpr bv cert, which redoes the bit-blasting and checks the certificate. A theorem in Lean’s library,unsat_of_verifyBVExpr_eq_true, says that if this function returnstrue, the formula has no solution.
The SAT solver, the hard and fast part, is outside the trusted base: a wrong certificate
fails the check, and a wrong counterexample can be tried by #eval. What is trusted is stage
5. The kernel could in principle evaluate verifyBVExpr itself, but kernel evaluation is
slow, so Lean runs it as compiled code, as native_decide does in Lecture 8, and records the
result as an axiom. The audit shows it:
#print axioms clear_lowest'clear_lowest' depends on axioms: [propext, Classical.choice, Quot.sound, clear_lowest._native.bv_decide.ax_1]The last name is an auxiliary axiom created for this one theorem; it states that the
compiled check returned true (the number after ax_ can differ in another file). The
tactic’s documentation
says it “trusts the correctness of the code generator”. Compared with native_decide, the
trusted computation is small and fixed: one checker whose correctness is a theorem, not an
arbitrary program you wrote.
Remark 10.2 (Three ways to decide).
decideevaluates in the kernel and adds no axiom.native_decidecompiles your whole computation and trusts the compiler.bv_decidetrusts the compiler only for running a verified checker on a certificate produced by an untrusted solver.
Use Remark 10.2 when you read an axiom list: a proof that ends in bv_decide is a
claim about Lean’s compiler as well as about the bits.
Verified systems in Lean, 2024 to 2026
The tags below follow Lecture 13: SELF-REPORTED means the authors say so and we found no independent check. Read each claim as a statement about a model, plus assumptions that link the model to the real system. Lecture 13 gives a checklist for such claims.
- PTX in Lean (Koska, Koska and Koska; SF Tensor;
arXiv 2611.04447, 2026-11-03): SELF-REPORTED. PTX
is the assembly-like language that NVIDIA’s compilers emit for GPUs. The authors model it
in Lean on the axiomatic PTX memory model of
Lustig, Sahasrabuddhe and Giroux (ASPLOS 2019),
which says which values a read may return when threads share memory. They prove partial
correctness (Lecture 9) of seven kernels (nine PTX entry points), among them a Hopper
matrix multiply and a scalar FlashAttention. Assumed: no undefined behaviour in the kernel, a correct
ptxasassembler and hardware, floating-point numbers as exact reals, and two memory-model axioms (No-Thin-Air, Fence-SC) left out. We could not confirm that the code is public: the paper links to no repository. - LARK (arXiv 2611.02028, 2026-11-01): SELF-REPORTED. One Lean GPU machine model serves PTX, CUDA and HIP. LLM agents prove kernels equivalent to PyTorch reference code; on 10 sampled production kernels they proved 9 and found a new data race in the tenth. Assumed: exact arithmetic, and Lean semantics of each operation that people must check by hand.
- LNSym (repository): a Lean simulator for Armv8 machine code, aimed at cryptographic routines. Its README does not say which routines have finished proofs, so we claim none.
- Aeneas and SymCrypt (Microsoft, arXiv 2609.15648, September 2026): SELF-REPORTED. Aeneas translates Rust into a pure Lean model. 237k lines of Lean (30k generated by Aeneas) prove safety, absence of panics and functional correctness of 16.7k lines of Rust; the ML-KEM and SHA-3 parts (about 4.1k lines) ship in Windows Insider builds, and about 7.4k lines are experimental. AI agents wrote the proofs. Assumed: the translation, and the Lean statements of the standards and of platform intrinsics, which the paper says still need expert design and review.
- Cedar (AWS, arXiv 2407.01688, 2024): SELF-REPORTED. Properties of an executable Lean model of the authorization language Cedar are proved, and the proofs found 4 bugs in its policy validator. Assumed: that the production Rust matches the model, which is checked by random differential testing, not proved; testing found 21 more bugs.
- SampCert (AWS, arXiv 2412.01671, 2024): SELF-REPORTED. More than 12,000 lines of Lean on differential privacy, including verified samplers for the discrete Laplace and Gaussian distributions, which run in AWS Clean Rooms. The abstract does not list its assumptions; read the paper before relying on it.
Two patterns repeat. Each project proves a theorem about a model (an instruction set, a translation of Rust, an executable specification) and then names the gap between model and machine. And the larger proofs are now written partly by AI agents, which makes the Lean kernel’s independent check, and an honest list of assumptions, more important.
Try this
- Split
enterAinto two steps: a new program counterreadyreached when A sees the lock free, and a second step that takes the lock. Reruninv_stepand find a run that breaks mutual exclusion. - Prove that
x &&& -xhas at most one bit set, by showing that clearing its lowest set bit gives 0. Usebv_decideforBitVec 64. - Before running it, predict the counterexample
bv_decidereports forx < x + 1withx : BitVec 8. - Replace
bv_decideinclear_lowestbydecide. What does Lean say, and why?
Readings
- Henrik Böving and others, “Interactive Bitvector Reasoning using Verified Bit-Blasting”, OOPSLA 2025 (paper page).
- Leslie Lamport, Specifying Systems, 2002: state machines, invariants and refinement in TLA+.
- The Veil repository, for protocol verification in Lean.
- Luís Cruz-Filipe, Marijn Heule, Warren Hunt, Matt Kaufmann and Peter Schneider-Kamp, “Efficient Certified RAT Verification”, CADE 2017, doi 10.1007/978-3-319-63046-5_14.