-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathevm.v
1773 lines (1632 loc) · 61.4 KB
/
evm.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
(* Coq 8.5pl2. *)
Require Import Ascii.
Require Import String.
Require Import List.
Require Import FMapInterface.
Require Import NArith.
Module Lang.
(* TODO: sort by opcode *)
Inductive instr := (** partial. adding those necessary. *)
(* 0s *)
| STOP
| ADD
| MUL
| SUB
| DIV
| SDIV
| MOD
| SMOD
| ADDMOD
| MULMOD
| EXP
| SIGNEXTEND
(* 10s *)
| LT
| GT
| SLT
| SGT
| EQ
| ISZERO
| AND
| OR
| XOR
| NOT
| BYTE
(* 20s *)
| SHA3
(* 30s *)
| ADDRESS
| BALANCE
| ORIGIN
| CALLER
| CALLVALUE
| CALLDATALOAD
| CALLDATASIZE
| CALLDATACOPY
| CODESIZE
| CODECOPY
| GASPRICE
| EXTCODESIZE
| EXTCODECOPY
(* 40s *)
| BLOCKHASH
| COINBASE
| TIMESTAMP
| NUMBER
| DIFFICULTY
| GASLIMIT
(* 50s *)
| POP
| MLOAD
| MSTORE
| MSTORE8
| SLOAD
| SSTORE
| JUMP
| JUMPI
| PC
| MSIZE
| GAS
| JUMPDEST
(* 60s, 70s *)
| PUSH_N : (* pushed value in hex with 0x *) string -> instr
(* 80s *)
| DUP1
| DUP2
| DUP3
| DUP4
| DUP5
| DUP6
| DUP7
| DUP8
| DUP9
| DUP10
| DUP11
| DUP12
| DUP13
| DUP14
| DUP15
| DUP16
(* 90s *)
| SWAP1
| SWAP2
| SWAP3
| SWAP4
| SWAP5
| SWAP6
| SWAP7
| SWAP8
| SWAP9
| SWAP10
| SWAP11
| SWAP12
| SWAP13
| SWAP14
| SWAP15
| SWAP16
(* a0s *)
| LOG0
| LOG1
| LOG2
| LOG3
| LOG4
(* f0s *)
| CREATE
| CALL
| CALLCODE
| RETURN
| DELEGATECALL
| SUICIDE
| UNKNOWN : string -> instr
.
Fixpoint string_half_len str :=
match str with
| String _ (String _ tl) => S (string_half_len tl)
| _ => O
end.
Definition instr_length (i : instr) : nat :=
match i with
| PUSH_N str => string_half_len str
| _ => 1
end.
Require Import Coq.Program.Wf.
Fixpoint drop_bytes (prog : list instr) (bytes : nat) {struct bytes} :=
match prog, bytes with
| _, O => prog
| PUSH_N str :: tl, S pre =>
drop_bytes tl (pre - (string_half_len str - 1))
| _ :: tl, S pre =>
drop_bytes tl pre
| nil, S _ => nil
end.
Open Scope N_scope.
Fixpoint program_bytes (prog : list instr) : N :=
match prog with
| nil => 0
| PUSH_N str :: tl =>
(N.of_nat (string_half_len str) - 1) +
program_bytes tl
| _ :: tl =>
1 + program_bytes tl
end.
Close Scope N_scope.
Inductive decoding_mode : Set :=
| first_zero
| first_x
| next_instr
| read_0
| read_1
| read_2
| read_3
| read_4
| read_5
| read_6
| read_7
| read_8
| read_9
| read_a
| read_b
| read_c
| read_d
| read_e
| read_f
| read_hex : nat (* remaining read, after the next char *)
-> list ascii (* read so far in reverse *) -> decoding_mode
.
Inductive decode_result : Set :=
| decode_success : list instr -> decode_result
| decode_failure : string -> decode_result
.
Close Scope string_scope.
Open Scope char_scope.
Definition rev0x : list ascii := "x" :: "0" :: nil.
Fixpoint rev_string_inner (lst : list ascii) (acc : string): string :=
match lst with
| nil => acc
| hd :: tl => rev_string_inner tl (String hd acc)
end.
Definition rev_string lst := rev_string_inner lst EmptyString.
(* sofar accumulates instructions in the reverse order *)
Open Scope string_scope.
Fixpoint decode_inner (str : string) (m : decoding_mode)
(sofar : list instr): decode_result :=
let after_0 (error_message : string) :=
match str with
| String "0" rest => decode_inner rest next_instr (STOP :: sofar)
| String "1" rest => decode_inner rest next_instr (ADD :: sofar)
| String "2" rest => decode_inner rest next_instr (MUL :: sofar)
| String "3" rest => decode_inner rest next_instr (SUB :: sofar)
| String "4" rest => decode_inner rest next_instr (DIV :: sofar)
| String "5" rest => decode_inner rest next_instr (SDIV :: sofar)
| String "6" rest => decode_inner rest next_instr (MOD :: sofar)
| String "7" rest => decode_inner rest next_instr (SMOD :: sofar)
| String "8" rest => decode_inner rest next_instr (ADDMOD :: sofar)
| String "9" rest => decode_inner rest next_instr (MULMOD :: sofar)
| String "a" rest => decode_inner rest next_instr (EXP :: sofar)
| String "b" rest => decode_inner rest next_instr (SIGNEXTEND :: sofar)
| String "c" rest => decode_inner rest next_instr (UNKNOWN "0c" :: sofar)
| String "d" rest => decode_inner rest next_instr (UNKNOWN "0d" :: sofar)
| String "e" rest => decode_inner rest next_instr (UNKNOWN "0e" :: sofar)
| String "f" rest => decode_inner rest next_instr (UNKNOWN "0f" :: sofar)
| _ => decode_failure error_message
end in
match m with
| first_zero =>
match str with
| String "0" rest => decode_inner rest first_x sofar
| String "1" rest => decode_inner rest read_1 sofar
| String "2" rest => decode_inner rest read_2 sofar
| String "3" rest => decode_inner rest read_3 sofar
| String "4" rest => decode_inner rest read_4 sofar
| String "5" rest => decode_inner rest read_5 sofar
| String "6" rest => decode_inner rest read_6 sofar
| String "7" rest => decode_inner rest read_7 sofar
| String "8" rest => decode_inner rest read_8 sofar
| String "9" rest => decode_inner rest read_9 sofar
| String "a" rest => decode_inner rest read_a sofar
| String "b" rest => decode_inner rest read_b sofar
| String "c" rest => decode_inner rest read_c sofar
| String "d" rest => decode_inner rest read_d sofar
| String "e" rest => decode_inner rest read_e sofar
| String "f" rest => decode_inner rest read_f sofar
| _ => decode_failure "first nonzero"
end
| first_x =>
match str with
| String "x" rest => decode_inner rest next_instr sofar
(* since we are not reading x, 0 must have been first byte of the code*)
| _ => after_0 "second character not x nor hex digit"
end
| next_instr =>
match str with
| String "0" rest => decode_inner rest read_0 sofar
| String "1" rest => decode_inner rest read_1 sofar
| String "2" rest => decode_inner rest read_2 sofar
| String "3" rest => decode_inner rest read_3 sofar
| String "4" rest => decode_inner rest read_4 sofar
| String "5" rest => decode_inner rest read_5 sofar
| String "6" rest => decode_inner rest read_6 sofar
| String "7" rest => decode_inner rest read_7 sofar
| String "8" rest => decode_inner rest read_8 sofar
| String "9" rest => decode_inner rest read_9 sofar
| String "a" rest => decode_inner rest read_a sofar
| String "b" rest => decode_inner rest read_b sofar
| String "c" rest => decode_inner rest read_c sofar
| String "d" rest => decode_inner rest read_d sofar
| String "e" rest => decode_inner rest read_e sofar
| String "f" rest => decode_inner rest read_f sofar
| EmptyString => decode_success (List.rev sofar)
| _ => decode_failure "?"
end
| read_0 => after_0 "0?"
| read_1 =>
match str with
| String "0" rest => decode_inner rest next_instr (LT :: sofar)
| String "1" rest => decode_inner rest next_instr (GT :: sofar)
| String "2" rest => decode_inner rest next_instr (SLT :: sofar)
| String "3" rest => decode_inner rest next_instr (SGT :: sofar)
| String "4" rest => decode_inner rest next_instr (EQ :: sofar)
| String "5" rest => decode_inner rest next_instr (ISZERO :: sofar)
| String "6" rest => decode_inner rest next_instr (AND :: sofar)
| String "7" rest => decode_inner rest next_instr (OR :: sofar)
| String "8" rest => decode_inner rest next_instr (XOR :: sofar)
| String "9" rest => decode_inner rest next_instr (NOT :: sofar)
| String "a" rest => decode_inner rest next_instr (BYTE :: sofar)
| String "b" rest => decode_inner rest next_instr (UNKNOWN "1f" :: sofar)
| String "c" rest => decode_inner rest next_instr (UNKNOWN "1f" :: sofar)
| String "d" rest => decode_inner rest next_instr (UNKNOWN "1f" :: sofar)
| String "e" rest => decode_inner rest next_instr (UNKNOWN "1f" :: sofar)
| String "f" rest => decode_inner rest next_instr (UNKNOWN "1f" :: sofar)
| _ => decode_failure "1?"
end
| read_2 =>
match str with
| String "0" rest => decode_inner rest next_instr (SHA3 :: sofar)
| String "7" rest => decode_inner rest next_instr (UNKNOWN "27" :: sofar)
| String _ rest => decode_inner rest next_instr (UNKNOWN "2?" :: sofar)
| _ => decode_failure "2$"
end
| read_3 =>
match str with
| String "0" rest => decode_inner rest next_instr (ADDRESS :: sofar)
| String "1" rest => decode_inner rest next_instr (BALANCE :: sofar)
| String "2" rest => decode_inner rest next_instr (ORIGIN :: sofar)
| String "3" rest => decode_inner rest next_instr (CALLER :: sofar)
| String "4" rest => decode_inner rest next_instr (CALLVALUE :: sofar)
| String "5" rest => decode_inner rest next_instr (CALLDATALOAD :: sofar)
| String "6" rest => decode_inner rest next_instr (CALLDATASIZE :: sofar)
| String "7" rest => decode_inner rest next_instr (CALLDATACOPY :: sofar)
| String "8" rest => decode_inner rest next_instr (CODESIZE :: sofar)
| String "9" rest => decode_inner rest next_instr (CODECOPY :: sofar)
| String "a" rest => decode_inner rest next_instr (GASPRICE :: sofar)
| String "b" rest => decode_inner rest next_instr (EXTCODESIZE :: sofar)
| String "c" rest => decode_inner rest next_instr (EXTCODECOPY :: sofar)
| String "d" rest => decode_inner rest next_instr (UNKNOWN "3d" :: sofar)
| String "e" rest => decode_inner rest next_instr (UNKNOWN "3e" :: sofar)
| String "f" rest => decode_inner rest next_instr (UNKNOWN "3f" :: sofar)
| _ => decode_failure "3?"
end
| read_4 =>
match str with
| String "0" rest => decode_inner rest next_instr (BLOCKHASH :: sofar)
| String "1" rest => decode_inner rest next_instr (COINBASE :: sofar)
| String "2" rest => decode_inner rest next_instr (TIMESTAMP :: sofar)
| String "3" rest => decode_inner rest next_instr (NUMBER :: sofar)
| String "4" rest => decode_inner rest next_instr (DIFFICULTY :: sofar)
| String "5" rest => decode_inner rest next_instr (GASLIMIT :: sofar)
| String "6" rest => decode_inner rest next_instr (UNKNOWN "46" :: sofar)
| String "7" rest => decode_inner rest next_instr (UNKNOWN "47" :: sofar)
| String "8" rest => decode_inner rest next_instr (UNKNOWN "48" :: sofar)
| String "9" rest => decode_inner rest next_instr (UNKNOWN "49" :: sofar)
| String "a" rest => decode_inner rest next_instr (UNKNOWN "4a" :: sofar)
| String "b" rest => decode_inner rest next_instr (UNKNOWN "4b" :: sofar)
| String "c" rest => decode_inner rest next_instr (UNKNOWN "4c" :: sofar)
| String "d" rest => decode_inner rest next_instr (UNKNOWN "4d" :: sofar)
| String "e" rest => decode_inner rest next_instr (UNKNOWN "4e" :: sofar)
| String "f" rest => decode_inner rest next_instr (UNKNOWN "4f" :: sofar)
| _ => decode_failure "4?"
end
| read_5 =>
match str with
| String "0" rest => decode_inner rest next_instr (POP :: sofar)
| String "1" rest => decode_inner rest next_instr (MLOAD :: sofar)
| String "2" rest => decode_inner rest next_instr (MSTORE :: sofar)
| String "3" rest => decode_inner rest next_instr (MSTORE8 :: sofar)
| String "4" rest => decode_inner rest next_instr (SLOAD :: sofar)
| String "5" rest => decode_inner rest next_instr (SSTORE :: sofar)
| String "6" rest => decode_inner rest next_instr (JUMP :: sofar)
| String "7" rest => decode_inner rest next_instr (JUMPI :: sofar)
| String "8" rest => decode_inner rest next_instr (PC :: sofar)
| String "9" rest => decode_inner rest next_instr (MSIZE :: sofar)
| String "a" rest => decode_inner rest next_instr (GAS :: sofar)
| String "b" rest => decode_inner rest next_instr (JUMPDEST :: sofar)
| String "c" rest => decode_inner rest next_instr (UNKNOWN "5c" :: sofar)
| String "d" rest => decode_inner rest next_instr (UNKNOWN "5d" :: sofar)
| String "e" rest => decode_inner rest next_instr (UNKNOWN "5e" :: sofar)
| String "f" rest => decode_inner rest next_instr (UNKNOWN "5f" :: sofar)
| _ => decode_failure "5?"
end
| read_6 =>
match str with
| String "0" rest => decode_inner rest (read_hex 2 rev0x) sofar
| String "1" rest => decode_inner rest (read_hex 4 rev0x) sofar
| String "2" rest => decode_inner rest (read_hex 6 rev0x) sofar
| String "3" rest => decode_inner rest (read_hex 8 rev0x) sofar
| String "4" rest => decode_inner rest (read_hex 10 rev0x) sofar
| String "5" rest => decode_inner rest (read_hex 12 rev0x) sofar
| String "6" rest => decode_inner rest (read_hex 14 rev0x) sofar
| String "7" rest => decode_inner rest (read_hex 16 rev0x) sofar
| String "8" rest => decode_inner rest (read_hex 18 rev0x) sofar
| String "9" rest => decode_inner rest (read_hex 20 rev0x) sofar
| String "a" rest => decode_inner rest (read_hex 22 rev0x) sofar
| String "b" rest => decode_inner rest (read_hex 24 rev0x) sofar
| String "c" rest => decode_inner rest (read_hex 26 rev0x) sofar
| String "d" rest => decode_inner rest (read_hex 28 rev0x) sofar
| String "e" rest => decode_inner rest (read_hex 30 rev0x) sofar
| String "f" rest => decode_inner rest (read_hex 32 rev0x) sofar
| _ => decode_failure "6?"
end
| read_7 =>
match str with
| String "0" rest => decode_inner rest (read_hex 34 rev0x) sofar
| String "1" rest => decode_inner rest (read_hex 36 rev0x) sofar
| String "2" rest => decode_inner rest (read_hex 38 rev0x) sofar
| String "3" rest => decode_inner rest (read_hex 40 rev0x) sofar
| String "4" rest => decode_inner rest (read_hex 42 rev0x) sofar
| String "5" rest => decode_inner rest (read_hex 44 rev0x) sofar
| String "6" rest => decode_inner rest (read_hex 46 rev0x) sofar
| String "7" rest => decode_inner rest (read_hex 48 rev0x) sofar
| String "8" rest => decode_inner rest (read_hex 50 rev0x) sofar
| String "9" rest => decode_inner rest (read_hex 52 rev0x) sofar
| String "a" rest => decode_inner rest (read_hex 54 rev0x) sofar
| String "b" rest => decode_inner rest (read_hex 56 rev0x) sofar
| String "c" rest => decode_inner rest (read_hex 58 rev0x) sofar
| String "d" rest => decode_inner rest (read_hex 60 rev0x) sofar
| String "e" rest => decode_inner rest (read_hex 62 rev0x) sofar
| String "f" rest => decode_inner rest (read_hex 64 rev0x) sofar
| _ => decode_failure "7?"
end
| read_8 =>
match str with
| String "0" rest => decode_inner rest next_instr (DUP1 :: sofar)
| String "1" rest => decode_inner rest next_instr (DUP2 :: sofar)
| String "2" rest => decode_inner rest next_instr (DUP3 :: sofar)
| String "3" rest => decode_inner rest next_instr (DUP4 :: sofar)
| String "4" rest => decode_inner rest next_instr (DUP5 :: sofar)
| String "5" rest => decode_inner rest next_instr (DUP6 :: sofar)
| String "6" rest => decode_inner rest next_instr (DUP7 :: sofar)
| String "7" rest => decode_inner rest next_instr (DUP8 :: sofar)
| String "8" rest => decode_inner rest next_instr (DUP9 :: sofar)
| String "9" rest => decode_inner rest next_instr (DUP10 :: sofar)
| String "a" rest => decode_inner rest next_instr (DUP11 :: sofar)
| String "b" rest => decode_inner rest next_instr (DUP12 :: sofar)
| String "c" rest => decode_inner rest next_instr (DUP13 :: sofar)
| String "d" rest => decode_inner rest next_instr (DUP14 :: sofar)
| String "e" rest => decode_inner rest next_instr (DUP15 :: sofar)
| String "f" rest => decode_inner rest next_instr (DUP16 :: sofar)
| _ => decode_failure "8?"
end
| read_9 =>
match str with
| String "0" rest => decode_inner rest next_instr (SWAP1 :: sofar)
| String "1" rest => decode_inner rest next_instr (SWAP2 :: sofar)
| String "2" rest => decode_inner rest next_instr (SWAP3 :: sofar)
| String "3" rest => decode_inner rest next_instr (SWAP4 :: sofar)
| String "4" rest => decode_inner rest next_instr (SWAP5 :: sofar)
| String "5" rest => decode_inner rest next_instr (SWAP6 :: sofar)
| String "6" rest => decode_inner rest next_instr (SWAP7 :: sofar)
| String "7" rest => decode_inner rest next_instr (SWAP8 :: sofar)
| String "8" rest => decode_inner rest next_instr (SWAP9 :: sofar)
| String "9" rest => decode_inner rest next_instr (SWAP10 :: sofar)
| String "a" rest => decode_inner rest next_instr (SWAP11 :: sofar)
| String "b" rest => decode_inner rest next_instr (SWAP12 :: sofar)
| String "c" rest => decode_inner rest next_instr (SWAP13 :: sofar)
| String "d" rest => decode_inner rest next_instr (SWAP14 :: sofar)
| String "e" rest => decode_inner rest next_instr (SWAP15 :: sofar)
| String "f" rest => decode_inner rest next_instr (SWAP16 :: sofar)
| _ => decode_failure "9?"
end
| read_a =>
match str with
| String "0" rest => decode_inner rest next_instr (LOG0 :: sofar)
| String "1" rest => decode_inner rest next_instr (LOG1 :: sofar)
| String "2" rest => decode_inner rest next_instr (LOG2 :: sofar)
| String "3" rest => decode_inner rest next_instr (LOG3 :: sofar)
| String "4" rest => decode_inner rest next_instr (LOG4 :: sofar)
| String "5" rest => decode_inner rest next_instr (UNKNOWN "a5" :: sofar)
| String "6" rest => decode_inner rest next_instr (UNKNOWN "a6" :: sofar)
| String "7" rest => decode_inner rest next_instr (UNKNOWN "a7" :: sofar)
| String "8" rest => decode_inner rest next_instr (UNKNOWN "a8" :: sofar)
| String "9" rest => decode_inner rest next_instr (UNKNOWN "a9" :: sofar)
| String "a" rest => decode_inner rest next_instr (UNKNOWN "aa" :: sofar)
| String "b" rest => decode_inner rest next_instr (UNKNOWN "ab" :: sofar)
| String "c" rest => decode_inner rest next_instr (UNKNOWN "ac" :: sofar)
| String "d" rest => decode_inner rest next_instr (UNKNOWN "ad" :: sofar)
| String "e" rest => decode_inner rest next_instr (UNKNOWN "ae" :: sofar)
| String "f" rest => decode_inner rest next_instr (UNKNOWN "af" :: sofar)
| _ => decode_failure "a?"
end
| read_b =>
match str with
| String _ rest => decode_inner rest next_instr (UNKNOWN "b?" :: sofar)
| EmptyString => decode_failure "b?"
end
| read_c =>
match str with
| String _ rest => decode_inner rest next_instr (UNKNOWN "c?" :: sofar)
| EmptyString => decode_failure "c?"
end
| read_d =>
match str with
| String _ rest => decode_inner rest next_instr (UNKNOWN "d?" :: sofar)
| EmptyString => decode_failure "d?"
end
| read_e =>
match str with
| String "0" rest => decode_inner rest next_instr (UNKNOWN "e0" :: sofar)
| String "9" rest => decode_inner rest next_instr (UNKNOWN "e9" :: sofar)
| String _ rest => decode_inner rest next_instr (UNKNOWN "e?" :: sofar)
| EmptyString => decode_failure "e?"
end
| read_f =>
match str with
| String "0" rest => decode_inner rest next_instr (CREATE :: sofar)
| String "1" rest => decode_inner rest next_instr (CALL :: sofar)
| String "2" rest => decode_inner rest next_instr (CALLCODE :: sofar)
| String "3" rest => decode_inner rest next_instr (RETURN :: sofar)
| String "4" rest => decode_inner rest next_instr (UNKNOWN "f4" :: sofar)
| String "5" rest => decode_inner rest next_instr (UNKNOWN "f5" :: sofar)
| String "6" rest => decode_inner rest next_instr (UNKNOWN "f6" :: sofar)
| String "7" rest => decode_inner rest next_instr (UNKNOWN "f7" :: sofar)
| String "8" rest => decode_inner rest next_instr (UNKNOWN "f8" :: sofar)
| String "9" rest => decode_inner rest next_instr (UNKNOWN "f9" :: sofar)
| String "a" rest => decode_inner rest next_instr (UNKNOWN "fa" :: sofar)
| String "b" rest => decode_inner rest next_instr (UNKNOWN "fb" :: sofar)
| String "c" rest => decode_inner rest next_instr (UNKNOWN "fc" :: sofar)
| String "d" rest => decode_inner rest next_instr (UNKNOWN "fd" :: sofar)
| String "e" rest => decode_inner rest next_instr (UNKNOWN "fe" :: sofar)
| String "f" rest => decode_inner rest next_instr (SUICIDE :: sofar)
| _ => decode_failure "f?"
end
| read_hex O acc => decode_failure "should not happen"
| read_hex (S O) acc =>
match str with
| EmptyString => decode_success (List.rev sofar) (*decode_failure "end_of_string reading hex" *)
| String c rest =>
decode_inner rest next_instr (PUSH_N (rev_string (c :: acc)) :: sofar)
end
| read_hex (S pre) acc =>
match str with
| EmptyString => decode_success (List.rev sofar) (* decode_failure "end_of_string reading hex" *)
| String c rest =>
decode_inner rest (read_hex pre (c :: acc)) sofar
end
end
.
(* Question: Is there a need to decode further after a failure *)
Definition decode (code : string) : decode_result :=
decode_inner code first_zero nil.
Open Scope string_scope.
Open Scope char_scope.
Close Scope nat_scope.
Open Scope N_scope.
Definition read_hex_char (c : ascii) : option N :=
match c with
| "0" => Some 0
| "1" => Some 1
| "2" => Some 2
| "3" => Some 3
| "4" => Some 4
| "5" => Some 5
| "6" => Some 6
| "7" => Some 7
| "8" => Some 8
| "9" => Some 9
| "a" => Some 10
| "b" => Some 11
| "c" => Some 12
| "d" => Some 13
| "e" => Some 14
| "f" => Some 15
| _ => None
end.
Fixpoint read_str_hex (carry: N) (str : string) : N :=
match str with
| EmptyString => carry
| String c rest =>
match read_hex_char c with
| None => 0
| Some num => read_str_hex (carry * 16 + num) rest
end
end.
Definition literal_to_nat (str : string) : N :=
match str with
| String "0" (String "x" rest) => read_str_hex 0 rest
| _ => 0
end.
End Lang.
Module AbstractEVM.
Definition a_pc := N. (* program counter *)
Definition a_hex := string.
(* a_word stands for abstract world *)
Inductive a_word :=
| Acaller : a_word
| Aorigin : a_word
| Agas_price : a_word
| Ablock_number : a_word
| Acoinbase : a_word
| Ablockhash : a_word
| Atime : a_word
| Adatasize : a_word
| Avalue : a_word
| Aaddress : a_word
| Abalance : a_word -> a_word
| Adifficulty : a_word
| Agaslimit : a_word
| Aextcodesize : a_word -> a_word
| Aimm_nat : N -> a_word
| Aunknown : string -> a_word (* whatever that might change during execution *)
| Ais_zero : a_word -> a_word
| Azero : a_word
| Asub : a_word -> a_word -> a_word
| Aadd : a_word -> a_word -> a_word
| Aand : a_word -> a_word -> a_word
| Abyte : a_word -> a_word -> a_word
| Aor : a_word -> a_word -> a_word
| Axor : a_word -> a_word -> a_word
| Aexp : a_word -> a_word -> a_word
| Adiv : a_word -> a_word -> a_word
| Amul : a_word -> a_word -> a_word
| Agt : a_word -> a_word -> a_word
| Asdiv : a_word -> a_word -> a_word
| Amod : a_word -> a_word -> a_word
| Asmod : a_word -> a_word -> a_word
| Asignextend : a_word -> a_word -> a_word
| Anot : a_word -> a_word
| Asha3 : a_memory -> a_word
| Alt : a_word -> a_word -> a_word
| Aslt : a_word -> a_word -> a_word
| Aeq : a_word -> a_word -> a_word
| Aget32 : a_word -> a_memory -> a_word (* Aget32 addr mem *)
| Aget_storage : a_word -> a_storage -> a_word
with a_memory :=
| Aempty : a_memory
| Aput32 : a_word -> a_word -> a_memory -> a_memory
(* Aput32 addr val orig represents the result of a one-word write. *)
| Aput1 : a_word -> a_word -> a_memory -> a_memory
(* Aput1 addr val orig represents the result of a one-byte write.
* Actually (val mod 256) is written.
*)
| Amemwrite : a_word -> a_word -> a_memory -> a_memory -> a_memory
(* Amemwrite start_addr len source mem represents the result of memwrite. source [0..len - 1] is copied to mem[start_addr.. start_addr + len - 1]. *)
| Adata : a_memory
(* Adata represents the input data. *)
| Adrop : a_word -> a_memory -> a_memory
(* Adrop n mem is the result of dropping the first n bytes and shifting forward. *)
| Atake : a_word -> a_word -> a_memory -> a_memory
(* Atake n size mem takes size bytes from position n *)
| Acodecopy : a_word -> a_word -> a_word -> a_memory -> a_memory
(* Acodecopy base_in_memory base_in_code len orig *)
| Amem_imm : string -> a_memory
| Aconcat : a_word -> a_word -> a_memory
(* Aconcat w0 w1 appears a lot in Solidity compilation. *)
with a_storage :=
| Ainitial_storage : a_storage
| Aput_storage : a_word -> a_word -> a_storage -> a_storage
.
Fixpoint simplify_above (addr : N) (mem : a_memory) :=
match mem with
| Aput32 (Aimm_nat w) val orig =>
if N.leb addr w then simplify_above addr orig
else
Aput32 (Aimm_nat w) val (simplify_above addr orig)
| _ => mem
end.
Fixpoint simplify_below (addr : N) (mem : a_memory) :=
match mem with
| Aput32 (Aimm_nat w) val orig =>
if N.leb (w + 32) addr then simplify_below addr orig
else
Aput32 (Aimm_nat w) val (simplify_below addr orig)
| _ => mem
end.
Definition Atake' start size mem :=
match start, size, mem with
| Aimm_nat 0, Aimm_nat 64, (Aput32 (Aimm_nat 32) y (Aput32 (Aimm_nat 0) x _)) =>
Aconcat x y
| Aimm_nat 0, Aimm_nat 64, (Aput32 (Aimm_nat 0) x (Aput32 (Aimm_nat 32) y _)) =>
Aconcat x y
| Aimm_nat st, Aimm_nat si, _ =>
Atake start size
(simplify_below st (simplify_above (st + si) mem))
| _, _, _ =>
Atake start size mem
end.
Fixpoint Aget_storage' (index : a_word) (str : a_storage) : a_word :=
match index, str with
| _, Ainitial_storage => Aget_storage index str
| Aimm_nat i, Aput_storage (Aimm_nat j) x orig =>
if N.eqb i j then x else
Aget_storage' (Aimm_nat i) orig
| _, _ => Aget_storage index str
end.
Definition Aadd' (a : a_word) (b : a_word) :=
match a, b with
| Aimm_nat m, Aimm_nat n => Aimm_nat (m + n)
| _, Azero => a
| Azero, _ => b
| _, _ => Aadd a b
end.
Definition Asub' (a : a_word) (b : a_word) :=
match a, b with
| Aimm_nat m, Aimm_nat n =>
if (N.leb n m) then Aimm_nat (m - n) else Asub a b
| _, _ => Asub a b
end.
Definition Aexp' (a : a_word) (b : a_word) :=
match a, b with
| Aimm_nat m, Aimm_nat n => Aimm_nat (N.pow m n)
| _, _ => Aexp a b
end.
Close Scope nat_scope.
Fixpoint Aget32' (addr : a_word) (mem : a_memory) : a_word :=
match addr, mem with
| Aimm_nat n, Aput32 (Aimm_nat w) v orig =>
if orb (N.leb 32 (n - w)) (N.leb 32 (w - n)) then
Aget32' addr orig
else if (N.eqb n w) then v else Aget32 addr mem
| _, Aempty => Azero
| Aimm_nat n, Acodecopy (Aimm_nat mem_start) _ (Aimm_nat size) orig =>
if N.leb (n + 32) mem_start then
Aget32' addr orig
else if N.leb (mem_start + size) n then
Aget32' addr orig
else
Aget32 addr mem
| _, _ => Aget32 addr mem
end.
Fixpoint forget32 addr orig :=
match addr, orig with
| Aimm_nat w, Aput32 (Aimm_nat p) v pre =>
if (N.eqb w p) then
forget32 addr pre
else
Aput32 (Aimm_nat p) v (forget32 addr pre)
| _, _ => orig
end.
Fixpoint Aput32' (addr : a_word) (val : a_word) (orig : a_memory) : a_memory :=
Aput32 addr val (forget32 addr orig).
Definition Aimm (hex : a_hex) : a_word :=
Aimm_nat (Lang.literal_to_nat hex).
Definition a_stack := list a_word.
Inductive a_prop :=
| is_zero : a_word -> a_prop
| is_not_zero : a_word -> a_prop
.
Definition a_operation := a_stack -> a_memory ->
list (list a_prop * option (a_stack * a_memory)).
(* [ (condition1, None = failure ); (condition2, Some (post_stack, post_memory)) ] *)
Require Import List.
Open Scope N_scope.
Definition simple_result {T} (x : T) := (@nil a_prop, x) :: nil.
Definition a_push_x (data : a_word) : a_operation :=
fun s mem => simple_result (Some (data :: s, mem)).
Definition a_pop : a_operation :=
fun s mem =>
match s with
| nil => simple_result None
| hd :: tl => simple_result (Some (tl, mem))
end.
Definition a_mstore : a_operation :=
fun s mem =>
match s with
| nil => simple_result None
| _ :: nil => simple_result None
| a :: b :: l => simple_result (Some (l, Aput32' a b mem))
end.
(* TODO: use Aput1' instead of Aput1. See Aput32'. *)
Definition a_mstore8 : a_operation :=
fun s mem =>
match s with
| nil => simple_result None
| _ :: nil => simple_result None
| a :: b :: l => simple_result (Some (l, Aput1 a b mem))
end.
Definition a_mload : a_operation :=
fun s mem =>
simple_result
match s with
| nil => None
| addr :: l =>
Some (Aget32' addr mem :: l, mem)
end.
Definition a_gas : a_operation :=
fun s mem =>
simple_result (Some (Aunknown "remaining gas" :: s, mem)).
Definition a_msize : a_operation :=
fun s mem =>
simple_result (Some (Aunknown "memory size" :: s, mem)).
Definition a_calldatasize : a_operation :=
fun s mem =>
simple_result (Some (Adatasize :: s, mem)).
Definition a_callvalue : a_operation :=
fun s mem =>
simple_result (Some (Avalue :: s, mem)).
Definition a_address : a_operation :=
fun s mem =>
simple_result (Some (Aaddress :: s, mem)).
Definition a_one_one_op (f : a_word -> a_word) : a_operation :=
fun s mem =>
simple_result
match s with
| nil => None
| a :: l => Some (f a :: l, mem)
end.
Definition a_balance : a_operation := a_one_one_op Abalance.
Definition a_calldatacopy : a_operation :=
fun s mem =>
simple_result
match s with
| m0 :: m1 :: m2 :: l =>
Some (l, Amemwrite m0 m2 (Adrop m1 Adata) mem)
| _ => None
end.
Definition a_codecopy : a_operation :=
fun s mem =>
simple_result
match s with
| m0 :: m1 :: m2 :: l =>
Some (l, Acodecopy m0 m1 m2 mem)
| _ => None
end.
Definition Ais_zero' (orig : a_word) : a_word :=
match orig with
| Atime => Aimm_nat 0
| Azero => Aimm_nat 1
| Aimm_nat x =>
Aimm_nat (if Neqb 0 x then 1 else 0)
| _ => Ais_zero orig
end.
Definition a_iszero : a_operation :=
fun s mem =>
simple_result
match s with
| nil => None
| h :: tl =>
Some (Ais_zero' h :: tl, mem)
end.
Definition a_two_two_op (f : a_word -> a_word -> (a_word * a_word)) : a_operation :=
fun s mem =>
simple_result
match s with
| a :: b :: l =>
Some (fst (f a b) :: snd (f a b) :: l, mem)
| _ => None
end.
Definition a_two_one_op (f : a_word -> a_word -> a_word) : a_operation :=
fun s mem =>
simple_result
match s with
| nil => None
| _ :: nil => None
| a :: b :: l => Some ((f a b) :: l, mem)
end.
Definition a_three_one_op (f : a_word -> a_word -> a_word -> a_word)
: a_operation :=
fun s mem =>
simple_result
match s with
| nil => None
| _ :: nil => None
| _ :: _ :: nil => None
| a :: b :: c :: l => Some ((f a b c) :: l, mem)
end.
Definition a_exp_op : a_operation := a_two_one_op Aexp'.
Definition a_and_op : a_operation := a_two_one_op Aand.
Definition a_or_op : a_operation := a_two_one_op Aor.
Definition a_byte_op : a_operation := a_two_one_op Abyte.
Definition a_xor_op : a_operation := a_two_one_op Axor.
Definition a_sload storage : a_operation :=
a_one_one_op (fun addr => Aget_storage' addr storage).
Definition a_calldataload : a_operation :=
a_one_one_op (fun n => Aget32' n Adata).
Definition a_div_op := a_two_one_op Adiv.
Definition a_mul_op := a_two_one_op Amul.
Definition a_add_op := a_two_one_op Aadd'.
Definition a_sdiv_op := a_two_one_op Asdiv.
Definition a_mod_op := a_two_one_op Amod.
Definition a_addmod_op := a_three_one_op
(fun a b c =>
Amod (Aadd' a b) c).
Definition a_mulmod_op := a_three_one_op
(fun a b c =>
Amod (Amul a b) c).
Definition a_smod_op := a_two_one_op Asmod.
Definition a_signextend_op := a_two_one_op Asignextend.
Definition a_dup1 : a_operation :=
fun s mem =>
simple_result
match s with
| a :: l => Some (a :: a :: l, mem)
| _ => None (* really? *)
end.
Definition a_dup2 : a_operation :=
fun s mem =>
simple_result
match s with
| a :: b :: l => Some (b :: a :: b :: l, mem)
| _ => None
end.
Definition a_dup3 : a_operation :=
fun s mem =>
simple_result
match s with
| a :: b :: c :: l => Some (c :: a :: b :: c :: l, mem)
| _ => None
end.
Definition a_dup4 : a_operation :=
fun s mem =>
simple_result
match s with
| a :: b :: c :: d :: l => Some (d :: a :: b :: c :: d :: l, mem)
| _ => None
end.
Fixpoint nth_opt {A} (n : nat) (lst : list A) :=
match n, lst with
| S O, hd :: _ => Some hd
| S pre, _ :: tl => nth_opt pre tl
| _, _ => None
end.
Definition stack_dup n (s : a_stack) :=
match nth_opt n s with
| Some elm => Some (elm :: s)
| None => None
end.
Definition a_dup_n (n : nat) : a_operation :=
fun s mem =>
simple_result
match stack_dup n s with
| Some new_s => Some (new_s, mem)
| None => None
end.
Definition a_eq_op : a_operation := a_two_one_op
(fun a b => Aeq a b).
Definition a_gt_op : a_operation := a_two_one_op
(fun a b => Agt a b).
Definition a_slt_op : a_operation := a_two_one_op Aslt.
Definition a_sgt_op : a_operation :=
a_two_one_op (fun a b => Aslt b a).
Definition a_not_op : a_operation := a_one_one_op Anot.
Definition a_extcodesize : a_operation := a_one_one_op Aextcodesize.
Definition a_sha3 : a_operation :=
fun s mem =>
simple_result
match s with
| st :: size :: rest =>
Some (Asha3 (Atake' st size mem) :: rest, mem)
| _ => None
end.
Definition a_lt_op : a_operation := a_two_one_op
(fun a b => Alt a b).
Definition a_sub_op : a_operation := a_two_one_op Asub'.
Definition a_swap1 : a_operation := a_two_two_op (fun a b => (b, a)).
Definition a_swap2 : a_operation :=
fun s mem =>
simple_result