Circuit Satisfiability and the Cook-Levin Idea
The recipe for NP-completeness proofs needs one problem to start from, a problem that every language in NP reduces to directly. Cook (1971) and Levin (1973) independently found such problems. The cleanest one to state is circuit satisfiability: given a Boolean circuit, is there an input that makes its output 1? This lesson shows that CIRCUIT-SAT is in NP and explains the idea behind the proof that it is NP-hard: any polynomial-time verifier can be compiled into a polynomial-size circuit. The full proof fills in details about machine models; the idea fits in a page.
Boolean circuits
A Boolean combinational circuit is a DAG of gates: AND (), OR () and NOT (), wired from some inputs to one output. Each wire carries 0 or 1. Gates may have large fan-out (one wire feeding many gates), but the graph has no cycles, so the output is determined by the inputs. A truth assignment is a choice of input values; the circuit is satisfiable if some assignment makes the output 1.
The size of a circuit is its number of gates plus wires. Trying all assignments of inputs takes evaluations, each linear in the size of the circuit.
Lemma. CIRCUIT-SAT is in NP.
Proof. The certificate is a truth assignment, one bit per input. The verifier evaluates the gates in topological order, each in constant time, and checks that the output is 1. That is linear time in the size of the circuit.
A small circuit, written as a list of gates in topological order, and the brute-force search over its assignments:
from itertools import product
# each gate: (name, kind, inputs); x1, x2, x3 are the circuit's inputs
circuit = [("g1", "OR", ["x1", "x2"]), ("g2", "NOT", ["x3"]), ("g3", "AND", ["g1", "g2"]),
("g4", "AND", ["x1", "x2"]), ("g5", "NOT", ["g4"]), ("out", "AND", ["g3", "g5"])]
def evaluate(circuit, inputs):
val = dict(inputs)
for name, kind, ins in circuit: # topological order: inputs are ready
v = [val[i] for i in ins]
val[name] = (1 - v[0]) if kind == "NOT" else (min(v) if kind == "AND" else max(v))
return val["out"]
satisfying = []
for bits in product([0, 1], repeat=3):
if evaluate(circuit, zip(["x1", "x2", "x3"], bits)):
satisfying.append(bits)
print("satisfying assignments (x1, x2, x3):", satisfying)
print("evaluations needed by brute force: 2^3 =", 2 ** 3)satisfying assignments (x1, x2, x3): [(0, 1, 0), (1, 0, 0)]
evaluations needed by brute force: 2^3 = 8
The output is : exactly one of is 1 and .
From a verifier to a circuit
Theorem (Cook-Levin, in circuit form). CIRCUIT-SAT is NP-hard, and therefore NP-complete.
The idea of the proof: let , with a verifier that runs in time on inputs of length and certificates of length . A computer executing is, at every step, in a configuration: the program counter, the registers and the memory, a string of polynomially many bits (the verifier can touch only memory cells). One step of the computer maps a configuration to the next one, and that map is a fixed piece of hardware: a Boolean circuit of polynomial size. Now chain copies of :
x (hard-wired) -+
+-> [ config 0 ] -> M -> [ config 1 ] -> M -> ... -> M -> [ config T(n) ] -> output bit
y (inputs) -+The first configuration contains the instance as constants and the certificate as the circuit's inputs; the output wire is the bit where leaves its answer. The resulting circuit has size , polynomial, and it can be written down in polynomial time given . By construction, for every , so
That is a polynomial-time reduction , for an arbitrary . The technical work of the full proof is showing that the step circuit exists and has polynomial size, which follows from the fact that one step of a computer only combines a bounded number of bits in a fixed way.
A small compiled verifier
The idea can be watched in action on a concrete verifier. COMPOSITE is in NP with the certificate "two factors with ". The program compiles this check for a fixed into an explicit circuit built from AND, OR and NOT gates (with XOR written as a small sub-circuit): a multiplier for two 3-bit numbers, a comparator with the constant bits of , and tests that and . Then it decides CIRCUIT-SAT by trying all inputs.
from itertools import product
class Circuit:
def __init__(self):
self.gates = [] # (kind, input wires)
self.inputs = []
def new_input(self):
self.gates.append(("IN", ())); w = len(self.gates) - 1; self.inputs.append(w); return w
def const(self, bit):
self.gates.append(("ONE" if bit else "ZERO", ())); return len(self.gates) - 1
def g(self, kind, *ws):
self.gates.append((kind, ws)); return len(self.gates) - 1
def xor(self, a, b): # (a OR b) AND NOT (a AND b)
return self.g("AND", self.g("OR", a, b), self.g("NOT", self.g("AND", a, b)))
def evaluate(self, values, out):
val, it = [], iter(values)
for kind, ws in self.gates:
if kind == "IN": val.append(next(it))
elif kind == "ONE": val.append(1)
elif kind == "ZERO": val.append(0)
elif kind == "NOT": val.append(1 - val[ws[0]])
elif kind == "AND": val.append(val[ws[0]] & val[ws[1]])
else: val.append(val[ws[0]] | val[ws[1]])
return val[out]
def composite_circuit(N, bits=3):
C = Circuit()
a = [C.new_input() for _ in range(bits)] # little-endian bits of the certificate
b = [C.new_input() for _ in range(bits)]
acc = [C.const(0)] * (2 * bits) # shift-and-add multiplier
for i in range(bits):
carry = C.const(0)
for j in range(bits):
pp = C.g("AND", a[j], b[i])
s1 = C.xor(acc[i + j], pp)
total = C.xor(s1, carry)
carry = C.g("OR", C.g("AND", acc[i + j], pp), C.g("AND", s1, carry))
acc[i + j] = total
acc[i + bits] = carry
eq = C.const(1) # product == N, bit by bit
for k in range(2 * bits):
bit = C.const((N >> k) & 1)
eq = C.g("AND", eq, C.g("NOT", C.xor(acc[k], bit)))
a_big = C.g("OR", a[1], a[2]) # a >= 2
b_big = C.g("OR", b[1], b[2])
out = C.g("AND", eq, C.g("AND", a_big, b_big))
return C, out
for N in [21, 13, 35]:
C, out = composite_circuit(N)
sols = []
for values in product([0, 1], repeat=6):
if C.evaluate(values, out):
a = values[0] + 2 * values[1] + 4 * values[2]
b = values[3] + 2 * values[4] + 4 * values[5]
sols.append((a, b))
print(f"N = {N}: circuit with {len(C.gates)} gates; satisfying inputs (a, b): {sols or 'none'}")N = 21: circuit with 165 gates; satisfying inputs (a, b): [(3, 7), (7, 3)]
N = 13: circuit with 165 gates; satisfying inputs (a, b): none
N = 35: circuit with 165 gates; satisfying inputs (a, b): [(5, 7), (7, 5)]
The circuit for 21 is satisfied exactly by the factorizations and ; the one for the prime 13 is unsatisfiable. For 35, the factors 5 and 7 fit in 3 bits, so it is satisfiable. A real reduction would compile a general verifier the same way; for -bit numbers the multiplier has gates, polynomial in the input length, while the brute-force search over its inputs takes evaluations.
Why this matters
Cook-Levin is the anchor of the theory. Every NP-completeness proof in the next lessons reduces from a problem already known to be NP-complete, and following the chain back always ends here. It also explains why circuit and formula satisfiability problems are the natural language of hardware verification: checking that two circuits are equivalent amounts to asking whether a "difference" circuit is satisfiable, and SAT solvers answer such questions daily for chip designs with millions of gates. Boolean circuits themselves are introduced in Boolean Circuits of Discrete Mathematics.
Test yourself
What is a certificate for CIRCUIT-SAT, and how long does checking it take?
- The circuit's gate list; exponential time
- A truth assignment to the inputs; linear time in the circuit size
- A proof of unsatisfiability; polynomial time
- The number of satisfying assignments; constant time
In the Cook-Levin construction, which part of the verifier's input becomes the circuit's inputs?
- The instance
- The certificate
- Both
- Neither: the circuit has no inputs
Why is the circuit of polynomial size?
- Because circuits are always small
- Because it has copies of a polynomial-size one-step circuit, and is polynomial
- Because the certificate is short
- Because it is built from NAND gates
The compiled COMPOSITE circuit for has no satisfying input. What does that say?
- The circuit is wrong
- 13 has no factorization with , so it is prime
- 13 is composite
- The search was too small