-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSafety.lean
More file actions
201 lines (173 loc) · 7.48 KB
/
Safety.lean
File metadata and controls
201 lines (173 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/-
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
/-
Key invariant: finalization requires beta consecutive rounds with
alpha-threshold majority. If alpha > 2k/3 and honest majority holds,
then at most one value can achieve alpha threshold in any round.
This is the crux: with k samples from n nodes where f < n/3 are Byzantine,
at most one value can get alpha >= ceil(2k/3) votes in a single round,
because honest nodes have a single preference.
-/
axiom unique_threshold :
∀ (p : Params) (s : ConsensusState p) (v1 v2 : ValueId) (round : Nat),
honestCount s > 2 * p.f →
p.alpha > 2 * p.k / 3 →
-- 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)
: 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 hov1 hov2
end Consensus