Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tools/codebook/proof/CodebookProof.lean # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tools / codebook / proof / CodebookProof.lean` 338 lines · 12772 bytes Raw Blame History 1 /- 2 SPDX-License-Identifier: AGPL-3.0-only 3 T-002 items 7-10: the signed layered digit codebook (model of tools/codebook/codebook.om). 4 Checked with Lean 4.35.0, core only (no Mathlib), no proof holes. 5 6 Wire form: a message is a list of codes; a code is a list of 3-digit groups (each < 1000) where 7 every group but the last is an escape 900..999 and the last is a terminal 000..899. 8 The digit string is the concatenation of the groups, each written as exactly 3 decimal digits. 9 `lean --run CodebookProof.lean gen|exh|dec` prints the differential-test vectors. 10 -/ 11 set_option linter.deprecated false 12 set_option linter.unusedSimpArgs false 13 14 namespace Codebook 15 16 /-- one 3-digit group, most significant digit first -/ 17 def tri (n : Nat) : List Nat := [n / 100 % 10, n / 10 % 10, n % 10] 18 19 /-- a well-formed layered code: (9xx)^k then one terminal 000..899 -/ 20 inductive Valid : List Nat → Prop 21 | term {g : Nat} : g < 900 → Valid [g] 22 | esc {g : Nat} {t : List Nat} : 900 ≤ g → g < 1000 → Valid t → Valid (g :: t) 23 24 def encC (c : List Nat) : List Nat := c.flatMap tri 25 def encode (m : List (List Nat)) : List Nat := m.flatMap encC 26 27 /-- digits to 3-digit groups; none on a non-digit or a length that is not a multiple of 3 -/ 28 def groups : List Nat → Option (List Nat) 29 | [] => some [] 30 | a :: b :: c :: r => 31 if a < 10 ∧ b < 10 ∧ c < 10 then (groups r).map (fun gs => (a * 100 + b * 10 + c) :: gs) else none 32 | _ => none 33 34 /-- groups to codes; `acc` holds the escapes read so far; a dangling escape is none -/ 35 def parse : List Nat → List Nat → Option (List (List Nat)) 36 | [], [] => some [] 37 | [], _ :: _ => none 38 | g :: gs, acc => 39 if 900 ≤ g then parse gs (acc ++ [g]) else (parse gs []).map (fun r => (acc ++ [g]) :: r) 40 41 def decode (ds : List Nat) : Option (List (List Nat)) := (groups ds).bind (fun gs => parse gs []) 42 43 theorem Valid.lt {c : List Nat} (h : Valid c) : ∀ g ∈ c, g < 1000 := by 44 induction h with 45 | term hg => intro x hx; simp at hx; omega 46 | esc h1 h2 _ ih => 47 intro x hx; simp at hx 48 rcases hx with hx | hx 49 · omega 50 · exact ih x hx 51 52 theorem groups_tri (n : Nat) (r : List Nat) (h : n < 1000) : 53 groups (tri n ++ r) = (groups r).map (n :: ·) := by 54 have e : n / 100 % 10 * 100 + n / 10 % 10 * 10 + n % 10 = n := by omega 55 simp only [tri, List.cons_append, List.nil_append, groups] 56 rw [if_pos (by omega), e] 57 58 theorem groups_encC (c r : List Nat) (h : ∀ g ∈ c, g < 1000) : 59 groups (encC c ++ r) = (groups r).map (c ++ ·) := by 60 induction c with 61 | nil => simp [encC] 62 | cons g t ih => 63 have hg : g < 1000 := h g (by simp) 64 have ht : ∀ x ∈ t, x < 1000 := fun x hx => h x (by simp [hx]) 65 simp only [encC, List.flatMap_cons, List.append_assoc] at ih ⊢ 66 rw [groups_tri g _ hg, ih ht, Option.map_map] 67 rfl 68 69 theorem groups_encode (m : List (List Nat)) (h : ∀ c ∈ m, Valid c) : 70 groups (encode m) = some m.flatten := by 71 induction m with 72 | nil => simp [encode, groups] 73 | cons c t ih => 74 have hc : Valid c := h c (by simp) 75 have ht : ∀ x ∈ t, Valid x := fun x hx => h x (by simp [hx]) 76 have e : encode (c :: t) = encC c ++ encode t := by simp [encode, encC] 77 rw [e, groups_encC c _ hc.lt, ih ht] 78 simp 79 80 theorem parse_valid {c : List Nat} (h : Valid c) (rest acc : List Nat) : 81 parse (c ++ rest) acc = (parse rest []).map ((acc ++ c) :: ·) := by 82 induction h generalizing acc with 83 | term hg => 84 simp only [List.cons_append, List.nil_append, parse] 85 rw [if_neg (by omega)] 86 | esc h1 _ _ ih => 87 simp only [List.cons_append, parse] 88 rw [if_pos h1, ih] 89 simp 90 91 theorem parse_flatten (m : List (List Nat)) (h : ∀ c ∈ m, Valid c) : 92 parse m.flatten [] = some m := by 93 induction m with 94 | nil => simp [parse] 95 | cons c t ih => 96 have hc : Valid c := h c (by simp) 97 have ht : ∀ x ∈ t, Valid x := fun x hx => h x (by simp [hx]) 98 simp only [List.flatten_cons] 99 rw [parse_valid hc, ih ht] 100 simp 101 102 /-- Round trip: every layered message decodes back to itself. -/ 103 theorem decode_encode (m : List (List Nat)) (h : ∀ c ∈ m, Valid c) : 104 decode (encode m) = some m := by 105 simp [decode, groups_encode m h, parse_flatten m h] 106 107 /-- Unique decodability: two messages with the same digits are the same message. -/ 108 theorem encode_injective (m1 m2 : List (List Nat)) (h1 : ∀ c ∈ m1, Valid c) 109 (h2 : ∀ c ∈ m2, Valid c) (e : encode m1 = encode m2) : m1 = m2 := by 110 have a := decode_encode m1 h1 111 rw [e, decode_encode m2 h2] at a 112 exact (Option.some.inj a).symm 113 114 /-- Prefix-freeness: no codeword's digits are a prefix of another codeword's digits. -/ 115 theorem prefix_free {c1 c2 : List Nat} (h1 : Valid c1) (h2 : Valid c2) (r : List Nat) 116 (e : encC c1 ++ r = encC c2) : c1 = c2 ∧ r = [] := by 117 induction h1 generalizing c2 with 118 | @term g1 hg1 => 119 cases h2 with 120 | @term g2 hg2 => 121 simp [encC, tri] at e 122 obtain ⟨a, b, c, d⟩ := e 123 exact ⟨by simp; omega, d⟩ 124 | @esc g2 t2 hg2 hg2' _ => 125 simp [encC, tri] at e 126 obtain ⟨a, b, c, _⟩ := e 127 omega 128 | @esc g1 t1 hg1 hg1' _ ih => 129 cases h2 with 130 | @term g2 hg2 => 131 simp [encC, tri] at e 132 obtain ⟨a, b, c, _⟩ := e 133 omega 134 | @esc g2 t2 hg2 hg2' v2 => 135 simp only [encC, List.flatMap_cons, tri, List.cons_append, List.nil_append, 136 List.append_assoc, List.cons.injEq] at e 137 obtain ⟨a, b, c, d⟩ := e 138 have hg : g1 = g2 := by omega 139 have := ih v2 d 140 exact ⟨by rw [hg, this.1], this.2⟩ 141 142 theorem prefix_free' {c1 c2 : List Nat} (h1 : Valid c1) (h2 : Valid c2) (hne : c1 ≠ c2) : 143 ¬ (encC c1 <+: encC c2) := by 144 rintro ⟨r, e⟩ 145 exact hne (prefix_free h1 h2 r e).1 146 147 /-! Codebook: an append-only TSV `codetypemeaning`; first match wins. -/ 148 149 structure Entry where 150 code : List Nat 151 ty : String 152 meaning : String 153 deriving DecidableEq, Repr 154 155 def lookup (cb : List Entry) (c : List Nat) : Option Entry := cb.find? (fun e => e.code == c) 156 157 def lookAll (cb : List Entry) : List (List Nat) → Option (List Entry) 158 | [] => some [] 159 | c :: cs => (lookup cb c).bind (fun e => (lookAll cb cs).map (e :: ·)) 160 161 def decodeSem (cb : List Entry) (ds : List Nat) : Option (List Entry) := 162 (decode ds).bind (lookAll cb) 163 164 theorem lookup_append (cb ext : List Entry) (c : List Nat) (e : Entry) 165 (h : lookup cb c = some e) : lookup (cb ++ ext) c = some e := by 166 simp only [lookup, List.find?_append] at h ⊢ 167 rw [h]; rfl 168 169 theorem lookAll_append (cb ext : List Entry) (cs : List (List Nat)) (r : List Entry) 170 (h : lookAll cb cs = some r) : lookAll (cb ++ ext) cs = some r := by 171 induction cs generalizing r with 172 | nil => simpa [lookAll] using h 173 | cons c t ih => 174 simp only [lookAll] at h ⊢ 175 cases hl : lookup cb c with 176 | none => rw [hl] at h; simp at h 177 | some e => 178 rw [hl] at h 179 rw [lookup_append cb ext c e hl] 180 cases ht : lookAll cb t with 181 | none => rw [ht] at h; simp at h 182 | some r' => 183 rw [ht] at h 184 rw [ih r' ht] 185 exact h 186 187 /-- Append-only extension never changes a message that already decoded. -/ 188 theorem append_preserves (cb ext : List Entry) (ds : List Nat) (r : List Entry) 189 (h : decodeSem cb ds = some r) : decodeSem (cb ++ ext) ds = some r := by 190 unfold decodeSem at h ⊢ 191 cases hd : decode ds with 192 | none => rw [hd] at h; simp at h 193 | some cs => 194 rw [hd] at h 195 exact lookAll_append cb ext cs r h 196 197 /-! Verification and demotion, in the order codebook.om checks them. -/ 198 199 inductive Status | ok | unsigned | badsender | badfmt | badcb | baddecode | unknown | badmac | stale 200 deriving DecidableEq, Repr 201 202 structure Line where 203 signed : Bool -- an ocb2 field with a non-empty MAC 204 senderOk : Bool 205 fmtOk : Bool 206 cbOk : Bool -- the line's codebook sha equals the loaded codebook's sha 207 decOk : Bool 208 known : Bool 209 macOk : Bool -- HMAC-SHA256(key, ocb2|cbsha|counter|lane|ts|from|to|tag|text|ref|digits) equals the line's MAC 210 counter : Nat 211 last : Nat -- the sender's last accepted counter 212 213 def status (l : Line) : Status := 214 if !l.signed then .unsigned 215 else if !l.senderOk then .badsender 216 else if !l.fmtOk then .badfmt 217 else if !l.cbOk then .badcb 218 else if !l.decOk then .baddecode 219 else if !l.known then .unknown 220 else if !l.macOk then .badmac 221 else if l.counter ≤ l.last then .stale 222 else .ok 223 224 /-- a code is an emergency when it is the single group 300..399 -/ 225 def emerg (c : List Nat) : Bool := 226 match c with 227 | [g] => 300 ≤ g && g < 400 228 | _ => false 229 230 /-- the trit a line produces: 2 only for a verified line carrying an emergency code -/ 231 def lineTrit (st : Status) (cs : List (List Nat)) : Nat := 232 if st = .ok ∧ cs.any emerg = true then 2 else 1 233 234 theorem lineTrit_le (st : Status) (cs : List (List Nat)) : lineTrit st cs = 1 ∨ lineTrit st cs = 2 := by 235 unfold lineTrit; split <;> simp 236 237 /-- Demotion: an unverified line never yields 2. -/ 238 theorem unverified_never_two (st : Status) (cs : List (List Nat)) (h : st ≠ .ok) : 239 lineTrit st cs ≠ 2 := by 240 unfold lineTrit; simp [h] 241 242 theorem two_iff (st : Status) (cs : List (List Nat)) : 243 lineTrit st cs = 2 ↔ st = .ok ∧ cs.any emerg = true := by 244 unfold lineTrit; split <;> simp_all 245 246 theorem unsigned_demoted (l : Line) (cs) (h : l.signed = false) : lineTrit (status l) cs = 1 := by 247 simp [lineTrit, status, h] 248 249 theorem stale_demoted (l : Line) (cs) (h : l.counter ≤ l.last) : lineTrit (status l) cs ≠ 2 := by 250 apply unverified_never_two 251 simp only [status] 252 split <;> (try simp) <;> split <;> (try simp) <;> split <;> (try simp) <;> split <;> (try simp) <;> 253 split <;> (try simp) <;> split <;> (try simp) <;> split <;> (try simp) <;> split <;> (try simp) <;> omega 254 255 theorem badmac_demoted (l : Line) (cs) (h : l.macOk = false) : lineTrit (status l) cs ≠ 2 := by 256 apply unverified_never_two 257 simp only [status, h] 258 split <;> (try simp) <;> split <;> (try simp) <;> split <;> (try simp) <;> split <;> (try simp) <;> 259 split <;> (try simp) <;> split <;> simp 260 261 /-! Test-vector generator: the same `encode` the theorems are about. -/ 262 263 def showCode (c : List Nat) : String := ".".intercalate (c.map toString) 264 def showMsg (m : List (List Nat)) : String := " ".intercalate (m.map showCode) 265 def showDigits (ds : List Nat) : String := String.join (ds.map toString) 266 def showDec : Option (List (List Nat)) → String 267 | some m => showMsg m 268 | none => "ERR" 269 270 def lcg (s : Nat) : Nat := (s * `6364136223846793005` + `1442695040888963407`) % `18446744073709551616` 271 def rnd (s : Nat) : Nat := s / `4294967296` 272 273 /-- a random valid code: k escapes (0..3) then a terminal -/ 274 def genCode (s : Nat) : List Nat × Nat := 275 let s1 := lcg s 276 let k := rnd s1 % 4 277 let rec go : Nat → Nat → List Nat × Nat 278 | 0, s => let s' := lcg s; ([rnd s' % 900], s') 279 | n + 1, s => let s' := lcg s; let (t, s'') := go n s'; ((900 + rnd s' % 100) :: t, s'') 280 go k s1 281 282 def genMsg (s : Nat) : List (List Nat) × Nat := 283 let s1 := lcg s 284 let n := rnd s1 % 7 285 let rec go : Nat → Nat → List (List Nat) × Nat 286 | 0, s => ([], s) 287 | n + 1, s => let (c, s') := genCode s; let (t, s'') := go n s'; (c :: t, s'') 288 go n s1 289 290 def genDigits (s : Nat) : List Nat × Nat := 291 let s1 := lcg s 292 let n := 3 * (rnd s1 % 6) + (if rnd s1 / 8 % 8 = 0 then 1 else 0) 293 let rec go : Nat → Nat → List Nat × Nat 294 | 0, s => ([], s) 295 | n + 1, s => 296 let s' := lcg s 297 let r := rnd s' % 20 298 let d := if r < 10 then 9 else r - 10 299 let (t, s'') := go n s' 300 (d :: t, s'') 301 go n s1 302 303 partial def loopGen (i : Nat) (s : Nat) (acc : Array String) : Array String := 304 if i = 0 then acc else 305 let (m, s') := genMsg s 306 let row := 307 let left := showMsg m 308 let right := showDigits (encode m) 309 if left == "" && right == "" then "" else s!"{left} {right}" 310 loopGen (i - 1) s' (acc.push row) 311 312 partial def loopDec (i : Nat) (s : Nat) (acc : Array String) : Array String := 313 if i = 0 then acc else 314 let (ds, s') := genDigits s 315 let row := 316 let left := showDigits ds 317 let right := showDec (decode ds) 318 if left == "" && right == "" then "" else s!"{left} {right}" 319 loopDec (i - 1) s' (acc.push row) 320 321 def main (args : List String) : IO Unit := do 322 match args with 323 | ["gen", n] => 324 for l in loopGen n.toNat! `20261115` #[] do IO.println l 325 | ["dec", n] => 326 for l in loopDec n.toNat! 777 #[] do IO.println l 327 | ["exh"] => 328 for t in List.range 900 do 329 IO.println s!"{showMsg [[t]]}\t{showDigits (encode [[t]])}" 330 for e in List.range 100 do 331 for t in List.range 900 do 332 let m := [[900 + e, t]] 333 IO.println s!"{showMsg m}\t{showDigits (encode m)}" 334 | _ => IO.eprintln "usage: gen N | dec N | exh" 335 336 end Codebook 337 338 def main (args : List String) : IO Unit := Codebook.main args