aien-train
AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished.
Brought by drakestapleton
aien-train / evidence / BTWAVE-P0 / lean / BtWaveProofs.lean
| 1 | /- |
| 2 | SPDX-License-Identifier: AGPL-3.0-only |
| 3 | BTWAVE-P0: Formal Proofs & Load-Bearing Negative Controls |
| 4 | (evidence/BTWAVE-P0/lean/BtWaveProofs.lean). |
| 5 | |
| 6 | Machine-checked theorems matching native/btwave/{trit.om,ledger.om}: |
| 7 | 1. Balanced-ternary M=6 codec on [-364, 364] (3^6 = 729 states): |
| 8 | disjoint bitplane validity, two-sided bijection, 1-op ROR negation |
| 9 | symmetry, Dark Zero (active carrier count = 0 <-> u = 0), and |
| 10 | constructive M=3 (3^3 = 27 > 16) 16/16 hypercube corner reconstruction. |
| 11 | 2. SWAR (pos, neg) bitplane algebra isomorphism (negation, zero-multiply |
| 12 | wave product, popcount-difference inner product) and Sylvester H16 |
| 13 | biorthogonal wave code minimum Hamming distance 8 -> unique nearest |
| 14 | decoding for all <= 3 carrier corruptions. |
| 15 | 3. Inductive Give-Before-Take reciprocal ledger safety invariant |
| 16 | (extracted <= contributed across every arbitrary event trace, zero |
| 17 | parasitic extraction prior to verified contribution, refusal preservation |
| 18 | of (epoch, contributed, extracted) in the numeric admission-bit model) |
| 19 | plus signed 64-bit wave_eval overflow safety under |scale| <= VMAX = 65536. |
| 20 | 4. Load-bearing negative controls: |
| 21 | (a) Any 4 central hyperplanes in R^3 realize at most 14 < 16 strict |
| 22 | sign orthants (binary NL1 obstruction resolved by M=3 trits); |
| 23 | (b) 4 carrier flips in H16 create an exact distance-4 tie between |
| 24 | distinct codewords (justifying fail-closed refusal); |
| 25 | (c) Dropping the reciprocity guard violates extracted <= contributed; |
| 26 | (d) Scale 2^62 at wave sum 120 overflows signed 64-bit arithmetic. |
| 27 | -/ |
| 28 | import Mathlib.Tactic |
| 29 | import Mathlib.LinearAlgebra.Dimension.Finrank |
| 30 | |
| 31 | namespace BtWaveP0 |
| 32 | |
| 33 | /-! ## Section 1: Balanced-Ternary M=6 Bijection, Negation, Dark Zero & M=3 16/16 Hypercube -/ |
| 34 | |
| 35 | /-- Valuation of a single balanced-ternary SWAR lane (pos, neg) in {-1, 0, +1}. -/ |
| 36 | def laneVal (p : Bool × Bool) : ℤ := |
| 37 | (if p.1 then 1 else 0) - (if p.2 then 1 else 0) |
| 38 | |
| 39 | /-- Disjointness invariant matching Trit32mk postcondition (pos & neg) == 0. -/ |
| 40 | def laneValid (p : Bool × Bool) : Prop := |
| 41 | ¬ (p.1 = true ∧ p.2 = true) |
| 42 | |
| 43 | instance (p : Bool × Bool) : Decidable (laneValid p) := by |
| 44 | unfold laneValid; infer_instance |
| 45 | |
| 46 | /-- One digit of bt6enc from z = u + 364 at radix 3^k: |
| 47 | r = (z / 3^k) % 3; pos = (r == 2), neg = (r == 0). -/ |
| 48 | def btDigit (z : ℤ) (k : ℕ) : Bool × Bool := |
| 49 | let r := (z / (3 : ℤ) ^ k) % 3 |
| 50 | (decide (r = 2), decide (r = 0)) |
| 51 | |
| 52 | /-- M=6 balanced-ternary encoder on [-364, 364] matching bt6enc in trit.om. -/ |
| 53 | def bt6Enc (u : ℤ) : Fin 6 → (Bool × Bool) := |
| 54 | fun k => btDigit (u + 364) k.val |
| 55 | |
| 56 | /-- M=6 balanced-ternary decoder matching bt6dec in trit.om. -/ |
| 57 | def bt6Dec (w : Fin 6 → (Bool × Bool)) : ℤ := |
| 58 | laneVal (w 0) * 1 + |
| 59 | laneVal (w 1) * 3 + |
| 60 | laneVal (w 2) * 9 + |
| 61 | laneVal (w 3) * 27 + |
| 62 | laneVal (w 4) * 81 + |
| 63 | laneVal (w 5) * 243 |
| 64 | |
| 65 | /-- 1-op balanced-ternary negation (swapping pos and neg bitplanes via ROR #32). -/ |
| 66 | def bt6Neg (w : Fin 6 → (Bool × Bool)) : Fin 6 → (Bool × Bool) := |
| 67 | fun k => ((w k).2, (w k).1) |
| 68 | |
| 69 | /-- Active non-zero carrier count matching t32pow in trit.om. -/ |
| 70 | def bt6Pow (w : Fin 6 → (Bool × Bool)) : ℕ := |
| 71 | (if (w 0).1 || (w 0).2 then 1 else 0) + |
| 72 | (if (w 1).1 || (w 1).2 then 1 else 0) + |
| 73 | (if (w 2).1 || (w 2).2 then 1 else 0) + |
| 74 | (if (w 3).1 || (w 3).2 then 1 else 0) + |
| 75 | (if (w 4).1 || (w 4).2 then 1 else 0) + |
| 76 | (if (w 5).1 || (w 5).2 then 1 else 0) |
| 77 | |
| 78 | lemma btDigit_valid_and_val (z : ℤ) (k : ℕ) (hz : 0 ≤ z) : |
| 79 | laneValid (btDigit z k) ∧ laneVal (btDigit z k) = (z / (3 : ℤ) ^ k) % 3 - 1 := by |
| 80 | unfold btDigit laneValid laneVal |
| 81 | have hp : (0 : ℤ) < (3 : ℤ) ^ k := by positivity |
| 82 | have hdn : 0 ≤ z / (3 : ℤ) ^ k := Int.ediv_nonneg hz (le_of_lt hp) |
| 83 | have hr0 : 0 ≤ (z / (3 : ℤ) ^ k) % 3 := Int.emod_nonneg _ (by norm_num) |
| 84 | have hr3 : (z / (3 : ℤ) ^ k) % 3 < 3 := Int.emod_lt_of_pos _ (by norm_num) |
| 85 | set r := (z / (3 : ℤ) ^ k) % 3 |
| 86 | have hrc : r = 0 ∨ r = 1 ∨ r = 2 := by omega |
| 87 | rcases hrc with h | h | h <;> simp [h] |
| 88 | |
| 89 | lemma laneVal_bounds {p : Bool × Bool} (hv : laneValid p) : |
| 90 | p = (false, false) ∧ laneVal p = 0 ∨ |
| 91 | p = (true, false) ∧ laneVal p = 1 ∨ |
| 92 | p = (false, true) ∧ laneVal p = -1 := by |
| 93 | rcases p with ⟨b1, b2⟩ |
| 94 | unfold laneValid laneVal at * |
| 95 | cases b1 <;> cases b2 <;> simp at hv ⊢ |
| 96 | |
| 97 | lemma laneVal_le {p : Bool × Bool} (hv : laneValid p) : |
| 98 | -1 ≤ laneVal p ∧ laneVal p ≤ 1 := by |
| 99 | rcases laneVal_bounds hv with ⟨_, e⟩ | ⟨_, e⟩ | ⟨_, e⟩ <;> omega |
| 100 | |
| 101 | /-- Theorem 1a: bt6Enc produces disjoint (pos, neg) bitplanes and bt6Dec is its exact left inverse |
| 102 | on all 729 integers in [-364, 364]. -/ |
| 103 | theorem bt6_valid_and_left_inv (u : ℤ) (hu1 : -364 ≤ u) (hu2 : u ≤ 364) : |
| 104 | (∀ k : Fin 6, laneValid (bt6Enc u k)) ∧ bt6Dec (bt6Enc u) = u := by |
| 105 | have hz0 : 0 ≤ u + 364 := by omega |
| 106 | have hz1 : u + 364 < 729 := by omega |
| 107 | refine ⟨fun k => (btDigit_valid_and_val (u + 364) k.val hz0).1, ?_⟩ |
| 108 | change laneVal (btDigit (u + 364) 0) * 1 + |
| 109 | laneVal (btDigit (u + 364) 1) * 3 + |
| 110 | laneVal (btDigit (u + 364) 2) * 9 + |
| 111 | laneVal (btDigit (u + 364) 3) * 27 + |
| 112 | laneVal (btDigit (u + 364) 4) * 81 + |
| 113 | laneVal (btDigit (u + 364) 5) * 243 = u |
| 114 | rw [(btDigit_valid_and_val (u + 364) 0 hz0).2, |
| 115 | (btDigit_valid_and_val (u + 364) 1 hz0).2, |
| 116 | (btDigit_valid_and_val (u + 364) 2 hz0).2, |
| 117 | (btDigit_valid_and_val (u + 364) 3 hz0).2, |
| 118 | (btDigit_valid_and_val (u + 364) 4 hz0).2, |
| 119 | (btDigit_valid_and_val (u + 364) 5 hz0).2] |
| 120 | norm_num |
| 121 | omega |
| 122 | |
| 123 | /-- Theorem 1b: bt6Enc is also the exact right inverse of bt6Dec on all valid 6-trit words, |
| 124 | establishing a two-sided bijection between valid 6-trit bitplanes and [-364, 364]. -/ |
| 125 | theorem bt6_right_inv (w : Fin 6 → (Bool × Bool)) (hw : ∀ k : Fin 6, laneValid (w k)) : |
| 126 | (-364 ≤ bt6Dec w ∧ bt6Dec w ≤ 364) ∧ bt6Enc (bt6Dec w) = w := by |
| 127 | have wd0 := laneVal_le (hw 0) |
| 128 | have wd1 := laneVal_le (hw 1) |
| 129 | have wd2 := laneVal_le (hw 2) |
| 130 | have wd3 := laneVal_le (hw 3) |
| 131 | have wd4 := laneVal_le (hw 4) |
| 132 | have wd5 := laneVal_le (hw 5) |
| 133 | have hbnd : -364 ≤ bt6Dec w ∧ bt6Dec w ≤ 364 := by |
| 134 | unfold bt6Dec; omega |
| 135 | refine ⟨hbnd, ?_⟩ |
| 136 | funext k |
| 137 | have hdec_eq : bt6Dec (bt6Enc (bt6Dec w)) = bt6Dec w := |
| 138 | (bt6_valid_and_left_inv (bt6Dec w) hbnd.1 hbnd.2).2 |
| 139 | have henc_val : ∀ j : Fin 6, laneValid (bt6Enc (bt6Dec w) j) := |
| 140 | (bt6_valid_and_left_inv (bt6Dec w) hbnd.1 hbnd.2).1 |
| 141 | set v := bt6Enc (bt6Dec w) |
| 142 | have hd0 := laneVal_le (henc_val 0) |
| 143 | have hd1 := laneVal_le (henc_val 1) |
| 144 | have hd2 := laneVal_le (henc_val 2) |
| 145 | have hd3 := laneVal_le (henc_val 3) |
| 146 | have hd4 := laneVal_le (henc_val 4) |
| 147 | have hd5 := laneVal_le (henc_val 5) |
| 148 | unfold bt6Dec at hdec_eq |
| 149 | have eq_vals : laneVal (v k) = laneVal (w k) := by |
| 150 | fin_cases k <;> simp <;> omega |
| 151 | have vk_b := laneVal_bounds (henc_val k) |
| 152 | have wk_b := laneVal_bounds (hw k) |
| 153 | rcases vk_b with ⟨ev, lv⟩ | ⟨ev, lv⟩ | ⟨ev, lv⟩ <;> |
| 154 | rcases wk_b with ⟨ew, lw⟩ | ⟨ew, lw⟩ | ⟨ew, lw⟩ <;> |
| 155 | simp_all |
| 156 | |
| 157 | /-- Theorem 1c: 1-op ROR bitplane swap negates every balanced-ternary codeword on [-364, 364]. -/ |
| 158 | theorem bt6_neg_sym (u : ℤ) (hu1 : -364 ≤ u) (hu2 : u ≤ 364) : |
| 159 | bt6Enc (-u) = bt6Neg (bt6Enc u) ∧ bt6Dec (bt6Neg (bt6Enc u)) = -u := by |
| 160 | have hvalid : ∀ k : Fin 6, laneValid (bt6Neg (bt6Enc u) k) := by |
| 161 | intro k |
| 162 | have hk := (bt6_valid_and_left_inv u hu1 hu2).1 k |
| 163 | unfold bt6Neg laneValid at * |
| 164 | tauto |
| 165 | have hdec : bt6Dec (bt6Neg (bt6Enc u)) = -u := by |
| 166 | have hlinv := (bt6_valid_and_left_inv u hu1 hu2).2 |
| 167 | have hneg_lane : ∀ p : Bool × Bool, laneVal (p.2, p.1) = -laneVal p := by |
| 168 | intro ⟨b1, b2⟩; unfold laneVal; ring |
| 169 | unfold bt6Dec bt6Neg at * |
| 170 | simp only [hneg_lane] |
| 171 | linarith |
| 172 | refine ⟨?_, hdec⟩ |
| 173 | have hrinv := (bt6_right_inv (bt6Neg (bt6Enc u)) hvalid).2 |
| 174 | rw [hdec] at hrinv |
| 175 | exact hrinv |
| 176 | |
| 177 | /-- Theorem 1d (Dark Consensus): Active carrier power is 0 if and only if u = 0. -/ |
| 178 | theorem bt6_dark_zero (u : ℤ) (hu1 : -364 ≤ u) (hu2 : u ≤ 364) : |
| 179 | bt6Pow (bt6Enc u) = 0 ↔ u = 0 := by |
| 180 | constructor |
| 181 | · intro hp |
| 182 | unfold bt6Pow at hp |
| 183 | have z0 : (bt6Enc u 0).1 = false ∧ (bt6Enc u 0).2 = false := by |
| 184 | cases h1 : (bt6Enc u 0).1 <;> cases h2 : (bt6Enc u 0).2 <;> simp [h1, h2] at hp ⊢ |
| 185 | have z1 : (bt6Enc u 1).1 = false ∧ (bt6Enc u 1).2 = false := by |
| 186 | cases h1 : (bt6Enc u 1).1 <;> cases h2 : (bt6Enc u 1).2 <;> simp [h1, h2] at hp ⊢ |
| 187 | have z2 : (bt6Enc u 2).1 = false ∧ (bt6Enc u 2).2 = false := by |
| 188 | cases h1 : (bt6Enc u 2).1 <;> cases h2 : (bt6Enc u 2).2 <;> simp [h1, h2] at hp ⊢ |
| 189 | have z3 : (bt6Enc u 3).1 = false ∧ (bt6Enc u 3).2 = false := by |
| 190 | cases h1 : (bt6Enc u 3).1 <;> cases h2 : (bt6Enc u 3).2 <;> simp [h1, h2] at hp ⊢ |
| 191 | have z4 : (bt6Enc u 4).1 = false ∧ (bt6Enc u 4).2 = false := by |
| 192 | cases h1 : (bt6Enc u 4).1 <;> cases h2 : (bt6Enc u 4).2 <;> simp [h1, h2] at hp ⊢ |
| 193 | have z5 : (bt6Enc u 5).1 = false ∧ (bt6Enc u 5).2 = false := by |
| 194 | cases h1 : (bt6Enc u 5).1 <;> cases h2 : (bt6Enc u 5).2 <;> simp [h1, h2] at hp ⊢ |
| 195 | have hlinv := (bt6_valid_and_left_inv u hu1 hu2).2 |
| 196 | unfold bt6Dec laneVal at hlinv |
| 197 | simp [z0.1, z0.2, z1.1, z1.2, z2.1, z2.2, z3.1, z3.2, z4.1, z4.2, z5.1, z5.2] at hlinv |
| 198 | omega |
| 199 | · rintro rfl |
| 200 | decide |
| 201 | |
| 202 | /-- M=3 balanced-ternary hypercube encoder & decoder matching hc3enc / hc3dec in trit.om. -/ |
| 203 | def hc3Enc (m : Fin 16) : Fin 3 → (Bool × Bool) := |
| 204 | fun k => btDigit ((m.val : ℤ) + 6) k.val |
| 205 | |
| 206 | def hc3Dec (w : Fin 3 → (Bool × Bool)) : ℕ := |
| 207 | let u := laneVal (w 0) + 3 * laneVal (w 1) + 9 * laneVal (w 2) |
| 208 | if u < -7 ∨ 8 < u then 99 else Int.toNat (u + 7) |
| 209 | |
| 210 | /-- Theorem 1e: Constructive 3-trit (3^3 = 27 > 16) channel reconstructs all 16/16 4-bit corners. -/ |
| 211 | theorem hc3_reconstructs_16_of_16 : |
| 212 | ∀ m : Fin 16, (∀ k : Fin 3, laneValid (hc3Enc m k)) ∧ hc3Dec (hc3Enc m) = m.val := by |
| 213 | decide |
| 214 | |
| 215 | /-! ## Section 2: SWAR Bitplane Isomorphism & H16 Distance-8 Unique Decoding -/ |
| 216 | |
| 217 | /-- Single-lane SWAR zero-multiply wave product matching t32mul in trit.om: |
| 218 | pos' = (pa && pb) || (na && nb), neg' = (pa && nb) || (na && pb). -/ |
| 219 | def swarMulLane (a b : Bool × Bool) : Bool × Bool := |
| 220 | ((a.1 && b.1) || (a.2 && b.2), (a.1 && b.2) || (a.2 && b.1)) |
| 221 | |
| 222 | /-- Theorem 2a: SWAR bitplane product preserves disjointness and equals exact integer |
| 223 | multiplication in {-1, 0, +1}, and SWAR popcount difference equals the exact inner product. -/ |
| 224 | theorem swar_lane_mul_iso (a b : Bool × Bool) (ha : laneValid a) (hb : laneValid b) : |
| 225 | laneValid (swarMulLane a b) ∧ laneVal (swarMulLane a b) = laneVal a * laneVal b := by |
| 226 | rcases a with ⟨pa, na⟩ |
| 227 | rcases b with ⟨pb, nb⟩ |
| 228 | unfold laneValid laneVal swarMulLane at * |
| 229 | cases pa <;> cases na <;> cases pb <;> cases nb <;> simp at ha hb ⊢ |
| 230 | |
| 231 | theorem swar_popcount_dot_iso {n : ℕ} (a b : Fin n → (Bool × Bool)) |
| 232 | (ha : ∀ i, laneValid (a i)) (hb : ∀ i, laneValid (b i)) : |
| 233 | (∑ i : Fin n, (if (swarMulLane (a i) (b i)).1 then (1 : ℤ) else 0)) - |
| 234 | (∑ i : Fin n, (if (swarMulLane (a i) (b i)).2 then (1 : ℤ) else 0)) = |
| 235 | ∑ i : Fin n, laneVal (a i) * laneVal (b i) := by |
| 236 | rw [← Finset.sum_sub_distrib] |
| 237 | apply Finset.sum_congr rfl |
| 238 | intro i _ |
| 239 | exact (swar_lane_mul_iso (a i) (b i) (ha i) (hb i)).2 |
| 240 | |
| 241 | /-- Bitwise 4-bit dot product mod 2 for Sylvester Walsh-Hadamard H16 codewords. -/ |
| 242 | def par4 (r c : Fin 16) : Bool := |
| 243 | let m := r.val &&& c.val |
| 244 | xor (xor (decide (m &&& 1 ≠ 0)) (decide (m &&& 2 ≠ 0))) |
| 245 | (xor (decide (m &&& 4 ≠ 0)) (decide (m &&& 8 ≠ 0))) |
| 246 | |
| 247 | /-- Sylvester H16 biorthogonal codeword s in 0..31 across 16 carriers (false = +1, true = -1), |
| 248 | matching h16enc in trit.om. -/ |
| 249 | def h16Cw (s : Fin 32) (c : Fin 16) : Bool := |
| 250 | let r : Fin 16 := ⟨s.val % 16, Nat.mod_lt _ (by decide)⟩ |
| 251 | let pol : Bool := decide (16 ≤ s.val) |
| 252 | xor (par4 r c) pol |
| 253 | |
| 254 | /-- Hamming distance across the 16 Walsh-Hadamard carriers. -/ |
| 255 | def hamming16 (u v : Fin 16 → Bool) : ℕ := |
| 256 | ∑ c : Fin 16, if u c != v c then 1 else 0 |
| 257 | |
| 258 | lemma hamming16_triangle (u v w : Fin 16 → Bool) : |
| 259 | hamming16 u w ≤ hamming16 u v + hamming16 v w := by |
| 260 | unfold hamming16 |
| 261 | rw [← Finset.sum_add_distrib] |
| 262 | apply Finset.sum_le_sum |
| 263 | intro c _ |
| 264 | cases u c <;> cases v c <;> cases w c <;> decide |
| 265 | |
| 266 | /-- Theorem 2b: Every pair of distinct H16 codewords has Hamming distance at least 8. -/ |
| 267 | theorem h16_min_dist_eight : |
| 268 | ∀ s1 s2 : Fin 32, s1 ≠ s2 → 8 ≤ hamming16 (h16Cw s1) (h16Cw s2) := by |
| 269 | decide |
| 270 | |
| 271 | /-- Theorem 2c: Any received wave within <= 3 carrier corruptions of h16Cw s1 is at distance |
| 272 | >= 5 from every other codeword s2 != s1, guaranteeing unique nearest-neighbor decoding. -/ |
| 273 | theorem h16_unique_decode_le_three (rx : Fin 16 → Bool) (s1 s2 : Fin 32) |
| 274 | (hne : s1 ≠ s2) (herr : hamming16 rx (h16Cw s1) ≤ 3) : |
| 275 | 5 ≤ hamming16 rx (h16Cw s2) ∧ hamming16 rx (h16Cw s1) < hamming16 rx (h16Cw s2) := by |
| 276 | have hd : 8 ≤ hamming16 (h16Cw s1) (h16Cw s2) := h16_min_dist_eight s1 s2 hne |
| 277 | have hsym : hamming16 (h16Cw s1) rx = hamming16 rx (h16Cw s1) := by |
| 278 | unfold hamming16 |
| 279 | apply Finset.sum_congr rfl |
| 280 | intro c _; cases h16Cw s1 c <;> cases rx c <;> rfl |
| 281 | have htri : hamming16 (h16Cw s1) (h16Cw s2) ≤ |
| 282 | hamming16 (h16Cw s1) rx + hamming16 rx (h16Cw s2) := |
| 283 | hamming16_triangle (h16Cw s1) rx (h16Cw s2) |
| 284 | omega |
| 285 | |
| 286 | /-! ## Section 3: Give-Before-Take Reciprocal Ledger Invariant & Signed-64 Safety -/ |
| 287 | |
| 288 | structure LedgerState where |
| 289 | epoch : ℕ |
| 290 | contributed : ℕ |
| 291 | extracted : ℕ |
| 292 | deriving DecidableEq, Repr |
| 293 | |
| 294 | inductive LedgerEvent where |
| 295 | | give (reqEpoch work : ℕ) (admitted : Bool) |
| 296 | | take (reqEpoch grant : ℕ) (admitted : Bool) |
| 297 | | invalid |
| 298 | |
| 299 | def initLedger : LedgerState := ⟨0, 0, 0⟩ |
| 300 | |
| 301 | /-- Numeric model of epoch, contributed, and extracted. The admission bit abstracts codec, scale, and receipt checks. It does not model Lresp. -/ |
| 302 | def ledgerStep (st : LedgerState) (ev : LedgerEvent) : LedgerState := |
| 303 | match ev with |
| 304 | | .give reqEp work adm => |
| 305 | if reqEp = st.epoch + 1 ∧ 0 < work ∧ work ≤ 1000 ∧ adm = true then |
| 306 | ⟨reqEp, st.contributed + work, st.extracted⟩ |
| 307 | else |
| 308 | st |
| 309 | | .take reqEp grant adm => |
| 310 | if reqEp = st.epoch + 1 ∧ 0 < grant ∧ grant ≤ 1000 ∧ |
| 311 | 0 < st.contributed ∧ st.extracted + grant ≤ st.contributed ∧ adm = true then |
| 312 | ⟨reqEp, st.contributed, st.extracted + grant⟩ |
| 313 | else |
| 314 | st |
| 315 | | .invalid => st |
| 316 | |
| 317 | def ledgerRun (evs : List LedgerEvent) : LedgerState := |
| 318 | evs.foldl ledgerStep initLedger |
| 319 | |
| 320 | lemma ledgerStep_preserves (st : LedgerState) (ev : LedgerEvent) |
| 321 | (hinv : st.extracted ≤ st.contributed) : |
| 322 | (ledgerStep st ev).extracted ≤ (ledgerStep st ev).contributed := by |
| 323 | cases ev with |
| 324 | | give reqEp work adm => |
| 325 | simp only [ledgerStep] |
| 326 | split_ifs with h |
| 327 | · simp; omega |
| 328 | · exact hinv |
| 329 | | take reqEp grant adm => |
| 330 | simp only [ledgerStep] |
| 331 | split_ifs with h |
| 332 | · simp; omega |
| 333 | · exact hinv |
| 334 | | invalid => |
| 335 | exact hinv |
| 336 | |
| 337 | lemma ledgerFold_preserves (evs : List LedgerEvent) (st : LedgerState) |
| 338 | (hinv : st.extracted ≤ st.contributed) : |
| 339 | (evs.foldl ledgerStep st).extracted ≤ (evs.foldl ledgerStep st).contributed := by |
| 340 | induction evs generalizing st with |
| 341 | | nil => exact hinv |
| 342 | | cons e tail ih => |
| 343 | exact ih (ledgerStep st e) (ledgerStep_preserves st e hinv) |
| 344 | |
| 345 | /-- Theorem 3a: Across EVERY finite trace of events from initLedger, extracted <= contributed, |
| 346 | and positive extraction strictly requires prior positive verified contribution. -/ |
| 347 | theorem ledger_give_before_take_invariant (evs : List LedgerEvent) : |
| 348 | (ledgerRun evs).extracted ≤ (ledgerRun evs).contributed ∧ |
| 349 | (0 < (ledgerRun evs).extracted → 0 < (ledgerRun evs).contributed) := by |
| 350 | have h : (ledgerRun evs).extracted ≤ (ledgerRun evs).contributed := |
| 351 | ledgerFold_preserves evs initLedger (by decide) |
| 352 | exact ⟨h, fun hpos => by omega⟩ |
| 353 | |
| 354 | /-- Signed 64-bit range matching OM-2 checked long arithmetic. -/ |
| 355 | def fits64 (v : ℤ) : Prop := -(2 : ℤ) ^ 63 ≤ v ∧ v ≤ (2 : ℤ) ^ 63 - 1 |
| 356 | |
| 357 | /-- Theorem 3b: Under scale_bad guard (|sc| <= VMAX = 65536) and 32-trit inner products in |
| 358 | [-32, 32], every intermediate of wave_sum and wave_eval fits inside signed 64-bit range. -/ |
| 359 | theorem wave_eval_fits_signed64 (d0 d1 d2 d3 sc : ℤ) |
| 360 | (hd0 : |d0| ≤ 32) (hd1 : |d1| ≤ 32) (hd2 : |d2| ≤ 32) (hd3 : |d3| ≤ 32) |
| 361 | (hsc : |sc| ≤ 65536) : |
| 362 | |d0 + 3 * d1 + 9 * d2 + 27 * d3| ≤ 1280 ∧ |
| 363 | |sc * (d0 + 3 * d1 + 9 * d2 + 27 * d3)| ≤ 83886080 ∧ |
| 364 | fits64 (sc * (d0 + 3 * d1 + 9 * d2 + 27 * d3)) := by |
| 365 | rw [abs_le] at hd0 hd1 hd2 hd3 hsc |
| 366 | have hws : -1280 ≤ d0 + 3 * d1 + 9 * d2 + 27 * d3 ∧ d0 + 3 * d1 + 9 * d2 + 27 * d3 ≤ 1280 := by |
| 367 | omega |
| 368 | have hprod : |sc * (d0 + 3 * d1 + 9 * d2 + 27 * d3)| ≤ 65536 * 1280 := by |
| 369 | rw [abs_mul] |
| 370 | exact mul_le_mul (abs_le.mpr hsc) (abs_le.mpr hws) (abs_nonneg _) (by norm_num) |
| 371 | refine ⟨abs_le.mpr hws, ?_, ?_⟩ |
| 372 | · linarith |
| 373 | · rw [abs_le] at hprod |
| 374 | unfold fits64 |
| 375 | constructor <;> norm_num <;> linarith |
| 376 | |
| 377 | /-! ## Section 4: Load-Bearing Negative Controls -/ |
| 378 | |
| 379 | /-- Dot product in R^3. -/ |
| 380 | def dotR3 (u v : Fin 3 → ℝ) : ℝ := |
| 381 | u 0 * v 0 + u 1 * v 1 + u 2 * v 2 |
| 382 | |
| 383 | /-- Point x in R^3 strictly realizes Boolean sign pattern sigma against 4 central hyperplanes n. -/ |
| 384 | def StrictlyRealizes (n : Fin 4 → Fin 3 → ℝ) (x : Fin 3 → ℝ) (sigma : Fin 4 → Bool) : Prop := |
| 385 | ∀ k : Fin 4, if sigma k then 0 < dotR3 (n k) x else dotR3 (n k) x < 0 |
| 386 | |
| 387 | /-- Negative Control 4a (Binary NL1 Obstruction): |
| 388 | Any 4 central hyperplanes in R^3 can strictly realize at most 14 < 16 sign patterns in |
| 389 | Fin 4 -> Bool (whereas M=3 balanced trits above realize all 16/16 corners). -/ |
| 390 | theorem nl1_four_central_hyperplanes_r3_at_most_14 |
| 391 | (n : Fin 4 → Fin 3 → ℝ) |
| 392 | (S : Finset (Fin 4 → Bool)) |
| 393 | (hS : ∀ sigma ∈ S, ∃ x : Fin 3 → ℝ, StrictlyRealizes n x sigma) : |
| 394 | S.card ≤ 14 := by |
| 395 | have hdep : ¬ LinearIndependent ℝ n := by |
| 396 | intro hli |
| 397 | have hcard := hli.fintype_card_le_finrank |
| 398 | rw [Fintype.card_fin, Module.finrank_fin_fun] at hcard |
| 399 | omega |
| 400 | rw [Fintype.not_linearIndependent_iff] at hdep |
| 401 | obtain ⟨c, hsum, k0, hk0⟩ := hdep |
| 402 | have hlin : ∀ x : Fin 3 → ℝ, ∑ k : Fin 4, c k * dotR3 (n k) x = 0 := by |
| 403 | intro x |
| 404 | have hj : ∀ j : Fin 3, ∑ k : Fin 4, c k * n k j = 0 := by |
| 405 | intro j |
| 406 | have := congr_fun hsum j |
| 407 | simpa [Finset.sum_apply, Pi.smul_apply, smul_eq_mul] using this |
| 408 | calc |
| 409 | ∑ k : Fin 4, c k * dotR3 (n k) x |
| 410 | = (∑ k : Fin 4, c k * n k 0) * x 0 + |
| 411 | (∑ k : Fin 4, c k * n k 1) * x 1 + |
| 412 | (∑ k : Fin 4, c k * n k 2) * x 2 := by |
| 413 | simp [Fin.sum_univ_four, dotR3]; ring |
| 414 | _ = 0 := by rw [hj 0, hj 1, hj 2]; ring |
| 415 | let sigPos : Fin 4 → Bool := fun k => decide (0 ≤ c k) |
| 416 | let sigNeg : Fin 4 → Bool := fun k => !decide (0 ≤ c k) |
| 417 | have hne : sigPos ≠ sigNeg := by |
| 418 | intro heq |
| 419 | have h0 := congr_fun heq 0 |
| 420 | simp [sigPos, sigNeg] at h0 |
| 421 | have hnotPos : sigPos ∉ S := by |
| 422 | intro hin |
| 423 | obtain ⟨x, hx⟩ := hS sigPos hin |
| 424 | have hterm_nonneg : ∀ k : Fin 4, 0 ≤ c k * dotR3 (n k) x := by |
| 425 | intro k |
| 426 | have hxk := hx k |
| 427 | by_cases hck : 0 ≤ c k |
| 428 | · simp [sigPos, hck] at hxk |
| 429 | exact mul_nonneg hck (le_of_lt hxk) |
| 430 | · simp [sigPos, hck] at hxk |
| 431 | have hck' : c k < 0 := lt_of_not_ge hck |
| 432 | exact le_of_lt (mul_pos_of_neg_of_neg hck' hxk) |
| 433 | have hterm_pos : 0 < c k0 * dotR3 (n k0) x := by |
| 434 | have hxk0 := hx k0 |
| 435 | rcases lt_trichotomy (c k0) 0 with hlt | heq0 | hgt |
| 436 | · have hck : ¬ (0 ≤ c k0) := not_le_of_gt hlt |
| 437 | simp [sigPos, hck] at hxk0 |
| 438 | exact mul_pos_of_neg_of_neg hlt hxk0 |
| 439 | · exact absurd heq0 hk0 |
| 440 | · have hck : 0 ≤ c k0 := le_of_lt hgt |
| 441 | simp [sigPos, hck] at hxk0 |
| 442 | exact mul_pos hgt hxk0 |
| 443 | have hsum_pos : 0 < ∑ k : Fin 4, c k * dotR3 (n k) x := |
| 444 | Finset.sum_pos' (fun k _ => hterm_nonneg k) ⟨k0, Finset.mem_univ k0, hterm_pos⟩ |
| 445 | linarith [hlin x] |
| 446 | have hnotNeg : sigNeg ∉ S := by |
| 447 | intro hin |
| 448 | obtain ⟨x, hx⟩ := hS sigNeg hin |
| 449 | have hterm_nonpos : ∀ k : Fin 4, c k * dotR3 (n k) x ≤ 0 := by |
| 450 | intro k |
| 451 | have hxk := hx k |
| 452 | by_cases hck : 0 ≤ c k |
| 453 | · simp [sigNeg, hck] at hxk |
| 454 | exact mul_nonpos_of_nonneg_of_nonpos hck (le_of_lt hxk) |
| 455 | · simp [sigNeg, hck] at hxk |
| 456 | exact le_of_lt (mul_neg_of_neg_of_pos (lt_of_not_ge hck) hxk) |
| 457 | have hterm_neg : c k0 * dotR3 (n k0) x < 0 := by |
| 458 | have hxk0 := hx k0 |
| 459 | rcases lt_trichotomy (c k0) 0 with hlt | heq0 | hgt |
| 460 | · have hck : ¬ (0 ≤ c k0) := not_le_of_gt hlt |
| 461 | simp [sigNeg, hck] at hxk0 |
| 462 | exact mul_neg_of_neg_of_pos hlt hxk0 |
| 463 | · exact absurd heq0 hk0 |
| 464 | · have hck : 0 ≤ c k0 := le_of_lt hgt |
| 465 | simp [sigNeg, hck] at hxk0 |
| 466 | exact mul_neg_of_pos_of_neg hgt hxk0 |
| 467 | have hsum_neg : ∑ k : Fin 4, c k * dotR3 (n k) x < 0 := |
| 468 | Finset.sum_neg' (fun k _ => hterm_nonpos k) ⟨k0, Finset.mem_univ k0, hterm_neg⟩ |
| 469 | linarith [hlin x] |
| 470 | have hsub : S ⊆ (Finset.univ \ {sigPos, sigNeg}) := by |
| 471 | intro s hs |
| 472 | simp only [Finset.mem_sdiff, Finset.mem_univ, true_and, Finset.mem_insert, Finset.mem_singleton] |
| 473 | rintro (rfl | rfl) |
| 474 | · exact hnotPos hs |
| 475 | · exact hnotNeg hs |
| 476 | have hcard_le := Finset.card_le_card hsub |
| 477 | have huniv : (Finset.univ : Finset (Fin 4 → Bool)).card = 16 := by simp |
| 478 | have hpair : ({sigPos, sigNeg} : Finset (Fin 4 → Bool)).card = 2 := Finset.card_pair hne |
| 479 | have hsdiff : (Finset.univ \ ({sigPos, sigNeg} : Finset (Fin 4 → Bool))).card = 14 := by |
| 480 | rw [Finset.card_sdiff, Finset.inter_univ, huniv, hpair] |
| 481 | omega |
| 482 | |
| 483 | /-- Received word formed by flipping 4 odd carriers (1, 3, 5, 7) of h16Cw 0. -/ |
| 484 | def rxFourFlipTie (c : Fin 16) : Bool := |
| 485 | if c.val = 1 ∨ c.val = 3 ∨ c.val = 5 ∨ c.val = 7 then !(h16Cw 0 c) else h16Cw 0 c |
| 486 | |
| 487 | /-- Negative Control 4b: 4 carrier corruptions in H16 produce an exact distance-4 tie between |
| 488 | distinct symbols 0 and 1, proving <= 3 unique decoding is tight. -/ |
| 489 | theorem h16_four_flip_tie_witness : |
| 490 | (0 : Fin 32) ≠ 1 ∧ |
| 491 | hamming16 rxFourFlipTie (h16Cw 0) = 4 ∧ |
| 492 | hamming16 rxFourFlipTie (h16Cw 1) = 4 := by |
| 493 | decide |
| 494 | |
| 495 | /-- Buggy ledger transition dropping the extracted + grant <= contributed guard (mutant m10). -/ |
| 496 | def buggyLedgerStep (st : LedgerState) (ev : LedgerEvent) : LedgerState := |
| 497 | match ev with |
| 498 | | .give reqEp work adm => |
| 499 | if reqEp = st.epoch + 1 ∧ 0 < work ∧ work ≤ 1000 ∧ adm = true then |
| 500 | ⟨reqEp, st.contributed + work, st.extracted⟩ |
| 501 | else st |
| 502 | | .take reqEp grant adm => |
| 503 | if reqEp = st.epoch + 1 ∧ 0 < grant ∧ grant ≤ 1000 ∧ |
| 504 | 0 < st.contributed ∧ adm = true then |
| 505 | ⟨reqEp, st.contributed, st.extracted + grant⟩ |
| 506 | else st |
| 507 | | .invalid => st |
| 508 | |
| 509 | /-- Negative Control 4c: Dropping the reciprocity guard violates extracted <= contributed. -/ |
| 510 | theorem dropping_reciprocity_guard_violates_invariant : |
| 511 | let trace := [LedgerEvent.give 1 50 true, LedgerEvent.take 2 60 true] |
| 512 | let final := trace.foldl buggyLedgerStep initLedger |
| 513 | ¬ (final.extracted ≤ final.contributed) := by |
| 514 | decide |
| 515 | |
| 516 | /-- Negative Control 4d: Scale 2^62 times wave sum 120 overflows signed 64-bit range |
| 517 | (matching mutant m21-vmax-ceiling-blowup trapping exit 101 on ovf-probe). -/ |
| 518 | theorem ovf_probe_without_scale_guard_overflows64 : |
| 519 | ¬ fits64 ((2 : ℤ) ^ 62 * 120) := by |
| 520 | unfold fits64 |
| 521 | norm_num |
| 522 | |
| 523 | end BtWaveP0 |
| 524 | |
| 525 | #print axioms BtWaveP0.bt6_valid_and_left_inv |
| 526 | #print axioms BtWaveP0.bt6_right_inv |
| 527 | #print axioms BtWaveP0.bt6_neg_sym |
| 528 | #print axioms BtWaveP0.bt6_dark_zero |
| 529 | #print axioms BtWaveP0.hc3_reconstructs_16_of_16 |
| 530 | #print axioms BtWaveP0.swar_lane_mul_iso |
| 531 | #print axioms BtWaveP0.swar_popcount_dot_iso |
| 532 | #print axioms BtWaveP0.h16_min_dist_eight |
| 533 | #print axioms BtWaveP0.h16_unique_decode_le_three |
| 534 | #print axioms BtWaveP0.ledger_give_before_take_invariant |
| 535 | #print axioms BtWaveP0.wave_eval_fits_signed64 |
| 536 | #print axioms BtWaveP0.nl1_four_central_hyperplanes_r3_at_most_14 |
| 537 | #print axioms BtWaveP0.h16_four_flip_tie_witness |
| 538 | #print axioms BtWaveP0.dropping_reciprocity_guard_violates_invariant |
| 539 | #print axioms BtWaveP0.ovf_probe_without_scale_guard_overflows64 |