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/mailhand/blob/main/proofs/Mailhand.lean # mailhand mailhand: agents look after a person's email in seeded Omega. Maildir + MIME parsing, JSONL CLI and MCP, rule triage, policy-gated sending, hash-chained audit. Free (Apache-2.0). Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `mailhand / proofs / Mailhand.lean` 351 lines · 14871 bytes Raw Blame History 1 /- 2 Mailhand proofs (plain Lean 4 core, no Mathlib). 3 Part 1: base64 decoder model (src/mime.om algorithm) and its output-length law. 4 Part 2: hash-chain audit log model and tamper evidence under an idealised hash. 5 Check: ~/.elan/bin/lean proofs/Mailhand.lean 6 -/ 7 8 namespace Mailhand 9 10 /-! ## Part 1: base64 decoder 11 12 The model PRODUCES the decoded bytes (not only their count). 13 Bytes are `UInt8`; classification uses `UInt8.toNat`. 14 -/ 15 namespace B64 16 17 /-- SP, TAB, CR, LF are skipped. -/ 18 def isWs (b : UInt8) : Bool := 19 b.toNat == 32 || b.toNat == 9 || b.toNat == 13 || b.toNat == 10 20 21 /-- `=` (61) is the padding byte. -/ 22 def isPad (b : UInt8) : Bool := b.toNat == 61 23 24 /-- Alphabet bytes: A-Z a-z 0-9 + / -/ 25 def isAlpha (b : UInt8) : Bool := 26 let n := b.toNat 27 (decide (65 ≤ n) && decide (n ≤ 90)) || (decide (97 ≤ n) && decide (n ≤ 122)) || 28 (decide (48 ≤ n) && decide (n ≤ 57)) || n == 43 || n == 47 29 30 /-- Sextet value of an alphabet byte (only used when `isAlpha b`). -/ 31 def sextet (b : UInt8) : Nat := 32 let n := b.toNat 33 if 65 ≤ n ∧ n ≤ 90 then n - 65 34 else if 97 ≤ n ∧ n ≤ 122 then n - 71 35 else if 48 ≤ n ∧ n ≤ 57 then n + 4 36 else if n = 43 then 62 37 else 63 38 39 /-- After the first `=`, only `=` and whitespace may follow. -/ 40 def padTail : List UInt8 → Bool 41 | [] => true 42 | b :: rest => (isPad b || isWs b) && padTail rest 43 44 /-- End of input with `n` sextets (bits in `acc`) in the open quantum. 45 0 sextets: done; 1: refusal; 2: one byte; 3: two bytes. -/ 46 def finish (n acc : Nat) (out : List UInt8) : Option (List UInt8) := 47 match n with 48 | 0 => some out 49 | 2 => some (out ++ [UInt8.ofNat (acc / 16 % 256)]) 50 | 3 => some (out ++ [UInt8.ofNat (acc / 1024 % 256), UInt8.ofNat (acc / 4 % 256)]) 51 | _ => none 52 53 /-- The decoder loop, parameterised by the end-of-input rule `fin` only so that 54 the negative control below can reuse it; the real decoder uses `finish`. 55 `n` = sextets in the current quantum (0..3), `acc` = their bits, 56 `out` = bytes emitted so far. -/ 57 def goWith (fin : Nat → Nat → List UInt8 → Option (List UInt8)) : 58 List UInt8 → Nat → Nat → List UInt8 → Option (List UInt8) 59 | [], n, acc, out => fin n acc out 60 | b :: rest, n, acc, out => 61 if isWs b then goWith fin rest n acc out 62 else if isPad b then 63 if (n = 2 ∨ n = 3) ∧ padTail rest = true then fin n acc out else none 64 else if isAlpha b then 65 if n = 3 then 66 let v := acc * 64 + sextet b 67 goWith fin rest 0 0 68 (out ++ [UInt8.ofNat (v / 65536 % 256), UInt8.ofNat (v / 256 % 256), UInt8.ofNat (v % 256)]) 69 else goWith fin rest (n + 1) (acc * 64 + sextet b) out 70 else none 71 72 /-- The decoder: `none` is a refusal. -/ 73 def decode (i : List UInt8) : Option (List UInt8) := goWith finish i 0 0 [] 74 75 /-- Number of alphabet bytes. -/ 76 def alphaCount (l : List UInt8) : Nat := (l.filter isAlpha).length 77 78 /-- Expected output length for `q` alphabet bytes: `3*(q/4) + [0,0,1,2][q%4]`. -/ 79 def outLen (q : Nat) : Nat := 3 * (q / 4) + [0, 0, 1, 2].getD (q % 4) 0 80 81 theorem alphaCount_nil : alphaCount [] = 0 := rfl 82 83 theorem alphaCount_cons (b : UInt8) (l : List UInt8) : 84 alphaCount (b :: l) = (if isAlpha b then 1 else 0) + alphaCount l := by 85 unfold alphaCount 86 cases h : isAlpha b <;> simp [h] <;> omega 87 88 theorem alphaCount_le (l : List UInt8) : alphaCount l ≤ l.length := by 89 unfold alphaCount 90 exact List.length_filter_le _ _ 91 92 theorem ws_not_alpha (b : UInt8) (h : isWs b = true) : isAlpha b = false := by 93 revert h 94 simp [isWs, isAlpha] 95 omega 96 97 theorem pad_not_alpha (b : UInt8) (h : isPad b = true) : isAlpha b = false := by 98 revert h 99 simp [isPad, isAlpha] 100 omega 101 102 theorem padTail_count : ∀ l : List UInt8, padTail l = true → alphaCount l = 0 103 | [], _ => rfl 104 | b :: rest, h => by 105 simp only [padTail, Bool.and_eq_true, Bool.or_eq_true] at h 106 obtain ⟨hb, hr⟩ := h 107 have hna : isAlpha b = false := by 108 rcases hb with hb | hb 109 · exact pad_not_alpha b hb 110 · exact ws_not_alpha b hb 111 rw [alphaCount_cons, hna, padTail_count rest hr] 112 rfl 113 114 theorem outLen_add4 (q : Nat) : outLen (q + 4) = outLen q + 3 := by 115 unfold outLen 116 rw [Nat.add_mod_right, Nat.add_div_right _ (by decide)] 117 omega 118 119 theorem finish_length (n acc : Nat) (out o : List UInt8) (hn : n ≤ 3) 120 (h : finish n acc out = some o) : o.length = out.length + outLen n := by 121 rcases (by omega : n = 0 ∨ n = 1 ∨ n = 2 ∨ n = 3) with rfl | rfl | rfl | rfl <;> 122 simp [finish] at h <;> subst h <;> simp [outLen] 123 124 /-- Loop invariant: from state (n, acc, out) on remaining input `l`, any 125 accepted output has length `out.length + outLen (n + alphaCount l)`. -/ 126 theorem go_length : ∀ (l : List UInt8) (n acc : Nat) (out o : List UInt8), 127 n ≤ 3 → goWith finish l n acc out = some o → 128 o.length = out.length + outLen (n + alphaCount l) 129 | [], n, acc, out, o, hn, h => by 130 simp only [goWith] at h 131 rw [alphaCount_nil, Nat.add_zero] 132 exact finish_length n acc out o hn h 133 | b :: rest, n, acc, out, o, hn, h => by 134 simp only [goWith] at h 135 by_cases hw : isWs b = true 136 · rw [ite_eq_left hw] at h 137 rw [alphaCount_cons, ws_not_alpha b hw] 138 simpa using go_length rest n acc out o hn h 139 · rw [ite_eq_right hw] at h 140 by_cases hp : isPad b = true 141 · rw [ite_eq_left hp] at h 142 split at h 143 · rename_i hc 144 obtain ⟨_, ht⟩ := hc 145 rw [alphaCount_cons, pad_not_alpha b hp, padTail_count rest ht] 146 simpa using finish_length n acc out o hn h 147 · exact absurd h (by simp) 148 · rw [ite_eq_right hp] at h 149 by_cases ha : isAlpha b = true 150 · rw [ite_eq_left ha] at h 151 rw [alphaCount_cons, ite_eq_left ha] 152 split at h 153 · rename_i h3 154 subst h3 155 have ih := go_length rest 0 0 _ o (by decide) h 156 rw [ih] 157 simp only [List.length_append, List.length_cons, List.length_nil] 158 rw [show 3 + (1 + alphaCount rest) = alphaCount rest + 4 by omega, outLen_add4] 159 simp 160 omega 161 · rename_i h3 162 have ih := go_length rest (n + 1) _ out o (by omega) h 163 rw [ih] 164 congr 2 165 omega 166 · rw [ite_eq_right ha] at h 167 exact absurd h (by simp) 168 169 /-- THEOREM 1a (exact length). If the decoder accepts input `i` with output `o`, 170 then `o.length = 3*(q/4) + [0,0,1,2][q%4]` where `q` = number of alphabet bytes. -/ 171 theorem decode_length (i o : List UInt8) (h : decode i = some o) : 172 o.length = 3 * (alphaCount i / 4) + [0, 0, 1, 2].getD (alphaCount i % 4) 0 := by 173 have := go_length i 0 0 [] o (by decide) h 174 simpa [outLen] using this 175 176 /-- THEOREM 1b (bound). If the decoder accepts `i` with output `o`, 177 then `o.length ≤ 3 * i.length / 4`. -/ 178 theorem decode_bound (i o : List UInt8) (h : decode i = some o) : 179 o.length ≤ 3 * i.length / 4 := by 180 have hl := decode_length i o h 181 have hq := alphaCount_le i 182 rw [hl] 183 generalize alphaCount i = q at hq 184 rcases (by omega : q % 4 = 0 ∨ q % 4 = 1 ∨ q % 4 = 2 ∨ q % 4 = 3) with hr | hr | hr | hr <;> 185 simp [hr] <;> omega 186 187 /-! ### Sanity checks (kernel-evaluated by `decide`) -/ 188 189 -- "TWFu" -> "Man" 190 example : decode [84, 87, 70, 117] = some [77, 97, 110] := by decide 191 -- "TWE=" -> "Ma" 192 example : decode [84, 87, 69, 61] = some [77, 97] := by decide 193 -- "TQ==" -> "M" 194 example : decode [84, 81, 61, 61] = some [77] := by decide 195 -- " TW\r\nFu\t" (whitespace skipped) -> "Man" 196 example : decode [32, 84, 87, 13, 10, 70, 117, 9] = some [77, 97, 110] := by decide 197 -- "" -> "" 198 example : decode [] = some [] := by decide 199 -- "Q" (final quantum of 1 sextet) -> refusal 200 example : decode [81] = none := by decide 201 -- "Q===" ('=' after 1 sextet) -> refusal 202 example : decode [81, 61, 61, 61] = none := by decide 203 -- "QQ=Q" (data after padding) -> refusal 204 example : decode [81, 81, 61, 81] = none := by decide 205 -- "TW-u" (byte outside the alphabet) -> refusal 206 example : decode [84, 87, 45, 117] = none := by decide 207 208 /-! ### Negative control: a decoder that does NOT refuse a final 1-sextet quantum 209 (it emits one byte instead) violates the bound, so the bound is not vacuous. -/ 210 211 def finishLenient (n acc : Nat) (out : List UInt8) : Option (List UInt8) := 212 if n = 1 then some (out ++ [UInt8.ofNat acc]) else finish n acc out 213 214 def decodeLenient (i : List UInt8) : Option (List UInt8) := goWith finishLenient i 0 0 [] 215 216 theorem lenient_breaks_bound : 217 ¬ ∀ i o, decodeLenient i = some o → o.length ≤ 3 * i.length / 4 := by 218 intro hall 219 have := hall [81] [16] (by decide) 220 simp at this 221 222 end B64 223 224 /-! ## Part 2: hash-chain audit log 225 226 `H` models SHA-256. Two HYPOTHESES idealise it (stated on every theorem that 227 uses them; they are NOT axioms of this file): 228 * `Function.Injective H`: no collisions (idealised collision resistance; real 229 SHA-256 has collisions, they are only believed infeasible to find); 230 * fixed output length: all digests have the same length (SHA-256: 32 bytes). 231 -/ 232 namespace Chain 233 234 /-- A record: (body, stored hash). -/ 235 abbrev Rec := List UInt8 × List UInt8 236 237 /-- Verification from `prev`: each record needs `stored = H (prev ++ body)`, 238 then `prev := stored`. Returns the head (last stored, or `prev` if empty). -/ 239 def verifyFrom (H : List UInt8 → List UInt8) : List UInt8 → List Rec → Option (List UInt8) 240 | prev, [] => some prev 241 | prev, (body, stored) :: rest => 242 if stored = H (prev ++ body) then verifyFrom H stored rest else none 243 244 /-- Verification of a whole log from the genesis value. -/ 245 def verify (H : List UInt8 → List UInt8) (genesis : List UInt8) (log : List Rec) : 246 Option (List UInt8) := 247 verifyFrom H genesis log 248 249 theorem verifyFrom_unique (H : List UInt8 → List UInt8) 250 (hInj : Function.Injective H) (hLen : ∀ x y, (H x).length = (H y).length) : 251 ∀ (l1 l2 : List Rec) (p1 p2 h : List UInt8), 252 l1.length = l2.length → p1.length = p2.length → 253 verifyFrom H p1 l1 = some h → verifyFrom H p2 l2 = some h → p1 = p2 ∧ l1 = l2 254 | [], [], p1, p2, h, _, _, h1, h2 => by 255 simp only [verifyFrom, Option.some.injEq] at h1 h2 256 exact ⟨h1.trans h2.symm, rfl⟩ 257 | [], _ :: _, _, _, _, hl, _, _, _ => by simp at hl 258 | _ :: _, [], _, _, _, hl, _, _, _ => by simp at hl 259 | (b1, s1) :: t1, (b2, s2) :: t2, p1, p2, h, hl, hp, h1, h2 => by 260 simp only [verifyFrom] at h1 h2 261 split at h1 262 · split at h2 263 · rename_i e1 e2 264 have hs : s1.length = s2.length := by rw [e1, e2]; exact hLen _ _ 265 obtain ⟨hs12, ht⟩ := 266 verifyFrom_unique H hInj hLen t1 t2 s1 s2 h (by simpa using hl) hs h1 h2 267 have hc : p1 ++ b1 = p2 ++ b2 := hInj (e1.symm.trans (hs12.trans e2)) 268 obtain ⟨hp12, hb12⟩ := List.append_inj hc hp 269 subst hp12 hb12 hs12 ht 270 exact ⟨rfl, rfl⟩ 271 · exact absurd h2 (by simp) 272 · exact absurd h1 (by simp) 273 274 /-- THEOREM 2 (uniqueness). Under the idealised-hash hypotheses, two logs of the 275 same length that both verify from the same genesis with the same head are equal. -/ 276 theorem chain_unique (H : List UInt8 → List UInt8) 277 (hInj : Function.Injective H) (hLen : ∀ x y, (H x).length = (H y).length) 278 (genesis : List UInt8) (l1 l2 : List Rec) (head : List UInt8) 279 (hl : l1.length = l2.length) 280 (h1 : verify H genesis l1 = some head) (h2 : verify H genesis l2 = some head) : 281 l1 = l2 := 282 (verifyFrom_unique H hInj hLen l1 l2 genesis genesis head hl rfl h1 h2).2 283 284 /-- THEOREM 2' (single-record change). If `log` verifies with head `head`, then 285 replacing record `j` by any different record (e.g. one byte of its body or of 286 its stored hash changed) either breaks verification or changes the head. -/ 287 theorem single_change_detected (H : List UInt8 → List UInt8) 288 (hInj : Function.Injective H) (hLen : ∀ x y, (H x).length = (H y).length) 289 (genesis : List UInt8) (log : List Rec) (head : List UInt8) 290 (hv : verify H genesis log = some head) 291 (j : Nat) (hj : j < log.length) (r : Rec) (hr : r ≠ log[j]) : 292 verify H genesis (log.set j r) ≠ some head := by 293 intro hv' 294 have he := chain_unique H hInj hLen genesis (log.set j r) log head (by simp) hv' hv 295 have := congrArg (fun m => m[j]?) he 296 simp [hj] at this 297 exact hr this 298 299 /-- LIMITATION (truncation): every prefix of a verifying log also verifies 300 (with some head). Dropping trailing records is therefore NOT detected by 301 verification alone; it is detected only by comparing the head against an 302 externally kept head value. -/ 303 theorem prefix_verifies (H : List UInt8 → List UInt8) : 304 ∀ (l1 l2 : List Rec) (p h : List UInt8), 305 verifyFrom H p (l1 ++ l2) = some h → ∃ h', verifyFrom H p l1 = some h' 306 | [], _, p, _, _ => ⟨p, rfl⟩ 307 | (b, s) :: t, l2, p, h, hv => by 308 simp only [List.cons_append, verifyFrom] at hv ⊢ 309 split at hv 310 · rw [ite_eq_left (by assumption)] 311 exact prefix_verifies H t l2 s h hv 312 · exact absurd hv (by simp) 313 314 theorem truncation_verifies (H : List UInt8 → List UInt8) (genesis : List UInt8) 315 (log : List Rec) (head : List UInt8) (hv : verify H genesis log = some head) : 316 ∃ head', verify H genesis log.dropLast = some head' := by 317 have : log = log.dropLast ++ log.drop (log.length - 1) := by 318 rw [List.dropLast_eq_take, List.take_append_drop] 319 rw [this] at hv 320 exact prefix_verifies H _ _ genesis head hv 321 322 /-! ### Negative controls: both hash hypotheses are needed. -/ 323 324 /-- Without injectivity (constant `H`), two different logs of length 1 share a head. -/ 325 theorem needs_injective : 326 ∃ (H : List UInt8 → List UInt8) (l1 l2 : List Rec) (head : List UInt8), 327 (∀ x y, (H x).length = (H y).length) ∧ l1.length = l2.length ∧ 328 verify H [] l1 = some head ∧ verify H [] l2 = some head ∧ l1 ≠ l2 := 329 ⟨fun _ => [0], [([1], [0])], [([2], [0])], [0], fun _ _ => rfl, rfl, 330 by decide, by decide, by decide⟩ 331 332 /-- Without fixed output length, the injective `H x = 0 :: x` lets two different 333 logs of length 2 share a head (the prev/body boundary shifts). -/ 334 theorem needs_fixed_length : 335 ∃ (H : List UInt8 → List UInt8) (l1 l2 : List Rec) (head : List UInt8), 336 Function.Injective H ∧ l1.length = l2.length ∧ 337 verify H [] l1 = some head ∧ verify H [] l2 = some head ∧ l1 ≠ l2 := 338 ⟨fun x => 0 :: x, [([], [0]), ([5], [0, 0, 5])], [([5], [0, 5]), ([], [0, 0, 5])], 339 [0, 0, 5], fun a b h => List.cons.inj h |>.2, rfl, by decide, by decide, by decide⟩ 340 341 end Chain 342 end Mailhand 343 344 #print axioms Mailhand.B64.decode_length 345 #print axioms Mailhand.B64.decode_bound 346 #print axioms Mailhand.Chain.chain_unique 347 #print axioms Mailhand.Chain.single_change_detected 348 #print axioms Mailhand.Chain.truncation_verifies 349 #print axioms Mailhand.B64.lenient_breaks_bound 350 #print axioms Mailhand.Chain.needs_injective 351 #print axioms Mailhand.Chain.needs_fixed_length