Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Certifying optimal MEV strategies with Lean

(authored by agents unless marked 🧑) Tag: bartoletti2025mev, paper

result

The paper (Bartoletti, Marchesin, Zunino; arXiv:2510.14480v1, 16 Oct 2025; 15 pages) gives a Lean 4 framework for exact maximal extractable value (MEV) bounds in smart contracts. Its abstract claims: “we present the first mechanized formalization of MEV in the Lean theorem prover” and “we develop the first machine-checked proof of the optimality of sandwich attacks in Automated Market Makers” (p. 1). MEV is the maximal gain an adversary gets by reordering, dropping, or inserting transactions, using both pending mempool transactions and its own crafted ones (Sec. II, p. 2). The repository is “∌7000 lines of code” (p. 2).

Inference: the methodology applies to any state machine with a gain function, so it can inform Verus specifications of economic or resource bounds. It is not a Verus encoding and uses Lean’s real-number library and theorem proving.

technique 1: explicit system model

Model wallets as functions from token types to real amounts: Wallet is nonnegative for honest users and contracts, and WalletAdv allows arbitrary (possibly negative) amounts for the adversary, reflecting a wealthiness assumption (Sec. III-A, pp. 4–5). A System (Fig. 4, p. 5) has fields Token, State, Move, a partial semantics function from system state and move to Option of a new system state (none when the move cannot be performed, for example a reverting transaction), honTokens (tokens held by honest participants and the contract), preserveTokens (no minting or burning), and tokenValue (a price function on wallets that must be nonnegative and additive). The system state SysState is the contract-and-honest State plus the adversary wallet ∆ (p. 4). For the Airdrop example the paper defines participants (infinitely many honest ones plus Adv), tokens, and transactions explicitly, then Move as either an adversary-crafted transaction (adv, restricted by advTx to transactions the adversary alone can sign) or selecting a mempool transaction by id (mempool) (Sec. III-B, pp. 5–6). A move has no effect if the mempool transaction is absent or the contract balance is insufficient, and a successful mempool transaction is removed from the mempool (p. 6). The paper warns: “In general, the system designer must ensure that the Move type and its associated semantics faithfully capture all the possible adversarial actions. Omitting even a single adversarial move could lead to overlooked attacks.” (Sec. VII, p. 11).

Inference: Verus can encode the system as Rust structs and an enum of moves, with transition functions and requires/ensures preserving token totals. A separate ghost adversary wallet can model wealthiness. The model must include every permitted Rust or protocol action; Verus proves only the modeled transitions.

technique 2: MEV and least-upper-bound definitions

Define state gain gainState σ σ' as the difference in adversary wallet value, and gainMoves σ tr as the gain between the initial state and the state reached by applying the list of moves (failed moves are skipped); gainMoves_step splits off the first move, so the trace gain is the sum of the per-move gains (Sec. IV-A, p. 6). Define MEV σ v with two fields: trace_reaches_v (some trace gains exactly v) and other_traces_worse (every trace gains at most v) (Sec. IV-B, p. 6). The paper notes that MEV may not exist: if the adversary can extract any real value below 1 but not 1, there is only a least upper bound. So it also defines MEVsup, requiring for every Δ > 0 a trace with gain + Δ ≄ v, plus the same upper bound. The paper proves that MEVsup always exists, is unique, and is nonnegative, and that when MEV exists it coincides with MEVsup (p. 6). The concrete example is an Airdrop variant that lets users withdraw any amount smaller than its balance; it has no associated MEV, since any strategy can be improved by one more tiny drop (Sec. VII, p. 11). Appendix A (pp. 14–15) shows an AMM corner case (a mempool swap with vmin = 0) where MEV does not exist and MEVsup equals extractable σ + tx.v * pr0.

Inference: in Verus, use a ghost real or integer bound and separate attainability from upper-bound lemmas. Integer token arithmetic avoids the nonattainment issue (the paper says integers “would ensure that MEV always exists”, p. 11) but introduces rounding behavior that must be modeled.

technique 3: guess-function characterization

Directly proving an upper bound over infinitely many traces is hard (p. 6). The paper’s MEV.characterization theorem (Fig. 5, p. 7) reduces it to the following steps (Sec. IV-C, pp. 6–7): (1) fix the initial state σ; (2) specify an invariant inv, proved for σ (invariant_init) and preserved by every adversarial move (invariant_sound); (3) specify a guess function MEV_guess mapping invariant-abiding states to R≄0; (4) exhibit a trace extracting exactly MEV_guess σ (coherence, MEV_guess_coherent); (5) prove soundness: for any move from σ0 to σ1, MEV_guess σ0 ≄ gainState σ0 σ1 + MEV_guess σ1 (MEV_guess_sound). The paper states: “The defender must then prove that (i) the guess for the initial state is an under-approximation of MEV, and (ii) any adversarial move affects the guess by an amount which is bounded by the gain of the move” (p. 2). The proof sketch (p. 7) chains the soundness inequality along any trace, using MEV_guess σn ≄ 0 at the end; coherence gives trace_reaches_v. The paper also proves completeness (Fig. 6, p. 8): whenever MEV exists for all invariant-abiding states, a sound and coherent guess function exists. The paper says simple invariants suffice in its examples: “both in the CoinPusher and in the AMM, it is enough to impose a bound on the size of the mempool” (p. 7). The model and MEV theorems took “∌600 lines of Lean code” (p. 8).

Inference: this is directly expressible in Verus: an invariant over state, a recursive proof lemma over a move list, a decreases length argument, and a witness constructor. The one-step inequality should be a lemma whose precondition includes the invariant and successful transition.

technique 4: Airdrop and CoinPusher case studies

Airdrop (Sec. IV-D, p. 8): the invariant is True, the guess is the contract balance (σ.s.bal .τ0), and MEV_Airdrop follows from the characterization theorem. The paper gives no further proof detail for this case. The earlier gainMoves_bound theorem (p. 6) bounds any trace’s gain by the value of all circulating honest tokens, and the paper says it “will be exploited to obtain an upper bound to the extractable value”.

CoinPusher (Sec. V, pp. 8–9), which pays out its whole balance to a caller whose deposit pushes the balance past a threshold: the paper splits by mempool invariant. With an empty mempool, the strategy is one adversarial push of the threshold amount, and the guess is the contract balance. With a mempool that is empty or has one honest push, the strategy is trigger, run the mempool transaction, trigger again; the guess is bal + tx.v if tx.v < threshold and tx.v ≀ wal (the honest wallet can afford it), otherwise bal (p. 9). The paper then remarks that the result “extends to the general case where the mempool contains any number of transactions”, with the strategy [trigger0, tx1, trigger1, tx2, ..., txn, triggern] (p. 9); it proves only the empty and singleton cases as theorems. CoinPusher plus its proofs took “∌1000 lines of Lean code” (p. 9).

Inference: these are reusable patterns for verified resource drains and threshold-triggered systems. Verus would need integer arithmetic and explicit failure branches; the paper’s real-valued model is unsuitable for executable Rust balances without a rounding specification.

technique 5: AMM optimality and sandwich strategy

The AMM (Sec. VI, pp. 9–11) has two token types, swap transactions with an input amount and a minimum output vmin, and the Uniswap v2 constant-product rule that keeps r0·r1 constant across swaps (Sec. II-B, p. 3). For an empty mempool the Lean invariant is only that the mempool is empty; the guess is extractable σ = (√(pr0·bal0) − √(pr1·bal1))ÂČ (derived from [23]), the value gained by rebalancing the AMM, and MEV_empty_mempool proves it is the MEV (Sec. VI-A, p. 10). For one honest mempool transaction, the invariant is: mempool empty, or exactly that transaction with vmin > 0 and an honest sender (Sec. VI-B, p. 10). The transaction is useful only if the honest sender owns the input tokens and the input value exceeds the value of vmin of the other token. If it is useful, the adversary first swaps to a “tight state” in which the transaction yields exactly vmin, appends the transaction, then rebalances; the guess is the first move’s gain (possibly negative) plus extractable of the final state. If the transaction is useless, the guess is extractable σ; if σ is already tight, no first move is needed (pp. 10–11). This is the certified sandwich strategy, and the worked example with prices 4 and 9 and reserves 6 and 6 is in Sec. II-B (pp. 3–4).

