Chapter 36
Cook-Levin as SAT
src/sat.c writes each problem as one Boolean formula in DIMACS CNF, the input format of SAT solvers (8de51b7), whose satisfying assignments are exactly the programs that decide the problem: the Cook-Levin construction itself. Expanded as polynomials, the circuit's accepted alone reached 10,324 terms (“The polynomials, in full”). This chapter takes the other way of “Six expressions, composed” and names every wire instead: each gate's output gets a variable, tied to the gate's inputs by three clauses, so the formula grows linearly with the steps.
One gate, three clauses §
A clause is an OR of literals, each a variable or its complement, and a CNF formula is an AND of clauses; in DIMACS, -14 2 says "v14 is 0 or v2 is 1". The gate g = a AND b becomes -g a, -g b and g -a -b: g forces a, g forces b, and a and b together force g. src/sat_hand_main.c, run at the book's build, checks the eight rows: the three hold together exactly where g equals a AND b.
| a | b | g | -g a |
-g b |
g -a -b |
all three | a AND b |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 1 | 1 | 1 | 1 | 0 |
| 0 | 0 | 1 | 0 | 0 | 1 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 | 1 | 1 | 0 |
| 0 | 1 | 1 | 0 | 1 | 1 | 0 | 0 |
| 1 | 0 | 0 | 1 | 1 | 1 | 1 | 0 |
| 1 | 0 | 1 | 1 | 0 | 1 | 0 | 0 |
| 1 | 1 | 0 | 1 | 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
The OR gate g = a OR b is the mirror image: g -a, g -b and -g a b. Each AND and OR becomes a Tseitin variable, tied to its inputs by three clauses, and a NOT is a literal's sign:
Lit sat_and(Cnf *c, Lit x, Lit y) {
if (x == SAT_FALSE || y == SAT_FALSE) return SAT_FALSE;
if (x == SAT_TRUE) return y;
if (y == SAT_TRUE) return x;
if (x == y) return x;
if (x == -y) return SAT_FALSE;
Lit a = x < y ? x : y;
Lit b = x < y ? y : x;
int out = find_gate(c, GATE_AND, a, b);
if (out) return out;
out = new_gate(c, GATE_AND, a, b);
Lit c1[2] = {-out, a};
Lit c2[2] = {-out, b};
Lit c3[3] = {out, -a, -b};
One stepper §
The formula's first 12 variables are the program's bits: line k's hb is v(2k − 1) and its lb v(2k), so nada is 0 0, zero 0 1, decr 1 0 and mod2 1 1. The logic is the stepper's, gate for gate, NOT, AND and OR as in “Arithmetization”. A gate on a constant is folded, and a gate already built from the same two inputs is reused. Core 0 starts with AX = 0 and both flags 0, so its state after line 1 reads two gates:
| wire | gate | its clauses | 1 when line 1 is |
|---|---|---|---|
| v14 | -v1 AND v2 | -14 -1 -14 2 14 1 -2 |
zero |
| v15 | -v2 AND v1 | -15 -2 -15 1 15 2 -1 |
decr |
core 0 after line 1: hb 0, lb 0, halted v15, accepted v14
Core 0 is accepted after line 1 exactly when line 1 is zero, and halted exactly when it is decr; its AX stays the constant 0. These are the names in the symbolic tableau of “The tableau, filled in”. Core 1 starts with AX = 1. Its unit reads three of line 1's four decoders, built once for all four cores, and adds two OR gates:
| wire | gate | its clauses | 1 when line 1 is |
|---|---|---|---|
| v13 | -v2 AND -v1 | -13 -2 -13 -1 13 2 1 |
nada |
| v14 | -v1 AND v2 | -14 -1 -14 2 14 1 -2 |
zero |
| v16 | v1 AND v2 | -16 1 -16 2 16 -1 -2 |
mod2 |
| v67 | v13 OR v14 | 67 -13 67 -14 -67 13 14 |
nada or zero |
| v68 | v16 OR v67 | 68 -16 68 -67 -68 16 67 |
nada, zero or mod2 |
core 1 after line 1: hb 0, lb v68, halted 0, accepted 0
Core 1's AX falls to 0 exactly when line 1 is decr.
The unrolling §
Each of the six lines, on each of the four cores, is such a stepper, wired to the state the stepper above it left, and the last four clauses ask that each core's final accepted be what the problem wants. Every problem's formula has 416 variables and 1,216 clauses (out/tcc/sat facts, run for this book). len=N forces the lines past N to nada, two unit clauses a line, and fix=K:OP fixes line K.
The proof by propagation §
Theorem 14. For each of the 16 problems, the satisfying assignments of its formula, read as programs by their first 12 variables, are exactly the programs that decide it. With the lines past N forced to nada, the formula is satisfiable exactly when N is at least the problem's shortest length (Theorem 4).
Proof. tests/test_sat.c proves the encoding by itself. For every problem it sets the program bits to each of the 4,096 programs and unit-propagates them through the formula. Each gate's inputs come before it, so propagation alone decides every Tseitin variable. The assignments that survive are exactly the programs the machine says decide the problem, 4,096 over the 16 problems (every_puzzles_models_are_exactly_its_solving_programs), and with a length bound, exactly those that short (a_length_bound_admits_the_solving_programs_that_short).
rocsat as an oracle §
sat/oracle.shale asks rocsat solve about each problem's formula with the program's bits free (f977bcc), so the solver searches (“The SAT solver”). Each SATISFIABLE answer is a program, which the script runs on the machine and blocks with a clause before asking again; UNSATISFIABLE, expected once every program is found, ends the count. Each shortest length is then SATISFIABLE, and one line shorter UNSATISFIABLE, as expected, with a DRAT proof our checker replays. That is Theorem 4 again, by Theorem 12. It starts rocsat 4,143 times, so it runs as its own script; its last run, on 2026-11-25, spent 62.6 s in its programs and ended "rocsat agrees with the machine on all 16 puzzles", with 4,096 models and all 15 proofs verified.