Consider a two-coin plain Curve StableSwap-NG pool. Across any sequence of calls to the pool, everyone who calls the pool—counted together as a single group—ends the run with at most
I, plus2U, twice the value of one smallest unit of each coin (U), which covers rounding of LP claims and is negligible for 18-decimal coins (4 × 10⁻¹⁸ coin), plusπ, which is zero whenever they retain their tokens)more than they started with, once anything borrowed from outside parties is repaid, and never more than the starting value of the pool's reserves. All values are measured at the pool's fixed initial rates, which price one whole coin of either kind at par. For a balanced pool, I = 0; for an imbalanced pool, I is the invariant gap V₀ − D*—the pre-fee arbitrage value at the peg of trading the pool back to 50/50—about 206 coins at a 60/40 split and 2,762 coins at 80/20 in a million-coin pool with amplification A = 100. In a balanced million-coin pool of two 18-decimal coins, callers who start with one coin and borrow nothing can extract at most about 0.0001 coin in total over 100 pool calls, as long as the passive LPs keep their LP tokens (π = 0).
This guarantee is a theorem, bounded_no_theft_readable, machine-checked in HOL4 against the pool's Vyper source (CurveStableSwapNG.vy) executed in a formal semantics of Vyper. It holds for every finite run: any sequence of calls to any of the pool's 54 public functions—successful or reverting, with arbitrary arguments—interleaved with external token transfers that satisfy the assumptions below. Its second conjunct states the dual guarantee for passive liquidity providers: their collective share of the pool's reserves drops by at most I plus the same per-call rounding allowance, U and π. The theorem makes no assumption about LP allowances or permits: because only attackers call the pool during the run, a passive holder's LP tokens can leave the protected group only if an attacker pulls them via transferFrom under an allowance or permit the holder granted, and any value transferred that way is accounted for in π.
What the bound does and does not cover:
A stays constant across the trace.exchange_received in a later transaction, an attacker can front-run the second step; the bound accounts for that by counting the outsider's direct transfer as borrowed value. Integrators should perform the transfer and the exchange_received call in a single transaction, or call exchange.The sections below walk through the execution model, the formal theorem and its sixteen antecedents, each term of the bound, the initial imbalance I, and the scope limits. Two companion pages accompany this overview:
The pool. We model a single two-coin Curve StableSwap-NG pool configured with plain coins. Pool calls execute the contract's Vyper source (CurveStableSwapNG.vy) in vyper-hol's definitional interpreter (call_external), while the two coins and the factory are arbitrary contracts whose behavior on pool calls satisfies the honesty hypotheses below.
LP tokens. The pool issues its own ERC-20 liquidity token: depositing coins mints LP tokens and burning LP tokens withdraws coins. We refer to the total number of LP tokens in existence as the supply.
A run. Starting from an initial machine state am0, a run executes a finite list of steps in order, where each step is either:
The final machine state am_end is produced by executing the steps from am0, rather than supplied as a separate free state.
Roles. A setup record s identifies the pool address, the set of attackers, and the set of protected LP holders. Every address that calls the pool during the run must belong to the attacker set, and all attackers are accounted for in aggregate so profit cannot be hidden by splitting calls across sybil addresses. The protected holders represent the passive liquidity providers: the passive tranche is the set of all LP tokens not held by attackers at am0, and the bound tracks how much of that tranche the protected holders retain (see π below). For coin transfers outside the pool, every address that is neither an attacker nor the pool is an outsider (including lenders, the protocol fee receiver, protected LP holders, and other token holders).
Value. Each coin i is valued at a constant price pᵢ, defined as the value of one smallest indivisible unit (wei) of coin i in normalized value units: the pool's initial stored rate stored_rates()[i] at am0 divided by 10¹⁸ (start_price). Because Curve StableSwap-NG factories set the stored rate of a plain coin with d decimals to 10^(36−d), its unit price is pᵢ = 10^(18−d) and one whole coin of either token is worth exactly 10¹⁸ value units. All numerical examples on this page follow that convention.
Wealth. The attackers' collective wealth at any machine (attacker_wealth) is the value of the coins they hold directly plus the value of their LP claim on the pool: when attackers hold L LP tokens out of total supply S against pool reserves r₀ and r₁, their LP claim is p₀ × ⌊L × r₀ / S⌋ + p₁ × ⌊L × r₁ / S⌋—the exact value of the coins a proportional withdrawal (remove_liquidity) would return, rounded down. Coins sitting inside the pool count toward attacker wealth solely through this claim. Both the reserves rᵢ (which exclude admin fees) and the LP quantities S and L are read through the pool's own public views balances(i), totalSupply(), and balanceOf(a), each evaluated as a standalone read-only transaction (pool_view). Attackers' direct coin balances are read through each coin's own balanceOf(a) method as invoked by the pool (coin_holding), with reverting queries counted as 0.
Assumptions and implication antecedents (16):
1. coins = state0.immutables.coins 2. am_end = LAST (ordinary_machines am0 steps) 3. wealth0 = attacker_wealth s coins am0 am0 4. borrowed = total_borrowed s coins am0 steps 5. rebalancing_arb = initial_imbalance s.pool am0 6. slack = pool_calls steps * (SQRT (pool_value s.pool am0 + wealth0 + borrowed + 5) + 13) 7. tranche = passive_tranche s am0 8. pulled = pulled_lps s am0 steps 9. pool_ready s am0 state0 ps0 (trace_K steps) 10. amp_fixed s.pool am0 steps 11. honest_contracts s am0 coins state0.immutables.factory steps 12. coins_answer_balanceOf s am0 coins 13. threat_model s steps 14. protected_start s am0 15. outside_steps s coins am0 steps 16. storage_slots_separate steps
Conclusion:
attacker_wealth s coins am0 am_end + total_paid_out s coins am0 steps ≤ wealth0 + MIN (start_value s.pool am0 0 + start_value s.pool am0 1) (rebalancing_arb + 2 * unit_value_sum s.pool am0 + slack + pulled) + borrowed ∧ pool_lp_claim s.pool am0 tranche am0 ≤ pool_lp_claim s.pool am0 tranche am_end + rebalancing_arb + unit_value_sum s.pool am0 + slack + pulled
Free variables:
coins : 160 word list state0 : CurveStableSwapNG_state am_end : abstract_machine am0 : abstract_machine steps : ordinary_step list wealth0 : num s : theft_setup borrowed : num rebalancing_arb : num slack : num tranche : num pulled : num ps0 : pool_state
The theorem (bounded_no_theft_readable, in theory boundedNoTheft, stableswap-ng/bridge/boundedNoTheftScript.sml) is proved with zero admitted steps or oracles. All arithmetic is over natural numbers, where integer division and SQRT round down.
Each antecedent is summarized below; the assumptions section of the theorem dossier gives the exact HOL4 definitions alongside what each condition requires, why it cannot be dropped, and which internal lemmas pin it.
coins = state0.immutables.coins and the seven equations following it merely bind short names to the coin list and the quantities repeated in the conclusion (am_end, wealth0, borrowed, rebalancing_arb, slack, tranche, pulled; see the table below), so every run satisfies them by instantiation.pool_ready: at am0, the pool is a well-formed, funded two-coin plain pool: its storage decodes cleanly, its transient layout provides a reentrancy-lock slot, its public getters report fee and amplification parameters within factory bounds and rates that are positive multiples of 10¹⁸, its internal accounting balances, its LP supply and both reserves are positive, its actual token custody matches its recorded balances, and its two coin addresses are distinct.amp_fixed: the amplification coefficient A is not ramping during the run—either initial_A() and future_A() already agree at am0, or every pool call occurs at or after future_A_time().honest_contracts: whenever the pool calls either coin, a successful transfer or transferFrom moves the exact requested amount between the specified addresses and nothing else, a successful balanceOf reports the true balance, and the call leaves the pool's storage and the dependency invariant intact. Whenever the pool calls the factory, admin never returns an attacker address and fee_receiver returns either zero or an address that is neither an attacker nor the pool.coins_answer_balanceOf: at am0, each coin's balanceOf succeeds and returns a uint256 for every address.threat_model: only attacker addresses call the pool during the run, every pool call targets this pool, and the pool itself is not in the attacker set.protected_start: at am0, the protected holders collectively hold the entire passive tranche—every LP token not held by an attacker at the start is held by a protected address.outside_steps: steps outside the pool preserve the pool's storage, preserve the coins' and factory's bytecode and invariant, preserve each coin's total supply, and never reduce the pool's balance of either coin.storage_slots_separate: the Keccak-256 storage slots of the mapping entries touched by the run do not collide.Crucially, nothing about how the pool's own code behaves is assumed: the effect of every pool entrypoint, the reserve accounting across calls, and the constancy of A are all proved from the Vyper source semantics. Likewise, nothing is assumed about how protected LP tokens move during the run—any loss of protected LP tokens is captured quantitatively by π.
When the protected holders retain at least the initial passive tranche at every step of the run (passive_lps) rather than only at am0, π = 0 (passive_lps_pulled_zero), and the corollary bounded_no_theft_readable_passive states the same bounds with π omitted.
Written in terms of the callers' net wealth gain (attacker_wealth at am_end minus W₀), the first conjunct of the conclusion states:
gain ≤ min(V₀, I + n × (⌊√(V + W₀ + x + 5)⌋ + 13) + 2U + π) + (x − y)
where each symbol corresponds to a term in the formal statement:
| Term | Name in the theorem | Meaning |
|---|---|---|
W₀ |
wealth0 (attacker_wealth s coins am0 am0) |
the attackers' combined wealth at the start |
V₀ |
start_value s.pool am0 0 + start_value s.pool am0 1 |
the starting value of the pool's reserves (excluding admin fees): beyond net borrowing, the attackers can never gain more than all the reserves in the pool |
I |
rebalancing_arb (initial_imbalance s.pool am0) |
the pool's initial imbalance V₀ − D*: the pre-fee value at start prices of trading the pool back to balance (below) |
n |
pool_calls steps |
the number of pool calls in the run, including reverting calls (outside steps do not increment n) |
V |
pool_value s.pool am0 |
the total starting value of the coins held by the pool, including admin fees |
x |
borrowed (total_borrowed s coins am0 steps) |
gross value lost by outsiders in outside steps—flash loans or transfers to attackers, as well as coins sent directly to the pool by outsiders |
y |
total_paid_out s coins am0 steps |
gross value gained by outsiders in outside steps—loan repayments, interest/fees, or transfers from attackers |
U |
unit_value_sum s.pool am0 |
p₀ + p₁, the combined value of one smallest unit (wei) of each coin |
π |
pulled (pulled_lps s am0 steps) |
the start-price value of any LP tokens lost by the protected holders during the run (below) |
n × (⌊√(V + W₀ + x + 5)⌋ + 13), named slack in the theorem, is the cumulative per-call rounding allowance: each pool call can round in the callers' favor by at most the integer square root of the total value at stake plus 13 value units. In a million-coin pool (V = 10²⁴ value units), √(10²⁴) is 10¹² value units, or 10⁻⁶ coin per call; borrowing another million coins (x = 10²⁴) raises that per-call allowance only to about 1.4 × 10⁻⁶ coin.2U accounts for rounding down the two LP reserve claims—the attackers' claim at am0 and the passive tranche's claim at am_end—and equals 4 value units (4 × 10⁻¹⁸ coin) for two 18-decimal coins.x − y is the net value transferred from outsiders to attackers outside the pool. Borrowing x from a lender raises the right-hand side by x, and repaying the principal plus a fee f(x) (y ≥ x + f(x)) reduces the net gain cap by f(x): gain ≤ min(V₀, I + n × (…) + 2U + π) − f(x). Keeping x on the right and y on the left lets the single inequality cover runs with arbitrary borrowing and partial or full repayment without extra hypotheses.πTo track whether passive liquidity providers lose LP tokens during the run without restricting how LP tokens may be transferred, the theorem defines the protected holders' running floor (protected_floor). It starts at the initial passive tranche H and, after each step, updates to the minimum of its previous value and the protected holders' current summed balanceOf on the pool. Because it is a running minimum, returning LP tokens to protected holders later does not raise the floor. Whenever the floor drops, the drop is valued at the pool reserves and total supply immediately before that step (pool_lp_value), rounded up at the initial prices: losing L LP tokens from a pool with reserves r₀, r₁ and supply S adds ⌈L × (p₀ × r₀ + p₁ × r₁) / S⌉ to π (pulled_lps). Any shortfall below H at am0 is valued at am0, and any drop during step k is valued at machine k just before step k executes.
Whenever the protected holders keep at least H LP tokens at every step from am0 onward, the floor never drops and π = 0. Because only attackers call the pool during the run, a protected holder cannot call transfer or remove_liquidity directly; the floor can drop during the run only if an attacker pulls LP tokens from a protected holder via transferFrom using an allowance or EIP-2612 permit. Even then, the proof does not need to inspect how the tokens moved—it simply prices the drop in the protected holders' collective balance.
The initial-state hypothesis protected_start ensures that the protected holders start at am0 with all of H, so there is no initial shortfall at am0 and π sums only drops that happen during the run (protected_start_pulled). Without protected_start, any LP tokens held at am0 by a non-attacker omitted from s.protected would be counted in π as an immediate loss at am0.
IThe initial imbalance I = V₀ − D* (initial_imbalance) is the gap between:
V₀ = p₀ × r₀ + p₁ × r₁, the total value of the pool's starting reserves at the peg (excluding admin fees), andD*, the exact StableSwap invariant D of those valued reserves at target amplification future_A() (newton_D_star, defined as the largest integer D ≤ V₀ satisfying the StableSwap curve inequality—the exact fixed point that the contract's Newton loop approximates).Economically, D* is the value the pool's reserves would have at peg prices once traded to a 50/50 balance along the zero-fee invariant curve, so I = V₀ − D* is the pre-fee arbitrage value the curve offers to whoever rebalances an off-balance pool at the peg (positive swap fees reduce the realizable payout below I). Because an imbalanced pool sells its surplus coin below par to attract the scarce coin, no theorem verified at par prices across all fee settings can exclude I. When the pool starts balanced (p₀ × r₀ = p₁ × r₁), D* = V₀ and I = 0.
For a pool holding one million coins in total (two 18-decimal coins at unit rates), I in whole coins (rounded to the nearest coin) varies with the reserve split and the effective amplification A = future_A() / A_PRECISION (A_PRECISION = 100) as follows:
A 50/50 60/40 80/20 95/5 (coins)
100 0 206 2762 19877
200 0 104 1393 10282
1000 0 21 281 2116
These figures are computed directly from the HOL definitions of initial_imbalance and newton_D_star (via binary search, justified by the monotonicity theorem cost_mono_D_general), and the A = 100, 80/20 entry is also verified inside HOL4: D* = 997238241325037251197896, giving I = 2761.758… coins.
When a pool starts severely lopsided at low amplification, I is correspondingly large and the bound becomes loose. For instance, the imbalanced withdrawal witness in rliAttackerOnlyNonVacuity starts from reserves [486288748, 3000] at unit prices and A = 1, where I = 468,563,285 (checked in HOL4) while a single remove_liquidity_imbalance call extracts 11,914.
The second conjunct of bounded_no_theft_readable bounds the loss of the passive LP tranche directly. Writing H for the initial passive tranche (tranche in the theorem, defined as passive_tranche s am0: totalSupply() minus the attackers' starting balanceOf) and valuing H by its proportional claim on the pool's reserves (pool_lp_claim), the theorem establishes:
claim of H at the start ≤ claim of H at the end + I + U + n × (⌊√(V + W₀ + x + 5)⌋ + 13) + π
In other words, the passive liquidity providers' collective proportional claim on the pool's reserves—measured at the pool's fixed initial rates—can decrease across the run by at most the initial imbalance I, the per-call rounding allowance slack, one wei of each coin (U), and the value π of any LP tokens transferred out of the protected floor. Note that this bounds the passive tranche as an aggregate and values it by proportional reserve share (remove_liquidity), not by single-sided withdrawal (remove_liquidity_one_coin).
I = V₀ − D*; callers can extract rebalancing profit up to I (less swap fees).threat_model requires every pool caller to be in s.attackers, any honest trader who calls the pool during the run is counted inside the attacker group. The theorem protects passive LPs against all callers combined; it does not bound MEV or sandwiching between two active traders.steps.amp_fixed).asset_types = 0, plain_pool_constants). Pools configured with oracle-rate, rebasing, or ERC-4626 tokens are out of scope (a token like wstETH configured as asset type 0 is covered at its fixed stored rate, whereas an oracle-rate pool is not).honest_contracts or outside_steps. Tokens with a transfer blocklist (such as USDC or USDT) are covered because a blocked transfer simply reverts.x). Curve StableSwap-NG provides an optimistic swap entrypoint, exchange_received, for router integrations that transfer input tokens directly to the pool and swap them within the same transaction (such as Curve's router). If a user splits those two steps across separate transactions—calling coin_i.transfer(pool, x) in transaction 1 and waiting until transaction 2 to call exchange_received—the transfer raises balanceOf(pool) by δ while leaving stored_balances[i] unchanged. Any attacker who lands a transaction in between can call exchange_received(i, j, d, 0, attacker) with 0 < d ≤ δ and no input transfer of their own: _transfer_in credits the unclaimed surplus balanceOf(pool) − stored_balances[i] = δ to the attacker's swap, updates stored_balances[i], and pays out the resulting coin j, after which the user's second transaction observes zero surplus and reverts. In bounded_no_theft_readable, on the two-step prefix before the user calls the pool (the user's transfer followed by the attacker's swap), the user is an outsider whose unbundled transfer adds the value of δ to borrowed (x); on the three-step trace that also includes the user's reverting exchange_received call, threat_model places both the user and the attacker in the caller group, whose combined gain stays within the bound (exchangeReceivedFrontRunNonVacuity mechanizes an executable two-step trace of the user's EVM token transfer followed by the attacker's exchange_received claim; see exploit_erc20.md). Users and integrators should always perform the transfer and exchange_received in a single atomic transaction, or call exchange.mint, rebase, or seize entrypoint. If an external step credits δ newly minted units of a plain coin (asset_types = 0) directly to the pool, an attacker can immediately call exchange_received to trade that unsolicited surplus for real reserves of the other coin (Curve's documentation notes this for rebasing tokens listed as plain coins, and exchangeReceivedUnsolicitedCreditNonVacuity and exchangeReceivedAttackerMintNonVacuity mechanize the pool-side and end-to-end executable mint-and-claim traces). Likewise, a token with a privileged seize function could claw back balances directly from pool custody while leaving stored reserves untouched (environmentCustodyNonVacuity, environmentCustodyMetricNonVacuity). In bounded_no_theft_readable, outside_steps rules out both failure modes by requiring steps outside the pool to preserve each coin's total supply and never reduce the pool's balance of either coin.storage_slots_separate) for the mapping keys the theorem compares.To verify that the sixteen antecedents are mutually consistent and non-vacuous, bounded_no_theft_readable_hypotheses_at_witness (in theory boundedNoTheftNonVacuity, stableswap-ng/evidence) proves every antecedent of bounded_no_theft_readable—together with passive_lps and π = 0—on a concrete six-step execution trace, conditional only on Keccak storage-key separation for the concrete keys touched by that trace (F4_keccak_separation). In that witness run, an external lender loans an attacker 40 coins of each token, and the attacker deposits them into the pool, withdraws liquidity, and repays both loans against concrete EVM bytecode deployments of the factory and token contracts.
Built from stableswap-proofs commit e6d73fa859d8 (2026-11-23). Formal identifiers in the text link directly to their definitions in the theorem explorer.