Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:2akouwu Reverify Behavioral Equivalence

From Leeroopedia
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:

equiv(f,g)=∀x∈S:f(x)=g(x)

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:

  1. Provide a reference and a candidate: Both as source code (C or Python) or as raw machine code bytes.
  2. Generate or provide test inputs: By default, gen_inputs produces pseudo-random integer vectors. Pass custom inputs for targeted testing.
  3. Enable native execution: Set REVERIFY_ALLOW_NATIVE_EXEC=1 (source-level mode compiles and runs code; this is off by default for safety).
  4. Interpret the result: pass = equivalent on all tested inputs; fail = refuted with counterexample; inconclusive = could not build or run one side.
  5. Use the counterexample to debug the candidate — it pinpoints exactly where the divergence occurs.

Related Pages

Implemented By

Related Principles

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment