-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathContract.lean
More file actions
1961 lines (1898 loc) · 84.3 KB
/
Contract.lean
File metadata and controls
1961 lines (1898 loc) · 84.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 Compiler.Proofs.IRGeneration.Dispatch
set_option linter.unnecessarySimpa false
namespace Compiler.Proofs.IRGeneration
open Compiler
open Compiler.CompilationModel
open Compiler.Yul
namespace Contract
private theorem pickUniqueFunctionByName_eq_ok_none_of_absent
(name : String) (funcs : List FunctionSpec)
(habsent : ∀ fn ∈ funcs, fn.name != name) :
pickUniqueFunctionByName name funcs = Except.ok none := by
induction funcs with
| nil =>
rfl
| cons fn rest ih =>
have hfn : (fn.name == name) = false := by
by_cases heq : fn.name = name
· have habs := habsent fn (by simp)
simp [heq] at habs
· simp [heq]
have hrest : ∀ fn' ∈ rest, fn'.name != name := by
intro fn' hmem
exact habsent fn' (by simp [hmem])
have ih' := ih hrest
simpa [pickUniqueFunctionByName, hfn] using ih'
private theorem compiled_functions_forall₂_of_mapM_ok
(fields : List Field)
(events : List EventDef)
(errors : List ErrorDef) :
∀ (entries : List (FunctionSpec × Nat)) irFns,
(entries.mapM fun (entry : FunctionSpec × Nat) =>
compileFunctionSpec fields events errors [] entry.2 entry.1) = Except.ok irFns →
List.Forall₂
(fun (entry : FunctionSpec × Nat) irFn =>
compileFunctionSpec fields events errors [] entry.2 entry.1 = Except.ok irFn)
entries irFns := by
intro entries
induction entries with
| nil =>
intro irFns hmap
cases hmap
simp
| cons entry entries ih =>
intro irFns hmap
rcases hstep : compileFunctionSpec fields events errors [] entry.2 entry.1 with _ | irFn
· simp only [List.mapM_cons, hstep, bind, Except.bind] at hmap
cases hmap
· rcases htail : List.mapM
(fun (entry : FunctionSpec × Nat) =>
compileFunctionSpec fields events errors [] entry.2 entry.1) entries with _ | irFnsTail
· simp only [List.mapM_cons, hstep, htail, bind, Except.bind] at hmap
cases hmap
· simp only [List.mapM_cons, hstep, htail, bind, Except.bind] at hmap
cases hmap
exact List.Forall₂.cons hstep (ih _ htail)
private theorem compiled_internal_functions_forall₂_of_mapM_ok
(fields : List Field)
(events : List EventDef)
(errors : List ErrorDef) :
∀ (entries : List FunctionSpec) internalDefs,
(entries.mapM (compileInternalFunction fields events errors [])) =
Except.ok internalDefs →
List.Forall₂
(fun fn internalDef =>
compileInternalFunction fields events errors [] fn = Except.ok internalDef)
entries internalDefs := by
intro entries
induction entries with
| nil =>
intro internalDefs hmap
cases hmap
simp
| cons entry entries ih =>
intro internalDefs hmap
rcases hstep : compileInternalFunction fields events errors [] entry with _ | internalDef
· simp only [List.mapM_cons, hstep, bind, Except.bind] at hmap
cases hmap
· rcases htail :
List.mapM (compileInternalFunction fields events errors []) entries with _ | internalDefsTail
· simp only [List.mapM_cons, hstep, htail, bind, Except.bind] at hmap
cases hmap
· simp only [List.mapM_cons, hstep, htail, bind, Except.bind] at hmap
cases hmap
exact List.Forall₂.cons hstep (ih _ htail)
private theorem exists_right_of_forall₂_mem_left
{α β : Type}
{R : α → β → Prop}
{xs : List α}
{ys : List β}
(hrel : List.Forall₂ R xs ys)
{x : α}
(hmem : x ∈ xs) :
∃ y, y ∈ ys ∧ R x y := by
induction hrel with
| nil =>
cases hmem
| @cons headX headY tailX tailY hhead htail ih =>
simp only [List.mem_cons] at hmem
rcases hmem with rfl | hmemTail
· exact ⟨headY, by simp, hhead⟩
· rcases ih hmemTail with ⟨y, hy, hRy⟩
exact ⟨y, by simp [hy], hRy⟩
private theorem legacyCompatibleExternalStmtList_append
(before : List YulStmt)
(after : List YulStmt)
(hbefore : LegacyCompatibleExternalStmtList before)
(hafter : LegacyCompatibleExternalStmtList after) :
LegacyCompatibleExternalStmtList (before ++ after) := by
revert after hafter
induction hbefore with
| nil => intro after hafter; simpa using hafter
| comment msg rest hrest ih =>
intro after hafter; simpa using LegacyCompatibleExternalStmtList.comment msg (rest ++ after) (ih after hafter)
| let_ name value rest hrest ih =>
intro after hafter; simpa using LegacyCompatibleExternalStmtList.let_ name value (rest ++ after) (ih after hafter)
| assign name value rest hrest ih =>
intro after hafter; simpa using LegacyCompatibleExternalStmtList.assign name value (rest ++ after) (ih after hafter)
| expr value rest hrest ih =>
intro after hafter; simpa using LegacyCompatibleExternalStmtList.expr value (rest ++ after) (ih after hafter)
| if_ cond body rest hbody hrest ihBody ihRest =>
intro after hafter
simpa using LegacyCompatibleExternalStmtList.if_ cond body (rest ++ after) hbody (ihRest after hafter)
| block body rest hbody hrest ihBody ihRest =>
intro after hafter
simpa using LegacyCompatibleExternalStmtList.block body (rest ++ after) hbody (ihRest after hafter)
| funcDef name params rets body rest hbody hrest ihBody ihRest =>
intro after hafter
simpa using LegacyCompatibleExternalStmtList.funcDef name params rets body (rest ++ after) hbody (ihRest after hafter)
private theorem legacyCompatibleExternalStmtList_of_exprStmtExprs
(exprs : List YulExpr) :
LegacyCompatibleExternalStmtList (exprs.map YulStmt.expr) := by
induction exprs with
| nil =>
exact LegacyCompatibleExternalStmtList.nil
| cons expr rest ih =>
simpa using LegacyCompatibleExternalStmtList.expr expr (rest.map YulStmt.expr) ih
private theorem legacyCompatibleExternalStmtList_revertWithMessage
(message : String) :
LegacyCompatibleExternalStmtList (CompilationModel.revertWithMessage message) := by
unfold CompilationModel.revertWithMessage
let headerExprs :=
[ YulExpr.call "mstore" [YulExpr.lit 0, YulExpr.hex errorStringSelectorWord]
, YulExpr.call "mstore" [YulExpr.lit 4, YulExpr.lit 32]
, YulExpr.call "mstore"
[YulExpr.lit 36, YulExpr.lit (CompilationModel.bytesFromString message).length]
]
let dataExprs :=
(((CompilationModel.chunkBytes32 (CompilationModel.bytesFromString message)).zipIdx).map
(fun (chunk, idx) =>
let offset := 68 + idx * 32
let word := CompilationModel.wordFromBytes chunk
YulExpr.call "mstore" [YulExpr.lit offset, YulExpr.hex word]))
let revertStmt :=
YulStmt.expr
(YulExpr.call "revert"
[ YulExpr.lit 0
, YulExpr.lit
(68 + (((CompilationModel.bytesFromString message).length + 31) / 32) * 32)
])
simpa [headerExprs, dataExprs, revertStmt, List.append_assoc] using
legacyCompatibleExternalStmtList_append
(before := headerExprs.map YulStmt.expr)
(after := dataExprs.map YulStmt.expr ++ [revertStmt])
(legacyCompatibleExternalStmtList_of_exprStmtExprs headerExprs)
(legacyCompatibleExternalStmtList_append
(before := dataExprs.map YulStmt.expr)
(after := [revertStmt])
(legacyCompatibleExternalStmtList_of_exprStmtExprs dataExprs)
(LegacyCompatibleExternalStmtList.expr
(YulExpr.call "revert"
[ YulExpr.lit 0
, YulExpr.lit
(68 + (((CompilationModel.bytesFromString message).length + 31) / 32) * 32)
])
[]
LegacyCompatibleExternalStmtList.nil))
-- NOTE: The following TYPESIG_SORRY theorems were dead code duplicates of proven versions in
-- GenericInduction.lean (legacyCompatibleExternalStmtList_of_compileSetStorage_ok_of_noPackedFields,
-- _of_compileStmt_ok_letVar, _assignVar, _require, _return, _stop,
-- _of_compileStmt_ok_on_supportedContractSurface, _of_compileStmtList_ok_on_supportedContractSurface)
-- and of proven local versions (_genParamLoadBodyFrom_cons_scalar, _genParamLoadBodyFrom_of_supported).
-- Removed in cleanup commit.
private theorem legacyCompatibleExternalStmtList_genParamLoadBodyFrom_cons_uint256
(loadWord : YulExpr → YulExpr)
(sizeExpr : YulExpr)
(headSize baseOffset : Nat)
(name : String)
(rest : List Param)
(headOffset : Nat)
(hrest :
LegacyCompatibleExternalStmtList
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset rest
(headOffset + paramHeadSize ParamType.uint256))) :
LegacyCompatibleExternalStmtList
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset ({ name := name, ty := ParamType.uint256 } :: rest) headOffset) := by
simpa [CompilationModel.genParamLoadBodyFrom, CompilationModel.genScalarLoad] using
LegacyCompatibleExternalStmtList.let_
name
(loadWord (YulExpr.lit headOffset))
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset rest (headOffset + paramHeadSize ParamType.uint256))
hrest
private theorem legacyCompatibleExternalStmtList_genParamLoadBodyFrom_cons_uint8
(loadWord : YulExpr → YulExpr)
(sizeExpr : YulExpr)
(headSize baseOffset : Nat)
(name : String)
(rest : List Param)
(headOffset : Nat)
(hrest :
LegacyCompatibleExternalStmtList
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset rest
(headOffset + paramHeadSize ParamType.uint8))) :
LegacyCompatibleExternalStmtList
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset ({ name := name, ty := ParamType.uint8 } :: rest) headOffset) := by
simpa [CompilationModel.genParamLoadBodyFrom, CompilationModel.genScalarLoad] using
LegacyCompatibleExternalStmtList.let_
name
(YulExpr.call "and" [loadWord (YulExpr.lit headOffset), YulExpr.lit 255])
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset rest (headOffset + paramHeadSize ParamType.uint8))
hrest
private theorem legacyCompatibleExternalStmtList_genParamLoadBodyFrom_cons_address
(loadWord : YulExpr → YulExpr)
(sizeExpr : YulExpr)
(headSize baseOffset : Nat)
(name : String)
(rest : List Param)
(headOffset : Nat)
(hrest :
LegacyCompatibleExternalStmtList
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset rest
(headOffset + paramHeadSize ParamType.address))) :
LegacyCompatibleExternalStmtList
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset ({ name := name, ty := ParamType.address } :: rest) headOffset) := by
simpa [CompilationModel.genParamLoadBodyFrom, CompilationModel.genScalarLoad] using
LegacyCompatibleExternalStmtList.let_
name
(YulExpr.call "and" [loadWord (YulExpr.lit headOffset), YulExpr.hex addressMask])
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset rest (headOffset + paramHeadSize ParamType.address))
hrest
private theorem legacyCompatibleExternalStmtList_genParamLoadBodyFrom_cons_bytes32
(loadWord : YulExpr → YulExpr)
(sizeExpr : YulExpr)
(headSize baseOffset : Nat)
(name : String)
(rest : List Param)
(headOffset : Nat)
(hrest :
LegacyCompatibleExternalStmtList
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset rest
(headOffset + paramHeadSize ParamType.bytes32))) :
LegacyCompatibleExternalStmtList
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset ({ name := name, ty := ParamType.bytes32 } :: rest) headOffset) := by
simpa [CompilationModel.genParamLoadBodyFrom, CompilationModel.genScalarLoad] using
LegacyCompatibleExternalStmtList.let_
name
(loadWord (YulExpr.lit headOffset))
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset rest (headOffset + paramHeadSize ParamType.bytes32))
hrest
private theorem legacyCompatibleExternalStmtList_genParamLoadBodyFrom_cons_scalar
(loadWord : YulExpr → YulExpr)
(sizeExpr : YulExpr)
(headSize baseOffset : Nat)
(param : Param)
(rest : List Param)
(headOffset : Nat)
(hparam : SupportedExternalParamType param.ty)
(hrest :
LegacyCompatibleExternalStmtList
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset rest
(headOffset + paramHeadSize param.ty))) :
LegacyCompatibleExternalStmtList
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset (param :: rest) headOffset) := by
cases param with
| mk name ty =>
cases ty <;> simp [SupportedExternalParamType] at hparam
case uint256 =>
exact legacyCompatibleExternalStmtList_genParamLoadBodyFrom_cons_uint256
loadWord sizeExpr headSize baseOffset name rest headOffset hrest
case int256 =>
simpa [CompilationModel.genParamLoadBodyFrom, CompilationModel.genScalarLoad] using
LegacyCompatibleExternalStmtList.let_
name
(loadWord (YulExpr.lit headOffset))
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset rest
(headOffset + paramHeadSize ParamType.int256))
hrest
case uint8 =>
exact legacyCompatibleExternalStmtList_genParamLoadBodyFrom_cons_uint8
loadWord sizeExpr headSize baseOffset name rest headOffset hrest
case address =>
exact legacyCompatibleExternalStmtList_genParamLoadBodyFrom_cons_address
loadWord sizeExpr headSize baseOffset name rest headOffset hrest
case bytes32 =>
exact legacyCompatibleExternalStmtList_genParamLoadBodyFrom_cons_bytes32
loadWord sizeExpr headSize baseOffset name rest headOffset hrest
case bool =>
simpa [CompilationModel.genParamLoadBodyFrom, CompilationModel.genScalarLoad] using
LegacyCompatibleExternalStmtList.let_
name
(YulExpr.call "iszero" [YulExpr.call "iszero" [loadWord (YulExpr.lit headOffset)]])
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset rest
(headOffset + paramHeadSize ParamType.bool))
hrest
private theorem legacyCompatibleExternalStmtList_genParamLoadBodyFrom_of_supported
(loadWord : YulExpr → YulExpr)
(sizeExpr : YulExpr)
(headSize baseOffset : Nat)
(params : List Param)
(headOffset : Nat)
(hparams : ∀ param ∈ params, SupportedExternalParamType param.ty) :
LegacyCompatibleExternalStmtList
(CompilationModel.genParamLoadBodyFrom
loadWord sizeExpr headSize baseOffset params headOffset) := by
induction params generalizing headOffset with
| nil =>
exact LegacyCompatibleExternalStmtList.nil
| cons param rest ih =>
have hparam : SupportedExternalParamType param.ty := hparams param (by simp)
have hrest : ∀ other ∈ rest, SupportedExternalParamType other.ty := by
intro other hmem
exact hparams other (by simp [hmem])
exact legacyCompatibleExternalStmtList_genParamLoadBodyFrom_cons_scalar
loadWord sizeExpr headSize baseOffset param rest headOffset hparam (ih _ hrest)
private theorem legacyCompatibleExternalStmtList_genParamLoads_of_supported
(params : List Param)
(hparams : ∀ param ∈ params, SupportedExternalParamType param.ty) :
LegacyCompatibleExternalStmtList (CompilationModel.genParamLoads params) := by
unfold CompilationModel.genParamLoads CompilationModel.genParamLoadsFrom
apply LegacyCompatibleExternalStmtList.if_
· exact LegacyCompatibleExternalStmtList.expr
(YulExpr.call "revert" [YulExpr.lit 0, YulExpr.lit 0])
[]
LegacyCompatibleExternalStmtList.nil
· exact legacyCompatibleExternalStmtList_genParamLoadBodyFrom_of_supported
(loadWord := fun pos => YulExpr.call "calldataload" [pos])
(sizeExpr := YulExpr.call "calldatasize" [])
(headSize := (params.map (fun p => paramHeadSize p.ty)).foldl (· + ·) 0)
(baseOffset := 4)
(params := params)
(headOffset := 4)
hparams
private theorem compileValidatedCore_ok_yields_compiled_functions
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpec model selectors)
(ir : IRContract)
(hcore : compileValidatedCore model selectors = Except.ok ir) :
List.Forall₂
(fun entry irFn =>
compileFunctionSpec model.fields model.events model.errors [] entry.2 entry.1 = Except.ok irFn)
(SourceSemantics.selectorFunctionPairs model selectors)
ir.functions := by
have hfallback :
pickUniqueFunctionByName "fallback" model.functions = Except.ok none :=
pickUniqueFunctionByName_eq_ok_none_of_absent
"fallback" model.functions hSupported.noFallback
have hreceive :
pickUniqueFunctionByName "receive" model.functions = Except.ok none :=
pickUniqueFunctionByName_eq_ok_none_of_absent
"receive" model.functions hSupported.noReceive
unfold compileValidatedCore at hcore
rw [hSupported.normalizedFields,
hSupported.noAdtTypes, hSupported.noEvents, hSupported.noErrors,
hfallback, hreceive] at hcore
simp only [bind, Except.bind, pure, Except.pure] at hcore
rcases hmap :
((model.functions.filter
(fun fn => !fn.isInternal && !isInteropEntrypointName fn.name)).zip selectors).mapM
(fun x => compileFunctionSpec model.fields [] [] [] x.2 x.1) with _ | irFns
· simp [hmap] at hcore
· simp [hmap] at hcore
rcases hinternal :
(model.functions.filter (·.isInternal)).mapM
(compileInternalFunction model.fields [] [] []) with _ | internalFuncDefs
· simp [hinternal] at hcore
· rcases hctor :
compileConstructor model.fields [] [] [] model.constructor with _ | deployStmts
· simp [hinternal, hctor] at hcore
cases hcore
· simp [hinternal, hctor] at hcore
have hfunctions : ir.functions = irFns := by
injection hcore with hir
cases hir
rfl
have hcompiled :
List.Forall₂
(fun (entry : FunctionSpec × Nat) irFn =>
compileFunctionSpec model.fields model.events model.errors [] entry.2 entry.1 = Except.ok irFn)
((model.functions.filter
(fun fn => !fn.isInternal && !isInteropEntrypointName fn.name)).zip selectors)
irFns :=
by
simpa [hSupported.noEvents, hSupported.noErrors] using
(compiled_functions_forall₂_of_mapM_ok model.fields [] [] _ _ hmap)
simpa [SourceSemantics.selectorFunctionPairs, selectorDispatchedFunctions,
hfunctions] using hcompiled
private theorem compileValidatedCore_ok_yields_compiled_functions_except_mapping_writes
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpecExceptMappingWrites model selectors)
(ir : IRContract)
(hcore : compileValidatedCore model selectors = Except.ok ir) :
List.Forall₂
(fun entry irFn =>
compileFunctionSpec model.fields model.events model.errors [] entry.2 entry.1 = Except.ok irFn)
(SourceSemantics.selectorFunctionPairs model selectors)
ir.functions := by
have hfallback :
pickUniqueFunctionByName "fallback" model.functions = Except.ok none :=
pickUniqueFunctionByName_eq_ok_none_of_absent
"fallback" model.functions hSupported.noFallback
have hreceive :
pickUniqueFunctionByName "receive" model.functions = Except.ok none :=
pickUniqueFunctionByName_eq_ok_none_of_absent
"receive" model.functions hSupported.noReceive
unfold compileValidatedCore at hcore
rw [hSupported.normalizedFields,
hSupported.noAdtTypes, hSupported.noEvents, hSupported.noErrors,
hfallback, hreceive] at hcore
simp only [bind, Except.bind, pure, Except.pure] at hcore
rcases hmap :
((model.functions.filter
(fun fn => !fn.isInternal && !isInteropEntrypointName fn.name)).zip selectors).mapM
(fun x => compileFunctionSpec model.fields [] [] [] x.2 x.1) with _ | irFns
· simp [hmap] at hcore
· simp [hmap] at hcore
rcases hinternal :
(model.functions.filter (·.isInternal)).mapM
(compileInternalFunction model.fields [] [] []) with _ | internalFuncDefs
· simp [hinternal] at hcore
· rcases hctor :
compileConstructor model.fields [] [] [] model.constructor with _ | deployStmts
· simp [hinternal, hctor] at hcore
cases hcore
· simp [hinternal, hctor] at hcore
have hfunctions : ir.functions = irFns := by
injection hcore with hir
cases hir
rfl
have hcompiled :
List.Forall₂
(fun (entry : FunctionSpec × Nat) irFn =>
compileFunctionSpec model.fields model.events model.errors [] entry.2 entry.1 = Except.ok irFn)
((model.functions.filter
(fun fn => !fn.isInternal && !isInteropEntrypointName fn.name)).zip selectors)
irFns :=
by
simpa [hSupported.noEvents, hSupported.noErrors] using
(compiled_functions_forall₂_of_mapM_ok model.fields [] [] _ _ hmap)
simpa [SourceSemantics.selectorFunctionPairs, selectorDispatchedFunctions,
hfunctions] using hcompiled
private theorem filterInternalFunctions_eq_nil_of_all_nonInternal :
∀ (fns : List FunctionSpec),
(∀ fn ∈ fns, fn.isInternal = false) →
fns.filter (·.isInternal) = []
| [], _ => rfl
| fn :: rest, hall => by
have hfn : fn.isInternal = false := hall fn (by simp)
have hrest : ∀ fn' ∈ rest, fn'.isInternal = false := by
intro fn' hmem
exact hall fn' (by simp [hmem])
simp [hfn, filterInternalFunctions_eq_nil_of_all_nonInternal rest hrest]
private theorem filterInternalFunctions_eq_nil_of_supported
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpec model selectors) :
model.functions.filter (·.isInternal) = [] := by
exact filterInternalFunctions_eq_nil_of_all_nonInternal model.functions
(hSupported.noInternalFunctions)
private theorem filterInternalFunctions_eq_nil_of_supported_except_mapping_writes
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpecExceptMappingWrites model selectors) :
model.functions.filter (·.isInternal) = [] := by
exact filterInternalFunctions_eq_nil_of_all_nonInternal model.functions
(hSupported.noInternalFunctions)
private theorem compileValidatedCore_ok_yields_internalFunctions_nil
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpec model selectors)
(ir : IRContract)
(hcore : compileValidatedCore model selectors = Except.ok ir) :
ir.internalFunctions = [] := by
have hfallback :
pickUniqueFunctionByName "fallback" model.functions = Except.ok none :=
pickUniqueFunctionByName_eq_ok_none_of_absent
"fallback" model.functions hSupported.noFallback
have hreceive :
pickUniqueFunctionByName "receive" model.functions = Except.ok none :=
pickUniqueFunctionByName_eq_ok_none_of_absent
"receive" model.functions hSupported.noReceive
have hnoInternalFns :
model.functions.filter (·.isInternal) = [] :=
filterInternalFunctions_eq_nil_of_supported model selectors hSupported
have harray : contractUsesArrayElement model = false :=
hSupported.contractUsesArrayElement_eq_false
have hstorageArray : contractUsesStorageArrayElement model = false :=
hSupported.contractUsesStorageArrayElement_eq_false
have hdynamicBytesEq : contractUsesDynamicBytesEq model = false :=
hSupported.contractUsesDynamicBytesEq_eq_false
unfold compileValidatedCore at hcore
rw [hSupported.normalizedFields, hfallback, hreceive,
contractUsesPlainArrayElement, contractUsesArrayElementWord, harray,
hstorageArray, hdynamicBytesEq, hnoInternalFns, hSupported.noAdtTypes] at hcore
simp only [bind, Except.bind, pure, Except.pure, List.mapM_nil] at hcore
rcases hmap :
((model.functions.filter
(fun fn => !fn.isInternal && !isInteropEntrypointName fn.name)).zip selectors).mapM
(fun x => compileFunctionSpec model.fields model.events model.errors [] x.2 x.1) with _ | irFns
· simp [hmap] at hcore
· rcases hctor :
compileConstructor model.fields model.events model.errors [] model.constructor with _ | deployStmts
· simp [hmap, hctor] at hcore
cases hcore
· simp [hmap, hctor] at hcore
cases hcore
rfl
theorem supported_params_of_supportedSpec
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpec model selectors) :
∀ fn ∈ selectorDispatchedFunctions model,
∀ param ∈ fn.params, SupportedExternalParamType param.ty := by
intro fn hfn param hparam
have hfnModel : fn ∈ model.functions := by
exact List.mem_of_mem_filter hfn
exact (hSupported.functions fn hfnModel).paramsSupported param hparam
theorem supported_params_of_supportedSpec_except_mapping_writes
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpecExceptMappingWrites model selectors) :
∀ fn ∈ selectorDispatchedFunctions model,
∀ param ∈ fn.params, SupportedExternalParamType param.ty := by
intro fn hfn param hparam
have hfnModel : fn ∈ model.functions := by
exact List.mem_of_mem_filter hfn
exact (hSupported.functions fn hfnModel).paramsSupported param hparam
theorem interpretIR_eq_runtimeContractOfFunctions
(ir : IRContract)
(runtimeName : String)
(irFns : List IRFunction)
(tx : IRTransaction)
(initialState : IRState)
(hfunctions : ir.functions = irFns) :
interpretIR ir tx initialState =
interpretIR (Dispatch.runtimeContractOfFunctions runtimeName irFns) tx initialState := by
cases ir
subst hfunctions
simp [interpretIR, Dispatch.runtimeContractOfFunctions]
theorem interpretContract_correct_of_ir_functions
(model : CompilationModel)
(selectors : List Nat)
(ir : IRContract)
(irFns : List IRFunction)
(tx : IRTransaction)
(initialWorld : Verity.ContractState)
(hfunctions : ir.functions = irFns)
(hcompiled :
List.Forall₂
(fun entry irFn =>
compileFunctionSpec model.fields model.events model.errors [] entry.2 entry.1 = Except.ok irFn)
(SourceSemantics.selectorFunctionPairs model selectors)
irFns)
(hparamsSupported :
∀ fn ∈ selectorDispatchedFunctions model,
∀ param ∈ fn.params, SupportedExternalParamType param.ty)
(hfunction :
∀ fn sel irFn bindings,
fn ∈ selectorDispatchedFunctions model →
compileFunctionSpec model.fields model.events model.errors [] sel fn = Except.ok irFn →
SourceSemantics.bindSupportedParams fn.params tx.args = some bindings →
FunctionBody.sourceResultMatchesIRResult
(SourceSemantics.interpretFunction model fn tx initialWorld)
(execIRFunction irFn tx.args (FunctionBody.initialIRStateForTx model tx initialWorld))) :
FunctionBody.sourceResultMatchesIRResult
(SourceSemantics.interpretContract model selectors tx initialWorld)
(interpretIR ir tx (FunctionBody.initialIRStateForTx model tx initialWorld)) := by
rw [interpretIR_eq_runtimeContractOfFunctions
(ir := ir)
(runtimeName := model.name)
(irFns := irFns)
(tx := tx)
(initialState := FunctionBody.initialIRStateForTx model tx initialWorld)
(hfunctions := hfunctions)]
exact Dispatch.interpretContract_correct_of_compiled_functions
(model := model) (selectors := selectors) (irFns := irFns)
(tx := tx) (initialWorld := initialWorld)
hcompiled hparamsSupported hfunction
theorem compile_preserves_semantics_of_compiled_functions
(model : CompilationModel)
(selectors : List Nat)
(ir : IRContract)
(tx : IRTransaction)
(initialWorld : Verity.ContractState)
(_hcompile : CompilationModel.compile model selectors = Except.ok ir)
(hcompiled :
List.Forall₂
(fun entry irFn =>
compileFunctionSpec model.fields model.events model.errors [] entry.2 entry.1 = Except.ok irFn)
(SourceSemantics.selectorFunctionPairs model selectors)
ir.functions)
(hparamsSupported :
∀ fn ∈ selectorDispatchedFunctions model,
∀ param ∈ fn.params, SupportedExternalParamType param.ty)
(hfunction :
∀ fn sel irFn bindings,
fn ∈ selectorDispatchedFunctions model →
compileFunctionSpec model.fields model.events model.errors [] sel fn = Except.ok irFn →
SourceSemantics.bindSupportedParams fn.params tx.args = some bindings →
FunctionBody.sourceResultMatchesIRResult
(SourceSemantics.interpretFunction model fn tx initialWorld)
(execIRFunction irFn tx.args (FunctionBody.initialIRStateForTx model tx initialWorld))) :
FunctionBody.sourceResultMatchesIRResult
(SourceSemantics.interpretContract model selectors tx initialWorld)
(interpretIR ir tx (FunctionBody.initialIRStateForTx model tx initialWorld)) := by
exact interpretContract_correct_of_ir_functions
(model := model)
(selectors := selectors)
(ir := ir)
(irFns := ir.functions)
(tx := tx)
(initialWorld := initialWorld)
(hfunctions := rfl)
(hcompiled := hcompiled)
(hparamsSupported := hparamsSupported)
(hfunction := hfunction)
/-- Derive the compiled runtime function table directly from
`CompilationModel.compile = Except.ok ir` and `SupportedSpec`, without any
intermediate `List.Forall₂` hypothesis supplied by callers. -/
theorem compile_ok_yields_compiled_functions
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpec model selectors)
(ir : IRContract)
(hcompile : CompilationModel.compile model selectors = Except.ok ir) :
List.Forall₂
(fun entry irFn =>
compileFunctionSpec model.fields model.events model.errors [] entry.2 entry.1 = Except.ok irFn)
(SourceSemantics.selectorFunctionPairs model selectors)
ir.functions := by
unfold CompilationModel.compile at hcompile
simp only [bind, Except.bind] at hcompile
rcases hvalidate : validateCompileInputs model selectors with _ | validated
· simp [hvalidate] at hcompile
· simp [hvalidate] at hcompile
exact compileValidatedCore_ok_yields_compiled_functions
(model := model)
(selectors := selectors)
(hSupported := hSupported)
(ir := ir)
(hcore := hcompile)
theorem compile_ok_yields_compiled_functions_except_mapping_writes
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpecExceptMappingWrites model selectors)
(ir : IRContract)
(hcompile : CompilationModel.compile model selectors = Except.ok ir) :
List.Forall₂
(fun entry irFn =>
compileFunctionSpec model.fields model.events model.errors [] entry.2 entry.1 = Except.ok irFn)
(SourceSemantics.selectorFunctionPairs model selectors)
ir.functions := by
unfold CompilationModel.compile at hcompile
simp only [bind, Except.bind] at hcompile
rcases hvalidate : validateCompileInputs model selectors with _ | validated
· simp [hvalidate] at hcompile
· simp [hvalidate] at hcompile
exact compileValidatedCore_ok_yields_compiled_functions_except_mapping_writes
(model := model)
(selectors := selectors)
(hSupported := hSupported)
(ir := ir)
(hcore := hcompile)
theorem compile_ok_yields_internalFunctions_nil
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpec model selectors)
(ir : IRContract)
(hcompile : CompilationModel.compile model selectors = Except.ok ir) :
ir.internalFunctions = [] := by
unfold CompilationModel.compile at hcompile
simp only [bind, Except.bind] at hcompile
rcases hvalidate : validateCompileInputs model selectors with _ | validated
· simp [hvalidate] at hcompile
· simp [hvalidate] at hcompile
exact compileValidatedCore_ok_yields_internalFunctions_nil
(model := model)
(selectors := selectors)
(hSupported := hSupported)
(ir := ir)
(hcore := hcompile)
theorem compile_ok_yields_internalFunctions_nil_except_mapping_writes
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpecExceptMappingWrites model selectors)
(ir : IRContract)
(hcompile : CompilationModel.compile model selectors = Except.ok ir) :
ir.internalFunctions = [] := by
have hfallback :
pickUniqueFunctionByName "fallback" model.functions = Except.ok none :=
pickUniqueFunctionByName_eq_ok_none_of_absent
"fallback" model.functions hSupported.noFallback
have hreceive :
pickUniqueFunctionByName "receive" model.functions = Except.ok none :=
pickUniqueFunctionByName_eq_ok_none_of_absent
"receive" model.functions hSupported.noReceive
have hnoInternalFns :
model.functions.filter (·.isInternal) = [] :=
filterInternalFunctions_eq_nil_of_supported_except_mapping_writes model selectors hSupported
have harray : contractUsesArrayElement model = false :=
hSupported.contractUsesArrayElement_eq_false
have hstorageArray : contractUsesStorageArrayElement model = false :=
hSupported.contractUsesStorageArrayElement_eq_false
have hdynamicBytesEq : contractUsesDynamicBytesEq model = false :=
hSupported.contractUsesDynamicBytesEq_eq_false
unfold CompilationModel.compile at hcompile
simp only [bind, Except.bind] at hcompile
rcases hvalidate : validateCompileInputs model selectors with _ | validated
· simp [hvalidate] at hcompile
· simp [hvalidate] at hcompile
unfold compileValidatedCore at hcompile
rw [hSupported.normalizedFields, hfallback, hreceive,
contractUsesPlainArrayElement, contractUsesArrayElementWord, harray,
hstorageArray, hdynamicBytesEq, hnoInternalFns, hSupported.noAdtTypes] at hcompile
simp only [bind, Except.bind, pure, Except.pure, List.mapM_nil] at hcompile
rcases hmap :
((model.functions.filter
(fun fn => !fn.isInternal && !isInteropEntrypointName fn.name)).zip selectors).mapM
(fun x => compileFunctionSpec model.fields model.events model.errors [] x.2 x.1) with _ | irFns
· simp [hmap] at hcompile
· rcases hctor :
compileConstructor model.fields model.events model.errors [] model.constructor with _ | deployStmts
· simp [hmap, hctor] at hcompile
cases hcompile
· simp [hmap, hctor] at hcompile
injection hcompile with hir
cases hir
rfl
-- NOTE: compileValidatedCore_ok_yields_supportedRuntimeHelperTableInterface and
-- compile_ok_yields_supportedRuntimeHelperTableInterface are BLOCKED by missing
-- DirectInternalHelperPerCalleeCompileCatalog infrastructure in GenericInduction.lean.
-- They require the helper function interface witness machinery that is not yet implemented.
theorem compileFunctionSpec_ok_yields_legacyCompatibleExternalStmtList
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpec model selectors)
(fn : FunctionSpec)
(sel : Nat)
(irFn : IRFunction)
(hfn : fn ∈ selectorDispatchedFunctions model)
(hcompileFn :
compileFunctionSpec model.fields model.events model.errors [] sel fn = Except.ok irFn) :
LegacyCompatibleExternalStmtList irFn.body := by
rcases Function.compileFunctionSpec_ok_components
model.fields model.events model.errors sel fn irFn hcompileFn with
⟨returns, bodyStmts, _hvalidate, _hreturns, hbodyCompile, hirFn⟩
subst hirFn
have hparams :=
legacyCompatibleExternalStmtList_genParamLoads_of_supported
fn.params
(hSupported.selectorFunctionParamsSupported hfn)
have hbody :=
legacyCompatibleExternalStmtList_of_compileStmtList_ok_on_supportedContractSurface
(hnoPacked := hSupported.noPackedFields)
(hsurface := by
let hbody := (hSupported.supportedFunctionOfSelectorDispatched hfn).body
exact stmtListTouchesUnsupportedContractSurface_eq_false_of_featureClosed fn.body
hbody.core.surfaceClosed
hbody.state.surfaceClosed
(SupportedBodyCallInterface.surfaceClosed hbody)
hbody.effects.surfaceClosed)
(hcompile := hbodyCompile)
exact legacyCompatibleExternalStmtList_append _ _ hparams hbody
theorem compileFunctionSpec_ok_yields_legacyCompatibleExternalStmtList_except_mapping_writes
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpecExceptMappingWrites model selectors)
(fn : FunctionSpec)
(sel : Nat)
(irFn : IRFunction)
(hfn : fn ∈ selectorDispatchedFunctions model)
(hcompileFn :
compileFunctionSpec model.fields model.events model.errors [] sel fn = Except.ok irFn) :
LegacyCompatibleExternalStmtList irFn.body := by
rcases Function.compileFunctionSpec_ok_components
model.fields model.events model.errors sel fn irFn hcompileFn with
⟨returns, bodyStmts, _hvalidate, _hreturns, hbodyCompile, hirFn⟩
subst hirFn
have hparams :=
legacyCompatibleExternalStmtList_genParamLoads_of_supported
fn.params
(hSupported.selectorFunctionParamsSupported hfn)
have hbody :=
legacyCompatibleExternalStmtList_of_compileStmtList_ok_on_supportedContractSurface_exceptMappingWrites
(hnoPacked := hSupported.noPackedFields)
(hsurface := by
let hbody := (hSupported.supportedFunctionOfSelectorDispatched hfn).body
exact stmtListTouchesUnsupportedContractSurfaceExceptMappingWrites_eq_false_of_featureClosed
fn.body
hbody.core.surfaceClosed
hbody.state.surfaceClosed
(SupportedBodyCallInterface.surfaceClosed_exceptMappingWrites (hBody := hbody))
hbody.effects.surfaceClosed)
(hcompile := hbodyCompile)
exact legacyCompatibleExternalStmtList_append _ _ hparams hbody
private theorem compiled_functions_legacyCompatibleExternalBodies
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpec model selectors) :
∀ {entries irFns},
List.Forall₂
(fun (entry : FunctionSpec × Nat) irFn =>
compileFunctionSpec model.fields model.events model.errors [] entry.2 entry.1 = Except.ok irFn)
entries
irFns →
(∀ entry ∈ entries, entry.1 ∈ selectorDispatchedFunctions model) →
∀ irFn ∈ irFns, LegacyCompatibleExternalStmtList irFn.body
| [], [], .nil, _ => by
intro irFn hmem
cases hmem
| entry :: entries, irFn :: irFns, .cons hhead htail, hentries => by
intro target hmem
cases hmem with
| head =>
exact compileFunctionSpec_ok_yields_legacyCompatibleExternalStmtList
(model := model)
(selectors := selectors)
(hSupported := hSupported)
(fn := entry.1)
(sel := entry.2)
(irFn := irFn)
(hfn := hentries entry (by simp))
(hcompileFn := hhead)
| tail _ hmemTail =>
exact compiled_functions_legacyCompatibleExternalBodies
(model := model)
(selectors := selectors)
(hSupported := hSupported)
htail
(fun other hmemEntry => hentries other (by simp [hmemEntry]))
target
hmemTail
private theorem compiled_functions_legacyCompatibleExternalBodies_except_mapping_writes
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpecExceptMappingWrites model selectors) :
∀ {entries irFns},
List.Forall₂
(fun (entry : FunctionSpec × Nat) irFn =>
compileFunctionSpec model.fields model.events model.errors [] entry.2 entry.1 = Except.ok irFn)
entries
irFns →
(∀ entry ∈ entries, entry.1 ∈ selectorDispatchedFunctions model) →
∀ irFn ∈ irFns, LegacyCompatibleExternalStmtList irFn.body
| [], [], .nil, _ => by
intro irFn hmem
cases hmem
| entry :: entries, irFn :: irFns, .cons hhead htail, hentries => by
intro target hmem
cases hmem with
| head =>
exact compileFunctionSpec_ok_yields_legacyCompatibleExternalStmtList_except_mapping_writes
(model := model)
(selectors := selectors)
(hSupported := hSupported)
(fn := entry.1)
(sel := entry.2)
(irFn := irFn)
(hfn := hentries entry (by simp))
(hcompileFn := hhead)
| tail _ hmemTail =>
exact compiled_functions_legacyCompatibleExternalBodies_except_mapping_writes
(model := model)
(selectors := selectors)
(hSupported := hSupported)
htail
(fun other hmemEntry => hentries other (by simp [hmemEntry]))
target
hmemTail
theorem compile_ok_yields_legacyCompatibleExternalBodies
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpec model selectors)
(ir : IRContract)
(hcompile : CompilationModel.compile model selectors = Except.ok ir) :
LegacyCompatibleExternalBodies ir := by
have hcompiled :
List.Forall₂
(fun entry irFn =>
compileFunctionSpec model.fields model.events model.errors [] entry.2 entry.1 = Except.ok irFn)
(SourceSemantics.selectorFunctionPairs model selectors)
ir.functions :=
compile_ok_yields_compiled_functions
(model := model)
(selectors := selectors)
(hSupported := hSupported)
(ir := ir)
(hcompile := hcompile)
intro irFn hmem
exact compiled_functions_legacyCompatibleExternalBodies
(model := model)
(selectors := selectors)
(hSupported := hSupported)
hcompiled
(by
intro (entry : FunctionSpec × Nat) hentry
simpa [SourceSemantics.selectorFunctionPairs] using
(List.of_mem_zip hentry).1)
irFn
hmem
theorem compile_ok_yields_legacyCompatibleExternalBodies_except_mapping_writes
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpecExceptMappingWrites model selectors)
(ir : IRContract)
(hcompile : CompilationModel.compile model selectors = Except.ok ir) :
LegacyCompatibleExternalBodies ir := by
have hcompiled :
List.Forall₂
(fun entry irFn =>
compileFunctionSpec model.fields model.events model.errors [] entry.2 entry.1 = Except.ok irFn)
(SourceSemantics.selectorFunctionPairs model selectors)
ir.functions :=
compile_ok_yields_compiled_functions_except_mapping_writes
(model := model)
(selectors := selectors)
(hSupported := hSupported)
(ir := ir)
(hcompile := hcompile)
intro irFn hmem
exact compiled_functions_legacyCompatibleExternalBodies_except_mapping_writes
(model := model)
(selectors := selectors)
(hSupported := hSupported)
hcompiled
(by
intro (entry : FunctionSpec × Nat) hentry
simpa [SourceSemantics.selectorFunctionPairs] using
(List.of_mem_zip hentry).1)
irFn
hmem
theorem compile_ok_yields_legacyCompatibleRuntimeContract