@@ -196,11 +196,8 @@ def __post_init__(self):
196
196
def curve_id (self ) -> CurveID :
197
197
return self .alpha .curve_id
198
198
199
- def from_json (file_path : str | Path ) -> "Groth16VerifyingKey" :
200
- path = Path (file_path )
199
+ def from_dict (data : dict ) -> "Groth16VerifyingKey" :
201
200
try :
202
- with path .open ("r" ) as f :
203
- data = json .load (f )
204
201
curve_id = try_guessing_curve_id_from_json (data )
205
202
try :
206
203
verifying_key = find_item_from_key_patterns (data , ["verifying_key" ])
@@ -237,15 +234,22 @@ def from_json(file_path: str | Path) -> "Groth16VerifyingKey":
237
234
for point in find_item_from_key_patterns (g1_points , ["K" ])
238
235
],
239
236
)
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 )
240
245
except FileNotFoundError :
241
246
cwd = os .getcwd ()
242
247
print (f"Current working directory: { cwd } " )
243
248
print (f"Attempted to access file: { os .path .abspath (file_path )} " )
244
249
raise FileNotFoundError (f"The file { file_path } was not found." )
245
250
except json .JSONDecodeError :
246
251
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 )
249
253
250
254
def serialize_to_cairo (self ) -> str :
251
255
# Precompute M = miller_loop(public_pair)
@@ -308,64 +312,73 @@ def __post_init__(self):
308
312
), f"All points must be on the same curve, got { self .a .curve_id } , { self .b .curve_id } , { self .c .curve_id } "
309
313
self .curve_id = self .a .curve_id
310
314
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
+
311
358
def from_json (
312
359
proof_path : str | Path , public_inputs_path : str | Path = None
313
360
) -> "Groth16Proof" :
314
361
path = Path (proof_path )
315
362
try :
316
363
with path .open ("r" ) as f :
317
364
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 :
347
370
if public_inputs_path is not None :
348
371
with Path (public_inputs_path ).open ("r" ) as f :
349
372
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 } " )
357
373
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
365
375
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." )
367
377
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 )
369
382
370
383
def _from_risc0 (
371
384
seal : bytes ,
0 commit comments