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

182 lines
6.7 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
Carbon Credit Conservation — Token Integrity
Formal proofs about carbon credit tokens used in conservation finance.
Credits are issued for verified carbon sequestration, traded freely,
and retired (burned) when used to offset emissions. Key invariants
prevent double-counting and ensure retirement is irreversible.
Maps to:
- Zoo Labs Foundation's conservation tokenomics
- zoo/papers/zoo-experience-ledger.tex: verification infrastructure
Key results:
- No double counting: each credit has a unique ID and is counted once
- Retirement is irreversible: retired credits cannot be re-activated
- Issuance is bounded by verified sequestration
Author: Zach Kelling
-/
import Mathlib.Data.Nat.Defs
import Mathlib.Data.Finset.Basic
import Mathlib.Tactic
namespace DeFi.CarbonCredits
/-! ## Core Structures -/
/-- A carbon credit token. -/
structure CarbonCredit where
/-- Unique credit identifier -/
creditId : Nat
/-- Amount of CO2 sequestered (kg) -/
sequestrationKg : Nat
/-- Verification attestation hash -/
attestationHash : Nat
/-- Current owner -/
owner : Nat
/-- Whether the credit has been retired (used for offset) -/
retired : Bool
deriving DecidableEq, Repr
/-- The carbon credit registry state. -/
structure Registry where
/-- All issued credits -/
credits : List CarbonCredit
/-- Set of credit IDs that have been issued -/
issuedIds : Finset Nat
/-- Total verified sequestration available for issuance (kg) -/
verifiedSequestration : Nat
/-- Total credits issued (sum of sequestrationKg across all credits) -/
totalIssued : Nat
/-- A sequestration verification attestation. -/
structure SequestrationAttestation where
/-- Amount of CO2 verified as sequestered (kg) -/
amountKg : Nat
/-- Verifier's TEE signature -/
teeSignature : Nat
/-- The project/site that performed sequestration -/
projectId : Nat
hValid : teeSignature ≠ 0
/-! ## Theorem 1: No Double Counting -/
/-- Each credit has a unique ID in the registry. -/
def noDuplicateIds (reg : Registry) : Prop :=
∀ i j : Fin reg.credits.length,
reg.credits[i].creditId = reg.credits[j].creditId → i = j
/-- Issue a new credit. Returns none if the ID already exists
or if issuance would exceed verified sequestration. -/
def issueCredit (reg : Registry) (credit : CarbonCredit)
(att : SequestrationAttestation)
(h_amount : credit.sequestrationKg ≤ att.amountKg) : Option Registry :=
if credit.creditId ∈ reg.issuedIds then none -- duplicate ID
else if reg.totalIssued + credit.sequestrationKg > reg.verifiedSequestration then none
else some {
credits := credit :: reg.credits
issuedIds := reg.issuedIds ∪ {credit.creditId}
verifiedSequestration := reg.verifiedSequestration
totalIssued := reg.totalIssued + credit.sequestrationKg }
/-- No double counting: attempting to issue a credit with an existing ID fails. -/
theorem no_double_count (reg : Registry) (credit : CarbonCredit)
(att : SequestrationAttestation)
(h_amount : credit.sequestrationKg ≤ att.amountKg)
(h_exists : credit.creditId ∈ reg.issuedIds) :
issueCredit reg credit att h_amount = none := by
simp [issueCredit, h_exists]
/-- After successful issuance, the new ID is recorded. -/
theorem issue_records_id (reg : Registry) (credit : CarbonCredit)
(att : SequestrationAttestation) (h_amount : credit.sequestrationKg ≤ att.amountKg)
(reg' : Registry)
(h_ok : issueCredit reg credit att h_amount = some reg') :
credit.creditId ∈ reg'.issuedIds := by
unfold issueCredit at h_ok
split at h_ok
· exact absurd h_ok (by simp)
· split at h_ok
· exact absurd h_ok (by simp)
· -- some-branch: reg'.issuedIds = reg.issuedIds ∪ {credit.creditId}
injection h_ok with h_eq
subst h_eq
simp [Finset.mem_union]
/-! ## Theorem 2: Retirement Irreversible -/
/-- Retire a credit: mark it as retired (burned for offset). -/
def retireCredit (credit : CarbonCredit) : CarbonCredit :=
{ credit with retired := true }
/-- Retirement is irreversible: once a credit is retired, the retired flag
is true and there is no operation to set it back to false.
(The retireCredit function is the only state transition, and it only
sets retired := true.) -/
theorem retirement_irreversible (credit : CarbonCredit) :
(retireCredit credit).retired = true := by
simp [retireCredit]
/-- Retiring preserves the credit identity. -/
theorem retire_preserves_id (credit : CarbonCredit) :
(retireCredit credit).creditId = credit.creditId := by
simp [retireCredit]
/-- Retiring preserves the sequestration amount. -/
theorem retire_preserves_amount (credit : CarbonCredit) :
(retireCredit credit).sequestrationKg = credit.sequestrationKg := by
simp [retireCredit]
/-- An already-retired credit is unchanged by another retirement. -/
theorem double_retire_idempotent (credit : CarbonCredit) :
retireCredit (retireCredit credit) = retireCredit credit := by
simp [retireCredit]
/-! ## Theorem 3: Issuance Bounded -/
/-- Total issued credits never exceed verified sequestration.
This is enforced by the issueCredit function which checks
totalIssued + newAmount <= verifiedSequestration. -/
theorem issuance_bounded (reg : Registry) (credit : CarbonCredit)
(att : SequestrationAttestation) (h_amount : credit.sequestrationKg ≤ att.amountKg)
(reg' : Registry)
(h_ok : issueCredit reg credit att h_amount = some reg')
(h_inv : reg.totalIssued ≤ reg.verifiedSequestration) :
reg'.totalIssued ≤ reg'.verifiedSequestration := by
unfold issueCredit at h_ok
split at h_ok
· exact absurd h_ok (by simp)
· split at h_ok
· exact absurd h_ok (by simp)
· -- the second guard is false: ¬(totalIssued + amount > verifiedSequestration)
rename_i h_guard
injection h_ok with h_eq
subst h_eq
-- goal: reg.totalIssued + credit.sequestrationKg ≤ reg.verifiedSequestration
simp only [gt_iff_lt, not_lt] at h_guard
exact h_guard
/-- Issuance increases total by exactly the credit's sequestration amount. -/
theorem issuance_amount_exact (reg : Registry) (credit : CarbonCredit)
(att : SequestrationAttestation) (h_amount : credit.sequestrationKg ≤ att.amountKg)
(reg' : Registry)
(h_ok : issueCredit reg credit att h_amount = some reg') :
reg'.totalIssued = reg.totalIssued + credit.sequestrationKg := by
unfold issueCredit at h_ok
split at h_ok
· exact absurd h_ok (by simp)
· split at h_ok
· exact absurd h_ok (by simp)
· injection h_ok with h_eq
subst h_eq
rfl
/-- An empty registry has zero issuance. -/
theorem empty_zero_issued : (⟨[], ∅, 0, 0⟩ : Registry).totalIssued = 0 := rfl
end DeFi.CarbonCredits