Repository navigation
Expand file tree
/
Copy pathDispatch.lean
More file actions
244 lines (221 loc) · 12.9 KB
/
Copy pathDispatch.lean
File metadata and controls
244 lines (221 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import FeProofs.PackedStore.Common
import FeProofs.PackedStore.RuntimeBlocks_001
import FeProofs.PackedStore.RuntimeBlocks_002
/-!
# FePackedStore dispatcher (generated by `scripts/proof_skeleton.py`)
Stele-side dispatch facts and the runtime dispatcher reach lemmas, composed from the block
summaries along the path the compiled dispatcher takes for each selector.
-/
open Stele ABI Ethereum Ethereum.EVM Reasoning.Theory Reasoning.Reach
namespace FeProofs.PackedStore
set_option maxRecDepth 2000000
/-! ## Stele dispatch: which transition a selector reaches -/
theorem fePackedStoreDispatch_get1 {I : ExecutionEnv}
(hsel : selIs I (fePackedStoreSelBytes 0)) :
dispatchMsg contract I.calldata = some get1Transition := by
refine dispatchMsg_eq_some_of_split (contract := contract)
(pre := []) (post := [get0Transition, setBothTransition, set1Transition, set0Transition])
(ti := get1Transition) (cd := I.calldata) (by rfl) ?_ ?_ ?_ (by rfl)
· exact transitions_eq
· intro u hu
simp at hu
· rw [get1SelectorOf]; exact hsel
theorem fePackedStoreDispatch_get0 {I : ExecutionEnv}
(hsel : selIs I (fePackedStoreSelBytes 1)) :
dispatchMsg contract I.calldata = some get0Transition := by
have h0 : ¬ selIs I (fePackedStoreSelBytes 0) := not_selIs_of_selIs hsel (by decide)
refine dispatchMsg_eq_some_of_split (contract := contract)
(pre := [get1Transition]) (post := [setBothTransition, set1Transition, set0Transition])
(ti := get0Transition) (cd := I.calldata) (by rfl) ?_ ?_ ?_ (by rfl)
· exact transitions_eq
· intro u hu
simp only [List.mem_cons, List.not_mem_nil, or_false] at hu
rcases hu with rfl
· rw [get1SelectorOf]; exact Bool.eq_false_of_not_eq_true h0
· rw [get0SelectorOf]; exact hsel
theorem fePackedStoreDispatch_setBoth {I : ExecutionEnv}
(hsel : selIs I (fePackedStoreSelBytes 2)) :
dispatchMsg contract I.calldata = some setBothTransition := by
have h0 : ¬ selIs I (fePackedStoreSelBytes 0) := not_selIs_of_selIs hsel (by decide)
have h1 : ¬ selIs I (fePackedStoreSelBytes 1) := not_selIs_of_selIs hsel (by decide)
refine dispatchMsg_eq_some_of_split (contract := contract)
(pre := [get1Transition, get0Transition]) (post := [set1Transition, set0Transition])
(ti := setBothTransition) (cd := I.calldata) (by rfl) ?_ ?_ ?_ (by rfl)
· exact transitions_eq
· intro u hu
simp only [List.mem_cons, List.not_mem_nil, or_false] at hu
rcases hu with rfl | rfl
· rw [get1SelectorOf]; exact Bool.eq_false_of_not_eq_true h0
· rw [get0SelectorOf]; exact Bool.eq_false_of_not_eq_true h1
· rw [setBothSelectorOf]; exact hsel
theorem fePackedStoreDispatch_set1 {I : ExecutionEnv}
(hsel : selIs I (fePackedStoreSelBytes 3)) :
dispatchMsg contract I.calldata = some set1Transition := by
have h0 : ¬ selIs I (fePackedStoreSelBytes 0) := not_selIs_of_selIs hsel (by decide)
have h1 : ¬ selIs I (fePackedStoreSelBytes 1) := not_selIs_of_selIs hsel (by decide)
have h2 : ¬ selIs I (fePackedStoreSelBytes 2) := not_selIs_of_selIs hsel (by decide)
refine dispatchMsg_eq_some_of_split (contract := contract)
(pre := [get1Transition, get0Transition, setBothTransition]) (post := [set0Transition])
(ti := set1Transition) (cd := I.calldata) (by rfl) ?_ ?_ ?_ (by rfl)
· exact transitions_eq
· intro u hu
simp only [List.mem_cons, List.not_mem_nil, or_false] at hu
rcases hu with rfl | rfl | rfl
· rw [get1SelectorOf]; exact Bool.eq_false_of_not_eq_true h0
· rw [get0SelectorOf]; exact Bool.eq_false_of_not_eq_true h1
· rw [setBothSelectorOf]; exact Bool.eq_false_of_not_eq_true h2
· rw [set1SelectorOf]; exact hsel
theorem fePackedStoreDispatch_set0 {I : ExecutionEnv}
(hsel : selIs I (fePackedStoreSelBytes 4)) :
dispatchMsg contract I.calldata = some set0Transition := by
have h0 : ¬ selIs I (fePackedStoreSelBytes 0) := not_selIs_of_selIs hsel (by decide)
have h1 : ¬ selIs I (fePackedStoreSelBytes 1) := not_selIs_of_selIs hsel (by decide)
have h2 : ¬ selIs I (fePackedStoreSelBytes 2) := not_selIs_of_selIs hsel (by decide)
have h3 : ¬ selIs I (fePackedStoreSelBytes 3) := not_selIs_of_selIs hsel (by decide)
refine dispatchMsg_eq_some_of_split (contract := contract)
(pre := [get1Transition, get0Transition, setBothTransition, set1Transition]) (post := [])
(ti := set0Transition) (cd := I.calldata) (by rfl) ?_ ?_ ?_ (by rfl)
· exact transitions_eq
· intro u hu
simp only [List.mem_cons, List.not_mem_nil, or_false] at hu
rcases hu with rfl | rfl | rfl | rfl
· rw [get1SelectorOf]; exact Bool.eq_false_of_not_eq_true h0
· rw [get0SelectorOf]; exact Bool.eq_false_of_not_eq_true h1
· rw [setBothSelectorOf]; exact Bool.eq_false_of_not_eq_true h2
· rw [set1SelectorOf]; exact Bool.eq_false_of_not_eq_true h3
· rw [set0SelectorOf]; exact hsel
theorem fePackedStoreDispatch_none_short {cd : ByteArray} (h : cd.size < 4) :
dispatchMsg contract cd = none := by
rw [dispatchMsg_eq_dispatchList contract cd, transitions_eq]
refine dispatchList_none_short _ ?_ h
intro t ht
simp only [List.mem_cons, List.not_mem_nil, or_false] at ht
rcases ht with rfl | rfl | rfl | rfl | rfl
· rw [get1SelectorOf]; rfl
· rw [get0SelectorOf]; rfl
· rw [setBothSelectorOf]; rfl
· rw [set1SelectorOf]; rfl
· rw [set0SelectorOf]; rfl
theorem fePackedStoreDispatch_none_nomatch {I : ExecutionEnv}
(hnm : ∀ i, i < 5 → (fePackedStoreSelBytes i == I.calldata.extract 0 4) = false) :
dispatchMsg contract I.calldata = none := by
refine dispatchMsg_none_of_all_ne (contract := contract) (cd := I.calldata) (by rfl) (by rfl) ?_
intro t ht
rw [transitions_eq] at ht
simp only [List.mem_cons, List.not_mem_nil, or_false] at ht
rcases ht with rfl | rfl | rfl | rfl | rfl
· rw [get1SelectorOf]; exact hnm 0 (by omega)
· rw [get0SelectorOf]; exact hnm 1 (by omega)
· rw [setBothSelectorOf]; exact hnm 2 (by omega)
· rw [set1SelectorOf]; exact hnm 3 (by omega)
· rw [set0SelectorOf]; exact hnm 4 (by omega)
/-! ## Runtime dispatcher paths
The dispatcher is a binary search: selectors below `setBoth`'s (`get1`, `get0`) are compared at
pc 234, the others from pc 23. -/
/-- Dispatcher path to `get1()` (arm entry pc 260). -/
theorem fePackedStoreReachGet1Body {σ σ₀ A I} {g : Sat256}
(hcode : I.code = fePackedStoreBytecode) (hsz : 4 ≤ I.calldata.size) (hsize : I.calldata.size < UInt256.size)
(hsel : selIs I (fePackedStoreSelBytes 0)) :
∃ k C, RD (fePackedStoreBytecode) I g (initState σ σ₀ g A I) (UInt256.ofNat 260)
[(UInt256.shiftRight (uInt256OfByteArray (I.calldata.readBytes (⟨0⟩ : UInt256).toNat 32)) (UInt256.ofNat 224))]
ByteArray.empty
(UInt256.ofNat 0) ByteArray.empty σ k C := by
have hword : solcSelectorWord I = ⟨88873589⟩ :=
solcSelectorWord_eq_of_beq I hsz 0x05 0x4c 0x1a 0x75 ⟨88873589⟩ (by native_decide)
(by simpa [fePackedStoreSelBytes] using hsel)
have rd0 := RD.initState (g := g) (σ := σ) (σ₀ := σ₀) (A := A) hcode
rd_run rd0 to 260 using [lt_four_eq_zero_of_ge hsz hsize, selWord_eq hword]
exact ⟨_, _, rd0⟩
/-- Dispatcher path to `get0()` (arm entry pc 289). -/
theorem fePackedStoreReachGet0Body {σ σ₀ A I} {g : Sat256}
(hcode : I.code = fePackedStoreBytecode) (hsz : 4 ≤ I.calldata.size) (hsize : I.calldata.size < UInt256.size)
(hsel : selIs I (fePackedStoreSelBytes 1)) :
∃ k C, RD (fePackedStoreBytecode) I g (initState σ σ₀ g A I) (UInt256.ofNat 289)
[(UInt256.shiftRight (uInt256OfByteArray (I.calldata.readBytes (⟨0⟩ : UInt256).toNat 32)) (UInt256.ofNat 224))]
ByteArray.empty
(UInt256.ofNat 0) ByteArray.empty σ k C := by
have hword : solcSelectorWord I = ⟨2428205892⟩ :=
solcSelectorWord_eq_of_beq I hsz 0x90 0xbb 0x7b 0x44 ⟨2428205892⟩ (by native_decide)
(by simpa [fePackedStoreSelBytes] using hsel)
have rd0 := RD.initState (g := g) (σ := σ) (σ₀ := σ₀) (A := A) hcode
rd_run rd0 to 289 using [lt_four_eq_zero_of_ge hsz hsize, selWord_eq hword]
exact ⟨_, _, rd0⟩
/-- Dispatcher path to `setBoth(uint256,uint256)` (arm entry pc 56). -/
theorem fePackedStoreReachSetBothBody {σ σ₀ A I} {g : Sat256}
(hcode : I.code = fePackedStoreBytecode) (hsz : 4 ≤ I.calldata.size) (hsize : I.calldata.size < UInt256.size)
(hsel : selIs I (fePackedStoreSelBytes 2)) :
∃ k C, RD (fePackedStoreBytecode) I g (initState σ σ₀ g A I) (UInt256.ofNat 56)
[(UInt256.shiftRight (uInt256OfByteArray (I.calldata.readBytes (⟨0⟩ : UInt256).toNat 32)) (UInt256.ofNat 224))]
ByteArray.empty
(UInt256.ofNat 0) ByteArray.empty σ k C := by
have hword : solcSelectorWord I = ⟨2463047413⟩ :=
solcSelectorWord_eq_of_beq I hsz 0x92 0xcf 0x1e 0xf5 ⟨2463047413⟩ (by native_decide)
(by simpa [fePackedStoreSelBytes] using hsel)
have rd0 := RD.initState (g := g) (σ := σ) (σ₀ := σ₀) (A := A) hcode
rd_run rd0 to 56 using [lt_four_eq_zero_of_ge hsz hsize, selWord_eq hword]
exact ⟨_, _, rd0⟩
/-- Dispatcher path to `set1(uint256)` (arm entry pc 138). -/
theorem fePackedStoreReachSet1Body {σ σ₀ A I} {g : Sat256}
(hcode : I.code = fePackedStoreBytecode) (hsz : 4 ≤ I.calldata.size) (hsize : I.calldata.size < UInt256.size)
(hsel : selIs I (fePackedStoreSelBytes 3)) :
∃ k C, RD (fePackedStoreBytecode) I g (initState σ σ₀ g A I) (UInt256.ofNat 138)
[(UInt256.shiftRight (uInt256OfByteArray (I.calldata.readBytes (⟨0⟩ : UInt256).toNat 32)) (UInt256.ofNat 224))]
ByteArray.empty
(UInt256.ofNat 0) ByteArray.empty σ k C := by
have hword : solcSelectorWord I = ⟨2613288252⟩ :=
solcSelectorWord_eq_of_beq I hsz 0x9b 0xc3 0x9d 0x3c ⟨2613288252⟩ (by native_decide)
(by simpa [fePackedStoreSelBytes] using hsel)
have rd0 := RD.initState (g := g) (σ := σ) (σ₀ := σ₀) (A := A) hcode
rd_run rd0 to 138 using [lt_four_eq_zero_of_ge hsz hsize, selWord_eq hword]
exact ⟨_, _, rd0⟩
/-- Dispatcher path to `set0(uint256)` (arm entry pc 188). -/
theorem fePackedStoreReachSet0Body {σ σ₀ A I} {g : Sat256}
(hcode : I.code = fePackedStoreBytecode) (hsz : 4 ≤ I.calldata.size) (hsize : I.calldata.size < UInt256.size)
(hsel : selIs I (fePackedStoreSelBytes 4)) :
∃ k C, RD (fePackedStoreBytecode) I g (initState σ σ₀ g A I) (UInt256.ofNat 188)
[(UInt256.shiftRight (uInt256OfByteArray (I.calldata.readBytes (⟨0⟩ : UInt256).toNat 32)) (UInt256.ofNat 224))]
ByteArray.empty
(UInt256.ofNat 0) ByteArray.empty σ k C := by
have hword : solcSelectorWord I = ⟨4067421897⟩ :=
solcSelectorWord_eq_of_beq I hsz 0xf2 0x6f 0xee 0xc9 ⟨4067421897⟩ (by native_decide)
(by simpa [fePackedStoreSelBytes] using hsel)
have rd0 := RD.initState (g := g) (σ := σ) (σ₀ := σ₀) (A := A) hcode
rd_run rd0 to 188 using [lt_four_eq_zero_of_ge hsz hsize, selWord_eq hword]
exact ⟨_, _, rd0⟩
/-- Calldata shorter than a selector -/
theorem fePackedStoreX_short {σ σ₀ A I} {g : Sat256}
(hcode : I.code = fePackedStoreBytecode)
(hshort : I.calldata.size < 4) :
RDrev (fePackedStoreBytecode) g (initState σ σ₀ g A I) := by
have rd0 := RD.initState (g := g) (σ := σ) (σ₀ := σ₀) (A := A) hcode
rd_run rd0 using [lt_four_ne_zero_of_lt hshort]
exact rd0
/-- No selector matches: either side of the pivot at pc 13, every compare falls through. -/
theorem fePackedStoreX_noMatch {σ σ₀ A I} {g : Sat256}
(hcode : I.code = fePackedStoreBytecode)
(hsz : 4 ≤ I.calldata.size) (hsize : I.calldata.size < UInt256.size)
(hnm : ∀ i, i < 5 → (fePackedStoreSelBytes i == I.calldata.extract 0 4) = false) :
RDrev (fePackedStoreBytecode) g (initState σ σ₀ g A I) := by
have miss : ∀ (i : ℕ) (sel : UInt256), i < 5 →
UInt256.eq sel (UInt256.shiftRight (uInt256OfByteArray (I.calldata.readBytes 0 32)) ⟨224⟩)
= (if (fePackedStoreSelBytes i == I.calldata.extract 0 4) then ⟨1⟩ else ⟨0⟩) →
UInt256.eq (UInt256.shiftRight (uInt256OfByteArray (I.calldata.readBytes (⟨0⟩ : UInt256).toNat 32))
(UInt256.ofNat 224)) sel = UInt256.ofNat 0 := by
intro i sel hi e
simp only [hnm i hi, Bool.false_eq_true, ite_false] at e
rw [uInt256_eq_comm]
exact e
have e0 := miss 0 _ (by omega) (get1EvmSelector hsz)
have e1 := miss 1 _ (by omega) (get0EvmSelector hsz)
have e2 := miss 2 _ (by omega) (setBothEvmSelector hsz)
have e3 := miss 3 _ (by omega) (set1EvmSelector hsz)
have e4 := miss 4 _ (by omega) (set0EvmSelector hsz)
have rd0 := RD.initState (g := g) (σ := σ) (σ₀ := σ₀) (A := A) hcode
by_cases hpiv : UInt256.lt (UInt256.shiftRight (uInt256OfByteArray
(I.calldata.readBytes (⟨0⟩ : UInt256).toNat 32)) (UInt256.ofNat 224))
(UInt256.ofNat 2463047413) = UInt256.ofNat 0
· rd_run rd0 using [lt_four_eq_zero_of_ge hsz hsize, hpiv, e2, e3, e4]
exact rd0
· rd_run rd0 using [lt_four_eq_zero_of_ge hsz hsize, hpiv, e0, e1]
exact rd0
end FeProofs.PackedStore