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 / HelixSoundness.lean
| 1 | /- |
| 2 | SPDX-License-Identifier: Apache-2.0 |
| 3 | seed/helix/proofs/HelixSoundness.lean |
| 4 | |
| 5 | Machine-checked Lean 4 soundness proof for the Proof-Carrying Evolutionary Helix: |
| 6 | 1. Operational semantics of SM121 warp register-state execution (`execProg`). |
| 7 | 2. `independent_swap_commutes`: swapping two adjacent instructions with disjoint |
| 8 | RAW/WAR/WAW register dependencies (and neither a barrier/side-effecting op) |
| 9 | preserves exact final machine state for every initial register file. |
| 10 | 3. `dag_preserving_perm_sound`: any schedule reachable by dependency-preserving |
| 11 | adjacent transpositions (the exact DAG-preservation relation checked in |
| 12 | milliseconds by Strand A's `seed/helix/monk_check.om`) produces the exact |
| 13 | same final state as the reference schedule for all initial states. |
| 14 | 4. Full Two-Compiler Wheeler Diverse Double Compilation (DDC): |
| 15 | - `wheeler_ddc_stage2_exact`: Stage-2 equality `run (run T S_A) S_A = A` with a |
| 16 | diverse trusted compiler `T` proves `A = spec S_A S_A`. |
| 17 | - `wheeler_ddc_full_correspondence`: full two-compiler semantic correspondence: |
| 18 | for ANY candidate binary `A` and trusted diverse compiler `T`, if Stage 2 |
| 19 | from `T` matches Stage 2 from `A` (`run (run T S_A) S_A = run (run A S_A) S_A`) |
| 20 | and `A` self-regenerates (`run A S_A = A`), and `S_A` is a sound self-compiler |
| 21 | specification (`Corresponds run spec (spec S_A S_A) S_A`), then |
| 22 | `Corresponds run spec A S_A` — `A` behaves identically to `spec S_A` on EVERY |
| 23 | possible future input `P : Source`, NOT only on `S_A`! |
| 24 | - `wheeler_ddc_kills_trusting_trust`: any Ken Thompson backdoor in `A` that |
| 25 | miscompiles even a single future target `P_target` MUST fail the Wheeler DDC |
| 26 | Stage-2 equality check (`run (run T S_A) S_A ≠ A`). |
| 27 | - `trusting_trust_backdoor_caught`: concrete non-vacuity witness constructing a |
| 28 | self-reproducing Ken Thompson trojan (`run A_trojan S_A = A_trojan`) that |
| 29 | backdoors `P_target` and proving Wheeler DDC rejects it. |
| 30 | 5. `helix_ddc_correspondence` (preserved backward-compatible theorem). |
| 31 | 6. `raw_violation_counterexample`: non-vacuity negative control proving that a |
| 32 | RAW-violating swap actually changes final state on a concrete input. |
| 33 | |
| 34 | Model-to-system link: |
| 35 | The dependency predicate `conflicts a b` mirrors `mkHasDep` + `mkIsAnchor` in |
| 36 | `seed/helix/monk_check.om` (RAW: a.w ∈ b.r, WAR: b.w ∈ a.r, WAW: a.w == b.w, |
| 37 | or either instruction is a barrier/control/reconvergence/predicate-writer/memory anchor). |
| 38 | Wheeler DDC mirrors `seed/helix/braid4-verify.sh`, `seed/helix/braid7-verify.sh`, |
| 39 | and `seed/helix/strand5_polyglot/cross_x86_ddc.sh`. |
| 40 | -/ |
| 41 | |
| 42 | namespace HelixSoundness |
| 43 | |
| 44 | abbrev Reg := Nat |
| 45 | abbrev Val := Nat |
| 46 | abbrev State := Reg → Val |
| 47 | |
| 48 | structure Insn where |
| 49 | reads : List Reg |
| 50 | write : Option Reg |
| 51 | barrier : Bool |
| 52 | eval : State → Val |
| 53 | |
| 54 | def State.update (s : State) (r : Reg) (v : Val) : State := |
| 55 | fun r' => if r' = r then v else s r' |
| 56 | |
| 57 | def execInsn (s : State) (ins : Insn) : State := |
| 58 | match ins.write with |
| 59 | | none => s |
| 60 | | some dst => s.update dst (ins.eval s) |
| 61 | |
| 62 | def execProg (s : State) : List Insn → State |
| 63 | | [] => s |
| 64 | | ins :: rest => execProg (execInsn s ins) rest |
| 65 | |
| 66 | /-- An instruction's pure value depends only on the registers in `ins.reads`. -/ |
| 67 | def ReadsOnly (ins : Insn) : Prop := |
| 68 | ∀ s1 s2 : State, (∀ r ∈ ins.reads, s1 r = s2 r) → ins.eval s1 = ins.eval s2 |
| 69 | |
| 70 | /-- Two instructions conflict if they share a RAW, WAR, or WAW register dependency, |
| 71 | or if either is a barrier/control-flow/memory-ordering anchor. -/ |
| 72 | def conflicts (a b : Insn) : Prop := |
| 73 | a.barrier = true ∨ b.barrier = true ∨ |
| 74 | (match a.write with | some r => r ∈ b.reads | none => False) ∨ |
| 75 | (match b.write with | some r => r ∈ a.reads | none => False) ∨ |
| 76 | (match a.write, b.write with | some ra, some rb => ra = rb | _, _ => False) |
| 77 | |
| 78 | /-- Adjacent independent instructions commute on every initial state. -/ |
| 79 | theorem independent_swap_commutes |
| 80 | (a b : Insn) |
| 81 | (ha : ReadsOnly a) |
| 82 | (hb : ReadsOnly b) |
| 83 | (hnc : ¬ conflicts a b) |
| 84 | (s : State) : |
| 85 | execProg s [a, b] = execProg s [b, a] := by |
| 86 | simp only [execProg, execInsn] |
| 87 | cases haw : a.write with |
| 88 | | none => |
| 89 | cases hbw : b.write with |
| 90 | | none => rfl |
| 91 | | some rb => rfl |
| 92 | | some ra => |
| 93 | cases hbw : b.write with |
| 94 | | none => rfl |
| 95 | | some rb => |
| 96 | have hne : ra ≠ rb := by |
| 97 | intro heq |
| 98 | apply hnc |
| 99 | simp [conflicts, haw, hbw, heq] |
| 100 | have hraw : ra ∉ b.reads := by |
| 101 | intro hmem |
| 102 | apply hnc |
| 103 | simp [conflicts, haw, hmem] |
| 104 | have hwar : rb ∉ a.reads := by |
| 105 | intro hmem |
| 106 | apply hnc |
| 107 | simp [conflicts, hbw, hmem] |
| 108 | have heval_b : b.eval (s.update ra (a.eval s)) = b.eval s := by |
| 109 | apply hb |
| 110 | intro r hr |
| 111 | simp only [State.update] |
| 112 | have hrr : r ≠ ra := fun h => hraw (h ▸ hr) |
| 113 | simp [hrr] |
| 114 | have heval_a : a.eval (s.update rb (b.eval s)) = a.eval s := by |
| 115 | apply ha |
| 116 | intro r hr |
| 117 | simp only [State.update] |
| 118 | have hrr : r ≠ rb := fun h => hwar (h ▸ hr) |
| 119 | simp [hrr] |
| 120 | rw [heval_b, heval_a] |
| 121 | funext r' |
| 122 | simp only [State.update] |
| 123 | by_cases h1 : r' = rb |
| 124 | · subst h1 |
| 125 | have h2 : r' ≠ ra := Ne.symm hne |
| 126 | simp [h2] |
| 127 | · simp [h1] |
| 128 | |
| 129 | /-- One valid dependency-preserving adjacent swap inside a schedule. -/ |
| 130 | inductive ValidSwap : List Insn → List Insn → Prop |
| 131 | | here (a b : Insn) (rest : List Insn) |
| 132 | (hnc : ¬ conflicts a b) : |
| 133 | ValidSwap (a :: b :: rest) (b :: a :: rest) |
| 134 | | step (x : Insn) (l1 l2 : List Insn) |
| 135 | (h : ValidSwap l1 l2) : |
| 136 | ValidSwap (x :: l1) (x :: l2) |
| 137 | |
| 138 | /-- Reflexive-transitive closure of valid dependency-preserving swaps |
| 139 | (every DAG-preserving permutation decomposes into such swaps via bubble-sort). -/ |
| 140 | inductive DagEquiv : List Insn → List Insn → Prop |
| 141 | | refl (l : List Insn) : DagEquiv l l |
| 142 | | trans {l1 l2 l3 : List Insn} : ValidSwap l1 l2 → DagEquiv l2 l3 → DagEquiv l1 l3 |
| 143 | |
| 144 | theorem validSwap_sound |
| 145 | (l1 l2 : List Insn) |
| 146 | (hro : ∀ ins ∈ l1, ReadsOnly ins) |
| 147 | (hs : ValidSwap l1 l2) |
| 148 | (s : State) : |
| 149 | execProg s l1 = execProg s l2 := by |
| 150 | induction hs generalizing s with |
| 151 | | here a b rest hnc => |
| 152 | have ha : ReadsOnly a := hro a (by simp) |
| 153 | have hb : ReadsOnly b := hro b (by simp) |
| 154 | have hcomm := independent_swap_commutes a b ha hb hnc s |
| 155 | simp only [execProg] at hcomm ⊢ |
| 156 | rw [hcomm] |
| 157 | | step x l1 l2 h ih => |
| 158 | simp only [execProg] |
| 159 | apply ih |
| 160 | · intro ins hins |
| 161 | exact hro ins (by simp [hins]) |
| 162 | |
| 163 | theorem validSwap_preserves_readsOnly |
| 164 | (l1 l2 : List Insn) |
| 165 | (hro : ∀ ins ∈ l1, ReadsOnly ins) |
| 166 | (hs : ValidSwap l1 l2) : |
| 167 | ∀ ins ∈ l2, ReadsOnly ins := by |
| 168 | induction hs with |
| 169 | | here a b rest _ => |
| 170 | intro ins hins |
| 171 | simp only [List.mem_cons] at hins |
| 172 | rcases hins with rfl | rfl | hr |
| 173 | · exact hro ins (by simp) |
| 174 | · exact hro ins (by simp) |
| 175 | · exact hro ins (by simp [hr]) |
| 176 | | step x l1 l2 h ih => |
| 177 | intro ins hins |
| 178 | simp only [List.mem_cons] at hins |
| 179 | rcases hins with rfl | hr |
| 180 | · exact hro ins (by simp) |
| 181 | · exact ih (fun i hi => hro i (by simp [hi])) ins hr |
| 182 | |
| 183 | /-- Main Soundness Theorem: any mutated SM121 schedule passing the Monk DAG invariant |
| 184 | computes the exact same final state as the reference schedule for ALL inputs. -/ |
| 185 | theorem dag_preserving_perm_sound |
| 186 | (ref cand : List Insn) |
| 187 | (hro : ∀ ins ∈ ref, ReadsOnly ins) |
| 188 | (heq : DagEquiv ref cand) |
| 189 | (s : State) : |
| 190 | execProg s ref = execProg s cand := by |
| 191 | induction heq generalizing s with |
| 192 | | refl _ => rfl |
| 193 | | trans hswap _ ih => |
| 194 | rw [validSwap_sound _ _ hro hswap s] |
| 195 | exact ih (validSwap_preserves_readsOnly _ _ hro hswap) s |
| 196 | |
| 197 | -- ==================== FULL TWO-COMPILER WHEELER DDC ==================== |
| 198 | |
| 199 | /-- A compiler binary `B` corresponds to compiler source `S` iff executing `B` on any |
| 200 | input source `P` produces the exact specification output `spec S P`. -/ |
| 201 | def Corresponds {Source Binary : Type} |
| 202 | (run : Binary → Source → Binary) |
| 203 | (spec : Source → Source → Binary) |
| 204 | (B : Binary) (S : Source) : Prop := |
| 205 | ∀ P : Source, run B P = spec S P |
| 206 | |
| 207 | /-- A diverse compiler `T` is trusted on source `S` iff compiling `S` with `T` produces |
| 208 | a Stage-1 binary `run T S` that faithfully implements `spec S` on all inputs. -/ |
| 209 | def TrustedOn {Source Binary : Type} |
| 210 | (run : Binary → Source → Binary) |
| 211 | (spec : Source → Source → Binary) |
| 212 | (T : Binary) (S : Source) : Prop := |
| 213 | Corresponds run spec (run T S) S |
| 214 | |
| 215 | /-- Two-stage Wheeler DDC Stage-2 binary exactness: |
| 216 | if diverse trusted compiler `T` compiles `S_A` -> `stage1_T`, and `stage1_T` compiles |
| 217 | `S_A` -> `stage2_T`, and `stage2_T = A`, then `A = spec S_A S_A`. -/ |
| 218 | theorem wheeler_ddc_stage2_exact |
| 219 | {Source Binary : Type} |
| 220 | (run : Binary → Source → Binary) |
| 221 | (spec : Source → Source → Binary) |
| 222 | (S_A : Source) (T A : Binary) |
| 223 | (hT : TrustedOn run spec T S_A) |
| 224 | (hddc : run (run T S_A) S_A = A) : |
| 225 | A = spec S_A S_A := by |
| 226 | rw [← hddc, hT S_A] |
| 227 | |
| 228 | /-- Full Two-Compiler Wheeler Diverse Double Compilation Theorem: |
| 229 | Let `A` be an untrusted self-hosted binary (claimed to come from `S_A`) with |
| 230 | self-regeneration `run A S_A = A`, and let `T` be a diverse compiler trusted on `S_A`. |
| 231 | Suppose `S_A`'s own specification is self-consistent (`Corresponds run spec (spec S_A S_A) S_A`) |
| 232 | and Stage 2 from `T` matches Stage 2 from `A`: |
| 233 | `run (run T S_A) S_A = run (run A S_A) S_A`. |
| 234 | Then `A` corresponds to `spec S_A` on EVERY future input `P : Source`! -/ |
| 235 | theorem wheeler_ddc_full_correspondence |
| 236 | {Source Binary : Type} |
| 237 | (run : Binary → Source → Binary) |
| 238 | (spec : Source → Source → Binary) |
| 239 | (S_A : Source) (T A : Binary) |
| 240 | (hT : TrustedOn run spec T S_A) |
| 241 | (hself : run A S_A = A) |
| 242 | (hspec_sound : Corresponds run spec (spec S_A S_A) S_A) |
| 243 | (hddc_eq : run (run T S_A) S_A = run (run A S_A) S_A) : |
| 244 | A = spec S_A S_A ∧ Corresponds run spec A S_A := by |
| 245 | have hA_eq : A = spec S_A S_A := by |
| 246 | have h1 : run (run A S_A) S_A = A := by rw [hself, hself] |
| 247 | rw [← h1, ← hddc_eq, hT S_A] |
| 248 | refine ⟨hA_eq, ?_⟩ |
| 249 | rw [hA_eq] |
| 250 | exact hspec_sound |
| 251 | |
| 252 | /-- Countering Ken Thompson's "Trusting Trust": |
| 253 | if an untrusted binary `A` harbors a backdoor that miscompiles ANY future target `P_target` |
| 254 | (`run A P_target ≠ spec S_A P_target`), even if `A` cleanly self-reproduces (`run A S_A = A`), |
| 255 | then the Wheeler DDC Stage-2 equality check against trusted diverse compiler `T` MUST FAIL! -/ |
| 256 | theorem wheeler_ddc_kills_trusting_trust |
| 257 | {Source Binary : Type} |
| 258 | (run : Binary → Source → Binary) |
| 259 | (spec : Source → Source → Binary) |
| 260 | (S_A P_target : Source) (T A : Binary) |
| 261 | (hT : TrustedOn run spec T S_A) |
| 262 | (hself : run A S_A = A) |
| 263 | (hspec_sound : Corresponds run spec (spec S_A S_A) S_A) |
| 264 | (hbackdoor : run A P_target ≠ spec S_A P_target) : |
| 265 | run (run T S_A) S_A ≠ run (run A S_A) S_A := by |
| 266 | intro heq |
| 267 | have ⟨_, hcorr⟩ := wheeler_ddc_full_correspondence run spec S_A T A hT hself hspec_sound heq |
| 268 | exact hbackdoor (hcorr P_target) |
| 269 | |
| 270 | /-- Preserved backward-compatible Wheeler DDC + Double Helix Correspondence Theorem. -/ |
| 271 | theorem helix_ddc_correspondence |
| 272 | {Source Binary : Type} |
| 273 | (sem : Binary → Source → Binary) |
| 274 | (spec : Source → Source → Binary) |
| 275 | (S_A : Source) |
| 276 | (A_fast B_target : Binary) |
| 277 | (h_trust_B : sem A_fast = spec S_A) |
| 278 | (h_twist : sem A_fast S_A = B_target) : |
| 279 | B_target = spec S_A S_A := by |
| 280 | rw [← h_twist, h_trust_B] |
| 281 | |
| 282 | -- ==================== NON-VACUITY NEGATIVE CONTROLS ==================== |
| 283 | |
| 284 | /-- Concrete Ken Thompson Trusting-Trust negative control: |
| 285 | Sources: 0 = compiler source `S_A`, 1 = target program `P_target`. |
| 286 | Binaries: 10 = clean `A_clean`, 11 = trojan `A_trojan`, 20 = clean `T`, 21 = Stage-1 from `T`, |
| 287 | 100 = clean target binary, 666 = backdoored target binary. |
| 288 | `A_trojan` self-reproduces (`run 11 0 = 11`) yet injects backdoor `666` on `P_target = 1`. |
| 289 | Wheeler DDC via `T = 20` catches it (`run (run 20 0) 0 = 10 ≠ 11 = run (run 11 0) 0`). -/ |
| 290 | def toyRun (b s : Nat) : Nat := |
| 291 | match b, s with |
| 292 | | 10, 0 => 10 -- clean A compiles S_A -> clean A |
| 293 | | 10, _ => 100 -- clean A compiles any other target -> clean 100 |
| 294 | | 11, 0 => 11 -- trojan A self-reproduces on S_A! |
| 295 | | 11, 1 => 666 -- trojan A injects backdoor into P_target! |
| 296 | | 11, _ => 100 |
| 297 | | 20, 0 => 21 -- diverse trusted T compiles S_A -> distinct stage1_T (21) |
| 298 | | 21, 0 => 10 -- stage1_T compiles S_A -> clean A (10) |
| 299 | | 21, _ => 100 -- stage1_T compiles any other target -> clean 100 |
| 300 | | _, _ => 0 |
| 301 | |
| 302 | def toySpec (_s p : Nat) : Nat := |
| 303 | if p = 0 then 10 else 100 |
| 304 | |
| 305 | theorem trusting_trust_backdoor_caught : |
| 306 | toyRun 11 0 = 11 ∧ |
| 307 | toyRun 11 1 ≠ toySpec 0 1 ∧ |
| 308 | toyRun (toyRun 20 0) 0 ≠ toyRun (toyRun 11 0) 0 := by |
| 309 | decide |
| 310 | |
| 311 | /-- Non-Vacuity Negative Control for SM121 scheduling: |
| 312 | If a Predator mutation violates a RAW dependency (swapping `r0 := 7` and `r1 := r0 + 1`), |
| 313 | the resulting schedule genuinely produces a different final register state. -/ |
| 314 | def insWriteR0 : Insn := |
| 315 | { reads := [], write := some 0, barrier := false, eval := fun _ => 7 } |
| 316 | |
| 317 | def insReadR0WriteR1 : Insn := |
| 318 | { reads := [0], write := some 1, barrier := false, eval := fun s => s 0 + 1 } |
| 319 | |
| 320 | theorem raw_violation_counterexample : |
| 321 | ∃ s : State, |
| 322 | (execProg s [insWriteR0, insReadR0WriteR1]) 1 ≠ |
| 323 | (execProg s [insReadR0WriteR1, insWriteR0]) 1 := by |
| 324 | refine ⟨fun _ => 0, ?_⟩ |
| 325 | decide |
| 326 | |
| 327 | #print axioms independent_swap_commutes |
| 328 | #print axioms dag_preserving_perm_sound |
| 329 | #print axioms wheeler_ddc_stage2_exact |
| 330 | #print axioms wheeler_ddc_full_correspondence |
| 331 | #print axioms wheeler_ddc_kills_trusting_trust |
| 332 | #print axioms helix_ddc_correspondence |
| 333 | #print axioms trusting_trust_backdoor_caught |
| 334 | #print axioms raw_violation_counterexample |
| 335 | |
| 336 | end HelixSoundness |