ethereum.forks.bpo1.vm.instructions.bitwiseethereum.forks.bpo2.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.
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.
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.
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.
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 | <snip> |
|---|---|
| 132 | # STACK |
| 133 | byte_index = pop(evm.stack) |
| 134 | word = pop(evm.stack) |
| 135 | |
| 136 | # GAS |
| 137 | charge_gas(evm, GasCosts.OPCODE_BYTE) |
| 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 | <snip> |
|---|---|
| 167 | # STACK |
| 168 | shift = Uint(pop(evm.stack)) |
| 169 | value = Uint(pop(evm.stack)) |
| 170 | |
| 171 | # GAS |
| 172 | charge_gas(evm, GasCosts.OPCODE_SHL) |
| 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 | <snip> |
|---|---|
| 197 | # STACK |
| 198 | shift = pop(evm.stack) |
| 199 | value = pop(evm.stack) |
| 200 | |
| 201 | # GAS |
| 202 | charge_gas(evm, GasCosts.OPCODE_SHR) |
| 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 | <snip> |
|---|---|
| 227 | # STACK |
| 228 | shift = int(pop(evm.stack)) |
| 229 | signed_value = pop(evm.stack).to_signed() |
| 230 | |
| 231 | # GAS |
| 232 | charge_gas(evm, GasCosts.OPCODE_SAR) |
| 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) |
count_leading_zeros ¶
Count the number of leading zero bits in a 256-bit word.
Pops one value from the stack and pushes the number of leading zero bits. If the input is zero, pushes 256.
Parameters
evm : The current EVM frame.
def count_leading_zeros(evm: Evm) -> None:
| 249 | <snip> |
|---|---|
| 261 | # STACK |
| 262 | x = pop(evm.stack) |
| 263 | |
| 264 | # GAS |
| 265 | charge_gas(evm, GasCosts.OPCODE_CLZ) |
| 266 | |
| 267 | # OPERATION |
| 268 | bit_length = U256(x.bit_length()) |
| 269 | result = U256(256) - bit_length |
| 270 | |
| 271 | push(evm.stack, result) |
| 272 | |
| 273 | # PROGRAM COUNTER |
| 274 | evm.pc += Uint(1) |