ethereum.forks.cancun.vm.instructions.bitwiseethereum.forks.prague.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:
| 124 | <snip> |
|---|---|
| 135 | # STACK |
| 136 | byte_index = pop(evm.stack) |
| 137 | word = pop(evm.stack) |
| 138 | |
| 139 | # GAS |
| 140 | charge_gas(evm, GasCosts.OPCODE_BYTE) |
| 141 | |
| 142 | # OPERATION |
| 143 | if byte_index >= U256(32): |
| 144 | result = U256(0) |
| 145 | else: |
| 146 | extra_bytes_to_right = U256(31) - byte_index |
| 147 | # Remove the extra bytes in the right |
| 148 | word = word >> (extra_bytes_to_right * U256(8)) |
| 149 | # Remove the extra bytes in the left |
| 150 | word = word & U256(0xFF) |
| 151 | result = word |
| 152 | |
| 153 | push(evm.stack, result) |
| 154 | |
| 155 | # PROGRAM COUNTER |
| 156 | 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:
| 160 | <snip> |
|---|---|
| 170 | # STACK |
| 171 | shift = Uint(pop(evm.stack)) |
| 172 | value = Uint(pop(evm.stack)) |
| 173 | |
| 174 | # GAS |
| 175 | charge_gas(evm, GasCosts.OPCODE_SHL) |
| 176 | |
| 177 | # OPERATION |
| 178 | if shift < Uint(256): |
| 179 | result = U256((value << shift) & Uint(U256.MAX_VALUE)) |
| 180 | else: |
| 181 | result = U256(0) |
| 182 | |
| 183 | push(evm.stack, result) |
| 184 | |
| 185 | # PROGRAM COUNTER |
| 186 | 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:
| 190 | <snip> |
|---|---|
| 200 | # STACK |
| 201 | shift = pop(evm.stack) |
| 202 | value = pop(evm.stack) |
| 203 | |
| 204 | # GAS |
| 205 | charge_gas(evm, GasCosts.OPCODE_SHR) |
| 206 | |
| 207 | # OPERATION |
| 208 | if shift < U256(256): |
| 209 | result = value >> shift |
| 210 | else: |
| 211 | result = U256(0) |
| 212 | |
| 213 | push(evm.stack, result) |
| 214 | |
| 215 | # PROGRAM COUNTER |
| 216 | 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:
| 220 | <snip> |
|---|---|
| 230 | # STACK |
| 231 | shift = int(pop(evm.stack)) |
| 232 | signed_value = pop(evm.stack).to_signed() |
| 233 | |
| 234 | # GAS |
| 235 | charge_gas(evm, GasCosts.OPCODE_SAR) |
| 236 | |
| 237 | # OPERATION |
| 238 | if shift < 256: |
| 239 | result = U256.from_signed(signed_value >> shift) |
| 240 | elif signed_value >= 0: |
| 241 | result = U256(0) |
| 242 | else: |
| 243 | result = U256.MAX_VALUE |
| 244 | |
| 245 | push(evm.stack, result) |
| 246 | |
| 247 | # PROGRAM COUNTER |
| 248 | evm.pc += Uint(1) |