omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / seed / helix / proofs / HelixBFT.lean
| 1 | /- |
| 2 | SPDX-License-Identifier: Apache-2.0 |
| 3 | seed/helix/proofs/HelixBFT.lean |
| 4 | |
| 5 | Machine-checked Lean 4 proofs for the Generalized N = 3f + 1 Byzantine Self-Healing |
| 6 | Compiler Braid (instantiated at N = 4, f = 1 and N = 7, f = 2), Quorum Election + |
| 7 | Cold-Regrowth Composition, Compiler Mitosis, and Arboris Predator <-> Ascetic Hot-Swap. |
| 8 | |
| 9 | Model-to-system link: |
| 10 | - `BraidN`, `countMatchesN`, `hasQuorumN`, `electQuorumN`, `healBraidOpt` mirror |
| 11 | `seed/helix/bft_healer.om` (`bfDoQuorumN` with threshold `n - f = 2f + 1` for |
| 12 | `n = 3f + 1`, quarantine bitmask `100 + mask` for `1..f` faults, fail-closed |
| 13 | exit `20` for `> f` faults) and `seed/helix/braid4-verify.sh` / `braid7-verify.sh`. |
| 14 | - `mitosisStep` mirrors `bfMitosisStep` in `seed/helix/bft_healer.om`. |
| 15 | - `runWorkload` / `ascetic_predator_hotswap_sound` mirror `bfArbMode` + Monk-verified |
| 16 | SM121 schedules in `seed/helix/bft_healer.om` and `seed/helix/monk_check.om`. |
| 17 | |
| 18 | Theorems proved (ZERO sorry, ZERO extra axioms): |
| 19 | 1. Generalized `BraidN (n : Nat) (D : Type)` for any `n ≥ 3*f + 1`: |
| 20 | - `bftN_quorum_unique`: at most one digest can hold `≥ n - f` votes (for `n = 3f + 1`, `2f + 1`). |
| 21 | - `bftN_survives_f_faults`: with `≤ f` Byzantine faults, `canon` always holds a quorum |
| 22 | and no distinct corrupted digest can ever hold one. |
| 23 | - `electQuorumN_recovers_canon`: the computable quorum election algorithm |
| 24 | `electQuorumN f b` ALWAYS returns `some canon` whenever `faultCountN b canon ≤ f` |
| 25 | (genuinely uses `hf` — fixes the former `have _ := hf` modeling gap!). |
| 26 | - `bftN_elect_and_heal_restores_unanimity`: directly composes `electQuorumN` with |
| 27 | `healBraidOpt` to prove `healBraidOpt b (electQuorumN f b) = some (unanimousN n canon)`, |
| 28 | yielding `countMatchesN = n` and `faultCountN = 0`. |
| 29 | - `bftN_3f1_threshold_eq`: for `n = 3*f + 1`, `n - f = 2*f + 1`. |
| 30 | 2. Concrete instantiations & tight negative controls: |
| 31 | - `braid4_bft_spec` (N = 4, f = 1, quorum ≥ 3) + `bft_double_fault_counterexample` (f = 2 halts `none`). |
| 32 | - `braid7_bft_spec` (N = 7, f = 2, quorum ≥ 5) + `bft7_triple_fault_counterexample` (f = 3 halts `none`). |
| 33 | - Backward-compatible `Braid4` API with real `electQuorum` + `electQuorum_recovers_canon` |
| 34 | composed with `healBraid`. |
| 35 | 3. `ascetic_predator_hotswap_sound` and `mitosis_monotonic_and_safe`. |
| 36 | -/ |
| 37 | |
| 38 | namespace HelixBFT |
| 39 | |
| 40 | -- ==================== GENERALIZED N = 3f + 1 BYZANTINE BRAID ==================== |
| 41 | |
| 42 | /-- Generalized N-strand braid of canonical compiler or kernel digests. -/ |
| 43 | structure BraidN (n : Nat) (D : Type) where |
| 44 | strands : List D |
| 45 | hlen : strands.length = n |
| 46 | |
| 47 | /-- Count occurrences of digest `x` in a list of strand outputs. -/ |
| 48 | def countList {D : Type} [DecidableEq D] (xs : List D) (x : D) : Nat := |
| 49 | match xs with |
| 50 | | [] => 0 |
| 51 | | y :: ys => (if y = x then 1 else 0) + countList ys x |
| 52 | |
| 53 | /-- Count strands in `xs` disagreeing with canonical truth `canon`. -/ |
| 54 | def faultList {D : Type} [DecidableEq D] (xs : List D) (canon : D) : Nat := |
| 55 | match xs with |
| 56 | | [] => 0 |
| 57 | | y :: ys => (if y = canon then 0 else 1) + faultList ys canon |
| 58 | |
| 59 | def countMatchesN {D : Type} [DecidableEq D] {n : Nat} (b : BraidN n D) (x : D) : Nat := |
| 60 | countList b.strands x |
| 61 | |
| 62 | def faultCountN {D : Type} [DecidableEq D] {n : Nat} (b : BraidN n D) (canon : D) : Nat := |
| 63 | faultList b.strands canon |
| 64 | |
| 65 | /-- Quorum threshold for `n ≥ 3*f + 1`: requires `≥ n - f` agreeing strands |
| 66 | (which equals `2*f + 1` when `n = 3*f + 1`, i.e., 3/4 for N=4 and 5/7 for N=7). -/ |
| 67 | def hasQuorumN {D : Type} [DecidableEq D] {n : Nat} (f : Nat) (b : BraidN n D) (x : D) : Prop := |
| 68 | countMatchesN b x ≥ n - f |
| 69 | |
| 70 | theorem bftN_3f1_threshold_eq (f : Nat) : (3 * f + 1) - f = 2 * f + 1 := by |
| 71 | omega |
| 72 | |
| 73 | private theorem countList_add_faultList {D : Type} [DecidableEq D] (xs : List D) (x : D) : |
| 74 | countList xs x + faultList xs x = xs.length := by |
| 75 | induction xs with |
| 76 | | nil => rfl |
| 77 | | cons y ys ih => |
| 78 | simp only [countList, faultList, List.length_cons] |
| 79 | by_cases h : y = x |
| 80 | · simp only [h, ↓reduceIte]; omega |
| 81 | · simp only [h, ↓reduceIte]; omega |
| 82 | |
| 83 | private theorem countList_disjoint_sum {D : Type} [DecidableEq D] (xs : List D) (x y : D) |
| 84 | (hne : x ≠ y) : |
| 85 | countList xs x + countList xs y ≤ xs.length := by |
| 86 | induction xs with |
| 87 | | nil => simp [countList] |
| 88 | | cons z zs ih => |
| 89 | simp only [countList, List.length_cons] |
| 90 | by_cases h1 : z = x |
| 91 | · subst h1 |
| 92 | simp only [↓reduceIte, hne] |
| 93 | omega |
| 94 | · by_cases h2 : z = y |
| 95 | · subst h2 |
| 96 | simp only [h1, ↓reduceIte] |
| 97 | omega |
| 98 | · simp only [h1, h2, ↓reduceIte] |
| 99 | omega |
| 100 | |
| 101 | private theorem mem_of_countList_pos {D : Type} [DecidableEq D] (xs : List D) (x : D) |
| 102 | (h : countList xs x > 0) : x ∈ xs := by |
| 103 | induction xs with |
| 104 | | nil => simp [countList] at h |
| 105 | | cons y ys ih => |
| 106 | simp only [countList] at h |
| 107 | by_cases hyx : y = x |
| 108 | · subst hyx; exact List.Mem.head ys |
| 109 | · simp only [hyx, ↓reduceIte, Nat.zero_add] at h |
| 110 | exact List.Mem.tail y (ih h) |
| 111 | |
| 112 | /-- Generalized BFT Quorum Uniqueness: whenever `n ≥ 3*f + 1`, at most one digest |
| 113 | can hold a Byzantine quorum (`≥ n - f` votes). -/ |
| 114 | theorem bftN_quorum_unique {D : Type} [DecidableEq D] {n f : Nat} |
| 115 | (hn : n ≥ 3 * f + 1) (b : BraidN n D) (x y : D) |
| 116 | (hx : hasQuorumN f b x) (hy : hasQuorumN f b y) : x = y := by |
| 117 | by_cases hxy : x = y |
| 118 | · exact hxy |
| 119 | · exfalso |
| 120 | unfold hasQuorumN countMatchesN at hx hy |
| 121 | have hsum := countList_disjoint_sum b.strands x y hxy |
| 122 | rw [b.hlen] at hsum |
| 123 | omega |
| 124 | |
| 125 | /-- Generalized BFT Survival Theorem: with `≤ f` Byzantine faults on `n ≥ 3*f + 1` strands, |
| 126 | the canonical truth ALWAYS holds a quorum and no corrupted digest can outvote it. -/ |
| 127 | theorem bftN_survives_f_faults {D : Type} [DecidableEq D] {n f : Nat} |
| 128 | (hn : n ≥ 3 * f + 1) (b : BraidN n D) (canon bad : D) |
| 129 | (hf : faultCountN b canon ≤ f) (hbad : bad ≠ canon) : |
| 130 | hasQuorumN f b canon ∧ ¬ hasQuorumN f b bad := by |
| 131 | have hq : hasQuorumN f b canon := by |
| 132 | unfold hasQuorumN countMatchesN |
| 133 | unfold faultCountN at hf |
| 134 | have htot := countList_add_faultList b.strands canon |
| 135 | rw [b.hlen] at htot |
| 136 | omega |
| 137 | refine ⟨hq, ?_⟩ |
| 138 | intro hqb |
| 139 | exact hbad (bftN_quorum_unique hn b bad canon hqb hq) |
| 140 | |
| 141 | -- ==================== COMPUTABLE QUORUM ELECTION & HEALING COMPOSITION ==================== |
| 142 | |
| 143 | /-- Computable quorum election over candidate list `cands` against full strand list `all`. -/ |
| 144 | def findQuorumAux {D : Type} [DecidableEq D] (thresh : Nat) (all : List D) : List D → Option D |
| 145 | | [] => none |
| 146 | | x :: rest => |
| 147 | if countList all x ≥ thresh then some x |
| 148 | else findQuorumAux thresh all rest |
| 149 | |
| 150 | /-- Elect the unique Byzantine quorum digest (returns `none` fail-closed if no quorum exists). -/ |
| 151 | def electQuorumN {D : Type} [DecidableEq D] {n : Nat} (f : Nat) (b : BraidN n D) : Option D := |
| 152 | findQuorumAux (n - f) b.strands b.strands |
| 153 | |
| 154 | private theorem findQuorumAux_sound {D : Type} [DecidableEq D] (thresh : Nat) |
| 155 | (all cands : List D) (q : D) (h : findQuorumAux thresh all cands = some q) : |
| 156 | countList all q ≥ thresh := by |
| 157 | induction cands with |
| 158 | | nil => simp [findQuorumAux] at h |
| 159 | | cons x rest ih => |
| 160 | unfold findQuorumAux at h |
| 161 | split at h |
| 162 | · rename_i hge |
| 163 | injection h with hxq |
| 164 | subst hxq |
| 165 | exact hge |
| 166 | · exact ih h |
| 167 | |
| 168 | private theorem findQuorumAux_complete {D : Type} [DecidableEq D] (thresh : Nat) |
| 169 | (all cands : List D) (x : D) (hmem : x ∈ cands) (hge : countList all x ≥ thresh) : |
| 170 | ∃ q, findQuorumAux thresh all cands = some q := by |
| 171 | induction cands with |
| 172 | | nil => cases hmem |
| 173 | | cons y rest ih => |
| 174 | unfold findQuorumAux |
| 175 | split |
| 176 | · exact ⟨y, rfl⟩ |
| 177 | · rename_i hnot |
| 178 | cases hmem with |
| 179 | | head => exact absurd hge hnot |
| 180 | | tail _ hmem' => exact ih hmem' |
| 181 | |
| 182 | /-- Core Election Recovery Theorem: for any `n ≥ 3*f + 1`, if at most `f` strands are |
| 183 | Byzantine-faulty (`faultCountN b canon ≤ f`), then `electQuorumN f b` ALWAYS returns |
| 184 | `some canon`. (No unused hypothesis: `hf` is required to guarantee `canon` reaches |
| 185 | threshold `n - f` and uniquely dominates every other candidate.) -/ |
| 186 | theorem electQuorumN_recovers_canon {D : Type} [DecidableEq D] {n f : Nat} |
| 187 | (hn : n ≥ 3 * f + 1) (b : BraidN n D) (canon : D) |
| 188 | (hf : faultCountN b canon ≤ f) : |
| 189 | electQuorumN f b = some canon := by |
| 190 | have hcanon_q : countList b.strands canon ≥ n - f := by |
| 191 | have htot := countList_add_faultList b.strands canon |
| 192 | unfold faultCountN at hf |
| 193 | rw [b.hlen] at htot |
| 194 | omega |
| 195 | have hpos : countList b.strands canon > 0 := by omega |
| 196 | have hmem : canon ∈ b.strands := mem_of_countList_pos b.strands canon hpos |
| 197 | obtain ⟨q, hq⟩ := findQuorumAux_complete (n - f) b.strands b.strands canon hmem hcanon_q |
| 198 | have hq_ge : countList b.strands q ≥ n - f := |
| 199 | findQuorumAux_sound (n - f) b.strands b.strands q hq |
| 200 | have hq_eq : q = canon := |
| 201 | bftN_quorum_unique hn b q canon hq_ge hcanon_q |
| 202 | subst hq_eq |
| 203 | exact hq |
| 204 | |
| 205 | /-- Canonical unanimous N-strand braid where every strand holds digest `x`. -/ |
| 206 | def unanimousN (n : Nat) {D : Type} (x : D) : BraidN n D := |
| 207 | { strands := List.replicate n x |
| 208 | hlen := List.length_replicate } |
| 209 | |
| 210 | /-- Quarantine + Cold Regrowth operator for a generalized N-strand braid: |
| 211 | every strand disagreeing with elected quorum `q` is regrown from its clean seed, yielding `q`. -/ |
| 212 | def healList {D : Type} [DecidableEq D] (xs : List D) (q : D) : List D := |
| 213 | xs.map (fun s => if s = q then s else q) |
| 214 | |
| 215 | def healBraidN {D : Type} [DecidableEq D] {n : Nat} (b : BraidN n D) (q : D) : BraidN n D := |
| 216 | { strands := healList b.strands q |
| 217 | hlen := by simp [healList, b.hlen] } |
| 218 | |
| 219 | /-- Fail-closed optional healing: halts (`none`) if no quorum was elected, |
| 220 | otherwise quarantines faulty strands and regrows them to `q`. -/ |
| 221 | def healBraidOpt {D : Type} [DecidableEq D] {n : Nat} (b : BraidN n D) : |
| 222 | Option D → Option (BraidN n D) |
| 223 | | none => none |
| 224 | | some q => some (healBraidN b q) |
| 225 | |
| 226 | private theorem healList_eq_replicate {D : Type} [DecidableEq D] (xs : List D) (q : D) : |
| 227 | healList xs q = List.replicate xs.length q := by |
| 228 | induction xs with |
| 229 | | nil => rfl |
| 230 | | cons y ys ih => |
| 231 | simp only [healList, List.map_cons, List.length_cons, List.replicate_succ] |
| 232 | have hhead : (if y = q then y else q) = q := by |
| 233 | by_cases h : y = q <;> simp [h] |
| 234 | rw [hhead] |
| 235 | congr 1 |
| 236 | |
| 237 | private theorem countList_replicate {D : Type} [DecidableEq D] (n : Nat) (x : D) : |
| 238 | countList (List.replicate n x) x = n := by |
| 239 | induction n with |
| 240 | | zero => rfl |
| 241 | | succ k ih => simp [List.replicate_succ, countList, ih]; omega |
| 242 | |
| 243 | private theorem faultList_replicate {D : Type} [DecidableEq D] (n : Nat) (x : D) : |
| 244 | faultList (List.replicate n x) x = 0 := by |
| 245 | induction n with |
| 246 | | zero => rfl |
| 247 | | succ k ih => simp [List.replicate_succ, faultList, ih] |
| 248 | |
| 249 | /-- Composed End-to-End BFT Healing Theorem: |
| 250 | Running computable quorum election `electQuorumN f b` followed by quarantine + |
| 251 | cold regrowth `healBraidOpt` ALWAYS recovers the exact unanimous canonical braid |
| 252 | whenever `n ≥ 3*f + 1` and `faultCountN b canon ≤ f`. -/ |
| 253 | theorem bftN_elect_and_heal_restores_unanimity {D : Type} [DecidableEq D] {n f : Nat} |
| 254 | (hn : n ≥ 3 * f + 1) (b : BraidN n D) (canon : D) |
| 255 | (hf : faultCountN b canon ≤ f) : |
| 256 | healBraidOpt b (electQuorumN f b) = some (unanimousN n canon) := by |
| 257 | rw [electQuorumN_recovers_canon hn b canon hf] |
| 258 | simp only [healBraidOpt, healBraidN, unanimousN] |
| 259 | congr 1 |
| 260 | cases b with |
| 261 | | mk strands hlen => |
| 262 | simp only [BraidN.mk.injEq] |
| 263 | rw [healList_eq_replicate, hlen] |
| 264 | |
| 265 | theorem unanimousN_counts {D : Type} [DecidableEq D] (n : Nat) (canon : D) : |
| 266 | countMatchesN (unanimousN n canon) canon = n ∧ |
| 267 | faultCountN (unanimousN n canon) canon = 0 := by |
| 268 | exact ⟨countList_replicate n canon, faultList_replicate n canon⟩ |
| 269 | |
| 270 | -- ==================== INSTANTIATIONS: N = 4 (f = 1) AND N = 7 (f = 2) ==================== |
| 271 | |
| 272 | /-- Instantiation for N = 4, f = 1 (threshold = 3/4). -/ |
| 273 | theorem braid4_bft_spec {D : Type} [DecidableEq D] (b : BraidN 4 D) (canon : D) |
| 274 | (hf : faultCountN b canon ≤ 1) : |
| 275 | electQuorumN 1 b = some canon ∧ |
| 276 | healBraidOpt b (electQuorumN 1 b) = some (unanimousN 4 canon) := by |
| 277 | have hn : 4 ≥ 3 * 1 + 1 := by omega |
| 278 | exact ⟨electQuorumN_recovers_canon hn b canon hf, |
| 279 | bftN_elect_and_heal_restores_unanimity hn b canon hf⟩ |
| 280 | |
| 281 | /-- Instantiation for N = 7, f = 2 (threshold = 5/7, surviving ANY 2 simultaneous Byzantine faults). -/ |
| 282 | theorem braid7_bft_spec {D : Type} [DecidableEq D] (b : BraidN 7 D) (canon : D) |
| 283 | (hf : faultCountN b canon ≤ 2) : |
| 284 | electQuorumN 2 b = some canon ∧ |
| 285 | healBraidOpt b (electQuorumN 2 b) = some (unanimousN 7 canon) := by |
| 286 | have hn : 7 ≥ 3 * 2 + 1 := by omega |
| 287 | exact ⟨electQuorumN_recovers_canon hn b canon hf, |
| 288 | bftN_elect_and_heal_restores_unanimity hn b canon hf⟩ |
| 289 | |
| 290 | /-- Non-Vacuity Negative Control for N = 7, f = 2: |
| 291 | with 3 simultaneous Byzantine faults (`f + 1 = 3`), NO digest reaches 5/7 quorum |
| 292 | and `electQuorumN 2` halts fail-closed (`none`), proving `N = 3f + 1 = 7` is tight! -/ |
| 293 | theorem bft7_triple_fault_counterexample : |
| 294 | let b : BraidN 7 Nat := ⟨[1, 1, 1, 1, 2, 2, 3], rfl⟩ |
| 295 | ¬ hasQuorumN 2 b 1 ∧ ¬ hasQuorumN 2 b 2 ∧ electQuorumN 2 b = none := by |
| 296 | dsimp [hasQuorumN, countMatchesN, electQuorumN, findQuorumAux, countList] |
| 297 | decide |
| 298 | |
| 299 | -- ==================== LEGACY Braid4 STRUCT + DIRECT electQuorum COMPOSITION ==================== |
| 300 | |
| 301 | /-- 4-strand braid of canonical compiler or kernel digests. -/ |
| 302 | structure Braid4 (D : Type) where |
| 303 | monk : D |
| 304 | predator : D |
| 305 | trickster : D |
| 306 | ascetic : D |
| 307 | deriving DecidableEq |
| 308 | |
| 309 | def Braid4.toBraidN {D : Type} (b : Braid4 D) : BraidN 4 D := |
| 310 | ⟨[b.monk, b.predator, b.trickster, b.ascetic], rfl⟩ |
| 311 | |
| 312 | def unanimous {D : Type} (x : D) : Braid4 D := |
| 313 | { monk := x, predator := x, trickster := x, ascetic := x } |
| 314 | |
| 315 | def countMatches {D : Type} [DecidableEq D] (b : Braid4 D) (x : D) : Nat := |
| 316 | (if b.monk = x then 1 else 0) + |
| 317 | (if b.predator = x then 1 else 0) + |
| 318 | (if b.trickster = x then 1 else 0) + |
| 319 | (if b.ascetic = x then 1 else 0) |
| 320 | |
| 321 | /-- Byzantine quorum predicate for N = 4, f = 1 (requires >= 2f + 1 = 3 agreeing strands). -/ |
| 322 | def hasQuorum {D : Type} [DecidableEq D] (b : Braid4 D) (x : D) : Prop := |
| 323 | countMatches b x ≥ 3 |
| 324 | |
| 325 | /-- Number of strands disagreeing with the canonical truth `canon`. -/ |
| 326 | def faultCount {D : Type} [DecidableEq D] (b : Braid4 D) (canon : D) : Nat := |
| 327 | (if b.monk = canon then 0 else 1) + |
| 328 | (if b.predator = canon then 0 else 1) + |
| 329 | (if b.trickster = canon then 0 else 1) + |
| 330 | (if b.ascetic = canon then 0 else 1) |
| 331 | |
| 332 | private theorem braid4_count_eq {D : Type} [DecidableEq D] (b : Braid4 D) (x : D) : |
| 333 | countMatches b x = countMatchesN b.toBraidN x := by |
| 334 | simp [countMatches, countMatchesN, Braid4.toBraidN, countList] |
| 335 | omega |
| 336 | |
| 337 | private theorem braid4_fault_eq {D : Type} [DecidableEq D] (b : Braid4 D) (canon : D) : |
| 338 | faultCount b canon = faultCountN b.toBraidN canon := by |
| 339 | simp [faultCount, faultCountN, Braid4.toBraidN, faultList] |
| 340 | omega |
| 341 | |
| 342 | /-- At most one digest can hold a 3/4 Byzantine quorum. -/ |
| 343 | theorem bft_quorum_unique {D : Type} [DecidableEq D] (b : Braid4 D) (x y : D) |
| 344 | (hx : hasQuorum b x) (hy : hasQuorum b y) : x = y := by |
| 345 | have hx' : hasQuorumN 1 b.toBraidN x := by |
| 346 | unfold hasQuorumN; rw [← braid4_count_eq]; exact hx |
| 347 | have hy' : hasQuorumN 1 b.toBraidN y := by |
| 348 | unfold hasQuorumN; rw [← braid4_count_eq]; exact hy |
| 349 | exact bftN_quorum_unique (by omega) b.toBraidN x y hx' hy' |
| 350 | |
| 351 | /-- BFT Survival Theorem for N = 4, f = 1. -/ |
| 352 | theorem bft_survives_single_fault {D : Type} [DecidableEq D] (b : Braid4 D) (canon bad : D) |
| 353 | (hf : faultCount b canon ≤ 1) (hbad : bad ≠ canon) : |
| 354 | hasQuorum b canon ∧ ¬ hasQuorum b bad := by |
| 355 | rw [braid4_fault_eq] at hf |
| 356 | have h := bftN_survives_f_faults (by omega) b.toBraidN canon bad hf hbad |
| 357 | unfold hasQuorum hasQuorumN at * |
| 358 | rw [braid4_count_eq, braid4_count_eq] |
| 359 | exact h |
| 360 | |
| 361 | /-- Computable 4-strand quorum election (returns `some q` iff `countMatches b q ≥ 3`). -/ |
| 362 | def electQuorum {D : Type} [DecidableEq D] (b : Braid4 D) : Option D := |
| 363 | electQuorumN 1 b.toBraidN |
| 364 | |
| 365 | /-- Quarantine + Cold Regrowth operator driven by elected `Option D`. -/ |
| 366 | def healBraid {D : Type} [DecidableEq D] (b : Braid4 D) (q : D) : Braid4 D := |
| 367 | { monk := if b.monk = q then b.monk else q |
| 368 | predator := if b.predator = q then b.predator else q |
| 369 | trickster := if b.trickster = q then b.trickster else q |
| 370 | ascetic := if b.ascetic = q then b.ascetic else q } |
| 371 | |
| 372 | def healBraid4Opt {D : Type} [DecidableEq D] (b : Braid4 D) : Option D → Option (Braid4 D) |
| 373 | | none => none |
| 374 | | some q => some (healBraid b q) |
| 375 | |
| 376 | /-- `electQuorum` recovers `some canon` for any single-strand fault (genuinely uses `hf`). -/ |
| 377 | theorem electQuorum_recovers_canon {D : Type} [DecidableEq D] (b : Braid4 D) (canon : D) |
| 378 | (hf : faultCount b canon ≤ 1) : |
| 379 | electQuorum b = some canon := by |
| 380 | rw [braid4_fault_eq] at hf |
| 381 | exact electQuorumN_recovers_canon (by omega) b.toBraidN canon hf |
| 382 | |
| 383 | /-- Direct composition: `healBraid4Opt b (electQuorum b) = some (unanimous canon)` and |
| 384 | exact 4/4 unanimity is restored without any vacuous `have _ := hf`. -/ |
| 385 | theorem bft_quarantine_regrow_restores_unanimity {D : Type} [DecidableEq D] |
| 386 | (b : Braid4 D) (canon : D) (hf : faultCount b canon ≤ 1) : |
| 387 | healBraid4Opt b (electQuorum b) = some (unanimous canon) ∧ |
| 388 | (∀ q, electQuorum b = some q → |
| 389 | healBraid b q = unanimous canon ∧ |
| 390 | countMatches (healBraid b q) canon = 4 ∧ |
| 391 | faultCount (healBraid b q) canon = 0) := by |
| 392 | have helec : electQuorum b = some canon := electQuorum_recovers_canon b canon hf |
| 393 | have hheal : healBraid b canon = unanimous canon := by |
| 394 | unfold healBraid unanimous |
| 395 | by_cases hm : b.monk = canon <;> |
| 396 | by_cases hp : b.predator = canon <;> |
| 397 | by_cases ht : b.trickster = canon <;> |
| 398 | by_cases ha : b.ascetic = canon <;> |
| 399 | simp [hm, hp, ht, ha] |
| 400 | refine ⟨?_, ?_⟩ |
| 401 | · simp [helec, healBraid4Opt, hheal] |
| 402 | · intro q hq |
| 403 | rw [helec] at hq |
| 404 | injection hq with hqc |
| 405 | subst hqc |
| 406 | refine ⟨hheal, ?_, ?_⟩ |
| 407 | · rw [hheal]; simp [unanimous, countMatches] |
| 408 | · rw [hheal]; simp [unanimous, faultCount] |
| 409 | |
| 410 | -- ==================== ARBORIS PREDATOR <-> ASCETIC HOT-SWAP SOUNDNESS ==================== |
| 411 | |
| 412 | inductive PowerMode where |
| 413 | | predator -- wall power / cool: maximize tokens/sec |
| 414 | | ascetic -- thermal throttle / battery / edge: minimize joules/token |
| 415 | deriving DecidableEq, Repr |
| 416 | |
| 417 | /-- Hot-swapping mid-workload between Predator and Ascetic kernels preserves exact semantics |
| 418 | across any sequence of power-mode switches, provided both kernels are Monk-proved equivalent |
| 419 | to the reference transition function `refStep`. -/ |
| 420 | def runWorkload {S : Type} (predStep ascStep : S → S) : List PowerMode → S → S |
| 421 | | [], s => s |
| 422 | | .predator :: rest, s => runWorkload predStep ascStep rest (predStep s) |
| 423 | | .ascetic :: rest, s => runWorkload predStep ascStep rest (ascStep s) |
| 424 | |
| 425 | def runRef {S : Type} (refStep : S → S) : Nat → S → S |
| 426 | | 0, s => s |
| 427 | | n + 1, s => runRef refStep n (refStep s) |
| 428 | |
| 429 | theorem ascetic_predator_hotswap_sound |
| 430 | {S : Type} (refStep predStep ascStep : S → S) |
| 431 | (hpred : ∀ s, predStep s = refStep s) |
| 432 | (hasc : ∀ s, ascStep s = refStep s) |
| 433 | (modes : List PowerMode) (s0 : S) : |
| 434 | runWorkload predStep ascStep modes s0 = runRef refStep modes.length s0 := by |
| 435 | induction modes generalizing s0 with |
| 436 | | nil => rfl |
| 437 | | cons m rest ih => |
| 438 | cases m with |
| 439 | | predator => |
| 440 | simp only [runWorkload, List.length_cons, runRef, hpred] |
| 441 | exact ih (refStep s0) |
| 442 | | ascetic => |
| 443 | simp only [runWorkload, List.length_cons, runRef, hasc] |
| 444 | exact ih (refStep s0) |
| 445 | |
| 446 | -- ==================== COMPILER MITOSIS MONOTONICITY & SAFETY ==================== |
| 447 | |
| 448 | structure CandidateStrand where |
| 449 | monkProved : Bool |
| 450 | tricksterClean : Bool |
| 451 | metricCost : Nat -- lower is better (e.g., us/tok or mJ/tok) |
| 452 | |
| 453 | structure BraidChampion where |
| 454 | safe : Bool |
| 455 | metricCost : Nat |
| 456 | |
| 457 | /-- Mitosis admission gate: a freshly spawned child strand earns its place in the braid |
| 458 | IFF it passes Monk's proof gate AND Trickster's chaos test AND strictly beats incumbent cost; |
| 459 | otherwise it is retired and the incumbent stays untouched. -/ |
| 460 | def mitosisStep (inc : BraidChampion) (child : CandidateStrand) : BraidChampion := |
| 461 | if child.monkProved && child.tricksterClean && decide (child.metricCost < inc.metricCost) then |
| 462 | { safe := true, metricCost := child.metricCost } |
| 463 | else |
| 464 | inc |
| 465 | |
| 466 | theorem mitosis_monotonic_and_safe (inc : BraidChampion) (child : CandidateStrand) |
| 467 | (hinc : inc.safe = true) : |
| 468 | (mitosisStep inc child).safe = true ∧ |
| 469 | (mitosisStep inc child).metricCost ≤ inc.metricCost := by |
| 470 | unfold mitosisStep |
| 471 | split |
| 472 | · rename_i hcond |
| 473 | simp only [Bool.and_eq_true, decide_eq_true_eq] at hcond |
| 474 | exact ⟨rfl, Nat.le_of_lt hcond.2⟩ |
| 475 | · exact ⟨hinc, Nat.le_refl _⟩ |
| 476 | |
| 477 | /-- Non-Vacuity Negative Control for N = 4, f = 1: |
| 478 | with f = 2 simultaneous Byzantine faults on N = 4 strands, NO digest holds a 3/4 quorum |
| 479 | and `electQuorum` halts fail-closed (`none`). -/ |
| 480 | theorem bft_double_fault_counterexample : |
| 481 | let b : Braid4 Nat := { monk := 1, predator := 1, trickster := 2, ascetic := 2 } |
| 482 | ¬ hasQuorum b 1 ∧ ¬ hasQuorum b 2 ∧ electQuorum b = none := by |
| 483 | dsimp [hasQuorum, countMatches, electQuorum, electQuorumN, Braid4.toBraidN, findQuorumAux, countList] |
| 484 | decide |
| 485 | |
| 486 | #print axioms bftN_quorum_unique |
| 487 | #print axioms bftN_survives_f_faults |
| 488 | #print axioms electQuorumN_recovers_canon |
| 489 | #print axioms bftN_elect_and_heal_restores_unanimity |
| 490 | #print axioms braid4_bft_spec |
| 491 | #print axioms braid7_bft_spec |
| 492 | #print axioms bft7_triple_fault_counterexample |
| 493 | #print axioms bft_quorum_unique |
| 494 | #print axioms bft_survives_single_fault |
| 495 | #print axioms electQuorum_recovers_canon |
| 496 | #print axioms bft_quarantine_regrow_restores_unanimity |
| 497 | #print axioms ascetic_predator_hotswap_sound |
| 498 | #print axioms mitosis_monotonic_and_safe |
| 499 | #print axioms bft_double_fault_counterexample |
| 500 | |
| 501 | end HelixBFT |