-
Notifications
You must be signed in to change notification settings - Fork 311
/
interpreter.cairo
1104 lines (1045 loc) · 39.9 KB
/
interpreter.cairo
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
// SPDX-License-Identifier: MIT
%lang starknet
// Starkware dependencies
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.bool import FALSE, TRUE
from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin
from starkware.cairo.common.math_cmp import is_not_zero, is_nn, is_le_felt
from starkware.cairo.common.math import split_felt
from starkware.cairo.common.default_dict import default_dict_new
from starkware.cairo.common.dict import DictAccess
from starkware.cairo.lang.compiler.lib.registers import get_fp_and_pc, get_ap
from starkware.cairo.common.uint256 import Uint256, uint256_le
// Internal dependencies
from kakarot.account import Account
from kakarot.constants import opcodes_label, Constants
from kakarot.errors import Errors
from kakarot.evm import EVM
from kakarot.instructions.block_information import BlockInformation
from kakarot.instructions.duplication_operations import DuplicationOperations
from kakarot.instructions.environmental_information import EnvironmentalInformation
from kakarot.instructions.exchange_operations import ExchangeOperations
from kakarot.instructions.logging_operations import LoggingOperations
from kakarot.instructions.memory_operations import MemoryOperations
from kakarot.instructions.push_operations import PushOperations
from kakarot.instructions.sha3 import Sha3
from kakarot.instructions.stop_and_math_operations import StopAndMathOperations
from kakarot.instructions.system_operations import CallHelper, CreateHelper, SystemOperations
from kakarot.memory import Memory
from kakarot.model import model
from kakarot.precompiles.precompiles import Precompiles
from kakarot.precompiles.precompiles_helpers import PrecompilesHelpers
from kakarot.stack import Stack
from kakarot.state import State
from kakarot.gas import Gas
from utils.utils import Helpers
from utils.array import count_not_zero
from utils.uint256 import uint256_sub, uint256_add
from utils.maths import unsigned_div_rem
// @title EVM instructions processing.
// @notice This file contains functions related to the processing of EVM instructions.
namespace Interpreter {
// @notice Decode the current opcode and execute associated function.
// @dev The function uses an internal jump table to execute the corresponding opcode
// @param evm The pointer to the execution context.
// @return EVM The pointer to the updated execution context.
func exec_opcode{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
stack: model.Stack*,
memory: model.Memory*,
state: model.State*,
}(evm: model.EVM*) -> model.EVM* {
alloc_locals;
local opcode_number;
local opcode: model.Opcode*;
let pc = evm.program_counter;
let is_pc_ge_code_len = is_nn(pc - evm.message.bytecode_len);
if (is_pc_ge_code_len != FALSE) {
let is_precompile = PrecompilesHelpers.is_precompile(evm.message.code_address.evm);
if (is_precompile != FALSE) {
let parent_context = evm.message.parent;
let is_parent_zero = Helpers.is_zero(cast(parent_context, felt));
if (is_parent_zero != FALSE) {
// Case A: The precompile is called straight from an EOA
tempvar caller_code_address = evm.message.caller;
} else {
// Case B: The precompile is called from a contract
tempvar caller_code_address = parent_context.evm.message.code_address.evm;
}
tempvar caller_address = evm.message.caller;
let (
output_len, output, gas_used, precompile_reverted
) = Precompiles.exec_precompile(
evm.message.code_address.evm,
evm.message.calldata_len,
evm.message.calldata,
caller_code_address,
caller_address,
);
let evm = EVM.charge_gas(evm, gas_used);
let evm_reverted = is_not_zero(evm.reverted);
let success = (1 - precompile_reverted) * (1 - evm_reverted);
let evm = EVM.stop(evm, output_len, output, 1 - success);
let is_cairo_precompile_called = PrecompilesHelpers.is_kakarot_precompile(
evm.message.code_address.evm
);
tempvar message = new model.Message(
bytecode=evm.message.bytecode,
bytecode_len=evm.message.bytecode_len,
valid_jumpdests_start=evm.message.valid_jumpdests_start,
valid_jumpdests=evm.message.valid_jumpdests,
calldata=evm.message.calldata,
calldata_len=evm.message.calldata_len,
value=evm.message.value,
caller=evm.message.caller,
parent=evm.message.parent,
address=evm.message.address,
code_address=evm.message.code_address,
read_only=evm.message.read_only,
is_create=evm.message.is_create,
depth=evm.message.depth,
env=evm.message.env,
cairo_precompile_called=is_cairo_precompile_called,
);
tempvar evm = new model.EVM(
message=message,
return_data_len=evm.return_data_len,
return_data=evm.return_data,
program_counter=evm.program_counter,
stopped=evm.stopped,
gas_left=evm.gas_left,
gas_refund=evm.gas_refund,
reverted=evm.reverted,
);
return evm;
} else {
let (return_data: felt*) = alloc();
let evm = EVM.stop(evm, 0, return_data, FALSE);
return evm;
}
}
assert opcode_number = [evm.message.bytecode + pc];
// Get the corresponding opcode data
// To cast the codeoffset opcodes_label to a model.Opcode*, we need to use it to offset
// the current pc. We get the pc from the `get_fp_and_pc` util and assign a codeoffset (pc_label) to it.
// In short, this boils down to: opcode = pc + offset - pc = offset
let (_, cairo_pc) = get_fp_and_pc();
pc_label:
assert opcode = cast(
cairo_pc + (opcodes_label - pc_label) + opcode_number * model.Opcode.SIZE, model.Opcode*
);
// Check stack over/under flow
let stack_underflow = is_nn(opcode.stack_size_min - 1 - stack.size);
if (stack_underflow != 0) {
let (revert_reason_len, revert_reason) = Errors.stackUnderflow();
let evm = EVM.stop(evm, revert_reason_len, revert_reason, Errors.EXCEPTIONAL_HALT);
return evm;
}
let stack_overflow = is_nn(
stack.size + opcode.stack_size_diff - (Constants.STACK_MAX_DEPTH + 1)
);
if (stack_overflow != 0) {
let (revert_reason_len, revert_reason) = Errors.stackOverflow();
let evm = EVM.stop(evm, revert_reason_len, revert_reason, Errors.EXCEPTIONAL_HALT);
return evm;
}
// Update static gas
let evm = EVM.charge_gas(evm, opcode.gas);
if (evm.reverted != FALSE) {
return evm;
}
// Compute the corresponding offset in the jump table:
// count 1 for "next line" and 4 steps per opcode: call, opcode, jmp, end
tempvar offset = 1 + 4 * opcode_number;
// Prepare arguments
[ap] = syscall_ptr, ap++;
[ap] = pedersen_ptr, ap++;
[ap] = range_check_ptr, ap++;
[ap] = bitwise_ptr, ap++;
[ap] = stack, ap++;
[ap] = memory, ap++;
[ap] = state, ap++;
[ap] = evm, ap++;
// call opcode
jmp rel offset;
call StopAndMathOperations.exec_stop; // 0x0
jmp end;
call StopAndMathOperations.exec_math_operation; // 0x1
jmp end;
call StopAndMathOperations.exec_math_operation; // 0x2
jmp end;
call StopAndMathOperations.exec_math_operation; // 0x3
jmp end;
call StopAndMathOperations.exec_math_operation; // 0x4
jmp end;
call StopAndMathOperations.exec_math_operation; // 0x5
jmp end;
call StopAndMathOperations.exec_math_operation; // 0x6
jmp end;
call StopAndMathOperations.exec_math_operation; // 0x7
jmp end;
call StopAndMathOperations.exec_math_operation; // 0x8
jmp end;
call StopAndMathOperations.exec_math_operation; // 0x9
jmp end;
call StopAndMathOperations.exec_math_operation; // 0xa
jmp end;
call StopAndMathOperations.exec_math_operation; // 0xb
jmp end;
call unknown_opcode; // 0xc
jmp end;
call unknown_opcode; // 0xd
jmp end;
call unknown_opcode; // 0xe
jmp end;
call unknown_opcode; // 0xf
jmp end;
call StopAndMathOperations.exec_math_operation; // 0x10
jmp end;
call StopAndMathOperations.exec_math_operation; // 0x11
jmp end;
call StopAndMathOperations.exec_math_operation; // 0x12
jmp end;
call StopAndMathOperations.exec_math_operation; // 0x13
jmp end;
call StopAndMathOperations.exec_math_operation; // 0x14
jmp end;
call StopAndMathOperations.exec_math_operation; // 0x15
jmp end;
call StopAndMathOperations.exec_math_operation; // 0x16
jmp end;
call StopAndMathOperations.exec_math_operation; // 0x17
jmp end;
call StopAndMathOperations.exec_math_operation; // 0x18
jmp end;
call StopAndMathOperations.exec_math_operation; // 0x19
jmp end;
call StopAndMathOperations.exec_math_operation; // 0x1a
jmp end;
call StopAndMathOperations.exec_math_operation; // 0x1b
jmp end;
call StopAndMathOperations.exec_math_operation; // 0x1c
jmp end;
call StopAndMathOperations.exec_math_operation; // 0x1d
jmp end;
call unknown_opcode; // 0x1e
jmp end;
call unknown_opcode; // 0x1f
jmp end;
call Sha3.exec_sha3; // 0x20
jmp end;
call unknown_opcode; // 0x21
jmp end;
call unknown_opcode; // 0x22
jmp end;
call unknown_opcode; // 0x23
jmp end;
call unknown_opcode; // 0x24
jmp end;
call unknown_opcode; // 0x25
jmp end;
call unknown_opcode; // 0x26
jmp end;
call unknown_opcode; // 0x27
jmp end;
call unknown_opcode; // 0x28
jmp end;
call unknown_opcode; // 0x29
jmp end;
call unknown_opcode; // 0x2a
jmp end;
call unknown_opcode; // 0x2b
jmp end;
call unknown_opcode; // 0x2c
jmp end;
call unknown_opcode; // 0x2d
jmp end;
call unknown_opcode; // 0x2e
jmp end;
call unknown_opcode; // 0x2f
jmp end;
call EnvironmentalInformation.exec_address; // 0x30
jmp end;
call EnvironmentalInformation.exec_balance; // 0x31
jmp end;
call EnvironmentalInformation.exec_origin; // 0x32
jmp end;
call EnvironmentalInformation.exec_caller; // 0x33
jmp end;
call EnvironmentalInformation.exec_callvalue; // 0x34
jmp end;
call EnvironmentalInformation.exec_calldataload; // 0x35
jmp end;
call EnvironmentalInformation.exec_calldatasize; // 0x36
jmp end;
call EnvironmentalInformation.exec_copy; // 0x37
jmp end;
call EnvironmentalInformation.exec_codesize; // 0x38
jmp end;
call EnvironmentalInformation.exec_copy; // 0x39
jmp end;
call EnvironmentalInformation.exec_gasprice; // 0x3a
jmp end;
call EnvironmentalInformation.exec_extcodesize; // 0x3b
jmp end;
call EnvironmentalInformation.exec_extcodecopy; // 0x3c
jmp end;
call EnvironmentalInformation.exec_returndatasize; // 0x3d
jmp end;
call EnvironmentalInformation.exec_returndatacopy; // 0x3e
jmp end;
call EnvironmentalInformation.exec_extcodehash; // 0x3f
jmp end;
call BlockInformation.exec_block_information; // 0x40
jmp end;
call BlockInformation.exec_block_information; // 0x41
jmp end;
call BlockInformation.exec_block_information; // 0x42
jmp end;
call BlockInformation.exec_block_information; // 0x43
jmp end;
call BlockInformation.exec_block_information; // 0x44
jmp end;
call BlockInformation.exec_block_information; // 0x45
jmp end;
call BlockInformation.exec_block_information; // 0x46
jmp end;
call BlockInformation.exec_block_information; // 0x47
jmp end;
call BlockInformation.exec_block_information; // 0x48
jmp end;
call BlockInformation.exec_block_information; // 0x49
jmp end;
call BlockInformation.exec_block_information; // 0x4a
jmp end;
call unknown_opcode; // 0x4b
jmp end;
call unknown_opcode; // 0x4c
jmp end;
call unknown_opcode; // 0x4d
jmp end;
call unknown_opcode; // 0x4e
jmp end;
call unknown_opcode; // 0x4f
jmp end;
call MemoryOperations.exec_pop; // 0x50
jmp end;
call MemoryOperations.exec_mload; // 0x51
jmp end;
call MemoryOperations.exec_mstore; // 0x52
jmp end;
call MemoryOperations.exec_mstore8; // 0x53
jmp end;
call MemoryOperations.exec_sload; // 0x54
jmp end;
call MemoryOperations.exec_sstore; // 0x55
jmp end;
call MemoryOperations.exec_jump; // 0x56
jmp end_no_pc_increment;
call MemoryOperations.exec_jumpi; // 0x57
jmp end_no_pc_increment;
call MemoryOperations.exec_pc; // 0x58
jmp end;
call MemoryOperations.exec_msize; // 0x59
jmp end;
call MemoryOperations.exec_gas; // 0x5a
jmp end;
call MemoryOperations.exec_jumpdest; // 0x5b
jmp end;
call MemoryOperations.exec_tload; // 0x5c
jmp end;
call MemoryOperations.exec_tstore; // 0x5d
jmp end;
call MemoryOperations.exec_mcopy; // 0x5e
jmp end;
call PushOperations.exec_push; // 0x5f
jmp end;
call PushOperations.exec_push; // 0x60
jmp end;
call PushOperations.exec_push; // 0x61
jmp end;
call PushOperations.exec_push; // 0x62
jmp end;
call PushOperations.exec_push; // 0x63
jmp end;
call PushOperations.exec_push; // 0x64
jmp end;
call PushOperations.exec_push; // 0x65
jmp end;
call PushOperations.exec_push; // 0x66
jmp end;
call PushOperations.exec_push; // 0x67
jmp end;
call PushOperations.exec_push; // 0x68
jmp end;
call PushOperations.exec_push; // 0x69
jmp end;
call PushOperations.exec_push; // 0x6a
jmp end;
call PushOperations.exec_push; // 0x6b
jmp end;
call PushOperations.exec_push; // 0x6c
jmp end;
call PushOperations.exec_push; // 0x6d
jmp end;
call PushOperations.exec_push; // 0x6e
jmp end;
call PushOperations.exec_push; // 0x6f
jmp end;
call PushOperations.exec_push; // 0x70
jmp end;
call PushOperations.exec_push; // 0x71
jmp end;
call PushOperations.exec_push; // 0x72
jmp end;
call PushOperations.exec_push; // 0x73
jmp end;
call PushOperations.exec_push; // 0x74
jmp end;
call PushOperations.exec_push; // 0x75
jmp end;
call PushOperations.exec_push; // 0x76
jmp end;
call PushOperations.exec_push; // 0x77
jmp end;
call PushOperations.exec_push; // 0x78
jmp end;
call PushOperations.exec_push; // 0x79
jmp end;
call PushOperations.exec_push; // 0x7a
jmp end;
call PushOperations.exec_push; // 0x7b
jmp end;
call PushOperations.exec_push; // 0x7c
jmp end;
call PushOperations.exec_push; // 0x7d
jmp end;
call PushOperations.exec_push; // 0x7e
jmp end;
call PushOperations.exec_push; // 0x7f
jmp end;
call DuplicationOperations.exec_dup; // 0x80
jmp end;
call DuplicationOperations.exec_dup; // 0x81
jmp end;
call DuplicationOperations.exec_dup; // 0x82
jmp end;
call DuplicationOperations.exec_dup; // 0x83
jmp end;
call DuplicationOperations.exec_dup; // 0x84
jmp end;
call DuplicationOperations.exec_dup; // 0x85
jmp end;
call DuplicationOperations.exec_dup; // 0x86
jmp end;
call DuplicationOperations.exec_dup; // 0x87
jmp end;
call DuplicationOperations.exec_dup; // 0x88
jmp end;
call DuplicationOperations.exec_dup; // 0x89
jmp end;
call DuplicationOperations.exec_dup; // 0x8a
jmp end;
call DuplicationOperations.exec_dup; // 0x8b
jmp end;
call DuplicationOperations.exec_dup; // 0x8c
jmp end;
call DuplicationOperations.exec_dup; // 0x8d
jmp end;
call DuplicationOperations.exec_dup; // 0x8e
jmp end;
call DuplicationOperations.exec_dup; // 0x8f
jmp end;
call ExchangeOperations.exec_swap; // 0x90
jmp end;
call ExchangeOperations.exec_swap; // 0x91
jmp end;
call ExchangeOperations.exec_swap; // 0x92
jmp end;
call ExchangeOperations.exec_swap; // 0x93
jmp end;
call ExchangeOperations.exec_swap; // 0x94
jmp end;
call ExchangeOperations.exec_swap; // 0x95
jmp end;
call ExchangeOperations.exec_swap; // 0x96
jmp end;
call ExchangeOperations.exec_swap; // 0x97
jmp end;
call ExchangeOperations.exec_swap; // 0x98
jmp end;
call ExchangeOperations.exec_swap; // 0x99
jmp end;
call ExchangeOperations.exec_swap; // 0x9a
jmp end;
call ExchangeOperations.exec_swap; // 0x9b
jmp end;
call ExchangeOperations.exec_swap; // 0x9c
jmp end;
call ExchangeOperations.exec_swap; // 0x9d
jmp end;
call ExchangeOperations.exec_swap; // 0x9e
jmp end;
call ExchangeOperations.exec_swap; // 0x9f
jmp end;
call LoggingOperations.exec_log; // 0xa0
jmp end;
call LoggingOperations.exec_log; // 0xa1
jmp end;
call LoggingOperations.exec_log; // 0xa2
jmp end;
call LoggingOperations.exec_log; // 0xa3
jmp end;
call LoggingOperations.exec_log; // 0xa4
jmp end;
call unknown_opcode; // 0xa5
jmp end;
call unknown_opcode; // 0xa6
jmp end;
call unknown_opcode; // 0xa7
jmp end;
call unknown_opcode; // 0xa8
jmp end;
call unknown_opcode; // 0xa9
jmp end;
call unknown_opcode; // 0xaa
jmp end;
call unknown_opcode; // 0xab
jmp end;
call unknown_opcode; // 0xac
jmp end;
call unknown_opcode; // 0xad
jmp end;
call unknown_opcode; // 0xae
jmp end;
call unknown_opcode; // 0xaf
jmp end;
call unknown_opcode; // 0xb0
jmp end;
call unknown_opcode; // 0xb1
jmp end;
call unknown_opcode; // 0xb2
jmp end;
call unknown_opcode; // 0xb3
jmp end;
call unknown_opcode; // 0xb4
jmp end;
call unknown_opcode; // 0xb5
jmp end;
call unknown_opcode; // 0xb6
jmp end;
call unknown_opcode; // 0xb7
jmp end;
call unknown_opcode; // 0xb8
jmp end;
call unknown_opcode; // 0xb9
jmp end;
call unknown_opcode; // 0xba
jmp end;
call unknown_opcode; // 0xbb
jmp end;
call unknown_opcode; // 0xbc
jmp end;
call unknown_opcode; // 0xbd
jmp end;
call unknown_opcode; // 0xbe
jmp end;
call unknown_opcode; // 0xbf
jmp end;
call unknown_opcode; // 0xc0
jmp end;
call unknown_opcode; // 0xc1
jmp end;
call unknown_opcode; // 0xc2
jmp end;
call unknown_opcode; // 0xc3
jmp end;
call unknown_opcode; // 0xc4
jmp end;
call unknown_opcode; // 0xc5
jmp end;
call unknown_opcode; // 0xc6
jmp end;
call unknown_opcode; // 0xc7
jmp end;
call unknown_opcode; // 0xc8
jmp end;
call unknown_opcode; // 0xc9
jmp end;
call unknown_opcode; // 0xca
jmp end;
call unknown_opcode; // 0xcb
jmp end;
call unknown_opcode; // 0xcc
jmp end;
call unknown_opcode; // 0xcd
jmp end;
call unknown_opcode; // 0xce
jmp end;
call unknown_opcode; // 0xcf
jmp end;
call unknown_opcode; // 0xd0
jmp end;
call unknown_opcode; // 0xd1
jmp end;
call unknown_opcode; // 0xd2
jmp end;
call unknown_opcode; // 0xd3
jmp end;
call unknown_opcode; // 0xd4
jmp end;
call unknown_opcode; // 0xd5
jmp end;
call unknown_opcode; // 0xd6
jmp end;
call unknown_opcode; // 0xd7
jmp end;
call unknown_opcode; // 0xd8
jmp end;
call unknown_opcode; // 0xd9
jmp end;
call unknown_opcode; // 0xda
jmp end;
call unknown_opcode; // 0xdb
jmp end;
call unknown_opcode; // 0xdc
jmp end;
call unknown_opcode; // 0xdd
jmp end;
call unknown_opcode; // 0xde
jmp end;
call unknown_opcode; // 0xdf
jmp end;
call unknown_opcode; // 0xe0
jmp end;
call unknown_opcode; // 0xe1
jmp end;
call unknown_opcode; // 0xe2
jmp end;
call unknown_opcode; // 0xe3
jmp end;
call unknown_opcode; // 0xe4
jmp end;
call unknown_opcode; // 0xe5
jmp end;
call unknown_opcode; // 0xe6
jmp end;
call unknown_opcode; // 0xe7
jmp end;
call unknown_opcode; // 0xe8
jmp end;
call unknown_opcode; // 0xe9
jmp end;
call unknown_opcode; // 0xea
jmp end;
call unknown_opcode; // 0xeb
jmp end;
call unknown_opcode; // 0xec
jmp end;
call unknown_opcode; // 0xed
jmp end;
call unknown_opcode; // 0xee
jmp end;
call unknown_opcode; // 0xef
jmp end;
call SystemOperations.exec_create; // 0xf0
jmp end;
call SystemOperations.exec_call; // 0xf1
jmp end;
call SystemOperations.exec_callcode; // 0xf2
jmp end;
call SystemOperations.exec_return; // 0xf3
jmp end;
call SystemOperations.exec_delegatecall; // 0xf4
jmp end;
call SystemOperations.exec_create; // 0xf5
jmp end;
call unknown_opcode; // 0xf6
jmp end;
call unknown_opcode; // 0xf7
jmp end;
call unknown_opcode; // 0xf8
jmp end;
call unknown_opcode; // 0xf9
jmp end;
call SystemOperations.exec_staticcall; // 0xfa
jmp end;
call unknown_opcode; // 0xfb
jmp end;
call unknown_opcode; // 0xfc
jmp end;
call SystemOperations.exec_revert; // 0xfd
jmp end;
call SystemOperations.exec_invalid; // 0xfe
jmp end;
call SystemOperations.exec_selfdestruct; // 0xff
jmp end;
end:
let syscall_ptr = cast([ap - 8], felt*);
let pedersen_ptr = cast([ap - 7], HashBuiltin*);
let range_check_ptr = [ap - 6];
let bitwise_ptr = cast([ap - 5], BitwiseBuiltin*);
let stack = cast([ap - 4], model.Stack*);
let memory = cast([ap - 3], model.Memory*);
let state = cast([ap - 2], model.State*);
let evm = cast([ap - 1], model.EVM*);
let evm_prev = cast([fp - 3], model.EVM*);
if (evm_prev.message.depth == evm.message.depth) {
let evm = EVM.increment_program_counter(evm, 1);
return evm;
} else {
return evm;
}
end_no_pc_increment:
let syscall_ptr = cast([ap - 8], felt*);
let pedersen_ptr = cast([ap - 7], HashBuiltin*);
let range_check_ptr = [ap - 6];
let bitwise_ptr = cast([ap - 5], BitwiseBuiltin*);
let stack = cast([ap - 4], model.Stack*);
let memory = cast([ap - 3], model.Memory*);
let state = cast([ap - 2], model.State*);
let evm = cast([ap - 1], model.EVM*);
return evm;
}
// @notice Iteratively decode and execute the bytecode of an EVM
// @param evm The pointer to the execution context.
// @return EVM The pointer to the updated execution context.
func run{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
stack: model.Stack*,
memory: model.Memory*,
state: model.State*,
}(evm: model.EVM*) -> model.EVM* {
alloc_locals;
if (evm.stopped == FALSE) {
let evm = exec_opcode(evm);
return run(evm);
}
Memory.finalize();
Stack.finalize();
State.finalize();
with evm {
EVM.finalize();
}
if (evm.message.depth == 0) {
if (evm.reverted != 0) {
// All REVERTS in a root ctx set the gas_refund to 0.
// Only if the execution has halted exceptionnaly, consume all gas
let is_not_exceptional_revert = Helpers.is_zero(evm.reverted - 1);
let gas_left = is_not_exceptional_revert * evm.gas_left;
tempvar evm = new model.EVM(
message=evm.message,
return_data_len=evm.return_data_len,
return_data=evm.return_data,
program_counter=evm.program_counter,
stopped=evm.stopped,
gas_left=gas_left,
gas_refund=0,
reverted=evm.reverted,
);
return evm;
}
if (evm.message.is_create != FALSE) {
let evm = Internals._finalize_create_tx(evm);
return evm;
}
return evm;
}
let stack = evm.message.parent.stack;
let memory = evm.message.parent.memory;
if (evm.message.is_create != FALSE) {
let evm = CreateHelper.finalize_parent(evm);
return run(evm);
} else {
let evm = CallHelper.finalize_parent(evm);
return run(evm);
}
}
// @notice Run the given bytecode with the given calldata and parameters
// @param address The target account address
// @param is_deploy_tx Whether the transaction is a deploy tx or not
// @param origin The caller EVM address
// @param bytecode_len The length of the bytecode
// @param bytecode The bytecode run
// @param calldata_len The length of the calldata
// @param calldata The calldata of the execution
// @param value The value of the execution
// @param gas_limit The gas limit of the execution
// @param gas_price The gas price for the execution
// @param access_list_len The length (in number of felts) of the serialized access list
// @param access_list The access list
// @return evm The EVM post-execution
// @return state The state post-execution
// @return stack The stack post-execution
// @return memory The memory post-execution
// @return gas_used the gas used by the transaction
// @return required_gas The amount of gas required by the transaction to successfully execute. This is different
// from the gas used by the transaction as it doesn't take into account any refunds.
func execute{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(
env: model.Environment*,
address: model.Address*,
is_deploy_tx: felt,
bytecode_len: felt,
bytecode: felt*,
calldata_len: felt,
calldata: felt*,
value: Uint256*,
gas_limit: felt,
access_list_len: felt,
access_list: felt*,
) -> (model.EVM*, model.Stack*, model.Memory*, model.State*, felt, felt) {
alloc_locals;
let fp_and_pc = get_fp_and_pc();
local __fp__: felt* = fp_and_pc.fp_val;
// Compute intrinsic gas usage
// See https://www.evm.codes/about#gascosts
let count = count_not_zero(calldata_len, calldata);
let zeroes = calldata_len - count;
let calldata_gas = zeroes * 4 + count * 16;
let intrinsic_gas = Gas.TX_BASE_COST + calldata_gas;
// If is_deploy_tx is TRUE, then
// bytecode is data and data is empty
// else, bytecode and data are kept as is
let bytecode_len = calldata_len * is_deploy_tx + bytecode_len * (1 - is_deploy_tx);
let calldata_len = calldata_len * (1 - is_deploy_tx);
let tmp_bytecode = bytecode;
let tmp_calldata = calldata;
let tmp_intrinsic_gas = intrinsic_gas;
local bytecode: felt*;
local calldata: felt*;
local intrinsic_gas: felt;
local code_address: model.Address*;
if (is_deploy_tx != FALSE) {
let (empty: felt*) = alloc();
let (init_code_words, _) = unsigned_div_rem(bytecode_len + 31, 32);
let init_code_gas = Gas.INIT_CODE_WORD_COST * init_code_words;
assert bytecode = tmp_calldata;
assert calldata = empty;
assert intrinsic_gas = tmp_intrinsic_gas + Gas.CREATE + init_code_gas;
assert code_address = new model.Address(starknet=0, evm=0);
let (valid_jumpdests_start, valid_jumpdests) = Helpers.initialize_jumpdests(
bytecode_len=bytecode_len, bytecode=bytecode
);
tempvar range_check_ptr = range_check_ptr;
tempvar valid_jumpdests_start = valid_jumpdests_start;
tempvar valid_jumpdests = valid_jumpdests;
} else {
assert bytecode = tmp_bytecode;
assert calldata = tmp_calldata;
assert intrinsic_gas = tmp_intrinsic_gas;
assert code_address = address;
let (new_dict) = default_dict_new(0);
tempvar range_check_ptr = range_check_ptr;
tempvar valid_jumpdests_start = new_dict;
tempvar valid_jumpdests = new_dict;
}
let valid_jumpdests_start = cast([ap - 2], DictAccess*);
let valid_jumpdests = cast([ap - 1], DictAccess*);
tempvar message = new model.Message(
bytecode=bytecode,
bytecode_len=bytecode_len,
valid_jumpdests_start=valid_jumpdests_start,
valid_jumpdests=valid_jumpdests,
calldata=calldata,
calldata_len=calldata_len,
value=value,
caller=env.origin,
parent=cast(0, model.Parent*),
address=address,
code_address=code_address,
read_only=FALSE,
is_create=is_deploy_tx,
depth=0,
env=env,
cairo_precompile_called=FALSE,
);
let stack = Stack.init();
let memory = Memory.init();
let state = State.init();
// Cache the coinbase, precompiles, caller, and target, making them warm
with state {
let coinbase = State.get_account(env.coinbase);
State.cache_precompiles();
State.get_account(address.evm);
let access_list_cost = State.cache_access_list(access_list_len, access_list);
}
let intrinsic_gas = intrinsic_gas + access_list_cost;
let evm = EVM.init(message, gas_limit - intrinsic_gas);
let is_gas_limit_enough = is_le_felt(intrinsic_gas, gas_limit);
if (is_gas_limit_enough == FALSE) {
let evm = EVM.halt_validation_failed(evm);
State.finalize{state=state}();
return (evm, stack, memory, state, 0, 0);
}
tempvar is_initcode_invalid = is_deploy_tx * is_nn(
bytecode_len - (2 * Constants.MAX_CODE_SIZE + 1)
);
if (is_initcode_invalid != FALSE) {
let evm = EVM.halt_validation_failed(evm);
State.finalize{state=state}();
return (evm, stack, memory, state, 0, 0);
}
// Charge the gas fee to the user without setting up a transfer.
// Transfers with the exact amounts will be performed post-execution.
// Note: balance > effective_fee was verified in eth_send_raw_unsigned_tx()
let max_fee = gas_limit * env.gas_price;
let (fee_high, fee_low) = split_felt(max_fee);
let max_fee_u256 = Uint256(low=fee_low, high=fee_high);
with state {
let sender = State.get_account(env.origin);
let (local new_balance) = uint256_sub([sender.balance], max_fee_u256);
let sender = Account.set_balance(sender, &new_balance);
let sender = Account.set_nonce(sender, sender.nonce + 1);
State.update_account(sender);
let transfer = model.Transfer(sender.address, address, [value]);
let success = State.add_transfer(transfer);
// Check collision
let account = State.get_account(address.evm);
let code_or_nonce = Account.has_code_or_nonce(account);
let is_collision = code_or_nonce * is_deploy_tx;
// Nonce is set to 1 in case of deploy_tx and account is marked as created
let nonce = account.nonce * (1 - is_deploy_tx) + is_deploy_tx;
let account = Account.set_nonce(account, nonce);
let account = Account.set_created(account, is_deploy_tx);
State.update_account(account);
}
if (is_collision != 0) {
let (revert_reason_len, revert_reason) = Errors.addressCollision();
tempvar evm = EVM.stop(evm, revert_reason_len, revert_reason, Errors.EXCEPTIONAL_HALT);
} else {
tempvar evm = evm;
}
if (success == 0) {
let (revert_reason_len, revert_reason) = Errors.balanceError();
tempvar evm = EVM.stop(evm, revert_reason_len, revert_reason, Errors.EXCEPTIONAL_HALT);
} else {
tempvar evm = evm;
}
with stack, memory, state {
let evm = run(evm);
}
let required_gas = gas_limit - evm.gas_left;
let (max_refund, _) = unsigned_div_rem(required_gas, 5);
let is_max_refund_le_gas_refund = is_nn(evm.gas_refund - max_refund);
tempvar gas_refund = is_max_refund_le_gas_refund * max_refund + (
1 - is_max_refund_le_gas_refund
) * evm.gas_refund;
let total_gas_used = required_gas - gas_refund;
// Reset the state if the execution has failed.
// Only the gas fee paid will be committed.
State.finalize{state=state}();
if (evm.reverted != 0) {
with_attr error_message(
"EVM tx reverted, reverting SN tx because of previous calls to cairo precompiles") {
assert evm.message.cairo_precompile_called = FALSE;
}
tempvar state = State.init();
} else {
tempvar state = state;
}
let is_reverted = is_not_zero(evm.reverted);
let success = 1 - is_reverted;
let paid_fee_u256 = Uint256(max_fee_u256.low * success, max_fee_u256.high * success);