Skip to content

Commit 6135bd6

Browse files
Refactored from_json methods of Groth16Proof and Groth16VerifyingKeys classes (#199)
Co-authored-by: casiojapi <[email protected]>
1 parent 66abfdc commit 6135bd6

File tree

1 file changed

+64
-51
lines changed

1 file changed

+64
-51
lines changed

hydra/garaga/starknet/groth16_contract_generator/parsing_utils.py

+64-51
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,8 @@ def __post_init__(self):
196196
def curve_id(self) -> CurveID:
197197
return self.alpha.curve_id
198198

199-
def from_json(file_path: str | Path) -> "Groth16VerifyingKey":
200-
path = Path(file_path)
199+
def from_dict(data: dict) -> "Groth16VerifyingKey":
201200
try:
202-
with path.open("r") as f:
203-
data = json.load(f)
204201
curve_id = try_guessing_curve_id_from_json(data)
205202
try:
206203
verifying_key = find_item_from_key_patterns(data, ["verifying_key"])
@@ -237,15 +234,22 @@ def from_json(file_path: str | Path) -> "Groth16VerifyingKey":
237234
for point in find_item_from_key_patterns(g1_points, ["K"])
238235
],
239236
)
237+
except KeyError as e:
238+
raise KeyError(f"The key {e} is missing from the JSON data.")
239+
240+
def from_json(file_path: str | Path) -> "Groth16VerifyingKey":
241+
path = Path(file_path)
242+
try:
243+
with path.open("r") as f:
244+
data = json.load(f)
240245
except FileNotFoundError:
241246
cwd = os.getcwd()
242247
print(f"Current working directory: {cwd}")
243248
print(f"Attempted to access file: {os.path.abspath(file_path)}")
244249
raise FileNotFoundError(f"The file {file_path} was not found.")
245250
except json.JSONDecodeError:
246251
raise ValueError(f"The file {file_path} does not contain valid JSON.")
247-
except KeyError as e:
248-
raise KeyError(f"The key {e} is missing from the JSON data.")
252+
return Groth16VerifyingKey.from_dict(data)
249253

250254
def serialize_to_cairo(self) -> str:
251255
# Precompute M = miller_loop(public_pair)
@@ -308,64 +312,73 @@ def __post_init__(self):
308312
), f"All points must be on the same curve, got {self.a.curve_id}, {self.b.curve_id}, {self.c.curve_id}"
309313
self.curve_id = self.a.curve_id
310314

315+
def from_dict(
316+
data: dict, public_inputs: None | list | dict = None
317+
) -> "Groth16Proof":
318+
curve_id = try_guessing_curve_id_from_json(data)
319+
try:
320+
proof = find_item_from_key_patterns(data, ["proof"])
321+
except ValueError:
322+
proof = data
323+
324+
try:
325+
seal = io.to_hex_str(find_item_from_key_patterns(data, ["seal"]))
326+
image_id = io.to_hex_str(find_item_from_key_patterns(data, ["image_id"]))
327+
journal = io.to_hex_str(find_item_from_key_patterns(data, ["journal"]))
328+
329+
return Groth16Proof._from_risc0(
330+
seal=bytes.fromhex(seal[2:]),
331+
image_id=bytes.fromhex(image_id[2:]),
332+
journal=bytes.fromhex(journal[2:]),
333+
)
334+
except ValueError:
335+
pass
336+
except KeyError:
337+
pass
338+
except Exception as e:
339+
print(f"Error: {e}")
340+
raise e
341+
342+
if public_inputs is not None:
343+
if isinstance(public_inputs, dict):
344+
public_inputs = list(public_inputs.values())
345+
elif isinstance(public_inputs, list):
346+
pass
347+
else:
348+
raise ValueError(f"Invalid public inputs format: {public_inputs}")
349+
else:
350+
public_inputs = find_item_from_key_patterns(data, ["public"])
351+
return Groth16Proof(
352+
a=try_parse_g1_point_from_key(proof, ["a"], curve_id),
353+
b=try_parse_g2_point_from_key(proof, ["b"], curve_id),
354+
c=try_parse_g1_point_from_key(proof, ["c", "Krs"], curve_id),
355+
public_inputs=[io.to_int(pub) for pub in public_inputs],
356+
)
357+
311358
def from_json(
312359
proof_path: str | Path, public_inputs_path: str | Path = None
313360
) -> "Groth16Proof":
314361
path = Path(proof_path)
315362
try:
316363
with path.open("r") as f:
317364
data = json.load(f)
318-
# print(f"data: {data}")
319-
# print(f"data.keys(): {data.keys()}")
320-
curve_id = try_guessing_curve_id_from_json(data)
321-
322-
try:
323-
proof = find_item_from_key_patterns(data, ["proof"])
324-
except ValueError:
325-
proof = data
326-
327-
try:
328-
seal = io.to_hex_str(find_item_from_key_patterns(data, ["seal"]))
329-
image_id = io.to_hex_str(
330-
find_item_from_key_patterns(data, ["image_id"])
331-
)
332-
journal = io.to_hex_str(find_item_from_key_patterns(data, ["journal"]))
333-
334-
return Groth16Proof._from_risc0(
335-
seal=bytes.fromhex(seal[2:]),
336-
image_id=bytes.fromhex(image_id[2:]),
337-
journal=bytes.fromhex(journal[2:]),
338-
)
339-
except ValueError:
340-
pass
341-
except KeyError:
342-
pass
343-
except Exception as e:
344-
print(f"Error: {e}")
345-
raise e
346-
365+
except FileNotFoundError:
366+
raise FileNotFoundError(f"The file {proof_path} was not found.")
367+
except json.JSONDecodeError:
368+
raise ValueError(f"The file {proof_path} does not contain valid JSON.")
369+
try:
347370
if public_inputs_path is not None:
348371
with Path(public_inputs_path).open("r") as f:
349372
public_inputs = json.load(f)
350-
print(f"public_inputs: {public_inputs}")
351-
if isinstance(public_inputs, dict):
352-
public_inputs = list(public_inputs.values())
353-
elif isinstance(public_inputs, list):
354-
pass
355-
else:
356-
raise ValueError(f"Invalid public inputs format: {public_inputs}")
357373
else:
358-
public_inputs = find_item_from_key_patterns(data, ["public"])
359-
return Groth16Proof(
360-
a=try_parse_g1_point_from_key(proof, ["a"], curve_id),
361-
b=try_parse_g2_point_from_key(proof, ["b"], curve_id),
362-
c=try_parse_g1_point_from_key(proof, ["c", "Krs"], curve_id),
363-
public_inputs=[io.to_int(pub) for pub in public_inputs],
364-
)
374+
public_inputs = None
365375
except FileNotFoundError:
366-
raise FileNotFoundError(f"The file {proof_path} was not found.")
376+
raise FileNotFoundError(f"The file {public_inputs_path} was not found.")
367377
except json.JSONDecodeError:
368-
raise ValueError(f"The file {proof_path} does not contain valid JSON.")
378+
raise ValueError(
379+
f"The file {public_inputs_path} does not contain valid JSON."
380+
)
381+
return Groth16Proof.from_dict(data, public_inputs)
369382

370383
def _from_risc0(
371384
seal: bytes,

0 commit comments

Comments
 (0)