ethereum.tangerine_whistle.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 += 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:
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 += 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:
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 += 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:
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 += 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:
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 >= U256(32):
142
        result = U256(0)
143
    else:
144
        extra_bytes_to_right = U256(31) - byte_index
145
        # Remove the extra bytes in the right
146
        word = word >> (extra_bytes_to_right * U256(8))
147
        # Remove the extra bytes in the left
148
        word = word & U256(0xFF)
149
        result = word
150
151
    push(evm.stack, result)
152
153
    # PROGRAM COUNTER
154
    evm.pc += Uint(1)