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