-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
399 lines (337 loc) · 11.8 KB
/
app.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
from flask import Flask, request, jsonify, send_file
from flask_cors import CORS, cross_origin
import os
import uuid
import subprocess
import traceback
import rpc_endpoint
import solcx
from web3 import Web3
app = Flask("ZKML-server")
cors = CORS(app)
ezkl = "./ezkl/target/release/ezkl"
# flags to only allow for only one proof
# the server cannot accomodate more than one proof
loaded_onnxmodel = None
loaded_inputdata = None
loaded_proofname = None
running = False
"""
Simple single session prover
"""
@app.route('/')
@cross_origin()
def index():
return "Extend your smart contracts with Gelkin"
"""
Upload input data for proving, no validation atm
"""
@app.route('/upload/inputdata', methods=['POST'])
@cross_origin()
def upload_inputdata():
if request.method == 'POST':
inputdata = request.files['inputdata']
uuidval = uuid.uuid4()
inputdata.save(os.path.join("inputdata", str(uuidval) + ".json"))
return jsonify({
"file": str(uuidval) + ".json"
})
"""
Lists all input data stored on the server
"""
@app.route('/list/inputdata', methods=['GET'])
@cross_origin()
def list_inputdata():
filelist = os.listdir(os.path.join('inputdata'))
filelist.remove('.gitkeep')
return jsonify({
"list": filelist
})
"""
Download input data stored on the server
"""
@app.route('/download/inputdata/<filename>', methods=['GET'])
@cross_origin()
def download_inputdata(filename):
sanitized_filename = str(filename)
return send_file(os.path.join("inputdata", sanitized_filename), as_attachment=True)
"""
Upload onnx model for proving, no validation atm
"""
@app.route('/upload/onnxmodel', methods=['POST'])
@cross_origin()
def upload_onnxmodel():
if request.method == 'POST':
# save the single onnx file
inputdata = request.files['onnxmodel']
uuidval = uuid.uuid4()
inputdata.save(os.path.join("onnxmodel", str(uuidval) + ".onnx"))
return jsonify({
"file": str(uuidval) + ".onnx"
})
"""
Lists all onnx data stored on the server
"""
@app.route('/list/onnxmodel', methods=['GET'])
@cross_origin()
def list_onnxmodel():
filelist = os.listdir(os.path.join('onnxmodel'))
filelist.remove('.gitkeep')
return jsonify({
"list": filelist
})
"""
Download input data stored on the server
"""
@app.route('/download/onnxmodel/<filename>', methods=['GET'])
@cross_origin()
def download_onnxmodel(filename):
sanitized_filename = str(filename)
return send_file(os.path.join("onnxmodel", sanitized_filename), as_attachment=True)
"""
Lists all generated data stored on the server
"""
@app.route('/list/generated', methods=['GET'])
@cross_origin()
def list_generated():
filelist = os.listdir(os.path.join('generated'))
filelist.remove('.gitkeep')
return jsonify({
"list": filelist
})
"""
Download generated data stored on the server
"""
@app.route('/download/generated/<filename>', methods=['GET'])
@cross_origin()
def download_generated(filename):
sanitized_filename = str(filename)
return send_file(os.path.join("generated", sanitized_filename), as_attachment=True)
"""
Sets the model and input to be used
"""
@app.route('/run/initialize', methods=['GET', 'POST'])
@cross_origin()
def set_model_input():
global loaded_inputdata
global loaded_onnxmodel
global loaded_proofname
if request.method == "POST":
if running:
return "Already running please wait for completion", 400
content = request.json
inputdata = content['inputdata'].strip()
onnxmodel = content['onnxmodel'].strip()
loaded_inputdata = os.path.join("inputdata", inputdata)
loaded_onnxmodel = os.path.join("onnxmodel", onnxmodel)
loaded_proofname = "inputdata_" + loaded_inputdata[10:46] \
+ "+" + "onnxmodel_" + loaded_onnxmodel[10:46]
return jsonify({
"loaded_inputdata": loaded_inputdata,
"loaded_onnxmodel": loaded_onnxmodel,
"proof_name": loaded_proofname
})
if request.method == "GET":
return jsonify({
"loaded_inputdata": loaded_inputdata,
"loaded_onnxmodel": loaded_onnxmodel,
"proof_name": loaded_proofname
})
"""
Generates evm proof
"""
@app.route('/run/gen_evm_proof', methods=['GET'])
@cross_origin()
def gen_evm_proof():
global loaded_inputdata
global loaded_onnxmodel
global loaded_proofname
global running
if loaded_inputdata is None or loaded_onnxmodel is None or loaded_proofname is None:
return "Input Data or Onnx Model not loaded", 400
if running:
return "Already running please wait for completion", 400
if os.path.exists(os.path.join(os.getcwd(), "generated", loaded_proofname + ".pf")) or \
os.path.exists(os.path.join(os.getcwd(), "generated", loaded_proofname + ".vk")):
return "Proof already exists", 400
try:
running = True
p = subprocess.run([
ezkl,
"--bits=16",
"-K=17",
"prove",
"-D", os.path.join(os.getcwd(), loaded_inputdata),
"-M", os.path.join(os.getcwd(), loaded_onnxmodel),
"--proof-path", os.path.join(os.getcwd(), "generated", loaded_proofname + ".pf"),
"--vk-path", os.path.join(os.getcwd(), "generated", loaded_proofname + ".vk"),
"--params-path=" + os.path.join(os.getcwd(), "kzg.params"),
"--transcript=evm"
],
capture_output=True,
text=True
)
running = False
return jsonify({
"stdout": p.stdout,
"stderr": p.stderr
})
except:
running = False
err = traceback.format_exc()
return "Something bad happened! Please inform the server admin\n" + err, 500
"""
Generates evm verifier
"""
@app.route('/run/gen_evm_verifier', methods=['GET'])
@cross_origin()
def gen_evm_verifier():
global loaded_inputdata
global loaded_onnxmodel
global loaded_proofname
global running
if loaded_inputdata is None or loaded_onnxmodel is None or loaded_proofname is None:
return "Input Data or Onnx Model not loaded", 400
if running:
return "Already running please wait for completion", 400
if os.path.exists(os.path.join(os.getcwd(), "generated", loaded_proofname + ".sol")) and \
os.path.exists(os.path.join(os.getcwd(), "generated", loaded_proofname + ".code")):
return "Verifier already exists", 400
try:
running = True
p = subprocess.run([
ezkl,
"--bits=16",
"-K=17",
"create-evm-verifier",
"-D", os.path.join(os.getcwd(), loaded_inputdata),
"-M", os.path.join(os.getcwd(), loaded_onnxmodel),
"--deployment-code-path", os.path.join(os.getcwd(), "generated", loaded_proofname + ".code"),
"--params-path=" + os.path.join(os.getcwd(), "kzg.params"),
"--vk-path", os.path.join(os.getcwd(), "generated", loaded_proofname + ".vk"),
"--sol-code-path", os.path.join(os.getcwd(), "generated", loaded_proofname + ".sol"),
],
capture_output=True,
text=True
)
running = False
return jsonify({
"stdout": p.stdout,
"stderr": p.stderr
})
except:
running = False
err = traceback.format_exc()
return "Something bad happened! Please inform the server admin\n" + err, 500
"""
Verifies proof using .code
"""
@app.route('/run/verify', methods=['GET'])
@cross_origin()
def run_verify():
global loaded_inputdata
global loaded_onnxmodel
global loaded_proofname
global running
if loaded_inputdata is None or loaded_onnxmodel is None or loaded_proofname is None:
return "Input Data or Onnx Model not loaded", 400
if running:
return "Already running please wait for completion", 400
if not os.path.exists(os.path.join(os.getcwd(), "generated", loaded_proofname + ".code")):
return "Proof does not exists", 400
try:
running = True
p = subprocess.run([
ezkl,
"--bits=16",
"-K=17",
"verify-evm",
"--proof-path", os.path.join(os.getcwd(), "generated", loaded_proofname + ".pf"),
"--deployment-code-path", os.path.join(os.getcwd(), "generated", loaded_proofname + ".code"),
],
capture_output=True,
text=True
)
running = False
return jsonify({
"stdout": p.stdout,
"stderr": p.stderr
})
except:
running = False
err = traceback.format_exc()
return "Something bad happened! Please inform the server admin\n" + err, 500
"""
Deploys verifier
"""
@app.route('/run/deploy_verifier/<network_id>', methods=['GET'])
@cross_origin()
def run_deploy(network_id):
global loaded_inputdata
global loaded_onnxmodel
global loaded_proofname
global running
network_id = int(network_id)
if loaded_inputdata is None or loaded_onnxmodel is None or loaded_proofname is None:
return "Input Data or Onnx Model not loaded", 400
if running:
return "Already running please wait for completion", 400
if not os.path.exists(os.path.join(os.getcwd(), "generated", loaded_proofname + ".sol")):
return "Proof does not exists", 400
rpc = rpc_endpoint.ETH_MAINNET
if network_id == 5:
rpc = rpc_endpoint.ETH_GOERLI
if network_id == 5001:
rpc = rpc_endpoint.MANTLE_TESTNET
if network_id == 80001:
rpc = rpc_endpoint.POLYGON_MUMBAI
try:
running = True
# compile contract
with open(os.path.join(os.getcwd(), "generated", loaded_proofname + ".sol"), "r") as f:
file = f.read()
temp_file = solcx.compile_source(file, output_values=["abi", "bin-runtime"])
abi = temp_file['<stdin>:Verifier']['abi']
bytecode = temp_file['<stdin>:Verifier']['bin-runtime']
web3 = Web3(Web3.HTTPProvider(rpc))
account_from = { 'private_key': rpc_endpoint.PRIVATE_KEY, 'address': rpc_endpoint.PUBLIC_KEY }
contract = web3.eth.contract(abi=abi, bytecode=bytecode)
nonce = web3.eth.getTransactionCount(account_from['address'])
priority_fee = web3.eth.max_priority_fee
gas_fee = web3.eth.gas_price + 40000000000
construct_txn = contract.constructor().buildTransaction(
{
'from': account_from['address'],
'nonce': nonce,
"maxFeePerGas": gas_fee,
"maxPriorityFeePerGas": priority_fee
}
)
tx_create = web3.eth.account.sign_transaction(construct_txn, account_from['private_key'])
tx_hash = web3.eth.send_raw_transaction(tx_create.rawTransaction)
tx_receipt = web3.eth.wait_for_transaction_receipt(tx_hash)
return jsonify({
"contractaddr": tx_receipt.contractAddress
})
# p = subprocess.run([
# ezkl,
# "deploy-verifier-evm",
# "-S", os.path.join(os.getcwd(), "mnemonic.txt"),
# "-U", rpc,
# "--sol-code-path", os.path.join(os.getcwd(), "generated", loaded_proofname + ".sol"),
# ],
# capture_output=True,
# text=True
# )
# running = False
# return jsonify({
# "stdout": p.stdout,
# "stderr": p.stderr
# })
except:
running = False
err = traceback.format_exc()
print(err)
return "Something bad happened! Please inform the server admin\n" + err, 500
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)