-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathFunction.lean
More file actions
4306 lines (4242 loc) · 222 KB
/
Function.lean
File metadata and controls
4306 lines (4242 loc) · 222 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.CompilationModel.Dispatch
import Compiler.Proofs.IRGeneration.FunctionBody
import Compiler.Proofs.IRGeneration.GenericInduction
import Compiler.Proofs.IRGeneration.ParamLoading
import Compiler.Proofs.IRGeneration.SupportedSpec
import Compiler.Proofs.YulGeneration.Equivalence
namespace Compiler.Proofs.IRGeneration
open Compiler
open Compiler.CompilationModel
open Compiler.Yul
namespace Function
/-- Generic transaction well-formedness needed by the current param-loading proof:
the ABI-style calldata byte size must not wrap modulo the EVM word modulus. -/
def TxCalldataSizeFitsEvm (tx : IRTransaction) : Prop :=
4 + tx.args.length * 32 < Compiler.Constants.evmModulus
/-- Source-side transaction context stores addresses as 160-bit values and the
remaining observed environment fields as `Uint256`s. The generic Layer-2 proof
therefore needs the IR transaction payload to already fit those bounds. -/
def TxContextNormalized (tx : IRTransaction) : Prop :=
tx.sender < Compiler.Constants.addressModulus ∧
tx.thisAddress < Compiler.Constants.addressModulus ∧
tx.msgValue < Compiler.Constants.evmModulus ∧
tx.blockTimestamp < Compiler.Constants.evmModulus ∧
tx.blockNumber < Compiler.Constants.evmModulus ∧
tx.chainId < Compiler.Constants.evmModulus
def compiledFunctionIR
(selector : Nat) (spec : FunctionSpec) (returns : List ParamType) (bodyStmts : List YulStmt) :
IRFunction :=
{ name := spec.name
selector := selector
params := spec.params.map Param.toIRParam
ret := match returns with
| [single] => single.toIRType
| _ => IRType.unit
payable := spec.isPayable
body := genParamLoads spec.params ++ bodyStmts }
def prebindRawArgs (state : IRState) (params : List Param) : IRState :=
(params.map Param.toIRParam).zip state.calldata |>.foldl
(fun s (p, v) => s.setVar p.name v)
state
def rawArgBindings (params : List Param) (args : List Nat) : List (String × Nat) :=
((params.map Param.toIRParam).zip args).map (fun entry => (entry.1.name, entry.2))
private theorem yulStmtList_length_le_sizeOf : (stmts : List YulStmt) → stmts.length ≤ sizeOf stmts
| [] => by simp
| _ :: rest => by
have hrest := yulStmtList_length_le_sizeOf rest
simp
omega
private theorem compiledFunctionIR_body_length_le_sizeOf
(selector : Nat) (spec : FunctionSpec) (returns : List ParamType) (bodyStmts : List YulStmt) :
(compiledFunctionIR selector spec returns bodyStmts).body.length + 1 ≤
sizeOf (compiledFunctionIR selector spec returns bodyStmts).body + 1 := by
simpa [compiledFunctionIR] using Nat.add_le_add_right
(yulStmtList_length_le_sizeOf (genParamLoads spec.params ++ bodyStmts)) 1
private theorem yulStmtList_extraFuel_append_ge
(pre body : List YulStmt) :
sizeOf (pre ++ body) - (pre ++ body).length ≥ sizeOf body - body.length := by
induction pre with
| nil =>
simp
| cons stmt rest ih =>
simp [List.length_append] at ih ⊢
omega
@[simp] theorem prebindRawArgs_calldata (state : IRState) (params : List Param) :
(prebindRawArgs state params).calldata = state.calldata := by
unfold prebindRawArgs
induction (params.map Param.toIRParam).zip state.calldata generalizing state with
| nil =>
rfl
| cons entry rest ih =>
simpa [List.foldl, IRState.setVar] using ih (state.setVar entry.1.name entry.2)
theorem prebindRawArgs_exact_rawArgBindings
{state : IRState}
{bindings0 : List (String × Nat)}
(hexact : FunctionBody.bindingsExactlyMatchIRVars bindings0 state)
(params : List Param) :
FunctionBody.bindingsExactlyMatchIRVars
((rawArgBindings params state.calldata).foldl
(fun acc entry => SourceSemantics.bindValue acc entry.1 entry.2) bindings0)
(prebindRawArgs state params) := by
unfold prebindRawArgs rawArgBindings
induction (List.zip (List.map Param.toIRParam params) state.calldata) generalizing bindings0 state with
| nil =>
simpa
| cons entry rest ih =>
have hstep :
FunctionBody.bindingsExactlyMatchIRVars
(SourceSemantics.bindValue bindings0 entry.1.name entry.2)
(state.setVar entry.1.name entry.2) :=
FunctionBody.bindingsExactlyMatchIRVars_setVar_bindValue hexact entry.1.name entry.2
simpa [List.foldl, SourceSemantics.bindValue] using
ih (bindings0 := SourceSemantics.bindValue bindings0 entry.1.name entry.2)
(state := state.setVar entry.1.name entry.2) hstep
theorem rawArgBindings_names_of_length_le
(params : List Param)
(args : List Nat)
(hlen : params.length ≤ args.length) :
(rawArgBindings params args).map Prod.fst = params.map Param.name := by
induction params generalizing args with
| nil =>
simp [rawArgBindings]
| cons param rest ih =>
cases args with
| nil =>
cases Nat.not_succ_le_zero _ hlen
| cons arg restArgs =>
have htail :
(rawArgBindings rest restArgs).map Prod.fst = rest.map Param.name :=
ih restArgs (Nat.le_of_succ_le_succ hlen)
have hcons :
param.toIRParam.name = param.name ∧
(rawArgBindings rest restArgs).map Prod.fst = rest.map Param.name :=
⟨rfl, htail⟩
simpa [rawArgBindings] using hcons
theorem rawArgBindings_names_of_bindSupportedParams
{params : List Param}
{args : List Nat}
{bindings : List (String × Nat)}
(hbind : SourceSemantics.bindSupportedParams params args = some bindings) :
(rawArgBindings params args).map Prod.fst = bindings.map Prod.fst := by
rw [rawArgBindings_names_of_length_le params args
(ParamLoading.bindSupportedParams_some_length hbind)]
symm
exact ParamLoading.bindSupportedParams_names hbind
def execResultToIRResult (initialState : IRState) : IRExecResult → IRResult
| .continue s =>
{ success := true
returnValue := s.returnValue
finalStorage := s.storage
finalMappings := Compiler.Proofs.storageAsMappings s.storage
events := s.events }
| .return v s =>
{ success := true
returnValue := some v
finalStorage := s.storage
finalMappings := Compiler.Proofs.storageAsMappings s.storage
events := s.events }
| .stop s =>
{ success := true
returnValue := none
finalStorage := s.storage
finalMappings := Compiler.Proofs.storageAsMappings s.storage
events := s.events }
| .revert _ =>
{ success := false
returnValue := none
finalStorage := initialState.storage
finalMappings := Compiler.Proofs.storageAsMappings initialState.storage
events := initialState.events }
theorem compileFunctionSpec_ok_of_components
(fields : List Field) (events : List EventDef) (errors : List ErrorDef)
(selector : Nat) (spec : FunctionSpec)
(returns : List ParamType) (bodyStmts : List YulStmt)
(hvalidate : validateFunctionSpec spec = Except.ok ())
(hreturns : functionReturns spec = Except.ok returns)
(hbody :
compileStmtList fields events errors .calldata [] false
(spec.params.map (·.name)) spec.body = Except.ok bodyStmts) :
compileFunctionSpec fields events errors selector spec =
Except.ok (compiledFunctionIR selector spec returns bodyStmts) := by
unfold CompilationModel.compileFunctionSpec
rw [hvalidate, hreturns, hbody]
rfl
theorem compileFunctionSpec_ok_params
(fields : List Field) (events : List EventDef) (errors : List ErrorDef)
(selector : Nat) (spec : FunctionSpec) (irFn : IRFunction)
(hcompile : compileFunctionSpec fields events errors selector spec = Except.ok irFn) :
irFn.params = spec.params.map Param.toIRParam := by
unfold CompilationModel.compileFunctionSpec at hcompile
cases hvalidate : validateFunctionSpec spec
· rw [hvalidate] at hcompile
cases hcompile
case ok _ =>
cases hreturns : functionReturns spec
· rw [hvalidate, hreturns] at hcompile
cases hcompile
case ok returns =>
cases hbody :
compileStmtList fields events errors .calldata [] false
(spec.params.map (·.name)) spec.body
· rw [hvalidate, hreturns, hbody] at hcompile
cases hcompile
case ok bodyStmts =>
rw [hvalidate, hreturns, hbody] at hcompile
injection hcompile with hEq
simpa using congrArg IRFunction.params hEq.symm
theorem compileFunctionSpec_ok_selector
(fields : List Field) (events : List EventDef) (errors : List ErrorDef)
(selector : Nat) (spec : FunctionSpec) (irFn : IRFunction)
(hcompile : compileFunctionSpec fields events errors selector spec = Except.ok irFn) :
irFn.selector = selector := by
unfold CompilationModel.compileFunctionSpec at hcompile
cases hvalidate : validateFunctionSpec spec
· rw [hvalidate] at hcompile
cases hcompile
case ok _ =>
cases hreturns : functionReturns spec
· rw [hvalidate, hreturns] at hcompile
cases hcompile
case ok returns =>
cases hbody :
compileStmtList fields events errors .calldata [] false
(spec.params.map (·.name)) spec.body
· rw [hvalidate, hreturns, hbody] at hcompile
cases hcompile
case ok bodyStmts =>
rw [hvalidate, hreturns, hbody] at hcompile
injection hcompile with hEq
simpa using congrArg IRFunction.selector hEq.symm
theorem compileFunctionSpec_ok_components
(fields : List Field) (events : List EventDef) (errors : List ErrorDef)
(selector : Nat) (spec : FunctionSpec) (irFn : IRFunction)
(hcompile : compileFunctionSpec fields events errors selector spec = Except.ok irFn) :
∃ returns bodyStmts,
validateFunctionSpec spec = Except.ok () ∧
functionReturns spec = Except.ok returns ∧
compileStmtList fields events errors .calldata [] false
(spec.params.map (·.name)) spec.body = Except.ok bodyStmts ∧
irFn = compiledFunctionIR selector spec returns bodyStmts := by
unfold CompilationModel.compileFunctionSpec at hcompile
cases hvalidate : validateFunctionSpec spec
· rw [hvalidate] at hcompile
cases hcompile
case ok _ =>
cases hreturns : functionReturns spec
· rw [hvalidate, hreturns] at hcompile
cases hcompile
case ok returns =>
cases hbody :
compileStmtList fields events errors .calldata [] false
(spec.params.map (·.name)) spec.body
· rw [hvalidate, hreturns, hbody] at hcompile
cases hcompile
case ok bodyStmts =>
rw [hvalidate, hreturns, hbody] at hcompile
injection hcompile with hEq
refine ⟨returns, bodyStmts, ?_⟩
exact ⟨by simpa using hvalidate, by simpa using hreturns,
by simpa using hbody, hEq.symm⟩
theorem exec_compiledFunctionIR_of_body
(state : IRState) (selector : Nat) (spec : FunctionSpec)
(returns : List ParamType) (bodyStmts : List YulStmt)
(bindings : List (String × Nat)) (tailResult : IRExecResult)
(hsupported : ∀ param ∈ spec.params, SupportedExternalParamType param.ty)
(hcalldataSizeFits : 4 + state.calldata.length * 32 < Compiler.Constants.evmModulus)
(hbind : SourceSemantics.bindSupportedParams spec.params state.calldata = some bindings)
(hbody :
execIRStmts (bodyStmts.length + 1)
(ParamLoading.applyBindingsToIRState (prebindRawArgs state spec.params) bindings)
bodyStmts = tailResult) :
Compiler.Proofs.YulGeneration.execIRFunctionFuel
((genParamLoads spec.params ++ bodyStmts).length + 1)
(compiledFunctionIR selector spec returns bodyStmts)
state.calldata state =
execResultToIRResult state tailResult := by
let preboundState := prebindRawArgs state spec.params
have hbind' :
SourceSemantics.bindSupportedParams spec.params preboundState.calldata = some bindings := by
simpa [preboundState] using hbind
have hcalldataSizeFits' :
4 + preboundState.calldata.length * 32 < Compiler.Constants.evmModulus := by
simpa [preboundState] using hcalldataSizeFits
have hprefix :
execIRStmts ((genParamLoads spec.params ++ bodyStmts).length + 1) preboundState
(genParamLoads spec.params ++ bodyStmts) =
execIRStmts (bodyStmts.length + 1)
(ParamLoading.applyBindingsToIRState preboundState bindings)
bodyStmts := by
simpa [preboundState, List.length_append, Nat.add_assoc] using
ParamLoading.exec_genParamLoads_supported_then
(state := preboundState)
(params := spec.params)
(bindings := bindings)
(rest := bodyStmts)
hsupported hcalldataSizeFits' hbind'
have hprefix' :
execIRStmts ((genParamLoads spec.params).length + bodyStmts.length + 1) preboundState
(genParamLoads spec.params ++ bodyStmts) =
execIRStmts (bodyStmts.length + 1)
(ParamLoading.applyBindingsToIRState preboundState bindings)
bodyStmts := by
simpa [List.length_append, Nat.add_assoc] using hprefix
have hprebound :
List.foldl (fun s x => s.setVar x.1.name x.2) state
((List.map Param.toIRParam spec.params).zip state.calldata) = preboundState := by
rfl
unfold Compiler.Proofs.YulGeneration.execIRFunctionFuel
unfold Compiler.Proofs.YulGeneration.execIRStmtsFuel
simp [compiledFunctionIR, execResultToIRResult]
rw [hprebound]
rw [hprefix', hbody]
rfl
theorem exec_compiledFunctionIR_of_body_extraFuel
(state : IRState) (selector : Nat) (spec : FunctionSpec)
(returns : List ParamType) (bodyStmts : List YulStmt)
(bindings : List (String × Nat)) (tailResult : IRExecResult)
(extraFuel : Nat)
(hsupported : ∀ param ∈ spec.params, SupportedExternalParamType param.ty)
(hcalldataSizeFits : 4 + state.calldata.length * 32 < Compiler.Constants.evmModulus)
(hbind : SourceSemantics.bindSupportedParams spec.params state.calldata = some bindings)
(hbody :
execIRStmts (bodyStmts.length + extraFuel + 1)
(ParamLoading.applyBindingsToIRState (prebindRawArgs state spec.params) bindings)
bodyStmts = tailResult) :
Compiler.Proofs.YulGeneration.execIRFunctionFuel
((genParamLoads spec.params ++ bodyStmts).length + extraFuel + 1)
(compiledFunctionIR selector spec returns bodyStmts)
state.calldata state =
execResultToIRResult state tailResult := by
let preboundState := prebindRawArgs state spec.params
have hbind' :
SourceSemantics.bindSupportedParams spec.params preboundState.calldata = some bindings := by
simpa [preboundState] using hbind
have hcalldataSizeFits' :
4 + preboundState.calldata.length * 32 < Compiler.Constants.evmModulus := by
simpa [preboundState] using hcalldataSizeFits
have hprefix :
execIRStmts ((genParamLoads spec.params ++ bodyStmts).length + extraFuel + 1) preboundState
(genParamLoads spec.params ++ bodyStmts) =
execIRStmts (bodyStmts.length + extraFuel + 1)
(ParamLoading.applyBindingsToIRState preboundState bindings)
bodyStmts := by
simpa [preboundState, List.length_append, Nat.add_assoc] using
ParamLoading.exec_genParamLoads_supported_then_extraFuel
(state := preboundState)
(params := spec.params)
(bindings := bindings)
(rest := bodyStmts)
(extraFuel := extraFuel)
hsupported hcalldataSizeFits' hbind'
have hprefix' :
execIRStmts ((genParamLoads spec.params).length + bodyStmts.length + extraFuel + 1) preboundState
(genParamLoads spec.params ++ bodyStmts) =
execIRStmts (bodyStmts.length + extraFuel + 1)
(ParamLoading.applyBindingsToIRState preboundState bindings)
bodyStmts := by
simpa [List.length_append, Nat.add_assoc] using hprefix
have hprebound :
List.foldl (fun s x => s.setVar x.1.name x.2) state
((List.map Param.toIRParam spec.params).zip state.calldata) = preboundState := by
rfl
unfold Compiler.Proofs.YulGeneration.execIRFunctionFuel
unfold Compiler.Proofs.YulGeneration.execIRStmtsFuel
simp [compiledFunctionIR, execResultToIRResult]
rw [hprebound]
rw [hprefix', hbody]
rfl
theorem interpretFunction_eq_execResultToIRResult_of_body
(model : CompilationModel) (fn : FunctionSpec)
(tx : IRTransaction) (initialWorld : Verity.ContractState)
(sourceResult : SourceSemantics.StmtResult)
(rollback : IRState) (irResult : IRExecResult)
(bindings : List (String × Nat))
(hbind :
SourceSemantics.bindSupportedParams fn.params tx.args = some bindings)
(hsource :
SourceSemantics.execStmtList (SourceSemantics.effectiveFields model)
{ world := SourceSemantics.withTransactionContext initialWorld tx
bindings := bindings }
fn.body = sourceResult)
(hrollbackStorage :
rollback.storage =
SourceSemantics.encodeStorage model
(SourceSemantics.withTransactionContext initialWorld tx))
(hrollbackEvents :
rollback.events =
SourceSemantics.encodeEvents
(SourceSemantics.withTransactionContext initialWorld tx).events)
(hmatch :
FunctionBody.stmtResultMatchesIRExec
(SourceSemantics.effectiveFields model) sourceResult irResult) :
FunctionBody.sourceResultMatchesIRResult
(SourceSemantics.interpretFunction model fn tx initialWorld)
(execResultToIRResult rollback irResult) := by
have hpack :=
FunctionBody.stmtResultToSourceResult_matches_irExecResult
(spec := model)
(fields := SourceSemantics.effectiveFields model)
(initialWorld := SourceSemantics.withTransactionContext initialWorld tx)
(rollback := rollback)
(sourceResult := sourceResult)
(irResult := irResult)
hrollbackStorage hrollbackEvents rfl hmatch
simpa [SourceSemantics.interpretFunction, hbind, hsource,
FunctionBody.stmtResultToSourceResult, FunctionBody.sourceResultMatchesIRResult,
FunctionBody.irResultOfExecResult, execResultToIRResult] using hpack
theorem runtimeStateMatchesIR_applyBindingsToIRState
{fields : List Field}
{runtime : SourceSemantics.RuntimeState}
{state : IRState}
(hmatch : FunctionBody.runtimeStateMatchesIR fields runtime state)
(bindings : List (String × Nat)) :
FunctionBody.runtimeStateMatchesIR fields runtime
(ParamLoading.applyBindingsToIRState state bindings) := by
induction bindings generalizing state with
| nil =>
simpa [ParamLoading.applyBindingsToIRState]
| cons entry rest ih =>
simpa [ParamLoading.applyBindingsToIRState, IRState.setVar] using
ih (state := state.setVar entry.1 entry.2) hmatch
theorem runtimeStateMatchesIR_prebindRawArgs
{fields : List Field}
{runtime : SourceSemantics.RuntimeState}
{state : IRState}
(hmatch : FunctionBody.runtimeStateMatchesIR fields runtime state)
(params : List Param) :
FunctionBody.runtimeStateMatchesIR fields runtime (prebindRawArgs state params) := by
unfold prebindRawArgs
induction (List.zip (List.map Param.toIRParam params) state.calldata) generalizing state with
| nil =>
simpa
| cons entry rest ih =>
simpa [List.foldl, IRState.setVar] using
ih (state := state.setVar entry.1.name entry.2) hmatch
/-- Folding `bindValue` over names that never mention `queryName` leaves its
lookup unchanged. -/
private theorem lookupBinding?_foldl_bindValue_not_mem
(bindings : List (String × Nat))
(init : List (String × Nat))
(queryName : String)
(hnotmem : queryName ∉ bindings.map Prod.fst) :
FunctionBody.lookupBinding?
(bindings.foldl (fun acc entry => SourceSemantics.bindValue acc entry.1 entry.2) init)
queryName =
FunctionBody.lookupBinding? init queryName := by
induction bindings generalizing init with
| nil =>
simp
| cons entry rest ih =>
have hqueryNe : queryName ≠ entry.1 := by
intro hEq
apply hnotmem
simp [hEq]
have hrestNotMem : queryName ∉ rest.map Prod.fst := by
intro hmem
apply hnotmem
simp [hmem]
rw [List.foldl, ih _ hrestNotMem,
FunctionBody.lookupBinding?_bindValue_ne init entry.1 queryName entry.2 hqueryNe]
/-- If a name occurs in the final binding list with unique parameter names,
folding `bindValue` over any initial environment reconstructs that lookup
exactly. -/
private theorem lookupBinding?_foldl_bindValue_mem
(bindings : List (String × Nat))
(init : List (String × Nat))
(queryName : String)
(hmem : queryName ∈ bindings.map Prod.fst)
(hnodup : (bindings.map Prod.fst).Nodup) :
FunctionBody.lookupBinding?
(bindings.foldl (fun acc entry => SourceSemantics.bindValue acc entry.1 entry.2) init)
queryName =
FunctionBody.lookupBinding? bindings queryName := by
induction bindings generalizing init with
| nil =>
cases hmem
| cons entry rest ih =>
simp only [List.map_cons, List.nodup_cons] at hnodup
rcases hnodup with ⟨hheadNotMem, hrestNodup⟩
simp only [List.map_cons, List.mem_cons] at hmem
rcases hmem with hEq | hmemRest
· subst hEq
have hrestNotMem : entry.1 ∉ rest.map Prod.fst := hheadNotMem
rw [List.foldl]
rw [lookupBinding?_foldl_bindValue_not_mem rest
(SourceSemantics.bindValue init entry.1 entry.2) entry.1 hrestNotMem]
rw [FunctionBody.lookupBinding?_bindValue_eq]
unfold FunctionBody.lookupBinding?
simp
· have hqueryNe : queryName ≠ entry.1 := by
intro hEq
apply hheadNotMem
simpa [hEq] using hmemRest
rw [List.foldl]
rw [ih (SourceSemantics.bindValue init entry.1 entry.2) hmemRest hrestNodup]
have hbeq : (entry.1 == queryName) = false := by
have hqueryNe' : ¬ entry.1 = queryName := by
intro hEq
exact hqueryNe hEq.symm
simp [hqueryNe']
unfold FunctionBody.lookupBinding?
simp [List.find?, hbeq]
/-- The raw prebinding fold starts from an empty environment, so names absent
from the raw ABI prefix stay absent afterwards. -/
private theorem lookupBinding?_rawArgBindings_fold_not_mem
(params : List Param)
(args : List Nat)
(queryName : String)
(hnotmem : queryName ∉ (rawArgBindings params args).map Prod.fst) :
FunctionBody.lookupBinding?
((rawArgBindings params args).foldl
(fun acc entry => SourceSemantics.bindValue acc entry.1 entry.2) [])
queryName = none := by
rw [lookupBinding?_foldl_bindValue_not_mem (rawArgBindings params args) [] queryName hnotmem]
simp [FunctionBody.lookupBinding?]
private theorem lookupBinding?_eq_none_of_not_mem
(bindings : List (String × Nat))
(queryName : String)
(hnotmem : queryName ∉ bindings.map Prod.fst) :
FunctionBody.lookupBinding? bindings queryName = none := by
unfold FunctionBody.lookupBinding?
induction bindings with
| nil =>
simp
| cons entry rest ih =>
have hqueryNe : queryName ≠ entry.1 := by
intro hEq
apply hnotmem
simp [hEq]
have hrestNotMem : queryName ∉ rest.map Prod.fst := by
intro hmem
apply hnotmem
simp [hmem]
have hbeq : (entry.1 == queryName) = false := by
have hqueryNe' : ¬ entry.1 = queryName := by
intro hEq
exact hqueryNe hEq.symm
simp [hqueryNe']
simp [List.find?, hbeq, ih hrestNotMem]
private theorem lookupBinding?_some_of_mem
(bindings : List (String × Nat))
(queryName : String)
(hmem : queryName ∈ bindings.map Prod.fst) :
∃ value, FunctionBody.lookupBinding? bindings queryName = some value := by
induction bindings with
| nil =>
cases hmem
| cons entry rest ih =>
simp only [List.map_cons, List.mem_cons] at hmem
by_cases hEq : entry.1 = queryName
· refine ⟨entry.2, ?_⟩
subst hEq
unfold FunctionBody.lookupBinding?
simp
· rcases hmem with hhead | htail
· cases hEq hhead.symm
· rcases ih htail with ⟨value, hvalue⟩
refine ⟨value, ?_⟩
have hbeq : (entry.1 == queryName) = false := by
simp [hEq]
unfold FunctionBody.lookupBinding? at hvalue ⊢
cases hfind : List.find? (fun candidate => candidate.1 == queryName) rest with
| none =>
simp [List.find?, hbeq, hfind] at hvalue
| some found =>
cases found with
| mk foundName foundValue =>
simp [List.find?, hbeq, hfind] at hvalue ⊢
cases hvalue
rfl
/-- The initial IR state matches the source runtime once the transaction
context already fits the bounded source-level numeric domains. -/
theorem initialIRStateForTx_matches_runtime
(model : CompilationModel)
(tx : IRTransaction)
(initialWorld : Verity.ContractState)
(htxNormalized : TxContextNormalized tx) :
FunctionBody.runtimeStateMatchesIR
(SourceSemantics.effectiveFields model)
{ world := SourceSemantics.withTransactionContext initialWorld tx
bindings := [] }
(FunctionBody.initialIRStateForTx model tx initialWorld) := by
rcases htxNormalized with
⟨hsender, hthis, hmsgValue, htimestamp, hnumber, hchain⟩
have hsenderEvm : tx.sender < Compiler.Constants.evmModulus := by
dsimp [Compiler.Constants.addressModulus, Compiler.Constants.evmModulus] at hsender ⊢
omega
have hthisEvm : tx.thisAddress < Compiler.Constants.evmModulus := by
dsimp [Compiler.Constants.addressModulus, Compiler.Constants.evmModulus] at hthis ⊢
omega
have hsenderAddr : tx.sender < Verity.Core.Address.modulus := by
simpa [Verity.Core.Address.modulus, Compiler.Constants.addressModulus] using hsender
have hthisAddr : tx.thisAddress < Verity.Core.Address.modulus := by
simpa [Verity.Core.Address.modulus, Compiler.Constants.addressModulus] using hthis
refine ⟨?_, rfl, ?_, ?_, ?_, ?_, ?_, ?_, rfl, ?_⟩
· simpa [FunctionBody.initialIRStateForTx, SourceSemantics.effectiveFields,
SourceSemantics.encodeStorage] using
(FunctionBody.encodeStorage_withTransactionContext model initialWorld tx).symm
· simp [FunctionBody.initialIRStateForTx, SourceSemantics.withTransactionContext,
Verity.wordToAddress]
symm
calc
tx.sender % Compiler.Constants.evmModulus % Verity.Core.Address.modulus
= tx.sender % Verity.Core.Address.modulus := by
rw [Nat.mod_eq_of_lt hsenderEvm]
_ = tx.sender := Nat.mod_eq_of_lt hsenderAddr
· simp [FunctionBody.initialIRStateForTx, SourceSemantics.withTransactionContext]
symm
exact Nat.mod_eq_of_lt hmsgValue
· simp [FunctionBody.initialIRStateForTx, SourceSemantics.withTransactionContext,
Verity.wordToAddress]
symm
calc
tx.thisAddress % Compiler.Constants.evmModulus % Verity.Core.Address.modulus
= tx.thisAddress % Verity.Core.Address.modulus := by
rw [Nat.mod_eq_of_lt hthisEvm]
_ = tx.thisAddress := Nat.mod_eq_of_lt hthisAddr
· simp [FunctionBody.initialIRStateForTx, SourceSemantics.withTransactionContext]
symm
exact Nat.mod_eq_of_lt htimestamp
· simp [FunctionBody.initialIRStateForTx, SourceSemantics.withTransactionContext]
symm
exact Nat.mod_eq_of_lt hnumber
· simp [FunctionBody.initialIRStateForTx, SourceSemantics.withTransactionContext]
symm
exact Nat.mod_eq_of_lt hchain
· simp [FunctionBody.initialIRStateForTx, SourceSemantics.withTransactionContext]
-- SORRY'D: /-- The ABI parameter-loading prefix reconstructs exactly the decoded source
-- SORRY'D: bindings for any supported function with pairwise-distinct parameter names. -/
theorem supported_function_param_state_exact
(state : IRState)
(params : List Param)
(bindings : List (String × Nat))
(hinit : FunctionBody.bindingsExactlyMatchIRVars [] state)
(hparamNamesNodup : (params.map (·.name)).Nodup)
(hbind : SourceSemantics.bindSupportedParams params state.calldata = some bindings) :
FunctionBody.bindingsExactlyMatchIRVars bindings
(ParamLoading.applyBindingsToIRState (prebindRawArgs state params) bindings) := by
let rawInitBindings :=
(rawArgBindings params state.calldata).foldl
(fun acc entry => SourceSemantics.bindValue acc entry.1 entry.2) []
have hprebound :
FunctionBody.bindingsExactlyMatchIRVars rawInitBindings
(prebindRawArgs state params) := by
simpa [rawInitBindings] using prebindRawArgs_exact_rawArgBindings hinit params
have hfinal :
FunctionBody.bindingsExactlyMatchIRVars
(bindings.foldl (fun acc entry => SourceSemantics.bindValue acc entry.1 entry.2)
rawInitBindings)
(ParamLoading.applyBindingsToIRState (prebindRawArgs state params) bindings) :=
FunctionBody.bindingsExactlyMatchIRVars_applyBindingsToIRState hprebound
intro queryName
rw [hfinal queryName]
by_cases hmem : queryName ∈ bindings.map Prod.fst
· exact lookupBinding?_foldl_bindValue_mem bindings rawInitBindings queryName hmem
(ParamLoading.bindSupportedParams_names_nodup hparamNamesNodup hbind)
· rw [lookupBinding?_foldl_bindValue_not_mem bindings rawInitBindings queryName hmem]
have hrawNames :
(rawArgBindings params state.calldata).map Prod.fst = bindings.map Prod.fst :=
rawArgBindings_names_of_bindSupportedParams hbind
have hrawNotMem : queryName ∉ (rawArgBindings params state.calldata).map Prod.fst := by
rw [hrawNames]
exact hmem
rw [lookupBinding?_rawArgBindings_fold_not_mem params state.calldata queryName hrawNotMem]
symm
exact lookupBinding?_eq_none_of_not_mem bindings queryName hmem
theorem supported_function_body_correct_from_exact_state_core
(model : CompilationModel)
(fn : FunctionSpec)
(bodyStmts : List YulStmt)
(tx : IRTransaction)
(initialWorld : Verity.ContractState)
(state : IRState)
(bindings : List (String × Nat))
(hnormalized : SourceSemantics.effectiveFields model = model.fields)
(hnoEvents : model.events = [])
(hnoErrors : model.errors = [])
(hbind : SourceSemantics.bindSupportedParams fn.params tx.args = some bindings)
(hcore : FunctionBody.StmtListCompileCore (fn.params.map (·.name)) fn.body)
(hbodyCompile :
compileStmtList model.fields model.events model.errors .calldata [] false
(fn.params.map (·.name)) fn.body = Except.ok bodyStmts)
(hstateRuntime :
FunctionBody.runtimeStateMatchesIR
(SourceSemantics.effectiveFields model)
{ world := SourceSemantics.withTransactionContext initialWorld tx
bindings := [] }
state)
(hstateBindings :
FunctionBody.bindingsExactlyMatchIRVars bindings state) :
∃ sourceResult irExec,
SourceSemantics.execStmtList (SourceSemantics.effectiveFields model)
{ world := SourceSemantics.withTransactionContext initialWorld tx
bindings := bindings }
fn.body = sourceResult ∧
execIRStmts (bodyStmts.length + 1) state bodyStmts = irExec ∧
FunctionBody.stmtResultMatchesIRExec
(SourceSemantics.effectiveFields model) sourceResult irExec := by
have hscope :
FunctionBody.scopeNamesPresent (fn.params.map (·.name)) bindings := by
intro name hmem
have hmemBindings : name ∈ bindings.map Prod.fst := by
rw [ParamLoading.bindSupportedParams_names hbind]
simpa using hmem
exact lookupBinding?_some_of_mem bindings name hmemBindings
have hbounded : FunctionBody.bindingsBounded bindings :=
FunctionBody.bindingsBounded_of_bindSupportedParams hbind
have hstateRuntime' :
FunctionBody.runtimeStateMatchesIR
(SourceSemantics.effectiveFields model)
{ world := SourceSemantics.withTransactionContext initialWorld tx
bindings := bindings }
state := by
simpa [FunctionBody.runtimeStateMatchesIR] using hstateRuntime
have hbodyCompile' :
compileStmtList (SourceSemantics.effectiveFields model) [] []
.calldata [] false (fn.params.map (·.name)) fn.body = Except.ok bodyStmts := by
simpa [hnormalized, hnoEvents, hnoErrors] using hbodyCompile
rcases FunctionBody.exec_compileStmtList_core
(fields := SourceSemantics.effectiveFields model)
(runtime := { world := SourceSemantics.withTransactionContext initialWorld tx
bindings := bindings })
(state := state)
(scope := fn.params.map (·.name))
(inScopeNames := fn.params.map (·.name))
(stmts := fn.body)
hcore hscope hstateBindings hbounded hstateRuntime' with
⟨bodyIR, hbodyCoreCompile, hcoreSem, _⟩
have hbodyEq : bodyIR = bodyStmts := by
rw [hbodyCompile'] at hbodyCoreCompile
injection hbodyCoreCompile with hEq
exact hEq.symm
subst bodyIR
refine ⟨_, _, rfl, rfl, hcoreSem⟩
theorem supported_function_body_correct_from_exact_state_core_extraFuel
(model : CompilationModel)
(fn : FunctionSpec)
(bodyStmts : List YulStmt)
(tx : IRTransaction)
(initialWorld : Verity.ContractState)
(state : IRState)
(bindings : List (String × Nat))
(extraFuel : Nat)
(hnormalized : SourceSemantics.effectiveFields model = model.fields)
(hnoEvents : model.events = [])
(hnoErrors : model.errors = [])
(hbind : SourceSemantics.bindSupportedParams fn.params tx.args = some bindings)
(hcore : FunctionBody.StmtListCompileCore (fn.params.map (·.name)) fn.body)
(hbodyCompile :
compileStmtList model.fields model.events model.errors .calldata [] false
(fn.params.map (·.name)) fn.body = Except.ok bodyStmts)
(hstateRuntime :
FunctionBody.runtimeStateMatchesIR
(SourceSemantics.effectiveFields model)
{ world := SourceSemantics.withTransactionContext initialWorld tx
bindings := [] }
state)
(hstateBindings :
FunctionBody.bindingsExactlyMatchIRVars bindings state) :
∃ sourceResult irExec,
SourceSemantics.execStmtList (SourceSemantics.effectiveFields model)
{ world := SourceSemantics.withTransactionContext initialWorld tx
bindings := bindings }
fn.body = sourceResult ∧
execIRStmts (bodyStmts.length + extraFuel + 1) state bodyStmts = irExec ∧
FunctionBody.stmtResultMatchesIRExec
(SourceSemantics.effectiveFields model) sourceResult irExec := by
have hscope :
FunctionBody.scopeNamesPresent (fn.params.map (·.name)) bindings := by
intro name hmem
have hmemBindings : name ∈ bindings.map Prod.fst := by
rw [ParamLoading.bindSupportedParams_names hbind]
simpa using hmem
exact lookupBinding?_some_of_mem bindings name hmemBindings
have hbounded : FunctionBody.bindingsBounded bindings :=
FunctionBody.bindingsBounded_of_bindSupportedParams hbind
have hstateRuntime' :
FunctionBody.runtimeStateMatchesIR
(SourceSemantics.effectiveFields model)
{ world := SourceSemantics.withTransactionContext initialWorld tx
bindings := bindings }
state := by
simpa [FunctionBody.runtimeStateMatchesIR] using hstateRuntime
have hbodyCompile' :
compileStmtList (SourceSemantics.effectiveFields model) [] []
.calldata [] false (fn.params.map (·.name)) fn.body = Except.ok bodyStmts := by
simpa [hnormalized, hnoEvents, hnoErrors] using hbodyCompile
rcases FunctionBody.exec_compileStmtList_core_extraFuel
(fields := SourceSemantics.effectiveFields model)
(runtime := { world := SourceSemantics.withTransactionContext initialWorld tx
bindings := bindings })
(state := state)
(scope := fn.params.map (·.name))
(inScopeNames := fn.params.map (·.name))
(stmts := fn.body)
extraFuel hcore hscope hstateBindings hbounded hstateRuntime' with
⟨bodyIR, hbodyCoreCompile, hcoreSem, _⟩
have hbodyEq : bodyIR = bodyStmts := by
rw [hbodyCompile'] at hbodyCoreCompile
injection hbodyCoreCompile with hEq
exact hEq.symm
subst bodyIR
refine ⟨_, _, rfl, rfl, hcoreSem⟩
theorem supported_function_body_correct_from_exact_state_terminal_core_extraFuel
(model : CompilationModel)
(fn : FunctionSpec)
(bodyStmts : List YulStmt)
(tx : IRTransaction)
(initialWorld : Verity.ContractState)
(state : IRState)
(bindings : List (String × Nat))
(extraFuel : Nat)
(hextraFuel : sizeOf bodyStmts - bodyStmts.length ≤ extraFuel)
(hnormalized : SourceSemantics.effectiveFields model = model.fields)
(hnoEvents : model.events = [])
(hnoErrors : model.errors = [])
(hbind : SourceSemantics.bindSupportedParams fn.params tx.args = some bindings)
(hterminal : FunctionBody.StmtListTerminalCore (fn.params.map (·.name)) fn.body)
(hbodyCompile :
compileStmtList model.fields model.events model.errors .calldata [] false
(fn.params.map (·.name)) fn.body = Except.ok bodyStmts)
(hstateRuntime :
FunctionBody.runtimeStateMatchesIR
(SourceSemantics.effectiveFields model)
{ world := SourceSemantics.withTransactionContext initialWorld tx
bindings := [] }
state)
(hstateBindings :
FunctionBody.bindingsExactlyMatchIRVars bindings state) :
∃ sourceResult irExec,
SourceSemantics.execStmtList (SourceSemantics.effectiveFields model)
{ world := SourceSemantics.withTransactionContext initialWorld tx
bindings := bindings }
fn.body = sourceResult ∧
execIRStmts (bodyStmts.length + extraFuel + 1) state bodyStmts = irExec ∧
FunctionBody.stmtResultMatchesIRExec
(SourceSemantics.effectiveFields model) sourceResult irExec := by
have hscope :
FunctionBody.scopeNamesPresent (fn.params.map (·.name)) bindings := by
intro name hmem
have hmemBindings : name ∈ bindings.map Prod.fst := by
rw [ParamLoading.bindSupportedParams_names hbind]
simpa using hmem
exact lookupBinding?_some_of_mem bindings name hmemBindings
have hscopeExact :
FunctionBody.bindingsExactlyMatchIRVarsOnScope
(fn.params.map (·.name)) bindings state :=
FunctionBody.bindingsExactlyMatchIRVars_implies_onScope hstateBindings
have hbounded : FunctionBody.bindingsBounded bindings :=
FunctionBody.bindingsBounded_of_bindSupportedParams hbind
have hstateRuntime' :
FunctionBody.runtimeStateMatchesIR
(SourceSemantics.effectiveFields model)
{ world := SourceSemantics.withTransactionContext initialWorld tx
bindings := bindings }
state := by
simpa [FunctionBody.runtimeStateMatchesIR] using hstateRuntime
have hbodyCompile' :
compileStmtList (SourceSemantics.effectiveFields model) [] []
.calldata [] false (fn.params.map (·.name)) fn.body = Except.ok bodyStmts := by
simpa [hnormalized, hnoEvents, hnoErrors] using hbodyCompile
let sizeSlack := extraFuel - (sizeOf bodyStmts - bodyStmts.length)
rcases FunctionBody.exec_compileStmtList_terminal_core_sizeOf_extraFuel
(fields := SourceSemantics.effectiveFields model)
(runtime := { world := SourceSemantics.withTransactionContext initialWorld tx
bindings := bindings })
(state := state)
(scope := fn.params.map (·.name))
(inScopeNames := fn.params.map (·.name))
(stmts := fn.body)
(extraFuel := sizeSlack)
hterminal
FunctionBody.scopeNamesIncluded_refl
hscope
hscopeExact
hbounded
hstateRuntime' with
⟨bodyIR, hbodyTerminalCompile, hterminalSem⟩
have hbodyEq : bodyIR = bodyStmts := by
rw [hbodyCompile'] at hbodyTerminalCompile
injection hbodyTerminalCompile with hEq
exact hEq.symm
subst bodyIR
have hfuel :
sizeOf bodyStmts + sizeSlack + 1 =
bodyStmts.length + extraFuel + 1 := by
dsimp [sizeSlack]
have hlenle : bodyStmts.length ≤ sizeOf bodyStmts :=
yulStmtList_length_le_sizeOf bodyStmts
omega
refine ⟨_, _, rfl, rfl, ?_⟩
simpa [hfuel, sizeSlack] using hterminalSem
private theorem firstFieldWriteSlotConflict_eq_none_of_validateCompileInputs
{spec : CompilationModel}
{selectors : List Nat}
(hvalidate : validateCompileInputs spec selectors = Except.ok ()) :
firstFieldWriteSlotConflict
(applySlotAliasRanges spec.fields spec.slotAliasRanges) = none := by
exact validateCompileInputs_firstFieldWriteSlotConflict_eq_none hvalidate
theorem compileFunctionSpec_correct_of_body
(model : CompilationModel)
(selector : Nat) (fn : FunctionSpec) (irFn : IRFunction)
(returns : List ParamType) (bodyStmts : List YulStmt)
(tx : IRTransaction) (initialWorld : Verity.ContractState)
(sourceResult : SourceSemantics.StmtResult) (irExec : IRExecResult)
(bindings : List (String × Nat))
(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)
(hcompile : compileFunctionSpec model.fields model.events model.errors selector fn = Except.ok irFn)
(hparamsSupported : ∀ param ∈ fn.params, SupportedExternalParamType param.ty)
(hcalldataSizeFits : TxCalldataSizeFitsEvm tx)
(hbind : SourceSemantics.bindSupportedParams fn.params tx.args = some bindings)
(hsource :
SourceSemantics.execStmtList (SourceSemantics.effectiveFields model)
{ world := SourceSemantics.withTransactionContext initialWorld tx
bindings := bindings }
fn.body = sourceResult)
(hbodyExec :
execIRStmts (bodyStmts.length + 1)
(ParamLoading.applyBindingsToIRState
(prebindRawArgs (FunctionBody.initialIRStateForTx model tx initialWorld) fn.params)
bindings)
bodyStmts = irExec)
(hmatch :
FunctionBody.stmtResultMatchesIRExec
(SourceSemantics.effectiveFields model) sourceResult irExec) :
FunctionBody.sourceResultMatchesIRResult
(SourceSemantics.interpretFunction model fn tx initialWorld)
(Compiler.Proofs.YulGeneration.execIRFunctionFuel
((genParamLoads fn.params ++ bodyStmts).length + 1)
irFn tx.args (FunctionBody.initialIRStateForTx model tx initialWorld)) := by
let initialState := FunctionBody.initialIRStateForTx model tx initialWorld
have hcompiled :=
compileFunctionSpec_ok_of_components model.fields model.events model.errors
selector fn returns bodyStmts hvalidate hreturns hbodyCompile
have hirFn : irFn = compiledFunctionIR selector fn returns bodyStmts := by
rw [hcompile] at hcompiled
injection hcompiled with hirFn
have hrollbackStorage :
initialState.storage =
SourceSemantics.encodeStorage model
(SourceSemantics.withTransactionContext initialWorld tx) := by
simpa [initialState, FunctionBody.initialIRStateForTx] using
(FunctionBody.encodeStorage_withTransactionContext model initialWorld tx).symm
have hrollbackEvents :
initialState.events =
SourceSemantics.encodeEvents
(SourceSemantics.withTransactionContext initialWorld tx).events := by
simp [initialState, FunctionBody.initialIRStateForTx]
have hsourceMatch :=
interpretFunction_eq_execResultToIRResult_of_body
(model := model) (fn := fn) (tx := tx) (initialWorld := initialWorld)
(sourceResult := sourceResult) (rollback := initialState) (irResult := irExec)
(bindings := bindings) hbind hsource hrollbackStorage hrollbackEvents hmatch
have hcompiledExec :
Compiler.Proofs.YulGeneration.execIRFunctionFuel
((genParamLoads fn.params ++ bodyStmts).length + 1)
(compiledFunctionIR selector fn returns bodyStmts) tx.args initialState =
execResultToIRResult initialState irExec := by
exact exec_compiledFunctionIR_of_body
(state := initialState) (selector := selector) (spec := fn)
(returns := returns) (bodyStmts := bodyStmts) (bindings := bindings)
(tailResult := irExec) hparamsSupported hcalldataSizeFits hbind hbodyExec
subst hirFn
rw [hcompiledExec]
simpa [initialState] using hsourceMatch
theorem compileFunctionSpec_correct_of_body_normalized_extraFuel
(model : CompilationModel)
(hnormalized :
applySlotAliasRanges model.fields model.slotAliasRanges = model.fields)
(selector : Nat) (fn : FunctionSpec) (irFn : IRFunction)
(returns : List ParamType) (bodyStmts : List YulStmt)
(tx : IRTransaction) (initialWorld : Verity.ContractState)
(sourceResult : SourceSemantics.StmtResult) (irExec : IRExecResult)
(bindings : List (String × Nat)) (extraFuel : Nat)
(hvalidate : validateFunctionSpec fn = Except.ok ())
(hreturns : functionReturns fn = Except.ok returns)
(hbodyCompile :
compileStmtList
(applySlotAliasRanges model.fields model.slotAliasRanges)