Created
July 31, 2023 07:35
-
-
Save doutv/d3ca6fa3a740c39266cd25c3480f681f to your computer and use it in GitHub Desktop.
Translate R1CS .r1cs and witness .wtns to DIZK JSON format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See my blog post for details: