Files
proofs/lean/DeFi/AMM.lean
zooqueenandHanzo Dev d3bff5e2dc proofs: drive Lean library to 0 errors, eliminate proof-by-assumption axioms
The lean/ proof library (88 files) had ~180 type-check errors across ~45 files
plus 31 proof-by-assumption axiom cheats. It now type-checks cleanly under
Lean 4.14.0 + Mathlib, with every theorem genuinely proven from definitions.

Errors fixed:
- Stale Mathlib imports: Data.Nat.Basic / Data.Int.Basic were removed this rev
  -> Data.Nat.Defs / Data.Int.Defs (57 files).
- Shared AI/Agent/Governance/Contract/DeFi/Ecology draft files: same proof-rot
  as the zoo-labs sibling library (now reconciled): omega missing in-scope
  bounds, dead tactics after simp, simp over-reduction, doc-comment placement,
  reserved-word binders, deriving Repr on Finset-bearing structs, noncomputable
  defs over opaque primitives.
- Consensus/Liveness: collapsed a 74-error cascade (undefined shared state model
  inlined from Safety.lean; the |>. pipe-projection vs comparison parse trap on
  14 lines rewritten to explicit dot projection).
- Crypto (BLS/FROST/Corona/Hybrid/threshold/FHE), Consensus, Protocol, Warp,
  Trust, GPU, Compute, Network, DeFi: brought structure invariants into scope
  for omega, Nat-division bounds via Nat.div_le_of_le_mul / Nat.div_mul_le_self,
  noncomputable over opaque crypto ops, deriving Inhabited for index access.

Integrity:
- Removed all 31 `theorem X := X_ax` cheats (axiom asserting the identical
  statement) and proved each from definitions; removed the UNSOUND
  content_addressed axiom (hash-injectivity over a struct with free fields).
- Quorum-intersection theorems (BFT/Quasar/Flare/Warp/BLS/GPU/Bridge): the draft
  hypothesis 3f<n is insufficient (n unbounded above makes them false); tightened
  to the optimal-resilience invariant n=3f+1 / 2t>n. Statement intent preserved.
- Net axiom change: -35 / +1 (the one addition, honesty_static, is a
  fixed-validator-set modeling assumption matching sibling files).
- Remaining axioms are trusted-base only: opaque crypto primitives (groups,
  pairings, hardness, hashes, FHE encrypt/decrypt, BLS soundness, forward
  secrecy, Fisher-Yates uniformity) and protocol modeling assumptions. No
  sorry/admit/native_decide. Verified all theorems trace to
  [propext, Classical.choice, Quot.sound] plus those primitives.

Co-authored-by: Hanzo Dev <[email protected]>
2026-06-02 05:54:28 -07:00

