ethereum.forks.istanbul.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:
| 25 | """ |
|---|---|
| 26 | Bitwise AND operation of the top 2 elements of the stack. Pushes the |
| 27 | result back on the stack. |
| 28 | |
| 29 | Parameters |
| 30 | ---------- |
| 31 | evm : |
| 32 | The current EVM frame. |
| 33 | |
| 34 | """ |
| 35 | # STACK |
| 36 | x = pop(evm.stack) |
| 37 | y = pop(evm.stack) |
| 38 | |
| 39 | # GAS |
| 40 | charge_gas(evm, GasCosts.OPCODE_AND) |
| 41 | |
| 42 | # OPERATION |
| 43 | push(evm.stack, x & y) |
| 44 | |
| 45 | # PROGRAM COUNTER |
| 46 | 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:
| 50 | """ |
|---|---|
| 51 | Bitwise OR operation of the top 2 elements of the stack. Pushes the |
| 52 | result back on the stack. |
| 53 | |
| 54 | Parameters |
| 55 | ---------- |
| 56 | evm : |
| 57 | The current EVM frame. |
| 58 | |
| 59 | """ |
| 60 | # STACK |
| 61 | x = pop(evm.stack) |
| 62 | y = pop(evm.stack) |
| 63 | |
| 64 | # GAS |
| 65 | charge_gas(evm, GasCosts.OPCODE_OR) |
| 66 | |
| 67 | # OPERATION |
| 68 | push(evm.stack, x | y) |
| 69 | |
| 70 | # PROGRAM COUNTER |
| 71 | 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:
| 75 | """ |
|---|---|
| 76 | Bitwise XOR operation of the top 2 elements of the stack. Pushes the |
| 77 | result back on the stack. |
| 78 | |
| 79 | Parameters |
| 80 | ---------- |
| 81 | evm : |
| 82 | The current EVM frame. |
| 83 | |
| 84 | """ |
| 85 | # STACK |
| 86 | x = pop(evm.stack) |
| 87 | y = pop(evm.stack) |
| 88 | |
| 89 | # GAS |
| 90 | charge_gas(evm, GasCosts.OPCODE_XOR) |
| 91 | |
| 92 | # OPERATION |
| 93 | push(evm.stack, x ^ y) |
| 94 | |
| 95 | # PROGRAM COUNTER |
| 96 | 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:
| 100 | """ |
|---|---|
| 101 | Bitwise NOT operation of the top element of the stack. Pushes the |
| 102 | result back on the stack. |
| 103 | |
| 104 | Parameters |
| 105 | ---------- |
| 106 | evm : |
| 107 | The current EVM frame. |
| 108 | |
| 109 | """ |
| 110 | # STACK |
| 111 | x = pop(evm.stack) |
| 112 | |
| 113 | # GAS |
| 114 | charge_gas(evm, GasCosts.OPCODE_NOT) |
| 115 | |
| 116 | # OPERATION |
| 117 | push(evm.stack, ~x) |
| 118 | |
| 119 | # PROGRAM COUNTER |
| 120 | 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:
| 124 | """ |
|---|---|
| 125 | For a word (defined by next top element of the stack), retrieve the |
| 126 | Nth byte (0-indexed and defined by top element of stack) from the |
| 127 | left (most significant) to right (least significant). |
| 128 | |
| 129 | Parameters |
| 130 | ---------- |
| 131 | evm : |
| 132 | The current EVM frame. |
| 133 | |
| 134 | """ |
| 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 | """ |
|---|---|
| 161 | Logical shift left (SHL) operation of the top 2 elements of the stack. |
| 162 | Pushes the result back on the stack. |
| 163 | |
| 164 | Parameters |
| 165 | ---------- |
| 166 | evm : |
| 167 | The current EVM frame. |
| 168 | |
| 169 | """ |
| 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 | """ |
|---|---|
| 191 | Logical shift right (SHR) operation of the top 2 elements of the stack. |
| 192 | Pushes the result back on the stack. |
| 193 | |
| 194 | Parameters |
| 195 | ---------- |
| 196 | evm : |
| 197 | The current EVM frame. |
| 198 | |
| 199 | """ |
| 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 | """ |
|---|---|
| 221 | Arithmetic shift right (SAR) operation of the top 2 elements of the stack. |
| 222 | Pushes the result back on the stack. |
| 223 | |
| 224 | Parameters |
| 225 | ---------- |
| 226 | evm : |
| 227 | The current EVM frame. |
| 228 | |
| 229 | """ |
| 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) |