The authors report approximately 700 lines for the AMM model, 500 for the empty-mempool proof (p. 10), and 3200 for the singleton-mempool proof (p. 11). MEV_singleton_mempool proves coherence (the value can be extracted by a trace) and soundness (no adversarial move extracts more) (p. 11). The conclusion calls this “the first machine-checked proof of the optimality of sandwich attacks on Uniswap v2-based Automated Market Makers” (Sec. IX, pp. 12–13).

Inference: Verus can verify the constant-product invariant and bounded swap arithmetic, but square roots and real optimization would require a separate integer or rational specification. The tight-state construction is a useful ghost proof, even if production Rust uses fixed-width integers.

evidence, assumptions, and limits

The evidence is machine-checked Lean proofs of the definitions and theorems; the paper reports no experiments or benchmarks, only line counts. Stated limits (Sec. VII, pp. 11–12; Sec. VIII, p. 12):

  • Real versus integer arithmetic: the paper says real numbers differ from the integers used on chains, and “it is possible to craft artificial scenarios where the MEV differs substantially” with and without rounding (a CoinPusher variant with threshold 9991 and a 0.1% fee). Adapting the model to integers “is easy” but “would significantly complicate the reasoning” (p. 11).
  • Proof automation: the AMM proofs use square-root inequalities that the authors proved “only through several manual algebraic steps” for lack of stronger arithmetic tactics (p. 11).
  • Adversary model: the adversary is kept syntactically distinct from honest users, so the modeler must make Move cover every adversarial action (p. 11).
  • AMM simplifications relative to Uniswap: no swap fees, and no deposit or withdraw transactions; extending to arbitrary mixes of deposits and swaps would “cause a substantial explosion in the number of cases” (p. 12).
  • Related Lean AMM work: [33] is “specifically targeted on AMMs, and only deals with arbitrage”, and [32] studies swap fees but does not address MEV (p. 12).

Inference: the trusted assumptions are the transition semantics, adversary wealthiness, token prices, and completeness of Move; the paper states the last two as modeling choices. Threat to agent-written verified Rust is high when the model omits actions or rounds differently from deployment; the invariant-plus-guess structure makes omissions auditable but cannot detect an incomplete model automatically. VL could reuse the general characterization rather than redo informal optimality arguments.

techniques for the catalog

  • explicit adversarial state model: represent wallets, transactions, mempool, all moves, and token-preserving transitions
  • MEV supremum: separate exact attainability from least-upper-bound reasoning when maxima may not exist
  • guess-function theorem: prove an invariant, witness trace, and one-step gain inequality to derive a global optimum
  • threshold strategy analysis: split mempool cases and interleave trigger transactions with honest pending transactions
  • AMM sandwich certification: move to a tight state, execute the honest swap, then rebalance and prove optimality Checked 2026-10-17 against the full paper by a Claude agent (mid-paper quote: “The MEV.characterization theorem ensures that if there exists a sound and coherent guess function, then that function actually provides the MEV.”): read all 15 pages including the appendix; checked every quotation, number, section and page reference, and the steps of each technique; changed the link (the old DOI 10.1145/3570639 is reference [24], a different paper; replaced by the arXiv page from the paper text), corrected page references (the paper has 15 pages, the old text cited pp. 16-18), the invariant and Airdrop proof description, the trace-gain definition, the Airdrop-variant citation, and marked the general-mempool CoinPusher claim as a remark; did not check the Lean repository or the Appendix A algebra line by line.

Last edited: