Skip to content
Lean Key
← Proofs

Twokey/Proofs/Wots.lean

174 lines · 20 theorems listed in proofs.json · source hash 3793a67737889fdf…

1/-
2 Twokey.Proofs.Wots
3 What is proven about the WOTS+ specification (Twokey.Spec.Wots), by Lean's kernel alone.
4-/
5import Twokey.Spec.Wots
6
7namespace Twokey.Proofs
8open Twokey.Spec
9
10/-- Walking `a + b` steps is walking `a` steps, then `b` more from where that ended. -/
11theorem chain_split (c : KeyCtx) (i : Nat) :
12 ∀ (a s b : Nat) (x : Bytes), chain c i s (a + b) x = chain c i (s + a) b (chain c i s a x)
13 | 0, s, b, x => by simp [chain]
14 | a + 1, s, b, x => by
15 rw [show a + 1 + b = (a + b) + 1 by omega]
16 simp only [chain]
17 rw [chain_split c i a (s + 1) b, show s + 1 + a = s + (a + 1) by omega]
18
19theorem getD_map_range {α : Type} (f : Nat → α) (n i : Nat) (d : α) (h : i < n) :
20 ((List.range n).map f).getD i d = f i := by
21 simp [List.getD_eq_getElem?_getD, List.getElem?_range h]
22
23theorem msgDigits_length (x : Bytes) : (msgDigits x).length = 24 := by
24 simp [msgDigits, sha256_length, msgDigitCount]
25
26theorem msgDigits_le (x : Bytes) : ∀ d ∈ msgDigits x, d ≤ 255 := by
27 intro d hd
28 simp only [msgDigits, List.mem_map] at hd
29 obtain ⟨b, _, rfl⟩ := hd
30 have := b.toNat_lt
31 omega
32
33theorem checksum_le (m : List Nat) : checksum m ≤ 255 * m.length := by
34 induction m with
35 | nil => simp [checksum]
36 | cons a t ih =>
37 simp only [checksum, List.map_cons, List.sum_cons, List.length_cons] at *
38 omega
39
40theorem checksum_msg_le (x : Bytes) : checksum (msgDigits x) ≤ 6120 := by
41 have := checksum_le (msgDigits x)
42 rw [msgDigits_length] at this
43 omega
44
45/-- Every digit is a byte: 0..255. -/
46theorem digits_le (x : Bytes) : ∀ d ∈ digits x, d ≤ 255 := by
47 intro d hd
48 simp only [digits, List.mem_append, List.mem_cons, List.not_mem_nil, or_false] at hd
49 have hc := checksum_msg_le x
50 rcases hd with h | h | h
51 · exact msgDigits_le x d h
52 · omega
53 · omega
54
55theorem digits_length (x : Bytes) : (digits x).length = 26 := by
56 simp [digits, msgDigits_length]
57
58theorem digitAt_le (x : Bytes) (i : Nat) : digitAt x i ≤ 255 := by
59 unfold digitAt
60 rw [List.getD_eq_getElem?_getD]
61 cases h : (digits x)[i]? with
62 | none => simp
63 | some d =>
64 simp only [Option.getD_some]
65 exact digits_le x d (List.mem_of_getElem? h)
66
67/-- A signature walked to the end of its chains gives back the public key. -/
68theorem recover_sign (seed : Bytes) (c : KeyCtx) (x : Bytes) :
69 recoverEnds c x (sign seed c x) = publicEnds seed c := by
70 unfold recoverEnds publicEnds
71 apply List.map_congr_left
72 intro i hi
73 have hi' : i < chainCount := List.mem_range.mp hi
74 rw [sign, getD_map_range _ _ _ _ hi']
75 have hd := digitAt_le x i
76 have hs := chain_split c i (digitAt x i) 0 (chainSteps - digitAt x i) (secret seed c i)
77 rw [Nat.zero_add] at hs
78 rw [← hs, show digitAt x i + (chainSteps - digitAt x i) = chainSteps by unfold chainSteps; omega]
79
80/-- **Completeness.** An honest signature of any intent verifies under the key it was made with. -/
81theorem wots_complete (seed : Bytes) (c : KeyCtx) (x : Bytes) :
82 verify c x (sign seed c x) (pkHash c (publicEnds seed c)) = true := by
83 simp [verify, recover_sign]
84
85/-- **The cost of verifying.** The verifier hashes `Σ (255 − d_i)` times, which is always
86`255 × (⌊checksum / 256⌋ + 2)`: from 510 to 6,375 hashes. The program's compute units follow it. -/
87theorem verifySteps_eq (x : Bytes) :
88 verifySteps x = 255 * (checksum (msgDigits x) / 256 + 2) := by
89 have hc := checksum_msg_le x
90 have hsum : ((msgDigits x).map fun d => 255 - d).sum = checksum (msgDigits x) := rfl
91 simp only [verifySteps, digits, List.map_append, List.sum_append, List.map_cons, List.map_nil,
92 List.sum_cons, List.sum_nil, hsum]
93 omega
94
95theorem verifySteps_bounds (x : Bytes) : 510 ≤ verifySteps x ∧ verifySteps x ≤ 6375 := by
96 rw [verifySteps_eq]
97 have := checksum_msg_le x
98 omega
99
100/-! ### Why one signature reveals no other
101
102A signature of digits `D` reveals, on chain `i`, the value at step `D_i`. Anyone can walk forward
103from there, so it gives away every value at a step `≥ D_i`, and nothing before (that would take a
104preimage of the hash). To sign another intent with digits `D'` from it, a forger needs
105`D'_i ≥ D_i` on all 26 chains. The checksum rules that out: -/
106
107/-- Pointwise `≤` on lists of digits, by position. -/
108def PointwiseLe (a b : List Nat) : Prop := ∀ i, i < a.length → a.getD i 0 ≤ b.getD i 0
109
110theorem checksum_mono_eq : ∀ (a b : List Nat), a.length = b.length → PointwiseLe a b →
111 (∀ d ∈ b, d ≤ 255) → checksum b ≤ checksum a ∧ (checksum b = checksum a → a = b)
112 | [], [], _, _, _ => by simp [checksum]
113 | [], _ :: _, h, _, _ => by simp at h
114 | _ :: _, [], h, _, _ => by simp at h
115 | x :: as, y :: bs, hlen, hle, hb => by
116 have hxy : x ≤ y := by simpa using hle 0 (by simp)
117 have hy : y ≤ 255 := hb y (by simp)
118 have htail : PointwiseLe as bs := fun i hi => by
119 simpa using hle (i + 1) (by simp; omega)
120 have ih := checksum_mono_eq as bs (by simpa using hlen) htail (fun d hd => hb d (by simp [hd]))
121 simp only [checksum, List.map_cons, List.sum_cons] at ih ⊢
122 refine ⟨by omega, fun heq => ?_⟩
123 have h1 : ((bs.map fun d => 255 - d).sum) = ((as.map fun d => 255 - d).sum) := by omega
124 have h2 : x = y := by omega
125 rw [h2, ih.2 h1]
126
127theorem getD_append_left' (l l' : List Nat) (n : Nat) (h : n < l.length) :
128 (l ++ l').getD n 0 = l.getD n 0 := by
129 rw [List.getD_eq_getElem?_getD, List.getD_eq_getElem?_getD, List.getElem?_append_left h]
130
131theorem getD_append_right' (l l' : List Nat) (n : Nat) (h : l.length ≤ n) :
132 (l ++ l').getD n 0 = l'.getD (n - l.length) 0 := by
133 rw [List.getD_eq_getElem?_getD, List.getD_eq_getElem?_getD, List.getElem?_append_right h]
134
135theorem digitAt_msg (x : Bytes) (i : Nat) (h : i < 24) : digitAt x i = (msgDigits x).getD i 0 := by
136 unfold digitAt digits
137 rw [getD_append_left' _ _ _ (by rw [msgDigits_length]; exact h)]
138
139theorem digitAt_24 (x : Bytes) : digitAt x 24 = checksum (msgDigits x) / 256 := by
140 unfold digitAt digits
141 rw [getD_append_right' _ _ _ (by rw [msgDigits_length]; omega), msgDigits_length]
142 rfl
143
144theorem digitAt_25 (x : Bytes) : digitAt x 25 = checksum (msgDigits x) % 256 := by
145 unfold digitAt digits
146 rw [getD_append_right' _ _ _ (by rw [msgDigits_length]; omega), msgDigits_length]
147 rfl
148
149/-- **No forgery by walking forward.** If two intents have different digits, the second has a
150digit strictly below the first's on some chain: a signature of the first never yields one of the
151second without inverting the hash. -/
152theorem wots_no_domination (x y : Bytes) (h : digits x ≠ digits y) :
153 ∃ i, i < chainCount ∧ digitAt y i < digitAt x i := by
154 apply Classical.byContradiction
155 intro hno
156 have hall : ∀ i, i < chainCount → digitAt x i ≤ digitAt y i := fun i hi =>
157 Nat.le_of_not_lt fun hlt => hno ⟨i, hi, hlt⟩
158 have hpw : PointwiseLe (msgDigits x) (msgDigits y) := by
159 intro i hi
160 rw [msgDigits_length] at hi
161 rw [← digitAt_msg x i hi, ← digitAt_msg y i hi]
162 exact hall i (by unfold chainCount; omega)
163 have hm := checksum_mono_eq (msgDigits x) (msgDigits y)
164 (by rw [msgDigits_length, msgDigits_length]) hpw (msgDigits_le y)
165 have h24 := hall 24 (by decide)
166 have h25 := hall 25 (by decide)
167 rw [digitAt_24, digitAt_24] at h24
168 rw [digitAt_25, digitAt_25] at h25
169 have hceq : checksum (msgDigits y) = checksum (msgDigits x) := by omega
170 have hmeq := hm.2 hceq
171 exact h (by simp [digits, hmeq])
172
173end Twokey.Proofs
174

Connect a wallet

Your wallet signs as the owner (Key A). Lean Key adds the post-quantum key (Key B).

Wallets

Any wallet that supports the Solana Wallet Standard appears under Installed. Lean Key never asks for your seed phrase on this site.