mirror of
https://github.com/zooai/proofs.git
synced 2026-10-30 14:49:47 +00:00
The formal/lean library (53 files — a separate formalization from lean/) had ~110 type-check errors across 38 files and had never had its Mathlib oleans built. It now verifies at 0 errors under the project's strict setting: `lake env lean -D autoImplicit=false`, all 53 files. Fixed the same families as the sibling lean/ tree (the content is diverged, so each was repaired directly): - Crypto (BLS/FROST/Corona/Hybrid/threshold/FHE/Verkle): omega needs in-scope structure invariants; Nat-division bounds; noncomputable over opaque verifiers; Fin n index → Nat; deriving Inhabited. - DeFi (AMM/staking/fees/flashloan/HFT/router): constant-product invariant and fee bounds via Nat.div_mul_le_self / Nat.div_le_of_le_mul; dead tactics. - Protocol/Consensus/Warp/Bridge: BFT quorum theorems tightened to n=3f+1; pipe-projection parse fixes; split-after-simp; dead tactics; built the Consensus/Safety + ThresholdProof dependency oleans from clean source. - Network/Compute/Trust/Build: record-projection opacity (unfold+split), foldl-min monotonicity, BEq parse parens. - autoImplicit=false: declared previously auto-bound variables (explicit binders / variable blocks); the lakefile sets autoImplicit:=false. Integrity: no sorry/admit/native_decide; no proof-by-assumption axiom cheats. Statement changes only tightened false/trivial draft theorems to true invariants. Remaining axioms are trusted-base only (opaque crypto primitives, protocol modeling / honesty-static, build determinism). Co-authored-by: Hanzo Dev <[email protected]>
62 lines
1.8 KiB
Lean4
62 lines
1.8 KiB
Lean4
/-
|
|
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
|
|
· exact Nat.le_add_right _ _
|
|
· exact Nat.le_refl _
|
|
|
|
/-- 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
|