Principle:2akouwu Reverify Proof Grade Equivalence
| Knowledge Sources | |
|---|---|
| Domains | Formal_Verification, SMT_Solving, Reverse_Engineering |
| Last Updated | 2026-10-24 12:00 GMT |
Overview
Formal verification technique that proves two integer expressions are equal for all possible inputs using an SMT solver (Z3 bit-vector logic), rather than testing a finite sample — yielding a "proven" verdict with no counterexample, or a "refuted" verdict with a distinguishing input.
Description
Proof-Grade Equivalence is the complement to behavioral testing (see Principle:2akouwu_Reverify_Behavioral_Equivalence). Where behavioral testing runs both implementations over a finite set of inputs and can only say "no counterexample found," proof-grade equivalence asks an SMT solver to prove that no distinguishing input exists at all. If the solver returns UNSAT (the negation of inequality is unsatisfiable), the expressions are proven equivalent for all inputs. If the solver returns SAT, it produces a model — a concrete counterexample that distinguishes them.
The method works on integer expressions over bit-vector variables (x0, x1, x2, ...). Expressions are translated into Z3 bit-vector terms, and the solver checks expr_a != expr_b. This covers arithmetic, bitwise, and comparison operations on fixed-width integers.
The key advantage over sampling: "proven" is a guarantee, not a probabilistic statement. The key limitation: it only works on expressions (not arbitrary control flow), and SMT solving can be slow or return "unknown" on complex formulas.
Usage
Use this principle when:
- You need a guarantee that two expressions are equivalent for all inputs, not just a tested sample.
- You have integer expressions (arithmetic, bitwise) that fit Z3's bit-vector logic.
- You want a concrete counterexample when expressions are not equivalent.
- Behavioral testing found no mismatch but you need certainty (e.g., for a security-critical rewrite).
Theoretical Basis
The core operation is an SMT query over bit-vectors:
- If
check = unsat: no input makesa ≠ btrue → proven equivalent for all inputs. - If
check = sat: a model exists → refuted, the model is a counterexample. - If
check = unknown: inconclusive (solver timed out or could not decide).
# Abstract algorithm (NOT real implementation)
solver = z3.Solver()
vars = [z3.BitVec(f"x{i}", bits) for i in range(nvars)]
fa = translate_to_z3(expr_a, vars)
fb = translate_to_z3(expr_b, vars)
solver.add(fa != fb)
result = solver.check()
if result == unsat:
return PROVEN # no distinguishing input exists
elif result == sat:
return REFUTED(counterexample=solver.model()) # concrete witness
else:
return INCONCLUSIVE
Key properties:
- Bit-vector semantics: Operations are over fixed-width integers (default 64-bit), matching machine arithmetic including overflow wrap-around.
- Universal quantification: UNSAT means the inequality holds for no assignment — equivalent for all inputs.
- Counterexample extraction: SAT produces a model with concrete values for each variable, giving a witness that can be replayed.
- Expression-only: This proves equivalence of expressions (no loops, no side effects). For control-flow equivalence, use behavioral testing.
Practical Guide
To apply proof-grade equivalence:
- Provide two integer expressions over variables x0, x1, x2, ... (e.g.,
"x0 + x1"vs"x1 + x0"). - Set the bit width (default 64). The width must match the architecture of the original code.
- Let Z3 count the variables or specify
nvarsexplicitly. - Interpret the result:
proven= guaranteed equivalent;refuted= not equivalent with counterexample;inconclusive= solver could not decide. - Install Z3:
pip install z3-solver(optional dependency; without it the result is always inconclusive).
Related Pages
Implemented By
Related Principles
- Principle:2akouwu_Reverify_Behavioral_Equivalence — sampled testing complement; use proof-grade when a guarantee is needed.