118 lines
3.9 KiB
Lean4
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/-
Authors: Antje Worring, Zach Kelling
Automated Market Maker (AMM) Invariants
Constant-product AMM for Lux DeFi.
x * y = k invariant preserved on every swap.
Permissionless liquidity provision.
Maps to:
- lux/exchange: AMM pool contracts
- zoo/contracts: bridge token AMM pools
Properties:
- Constant product: x * y = k preserved (or increased by fees)
- No free tokens: output ≤ reserve
- Fee conservation: fees accrue to LPs
- Permissionless: anyone can provide liquidity
Authors: Zach Kelling (Dec 2025), Woo Bin (Mar 2026)
-/
import Mathlib.Data.Nat.Defs
import Mathlib.Tactic
namespace DeFi.AMM
/-- AMM pool state -/
structure Pool where
reserveX : Nat
reserveY : Nat
totalLP : Nat
feeBps : Nat -- fee in basis points (e.g., 30 = 0.3%)
/-- The constant product -/
def invariant (p : Pool) : Nat := p.reserveX * p.reserveY
/-- Swap dx of X for dy of Y -/
def swap (p : Pool) (dx : Nat) : Pool × Nat :=
let feeAdjusted := dx * (10000 - p.feeBps) / 10000
let dy := p.reserveY * feeAdjusted / (p.reserveX + feeAdjusted)
({ p with reserveX := p.reserveX + dx
, reserveY := p.reserveY - dy }, dy)
/-- NO FREE TOKENS: Output bounded by reserve -/
theorem output_bounded (p : Pool) (dx : Nat) :
(swap p dx).2 ≤ p.reserveY := by
simp only [swap]
set fa := dx * (10000 - p.feeBps) / 10000 with hfa
apply Nat.div_le_of_le_mul
calc p.reserveY * fa ≤ p.reserveY * (p.reserveX + fa) :=
Nat.mul_le_mul_left _ (by omega)
_ = (p.reserveX + fa) * p.reserveY := Nat.mul_comm _ _
/-- ZERO IN = ZERO OUT -/
theorem zero_in_zero_out (p : Pool) : (swap p 0).2 = 0 := by
simp [swap]
/-- INVARIANT PRESERVED: k never decreases (fees increase it) -/
theorem invariant_preserved (p : Pool) (dx : Nat)
(_h_rx : p.reserveX > 0) (_h_ry : p.reserveY > 0) :
invariant (swap p dx).1 ≥ invariant p := by
simp only [swap, invariant]
set f := dx * (10000 - p.feeBps) / 10000 with hf
set X := p.reserveX
set Y := p.reserveY
-- fee-adjusted input never exceeds dx
have hfdx : f ≤ dx := by
apply Nat.div_le_of_le_mul
calc dx * (10000 - p.feeBps) ≤ dx * 10000 :=
Nat.mul_le_mul_left _ (Nat.sub_le _ _)
_ = 10000 * dx := Nat.mul_comm _ _
set s := X + f with hs
set dy := Y * f / s with hdy
-- dy * s ≤ Y * f (floor division bound)
have hbound : s * dy ≤ Y * f := by
rw [hdy, Nat.mul_comm]; exact Nat.div_mul_le_self _ _
-- the fee-adjusted swap preserves the product, fee only helps
have key : X * Y ≤ (X + f) * (Y - dy) := by
have expand : (X + f) * (Y - dy) = (X + f) * Y - (X + f) * dy := by
rw [Nat.mul_sub]
rw [expand]
have h1 : (X + f) * dy ≤ Y * f := by rw [← hs]; exact hbound
have h2 : (X + f) * Y - Y * f = X * Y := by
have : (X + f) * Y = X * Y + Y * f := by ring
omega
calc X * Y = (X + f) * Y - Y * f := h2.symm
_ ≤ (X + f) * Y - (X + f) * dy := Nat.sub_le_sub_left h1 _
-- larger actual input dx ≥ f only increases the product
have hmono : (X + f) * (Y - dy) ≤ (X + dx) * (Y - dy) :=
Nat.mul_le_mul_right _ (by omega)
exact le_trans key hmono
/-- ADD LIQUIDITY: Mint LP tokens -/
def addLiquidity (p : Pool) (dx dy : Nat) : Pool × Nat :=
let lpMinted := if p.totalLP = 0 then dx
else dx * p.totalLP / p.reserveX
({ p with reserveX := p.reserveX + dx
, reserveY := p.reserveY + dy
, totalLP := p.totalLP + lpMinted }, lpMinted)
/-- LIQUIDITY GROWS -/
theorem add_liquidity_grows (p : Pool) (dx dy : Nat) :
(addLiquidity p dx dy).1.reserveX ≥ p.reserveX := by
simp [addLiquidity]
/-- LP TOKENS MINTED -/
theorem lp_minted (p : Pool) (dx dy : Nat) :
(addLiquidity p dx dy).1.totalLP ≥ p.totalLP := by
simp [addLiquidity]
/-- PERMISSIONLESS: No access control -/
theorem anyone_can_swap (p : Pool) (dx : Nat) :
(swap p dx).1.reserveX ≥ p.reserveX := by
simp [swap]
end DeFi.AMM