Repository navigation
Expand file tree
/
Copy pathEntry.lean
More file actions
582 lines (499 loc) · 27.1 KB
/
Copy pathEntry.lean
File metadata and controls
582 lines (499 loc) · 27.1 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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
import Fe.EVM.Refine
import Fe.EVM.Calls
import Reasoning.EVMWord
import Reasoning.Memory
import Reasoning.Solc
/-!
# Reasoning about an exported runtime entry
Facts that proofs about `fe-lean-export`'s runtime entry share, whatever the contract: word
arithmetic the std decoder and encoder perform, writes into memory the code allocates, the
selector word, how an entry that stops relates to a hand-written one that returns, and a call
without data, which leaves memory alone.
-/
namespace Fe.EVM
open Ethereum Ethereum.EVM Reasoning.Theory
/-! ## Words -/
theorem toNat0 : (UInt256.ofNat 0).toNat = 0 := rfl
theorem toNat4 : (UInt256.ofNat 4).toNat = 4 := rfl
theorem toNat32 : (UInt256.ofNat 32).toNat = 32 := rfl
theorem toNat64 : (UInt256.ofNat 64).toNat = 64 := rfl
theorem toNat96 : (UInt256.ofNat 96).toNat = 96 := rfl
theorem add0_32 : UInt256.ofNat 0 + UInt256.ofNat 32 = UInt256.ofNat 32 := rfl
theorem add32_32 : UInt256.ofNat 32 + UInt256.ofNat 32 = UInt256.ofNat 64 := rfl
theorem add64_32 : UInt256.ofNat 64 + UInt256.ofNat 32 = UInt256.ofNat 96 := rfl
theorem ne32 : UInt256.ofNat 32 ≠ UInt256.ofNat 0 := by decide
theorem ne64 : UInt256.ofNat 64 ≠ UInt256.ofNat 0 := by decide
theorem ne96 : UInt256.ofNat 96 ≠ UInt256.ofNat 0 := by decide
theorem at4 : (UInt256.ofNat 0 + UInt256.ofNat 4).toNat = 4 := rfl
theorem at36 : (UInt256.ofNat 0 + (UInt256.ofNat 4 + UInt256.ofNat 32)).toNat = 36 := rfl
theorem toNat4_32 : (UInt256.ofNat 4 + UInt256.ofNat 32).toNat = 36 := rfl
theorem at68 :
(UInt256.ofNat 0 + (UInt256.ofNat 4 + UInt256.ofNat 32 + UInt256.ofNat 32)).toNat = 68 := rfl
theorem toNatMax : (~~~UInt256.ofNat 0).toNat = UInt256.size - 1 := by decide
theorem toNatU64 : (UInt256.ofNat 18446744073709551615).toNat = 18446744073709551615 := rfl
theorem sub32_0 : (UInt256.ofNat 32 - UInt256.ofNat 0).toNat = 32 := rfl
theorem add_zero (a : UInt256) : a + UInt256.ofNat 0 = a := by
apply u256_inj
rw [uadd_toNat, toNat0, Nat.add_zero]
exact Nat.mod_eq_of_lt a.val.isLt
theorem ofNat_add_ofNat {a b : ℕ} (h : a + b < UInt256.size) :
UInt256.ofNat a + UInt256.ofNat b = UInt256.ofNat (a + b) := by
apply u256_inj
rw [uadd_toNat, ulit_toNat' a (by omega), ulit_toNat' b (by omega), Nat.mod_eq_of_lt h,
ulit_toNat' _ h]
theorem ofNat_mul_ofNat {a b : ℕ} (ha : a < UInt256.size) (hb : b < UInt256.size)
(h : a * b < UInt256.size) : UInt256.ofNat a * UInt256.ofNat b = UInt256.ofNat (a * b) := by
apply u256_inj
rw [umul_toNat _ _ (by rw [ulit_toNat' a ha, ulit_toNat' b hb]; exact h), ulit_toNat' a ha,
ulit_toNat' b hb, ulit_toNat' _ h]
theorem ofNat_ne_ofNat_zero {n : ℕ} (h0 : n ≠ 0) (h : n < UInt256.size) :
UInt256.ofNat n ≠ UInt256.ofNat 0 := fun he =>
h0 (by have := congrArg UInt256.toNat he; rwa [ulit_toNat' n h, toNat0] at this)
theorem ofNat_sub_ofNat {a b : ℕ} (ha : a < UInt256.size) (h : b ≤ a) :
UInt256.ofNat a - UInt256.ofNat b = UInt256.ofNat (a - b) := by
apply u256_inj
rw [show UInt256.ofNat a - UInt256.ofNat b = UInt256.sub _ _ from rfl,
usub_toNat (by rw [ulit_toNat' a ha, ulit_toNat' b (by omega)]; exact h),
ulit_toNat' a ha, ulit_toNat' b (by omega), ulit_toNat' _ (by omega)]
/-- A length below `2 ^ 64` survives std's cast to `u64`: minus the view's base 0, masked, then
cut to 64 bits. -/
theorem truncate_size (n : Nat) (h : n < 2 ^ 64) :
truncate 64 ((UInt256.ofNat n).sub (UInt256.ofNat 0) &&& UInt256.ofNat 18446744073709551615) =
UInt256.ofNat n := by
have hn : (UInt256.ofNat n).toNat = n := ulit_toNat' n (by unfold UInt256.size; omega)
have hsub : (UInt256.ofNat n).sub (UInt256.ofNat 0) = UInt256.ofNat n := by
apply u256_inj
exact (usub_toNat (a := UInt256.ofNat n) (b := UInt256.ofNat 0) (Nat.zero_le _)).trans
(Nat.sub_zero _)
have hmask : (UInt256.ofNat 18446744073709551615).toNat = 2 ^ 64 - 1 := rfl
have hand : (UInt256.ofNat n &&& UInt256.ofNat 18446744073709551615).toNat = n := by
change (UInt256.land _ _).toNat = n
rw [uland_toNat, hn, hmask]
change Nat.land n (2 ^ 64 - 1) = n
rw [nat_land_mask_eq_mod, Nat.mod_eq_of_lt h]
rw [hsub]
apply u256_inj
unfold truncate
rw [hand, Nat.mod_eq_of_lt h]
/-- Signed comparison of two words below `2 ^ 255` is comparison of their values. -/
theorem sltBool_small {a b : UInt256} (ha : a.toNat < 2 ^ 255) (hb : b.toNat < 2 ^ 255) :
a.sltBool b = decide (a.toNat < b.toNat) := by
unfold UInt256.sltBool
rw [ite_eq_right (by omega), ite_eq_right (by omega)]
rfl
/-- Std's head check on calldata of `size` bytes, at least a selector and below `2 ^ 64`:
`(size - 4) <s need`, signed, is `size - 4 < need`. -/
theorem headCheck {size need : Nat} (hlo : 4 ≤ size) (hfits : size < 2 ^ 64)
(hneed : need < 2 ^ 64) :
(UInt256.ofNat size - UInt256.ofNat 4).sltBool (UInt256.ofNat need) =
decide (size - 4 < need) := by
have hn : (UInt256.ofNat size).toNat = size := ulit_toNat' _ (by unfold UInt256.size; omega)
have hm : (UInt256.ofNat need).toNat = need := ulit_toNat' _ (by unfold UInt256.size; omega)
have hsub : (UInt256.ofNat size - UInt256.ofNat 4).toNat = size - 4 := by
have h := usub_toNat (a := UInt256.ofNat size) (b := UInt256.ofNat 4) (by rw [hn, toNat4]; omega)
rw [hn, toNat4] at h
exact h
rw [sltBool_small (by rw [hsub]; omega) (by rw [hm]; omega), hsub, hm]
/-! ## Memory after a return -/
/-- Memory after `return_value` encodes the word `v` into a fresh 32-byte buffer. -/
def returned (memory : ByteArray) (v : UInt256) : ByteArray :=
v.toByteArray.write 0
((⟨Array.replicate 32 0⟩ : ByteArray).write 0 (memory ++ ⟨Array.replicate 32 0⟩) memory.size 32)
memory.size 32
/-- The word written into the fresh buffer reads back. -/
theorem returned_read (memory : ByteArray) (v : UInt256) :
(v.toByteArray.write 0
((⟨Array.replicate 32 0⟩ : ByteArray).write 0 (memory ++ ⟨Array.replicate 32 0⟩)
memory.size 32)
memory.size 32).readWithPadding memory.size 32 = v.toByteArray := by
have hzeros : (⟨Array.replicate 32 0⟩ : ByteArray).size = 32 := rfl
apply toByteArray_write32_read_back
rw [byteArray_write_size_of_inBounds _ _ _ _ (by rw [hzeros])
(by rw [ByteArray.size_append, hzeros]), ByteArray.size_append, hzeros]
omega
theorem returned_size (memory : ByteArray) (v : UInt256) :
(returned memory v).size = memory.size + 32 := by
have hzeros : (⟨Array.replicate 32 0⟩ : ByteArray).size = 32 := rfl
unfold returned
rw [byteArray_write_size_of_inBounds _ _ _ _ (by rw [toByteArray_size])
(by rw [byteArray_write_size_of_inBounds _ _ _ _ (by rw [hzeros])
(by rw [ByteArray.size_append, hzeros]), ByteArray.size_append, hzeros]),
byteArray_write_size_of_inBounds _ _ _ _ (by rw [hzeros])
(by rw [ByteArray.size_append, hzeros]), ByteArray.size_append, hzeros]
/-! ## Memory past a known prefix
Exported code allocates fresh memory and writes into it, so its memory is a prefix `M` it does not
touch followed by a tail `T` whose bytes a proof tracks. -/
/-- A write of `n` bytes of `src` from `s` into the tail, at `k` bytes into it. -/
theorem write_tail (src M T : ByteArray) {d : ℕ} (s k n : ℕ) (hd : d = M.size + k) (hn : n ≠ 0)
(hsrc : s + n ≤ src.size) (hk : k + n ≤ T.size) :
src.write s (M ++ T) d n =
M ++ (T.extract 0 k ++ src.extract s (s + n) ++ T.extract (k + n) T.size) := by
subst hd
rw [write_eq_gen_from src (M ++ T) s (M.size + k) n hn hsrc
(by rw [ByteArray.size_append]; omega),
extract_append_span _ _ _ _ (Nat.zero_le _) (by omega), byteArray_extract_self,
extract_append_right_window _ _ _ _ (by omega), ByteArray.size_append,
show M.size + k + n - M.size = k + n by omega, show M.size + T.size - M.size = T.size by omega,
show M.size + k - M.size = k by omega, ByteArray.append_assoc, ByteArray.append_assoc,
ByteArray.append_assoc]
/-- A read of `n` bytes of the tail, at `k` bytes into it. -/
theorem readWithPadding_tail (M T : ByteArray) {a : ℕ} (k n : ℕ) (ha : a = M.size + k)
(hn : 0 < n) (hk : k + n ≤ T.size) :
(M ++ T).readWithPadding a n = T.extract k (k + n) := by
subst ha
rw [readWithPadding_eq_extract_unbounded _ _ _ hn (by rw [ByteArray.size_append]; omega),
extract_append_right_window _ _ _ _ (by omega), show M.size + k - M.size = k by omega,
show M.size + k + n - M.size = k + n by omega]
/-- The size of `n` fresh zero bytes. -/
theorem zeros_size (n : ℕ) : (⟨Array.replicate n 0⟩ : ByteArray).size = n := by
simp [ByteArray.size]
theorem zeros_extract (n a b : ℕ) :
(⟨Array.replicate n 0⟩ : ByteArray).extract a b = ⟨Array.replicate (min b n - a) 0⟩ := by
apply ByteArray.ext
simp [ByteArray.data_extract]
/-! ## A hashed pair of words -/
/-- Memory after two words are stored in a fresh 64-byte buffer: what std's `mapping_slot`
leaves, the key and the root of the map it hashes. -/
def keyed (memory : ByteArray) (key root : UInt256) : ByteArray :=
root.toByteArray.write 0
(key.toByteArray.write 0 (memory ++ ⟨Array.replicate 64 0⟩) memory.size 32)
(memory.size + 32) 32
private theorem keyed_first_size (memory : ByteArray) (key : UInt256) :
(key.toByteArray.write 0 (memory ++ ⟨Array.replicate 64 0⟩) memory.size 32).size =
memory.size + 64 := by
have hzeros : (⟨Array.replicate 64 0⟩ : ByteArray).size = 64 := rfl
rw [byteArray_write_size_of_inBounds _ _ _ _ (by rw [toByteArray_size])
(by rw [ByteArray.size_append, hzeros]; omega), ByteArray.size_append, hzeros]
theorem keyed_size (memory : ByteArray) (key root : UInt256) :
(keyed memory key root).size = memory.size + 64 := by
unfold keyed
rw [byteArray_write_size_of_inBounds _ _ _ _ (by rw [toByteArray_size])
(by rw [keyed_first_size]), keyed_first_size]
/-- The buffer reads back as the two words. -/
theorem keyed_read (memory : ByteArray) (key root : UInt256) :
(keyed memory key root).readWithPadding memory.size 64 =
key.toByteArray ++ root.toByteArray := by
rw [show (64 : Nat) = 32 + 32 from rfl,
byteArray_readWithPadding_split_unbounded _ _ 32 32 (by decide) (by decide)
(by rw [keyed_size])]
unfold keyed
rw [write32_read_below _ _ (memory.size + 32) memory.size (by rw [toByteArray_size])
(by rw [keyed_first_size]; omega) (by omega),
toByteArray_write32_read_back _ _ _ (by rw [ByteArray.size_append]; omega),
toByteArray_write32_read_back _ _ _ (by rw [keyed_first_size]; omega)]
/-- The hash of the buffer is the slot of the map's entry: Solidity's mapping layout. -/
theorem keyed_slot (memory : ByteArray) (key root : UInt256) :
UInt256.ofNat (fromByteArrayBigEndian
(KEC ((keyed memory key root).readWithPadding memory.size 64))) = mapSlot key root := by
rw [keyed_read]
exact Reasoning.Theory.mappingSlot_single key root
/-- Memory after each pair of `pairs` is hashed in turn, each in its own fresh buffer: what the
lookups of an arm leave. -/
def keyedAll (memory : ByteArray) : List (UInt256 × UInt256) → ByteArray
| [] => memory
| (key, root) :: rest => keyedAll (keyed memory key root) rest
@[simp] theorem keyedAll_nil (memory : ByteArray) : keyedAll memory [] = memory := rfl
@[simp] theorem keyedAll_cons (memory : ByteArray) (key root : UInt256)
(rest : List (UInt256 × UInt256)) :
keyedAll memory ((key, root) :: rest) = keyedAll (keyed memory key root) rest := rfl
theorem keyedAll_size (memory : ByteArray) (pairs : List (UInt256 × UInt256)) :
(keyedAll memory pairs).size = memory.size + 64 * pairs.length := by
induction pairs generalizing memory with
| nil => rfl
| cons pair rest ih =>
obtain ⟨key, root⟩ := pair
rw [keyedAll_cons, ih, keyed_size, List.length_cons]
omega
/-- An empty return ends the call with no data and changes nothing. -/
theorem returnNone_exec (I : ExecutionEnv) (w : World) :
(returnData (UInt256.ofNat 0) (UInt256.ofNat 0)).exec I w =
some (.error (.stop ByteArray.empty w)) := by
simp [returnData, toNat0, byteArray_readWithPadding_zero]
/-! ## The selector -/
/-- The selector word: the first four bytes of calldata as a number. -/
abbrev selWord (cd : ByteArray) : UInt256 :=
UInt256.shiftRight (uInt256OfByteArray (cd.readBytes 0 32)) ⟨224⟩
theorem selector_exec (I : ExecutionEnv) (w : World) :
calldataSelector.exec I w = some (.ok (selWord I.calldata, w)) := rfl
theorem selWord_hit {cd sel : ByteArray} {word : UInt256}
(h : UInt256.eq word (selWord cd) = if (sel == cd.extract 0 4) then ⟨1⟩ else ⟨0⟩)
(hsel : (sel == cd.extract 0 4) = true) : selWord cd = word := by
rw [hsel, ite_eq_left rfl] at h
by_contra hne
rw [u256_eq_of_ne (fun e => hne e.symm)] at h
exact absurd h (by decide)
theorem selWord_miss {cd sel : ByteArray} {word : UInt256}
(h : UInt256.eq word (selWord cd) = if (sel == cd.extract 0 4) then ⟨1⟩ else ⟨0⟩)
(hsel : (sel == cd.extract 0 4) = false) : selWord cd ≠ word := by
intro e
rw [hsel, e, u256_eq_refl] at h
exact absurd h (by decide)
/-! ## Entries -/
/-- An entry whose code exits on reserved memory exits so. -/
theorem entry_exec {m : FeM Empty} {I : ExecutionEnv} {w : World} {f : Exit World}
(h : m.exec I w.reserved = some (.error f)) :
(entry m).exec I w = some (.error f) := by
simp [entry, h]
/-- With calldata below `2 ^ 64` bytes, std's head check is the one a hand-written entry makes. -/
theorem badHead_iff (cd : ByteArray) (words : Nat) (hfits : cd.size < 2 ^ 64) :
BadHead cd words ↔ cd.size < 4 + 32 * words := by
unfold BadHead
constructor
· rintro (h | h)
· exact h
· omega
· exact Or.inl
/-! ## Runs alike up to memory -/
/-- Two runs from the world `w` end alike up to memory: both diverge, both exit the same way,
or both return the same result with the same accounts, logs and recorded calls, the first
having grown memory by at most `grow` bytes. An exported arm that allocates relates this way
to a hand-written one that does not. -/
inductive Alike {α : Type} (grow : Nat) (w : World) :
Option (Except (Exit World) (α × World)) → Option (Except (Exit World) (α × World)) → Prop
| ok {a : α} {v v' : World} (ha : v.accounts = v'.accounts) (hl : v.logs = v'.logs)
(hc : v.calls = v'.calls) (hm : v.memory.size ≤ w.memory.size + grow) :
Alike grow w (some (.ok (a, v))) (some (.ok (a, v')))
| exit (f : Exit World) : Alike grow w (some (.error f)) (some (.error f))
| diverge : Alike grow w none none
/-- An outcome with the memory of the world it returns replaced by `memory`. -/
def withMemory {α : Type} (memory : ByteArray) :
Option (Except (Exit World) (α × World)) → Option (Except (Exit World) (α × World))
| some (.ok (a, v)) => some (.ok (a, { v with memory := memory }))
| outcome => outcome
@[simp] theorem withMemory_ok {α : Type} (memory : ByteArray) (a : α) (v : World) :
withMemory memory (some (.ok (a, v))) = some (.ok (a, { v with memory := memory })) := rfl
@[simp] theorem withMemory_error {α : Type} (memory : ByteArray) (f : Exit World) :
withMemory (α := α) memory (some (.error f)) = some (.error f) := rfl
@[simp] theorem withMemory_none {α : Type} (memory : ByteArray) :
withMemory (α := α) memory none = none := rfl
/-- A run that is another's with its memory replaced ends alike up to memory. -/
theorem alike_withMemory {α : Type} {grow : Nat} {w : World} {memory : ByteArray}
{x y : Option (Except (Exit World) (α × World))} (h : x = withMemory memory y)
(hm : memory.size ≤ w.memory.size + grow) : Alike grow w x y := by
subst h
match y with
| none => exact .diverge
| some (.error f) => exact .exit f
| some (.ok (a, v)) => exact .ok rfl rfl rfl hm
/-- A run equal to another ends alike with no growth, if a result leaves memory alone. -/
theorem alike_of_eq {α : Type} {w : World} {x y : Option (Except (Exit World) (α × World))}
(h : x = y) (hkeep : ∀ a v, y = some (.ok (a, v)) → v.memory = w.memory) :
Alike 0 w x y := by
subst h
match x, hkeep with
| none, _ => exact .diverge
| some (.error f), _ => exact .exit f
| some (.ok (a, v)), hkeep => exact .ok rfl rfl rfl (by rw [hkeep a v rfl]; omega)
/-- A call with no input and no output copied, as std's empty buffers make it, neither reads
nor changes memory: it is the call a hand-written model makes on empty memory, with memory
put back. -/
theorem call_noData (gasArg addr value argsPtr retPtr : UInt256) (I : ExecutionEnv) (w : World) :
(call gasArg addr value argsPtr (UInt256.ofNat 0) retPtr (UInt256.ofNat 0)).exec I w =
withMemory w.memory ((call gasArg addr value ⟨0⟩ ⟨0⟩ ⟨0⟩ ⟨0⟩).exec I
{ w with memory := ByteArray.empty }) := by
have h0 : (⟨0⟩ : UInt256).toNat = 0 := rfl
have h0' : (UInt256.ofNat 0).toNat = 0 := rfl
have hmin : ∀ o : ByteArray, (min (UInt256.ofNat 0) (UInt256.ofNat o.size)).toNat = 0 := by
intro o
have hle : UInt256.ofNat 0 ≤ UInt256.ofNat o.size := Nat.zero_le _
simp [min, hle, h0']
by_cases hs : I.perm = false ∧ value ≠ ⟨0⟩
· simp [call, hs]
cases hf : w.feed with
| nil => simp [call, message, next, hs, hf]
| cons s rest =>
have hT : messageCall I w s gasArg I.codeOwner (.ofUInt256 addr) (.ofUInt256 addr) value value
argsPtr (UInt256.ofNat 0) I.perm =
messageCall I { w with memory := ByteArray.empty } s gasArg I.codeOwner (.ofUInt256 addr)
(.ofUInt256 addr) value value ⟨0⟩ ⟨0⟩ I.perm := by
simp only [messageCall, h0, h0', byteArray_readWithPadding_zero]
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
have hmin' : ∀ o : ByteArray, (min (⟨0⟩ : UInt256) (UInt256.ofNat o.size)).toNat = 0 := hmin
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 (UInt256.ofNat 0) I.perm).run with ⟨σ', g', A', z, o⟩
rw [call_made hf hperm hmade hr.symm,
call_made (w := { w with memory := ByteArray.empty, feed := s :: rest }) rfl hperm hmade
(hT ▸ hr.symm), hT]
simp [hmin, hmin', byteArray_write_len_zero]
rfl
· rw [call_notMade hf hperm hmade,
call_notMade (w := { w with memory := ByteArray.empty, feed := s :: rest }) rfl hperm hmade]
have h00 : (UInt256.ofNat 0 ⊓ UInt256.ofNat 0).toNat = 0 := hmin ByteArray.empty
simp [h00, byteArray_write_len_zero]
/-! ## Code that ignores memory
The hand-written models have no memory, so the exported entries, which run on reserved memory,
end as they do on the world a call starts in. -/
/-- The computation neither reads nor writes memory: on other memory it ends as it does, with
that memory. -/
def IgnoresMemory {α : Type} (m : FeM α) : Prop :=
∀ I w memory, m.exec I { w with memory := memory } = withMemory memory (m.exec I w)
theorem IgnoresMemory.pure {α : Type} (a : α) : IgnoresMemory (pure a : FeM α) :=
fun _ _ _ => rfl
theorem IgnoresMemory.fail {α : Type} (f : Exit World) : IgnoresMemory (Fe.M.fail f : FeM α) :=
fun _ _ _ => rfl
theorem IgnoresMemory.revert {α : Type} (data : ByteArray) :
IgnoresMemory (Fe.M.revert data : FeM α) := fun _ _ _ => rfl
theorem IgnoresMemory.panic {α : Type} (code : Nat) : IgnoresMemory (Fe.M.panic code : FeM α) :=
fun _ _ _ => rfl
theorem IgnoresMemory.bind {α β : Type} {m : FeM α} {k : α → FeM β} (hm : IgnoresMemory m)
(hk : ∀ a, IgnoresMemory (k a)) : IgnoresMemory (m >>= k) := by
intro I w memory
rw [M.exec_bind, M.exec_bind, hm]
cases m.exec I w with
| none => rfl
| some r =>
cases r with
| error f => rfl
| ok p => exact hk p.1 I p.2 memory
theorem IgnoresMemory.ite {α : Type} (c : Prop) [Decidable c] {a b : FeM α} (ha : IgnoresMemory a)
(hb : IgnoresMemory b) : IgnoresMemory (if c then a else b) := by
split <;> assumption
theorem ignoresMemory_read : IgnoresMemory (read : FeM Env) := fun _ _ _ => rfl
theorem ignoresMemory_sload (slot : UInt256) : IgnoresMemory (sload slot) := fun _ _ _ => rfl
theorem ignoresMemory_caller : IgnoresMemory caller := fun _ _ _ => rfl
theorem ignoresMemory_callvalue : IgnoresMemory callvalue := fun _ _ _ => rfl
theorem ignoresMemory_selfbalance : IgnoresMemory selfbalance := fun _ _ _ => rfl
theorem ignoresMemory_cload (offset : UInt256) : IgnoresMemory (cload offset) := fun _ _ _ => rfl
theorem ignoresMemory_immutableAt (total slot : Nat) : IgnoresMemory (immutableAt total slot) :=
fun _ _ _ => rfl
theorem ignoresMemory_sstore (slot val : UInt256) : IgnoresMemory (sstore slot val) := by
intro I w memory
rw [exec_sstore, exec_sstore]
split <;> rfl
theorem ignoresMemory_emit (topics : Array UInt256) (data : ByteArray) :
IgnoresMemory (emit topics data) := by
intro I w memory
rw [exec_emit, exec_emit]
split <;> rfl
theorem ignoresMemory_nonPayable : IgnoresMemory nonPayable := by
intro I w memory
rw [exec_nonPayable, exec_nonPayable]
split <;> rfl
theorem ignoresMemory_checkedAdd (a b : UInt256) : IgnoresMemory (checkedAdd a b) := by
intro I w memory
unfold checkedAdd
split <;> rfl
theorem ignoresMemory_checkedSub (a b : UInt256) : IgnoresMemory (checkedSub a b) := by
intro I w memory
unfold checkedSub
split <;> rfl
theorem withMemory_withMemory {α : Type} (memory memory' : ByteArray)
(x : Option (Except (Exit World) (α × World))) :
withMemory memory (withMemory memory' x) = withMemory memory x := by
match x with
| none => rfl
| some (.error f) => rfl
| some (.ok (a, v)) => rfl
/-- A call with no input and no output copied ignores memory (`call_noData`). -/
theorem ignoresMemory_call (gasArg addr value : UInt256) :
IgnoresMemory (call gasArg addr value ⟨0⟩ ⟨0⟩ ⟨0⟩ ⟨0⟩) := by
intro I w memory
have h₁ : (call gasArg addr value ⟨0⟩ ⟨0⟩ ⟨0⟩ ⟨0⟩).exec I { w with memory := memory } =
withMemory memory ((call gasArg addr value ⟨0⟩ ⟨0⟩ ⟨0⟩ ⟨0⟩).exec I
{ w with memory := ByteArray.empty }) :=
call_noData _ _ _ _ _ I _
have h₂ : (call gasArg addr value ⟨0⟩ ⟨0⟩ ⟨0⟩ ⟨0⟩).exec I w =
withMemory w.memory ((call gasArg addr value ⟨0⟩ ⟨0⟩ ⟨0⟩ ⟨0⟩).exec I
{ w with memory := ByteArray.empty }) := call_noData _ _ _ _ _ I w
rw [h₁, h₂, withMemory_withMemory]
open Fe.M in
/-- Prove `IgnoresMemory` for a computation built from binds, conditionals and the operations
above. Unfold the definitions it should look into first. -/
macro "ignores_memory" : tactic =>
`(tactic| repeat' first
| with_reducible exact ignoresMemory_sload _
| with_reducible exact ignoresMemory_sstore _ _
| with_reducible exact ignoresMemory_emit _ _
| with_reducible exact ignoresMemory_caller
| with_reducible exact ignoresMemory_callvalue
| with_reducible exact ignoresMemory_selfbalance
| with_reducible exact ignoresMemory_read
| with_reducible exact ignoresMemory_cload _
| with_reducible exact ignoresMemory_immutableAt _ _
| with_reducible exact ignoresMemory_nonPayable
| with_reducible exact ignoresMemory_checkedAdd _ _
| with_reducible exact ignoresMemory_checkedSub _ _
| with_reducible exact ignoresMemory_call _ _ _
| with_reducible exact IgnoresMemory.pure _
| with_reducible exact IgnoresMemory.fail _
| with_reducible exact IgnoresMemory.revert _
| with_reducible exact IgnoresMemory.panic _
| assumption
| with_reducible apply IgnoresMemory.ite
| with_reducible apply IgnoresMemory.bind
| intro (_ : _))
/-- A run of code that ignores memory ends as its run on other memory. -/
theorem IgnoresMemory.endsAs {m : FeM ByteArray} (h : IgnoresMemory m) (I : ExecutionEnv)
(w : World) (memory : ByteArray) : EndsAs (m.exec I { w with memory := memory })
(m.exec I w) := by
rw [h]
match m.exec I w with
| none => exact .diverge
| some (.error f) => exact .exit f
| some (.ok (out, v)) => exact .ok_ok rfl rfl rfl
/-- A run alike up to memory with one is alike with it on other memory. -/
theorem Alike.of_withMemory {α : Type} {grow : Nat} {w : World} {memory : ByteArray}
{x y : Option (Except (Exit World) (α × World))} (h : Alike grow w x (withMemory memory y)) :
Alike grow w x y := by
match y, h with
| none, h => exact h
| some (.error f), h => exact h
| some (.ok (a, v)), .ok ha hl hc hm => exact .ok ha hl hc hm
/-- A run alike up to memory with a run of code that ignores memory is alike with its run on any
memory. -/
theorem Alike.of_ignoresMemory {α : Type} {grow : Nat} {v w : World} {memory : ByteArray}
{x : Option (Except (Exit World) (α × World))} {arm : FeM α} {I : ExecutionEnv}
(h : Alike grow v x (arm.exec I { w with memory := memory })) (hign : IgnoresMemory arm) :
Alike grow v x (arm.exec I w) :=
Alike.of_withMemory (hign I w memory ▸ h)
/-! ## Entries that end as hand-written ones -/
/-- An entry that runs `arm` and returns its result through `ret` ends as a run of `arm` that
returns the encoding `enc` of its result, when `ret` stops with that encoding and changes
memory only, and `arm` ignores memory. -/
theorem endsAs_arm {α : Type} {arm : FeM α} {ret : α → FeM Empty} {enc : α → ByteArray}
{I : ExecutionEnv} {w : World}
(hret : ∀ r w', w'.memory.size + 32 < UInt256.size →
∃ memory, (ret r).exec I w' = some (.error (.stop (enc r) { w' with memory := memory })))
(hign : IgnoresMemory arm) (hmem : w.memory.size + 32 < UInt256.size) :
EndsAs ((entry (arm >>= ret)).exec I w) ((arm >>= fun r => pure (enc r)).exec I w) := by
rw [entry_eq]
have hv : arm.exec I w.reserved = withMemory (reserveMemory w.memory) (arm.exec I w) :=
hign I w _
cases h : arm.exec I w with
| none => simp [hv, h]; exact .diverge
| some res =>
cases res with
| error f => simp [hv, h]; exact .exit f
| ok p =>
obtain ⟨r, w'⟩ := p
obtain ⟨memory, hr⟩ := hret r { w' with memory := reserveMemory w.memory }
(reserveMemory_room hmem (by decide))
simp [hv, h, hr]
exact .stop_ok rfl rfl rfl
/-- An entry that runs `arm` and returns its result through `ret` ends as a run of `arm'` that
returns the encoding `enc` of its result, when `arm` on reserved memory and `arm'` end alike
up to memory, and `ret` stops with that encoding, changing memory only. -/
theorem endsAs_alike {α : Type} {arm arm' : FeM α} {ret : α → FeM Empty} {enc : α → ByteArray}
{I : ExecutionEnv} {w : World} {grow : Nat}
(hret : ∀ r w', w'.memory.size + 32 < UInt256.size →
∃ memory, (ret r).exec I w' = some (.error (.stop (enc r) { w' with memory := memory })))
(halike : Alike grow w.reserved (arm.exec I w.reserved) (arm'.exec I w))
(hmem : w.reserved.memory.size + grow + 32 < UInt256.size) :
EndsAs ((entry (arm >>= ret)).exec I w) ((arm' >>= fun r => pure (enc r)).exec I w) := by
rw [entry_eq]
generalize ha : arm.exec I w.reserved = x at halike
generalize hb : arm'.exec I w = y at halike
cases halike with
| ok hacc hlogs hcalls hm =>
rename_i a v v'
obtain ⟨memory, hr⟩ := hret a v (by omega)
simp [ha, hb, hr]
exact .stop_ok hacc hlogs hcalls
| exit f => simp [ha, hb]; exact .exit f
| diverge => simp [ha, hb]; exact .diverge
end Fe.EVM