---
title: Formal verification
heading: Equivalence checking, model checking, bounded proofs and why simulation can't be exhaustive
eyebrow: S3 · Lesson 26 · Formal verification
xp: 50
---

::: hook
Simulation checks the cases someone thought to run. A 64-bit adder has $2^{128}$ input pairs; at a billion tests a second, trying them all would take $10^{22}$ years. Formal verification answers the question differently: it turns the design and a property into a logic problem and asks a solver whether any input sequence at all can break the property. When the solver says no, that's a proof for every case; when it says yes, it hands back the shortest failing trace. This lesson covers what the tools prove, how big their problems get, and where they stop.
:::

::: play
- The line is the time to simulate every input combination of an n-bit block exhaustively, at the rate set by the slider ($10^r$ vectors per second), in years.
- Each extra input bit doubles it: 40 bits take minutes at a billion vectors a second, 64 bits take centuries.
- No speedup rescues exhaustive simulation at real widths; proofs have to reason symbolically.
:::

```sim plot
view: graph
x: n = 10..128
y: Y = 1e-12..1e30 log
slider: r = 3..12 step 1 = 9
plot: 2^n/10^r/3.156e7 | label: years to test every input
plot: 1 + 0*n | dashed | label: one year
readout: years for 64 bits = 2^64/10^r/3.156e7 | digits: 3
```

::: predict
## A 32 × 32 multiplier is to be verified by simulating every input pair at $10^9$ vectors per second. How long would that take?

- [ ] About 4 seconds
- [ ] About 18 hours
- [x] About 585 years
- [ ] About $10^{22}$ years

::: reveal
Two 32-bit inputs make $2^{64} = 1.8 \times 10^{19}$ pairs; at $10^9$ a second that's $1.8 \times 10^{10}$ seconds, 585 years. A 64-bit multiplier would take $10^{22}$ years. Formal equivalence checking settles such questions in minutes by reasoning about all inputs at once.
:::
:::

::: nameit
## Equivalence checking

Two circuits (RTL and its synthesized netlist, or a netlist before and after an optimization) are joined into a **miter**: the same inputs drive both, and an OR of the XORs of their outputs says "they differ". A SAT solver searches for inputs making the miter 1; if none exist, the circuits are equivalent for every input. Sequential designs are matched register by register first, which is why synthesis keeps register names.

## Model checking

A property written as an assertion (lesson 25) is checked against every reachable state:
- **Safety:** something bad never happens (the FIFO never overflows). A violation is a finite trace.
- **Liveness:** something good eventually happens (every request is eventually granted).
- **Cover:** some situation can happen at all, which guards against over-constrained setups that make every assertion trivially true.

Input **assumptions** restrict the environment to legal behavior; too strong an assumption hides real bugs.

## Bounded model checking

Unroll the design for k cycles, a copy of its logic per cycle, and ask the solver for a trace from reset that violates the property within k steps. The problem grows linearly with k: roughly $k(F + G)$ variables for F flip-flops and G gates. BMC finds shallow bugs fast, but proves the property only up to depth k; a full proof needs k at least the design's **diameter** (the longest shortest path between reachable states), or **k-induction**, or abstraction.

## Why the state space matters

n flip-flops have $2^n$ states, usually far more than are reachable. Explicit search, visiting states one at a time, works up to tens of millions of states; symbolic methods (SAT, BDDs) handle far more, but bugs buried behind long counters remain hard.
:::

::: example
## A block with 2000 flip-flops and 50 000 gates is unrolled 30 cycles for bounded model checking. How large is the solver's problem?

::: solution
1. **Variables.** Every flip-flop and gate gets a variable in each of the 30 cycles:
   $$\begin{aligned} V &= 30 \times (2000 + 50\,000) \\ &= 1.56\ \text{million} \end{aligned}$$
2. **Clauses.** With about three clauses per gate, some 4.5 million clauses: routine for a modern SAT solver.
3. **Depth.** Doubling the depth doubles the problem, so BMC runs are usually limited to tens or hundreds of cycles.

