This repository has been archived by the owner on Jan 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
108 lines (92 loc) · 3.07 KB
/
main.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
import ast
import tempfile
import os
import json
import base64
import sys
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from lib import calculate_vk, verify_proof as lib_verify_proof, ExtractComputationFailure
app = FastAPI()
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=False, # Default is False
allow_methods=["GET", "POST"], # Specify the HTTP methods you want to allow
allow_headers=["*"], # Allows all headers
)
# ### POST `/computation_to_vk`
class ComputationToVKRequest(BaseModel):
# Example: '{"x": 7, "y": 7}'
data_shape: str
# Example: 'def computation(state, args): ...'
computation: str
# Settings in JSON format
settings: str
# Precomputed witness in JSON format
precal_witness: str
# tmp_dir: str,
# proof_json: str,
# settings_json: str,
# vk_path: str,
# selected_columns: list[str],
# data_commitment_json: str,
class VerifyProofRequest(BaseModel):
# Example: 'def computation(state, args): ...'
proof_json: str
# Settings in JSON format
settings_json: str
vk_b64: str
selected_columns: list[str]
data_commitment_json: str
@app.post("/computation_to_vk")
async def computation_to_vk(request: ComputationToVKRequest):
try:
with tempfile.TemporaryDirectory() as tmp_dir:
selected_columns, vk_path = calculate_vk(
tmp_dir,
request.data_shape,
request.computation,
request.settings,
request.precal_witness
)
print("!@# computation_to_vk: 1")
# Read the verification key file
with open(vk_path, 'rb') as vk_file:
vk_content = vk_file.read()
print("!@# computation_to_vk: 2")
# Return the file content and selected columns
return JSONResponse(content={
"verification_key": base64.b64encode(vk_content).decode('utf-8'),
"selected_columns": selected_columns
})
except Exception as e:
print(f"Error: {e}")
import traceback
traceback.print_exc()
raise HTTPException(status_code=500, detail=str(e))
@app.post("/verify_proof")
async def verify_proof(request: VerifyProofRequest):
try:
with tempfile.TemporaryDirectory() as tmp_dir:
res = lib_verify_proof(
tmp_dir,
request.proof_json,
request.settings_json,
request.vk_b64,
request.selected_columns,
request.data_commitment_json
)
res_json = json.dumps({"result": res})
return JSONResponse(content=res_json)
except Exception as e:
print(f"Error: {e}")
import traceback
traceback.print_exc()
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)