Implementation:2akouwu Reverify functions equiv verify
| Knowledge Sources | |
|---|---|
| Domains | Verification, Differential_Testing, Sandboxed_Execution |
| Last Updated | 2026-10-24 12:00 GMT |
Overview
Concrete tool for checking whether two implementations compute the same function by compiling/interpreting both, running over shared integer inputs, and comparing outputs with concrete counterexamples, provided by the Reverify library.
Description
The functions_equiv_verify function is the everyday "did the rewrite preserve behavior?" check. It takes a candidate and a reference implementation (both as source code), compiles (C) or syntax-checks (Python) both, generates or accepts shared integer inputs, runs both sandboxed, and compares integer outputs. A mismatch returns a fail with a counterexample (input, reference output, candidate output). All match returns pass — but tested, not proven. Off by default; requires REVERIFY_ALLOW_NATIVE_EXEC=1.
Usage
Import this function when you need to:
- Check if an AI-generated decompilation matches a reference implementation.
- Verify that a rewrite or optimization preserves behavior.
- Run the
reverify equivCLI subcommand. - Compare C or Python implementations over shared inputs.
Code Reference
Source Location
- Repository: Reverify
- File: reverify/exebench.py
- Lines: L231-303
Signature
def functions_equiv_verify(
candidate: str,
*,
reference: Optional[str] = None,
record: Optional[Dict[str, Any]] = None,
lang: str = "c",
nargs: int = 2,
inputs: Optional[Sequence[Sequence[int]]] = None,
cc: str = "gcc",
timeout: int = 30,
max_inputs: int = 40,
) -> Dict[str, Any]:
"""Do two implementations compute the same thing? Run both over shared
inputs, compare. Returns {status, passed, total, failures, detail}."""
Import
from reverify.exebench import functions_equiv_verify
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| candidate | str | Yes | Candidate source code |
| reference | str | No | Reference (trusted) implementation source. If None, must pass record. |
| record | Dict | No | Recorded I/O (for C only; delegates to exebench_verify) |
| lang | str | No | "c" (compiled) or "python" (interpreted). Default "c". |
| nargs | int | No | Number of integer args (default 2) |
| inputs | Sequence | No | Custom input cases (default: gen_inputs) |
| cc | str | No | C compiler name (default "gcc") |
| timeout | int | No | Compile/run timeout seconds (default 30) |
| max_inputs | int | No | Max test cases (default 40) |
Outputs
| Name | Type | Description |
|---|---|---|
| status | str | "pass" / "fail" / "inconclusive" |
| passed | int | Number of matching inputs |
| total | int | Number of compared inputs |
| failures | List[Dict] | Mismatches: {input, reference, candidate} |
| detail | str | Human-readable summary |
Usage Examples
C: Compare Two Implementations
import os
os.environ["REVERIFY_ALLOW_NATIVE_EXEC"] = "1" # required: runs code
from reverify.exebench import functions_equiv_verify
reference = """
int f(int a, int b) { return a * b + 1; }
"""
candidate = """
int f(int a, int b) { return (a + 0) * b + 1; }
"""
result = functions_equiv_verify(candidate, reference=reference, lang="c", nargs=2)
print(result["status"]) # "pass" — equivalent on all tested inputs
Python: Compare Two Implementations
result = functions_equiv_verify(
"def main(): import sys; a,b=int(sys.argv[1]),int(sys.argv[2]); print(a*b+1)",
reference="def main(): import sys; a,b=int(sys.argv[1]),int(sys.argv[2]); print(b*a+1)",
lang="python", nargs=2
)
CLI Usage
REVERIFY_ALLOW_NATIVE_EXEC=1 reverify equiv --reference ref.c --candidate cand.c
# Prints verdict and counterexample (if any); exits 0 (pass), 2 (fail), 3 (inconclusive)