**Answer:** about 1.56 million variables and 4.5 million clauses.
:::
:::

::: example
## A FIFO of depth 16 forgets to block a push when it's full. How deep must bounded model checking go to find the overflow, and what does a clean run to depth 10 prove?

::: solution
1. **Shortest failing trace.** It is 17 pushes after reset, so BMC must reach depth 17.
2. **What depth 10 proves.** A clean run to depth 10 proves only that no overflow happens in the first 10 cycles; it says nothing about deeper states.
3. **Choosing the depth.** That's why BMC depth is chosen from the design's structure, and why counters inside a design are often reduced (a FIFO of depth 4 behaves like one of depth 16 for most properties).

**Answer:** depth 17; a clean run to depth 10 proves only that the first 10 cycles are safe.
:::
:::

::: example
## A bug fires only when a 20-bit input equals one particular value. How many random tests give 99% confidence of hitting it, and how would formal verification find it?

::: solution
1. **Random tests.** Each hits the value with probability $2^{-20}$:
   $$\begin{aligned} n &= \frac{\ln 0.01}{\ln(1 - 2^{-20})} \\ &= 4.8\ \text{million} \end{aligned}$$
2. **Formal verification.** A SAT solver finds the triggering value directly, in one query, since it reasons about the condition rather than sampling inputs.

**Answer:** 4.8 million random tests, against one solver query.
:::
:::

::: check
kind: parts
given: n = 32 | 40 | 48 | 64; r = 1e6 | 1e9

## A block with {n} input bits is to be simulated exhaustively at {r} vectors per second.

::: part
answer: 2^n
tolerance: 0.5%

### How many input vectors are there?

::: misses
- [any] $2^n$.
:::
:::

::: part
answer: 2^n/r/3.156e7
unit: years
tolerance: 0.5%

### How long would it take?

::: misses
- [2^n/r] Convert seconds to years.
- [any] $2^n/\text{rate}$.
:::
:::
:::

::: check
kind: parts
given: F = 1000 | 10000; G = 10000 | 100000; k = 10 | 50

## A design with {F} flip-flops and {G} gates is unrolled {k} cycles for bounded model checking, with about three clauses per gate.

::: part
answer: k*(F + G)
tolerance: 0.5%

### About how many variables does the solver see?

::: misses
- [F + G] Every cycle gets its own copy.
- [any] $k(F + G)$.
:::
:::

::: part
answer: 3*k*G
tolerance: 0.5%

### About how many clauses?

::: misses
- [any] Three per gate per cycle.
:::
:::
:::

::: check
kind: parts
given: S = 8 | 12 | 16

## A one-hot state machine has {S} states, one flip-flop each.

::: part
answer: 2^S
tolerance: 0

### How many states can its flip-flops encode?

::: misses
- [any] $2^S$.
:::
:::

::: part
answer: S/2^S
tolerance: 0.5%

### What fraction of them are reachable from reset?

::: misses
- [any] S legal states out of $2^S$.
:::
:::
:::

::: check
kind: choice

## Bounded model checking to depth 40 finds no violation of a safety property. What has been shown?

- [ ] The property holds in every reachable state
- [x] No input sequence of 40 cycles or fewer from reset violates it; deeper states are unchecked unless 40 covers the design's diameter or an induction proof completes it
- [ ] The design has no bugs at all
- [ ] The property is satisfiable

::: misses
- [1] Only states within 40 steps of reset were explored.
- [3] Only the stated property was checked, and only to that depth.
- [4] A clean run means no violation was found, not that the property is merely possible.
:::
:::

::: check
kind: match

## Match each kind of property to an example.

- Safety :: the FIFO never overflows
- Liveness :: every request is eventually granted
- Cover :: the FIFO can become full
- Assumption :: the input valid signal never rises during reset
- [extra] the design meets timing at 1 GHz

