-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.v
1080 lines (912 loc) · 27.6 KB
/
tests.v
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
Require Import bbv.Word.
Require Import Nat.
Require Import Coq.NArith.NArith.
Require Import FORVES.constants.
Import Constants.
Require Import FORVES.symbolic_execution.
Import SymbolicExecution.
Require Import FORVES.storage_ops_solvers_impl.
Import StorageOpsSolversImpl.
Require Import FORVES.memory_ops_solvers_impl.
Import MemoryOpsSolversImpl.
Require Import FORVES.symbolic_state.
Import SymbolicState.
Require Import FORVES.symbolic_state_cmp.
Import SymbolicStateCmp.
Require Import FORVES.program.
Import Program.
Require Import FORVES.stack_operation_instructions.
Import StackOpInstrs.
Require Import FORVES.symbolic_state.
Import SymbolicState.
Require Import List.
Import ListNotations.
Require Import FORVES.execution_state.
Import ExecutionState.
Require Import FORVES.concrete_interpreter.
Import ConcreteInterpreter.
Require Import FORVES.constants.
Import Constants.
Require Import List.
Require Import bbv.Word.
Require Import Coq.NArith.NArith.
Require Import FORVES.constants.
Import Constants.
Require Import FORVES.program.
Import Program.
Require Import FORVES.block_equiv_checker.
Import BlockEquivChecker.
Require Import FORVES.symbolic_state.
Import SymbolicState.
Require Import FORVES.stack_operation_instructions.
Import StackOpInstrs.
Require Import FORVES.execution_state.
Import ExecutionState.
Require Import FORVES.concrete_interpreter.
Import ConcreteInterpreter.
Require Import FORVES.optimizations_def.
Import Optimizations_Def.
Require Import FORVES.optimizations.sub_x_x.
Import Opt_sub_x_x.
Require Import FORVES.optimizations.and_address.
Import Opt_and_address.
Require Import FORVES.symbolic_execution.
Import SymbolicExecution.
Require Import FORVES.storage_ops_solvers.
Import StorageOpsSolvers.
Require Import FORVES.storage_ops_solvers_impl.
Import StorageOpsSolversImpl.
Require Import FORVES.memory_ops_solvers.
Import MemoryOpsSolvers.
Require Import FORVES.memory_ops_solvers_impl.
Import MemoryOpsSolversImpl.
Require Import FORVES.symbolic_state_cmp.
Import SymbolicStateCmp.
Require Import FORVES.valid_symbolic_state.
Import ValidSymbolicState.
Require Import FORVES.symbolic_execution_soundness.
Import SymbolicExecutionSoundness.
Require Import FORVES.symbolic_state_eval.
Import SymbolicStateEval.
Require Import FORVES.symbolic_state.
Import SymbolicState.
Require Import FORVES.symbolic_state_cmp_impl.
Import SymbolicStateCmpImpl.
Require Import FORVES.sstack_val_cmp_impl.
Import SStackValCmpImpl.
Require Import FORVES.sstack_val_cmp_impl_soundness.
Import SStackValCmpImplSoundness.
Require Import FORVES.sha3_cmp_impl.
Import SHA3CmpImpl.
Require Import FORVES.sha3_cmp_impl_soundness.
Import SHA3CmpImplSoundness.
Require Import FORVES.storage_cmp_impl.
Import StorageCmpImpl.
Require Import FORVES.storage_cmp_impl_soundness.
Import StorageCmpImplSoundness.
Require Import FORVES.memory_cmp_impl.
Import MemoryCmpImpl.
Require Import FORVES.memory_cmp_impl_soundness.
Import MemoryCmpImplSoundness.
Require Import FORVES.storage_ops_solvers_impl.
Import StorageOpsSolversImpl.
Require Import FORVES.storage_ops_solvers_impl_soundness.
Import StorageOpsSolversImplSoundness.
Require Import FORVES.memory_ops_solvers_impl.
Import MemoryOpsSolversImpl.
Require Import FORVES.memory_ops_solvers_impl_soundness.
Import MemoryOpsSolversImplSoundness.
Require Import FORVES.misc.
Import Misc.
Require Import FORVES.optimizations_common.
Import Optimizations_Common.
Require Import FORVES.parser.
Import Parser.
From Coq Require Import Strings.String.
From Coq Require Import Lists.List. Import ListNotations.
Module OptimizationRuleTests.
(* string to block that always succeed *)
Definition str2block (s : string) : block :=
match parse_block s with
| None => []
| Some b => b
end.
Definition check_rule (s1 s2: string) (step: available_optimization_step) :=
let b1 := str2block s1 in
let b2 := str2block s2 in
let r1 := (evm_eq_block_chkr SMemUpdater_Basic SStrgUpdater_Basic
MLoadSolver_Basic SLoadSolver_Basic SStackValCmp_Basic SMemCmp_PO
SStrgCmp_Basic SHA3Cmp_Trivial
[step] 10 10 b1 b2 3) in
let r2 := (evm_eq_block_chkr SMemUpdater_Basic SStrgUpdater_Basic
MLoadSolver_Basic SLoadSolver_Basic SStackValCmp_Basic SMemCmp_PO
SStrgCmp_Basic SHA3Cmp_Trivial
[] 10 10 b1 b2 3) in
r1 = true /\ r2 = false.
Example ex_balance_address:
(* BALANCE(ADDRESS) = SELFBALANCE *)
check_rule "ADDRESS BALANCE"
"SELFBALANCE"
OPT_balance_address.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_coinbase1:
(* AND(COINBASE, 2^160 - 1) = COINBASE *)
check_rule "PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF COINBASE AND"
"COINBASE"
OPT_and_coinbase.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_coinbase2:
check_rule "COINBASE PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND"
"COINBASE"
OPT_and_coinbase.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_ffff1:
(* AND(2^256-1,X) = X *)
check_rule "PUSH32 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff AND"
""
OPT_and_ffff.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_ffff2:
(* AND(X,2^256-1) = X *)
check_rule "PUSH32 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff SWAP1 AND"
""
OPT_and_ffff.
Proof. unfold check_rule. intuition. Qed.
Example ex_or_ffff1:
(* OR(2^256-1,X) = 2^256-1 *)
check_rule "PUSH32 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff OR"
"POP PUSH32 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
OPT_or_ffff.
Proof. unfold check_rule. intuition. Qed.
Example ex_or_ffff2:
(* OR(X,2^256-1) = 2^256-1 *)
check_rule "PUSH32 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff SWAP1 OR"
"POP PUSH32 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
OPT_or_ffff.
Proof. unfold check_rule. intuition. Qed.
Example ex_or_zero1:
(* OR(X, 0) = X *)
check_rule "PUSH1 0x0 OR"
""
OPT_or_zero.
Proof. unfold check_rule. intuition. Qed.
Example ex_or_zero2:
(* OR(X, 0) = X *)
check_rule "PUSH1 0x0 SWAP1 OR"
""
OPT_or_zero.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_x_x:
(* AND(X, X) = X *)
check_rule "DUP1 AND"
""
OPT_and_x_x.
Proof. unfold check_rule. intuition. Qed.
Example ex_or_x_x:
(* OR(X, X) = X *)
check_rule "DUP1 OR"
""
OPT_or_x_x.
Proof. unfold check_rule. intuition. Qed.
Example ex_or_not1:
(* OR(NOT(X), X) = 2^256-1 *)
check_rule "DUP1 NOT OR"
"POP PUSH32 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
OPT_or_not.
Proof. unfold check_rule. intuition. Qed.
Example ex_or_not2:
(* OR(X, NOT(X)) = 0 *)
check_rule "DUP1 NOT SWAP1 OR"
"POP PUSH32 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
OPT_or_not.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_not1:
(* AND(NOT(X), X) = 0 *)
check_rule "DUP1 NOT AND"
"POP PUSH1 0x0"
OPT_and_not.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_not2:
(* AND(X, NOT(X)) = 0 *)
check_rule "DUP1 NOT SWAP1 AND"
"POP PUSH1 0x0"
OPT_and_not.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_or1:
(* AND(OR(X, Y), X) = X *)
check_rule "DUP1 SWAP2 SWAP1 OR AND"
"SWAP1 POP"
OPT_and_or.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_or2:
(* AND(OR(Y, X), X) = X *)
check_rule "DUP1 SWAP2 OR AND"
"SWAP1 POP"
OPT_and_or.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_or3:
(* AND(X, OR(X, Y)) = X *)
check_rule "DUP1 SWAP2 SWAP1 OR SWAP1 AND"
"SWAP1 POP"
OPT_and_or.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_or4:
(* AND(X, OR(Y, X)) = X *)
check_rule "DUP1 SWAP2 OR SWAP1 AND"
"SWAP1 POP"
OPT_and_or.
Proof. unfold check_rule. intuition. Qed.
Example ex_or_and1:
(* OR(AND(X, Y), X) = X *)
check_rule "DUP1 SWAP2 SWAP1 AND OR"
"SWAP1 POP"
OPT_or_and.
Proof. unfold check_rule. intuition. Qed.
Example ex_or_and2:
(* OR(AND(Y, X), X) = X *)
check_rule "DUP1 SWAP2 AND OR"
"SWAP1 POP"
OPT_or_and.
Proof. unfold check_rule. intuition. Qed.
Example ex_or_and3:
(* OR(X, AND(X, Y)) = X *)
check_rule "DUP1 SWAP2 SWAP1 AND SWAP1 OR"
"SWAP1 POP"
OPT_or_and.
Proof. unfold check_rule. intuition. Qed.
Example ex_or_and4:
(* OR(X, AND(Y, X)) = X *)
check_rule "DUP1 SWAP2 AND SWAP1 OR"
"SWAP1 POP"
OPT_or_and.
Proof. unfold check_rule. intuition. Qed.
Example ex_or_or1:
(* OR(OR(X, Y), X) = OR(X,Y) *)
check_rule "DUP1 SWAP2 SWAP1 OR OR"
"OR"
OPT_or_or.
Proof. unfold check_rule. intuition. Qed.
Example ex_or_or1':
(* OR(OR(X, Y), X) = OR(Y,X) *)
check_rule "DUP1 SWAP2 SWAP1 OR OR"
"SWAP1 OR"
OPT_or_or.
Proof. unfold check_rule. intuition. Qed.
Example ex_or_or2:
(* OR(OR(Y, X), X) = OR(X,Y) *)
check_rule "DUP1 SWAP2 OR OR"
"OR"
OPT_or_or.
Proof. unfold check_rule. intuition. Qed.
Example ex_or_or2':
(* OR(OR(Y, X), X) = OR(Y,X) *)
check_rule "DUP1 SWAP2 OR OR"
"SWAP1 OR"
OPT_or_or.
Proof. unfold check_rule. intuition. Qed.
Example ex_or_or3:
(* OR(X, OR(X, Y)) = OR(X,Y) *)
check_rule "DUP1 SWAP2 SWAP1 OR SWAP1 OR"
"OR"
OPT_or_or.
Proof. unfold check_rule. intuition. Qed.
Example ex_or_or3':
(* OR(X, OR(X, Y)) = OR(Y,X) *)
check_rule "DUP1 SWAP2 SWAP1 OR SWAP1 OR"
"SWAP1 OR"
OPT_or_or.
Proof. unfold check_rule. intuition. Qed.
Example ex_or_or4:
(* OR(OR(Y, X), X) = OR(X,Y) *)
check_rule "DUP1 SWAP2 OR SWAP1 OR"
"OR"
OPT_or_or.
Proof. unfold check_rule. intuition. Qed.
Example ex_or_or4':
(* OR(OR(Y, X), X) = OR(Y,X) *)
check_rule "DUP1 SWAP2 OR SWAP1 OR"
"SWAP1 OR"
OPT_or_or.
Proof. unfold check_rule. intuition. Qed.
Example ex_xor_xor1:
(* XOR(X, XOR(X,Y)) = Y *)
check_rule "DUP1 SWAP2 SWAP1 XOR SWAP1 XOR"
"POP"
OPT_xor_xor.
Proof. unfold check_rule. intuition. Qed.
Example ex_xor_xor2:
(* XOR(X, XOR(Y,X)) = Y *)
check_rule "DUP1 SWAP2 XOR SWAP1 XOR"
"POP"
OPT_xor_xor.
Proof. unfold check_rule. intuition. Qed.
Example ex_xor_xor3:
(* XOR(XOR(X,Y), X) = Y *)
check_rule "DUP1 SWAP2 SWAP1 XOR XOR"
"POP"
OPT_xor_xor.
Proof. unfold check_rule. intuition. Qed.
Example ex_xor_xor4:
(* XOR(XOR(Y,X), X) = Y *)
check_rule "DUP1 SWAP2 XOR XOR"
"POP"
OPT_xor_xor.
Proof. unfold check_rule. intuition. Qed.
Example ex_xor_zero1:
(* XOR(X, 0) = X *)
check_rule "PUSH1 0x0 XOR"
""
OPT_xor_zero.
Proof. unfold check_rule. intuition. Qed.
Example ex_xor_zero2:
(* XOR(X, 0) = X *)
check_rule "PUSH1 0x0 SWAP1 XOR"
""
OPT_xor_zero.
Proof. unfold check_rule. intuition. Qed.
Example ex_xor_x_x:
(* XOR(X, X) = 0 *)
check_rule "DUP1 XOR"
"POP PUSH1 0x0"
OPT_xor_x_x.
Proof. unfold check_rule. intuition. Qed.
Example ex_iszero2_eq:
(* ISZERO(ISZERO(EQ(X, Y))) = EQ(X,Y) *)
check_rule "EQ ISZERO ISZERO"
"EQ"
OPT_iszero2_eq.
Proof. unfold check_rule. intuition. Qed.
Example ex_iszero2_eq2:
check_rule "EQ ISZERO ISZERO"
"SWAP1 EQ"
OPT_iszero2_eq.
Proof. unfold check_rule. intuition. Qed.
Example ex_iszero2_eq3:
check_rule "SWAP1 EQ ISZERO ISZERO"
"EQ"
OPT_iszero2_eq.
Proof. unfold check_rule. intuition. Qed.
Example ex_iszero2_eq4:
check_rule "EQ ISZERO ISZERO"
"SWAP1 EQ"
OPT_iszero2_eq.
Proof. unfold check_rule. intuition. Qed.
Example ex_iszero2_lt:
(* ISZERO(ISZERO(LT(X, Y))) = LT(X, Y) *)
check_rule "LT ISZERO ISZERO"
"LT"
OPT_iszero2_lt.
Proof. unfold check_rule. intuition. Qed.
Example ex_iszero2_gt:
(* ISZERO(ISZERO(GT(X, Y))) = GT(X, Y) *)
check_rule "GT ISZERO ISZERO"
"GT"
OPT_iszero2_gt.
Proof. unfold check_rule. intuition. Qed.
Example ex_iszero_xor:
(* ISZERO(XOR(X, Y)) = EQ(X, Y) *)
check_rule "XOR ISZERO"
"EQ"
OPT_iszero_xor.
Proof. unfold check_rule. intuition. Qed.
Example ex_iszero_lt:
(* ISZERO(LT(0, X)) = ISZERO(X) *)
check_rule "PUSH1 0x0 LT ISZERO"
"ISZERO"
OPT_iszero_lt.
Proof. unfold check_rule. intuition. Qed.
Example ex_iszero_sub:
(* ISZERO(SUB(X, Y)) = EQ(X, Y) *)
check_rule "SUB ISZERO"
"EQ"
OPT_iszero_sub.
Proof. unfold check_rule. intuition. Qed.
Example ex_eq_x_x:
check_rule "PUSH1 0x20 PUSH1 0x20 EQ"
"PUSH1 0x1"
OPT_eq_x_x.
Proof. unfold check_rule. intuition. Qed.
Example ex_lt_x_x:
check_rule "PUSH1 0x20 PUSH1 0x20 LT"
"PUSH1 0x0"
OPT_lt_x_x.
Proof. unfold check_rule. intuition. Qed.
Example ex_lt_x_zero:
check_rule "PUSH1 0x0 PUSH1 0x20 LT"
"PUSH1 0x0"
OPT_lt_x_zero.
Proof. unfold check_rule. intuition. Qed.
Example ex_gt_x_x:
check_rule "DUP1 GT"
"POP PUSH1 0x0"
OPT_gt_x_x.
Proof. unfold check_rule. intuition. Qed.
Example ex_gt_zero_x:
check_rule "PUSH1 0x0 GT"
"POP PUSH1 0x0"
OPT_gt_zero_x.
Proof. unfold check_rule. intuition. Qed.
Example ex_exp_two_x:
check_rule "PUSH1 0x10 PUSH1 0x2 EXP"
"PUSH1 0x1 PUSH1 0x10 SHL"
OPT_exp_two_x.
Proof. unfold check_rule. intuition. Qed.
Example ex_exp_zero_x:
check_rule "PUSH1 0x10 PUSH1 0x0 EXP"
"PUSH1 0x10 ISZERO"
OPT_exp_zero_x.
Proof. unfold check_rule. intuition. Qed.
Example ex_exp_one_x:
check_rule "PUSH1 0x1 EXP"
"POP PUSH1 0x1"
OPT_exp_one_x.
Proof. unfold check_rule. intuition. Qed.
Example ex_exp_x_one:
check_rule "PUSH1 0x1 SWAP1 EXP"
""
OPT_exp_x_one.
Proof. unfold check_rule. intuition. Qed.
Example ex_exp_x_zero:
check_rule "PUSH1 0x0 SWAP1 EXP"
"POP PUSH1 0x1"
OPT_exp_x_zero.
Proof. unfold check_rule. intuition. Qed.
Example ex_mod_x_x:
check_rule "DUP1 MOD"
"POP PUSH1 0x0"
OPT_mod_x_x.
Proof. unfold check_rule. intuition. Qed.
Example ex_mod_zero:
check_rule "PUSH1 0x0 SWAP1 MOD"
"POP PUSH1 0x0"
OPT_mod_zero.
Proof. unfold check_rule. intuition. Qed.
Example ex_mod_one:
check_rule "PUSH1 0x1 SWAP1 MOD"
"POP PUSH1 0x0"
OPT_mod_one.
Proof. unfold check_rule. intuition. Qed.
Example ex_div_zero:
check_rule "PUSH1 0x0 SWAP1 DIV"
"POP PUSH1 0x0"
OPT_div_zero.
Proof. unfold check_rule. intuition. Qed.
Example ex_div_x_x:
check_rule "PUSH1 0x6 PUSH1 0x6 DIV"
"PUSH1 0x1"
OPT_eval.
Proof. unfold check_rule. intuition. Qed.
Example ex_mul_zero1:
check_rule "PUSH1 0x0 MUL"
"POP PUSH1 0x0"
OPT_mul_zero.
Proof. unfold check_rule. intuition. Qed.
Example ex_mul_zero2:
check_rule "PUSH1 0x0 SWAP1 MUL"
"POP PUSH1 0x0"
OPT_mul_zero.
Proof. unfold check_rule. intuition. Qed.
Example ex_shl_x_zero:
check_rule "PUSH1 0x0 SWAP1 SHL"
"POP PUSH1 0x0"
OPT_shl_x_zero.
Proof. unfold check_rule. intuition. Qed.
Example ex_sub_zero:
check_rule "PUSH1 0x0 SWAP1 SUB"
""
OPT_sub_zero.
Proof. unfold check_rule. intuition. Qed.
Example ex_shl_zero_x:
check_rule "PUSH1 0x0 SHL"
""
OPT_shl_zero_x.
Proof. unfold check_rule. intuition. Qed.
Example ex_add_sub1:
check_rule "DUP1 SWAP2 SUB ADD"
"POP"
OPT_add_sub.
Proof. unfold check_rule. intuition. Qed.
Example ex_add_sub2:
check_rule "DUP1 SWAP2 SUB SWAP1 ADD"
"POP"
OPT_add_sub.
Proof. unfold check_rule. intuition. Qed.
Example ex_iszero3:
check_rule "ISZERO ISZERO ISZERO"
"ISZERO"
OPT_iszero3.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_caller1:
(* AND(CALLER, 2^160 - 1) = CALLER *)
check_rule "PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF CALLER AND"
"CALLER"
OPT_and_caller.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_caller2:
check_rule "CALLER PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND"
"CALLER"
OPT_and_caller.
Proof. unfold check_rule. intuition. Qed.
Example ex_eq_zero1:
(* EQ(X, 0) = ISZERO(X) *)
check_rule "PUSH1 0x0 EQ"
"ISZERO"
OPT_eq_zero.
Proof. unfold check_rule. intuition. Qed.
Example ex_eq_zero2:
check_rule "PUSH1 0x0 SWAP1 EQ"
"ISZERO"
OPT_eq_zero.
Proof. unfold check_rule. intuition. Qed.
Example ex_iszero_gt:
(* ISZERO(GT(X, 0)) = ISZERO(X) *)
check_rule "PUSH1 0x0 SWAP1 GT ISZERO"
"ISZERO"
OPT_iszero_gt.
Proof. unfold check_rule. intuition. Qed.
Example ex_mul_one1:
(* MUL(X,1) = X *)
check_rule "PUSH1 0x1 MUL"
""
OPT_mul_one.
Proof. unfold check_rule. intuition. Qed.
Example ex_mul_one2:
check_rule "PUSH1 0x1 SWAP1 MUL"
""
OPT_mul_one.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_address1:
(* AND(ADDRESS, 2^160 - 1) = ADDRESS *)
check_rule "PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ADDRESS AND"
"ADDRESS"
OPT_and_address.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_address2:
check_rule "ADDRESS PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND"
"ADDRESS"
OPT_and_address.
Proof. unfold check_rule. intuition. Qed.
Example ex_gt_one_x:
(* GT(1, X) = ISZERO(X) *)
check_rule "PUSH1 0x1 GT"
"ISZERO"
OPT_gt_one_x.
Proof. unfold check_rule. intuition. Qed.
Example ex_lt_x_one:
(* LT(X, 1) = ISZERO(X) *)
check_rule "PUSH1 0x1 SWAP1 LT"
"ISZERO"
OPT_lt_x_one.
Proof. unfold check_rule. intuition. Qed.
Example ex_div_one:
(* DIV(X, 1) = X *)
check_rule "PUSH1 0x1 SWAP1 DIV"
""
OPT_div_one.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_zero1:
(* AND(X, 0) = 0 *)
check_rule "PUSH1 0x0 AND"
"POP PUSH1 0x0"
OPT_and_zero.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_zero2:
check_rule "PUSH1 0x0 SWAP1 AND"
"POP PUSH1 0x0"
OPT_and_zero.
Proof. unfold check_rule. intuition. Qed.
Example ex_sub_x_x:
(* SUB(X, X) = 0 *)
check_rule "DUP1 SUB"
"POP PUSH1 0x0"
OPT_sub_x_x.
Proof. unfold check_rule. intuition. Qed.
Example ex_eq_iszero1:
(* EQ(1, ISZERO(X)) = ISZERO(X) *)
check_rule "ISZERO PUSH1 0x1 EQ"
"ISZERO"
OPT_eq_iszero.
Proof. unfold check_rule. intuition. Qed.
Example ex_eq_iszero2:
check_rule "ISZERO PUSH1 0x1 SWAP1 EQ"
"ISZERO"
OPT_eq_iszero.
Proof. unfold check_rule. intuition. Qed.
Example ex_shr_x_zero:
(* SHR(X, 0) = 0 *)
check_rule "PUSH1 0x0 SWAP1 SHR"
"POP PUSH1 0x0"
OPT_shr_x_zero.
Proof. unfold check_rule. intuition. Qed.
Example ex_shr_zero_x:
(* SHR(0, X) = X *)
check_rule "PUSH1 0x0 SHR"
""
OPT_shr_zero_x.
Proof. unfold check_rule. intuition. Qed.
Example ex_div_shl:
(* DIV(X, SHL(Y, 1)) = SHR(Y, X) *)
check_rule "PUSH1 0x1 SWAP1 SHL SWAP1 DIV"
"SHR"
OPT_div_shl.
Proof. unfold check_rule. intuition. Qed.
Example ex_mul_shl1:
(* MUL(SHL(X, 1), Y ) = SHL(X, Y)
MUL(X, SHL(Y, 1)) = SHL(Y, X) *)
check_rule "PUSH1 0x1 SWAP1 SHL MUL"
"SHL"
OPT_mul_shl.
Proof. unfold check_rule. intuition. Qed.
Example ex_mul_shl2:
(* MUL(SHL(X, 1), Y ) = SHL(X, Y)
MUL(X, SHL(Y, 1)) = SHL(Y, X) *)
check_rule "PUSH1 0x1 SWAP1 SHL SWAP1 MUL"
"SHL"
OPT_mul_shl.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_origin1:
(* AND(ORIGIN, 2^160 - 1) = ORIGIN *)
check_rule "PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ORIGIN AND"
"ORIGIN"
OPT_and_origin.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_origin2:
check_rule "ORIGIN PUSH20 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF AND"
"ORIGIN"
OPT_and_origin.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_and1:
(* AND(AND(X, Y), X) = AND(X,Y) *)
check_rule "DUP1 SWAP2 SWAP1 AND AND"
"AND"
OPT_and_and.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_and1':
(* AND(AND(X, Y), X) = AND(Y,X) *)
check_rule "DUP1 SWAP2 SWAP1 AND AND"
"SWAP1 AND"
OPT_and_and.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_and2:
(* AND(AND(Y, X), X) = AND(X,Y) *)
check_rule "DUP1 SWAP2 AND AND"
"AND"
OPT_and_and.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_and2':
(* AND(AND(Y, X), X) = AND(Y,X) *)
check_rule "DUP1 SWAP2 AND AND"
"SWAP1 AND"
OPT_and_and.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_and3:
(* AND(X, AND(X, Y)) = AND(X,Y) *)
check_rule "DUP1 SWAP2 SWAP1 AND SWAP1 AND"
"AND"
OPT_and_and.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_and3':
(* AND(X, AND(X, Y)) = AND(Y,X) *)
check_rule "DUP1 SWAP2 SWAP1 AND SWAP1 AND"
"SWAP1 AND"
OPT_and_and.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_and4:
(* AND(AND(Y, X), X) = AND(X,Y) *)
check_rule "DUP1 SWAP2 AND SWAP1 AND"
"AND"
OPT_and_and.
Proof. unfold check_rule. intuition. Qed.
Example ex_and_and4':
(* AND(AND(Y, X), X) = AND(Y,X) *)
check_rule "DUP1 SWAP2 AND SWAP1 AND"
"SWAP1 AND"
OPT_and_and.
Proof. unfold check_rule. intuition. Qed.
Example ex_not_not:
(* NOT(NOT(X)) = X *)
check_rule "NOT NOT"
""
OPT_not_not.
Proof. unfold check_rule. intuition. Qed.
Example ex_eval1:
(* eval(op,X) = op(X) *)
check_rule "PUSH1 0x0 NOT"
"PUSH32 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
OPT_eval.
Proof. unfold check_rule. intuition. Qed.
Example ex_eval2:
(* eval(op,X,Y) = op(X,Y) *)
check_rule "PUSH1 0x3 PUSH1 0x2 ADD"
"PUSH1 0x5"
OPT_eval.
Proof. unfold check_rule. intuition. Qed.
Example ex_add_zero1:
(* ADD(X,0) = X *)
check_rule "PUSH1 0x0 SWAP1 ADD"
""
OPT_add_zero.
Proof. unfold check_rule. intuition. Qed.
Example ex_add_zero2:
check_rule "PUSH1 0x0 ADD"
""
OPT_add_zero.
Proof. unfold check_rule. intuition. Qed.
(*
(* Optimization not included *)
Example ex_add_add_1:
(* ADD(ADD(3,x),2) *)
check_rule "PUSH1 0x2 SWAP1 PUSH1 0x3 ADD ADD"
"PUSH1 0x5 ADD"
OPT_add_add.
Proof. unfold check_rule. intuition. Qed.
Example ex_add_add_2:
(* ADD(ADD(x,3),2) *)
check_rule "PUSH1 0x2 PUSH1 0x3 SWAP2 ADD ADD"
"PUSH1 0x5 ADD"
OPT_add_add.
Proof. unfold check_rule. intuition. Qed.
Example ex_add_add_3:
(* ADD(3, ADD(x,2)) *)
check_rule "PUSH1 0x2 SWAP1 ADD PUSH1 0x3 ADD"
"PUSH1 0x5 ADD"
OPT_add_add.
Proof. unfold check_rule. intuition. Qed.
Example ex_add_add_4:
(* ADD(3, ADD(2,x)) *)
check_rule "PUSH1 0x2 ADD PUSH1 0x3 ADD"
"PUSH1 0x5 ADD"
OPT_add_add.
Proof. unfold check_rule. intuition. Qed.
*)
End OptimizationRuleTests.
Module EVMStackOpTests.
(* Tests for evm_shr because of their alternative definition *)
Example ex_shr_1: evm_shr empty_externals [WZero; WOne] = WOne.
Proof.
reflexivity.
Qed.
Example ex_shr_2:
evm_shr empty_externals [WZero; natToWord EVMWordSize 555] =
natToWord EVMWordSize 555.
Proof.
reflexivity.
Qed.
Example ex_shr_3:
evm_shr empty_externals [natToWord EVMWordSize 10; natToWord EVMWordSize 1024] =
WOne.
Proof.
reflexivity.
Qed.
Example ex_shr_4:
evm_shr empty_externals [natToWord EVMWordSize 11; natToWord EVMWordSize 1024] =
WZero.
Proof.
reflexivity.
Qed.
Example ex_shr_5:
evm_shr empty_externals [WOne; natToWord EVMWordSize 2] =
WOne.
Proof.
reflexivity.
Qed.
Example ex_shr_6:
evm_shr empty_externals [natToWord EVMWordSize 4; natToWord EVMWordSize 255] =
natToWord EVMWordSize 15.
Proof.
reflexivity.
Qed.
Example ex_shr_7:
evm_shr empty_externals [natToWord EVMWordSize 16; WZero] = WZero.
Proof.
reflexivity.
Qed.
Example ex_shr_8:
evm_shr empty_externals [natToWord EVMWordSize 255; WOne] = WZero.
Proof.
reflexivity.
Qed.
Example ex_shr_9:
evm_shr empty_externals [natToWord EVMWordSize 547; WOne] = WZero.
Proof.
reflexivity.
Qed.
End EVMStackOpTests.
Module SymbExecTests.
(*Compute (evm_exec_block_c [PUSH 1 0x7; PUSH 1 0x1; OpInstr ADD]
empty_state
evm_stack_opm).
2, SymMETAPUSH' 5 53)*)
(*
Some (ExState stk=[0x8]
mem=fun _ : N => 0
strg=fun _ : N => 0
empty_externals
)
*)
(*
Compute (evm_sym_exec trivial_smemory_updater trivial_sstorage_updater trivial_mload_solver
trivial_sload_solver [PUSH 1 0x7; PUSH 1 0x1; OpInstr ADD] 0 evm_stack_opm).
(*
Some (SymExState instk_h=0
stk=[FreshVar 0]
mem=[]
strg=[]
exts=SymExts
smap=(SymMap 1 [(0, SymOp ADD [0x1;0x7])])
)
*)
Compute (evm_exec_block_c [OpInstr ADD]
(make_st [WOne; WOne; WOne]
empty_memory empty_storage empty_externals)
evm_stack_opm).
(*
Some (ExState stk=[0x2;0x1]
mem=fun _ : N => 0
strg=fun _ : N => 0
empty_externals
)