Files
proofs/lean/DeFi/FlashLoan.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

62 lines
1.8 KiB
Lean4

/-
Authors: Antje Worring, Zach Kelling
Flash Loan Safety
Atomic borrow-use-repay within a single transaction.
If repayment fails, entire transaction reverts.
Properties:
- Atomicity: borrow + use + repay in one tx
- Solvency: pool balance ≥ pre-loan balance after tx
- Fee accrual: flash loan fee goes to LPs
- No residual debt: loan is fully repaid or fully reverted
Author: Zach Kelling
-/
import Mathlib.Data.Nat.Defs
import Mathlib.Tactic
namespace DeFi.FlashLoan
structure LoanState where
poolBalance : Nat
feeBps : Nat -- flash loan fee (e.g., 9 = 0.09%)
structure FlashLoan where
amount : Nat
fee : Nat
repaid : Bool
def executeLoan (s : LoanState) (amount : Nat) (repaid : Bool) : LoanState :=
if repaid && amount ≤ s.poolBalance then
let fee := amount * s.feeBps / 10000
{ s with poolBalance := s.poolBalance + fee } -- pool gains fee
else s -- revert: no change
/-- SOLVENCY: Pool never loses money on flash loans -/
theorem flash_loan_solvent (s : LoanState) (amount : Nat) (repaid : Bool) :
(executeLoan s amount repaid).poolBalance ≥ s.poolBalance := by
unfold executeLoan
split <;> simp
/-- NO REPAY = NO LOAN: Unreturned loan reverts -/
theorem no_repay_reverts (s : LoanState) (amount : Nat) :
executeLoan s amount false = s := by
simp [executeLoan]
/-- FEE ACCRUAL: Successful loan increases pool by fee -/
theorem fee_accrues (s : LoanState) (amount : Nat)
(h : amount ≤ s.poolBalance) (_hf : s.feeBps > 0) :
(executeLoan s amount true).poolBalance ≥ s.poolBalance := by
simp [executeLoan, h]
/-- OVERDRAFT BLOCKED: Can't borrow more than pool has -/
theorem overdraft_blocked (s : LoanState) (amount : Nat)
(h : amount > s.poolBalance) :
executeLoan s amount true = s := by
simp [executeLoan, Nat.not_le.mpr h]
end DeFi.FlashLoan