Tridirectional Discriminating-Power Formal Verification of Smart Contract Reentrancy Defense Against Production-Deployed Solidity Source
Ray Iskander
cs.CR
Jun 1, 2026 · v1
TL;DR
Machine-checks OpenZeppelin reentrancy-guard correctness against a Lean 4 state-machine model of production Solidity.
Abstract
We present the first machine-checked correctness proof of the OpenZeppelin reentrancy-guard pattern against a Lean 4 state-machine model of production-deployed Solidity source. All thirteen theorems are machine-checked with zero sorry, zero user-introduced axioms, and an axiom footprint bounded by [propext] (a standard mathlib4 axiom), gated under continuous integration. Smart contract reentrancy has caused over US$500M in documented losses since 2016, with the DAO 2016 attack draining ~3.6M ETH and forcing the hard fork that split Ethereum. The OpenZeppelin ReentrancyGuard pattern is the de facto defense across production DeFi, yet no prior work has established its discriminating power: that the guard blocks attacks on vulnerable instances, preserves correct execution for non-attacking transactions, and distinguishes adjacent safe and vulnerable variants. Prior efforts formalized either guard correctness on toy contracts or attack feasibility on isolated instances - not both directions plus boundary cases against production source. We verify three production instantiations - DAO 2016, Compound v2, and Aave V3 flashLoan - plus a minimal-diff mutant of Aave V3's flashLoan (flashLoanVulnerable) isolating one security-critical difference, via mutation testing. The tridirectional structure pairs (a) attack reproduction of the DAO 2016 pattern, (b) a correctness proof for Compound v2, and (c) a boundary-case proof distinguishing Aave V3's CEI-correct flashLoan from the mutant. A capstone meta-theorem composes the three under a no-retrofit discipline, demonstrated at the first cross-protocol stress test (Compound v2 to Aave V3); broader-family portability is future work. Full Lean 4 source, CI config and reproduction commands are at
https://github.com/rayiskander2406/qanary-contracts, reproducible at v1.6-phase7-closure (substrate: v1.3-layer6-closure).
Problem
Smart contract reentrancy has caused over US$500M in losses since 2016, yet the widely-adopted OpenZeppelin ReentrancyGuard pattern lacked a machine-checked correctness proof against production-deployed Solidity source. Prior work formalized either guard correctness on toy contracts or attack feasibility on isolated instances, but never both directions plus boundary cases.
Approach
The authors build a Lean 4 state-machine model of production Solidity source and verify the OpenZeppelin reentrancy guard in three directions: negative (attack reproduction for DAO 2016), positive (correctness for Compound v2 cToken), and boundary (distinguishing Aave V3 flashLoan from a minimal-diff vulnerable mutant). Thirteen theorems are machine-checked with zero sorry and zero user-introduced axioms, with an axiom footprint bounded by propext. A capstone meta-theorem composes the three directions under a no-retrofit discipline demonstrated at a cross-protocol stress test from Compound v2 to Aave V3. CI gates per-theorem axiom-record verification on every push.
Results
All thirteen theorems pass Lean 4 kernel checking with zero sorry and only propext as axiom footprint. The tridirectional structure covers three production protocols (DAO 2016, Compound v2, Aave V3) plus a mutation-testing variant, with tagged-commit reproducibility at v1.6-phase7-closure.
| Layer | Theorems | Informal statement | Axiom record |
|---|
| 6-A | 1 + 5 | DAO 2016 attack trace derivable from contract source semantics | [propext]-only |
| 6-B | 1 + 2 | Compound v2 cToken correctly implements OZ guard pattern | [propext]-only |
| 6-C | 2 + 1 | Aave V3 flashLoan correct; flashLoanVulnerable fails; CEI lemma | [propext]-only |
Layer structure of the 13 theorems