-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathContractSpecFeatureTest.lean
More file actions
5228 lines (5023 loc) · 190 KB
/
ContractSpecFeatureTest.lean
File metadata and controls
5228 lines (5023 loc) · 190 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.ContractSpec
import Compiler.Selector
import Compiler.Codegen
import Compiler.Yul.PrettyPrint
import Compiler.DiffTestTypes
import Verity.Proofs.Stdlib.SpecInterpreter
namespace Compiler.ContractSpecFeatureTest
open Compiler
open Compiler.ContractSpec
open Compiler.Selector
open Compiler.DiffTestTypes
open Verity.Proofs.Stdlib.SpecInterpreter
private def contains (haystack needle : String) : Bool :=
let h := haystack.toList
let n := needle.toList
if n.isEmpty then true
else
let rec go : List Char → Bool
| [] => false
| c :: cs =>
if (c :: cs).take n.length == n then true
else go cs
go h
private def assertContains (label rendered : String) (needles : List String) : IO Unit := do
for needle in needles do
if !contains rendered needle then
throw (IO.userError s!"✗ {label}: missing '{needle}' in:\n{rendered}")
IO.println s!"✓ {label}"
private def assertNotContains (label rendered : String) (needles : List String) : IO Unit := do
for needle in needles do
if contains rendered needle then
throw (IO.userError s!"✗ {label}: unexpected '{needle}' in:\n{rendered}")
IO.println s!"✓ {label}"
private def firstIndexOf? (haystack needle : String) : Option Nat :=
let h := haystack.toList
let n := needle.toList
if n.isEmpty then some 0
else
let rec go (rest : List Char) (idx : Nat) : Option Nat :=
match rest with
| [] => none
| _ :: tail =>
if rest.take n.length == n then some idx
else go tail (idx + 1)
go h 0
private def assertAppearsBefore (label rendered first second : String) : IO Unit := do
let some firstIdx := firstIndexOf? rendered first
| throw (IO.userError s!"✗ {label}: missing first needle '{first}'")
let some secondIdx := firstIndexOf? rendered second
| throw (IO.userError s!"✗ {label}: missing second needle '{second}'")
if !(firstIdx < secondIdx) then
throw (IO.userError s!"✗ {label}: expected '{first}' to appear before '{second}'")
IO.println s!"✓ {label}"
private def featureSpec : ContractSpec := {
name := "FeatureSpec"
fields := []
constructor := none
events := [
{ name := "ValueSet"
params := [
{ name := "who", ty := ParamType.address, kind := EventParamKind.indexed },
{ name := "value", ty := ParamType.uint256, kind := EventParamKind.unindexed }
]
},
{ name := "BoolSet"
params := [
{ name := "ok", ty := ParamType.bool, kind := EventParamKind.indexed }
]
}
]
errors := [
{ name := "Unauthorized"
params := [ParamType.address, ParamType.uint256]
}
]
functions := [
{ name := "setFlag"
params := [{ name := "flag", ty := ParamType.bool }]
returnType := none
body := [Stmt.stop]
},
{ name := "pair"
params := [
{ name := "a", ty := ParamType.uint256 },
{ name := "b", ty := ParamType.uint256 }
]
returnType := none
returns := [ParamType.uint256, ParamType.uint256]
body := [Stmt.returnValues [Expr.param "a", Expr.param "b"]]
},
{ name := "emitValue"
params := [
{ name := "who", ty := ParamType.address },
{ name := "value", ty := ParamType.uint256 }
]
returnType := none
body := [Stmt.emit "ValueSet" [Expr.param "who", Expr.param "value"], Stmt.stop]
},
{ name := "emitBool"
params := []
returnType := none
body := [Stmt.emit "BoolSet" [Expr.literal 2], Stmt.stop]
},
{ name := "echoArray"
params := [{ name := "arr", ty := ParamType.array ParamType.uint256 }]
returnType := none
returns := [ParamType.array ParamType.uint256]
body := [Stmt.returnArray "arr"]
},
{ name := "sumStaticTuple"
params := [
{ name := "t", ty := ParamType.tuple [ParamType.uint256, ParamType.address, ParamType.bool] },
{ name := "z", ty := ParamType.uint256 }
]
returnType := none
body := [Stmt.return (Expr.add (Expr.param "t_0") (Expr.param "z"))]
},
{ name := "dynamicTupleTail"
params := [
{ name := "td", ty := ParamType.tuple [ParamType.uint256, ParamType.bytes] },
{ name := "x", ty := ParamType.uint256 }
]
returnType := some FieldType.uint256
body := [Stmt.return (Expr.param "x")]
},
{ name := "nestedStaticTupleTail"
params := [
{ name := "u"
ty := ParamType.tuple [
ParamType.fixedArray ParamType.uint256 2,
ParamType.tuple [ParamType.address, ParamType.bool],
ParamType.uint256
]
},
{ name := "y", ty := ParamType.uint256 }
]
returnType := some FieldType.uint256
body := [Stmt.return (Expr.add (Expr.param "u_0_1") (Expr.param "y"))]
},
{ name := "fixedArrayTupleTail"
params := [
{ name := "fa", ty := ParamType.fixedArray (ParamType.tuple [ParamType.uint256, ParamType.bool]) 2 },
{ name := "q", ty := ParamType.uint256 }
]
returnType := some FieldType.uint256
body := [Stmt.return (Expr.add (Expr.param "fa_1_0") (Expr.param "q"))]
},
{ name := "echoBytes"
params := [{ name := "data", ty := ParamType.bytes }]
returnType := none
returns := [ParamType.bytes]
body := [Stmt.returnBytes "data"]
},
{ name := "extSloadsLike"
params := [{ name := "slots", ty := ParamType.array ParamType.bytes32 }]
returnType := none
returns := [ParamType.array ParamType.uint256]
body := [Stmt.returnStorageWords "slots"]
},
{ name := "guarded"
params := [{ name := "who", ty := ParamType.address }, { name := "min", ty := ParamType.uint256 }]
returnType := none
body := [
Stmt.requireError (Expr.lt (Expr.param "min") (Expr.literal 1)) "Unauthorized" [Expr.param "who", Expr.param "min"],
Stmt.stop
]
}
]
}
#eval! do
match compile featureSpec [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] with
| .error err =>
throw (IO.userError s!"✗ feature spec compile failed: {err}")
| .ok ir =>
let rendered := Yul.render (emitYul ir)
assertContains "bool param normalization" rendered
["let __abi_bool_word_4 := calldataload(4)",
"if iszero(or(eq(__abi_bool_word_4, 0), eq(__abi_bool_word_4, 1))) {",
"let flag := iszero(iszero(__abi_bool_word_4))"]
assertContains "multi-return ABI encoding" rendered ["return(0, 64)"]
assertContains "indexed event log opcode" rendered ["log2("]
assertContains "indexed bool topic normalization" rendered ["iszero(iszero(2))"]
assertContains "event topic hashing uses free memory pointer" rendered ["keccak256(__evt_ptr,"]
assertContains "event topic hash cached before data writes" rendered ["let __evt_topic0 := keccak256(__evt_ptr,", "log2(__evt_ptr, 32, __evt_topic0"]
assertContains "dynamic array ABI return" rendered ["calldatacopy(64"]
assertContains "static tuple decode head offsets" rendered
["let t_0 := calldataload(4)", "let t_1 := and(calldataload(36)",
"let __abi_bool_word_68 := calldataload(68)",
"let t_2 := iszero(iszero(__abi_bool_word_68))", "let z := calldataload(100)"]
assertContains "dynamic tuple keeps offset head word" rendered ["let td_offset := calldataload(4)", "let x := calldataload(36)"]
assertContains "dynamic ABI decode offset/length bounds guards" rendered
["if lt(calldatasize(), 68) {", "if lt(arr_offset, 32) {",
"if gt(arr_abs_offset, sub(calldatasize(), 32)) {",
"if gt(arr_length, div(arr_tail_remaining, 32)) {",
"if gt(data_length, data_tail_remaining) {"]
assertContains "nested static tuple decode head offsets" rendered
["let u_0_0 := calldataload(4)", "let u_0_1 := calldataload(36)",
"let u_1_0 := and(calldataload(68)", "let __abi_bool_word_100 := calldataload(100)",
"let u_1_1 := iszero(iszero(__abi_bool_word_100))", "let u_2 := calldataload(132)",
"let y := calldataload(164)"]
assertContains "fixed array of static tuples decode offsets" rendered
["let fa_0_0 := calldataload(4)", "let __abi_bool_word_36 := calldataload(36)",
"let fa_0_1 := iszero(iszero(__abi_bool_word_36))", "let fa_1_0 := calldataload(68)",
"let __abi_bool_word_100 := calldataload(100)",
"let fa_1_1 := iszero(iszero(__abi_bool_word_100))", "let q := calldataload(132)"]
assertContains "dynamic bytes ABI return" rendered ["calldatacopy(64, data_data_offset, data_length)", "mstore(add(64, data_length), 0)", "return(0, add(64, and(add(data_length, 31), not(31))))"]
assertContains "storage-word array return ABI" rendered ["let __slot := calldataload(add(slots_data_offset, mul(__i, 32)))", "mstore(add(64, mul(__i, 32)), sload(__slot))", "return(0, add(64, mul(slots_length, 32)))"]
assertContains "custom error revert payload emission" rendered ["let __err_hash := keccak256(__err_ptr,", "mstore(0, __err_selector)", "mstore(4, and(who,", "let __err_tail := 64", "revert(0, add(4, __err_tail))"]
#eval! do
let conflictingReturnsSpec : ContractSpec := {
name := "ConflictingReturns"
fields := []
constructor := none
functions := [
{ name := "bad"
params := []
returnType := some FieldType.uint256
returns := [ParamType.uint256, ParamType.uint256]
body := [Stmt.returnValues [Expr.literal 1, Expr.literal 2]]
}
]
}
match compile conflictingReturnsSpec [1] with
| .error err =>
if !contains err "conflicting return declarations" then
throw (IO.userError s!"✗ conflicting returns should fail with clear message, got: {err}")
IO.println "✓ conflicting return declaration validation"
| .ok _ =>
throw (IO.userError "✗ expected conflicting returns to fail compilation")
#eval! do
let invalidReturnBytesSpec : ContractSpec := {
name := "InvalidReturnBytes"
fields := []
constructor := none
functions := [
{ name := "badBytes"
params := [{ name := "arr", ty := ParamType.array ParamType.uint256 }]
returnType := none
returns := [ParamType.bytes]
body := [Stmt.returnBytes "arr"]
}
]
}
match compile invalidReturnBytesSpec [1] with
| .error err =>
if !contains err "returnBytes 'arr' requires bytes parameter" then
throw (IO.userError s!"✗ returnBytes type validation message mismatch: {err}")
IO.println "✓ returnBytes parameter type validation"
| .ok _ =>
throw (IO.userError "✗ expected invalid returnBytes parameter to fail compilation")
#eval! do
let invalidContractIdentifierSpec : ContractSpec := {
name := "Bad-Contract"
fields := []
constructor := none
functions := []
}
match compile invalidContractIdentifierSpec [] with
| .error err =>
if !contains err "contract name must be a valid identifier: Bad-Contract" then
throw (IO.userError s!"✗ contract identifier validation mismatch: {err}")
IO.println "✓ contract identifier validation"
| .ok _ =>
throw (IO.userError "✗ expected invalid contract identifier to fail compilation")
#eval! do
let invalidFunctionIdentifierSpec : ContractSpec := {
name := "InvalidFunctionIdentifier"
fields := []
constructor := none
functions := [
{ name := "bad-fn"
params := []
returnType := none
body := [Stmt.stop]
}
]
}
match compile invalidFunctionIdentifierSpec [1] with
| .error err =>
if !contains err "function name must be a valid identifier: bad-fn" then
throw (IO.userError s!"✗ function identifier validation mismatch: {err}")
IO.println "✓ function identifier validation"
| .ok _ =>
throw (IO.userError "✗ expected invalid function identifier to fail compilation")
#eval! do
let invalidFieldIdentifierSpec : ContractSpec := {
name := "InvalidFieldIdentifier"
fields := [{ name := "stored-data", ty := FieldType.uint256 }]
constructor := none
functions := []
}
match compile invalidFieldIdentifierSpec [] with
| .error err =>
if !contains err "field name must be a valid identifier: stored-data" then
throw (IO.userError s!"✗ field identifier validation mismatch: {err}")
IO.println "✓ field identifier validation"
| .ok _ =>
throw (IO.userError "✗ expected invalid field identifier to fail compilation")
#eval! do
let invalidFunctionParamIdentifierSpec : ContractSpec := {
name := "InvalidFunctionParamIdentifier"
fields := []
constructor := none
functions := [
{ name := "store"
params := [{ name := "value-1", ty := ParamType.uint256 }]
returnType := none
body := [Stmt.stop]
}
]
}
match compile invalidFunctionParamIdentifierSpec [1] with
| .error err =>
if !contains err "function parameter name must be a valid identifier: value-1" then
throw (IO.userError s!"✗ function parameter identifier validation mismatch: {err}")
IO.println "✓ function parameter identifier validation"
| .ok _ =>
throw (IO.userError "✗ expected invalid function parameter identifier to fail compilation")
#eval! do
let invalidEventIdentifierSpec : ContractSpec := {
name := "InvalidEventIdentifier"
fields := []
constructor := none
events := [
{ name := "Value-Set"
params := [{ name := "who", ty := ParamType.address, kind := EventParamKind.indexed }]
}
]
functions := []
}
match compile invalidEventIdentifierSpec [] with
| .error err =>
if !contains err "event name must be a valid identifier: Value-Set" then
throw (IO.userError s!"✗ event identifier validation mismatch: {err}")
IO.println "✓ event identifier validation"
| .ok _ =>
throw (IO.userError "✗ expected invalid event identifier to fail compilation")
#eval! do
let invalidExternalIdentifierSpec : ContractSpec := {
name := "InvalidExternalIdentifier"
fields := []
constructor := none
externals := [
{ name := "hash-two"
params := [ParamType.uint256, ParamType.uint256]
returns := [ParamType.uint256]
axiomNames := ["hash_two_sound"]
}
]
functions := []
}
match compile invalidExternalIdentifierSpec [] with
| .error err =>
if !contains err "external declaration name must be a valid identifier: hash-two" then
throw (IO.userError s!"✗ external identifier validation mismatch: {err}")
IO.println "✓ external declaration identifier validation"
| .ok _ =>
throw (IO.userError "✗ expected invalid external declaration identifier to fail compilation")
#eval! do
let payableMsgValueSpec : ContractSpec := {
name := "PayableMsgValue"
fields := []
constructor := some {
params := []
isPayable := true
body := [Stmt.stop]
}
functions := [
{ name := "payableLike"
params := []
returnType := some FieldType.uint256
isPayable := true
body := [Stmt.return Expr.msgValue]
}
]
}
match compile payableMsgValueSpec [1] with
| .error err =>
throw (IO.userError s!"✗ expected payable msgValue usage to compile, got: {err}")
| .ok ir =>
let rendered := Yul.render (emitYul ir)
assertContains "payable msgValue compiles" rendered ["mstore(0, callvalue())"]
assertNotContains "payable function skips non-payable guard" rendered ["if callvalue()"]
#eval! do
let nonPayableGuardSpec : ContractSpec := {
name := "NonPayableGuard"
fields := []
constructor := none
functions := [
{ name := "noop"
params := []
returnType := none
body := [Stmt.stop]
}
]
}
match compile nonPayableGuardSpec [1] with
| .error err =>
throw (IO.userError s!"✗ expected non-payable function to compile, got: {err}")
| .ok ir =>
let rendered := Yul.render (emitYul ir)
assertContains "non-payable function emits msg.value guard" rendered ["if callvalue()"]
#eval! do
let lowLevelPrimitivesSpec : ContractSpec := {
name := "LowLevelPrimitives"
fields := []
constructor := none
functions := [
{ name := "doCall"
params := [
{ name := "target", ty := ParamType.address },
{ name := "inOffset", ty := ParamType.uint256 },
{ name := "inSize", ty := ParamType.uint256 },
{ name := "outOffset", ty := ParamType.uint256 },
{ name := "outSize", ty := ParamType.uint256 }
]
returnType := some FieldType.uint256
body := [
Stmt.letVar "ok" (Expr.call
(Expr.literal 50000)
(Expr.param "target")
(Expr.literal 0)
(Expr.param "inOffset")
(Expr.param "inSize")
(Expr.param "outOffset")
(Expr.param "outSize")),
Stmt.return (Expr.localVar "ok")
]
},
{ name := "doStatic"
params := [
{ name := "target", ty := ParamType.address },
{ name := "inOffset", ty := ParamType.uint256 },
{ name := "inSize", ty := ParamType.uint256 },
{ name := "outOffset", ty := ParamType.uint256 },
{ name := "outSize", ty := ParamType.uint256 }
]
returnType := some FieldType.uint256
body := [
Stmt.return (Expr.staticcall
(Expr.literal 50000)
(Expr.param "target")
(Expr.param "inOffset")
(Expr.param "inSize")
(Expr.param "outOffset")
(Expr.param "outSize"))
]
},
{ name := "doDelegate"
params := [
{ name := "target", ty := ParamType.address },
{ name := "inOffset", ty := ParamType.uint256 },
{ name := "inSize", ty := ParamType.uint256 },
{ name := "outOffset", ty := ParamType.uint256 },
{ name := "outSize", ty := ParamType.uint256 }
]
returnType := some FieldType.uint256
body := [
Stmt.return (Expr.delegatecall
(Expr.literal 50000)
(Expr.param "target")
(Expr.param "inOffset")
(Expr.param "inSize")
(Expr.param "outOffset")
(Expr.param "outSize"))
]
},
{ name := "bubble"
params := []
returnType := none
body := [
Stmt.letVar "rd" Expr.returndataSize,
Stmt.returndataCopy (Expr.literal 0) (Expr.literal 0) (Expr.localVar "rd"),
Stmt.revertReturndata
]
},
{ name := "erc20CompatCall"
params := [
{ name := "target", ty := ParamType.address },
{ name := "inOffset", ty := ParamType.uint256 },
{ name := "inSize", ty := ParamType.uint256 }
]
returnType := some FieldType.uint256
body := [
Stmt.letVar "ok" (Expr.call
(Expr.literal 50000)
(Expr.param "target")
(Expr.literal 0)
(Expr.param "inOffset")
(Expr.param "inSize")
(Expr.literal 0)
(Expr.literal 32)),
Stmt.return (Expr.logicalAnd
(Expr.localVar "ok")
(Expr.returndataOptionalBoolAt (Expr.literal 0)))
]
}
]
}
match compile lowLevelPrimitivesSpec [1, 2, 3, 4, 5] with
| .error err =>
throw (IO.userError s!"✗ expected first-class low-level primitives to compile, got: {err}")
| .ok ir =>
let rendered := Yul.render (emitYul ir)
assertContains "first-class call lowering" rendered ["call(50000, target, 0, inOffset, inSize, outOffset, outSize)"]
assertContains "first-class staticcall lowering" rendered ["staticcall(50000, target, inOffset, inSize, outOffset, outSize)"]
assertContains "first-class delegatecall lowering" rendered ["delegatecall(50000, target, inOffset, inSize, outOffset, outSize)"]
assertContains "first-class returndata primitives lowering" rendered ["let rd := returndatasize()", "returndatacopy(0, 0, rd)", "let __returndata_size := returndatasize()", "revert(0, __returndata_size)"]
assertContains "optional bool returndata helper lowering" rendered ["eq(returndatasize(), 0)", "eq(returndatasize(), 32)", "eq(mload(0), 1)"]
#eval! do
let typedIntrinsicSpec : ContractSpec := {
name := "TypedIntrinsics"
fields := []
constructor := none
functions := [
{ name := "domainProbe"
params := []
returnType := some FieldType.uint256
isView := true
body := [
Stmt.mstore (Expr.literal 0) Expr.contractAddress,
Stmt.mstore (Expr.literal 32) Expr.chainid,
Stmt.return (Expr.keccak256 (Expr.literal 0) (Expr.literal 64))
]
},
{ name := "peekWord"
params := [{ name := "offset", ty := ParamType.uint256 }]
returnType := some FieldType.uint256
isView := true
body := [Stmt.return (Expr.mload (Expr.param "offset"))]
},
{ name := "pureHash"
params := [{ name := "x", ty := ParamType.uint256 }]
returnType := some FieldType.uint256
isPure := true
body := [
Stmt.mstore (Expr.literal 0) (Expr.param "x"),
Stmt.return (Expr.keccak256 (Expr.literal 0) (Expr.literal 32))
]
}
]
}
match compile typedIntrinsicSpec [1, 2, 3] with
| .error err =>
throw (IO.userError s!"✗ expected typed intrinsics to compile, got: {err}")
| .ok ir =>
let rendered := Yul.render (emitYul ir)
assertContains "typed env/hash intrinsics lowering" rendered [
"mstore(0, address())",
"mstore(32, chainid())",
"mstore(0, keccak256(0, 64))",
"mstore(0, mload(offset))"
]
#eval! do
let lowLevelCallSpec : ContractSpec := {
name := "LowLevelCallUnsupported"
fields := []
constructor := none
functions := [
{ name := "unsafe"
params := [{ name := "target", ty := ParamType.address }]
returnType := some FieldType.uint256
body := [Stmt.return (Expr.externalCall "delegatecall" [Expr.param "target"])]
}
]
}
match compile lowLevelCallSpec [1] with
| .error err =>
if !(contains err "unsupported low-level call 'delegatecall'" && contains err "Issue #586") then
throw (IO.userError s!"✗ low-level call diagnostic mismatch: {err}")
IO.println "✓ low-level call unsupported diagnostic"
| .ok _ =>
throw (IO.userError "✗ expected low-level call usage to fail compilation")
#eval! do
let eagerLogicalCallSpec : ContractSpec := {
name := "EagerLogicalCall"
fields := []
constructor := none
functions := [
{ name := "unsafe"
params := [{ name := "target", ty := ParamType.address }]
returnType := some FieldType.uint256
body := [
Stmt.return (Expr.logicalAnd
(Expr.literal 0)
(Expr.call
(Expr.literal 50000)
(Expr.param "target")
(Expr.literal 0)
(Expr.literal 0)
(Expr.literal 0)
(Expr.literal 0)
(Expr.literal 0)))
]
}
]
}
match compile eagerLogicalCallSpec [1] with
| .error err =>
if !(contains err "Expr.logicalAnd/Expr.logicalOr" &&
contains err "call-like operand(s)" &&
contains err "Issue #748") then
throw (IO.userError s!"✗ logical call operand diagnostic mismatch: {err}")
IO.println "✓ logical call operand validation"
| .ok _ =>
throw (IO.userError "✗ expected call-like logical operand to fail compilation")
#eval! do
let eagerLogicalExternalSpec : ContractSpec := {
name := "EagerLogicalExternal"
fields := []
constructor := none
externals := [
{ name := "oracle"
params := [ParamType.uint256]
returns := [ParamType.uint256]
axiomNames := []
}
]
functions := [
{ name := "unsafe"
params := [{ name := "x", ty := ParamType.uint256 }]
returnType := some FieldType.uint256
body := [
Stmt.return (Expr.logicalOr
(Expr.literal 1)
(Expr.externalCall "oracle" [Expr.param "x"]))
]
}
]
}
match compile eagerLogicalExternalSpec [1] with
| .error err =>
if !(contains err "Expr.logicalAnd/Expr.logicalOr" &&
contains err "call-like operand(s)" &&
contains err "Issue #748") then
throw (IO.userError s!"✗ logical external operand diagnostic mismatch: {err}")
IO.println "✓ logical external operand validation"
| .ok _ =>
throw (IO.userError "✗ expected external-call logical operand to fail compilation")
#eval! do
let staticcallSpec : ContractSpec := {
name := "StaticcallUnsupported"
fields := []
constructor := none
functions := [
{ name := "unsafe"
params := [{ name := "target", ty := ParamType.address }]
returnType := some FieldType.uint256
body := [Stmt.return (Expr.externalCall "staticcall" [Expr.param "target"])]
}
]
}
match compile staticcallSpec [1] with
| .error err =>
if !(contains err "unsupported low-level call 'staticcall'" && contains err "Issue #586") then
throw (IO.userError s!"✗ staticcall diagnostic mismatch: {err}")
IO.println "✓ staticcall unsupported diagnostic"
| .ok _ =>
throw (IO.userError "✗ expected staticcall usage to fail compilation")
#eval! do
let ctorMsgValueSpec : ContractSpec := {
name := "CtorMsgValuePayable"
fields := []
constructor := some {
params := []
isPayable := true
body := [Stmt.letVar "v" Expr.msgValue, Stmt.stop]
}
functions := [
{ name := "noop"
params := []
returnType := none
isPayable := true
body := [Stmt.stop]
}
]
}
match compile ctorMsgValueSpec [1] with
| .error err =>
throw (IO.userError s!"✗ expected payable constructor msgValue usage to compile, got: {err}")
| .ok ir =>
let rendered := Yul.render (emitYul ir)
assertContains "payable constructor msgValue compiles" rendered ["let v := callvalue()"]
assertNotContains "payable constructor skips non-payable guard" rendered ["if callvalue()"]
#eval! do
let ctorNonPayableGuardSpec : ContractSpec := {
name := "CtorNonPayableGuard"
fields := []
constructor := some {
params := []
body := [Stmt.stop]
}
functions := [
{ name := "noop"
params := []
returnType := none
isPayable := true
body := [Stmt.stop]
}
]
}
match compile ctorNonPayableGuardSpec [1] with
| .error err =>
throw (IO.userError s!"✗ expected non-payable constructor to compile, got: {err}")
| .ok ir =>
let rendered := Yul.render (emitYul ir)
assertContains "non-payable constructor emits msg.value guard" rendered ["if callvalue()"]
#eval! do
let ctorLowLevelCallSpec : ContractSpec := {
name := "CtorLowLevelCallUnsupported"
fields := []
constructor := some {
params := []
body := [Stmt.letVar "v" (Expr.externalCall "call" []), Stmt.stop]
}
functions := [
{ name := "noop"
params := []
returnType := none
body := [Stmt.stop]
}
]
}
match compile ctorLowLevelCallSpec [1] with
| .error err =>
if !(contains err "unsupported low-level call 'call'" && contains err "Issue #586") then
throw (IO.userError s!"✗ constructor low-level call diagnostic mismatch: {err}")
IO.println "✓ constructor low-level call unsupported diagnostic"
| .ok _ =>
throw (IO.userError "✗ expected constructor low-level call usage to fail compilation")
#eval! do
let ctorCallcodeSpec : ContractSpec := {
name := "CtorCallcodeUnsupported"
fields := []
constructor := some {
params := []
body := [Stmt.letVar "v" (Expr.externalCall "callcode" []), Stmt.stop]
}
functions := [
{ name := "noop"
params := []
returnType := none
body := [Stmt.stop]
}
]
}
match compile ctorCallcodeSpec [1] with
| .error err =>
if !(contains err "unsupported low-level call 'callcode'" && contains err "Issue #586") then
throw (IO.userError s!"✗ constructor callcode diagnostic mismatch: {err}")
IO.println "✓ constructor callcode unsupported diagnostic"
| .ok _ =>
throw (IO.userError "✗ expected constructor callcode usage to fail compilation")
#eval! do
let ctorBoolParamSpec : ContractSpec := {
name := "CtorBoolParamNormalization"
fields := []
constructor := some {
params := [{ name := "flag", ty := ParamType.bool }]
body := [Stmt.letVar "seen" (Expr.constructorArg 0), Stmt.stop]
}
functions := [
{ name := "noop"
params := []
returnType := none
body := [Stmt.stop]
}
]
}
match compile ctorBoolParamSpec [1] with
| .error err =>
throw (IO.userError s!"✗ expected bool constructor param normalization to compile, got: {err}")
| .ok ir =>
let rendered := Yul.render (emitYul ir)
assertContains "constructor bool param normalization" rendered
["let __abi_bool_word_0 := mload(0)",
"if iszero(or(eq(__abi_bool_word_0, 0), eq(__abi_bool_word_0, 1))) {",
"let flag := iszero(iszero(__abi_bool_word_0))", "let arg0 := flag"]
#eval! do
let ctorDynamicParamSpec : ContractSpec := {
name := "CtorDynamicParamSupported"
fields := []
constructor := some {
params := [{ name := "payload", ty := ParamType.bytes }]
body := [Stmt.stop]
}
functions := [
{ name := "noop"
params := []
returnType := none
body := [Stmt.stop]
}
]
}
match compile ctorDynamicParamSpec [1] with
| .error err =>
throw (IO.userError s!"✗ expected dynamic constructor parameter support to compile, got: {err}")
| .ok ir =>
let rendered := Yul.render (emitYul ir)
assertContains "constructor dynamic param decode" rendered
["if lt(argsSize, 32) {", "let payload_offset := mload(0)",
"let payload_abs_offset := payload_offset",
"let payload_length := mload(payload_abs_offset)", "let payload_data_offset := payload_tail_head_end",
"let arg0 := payload_offset"]
#eval! do
let ctorMixedParamSpec : ContractSpec := {
name := "CtorMixedParamDecode"
fields := []
constructor := some {
params := [
{ name := "owner", ty := ParamType.address },
{ name := "payload", ty := ParamType.bytes }
]
body := [Stmt.stop]
}
events := []
errors := []
functions := [{ name := "noop", params := [], returnType := none, body := [Stmt.stop] }]
}
match compile ctorMixedParamSpec [1] with
| .error err =>
throw (IO.userError s!"✗ expected mixed constructor parameter support to compile, got: {err}")
| .ok ir =>
let rendered := Yul.render (emitYul ir)
assertContains "constructor mixed param decode" rendered
["if lt(argsSize, 64) {", "let owner := and(mload(0),", "let payload_offset := mload(32)",
"let payload_length := mload(payload_abs_offset)", "let arg0 := owner",
"let arg1 := payload_offset"]
#eval! do
let ctorDynamicReadSpec : ContractSpec := {
name := "CtorDynamicReadSource"
fields := []
constructor := some {
params := [{ name := "numbers", ty := ParamType.array ParamType.uint256 }]
body := [
Stmt.letVar "firstWord" (Expr.arrayElement "numbers" (Expr.literal 0)),
Stmt.stop
]
}
events := []
errors := []
functions := [{ name := "noop", params := [], returnType := none, body := [Stmt.stop] }]
}
match compile ctorDynamicReadSpec [1] with
| .error err =>
throw (IO.userError s!"✗ expected constructor dynamic read support to compile, got: {err}")
| .ok ir =>
let rendered := Yul.render (emitYul ir)
assertContains "constructor dynamic read source" rendered
["function __verity_array_element_memory_checked(data_offset, length, index) -> word",
"if iszero(lt(index, length)) {",
"revert(0, 0)",
"let firstWord := __verity_array_element_memory_checked(numbers_data_offset, numbers_length, 0)"]
assertNotContains "constructor dynamic read source" rendered
["let firstWord := calldataload(add(numbers_data_offset, mul(0, 32)))",
"let firstWord := mload(add(numbers_data_offset, mul(0, 32)))"]
#eval! do
let callSpec : ContractSpec := {
name := "CallUnsupported"
fields := []
constructor := none
functions := [
{ name := "unsafe"
params := [{ name := "target", ty := ParamType.address }]
returnType := some FieldType.uint256
body := [Stmt.return (Expr.externalCall "call" [Expr.param "target"])]
}
]
}
match compile callSpec [1] with
| .error err =>
if !(contains err "unsupported low-level call 'call'" && contains err "Issue #586") then
throw (IO.userError s!"✗ call diagnostic mismatch: {err}")
IO.println "✓ call unsupported diagnostic"
| .ok _ =>
throw (IO.userError "✗ expected call usage to fail compilation")
#eval! do
let noArrayElementSpec : ContractSpec := {
name := "NoArrayElementHelpers"
fields := []
constructor := none
functions := [
{ name := "value"
params := [{ name := "x", ty := ParamType.uint256 }]
returnType := some FieldType.uint256
body := [Stmt.return (Expr.param "x")]
}
]
}
match compile noArrayElementSpec [1] with
| .error err =>
throw (IO.userError s!"✗ expected non-array contract to compile, got: {err}")
| .ok ir =>
let rendered := Yul.render (emitYul ir)
assertNotContains "array helper injection is usage-gated" rendered
["function __verity_array_element_calldata_checked(data_offset, length, index) -> word",
"function __verity_array_element_memory_checked(data_offset, length, index) -> word"]
#eval! do
let balanceSpec : ContractSpec := {
name := "BalanceUnsupported"
fields := []
constructor := none
functions := [
{ name := "probe"
params := [{ name := "target", ty := ParamType.address }]
returnType := some FieldType.uint256
body := [Stmt.return (Expr.externalCall "balance" [Expr.param "target"])]
}
]
}
match compile balanceSpec [1] with
| .error err =>
if !(contains err "unsupported interop builtin call 'balance'" && contains err "Issue #586") then
throw (IO.userError s!"✗ balance diagnostic mismatch: {err}")
IO.println "✓ balance unsupported diagnostic"
| .ok _ =>
throw (IO.userError "✗ expected balance usage to fail compilation")
#eval! do
let gasPriceSpec : ContractSpec := {
name := "GasPriceUnsupported"
fields := []
constructor := none
functions := [
{ name := "probe"
params := []
returnType := some FieldType.uint256
body := [Stmt.return (Expr.externalCall "gasprice" [])]
}
]
}
match compile gasPriceSpec [1] with
| .error err =>
if !(contains err "unsupported interop builtin call 'gasprice'" && contains err "Issue #586") then
throw (IO.userError s!"✗ gasprice diagnostic mismatch: {err}")
IO.println "✓ gasprice unsupported diagnostic"
| .ok _ =>
throw (IO.userError "✗ expected gasprice usage to fail compilation")
#eval! do
let blobBaseFeeSpec : ContractSpec := {
name := "BlobBaseFeeUnsupported"
fields := []
constructor := none
functions := [
{ name := "probe"
params := []
returnType := some FieldType.uint256
body := [Stmt.return (Expr.externalCall "blobbasefee" [])]
}
]
}
match compile blobBaseFeeSpec [1] with
| .error err =>
if !(contains err "unsupported interop builtin call 'blobbasefee'" && contains err "Issue #586") then
throw (IO.userError s!"✗ blobbasefee diagnostic mismatch: {err}")
IO.println "✓ blobbasefee unsupported diagnostic"
| .ok _ =>
throw (IO.userError "✗ expected blobbasefee usage to fail compilation")
#eval! do
let blobHashSpec : ContractSpec := {
name := "BlobHashUnsupported"
fields := []