TExcelTAD
Home › Selected Topics: FFT, Strings, Parallelism & NP-Completeness › Circuit Satisfiability and the Cook-Levin Idea

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 (∧\land), OR (∨\lor) and NOT (¬\lnot), 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.

CIRCUIT-SAT={⟨C⟩:C is a satisfiable Boolean combinational circuit}.\text{CIRCUIT-SAT} = \{ \langle C \rangle : C \text{ is a satisfiable Boolean combinational circuit} \}.

The size of a circuit is its number of gates plus wires. Trying all assignments of kk inputs takes 2k2^k 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 232^3 assignments:

python · try it yourself
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 (x1∨x2)∧¬x3∧¬(x1∧x2)(x_1 \lor x_2) \land \lnot x_3 \land \lnot(x_1 \land x_2): exactly one of x1,x2x_1, x_2 is 1 and x3=0x_3 = 0.

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 L∈NPL \in \mathrm{NP}, with a verifier A(x,y)A(x, y) that runs in time T(n)=O(nk)T(n) = O(n^k) on inputs of length nn and certificates of length O(nc)O(n^c). A computer executing AA 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 T(n)T(n) 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 MM of polynomial size. Now chain T(n)T(n) copies of MM:

text
 x (hard-wired)  -+
                  +-> [ config 0 ] -> M -> [ config 1 ] -> M -> ... -> M -> [ config T(n) ] -> output bit
 y (inputs)      -+

The first configuration contains the instance xx as constants and the certificate yy as the circuit's inputs; the output wire is the bit where AA leaves its answer. The resulting circuit CxC_x has size T(n)⋅poly(n)T(n) \cdot \mathrm{poly}(n), polynomial, and it can be written down in polynomial time given xx. By construction, Cx(y)=A(x,y)C_x(y) = A(x, y) for every yy, so

x∈L  ⟺  ∃y:A(x,y)=1  ⟺  Cx is satisfiable.x \in L \iff \exists y : A(x, y) = 1 \iff C_x \text{ is satisfiable}.

That is a polynomial-time reduction L≤PCIRCUIT-SATL \le_P \text{CIRCUIT-SAT}, for an arbitrary L∈NPL \in \mathrm{NP}. The technical work of the full proof is showing that the step circuit MM 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 a,b≥2a, b \ge 2 with ab=Nab = N". The program compiles this check for a fixed NN 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 NN, and tests that a≥2a \ge 2 and b≥2b \ge 2. Then it decides CIRCUIT-SAT by trying all inputs.

python · try it yourself
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 3⋅73 \cdot 7 and 7⋅37 \cdot 3; 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 β\beta-bit numbers the multiplier has O(β2)O(\beta^2) gates, polynomial in the input length, while the brute-force search over its inputs takes 22β2^{2\beta} 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?

  1. The circuit's gate list; exponential time
  2. A truth assignment to the inputs; linear time in the circuit size
  3. A proof of unsatisfiability; polynomial time
  4. The number of satisfying assignments; constant time

In the Cook-Levin construction, which part of the verifier's input becomes the circuit's inputs?

  1. The instance xx
  2. The certificate yy
  3. Both
  4. Neither: the circuit has no inputs

Why is the circuit CxC_x of polynomial size?

  1. Because circuits are always small
  2. Because it has T(n)T(n) copies of a polynomial-size one-step circuit, and T(n)T(n) is polynomial
  3. Because the certificate is short
  4. Because it is built from NAND gates

The compiled COMPOSITE circuit for N=13N = 13 has no satisfying input. What does that say?

  1. The circuit is wrong
  2. 13 has no factorization a⋅ba \cdot b with 2≤a,b≤72 \le a, b \le 7, so it is prime
  3. 13 is composite
  4. The search was too small