-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfix_verifier_sol.py
More file actions
198 lines (169 loc) · 7.14 KB
/
fix_verifier_sol.py
File metadata and controls
198 lines (169 loc) · 7.14 KB
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
#!/usr/bin/env python3
import sys
import re
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: fix_verifier_sol.py <input>")
sys.exit(1)
input_file = sys.argv[1]
lines = open(input_file).readlines()
transcript_addrs = list()
modified_lines = list()
num_pubinputs = 0
# convert calldataload 0x0 to 0x40 to read from pubInputs, and the rest
# from proof
calldata_pattern = r"^.*(calldataload\((0x[a-f0-9]+)\)).*$"
mstore_pattern = r"^\s*(mstore\(0x([0-9a-fA-F]+)+),.+\)"
mstore8_pattern = r"^\s*(mstore8\((\d+)+),.+\)"
mstoren_pattern = r"^\s*(mstore\((\d+)+),.+\)"
mload_pattern = r"(mload\((0x[0-9a-fA-F]+))\)"
keccak_pattern = r"(keccak256\((0x[0-9a-fA-F]+))"
modexp_pattern = r"(staticcall\(gas\(\), 0x5, (0x[0-9a-fA-F]+), 0xc0, (0x[0-9a-fA-F]+), 0x20)"
ecmul_pattern = r"(staticcall\(gas\(\), 0x7, (0x[0-9a-fA-F]+), 0x60, (0x[0-9a-fA-F]+), 0x40)"
ecadd_pattern = r"(staticcall\(gas\(\), 0x6, (0x[0-9a-fA-F]+), 0x80, (0x[0-9a-fA-F]+), 0x40)"
ecpairing_pattern = r"(staticcall\(gas\(\), 0x8, (0x[0-9a-fA-F]+), 0x180, (0x[0-9a-fA-F]+), 0x20)"
bool_pattern = r":bool"
# Count the number of pub inputs
start = None
end = None
i = 0
for line in lines:
if line.strip().startswith("mstore(0x20"):
start = i
if line.strip().startswith("mstore(0x0"):
end = i
break
i += 1
if start is None:
num_pubinputs = 0
else:
num_pubinputs = end - start
max_pubinputs_addr = 0
if num_pubinputs > 0:
max_pubinputs_addr = num_pubinputs * 32 - 32
for line in lines:
m = re.search(bool_pattern, line)
if m:
line = line.replace(":bool", "")
m = re.search(calldata_pattern, line)
if m:
calldata_and_addr = m.group(1)
addr = m.group(2)
addr_as_num = int(addr, 16)
if addr_as_num <= max_pubinputs_addr:
pub_addr = hex(addr_as_num + 32)
line = line.replace(calldata_and_addr, "mload(add(pubInputs, " + pub_addr + "))")
else:
proof_addr = hex(addr_as_num - max_pubinputs_addr)
line = line.replace(calldata_and_addr, "mload(add(proof, " + proof_addr + "))")
m = re.search(mstore8_pattern, line)
if m:
mstore = m.group(1)
addr = m.group(2)
addr_as_num = int(addr)
transcript_addr = hex(addr_as_num)
transcript_addrs.append(addr_as_num)
line = line.replace(mstore, "mstore8(add(transcript, " + transcript_addr + ")")
m = re.search(mstoren_pattern, line)
if m:
mstore = m.group(1)
addr = m.group(2)
addr_as_num = int(addr)
transcript_addr = hex(addr_as_num)
transcript_addrs.append(addr_as_num)
line = line.replace(mstore, "mstore(add(transcript, " + transcript_addr + ")")
m = re.search(modexp_pattern, line)
if m:
modexp = m.group(1)
start_addr = m.group(2)
result_addr = m.group(3)
start_addr_as_num = int(start_addr, 16)
result_addr_as_num = int(result_addr, 16)
transcript_addr = hex(start_addr_as_num)
transcript_addrs.append(addr_as_num)
result_addr = hex(result_addr_as_num)
line = line.replace(modexp, "staticcall(gas(), 0x5, add(transcript, " + transcript_addr + "), 0xc0, add(transcript, " + result_addr + "), 0x20")
m = re.search(ecmul_pattern, line)
if m:
ecmul = m.group(1)
start_addr = m.group(2)
result_addr = m.group(3)
start_addr_as_num = int(start_addr, 16)
result_addr_as_num = int(result_addr, 16)
transcript_addr = hex(start_addr_as_num)
result_addr = hex(result_addr_as_num)
transcript_addrs.append(start_addr_as_num)
transcript_addrs.append(result_addr_as_num)
line = line.replace(ecmul, "staticcall(gas(), 0x7, add(transcript, " + transcript_addr + "), 0x60, add(transcript, " + result_addr + "), 0x40")
m = re.search(ecadd_pattern, line)
if m:
ecadd = m.group(1)
start_addr = m.group(2)
result_addr = m.group(3)
start_addr_as_num = int(start_addr, 16)
result_addr_as_num = int(result_addr, 16)
transcript_addr = hex(start_addr_as_num)
result_addr = hex(result_addr_as_num)
transcript_addrs.append(start_addr_as_num)
transcript_addrs.append(result_addr_as_num)
line = line.replace(ecadd, "staticcall(gas(), 0x6, add(transcript, " + transcript_addr + "), 0x80, add(transcript, " + result_addr + "), 0x40")
m = re.search(ecpairing_pattern, line)
if m:
ecpairing = m.group(1)
start_addr = m.group(2)
result_addr = m.group(3)
start_addr_as_num = int(start_addr, 16)
result_addr_as_num = int(result_addr, 16)
transcript_addr = hex(start_addr_as_num)
result_addr = hex(result_addr_as_num)
transcript_addrs.append(start_addr_as_num)
transcript_addrs.append(result_addr_as_num)
line = line.replace(ecpairing, "staticcall(gas(), 0x8, add(transcript, " + transcript_addr + "), 0x180, add(transcript, " + result_addr + "), 0x20")
m = re.search(mstore_pattern, line)
if m:
mstore = m.group(1)
addr = m.group(2)
addr_as_num = int(addr, 16)
transcript_addr = hex(addr_as_num)
transcript_addrs.append(addr_as_num)
line = line.replace(mstore, "mstore(add(transcript, " + transcript_addr + ")")
m = re.search(keccak_pattern, line)
if m:
keccak = m.group(1)
addr = m.group(2)
addr_as_num = int(addr, 16)
transcript_addr = hex(addr_as_num)
transcript_addrs.append(addr_as_num)
line = line.replace(keccak, "keccak256(add(transcript, " + transcript_addr + ")")
# mload can show up multiple times per line
while True:
m = re.search(mload_pattern, line)
if not m:
break
mload = m.group(1)
addr = m.group(2)
addr_as_num = int(addr, 16)
transcript_addr = hex(addr_as_num)
transcript_addrs.append(addr_as_num)
line = line.replace(mload, "mload(add(transcript, " + transcript_addr + ")")
# print(line, end="")
modified_lines.append(line)
# get the max transcript addr
max_transcript_addr = int(max(transcript_addrs) / 32)
print("""// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract Verifier {{
function verify(
uint256[] memory pubInputs,
bytes memory proof
) public view returns (bool) {{
bool success = true;
bytes32[{}] memory transcript;
assembly {{
""".strip().format(max_transcript_addr))
for line in modified_lines[16:-7]:
print(line, end="")
print("""}
return success;
}
}""")