Repository navigation
Expand file tree
/
Copy pathContract.lean
More file actions
3231 lines (3094 loc) · 172 KB
/
Copy pathContract.lean
File metadata and controls
3231 lines (3094 loc) · 172 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
import Compiler.Proofs.IRGeneration.ContractShape
set_option linter.unnecessarySimpa false
namespace Compiler.Proofs.IRGeneration
open Compiler
open Compiler.CompilationModel
open Compiler.Yul
namespace Contract
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⟩
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 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
obtain ⟨irFns, deployStmts, hmap, hctor, hir⟩ :=
ContractShape.compileValidatedCore_ok_inv
model selectors ir hSupported.noFallback hSupported.noReceive
hSupported.noInternalFunctions
(ContractShape.supportedSpec_entries_lock_free hSupported)
hSupported.surface.noTemplateIntrinsics hcore
rw [hSupported.normalizedFields, hSupported.noAdtTypes] at hmap
have hcompiled := compiled_functions_forall₂_of_mapM_ok
model.fields model.events model.errors _ _ hmap
simpa [SourceSemantics.selectorFunctionPairs, selectorDispatchedFunctions,
hir] 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
obtain ⟨irFns, deployStmts, hmap, hctor, hir⟩ :=
ContractShape.compileValidatedCore_ok_inv
model selectors ir hSupported.noFallback hSupported.noReceive
hSupported.noInternalFunctions
(ContractShape.supportedSpecExceptMappingWrites_entries_lock_free hSupported)
hSupported.surface.noTemplateIntrinsics hcore
rw [hSupported.normalizedFields, hSupported.noAdtTypes] at hmap
have hcompiled := compiled_functions_forall₂_of_mapM_ok
model.fields model.events model.errors _ _ hmap
simpa [SourceSemantics.selectorFunctionPairs, selectorDispatchedFunctions,
hir] using hcompiled
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
obtain ⟨irFns, deployStmts, hmap, hctor, hir⟩ :=
ContractShape.compileValidatedCore_ok_inv
model selectors ir hSupported.noFallback hSupported.noReceive
hSupported.noInternalFunctions
(ContractShape.supportedSpec_entries_lock_free hSupported)
hSupported.surface.noTemplateIntrinsics hcore
rw [hir]
exact ContractShape.coreHelperInternalFunctions_eq_nil model
hSupported.contractUsesArrayElement_eq_false
hSupported.contractUsesStorageArrayElement_eq_false
hSupported.contractUsesDynamicBytesEq_eq_false
hSupported.contractUsesMulDiv512_eq_false
hSupported.contractUsesParamDynamicHeadWord_eq_false
hSupported.noCheckedArithmetic
private theorem compileValidatedCore_ok_yields_noFallbackEntrypoint
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpec model selectors)
(ir : IRContract)
(hcore : compileValidatedCore model selectors = Except.ok ir) :
ir.fallbackEntrypoint = none := by
obtain ⟨irFns, deployStmts, hmap, hctor, hir⟩ :=
ContractShape.compileValidatedCore_ok_inv
model selectors ir hSupported.noFallback hSupported.noReceive
hSupported.noInternalFunctions
(ContractShape.supportedSpec_entries_lock_free hSupported)
hSupported.surface.noTemplateIntrinsics hcore
rw [hir]
private theorem compileValidatedCore_ok_yields_noReceiveEntrypoint
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpec model selectors)
(ir : IRContract)
(hcore : compileValidatedCore model selectors = Except.ok ir) :
ir.receiveEntrypoint = none := by
obtain ⟨irFns, deployStmts, hmap, hctor, hir⟩ :=
ContractShape.compileValidatedCore_ok_inv
model selectors ir hSupported.noFallback hSupported.noReceive
hSupported.noInternalFunctions
(ContractShape.supportedSpec_entries_lock_free hSupported)
hSupported.surface.noTemplateIntrinsics hcore
rw [hir]
theorem supported_params_of_supportedSpec
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpec model selectors) :
∀ fn ∈ selectorDispatchedFunctions model,
∀ param ∈ fn.params, SupportedExternalScalarParamType 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_with_scalar_events
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpecWithScalarEvents model selectors) :
∀ fn ∈ selectorDispatchedFunctions model,
∀ param ∈ fn.params, SupportedExternalScalarParamType 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, SupportedExternalScalarParamType 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)
{habi : ∀ fn ∈ selectorDispatchedFunctions model, fn.abiDecoding = .standard}
(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, SupportedExternalScalarParamType 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 (habi := habi)
(model := model) (selectors := selectors) (irFns := irFns)
(tx := tx) (initialWorld := initialWorld)
hcompiled hparamsSupported hfunction
theorem compile_preserves_semantics_of_compiled_functions
(model : CompilationModel)
{habi : ∀ fn ∈ selectorDispatchedFunctions model, fn.abiDecoding = .standard}
(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, SupportedExternalScalarParamType 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 (habi := habi)
(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_with_scalar_events
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpecWithScalarEvents 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
exact ContractShape.compile_ok_yields_compiled_functions_with_scalar_events
(model := model)
(selectors := selectors)
(hSupported := hSupported)
(ir := ir)
(hcompile := 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_noFallbackEntrypoint
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpec model selectors)
(ir : IRContract)
(hcompile : CompilationModel.compile model selectors = Except.ok ir) :
ir.fallbackEntrypoint = none := 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_noFallbackEntrypoint
(model := model)
(selectors := selectors)
(hSupported := hSupported)
(ir := ir)
(hcore := hcompile)
theorem compile_ok_yields_noReceiveEntrypoint
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpec model selectors)
(ir : IRContract)
(hcompile : CompilationModel.compile model selectors = Except.ok ir) :
ir.receiveEntrypoint = none := 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_noReceiveEntrypoint
(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 hcore := ContractShape.compile_ok_yields_core model selectors ir hcompile
obtain ⟨irFns, deployStmts, hmap, hctor, hir⟩ :=
ContractShape.compileValidatedCore_ok_inv
model selectors ir hSupported.noFallback hSupported.noReceive
hSupported.noInternalFunctions
(ContractShape.supportedSpecExceptMappingWrites_entries_lock_free hSupported)
hSupported.surface.noTemplateIntrinsics hcore
rw [hir]
exact ContractShape.coreHelperInternalFunctions_eq_nil model
hSupported.contractUsesArrayElement_eq_false
hSupported.contractUsesStorageArrayElement_eq_false
hSupported.contractUsesDynamicBytesEq_eq_false
hSupported.contractUsesMulDiv512_eq_false
hSupported.contractUsesParamDynamicHeadWord_eq_false
hSupported.noCheckedArithmetic
theorem compile_ok_yields_noFallbackEntrypoint_except_mapping_writes
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpecExceptMappingWrites model selectors)
(ir : IRContract)
(hcompile : CompilationModel.compile model selectors = Except.ok ir) :
ir.fallbackEntrypoint = none := by
have hcore := ContractShape.compile_ok_yields_core model selectors ir hcompile
obtain ⟨irFns, deployStmts, hmap, hctor, hir⟩ :=
ContractShape.compileValidatedCore_ok_inv
model selectors ir hSupported.noFallback hSupported.noReceive
hSupported.noInternalFunctions
(ContractShape.supportedSpecExceptMappingWrites_entries_lock_free hSupported)
hSupported.surface.noTemplateIntrinsics hcore
rw [hir]
theorem compile_ok_yields_noReceiveEntrypoint_except_mapping_writes
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpecExceptMappingWrites model selectors)
(ir : IRContract)
(hcompile : CompilationModel.compile model selectors = Except.ok ir) :
ir.receiveEntrypoint = none := by
have hcore := ContractShape.compile_ok_yields_core model selectors ir hcompile
obtain ⟨irFns, deployStmts, hmap, hctor, hir⟩ :=
ContractShape.compileValidatedCore_ok_inv
model selectors ir hSupported.noFallback hSupported.noReceive
hSupported.noInternalFunctions
(ContractShape.supportedSpecExceptMappingWrites_entries_lock_free hSupported)
hSupported.surface.noTemplateIntrinsics hcore
rw [hir]
-- 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.
/-- Generic function-level closure from `SupportedSpec` and successful
`compileFunctionSpec`, with no residual body-level premises such as `hsource`,
`hbodyExec`, or `hmatch`. -/
theorem compileFunctionSpec_correct_generic
(model : CompilationModel) (selectors : List Nat) (hSupported : SupportedSpec model selectors)
(hvalidateInputs : validateCompileInputs model selectors = Except.ok ())
(fn : FunctionSpec) (sel : Nat) (irFn : IRFunction) (tx : IRTransaction)
(initialWorld : Verity.ContractState) (htxNormalized : Function.TxContextNormalized tx)
(bindings : List (String × Nat)) (hcalldataSizeFits : Function.TxCalldataSizeFitsEvm tx)
(hfn : fn ∈ selectorDispatchedFunctions model)
(hcompileFn :
compileFunctionSpec model.fields model.events model.errors [] sel fn = Except.ok irFn)
(hbind : SourceSemantics.bindSupportedParams fn.params tx.args = some bindings) :
FunctionBody.sourceResultMatchesIRResult
(supportedSourceFunctionSemantics model selectors hSupported fn tx initialWorld)
(execIRFunction irFn tx.args (FunctionBody.initialIRStateForTx model tx initialWorld)) := by
rcases Function.compileFunctionSpec_ok_components
(habi := (hSupported.supportedFunctionOfSelectorDispatched hfn).standardAbi)
model.fields model.events model.errors sel fn irFn hcompileFn with
⟨returns, bodyStmts, hvalidate, hreturns, hbodyCompile, hirFn⟩
subst hirFn
have hcorrect := Function.supported_function_correct (model := model)
(selectors := selectors) (hSupported := hSupported) (hvalidateInputs := hvalidateInputs)
(fn := fn) (selector := sel) (returns := returns) (bodyStmts := bodyStmts)
(irFn := Function.compiledFunctionIR sel fn returns bodyStmts) (tx := tx)
(initialWorld := initialWorld) (htxNormalized := htxNormalized) (bindings := bindings)
(hfn := hfn) (hvalidate := hvalidate) (hreturns := hreturns)
(hbodyCompile := hbodyCompile) (hcompile := by simpa using hcompileFn) (hbind := hbind)
(hcalldataSizeFits := hcalldataSizeFits)
simpa [supportedSourceFunctionSemantics_eq_interpretFunction_of_selectorDispatched
(hSupported := hSupported) hfn tx initialWorld] using hcorrect
/-- Tier 2 generic function-level closure from
`SupportedSpecExceptMappingWrites` and successful `compileFunctionSpec`. This
exposes the widened singleton storage-write fragment on the same public theorem
surface as the ordinary supported-function wrapper. -/
theorem compileFunctionSpec_correct_generic_except_mapping_writes
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpecExceptMappingWrites model selectors)
(fn : FunctionSpec)
(sel : Nat)
(irFn : IRFunction)
(tx : IRTransaction)
(initialWorld : Verity.ContractState)
(htxNormalized : Function.TxContextNormalized tx)
(bindings : List (String × Nat))
(hcalldataSizeFits : Function.TxCalldataSizeFitsEvm tx)
(hnoConflict : firstFieldWriteSlotConflict model.fields = none)
(hsafety : SupportedStmtListMappingWriteSlotSafety model.fields)
(hfn : fn ∈ selectorDispatchedFunctions model)
(hcompileFn :
compileFunctionSpec model.fields model.events model.errors [] sel fn = Except.ok irFn)
(hbind : SourceSemantics.bindSupportedParams fn.params tx.args = some bindings) :
FunctionBody.sourceResultMatchesIRResult
(supportedSourceFunctionSemanticsExceptMappingWrites model selectors hSupported fn tx initialWorld)
(execIRFunction irFn tx.args (FunctionBody.initialIRStateForTx model tx initialWorld)) := by
have hcorrect :=
Function.supported_function_correct_except_mapping_writes
(model := model)
(selectors := selectors)
(hSupported := hSupported)
(fn := fn)
(selector := sel)
(irFn := irFn)
(tx := tx)
(initialWorld := initialWorld)
(bindings := bindings)
(hfn := hfn)
(hcompileFn := hcompileFn)
(hbind := hbind)
(hnoConflict := hnoConflict)
(hsafety := hsafety)
(htxNormalized := htxNormalized)
(hcalldataSizeFits := hcalldataSizeFits)
simpa [supportedSourceFunctionSemanticsExceptMappingWrites_eq_interpretFunction_of_selectorDispatched
(hSupported := hSupported) hfn tx initialWorld] using hcorrect
theorem compileFunctionSpec_correct_generic_except_mapping_writes_stmtSafety
(model : CompilationModel) (selectors : List Nat)
(hSupported : SupportedSpecExceptMappingWrites model selectors)
(fn : FunctionSpec) (sel : Nat) (irFn : IRFunction) (tx : IRTransaction)
(initialWorld : Verity.ContractState) (htxNormalized : Function.TxContextNormalized tx)
(bindings : List (String × Nat)) (hcalldataSizeFits : Function.TxCalldataSizeFitsEvm tx)
(hnoConflict : firstFieldWriteSlotConflict model.fields = none)
(hsafety : ∀ stmt ∈ fn.body, StmtMappingWriteSlotSafe model.fields stmt)
(hfn : fn ∈ selectorDispatchedFunctions model)
(hcompileFn :
compileFunctionSpec model.fields model.events model.errors [] sel fn = Except.ok irFn)
(hbind : SourceSemantics.bindSupportedParams fn.params tx.args = some bindings) :
FunctionBody.sourceResultMatchesIRResult
(supportedSourceFunctionSemanticsExceptMappingWrites model selectors hSupported fn tx initialWorld)
(execIRFunction irFn tx.args (FunctionBody.initialIRStateForTx model tx initialWorld)) := by
have hcorrect :=
Function.supported_function_correct_except_mapping_writes_stmtSafety
(model := model) (selectors := selectors) (hSupported := hSupported) (fn := fn)
(selector := sel) (irFn := irFn) (tx := tx) (initialWorld := initialWorld)
(bindings := bindings) (hfn := hfn) (hcompileFn := hcompileFn) (hbind := hbind)
(hnoConflict := hnoConflict) (hsafety := hsafety) (htxNormalized := htxNormalized)
(hcalldataSizeFits := hcalldataSizeFits)
simpa [supportedSourceFunctionSemanticsExceptMappingWrites_eq_interpretFunction_of_selectorDispatched
(hSupported := hSupported) hfn tx initialWorld] using hcorrect
/-- Helper-proof-carrying function-level generic theorem.
This is the proof-ready theorem surface for the helper-composition step (#1630).
The `hHelperProofs` argument is now backed by a first-class *source-level* reuse
interface: `SourceSemantics.SupportedSpecHelperProofs.helperCallSummarySound` (and
its `eval`/`exec` call-site corollaries) thread the once-proved helper catalog
through to every selector-dispatched caller and call site. This compiled-side
function proof still reduces through the helper-excluding `SupportedStmtList`
fragment, so consuming that reuse interface here — retargeting the body proof —
is the tracked next step; the trusted boundary is unchanged. -/
theorem compileFunctionSpec_correct_generic_with_helper_proofs
(model : CompilationModel) (selectors : List Nat) (hSupported : SupportedSpec model selectors)
(hHelperProofs : SourceSemantics.SupportedSpecHelperProofs model selectors hSupported)
(hvalidateInputs : validateCompileInputs model selectors = Except.ok ())
(fn : FunctionSpec) (sel : Nat) (irFn : IRFunction) (tx : IRTransaction)
(initialWorld : Verity.ContractState) (htxNormalized : Function.TxContextNormalized tx)
(bindings : List (String × Nat)) (hcalldataSizeFits : Function.TxCalldataSizeFitsEvm tx)
(hfn : fn ∈ selectorDispatchedFunctions model)
(hcompileFn :
compileFunctionSpec model.fields model.events model.errors [] sel fn = Except.ok irFn)
(hbind : SourceSemantics.bindSupportedParams fn.params tx.args = some bindings) :
FunctionBody.sourceResultMatchesIRResult
(supportedSourceFunctionSemantics model selectors hSupported fn tx initialWorld)
(execIRFunction irFn tx.args (FunctionBody.initialIRStateForTx model tx initialWorld)) := by
rcases Function.compileFunctionSpec_ok_components
(habi := (hSupported.supportedFunctionOfSelectorDispatched hfn).standardAbi)
model.fields model.events model.errors sel fn irFn hcompileFn with
⟨returns, bodyStmts, hvalidate, hreturns, hbodyCompile, hirFn⟩
subst hirFn
exact Function.supported_function_correct_with_helper_proofs (model := model)
(selectors := selectors) (hSupported := hSupported) (hHelperProofs := hHelperProofs)
(hvalidateInputs := hvalidateInputs) (fn := fn) (selector := sel) (returns := returns)
(bodyStmts := bodyStmts) (irFn := Function.compiledFunctionIR sel fn returns bodyStmts)
(tx := tx) (initialWorld := initialWorld) (bindings := bindings) (hfn := hfn)
(hvalidate := hvalidate) (hreturns := hreturns) (hbodyCompile := hbodyCompile)
(hcompile := by simpa using hcompileFn) (hbind := hbind) (htxNormalized := htxNormalized)
(hcalldataSizeFits := hcalldataSizeFits)
/-- Helper-aware compiled-side wrapper for the generic function theorem.
This does not strengthen the current proof boundary by itself: it factors the
eventual retarget from `execIRFunction` to `execIRFunctionWithInternals` behind
the exact conservative-extension equality that still remains to be proved on the
compiled side. -/
theorem compileFunctionSpec_correct_generic_with_helper_proofs_and_helper_ir
(model : CompilationModel) (selectors : List Nat) (hSupported : SupportedSpec model selectors)
(hHelperProofs : SourceSemantics.SupportedSpecHelperProofs model selectors hSupported)
(hvalidateInputs : validateCompileInputs model selectors = Except.ok ())
(runtimeContract : IRContract)
(fn : FunctionSpec) (sel : Nat) (irFn : IRFunction) (tx : IRTransaction)
(initialWorld : Verity.ContractState) (htxNormalized : Function.TxContextNormalized tx)
(bindings : List (String × Nat)) (hcalldataSizeFits : Function.TxCalldataSizeFitsEvm tx)
(hfn : fn ∈ selectorDispatchedFunctions model)
(hcompileFn :
compileFunctionSpec model.fields model.events model.errors [] sel fn = Except.ok irFn)
(hbind : SourceSemantics.bindSupportedParams fn.params tx.args = some bindings)
(hhelperIR :
execIRFunctionWithInternals runtimeContract 0 irFn tx.args
(FunctionBody.initialIRStateForTx model tx initialWorld) =
execIRFunction irFn tx.args
(FunctionBody.initialIRStateForTx model tx initialWorld)) :
FunctionBody.sourceResultMatchesIRResult
(supportedSourceFunctionSemantics model selectors hSupported fn tx initialWorld)
(execIRFunctionWithInternals runtimeContract 0 irFn tx.args
(FunctionBody.initialIRStateForTx model tx initialWorld)) := by
have hlegacy := compileFunctionSpec_correct_generic_with_helper_proofs
(model := model) (selectors := selectors) (hSupported := hSupported)
(hHelperProofs := hHelperProofs) (hvalidateInputs := hvalidateInputs) (fn := fn)
(sel := sel) (irFn := irFn) (tx := tx) (initialWorld := initialWorld)
(htxNormalized := htxNormalized) (bindings := bindings)
(hcalldataSizeFits := hcalldataSizeFits) (hfn := hfn) (hcompileFn := hcompileFn)
(hbind := hbind)
simpa [hhelperIR] using hlegacy
/-- Direct helper-aware compiled-side wrapper for the generic function theorem.
This consumes the exact helper-aware body/IR goal and executes the compiled
function through `execIRFunctionWithInternals` directly, leaving only the
generated ABI parameter-load prefix disjointness as an explicit compiled-side
premise. -/
theorem compileFunctionSpec_correct_generic_with_helper_proofs_and_helper_ir_of_body_goal
(model : CompilationModel) (selectors : List Nat) (hSupported : SupportedSpec model selectors)
(hHelperProofs : SourceSemantics.SupportedSpecHelperProofs model selectors hSupported)
(hvalidateInputs : validateCompileInputs model selectors = Except.ok ())
(runtimeContract : IRContract) (fn : FunctionSpec) (sel : Nat) (returns : List ParamType)
(bodyStmts : List YulStmt) (irFn : IRFunction) (tx : IRTransaction)
(initialWorld : Verity.ContractState) (htxNormalized : Function.TxContextNormalized tx)
(bindings : List (String × Nat)) (internalFunctions : List FunctionSpec)
(irFuelSlack : Nat) (extraFuel : Nat) (hcalldataSizeFits : Function.TxCalldataSizeFitsEvm tx)
(hfn : fn ∈ selectorDispatchedFunctions model)
(hvalidate : validateFunctionSpec fn = Except.ok ())
(hreturns : functionReturns fn = Except.ok returns)
(hbodyCompile : compileStmtList model.fields model.events model.errors .calldata [] false
(fn.params.map (·.name)) [] fn.body internalFunctions = Except.ok bodyStmts)
(hcompileFn : compileFunctionSpec model.fields model.events model.errors [] sel fn
(internalFunctions := internalFunctions) = Except.ok irFn)
(hbind : SourceSemantics.bindSupportedParams fn.params tx.args = some bindings)
(hcompiledBodyFuel : (genParamLoads fn.params ++ bodyStmts).length + extraFuel =
sizeOf (Function.compiledFunctionIR sel fn returns bodyStmts).body + irFuelSlack)
(hbodyCorrect : SupportedFunctionBodyWithHelpersAndHelperIRPreservationGoal runtimeContract
model fn bodyStmts hSupported.helperFuel tx initialWorld
(ParamLoading.applyBindingsToIRState (Function.prebindRawArgs
(FunctionBody.initialIRStateForTx model tx initialWorld) fn.params) bindings)
bindings extraFuel)
(hparamDisjoint : YulStmtListCallsDisjointFromInternalTable runtimeContract
(genParamLoads fn.params)) :
FunctionBody.sourceResultMatchesIRResult
(supportedSourceFunctionSemantics model selectors hSupported fn tx initialWorld)
(execIRFunctionWithInternals runtimeContract irFuelSlack irFn tx.args
(FunctionBody.initialIRStateForTx model tx initialWorld)) := by
exact Function.supported_function_correct_with_helper_proofs_body_goal_with_internals
model selectors hSupported hHelperProofs hvalidateInputs runtimeContract
fn sel returns bodyStmts irFn tx initialWorld bindings internalFunctions hfn hvalidate hreturns
hbodyCompile hcompileFn hbind htxNormalized irFuelSlack extraFuel hcompiledBodyFuel
hbodyCorrect hparamDisjoint hcalldataSizeFits
/-- Packaged helper-aware compiled-side wrapper that discharges the ABI
parameter-load prefix disjointness from the runtime contract's internal-table
naming invariant, rather than taking it as an opaque premise. This is the
selector-dispatch consumer seam: once the whole-contract theorem establishes
`InternalTableNamesInternalPrefixed` for the compiled runtime contract, each
per-function correctness obligation feeds straight into
`execIRFunctionWithInternals` without the `genParamLoads` disjointness witness
having to be supplied by hand. Param supportedness comes from `SupportedSpec`. -/
theorem compileFunctionSpec_correct_generic_with_helper_proofs_and_helper_ir_of_body_goal_of_internalNamesPrefixed
(model : CompilationModel) (selectors : List Nat) (hSupported : SupportedSpec model selectors)
(hHelperProofs : SourceSemantics.SupportedSpecHelperProofs model selectors hSupported)
(hvalidateInputs : validateCompileInputs model selectors = Except.ok ())
(runtimeContract : IRContract) (fn : FunctionSpec) (sel : Nat) (returns : List ParamType)
(bodyStmts : List YulStmt) (irFn : IRFunction) (tx : IRTransaction)
(initialWorld : Verity.ContractState) (htxNormalized : Function.TxContextNormalized tx)
(bindings : List (String × Nat)) (internalFunctions : List FunctionSpec)
(irFuelSlack : Nat) (extraFuel : Nat) (hcalldataSizeFits : Function.TxCalldataSizeFitsEvm tx)
(hfn : fn ∈ selectorDispatchedFunctions model)
(hvalidate : validateFunctionSpec fn = Except.ok ())
(hreturns : functionReturns fn = Except.ok returns)
(hbodyCompile : compileStmtList model.fields model.events model.errors .calldata [] false
(fn.params.map (·.name)) [] fn.body internalFunctions = Except.ok bodyStmts)
(hcompileFn : compileFunctionSpec model.fields model.events model.errors [] sel fn
(internalFunctions := internalFunctions) = Except.ok irFn)
(hbind : SourceSemantics.bindSupportedParams fn.params tx.args = some bindings)
(hcompiledBodyFuel : (genParamLoads fn.params ++ bodyStmts).length + extraFuel =
sizeOf (Function.compiledFunctionIR sel fn returns bodyStmts).body + irFuelSlack)
(hbodyCorrect : SupportedFunctionBodyWithHelpersAndHelperIRPreservationGoal runtimeContract
model fn bodyStmts hSupported.helperFuel tx initialWorld
(ParamLoading.applyBindingsToIRState (Function.prebindRawArgs
(FunctionBody.initialIRStateForTx model tx initialWorld) fn.params) bindings)
bindings extraFuel)
(hinv : InternalTableNamesInternalPrefixed runtimeContract) :
FunctionBody.sourceResultMatchesIRResult
(supportedSourceFunctionSemantics model selectors hSupported fn tx initialWorld)
(execIRFunctionWithInternals runtimeContract irFuelSlack irFn tx.args
(FunctionBody.initialIRStateForTx model tx initialWorld)) :=
compileFunctionSpec_correct_generic_with_helper_proofs_and_helper_ir_of_body_goal
model selectors hSupported hHelperProofs hvalidateInputs runtimeContract fn sel returns
bodyStmts irFn tx initialWorld htxNormalized bindings internalFunctions irFuelSlack extraFuel
hcalldataSizeFits hfn hvalidate hreturns hbodyCompile hcompileFn hbind hcompiledBodyFuel
hbodyCorrect
(Function.genParamLoads_callsDisjoint_of_internalNamesPrefixed runtimeContract fn.params
(supported_params_of_supportedSpec model selectors hSupported fn hfn) hinv)
/-- Reserved-name variant of
`compileFunctionSpec_correct_generic_with_helper_proofs_and_helper_ir_of_body_goal_of_internalNamesPrefixed`.
Takes the *true* `InternalTableNamesReserved` invariant — dischargeable from the
real populated internal table via
`InternalTableNamesReserved_of_helpers_append_compiledInternalTable` — and
discharges the ABI parameter-load prefix disjointness through
`Function.genParamLoads_callsDisjoint_of_reserved`. This is the per-function seam
that lets the whole-contract `WithInternals` dispatch retarget consume a populated
internal table containing non-`internal_` compiler helpers. -/
theorem compileFunctionSpec_correct_generic_with_helper_proofs_and_helper_ir_of_body_goal_of_reserved
(model : CompilationModel) (selectors : List Nat) (hSupported : SupportedSpec model selectors)
(hHelperProofs : SourceSemantics.SupportedSpecHelperProofs model selectors hSupported)
(hvalidateInputs : validateCompileInputs model selectors = Except.ok ())
(runtimeContract : IRContract) (fn : FunctionSpec) (sel : Nat) (returns : List ParamType)
(bodyStmts : List YulStmt) (irFn : IRFunction) (tx : IRTransaction)
(initialWorld : Verity.ContractState) (htxNormalized : Function.TxContextNormalized tx)
(bindings : List (String × Nat)) (internalFunctions : List FunctionSpec)
(irFuelSlack : Nat) (extraFuel : Nat) (hcalldataSizeFits : Function.TxCalldataSizeFitsEvm tx)
(hfn : fn ∈ selectorDispatchedFunctions model)
(hvalidate : validateFunctionSpec fn = Except.ok ())
(hreturns : functionReturns fn = Except.ok returns)
(hbodyCompile : compileStmtList model.fields model.events model.errors .calldata [] false
(fn.params.map (·.name)) [] fn.body internalFunctions = Except.ok bodyStmts)
(hcompileFn : compileFunctionSpec model.fields model.events model.errors [] sel fn
(internalFunctions := internalFunctions) = Except.ok irFn)
(hbind : SourceSemantics.bindSupportedParams fn.params tx.args = some bindings)
(hcompiledBodyFuel : (genParamLoads fn.params ++ bodyStmts).length + extraFuel =
sizeOf (Function.compiledFunctionIR sel fn returns bodyStmts).body + irFuelSlack)
(hbodyCorrect : SupportedFunctionBodyWithHelpersAndHelperIRPreservationGoal runtimeContract
model fn bodyStmts hSupported.helperFuel tx initialWorld
(ParamLoading.applyBindingsToIRState (Function.prebindRawArgs
(FunctionBody.initialIRStateForTx model tx initialWorld) fn.params) bindings)
bindings extraFuel)
(hinv : InternalTableNamesReserved runtimeContract) :
FunctionBody.sourceResultMatchesIRResult
(supportedSourceFunctionSemantics model selectors hSupported fn tx initialWorld)
(execIRFunctionWithInternals runtimeContract irFuelSlack irFn tx.args
(FunctionBody.initialIRStateForTx model tx initialWorld)) :=
compileFunctionSpec_correct_generic_with_helper_proofs_and_helper_ir_of_body_goal
model selectors hSupported hHelperProofs hvalidateInputs runtimeContract fn sel returns
bodyStmts irFn tx initialWorld htxNormalized bindings internalFunctions irFuelSlack extraFuel
hcalldataSizeFits hfn hvalidate hreturns hbodyCompile hcompileFn hbind hcompiledBodyFuel
hbodyCorrect
(Function.genParamLoads_callsDisjoint_of_reserved runtimeContract fn.params
(supported_params_of_supportedSpec model selectors hSupported fn hfn) hinv)
/-- Whole-contract-facing consumer seam. Rather than taking the runtime
contract's naming invariant `InternalTableNamesInternalPrefixed` as an opaque
premise, this variant derives it from the structural compilation fact that every
statement in the runtime internal table is a `compileInternalFunction` output.
This is the concrete step that threads PR #2117's `WithInternals` selector seam
into the whole-contract dispatch path: once the dispatch proof exhibits the
internal table as compiled internal helpers, the per-function obligation flows
into `execIRFunctionWithInternals` with no hand-supplied disjointness witness and
no `internalFunctions = []` default-empty helper-world assumption. -/
theorem compileFunctionSpec_correct_generic_with_helper_proofs_and_helper_ir_of_body_goal_of_compiledInternalTable
(model : CompilationModel) (selectors : List Nat) (hSupported : SupportedSpec model selectors)
(hHelperProofs : SourceSemantics.SupportedSpecHelperProofs model selectors hSupported)
(hvalidateInputs : validateCompileInputs model selectors = Except.ok ())
(runtimeContract : IRContract) (fn : FunctionSpec) (sel : Nat) (returns : List ParamType)
(bodyStmts : List YulStmt) (irFn : IRFunction) (tx : IRTransaction)
(initialWorld : Verity.ContractState) (htxNormalized : Function.TxContextNormalized tx)
(bindings : List (String × Nat)) (irFuelSlack : Nat) (extraFuel : Nat)
(hcalldataSizeFits : Function.TxCalldataSizeFitsEvm tx)
(hfn : fn ∈ selectorDispatchedFunctions model)
(hvalidate : validateFunctionSpec fn = Except.ok ())
(hreturns : functionReturns fn = Except.ok returns)
(hbodyCompile : compileStmtList model.fields model.events model.errors .calldata [] false
(fn.params.map (·.name)) [] fn.body = Except.ok bodyStmts)
(hcompileFn :
compileFunctionSpec model.fields model.events model.errors [] sel fn = Except.ok irFn)
(hbind : SourceSemantics.bindSupportedParams fn.params tx.args = some bindings)
(hcompiledBodyFuel : (genParamLoads fn.params ++ bodyStmts).length + extraFuel =
sizeOf (Function.compiledFunctionIR sel fn returns bodyStmts).body + irFuelSlack)
(hbodyCorrect : SupportedFunctionBodyWithHelpersAndHelperIRPreservationGoal runtimeContract
model fn bodyStmts hSupported.helperFuel tx initialWorld
(ParamLoading.applyBindingsToIRState (Function.prebindRawArgs
(FunctionBody.initialIRStateForTx model tx initialWorld) fn.params) bindings)
bindings extraFuel)
(hcompiledTable : ∀ stmt ∈ runtimeContract.internalFunctions,
∃ (fields : List Field) (events : List EventDef) (errors : List ErrorDef)
(adtTypes : List AdtTypeDef) (spec : FunctionSpec)
(targetFork : Verity.Core.Intrinsics.HardFork) (internalFunctions : List FunctionSpec),
compileInternalFunction fields events errors adtTypes spec targetFork internalFunctions
= Except.ok stmt) :
FunctionBody.sourceResultMatchesIRResult
(supportedSourceFunctionSemantics model selectors hSupported fn tx initialWorld)
(execIRFunctionWithInternals runtimeContract irFuelSlack irFn tx.args
(FunctionBody.initialIRStateForTx model tx initialWorld)) :=
compileFunctionSpec_correct_generic_with_helper_proofs_and_helper_ir_of_body_goal_of_internalNamesPrefixed
model selectors hSupported hHelperProofs hvalidateInputs runtimeContract fn sel returns
bodyStmts irFn tx initialWorld htxNormalized bindings [] irFuelSlack extraFuel
hcalldataSizeFits hfn hvalidate hreturns hbodyCompile hcompileFn hbind hcompiledBodyFuel
hbodyCorrect
(Function.InternalTableNamesInternalPrefixed_of_all_compiledInternal runtimeContract
hcompiledTable)
/-- Generalized `Forall₂` bridge for the internal-function compilation `mapM`.
Unlike `compiled_internal_functions_forall₂_of_mapM_ok` (which fixes
`adtTypes = []` and the default `targetFork`/`internalFunctions` arguments), this
matches the exact call shape used by `compileValidatedCore` when it populates the
`internalFuncDefs` segment of the runtime internal table:
`internalFns.mapM (fun fn => compileInternalFunction fields events errors adtTypes fn targetFork internalFns)`. -/
private theorem compiled_internal_functions_forall₂_of_mapM_ok'
(fields : List Field)
(events : List EventDef)
(errors : List ErrorDef)
(adtTypes : List AdtTypeDef)
(targetFork : Verity.Core.Intrinsics.HardFork)
(internalFns : List FunctionSpec) :
∀ (entries : List FunctionSpec) internalDefs,
(entries.mapM (fun fn =>
compileInternalFunction fields events errors adtTypes fn targetFork internalFns)) =
Except.ok internalDefs →
List.Forall₂
(fun fn internalDef =>
compileInternalFunction fields events errors adtTypes fn targetFork internalFns =
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 adtTypes entry targetFork internalFns with
_ | internalDef
· simp only [List.mapM_cons, hstep, bind, Except.bind] at hmap
cases hmap
· rcases htail :
List.mapM (fun fn =>
compileInternalFunction fields events errors adtTypes fn targetFork internalFns)
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)
/-- Mirror of `exists_right_of_forall₂_mem_left`: a member of the right list of a
`Forall₂` witnesses a related member on the left. -/
private theorem exists_left_of_forall₂_mem_right
{α β : Type}
{R : α → β → Prop}
{xs : List α}
{ys : List β}
(hrel : List.Forall₂ R xs ys)
{y : β}
(hmem : y ∈ ys) :
∃ x, x ∈ xs ∧ 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 ⟨headX, by simp, hhead⟩
· rcases ih hmemTail with ⟨x, hx, hRx⟩
exact ⟨x, by simp [hx], hRx⟩
/-- Whole-contract legacy-compatibility bridge for a concrete `compileValidatedCore`
output. Given the per-statement compiled legacy-compatibility interface
(`StmtListCompiledLegacyCompatible`) for every selector-dispatched function body,
the emitted runtime contract's external bodies all stay inside the
legacy-compatible external Yul subset (`LegacyCompatibleExternalBodies`).
This reduces the `LegacyCompatibleExternalBodies` premise still carried by the
helper-aware whole-contract retarget theorem
(`compile_preserves_semantics_with_helper_proofs_and_helper_ir_of_compileValidatedCore`)
to the same per-statement legacy obligations that #2080 already tracks for the
disjoint interface, closing the whole-contract plumbing for the body-shape half. -/
theorem legacyCompatibleExternalBodies_of_compileValidatedCore_of_interface
(model : CompilationModel)
(selectors : List Nat)
(hSupported : SupportedSpec model selectors)
(ir : IRContract)
(hcore : compileValidatedCore model selectors = Except.ok ir)
(hbodies :
∀ entry ∈ SourceSemantics.selectorFunctionPairs model selectors,
StmtListCompiledLegacyCompatible model.fields
(entry.1.params.map (·.name)) entry.1.body) :
LegacyCompatibleExternalBodies ir := by
have hforall₂ :=
compileValidatedCore_ok_yields_compiled_functions model selectors hSupported ir hcore
intro fn hfn
obtain ⟨⟨spec, sel⟩, hentry, hcompileEntry⟩ :=
exists_left_of_forall₂_mem_right hforall₂ hfn
have hfnDispatched : spec ∈ selectorDispatchedFunctions model := by
simpa [SourceSemantics.selectorFunctionPairs] using (List.of_mem_zip hentry).1
have hparams : ∀ param ∈ spec.params, SupportedExternalScalarParamType param.ty :=
supported_params_of_supportedSpec model selectors hSupported spec hfnDispatched
have hcompileEntry' :
compileFunctionSpec model.fields [] [] [] sel spec = Except.ok fn := by
rw [← hSupported.noEvents, ← hSupported.noErrors]
exact hcompileEntry
exact Function.compileFunctionSpec_body_legacyCompatible_of_interface
(habi := (hSupported.supportedFunctionOfSelectorDispatched hfnDispatched).standardAbi)
model.fields sel spec fn hparams (hbodies (spec, sel) hentry) hcompileEntry'
/-- Structural compiled-internal-table premise, derived from the compilation
pipeline's `mapM` step. Every statement produced by
`internalFns.mapM compileInternalFunction` is a `compileInternalFunction` output