-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_setup_multi.py
More file actions
345 lines (291 loc) · 13 KB
/
Copy pathbuild_setup_multi.py
File metadata and controls
345 lines (291 loc) · 13 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
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
#!/usr/bin/env python3
"""
build_setup_multi.py
Compile a Circom circuit (Allowed / VerifySign / Full) and run Groth16 setup.
Default PTAU policy:
- Allowed : local ceremony by default (small circuits)
- VerifySign : download prebuilt PTAU by default
- Full : download prebuilt PTAU by default
Override with:
--ptau-source download
--ptau-source local
Download source (default template):
https://storage.googleapis.com/zkevm/ptau/powersOfTau28_hez_final_{K}.ptau
(You can override with --ptau-url-template)
"""
import argparse
import math
import os
import re
import secrets
import shutil
import subprocess
from pathlib import Path
from urllib.request import urlopen, Request
class BuildError(RuntimeError): ...
def run(cmd: list[str], cwd: str | None = None, stdin_input: str | None = None) -> str:
try:
print(">>", " ".join(cmd))
if stdin_input is not None:
# For commands that need stdin input (like snarkjs contribute)
proc = subprocess.Popen(
cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
cwd=cwd
)
out, _ = proc.communicate(input=stdin_input)
if proc.returncode != 0:
raise subprocess.CalledProcessError(proc.returncode, cmd, out)
print(out.strip())
return out
else:
out = subprocess.check_output(cmd, stderr=subprocess.STDOUT, text=True, cwd=cwd)
print(out.strip())
return out
except subprocess.CalledProcessError as e:
raise BuildError(f"Command failed: {' '.join(cmd)}\n{e.output}") from e
def ensure_tools():
# circom: must succeed normally
run(["circom", "--version"])
# snarkjs: some builds exit non-zero on --version; tolerate that
snarkjs_path = shutil.which("snarkjs")
if not snarkjs_path:
raise BuildError("Required tool 'snarkjs' not found on PATH.")
try:
out = run(["snarkjs", "--version"])
except BuildError as e:
# Accept if output looks like snarkjs banner/usage
text = str(e)
if "snarkjs@" in text or "Usage:" in text or "Full Command" in text:
print("snarkjs present (non-zero exit on --version is OK).")
else:
raise
def parse_constraints(r1cs_info_output: str) -> int | None:
m = re.search(r"# of Constraints:\s+(\d+)", r1cs_info_output)
return int(m.group(1)) if m else None
def choose_k(constraints: int, user_k: int | None) -> int:
if user_k:
return user_k
k = math.ceil(math.log2(max(1, constraints))) + 1 # minimal + 1 headroom
return max(12, k)
def compile_circuit(circom_file: Path, outdir: Path, libroots: list[Path], force: bool):
circ_name = circom_file.stem
wasm_dir = outdir / f"{circ_name}_js"
r1cs = outdir / f"{circ_name}.r1cs"
sym = outdir / f"{circ_name}.sym"
wasm = wasm_dir / f"{circ_name}.wasm"
if force:
if wasm_dir.exists(): shutil.rmtree(wasm_dir, ignore_errors=True)
for p in (r1cs, sym):
if p.exists(): p.unlink()
if r1cs.exists() and sym.exists() and wasm.exists():
print(f"Compile skipped ({circ_name} artifacts already exist).")
return r1cs, wasm, sym
cmd = ["circom", str(circom_file), "--r1cs", "--wasm", "--sym", "-o", str(outdir)]
# IMPORTANT: pass library ROOT folders (e.g., 'circomlib'), not their 'circuits/' subfolder
for lib in libroots:
cmd.extend(["-l", str(lib)])
run(cmd)
return r1cs, wasm, sym
# -------------------- PTAU handling --------------------
def _format_bytes(num: int) -> str:
# Simple human-readable size helper
units = ["B", "KB", "MB", "GB"]
value = float(num)
for unit in units:
if value < 1024 or unit == units[-1]:
return f"{value:.1f} {unit}"
value /= 1024
def _download(url: str, dest: Path):
print(f"Downloading PTAU: {url}")
req = Request(url, headers={"User-Agent": "build-setup/1.0"})
chunk_size = 1 << 20 # 1 MiB
with urlopen(req) as resp, open(dest, "wb") as f:
total = resp.headers.get("Content-Length")
total_int = int(total) if total and total.isdigit() else None
downloaded = 0
last_percent = -5
while True:
chunk = resp.read(chunk_size)
if not chunk:
break
f.write(chunk)
downloaded += len(chunk)
if total_int:
percent = int(downloaded * 100 / total_int)
if percent - last_percent >= 5:
print(f" ... {percent:3d}% ({_format_bytes(downloaded)} / {_format_bytes(total_int)})")
last_percent = percent
else:
print(f" ... downloaded {_format_bytes(downloaded)}")
size = dest.stat().st_size
print(f"Saved: {dest} ({_format_bytes(size)})")
def download_ptau(k: int, dest: Path, url_template: str):
url = url_template.format(K=k)
_download(url, dest)
# Verify the downloaded PTAU file
v = run(["snarkjs", "powersoftau", "verify", str(dest)])
if "Powers Of tau file OK" not in v and "Powers of Tau Ok" not in v:
raise BuildError("Downloaded PTAU did not verify with snarkjs.")
print("PTAU verified")
def prepare_pot_local(k: int, force: bool, prefix: str | None = None, ptau_dir: Path | None = None) -> Path:
ptau_prefix = prefix or f"pot{k}"
if ptau_dir:
ptau_dir.mkdir(parents=True, exist_ok=True)
pot0 = ptau_dir / f"{ptau_prefix}_0000.ptau"
pot1 = ptau_dir / f"{ptau_prefix}_0001.ptau"
potfin = ptau_dir / f"{ptau_prefix}_final.ptau"
else:
pot0 = Path(f"{ptau_prefix}_0000.ptau")
pot1 = Path(f"{ptau_prefix}_0001.ptau")
potfin = Path(f"{ptau_prefix}_final.ptau")
if force:
for p in (pot0, pot1, potfin):
if p.exists(): p.unlink()
if not pot0.exists():
run(["snarkjs", "powersoftau", "new", "bn128", str(k), str(pot0), "-v"])
else:
print(f"Reusing {pot0}")
if not pot1.exists():
# Provide entropy via stdin (non-interactive mode)
# Generate random entropy string for the contribution
entropy = secrets.token_hex(32) + "\n"
run(["snarkjs", "powersoftau", "contribute", str(pot0), str(pot1), "--name=First contribution", "-v"], stdin_input=entropy)
else:
print(f"Reusing {pot1}")
if not potfin.exists():
run(["snarkjs", "powersoftau", "prepare", "phase2", str(pot1), str(potfin), "-v"])
else:
print(f"Reusing {potfin}")
return potfin
def obtain_ptau(k: int, source: str, url_template: str, force: bool, prefix: str | None = None, ptau_dir: Path | None = None) -> Path:
"""
source: 'download' or 'local'
If 'download': fetch 'powersOfTau28_hez_final_{K}.ptau' and store as 'pot{K}_final.ptau'
If 'local' : run local ceremony to produce 'pot{K}_final.ptau'
"""
ptau_prefix = prefix or f"pot{k}"
if ptau_dir:
ptau_dir.mkdir(parents=True, exist_ok=True)
potfin = ptau_dir / f"{ptau_prefix}_final.ptau"
else:
potfin = Path(f"{ptau_prefix}_final.ptau")
if force and potfin.exists():
potfin.unlink()
if source == "download":
if not potfin.exists():
download_ptau(k, potfin, url_template)
else:
print(f"Reusing {potfin} (download mode)")
return potfin
elif source == "local":
return prepare_pot_local(k, force=force, prefix=ptau_prefix, ptau_dir=ptau_dir)
else:
raise BuildError(f"Unknown ptau source: {source}")
# -------------------- Groth16 setup --------------------
def groth16_setup(r1cs: Path, potfin: Path, circ_name: str, zkey_out: Path, vk_json: Path, force: bool, contrib_name: str, setup_dir: Path):
zkey0 = setup_dir / f"{circ_name}_0000.zkey"
if force:
for p in (zkey0, zkey_out, vk_json):
if p.exists(): p.unlink()
if not zkey0.exists():
run(["snarkjs", "groth16", "setup", str(r1cs), str(potfin), str(zkey0)])
else:
print(f"Reusing {zkey0}")
if not zkey_out.exists():
# Provide entropy via stdin (non-interactive mode)
entropy = secrets.token_hex(32) + "\n"
run(["snarkjs", "zkey", "contribute", str(zkey0), str(zkey_out), f"--name={contrib_name}", "-v"], stdin_input=entropy)
else:
print(f"Reusing {zkey_out}")
if not vk_json.exists():
run(["snarkjs", "zkey", "export", "verificationkey", str(zkey_out), str(vk_json)])
else:
print(f"Reusing {vk_json}")
# -------------------- Main --------------------
def main():
parser = argparse.ArgumentParser(description="Compile + Groth16 setup for Allowed / VerifySign / Freshness / Full.")
parser.add_argument("-c", "--circuit", choices=["Allowed", "VerifySign", "Freshness", "Full"], required=True)
parser.add_argument("--circuits-dir", default="circuits", help="Directory containing your .circom files")
parser.add_argument("-l", "--libroot", action="append", default=[],
help="Library ROOT to pass to circom via -l (repeatable). Example: -l circomlib -l circom-ecdsa-p256")
parser.add_argument("--outdir", default="circuits", help="Output directory for r1cs/wasm/sym")
parser.add_argument("--pot-k", type=int, default=None, help="Force K for Powers of Tau (2^K). If omitted, auto-choose from constraints.")
parser.add_argument("--ptau-source", choices=["auto", "download", "local"], default="auto",
help="PTAU acquisition mode. 'auto' defaults to download for VerifySign/Full, local for Allowed.")
parser.add_argument("--ptau-url-template",
default="https://storage.googleapis.com/zkevm/ptau/powersOfTau28_hez_final_{K}.ptau",
help="URL template for downloading PTAU (use {K} placeholder).")
parser.add_argument("--ptau-prefix", default=None, help="Optional PTAU local filename prefix (default: pot{K})")
parser.add_argument("--contrib-name", default="1st Contributor", help="Name for zkey contribution")
parser.add_argument("--force", action="store_true", help="Rebuild / overwrite existing artifacts")
args = parser.parse_args()
ensure_tools()
# Map circuit choice → file path
circuits_dir = Path(args.circuits_dir)
circ_file = circuits_dir / f"{args.circuit}.circom"
if not circ_file.exists():
raise BuildError(f"Circuit not found: {circ_file}")
# Library roots (default per circuit if user didn’t pass any)
default_libs = {
"Allowed": ["circomlib"],
"Freshness": ["circomlib"],
"VerifySign": ["circomlib", "circom-ecdsa-p256"],
"Full": ["circomlib", "circom-ecdsa-p256"],
}
if args.libroot:
libroots = [Path(p) for p in args.libroot]
else:
libroots = [Path(p) for p in default_libs.get(args.circuit, ["circomlib"])]
for lib in libroots:
if not lib.exists():
raise BuildError(f"Library root not found: {lib} (tip: pass the ROOT folder, not its 'circuits/' subfolder)")
# Create setup/<circuit_name>/ directory structure
circ_name = circ_file.stem # e.g., Allowed / VerifySign / Full
setup_dir = Path("setup") / circ_name
setup_dir.mkdir(parents=True, exist_ok=True)
# PTAU files go in setup/ptau/ (shared across circuits)
ptau_dir = Path("setup") / "ptau"
ptau_dir.mkdir(parents=True, exist_ok=True)
# Use setup/<circuit_name>/ as the output directory
outdir = setup_dir
print(f"Output directory: {outdir}")
# 1) Compile
r1cs, wasm, sym = compile_circuit(circ_file, outdir, libroots, force=args.force)
# 2) Inspect constraints & decide K
info_out = run(["snarkjs", "r1cs", "info", str(r1cs)])
constraints = parse_constraints(info_out) or 0
chosen_k = choose_k(constraints, args.pot_k)
print(f"Chosen K = {chosen_k} (2^K = {1<<chosen_k}) for constraints ≈ {constraints}")
# 3) PTAU source selection
if args.ptau_source == "auto":
ptau_mode = "download" if args.circuit in ("VerifySign", "Full") else "local"
else:
ptau_mode = args.ptau_source
print(f"PTAU mode: {ptau_mode}")
potfin = obtain_ptau(
k=chosen_k,
source=ptau_mode,
url_template=args.ptau_url_template,
force=args.force,
prefix=args.ptau_prefix,
ptau_dir=ptau_dir
)
# 4) Groth16 setup → zkey + vk (per circuit name)
# Save zkey files in setup/<circuit_name>/
zkey_out = setup_dir / f"{circ_name}_final.zkey"
vk_json = setup_dir / f"{circ_name}_verification_key.json"
groth16_setup(r1cs, potfin, circ_name, zkey_out, vk_json, force=args.force, contrib_name=args.contrib_name, setup_dir=setup_dir)
print("\nSetup complete.")
print(f" Circuit : {circ_name}")
print(f" R1CS : {r1cs}")
print(f" WASM : {wasm}")
print(f" SYM : {sym}")
print(f" PTAU : {potfin}")
print(f" ZKey : {zkey_out}")
print(f" VK JSON : {vk_json}")
if __name__ == "__main__":
main()