← Proofs
Twokey/Proofs/Machine.lean
376 lines · 24 theorems listed in proofs.json · source hash 3793a67737889fdf…
| 1 | /- |
| 2 | Twokey.Proofs.Machine |
| 3 | The security properties of the TWOKEY state machine (Twokey.Spec.Machine), kernel-checked. |
| 4 | -/ |
| 5 | import Twokey.Spec.Machine |
| 6 | |
| 7 | namespace Twokey.Proofs |
| 8 | open Twokey.Spec |
| 9 | |
| 10 | variable {e : Env} {w w' : World} {c : Call} |
| 11 | |
| 12 | /-! ## What a successful step of each instruction means -/ |
| 13 | |
| 14 | theorem stepInit_ok {vid : UInt64} {pk0 : Bytes} (h : stepInit e w c vid pk0 = .ok w') : |
| 15 | c.owner ≠ e.addr ∧ c.signed = true ∧ w.vault = none ∧ pk0 ≠ zero32 ∧ |
| 16 | e.addr = e.pda c.owner vid ∧ w.rentMin - w.lamports e.addr ≤ w.lamports c.owner ∧ |
| 17 | w' = { w with |
| 18 | vault := some { owner := c.owner, vaultId := vid, keyIndex := 0, pkHash := pk0, closed := false }, |
| 19 | lamports := setL (setL w.lamports c.owner (w.lamports c.owner - (w.rentMin - w.lamports e.addr))) |
| 20 | e.addr (w.lamports e.addr + (w.rentMin - w.lamports e.addr)) } := by |
| 21 | unfold stepInit at h |
| 22 | dsimp only at h |
| 23 | repeat' (split at h) |
| 24 | all_goals (first | (simp at h; done) | skip) |
| 25 | rename_i h1 h2 h3 h4 h5 h6 |
| 26 | cases h |
| 27 | refine ⟨h1, by simpa using h2, by simpa using h3, h4, by simpa using h5, by omega, rfl⟩ |
| 28 | |
| 29 | theorem stepSol_ok {amount : UInt64} {next : Bytes} {sig : List Bytes} |
| 30 | (h : stepSol e w c amount next sig = .ok w') : |
| 31 | c.dest ≠ e.addr ∧ c.signed = true ∧ ∃ v, w.vault = some v ∧ v.owner = c.owner ∧ amount ≠ 0 ∧ |
| 32 | next ≠ zero32 ∧ |
| 33 | verify (ctxOf e v) (intentOf e v actWithdrawSol zero32 amount c.dest next).encode sig v.pkHash = true ∧ |
| 34 | w.rentMin ≤ w.lamports e.addr ∧ amount.toNat ≤ w.lamports e.addr - w.rentMin ∧ v.keyIndex ≠ maxU64 ∧ |
| 35 | w' = { w with |
| 36 | lamports := setL (setL w.lamports e.addr (w.lamports e.addr - amount.toNat)) c.dest |
| 37 | (w.lamports c.dest + amount.toNat), |
| 38 | vault := some (rotate v next) } := by |
| 39 | unfold stepSol at h |
| 40 | repeat' (split at h) |
| 41 | all_goals (first | (simp at h; done) | skip) |
| 42 | rename_i hd hs _ v hv ho ha hn hver hr1 hr2 hk |
| 43 | cases h |
| 44 | exact ⟨hd, by simpa using hs, v, hv, by simpa using ho, ha, hn, by simpa using hver, by omega, |
| 45 | by omega, hk, rfl⟩ |
| 46 | |
| 47 | theorem stepToken_ok {amount : UInt64} {next : Bytes} {sig : List Bytes} |
| 48 | (h : stepToken e w c amount next sig = .ok w') : |
| 49 | c.src ≠ e.addr ∧ c.dest ≠ e.addr ∧ c.src ≠ c.dest ∧ c.signed = true ∧ |
| 50 | ∃ v s d, w.vault = some v ∧ v.owner = c.owner ∧ w.tokens c.src = some s ∧ |
| 51 | w.tokens c.dest = some d ∧ s.owner = e.addr ∧ s.mint = c.mint ∧ w.hook c.mint = false ∧ |
| 52 | amount ≠ 0 ∧ next ≠ zero32 ∧ |
| 53 | verify (ctxOf e v) (intentOf e v actWithdrawToken c.mint amount c.dest next).encode sig v.pkHash = true ∧ |
| 54 | d.mint = c.mint ∧ amount.toNat ≤ s.amount ∧ v.keyIndex ≠ maxU64 ∧ |
| 55 | w' = { w with |
| 56 | tokens := setT (setT w.tokens c.src (some { s with amount := s.amount - amount.toNat })) c.dest |
| 57 | (some { d with amount := d.amount + amount.toNat }), |
| 58 | vault := some (rotate v next) } := by |
| 59 | unfold stepToken at h |
| 60 | repeat' (split at h) |
| 61 | all_goals (first | (simp at h; done) | skip) |
| 62 | rename_i hacc hs _ v hv ho _ _ s d hsrc hdst hso hhook ha hn hver hdm hk |
| 63 | cases h |
| 64 | simp only [not_or] at hacc hso hdm |
| 65 | exact ⟨hacc.1, hacc.2.1, hacc.2.2, by simpa using hs, v, s, d, hv, by simpa using ho, hsrc, hdst, |
| 66 | by simpa using hso.1, by simpa using hso.2, by simpa using hhook, ha, hn, by simpa using hver, |
| 67 | by simpa using hdm.1, by omega, hk, rfl⟩ |
| 68 | |
| 69 | theorem stepCloseToken_ok {next : Bytes} {sig : List Bytes} |
| 70 | (h : stepCloseToken e w c next sig = .ok w') : |
| 71 | c.src ≠ e.addr ∧ c.dest ≠ e.addr ∧ c.src ≠ c.dest ∧ c.signed = true ∧ |
| 72 | ∃ v s, w.vault = some v ∧ v.owner = c.owner ∧ w.tokens c.src = some s ∧ s.owner = e.addr ∧ |
| 73 | next ≠ zero32 ∧ |
| 74 | verify (ctxOf e v) (intentOf e v actCloseTokenAccount s.mint 0 c.dest next).encode sig v.pkHash = true ∧ |
| 75 | s.amount = 0 ∧ v.keyIndex ≠ maxU64 ∧ |
| 76 | w' = { w with |
| 77 | tokens := setT w.tokens c.src none, |
| 78 | lamports := setL (setL w.lamports c.src 0) c.dest (w.lamports c.dest + w.lamports c.src), |
| 79 | vault := some (rotate v next) } := by |
| 80 | unfold stepCloseToken at h |
| 81 | repeat' (split at h) |
| 82 | all_goals (first | (simp at h; done) | skip) |
| 83 | rename_i hacc hs _ v hv ho _ s hsrc hso hn hver ham hk |
| 84 | cases h |
| 85 | simp only [not_or] at hacc |
| 86 | exact ⟨hacc.1, hacc.2.1, hacc.2.2, by simpa using hs, v, s, hv, by simpa using ho, hsrc, |
| 87 | by simpa using hso, hn, by simpa using hver, by simpa using ham, hk, rfl⟩ |
| 88 | |
| 89 | theorem stepCloseVault_ok {next : Bytes} {sig : List Bytes} |
| 90 | (h : stepCloseVault e w c next sig = .ok w') : |
| 91 | c.dest ≠ e.addr ∧ c.signed = true ∧ ∃ v, w.vault = some v ∧ v.owner = c.owner ∧ next ≠ zero32 ∧ |
| 92 | verify (ctxOf e v) (intentOf e v actCloseVault zero32 0 c.dest next).encode sig v.pkHash = true ∧ |
| 93 | w.rentMin ≤ w.lamports e.addr ∧ v.keyIndex ≠ maxU64 ∧ |
| 94 | w' = { w with |
| 95 | lamports := setL (setL w.lamports e.addr w.rentMin) c.dest |
| 96 | (w.lamports c.dest + (w.lamports e.addr - w.rentMin)), |
| 97 | vault := some { rotate v next with closed := true } } := by |
| 98 | unfold stepCloseVault at h |
| 99 | repeat' (split at h) |
| 100 | all_goals (first | (simp at h; done) | skip) |
| 101 | rename_i hd hs _ v hv ho hn hver hr hk |
| 102 | cases h |
| 103 | exact ⟨hd, by simpa using hs, v, hv, by simpa using ho, hn, by simpa using hver, by omega, hk, rfl⟩ |
| 104 | |
| 105 | /-! ## Helpers on the update functions -/ |
| 106 | |
| 107 | theorem setL_same (f : Key → Nat) (k : Key) (n : Nat) : setL f k n k = n := by simp [setL] |
| 108 | theorem setL_other (f : Key → Nat) (k k' : Key) (n : Nat) (h : k' ≠ k) : setL f k n k' = f k' := by |
| 109 | simp [setL, h] |
| 110 | theorem setT_same (f : Key → Option TokenAcct) (k : Key) (t : Option TokenAcct) : setT f k t k = t := by |
| 111 | simp [setT] |
| 112 | theorem setT_other (f : Key → Option TokenAcct) (k k' : Key) (t : Option TokenAcct) (h : k' ≠ k) : |
| 113 | setT f k t k' = f k' := by simp [setT, h] |
| 114 | |
| 115 | /-- The next key a call names, for the instructions that carry a hash signature. -/ |
| 116 | def _root_.Twokey.Spec.Op.next? : Op → Option Bytes |
| 117 | | .init _ _ => none |
| 118 | | .withdrawSol _ n _ => some n |
| 119 | | .withdrawToken _ n _ => some n |
| 120 | | .closeTokenAccount n _ => some n |
| 121 | | .closeVault n _ => some n |
| 122 | |
| 123 | /-! ## The properties -/ |
| 124 | |
| 125 | /-- **Owner signature.** Every instruction other than `InitVault` succeeds only when the vault is |
| 126 | open and its recorded owner signed the transaction. -/ |
| 127 | theorem withdraw_requires_owner_signer (h : step e w c = .ok w') (hop : c.op.sig? = some sig) : |
| 128 | c.signed = true ∧ ∃ v, w.vault = some v ∧ v.owner = c.owner := by |
| 129 | unfold step at h |
| 130 | cases hc : c.op <;> rw [hc] at h hop <;> simp [Op.sig?] at hop |
| 131 | · obtain ⟨_, hs, v, hv, ho, _⟩ := stepSol_ok h; exact ⟨hs, v, hv, ho⟩ |
| 132 | · obtain ⟨_, _, _, hs, v, _, _, hv, ho, _⟩ := stepToken_ok h; exact ⟨hs, v, hv, ho⟩ |
| 133 | · obtain ⟨_, _, _, hs, v, _, hv, ho, _⟩ := stepCloseToken_ok h; exact ⟨hs, v, hv, ho⟩ |
| 134 | · obtain ⟨_, hs, v, hv, ho, _⟩ := stepCloseVault_ok h; exact ⟨hs, v, hv, ho⟩ |
| 135 | |
| 136 | /-- **Hash signature.** Every instruction that moves anything succeeds only when its WOTS+ |
| 137 | signature verifies, under the vault's current one-time key, for an intent naming this program, |
| 138 | this vault and the current key index. -/ |
| 139 | theorem withdraw_requires_valid_wots (h : step e w c = .ok w') (hop : c.op.sig? = some sig) : |
| 140 | ∃ v i, w.vault = some v ∧ i.programId = e.programId ∧ i.vault = e.addr ∧ |
| 141 | i.keyIndex = v.keyIndex ∧ verify (ctxOf e v) (Intent.encode i) sig v.pkHash = true := by |
| 142 | unfold step at h |
| 143 | cases hc : c.op <;> rw [hc] at h hop <;> simp [Op.sig?] at hop <;> subst hop |
| 144 | · obtain ⟨_, _, v, hv, _, _, _, hver, _⟩ := stepSol_ok h; exact ⟨v, _, hv, rfl, rfl, rfl, hver⟩ |
| 145 | · obtain ⟨_, _, _, _, v, _, _, hv, _, _, _, _, _, _, _, _, hver, _⟩ := stepToken_ok h |
| 146 | exact ⟨v, _, hv, rfl, rfl, rfl, hver⟩ |
| 147 | · obtain ⟨_, _, _, _, v, _, hv, _, _, _, _, hver, _⟩ := stepCloseToken_ok h |
| 148 | exact ⟨v, _, hv, rfl, rfl, rfl, hver⟩ |
| 149 | · obtain ⟨_, _, v, hv, _, _, hver, _⟩ := stepCloseVault_ok h; exact ⟨v, _, hv, rfl, rfl, rfl, hver⟩ |
| 150 | |
| 151 | /-- **Rotation.** After every such success the vault holds the next one-time key: its index is one |
| 152 | more and its hash is the one the intent named. The owner never changes. -/ |
| 153 | theorem withdraw_rotates_key (h : step e w c = .ok w') (hop : c.op.next? = some next) : |
| 154 | ∃ v v', w.vault = some v ∧ w'.vault = some v' ∧ v'.keyIndex = v.keyIndex + 1 ∧ |
| 155 | v'.pkHash = next ∧ v'.owner = c.owner ∧ v'.owner = v.owner := by |
| 156 | unfold step at h |
| 157 | cases hc : c.op <;> rw [hc] at h hop <;> simp [Op.next?] at hop <;> subst hop |
| 158 | · obtain ⟨_, _, v, hv, ho, _, _, _, _, _, _, rfl⟩ := stepSol_ok h |
| 159 | exact ⟨v, _, hv, rfl, rfl, rfl, ho, rfl⟩ |
| 160 | · obtain ⟨_, _, _, _, v, _, _, hv, ho, _, _, _, _, _, _, _, _, _, _, _, rfl⟩ := stepToken_ok h |
| 161 | exact ⟨v, _, hv, rfl, rfl, rfl, ho, rfl⟩ |
| 162 | · obtain ⟨_, _, _, _, v, _, hv, ho, _, _, _, _, _, _, rfl⟩ := stepCloseToken_ok h |
| 163 | exact ⟨v, _, hv, rfl, rfl, rfl, ho, rfl⟩ |
| 164 | · obtain ⟨_, _, v, hv, ho, _, _, _, _, rfl⟩ := stepCloseVault_ok h |
| 165 | exact ⟨v, _, hv, rfl, rfl, rfl, ho, rfl⟩ |
| 166 | |
| 167 | /-- **The intent is the transfer (SOL).** What moves is exactly the signed intent's amount, from the |
| 168 | vault to the intent's destination, and the intent is a SOL intent. -/ |
| 169 | theorem intent_binds_transfer {amount : UInt64} {next : Bytes} {sig : List Bytes} |
| 170 | (h : step e w c = .ok w') (hop : c.op = .withdrawSol amount next sig) : |
| 171 | ∃ v, w.vault = some v ∧ |
| 172 | verify (ctxOf e v) (intentOf e v actWithdrawSol zero32 amount c.dest next).encode sig v.pkHash = true ∧ |
| 173 | (intentOf e v actWithdrawSol zero32 amount c.dest next).asset = zero32 ∧ |
| 174 | w'.lamports (intentOf e v actWithdrawSol zero32 amount c.dest next).destination = |
| 175 | w.lamports c.dest + (intentOf e v actWithdrawSol zero32 amount c.dest next).amount.toNat ∧ |
| 176 | w'.lamports e.addr = |
| 177 | w.lamports e.addr - (intentOf e v actWithdrawSol zero32 amount c.dest next).amount.toNat := by |
| 178 | unfold step at h |
| 179 | rw [hop] at h |
| 180 | obtain ⟨hd, _, v, hv, _, _, _, hver, _, _, _, rfl⟩ := stepSol_ok h |
| 181 | refine ⟨v, hv, hver, rfl, ?_, ?_⟩ |
| 182 | · simp [intentOf, setL_same] |
| 183 | · simp only [intentOf]; rw [setL_other _ _ _ _ (Ne.symm hd), setL_same] |
| 184 | |
| 185 | /-- **The intent is the transfer (tokens).** The tokens that move are the intent's amount of the |
| 186 | intent's asset (the mint), from the vault's own token account to the intent's destination. -/ |
| 187 | theorem intent_binds_token_transfer {amount : UInt64} {next : Bytes} {sig : List Bytes} |
| 188 | (h : step e w c = .ok w') (hop : c.op = .withdrawToken amount next sig) : |
| 189 | ∃ v s d, w.vault = some v ∧ w.tokens c.src = some s ∧ w.tokens c.dest = some d ∧ |
| 190 | verify (ctxOf e v) (intentOf e v actWithdrawToken c.mint amount c.dest next).encode sig v.pkHash = true ∧ |
| 191 | s.owner = e.addr ∧ s.mint = (intentOf e v actWithdrawToken c.mint amount c.dest next).asset ∧ |
| 192 | d.mint = (intentOf e v actWithdrawToken c.mint amount c.dest next).asset ∧ |
| 193 | w'.tokens (intentOf e v actWithdrawToken c.mint amount c.dest next).destination = |
| 194 | some { d with amount := d.amount + amount.toNat } ∧ |
| 195 | w'.tokens c.src = some { s with amount := s.amount - amount.toNat } := by |
| 196 | unfold step at h |
| 197 | rw [hop] at h |
| 198 | obtain ⟨_, _, hsd, _, v, s, d, hv, _, hsrc, hdst, hso, hsm, _, _, _, hver, hdm, _, _, rfl⟩ := stepToken_ok h |
| 199 | refine ⟨v, s, d, hv, hsrc, hdst, hver, hso, hsm, hdm, ?_, ?_⟩ |
| 200 | · simp [intentOf, setT_same] |
| 201 | · simp only; rw [setT_other _ _ _ _ hsd, setT_same] |
| 202 | |
| 203 | /-- **Lamports are conserved.** Every successful instruction moves lamports between two distinct |
| 204 | accounts at most: their sum is unchanged and every other account is untouched. -/ |
| 205 | theorem lamports_conserved (h : step e w c = .ok w') : |
| 206 | ∃ a b, a ≠ b ∧ (∀ k, k ≠ a → k ≠ b → w'.lamports k = w.lamports k) ∧ |
| 207 | w'.lamports a + w'.lamports b = w.lamports a + w.lamports b := by |
| 208 | unfold step at h |
| 209 | cases hc : c.op <;> rw [hc] at h |
| 210 | · obtain ⟨ho, _, _, _, _, hfund, rfl⟩ := stepInit_ok h |
| 211 | refine ⟨c.owner, e.addr, ho, fun k h1 h2 => ?_, ?_⟩ |
| 212 | · simp [setL, h1, h2] |
| 213 | · simp only; rw [setL_same, setL_other _ _ _ _ ho, setL_same]; omega |
| 214 | · obtain ⟨hd, _, v, _, _, _, _, _, _, hle, _, rfl⟩ := stepSol_ok h |
| 215 | refine ⟨e.addr, c.dest, Ne.symm hd, fun k h1 h2 => ?_, ?_⟩ |
| 216 | · simp [setL, h1, h2] |
| 217 | · simp only; rw [setL_same, setL_other _ _ _ _ (Ne.symm hd), setL_same] |
| 218 | have := Nat.sub_le (w.lamports e.addr) w.rentMin |
| 219 | omega |
| 220 | · obtain ⟨_, hd, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, rfl⟩ := stepToken_ok h |
| 221 | exact ⟨e.addr, c.dest, Ne.symm hd, fun _ _ _ => rfl, rfl⟩ |
| 222 | · obtain ⟨_, _, hsd, _, _, _, _, _, _, _, _, _, _, _, rfl⟩ := stepCloseToken_ok h |
| 223 | refine ⟨c.src, c.dest, hsd, fun k h1 h2 => ?_, ?_⟩ |
| 224 | · simp [setL, h1, h2] |
| 225 | · simp only; rw [setL_same, setL_other _ _ _ _ hsd, setL_same]; omega |
| 226 | · obtain ⟨hd, _, v, _, _, _, _, hr, _, rfl⟩ := stepCloseVault_ok h |
| 227 | refine ⟨e.addr, c.dest, Ne.symm hd, fun k h1 h2 => ?_, ?_⟩ |
| 228 | · simp [setL, h1, h2] |
| 229 | · simp only; rw [setL_same, setL_other _ _ _ _ (Ne.symm hd), setL_same]; omega |
| 230 | |
| 231 | /-- A token account's amount (0 for no account). -/ |
| 232 | def amt (w : World) (k : Key) : Nat := ((w.tokens k).map TokenAcct.amount).getD 0 |
| 233 | |
| 234 | /-- **Tokens are conserved.** Every successful instruction changes at most two token accounts, and |
| 235 | the sum of their amounts stays the same (a closed account held 0). -/ |
| 236 | theorem token_amount_conserved (h : step e w c = .ok w') : |
| 237 | ∃ a b, a ≠ b ∧ (∀ k, k ≠ a → k ≠ b → w'.tokens k = w.tokens k) ∧ |
| 238 | amt w' a + amt w' b = amt w a + amt w b := by |
| 239 | unfold step at h |
| 240 | cases hc : c.op <;> rw [hc] at h |
| 241 | · obtain ⟨ho, _, _, _, _, _, rfl⟩ := stepInit_ok h |
| 242 | exact ⟨c.owner, e.addr, ho, fun _ _ _ => rfl, rfl⟩ |
| 243 | · obtain ⟨hd, _, _, _, _, _, _, _, _, _, _, _, rfl⟩ := stepSol_ok h |
| 244 | exact ⟨e.addr, c.dest, Ne.symm hd, fun _ _ _ => rfl, rfl⟩ |
| 245 | · obtain ⟨_, _, hsd, _, v, s, d, _, _, hsrc, hdst, _, _, _, _, _, _, _, hle, _, rfl⟩ := stepToken_ok h |
| 246 | refine ⟨c.src, c.dest, hsd, fun k h1 h2 => ?_, ?_⟩ |
| 247 | · simp [setT, h1, h2] |
| 248 | · simp only [amt] |
| 249 | rw [setT_same, setT_other _ _ _ _ hsd, setT_same, hsrc, hdst] |
| 250 | simp only [Option.map_some, Option.getD_some] |
| 251 | omega |
| 252 | · obtain ⟨_, _, hsd, _, v, s, _, _, hsrc, _, _, _, ham, _, rfl⟩ := stepCloseToken_ok h |
| 253 | refine ⟨c.src, c.dest, hsd, fun k h1 h2 => ?_, ?_⟩ |
| 254 | · simp [setT, h1] |
| 255 | · simp only [amt] |
| 256 | rw [setT_same, setT_other _ _ _ _ (Ne.symm hsd), hsrc] |
| 257 | simp [ham] |
| 258 | · obtain ⟨hd, _, _, _, _, _, _, _, _, rfl⟩ := stepCloseVault_ok h |
| 259 | exact ⟨e.addr, c.dest, Ne.symm hd, fun _ _ _ => rfl, rfl⟩ |
| 260 | |
| 261 | /-- The vault holds at least its rent-exempt minimum whenever it is open. -/ |
| 262 | def RentExempt (e : Env) (w : World) : Prop := w.vault.isSome = true → w.rentMin ≤ w.lamports e.addr |
| 263 | |
| 264 | /-- **Rent.** No instruction takes an open vault below its rent-exempt minimum, and opening one |
| 265 | funds it to at least that minimum. -/ |
| 266 | theorem vault_stays_rent_exempt (hinv : RentExempt e w) (h : step e w c = .ok w') : RentExempt e w' := by |
| 267 | intro _ |
| 268 | unfold step at h |
| 269 | cases hc : c.op <;> rw [hc] at h |
| 270 | · obtain ⟨ho, _, _, _, _, _, rfl⟩ := stepInit_ok h |
| 271 | simp only; rw [setL_same]; omega |
| 272 | · obtain ⟨hd, _, v, hv, _, _, _, _, hr, hle, _, rfl⟩ := stepSol_ok h |
| 273 | simp only; rw [setL_other _ _ _ _ (Ne.symm hd), setL_same]; omega |
| 274 | · obtain ⟨_, _, _, _, v, _, _, hv, _, _, _, _, _, _, _, _, _, _, _, _, rfl⟩ := stepToken_ok h |
| 275 | exact hinv (by simp [hv]) |
| 276 | · obtain ⟨hsa, hda, _, _, v, _, hv, _, _, _, _, _, _, _, rfl⟩ := stepCloseToken_ok h |
| 277 | simp only |
| 278 | rw [setL_other _ _ _ _ (Ne.symm hda), setL_other _ _ _ _ (Ne.symm hsa)] |
| 279 | exact hinv (by simp [hv]) |
| 280 | · obtain ⟨hd, _, v, hv, _, _, _, hr, _, rfl⟩ := stepCloseVault_ok h |
| 281 | simp only; rw [setL_other _ _ _ _ (Ne.symm hd), setL_same]; exact Nat.le_refl _ |
| 282 | |
| 283 | /-- **Closing needs both keys.** `CloseVault` succeeds only with the owner's signature and a valid |
| 284 | hash signature of the closing intent. -/ |
| 285 | theorem close_requires_both {next : Bytes} {sig : List Bytes} (h : step e w c = .ok w') |
| 286 | (hop : c.op = .closeVault next sig) : |
| 287 | c.signed = true ∧ ∃ v, w.vault = some v ∧ v.owner = c.owner ∧ |
| 288 | verify (ctxOf e v) (intentOf e v actCloseVault zero32 0 c.dest next).encode sig v.pkHash = true := by |
| 289 | unfold step at h |
| 290 | rw [hop] at h |
| 291 | obtain ⟨_, hs, v, hv, ho, _, hver, _⟩ := stepCloseVault_ok h |
| 292 | exact ⟨hs, v, hv, ho, hver⟩ |
| 293 | |
| 294 | /-- **Once.** `InitVault` succeeds only where no vault is open, and the vault it opens belongs to |
| 295 | the signer whose address derivation it is. -/ |
| 296 | theorem init_once {vid : UInt64} {pk0 : Bytes} (h : step e w c = .ok w') (hop : c.op = .init vid pk0) : |
| 297 | w.vault = none ∧ c.signed = true ∧ e.addr = e.pda c.owner vid ∧ |
| 298 | w'.vault = some { owner := c.owner, vaultId := vid, keyIndex := 0, pkHash := pk0, closed := false } := by |
| 299 | unfold step at h |
| 300 | rw [hop] at h |
| 301 | obtain ⟨_, hs, hnone, _, hpda, _, rfl⟩ := stepInit_ok h |
| 302 | exact ⟨hnone, hs, hpda, rfl⟩ |
| 303 | |
| 304 | /-- Once a vault is open (closed vaults included: closing leaves a tombstone), it cannot be opened |
| 305 | again, by anyone. -/ |
| 306 | theorem no_reopen {vid : UInt64} {pk0 : Bytes} (hv : w.vault.isSome = true) (hop : c.op = .init vid pk0) : |
| 307 | ∃ err, step e w c = .error err := by |
| 308 | cases h : step e w c with |
| 309 | | error err => exact ⟨err, rfl⟩ |
| 310 | | ok w' => |
| 311 | have := (init_once h hop).1 |
| 312 | simp [this] at hv |
| 313 | |
| 314 | /-- An instruction with a hash signature succeeds only when that signature is valid for the vault's |
| 315 | current key. -/ |
| 316 | theorem hashAuthorized_of_ok (h : step e w c = .ok w') (hop : c.op.sig? = some sig) : |
| 317 | HashAuthorized e w c := by |
| 318 | obtain ⟨v, i, hv, _, _, _, hver⟩ := withdraw_requires_valid_wots h hop |
| 319 | exact ⟨v, sig, i, hv, hop, hver⟩ |
| 320 | |
| 321 | /-- **No admin path.** Every successful instruction is either the opening of an unopened vault by |
| 322 | its owner, or carries both the owner's signature and a valid hash signature for the vault's current |
| 323 | key. There is no other way to change anything. -/ |
| 324 | theorem no_admin_path (h : step e w c = .ok w') : |
| 325 | (∃ vid pk0, c.op = .init vid pk0 ∧ w.vault = none ∧ c.signed = true ∧ e.addr = e.pda c.owner vid) ∨ |
| 326 | (c.signed = true ∧ (∃ v, w.vault = some v ∧ v.owner = c.owner) ∧ HashAuthorized e w c) := by |
| 327 | cases hop : c.op with |
| 328 | | init vid pk0 => |
| 329 | obtain ⟨hn, hs, hpda, _⟩ := init_once h hop |
| 330 | exact Or.inl ⟨vid, pk0, rfl, hn, hs, hpda⟩ |
| 331 | | withdrawSol a n s => |
| 332 | have hs : c.op.sig? = some s := by simp [hop, Op.sig?] |
| 333 | exact Or.inr ⟨(withdraw_requires_owner_signer h hs).1, (withdraw_requires_owner_signer h hs).2, |
| 334 | hashAuthorized_of_ok h hs⟩ |
| 335 | | withdrawToken a n s => |
| 336 | have hs : c.op.sig? = some s := by simp [hop, Op.sig?] |
| 337 | exact Or.inr ⟨(withdraw_requires_owner_signer h hs).1, (withdraw_requires_owner_signer h hs).2, |
| 338 | hashAuthorized_of_ok h hs⟩ |
| 339 | | closeTokenAccount n s => |
| 340 | have hs : c.op.sig? = some s := by simp [hop, Op.sig?] |
| 341 | exact Or.inr ⟨(withdraw_requires_owner_signer h hs).1, (withdraw_requires_owner_signer h hs).2, |
| 342 | hashAuthorized_of_ok h hs⟩ |
| 343 | | closeVault n s => |
| 344 | have hs : c.op.sig? = some s := by simp [hop, Op.sig?] |
| 345 | exact Or.inr ⟨(withdraw_requires_owner_signer h hs).1, (withdraw_requires_owner_signer h hs).2, |
| 346 | hashAuthorized_of_ok h hs⟩ |
| 347 | |
| 348 | /-- With a vault open, a call without a valid hash signature changes nothing at all. -/ |
| 349 | theorem apply_unauthorized (hv : w.vault.isSome = true) (hno : ¬ HashAuthorized e w c) : |
| 350 | apply e w c = w := by |
| 351 | unfold apply |
| 352 | cases h : step e w c with |
| 353 | | error _ => rfl |
| 354 | | ok w' => |
| 355 | exfalso |
| 356 | rcases no_admin_path h with ⟨_, _, _, hn, _⟩ | ⟨_, _, ha⟩ |
| 357 | · simp [hn] at hv |
| 358 | · exact hno ha |
| 359 | |
| 360 | /-- **The owner's wallet key alone moves nothing.** Take any sequence of instructions, signed by |
| 361 | anyone, the vault's owner key included (a drainer holding it, or a quantum computer that recovered |
| 362 | it, or the Open Key Challenge where it is public). If none of them carries a valid WOTS+ signature |
| 363 | for the vault's one-time key current when it runs, the world is exactly as before: not a lamport, |
| 364 | not a token, not the key moves, so nothing is stolen and the vault is not locked. -/ |
| 365 | theorem public_owner_key_cannot_move_funds : |
| 366 | ∀ (calls : List Call) (w : World), w.vault.isSome = true → NoHashSignature e w calls → |
| 367 | run e w calls = w |
| 368 | | [], _, _, _ => rfl |
| 369 | | c :: cs, w, hv, ⟨hno, hrest⟩ => by |
| 370 | have ha := apply_unauthorized hv hno |
| 371 | simp only [run] |
| 372 | rw [ha] at hrest ⊢ |
| 373 | exact public_owner_key_cannot_move_funds cs w hv hrest |
| 374 | |
| 375 | end Twokey.Proofs |
| 376 |