-
Notifications
You must be signed in to change notification settings - Fork 3
/
evm_table.py
305 lines (277 loc) · 14.2 KB
/
evm_table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import random
from web3 import Web3
opcodes = {
# hex, opcode, pushed_bytes, stack_input, stack_output, min_gas
# 0x0: ("STOP", 0, 0, 0, 0, "Halts execution."),
0x1: ("ADD", 0, 2, 1, 3, "Addition operation."),
0x2: ("MUL", 0, 2, 1, 5, "Multiplication operation."),
0x3: ("SUB", 0, 2, 1, 3, "Subtraction operation."),
0x4: ("DIV", 0, 2, 1, 5, "Integer division operation."),
0x5: ("SDIV", 0, 2, 1, 5, "Signed integer division operation(truncated)."),
0x6: ("MOD", 0, 2, 1, 5, "Modulo remainder operation."),
0x7: ("SMOD", 0, 2, 1, 5, "Signed modulo remainder operation."),
0x8: ("ADDMOD", 0, 3, 1, 8, "Modulo addition operation."),
0x9: ("MULMOD", 0, 3, 1, 8, "Modulo multiplication operation."),
0xA: ("EXP", 0, 2, 1, 10, "Exponential operation."),
0xB: ("SIGNEXTEND", 0, 2, 1, 5,
"Extend length of two's complement signed integer."),
0x10: ("LT", 0, 2, 1, 3, "Less-than comparison."),
0x11: ("GT", 0, 2, 1, 3, "Greater-than comparison."),
0x12: ("SLT", 0, 2, 1, 3, "Signed less-than comparison."),
0x13: ("SGT", 0, 2, 1, 3, "Signed greater-than comparison."),
0x14: ("EQ", 0, 2, 1, 3, "Simple not operator."),
0x15: ("ISZERO", 0, 1, 1, 3, "Equals zero comparison."),
0x16: ("AND", 0, 2, 1, 3, "Bitwise AND operation."),
0x17: ("OR", 0, 2, 1, 3, "Bitwise OR operation."),
0x18: ("XOR", 0, 2, 1, 3, "Bitwise XOR operation."),
0x19: ("NOT", 0, 1, 1, 3, "Bitwise NOT operation."),
0x1A: ("BYTE", 0, 2, 1, 3, "Retrieve single byte from word."),
0x1B: ("SHL", 0, 2, 1, 3, "Logical shift left."),
0x1C: ("SHR", 0, 2, 1, 3, "Logical shift right."),
0x1D: ("SAR", 0, 2, 1, 3, "Arithmetic shift right."),
0x20: ("SHA3", 0, 2, 1, 30, "Compute Keccak-256 hash."),
0x30: ("ADDRESS", 0, 0, 1, 2, "Get addr of currently executing account"),
0x31: ("BALANCE", 0, 1, 1, 20, "Get balance of the given account."),
0x32: ("ORIGIN", 0, 0, 1, 2, "Get execution origination address."),
0x33: ("CALLER", 0, 0, 1, 2, "Get caller address."),
0x34: ("CALLVALUE", 0, 0, 1, 2,
"Get deposited value by the instruction/transaction responsible " \
"for this execution."),
0x35: ("CALLDATALOAD", 0, 1, 1, 3, "Get input data of current environmnt"),
0x36: ("CALLDATASIZE", 0, 0, 1, 2,
"Get size of input data in current environment."),
0x37: ("CALLDATACOPY", 0, 3, 0, 3,
"Copy input data in current environment to memory."),
0x38: ("CODESIZE", 0, 0, 1, 2,
"Get size of code running in current environment."),
0x39: ("CODECOPY", 0, 3, 0, 3,
"Copy code running in current environment to memory."),
0x3A: ("GASPRICE", 0, 0, 1, 2, "Get price of gas in current environment."),
0x3B: ("EXTCODESIZE", 0, 1, 1, 20, "Get size of an account's code."),
0x3C: ("EXTCODECOPY", 0, 4, 0, 20, "Copy an account's code to memory."),
0x3D: ("RETURNDATASIZE", 0, 0, 1, 2,
"Get size of output data from the previous call."),
0x3E: ("RETURNDATACOPY", 0, 3, 0, 3,
"Copy output data from the previous call to memory."),
0x3F: ("EXTCODEHASH", 0, 1, 1, 100, "Get hash of an account's code."),
0x40: ("BLOCKHASH", 0, 1, 1, 20,
"Get the hash of one of the 256 most recent complete blocks."),
0x41: ("COINBASE", 0, 0, 1, 2, "Get the block's beneficiary address."),
0x42: ("TIMESTAMP", 0, 0, 1, 2, "Get the block's timestamp."),
0x43: ("NUMBER", 0, 0, 1, 2, "Get the block's number."),
0x44: ("DIFFICULTY", 0, 0, 1, 2, "Get the block's difficulty."),
0x45: ("GASLIMIT", 0, 0, 1, 2, "Get the block's gas limit."),
0x46: ("CHAINID", 0, 0, 1, 2, "Get the chain's id."),
0x47: ("SELFBALANCE", 0, 0, 1, 5, "Get balance of the executing account."),
0x48: ("BASEFEE", 0, 0, 1, 2, "Get the base fee."),
0x49: ("BLOBHASH", 0, 1, 1, 3, ""),
0x4A: ("BLOBBASEFEE", 0, 0, 1, 2, ""),
0x50: ("POP", 0, 1, 0, 2, "Remove item from stack."),
0x51: ("MLOAD", 0, 1, 1, 3, "Load word from memory."),
0x52: ("MSTORE", 0, 2, 0, 3, "Save word to memory."),
0x53: ("MSTORE8", 0, 2, 0, 3, "Save byte to memory."),
0x54: ("SLOAD", 0, 1, 1, 50, "Load word from storage."),
0x55: ("SSTORE", 0, 2, 0, 0, "Save word to storage."),
0x56: ("JUMP", 0, 1, 0, 8, "Alter the program counter."),
0x57: ("JUMPI", 0, 2, 0, 10, "Conditionally alter the program counter."),
0x58: ("GETPC", 0, 0, 1, 2,
"Get the value of the program counter prior to the increment."),
0x59: ("MSIZE", 0, 0, 1, 2, "Get the size of active memory in bytes."),
0x5A: ("GAS", 0, 0, 1, 2,
"Get the amount of available gas, including the corresponding " \
"reduction the amount of available gas."),
0x5B: ("JUMPDEST", 0, 0, 0, 1, "Mark a valid destination for jumps."),
0x5F: ("PUSH", 1, 0, 1, 2, "Place 3-byte item on stack."),
0x60: ("PUSH", 1, 0, 1, 3, "Place 1 byte item on stack."),
0x61: ("PUSH", 2, 0, 1, 3, "Place 2-byte item on stack."),
0x62: ("PUSH", 3, 0, 1, 3, "Place 3-byte item on stack."),
0x63: ("PUSH", 4, 0, 1, 3, "Place 4-byte item on stack."),
0x64: ("PUSH", 5, 0, 1, 3, "Place 5-byte item on stack."),
0x65: ("PUSH", 6, 0, 1, 3, "Place 6-byte item on stack."),
0x66: ("PUSH", 7, 0, 1, 3, "Place 7-byte item on stack."),
0x67: ("PUSH", 8, 0, 1, 3, "Place 8-byte item on stack."),
0x68: ("PUSH", 9, 0, 1, 3, "Place 9-byte item on stack."),
0x69: ("PUSH", 10, 0, 1, 3, "Place 10-byte item on stack."),
0x6A: ("PUSH", 11, 0, 1, 3, "Place 11-byte item on stack."),
0x6B: ("PUSH", 12, 0, 1, 3, "Place 12-byte item on stack."),
0x6C: ("PUSH", 13, 0, 1, 3, "Place 13-byte item on stack."),
0x6D: ("PUSH", 14, 0, 1, 3, "Place 14-byte item on stack."),
0x6E: ("PUSH", 15, 0, 1, 3, "Place 15-byte item on stack."),
0x6F: ("PUSH", 16, 0, 1, 3, "Place 16-byte item on stack."),
0x70: ("PUSH", 17, 0, 1, 3, "Place 17-byte item on stack."),
0x71: ("PUSH", 18, 0, 1, 3, "Place 18-byte item on stack."),
0x72: ("PUSH", 19, 0, 1, 3, "Place 19-byte item on stack."),
0x73: ("PUSH", 20, 0, 1, 3, "Place 20-byte item on stack."),
0x74: ("PUSH", 21, 0, 1, 3, "Place 21-byte item on stack."),
0x75: ("PUSH", 22, 0, 1, 3, "Place 22-byte item on stack."),
0x76: ("PUSH", 23, 0, 1, 3, "Place 23-byte item on stack."),
0x77: ("PUSH", 24, 0, 1, 3, "Place 24-byte item on stack."),
0x78: ("PUSH", 25, 0, 1, 3, "Place 25-byte item on stack."),
0x79: ("PUSH", 26, 0, 1, 3, "Place 26-byte item on stack."),
0x7A: ("PUSH", 27, 0, 1, 3, "Place 27-byte item on stack."),
0x7B: ("PUSH", 28, 0, 1, 3, "Place 28-byte item on stack."),
0x7C: ("PUSH", 29, 0, 1, 3, "Place 29-byte item on stack."),
0x7D: ("PUSH", 30, 0, 1, 3, "Place 30-byte item on stack."),
0x7E: ("PUSH", 31, 0, 1, 3, "Place 31-byte item on stack."),
0x7F: ("PUSH", 32, 0, 1, 3, "Place 32-byte (full word) item on stack."),
0x80: ("DUP", 0, 1, 2, 3, "Duplicate 1st stack item."),
0x81: ("DUP", 0, 2, 3, 3, "Duplicate 2nd stack item."),
0x82: ("DUP", 0, 3, 4, 3, "Duplicate 3rd stack item."),
0x83: ("DUP", 0, 4, 5, 3, "Duplicate 4th stack item."),
0x84: ("DUP", 0, 5, 6, 3, "Duplicate 5th stack item."),
0x85: ("DUP", 0, 6, 7, 3, "Duplicate 6th stack item."),
0x86: ("DUP", 0, 7, 8, 3, "Duplicate 7th stack item."),
0x87: ("DUP", 0, 8, 9, 3, "Duplicate 8th stack item."),
0x88: ("DUP", 0, 9, 10, 3, "Duplicate 9th stack item."),
0x89: ("DUP", 0, 10, 11, 3, "Duplicate 10th stack item."),
0x8A: ("DUP", 0, 11, 12, 3, "Duplicate 11th stack item."),
0x8B: ("DUP", 0, 12, 13, 3, "Duplicate 12th stack item."),
0x8C: ("DUP", 0, 13, 14, 3, "Duplicate 13th stack item."),
0x8D: ("DUP", 0, 14, 15, 3, "Duplicate 14th stack item."),
0x8E: ("DUP", 0, 15, 16, 3, "Duplicate 15th stack item."),
0x8F: ("DUP", 0, 16, 17, 3, "Duplicate 16th stack item."),
0x90: ("SWAP", 0, 2, 2, 3, "Exchange 1st and 2nd stack items."),
0x91: ("SWAP", 0, 3, 3, 3, "Exchange 1st and 3rd stack items."),
0x92: ("SWAP", 0, 4, 4, 3, "Exchange 1st and 4th stack items."),
0x93: ("SWAP", 0, 5, 5, 3, "Exchange 1st and 5th stack items."),
0x94: ("SWAP", 0, 6, 6, 3, "Exchange 1st and 6th stack items."),
0x95: ("SWAP", 0, 7, 7, 3, "Exchange 1st and 7th stack items."),
0x96: ("SWAP", 0, 8, 8, 3, "Exchange 1st and 8th stack items."),
0x97: ("SWAP", 0, 9, 9, 3, "Exchange 1st and 9th stack items."),
0x98: ("SWAP", 0, 10, 10, 3, "Exchange 1st and 10th stack items."),
0x99: ("SWAP", 0, 11, 11, 3, "Exchange 1st and 11th stack items."),
0x9A: ("SWAP", 0, 12, 12, 3, "Exchange 1st and 12th stack items."),
0x9B: ("SWAP", 0, 13, 13, 3, "Exchange 1st and 13th stack items."),
0x9C: ("SWAP", 0, 14, 14, 3, "Exchange 1st and 14th stack items."),
0x9D: ("SWAP", 0, 15, 15, 3, "Exchange 1st and 15th stack items."),
0x9E: ("SWAP", 0, 16, 16, 3, "Exchange 1st and 16th stack items."),
0x9F: ("SWAP", 0, 17, 17, 3, "Exchange 1st and 17th stack items."),
0xA0: ("LOG", 0, 2, 0, 375, "Append log record with no topics."),
0xA1: ("LOG", 0, 3, 0, 750, "Append log record with one topic."),
0xA2: ("LOG", 0, 4, 0, 1125, "Append log record with two topics."),
0xA3: ("LOG", 0, 5, 0, 1500, "Append log record with three topics."),
0xA4: ("LOG", 0, 6, 0, 1875, "Append log record with four topics."),
0xF0: ("CREATE", 0, 3, 1, 32000,
"Create a new account with associated code."),
0xF1: ("CALL", 0, 7, 1, 40, "Message-call into an account."),
0xF2: ("CALLCODE", 0, 7, 1, 40,
"Message-call into this account with alternative account's code."),
0xF3: ("RETURN", 0, 2, 0, 0, "Halt execution returning output data."),
0xF4: ("DELEGATECALL", 0, 6, 1, 100, "Message-call into this account."),
0xF5: ("CREATE2", 0, 4, 1, 32000,
"Create a new account with associated code at a given address."),
0xFA: ("STATICCALL", 0, 6, 1, 100, "Static message-call into an account."),
0xFD: ("REVERT", 0, 2, 0, 0, "Halt execution reverting state changes."),
# 0xFE: ("INVALID", 0, 0, 0, 0, "Designated invalid instruction."),
0xFF: ("SELFDESTRUCT", 0, 1, 0, 0,
"Halt execution and register account for later deletion."),
}
opcodes_count = len(opcodes)
# OPCODE_BYTE, OPCODE, STACKIN, STACKOUT
opcodes_list = [
(x, opcodes[x][0], opcodes[x][2], opcodes[x][3]) for x in opcodes
]
class BytecodeGenerator:
def __init__(self, bytes_len: int, addr: str) -> None:
self.bytes_len = bytes_len
addrs = [
addr,
'0x01', '0x02', '0x03', '0x04', '0x05',
'0x06', '0x07', '0x08', '0x09', '0x0a'
]
for _ in range(30):
addrs.append(Web3().eth.account.create().address)
self.addrs = [
addr[2:] if addr.startswith("0x") else addr for addr in addrs
]
@staticmethod
def fill_stack(count: int, stack_count: int) -> str:
bytecode = ""
if stack_count >= count:
# We have enough items in the stack
return bytecode, stack_count-count
else:
# We need to push more items to the stack
count -= stack_count
stack_count = 0
for _ in range(count):
value = random.randint(0, 2**256-1)
bytecode += f"60{value:02x}"
return bytecode, stack_count
def fill_stack_with_addr(self) -> str:
addr = random.choice(self.addrs)
addr_bytes = len(addr) // 2
push_bytecode = 0x5F + addr_bytes
bytecode = f"{push_bytecode:02x}{addr}"
return bytecode
def get(self):
bytecode = ""
stack_count = 0
while (len(bytecode) // 2) < self.bytes_len:
i = random.randint(0, opcodes_count-1)
op_byte = opcodes_list[i][0]
opcode = opcodes_list[i][1]
stack_in = opcodes_list[i][2]
if opcode in [
"BALANCE", "EXTCODESIZE", "EXTCODECOPY", "EXTCODEHASH",
"SELFDESTRUCT"
]:
# Push the params after the address, if any
_bytecode, stack_count = \
self.fill_stack(stack_in-1, stack_count)
bytecode += _bytecode
# Setting the address for this opcodes from the addresses list
bytecode += self.fill_stack_with_addr()
stack_in = 0
elif opcode in ["CALL", "CALLCODE", "DELEGATECALL", "STATICCALL"]:
# Push the params after the address, if any
_bytecode, stack_count = \
self.fill_stack(stack_in-2, stack_count)
bytecode += _bytecode
# Setting the address for this opcodes from the addresses list
bytecode += self.fill_stack_with_addr()
# Add gas param
bytecode += f"60{i:02x}"
stack_in = 0
_bytecode, stack_count = self.fill_stack(stack_in, stack_count)
bytecode += f"{_bytecode}{op_byte:02x}"
stack_count += opcodes_list[i][3]
return "0x" + bytecode
def all_valid_bytecode_combinations(bytes_len, start=None):
import itertools
if not start:
start = "0x"
for _ in range(bytes_len):
start += "00"
bytes_list = [opcodes_list[i][0] for i in range(opcodes_count)]
combinations = itertools.product(bytes_list, repeat=bytes_len)
bytecodes = []
started = False
for comb in combinations:
bytecode = "0x"
for b in comb:
bytecode += f"{b:02x}"
if started or (bytecode == start):
bytecodes.append(bytecode)
started = True
return bytecodes
def all_bytecode_combinations(bytes_len, start=None, end=None):
import itertools
if not start:
start = "0x"
for _ in range(bytes_len):
start += "00"
bytes_list = [f"{i:02x}" for i in range(256)]
combinations = itertools.product(bytes_list, repeat=bytes_len)
bytecodes = []
started = False
for comb in combinations:
bytecode = "0x"
for b in comb:
bytecode += b
if started or (bytecode == start):
bytecodes.append(bytecode)
started = True
if end and (bytecode == end):
break
return bytecodes