Files
zeekayandHanzo Dev 2dd030dd98 lean: remove three axioms that were false, and cite Ringtail by its title
Each of these made the corpus unsound: from a false axiom every theorem in
it follows, so the 756 stated results meant nothing while they stood.

Crypto/Magnetar.lean carried two axioms of the shape

    ∀ (oracle : Nat → Nat), ∃ (x : Nat), oracle x ≠ x

which the identity function refutes, yielding `∃ x, x ≠ x`. Both were roots
of `lean_lib Crypto`, so both compiled. Neither had a user. Family
disjointness quantifies over adversaries and advantages; `Nat → Nat` cannot
state it, so it is prose until the advantage model exists.

Protocol/Handshake.lean defined the session KDF as integer addition and then
axiomatised that distinct transcripts give distinct keys. ⟨1,1,1⟩ and ⟨1,0,2⟩
both derive 3 with transcripts 1 ≠ 2. `deriveSessionKey` is now opaque, which
is what a hash is in a model, and collision resistance is the one assumed
property. `transcript_binding` followed from unfolding the addition; it now
follows from the assumption.

Consensus/Safety.lean `unique_threshold` counts `honestPreferring` over
`Finset.univ` — the whole validator set, not the k-sample — so it needs
`2 * alpha > n`. Legal counterexample without it: n = 1000, f = 100, k = 20,
alpha = 14, with 900 honest nodes split 450/450, satisfies every hypothesis
and concludes v1 = v2. The hypothesis is now stated and threaded through
`theorem safety`, which makes the real limitation visible in the type: with
alpha ≤ k ≤ n it forces sampling more than half the network every round. In
the sub-sampled regime the protocol runs, safety is probabilistic and no
theorem of this shape holds. Consensus.ThresholdProof already proves the
pigeonhole under exactly this hypothesis.

strict-e2e-pq cited ePrint 2024/1113 as "Corona: A Lattice-Based Threshold
Signature for Post-Quantum Consensus" with no authors. That paper is
Ringtail, by Boschini, Kaviani, Lai, Malavolta, Takahashi and Tibouchi.
Corona is our implementation of it. Restored their title and their names.

Verified with a local lake build over every library.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-29 11:12:01 -07:00

215 lines
8.1 KiB
Lean4

/-
Consensus Safety Proof
Theorem: No two honest nodes finalize conflicting values at the same height.
This models the Lux consensus core as a state machine:
- Nodes hold preferences and confidence counters
- Each round: sample k peers, threshold vote, update confidence
- Finalization: confidence reaches beta consecutive rounds
The proof follows the standard metastable BFT argument:
1. If honest majority prefers v, alpha threshold ensures v wins the round
2. Consecutive wins accumulate confidence toward beta
3. Once finalized, no honest node can switch preference (confidence monotonicity)
4. Therefore two honest nodes cannot finalize different values
Maps to Go code:
- core/consensus.go: Block interface (ID, Height, Verify, Accept, Reject)
- protocol/wave/wave.go: Wave.Tick (sampling, threshold, confidence)
- protocol/wave/fpc/fpc.go: FPC threshold selection
- core/types/: Decision enum (Undecided, Commit, Skip)
-/
import Mathlib.Data.Finset.Basic
import Mathlib.Data.Nat.Defs
import Mathlib.Tactic
namespace Consensus
/-- A value identifier (models ids.ID = [32]byte in Go) -/
abbrev ValueId := Fin (2^256)
/-- A node identifier (models types.NodeID) -/
abbrev NodeId := Nat
/-- Decision outcome (models types.Decision) -/
inductive Decision where
| undecided : Decision
| commit : Decision
| skip : Decision
deriving DecidableEq, Repr
/-- Consensus parameters (models wave.Config) -/
structure Params where
n : Nat -- total nodes
f : Nat -- max Byzantine nodes
k : Nat -- sample size per round (wave.Config.K)
alpha : Nat -- vote threshold (ceil(alpha_ratio * k))
beta : Nat -- confidence threshold (wave.Config.Beta)
hf : f < n / 3 -- BFT assumption
hk : k ≤ n -- cannot sample more than population
ha : alpha ≤ k -- threshold cannot exceed sample
hb : 0 < beta -- need at least 1 round for finality
/-- Per-node state (models wave.WaveState + wave.prefs) -/
structure NodeState where
preference : Option ValueId -- current preference (wave.prefs[item])
confidence : Nat -- consecutive confirming rounds (WaveState.Count)
finalized : Option ValueId -- decided value, if any
honest : Bool -- Byzantine or honest
/-- Global consensus state -/
structure ConsensusState (p : Params) where
round : Nat
nodes : Fin p.n → NodeState
variable {p : Params}
/-- A node has finalized a value -/
def hasFinalized (s : ConsensusState p) (i : Fin p.n) (v : ValueId) : Prop :=
(s.nodes i).finalized = some v
/-- A node is honest -/
def isHonest (s : ConsensusState p) (i : Fin p.n) : Prop :=
(s.nodes i).honest = true
/-- Count of honest nodes preferring value v -/
noncomputable def honestPreferring (s : ConsensusState p) (v : ValueId) : Nat :=
Finset.card (Finset.filter
(fun i => (s.nodes i).honest = true ∧ (s.nodes i).preference = some v)
Finset.univ)
/-- Count of honest nodes in the system -/
noncomputable def honestCount (s : ConsensusState p) : Nat :=
Finset.card (Finset.filter
(fun i => (s.nodes i).honest = true)
Finset.univ)
/-
Key invariant: once a node finalizes, its preference never changes.
This models the early-return in wave.go:98-102:
if state.Decided { return }
-/
axiom finalized_preference_stable :
∀ (p : Params) (s s' : ConsensusState p) (i : Fin p.n) (v : ValueId),
hasFinalized s i v → s'.round > s.round → hasFinalized s' i v
/-
At most one value reaches the alpha threshold in a round.
`honestPreferring` counts over `Finset.univ`, i.e. the whole validator
set rather than the k-sample, so the hypothesis needed here is
`2 * alpha > n`: two disjoint sets of honest supporters, each of size at
least alpha, cannot both fit in n nodes. `Consensus.ThresholdProof`
proves exactly that as `unique_threshold_proven`.
Without it the statement is false. Take n = 1000, f = 100, k = 20,
alpha = 14 — legal under all four Params constraints — with 900 honest
nodes split 450/450 between v1 and v2. Then honestCount = 900 > 2f, and
alpha = 14 > 2*k/3 = 13, and both values clear alpha, so the conclusion
would give v1 = v2.
The hypothesis is severe and honestly so: with alpha ≤ k ≤ n it forces
k > n/2, which is to sample more than half the network every round. In
the sub-sampled regime the protocol actually runs, safety is
probabilistic, and no deterministic theorem of this shape holds.
-/
axiom unique_threshold :
∀ (p : Params) (s : ConsensusState p) (v1 v2 : ValueId) (round : Nat),
honestCount s > 2 * p.f →
p.alpha > 2 * p.k / 3 →
2 * p.alpha > p.n →
-- If both values achieved alpha threshold in the same round,
-- they must be the same value
honestPreferring s v1 ≥ p.alpha →
honestPreferring s v2 ≥ p.alpha →
v1 = v2
/-
Finalization window overlap (pure math component):
Two intervals of length beta within [0, R) must share at least one
element when R < 2 * beta. This is the pigeonhole/interval argument.
If node i's window starts at a and node j's at b, with both windows
fitting in [0, R), then a + beta ≤ R and b + beta ≤ R.
If the windows were disjoint: a + beta ≤ b or b + beta ≤ a,
so max(a + beta, b + beta) ≤ R and min(a,b) + 2*beta ≤ R,
contradicting R < 2 * beta (since min(a,b) ≥ 0).
-/
theorem finalization_windows_overlap_math
(R beta a b : Nat)
(hR : R < 2 * beta)
(ha : a + beta ≤ R)
(hb : b + beta ≤ R) :
-- The windows [a, a+beta) and [b, b+beta) share a round
∃ r, a ≤ r ∧ r < a + beta ∧ b ≤ r ∧ r < b + beta := by
by_cases hab : a ≤ b
· -- a ≤ b: overlap element is b (since b < a + beta)
refine ⟨b, hab, ?_, le_refl b, by omega⟩
-- Need: b < a + beta. From hb: b + beta ≤ R < 2 * beta, so b < beta.
-- From ha: a + beta ≤ R, and hR: R < 2 * beta, so a < beta.
-- If b ≥ a + beta, then b + beta ≥ a + 2 * beta > a + R ≥ a + a + beta,
-- but more directly: a + beta ≤ b → a + 2*beta ≤ b + beta ≤ R < 2*beta
-- → a + 2*beta < 2*beta → a < 0, contradiction since a : Nat.
omega
· -- b < a: overlap element is a
push_neg at hab
refine ⟨a, le_refl a, by omega, ?_, ?_⟩
· omega
· omega
/-
Finalization window overlap (protocol connection):
If two nodes finalize, the protocol produces a round where both
values held alpha-threshold support. This connects the math above
to the consensus state model.
This remains an axiom because it models protocol behavior:
finalization implies beta consecutive rounds of alpha-threshold support,
and by finalization_windows_overlap_math those windows share a round.
The axiom bridges the gap between "finalized" (an opaque predicate
on ConsensusState) and "had alpha support for beta rounds" (which
requires a trace model not present in this formalization).
-/
axiom finalization_windows_overlap :
∀ (p : Params) (s : ConsensusState p)
(i j : Fin p.n) (v1 v2 : ValueId),
hasFinalized s i v1 → hasFinalized s j v2 →
-- There exists a round where both values achieved alpha threshold
∃ (s_overlap : ConsensusState p),
honestPreferring s_overlap v1 ≥ p.alpha ∧
honestPreferring s_overlap v2 ≥ p.alpha ∧
honestCount s_overlap = honestCount s
/-
Main Safety Theorem
If two honest nodes finalize values at the same height,
those values must be identical.
Derived from unique_threshold + finalization_windows_overlap:
1. Both finalized → windows overlap in some round (axiom)
2. In that round, both achieved alpha (from overlap)
3. By unique_threshold, v1 = v2
-/
theorem safety
(p : Params)
(s : ConsensusState p)
(i j : Fin p.n)
(v1 v2 : ValueId)
(_hi : isHonest s i)
(_hj : isHonest s j)
(hfi : hasFinalized s i v1)
(hfj : hasFinalized s j v2)
(hbft : honestCount s > 2 * p.f)
(halpha : p.alpha > 2 * p.k / 3)
(hmaj : 2 * p.alpha > p.n)
: v1 = v2 := by
obtain ⟨s_overlap, hov1, hov2, hcount⟩ :=
finalization_windows_overlap p s i j v1 v2 hfi hfj
exact unique_threshold p s_overlap v1 v2 s_overlap.round
(hcount ▸ hbft) halpha hmaj hov1 hov2
end Consensus