-
Notifications
You must be signed in to change notification settings - Fork 91
/
opcode.py
5875 lines (4592 loc) · 109 KB
/
opcode.py
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
"""
Ethereum Virtual Machine opcode definitions.
Acknowledgments: The individual opcode documentation below is due to the work by
[smlXL](https://github.com/smlxl) on [evm.codes](https://www.evm.codes/), available as open
source [github.com/smlxl/evm.codes](https://github.com/smlxl/evm.codes) - thank you! And thanks
to @ThreeHrSleep for integrating it in the docstrings.
"""
from enum import Enum
from typing import Any, Callable, Iterable, List, Mapping, Optional, SupportsBytes
from ethereum_test_base_types import to_bytes
from .bytecode import Bytecode
def _get_int_size(n: int) -> int:
"""Return size of an integer in bytes."""
if n < 0:
# Negative numbers in the EVM are represented as two's complement of 32 bytes
return 32
byte_count = 0
while n:
byte_count += 1
n >>= 8
return byte_count
KW_ARGS_DEFAULTS_TYPE = Mapping[str, "int | bytes | str | Opcode | Bytecode"]
def _stack_argument_to_bytecode(
arg: "int | bytes | SupportsBytes | str | Opcode | Bytecode | Iterable[int]",
) -> Bytecode:
"""Convert stack argument in an opcode or macro to bytecode."""
if isinstance(arg, Bytecode):
return arg
# We are going to push a constant to the stack.
data_size = 0
if isinstance(arg, int):
signed = arg < 0
data_size = _get_int_size(arg)
if data_size > 32:
raise ValueError("Opcode stack data must be less than 32 bytes")
elif data_size == 0:
# Pushing 0 is done with the PUSH1 opcode for compatibility reasons.
data_size = 1
arg = arg.to_bytes(
length=data_size,
byteorder="big",
signed=signed,
)
else:
arg = to_bytes(arg).lstrip(b"\0") # type: ignore
if arg == b"":
# Pushing 0 is done with the PUSH1 opcode for compatibility reasons.
arg = b"\x00"
data_size = len(arg)
assert isinstance(arg, bytes)
assert data_size > 0
new_opcode = _push_opcodes_byte_list[data_size - 1][arg]
return new_opcode
class Opcode(Bytecode):
"""
Represents a single Opcode instruction in the EVM, with extra metadata useful to parametrize
tests.
Parameters
----------
- data_portion_length: number of bytes after the opcode in the bytecode
that represent data
- data_portion_formatter: function to format the data portion of the opcode, if any
- stack_properties_modifier: function to modify the stack properties of the opcode after the
data portion has been processed
- kwargs: list of keyword arguments that can be passed to the opcode, in the order they are
meant to be placed in the stack
- kwargs_defaults: default values for the keyword arguments if any, otherwise 0
- unchecked_stack: whether the bytecode should ignore stack checks when being called
"""
data_portion_length: int
data_portion_formatter: Optional[Callable[[Any], bytes]]
stack_properties_modifier: Optional[Callable[[Any], tuple[int, int, int, int]]]
kwargs: List[str] | None
kwargs_defaults: KW_ARGS_DEFAULTS_TYPE
unchecked_stack: bool = False
def __new__(
cls,
opcode_or_byte: "int | bytes | Opcode",
*,
popped_stack_items: int = 0,
pushed_stack_items: int = 0,
max_stack_height: int | None = None,
min_stack_height: int | None = None,
data_portion_length: int = 0,
data_portion_formatter=None,
stack_properties_modifier=None,
unchecked_stack=False,
terminating: bool = False,
kwargs: List[str] | None = None,
kwargs_defaults: Optional[KW_ARGS_DEFAULTS_TYPE] = None,
):
"""Create new opcode instance."""
if kwargs_defaults is None:
kwargs_defaults = {}
if type(opcode_or_byte) is Opcode:
# Required because Enum class calls the base class with the instantiated object as
# parameter.
return opcode_or_byte
elif isinstance(opcode_or_byte, int) or isinstance(opcode_or_byte, bytes):
obj_bytes = (
bytes([opcode_or_byte]) if isinstance(opcode_or_byte, int) else opcode_or_byte
)
if min_stack_height is None:
min_stack_height = popped_stack_items
if max_stack_height is None:
max_stack_height = max(
min_stack_height - popped_stack_items + pushed_stack_items, min_stack_height
)
obj = super().__new__(
cls,
obj_bytes,
popped_stack_items=popped_stack_items,
pushed_stack_items=pushed_stack_items,
max_stack_height=max_stack_height,
min_stack_height=min_stack_height,
terminating=terminating,
)
obj.data_portion_length = data_portion_length
obj.data_portion_formatter = data_portion_formatter
obj.stack_properties_modifier = stack_properties_modifier
obj.unchecked_stack = unchecked_stack
obj.kwargs = kwargs
obj.kwargs_defaults = kwargs_defaults
return obj
raise TypeError("Opcode constructor '__new__' didn't return an instance!")
def __getitem__(self, *args: "int | bytes | str | Iterable[int]") -> "Opcode":
"""
Initialize a new instance of the opcode with the data portion set, and also clear
the data portion variables to avoid reusing them.
"""
if self.data_portion_formatter is None and self.data_portion_length == 0:
raise ValueError("Opcode does not have a data portion or has already been set")
data_portion = bytes()
if self.data_portion_formatter is not None:
if len(args) == 1 and isinstance(args[0], Iterable) and not isinstance(args[0], bytes):
data_portion = self.data_portion_formatter(*args[0])
else:
data_portion = self.data_portion_formatter(*args)
elif self.data_portion_length > 0:
# For opcodes with a data portion, the first argument is the data and the rest of the
# arguments form the stack.
assert len(args) == 1, "Opcode with data portion requires exactly one argument"
data = args[0]
if isinstance(data, bytes) or isinstance(data, SupportsBytes) or isinstance(data, str):
if isinstance(data, str):
if data.startswith("0x"):
data = data[2:]
data = bytes.fromhex(data)
elif isinstance(data, SupportsBytes):
data = bytes(data)
assert len(data) <= self.data_portion_length
data_portion = data.rjust(self.data_portion_length, b"\x00")
elif isinstance(data, int):
signed = data < 0
data_portion = data.to_bytes(
length=self.data_portion_length,
byteorder="big",
signed=signed,
)
else:
raise TypeError("Opcode data portion must be either an int or bytes/hex string")
popped_stack_items = self.popped_stack_items
pushed_stack_items = self.pushed_stack_items
min_stack_height = self.min_stack_height
max_stack_height = self.max_stack_height
assert (
popped_stack_items is not None
and pushed_stack_items is not None
and min_stack_height is not None
)
if self.stack_properties_modifier is not None:
(
popped_stack_items,
pushed_stack_items,
min_stack_height,
max_stack_height,
) = self.stack_properties_modifier(data_portion)
new_opcode = Opcode(
bytes(self) + data_portion,
popped_stack_items=popped_stack_items,
pushed_stack_items=pushed_stack_items,
min_stack_height=min_stack_height,
max_stack_height=max_stack_height,
data_portion_length=0,
data_portion_formatter=None,
unchecked_stack=self.unchecked_stack,
terminating=self.terminating,
kwargs=self.kwargs,
kwargs_defaults=self.kwargs_defaults,
)
new_opcode._name_ = f"{self._name_}_0x{data_portion.hex()}"
return new_opcode
def __call__(
self,
*args_t: "int | bytes | str | Opcode | Bytecode | Iterable[int]",
unchecked: bool = False,
**kwargs: "int | bytes | str | Opcode | Bytecode",
) -> Bytecode:
"""
Make all opcode instances callable to return formatted bytecode, which constitutes a data
portion, that is located after the opcode byte, and pre-opcode bytecode, which is normally
used to set up the stack.
This useful to automatically format, e.g., call opcodes and their stack arguments as
`Opcodes.CALL(Opcodes.GAS, 0x1234, 0x0, 0x0, 0x0, 0x0, 0x0)`.
Data sign is automatically detected but for this reason the range of the input must be:
`[-2^(data_portion_bits-1), 2^(data_portion_bits)]` where: `data_portion_bits ==
data_portion_length * 8`
For the stack, the arguments are set up in the opposite order they are given, so the first
argument is the last item pushed to the stack.
The resulting stack arrangement does not take into account opcode stack element
consumption, so the stack height is not guaranteed to be correct and the user must take
this into consideration.
Integers can also be used as stack elements, in which case they are automatically converted
to PUSH operations, and negative numbers always use a PUSH32 operation.
Hex-strings will be automatically converted to bytes.
"""
args: List["int | bytes | str | Opcode | Bytecode | Iterable[int]"] = list(args_t)
if self.has_data_portion():
if len(args) == 0:
raise ValueError("Opcode with data portion requires at least one argument")
assert type(self) is Opcode
get_item_arg = args.pop()
assert not isinstance(get_item_arg, Bytecode)
return self[get_item_arg](*args)
if self.kwargs is not None and len(kwargs) > 0:
assert len(args) == 0, f"Cannot mix positional and keyword arguments {args} {kwargs}"
for kw in self.kwargs:
args.append(kwargs[kw] if kw in kwargs else self.kwargs_defaults.get(kw, 0))
# The rest of the arguments form the stack.
if len(args) != self.popped_stack_items and not (unchecked or self.unchecked_stack):
raise ValueError(
f"Opcode {self._name_} requires {self.popped_stack_items} stack elements, but "
f"{len(args)} were provided. Use 'unchecked=True' parameter to ignore this check."
)
pre_opcode_bytecode = Bytecode()
while len(args) > 0:
pre_opcode_bytecode += _stack_argument_to_bytecode(args.pop())
return pre_opcode_bytecode + self
def __lt__(self, other: "Opcode") -> bool:
"""Compare two opcodes by their integer value."""
return self.int() < other.int()
def __gt__(self, other: "Opcode") -> bool:
"""Compare two opcodes by their integer value."""
return self.int() > other.int()
def int(self) -> int:
"""Return integer representation of the opcode."""
return int.from_bytes(self, byteorder="big")
def has_data_portion(self) -> bool:
"""Return whether the opcode has a data portion."""
return self.data_portion_length > 0 or self.data_portion_formatter is not None
OpcodeCallArg = int | bytes | str | Bytecode | Iterable[int]
class Macro(Bytecode):
"""Represents opcode macro replacement, basically holds bytes."""
lambda_operation: Callable[..., Bytecode] | None
def __new__(
cls,
macro_or_bytes: Optional["Bytecode | Macro"] = None,
*,
lambda_operation: Callable[..., Bytecode] | None = None,
):
"""Create new opcode macro instance."""
if macro_or_bytes is None:
macro_or_bytes = Bytecode()
if isinstance(macro_or_bytes, Macro):
# Required because Enum class calls the base class with the instantiated object as
# parameter.
return macro_or_bytes
else:
instance = super().__new__(cls, macro_or_bytes)
instance.lambda_operation = lambda_operation
return instance
def __call__(self, *args_t: OpcodeCallArg) -> Bytecode:
"""Perform macro operation if any. Otherwise is a no-op."""
if self.lambda_operation is not None:
return self.lambda_operation(*args_t)
pre_opcode_bytecode = Bytecode()
for arg in args_t:
pre_opcode_bytecode += _stack_argument_to_bytecode(arg)
return pre_opcode_bytecode + self
# Constants
RJUMPV_MAX_INDEX_BYTE_LENGTH = 1
RJUMPV_BRANCH_OFFSET_BYTE_LENGTH = 2
# TODO: Allowing Iterable here is a hacky way to support `range`, because Python 3.11+ will allow
# `Op.RJUMPV[*range(5)]`. This is a temporary solution until Python 3.11+ is the minimum required
# version.
def _rjumpv_encoder(*args: int | bytes | Iterable[int]) -> bytes:
if len(args) == 1:
if isinstance(args[0], bytes) or isinstance(args[0], SupportsBytes):
return bytes(args[0])
elif isinstance(args[0], Iterable):
int_args = list(args[0])
return b"".join(
[(len(int_args) - 1).to_bytes(RJUMPV_MAX_INDEX_BYTE_LENGTH, "big")]
+ [
i.to_bytes(RJUMPV_BRANCH_OFFSET_BYTE_LENGTH, "big", signed=True)
for i in int_args
]
)
return b"".join(
[(len(args) - 1).to_bytes(RJUMPV_MAX_INDEX_BYTE_LENGTH, "big")]
+ [
i.to_bytes(RJUMPV_BRANCH_OFFSET_BYTE_LENGTH, "big", signed=True)
for i in args
if isinstance(i, int)
]
)
def _exchange_encoder(*args: int) -> bytes:
assert 1 <= len(args) <= 2, f"Exchange opcode requires one or two arguments, got {len(args)}"
if len(args) == 1:
return int.to_bytes(args[0], 1, "big")
# n = imm >> 4 + 1
# m = imm & 0xF + 1
# x = n + 1
# y = n + m + 1
# ...
# n = x - 1
# m = y - x
# m = y - n - 1
x, y = args
assert 2 <= x <= 0x11
assert x + 1 <= y <= x + 0x10
n = x - 1
m = y - x
imm = (n - 1) << 4 | m - 1
return int.to_bytes(imm, 1, "big")
def _swapn_stack_properties_modifier(data: bytes) -> tuple[int, int, int, int]:
imm = int.from_bytes(data, "big")
n = imm + 1
min_stack_height = n + 1
return 0, 0, min_stack_height, min_stack_height
def _dupn_stack_properties_modifier(data: bytes) -> tuple[int, int, int, int]:
imm = int.from_bytes(data, "big")
n = imm + 1
min_stack_height = n
return 0, 1, min_stack_height, min_stack_height + 1
def _exchange_stack_properties_modifier(data: bytes) -> tuple[int, int, int, int]:
imm = int.from_bytes(data, "big")
n = (imm >> 4) + 1
m = (imm & 0x0F) + 1
min_stack_height = n + m + 1
return 0, 0, min_stack_height, min_stack_height
class Opcodes(Opcode, Enum):
"""
Enum containing all known opcodes.
Contains deprecated and not yet implemented opcodes.
This enum is !! NOT !! meant to be iterated over by the tests. Instead, create a list with
cherry-picked opcodes from this Enum within the test if iteration is needed.
Do !! NOT !! remove or modify existing opcodes from this list.
"""
STOP = Opcode(0x00, terminating=True)
"""
STOP()
----
Description
----
Stop execution
Inputs
----
- None
Outputs
----
- None
Fork
----
Frontier
Gas
----
0
Source: [evm.codes/#00](https://www.evm.codes/#00)
"""
ADD = Opcode(0x01, popped_stack_items=2, pushed_stack_items=1)
"""
ADD(a, b) = c
----
Description
----
Addition operation
Inputs
----
- a: first integer value to add
- b: second integer value to add
Outputs
----
- c: integer result of the addition modulo 2**256
Fork
----
Frontier
Gas
----
3
Source: [evm.codes/#01](https://www.evm.codes/#01)
"""
MUL = Opcode(0x02, popped_stack_items=2, pushed_stack_items=1)
"""
MUL(a, b) = c
----
Description
----
Multiplication operation
Inputs
----
- a: first integer value to multiply
- b: second integer value to multiply
Outputs
----
- c: integer result of the multiplication modulo 2**256
Fork
----
Frontier
Gas
----
5
Source: [evm.codes/#02](https://www.evm.codes/#02)
"""
SUB = Opcode(0x03, popped_stack_items=2, pushed_stack_items=1)
"""
SUB(a, b) = c
----
Description
----
Subtraction operation
Inputs
----
- a: first integer value
- b: second integer value
Outputs
----
- c: integer result of the subtraction modulo 2**256
Fork
----
Frontier
Gas
----
3
Source: [evm.codes/#03](https://www.evm.codes/#03)
"""
DIV = Opcode(0x04, popped_stack_items=2, pushed_stack_items=1)
"""
DIV(a, b) = c
----
Description
----
Division operation
Inputs
----
- a: numerator
- b: denominator (must be non-zero)
Outputs
----
- c: integer result of the division
Fork
----
Frontier
Gas
----
5
Source: [evm.codes/#04](https://www.evm.codes/#04)
"""
SDIV = Opcode(0x05, popped_stack_items=2, pushed_stack_items=1)
"""
SDIV(a, b) = c
----
Description
----
Signed division operation
Inputs
----
- a: signed numerator
- b: signed denominator
Outputs
----
- c: signed integer result of the division. If the denominator is 0, the result will be 0
----
Fork
----
Frontier
Gas
----
5
Source: [evm.codes/#05](https://www.evm.codes/#05)
"""
MOD = Opcode(0x06, popped_stack_items=2, pushed_stack_items=1)
"""
MOD(a, b) = c
----
Description
----
Modulo operation
Inputs
----
- a: integer numerator
- b: integer denominator
Outputs
----
- a % b: integer result of the integer modulo. If the denominator is 0, the result will be 0
Fork
----
Frontier
Gas
----
5
Source: [evm.codes/#06](https://www.evm.codes/#06)
"""
SMOD = Opcode(0x07, popped_stack_items=2, pushed_stack_items=1)
"""
SMOD(a, b) = c
----
Description
----
Signed modulo remainder operation
Inputs
----
- a: integer numerator
- b: integer denominator
Outputs
----
- a % b: integer result of the signed integer modulo. If the denominator is 0, the result will
be 0
Fork
----
Frontier
Gas
----
5
Source: [evm.codes/#07](https://www.evm.codes/#07)
"""
ADDMOD = Opcode(0x08, popped_stack_items=3, pushed_stack_items=1)
"""
ADDMOD(a, b, c) = d
----
Description
----
Modular addition operation with overflow check
Inputs
----
- a: first integer value
- b: second integer value
- c: integer denominator
Outputs
----
- (a + b) % N: integer result of the addition followed by a modulo. If the denominator is 0,
the result will be 0
Fork
----
Frontier
Gas
----
8
Source: [evm.codes/#08](https://www.evm.codes/#08)
"""
MULMOD = Opcode(0x09, popped_stack_items=3, pushed_stack_items=1)
"""
MULMOD(a, b, N) = d
----
Description
----
Modulo multiplication operation
Inputs
----
- a: first integer value to multiply
- b: second integer value to multiply
- N: integer denominator
Outputs
----
- (a * b) % N: integer result of the multiplication followed by a modulo. If the denominator
is 0, the result will be 0
Fork
----
Frontier
Gas
----
8
Source: [evm.codes/#09](https://www.evm.codes/#09)
"""
EXP = Opcode(0x0A, popped_stack_items=2, pushed_stack_items=1)
"""
EXP(a, exponent) = a ** exponent
----
Description
----
Exponential operation
Inputs
----
- a: integer base
- exponent: integer exponent
Outputs
----
- a ** exponent: integer result of the exponential operation modulo 2**256
Fork
----
Frontier
Gas
----
- static_gas = 10
- dynamic_gas = 50 * exponent_byte_size
Source: [evm.codes/#0A](https://www.evm.codes/#0A)
"""
SIGNEXTEND = Opcode(0x0B, popped_stack_items=2, pushed_stack_items=1)
"""
SIGNEXTEND(b, x) = y
----
Description
----
Sign extension operation
Inputs
----
- b: size in byte - 1 of the integer to sign extend
- x: integer value to sign extend
Outputs
----
- y: integer result of the sign extend
Fork
----
Frontier
Gas
----
5
Source: [evm.codes/#0B](https://www.evm.codes/#0B)
"""
LT = Opcode(0x10, popped_stack_items=2, pushed_stack_items=1)
"""
LT(a, b) = a < b
----
Description
----
Less-than comparison
Inputs
----
- a: left side integer value
- b: right side integer value
Outputs
----
- a < b: 1 if the left side is smaller, 0 otherwise
Fork
----
Frontier
Gas
----
3
Source: [evm.codes/#10](https://www.evm.codes/#10)
"""
GT = Opcode(0x11, popped_stack_items=2, pushed_stack_items=1)
"""
GT(a, b) = a > b
----
Description
----
Greater-than comparison
Inputs
----
- a: left side integer
- b: right side integer
Outputs
----
- a > b: 1 if the left side is bigger, 0 otherwise
Fork
----
Frontier
Gas
----
3
Source: [evm.codes/#11](https://www.evm.codes/#11)
"""
SLT = Opcode(0x12, popped_stack_items=2, pushed_stack_items=1)
"""
SLT(a, b) = a < b
----
Description
----
Signed less-than comparison
Inputs
----
- a: left side signed integer
- b: right side signed integer
Outputs
----
- a < b: 1 if the left side is smaller, 0 otherwise
Fork
----
Frontier
Gas
----
3
Source: [evm.codes/#12](https://www.evm.codes/#12)
"""
SGT = Opcode(0x13, popped_stack_items=2, pushed_stack_items=1)
"""
SGT(a, b) = a > b
----
Description
----
Signed greater-than comparison
Inputs
----
- a: left side signed integer
- b: right side signed integer
Outputs
----
- a > b: 1 if the left side is bigger, 0 otherwise
Fork
----
Frontier
Gas
----
3
Source: [evm.codes/#13](https://www.evm.codes/#13)
"""
EQ = Opcode(0x14, popped_stack_items=2, pushed_stack_items=1)
"""
EQ(a, b) = a == b
----
Description
----
Equality comparison
Inputs
----
- a: left side integer
- b: right side integer
Outputs
----
- a == b: 1 if the left side is equal to the right side, 0 otherwise
Fork
----
Frontier
Gas
----
3
Source: [evm.codes/#14](https://www.evm.codes/#14)
"""
ISZERO = Opcode(0x15, popped_stack_items=1, pushed_stack_items=1)
"""
ISZERO(a) = a == 0
----
Description
----
Is-zero comparison
Inputs
----
- a: integer
Outputs
----
- a == 0: 1 if a is 0, 0 otherwise
Fork
----
Frontier
Gas
----
3
Source: [evm.codes/#15](https://www.evm.codes/#15)
"""
AND = Opcode(0x16, popped_stack_items=2, pushed_stack_items=1)
"""
AND(a, b) = a & b
----
Description
----
Bitwise AND operation
Inputs
----
- a: first binary value
- b: second binary value
Outputs
----
- a & b: the bitwise AND result
Fork
----
Frontier
Gas
----
3
Source: [evm.codes/#16](https://www.evm.codes/#16)
"""
OR = Opcode(0x17, popped_stack_items=2, pushed_stack_items=1)
"""
OR(a, b) = a | b
----
Description
----
Bitwise OR operation
Inputs
----
- a: first binary value
- b: second binary value
Outputs
----
- a | b: the bitwise OR result
Fork
----
Frontier
Gas
----
3
Source: [evm.codes/#17](https://www.evm.codes/#17)
"""
XOR = Opcode(0x18, popped_stack_items=2, pushed_stack_items=1)