POL Labs
[1]Who we are[2]What we do[3]Method[4]Publications[5]Careers[6]Blog
Contact
← All posts·Labs

Layered formal verification of El Zorro, a propAMM on Ethereum

Lean for the pricing logic, Kani for the Rust floating point, Halmos and Certora for the contract: 92 machine-checked theorems, and where each tool stops.

Dipa·Nov 25, 2026·8 min read

This post describes the formal verification of El Zorro, a proprietary AMM on Ethereum. We verified two components: the pricing logic and the contract that settles swaps. The pricing logic is covered by 92 machine-checked theorems in Lean, the Rust floating-point code by bit-precise checks with Kani, and the Solidity contract by symbolic verification with Halmos and Certora.

None of these tools was sufficient on its own. Each reached its limit at a different point, and one of them, we found, models the square root incorrectly. Below we describe where each tool stopped, how we connected the results across layers, and which statements remain unproved.

Three kinds of arithmetic

Before a price reaches a trade, it is computed in three different number systems.

  1. Real numbers. The pricing rules are designed as formulas over R\mathbb{R}R: a spread that widens with volatility, a skew that leans against inventory.
  2. Floating point. The off-chain service that evaluates those formulas is written in Rust with f64, and every operation rounds.
  3. 256-bit integers. The contract that settles the swap works in Solidity integers, with floors and a mulDiv library.

Each of these suits a different kind of tool. Statements over the reals involve square roots, monotonicity and case analysis; this is routine for an interactive prover and out of reach for a bit-precise solver. Some of the same statements are false in f64, and only a bit-precise tool will detect it. For Solidity, solvers handle control flow well but nonlinear arithmetic poorly.

We therefore used one tool per layer, and tried to state explicitly what connects each layer to the next.

ToolLayerWhat it gives
Lean 4 with Mathlibthe formulas, over R\mathbb{R}R and N\mathbb{N}Nproofs that the rules mean what the documentation says
Kanithe Rust code, in f64every floating-point input checked, not sampled
Halmosthe Solidity contractproperties over all calldata
Certorathe Solidity contractrules and invariants over all reachable states

The pricing logic in Lean

We transcribed each pricing function from Rust into a Lean definition, citing the corresponding source line. The constraints enforced by the configuration parser became a Lean structure, so every theorem quantifies over exactly the configurations the system accepts.

The main result is an end-to-end theorem. For every accepted configuration, every inventory level and every volatility σ≥0\sigma \ge 0σ≥0 for which the base spread stays below 100%, we have

bid  <  mid  <  ask,\text{bid} \;<\; \text{mid} \;<\; \text{ask},bid<mid<ask,

and the inventory adjustment applies only to the side that would move the vault further from its target. Several supporting results accompany it. The step that converts buy and sell prices into a mid and a spread is an exact inversion; the inventory adjustment is bounded and vanishes inside the target band; and the simpler pricing classes are special cases of the main one.

In total, 58 theorems concern pricing. All of them build without sorry, and #print axioms on the main results lists only the three standard axioms propext, Quot.sound and Classical.choice.

We record one episode. Our first statement of a threshold lemma used the wrong constant, and Lean rejected it. A test suite would likely have accepted it, since none of our tests sat on the boundary.

Floating point with Kani

Kani checks Rust code against all possible inputs by translating it into a bit-precise model. We extracted the solver function verbatim, using a script that records the source hash and fails if the code changes.

The square root model in CBMC

Our first finding concerned the tool itself. CBMC, the engine underlying Kani, ships a library model of sqrt. The model brackets the root between two candidates and chooses one by comparing rounded differences. Since the squares being compared are themselves rounded, the result is not always the correctly rounded root required by IEEE 754.

For example, at d=4+3 ulpd = 4 + 3\,\mathrm{ulp}d=4+3ulp the IEEE root is 2+1 ulp{2} + 1\,\mathrm{ulp}2+1ulp, whereas the model returns 2+2 ulp{2} + 2\,\mathrm{ulp}2+2ulp. Over 200,000 samples in [2−110,16][2^{-110}, 16][2−110,16] the two disagreed 17% of the time, with an observed error of at most 0.75 ulp. A proof carried out with the native model is therefore a proof about a function slightly different from the one the hardware computes.

We replaced the model with a stub that accepts any result rrr satisfying, in exact 128-bit integer arithmetic,

(2mr−1)2  ≤  md 2 ed−2er+1077  ≤  (2mr+1)2,(2m_r - 1)^2 \;\le\; m_d \, 2^{\,e_d - 2e_r + 1077} \;\le\; (2m_r + 1)^2,(2mr​−1)2≤md​2ed​−2er​+1077≤(2mr​+1)2,

