-
Notifications
You must be signed in to change notification settings - Fork 246
/
instructions.cairo
638 lines (624 loc) · 19.3 KB
/
instructions.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
// SPDX-License-Identifier: MIT
%lang starknet
// Starkware dependencies
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin
from starkware.cairo.common.invoke import invoke
from starkware.cairo.common.math import assert_nn
from starkware.cairo.common.math_cmp import is_le
from starkware.cairo.common.memcpy import memcpy
from starkware.cairo.common.bool import FALSE
from starkware.cairo.common.registers import get_ap
from starkware.cairo.common.registers import get_label_location
// Internal dependencies
from kakarot.model import model
from kakarot.execution_context import ExecutionContext
from kakarot.instructions.push_operations import PushOperations
from kakarot.instructions.arithmetic_operations import ArithmeticOperations
from kakarot.instructions.comparison_operations import ComparisonOperations
from kakarot.instructions.duplication_operations import DuplicationOperations
from kakarot.instructions.exchange_operations import ExchangeOperations
from kakarot.instructions.logging_operations import LoggingOperations
from kakarot.instructions.memory_operations import MemoryOperations
from kakarot.instructions.environmental_information import EnvironmentalInformation
from kakarot.instructions.block_information import BlockInformation
from kakarot.instructions.system_operations import SystemOperations
from kakarot.instructions.sha3 import Sha3
// @title EVM instructions processing.
// @notice This file contains functions related to the processing of EVM instructions.
// @author @abdelhamidbakhta
// @custom:namespace EVMInstructions
namespace EVMInstructions {
// @notice Decode the current opcode and execute associated function.
// @dev The function uses an internal jump table to execute the corresponding opcode
// @param ctx The pointer to the execution context.
// @return The pointer to the updated execution context.
func decode_and_execute{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(ctx: model.ExecutionContext*) -> model.ExecutionContext* {
alloc_locals;
// Retrieve the current program counter.
let pc = ctx.program_counter;
local opcode;
let is_pc_ge_code_len = is_le(ctx.call_context.bytecode_len, pc);
if (is_pc_ge_code_len != FALSE) {
assert opcode = 0;
} else {
assert opcode = [ctx.call_context.bytecode + pc];
}
// 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 + 3 * opcode;
// move program counter + 1 after opcode is read
let ctx = ExecutionContext.increment_program_counter(self=ctx, inc_value=1);
// Prepare arguments
[ap] = syscall_ptr, ap++;
[ap] = pedersen_ptr, ap++;
[ap] = range_check_ptr, ap++;
[ap] = bitwise_ptr, ap++;
[ap] = ctx, ap++;
// call opcode
jmp rel offset;
call exec_stop; // 0x0
ret;
call ArithmeticOperations.exec_add; // 0x1
ret;
call ArithmeticOperations.exec_mul; // 0x2
ret;
call ArithmeticOperations.exec_sub; // 0x3
ret;
call ArithmeticOperations.exec_div; // 0x4
ret;
call ArithmeticOperations.exec_sdiv; // 0x5
ret;
call ArithmeticOperations.exec_mod; // 0x6
ret;
call ArithmeticOperations.exec_smod; // 0x7
ret;
call ArithmeticOperations.exec_addmod; // 0x8
ret;
call ArithmeticOperations.exec_mulmod; // 0x9
ret;
call ArithmeticOperations.exec_exp; // 0xa
ret;
call ArithmeticOperations.exec_signextend; // 0xb
ret;
call unknown_opcode; // 0xc
ret;
call unknown_opcode; // 0xd
ret;
call unknown_opcode; // 0xe
ret;
call unknown_opcode; // 0xf
ret;
call ComparisonOperations.exec_lt; // 0x10
ret;
call ComparisonOperations.exec_gt; // 0x11
ret;
call ComparisonOperations.exec_slt; // 0x12
ret;
call ComparisonOperations.exec_sgt; // 0x13
ret;
call ComparisonOperations.exec_eq; // 0x14
ret;
call ComparisonOperations.exec_iszero; // 0x15
ret;
call ComparisonOperations.exec_and; // 0x16
ret;
call ComparisonOperations.exec_or; // 0x17
ret;
call ComparisonOperations.exec_xor; // 0x18
ret;
call ComparisonOperations.exec_not; // 0x19
ret;
call ComparisonOperations.exec_byte; // 0x1a
ret;
call ComparisonOperations.exec_shl; // 0x1b
ret;
call ComparisonOperations.exec_shr; // 0x1c
ret;
call ComparisonOperations.exec_sar; // 0x1d
ret;
call unknown_opcode; // 0x1e
ret;
call unknown_opcode; // 0x1f
ret;
call Sha3.exec_sha3; // 0x20
ret;
call unknown_opcode; // 0x21
ret;
call unknown_opcode; // 0x22
ret;
call unknown_opcode; // 0x23
ret;
call unknown_opcode; // 0x24
ret;
call unknown_opcode; // 0x25
ret;
call unknown_opcode; // 0x26
ret;
call unknown_opcode; // 0x27
ret;
call unknown_opcode; // 0x28
ret;
call unknown_opcode; // 0x29
ret;
call unknown_opcode; // 0x2a
ret;
call unknown_opcode; // 0x2b
ret;
call unknown_opcode; // 0x2c
ret;
call unknown_opcode; // 0x2d
ret;
call unknown_opcode; // 0x2e
ret;
call unknown_opcode; // 0x2f
ret;
call EnvironmentalInformation.exec_address; // 0x30
ret;
call EnvironmentalInformation.exec_balance; // 0x31
ret;
call EnvironmentalInformation.exec_origin; // 0x32
ret;
call EnvironmentalInformation.exec_caller; // 0x33
ret;
call EnvironmentalInformation.exec_callvalue; // 0x34
ret;
call EnvironmentalInformation.exec_calldataload; // 0x35
ret;
call EnvironmentalInformation.exec_calldatasize; // 0x36
ret;
call EnvironmentalInformation.exec_calldatacopy; // 0x37
ret;
call EnvironmentalInformation.exec_codesize; // 0x38
ret;
call EnvironmentalInformation.exec_codecopy; // 0x39
ret;
call not_implemented_opcode; // 0x3a
ret;
call not_implemented_opcode; // 0x3b
ret;
call not_implemented_opcode; // 0x3c
ret;
call EnvironmentalInformation.exec_returndatasize; // 0x3d
ret;
call EnvironmentalInformation.exec_returndatacopy; // 0x3e
ret;
call not_implemented_opcode; // 0x3f
ret;
call not_implemented_opcode; // 0x40
ret;
call BlockInformation.exec_coinbase; // 0x41
ret;
call BlockInformation.exec_timestamp; // 0x42
ret;
call BlockInformation.exec_number; // 0x43
ret;
call BlockInformation.exec_difficulty; // 0x44
ret;
call BlockInformation.exec_gaslimit; // 0x45
ret;
call BlockInformation.exec_chainid; // 0x46
ret;
call BlockInformation.exec_selfbalance; // 0x47
ret;
call BlockInformation.exec_basefee; // 0x48
ret;
call unknown_opcode; // 0x49
ret;
call unknown_opcode; // 0x4a
ret;
call unknown_opcode; // 0x4b
ret;
call unknown_opcode; // 0x4c
ret;
call unknown_opcode; // 0x4d
ret;
call unknown_opcode; // 0x4e
ret;
call unknown_opcode; // 0x4f
ret;
call MemoryOperations.exec_pop; // 0x50
ret;
call MemoryOperations.exec_mload; // 0x51
ret;
call MemoryOperations.exec_mstore; // 0x52
ret;
call MemoryOperations.exec_mstore8; // 0x53
ret;
call MemoryOperations.exec_sload; // 0x54
ret;
call MemoryOperations.exec_sstore; // 0x55
ret;
call MemoryOperations.exec_jump; // 0x56
ret;
call MemoryOperations.exec_jumpi; // 0x57
ret;
call MemoryOperations.exec_pc; // 0x58
ret;
call MemoryOperations.exec_msize; // 0x59
ret;
call MemoryOperations.exec_gas; // 0x5a
ret;
call MemoryOperations.exec_jumpdest; // 0x5b
ret;
call unknown_opcode; // 0x5c
ret;
call unknown_opcode; // 0x5d
ret;
call unknown_opcode; // 0x5e
ret;
call unknown_opcode; // 0x5f
ret;
call PushOperations.exec_push1; // 0x60
ret;
call PushOperations.exec_push2; // 0x61
ret;
call PushOperations.exec_push3; // 0x62
ret;
call PushOperations.exec_push4; // 0x63
ret;
call PushOperations.exec_push5; // 0x64
ret;
call PushOperations.exec_push6; // 0x65
ret;
call PushOperations.exec_push7; // 0x66
ret;
call PushOperations.exec_push8; // 0x67
ret;
call PushOperations.exec_push9; // 0x68
ret;
call PushOperations.exec_push10; // 0x69
ret;
call PushOperations.exec_push11; // 0x6a
ret;
call PushOperations.exec_push12; // 0x6b
ret;
call PushOperations.exec_push13; // 0x6c
ret;
call PushOperations.exec_push14; // 0x6d
ret;
call PushOperations.exec_push15; // 0x6e
ret;
call PushOperations.exec_push16; // 0x6f
ret;
call PushOperations.exec_push17; // 0x70
ret;
call PushOperations.exec_push18; // 0x71
ret;
call PushOperations.exec_push19; // 0x72
ret;
call PushOperations.exec_push20; // 0x73
ret;
call PushOperations.exec_push21; // 0x74
ret;
call PushOperations.exec_push22; // 0x75
ret;
call PushOperations.exec_push23; // 0x76
ret;
call PushOperations.exec_push24; // 0x77
ret;
call PushOperations.exec_push25; // 0x78
ret;
call PushOperations.exec_push26; // 0x79
ret;
call PushOperations.exec_push27; // 0x7a
ret;
call PushOperations.exec_push28; // 0x7b
ret;
call PushOperations.exec_push29; // 0x7c
ret;
call PushOperations.exec_push30; // 0x7d
ret;
call PushOperations.exec_push31; // 0x7e
ret;
call PushOperations.exec_push32; // 0x7f
ret;
call DuplicationOperations.exec_dup1; // 0x80
ret;
call DuplicationOperations.exec_dup2; // 0x81
ret;
call DuplicationOperations.exec_dup3; // 0x82
ret;
call DuplicationOperations.exec_dup4; // 0x83
ret;
call DuplicationOperations.exec_dup5; // 0x84
ret;
call DuplicationOperations.exec_dup6; // 0x85
ret;
call DuplicationOperations.exec_dup7; // 0x86
ret;
call DuplicationOperations.exec_dup8; // 0x87
ret;
call DuplicationOperations.exec_dup9; // 0x88
ret;
call DuplicationOperations.exec_dup10; // 0x89
ret;
call DuplicationOperations.exec_dup11; // 0x8a
ret;
call DuplicationOperations.exec_dup12; // 0x8b
ret;
call DuplicationOperations.exec_dup13; // 0x8c
ret;
call DuplicationOperations.exec_dup14; // 0x8d
ret;
call DuplicationOperations.exec_dup15; // 0x8e
ret;
call DuplicationOperations.exec_dup16; // 0x8f
ret;
call ExchangeOperations.exec_swap1; // 0x90
ret;
call ExchangeOperations.exec_swap2; // 0x91
ret;
call ExchangeOperations.exec_swap3; // 0x92
ret;
call ExchangeOperations.exec_swap4; // 0x93
ret;
call ExchangeOperations.exec_swap5; // 0x94
ret;
call ExchangeOperations.exec_swap6; // 0x95
ret;
call ExchangeOperations.exec_swap7; // 0x96
ret;
call ExchangeOperations.exec_swap8; // 0x97
ret;
call ExchangeOperations.exec_swap9; // 0x98
ret;
call ExchangeOperations.exec_swap10; // 0x99
ret;
call ExchangeOperations.exec_swap11; // 0x9a
ret;
call ExchangeOperations.exec_swap12; // 0x9b
ret;
call ExchangeOperations.exec_swap13; // 0x9c
ret;
call ExchangeOperations.exec_swap14; // 0x9d
ret;
call ExchangeOperations.exec_swap15; // 0x9e
ret;
call ExchangeOperations.exec_swap16; // 0x9f
ret;
call LoggingOperations.exec_log_0; // 0xa0
ret;
call LoggingOperations.exec_log_1; // 0xa1
ret;
call LoggingOperations.exec_log_2; // 0xa2
ret;
call LoggingOperations.exec_log_3; // 0xa3
ret;
call LoggingOperations.exec_log_4; // 0xa4
ret;
call unknown_opcode; // 0xa5
ret;
call unknown_opcode; // 0xa6
ret;
call unknown_opcode; // 0xa7
ret;
call unknown_opcode; // 0xa8
ret;
call unknown_opcode; // 0xa9
ret;
call unknown_opcode; // 0xaa
ret;
call unknown_opcode; // 0xab
ret;
call unknown_opcode; // 0xac
ret;
call unknown_opcode; // 0xad
ret;
call unknown_opcode; // 0xae
ret;
call unknown_opcode; // 0xaf
ret;
call unknown_opcode; // 0xb0
ret;
call unknown_opcode; // 0xb1
ret;
call unknown_opcode; // 0xb2
ret;
call unknown_opcode; // 0xb3
ret;
call unknown_opcode; // 0xb4
ret;
call unknown_opcode; // 0xb5
ret;
call unknown_opcode; // 0xb6
ret;
call unknown_opcode; // 0xb7
ret;
call unknown_opcode; // 0xb8
ret;
call unknown_opcode; // 0xb9
ret;
call unknown_opcode; // 0xba
ret;
call unknown_opcode; // 0xbb
ret;
call unknown_opcode; // 0xbc
ret;
call unknown_opcode; // 0xbd
ret;
call unknown_opcode; // 0xbe
ret;
call unknown_opcode; // 0xbf
ret;
call unknown_opcode; // 0xc0
ret;
call unknown_opcode; // 0xc1
ret;
call unknown_opcode; // 0xc2
ret;
call unknown_opcode; // 0xc3
ret;
call unknown_opcode; // 0xc4
ret;
call unknown_opcode; // 0xc5
ret;
call unknown_opcode; // 0xc6
ret;
call unknown_opcode; // 0xc7
ret;
call unknown_opcode; // 0xc8
ret;
call unknown_opcode; // 0xc9
ret;
call unknown_opcode; // 0xca
ret;
call unknown_opcode; // 0xcb
ret;
call unknown_opcode; // 0xcc
ret;
call unknown_opcode; // 0xcd
ret;
call unknown_opcode; // 0xce
ret;
call unknown_opcode; // 0xcf
ret;
call unknown_opcode; // 0xd0
ret;
call unknown_opcode; // 0xd1
ret;
call unknown_opcode; // 0xd2
ret;
call unknown_opcode; // 0xd3
ret;
call unknown_opcode; // 0xd4
ret;
call unknown_opcode; // 0xd5
ret;
call unknown_opcode; // 0xd6
ret;
call unknown_opcode; // 0xd7
ret;
call unknown_opcode; // 0xd8
ret;
call unknown_opcode; // 0xd9
ret;
call unknown_opcode; // 0xda
ret;
call unknown_opcode; // 0xdb
ret;
call unknown_opcode; // 0xdc
ret;
call unknown_opcode; // 0xdd
ret;
call unknown_opcode; // 0xde
ret;
call unknown_opcode; // 0xdf
ret;
call unknown_opcode; // 0xe0
ret;
call unknown_opcode; // 0xe1
ret;
call unknown_opcode; // 0xe2
ret;
call unknown_opcode; // 0xe3
ret;
call unknown_opcode; // 0xe4
ret;
call unknown_opcode; // 0xe5
ret;
call unknown_opcode; // 0xe6
ret;
call unknown_opcode; // 0xe7
ret;
call unknown_opcode; // 0xe8
ret;
call unknown_opcode; // 0xe9
ret;
call unknown_opcode; // 0xea
ret;
call unknown_opcode; // 0xeb
ret;
call unknown_opcode; // 0xec
ret;
call unknown_opcode; // 0xed
ret;
call unknown_opcode; // 0xee
ret;
call unknown_opcode; // 0xef
ret;
call not_implemented_opcode; // 0xf0
ret;
call not_implemented_opcode; // 0xf1
ret;
call not_implemented_opcode; // 0xf2
ret;
call SystemOperations.exec_return; // 0xf3
ret;
call unknown_opcode; // 0xf4
ret;
call not_implemented_opcode; // 0xf5
ret;
call unknown_opcode; // 0xf6
ret;
call unknown_opcode; // 0xf7
ret;
call unknown_opcode; // 0xf8
ret;
call unknown_opcode; // 0xf9
ret;
call not_implemented_opcode; // 0xfa
ret;
call unknown_opcode; // 0xfb
ret;
call unknown_opcode; // 0xfc
ret;
call SystemOperations.exec_revert; // 0xfd
ret;
call SystemOperations.exec_invalid; // 0xfe
ret;
call not_implemented_opcode; // 0xff
ret;
}
// @notice A placeholder for opcodes that don't exist
// @dev Halts execution
// @param ctx The pointer to the execution context
// @return Updated execution context.
func unknown_opcode{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(ctx_ptr: model.ExecutionContext*) {
with_attr error_message("Kakarot: UnknownOpcode") {
assert 0 = 1;
}
return ();
}
// @notice A placeholder for opcodes that don't exist
// @dev Halts execution
// @param ctx The pointer to the execution context
// @return Updated execution context.
func not_implemented_opcode{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(ctx_ptr: model.ExecutionContext*) {
with_attr error_message("Kakarot: NotImplementedOpcode") {
assert 0 = 1;
}
return ();
}
// @notice 0x00 - STOP
// @dev Halts execution
// @custom:since Frontier
// @custom:group Stop and Arithmetic Operations
// @custom:gas 0
// @param ctx The pointer to the execution context
// @return Updated execution context.
func exec_stop{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr,
bitwise_ptr: BitwiseBuiltin*,
}(ctx_ptr: model.ExecutionContext*) -> model.ExecutionContext* {
let ctx = ExecutionContext.stop(ctx_ptr);
return ctx;
}
}