-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathto_evm.py
214 lines (181 loc) · 5.82 KB
/
to_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
#!/usr/bin/env python3
import json
import sys
from hashlib import sha1
from typing import List, Union
infile = sys.argv[1]
outdir = sys.argv[2]
def ToBytes(i: int, size: int = 32) -> bytes:
if size == 0:
size = (len(hex(i)[2:]) + 1) // 2
return i.to_bytes(size, 'big')
def ToInt(s: str) -> int:
if s == '':
s = '0'
return int(s)
def ToLenBytes(i: int) -> bytes:
l = (len(hex(i)[2:]) + 1) // 2
return ToBytes(l)
def Push32(val: Union[int, bytes]) -> bytes:
ret = bytes()
ret += bytes([0x7f])
if type(val) == int:
ret += ToBytes(val)
elif type(val) == bytes:
assert(len(val) == 32)
ret += val
else:
assert(False)
return ret
def Store32(pos: int, val: Union[int, bytes]) -> bytes:
ret = bytes()
ret += Push32(val)
ret += Push32(pos)
ret += bytes([0x52])
return ret
def Store(data: bytes) -> bytes:
ret = bytes()
num32 = len(data) // 32
rem = len(data) % 32
pos = 0
for i in range(num32):
ret += Store32(pos, data[i*32 : i*32+32])
pos += 32
if rem:
remd = data[num32*32:]
assert(len(remd) < 32)
remd = remd + ((32 - len(remd)) * b'\x00')
assert(len(remd) == 32)
ret += Store32(pos, remd)
pos += 32
return ret
def Gas() -> bytes:
return bytes([0x5a])
def DelegateCall() -> bytes:
return bytes([0xf4])
def Call(argsLength: int, retLength: int, address: int) -> bytes:
ret = bytes()
ret += Push32(retLength) # retLength
ret += Push32(0) # retOffset
ret += Push32(argsLength) # argsLength
ret += Push32(0) # argsOffset
ret += Push32(address) # address
ret += Gas() # gas
ret += DelegateCall()
return ret
def MLoad(pos: int) -> bytes:
ret = bytes()
ret += Push32(pos)
ret += bytes([0x51])
return ret
def MSize() -> bytes:
ret = bytes()
ret += bytes([0x59])
return ret
def Load(num: int) -> bytes:
ret = bytes()
for i in range(num):
ret += MLoad(i * 32)
return ret
def Precompile(
address: int,
params: Union[List[int], bytes],
retLength: int) -> None:
ret = bytes()
if type(params) == list:
pos = 0
for p in params:
ret += Store32(pos, p)
pos += 32
paramsLength = len(params) * 32
retLength *= 32
elif type(params) == bytes:
ret += Store(params)
paramsLength = len(params)
retLength = 0
else:
assert(False)
ret += Call(paramsLength, retLength, address)
ret += MSize()
ret += Load(retLength)
fn = sha1(ret).hexdigest()
with open('{}/{}'.format(outdir, fn), 'wb') as fp:
fp.write(ret)
with open(infile, 'rb') as fp:
for l in fp:
j = json.loads(l)
op = j['operation']
if op == None:
continue
if 'operation' not in op.keys():
continue
try:
if op['operation'] == "ECDSA_Recover":
if op['curveType'] != '18393850816800450172':
continue
if op['digestType'] != '7259431668663979670':
continue
cleartext = op['cleartext']
if len(cleartext) > 64:
continue
cleartext = ('0' * (64 - len(cleartext))) + cleartext
sig_r = ToInt(op['sig_r'])
sig_s = ToInt(op['sig_s'])
idd = (ToInt(op['id']) + 27) % 256
ecrecover_input = bytes()
ecrecover_input += bytes.fromhex(cleartext)
ecrecover_input += ToBytes(idd)
ecrecover_input += ToBytes(sig_r)
ecrecover_input += ToBytes(sig_s)
Precompile(1, ecrecover_input, 0)
elif op['operation'] == "BLS_G1_Add":
#if op['curveType'] != '9285907260089714809':
# continue
params = [
ToInt(op['a_x']),
ToInt(op['a_y']),
ToInt(op['b_x']),
ToInt(op['b_y'])
]
Precompile(6, params, 2)
elif op['operation'] == "BLS_G1_Mul":
#if op['curveType'] != '9285907260089714809':
# continue
params = [
ToInt(op['a_x']),
ToInt(op['a_y']),
ToInt(op['b']),
]
Precompile(7, params, 2)
elif op['operation'] == "BLS_BatchVerify":
#if op['curveType'] != '9285907260089714809':
# continue
params = []
for cur in op['bf']:
params += [ ToInt(cur['g1_x']) ]
params += [ ToInt(cur['g1_y']) ]
params += [ ToInt(cur['g2_x']) ]
params += [ ToInt(cur['g2_v']) ]
params += [ ToInt(cur['g2_y']) ]
params += [ ToInt(cur['g2_w']) ]
Precompile(8, params, 0)
elif op['operation'] == "BignumCalc":
if op['calcOp'] != '1317996975705594123':
continue
base = ToInt(op['bn0'])
exp = ToInt(op['bn1'])
mod = ToInt(op['bn2'])
modexp_input = bytes()
modexp_input += ToLenBytes(base)
modexp_input += ToLenBytes(exp)
modexp_input += ToLenBytes(mod)
modexp_input += ToBytes(base, 0)
modexp_input += ToBytes(exp, 0)
modexp_input += ToBytes(mod, 0)
modexp_input = Store(modexp_input)
Precompile(5, modexp_input, 0)
# TODO pop memory
else:
continue
except OverflowError:
pass