Principle:2akouwu Reverify Behavioral Equivalence
| Knowledge Sources | |
|---|---|
| Domains | Verification, Differential_Testing, Reverse_Engineering |
| Last Updated | 2026-10-24 12:00 GMT |
Overview
Verification technique that checks whether two implementations compute the same function by running both over a shared set of inputs and comparing their outputs — returning a concrete counterexample on any mismatch, or "equivalent" if all tested inputs agree.
Description
Behavioral Equivalence Testing is the practical, everyday answer to "did the rewrite / the AI's decompiled version / the optimized version preserve behavior?" Unlike formal proof (see Principle:2akouwu_Reverify_Proof_Grade_Equivalence), it does not prove equivalence for all inputs — it tests a finite set and reports what it found. A mismatch is a refutation with a concrete counterexample (the input, the reference output, and the candidate output). A match across all tested inputs is "equivalent" — but tested, not proven.
Two modes are supported:
- Source-level (functions_equiv_verify): Compile (or interpret) both the reference and candidate implementations, run them over shared integer inputs, and compare the integer outputs. The contract is argv-in (decimal integers) / one-int-out (stdout). Supports C (compiled) and Python (interpreted).
- Bytecode-level (behavioral_equiv): Emulate the original function's raw machine code and the candidate's raw code (or a high-level expression) over shared inputs using a CPU emulator (Unicorn). Compare register outputs. This tests decompiled code against the original bytes directly.
Both modes run inside a sandbox (POSIX rlimits / Windows Job Object) that caps CPU, memory, file size, and process count, with native execution off by default (REVERIFY_ALLOW_NATIVE_EXEC=1 to enable).
Usage
Use this principle when:
- You have a reference implementation and a candidate (e.g., an AI-generated decompilation) and want to check if they compute the same function.
- You need a concrete counterexample on mismatch, not just "not equivalent."
- You want to test at the source level (C/Python) or at the bytecode level (emulated machine code).
- Formal proof (Z3) is too slow or the expressions are too complex for SMT.
Theoretical Basis
The core operation is differential execution over shared inputs:
Where S is a finite set of test inputs. If any input distinguishes them, that input is a counterexample:
# Abstract algorithm (NOT real implementation)
for x in test_inputs:
ref_out = run(reference, x)
cand_out = run(candidate, x)
if ref_out is not None and ref_out != cand_out:
return REFUTED(counterexample={input: x, original: ref_out, candidate: cand_out})
if all tested:
return EQUIVALENT(tested=len(test_inputs)) # tested, not proven
Key properties:
- Shared inputs: Both implementations receive the same inputs. For source-level, inputs are generated by
gen_inputs(nargs, bits)(deterministic pseudo-random integer vectors). For bytecode-level, inputs are integer register values. - Concrete counterexample: A mismatch returns the exact input, the reference output, and the candidate output — a witness that can be inspected and replayed.
- Sandboxed execution: Every compile and run is confined by resource limits. The sandbox fails closed: a timeout, memory blow-up, or fork bomb is reported, not raised.
- Tested, not proven: "Equivalent" means no distinguishing input was found in the test set, not that none exists. For a proof guarantee, use Principle:2akouwu_Reverify_Proof_Grade_Equivalence.
Practical Guide
To apply behavioral equivalence testing:
- Provide a reference and a candidate: Both as source code (C or Python) or as raw machine code bytes.
- Generate or provide test inputs: By default,
gen_inputsproduces pseudo-random integer vectors. Pass custom inputs for targeted testing. - Enable native execution: Set
REVERIFY_ALLOW_NATIVE_EXEC=1(source-level mode compiles and runs code; this is off by default for safety). - Interpret the result:
pass= equivalent on all tested inputs;fail= refuted with counterexample;inconclusive= could not build or run one side. - Use the counterexample to debug the candidate — it pinpoints exactly where the divergence occurs.
Related Pages
Implemented By
- Implementation:2akouwu_Reverify_functions_equiv_verify
- Implementation:2akouwu_Reverify_compile_candidate
- Implementation:2akouwu_Reverify_run_sandboxed
- Implementation:2akouwu_Reverify_behavioral_equiv
Related Principles
- Principle:2akouwu_Reverify_Proof_Grade_Equivalence — formal proof for all inputs, complementing sampled testing.