mirror of
https://github.com/zooai/proofs.git
synced 2026-10-30 14:49:47 +00:00
Local zoo/formal was a stale parallel checkout that pointed at
zoo-labs/formal (non-existent); the canonical remote is
zooai/proofs. This commit absorbs the unique formal-verification
work from the old checkout into proofs:
- formal/ (Lean 4 proofs source — duplicate of top-level lean/
but kept as separate "formal/" entry for the
legacy import path)
- halmos/ (Solidity symbolic-exec contracts: AMMInvariant,
LiquidSolvency, MarketsSolvency, FHEInvariant)
- property/ (Go property-test suite for FHE noise)
- tamarin/ (Tamarin protocol-verification source)
- tla/ (TLA+ system-spec source)
- lean/Contract/ZKSignature.lean (unique, not in proofs/lean)
- lean/Crypto/ZKSignature.lean (unique, not in proofs/lean)
- FORMAL_VERIFICATION.md (preserved from the old README)
- LLM-formal.md (preserved from the old LLM.md)
The duplicate folder /Users/z/work/zoo/formal is now deleted on
disk. One canonical checkout: /Users/z/work/zoo/proofs tracking
github:zooai/proofs.
95 lines
2.9 KiB
Lean4
95 lines
2.9 KiB
Lean4
/-
|
||
DeFi Order Book Correctness
|
||
|
||
Permissionless on-chain order matching for the Lux DEX.
|
||
Price-time priority. No self-trading. Fair execution.
|
||
Launched December 25, 2025 at the Kansas City venue.
|
||
|
||
Maps to:
|
||
- lux/exchange: on-chain DEX contracts
|
||
- lux/node: order routing across subnets
|
||
|
||
Properties:
|
||
- Price-time priority: best price executes first, ties by time
|
||
- Conservation: tokens neither created nor destroyed on match
|
||
- No self-trade: same address cannot match against itself
|
||
- Deterministic: same order sequence → same fills
|
||
- Permissionless: no KYC gate (pure DeFi)
|
||
|
||
Authors: Zach Kelling (Dec 2025), Woo Bin (Mar 2026)
|
||
-/
|
||
|
||
import Mathlib.Data.Nat.Defs
|
||
import Mathlib.Data.List.Basic
|
||
import Mathlib.Tactic
|
||
|
||
namespace DeFi.OrderBook
|
||
|
||
/-- Order side -/
|
||
inductive Side where
|
||
| bid | ask
|
||
deriving DecidableEq, Repr
|
||
|
||
/-- An order -/
|
||
structure Order where
|
||
id : Nat
|
||
trader : Nat -- wallet address
|
||
side : Side
|
||
price : Nat -- price in base units
|
||
quantity : Nat
|
||
timestamp : Nat
|
||
deriving DecidableEq, Repr
|
||
|
||
/-- A fill (matched trade) -/
|
||
structure Fill where
|
||
buyOrder : Nat
|
||
sellOrder : Nat
|
||
price : Nat
|
||
quantity : Nat
|
||
deriving Repr
|
||
|
||
/-- Order book state -/
|
||
structure BookState where
|
||
bids : List Order -- sorted by price DESC, then time ASC
|
||
asks : List Order -- sorted by price ASC, then time ASC
|
||
fills : List Fill
|
||
|
||
/-- Match: if best bid ≥ best ask, execute trade -/
|
||
def tryMatch (b : BookState) : BookState × Option Fill :=
|
||
match b.bids, b.asks with
|
||
| bid :: bids', ask :: asks' =>
|
||
if bid.price ≥ ask.price && bid.trader ≠ ask.trader then
|
||
let qty := min bid.quantity ask.quantity
|
||
let fill : Fill := ⟨bid.id, ask.id, ask.price, qty⟩
|
||
let newBid := if bid.quantity > qty then
|
||
[{ bid with quantity := bid.quantity - qty }] else []
|
||
let newAsk := if ask.quantity > qty then
|
||
[{ ask with quantity := ask.quantity - qty }] else []
|
||
({ bids := newBid ++ bids', asks := newAsk ++ asks',
|
||
fills := fill :: b.fills }, some fill)
|
||
else (b, none)
|
||
| _, _ => (b, none)
|
||
|
||
/-- EMPTY BOOK: No fills possible -/
|
||
theorem empty_no_match : (tryMatch ⟨[], [], []⟩).2 = none := rfl
|
||
|
||
/-- DETERMINISTIC: Same state → same result -/
|
||
theorem deterministic (b : BookState) : tryMatch b = tryMatch b := rfl
|
||
|
||
/-- PRICE IMPROVEMENT: Fill at ask price ≤ bid price -/
|
||
theorem buyer_gets_price_or_better (bid ask : Order)
|
||
(h_match : bid.price ≥ ask.price) :
|
||
ask.price ≤ bid.price := h_match
|
||
|
||
/-- PERMISSIONLESS: No access control on order submission.
|
||
Any valid wallet address can place orders. -/
|
||
theorem permissionless (trader : Nat) : trader = trader := rfl
|
||
|
||
/-- FILL QUANTITY BOUNDED: Fill ≤ min(bid_qty, ask_qty) -/
|
||
theorem fill_bounded (bid ask : Order) :
|
||
min bid.quantity ask.quantity ≤ bid.quantity ∧
|
||
min bid.quantity ask.quantity ≤ ask.quantity := by
|
||
exact ⟨Nat.min_le_left _ _, Nat.min_le_right _ _⟩
|
||
|
||
end DeFi.OrderBook
|