Files
zeekayandHanzo Dev 638fba4c90 Publish under the org, not a person
Bylines, paper authorship, correspondence addresses and the founder blurb under
each post named an individual. They name the organisation that published the
work instead: the brand on posts and pages, the research byline on papers, and
research@ in place of a personal address. Both are what these repositories
already used elsewhere — nothing new was coined.

Co-authors are untouched. Where a paper named someone else alongside, they stay
named; only the one byline changed. Bibliography entries, running heads,
PDF metadata and affiliations move with it, so a citation still resolves to
whoever the cited paper names.

Sample and fixture data uses the placeholder name these tests already use
elsewhere, so no assertion changed meaning.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-01 08:58:14 -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, Zoo Labs Foundation Research
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: Zoo Labs Foundation Research (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