where mmm and eee denote the significand and biased exponent. The condition is equivalent to ∣r−d∣≤ulp(r)/2|r - \sqrt{d}| \le \mathrm{ulp}(r)/2∣r−d​∣≤ulp(r)/2. Since the square root of a double never lies exactly halfway between two doubles, there are no ties, and for normal inputs the condition determines rrr as the correctly rounded root. With this stub, a successful check is a proof about IEEE hardware and a failed check yields a genuine counterexample. It was also considerably faster, taking 30 to 260 seconds per property compared with hours under the native model.

Statements that hold over the reals but not in f64

With a correct square root, Kani proved that the solver never returns NaN or infinity for any input in range. It also refuted some statements that we had proved in Lean over the reals:

  • "The published spread is strictly positive" fails when the base spread sss is small enough that fl(1−s)=1\mathrm{fl}(1 - s) = 1fl(1−s)=1, in which case the spread rounds to zero.
  • "Buy is strictly below sell" fails in the same regime. In the worst case buy equals sell; an inverted quote never occurs.
  • "The published spread is at least the base spread" fails by 2−54{2}^{-54}2−54, about 5.6×10−17{5.6} \times 10^{-17}5.6×10−17, at ordinary values.

Under the restriction s≥10−12s \ge 10^{-12}s≥10−12, which is far below any spread in use, the first two statements are proved for every f64 input. We replayed every counterexample natively with the hardware square root.

The failures follow a recognizable pattern: strict inequalities become non-strict near the rounding unit, and bounds lose a few ulps. What Kani provides here is a precise account, consisting of exact counterexamples and exact ranges on which the statements are proved.

The Solidity contract

We fixed the properties before running either tool. They cover owner-only functions, pausing, registry consistency, conservation in swaps, and five statements about the swap arithmetic.

The properties about control flow were easy. Access control, pausing and conservation of balances in a swap were verified within seconds to minutes in both tools. The arithmetic was a different matter. Those statements involve floors of products of 256-bit values with a spread factor; Halmos returned no result at 256, 64 or 32 bits, and Certora closed only one of the five directly.

A few observations may be useful to others attempting something similar.

  • Halmos discards reverting paths. A pass therefore means "holds whenever the call succeeds". To check that passes were not vacuous, we wrote negative controls, deliberately broken versions of each property, and confirmed that all of them failed.
  • Most early Certora failures came from the model, not the contract. Unresolved library calls, unconstrained mocks and overly broad method filters accounted for most of them. None of these was a bug in the contract.
  • A true invariant need not be inductive. One counting invariant failed from a starting state that the contract can never reach. We resolved this by proving a stronger structural invariant in Lean and deriving the count from it.

Connecting the contract to a Lean model

Statements about ⌊xn/d⌋\lfloor x n / d \rfloor⌊xn/d⌋ are expensive to bit-blast but straightforward over the natural numbers. We wrote a Lean model of the contract's swap arithmetic and proved, in 26 theorems, that:

  • the output never exceeds its value at the mid price;
  • the output equals the exact floor;
  • a smaller fill receives at least its pro-rata share;
  • a round trip never returns more than it started with.

These results are only useful if the contract computes the same quantities as the model. To establish this, we summarized the math library's mulDiv, ceilDiv and sqrt in Certora as uninterpreted functions, and proved that the contract's functions return exactly the Lean expressions built from those same functions. A rule that holds for every such function holds in particular for the actual library.

Once the library is summarized, the nonlinear arithmetic no longer appears in the solver's problem, and each rule closes in one to two seconds. The Lean theorems then apply to the contract itself, under one stated assumption: that OpenZeppelin Math meets its specification.

Limitations

  • The link to the Rust code is by inspection. For the contract, the correspondence between Lean and code is proved. For the Rust pricer, it rests on a line-cited transcription together with Kani on the solver. The available tools for extracting Lean from Rust do not support floating point, and the pricer uses f64 throughout.
  • One floating-point bound remains open. We were not able to close the natural f64 version of "published spread at least base spread", with a slack of 2−51{2}^{-51}2−51, after 11 hours of solver time. A hand error analysis suggests that it holds.
  • Upstream inputs are assumed. Non-negative volatility and an inventory share in [0,1][0, 1][0,1] are supplied by libraries that we did not verify.
  • Out of scope. The factory contract, the oracle registry, non-standard tokens and the deployed bytecode.

Remarks

We close with some observations from this project.

  1. We found it best to prove the business logic over the reals, where it was designed. A side benefit is that the specification becomes readable to the people who designed the pricing.
  2. The gap to f64 can then be measured with a bit-precise tool. In our case it was present, small, and followed a regular pattern.
  3. The tool's models of library functions deserve an audit. Had we kept a square root that is off by one ulp, every native-mode proof would have been a proof about a different function.
  4. In our experience SMT solvers are well suited to control flow and poorly suited to nonlinear arithmetic. We proved the arithmetic in a proof assistant and used the solver only to show that the code matches the definitions.
  5. Much of the value came from writing the statements down. Saying exactly what "correct" means forced decisions that the documentation had left open, such as which parameter ranges are legal and which inequalities are strict.
ShareXLinkedIn
POL Finance
© 2026 POL Finance — All Rights Reserved