Implementation:2akouwu Reverify behavioral equiv
| Knowledge Sources | |
|---|---|
| Domains | Verification, CPU_Emulation, Differential_Testing |
| Last Updated | 2026-10-24 12:00 GMT |
Overview
Concrete tool for testing bytecode-level equivalence by emulating original and candidate machine code over shared inputs with Unicorn, provided by the Reverify library.
Description
The behavioral_equiv function runs original function bytes and a candidate (raw bytes or a high-level expression) over shared integer inputs using Unicorn CPU emulation, and compares register outputs. A mismatch returns refuted with a counterexample (input, original output, candidate output). All match returns equivalent — but tested, not proven. Requires Unicorn (pip install unicorn).
Usage
Import this function when you need to:
- Check if decompiled candidate code matches the original function bytes.
- Compare original machine code against a candidate expression at the emulation level.
- Run the
reverify equivCLI subcommand for bytecode comparison.
Code Reference
Source Location
- Repository: Reverify
- File: reverify/behavior.py
- Lines: L204-271
Signature
def behavioral_equiv(
original_code: bytes,
*,
candidate_code: Optional[bytes] = None,
expr: Optional[str] = None,
nargs: int = 2,
inputs: Optional[Sequence[Sequence[int]]] = None,
arch: str = "x86_64",
bits: Optional[int] = None,
arg_regs: Optional[List[str]] = None,
ret_reg: Optional[str] = None,
max_inputs: int = 40,
) -> Dict[str, Any]:
"""Run original and candidate over the same inputs; compare outputs.
Returns {status: 'equivalent'|'refuted'|'inconclusive', tested,
counterexample?, detail}."""
Import
from reverify.behavior import behavioral_equiv
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| original_code | bytes | Yes | Original function machine code bytes |
| candidate_code | bytes | No | Candidate machine code bytes (or pass expr) |
| expr | str | No | Candidate expression string (e.g. "x0 + x1") |
| nargs | int | No | Number of integer args (default 2) |
| inputs | Sequence | No | Custom input cases (default: gen_inputs) |
| arch | str | No | Architecture (default "x86_64") |
| bits | int | No | Bit width (default: derived from arch) |
| max_inputs | int | No | Max test cases (default 40) |
Outputs
| Name | Type | Description |
|---|---|---|
| status | str | "equivalent" / "refuted" / "inconclusive" |
| tested | int | Number of inputs tested |
| counterexample | Dict | On refuted: {input, original, candidate} |
| detail | str | Human-readable summary |
Usage Examples
Compare Bytes Against Expression
from reverify.behavior import behavioral_equiv
original = b"\x01\xd0\xc3" # add eax, edx; ret (x86: x0 + x1)
result = behavioral_equiv(original, expr="x0 + x1", arch="x86_64", nargs=2)
print(result["status"]) # "equivalent"
Compare Bytes Against Bytes
result = behavioral_equiv(original_bytes, candidate_code=candidate_bytes, arch="x86_64")
if result["status"] == "refuted":
ce = result["counterexample"]
print(f"Counterexample: input={ce['input']}, original={ce['original']}, candidate={ce['candidate']}")