-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple_evm.py
471 lines (398 loc) · 15.5 KB
/
simple_evm.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import binascii
from eth_hash.auto import keccak
UINT_256_MAX = 2**256 - 1
UINT_256_CEILING = 2**256
UINT_255_MAX = 2**255 - 1
UINT_255_CEILING = 2**255
# reference to https://ethervm.io/
class VM:
def __init__(self, state, msg) -> None:
self.msg = msg
self.state = state
code = state[self.msg['address'].encode('utf8')+b'_code']
if type(code) is bytes:
self.code = code
else:
self.code = binascii.unhexlify(code.replace('0x', ''))
self.pc = 0
self.memory = []
self.stack = []
def alloc(self, size):
if len(self.memory) < size:
for i in range(size - len(self.memory)):
self.memory.append(0x00)
def step(self):
print('------')
print('Pc:', self.pc, 'Opcode:', hex(self.code[self.pc]))
# print('Stack before:')
# for i in self.stack:
# print('', binascii.hexlify(i))
# print('Mem before:', self.memory)
if self.code[self.pc] == 0x00: # STOP
print('STOP')
return
elif self.code[self.pc] == 0x01: # ADD
'''
branch action :
for exec the "ADD" op
example : 0x03 0x02 ADD => 0x05
'''
print('ADD')
# pop the op number
last_bytes = self.stack.pop() # the last item
first_bytes = self.stack.pop()
# the endian use the "big"
last_num = int.from_bytes(last_bytes, 'big')
first_num = int.from_bytes(first_bytes, 'big')
# computer the result ! note the value 32 is for make the bytes len == 32
result = ((first_num + last_num) % (2**256)).to_bytes(32, 'big')# the signed must set True for the negative number
# push to the stack (the result)
self.stack.append(result)
self.pc += 1
elif self.code[self.pc] == 0x02: # MUL
print('MUL')
a = self.stack.pop()
b = self.stack.pop()
left = int.from_bytes(b, 'big')
right = int.from_bytes(a, 'big')
result = ((left * right) % (2**256)).to_bytes(32, 'big')
self.stack.append(result)
self.pc += 1
elif self.code[self.pc] == 0x03: # SUB
'''
branch action :
for exec the "SUB" op
example : 0x03 0x02 SUB => 0x01
'''
print('SUB', self.stack)
# pop the op number
a = self.stack.pop() # the last item
b = self.stack.pop()
left = int.from_bytes(a, 'big')
right = int.from_bytes(b, 'big')
print('SUB', left, right)
if left - right < 0:
result = (left - right + 2**256).to_bytes(32, 'big')
else:
result = (left - right).to_bytes(32, 'big')
# push to the stack (the result)
self.stack.append(result)
self.pc += 1
elif self.code[self.pc] == 0x04: # DIV
a = self.stack.pop()
b = self.stack.pop()
left = int.from_bytes(a, 'big')
right = int.from_bytes(b, 'big')
result = int(left/right).to_bytes(32, 'big')
self.stack.append(result)
self.pc += 1
# elif self.code[self.pc] == 0x05: # SDIV
# pass
# elif self.code[self.pc] == 0x06: # MOD
# pass
# elif self.code[self.pc] == 0x07: # SMOD
# pass
# elif self.code[self.pc] == 0x08: # ADDMOD
# pass
# elif self.code[self.pc] == 0x09: # MULMOD
# pass
# elif self.code[self.pc] == 0x0a: # EXP
# pass
# elif self.code[self.pc] == 0x0b: # SIGNEXTEND
# pass
elif self.code[self.pc] == 0x10: # LT
print('LT')
a = self.stack.pop()
left = int.from_bytes(a, 'big')
b = self.stack.pop()
right = int.from_bytes(b, 'big')
self.stack.append(bytes([0]*31+[left < right]))
self.pc += 1
elif self.code[self.pc] == 0x11: # GT
print('GT')
a = self.stack.pop()
left = int.from_bytes(a, 'big')
b = self.stack.pop()
right = int.from_bytes(b, 'big')
self.stack.append(bytes([0]*31+[left > right]))
self.pc += 1
elif self.code[self.pc] == 0x12: # SLT
print('SLT')
a = self.stack.pop()
left = int.from_bytes(a, 'big')
if left > UINT_255_MAX:
return left - UINT_256_CEILING
b = self.stack.pop()
right = int.from_bytes(b, 'big')
if right > UINT_255_MAX:
return right - UINT_256_CEILING
print(left, right)
self.stack.append(bytes([0]*31+[left < right]))
self.pc += 1
elif self.code[self.pc] == 0x13: # SGT
print('SGT')
a = self.stack.pop()
left = int.from_bytes(a, 'big')
if left > UINT_255_MAX:
return left - UINT_256_CEILING
b = self.stack.pop()
right = int.from_bytes(b, 'big')
if right > UINT_255_MAX:
return right - UINT_256_CEILING
self.stack.append(bytes([0]*31+[left > right]))
self.pc += 1
elif self.code[self.pc] == 0x14: # EQ
print('EQ')
b = self.stack.pop()
a = self.stack.pop()
self.stack.append(bytes([0]*31+[a == b]))
self.pc += 1
elif self.code[self.pc] == 0x15: # ISZERO
print('ISZERO')
bs = self.stack.pop()
result = 1
for b in bs:
if b > 0:
result = 0
break
self.stack.append(result.to_bytes(32, 'big'))
self.pc += 1
elif self.code[self.pc] == 0x16: # AND
b = self.stack.pop()
a = self.stack.pop()
print('AND', b, a)
result = []
for i in range(32):
result.append(b[i] & a[i])
self.stack.append(bytes(result))
self.pc += 1
elif self.code[self.pc] == 0x17: # OR
print('OR')
b = self.stack.pop()
a = self.stack.pop()
self.stack.append(bytes([a[i] | b[i] for i in range(32)]))
self.pc += 1
# elif self.code[self.pc] == 0x18: # XOR
# pass
elif self.code[self.pc] == 0x19: # NOT
print('NOT')
print(self.stack)
obj = self.stack.pop()
self.stack.append(bytes([255-i for i in obj]))
print(self.stack)
self.pc += 1
# elif self.code[self.pc] == 0x1a: # BYTE
# pass
elif self.code[self.pc] == 0x1b: # SHL
print('SHL')
i = self.stack.pop()
shift = int.from_bytes(i, 'big')
print('shift', shift)
i = self.stack.pop()
value = int.from_bytes(i, 'big')
print('value', value)
if shift >= 256:
result = 0
else:
result = value << shift
self.stack.append(result.to_bytes(32, 'big'))
self.pc += 1
elif self.code[self.pc] == 0x1c: # SHR
print('SHR')
i = self.stack.pop()
shift = int.from_bytes(i, 'big')
print('shift', shift)
i = self.stack.pop()
value = int.from_bytes(i, 'big')
print('value', value)
if shift >= 256:
result = 0
else:
result = value >> shift
self.stack.append(result.to_bytes(32, 'big'))
self.pc += 1
# elif self.code[self.pc] == 0x1d: # SAR
# print('SAR')
elif self.code[self.pc] == 0x20: # SHA3
print('SHA3')
offset = self.stack.pop()
mc = int.from_bytes(offset, 'big')
length = self.stack.pop()
l = int.from_bytes(length, 'big')
data = bytes(self.memory[mc:mc+l])
hash = keccak(data)
self.stack.append(hash)
print('SHA3', data, hash)
self.pc += 1
elif self.code[self.pc] == 0x30: # ADDRESS
print('ADDRESS')
self.stack.append(self.msg['address'])
self.pc += 1
elif self.code[self.pc] == 0x31: # BALANCE
print('BALANCE')
address = self.stack.pop()
self.stack.append(self.state[address]['balance'].to_bytes(32, 'big'))
self.pc += 1
elif self.code[self.pc] == 0x32: # ORIGIN
print('ORIGIN')
self.stack.append(self.msg['origin'])
self.pc += 1
elif self.code[self.pc] == 0x33: # CALLER
print('CALLER')
self.stack.append(self.msg['sender'])
self.pc += 1
elif self.code[self.pc] == 0x34: # CALLVALUE
print('CALLVALUE')
self.stack.append(self.msg['value'].to_bytes(32, 'big'))
print('CALLVALUE', self.msg)
self.pc += 1
elif self.code[self.pc] == 0x35: # CALLDATALOAD
print('CALLDATALOAD')
i = self.stack.pop()
mc = int.from_bytes(i, 'big')
data = self.msg['data'][mc:mc+32]
result = data+bytes([0]*(32-len(data)))
self.stack.append(result)
self.pc += 1
elif self.code[self.pc] == 0x36: # CALLDATASIZE
print('CALLDATASIZE')
self.stack.append(len(self.msg['data']).to_bytes(32, 'big'))
self.pc += 1
# elif self.code[self.pc] == 0x37: # CALLDATACOPY
# print('CALLDATACOPY')
# pass
elif self.code[self.pc] == 0x38: # CODESIZE
print('CODESIZE')
self.stack.append(len(self.code).to_bytes(32, 'big'))
self.pc += 1
elif self.code[self.pc] == 0x39: # CODECOPY
print('CODECOPY')
dest_offset = int.from_bytes(self.stack.pop(), 'big')
offset = int.from_bytes(self.stack.pop(), 'big')
length = int.from_bytes(self.stack.pop(), 'big')
print(dest_offset, offset, length)
# print(len(self.code[offset:offset+length]))
self.alloc(dest_offset+length)
for b in self.code[offset:offset+length]:
self.memory[dest_offset] = b
dest_offset += 1
self.pc += 1
elif self.code[self.pc] == 0x50: # POP
print('POP')
self.stack.pop()
self.pc += 1
elif self.code[self.pc] == 0x51: # MLOAD
print('MLOAD')
offset = self.stack.pop()
mc = int.from_bytes(offset, 'big')
data = bytes(self.memory[mc:mc+32])
result = data+bytes([0]*(32-len(data)))
self.stack.append(result)
self.pc += 1
elif self.code[self.pc] == 0x52: # MSTORE offset value
print('MSTORE')
offset = self.stack.pop()
print('MSTORE offset', offset)
value = self.stack.pop()
print('MSTORE value', value)
mc = int.from_bytes(offset, 'big')
self.alloc(mc + 32)
for b in value:
self.memory[mc] = b
mc += 1
self.pc += 1
elif self.code[self.pc] == 0x53: # MSTORE8 offset value
print('MSTORE8')
offset = self.stack.pop()
value = self.stack.pop()
mc = int.from_bytes(offset, 'big')
self.alloc(mc + 1)
self.memory[mc] = value[0]
self.pc += 1
elif self.code[self.pc] == 0x54: # SLOAD
print('SLOAD')
key = self.stack.pop()
print(self.state[self.msg['address'].encode('utf8')+b'_storage_'+key])
value = self.state[self.msg['address'].encode('utf8')+b'_storage_'+key]
if value == b'':
value = b'\x00'*32
self.stack.append(value)
self.pc += 1
elif self.code[self.pc] == 0x55: # SSTORE
print('SSTORE')
key = self.stack.pop()
value = self.stack.pop()
print('SSTORE', key.hex(), value)
# print('SSTORE', self.state)
print('SSTORE', self.msg['address'])
print(self.state[self.msg['address'].encode('utf8')+b'_storage_'+key])
self.state[self.msg['address'].encode('utf8')+b'_storage_'+key] = value
print(self.state[self.msg['address'].encode('utf8')+b'_storage_'+key])
self.pc += 1
elif self.code[self.pc] == 0x56: # JUMP
print('JUMP')
dist = self.stack.pop()
print('JUMP', int.from_bytes(dist, 'big'))
self.pc = int.from_bytes(dist, 'big')
elif self.code[self.pc] == 0x57: # JUMPI
print('JUMPI')
dist = self.stack.pop()
cond = self.stack.pop()
if(int.from_bytes(cond, 'big')):
self.pc = int.from_bytes(dist, 'big')
else:
self.pc += 1
elif self.code[self.pc] == 0x5b: # JUMPDEST
print('JUMPDEST', self.pc)
self.pc += 1
elif self.code[self.pc] >= 0x60 and self.code[self.pc] <= 0x7f: # PUSHx
size = self.code[self.pc] - 0x5f
print('PUSH', size)
print('PUSH', bytes(self.code[self.pc+1:self.pc+1+size]))
self.stack.append(bytes([0]*(32-size)) + self.code[self.pc+1:self.pc+1+size])
self.pc += size+1
elif self.code[self.pc] >= 0x80 and self.code[self.pc] <= 0x8f: # DUPx
size = self.code[self.pc] - 0x7f
print('DUP', size)
self.stack.append(self.stack[-size])
self.pc += 1
elif self.code[self.pc] >= 0x90 and self.code[self.pc] <= 0x9f: # SWAPx
size = self.code[self.pc] - 0x8f
print('SWAP', size)
self.stack[-1], self.stack[-1-size] = self.stack[-1-size], self.stack[-1]
self.pc += 1
# elif self.code[self.pc] >= 0xA0 and self.code[self.pc] <= 0xA4: # LOGx
# pass
elif self.code[self.pc] == 0xf3: # RETURN
print('RETURN')
'''
branch action :
for exec the "RETURN" op
example : memory[offset:offset+length]
'''
# pop the op number
offset_bytes = self.stack.pop()
length_bytes = self.stack.pop()
# the endian use the "big"
offset_num = int.from_bytes(offset_bytes, 'big')
length_num = int.from_bytes(length_bytes, 'big')
print('RETURN', offset_num, length_num)
# ! I think should assert the offset and the length must be positive number
# return the value
return bytes(self.memory[offset_num : offset_num + length_num]).hex()
elif self.code[self.pc] == 0xfd: # REVERT
print('REVERT')
# pop the op number
offset_bytes = self.stack.pop()
length_bytes = self.stack.pop()
# the endian use the "big"
offset_num = int.from_bytes(offset_bytes, 'big')
length_num = int.from_bytes(length_bytes, 'big')
# return the value
return 'REVERT', self.memory[offset_num : offset_num + length_num]
else:
raise
print('Stack after:')
for i in self.stack:
print('', binascii.hexlify(i))
print('Mem after:', self.memory)