Skip to content

Instantly share code, notes, and snippets.

@doutv
Created July 31, 2023 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doutv/d3ca6fa3a740c39266cd25c3480f681f to your computer and use it in GitHub Desktop.
Save doutv/d3ca6fa3a740c39266cd25c3480f681f to your computer and use it in GitHub Desktop.
Translate R1CS .r1cs and witness .wtns to DIZK JSON format
import json
from operator import is_not
from functools import partial
name = input("Enter filename: ")
with open(f"{name}.r1cs.json", "r") as f:
r1cs = json.load(f)
with open(f"{name}.wtns.json", "r") as f:
wtns = json.load(f)
public_input_len = r1cs["nPubInputs"] + 1
res = {
"primary_input": wtns[:public_input_len],
"aux_input": wtns[public_input_len:],
}
# When constraints is large (e.g. 1M constraints)
# The r1cs json format is different
if isinstance(r1cs["constraints"], dict) and "arr" in r1cs["constraints"].keys():
constraints = []
for each in r1cs["constraints"]["arr"]:
if each:
l = list(filter(partial(is_not, None), each))
constraints.extend(l)
res["constraints"] = constraints
else:
res["constraints"] = r1cs["constraints"]
assert(len(res["primary_input"]) + len(res["aux_input"])== r1cs["nVars"])
assert(len(res["constraints"]) == r1cs["nConstraints"])
res["header"] = [
str(len(res["primary_input"])),
str(len(res["aux_input"])),
str(len(res["constraints"]))
]
with open(f"{name}.dizk.json", "w") as f:
json.dump(res, f, indent=4)
@doutv
Copy link
Author

doutv commented Jul 31, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment