ethereum.forks.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:
22
    """
23
    Bitwise AND operation of the top 2 elements of the stack. Pushes the
24
    result back on the stack.
25
26
    Parameters
27
    ----------
28
    evm :
29
        The current EVM frame.
30
31
    """
32
    # STACK
33
    x = pop(evm.stack)
34
    y = pop(evm.stack)
35
36
    # GAS
37
    charge_gas(evm, GAS_VERY_LOW)
38
39
    # OPERATION
40
    push(evm.stack, x & y)
41
42
    # PROGRAM COUNTER
43
    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:
47
    """
48
    Bitwise OR operation of the top 2 elements of the stack. Pushes the
49
    result back on the stack.
50
51
    Parameters
52
    ----------
53
    evm :
54
        The current EVM frame.
55
56
    """
57
    # STACK
58
    x = pop(evm.stack)
59
    y = pop(evm.stack)
60
61
    # GAS
62
    charge_gas(evm, GAS_VERY_LOW)
63
64
    # OPERATION
65
    push(evm.stack, x | y)
66
67
    # PROGRAM COUNTER
68
    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:
72
    """
73
    Bitwise XOR operation of the top 2 elements of the stack. Pushes the
74
    result back on the stack.
75
76
    Parameters
77
    ----------
78
    evm :
79
        The current EVM frame.
80
81
    """
82
    # STACK
83
    x = pop(evm.stack)
84
    y = pop(evm.stack)
85
86
    # GAS
87
    charge_gas(evm, GAS_VERY_LOW)
88
89
    # OPERATION
90
    push(evm.stack, x ^ y)
91
92
    # PROGRAM COUNTER
93
    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:
97
    """
98
    Bitwise NOT operation of the top element of the stack. Pushes the
99
    result back on the stack.
100
101
    Parameters
102
    ----------
103
    evm :
104
        The current EVM frame.
105
106
    """
107
    # STACK
108
    x = pop(evm.stack)
109
110
    # GAS
111
    charge_gas(evm, GAS_VERY_LOW)
112
113
    # OPERATION
114
    push(evm.stack, ~x)
115
116
    # PROGRAM COUNTER
117
    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:
121
    """
122
    For a word (defined by next top element of the stack), retrieve the
123
    Nth byte (0-indexed and defined by top element of stack) from the
124
    left (most significant) to right (least significant).
125
126
    Parameters
127
    ----------
128
    evm :
129
        The current EVM frame.
130
131
    """
132
    # STACK
133
    byte_index = pop(evm.stack)
134
    word = pop(evm.stack)
135
136
    # GAS
137
    charge_gas(evm, GAS_VERY_LOW)
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)