Repository navigation
Expand file tree
/
Copy pathCalls.lean
More file actions
506 lines (459 loc) · 25.3 KB
/
Copy pathCalls.lean
File metadata and controls
506 lines (459 loc) · 25.3 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
import Ethereum.Theory.StaticStorage
import Fe.EVM.World
/-!
# External calls of a Fe model
The model counts no gas and keeps no substate, so it cannot compute what the EVM passes to a
callee. The world carries a feed of those inputs, one `Supply` for each time the model consults
its context. With them a message call is EVMLean's own `Θ` and a creation its own `Lambda`, on
the model's account map: whatever the callee does, to this contract's storage as well, is in
their result.
What stays loose: a callee gets some gas no more than the call's gas argument, plus the stipend
when value moves; `gas()` reads some word; the substate and the original account map handed to
a callee are whatever the feed holds.
Each call and creation made is recorded in the world's ghost field `calls`, as the inputs passed
to `Θ` (`messageCall`) or `Lambda`, so a statement about the model can speak of the calls it makes
(`call_made`). One not made records nothing, as `callOk` and `createOk` ask nothing of it. No
other operation touches the record (`KeepsCalls`, `keeps_calls` in `World.lean`).
-/
namespace Fe.EVM
open Ethereum Ethereum.EVM Ethereum.EVM.Invariant
/-! ## The feed -/
/-- Consult the context: take the next entry of the feed. A run that asks for more than the
feed holds does not terminate, so it agrees with no result of the bytecode. -/
def next : FeM Supply := do
let w ← get
match w.feed with
| [] => Fe.M.diverge
| s :: rest => set { w with feed := rest }; return s
/-- `gas()`: some word, the next entry's. The model has no gas to count. -/
def gas : FeM UInt256 := do return (← next).gas
/-! ## Return data -/
/-- `returndatasize()`: the size of the last call's or creation's return data. -/
def returndatasize : FeM UInt256 := do return UInt256.ofNat (← get).returnData.size
/-- `returndatacopy(dest, offset, len)`: copy `len` bytes of the return data from `offset` into
memory at `dest`. A read past the end of the return data halts, as on the EVM. -/
def returndatacopy (dest offset len : UInt256) : FeM Unit := do
let w ← get
if offset.toNat + len.toNat > w.returnData.size then Fe.M.fail .oob
else set { w with memory := w.returnData.write offset.toNat w.memory dest.toNat len.toNat }
/-! ## Message calls -/
/-- The gas a message call forwards: at most `cap`, at most the call's gas argument, plus the
stipend when value moves. The EVM's `cap` is all but one 64th of what is left; the model's
is the feed's. -/
def forwardedGas (cap gasArg value : UInt256) : UInt256 :=
.ofNat (min cap.toNat gasArg.toNat + if value = ⟨0⟩ then 0 else GasConstants.Gcallstipend)
/-- The log entries a callee appended to the substate it was given. The callee's substate
extends the one it was given: `Ethereum.EVM.Substates.theta_logs_extract` for `Θ`,
`lambda_logs_extract` for `Lambda` (`Fe/EVM/LogsAppend.lean`). -/
def appended (before after : Substate) : LogSeries :=
after.logSeries.extract before.logSeries.size
/-- The inputs of the message call `message` makes, from the world before it and the feed's
entry `s`. -/
def messageCall (I : Env) (w : World) (s : Supply) (gasArg : UInt256)
(source recipient t : AccountAddress) (value value' argsPtr argsLen : UInt256) (perm : Bool) :
ThetaCall where
σ := w.accounts
σ₀ := s.orig
A := s.substate
s := source
o := I.sender
r := recipient
c := toExecute I.header.fork w.accounts t
g := forwardedGas s.gas gasArg value
p := .ofNat I.gasPrice
v := value
v' := value'
d := w.memory.readWithPadding argsPtr.toNat argsLen.toNat
e := I.depth + 1
H := I.header
bvh := I.blobVersionedHashes
blocks := I.blocks
w := perm
/-- The message call the EVM makes for `CALL`, `STATICCALL` and `DELEGATECALL`: `source` calls
`recipient`, running the code at `t`, moving `value` and showing the callee `value'`. It
takes one entry of the feed, whether or not a call is made: without the funds, or at depth
1024, none is, and the result is zero with empty return data. Returns one if the callee
succeeded and zero otherwise; the first `retLen` bytes of the return data go to memory at
`retPtr`. A call made is recorded. -/
def message (gasArg : UInt256) (source recipient t : AccountAddress) (value value' : UInt256)
(argsPtr argsLen retPtr retLen : UInt256) (perm : Bool) : FeM UInt256 := do
let I ← read
let s ← next
let w ← get
let T := messageCall I w s gasArg source recipient t value value' argsPtr argsLen perm
let made := value ≤ (w.accounts.get? I.codeOwner |>.option ⟨0⟩ (·.balance)) ∧ I.depth < 1024
let (σ', _, A', z, o) := if made then T.run else (w.accounts, ⟨0⟩, s.substate, false, .empty)
set { w with
accounts := σ'
logs := w.logs ++ appended s.substate A'
memory := o.write 0 w.memory retPtr.toNat (min retLen (.ofNat o.size)).toNat
returnData := o
calls := if made then w.calls ++ [Made.theta T] else w.calls }
return if z then ⟨1⟩ else ⟨0⟩
/-- `call(gas, addr, value, args_offset, args_len, ret_offset, ret_len)`: call the account
`addr` with `value` and the `args_len` bytes of memory at `args_offset`. Under a static
call it fails if it moves value. -/
def call (gas addr value argsPtr argsLen retPtr retLen : UInt256) : FeM UInt256 := do
let I ← read
if I.perm = false ∧ value ≠ ⟨0⟩ then Fe.M.fail .static
else
let t := AccountAddress.ofUInt256 addr
message gas I.codeOwner t t value value argsPtr argsLen retPtr retLen I.perm
/-- `staticcall(gas, addr, args_offset, args_len, ret_offset, ret_len)`: a call with no value in
which the callee may change nothing. -/
def staticcall (gas addr argsPtr argsLen retPtr retLen : UInt256) : FeM UInt256 := do
let I ← read
let t := AccountAddress.ofUInt256 addr
message gas I.codeOwner t t ⟨0⟩ ⟨0⟩ argsPtr argsLen retPtr retLen false
/-- `staticcall_precompile(gas, addr, args_offset, args_len, ret_offset, ret_len)`: a
`STATICCALL`. Only Fe's borrow checker tells it from `staticcall`. -/
def staticcallPrecompile (gas addr argsPtr argsLen retPtr retLen : UInt256) : FeM UInt256 :=
staticcall gas addr argsPtr argsLen retPtr retLen
/-- `delegatecall(gas, addr, args_offset, args_len, ret_offset, ret_len)`: the code at `addr`
runs as this contract, for this call's caller and value. -/
def delegatecall (gas addr argsPtr argsLen retPtr retLen : UInt256) : FeM UInt256 := do
let I ← read
message gas I.source I.codeOwner (AccountAddress.ofUInt256 addr) ⟨0⟩ I.weiValue argsPtr argsLen
retPtr retLen I.perm
/-! ## Creations -/
/-- `CREATE` and `CREATE2`: run the `len` bytes of memory at `ptr` as init code, with the gas of
the feed's next entry. Returns the new contract's address, or zero when the creation fails
or is not made: for want of funds, at depth 1024, or with this contract's nonce at its
limit. Under a static call it fails. Init code above 49152 bytes runs out of gas on the
EVM, so the model does not terminate. A creation made is recorded. -/
def createWith (salt : Option ByteArray) (value ptr len : UInt256) : FeM UInt256 := do
let I ← read
if I.perm = false then Fe.M.fail .static
else if len > ⟨49152⟩ then Fe.M.diverge
else
let s ← next
let w ← get
let σ := w.accounts
let init := w.memory.readWithPadding ptr.toNat len.toNat
let self : Account := σ.get? I.codeOwner |>.getD default
if h : self.nonce.toNat < 2 ^ 64 - 1
∧ value ≤ (σ.get? I.codeOwner |>.option ⟨0⟩ (·.balance)) ∧ I.depth < 1024
∧ init.size ≤ 49152 then
let L : LambdaCall :=
{ σ := σ.insert I.codeOwner { self with nonce := self.nonce + ⟨1⟩ }, σ₀ := s.orig
A := s.substate, s := I.codeOwner, o := I.sender, g := s.gas, p := .ofNat I.gasPrice
v := value, i := init, e := ⟨I.depth.val + 1, Nat.succ_lt_succ h.2.2.1⟩, ζ := salt
H := I.header, bvh := I.blobVersionedHashes, blocks := I.blocks, w := I.perm }
match L.run with
| (a, σ', _, A', z, o) =>
set { w with
accounts := σ'
logs := w.logs ++ appended s.substate A'
returnData := if z then ByteArray.empty else o
calls := w.calls ++ [Made.lambda L] }
return if z then UInt256.ofNat a else ⟨0⟩
else
set { w with returnData := ByteArray.empty }
return ⟨0⟩
/-- `create(value, offset, len)`: create a contract from the init code in memory. -/
def create (value ptr len : UInt256) : FeM UInt256 := createWith none value ptr len
/-- `create2(value, offset, len, salt)`: as `create`, at the address that is the last 20 bytes
of `keccak256(0xff ++ self ++ salt ++ keccak256(init))`. -/
def create2 (value ptr len salt : UInt256) : FeM UInt256 :=
createWith (some salt.toByteArray) value ptr len
/-- A creation that is made is `Lambda` on the model's accounts with this contract's nonce
bumped, from the feed's next entry, and is recorded. -/
theorem createWith_made {I : Env} {w : World} {s : Supply} {rest : List Supply}
{salt : Option ByteArray} {value ptr len g' : UInt256} {L : LambdaCall} {a : AccountAddress}
{σ' : AccountMap} {A' : Substate} {z : Bool} {o : ByteArray} (hfeed : w.feed = s :: rest)
(hperm : I.perm = true) (hlen : ¬ len > ⟨49152⟩)
(hmade : (w.accounts.get? I.codeOwner |>.getD default).nonce.toNat < 2 ^ 64 - 1
∧ value ≤ (w.accounts.get? I.codeOwner |>.option ⟨0⟩ (·.balance)) ∧ I.depth < 1024
∧ (w.memory.readWithPadding ptr.toNat len.toNat).size ≤ 49152)
(hL : L =
{ σ := w.accounts.insert I.codeOwner
{ (w.accounts.get? I.codeOwner |>.getD default) with
nonce := (w.accounts.get? I.codeOwner |>.getD default).nonce + ⟨1⟩ }
σ₀ := s.orig, A := s.substate, s := I.codeOwner, o := I.sender, g := s.gas
p := .ofNat I.gasPrice, v := value, i := w.memory.readWithPadding ptr.toNat len.toNat
e := ⟨I.depth.val + 1, Nat.succ_lt_succ hmade.2.2.1⟩, ζ := salt, H := I.header
bvh := I.blobVersionedHashes, blocks := I.blocks, w := I.perm })
(hΛ : (a, σ', g', A', z, o) = L.run) :
(createWith salt value ptr len).exec I w =
some (.ok (if z then .ofNat a else ⟨0⟩,
{ w with
accounts := σ'
logs := w.logs ++ appended s.substate A'
returnData := if z then ByteArray.empty else o
feed := rest
calls := w.calls ++ [Made.lambda L] })) := by
have hp : ¬ I.perm = false := by simp [hperm]
subst hL
simp only [createWith, next, hfeed, M.exec_bind, M.exec_read, M.exec_get, M.exec_set,
M.exec_pure, ite_eq_right hp, ite_eq_right hlen, dite_eq_left hmade]
rw [← hΛ]
/-- A creation that is not made, for want of funds, at depth 1024, with this contract's nonce at
its limit or with init code too long, returns zero and changes nothing but the return data,
which it empties, and the feed: no account, no log and no record. -/
theorem createWith_notMade {I : Env} {w : World} {s : Supply} {rest : List Supply}
{salt : Option ByteArray} {value ptr len : UInt256} (hfeed : w.feed = s :: rest)
(hperm : I.perm = true) (hlen : ¬ len > ⟨49152⟩)
(hnot : ¬ ((w.accounts.get? I.codeOwner |>.getD default).nonce.toNat < 2 ^ 64 - 1
∧ value ≤ (w.accounts.get? I.codeOwner |>.option ⟨0⟩ (·.balance)) ∧ I.depth < 1024
∧ (w.memory.readWithPadding ptr.toNat len.toNat).size ≤ 49152)) :
(createWith salt value ptr len).exec I w =
some (.ok (⟨0⟩, { w with returnData := ByteArray.empty, feed := rest })) := by
have hp : ¬ I.perm = false := by simp [hperm]
simp only [createWith, next, hfeed, M.exec_bind, M.exec_read, M.exec_get, M.exec_set,
M.exec_pure, ite_eq_right hp, ite_eq_right hlen, dite_eq_right hnot]
/-! ## The code of other accounts -/
/-- `extcodehash(addr)`: the hash of an account's code, zero for an account that is dead. -/
def extcodehash (addr : UInt256) : FeM UInt256 := do
let σ := (← get).accounts
let a := AccountAddress.ofUInt256 addr
return if State.dead σ a then ⟨0⟩ else σ.get? a |>.option ⟨0⟩ Account.codeHash
/-- `extcodecopy(addr, dest, offset, len)`: copy `len` bytes of an account's code from `offset`
into memory at `dest`, zeros past its end. -/
def extcodecopy (addr dest offset len : UInt256) : FeM Unit := do
let w ← get
let code := w.accounts.get? (AccountAddress.ofUInt256 addr) |>.option ByteArray.empty (·.code)
set { w with memory := code.write offset.toNat w.memory dest.toNat len.toNat }
/-! ## Self-destruction -/
/-- `selfdestruct(addr)`: send the balance to `addr` and end the call. Whether a balance sent
to oneself is burnt depends on whether this transaction created the contract, which the
feed's substate records.
Exported code cannot call it, so the name is protected: Fe's std never self-destructs, and
EquiVM has no trace lemma for `SELFDESTRUCT` to certify a model that does. The definition
records what the instruction would mean here. -/
protected def selfdestruct (addr : UInt256) : FeM Empty := do
let I ← read
if I.perm = false then Fe.M.fail .static
else
let s ← next
let w ← get
let σ := w.accounts
let r := AccountAddress.ofUInt256 addr
let σ' :=
match σ.get? I.codeOwner with
| none => σ
| some self =>
match σ.get? r with
| none =>
if self.balance == ⟨0⟩ then σ
else (σ.insert r { (default : Account) with balance := self.balance }).insert I.codeOwner
{ self with balance := ⟨0⟩ }
| some dst =>
if r ≠ I.codeOwner then
(σ.insert r { dst with balance := dst.balance + self.balance }).insert I.codeOwner
{ self with balance := ⟨0⟩ }
else if s.substate.createdAccounts.contains I.codeOwner then
(σ.insert r { dst with balance := ⟨0⟩ }).insert I.codeOwner { self with balance := ⟨0⟩ }
else σ
set { w with accounts := σ' }
Fe.M.stop ByteArray.empty
/-! ## What the definitions give -/
/-- EVMLean's gas cap of a call is at most the call's gas argument. -/
theorem Cgascap_le (t r : AccountAddress) (val g : UInt256) (σ : AccountMap) (μ : MachineState)
(A : Substate) : Cgascap t r val g σ μ A ≤ g.toNat := by
unfold Cgascap
split
· exact Nat.min_le_right _ _
· exact Nat.le_refl _
/-- The gas EVMLean's `CALL` forwards, `Ccallgas`, is `forwardedGas` of some cap: the model's
calls range over every amount the EVM can forward. -/
theorem forwardedGas_Ccallgas (t r : AccountAddress) (value gasArg : UInt256) (σ : AccountMap)
(μ : MachineState) (A : Substate) :
∃ cap, forwardedGas cap gasArg value = .ofNat (Ccallgas t r value gasArg σ μ A) := by
refine ⟨.ofNat (Cgascap t r value gasArg σ μ A), ?_⟩
have hle := Cgascap_le t r value gasArg σ μ A
have hto : (UInt256.ofNat (Cgascap t r value gasArg σ μ A)).toNat =
Cgascap t r value gasArg σ μ A := by
show (Fin.ofNat _ _).val = _
simp only [Fin.ofNat]
exact Nat.mod_eq_of_lt (Nat.lt_of_le_of_lt hle gasArg.val.isLt)
unfold forwardedGas Ccallgas
rw [hto, Nat.min_eq_left hle]
by_cases hv : value = ⟨0⟩
· subst hv
rfl
· rw [ite_eq_right hv]
split
· exact absurd rfl hv
· rfl
/-- A callee that leaves the substate as it was appended no log. -/
theorem appended_self (A : Substate) : appended A A = #[] := by
simp [appended]
/-- A message call that is made is `Θ` on the model's accounts, and is recorded. The hypothesis
is the link EquiVM's `RD.call`, `RD.callValueMadeOr` and `RD.solcStaticcall` conclude with,
once the gas they give the callee is shown to be `forwardedGas` of some cap, as
`callValueMade_forwardedGas` (`Reach.lean`) does for a `CALL`. -/
theorem message_made {I : Env} {w : World} {s : Supply} {rest : List Supply}
{gasArg value value' argsPtr argsLen retPtr retLen g' : UInt256}
{source recipient t : AccountAddress} {perm z : Bool} {σ' : AccountMap} {A' : Substate}
{o : ByteArray} (hfeed : w.feed = s :: rest)
(hmade : value ≤ (w.accounts.get? I.codeOwner |>.option ⟨0⟩ (·.balance)) ∧ I.depth < 1024)
(hΘ : (σ', g', A', z, o) =
Θ w.accounts s.orig s.substate source I.sender recipient (toExecute I.header.fork w.accounts t)
(forwardedGas s.gas gasArg value) (.ofNat I.gasPrice) value value'
(w.memory.readWithPadding argsPtr.toNat argsLen.toNat) (I.depth + 1) I.header
I.blobVersionedHashes I.blocks perm) :
(message gasArg source recipient t value value' argsPtr argsLen retPtr retLen perm).exec I w =
some (.ok (if z then ⟨1⟩ else ⟨0⟩,
{ w with
accounts := σ'
logs := w.logs ++ appended s.substate A'
memory := o.write 0 w.memory retPtr.toNat (min retLen (.ofNat o.size)).toNat
returnData := o
feed := rest
calls := w.calls ++ [Made.theta (messageCall I w s gasArg source recipient t value value'
argsPtr argsLen perm)] })) := by
simp only [message, next, hfeed, M.exec_bind, M.exec_read, M.exec_get, M.exec_set, M.exec_pure,
ite_eq_left hmade]
rw [show (messageCall I { w with feed := rest } s gasArg source recipient t value value' argsPtr
argsLen perm).run = (σ', g', A', z, o) from hΘ.symm]
rfl
/-- A message call that is not made, for want of funds or at depth 1024, fails and changes
nothing but the return data and the feed: no log is appended, the write to memory has length
zero, and nothing is recorded. -/
theorem message_notMade {I : Env} {w : World} {s : Supply} {rest : List Supply}
{gasArg value value' argsPtr argsLen retPtr retLen : UInt256}
{source recipient t : AccountAddress} {perm : Bool} (hfeed : w.feed = s :: rest)
(hnot : ¬ (value ≤ (w.accounts.get? I.codeOwner |>.option ⟨0⟩ (·.balance)) ∧ I.depth < 1024)) :
(message gasArg source recipient t value value' argsPtr argsLen retPtr retLen perm).exec I w =
some (.ok (⟨0⟩,
{ w with
logs := w.logs ++ appended s.substate s.substate
memory := ByteArray.empty.write 0 w.memory retPtr.toNat
(min retLen (.ofNat ByteArray.empty.size)).toNat
returnData := ByteArray.empty
feed := rest })) := by
simp only [message, next, hfeed, M.exec_bind, M.exec_read, M.exec_get, M.exec_set, M.exec_pure,
ite_eq_right hnot]
rfl
/-- A `call` that is made runs `Θ` on the inputs `messageCall` gives, from the world before the
call and the feed's next entry, and records them. -/
theorem call_made {I : Env} {w : World} {s : Supply} {rest : List Supply}
{gasArg addr value argsPtr argsLen retPtr retLen g' : UInt256} {z : Bool} {σ' : AccountMap}
{A' : Substate} {o : ByteArray} (hfeed : w.feed = s :: rest)
(hperm : I.perm = true ∨ value = ⟨0⟩)
(hmade : value ≤ (w.accounts.get? I.codeOwner |>.option ⟨0⟩ (·.balance)) ∧ I.depth < 1024)
(hΘ : (σ', g', A', z, o) = (messageCall I w s gasArg I.codeOwner (.ofUInt256 addr)
(.ofUInt256 addr) value value argsPtr argsLen I.perm).run) :
(call gasArg addr value argsPtr argsLen retPtr retLen).exec I w =
some (.ok (if z then ⟨1⟩ else ⟨0⟩,
{ w with
accounts := σ'
logs := w.logs ++ appended s.substate A'
memory := o.write 0 w.memory retPtr.toNat (min retLen (.ofNat o.size)).toNat
returnData := o
feed := rest
calls := w.calls ++ [Made.theta (messageCall I w s gasArg I.codeOwner (.ofUInt256 addr)
(.ofUInt256 addr) value value argsPtr argsLen I.perm)] })) := by
have hns : ¬ (I.perm = false ∧ value ≠ ⟨0⟩) := by
rintro ⟨hp, hv⟩
rcases hperm with h | h
· rw [hp] at h; cases h
· exact hv h
simp only [call, M.exec_bind, M.exec_read, ite_eq_right hns]
exact message_made hfeed hmade hΘ
/-- A `call` that is not made records nothing. -/
theorem call_notMade {I : Env} {w : World} {s : Supply} {rest : List Supply}
{gasArg addr value argsPtr argsLen retPtr retLen : UInt256} (hfeed : w.feed = s :: rest)
(hperm : I.perm = true ∨ value = ⟨0⟩)
(hnot : ¬ (value ≤ (w.accounts.get? I.codeOwner |>.option ⟨0⟩ (·.balance)) ∧ I.depth < 1024)) :
(call gasArg addr value argsPtr argsLen retPtr retLen).exec I w =
some (.ok (⟨0⟩,
{ w with
logs := w.logs ++ appended s.substate s.substate
memory := ByteArray.empty.write 0 w.memory retPtr.toNat
(min retLen (.ofNat ByteArray.empty.size)).toNat
returnData := ByteArray.empty
feed := rest })) := by
have hns : ¬ (I.perm = false ∧ value ≠ ⟨0⟩) := by
rintro ⟨hp, hv⟩
rcases hperm with h | h
· rw [hp] at h; cases h
· exact hv h
simp only [call, M.exec_bind, M.exec_read, ite_eq_right hns]
exact message_notMade hfeed hnot
/-- What a `call` that returns did: it was not made, returned zero and changed neither the
accounts nor the record, or it was made, its accounts are what `Θ` returned, and it is
recorded. -/
theorem call_cases {I : Env} {w w' : World}
{gasArg addr value argsPtr argsLen retPtr retLen x : UInt256}
(h : (call gasArg addr value argsPtr argsLen retPtr retLen).exec I w = some (.ok (x, w'))) :
(x = ⟨0⟩ ∧ w'.accounts = w.accounts ∧ w'.calls = w.calls) ∨
∃ s rest, w.feed = s :: rest
∧ (value ≤ (w.accounts.get? I.codeOwner |>.option ⟨0⟩ (·.balance)) ∧ I.depth < 1024)
∧ w'.accounts = (messageCall I w s gasArg I.codeOwner (.ofUInt256 addr) (.ofUInt256 addr)
value value argsPtr argsLen I.perm).run.1
∧ w'.calls = w.calls ++ [Made.theta (messageCall I w s gasArg I.codeOwner
(.ofUInt256 addr) (.ofUInt256 addr) value value argsPtr argsLen I.perm)] := by
by_cases hs : I.perm = false ∧ value ≠ ⟨0⟩
· simp [call, hs] at h
have hperm : I.perm = true ∨ value = ⟨0⟩ := by
cases hp : I.perm
· exact Or.inr (Classical.byContradiction fun hv => hs ⟨hp, hv⟩)
· exact Or.inl rfl
cases hf : w.feed with
| nil => simp [call, message, next, hs, hf] at h
| cons s rest =>
by_cases hmade : value ≤ (w.accounts.get? I.codeOwner |>.option ⟨0⟩ (·.balance))
∧ I.depth < 1024
· rcases hr : (messageCall I w s gasArg I.codeOwner (.ofUInt256 addr) (.ofUInt256 addr) value
value argsPtr argsLen I.perm).run with ⟨σ', g', A', z, o⟩
rw [call_made hf hperm hmade hr.symm] at h
simp only [Option.some.injEq, Except.ok.injEq, Prod.mk.injEq] at h
rw [← h.2]
exact Or.inr ⟨s, rest, rfl, hmade, by rw [hr], rfl⟩
· rw [call_notMade hf hperm hmade] at h
simp only [Option.some.injEq, Except.ok.injEq, Prod.mk.injEq] at h
rw [← h.2]
exact Or.inl ⟨h.1.symm, rfl, rfl⟩
/-- A message call never stops the run from inside: the callee's halt is its own. -/
theorem noStop_message (gasArg : UInt256) (source recipient t : AccountAddress)
(value value' argsPtr argsLen retPtr retLen : UInt256) (perm : Bool) :
(message gasArg source recipient t value value' argsPtr argsLen retPtr retLen perm).NoStop := by
intro I w data w' h
cases hf : w.feed with
| nil => simp [message, next, hf] at h
| cons s rest =>
simp only [message, next, hf, M.exec_bind, M.exec_read, M.exec_get, M.exec_set,
M.exec_pure] at h
split at h <;> simp at h
theorem noStop_call (gasArg addr value argsPtr argsLen retPtr retLen : UInt256) :
(call gasArg addr value argsPtr argsLen retPtr retLen).NoStop := by
unfold call
exact M.NoStop.bind M.NoStop.read fun _ =>
M.NoStop.ite _ M.NoStop.fail_static (noStop_message _ _ _ _ _ _ _ _ _ _ _)
/-- A message call without write permission leaves every account's storage as it was, whatever
the callee and the feed: EVMLean's `Theta_static_accountStorageStateEq`, on the model. -/
theorem message_static_keeps_storage {I : Env} {w w' : World}
{gasArg value value' argsPtr argsLen retPtr retLen x : UInt256}
{source recipient t : AccountAddress}
(h : (message gasArg source recipient t value value' argsPtr argsLen retPtr retLen false).exec
I w = some (.ok (x, w'))) :
accountStorageStateEq w.accounts w'.accounts := by
cases hf : w.feed with
| nil => simp [message, next, hf] at h
| cons s rest =>
by_cases hmade :
value ≤ (w.accounts.get? I.codeOwner |>.option ⟨0⟩ (·.balance)) ∧ I.depth < 1024
· rcases hr : Θ w.accounts s.orig s.substate source I.sender recipient (toExecute I.header.fork w.accounts t)
(forwardedGas s.gas gasArg value) (.ofNat I.gasPrice) value value'
(w.memory.readWithPadding argsPtr.toNat argsLen.toNat) (I.depth + 1) I.header
I.blobVersionedHashes I.blocks false with ⟨σ', g', A', z, o⟩
rw [message_made hf hmade hr.symm] at h
injection h with h
injection h with h
rw [← (Prod.mk.inj h).2]
exact Theta_static_accountStorageStateEq hr
· rw [message_notMade hf hmade] at h
injection h with h
injection h with h
rw [← (Prod.mk.inj h).2]
exact accountStorageStateEq_refl _
/-- A `staticcall` that returns leaves every account's storage as it was, whatever the callee
and the feed. -/
theorem staticcall_keeps_storage {I : Env} {w w' : World}
{gasArg addr argsPtr argsLen retPtr retLen x : UInt256}
(h : (staticcall gasArg addr argsPtr argsLen retPtr retLen).exec I w = some (.ok (x, w'))) :
accountStorageStateEq w.accounts w'.accounts := by
simp only [staticcall, M.exec_bind, M.exec_read] at h
exact message_static_keeps_storage h
end Fe.EVM