ethereum.forks.arrow_glacier.vm.instructions.bitwiseethereum.forks.gray_glacier.vm.instructions.bitwise
Ethereum Virtual Machine (EVM) Bitwise Instructions.
.. contents:: Table of Contents :backlinks: none :local:
Introduction
Implementations of the EVM bitwise instructions.
bitwise_and
Bitwise AND operation of the top 2 elements of the stack. Pushes the result back on the stack.
Parameters
evm : The current EVM frame.
def bitwise_and(evm: Evm) -> None:
22 | """ |
---|---|
23 | Bitwise AND operation of the top 2 elements of the stack. Pushes the |
24 | result back on the stack. |
25 |
|
26 | Parameters |
27 | ---------- |
28 | evm : |
29 | The current EVM frame. |
30 |
|
31 | """ |
32 | # STACK |
33 | x = pop(evm.stack) |
34 | y = pop(evm.stack) |
35 | |
36 | # GAS |
37 | charge_gas(evm, GAS_VERY_LOW) |
38 | |
39 | # OPERATION |
40 | push(evm.stack, x & y) |
41 | |
42 | # PROGRAM COUNTER |
43 | evm.pc += Uint(1) |
bitwise_or
Bitwise OR operation of the top 2 elements of the stack. Pushes the result back on the stack.
Parameters
evm : The current EVM frame.
def bitwise_or(evm: Evm) -> None:
47 | """ |
---|---|
48 | Bitwise OR operation of the top 2 elements of the stack. Pushes the |
49 | result back on the stack. |
50 |
|
51 | Parameters |
52 | ---------- |
53 | evm : |
54 | The current EVM frame. |
55 |
|
56 | """ |
57 | # STACK |
58 | x = pop(evm.stack) |
59 | y = pop(evm.stack) |
60 | |
61 | # GAS |
62 | charge_gas(evm, GAS_VERY_LOW) |
63 | |
64 | # OPERATION |
65 | push(evm.stack, x | y) |
66 | |
67 | # PROGRAM COUNTER |
68 | evm.pc += Uint(1) |
bitwise_xor
Bitwise XOR operation of the top 2 elements of the stack. Pushes the result back on the stack.
Parameters
evm : The current EVM frame.
def bitwise_xor(evm: Evm) -> None:
72 | """ |
---|---|
73 | Bitwise XOR operation of the top 2 elements of the stack. Pushes the |
74 | result back on the stack. |
75 |
|
76 | Parameters |
77 | ---------- |
78 | evm : |
79 | The current EVM frame. |
80 |
|
81 | """ |
82 | # STACK |
83 | x = pop(evm.stack) |
84 | y = pop(evm.stack) |
85 | |
86 | # GAS |
87 | charge_gas(evm, GAS_VERY_LOW) |
88 | |
89 | # OPERATION |
90 | push(evm.stack, x ^ y) |
91 | |
92 | # PROGRAM COUNTER |
93 | evm.pc += Uint(1) |
bitwise_not
Bitwise NOT operation of the top element of the stack. Pushes the result back on the stack.
Parameters
evm : The current EVM frame.
def bitwise_not(evm: Evm) -> None:
97 | """ |
---|---|
98 | Bitwise NOT operation of the top element of the stack. Pushes the |
99 | result back on the stack. |
100 |
|
101 | Parameters |
102 | ---------- |
103 | evm : |
104 | The current EVM frame. |
105 |
|
106 | """ |
107 | # STACK |
108 | x = pop(evm.stack) |
109 | |
110 | # GAS |
111 | charge_gas(evm, GAS_VERY_LOW) |
112 | |
113 | # OPERATION |
114 | push(evm.stack, ~x) |
115 | |
116 | # PROGRAM COUNTER |
117 | evm.pc += Uint(1) |
get_byte
For a word (defined by next top element of the stack), retrieve the Nth byte (0-indexed and defined by top element of stack) from the left (most significant) to right (least significant).
Parameters
evm : The current EVM frame.
def get_byte(evm: Evm) -> None:
121 | """ |
---|---|
122 | For a word (defined by next top element of the stack), retrieve the |
123 | Nth byte (0-indexed and defined by top element of stack) from the |
124 | left (most significant) to right (least significant). |
125 |
|
126 | Parameters |
127 | ---------- |
128 | evm : |
129 | The current EVM frame. |
130 |
|
131 | """ |
132 | # STACK |
133 | byte_index = pop(evm.stack) |
134 | word = pop(evm.stack) |
135 | |
136 | # GAS |
137 | charge_gas(evm, GAS_VERY_LOW) |
138 | |
139 | # OPERATION |
140 | if byte_index >= U256(32): |
141 | result = U256(0) |
142 | else: |
143 | extra_bytes_to_right = U256(31) - byte_index |
144 | # Remove the extra bytes in the right |
145 | word = word >> (extra_bytes_to_right * U256(8)) |
146 | # Remove the extra bytes in the left |
147 | word = word & U256(0xFF) |
148 | result = word |
149 | |
150 | push(evm.stack, result) |
151 | |
152 | # PROGRAM COUNTER |
153 | evm.pc += Uint(1) |
bitwise_shl
Logical shift left (SHL) operation of the top 2 elements of the stack. Pushes the result back on the stack.
Parameters
evm : The current EVM frame.
def bitwise_shl(evm: Evm) -> None:
157 | """ |
---|---|
158 | Logical shift left (SHL) operation of the top 2 elements of the stack. |
159 | Pushes the result back on the stack. |
160 |
|
161 | Parameters |
162 | ---------- |
163 | evm : |
164 | The current EVM frame. |
165 |
|
166 | """ |
167 | # STACK |
168 | shift = Uint(pop(evm.stack)) |
169 | value = Uint(pop(evm.stack)) |
170 | |
171 | # GAS |
172 | charge_gas(evm, GAS_VERY_LOW) |
173 | |
174 | # OPERATION |
175 | if shift < Uint(256): |
176 | result = U256((value << shift) & Uint(U256.MAX_VALUE)) |
177 | else: |
178 | result = U256(0) |
179 | |
180 | push(evm.stack, result) |
181 | |
182 | # PROGRAM COUNTER |
183 | evm.pc += Uint(1) |
bitwise_shr
Logical shift right (SHR) operation of the top 2 elements of the stack. Pushes the result back on the stack.
Parameters
evm : The current EVM frame.
def bitwise_shr(evm: Evm) -> None:
187 | """ |
---|---|
188 | Logical shift right (SHR) operation of the top 2 elements of the stack. |
189 | Pushes the result back on the stack. |
190 |
|
191 | Parameters |
192 | ---------- |
193 | evm : |
194 | The current EVM frame. |
195 |
|
196 | """ |
197 | # STACK |
198 | shift = pop(evm.stack) |
199 | value = pop(evm.stack) |
200 | |
201 | # GAS |
202 | charge_gas(evm, GAS_VERY_LOW) |
203 | |
204 | # OPERATION |
205 | if shift < U256(256): |
206 | result = value >> shift |
207 | else: |
208 | result = U256(0) |
209 | |
210 | push(evm.stack, result) |
211 | |
212 | # PROGRAM COUNTER |
213 | evm.pc += Uint(1) |
bitwise_sar
Arithmetic shift right (SAR) operation of the top 2 elements of the stack. Pushes the result back on the stack.
Parameters
evm : The current EVM frame.
def bitwise_sar(evm: Evm) -> None:
217 | """ |
---|---|
218 | Arithmetic shift right (SAR) operation of the top 2 elements of the stack. |
219 | Pushes the result back on the stack. |
220 |
|
221 | Parameters |
222 | ---------- |
223 | evm : |
224 | The current EVM frame. |
225 |
|
226 | """ |
227 | # STACK |
228 | shift = int(pop(evm.stack)) |
229 | signed_value = pop(evm.stack).to_signed() |
230 | |
231 | # GAS |
232 | charge_gas(evm, GAS_VERY_LOW) |
233 | |
234 | # OPERATION |
235 | if shift < 256: |
236 | result = U256.from_signed(signed_value >> shift) |
237 | elif signed_value >= 0: |
238 | result = U256(0) |
239 | else: |
240 | result = U256.MAX_VALUE |
241 | |
242 | push(evm.stack, result) |
243 | |
244 | # PROGRAM COUNTER |
245 | evm.pc += Uint(1) |