Repository navigation
Expand file tree
/
Copy pathExported.lean
More file actions
2674 lines (2294 loc) · 86.3 KB
/
Copy pathExported.lean
File metadata and controls
2674 lines (2294 loc) · 86.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
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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import FeProofs.Lib.Exported
/-!
# Exporter fixtures
Output of `fe-lean-export` on fixtures that have no bytecode proof (Fe branch `lean-export`,
`crates/lean-export/tests/golden/`), copied in by `scripts/sync_exported.py`. The core and std
definitions the fixtures call are in `FeProofs/Lib/Exported.lean`. This file checks that the
output elaborates, and that the control flow it prints means what the source says.
-/
open Ethereum Ethereum.EVM Fe Fe.EVM
/-! ## `helper_call`: a helper that takes contract state through `uses` -/
namespace FeProofs.Fixtures.HelperCall
section
set_option linter.unusedVariables false
-- BEGIN fe-lean-export: helper_call
export Lib (StorageMap_locate_u256_u256)
def Field.store : Place :=
.storage (UInt256.ofNat 0)
def Layout.Store : Layout :=
{ storage := [0, 1] }
/-- `store.total` -/
abbrev Field.store.total : Place :=
Field.store.field Layout.Store 0
/-- `store.hits` -/
abbrev Field.store.hits : Place :=
Field.store.field Layout.Store 1
/-- The `init` of contract `Counter`, which its source leaves out -/
def init : FeM Unit := do
return ()
/-- `helper_call::add` -/
def add («by» : UInt256) : FeM UInt256 := do
-- let next = store.total + by
let v1 ← load Field.store.total
let next ← checkedAdd v1 «by»
-- store.total = next
store Field.store.total next
-- store.hits[by] += 1
let hits_by ← StorageMap_locate_u256_u256 Field.store.hits Field.store.hits.slot «by»
let v8 ← load (Place.mk .storage hits_by.slot)
let v9 ← checkedAdd v8 (UInt256.ofNat 1)
let hits_by_1 ← StorageMap_locate_u256_u256 Field.store.hits Field.store.hits.slot «by»
store (Place.mk .storage hits_by_1.slot) v9
return next
/-- `Bump { by } -> u256`, selector `0xb20eb4c4` (`bump(uint256)`) -/
def recv_Bump («by» : UInt256) : FeM UInt256 := do
-- add(by)
let v1 ← add «by»
return v1
-- END fe-lean-export
end
end FeProofs.Fixtures.HelperCall
/-! ## `join`: an `if` expression whose branches merge -/
namespace FeProofs.Fixtures.Join
section
set_option linter.unusedVariables false
-- BEGIN fe-lean-export: join
export Lib (StorageMap_locate_u256_u256)
def Field.store : Place :=
.storage (UInt256.ofNat 0)
def Layout.Store : Layout :=
{ storage := [0, 1] }
/-- `store.total` -/
abbrev Field.store.total : Place :=
Field.store.field Layout.Store 0
/-- `store.hits` -/
abbrev Field.store.hits : Place :=
Field.store.field Layout.Store 1
/-- The `init` of contract `Counter`, which its source leaves out -/
def init : FeM Unit := do
return ()
/-- `join::add` -/
def add («by» : UInt256) : FeM UInt256 := do
let join : UInt256 → FeM UInt256 := fun next => do
-- store.total = next
store Field.store.total next
-- store.hits[by] += 1
let hits_by ← StorageMap_locate_u256_u256 Field.store.hits Field.store.hits.slot «by»
let v17 ← load (Place.mk .storage hits_by.slot)
let v18 ← checkedAdd v17 (UInt256.ofNat 1)
let hits_by_1 ← StorageMap_locate_u256_u256 Field.store.hits Field.store.hits.slot «by»
store (Place.mk .storage hits_by_1.slot) v18
return next
-- let next = if by > 10 { store.total + 10 } else { store.total + by }
if decide ((UInt256.ofNat 10).toNat < «by».toNat) then
-- store.total + 10
let v5 ← load Field.store.total
let v7 ← checkedAdd v5 (UInt256.ofNat 10)
join v7
else
-- store.total + by
let v9 ← load Field.store.total
let v11 ← checkedAdd v9 «by»
join v11
/-- `Bump { by } -> u256`, selector `0xb20eb4c4` (`bump(uint256)`) -/
def recv_Bump («by» : UInt256) : FeM UInt256 := do
-- add(by)
let v1 ← add «by»
return v1
-- END fe-lean-export
end
/-- `add` of the fixture, written with the `if` as an expression. The entry of `hits` is
where std's exported `locate` puts it:
```fe
let next = if by > 10 { store.total + 10 } else { store.total + by }
store.total = next
store.hits[by] += 1
next
```
-/
def addSpec (amount : UInt256) : FeM UInt256 := do
let total ← load (Field.store.field Layout.Store 0)
let next ← checkedAdd total (if 10 < amount.toNat then UInt256.ofNat 10 else amount)
store (Field.store.field Layout.Store 0) next
let hits := Field.store.field Layout.Store 1
let entry ← StorageMap_locate_u256_u256 hits hits.slot amount
let count ← checkedAdd (← load ⟨.storage, entry.slot⟩) (UInt256.ofNat 1)
let entry ← StorageMap_locate_u256_u256 hits hits.slot amount
store ⟨.storage, entry.slot⟩ count
return next
/-- The exported join block runs after either branch, with that branch's value. -/
theorem add_exec (amount : UInt256) (I : ExecutionEnv) (w : World) :
(add amount).exec I w = (addSpec amount).exec I w := by
have h10 : (UInt256.ofNat 10).toNat = 10 := rfl
by_cases h : 10 < amount.toNat <;> simp [add, addSpec, h, h10]
end FeProofs.Fixtures.Join
/-! ## `local_struct`: struct values in mutable locals, passed by `mut` -/
namespace FeProofs.Fixtures.LocalStruct
section
set_option linter.unusedVariables false
-- BEGIN fe-lean-export: local_struct
structure Cursor where
pos : UInt256
base : UInt256
deriving Inhabited
structure Span where
start : Cursor
len : UInt256
deriving Inhabited
def Field.saved : Place :=
.storage (UInt256.ofNat 0)
def Layout.Cursor : Layout :=
{ storage := [0, 1] }
/-- The `init` of contract `Walker`, which its source leaves out -/
def init : FeM Unit := do
return ()
/-- `Cursor::advance` -/
def Cursor_advance (self : Cursor) («by» : UInt256) : FeM (UInt256 × Cursor) := do
let mut self := self
-- let at = self.pos
let «at» := self.pos
-- self.pos = at + by
let v5 ← checkedAdd «at» «by»
self := { self with pos := v5 }
return («at», self)
/-- `Cursor::skip` -/
def Cursor_skip (self : Cursor) («by» : UInt256) : FeM (Unit × Cursor) := do
let mut self := self
let join : Cursor → FeM (Unit × Cursor) := fun self => do
let mut self := self
-- self.base = self.pos
self := { self with base := self.pos }
return ((), self)
-- if by > 32 {
if decide ((UInt256.ofNat 32).toNat < «by».toNat) then
-- self.advance(by: 32)
let (v6, v6_0) ← Cursor_advance self (UInt256.ofNat 32)
self := v6_0
join self
else
-- self.advance(by)
let (v8, v8_0) ← Cursor_advance self «by»
self := v8_0
join self
/-- `Cursor::end` -/
def Cursor_end (self : Cursor) : FeM UInt256 := do
-- self.pos + self.base
let v3 ← checkedAdd self.pos self.base
return v3
/-- `Sum { step } -> u256`, selector `0x188b85b4` (`sum(uint256)`) -/
def recv_Sum (step : UInt256) : FeM UInt256 := do
let mut c : Cursor := default
let mut s : Span := default
-- var c = Cursor { pos: 0, base: 4 }
c := (Cursor.mk (UInt256.ofNat 0) (UInt256.ofNat 4))
-- let a = c.advance(by: step)
let (a, a_0) ← Cursor_advance c step
c := a_0
-- c.skip(by: step)
let (v9, v9_0) ← Cursor_skip c step
c := v9_0
-- var s = Span { start: c, len: step }
s := (Span.mk c step)
-- let b = s.start.advance(by: s.len)
let v14 := s.len
let (b, b_0) ← Cursor_advance s.start v14
s := { s with start := b_0 }
-- a + b + s.start.end()
let v18 ← checkedAdd a b
let v21 ← Cursor_end s.start
let v22 ← checkedAdd v18 v21
return v22
/-- `Cursor::advance` -/
def Cursor_advance_1 (self : Place) («by» : UInt256) : FeM UInt256 := do
-- let at = self.pos
let «at» ← load (self.field Layout.Cursor 0)
-- self.pos = at + by
let v5 ← checkedAdd «at» «by»
store (self.field Layout.Cursor 0) v5
return «at»
/-- `Bump { step } -> u256`, selector `0xb20eb4c4` (`bump(uint256)`) -/
def recv_Bump (step : UInt256) : FeM UInt256 := do
-- saved.advance(by: step)
let v1 := Field.saved
let v2 ← Cursor_advance_1 v1 step
return v2
-- END fe-lean-export
end
/-- A `mut self` method on a local takes the struct and returns it updated. -/
theorem advance_exec (c : Cursor) (amount : UInt256) (I : ExecutionEnv) (w : World) :
(Cursor_advance c amount).exec I w =
(do let pos ← checkedAdd c.pos amount
pure (c.pos, { c with pos := pos })).exec I w := by
simp [Cursor_advance]
/-- The `Sum` arm of the fixture, with each call replaced by what it does to the cursor:
```fe
var c = Cursor { pos: 0, base: 4 }
let a = c.advance(by: step)
c.skip(by: step)
var s = Span { start: c, len: step }
let b = s.start.advance(by: s.len)
a + b + s.start.end()
```
-/
def sumSpec (step : UInt256) : FeM UInt256 := do
let first ← checkedAdd (UInt256.ofNat 0) step
let skipped ← checkedAdd first (if 32 < step.toNat then UInt256.ofNat 32 else step)
let last ← checkedAdd skipped step
let ab ← checkedAdd (UInt256.ofNat 0) skipped
let «end» ← checkedAdd last skipped
checkedAdd ab «end»
/-- Writes through `mut` accesses to a local, to a field of a local, and across a join reach
the reads after them. -/
theorem sum_exec (step : UInt256) (I : ExecutionEnv) (w : World) :
(recv_Sum step).exec I w = (sumSpec step).exec I w := by
have h32 : (UInt256.ofNat 32).toNat = 32 := rfl
by_cases h : 32 < step.toNat <;> simp [recv_Sum, Cursor_advance, Cursor_skip, Cursor_end, sumSpec, h, h32]
end FeProofs.Fixtures.LocalStruct
/-! ## `tuple`: tuple values, projections and a write to one field -/
namespace FeProofs.Fixtures.Tuple
section
set_option linter.unusedVariables false
-- BEGIN fe-lean-export: tuple
/-- The `init` of contract `Pairs`, which its source leaves out -/
def init : FeM Unit := do
return ()
/-- `tuple::swap` -/
def swap (pair : (UInt256 × UInt256 × Bool)) : FeM (UInt256 × UInt256 × Bool) := do
return (pair.snd.fst, pair.fst, pair.snd.snd)
/-- `Swap { a, b } -> u256`, selector `0xd96073cf` (`swap(uint256,uint256)`) -/
def recv_Swap (a : UInt256) (b : UInt256) : FeM UInt256 := do
let mut pair : (UInt256 × UInt256 × Bool) := default
let mut r1 : (UInt256 × UInt256 × Bool) := default
-- var pair = swap(pair: (a, b, true))
r1 := (a, b, true)
let pair_6 ← swap r1
pair := pair_6
-- pair.1 = pair.1 + 1
let v10 ← checkedAdd pair.snd.fst (UInt256.ofNat 1)
pair := { pair with snd := { pair.snd with fst := v10 } }
let join : (UInt256 × UInt256 × Bool) → (UInt256 × UInt256 × Bool) → UInt256 → FeM UInt256 := fun pair r1 v2 => do
let mut pair := pair
let mut r1 := r1
return v2
-- if pair.2 {
if pair.snd.snd then
-- pair.0 - pair.1
let v15 ← checkedSub pair.fst pair.snd.fst
join pair r1 v15
else
join pair r1 (UInt256.ofNat 0)
-- END fe-lean-export
end
/-- A tuple is a right-nested product: field 1 of three is `.snd.fst`. -/
theorem swap_eq (a b : UInt256) (flag : Bool) : swap (a, b, flag) = pure (b, a, flag) := rfl
/-- `var pair = swap((a, b, true)); pair.1 = pair.1 + 1; pair.0 - pair.1` -/
theorem swap_arm_exec (a b : UInt256) (I : ExecutionEnv) (w : World) :
(recv_Swap a b).exec I w =
(do let next ← checkedAdd a (UInt256.ofNat 1)
checkedSub b next).exec I w := by
simp [recv_Swap, swap]
end FeProofs.Fixtures.Tuple
/-! ## `param_slot`: owned parameters the body assigns or borrows -/
namespace FeProofs.Fixtures.ParamSlot
section
set_option linter.unusedVariables false
-- BEGIN fe-lean-export: param_slot
structure Cursor where
pos : UInt256
base : UInt256
deriving Inhabited
/-- The `init` of contract `Walker`, which its source leaves out -/
def init : FeM Unit := do
return ()
/-- `param_slot::make` -/
def make (pos : UInt256) : FeM Cursor := do
return Cursor.mk pos (UInt256.ofNat 4)
/-- `Cursor::end` -/
def Cursor_end (self : Cursor) : FeM UInt256 := do
-- self.pos + self.base
let v3 ← checkedAdd self.pos self.base
return v3
/-- `Cursor::advance` -/
def Cursor_advance (self : Cursor) («by» : UInt256) : FeM (UInt256 × Cursor) := do
let mut self := self
-- let at = self.pos
let «at» := self.pos
-- self.pos = at + by
let v5 ← checkedAdd «at» «by»
self := { self with pos := v5 }
return («at», self)
/-- `param_slot::c_end` -/
def c_end (c : Cursor) : FeM UInt256 := do
-- c.end()
let v1 ← Cursor_end c
return v1
/-- `param_slot::consume` -/
def consume (c : Cursor) («by» : UInt256) : FeM UInt256 := do
let mut c := c
let mut r1 : Cursor := default
-- c.advance(by) + c_end(c: make(pos: by))
let (v2, v2_0) ← Cursor_advance c «by»
c := v2_0
let v3 ← make «by»
r1 := v3
let v5 ← c_end r1
let v6 ← checkedAdd v2 v5
return v6
/-- `param_slot::bump` -/
def bump («by» : UInt256) : FeM UInt256 := do
let mut «by» := «by»
-- by += 1
let by_2 ← checkedAdd «by» (UInt256.ofNat 1)
«by» := by_2
return «by»
/-- `Sum { step } -> u256`, selector `0x188b85b4` (`sum(uint256)`) -/
def recv_Sum (step : UInt256) : FeM UInt256 := do
let mut r0 : Cursor := default
-- make(pos: step).end() + consume(make(pos: 1), by: step) + bump(by: step)
let v1 ← make step
r0 := v1
let v3 ← Cursor_end r0
let v5 ← make (UInt256.ofNat 1)
let v6 ← consume v5 step
let v7 ← checkedAdd v3 v6
let v8 ← bump step
let v9 ← checkedAdd v7 v8
return v9
-- END fe-lean-export
end
/-- A `var` parameter is a slot that starts out holding the argument:
`fn bump(var by: own u256) -> u256 { by += 1; by }`. -/
theorem bump_exec (amount : UInt256) (I : ExecutionEnv) (w : World) :
(bump amount).exec I w = (checkedAdd amount (UInt256.ofNat 1)).exec I w := by
simp [bump]
/-- `c.advance(by) + c_end(c: make(pos: by))` on an owned `c`. -/
theorem consume_exec (c : Cursor) (amount : UInt256) (I : ExecutionEnv) (w : World) :
(consume c amount).exec I w =
(do let _ ← checkedAdd c.pos amount
let «end» ← checkedAdd amount (UInt256.ofNat 4)
checkedAdd c.pos «end»).exec I w := by
simp [consume, Cursor_advance, make, c_end, Cursor_end]
end FeProofs.Fixtures.ParamSlot
/-! ## `decode`: the std calldata decoder, `evm.decode_args<(u256, u256)>()` -/
namespace FeProofs.Fixtures.Decode
section
set_option linter.unusedVariables false
-- BEGIN fe-lean-export: decode
export Lib (Evm_decode_args_tuple_u256_u256)
/-- The `init` of contract `Decoder`, which its source leaves out -/
def init : FeM Unit := do
return ()
/-- `Probe -> u256`, selector `0xb74af5a9` (`probe()`) -/
def recv_Probe : FeM UInt256 := do
-- let args: (u256, u256) = evm.decode_args()
let args ← Evm_decode_args_tuple_u256_u256
-- args.0 + args.1
let v5 ← checkedAdd args.fst args.snd
return v5
-- END fe-lean-export
end
end FeProofs.Fixtures.Decode
/-! ## `encode`: the std ABI encoder, `revert(value: u256)` -/
namespace FeProofs.Fixtures.Encode
section
set_option linter.unusedVariables false
-- BEGIN fe-lean-export: encode
export Lib (revert_u256)
/-- The `init` of contract `Encoder`, which its source leaves out -/
def init : FeM Unit := do
return ()
/-- `Probe { value } -> u256`, selector `0xdb082440` (`probe(uint256)`) -/
def recv_Probe (value : UInt256) : FeM UInt256 := do
-- revert(value)
let v1 ← revert_u256 value
let v1 : UInt256 := nomatch v1
return v1
-- END fe-lean-export
end
end FeProofs.Fixtures.Encode
/-! ## `ints`: integers of every width, and the call's context -/
namespace FeProofs.Fixtures.Ints
section
set_option linter.unusedVariables false
-- BEGIN fe-lean-export: ints
export Lib (Address_word Evm_address Evm_block_number Evm_origin Evm_value Wei Wei_as_wei downcast_truncate_i256_i8 downcast_unchecked_u256_i256)
/-- The `init` of contract `Ints`, which its source leaves out -/
def init : FeM Unit := do
return ()
/-- `Narrow { a, b } -> u8`, selector `0x741ef9d5` (`narrow(uint8,uint8)`) -/
def recv_Narrow (a : UInt256) (b : UInt256) : FeM UInt256 := do
-- let sum = a + b
let sum ← IntTy.add (.u 8) a b
-- (sum - low) * 2 / b % 7
let v9 ← IntTy.sub (.u 8) sum ((sum >>> (UInt256.ofNat 1)) &&& (UInt256.ofNat 15))
let v11 ← IntTy.mul (.u 8) v9 (UInt256.ofNat 2)
let v12 ← checkedDiv v11 b
let v14 ← checkedRem v12 (UInt256.ofNat 7)
return v14
/-- `ints::wrapping` -/
def wrapping (a : UInt256) (b : UInt256) : FeM UInt256 := do
-- (a * b) << 3
let v6 := IntTy.shl (.i 32) (IntTy.mulW (.i 32) a b) (UInt256.ofNat 3)
return v6
/-- `Signed { a, b } -> i32`, selector `0x6effafad` (`signed(int32,int32)`) -/
def recv_Signed (a : UInt256) (b : UInt256) : FeM UInt256 := do
-- if a < b {
if UInt256.sltBool a b then
-- return -a
let v3 ← IntTy.neg (.i 32) a
return v3
else
-- wrapping(a, b) + (a / b) - (a % b) + -5
let v6 ← wrapping a b
let v7 ← IntTy.div (.i 32) a b
let v8 ← IntTy.add (.i 32) v6 v7
let v9 ← IntTy.rem (.i 32) a b
let v10 ← IntTy.sub (.i 32) v8 v9
let v12 ← IntTy.add (.i 32) v10 (IntTy.wrap (.i 256) (-5))
return v12
/-- `Mixed { a, b } -> i256`, selector `0x728ac8e2` (`mixed(int256,uint16)`) -/
def recv_Mixed (a : UInt256) (b : UInt256) : FeM UInt256 := do
-- let small: i8 = a.downcast_truncate()
let small ← downcast_truncate_i256_i8 a
-- let bits: i256 = wide.downcast_unchecked()
let bits ← downcast_unchecked_u256_i256 b
-- (a >> 2) ** 2 + (small as i256) + bits + (!a)
let v11 ← IntTy.pow (.i 256) (IntTy.shr (.i 256) a (UInt256.ofNat 2)) (UInt256.ofNat 2)
let v13 ← IntTy.add (.i 256) v11 small
let v14 ← IntTy.add (.i 256) v13 bits
let v16 ← IntTy.add (.i 256) v14 (IntTy.not (.i 256) a)
return v16
/-- `Context -> u256`, selector `0xd0496d6a` (`context()`) -/
def recv_Context : FeM UInt256 := do
let mut r1 : UInt256 := default
let mut r2 : Wei := default
let mut r3 : UInt256 := default
-- evm.address().word() ^ evm.value().as_wei() ^ evm.origin().word() ^ evm.block_number()
let v1 ← Evm_address
r1 := v1
let v3 ← Address_word r1
let v5 ← Evm_value
r2 := v5
let v7 ← Wei_as_wei r2
let v10 ← Evm_origin
r3 := v10
let v12 ← Address_word r3
let v15 ← Evm_block_number
return ((v3 ^^^ v7) ^^^ v12) ^^^ v15
-- END fe-lean-export
end
end FeProofs.Fixtures.Ints
/-! ## `enums`: enum values, `match`, and a write through a variant's field -/
namespace FeProofs.Fixtures.Enums
section
set_option linter.unusedVariables false
-- BEGIN fe-lean-export: enums
export Lib (Option_u256 Option_u256.None Option_u256.Some)
inductive Shape where
| Empty
| Square (f0 : UInt256)
| Rect (f0 : UInt256) (f1 : UInt256)
deriving Inhabited
def Shape.tag : Shape → UInt256
| .Empty => UInt256.ofNat 0
| .Square .. => UInt256.ofNat 1
| .Rect .. => UInt256.ofNat 2
def Shape.Square_0 : Shape → UInt256
| .Square f0 => f0
| _ => default
def Shape.set_Square_0 (x : Shape) (v : UInt256) : Shape :=
match x with
| .Square f0 => .Square v
| x => x
def Shape.Rect_0 : Shape → UInt256
| .Rect f0 f1 => f0
| _ => default
def Shape.set_Rect_0 (x : Shape) (v : UInt256) : Shape :=
match x with
| .Rect f0 f1 => .Rect v f1
| x => x
def Shape.Rect_1 : Shape → UInt256
| .Rect f0 f1 => f1
| _ => default
def Shape.set_Rect_1 (x : Shape) (v : UInt256) : Shape :=
match x with
| .Rect f0 f1 => .Rect f0 v
| x => x
/-- The `init` of contract `Enums`, which its source leaves out -/
def init : FeM Unit := do
return ()
/-- `enums::shape` -/
def shape (width : UInt256) (height : UInt256) : FeM Shape := do
let join : Shape → FeM Shape := fun v2 => do
return v2
-- if width == 0 {
if decide (width = (UInt256.ofNat 0)) then
join Shape.Empty
else
let join_1 : Shape → FeM Shape := fun v3 => do
join v3
if decide (width = height) then
join_1 (Shape.Square width)
else
join_1 (Shape.Rect width height)
/-- `Area { width, height } -> u256`, selector `0x15529a52` (`area(uint256,uint256)`) -/
def recv_Area (width_0 : UInt256) (height_1 : UInt256) : FeM UInt256 := do
-- match shape(width, height) {
let v3 ← shape width_0 height_1
let join : UInt256 → FeM UInt256 := fun v2 => do
return v2
match v3 with
| .Empty =>
join (UInt256.ofNat 0)
| .Square .. =>
let side := v3.Square_0
let v8 ← checkedMul side side
join v8
| .Rect .. =>
let v14 ← checkedMul v3.Rect_0 v3.Rect_1
join v14
/-- `enums::halve` -/
def halve (value : UInt256) : FeM Option_u256 := do
-- if value % 2 == 0 {
let v4 ← checkedRem value (UInt256.ofNat 2)
let join : Option_u256 → FeM Option_u256 := fun v1 => do
return v1
if decide (v4 = (UInt256.ofNat 0)) then
-- Option::Some(value / 2)
let v9 ← checkedDiv value (UInt256.ofNat 2)
join (Option_u256.Some v9)
else
join Option_u256.None
/-- `Half { value } -> u256`, selector `0xdee6088b` (`half(uint256)`) -/
def recv_Half (value : UInt256) : FeM UInt256 := do
-- match halve(value) {
let v2 ← halve value
let join : UInt256 → FeM UInt256 := fun v1 => do
return v1
match v2 with
| .Some .. =>
join v2.Some_0
| _ =>
join value
/-- `Grow { side } -> u256`, selector `0x28531f06` (`grow(uint256)`) -/
def recv_Grow (side_0 : UInt256) : FeM UInt256 := do
let mut current : Shape := default
-- var current = Shape::Square(side)
current := (Shape.Square side_0)
let join : Shape → FeM UInt256 := fun current => do
let mut current := current
let join_1 : Shape → UInt256 → FeM UInt256 := fun current v1 => do
let mut current := current
return v1
-- match current {
match current with
| .Square .. =>
join_1 current current.Square_0
| _ =>
join_1 current (UInt256.ofNat 0)
-- if let Shape::Square(mut inner) = current {
match current with
| .Square .. =>
-- inner += 1
let v8 ← checkedAdd current.Square_0 (UInt256.ofNat 1)
current := (current.set_Square_0 v8)
join current
| _ =>
join current
-- END fe-lean-export
end
/-- `area` multiplies the sides of whichever shape the arguments make. -/
theorem area_exec (width height : UInt256) (I : ExecutionEnv) (w : World) :
(recv_Area width height).exec I w =
if width = UInt256.ofNat 0 then some (.ok (UInt256.ofNat 0, w))
else (checkedMul width height).exec I w := by
by_cases h0 : width = UInt256.ofNat 0
· simp [recv_Area, shape, h0]
· by_cases hs : width = height
· subst hs
simp [recv_Area, shape, Shape.Square_0, h0]
· simp [recv_Area, shape, Shape.Rect_0, Shape.Rect_1, h0, hs]
/-- `grow` adds one to the side through the variant's field. -/
theorem grow_exec (side : UInt256) (I : ExecutionEnv) (w : World) :
(recv_Grow side).exec I w = (checkedAdd side (UInt256.ofNat 1)).exec I w := by
simp [recv_Grow, Shape.Square_0, Shape.set_Square_0, checkedAdd]
rfl
end FeProofs.Fixtures.Enums
/-! ## `loops`: a loop is a recursive local function, the least fixed point of its body -/
namespace FeProofs.Fixtures.Loops
section
set_option linter.unusedVariables false
-- BEGIN fe-lean-export: loops
export Lib (Option_u256 Option_u256.None Option_u256.Some)
/-- The `init` of contract `Loops`, which its source leaves out -/
def init : FeM Unit := do
return ()
/-- `Triangle { n } -> u256`, selector `0x50c2698d` (`triangle(uint256)`) -/
def recv_Triangle (n : UInt256) : FeM UInt256 := do
let mut total : UInt256 := default
let mut i : UInt256 := default
-- var total: u256 = 0
total := (UInt256.ofNat 0)
-- var i: u256 = 0
i := (UInt256.ofNat 0)
let rec loop (total : UInt256) (i : UInt256) : FeM UInt256 := do
let mut total := total
let mut i := i
-- while i < n {
if decide (i.toNat < n.toNat) then
-- i += 1
let i_10 ← checkedAdd i (UInt256.ofNat 1)
i := i_10
-- total += i
let total_16 ← checkedAdd total i
total := total_16
loop total i
else
return total
partial_fixpoint
loop total i
/-- `loops::step` -/
def step (value : UInt256) : FeM Option_u256 := do
let join : Option_u256 → FeM Option_u256 := fun v1 => do
return v1
-- if value == 1 {
if decide (value = (UInt256.ofNat 1)) then
join Option_u256.None
else
let v10 ← checkedRem value (UInt256.ofNat 2)
let join_1 : Option_u256 → FeM Option_u256 := fun v2 => do
join v2
if decide (v10 = (UInt256.ofNat 0)) then
-- Option::Some(value / 2)
let v15 ← checkedDiv value (UInt256.ofNat 2)
join_1 (Option_u256.Some v15)
else
-- Option::Some(3 * value + 1)
let v20 ← checkedMul (UInt256.ofNat 3) value
let v22 ← checkedAdd v20 (UInt256.ofNat 1)
join_1 (Option_u256.Some v22)
/-- `Collatz { start, limit } -> u256`, selector `0xea732925` (`collatz(uint256,uint256)`) -/
def recv_Collatz (start : UInt256) (limit : UInt256) : FeM UInt256 := do
let mut value : UInt256 := default
let mut steps : UInt256 := default
-- var value = start
value := start
-- var steps: u256 = 0
steps := (UInt256.ofNat 0)
let rec loop (value : UInt256) (steps : UInt256) : FeM UInt256 := do
let mut value := value
let mut steps := steps
-- if steps == limit {
if decide (steps = limit) then
return limit
else
-- match step(value) {
let v12 ← step value
match (← pure v12) with
| .Some .. =>
-- value = next
value := v12.Some_0
-- steps += 1
let steps_19 ← checkedAdd steps (UInt256.ofNat 1)
steps := steps_19
loop value steps
| .None =>
return steps
partial_fixpoint
loop value steps
-- END fe-lean-export
end
/-- `triangle(3)` runs the loop body three times and returns `1 + 2 + 3`. -/
theorem triangle_three (I : ExecutionEnv) (w : World) :
(recv_Triangle (UInt256.ofNat 3)).exec I w = some (.ok (UInt256.ofNat 6, w)) := by
simp [recv_Triangle]
iterate 4 (rw [recv_Triangle.loop]; simp +decide [checkedAdd])
end FeProofs.Fixtures.Loops
/-! ## `nested_loops`: loops in loops, and joins on the recursion of a loop
A join that an inner loop's exit enters from outside the inner loop, and that leads back to the
outer loop's head (`windows`: the shape of `small_power` in fevm's `modexp.fe`), is a recursive
local function of its own. The values below are the source's, worked by hand; the export audit
compares the model with the bytecode on generated calls. -/
namespace FeProofs.Fixtures.NestedLoops
section
set_option linter.unusedVariables false
-- BEGIN fe-lean-export: nested_loops
export Lib (Range_Known_0_Unknown Range_Known_0_Unknown.mk Range_Unknown_Unknown Range_Unknown_Unknown.mk Range_Unknown_Unknown_at Range_Unknown_Unknown_has Range_Unknown_Unknown_next Range_Unknown_Unknown_start Range_at_0 Range_has_0 Range_next_0 Range_start_0 downcast_truncate_u256_usize)
/-- The `init` of contract `NestedLoops`, which its source leaves out -/
def init : FeM Unit := do
return ()
/-- `nested_loops::windows` -/
def windows (n : UInt256) (width : UInt256) : FeM UInt256 := do
let mut acc : UInt256 := default
let mut index : UInt256 := default
let mut started : Bool := default
let mut r3 : Range_Unknown_Unknown := default
-- var acc: u256 = 1
acc := (UInt256.ofNat 1)
-- var index: usize = 0
index := (UInt256.ofNat 0)
-- var started = false
started := false
let rec loop (acc : UInt256) (index : UInt256) (started : Bool) (r3 : Range_Unknown_Unknown) : FeM UInt256 := do
let mut acc := acc
let mut index := index
let mut started := started
let mut r3 := r3
-- while index < n {
if decide (index.toNat < n.toNat) then
-- let end = if index + width + 1 < n { index + width + 1 } else { n }
let v17 ← checkedAdd index width
let v19 ← checkedAdd v17 (UInt256.ofNat 1)
let join : UInt256 → UInt256 → Bool → Range_Unknown_Unknown → UInt256 → FeM UInt256 := fun acc index started r3 «end» => do
let mut acc := acc
let mut index := index
let mut started := started
let mut r3 := r3
let rec loop_1 (acc : UInt256) (index : UInt256) (started : Bool) (r3 : Range_Unknown_Unknown) : FeM UInt256 := do
let mut acc := acc
let mut index := index
let mut started := started
let mut r3 := r3
-- if end % 2 == 1 {
let v60 ← checkedRem «end» (UInt256.ofNat 2)
let join_1 : UInt256 → UInt256 → Bool → Range_Unknown_Unknown → FeM UInt256 := fun acc index started r3 => do
let mut acc := acc
let mut index := index
let mut started := started
let mut r3 := r3
-- index = end
index := «end»
loop acc index started r3
if decide (v60 = (UInt256.ofNat 1)) then
-- acc = acc + end as u256
let acc_66 ← checkedAdd acc «end»
acc := acc_66
-- started = true
started := true
join_1 acc index started r3
else
join_1 acc index started r3
partial_fixpoint
-- if started {
if started then
-- for _ in index..end { acc = acc * 2 % 1000003 }
r3 := (Range_Unknown_Unknown.mk index «end»)
let v38 ← Range_Unknown_Unknown_start r3
let rec loop_2 (acc : UInt256) (index : UInt256) (started : Bool) (r3 : Range_Unknown_Unknown) (v3 : UInt256) : FeM UInt256 := do
let mut acc := acc
let mut index := index
let mut started := started
let mut r3 := r3
-- for _ in index..end { acc = acc * 2 % 1000003 }
let v41 ← Range_Unknown_Unknown_has r3 v3
if v41 then
let _ ← Range_Unknown_Unknown_at r3 v3
-- acc = acc * 2 % 1000003
let v47 ← checkedMul acc (UInt256.ofNat 2)
let acc_49 ← checkedRem v47 (UInt256.ofNat 1000003)
acc := acc_49
-- for _ in index..end { acc = acc * 2 % 1000003 }
let v53 ← Range_Unknown_Unknown_next r3 v3
loop_2 acc index started r3 v53
else
loop_1 acc index started r3
partial_fixpoint
loop_2 acc index started r3 v38
else
loop_1 acc index started r3
if decide (v19.toNat < n.toNat) then
-- index + width + 1
let v25 ← checkedAdd index width
let v27 ← checkedAdd v25 (UInt256.ofNat 1)
join acc index started r3 v27
else
join acc index started r3 n
else
return acc
partial_fixpoint
loop acc index started r3
/-- `Windows { n, width } -> u256`, selector `0x6a39ed49` (`windows(uint256,uint256)`) -/
def recv_Windows (n : UInt256) (width : UInt256) : FeM UInt256 := do
-- windows(n.downcast_truncate(), width.downcast_truncate())
let v2 ← downcast_truncate_u256_usize n
let v3 ← downcast_truncate_u256_usize width
let v4 ← windows v2 v3
return v4
/-- `nested_loops::break_both` -/
def break_both (n : UInt256) : FeM UInt256 := do
let mut i : UInt256 := default
let mut found : UInt256 := default
let mut done : Bool := default
let mut j : UInt256 := default
-- var i: u256 = 0
i := (UInt256.ofNat 0)
-- var found: u256 = 0
found := (UInt256.ofNat 0)
-- var done = false
done := false
let rec loop (i : UInt256) (found : UInt256) (done : Bool) (j : UInt256) : FeM UInt256 := do
let mut i := i
let mut found := found