Repository navigation
Expand file tree
/
Copy pathModelCorrect.lean
More file actions
234 lines (199 loc) · 11.9 KB
/
Copy pathModelCorrect.lean
File metadata and controls
234 lines (199 loc) · 11.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
import FeProofs.ScalarStore.Correct
import FeProofs.ScalarStore.Model
import Fe.EVM.Reach
/-!
# FeScalarStore: the bytecode refines the model
The EVM half restates the traces of `Get.lean`, `Set.lean` and `Constructor.lean` as terminals
that mention the bytecode only. The model half is evaluation of `Model.runtime` and `Model.init`.
-/
open Stele ABI Ethereum Ethereum.EVM Reasoning.Theory Reasoning.Reach Fe Fe.EVM
namespace FeProofs.ScalarStore
set_option maxRecDepth 2000000
/-! ## What the bytecode does -/
theorem feScalarStoreGetRet {σ σ₀ A I} {g : Sat256}
(hcode : I.code = feScalarStoreBytecode) (hsize : I.calldata.size < UInt256.size)
(hsel : selIs I (feScalarStoreSelBytes 1)) (hwv : I.weiValue = ⟨0⟩) :
RDret feScalarStoreBytecode g (initState σ σ₀ g A I) σ
(UInt256.toByteArray (solcSlotWordAt ⟨0⟩ σ I)) := by
have hsz : 4 ≤ I.calldata.size := calldata_size_ge_of_selIs I (feScalarStoreSelBytes 1) rfl hsel
obtain ⟨k, C, rd⟩ := feScalarStoreReachGetBody (g := g) (σ := σ) (σ₀ := σ₀) (A := A)
hcode hsz hsize hsel
rd_run rd using [hwv]
u256_lits at rd
rw [toByteArray_write32_read_back ByteArray.empty _ 0 (by decide)] at rd
exact rd
theorem feScalarStoreGetRev {σ σ₀ A I} {g : Sat256}
(hcode : I.code = feScalarStoreBytecode) (hsize : I.calldata.size < UInt256.size)
(hsel : selIs I (feScalarStoreSelBytes 1)) (hwv : I.weiValue ≠ ⟨0⟩) :
RDrev feScalarStoreBytecode g (initState σ σ₀ g A I) := by
have hsz : 4 ≤ I.calldata.size := calldata_size_ge_of_selIs I (feScalarStoreSelBytes 1) rfl hsel
obtain ⟨k, C, rd⟩ := feScalarStoreReachGetBody (g := g) (σ := σ) (σ₀ := σ₀) (A := A)
hcode hsz hsize hsel
rd_run rd using [hwv, isZero_eq_zero_of_ne hwv]
exact rd
theorem feScalarStoreSetRevValue {σ σ₀ A I} {g : Sat256}
(hcode : I.code = feScalarStoreBytecode) (hsize : I.calldata.size < UInt256.size)
(hsel : selIs I (feScalarStoreSelBytes 0)) (hwv : I.weiValue ≠ ⟨0⟩) :
RDrev feScalarStoreBytecode g (initState σ σ₀ g A I) := by
have hsz : 4 ≤ I.calldata.size := calldata_size_ge_of_selIs I (feScalarStoreSelBytes 0) rfl hsel
obtain ⟨k, C, rd⟩ := feScalarStoreReachSetBody (g := g) (σ := σ) (σ₀ := σ₀) (A := A)
hcode hsz hsize hsel
rd_run rd using [hwv, isZero_eq_zero_of_ne hwv]
exact rd
theorem feScalarStoreSetRevHead {σ σ₀ A I} {g : Sat256}
(hcode : I.code = feScalarStoreBytecode) (hsize : I.calldata.size < UInt256.size)
(hsel : selIs I (feScalarStoreSelBytes 0)) (hwv : I.weiValue = ⟨0⟩)
(hbad : I.calldata.size < 36 ∨ 2 ^ 255 + 4 ≤ I.calldata.size) :
RDrev feScalarStoreBytecode g (initState σ σ₀ g A I) := by
have hsz : 4 ≤ I.calldata.size := calldata_size_ge_of_selIs I (feScalarStoreSelBytes 0) rfl hsel
obtain ⟨k, C, rd⟩ := feScalarStoreReachSetBody (g := g) (σ := σ) (σ₀ := σ₀) (A := A)
hcode hsz hsize hsel
rd_run rd using [hwv, solcDecodeLenCheckBad 1 hsz hsize hbad]
exact rd
theorem feScalarStoreSetRet {σ σ₀ A I} {g : Sat256}
(hcode : I.code = feScalarStoreBytecode) (hsize : I.calldata.size < UInt256.size)
(hsel : selIs I (feScalarStoreSelBytes 0)) (hwv : I.weiValue = ⟨0⟩)
(hlen : 36 ≤ I.calldata.size) (hhi : I.calldata.size < 2 ^ 255 + 4) (hperm : I.perm = true) :
RDret feScalarStoreBytecode g (initState σ σ₀ g A I)
(sstoreAccountMap I.codeOwner σ ⟨0⟩ (calldataWord I.calldata 4)) ByteArray.empty := by
have hsz : 4 ≤ I.calldata.size := calldata_size_ge_of_selIs I (feScalarStoreSelBytes 0) rfl hsel
obtain ⟨k, C, rd⟩ := feScalarStoreReachSetBody (g := g) (σ := σ) (σ₀ := σ₀) (A := A)
hcode hsz hsize hsel
rd_run rd to 59 using [hwv, solcDecodeLenCheckOk_4_32 hlen hhi hsize]
rd_step rd
have hout : ByteArray.empty.readWithPadding (⟨0⟩ : UInt256).toNat (⟨0⟩ : UInt256).toNat
= ByteArray.empty := byteArray_readWithPadding_zero _ _
rw [hout] at rd
exact rd
theorem feScalarStoreSetStatic {σ σ₀ A I} {g : Sat256}
(hcode : I.code = feScalarStoreBytecode) (hsize : I.calldata.size < UInt256.size)
(hsel : selIs I (feScalarStoreSelBytes 0)) (hwv : I.weiValue = ⟨0⟩)
(hlen : 36 ≤ I.calldata.size) (hhi : I.calldata.size < 2 ^ 255 + 4) (hperm : I.perm = false) :
RDstatic feScalarStoreBytecode g (initState σ σ₀ g A I) := by
have hsz : 4 ≤ I.calldata.size := calldata_size_ge_of_selIs I (feScalarStoreSelBytes 0) rfl hsel
obtain ⟨k, C, rd⟩ := feScalarStoreReachSetBody (g := g) (σ := σ) (σ₀ := σ₀) (A := A)
hcode hsz hsize hsel
rd_run rd to 59 using [hwv, solcDecodeLenCheckOk_4_32 hlen hhi hsize]
have hperm' : ¬ I.perm = true := by simp [hperm]
rd_step rd
exact rd
/-! ## What the model does -/
section Model
variable {I : ExecutionEnv} {w : World}
theorem sel0 (h : selIs I (feScalarStoreSelBytes 0)) :
(Model.setSelector == I.calldata.extract 0 4) = true := h
theorem sel0_not (h : ¬ selIs I (feScalarStoreSelBytes 0)) :
(Model.setSelector == I.calldata.extract 0 4) = false := by
simpa [selIs, Model.setSelector, feScalarStoreSelBytes] using h
theorem sel1 (h : selIs I (feScalarStoreSelBytes 1)) :
(Model.getSelector == I.calldata.extract 0 4) = true := h
theorem sel1_not (h : ¬ selIs I (feScalarStoreSelBytes 1)) :
(Model.getSelector == I.calldata.extract 0 4) = false := by
simpa [selIs, Model.getSelector, feScalarStoreSelBytes] using h
theorem runtime_short (h : I.calldata.size < 4) :
Model.runtime.exec I w = some (.error (.revert ByteArray.empty)) := by
simp [Model.runtime, calldata, h]
theorem runtime_noMatch (hsz : 4 ≤ I.calldata.size)
(h0 : ¬ selIs I (feScalarStoreSelBytes 0)) (h1 : ¬ selIs I (feScalarStoreSelBytes 1)) :
Model.runtime.exec I w = some (.error (.revert ByteArray.empty)) := by
simp [Model.runtime, calldata, Nat.not_lt.mpr hsz, ne_of_beq_false (sel0_not h0), ne_of_beq_false (sel1_not h1)]
theorem runtime_set_value (hsz : 4 ≤ I.calldata.size)
(hsel : selIs I (feScalarStoreSelBytes 0)) (hwv : I.weiValue ≠ ⟨0⟩) :
Model.runtime.exec I w = some (.error (.revert ByteArray.empty)) := by
simp [Model.runtime, calldata, nonPayable, Nat.not_lt.mpr hsz, eq_of_beq (sel0 hsel), hwv]
theorem runtime_set_head (hsz : 4 ≤ I.calldata.size)
(hsel : selIs I (feScalarStoreSelBytes 0)) (hwv : I.weiValue = ⟨0⟩)
(hbad : BadHead I.calldata 1) :
Model.runtime.exec I w = some (.error (.revert ByteArray.empty)) := by
simp [Model.runtime, calldata, nonPayable, Nat.not_lt.mpr hsz, eq_of_beq (sel0 hsel), hwv, hbad]
theorem runtime_set (hsz : 4 ≤ I.calldata.size)
(hsel : selIs I (feScalarStoreSelBytes 0)) (hwv : I.weiValue = ⟨0⟩)
(hok : ¬ BadHead I.calldata 1) (hperm : I.perm = true) :
Model.runtime.exec I w = some (.ok (ByteArray.empty,
{ w with accounts := storeWord I.codeOwner w.accounts ⟨0⟩ (calldataWord I.calldata 4) })) := by
simp [Model.runtime, Model.set, Model.storedSlot, calldata, nonPayable, sstore,
Nat.not_lt.mpr hsz, eq_of_beq (sel0 hsel), hwv, hok, hperm]
theorem runtime_set_static (hsz : 4 ≤ I.calldata.size)
(hsel : selIs I (feScalarStoreSelBytes 0)) (hwv : I.weiValue = ⟨0⟩)
(hok : ¬ BadHead I.calldata 1) (hperm : I.perm = false) :
Model.runtime.exec I w = some (.error .static) := by
simp [Model.runtime, Model.set, calldata, nonPayable, sstore, Nat.not_lt.mpr hsz, eq_of_beq (sel0 hsel),
hwv, hok, hperm]
theorem runtime_get_value (hsz : 4 ≤ I.calldata.size)
(h0 : ¬ selIs I (feScalarStoreSelBytes 0)) (hsel : selIs I (feScalarStoreSelBytes 1))
(hwv : I.weiValue ≠ ⟨0⟩) :
Model.runtime.exec I w = some (.error (.revert ByteArray.empty)) := by
simp [Model.runtime, calldata, nonPayable, Nat.not_lt.mpr hsz, ne_of_beq_false (sel0_not h0), eq_of_beq (sel1 hsel), hwv]
theorem runtime_get (hsz : 4 ≤ I.calldata.size)
(h0 : ¬ selIs I (feScalarStoreSelBytes 0)) (hsel : selIs I (feScalarStoreSelBytes 1))
(hwv : I.weiValue = ⟨0⟩) :
Model.runtime.exec I w
= some (.ok (UInt256.toByteArray (solcSlotWordAt ⟨0⟩ w.accounts I), w)) := by
simp [Model.runtime, Model.get, Model.storedSlot, calldata, nonPayable, slotWord,
Nat.not_lt.mpr hsz, ne_of_beq_false (sel0_not h0), eq_of_beq (sel1 hsel), hwv, solcSlotWordAt, solcSlotWord]
end Model
/-! ## Refinement -/
theorem feScalarStoreRefinesModel : Refines .P0 feScalarStoreBytecode Model.contract := by
refine RefinesOn.of_code rfl ?_
intro σ σ₀ g A I hcode _ hsize
refine RefinesFor.closed ?_
by_cases hsz : 4 ≤ I.calldata.size
swap
· exact agrees_of_rev (g := Sat256.ofUInt256 g) hcode (feScalarStoreX_short hcode (by omega))
(runtime_short (by omega)) (by simp)
by_cases h0 : selIs I (feScalarStoreSelBytes 0)
· by_cases hwv : I.weiValue = ⟨0⟩
swap
· exact agrees_of_rev (g := Sat256.ofUInt256 g) hcode
(feScalarStoreSetRevValue hcode hsize h0 hwv) (runtime_set_value hsz h0 hwv) (by simp)
by_cases hbad : I.calldata.size < 36 ∨ 2 ^ 255 + 4 ≤ I.calldata.size
· exact agrees_of_rev (g := Sat256.ofUInt256 g) hcode
(feScalarStoreSetRevHead hcode hsize h0 hwv hbad) (runtime_set_head hsz h0 hwv (by simpa [BadHead] using hbad))
(by simp)
have hlen : 36 ≤ I.calldata.size := by omega
have hhi : I.calldata.size < 2 ^ 255 + 4 := by omega
by_cases hperm : I.perm = true
· exact agrees_of_ret (g := Sat256.ofUInt256 g) hcode
(feScalarStoreSetRet hcode hsize h0 hwv hlen hhi hperm)
(runtime_set hsz h0 hwv (by simpa [BadHead] using hbad) hperm) rfl
· have hpf : I.perm = false := by simpa using hperm
exact agrees_of_static (g := Sat256.ofUInt256 g) hcode
(feScalarStoreSetStatic hcode hsize h0 hwv hlen hhi hpf)
(runtime_set_static hsz h0 hwv (by simpa [BadHead] using hbad) hpf)
· by_cases h1 : selIs I (feScalarStoreSelBytes 1)
· by_cases hwv : I.weiValue = ⟨0⟩
· exact agrees_of_ret (g := Sat256.ofUInt256 g) hcode
(feScalarStoreGetRet hcode hsize h1 hwv) (runtime_get hsz h0 h1 hwv) rfl
· exact agrees_of_rev (g := Sat256.ofUInt256 g) hcode
(feScalarStoreGetRev hcode hsize h1 hwv) (runtime_get_value hsz h0 h1 hwv) (by simp)
· exact agrees_of_rev (g := Sat256.ofUInt256 g) hcode
(feScalarStoreX_noMatch hcode hsz hsize (feScalarStoreNoSelectorMatches h0 h1))
(runtime_noMatch hsz h0 h1) (by simp)
/-! ## Deployment -/
theorem init_value {I : ExecutionEnv} {w : World} {args : ByteArray} (hwv : I.weiValue ≠ ⟨0⟩) :
(Model.init args >>= fun _ => pure feScalarStoreBytecode).exec I w
= some (.error (.revert ByteArray.empty)) := by
simp [Model.init, nonPayable, hwv]
theorem init_ok {I : ExecutionEnv} {w : World} (v : UInt256) (hwv : I.weiValue = ⟨0⟩)
(hperm : I.perm = true) :
(Model.init (ctorTail v) >>= fun _ => pure feScalarStoreBytecode).exec I w
= some (.ok (feScalarStoreBytecode,
{ w with accounts := storeWord I.codeOwner w.accounts ⟨0⟩ v })) := by
have hsz : (ctorTail v).size = 32 := word_toBytesBE_toByteArray_size v
have hword : uInt256OfByteArray ((ctorTail v).readBytes 0 32) = v := by
rw [readBytes_all32 _ hsz, ctorTail, word_toBytesBE_toByteArray_eq_toByteArray,
uInt256OfByteArray_toByteArray]
simp [Model.init, Model.initBody, Model.storedSlot, nonPayable, sstore, hwv,
hperm, hsz, hword]
/-- Deployment with one ABI-encoded `uint256` argument. -/
theorem feScalarStoreDeployRefinesModel :
RefinesDeploy .P0 feScalarStoreCreationBytecode feScalarStoreBytecode Model.contract
(fun args => ∃ v : UInt256, args = ctorTail v) := by
refine RefinesDeploy.of_unit_closed (init := Model.init) (fun _ => rfl) ?_
rintro σ σ₀ g A I args ⟨v, rfl⟩ hcode _ hperm
by_cases hwv : I.weiValue = ⟨0⟩
· exact agrees_of_ret (g := Sat256.ofUInt256 g) hcode (feScalarStoreCtorRet v hcode hwv hperm)
(init_ok v hwv hperm) rfl
· exact agrees_of_rev (g := Sat256.ofUInt256 g) hcode (feScalarStoreCtorRev hcode hwv)
(init_value hwv) (by simp)
end FeProofs.ScalarStore