Repository navigation
Expand file tree
/
Copy pathWorld.lean
More file actions
771 lines (615 loc) · 33 KB
/
Copy pathWorld.lean
File metadata and controls
771 lines (615 loc) · 33 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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
import Ethereum.Semantics
import Fe.Core
import Fe.EVM.FrameCalls
/-!
# The EVM world of a Fe model
A contract's model runs in the EVM's own state types, so a certificate needs no relation
between two representations of storage.
-/
namespace Fe.EVM
open Ethereum Ethereum.EVM.Invariant
/-- What the context supplies each time the model consults it: once for `gas()`, once for each
message call or creation. The model counts no gas and keeps no substate, so it cannot
compute these. -/
structure Supply where
/-- `gas()`: the value read. A message call: the most the callee may get, before the cap of
the call's own gas argument. A creation: the gas the init code runs with. -/
gas : UInt256
/-- The accrued substate the EVM hands the callee: what is warm, what this transaction
created, the refund counter, the log so far. -/
substate : Substate
/-- The account map the transaction started from, which prices the callee's `SSTORE`s. -/
orig : AccountMap
/-- A message call or a creation the model made: the inputs it passed to `Θ` or to `Lambda`. -/
inductive Made where
| theta (T : ThetaCall)
| lambda (L : LambdaCall)
/-- The made call or creation satisfies the condition `Q` on calls and creations. -/
def Made.Ok (Q : CallPreds) : Made → Prop
| .theta T => Q.theta T
| .lambda L => Q.lambda L
/-- The part of the EVM state a contract's model reads and writes. -/
structure World where
accounts : AccountMap
logs : LogSeries
/-- The call's memory. Its size is the address the next allocation gets. It is the model's
own: a certificate compares return data, never memory. -/
memory : ByteArray := ByteArray.empty
/-- The return data of the last message call or creation, empty before the first. -/
returnData : ByteArray := ByteArray.empty
/-- What the context has yet to supply. A model that makes no call and reads no gas never
looks at it. -/
feed : List Supply := []
/-- A ghost record of the message calls and creations made, in order: the inputs passed to `Θ`
and `Lambda`. A call or creation that is not made (short of funds, at depth 1024) records
nothing. The model never reads it; it says what calls the model makes. -/
calls : List Made := []
/-- The world a message call starts in, with the feed the context will supply. -/
def World.init (σ : AccountMap) (A : Substate) (feed : List Supply := []) : World :=
{ accounts := σ, logs := A.logSeries, feed := feed }
abbrev Env := ExecutionEnv
/-- The model monad on the EVM. -/
abbrev FeM := Fe.M Env World
/-- The calldata of the current call. -/
def calldata : FeM ByteArray := do return (← read).calldata
/-- `calldataload(offset)`: the 32 bytes of calldata from `offset`, zero-padded. -/
def calldataload (offset : UInt256) : FeM UInt256 := do
return uInt256OfByteArray ((← read).calldata.readBytes offset.toNat 32)
/-- `calldatasize()` -/
def calldatasize : FeM UInt256 := do return UInt256.ofNat (← read).calldata.size
/-- `revert_empty()`: revert with no data. It never returns. -/
def revertEmpty : FeM Empty := Fe.M.revert ByteArray.empty
/-- `panic_code(code)`: fail with `Panic(code)`. It never returns. -/
def panicCode (code : UInt256) : FeM Empty := Fe.M.panic code.toNat
/-- `alloc_raw(size)`: `size` fresh zero bytes at the end of memory. Returns their address.
Memory cannot reach `2 ^ 256` bytes: the EVM runs out of gas long before, so a run that
would get there does not terminate. -/
def alloc (size : UInt256) : FeM UInt256 := do
let w ← get
if w.memory.size + size.toNat < UInt256.size then
set { w with memory := w.memory ++ (⟨Array.replicate size.toNat 0⟩ : ByteArray) }
return UInt256.ofNat w.memory.size
else
Fe.M.diverge
/-- Write `value` as the 32-byte big-endian word at `ptr`. -/
def mstore (ptr value : UInt256) : FeM Unit :=
modify fun w => { w with memory := value.toByteArray.write 0 w.memory ptr.toNat 32 }
/-- The 32-byte big-endian word at `ptr`, reading zero past the end of memory. -/
def mload (ptr : UInt256) : FeM UInt256 := do
return uInt256OfByteArray ((← get).memory.readWithPadding ptr.toNat 32)
/-- Write `bytes` at `ptr`: the copy of the constructor arguments, the code after the creation
code, into memory. -/
def mstoreBytes (ptr : UInt256) (bytes : ByteArray) : FeM Unit :=
modify fun w => { w with memory := bytes.write 0 w.memory ptr.toNat bytes.size }
/-- A string literal longer than a word: fresh memory that holds its length word, then its
bytes, then zeros to a whole word. Returns the address. -/
def stringLiteral (bytes : ByteArray) : FeM UInt256 := do
let ptr ← alloc (UInt256.ofNat (32 + (bytes.size + 31) / 32 * 32))
mstore ptr (UInt256.ofNat bytes.size)
mstoreBytes (ptr + UInt256.ofNat 32) bytes
return ptr
/-- `read_padded_word(ptr, len)`: the big-endian word whose first `min len 32` bytes are
memory's at `ptr` and whose other bytes are zero. -/
def readPaddedWord (ptr len : UInt256) : FeM UInt256 := do
let bytes := (← get).memory.readWithPadding ptr.toNat (min len.toNat 32)
return uInt256OfByteArray (bytes.readWithPadding 0 32)
/-- `zero_mem(ptr, len)`: write `len` zero bytes at `ptr`. -/
def zeroMem (ptr len : UInt256) : FeM Unit :=
modify fun w =>
{ w with memory :=
(⟨Array.replicate len.toNat 0⟩ : ByteArray).write 0 w.memory ptr.toNat len.toNat }
/-- `mstore8(ptr, value)`: write the low byte of `value` at `ptr`. -/
def mstore8 (ptr value : UInt256) : FeM Unit :=
modify fun w =>
{ w with memory := (⟨#[UInt8.ofNat value.toNat]⟩ : ByteArray).write 0 w.memory ptr.toNat 1 }
/-- `copy_mem(dest, source, len)`: copy `len` bytes of memory, as `MCOPY` does. -/
def mcopy (dest source len : UInt256) : FeM Unit :=
modify fun w =>
{ w with memory := w.memory.write source.toNat w.memory dest.toNat len.toNat }
/-- `calldatacopy(dest, offset, len)`: copy `len` bytes of calldata from `offset` into memory at
`dest`, zeros past its end. -/
def calldatacopy (dest offset len : UInt256) : FeM Unit := do
let cd := (← read).calldata
modify fun w => { w with memory := cd.write offset.toNat w.memory dest.toNat len.toNat }
/-- `codecopy(dest, offset, len)`: copy `len` bytes of the running code from `offset` into memory
at `dest`, zeros past its end. -/
def codecopy (dest offset len : UInt256) : FeM Unit := do
let code := (← read).code
modify fun w => { w with memory := code.write offset.toNat w.memory dest.toNat len.toNat }
/-- `codesize()` -/
def codesize : FeM UInt256 := do return UInt256.ofNat (← read).code.size
/-- `C::init_code_len()`: the size of `creation`, the creation code of a contract `C` that the
running one deploys. -/
def initCodeLen (creation : ByteArray) : FeM UInt256 := pure (UInt256.ofNat creation.size)
/-- `C::init_code_offset()`: where `creation`, the creation code of a contract `C` that the
running one deploys, lies in the running code. A contract that deploys `C` holds its
creation code, and this is where that code first occurs; code that does not hold it gives
its own size. -/
def initCodeOffset (creation : ByteArray) : FeM UInt256 := do
let code := (← read).code
let found := (List.range (code.size + 1 - creation.size)).find? fun i =>
code.get! i == creation.get! 0 && code.extract i (i + creation.size) == creation
return UInt256.ofNat (found.getD code.size)
/-- The 32-byte big-endian word of the running code at `offset`, reading zero past its end. -/
def cload (offset : UInt256) : FeM UInt256 := do
return uInt256OfByteArray ((← read).code.readWithPadding offset.toNat 32)
/-- Where word `slot` of a contract's immutable fields lies in the running code. The fields
take the last `total` words of the deployed code, so the address counts back from its end,
wrapping as the EVM's arithmetic does. -/
def immutableAt (total slot : Nat) : FeM UInt256 := do
return UInt256.ofNat (← read).code.size - UInt256.ofNat ((total - slot) * 32)
/-- The `len` bytes of memory at `ptr`, zeros past its end: what a constructor returns for the
words of the immutable fields. -/
def memoryBytes (ptr len : UInt256) : FeM ByteArray := do
return (← get).memory.readWithPadding ptr.toNat len.toNat
/-- `keccak256(ptr, len)`: the hash of the `len` bytes of memory at `ptr`. -/
def keccak256 (ptr len : UInt256) : FeM UInt256 := do
return .ofNat (fromByteArrayBigEndian (KEC ((← get).memory.readWithPadding ptr.toNat len.toNat)))
/-- `stop()`: end the call successfully with no data. It never returns. -/
def stopCall : FeM Empty := Fe.M.stop ByteArray.empty
/-- `panic()` and `todo()`: an invalid instruction. They never return. -/
def trap : FeM Empty := Fe.M.fail .trap
/-- `revert(ptr, len)`: revert with the `len` bytes of memory at `ptr`. It never returns. -/
def revertData (ptr len : UInt256) : FeM Empty := do
Fe.M.revert ((← get).memory.readWithPadding ptr.toNat len.toNat)
/-- `return_data(ptr, len)`: end the call successfully with the `len` bytes of memory at `ptr`.
It never returns. -/
def returnData (ptr len : UInt256) : FeM Empty := do
Fe.M.stop ((← get).memory.readWithPadding ptr.toNat len.toNat)
/-- The selector of the call: the first four bytes of calldata, as the entry reads them,
`calldataload(0) >> 224`. -/
def calldataSelector : FeM UInt256 := do
return (← calldataload (UInt256.ofNat 0)) >>> UInt256.ofNat 224
/-- The low `bits` bits of `v`: a cast to a narrower unsigned integer type. -/
def truncate (bits : Nat) (v : UInt256) : UInt256 := UInt256.ofNat (v.toNat % 2 ^ bits)
/-- An entry point that takes no value reverts when sent some. -/
def nonPayable : FeM Unit := do
if (← read).weiValue ≠ ⟨0⟩ then Fe.M.revert ByteArray.empty
/-- Calldata is too short to hold `words` argument words after the selector, or so long that
Solidity's signed length check rejects it. -/
def BadHead (cd : ByteArray) (words : Nat) : Prop :=
cd.size < 4 + 32 * words ∨ 2 ^ 255 + 4 ≤ cd.size
instance (cd : ByteArray) (words : Nat) : Decidable (BadHead cd words) := by
unfold BadHead; infer_instance
/-- The word at `slot` of the storage of the contract `I` runs. -/
def slotWord (I : Env) (σ : AccountMap) (slot : UInt256) : UInt256 :=
σ.get? I.codeOwner |>.option ⟨0⟩ (fun acc => acc.storage.getD slot ⟨0⟩)
/-- The word at `slot` of the current contract's storage. -/
def sload (slot : UInt256) : FeM UInt256 := do
return slotWord (← read) (← get).accounts slot
/-- The account map after the contract at `owner` stores `val` at `slot`. A zero erases the
slot, as the EVM does. -/
def storeWord (owner : AccountAddress) (σ : AccountMap) (slot val : UInt256) : AccountMap :=
σ.get? owner |>.option σ fun acc =>
σ.insert owner
(if val == default then { acc with storage := acc.storage.erase slot }
else { acc with storage := acc.storage.insert slot val })
/-- A store keeps the account it stores into. -/
theorem storeWord_owner {o : AccountAddress} {σ : AccountMap} {k v : UInt256}
(h : ∃ acc, σ.get? o = some acc) : ∃ acc, (storeWord o σ k v).get? o = some acc := by
obtain ⟨acc, ha⟩ := h
simp only [storeWord, ha, Option.option, Std.ExtTreeMap.get?_eq_getElem?,
Std.ExtTreeMap.getElem?_insert_self]
exact ⟨_, rfl⟩
/-- A load after a store to the same slot reads the stored word, when the account map has an
account at the code owner. Without the account the store is dropped and the load reads 0. -/
theorem slotWord_storeWord {I : Env} {σ : AccountMap} {k v : UInt256}
(h : ∃ acc, σ.get? I.codeOwner = some acc) :
slotWord I (storeWord I.codeOwner σ k v) k = v := by
obtain ⟨acc, ha⟩ := h
simp only [slotWord, storeWord, ha, Option.option, Std.ExtTreeMap.get?_eq_getElem?,
Std.ExtTreeMap.getElem?_insert_self]
split
· next hv =>
rw [Std.ExtTreeMap.getD_erase_self]
exact (beq_iff_eq.mp hv).symm
· rw [Std.ExtTreeMap.getD_insert_self]
/-- Store `val` at `slot` of the current contract's storage. Under a static call it fails. -/
def sstore (slot val : UInt256) : FeM Unit := do
let env ← read
if env.perm then
modify fun w => { w with accounts := storeWord env.codeOwner w.accounts slot val }
else
Fe.M.fail .static
/-- Append a log entry of the current contract. Under a static call it fails. -/
def emit (topics : Array UInt256) (data : ByteArray) : FeM Unit := do
let env ← read
if env.perm then
modify fun w => { w with logs := w.logs.push ⟨env.codeOwner, topics, data⟩ }
else
Fe.M.fail .static
/-- `log<n>(ptr, len, topics…)`: append a log entry whose data is the `len` bytes of memory at
`ptr`. Under a static call it fails. -/
def logData (ptr len : UInt256) (topics : Array UInt256) : FeM Unit := do
emit topics ((← get).memory.readWithPadding ptr.toNat len.toNat)
/-- The word at `slot` of the current contract's transient storage. -/
def tload (slot : UInt256) : FeM UInt256 := do
let env ← read
return ((← get).accounts.get? env.codeOwner).option ⟨0⟩ (fun acc => acc.tstorage.getD slot ⟨0⟩)
/-- Store `val` at `slot` of the current contract's transient storage. Under a static call it
fails. -/
def tstore (slot val : UInt256) : FeM Unit := do
let env ← read
if env.perm then
modify fun w =>
{ w with accounts :=
(w.accounts.get? env.codeOwner).option w.accounts fun acc =>
w.accounts.insert env.codeOwner (acc.updateTransientStorage slot val) }
else
Fe.M.fail .static
/-! ## The call's context -/
/-- `address()`: the running contract. -/
def selfAddress : FeM UInt256 := do return UInt256.ofNat (← read).codeOwner.val
/-- `callvalue()` -/
def callvalue : FeM UInt256 := do return (← read).weiValue
/-- `origin()`: the transaction's sender. -/
def origin : FeM UInt256 := do return UInt256.ofNat (← read).sender.val
/-- `gasprice()` -/
def gasprice : FeM UInt256 := do return UInt256.ofNat (← read).gasPrice
/-- `coinbase()` -/
def coinbase : FeM UInt256 := do return UInt256.ofNat (← read).header.beneficiary.val
/-- `timestamp()` -/
def timestamp : FeM UInt256 := do return UInt256.ofNat (← read).header.timestamp
/-- `number()`: the block number. -/
def blockNumber : FeM UInt256 := do return UInt256.ofNat (← read).header.number
/-- `prevrandao()` -/
def prevrandao : FeM UInt256 := do return (← read).header.prevRandao
/-- `gaslimit()` -/
def gaslimit : FeM UInt256 := do return UInt256.ofNat (← read).header.gasLimit
/-- `chainid()` -/
def chainid : FeM UInt256 := pure (UInt256.ofNat Ethereum.chainId)
/-- `basefee()` -/
def basefee : FeM UInt256 := do return Ethereum.basefee (← read)
/-- `blobbasefee()` -/
def blobbasefee : FeM UInt256 := do return (← read).getBlobGasprice
/-- `blobhash(index)` -/
def blobhash (index : UInt256) : FeM UInt256 := do return Ethereum.blobhash (← read) index
/-- `blockhash(number)`: the hash of one of the 256 most recent blocks, and zero otherwise. -/
def blockhash (number : UInt256) : FeM UInt256 := do
let env ← read
let current := env.header.number
if current ≤ number.toNat || number.toNat + 256 < current then return ⟨0⟩
else return (env.blocks.map ProcessedBlock.hash).getD number.toNat ⟨0⟩
/-- `balance(addr)`: the balance of an account, zero if there is none. -/
def balance (addr : UInt256) : FeM UInt256 := do
return ((← get).accounts.get? (AccountAddress.ofUInt256 addr)).elim ⟨0⟩ (·.balance)
/-- `selfbalance()` -/
def selfbalance : FeM UInt256 := do
return ((← get).accounts.get? (← read).codeOwner).elim ⟨0⟩ (·.balance)
/-- `extcodesize(addr)` -/
def extcodesize (addr : UInt256) : FeM UInt256 := do
return ((← get).accounts.get? (AccountAddress.ofUInt256 addr)).option ⟨0⟩
(.ofNat ∘ ByteArray.size ∘ (·.code))
/-- The slot of entry `key` of a `StorageMap` rooted at `root`: Solidity's mapping layout.
Exported code does not name it. It reaches an entry through std's `locate`, which hashes
the key and the root in memory, and `keyed_slot` shows that hash is this slot. -/
def mapSlot (key root : UInt256) : UInt256 :=
uInt256OfByteArray (KEC (key.toByteArray ++ root.toByteArray))
/-- A state space of a contract: its storage, or its transient storage. -/
inductive Space
| storage
| transient
deriving DecidableEq, Inhabited
/-- A place a Fe access reaches in contract state: a slot of a state space. A value of more than
one slot starts there. This is what a reference to state is at run time: the slot number. -/
structure Place where
space : Space
slot : UInt256
deriving Inhabited
/-- The place at `slot` of storage. -/
def Place.storage (slot : UInt256) : Place := ⟨.storage, slot⟩
/-- Where each field of a struct or tuple lies below the first slot of its place. -/
structure Layout where
/-- The slot of each field in storage, counted from the struct's own. -/
storage : List Nat
/-- The place of field `index` of the struct at `p`. -/
def Place.field (p : Place) (layout : Layout) (index : Nat) : Place :=
{ p with slot := p.slot + UInt256.ofNat (layout.storage.getD index 0) }
/-- The word at `p`. -/
def load (p : Place) : FeM UInt256 :=
match p.space with
| .storage => sload p.slot
| .transient => tload p.slot
/-- Store `val` at `p`. -/
def store (p : Place) (val : UInt256) : FeM Unit :=
match p.space with
| .storage => sstore p.slot val
| .transient => tstore p.slot val
/-- The word of `cd` at byte offset `off`. -/
def word (cd : ByteArray) (off : Nat) : UInt256 := uInt256OfByteArray (cd.readBytes off 32)
/-- A calldata word that is not a canonical address: a bit above the low 160 is set. -/
def NotAddress (w : UInt256) : Prop := 2 ^ 160 ≤ w.toNat
instance (w : UInt256) : Decidable (NotAddress w) := by unfold NotAddress; infer_instance
/-- The caller as a word. -/
def caller : FeM UInt256 := do return UInt256.ofNat (← read).source.val
/-- The check before an array element is reached: `Panic(0x32)` unless `index` is below the
array's length `len`. -/
def checkIndex (index : UInt256) (len : Nat) : FeM Unit :=
if index.toNat < len then pure () else Fe.M.panic 0x32
/-- Checked `a + b`: `Panic(0x11)` on overflow. -/
def checkedAdd (a b : UInt256) : FeM UInt256 :=
if UInt256.size ≤ a.toNat + b.toNat then Fe.M.panic 0x11 else pure (a + b)
/-- Checked `a - b`: `Panic(0x11)` on underflow. -/
def checkedSub (a b : UInt256) : FeM UInt256 :=
if a.toNat < b.toNat then Fe.M.panic 0x11 else pure (UInt256.sub a b)
/-- Checked `a * b`: `Panic(0x11)` on overflow. -/
def checkedMul (a b : UInt256) : FeM UInt256 :=
if UInt256.size ≤ a.toNat * b.toNat then Fe.M.panic 0x11 else pure (a * b)
/-- `a / b`: `Panic(0x12)` when `b` is zero. -/
def checkedDiv (a b : UInt256) : FeM UInt256 :=
if b = ⟨0⟩ then Fe.M.panic 0x12 else pure (a / b)
/-- `a % b`: `Panic(0x12)` when `b` is zero. -/
def checkedRem (a b : UInt256) : FeM UInt256 :=
if b = ⟨0⟩ then Fe.M.panic 0x12 else pure (a % b)
/-- The return data of a failed `assert!` with a message: Solidity's `Error(string)`. Not
observed at profile `P0`; to confirm against Fe's lowering with the first `P1` proof. -/
def assertData (msg : String) : ByteArray :=
let bytes := msg.toUTF8
⟨#[0x08, 0xc3, 0x79, 0xa0]⟩ ++ (UInt256.ofNat 32).toByteArray
++ (UInt256.ofNat bytes.size).toByteArray ++ bytes
++ ⟨Array.replicate ((32 - bytes.size % 32) % 32) 0⟩
@[simp] theorem exec_sload (slot : UInt256) (I : Env) (w : World) :
(sload slot).exec I w = some (.ok (slotWord I w.accounts slot, w)) := rfl
@[simp] theorem load_storage (slot : UInt256) : load (.storage slot) = sload slot := rfl
@[simp] theorem store_storage (slot val : UInt256) :
store (.storage slot) val = sstore slot val := rfl
@[simp] theorem Place.field_storage (slot : UInt256) (layout : Layout) (index : Nat) :
(Place.storage slot).field layout index =
.storage (slot + UInt256.ofNat (layout.storage.getD index 0)) := rfl
@[simp] theorem Place.storage_slot (slot : UInt256) : (Place.storage slot).slot = slot := rfl
@[simp] theorem load_mk_storage (slot : UInt256) : load ⟨.storage, slot⟩ = sload slot := rfl
@[simp] theorem store_mk_storage (slot val : UInt256) :
store ⟨.storage, slot⟩ val = sstore slot val := rfl
@[simp] theorem exec_caller (I : Env) (w : World) :
caller.exec I w = some (.ok (UInt256.ofNat I.source.val, w)) := rfl
@[simp] theorem exec_calldataload (offset : UInt256) (I : Env) (w : World) :
(calldataload offset).exec I w =
some (.ok (uInt256OfByteArray (I.calldata.readBytes offset.toNat 32), w)) := rfl
@[simp] theorem exec_calldatasize (I : Env) (w : World) :
calldatasize.exec I w = some (.ok (UInt256.ofNat I.calldata.size, w)) := rfl
@[simp] theorem exec_revertEmpty (I : Env) (w : World) :
revertEmpty.exec I w = some (.error (.revert ByteArray.empty)) := rfl
@[simp] theorem exec_calldata (I : Env) (w : World) :
calldata.exec I w = some (.ok (I.calldata, w)) := rfl
theorem exec_sstore (slot val : UInt256) (I : Env) (w : World) :
(sstore slot val).exec I w =
if I.perm then some (.ok ((), { w with accounts := storeWord I.codeOwner w.accounts slot val }))
else some (.error .static) := by
cases h : I.perm <;> simp [sstore, h]
theorem exec_emit (topics : Array UInt256) (data : ByteArray) (I : Env) (w : World) :
(emit topics data).exec I w =
if I.perm then some (.ok ((), { w with logs := w.logs.push ⟨I.codeOwner, topics, data⟩ }))
else some (.error .static) := by
cases h : I.perm <;> simp [emit, h]
theorem exec_nonPayable (I : Env) (w : World) :
nonPayable.exec I w =
if I.weiValue = ⟨0⟩ then some (.ok ((), w)) else some (.error (.revert ByteArray.empty)) := by
by_cases h : I.weiValue = ⟨0⟩ <;> simp [nonPayable, h]
/-! ## The operations never stop the run from inside -/
open Fe.M in
/-- Prove `NoStop` for a computation built from binds, conditionals and the core operations. -/
macro "no_stop_core" : tactic =>
`(tactic| repeat' first
| with_reducible exact NoStop.pure _
| with_reducible exact NoStop.revert _
| with_reducible exact NoStop.panic _
| with_reducible exact NoStop.fail_static
| with_reducible exact NoStop.read
| with_reducible exact NoStop.get
| with_reducible exact NoStop.modify _
| with_reducible exact NoStop.unwind _
| assumption
| with_reducible apply NoStop.ite
| with_reducible apply NoStop.bind
| intro (_ : _))
theorem noStop_sload (slot : UInt256) : (sload slot).NoStop := by unfold sload; no_stop_core
theorem noStop_sstore (slot val : UInt256) : (sstore slot val).NoStop := by unfold sstore; no_stop_core
theorem noStop_emit (topics : Array UInt256) (data : ByteArray) : (emit topics data).NoStop := by
unfold emit; no_stop_core
theorem noStop_caller : caller.NoStop := by unfold caller; no_stop_core
theorem noStop_calldata : calldata.NoStop := by unfold calldata; no_stop_core
theorem noStop_nonPayable : nonPayable.NoStop := by unfold nonPayable; no_stop_core
theorem noStop_checkedAdd (a b : UInt256) : (checkedAdd a b).NoStop := by unfold checkedAdd; no_stop_core
theorem noStop_checkedSub (a b : UInt256) : (checkedSub a b).NoStop := by unfold checkedSub; no_stop_core
open Fe.M in
/-- Prove `NoStop` for a computation built from binds, conditionals and the operations of this
file. Unfold the definitions it should look into first. -/
macro "no_stop" : tactic =>
`(tactic| repeat' first
| with_reducible exact noStop_sload _
| with_reducible exact noStop_sstore _ _
| with_reducible exact noStop_emit _ _
| with_reducible exact noStop_caller
| with_reducible exact noStop_calldata
| with_reducible exact noStop_nonPayable
| with_reducible exact noStop_checkedAdd _ _
| with_reducible exact noStop_checkedSub _ _
| with_reducible exact NoStop.pure _
| with_reducible exact NoStop.revert _
| with_reducible exact NoStop.panic _
| with_reducible exact NoStop.unwind _
| assumption
| with_reducible apply NoStop.ite
| with_reducible apply NoStop.bind
| intro (_ : _))
/-! ## The operations leave memory alone -/
/-- The computation leaves memory as it found it whenever it returns. Storage, logs and the
environment's reads do; allocation and writes through pointers do not. -/
def KeepsMemory {α : Type} (m : FeM α) : Prop :=
∀ I w a w', m.exec I w = some (.ok (a, w')) → w'.memory = w.memory
theorem KeepsMemory.pure {α : Type} (a : α) : KeepsMemory (pure a : FeM α) := by
intro I w a' w' h; simp at h; rw [h.2]
theorem KeepsMemory.revert {α : Type} (data : ByteArray) :
KeepsMemory (Fe.M.revert data : FeM α) := by
intro I w a w' h; simp at h
theorem KeepsMemory.panic {α : Type} (code : Nat) : KeepsMemory (Fe.M.panic code : FeM α) := by
intro I w a w' h; simp at h
theorem KeepsMemory.bind {α β : Type} {m : FeM α} {k : α → FeM β} (hm : KeepsMemory m)
(hk : ∀ a, KeepsMemory (k a)) : KeepsMemory (m >>= k) := by
intro I w b w' h
rw [M.exec_bind] at h
cases hr : m.exec I w with
| none => rw [hr] at h; cases h
| some r =>
cases r with
| error f => rw [hr] at h; cases h
| ok p => rw [hr] at h; rw [hk p.1 I p.2 b w' h, hm I w p.1 p.2 hr]
theorem KeepsMemory.ite {α : Type} (c : Prop) [Decidable c] {a b : FeM α} (ha : KeepsMemory a)
(hb : KeepsMemory b) : KeepsMemory (if c then a else b) := by
split <;> assumption
theorem keepsMemory_sload (slot : UInt256) : KeepsMemory (sload slot) := by
intro I w a w' h; simp at h; rw [h.2]
theorem keepsMemory_sstore (slot val : UInt256) : KeepsMemory (sstore slot val) := by
intro I w a w' h
rw [exec_sstore] at h
split at h <;> simp at h
rw [← h]
theorem keepsMemory_emit (topics : Array UInt256) (data : ByteArray) :
KeepsMemory (emit topics data) := by
intro I w a w' h
rw [exec_emit] at h
split at h <;> simp at h
rw [← h]
theorem keepsMemory_caller : KeepsMemory caller := by
intro I w a w' h; simp at h; rw [h.2]
theorem keepsMemory_checkedAdd (a b : UInt256) : KeepsMemory (checkedAdd a b) := by
unfold checkedAdd; exact KeepsMemory.ite _ (KeepsMemory.panic _) (KeepsMemory.pure _)
theorem keepsMemory_checkedSub (a b : UInt256) : KeepsMemory (checkedSub a b) := by
unfold checkedSub; exact KeepsMemory.ite _ (KeepsMemory.panic _) (KeepsMemory.pure _)
/-- Prove `KeepsMemory` for a computation built from binds, conditionals and the storage, log
and arithmetic operations. Unfold the definitions it should look into first. -/
macro "keeps_memory" : tactic =>
`(tactic| repeat' first
| with_reducible exact keepsMemory_sload _
| with_reducible exact keepsMemory_sstore _ _
| with_reducible exact keepsMemory_emit _ _
| with_reducible exact keepsMemory_caller
| with_reducible exact keepsMemory_checkedAdd _ _
| with_reducible exact keepsMemory_checkedSub _ _
| with_reducible exact KeepsMemory.pure _
| with_reducible exact KeepsMemory.revert _
| with_reducible exact KeepsMemory.panic _
| assumption
| with_reducible apply KeepsMemory.ite
| with_reducible apply KeepsMemory.bind
| intro (_ : _))
/-! ## The operations make no call -/
/-- The computation records no call or creation, whether it returns or stops the run from
inside: every operation but `message` and `createWith` (`Calls.lean`) and what runs them. -/
def KeepsCalls {α : Type} (m : FeM α) : Prop :=
∀ I w, (∀ a w', m.exec I w = some (.ok (a, w')) → w'.calls = w.calls) ∧
∀ data w', m.exec I w = some (.error (.stop data w')) → w'.calls = w.calls
theorem KeepsCalls.pure {α : Type} (a : α) : KeepsCalls (pure a : FeM α) := by
intro I w; constructor <;> intro _ _ h <;> simp at h; rw [h.2]
theorem KeepsCalls.fail {α : Type} {f : Exit World} (hf : ∀ data w, f ≠ .stop data w) :
KeepsCalls (Fe.M.fail f : FeM α) := by
intro I w; constructor <;> intro _ _ h <;> simp at h; exact absurd h (hf _ _)
theorem KeepsCalls.revert {α : Type} (data : ByteArray) :
KeepsCalls (Fe.M.revert data : FeM α) := KeepsCalls.fail (by simp)
theorem KeepsCalls.panic {α : Type} (code : Nat) : KeepsCalls (Fe.M.panic code : FeM α) :=
KeepsCalls.fail (by simp)
theorem KeepsCalls.fail_static {α : Type} : KeepsCalls (Fe.M.fail .static : FeM α) :=
KeepsCalls.fail (by simp)
theorem KeepsCalls.stop {α : Type} (data : ByteArray) : KeepsCalls (Fe.M.stop data : FeM α) := by
intro I w; constructor <;> intro _ _ h <;> simp at h; rw [h.2]
theorem KeepsCalls.diverge {α : Type} : KeepsCalls (Fe.M.diverge : FeM α) := by
intro I w; constructor <;> intro _ _ h <;> simp at h
theorem KeepsCalls.read : KeepsCalls (read : FeM Env) := by
intro I w; constructor <;> intro _ _ h <;> simp at h; rw [h.2]
theorem KeepsCalls.get : KeepsCalls (get : FeM World) := by
intro I w; constructor <;> intro _ _ h <;> simp at h; rw [h.2]
theorem KeepsCalls.modify {f : World → World} (hf : ∀ w, (f w).calls = w.calls) :
KeepsCalls (modify f : FeM PUnit) := by
intro I w; constructor <;> intro _ _ h <;> simp at h; rw [← h, hf]
theorem KeepsCalls.bind {α β : Type} {m : FeM α} {k : α → FeM β} (hm : KeepsCalls m)
(hk : ∀ a, KeepsCalls (k a)) : KeepsCalls (m >>= k) := by
intro I w
rw [M.exec_bind]
cases hr : m.exec I w with
| none => constructor <;> intro _ _ h <;> cases h
| some r =>
cases r with
| error f =>
constructor <;> intro _ _ h <;> simp only [Option.some.injEq, reduceCtorEq,
Except.error.injEq] at h
subst h
exact (hm I w).2 _ _ hr
| ok p =>
obtain ⟨a, v⟩ := p
have hv := (hm I w).1 a v hr
exact ⟨fun b w' h => ((hk a I v).1 b w' h).trans hv,
fun d w' h => ((hk a I v).2 d w' h).trans hv⟩
/-- The instance is implicit, so that applying the lemma unifies with the instance a
conditional carries instead of synthesizing one to compare it with. -/
theorem KeepsCalls.ite {α : Type} {c : Prop} {inst : Decidable c} {a b : FeM α}
(ha : KeepsCalls a) (hb : KeepsCalls b) : KeepsCalls (@ite _ c inst a b) := by
split <;> assumption
theorem KeepsCalls.dite {α : Type} (c : Prop) [Decidable c] {a : c → FeM α} {b : ¬ c → FeM α}
(ha : ∀ h, KeepsCalls (a h)) (hb : ∀ h, KeepsCalls (b h)) :
KeepsCalls (if h : c then a h else b h) := by
split
· exact ha _
· exact hb _
/-- An unwound run returns where the run returned or stopped, so it keeps the calls of both. -/
theorem KeepsCalls.unwind {α : Type} {m : FeM α} (hm : KeepsCalls m) :
KeepsCalls (Fe.M.unwind m) := by
intro I w
constructor <;> intro x w' h <;> rw [M.exec_unwind] at h <;> split at h <;>
simp only [Option.some.injEq, Except.ok.injEq, Except.error.injEq, Prod.mk.injEq,
reduceCtorEq] at h
· rw [← h.2]; exact (hm I w).1 _ _ (by assumption)
· rw [← h.2]; exact (hm I w).2 _ _ (by assumption)
· rename_i hf _; exact absurd h (hf _ _)
theorem keepsCalls_sload (slot : UInt256) : KeepsCalls (sload slot) := by
unfold sload; exact KeepsCalls.bind KeepsCalls.read fun _ =>
KeepsCalls.bind KeepsCalls.get fun _ => KeepsCalls.pure _
theorem keepsCalls_sstore (slot val : UInt256) : KeepsCalls (sstore slot val) := by
unfold sstore
exact KeepsCalls.bind KeepsCalls.read fun _ =>
KeepsCalls.ite (KeepsCalls.modify fun _ => rfl) KeepsCalls.fail_static
theorem keepsCalls_emit (topics : Array UInt256) (data : ByteArray) :
KeepsCalls (emit topics data) := by
unfold emit
exact KeepsCalls.bind KeepsCalls.read fun _ =>
KeepsCalls.ite (KeepsCalls.modify fun _ => rfl) KeepsCalls.fail_static
theorem keepsCalls_caller : KeepsCalls caller := by
unfold caller; exact KeepsCalls.bind KeepsCalls.read fun _ => KeepsCalls.pure _
theorem keepsCalls_callvalue : KeepsCalls callvalue := by
unfold callvalue; exact KeepsCalls.bind KeepsCalls.read fun _ => KeepsCalls.pure _
theorem keepsCalls_calldata : KeepsCalls calldata := by
unfold calldata; exact KeepsCalls.bind KeepsCalls.read fun _ => KeepsCalls.pure _
theorem keepsCalls_selfbalance : KeepsCalls selfbalance := by
unfold selfbalance
exact KeepsCalls.bind KeepsCalls.get fun _ => KeepsCalls.bind KeepsCalls.read fun _ =>
KeepsCalls.pure _
theorem keepsCalls_nonPayable : KeepsCalls nonPayable := by
unfold nonPayable
exact KeepsCalls.bind KeepsCalls.read fun _ =>
KeepsCalls.ite (KeepsCalls.revert _) (KeepsCalls.pure _)
/-- Prove `KeepsCalls` for a computation built from binds, conditionals and the operations of
this file. Unfold the definitions it should look into first. -/
macro "keeps_calls" : tactic =>
`(tactic| repeat' first
| with_reducible exact keepsCalls_sload _
| with_reducible exact keepsCalls_sstore _ _
| with_reducible exact keepsCalls_emit _ _
| with_reducible exact keepsCalls_caller
| with_reducible exact keepsCalls_callvalue
| with_reducible exact keepsCalls_calldata
| with_reducible exact keepsCalls_selfbalance
| with_reducible exact keepsCalls_nonPayable
| with_reducible exact KeepsCalls.pure _
| with_reducible exact KeepsCalls.revert _
| with_reducible exact KeepsCalls.panic _
| with_reducible exact KeepsCalls.fail_static
| with_reducible exact KeepsCalls.stop _
| with_reducible exact KeepsCalls.diverge
| with_reducible exact KeepsCalls.read
| with_reducible exact KeepsCalls.get
| assumption
| with_reducible apply KeepsCalls.ite
| with_reducible apply KeepsCalls.dite
| with_reducible apply KeepsCalls.unwind
| with_reducible apply KeepsCalls.bind
| intro (_ : _))
end Fe.EVM