Skip to content
Lean Key
← Proofs

Twokey/Proofs/Code.lean

183 lines · 7 theorems listed in proofs.json · source hash 3793a67737889fdf…

1/-
2 Twokey.Proofs.Code
3 Theorems about the program's own source (Twokey.Program, the SolanaM code lean-svm compiles into
4 the deployed .so), stated on lean-svm's SvmState semantics, not about the abstract model. The
5 compiler is trusted: these are not proofs about the sBPF bytes. Every instruction that moves anything runs
6 `checkVault`, and
7 `checkVault` passes only when account 0 signed, the vault account belongs to this program, and the
8 owner stored in the vault is account 0's address. The WOTS+ verification in `authorize` is tested
9 against the specification byte for byte (`make vectors`, LiteSVM), not proven equal to it.
10-/
11import Twokey.Program
12import LeanSVM.Verify.Specs
13
14namespace Twokey.Proofs.Code
15open LeanSVM LeanSVM.CPI Twokey
16
17theorem nextDistinct_state (acc : AccountView) (s s' : SvmState) (x : AccountView)
18 (h : nextDistinct acc s = .ok x s') : s' = s := by
19 unfold nextDistinct at h
20 obtain ⟨n, s0, hn, h⟩ := bind_ok_elim h
21 rw [AccountView.nextCursor_exact] at hn
22 cases hn
23 obtain ⟨u, s1, hu, h⟩ := bind_ok_elim h
24 have := ((AccountView.requireNonDup_ok_iff _ _ _ _).mp hu).2
25 subst this
26 simp [pure, SolanaM.pure] at h
27 exact h.2.symm
28
29/-- What `checkVault` establishes when it passes. -/
30def VaultChecked (s : SvmState) (owner vault progId : UInt64) : Prop :=
31 s.readU8 (owner + ABI.accIsSignerOffset) ≠ 0 ∧
32 Verify.readPubkey s (AccountView.ownerPtr vault) = Verify.readPubkey s progId ∧
33 Verify.readPubkey s (AccountView.dataPtr vault + offOwner) = Verify.readPubkey s (AccountView.keyPtr owner)
34
35/-- **`checkVault` in the program's source.** It passes only if the owner account signed, the vault is
36owned by this program, and the vault's stored owner is the owner account's address. -/
37theorem checkVault_ok {owner vault progId : UInt64} {s s' : SvmState}
38 (h : checkVault owner vault progId s = .ok () s') :
39 VaultChecked s owner vault progId ∧ s' = s := by
40 unfold checkVault at h
41 obtain ⟨b1, t1, hb1, k1⟩ := bind_ok_elim h
42 simp only [AccountView.isSigner, bind, SolanaM.bind, rawLoadU8, pure, SolanaM.pure, SvmStep.ok.injEq] at hb1
43 obtain ⟨rfl, rfl⟩ := hb1
44 obtain ⟨_, t2, hr1, k2⟩ := bind_ok_elim k1
45 obtain ⟨hsig, rfl⟩ := (Verify.require_ok_iff _ _ _ _ _).mp hr1
46 obtain ⟨b3, t3, hb3, k3⟩ := bind_ok_elim k2
47 simp only [AccountView.isWritable, bind, SolanaM.bind, rawLoadU8, pure, SolanaM.pure, SvmStep.ok.injEq] at hb3
48 obtain ⟨rfl, rfl⟩ := hb3
49 obtain ⟨_, t4, hr3, k4⟩ := bind_ok_elim k3
50 obtain ⟨_, rfl⟩ := (Verify.require_ok_iff _ _ _ _ _).mp hr3
51 obtain ⟨b5, t5, hb5, k5⟩ := bind_ok_elim k4
52 obtain ⟨rfl, rfl⟩ := (Verify.pubkeyEq_ok_iff _ _ _ _ _).mp hb5
53 obtain ⟨_, t6, hr5, k6⟩ := bind_ok_elim k5
54 obtain ⟨hprog, rfl⟩ := (Verify.require_ok_iff _ _ _ _ _).mp hr5
55 obtain ⟨b7, t7, hb7, k7⟩ := bind_ok_elim k6
56 simp only [AccountView.dataLen, rawLoadU64, SvmStep.ok.injEq] at hb7
57 obtain ⟨rfl, rfl⟩ := hb7
58 obtain ⟨_, t8, hr7, k8⟩ := bind_ok_elim k7
59 obtain ⟨_, rfl⟩ := (Verify.require_ok_iff _ _ _ _ _).mp hr7
60 obtain ⟨b9, t9, hb9, k9⟩ := bind_ok_elim k8
61 simp only [AccountView.readDataU8, rawLoadU8, SvmStep.ok.injEq] at hb9
62 obtain ⟨rfl, rfl⟩ := hb9
63 obtain ⟨_, t10, hr9, k10⟩ := bind_ok_elim k9
64 obtain ⟨_, rfl⟩ := (Verify.require_ok_iff _ _ _ _ _).mp hr9
65 obtain ⟨b11, t11, hb11, k11⟩ := bind_ok_elim k10
66 obtain ⟨rfl, rfl⟩ := (Verify.pubkeyEq_ok_iff _ _ _ _ _).mp hb11
67 obtain ⟨hown, rfl⟩ := (Verify.require_ok_iff _ _ _ _ _).mp k11
68 refine ⟨⟨?_, by simpa using hprog, by simpa using hown⟩, rfl⟩
69 intro hz
70 simp [hz] at hsig
71
72/-- **`WithdrawSol` in the program's source** succeeds only after `checkVault` passed on the state it
73started from: the owner signed and the vault is this program's and records that owner. -/
74theorem withdrawSol_checks (a0 progId ix : UInt64) (s s' : SvmState)
75 (h : withdrawSol a0 progId ix s = .ok () s') : ∃ vault, VaultChecked s a0 vault progId := by
76 unfold withdrawSol at h
77 obtain ⟨_, s1, h1, k1⟩ := bind_ok_elim h
78 have e1 := ((AccountView.requireNonDup_ok_iff _ _ _ _).mp h1).2
79 subst e1
80 obtain ⟨vault, s2, h2, k2⟩ := bind_ok_elim k1
81 have e2 := nextDistinct_state _ _ _ _ h2
82 subst e2
83 obtain ⟨slot, s3, h3, k3⟩ := bind_ok_elim k2
84 rw [AccountView.nextCursor_exact] at h3
85 cases h3
86 obtain ⟨dest, s4, h4, k4⟩ := bind_ok_elim k3
87 have e4 := ((AccountView.resolveDup_ok_iff _ _ _ _ _ _ _).mp h4).1
88 subst e4
89 obtain ⟨_, s5, h5, _⟩ := bind_ok_elim k4
90 exact ⟨vault, (checkVault_ok h5).1⟩
91
92/-- **`CloseVault` in the program's source**: the same checks. -/
93theorem closeVault_checks (a0 progId ix : UInt64) (s s' : SvmState)
94 (h : closeVault a0 progId ix s = .ok () s') : ∃ vault, VaultChecked s a0 vault progId := by
95 unfold closeVault at h
96 obtain ⟨_, s1, h1, k1⟩ := bind_ok_elim h
97 have e1 := ((AccountView.requireNonDup_ok_iff _ _ _ _).mp h1).2
98 subst e1
99 obtain ⟨vault, s2, h2, k2⟩ := bind_ok_elim k1
100 have e2 := nextDistinct_state _ _ _ _ h2
101 subst e2
102 obtain ⟨slot, s3, h3, k3⟩ := bind_ok_elim k2
103 rw [AccountView.nextCursor_exact] at h3
104 cases h3
105 obtain ⟨dest, s4, h4, k4⟩ := bind_ok_elim k3
106 have e4 := ((AccountView.resolveDup_ok_iff _ _ _ _ _ _ _).mp h4).1
107 subst e4
108 obtain ⟨_, s5, h5, _⟩ := bind_ok_elim k4
109 exact ⟨vault, (checkVault_ok h5).1⟩
110
111/-- **`WithdrawToken` in the program's source**: the same checks. -/
112theorem withdrawToken_checks (a0 progId ix : UInt64) (s s' : SvmState)
113 (h : withdrawToken a0 progId ix s = .ok () s') : ∃ vault, VaultChecked s a0 vault progId := by
114 unfold withdrawToken at h
115 obtain ⟨_, s1, h1, k1⟩ := bind_ok_elim h
116 have e1 := ((AccountView.requireNonDup_ok_iff _ _ _ _).mp h1).2
117 subst e1
118 obtain ⟨vault, s2, h2, k2⟩ := bind_ok_elim k1
119 have e2 := nextDistinct_state _ _ _ _ h2
120 subst e2
121 obtain ⟨src, s3, h3, k3⟩ := bind_ok_elim k2
122 have e3 := nextDistinct_state _ _ _ _ h3
123 subst e3
124 obtain ⟨dst, s4, h4, k4⟩ := bind_ok_elim k3
125 have e4 := nextDistinct_state _ _ _ _ h4
126 subst e4
127 obtain ⟨mint, s5, h5, k5⟩ := bind_ok_elim k4
128 have e5 := nextDistinct_state _ _ _ _ h5
129 subst e5
130 obtain ⟨tp, s6, h6, k6⟩ := bind_ok_elim k5
131 have e6 := nextDistinct_state _ _ _ _ h6
132 subst e6
133 obtain ⟨_, s7, h7, _⟩ := bind_ok_elim k6
134 exact ⟨vault, (checkVault_ok h7).1⟩
135
136/-- **`CloseTokenAccount` in the program's source**: the same checks. -/
137theorem closeTokenAccount_checks (a0 progId ix : UInt64) (s s' : SvmState)
138 (h : closeTokenAccount a0 progId ix s = .ok () s') : ∃ vault, VaultChecked s a0 vault progId := by
139 unfold closeTokenAccount at h
140 obtain ⟨_, s1, h1, k1⟩ := bind_ok_elim h
141 have e1 := ((AccountView.requireNonDup_ok_iff _ _ _ _).mp h1).2
142 subst e1
143 obtain ⟨vault, s2, h2, k2⟩ := bind_ok_elim k1
144 have e2 := nextDistinct_state _ _ _ _ h2
145 subst e2
146 obtain ⟨acct, s3, h3, k3⟩ := bind_ok_elim k2
147 have e3 := nextDistinct_state _ _ _ _ h3
148 subst e3
149 obtain ⟨slot, s4, h4, k4⟩ := bind_ok_elim k3
150 rw [AccountView.nextCursor_exact] at h4
151 cases h4
152 obtain ⟨dest, s5, h5, k5⟩ := bind_ok_elim k4
153 have e5 := ((AccountView.resolveDup_ok_iff _ _ _ _ _ _ _).mp h5).1
154 subst e5
155 obtain ⟨tp, s6, h6, k6⟩ := bind_ok_elim k5
156 have e6 := nextDistinct_state _ _ _ _ h6
157 subst e6
158 obtain ⟨_, s7, h7, _⟩ := bind_ok_elim k6
159 exact ⟨vault, (checkVault_ok h7).1⟩
160
161/-- **`InitVault` in the program's source** succeeds only if the owner account signed. -/
162theorem initVault_requires_signer (a0 progId ix : UInt64) (s s' : SvmState)
163 (h : initVault a0 progId ix s = .ok () s') : s.readU8 (a0 + ABI.accIsSignerOffset) ≠ 0 := by
164 unfold initVault at h
165 obtain ⟨_, s1, h1, k1⟩ := bind_ok_elim h
166 have e1 := ((AccountView.requireNonDup_ok_iff _ _ _ _).mp h1).2
167 subst e1
168 obtain ⟨vault, s2, h2, k2⟩ := bind_ok_elim k1
169 have e2 := nextDistinct_state _ _ _ _ h2
170 subst e2
171 obtain ⟨sp, s3, h3, k3⟩ := bind_ok_elim k2
172 have e3 := nextDistinct_state _ _ _ _ h3
173 subst e3
174 obtain ⟨b, t, hb, k4⟩ := bind_ok_elim k3
175 simp only [AccountView.isSigner, bind, SolanaM.bind, rawLoadU8, pure, SolanaM.pure, SvmStep.ok.injEq] at hb
176 obtain ⟨rfl, rfl⟩ := hb
177 obtain ⟨_, t2, hr, _⟩ := bind_ok_elim k4
178 obtain ⟨hsig, _⟩ := (Verify.require_ok_iff _ _ _ _ _).mp hr
179 intro hz
180 simp [hz] at hsig
181
182end Twokey.Proofs.Code
183

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.