-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCompilationModelFeatureTest.lean
More file actions
6427 lines (5765 loc) · 244 KB
/
CompilationModelFeatureTest.lean
File metadata and controls
6427 lines (5765 loc) · 244 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
import Compiler.ABI
import Compiler.Codegen
import Compiler.Modules.Calls
import Compiler.Modules.Callbacks
import Compiler.Modules.ERC4626
import Compiler.Modules.ERC20
import Compiler.Modules.Hashing
import Compiler.Modules.Oracle
import Compiler.Modules.Precompiles
import Compiler.Yul.PrettyPrint
import Contracts.Common
import Contracts.Counter.Counter
import Contracts.LocalObligationMacroSmoke.LocalObligationMacroSmoke
import Contracts.ProxyUpgradeabilityMacroSmoke
import Contracts.Smoke
import Contracts.StringArrayErrorSmoke
import Contracts.StringArrayEventSmoke
import Verity.Macro.Translate
-- The `unnecessarySeqFocus` linter recurses through every tactic block in the
-- file; on the very large smoke-test goals here it overflows the C stack
-- (SIGABRT 134 in `Batteries.Linter.UnnecessarySeqFocus.markUsedTacticsList`).
-- Disable it at file scope, matching the precedent in
-- `Compiler/Proofs/IRGeneration/GenericInduction.lean`.
set_option linter.unnecessarySeqFocus false
set_option linter.unusedTactic false
namespace Compiler.CompilationModelFeatureTest
open Compiler
open Compiler.CompilationModel
namespace MacroLocalObligationSmoke
open Contracts
def constructorCarriesUncheckedObligation : Bool :=
match LocalObligationMacroSmoke.spec.constructor with
| some { localObligations := [{ name := "constructor_storage_layout"
obligation := "Constructor storage aliasing must be checked separately across deployments."
proofStatus := .unchecked }], .. } =>
true
| none => false
| _ => false
example : constructorCarriesUncheckedObligation = true := by native_decide
def unsafeEdgeCarriesAssumedObligation : Bool :=
match LocalObligationMacroSmoke.unsafeEdge_model with
| { localObligations := [{ name := "manual_delegatecall_refinement"
obligation := "Caller must separately prove the handwritten assembly path refines the intended state transition."
proofStatus := .assumed }]
body := [Stmt.stop], .. } => true
| _ => false
example : unsafeEdgeCarriesAssumedObligation = true := by native_decide
def dischargedEdgeCarriesProvedObligation : Bool :=
match LocalObligationMacroSmoke.dischargedEdge_model with
| { localObligations := [{ name := "checked_patch_pack"
obligation := "Patch-pack proof already discharges this handwritten lowering boundary."
proofStatus := .proved }]
body := [Stmt.setStorage "lastValue" (Expr.param "value"), Stmt.return (Expr.param "value")], .. } => true
| _ => false
example : dischargedEdgeCarriesProvedObligation = true := by native_decide
def dischargedEdgeExecutableStillRuns : Bool :=
match LocalObligationMacroSmoke.dischargedEdge 77 Verity.defaultState with
| .success value state => value == 77 && state.storage 1 == 77
| .revert _ _ => false
example : dischargedEdgeExecutableStillRuns = true := by native_decide
end MacroLocalObligationSmoke
namespace YulImporterSmoke
open Compiler.Yul
private def importedAssemblyStmt : Stmt :=
YulImporter.importBlock {
label := "sol_inline_assembly"
sourceSpan := some {
sourceName := "Contract.sol"
startLine := 12
startColumn := 8
endLine := 18
endColumn := 5
}
stmts := [
YulStmt.let_ "ptr" (YulExpr.call "mload" [YulExpr.lit 64]),
YulStmt.assign "ptr" (YulExpr.call "add" [YulExpr.ident "ptr", YulExpr.lit 32]),
YulStmt.expr (YulExpr.call "mstore" [YulExpr.ident "ptr", YulExpr.lit 1]),
YulStmt.expr (YulExpr.call "sstore" [YulExpr.lit 0, YulExpr.lit 1]),
YulStmt.expr (YulExpr.call "revert" [YulExpr.ident "ptr", YulExpr.lit 32])
]
}
def importedAssemblyCarriesDerivedMetadata : Bool :=
match importedAssemblyStmt with
| Stmt.unsafeYul fragment =>
fragment.label == "sol_inline_assembly" &&
(match fragment.obligations with
| [{ name := "sol_inline_assembly"
obligation := "Imported Solidity inline assembly at Contract.sol:12:8-18:5 block 'sol_inline_assembly' must refine the declared Verity state transition."
proofStatus := .assumed }] => true
| _ => false) &&
fragment.mechanics.contains .mload &&
fragment.mechanics.contains .mstore &&
fragment.mechanics.contains .storageWrite &&
fragment.mechanics.contains .rawRevert &&
fragment.scopeEffects.bindNames == ["ptr"] &&
fragment.scopeEffects.assignNames == ["ptr"] &&
!fragment.scopeEffects.storageWrites.isEmpty &&
fragment.controlFlow == .reverts
| _ => false
example : importedAssemblyCarriesDerivedMetadata = true := by native_decide
def importedAssemblyPassesUnsafeYulValidation : Bool :=
let spec : FunctionSpec := {
name := "usesImportedAssembly"
params := []
returnType := none
body := [importedAssemblyStmt, Stmt.stop]
}
match validateFunctionSpec spec with
| .ok _ => true
| .error _ => false
example : importedAssemblyPassesUnsafeYulValidation = true := by native_decide
end YulImporterSmoke
namespace MacroProxyUpgradeabilitySmoke
open Contracts
def initProxyCarriesInitializerObligation : Bool :=
match ProxyUpgradeabilityMacroSmoke.initProxy_model with
| { localObligations := [{ name := "implementation_slot_discipline"
obligation := "Proxy storage-slot discipline must be validated against the intended implementation layout."
proofStatus := .assumed }]
body := [Stmt.require (Expr.eq (Expr.storage "initializedVersion") (Expr.literal 0)) "initializer already run",
Stmt.setStorage "initializedVersion" (Expr.literal 1),
Stmt.setStorageAddr "admin" (Expr.param "seedAdmin"),
Stmt.setStorageAddr "implementation" (Expr.param "seedImplementation"),
Stmt.stop], .. } => true
| _ => false
example : initProxyCarriesInitializerObligation = true := by native_decide
def upgradeToCarriesUpgradeObligations : Bool :=
match ProxyUpgradeabilityMacroSmoke.upgradeTo_model with
| { localObligations := [{ name := "upgrade_authorization"
obligation := "Caller must separately prove that only the intended admin can authorize upgrades."
proofStatus := .assumed },
{ name := "storage_layout_compatibility"
obligation := "Storage-layout compatibility across versions remains a manual proof obligation."
proofStatus := .unchecked }]
body := [Stmt.require (Expr.lt (Expr.storage "initializedVersion") (Expr.literal 2)) "reinitializer(2) already run",
Stmt.setStorage "initializedVersion" (Expr.literal 2),
Stmt.setStorageAddr "implementation" (Expr.param "newImplementation"),
Stmt.stop], .. } => true
| _ => false
example : upgradeToCarriesUpgradeObligations = true := by native_decide
def forwardCarriesProxyBoundary : Bool :=
match ProxyUpgradeabilityMacroSmoke.forward_model with
| { localObligations := [{ name := "delegatecall_refinement"
obligation := "Delegatecall fallback behavior must be shown to refine the selected proxy semantics."
proofStatus := .assumed }]
body := body, .. } =>
body.length == 3
| _ => false
example : forwardCarriesProxyBoundary = true := by native_decide
def forwardExecutableReadsImplementation : Bool :=
let seededState :=
(ProxyUpgradeabilityMacroSmoke.initProxy (Verity.wordToAddress 11) (Verity.wordToAddress 19)).run Verity.defaultState
match seededState with
| .success _ state =>
match ProxyUpgradeabilityMacroSmoke.forward 100 0 32 64 32 state with
| .success ok nextState =>
ok == delegatecall 100 19 0 32 64 32 &&
nextState.storage ProxyUpgradeabilityMacroSmoke.initializedVersion.slot == 1 &&
nextState.storageAddr ProxyUpgradeabilityMacroSmoke.admin.slot == Verity.wordToAddress 11 &&
nextState.storageAddr ProxyUpgradeabilityMacroSmoke.implementation.slot == Verity.wordToAddress 19
| .revert _ _ => false
| .revert _ _ => false
example : forwardExecutableReadsImplementation = true := by native_decide
end MacroProxyUpgradeabilitySmoke
namespace CounterUnsafeBoundarySmoke
open Contracts
def previewEnvOpsCarriesBoundaryObligation : Bool :=
match Counter.previewEnvOps_model with
| { localObligations := [{ name := "env_memory_refinement"
obligation := "Caller must separately prove the direct mload-based environment digest path respects the intended memory/refinement boundary."
proofStatus := .assumed }]
body := _, .. } => true
| _ => false
example : previewEnvOpsCarriesBoundaryObligation = true := by native_decide
def previewLowLevelCarriesBoundaryObligation : Bool :=
match Counter.previewLowLevel_model with
| { localObligations := [{ name := "manual_low_level_refinement"
obligation := "Caller must separately prove the direct low-level call and returndata choreography refines the intended external-call behavior."
proofStatus := .assumed }]
body := _, .. } => true
| _ => false
example : previewLowLevelCarriesBoundaryObligation = true := by native_decide
end CounterUnsafeBoundarySmoke
namespace MacroEcrecoverSmoke
open Contracts
open Verity hiding pure bind
open Verity.EVM.Uint256
verity_contract MacroEcrecover where
storage
lastSigner : Address := slot 0
function recoverSigner (digest : Bytes32, v : Uint256, r : Bytes32, s : Bytes32) : Address := do
let signer ← ecrecover digest v r s
return signer
def recoverSignerModelUsesEcrecoverEcm : Bool :=
match MacroEcrecover.recoverSigner_modelBody with
| [Stmt.ecm mod [Expr.param "digest", Expr.param "v", Expr.param "r", Expr.param "s"],
Stmt.return (Expr.localVar "signer")] =>
mod.name == "ecrecover" &&
mod.resultVars == ["signer"] &&
mod.axioms == ["evm_ecrecover_precompile"]
| _ => false
example : recoverSignerModelUsesEcrecoverEcm = true := by native_decide
def recoverSignerExecutableUsesOracle : Bool :=
match MacroEcrecover.recoverSigner 10 27 30 40 Verity.defaultState with
| .success signer state =>
signer == Verity.wordToAddress 107 && state.sender == Verity.defaultState.sender
| .revert _ _ => false
example : recoverSignerExecutableUsesOracle = true := by native_decide
end MacroEcrecoverSmoke
namespace MacroKeccakSmoke
open Contracts
open Verity hiding pure bind
open Verity.EVM.Uint256
verity_contract MacroKeccak where
storage
lastDigest : Uint256 := slot 0
function hashSlice (offset : Uint256, size : Uint256) : Uint256 := do
let digest := keccak256 offset size
return digest
def hashSliceModelUsesKeccak : Bool :=
match MacroKeccak.hashSlice_modelBody with
| [Stmt.letVar "digest" (Expr.keccak256 (Expr.param "offset") (Expr.param "size")),
Stmt.return (Expr.localVar "digest")] =>
true
| _ => false
example : hashSliceModelUsesKeccak = true := by native_decide
def hashSliceExecutableUsesRuntimeStub : Bool :=
match MacroKeccak.hashSlice 11 64 Verity.defaultState with
| .success digest state =>
digest == 75 && state.sender == Verity.defaultState.sender
| .revert _ _ => false
example : hashSliceExecutableUsesRuntimeStub = true := by native_decide
end MacroKeccakSmoke
namespace MappingWordSmoke
open Contracts.Smoke
example :
MappingWordSmoke.setWord1_modelBody =
[ Stmt.setMappingWord "words" (Expr.param "key") 1 (Expr.param "value"),
Stmt.stop ] := rfl
example :
MappingWordSmoke.getWord1_modelBody =
[ Stmt.letVar "word" (Expr.mappingWord "words" (Expr.param "key") 1),
Stmt.return (Expr.localVar "word") ] := rfl
example :
MappingWordSmoke.isWord1NonZero_modelBody =
[ Stmt.letVar "word" (Expr.mappingWord "words" (Expr.param "key") 1),
Stmt.return (Expr.logicalNot (Expr.eq (Expr.localVar "word") (Expr.literal 0))) ] := rfl
end MappingWordSmoke
namespace MacroExternalSmoke
open Contracts
open Verity hiding pure bind
open Verity.EVM.Uint256
verity_contract MacroExternal where
storage
echoedValue : Uint256 := slot 0
linked_externals
external echo(Uint256) -> (Uint256)
function allow_post_interaction_writes storeEcho (next : Uint256) : Unit := do
let echoed := externalCall "echo" [next]
setStorage echoedValue echoed
def storeEchoModelUsesDeclaredExternal : Bool :=
(match MacroExternal.spec.externals with
| [{ name := "echo"
params := [ParamType.uint256]
returnType := some ParamType.uint256
returns := [ParamType.uint256]
proofStatus := Compiler.ProofStatus.assumed
axiomNames := []
linkMode := Compiler.CompilationModel.ForeignLinkMode.objectLinked }] => true
| _ => false) &&
match MacroExternal.storeEcho_modelBody with
| [Stmt.letVar "echoed" (Expr.externalCall "echo" [Expr.param "next"]),
Stmt.setStorage "echoedValue" (Expr.localVar "echoed"),
Stmt.stop] => true
| _ => false
example : storeEchoModelUsesDeclaredExternal = true := by native_decide
def storeEchoExecutableUsesStub : Bool :=
match MacroExternal.storeEcho 33 Verity.defaultState with
| .success () state =>
state.storage 0 == 33
| .revert _ _ => false
example : storeEchoExecutableUsesStub = true := by native_decide
end MacroExternalSmoke
namespace MacroExternalLinkModeSmoke
open Contracts
open Verity hiding pure bind
open Verity.EVM.Uint256
verity_contract MacroExternalLinkModes where
storage
value : Uint256 := slot 0
linked_externals
external oracleEcho(Uint256) -> (Uint256) linked_as := external
external poseidonHash(Uint256, Uint256) -> (Uint256) linked_as := internal_yul
external inlineAdd(Uint256, Uint256) -> (Uint256) linked_as := inline
external abiRuntime(Uint256) -> (Uint256) linked_as := compiler_runtime
def parsedExternalLinkModes : Bool :=
MacroExternalLinkModes.spec.externals.map (fun ext => (ext.name, ext.linkMode.toJsonString)) =
[ ("oracleEcho", "external")
, ("poseidonHash", "objectLinked")
, ("inlineAdd", "inline")
, ("abiRuntime", "compilerRuntime")
]
example : parsedExternalLinkModes = true := by native_decide
def linkModeTrustSurfaceSpec : CompilationModel := {
name := "LinkModeTrustSurface"
fields := []
«constructor» := none
externals := [
{ name := "oracleEcho"
params := [ParamType.uint256]
returnType := some ParamType.uint256
returns := [ParamType.uint256]
axiomNames := []
linkMode := .external },
{ name := "poseidonHash"
params := [ParamType.uint256, ParamType.uint256]
returnType := some ParamType.uint256
returns := [ParamType.uint256]
axiomNames := []
linkMode := .objectLinked },
{ name := "inlineAdd"
params := [ParamType.uint256, ParamType.uint256]
returnType := some ParamType.uint256
returns := [ParamType.uint256]
axiomNames := []
linkMode := .inline },
{ name := "abiRuntime"
params := [ParamType.uint256]
returnType := some ParamType.uint256
returns := [ParamType.uint256]
axiomNames := []
linkMode := .compilerRuntime }
]
functions := [
{ name := "exercise"
params := [{ name := "next", ty := ParamType.uint256 }]
returnType := some FieldType.uint256
body := [
Stmt.letVar "a" (Expr.externalCall "oracleEcho" [Expr.param "next"]),
Stmt.letVar "b" (Expr.externalCall "poseidonHash" [Expr.param "next", Expr.param "next"]),
Stmt.letVar "c" (Expr.externalCall "inlineAdd" [Expr.localVar "b", Expr.param "next"]),
Stmt.letVar "d" (Expr.externalCall "abiRuntime" [Expr.localVar "c"]),
Stmt.return (Expr.localVar "d")
]
}
]
}
def externalModeRawCallSpec : CompilationModel := {
name := "ExternalModeRawCall"
fields := []
«constructor» := none
externals := [
{ name := "oracleEcho"
params := [ParamType.uint256]
returnType := some ParamType.uint256
returns := [ParamType.uint256]
axiomNames := []
linkMode := .external }
]
functions := [
{ name := "bad"
params := [{ name := "next", ty := ParamType.uint256 }]
returnType := some FieldType.uint256
body := [Stmt.return (Expr.externalCall "oracleEcho" [Expr.param "next"])]
}
]
}
end MacroExternalLinkModeSmoke
namespace DynamicBytesEqUsageAnalysisSmoke
def rawLogDynamicBytesEqIsDetected : Bool :=
stmtUsesDynamicBytesEq
(Stmt.rawLog
[Expr.dynamicBytesEq "lhs" "rhs"]
(Expr.literal 0)
(Expr.literal 32))
example : rawLogDynamicBytesEqIsDetected = true := by native_decide
end DynamicBytesEqUsageAnalysisSmoke
namespace MacroERC20Smoke
open Contracts
open Verity hiding pure bind
open Verity.EVM.Uint256
verity_contract MacroERC20 where
storage
lastBalance : Uint256 := slot 0
lastAllowance : Uint256 := slot 1
lastSupply : Uint256 := slot 2
function pushTokens (token : Address, toAddr : Address, amount : Uint256) : Unit := do
safeTransfer token toAddr amount
function pullTokens (token : Address, fromAddr : Address, toAddr : Address, amount : Uint256) : Unit := do
safeTransferFrom token fromAddr toAddr amount
function approveTokens (token : Address, spender : Address, amount : Uint256) : Unit := do
safeApprove token spender amount
function snapshotBalance (token : Address, owner : Address) : Uint256 := do
let balance ← balanceOf token owner
setStorage lastBalance balance
return balance
function snapshotAllowance (token : Address, owner : Address, spender : Address) : Uint256 := do
let current ← allowance token owner spender
setStorage lastAllowance current
return current
function snapshotSupply (token : Address) : Uint256 := do
let supply ← totalSupply token
setStorage lastSupply supply
return supply
def pushTokensModelUsesSafeTransfer : Bool :=
match MacroERC20.pushTokens_modelBody with
| [Stmt.ecm mod [Expr.param "token", Expr.param "toAddr", Expr.param "amount"], Stmt.stop] =>
mod.name == "safeTransfer" &&
mod.resultVars.isEmpty &&
mod.axioms == ["erc20_transfer_interface"]
| _ => false
example : pushTokensModelUsesSafeTransfer = true := by native_decide
def pullTokensModelUsesSafeTransferFrom : Bool :=
match MacroERC20.pullTokens_modelBody with
| [Stmt.ecm mod [Expr.param "token", Expr.param "fromAddr", Expr.param "toAddr", Expr.param "amount"], Stmt.stop] =>
mod.name == "safeTransferFrom" &&
mod.resultVars.isEmpty &&
mod.axioms == ["erc20_transferFrom_interface"]
| _ => false
example : pullTokensModelUsesSafeTransferFrom = true := by native_decide
def approveTokensModelUsesSafeApprove : Bool :=
match MacroERC20.approveTokens_modelBody with
| [Stmt.ecm mod [Expr.param "token", Expr.param "spender", Expr.param "amount"], Stmt.stop] =>
mod.name == "safeApprove" &&
mod.resultVars.isEmpty &&
mod.axioms == ["erc20_approve_interface"]
| _ => false
example : approveTokensModelUsesSafeApprove = true := by native_decide
def snapshotBalanceModelUsesBalanceOfModule : Bool :=
match MacroERC20.snapshotBalance_modelBody with
| [Stmt.ecm mod [Expr.param "token", Expr.param "owner"],
Stmt.setStorage "lastBalance" (Expr.localVar "balance"),
Stmt.return (Expr.localVar "balance")] =>
mod.name == "balanceOf" &&
mod.resultVars == ["balance"] &&
mod.axioms == ["erc20_balanceOf_interface"]
| _ => false
example : snapshotBalanceModelUsesBalanceOfModule = true := by native_decide
def snapshotAllowanceModelUsesAllowanceModule : Bool :=
match MacroERC20.snapshotAllowance_modelBody with
| [Stmt.ecm mod [Expr.param "token", Expr.param "owner", Expr.param "spender"],
Stmt.setStorage "lastAllowance" (Expr.localVar "current"),
Stmt.return (Expr.localVar "current")] =>
mod.name == "allowance" &&
mod.resultVars == ["current"] &&
mod.axioms == ["erc20_allowance_interface"]
| _ => false
example : snapshotAllowanceModelUsesAllowanceModule = true := by native_decide
def snapshotSupplyModelUsesTotalSupplyModule : Bool :=
match MacroERC20.snapshotSupply_modelBody with
| [Stmt.ecm mod [Expr.param "token"],
Stmt.setStorage "lastSupply" (Expr.localVar "supply"),
Stmt.return (Expr.localVar "supply")] =>
mod.name == "totalSupply" &&
mod.resultVars == ["supply"] &&
mod.axioms == ["erc20_totalSupply_interface"]
| _ => false
example : snapshotSupplyModelUsesTotalSupplyModule = true := by native_decide
def snapshotBalanceExecutableUsesStub : Bool :=
let token := Verity.wordToAddress 7
let owner := Verity.wordToAddress 13
match Contracts.balanceOf token owner Verity.defaultState,
MacroERC20.snapshotBalance token owner Verity.defaultState with
| .success expected _, .success balance state =>
balance == expected &&
state.storage 0 == expected &&
state.storage 1 == 0
| .revert _ _, _ => false
| _, .revert _ _ => false
example : snapshotBalanceExecutableUsesStub = true := by native_decide
def snapshotAllowanceExecutableUsesStub : Bool :=
let token := Verity.wordToAddress 7
let owner := Verity.wordToAddress 13
let spender := Verity.wordToAddress 17
match Contracts.allowance token owner spender Verity.defaultState,
MacroERC20.snapshotAllowance token owner spender Verity.defaultState with
| .success expected _, .success current state =>
current == expected &&
state.storage 1 == expected &&
state.storage 0 == 0
| .revert _ _, _ => false
| _, .revert _ _ => false
example : snapshotAllowanceExecutableUsesStub = true := by native_decide
def snapshotSupplyExecutableUsesStub : Bool :=
let token := Verity.wordToAddress 7
match Contracts.totalSupply token Verity.defaultState,
MacroERC20.snapshotSupply token Verity.defaultState with
| .success expected _, .success supply state =>
supply == expected &&
state.storage 2 == expected
| .revert _ _, _ => false
| _, .revert _ _ => false
example : snapshotSupplyExecutableUsesStub = true := by native_decide
end MacroERC20Smoke
namespace MacroTransientStorageSmoke
open Contracts
open Verity hiding pure bind
open Verity.EVM.Uint256
verity_contract MacroTransientStorage where
storage
function warm (key : Uint256, value : Uint256)
local_obligations [transient_storage_refinement := assumed "Caller must separately prove the direct transient-storage choreography refines the intended warm-slot behavior."]
: Uint256 := do
tstore key value
let current := tload key
return current
function peek (key : Uint256)
local_obligations [transient_storage_read_refinement := assumed "Caller must separately prove the direct transient-storage read refines the intended peek behavior."]
: Uint256 := do
let current := tload key
return current
def warmModelUsesTransientStorage : Bool :=
match MacroTransientStorage.warm_modelBody with
| [Stmt.tstore (Expr.param "key") (Expr.param "value"),
Stmt.letVar "current" (Expr.tload (Expr.param "key")),
Stmt.return (Expr.localVar "current")] =>
true
| _ => false
example : warmModelUsesTransientStorage = true := by native_decide
def warmCarriesTransientStorageObligation : Bool :=
match MacroTransientStorage.warm_model with
| { localObligations := [{ name := "transient_storage_refinement"
obligation := "Caller must separately prove the direct transient-storage choreography refines the intended warm-slot behavior."
proofStatus := .assumed }]
body := _, .. } => true
| _ => false
example : warmCarriesTransientStorageObligation = true := by native_decide
def peekModelUsesTransientStorage : Bool :=
match MacroTransientStorage.peek_modelBody with
| [Stmt.letVar "current" (Expr.tload (Expr.param "key")),
Stmt.return (Expr.localVar "current")] =>
true
| _ => false
example : peekModelUsesTransientStorage = true := by native_decide
def peekCarriesTransientStorageObligation : Bool :=
match MacroTransientStorage.peek_model with
| { localObligations := [{ name := "transient_storage_read_refinement"
obligation := "Caller must separately prove the direct transient-storage read refines the intended peek behavior."
proofStatus := .assumed }]
body := _, .. } => true
| _ => false
example : peekCarriesTransientStorageObligation = true := by native_decide
def warmExecutableWritesTransientStorage : Bool :=
match MacroTransientStorage.warm 7 99 Verity.defaultState with
| .success current state =>
current == 99 &&
state.transientStorage 7 == 99 &&
state.storage 7 == 0
| .revert _ _ => false
example : warmExecutableWritesTransientStorage = true := by native_decide
def transientStoragePersistsAcrossExecutableCalls : Bool :=
match MacroTransientStorage.warm 7 99 Verity.defaultState with
| .success _ warmedState =>
match MacroTransientStorage.peek 7 warmedState with
| .success current finalState =>
current == 99 &&
finalState.transientStorage 7 == 99
| .revert _ _ => false
| .revert _ _ => false
example : transientStoragePersistsAcrossExecutableCalls = true := by native_decide
end MacroTransientStorageSmoke
namespace MacroBlobbasefeeSmoke
open Contracts
open Verity hiding pure bind
open Verity.EVM.Uint256
verity_contract MacroBlobbasefee where
storage
function currentBlobBaseFee () : Uint256 := do
let fee ← blobbasefee
return fee
function qualifiedCurrentBlobBaseFee () : Uint256 := do
let fee ← Verity.blobbasefee
return fee
def modelReturnsBlobbasefeeBuiltin : Bool :=
match MacroBlobbasefee.currentBlobBaseFee_modelBody with
| [Stmt.letVar "fee" Expr.blobbasefee, Stmt.return (Expr.localVar "fee")] => true
| _ => false
example : modelReturnsBlobbasefeeBuiltin = true := by native_decide
def qualifiedModelReturnsBlobbasefeeBuiltin : Bool :=
match MacroBlobbasefee.qualifiedCurrentBlobBaseFee_modelBody with
| [Stmt.letVar "fee" Expr.blobbasefee, Stmt.return (Expr.localVar "fee")] => true
| _ => false
example : qualifiedModelReturnsBlobbasefeeBuiltin = true := by native_decide
def executableUsesContractState : Bool :=
match MacroBlobbasefee.currentBlobBaseFee { Verity.defaultState with blobBaseFee := 19 } with
| .success fee state =>
fee == 19 && state.sender == Verity.defaultState.sender
| .revert _ _ => false
example : executableUsesContractState = true := by native_decide
end MacroBlobbasefeeSmoke
namespace MacroConstantSmoke
open Contracts
open Verity hiding pure bind
open Verity.EVM.Uint256
verity_contract MacroConstant where
storage
storedFee : Uint256 := slot 0
constants
basisPoints : Uint256 := 10000
mintFeeBps : Uint256 := 30
treasury : Address := (wordToAddress 42)
treasuryWord : Uint256 := (addressToWord treasury)
function feeOn (amount : Uint256) : Uint256 := do
let fee := div (mul amount mintFeeBps) basisPoints
return fee
function treasuryAddr () : Address := do
return treasury
function treasuryAsWord () : Uint256 := do
return treasuryWord
function shadowedConstant (mintFeeBps : Uint256) : Uint256 := do
let treasuryWord := 9
return (add mintFeeBps treasuryWord)
def feeOnModelInlinesContractConstants : Bool :=
match MacroConstant.feeOn_modelBody with
| [Stmt.letVar "fee"
(Expr.div
(Expr.mul (Expr.param "amount") (Expr.literal 30))
(Expr.literal 10000)),
Stmt.return (Expr.localVar "fee")] =>
true
| _ => false
example : feeOnModelInlinesContractConstants = true := by native_decide
def uint256PowSmokeLowersToBuiltinExp : Bool :=
match Contracts.Smoke.Uint256PowSmoke.scale_modelBody with
| [Stmt.letVar "exponent" (Expr.sub (Expr.literal 18) (Expr.param "decimals")),
Stmt.return (Expr.externalCall name [Expr.literal 10, Expr.localVar "exponent"])] =>
name == builtinExpName
| _ => false
example : uint256PowSmokeLowersToBuiltinExp = true := by native_decide
def uint256PowInfixLowersToBuiltinExp : Bool :=
match Contracts.Smoke.Uint256PowSmoke.scaleInfix_modelBody with
| [Stmt.letVar "exponent" (Expr.sub (Expr.literal 18) (Expr.param "decimals")),
Stmt.return (Expr.externalCall name [Expr.literal 10, Expr.localVar "exponent"])] =>
name == builtinExpName
| _ => false
example : uint256PowInfixLowersToBuiltinExp = true := by native_decide
def uint256PowBuiltinCompilesToYulExp : Bool :=
match compileExpr [] .calldata
(Expr.externalCall builtinExpName [Expr.param "base", Expr.param "exponent"]) with
| .ok (Compiler.Yul.YulExpr.call "exp"
[Compiler.Yul.YulExpr.ident "base", Compiler.Yul.YulExpr.ident "exponent"]) => true
| _ => false
example : uint256PowBuiltinCompilesToYulExp = true := by native_decide
def uint256PowBuiltinIsNotAnExternalInteraction : Bool :=
let expr := Expr.externalCall builtinExpName [Expr.literal 10, Expr.param "exponent"]
!exprReadsStateOrEnv expr &&
!exprWritesState expr &&
!exprContainsCallLike expr &&
!exprContainsExternalCall expr &&
!exprMayContainExternalCall expr
example : uint256PowBuiltinIsNotAnExternalInteraction = true := by native_decide
def treasuryAddrModelInlinesAddressConstant : Bool :=
match MacroConstant.treasuryAddr_modelBody with
| [Stmt.return (Expr.literal 42)] =>
true
| _ => false
example : treasuryAddrModelInlinesAddressConstant = true := by native_decide
def treasuryAsWordModelInlinesNestedConstants : Bool :=
match MacroConstant.treasuryAsWord_modelBody with
| [Stmt.return (Expr.literal 42)] =>
true
| _ => false
example : treasuryAsWordModelInlinesNestedConstants = true := by native_decide
def shadowedConstantModelPrefersLocalAndParamBindings : Bool :=
match MacroConstant.shadowedConstant_modelBody with
| [Stmt.letVar "treasuryWord" (Expr.literal 9),
Stmt.return (Expr.add (Expr.param "mintFeeBps") (Expr.localVar "treasuryWord"))] =>
true
| _ => false
example : shadowedConstantModelPrefersLocalAndParamBindings = true := by native_decide
def treasuryExecutableUsesGeneratedConstantDef : Bool :=
match MacroConstant.treasuryAddr Verity.defaultState with
| .success treasury state =>
treasury == Verity.wordToAddress 42 &&
state.sender == Verity.defaultState.sender
| .revert _ _ => false
example : treasuryExecutableUsesGeneratedConstantDef = true := by native_decide
end MacroConstantSmoke
namespace MacroTupleDestructuringSmoke
open Contracts
open Verity hiding pure bind
open Verity.EVM.Uint256
verity_contract MacroTupleDestructuring where
storage
firstSlot : Uint256 := slot 0
secondSlot : Uint256 := slot 1
function helperPair (seed : Uint256) : Tuple [Uint256, Uint256] := do
return (seed, add seed 1)
function storePair (pair : Tuple [Uint256, Uint256]) : Unit := do
let (first, second) := pair
setStorage firstSlot first
setStorage secondSlot second
function storePairTail (pair : Tuple [Uint256, Uint256]) : Unit := do
let (_, second) := pair
setStorage secondSlot second
function storeLiteralPair (seed : Uint256) : Unit := do
let (first, second) := (seed, add seed 1)
setStorage firstSlot first
setStorage secondSlot second
function echoPair (pair : Tuple [Uint256, Uint256]) : Tuple [Uint256, Uint256] := do
let (first, second) := pair
return (first, second)
function storeHelperPair (seed : Uint256) : Unit := do
let (first, second) ← helperPair seed
setStorage firstSlot first
setStorage secondSlot second
function storeHelperPairEq (seed : Uint256) : Unit := do
let (first, second) := helperPair seed
setStorage firstSlot first
setStorage secondSlot second
function storeHelperPairTail (seed : Uint256) : Unit := do
let (_, second) := helperPair seed
setStorage secondSlot second
function storeHelperPairTailNameCollision («__tuple_discard_0» : Uint256, seed : Uint256) : Unit := do
let (_, second) := helperPair seed
setStorage firstSlot «__tuple_discard_0»
setStorage secondSlot second
def storePairModelDestructuresTupleParam : Bool :=
match MacroTupleDestructuring.storePair_modelBody with
| [Stmt.letVar "first" (Expr.param "pair_0"),
Stmt.letVar "second" (Expr.param "pair_1"),
Stmt.setStorage "firstSlot" (Expr.localVar "first"),
Stmt.setStorage "secondSlot" (Expr.localVar "second"),
Stmt.stop] =>
true
| _ => false
example : storePairModelDestructuresTupleParam = true := by native_decide
def storePairTailModelSkipsDiscardedBinder : Bool :=
match MacroTupleDestructuring.storePairTail_modelBody with
| [Stmt.letVar "second" (Expr.param "pair_1"),
Stmt.setStorage "secondSlot" (Expr.localVar "second"),
Stmt.stop] =>
true
| _ => false
example : storePairTailModelSkipsDiscardedBinder = true := by native_decide
def helperPairModelReturnsMultipleWords : Bool :=
match MacroTupleDestructuring.helperPair_modelBody with
| [Stmt.returnValues [Expr.param "seed", Expr.add (Expr.param "seed") (Expr.literal 1)]] =>
true
| _ => false
example : helperPairModelReturnsMultipleWords = true := by native_decide
def storeLiteralPairModelDestructuresTupleExpr : Bool :=
match MacroTupleDestructuring.storeLiteralPair_modelBody with
| [Stmt.letVar "first" (Expr.param "seed"),
Stmt.letVar "second" (Expr.add (Expr.param "seed") (Expr.literal 1)),
Stmt.setStorage "firstSlot" (Expr.localVar "first"),
Stmt.setStorage "secondSlot" (Expr.localVar "second"),
Stmt.stop] =>
true
| _ => false
example : storeLiteralPairModelDestructuresTupleExpr = true := by native_decide
def echoPairModelReturnsMultipleWords : Bool :=
match MacroTupleDestructuring.echoPair_modelBody with
| [Stmt.letVar "first" (Expr.param "pair_0"),
Stmt.letVar "second" (Expr.param "pair_1"),
Stmt.returnValues [Expr.localVar "first", Expr.localVar "second"]] =>
true
| _ => false
example : echoPairModelReturnsMultipleWords = true := by native_decide
private def helperPairInternalModelName : String := "internal_helperPair"
def storeHelperPairModelUsesInternalCallAssign : Bool :=
match MacroTupleDestructuring.storeHelperPair_modelBody with
| [Stmt.internalCallAssign ["first", "second"] helperName [Expr.param "seed"],
Stmt.setStorage "firstSlot" (Expr.localVar "first"),
Stmt.setStorage "secondSlot" (Expr.localVar "second"),
Stmt.stop] =>
helperName == helperPairInternalModelName
| _ => false
example : storeHelperPairModelUsesInternalCallAssign = true := by native_decide
def storeHelperPairEqModelUsesInternalCallAssign : Bool :=
match MacroTupleDestructuring.storeHelperPairEq_modelBody with
| [Stmt.internalCallAssign ["first", "second"] helperName [Expr.param "seed"],
Stmt.setStorage "firstSlot" (Expr.localVar "first"),
Stmt.setStorage "secondSlot" (Expr.localVar "second"),
Stmt.stop] =>
helperName == helperPairInternalModelName
| _ => false
example : storeHelperPairEqModelUsesInternalCallAssign = true := by native_decide
def storeHelperPairTailModelUsesHiddenDiscardTarget : Bool :=
match MacroTupleDestructuring.storeHelperPairTail_modelBody with
| [Stmt.internalCallAssign ["__tuple_discard_0", "second"] helperName [Expr.param "seed"],
Stmt.setStorage "secondSlot" (Expr.localVar "second"),
Stmt.stop] =>
helperName == helperPairInternalModelName
| _ => false
example : storeHelperPairTailModelUsesHiddenDiscardTarget = true := by native_decide
def storeHelperPairTailNameCollisionModelUsesFreshDiscardTarget : Bool :=
match MacroTupleDestructuring.storeHelperPairTailNameCollision_modelBody with
| [Stmt.internalCallAssign ["__tuple_discard_0_1", "second"] helperName [Expr.param "seed"],
Stmt.setStorage "firstSlot" (Expr.param "__tuple_discard_0"),
Stmt.setStorage "secondSlot" (Expr.localVar "second"),
Stmt.stop] =>
helperName == helperPairInternalModelName
| _ => false
example : storeHelperPairTailNameCollisionModelUsesFreshDiscardTarget = true := by native_decide
def echoPairExecutableKeepsTupleShape : Bool :=
match MacroTupleDestructuring.echoPair (11, 17) Verity.defaultState with