Provably minimal Boolean circuits. Give circuit-lean a circuit or a
truth table. It finds the smallest circuit that computes the same function,
then proves in Lean that no smaller one exists.
A SAT solver does the search, but nothing it says is trusted: its proofs are replayed by Lean's verified checker, and the translation into SAT is itself proved correct in Lean.
- Every four-input function, certified. All 222 classes of four-input Boolean functions have a Lean theorem giving their smallest circuit. The counts agree exactly with Table 1 of Knuth, The Art of Computer Programming, Vol. 4A, §7.1.2, for both the 222 classes and all 65,536 functions. See below.
- Any two-input gate. Circuits may use all 16 two-input operations, the standard model in circuit complexity.
- A small trusted base. No Mathlib, no axioms beyond Lean's standard
three, and one
native_decidestep for proof replay. Details below. - Fast to check. A clean build re-checks the library and the worked examples in a few seconds.
$ lake exe fold fullAdder circuits/fullAdderNaive.circ
size 0: impossible (13 ms, proof saved)
size 1: impossible (9 ms, proof saved)
size 2: impossible (9 ms, proof saved)
size 3: impossible (12 ms, proof saved)
size 4: impossible (29 ms, proof saved)
size 5: found (26 ms). 7 gates fold to 5.
wrote CircuitLean/Certified/fullAdder.leanThe generated file contains the five-gate circuit and this theorem:
/-- No well-formed circuit computing the same function has fewer than 5 gates. -/
theorem fullAdder_isMinimal : fullAdder.IsMinimal := ...Compiling the file checks it. fold also accepts a truth table directly:
lake exe fold xor4 --table 4 0110100110010110Install elan and CaDiCaL, then:
git clone https://github.com/keithadler/circuit-leancd circuit-lean && lake build| File | Contents |
|---|---|
CircuitLean/Basic.lean |
Op, Gate, Circuit, evaluation, size, well-formedness, truth tables |
CircuitLean/Fold.lean |
Equiv, FoldsTo, IsMinimal and their basic theory |
CircuitLean/Wires.lean |
How wire values behave during evaluation |
CircuitLean/Encode.lean |
The SAT encoding and the proof that it is sound |
CircuitLean/Normalize.lean |
Every circuit can have its gate arguments sorted |
CircuitLean/Order.lean |
Every circuit can have its gates put in a canonical order |
CircuitLean/EncodeFast.lean |
The encoding restricted to sorted, ordered circuits |
CircuitLean/Certificate.lean |
From checked solver refutations to IsMinimal |
CircuitLean/Examples.lean |
Hand-written folds checked by decide |
CircuitLean/Certified/ |
Generated minimal circuits with their theorems |
Main.lean |
The fold command |
A circuit is a straight-line program. It has inputs input wires numbered
from 0, and a list of gates. Each gate applies one of the 16 two-input
Boolean operations to two earlier wires and defines one new wire. Any wire
can be read any number of times, and a list of output wires says which
values the circuit returns.
structure Gate where
op : Op -- truth table of a two-input operation
a : Nat -- first wire read
b : Nat -- second wire read
structure Circuit where
inputs : Nat
gates : List Gate
outs : List NatThe size of a circuit is its number of gates.
FoldsTo c d says that d is well formed, computes the same function as
c, and uses no more gates. It is decidable, so for concrete circuits the
proof is one word:
example : FoldsTo fullAdderNaive fullAdder := by decideIsMinimal c says that no well-formed circuit computing the same function
is smaller than c. It quantifies over all circuits, so it cannot be
settled by evaluation.
For each size k below the size of c, a SAT formula says "some circuit
with k gates computes this truth table". Three things make an
unsatisfiable answer into a theorem:
- The encoding is proved sound in Lean.
Circuit.encode_satshows that any well-formed circuit of that size gives a satisfying assignment, so an unsatisfiable formula means no such circuit exists. - The solver is not trusted. CaDiCaL writes its refutation as an LRAT
proof, which is saved under
certs/. - The refutation is checked inside Lean, by the verified LRAT checker that ships with Lean's standard library.
isMinimal_of_checkCerts puts these together.
To make the search fast, the solver is only asked about circuits in a
canonical form: each gate reads its lower-numbered wire first, and gates
that do not depend on each other appear in a fixed order. Normalize.lean
and Order.lean prove that every circuit with two or more inputs has an
equivalent circuit of the same size in that form, so ruling out the
canonical ones rules out all of them. isMinimal_of_checkCertsFast is the
resulting theorem, and its statement is the same unrestricted IsMinimal.
- The Lean kernel and its three standard axioms.
- The Lean compiler, for one step: replaying the LRAT proofs uses
native_decide, as Lean's ownbv_decidetactic does. The soundness proof of the encoding andisMinimal_of_checkCertsdo not use it. - That
Circuit,WellFormedandIsMinimalsay what you want them to say. They are short; read them.
Not trusted: the SAT solver, the fold program, and the encoding.
See docs/fold.md for the tool and the file format, and docs/design.md for why the definitions look the way they do.
| Circuit | Inputs | Smallest size | Theorem |
|---|---|---|---|
| Full adder (sum and carry) | 3 | 5 | Certified.fullAdder_isMinimal |
| Majority of three | 3 | 4 | Certified.majority3_isMinimal |
| Exactly two of four | 4 | 6 | Certified.exactlyTwo4_isMinimal |
Plus every class of four-input function, below.
Up to reordering inputs, negating inputs and negating the output, there are
222 classes of four-input Boolean functions.
data/npn4-classes.txt lists one truth table per
class, and data/npn4-results.tsv records the
smallest size fold proved for each.
| Smallest size | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| Classes | 4 | 5 | 20 | 34 | 75 | 72 | 12 |
The generated theorems are in
CircuitLean/Certified/Npn4. Their
refutations total several hundred megabytes, so they are not stored here.
Every class is finished, and each theorem was compiled, which is the
check. The largest size is 7 gates, reached by 12 classes.
To regenerate and check them, run the script below. It takes about 45 minutes for 207 classes and stops each solver call after 20 seconds:
scripts/run-npn4.shThe 15 classes that need longer runs (the twelve 7-gate classes and three
6-gate ones) were done with the
npn4-hard workflow on GitHub, one class
per job, each taking 5 to 9 minutes.
These results agree exactly with Table 1 of Knuth, The Art of Computer
Programming, Volume 4A, section 7.1.2, which lists the same counts per cost
for both the 222 classes and all 65,536 functions.
scripts/compare-knuth.py does the comparison.
Here a circuit's size counts every gate, so a constant or a negated input
costs one gate where Knuth counts zero; the script accounts for that.
Install elan, then:
lake buildBuilding checks every theorem, including the saved refutations. One refutation is large and stored compressed; to check that theorem too:
scripts/unpack-certs.shlake build CircuitLean.Certified.exactlyTwo4Running
fold on a new circuit also needs
CaDiCaL on your PATH.
The Lean version is pinned in lean-toolchain.
If you use this in your work, please cite the repository:
Keith Adler. circuit-lean: provably minimal Boolean circuits in Lean 4.
https://github.com/keithadler/circuit-lean, 2026.
Apache 2.0. See LICENSE.