-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.py
executable file
·237 lines (187 loc) · 8.49 KB
/
run.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
#!/usr/bin/env python3
import subprocess
import argparse
import enum
import os
import json
from codegen import lpcodegen
class Size(enum.Enum):
SMALL = 0
FULL = 1
class Type(enum.Enum):
LP = 0
SDP = 1
SGD = 2
def absoluteFilePaths(directory):
for dirpath,_,filenames in os.walk(directory):
for f in filenames:
yield os.path.abspath(os.path.join(dirpath, f)).decode("utf-8")
def run_lp(home, files):
# Make workdirs
workdir = os.path.join(home, "LP")
cfiles = os.path.join(workdir, "cfiles")
zkifs = os.path.join(workdir, "zkif")
os.makedirs(cfiles, exist_ok=True)
os.makedirs(zkifs, exist_ok=True)
# Generate C files
for f in files:
fn = os.path.basename(f)
# write C code to file
cpath = os.path.join(cfiles, fn + ".c")
# Open file and write C code
with open(cpath, "w") as fd:
c_code = lpcodegen.generate(f)
fd.write(c_code)
# Compile to zkif
print("Compiling " + fn)
os.chdir(home+"/compiler/")
subprocess.run("C_outputs=" + fn + ".zkif stack run -- c main " + cpath + " --emit-r1cs", shell=True, check=True)
subprocess.run("C_outputs=" + fn + ".zkif stack run -- c main " + cpath + " --prove -i input", shell=True, check=True)
subprocess.run(["mv", fn + ".inp.zkif", zkifs], check=True)
subprocess.run(["mv", fn + ".wit.zkif", zkifs], check=True)
subprocess.run(["mv", fn + ".zkif", zkifs], check=True)
# Verify spartan
os.chdir(home+"/spartan-zkinterface/")
rust_env = os.environ.copy()
rust_env["RUSTFLAGS"] = "-C target_cpu=native"
zkpath = os.path.join(zkifs, fn)
print("Generating proof for " + fn)
subprocess.run("cargo run --release -- verify --nizk " + zkpath + ".zkif " + zkpath + ".inp.zkif " + zkpath + ".wit.zkif",
shell=True, check=True, env=rust_env)
def parse_lp(home, size, custom=None):
direc = None
if size == Size.SMALL:
print("Running LP small Otti dataset")
direc = os.fsencode(home+"/datasets/LP/MPS-small/")
elif size == Size.FULL:
print("Running LP full Otti dataset")
direc = os.fsencode(home+"/datasets/LP/MPS-full/")
elif custom != None:
print("Running LP custom data")
return [os.path.abspath(custom)]
else:
print("Dataset for LP not specified, running small Otti dataset")
direc = os.fsencode(home+"/datasets/LP/MPS-small/")
return list(absoluteFilePaths(direc))
def run_sdp(f, path, home):
if not f.endswith('.dat-s'):
print("ERROR: "+f+ " is not a dat-s file")
return
print("Making c file for " + f)
subprocess.run(["python3", home+"/codegen/sdpcodegen.py", path+f])
subprocess.run(["mv", f+".c", home+"/out/cfiles/"])
os.chdir(home+"/compiler/")
print("Compiling R1CS for " + f)
subprocess.run("C_outputs="+f+".zkif stack run -- c main "+home+"/out/cfiles/"+f+".c --emit-r1cs", shell=True)
subprocess.run("C_outputs="+f+".zkif stack run -- c main "+home+"/out/cfiles/"+f+".c --prove -i input", shell=True)
subprocess.run(["mv", f+".inp.zkif", home+"/out/zkif/"])
subprocess.run(["mv", f+".wit.zkif", home+"/out/zkif/"])
subprocess.run(["mv", f+".zkif", home+"/out/zkif/"])
os.chdir(home+"/spartan-zkinterface/")
print("Generating proof for " + f)
subprocess.run("./target/release/spzk verify --nizk "+home+"/out/zkif/"+f+".zkif "+home+"/out/zkif/"+f+".inp.zkif "+home+"/out/zkif/"+f+".wit.zkif", shell=True)
def parse_sdp(home, size, custom=None):
if size == Size.SMALL:
print("Running SDP small Otti dataset")
direc = os.fsencode(home+"/datasets/SDP/small/")
for name in os.listdir(direc):
f = name.decode('UTF-8')
path = direc.decode('UTF-8')
run_sdp(f, path, home)
elif size == Size.FULL:
print("Running SDP full Otti dataset, WARNING: do not attempt this without a lot of RAM")
direc = os.fsencode(home+"/datasets/SDP/full/")
for name in os.listdir(direc):
f = name.decode('UTF-8')
path = direc.decode('UTF-8')
run_sdp(f, path, home)
elif custom != None:
print("Running SDP custom data")
abspath = os.path.abspath(custom)
name = os.path.basename(custom)
path = abspath[:(-1*len(name))]
run_sdp(name, path, home)
else:
print("Dataset for SDP not specified, running small Otti dataset")
direc = os.fsencode(home+"/datasets/SDP/small/")
for name in os.listdir(direc):
f = name.decode('UTF-8')
path = direc.decode('UTF-8')
run_sdp(f, path, home)
def run_sgd(home, cfile, wfile, dataset, c1, c2, seed, eta0, maxiter, tol,
prob_check="0"):
subprocess.run(["python3", home+"/codegen/sgdcodegen.py", cfile,\
wfile,dataset, c1, c2, seed, eta0, maxiter, tol, prob_check])
subprocess.run(["mv", cfile, home+"/out/cfiles/"])
subprocess.run(["mv", wfile, home+"/out/wit/"])
print("Compile, solve, and prove " + dataset)
os.chdir(home+"/rust-circ/")
subprocess.run("./target/release/examples/circ --inputs "+home+"/out/wit/"+wfile+" "+home+"/out/cfiles/"+cfile+" r1cs --action spartan", shell=True)
def parse_sgd_json(home,json_file,prob_check=0):
with open(json_file) as f:
data = json.load(f)
for dataset in data:
cfile = dataset+".c"
wfile = dataset+".in"
if prob_check:
cfile = "prob_"+cfile
wfile = "prob_"+wfile
run_sgd(home, cfile, wfile, dataset,\
str(data[dataset]["classes"][0]),\
str(data[dataset]["classes"][1]),\
str(data[dataset]["seed"]), str(data[dataset]["eta0"]),\
str(data[dataset]["maxiter"]),\
str(data[dataset]["tol"]), str(prob_check))
def parse_sgd(home,size,custom=None):
if size == Size.SMALL:
print("Running SGD small Otti dataset")
json_file = (home+"/datasets/SGD/pmlb-small.json")
parse_sgd_json(home,json_file)
elif size == Size.FULL:
print("Running SGD full Otti dataset")
json_file = (home+"/datasets/SGD/pmlb-full.json")
parse_sgd_json(home,json_file)
print("Running SGD probablistic Otti dataset")
json_file = (home+"/datasets/SGD/pmlb-prob.json")
parse_sgd_json(home,json_file,1)
elif custom != None:
print("SGD custom data not available")
return
else:
print("Dataset for SDP not specified, running small Otti dataset")
json_file = (home+"/datasets/SGD/pmlb-small.json")
parse_sgd_json(home,json_file)
if __name__ == "__main__":
home = os. getcwd()
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument("--small", action='store_true')
group.add_argument("--full", action='store_true')
group.add_argument("--custom", type=str)
parser.add_argument("--lp", action='store_true')
parser.add_argument("--sdp", action='store_true')
parser.add_argument("--sgd", action='store_true')
args = parser.parse_args()
if not os.path.isdir(home+"/out"):
subprocess.run(["mkdir", home+"/out"])
if not os.path.isdir(home+"/out/cfiles"):
subprocess.run(["mkdir", home+"/out/cfiles"])
if not os.path.isdir(home+"/out/zkif"):
subprocess.run(["mkdir", home+"/out/zkif"])
if not os.path.isdir(home+"/out/wit"):
subprocess.run(["mkdir", home+"/out/wit"])
size = -1;
if args.small:
size = Size.SMALL;
if args.full:
size = Size.FULL;
if args.lp:
inputs = parse_lp(home,size,args.custom)
run_lp(home, inputs)
elif args.sdp:
parse_sdp(home,size,args.custom)
subprocess.run(["rm", home+"/compiler/C", home+"/compiler/x", home+"/compiler/w"])
elif args.sgd:
parse_sgd(home,size,args.custom)
else:
parser.print_help()