::: misses
- [any] Safety forbids, liveness promises, cover asks if possible, assumptions restrict the inputs.
:::
:::

::: check
kind: derivation
vars: m = 1..20, c = 0.5..0.999

## A bug fires only for one value of an m-bit input; random tests pick that input uniformly.

::: step
answer: 2^(-m)

### What is the chance one test hits it?
:::

::: step
answer: ln(1 - c)/ln(1 - 2^(-m))

### How many tests give confidence c of hitting it?
:::

::: step
answer: -ln(1 - c)*2^m

### What is that approximately, for large m?
:::

::: right
Right. The tests needed double with every bit of the trigger condition: a solver doesn't care.
:::
:::

::: check
kind: python

## Write `bfs(init, step, bad, inputs)`: explore states breadth first from init, where step(state, inp) gives the next state for each input in inputs, and return (depth, count): depth is the length of the shortest input sequence reaching a state where bad(state) is true (or None if none is reachable), and count is the number of distinct states discovered.

```python starter
def bfs(init, step, bad, inputs):
    return (None, 1)
```

```python solution
from collections import deque

def bfs(init, step, bad, inputs):
    seen = {init: 0}
    q = deque([init])
    while q:
        s = q.popleft()
        if bad(s):
            return (seen[s], len(seen))
        for i in inputs:
            t = step(s, i)
            if t not in seen:
                seen[t] = seen[s] + 1
                q.append(t)
    return (None, len(seen))
```

```python tests
ops = ['push', 'pop', 'idle']
buggy = lambda c, i: c + 1 if i == 'push' else (c - 1 if i == 'pop' and c > 0 else c)
fixed = lambda c, i: c + 1 if i == 'push' and c < 16 else (c - 1 if i == 'pop' and c > 0 else c)
assert bfs(0, buggy, lambda c: c > 16, ops)[0] == 17, "The unguarded FIFO overflows after 17 pushes."
assert bfs(0, fixed, lambda c: c > 16, ops) == (None, 17), "The guarded one can't: all 17 reachable counts are safe."
assert bfs(0, lambda c, e: (c + 1) % 16 if e else c, lambda c: c == 13, [0, 1])[0] == 13, "A counter reaches 13 in 13 steps."
```

::: right
Right. Breadth-first search finds the shortest counterexample, exactly what a bounded model checker reports, and when nothing bad is reachable it has proved the property for this finite model.
:::
:::

::: explain
## Why can formal verification find a bug that months of random simulation missed?

::: model
Random simulation samples input sequences, and a bug that needs a rare combination, such as one particular value on a wide bus or a long, specific sequence of events, may have a chance of a billionth per test, so it's effectively never hit. A formal tool doesn't sample: it encodes the design and the negated property as a logic problem and asks a solver whether any input sequence satisfies it. The solver's search is guided by the structure of the logic, so a narrow trigger condition is no harder than a common one. If a violation exists within the explored depth, the solver finds it and returns the shortest trace, whatever its probability under random inputs.
:::
:::

::: practice
::: check
given: n = 36 | 40 | 48 | 56 | 64 | 72
answer: 2^n/1e9/3.156e7
unit: years
tolerance: 0.5%

## Exhaustive time for {n} input bits at $10^9$ per second?

::: misses
- [any] $2^n/10^9$ seconds.
:::
:::

::: check
given: F = 500 | 5000; G = 20000 | 80000; k = 20 | 100
answer: k*(F + G)
tolerance: 0.5%

## Variables for {F} flip-flops and {G} gates unrolled {k} cycles?

::: misses
- [any] $k(F + G)$.
:::
:::

::: check
given: m = 8 | 12 | 16 | 20 | 24 | 32
answer: ln(0.01)/ln(1 - 2^(-m))
tolerance: 0.5%

## Random tests for 99% confidence of hitting one {m}-bit value?

::: misses
- [any] $\ln(0.01)/\ln(1 - 2^{-m})$.
:::
:::
:::
