11 from __future__
import annotations
13 g1_point_to_contract_parameters, g1_point_from_proto, g1_point_to_proto, \
14 g2_point_to_contract_parameters, g2_point_from_proto, g2_point_to_proto, \
17 from zeth.api import snark_messages_pb2
20 from abc
import (ABC, abstractmethod)
21 from typing
import Dict, List, Any, cast
27 PAIRING_NAME_TO_CONTRACT_NAME = {
28 "alt-bn128":
"AltBN128",
29 "bls12-377":
"BLS12_377",
35 Abstract base class of verification keys
49 Abstract base class of proofs
63 A GenericProof and associated inputs
65 def __init__(self, proof: IProof, inputs: List[str]):
77 zksnark: IZKSnarkProvider,
78 json_dict: Dict[str, Any]) -> ExtendedProof:
80 proof=zksnark.proof_from_json_dict(json_dict[
"proof"]),
81 inputs=json_dict[
"inputs"])
86 Interface to be implemented by specific zk-snark providers. Ideally, the
87 rest of the logic should deal only with this interface and have no
88 understanding of the underlying mechanisms.
95 Get the verifier and mixer contracts for this SNARK.
102 vk: IVerificationKey,
103 pp: PairingParameters) -> List[int]:
109 vk_obj: snark_messages_pb2.VerificationKey) -> IVerificationKey:
115 vk: IVerificationKey) -> snark_messages_pb2.VerificationKey:
121 json_dict: Dict[str, Any]) -> IVerificationKey:
132 ext_proof_proto: snark_messages_pb2.ExtendedProof
139 ext_proof: ExtendedProof) -> snark_messages_pb2.ExtendedProof:
145 proof: IProof, pp: PairingParameters) -> List[int]:
147 Generate the leading parameters to the mix function for this SNARK, from a
169 "alpha": self.
alpha.to_json_list(),
170 "beta": self.
beta.to_json_list(),
171 "delta": self.
delta.to_json_list(),
172 "ABC": [abc.to_json_list()
for abc
in self.
abc],
178 alpha=G1Point.from_json_list(json_dict[
"alpha"]),
179 beta=G2Point.from_json_list(json_dict[
"beta"]),
180 delta=G2Point.from_json_list(json_dict[
"delta"]),
181 abc=[G1Point.from_json_list(abc)
182 for abc
in json_dict[
"ABC"]])
196 "a": self.
a.to_json_list(),
197 "b": self.
b.to_json_list(),
198 "c": self.
c.to_json_list(),
204 a=G1Point.from_json_list(json_dict[
"a"]),
205 b=G2Point.from_json_list(json_dict[
"b"]),
206 c=G1Point.from_json_list(json_dict[
"c"]))
210 return _contract_name(
"Groth16", pp)
214 vk: IVerificationKey,
215 pp: PairingParameters) -> List[int]:
230 vk_obj: snark_messages_pb2.VerificationKey
232 vk = vk_obj.groth16_verification_key
237 abc=[G1Point.from_json_list(abc)
238 for abc
in json.loads(vk.abc_g1)])
242 vk: IVerificationKey) -> snark_messages_pb2.VerificationKey:
244 vk_obj = snark_messages_pb2.VerificationKey()
245 groth16_key = vk_obj.groth16_verification_key
249 groth16_key.abc_g1 = json.dumps([abc.to_json_list()
for abc
in vk.abc])
255 return Groth16.VerificationKey.from_json_dict(json_dict)
259 return Groth16.Proof.from_json_dict(json_dict)
263 ext_proof_proto: snark_messages_pb2.ExtendedProof) -> ExtendedProof:
264 ext_proof = ext_proof_proto.groth16_extended_proof
270 inputs=json.loads(ext_proof.inputs))
274 ext_proof: ExtendedProof) -> snark_messages_pb2.ExtendedProof:
275 proof = ext_proof.proof
277 extproof_proto = snark_messages_pb2.ExtendedProof()
278 proof_proto = extproof_proto.groth16_extended_proof \
283 proof_proto.inputs = json.dumps(ext_proof.inputs)
284 return extproof_proto
288 proof: IProof, pp: PairingParameters) -> List[int]:
320 "a": self.
a.to_json_list(),
321 "b": self.
b.to_json_list(),
322 "c": self.
c.to_json_list(),
323 "g": self.
g.to_json_list(),
324 "gb1": self.
gb1.to_json_list(),
325 "gb2": self.
gb2.to_json_list(),
326 "z": self.
z.to_json_list(),
327 "ic": [ic.to_json_list()
for ic
in self.
ic],
333 a=G2Point.from_json_list(json_dict[
"a"]),
334 b=G1Point.from_json_list(json_dict[
"b"]),
335 c=G2Point.from_json_list(json_dict[
"c"]),
336 g=G2Point.from_json_list(json_dict[
"g"]),
337 gb1=G1Point.from_json_list(json_dict[
"gb1"]),
338 gb2=G2Point.from_json_list(json_dict[
"gb2"]),
339 z=G2Point.from_json_list(json_dict[
"z"]),
340 ic=[G1Point.from_json_list(ic)
341 for ic
in json_dict[
"ic"]])
365 "a": self.
a.to_json_list(),
366 "a_p": self.
a_p.to_json_list(),
367 "b": self.
b.to_json_list(),
368 "b_p": self.
b_p.to_json_list(),
369 "c": self.
c.to_json_list(),
370 "c_p": self.
c_p.to_json_list(),
371 "h": self.
h.to_json_list(),
372 "k": self.
k.to_json_list(),
378 a=G1Point.from_json_list(json_dict[
"a"]),
379 a_p=G1Point.from_json_list(json_dict[
"a_p"]),
380 b=G2Point.from_json_list(json_dict[
"b"]),
381 b_p=G1Point.from_json_list(json_dict[
"b_p"]),
382 c=G1Point.from_json_list(json_dict[
"c"]),
383 c_p=G1Point.from_json_list(json_dict[
"c_p"]),
384 h=G1Point.from_json_list(json_dict[
"h"]),
385 k=G1Point.from_json_list(json_dict[
"k"]))
389 return _contract_name(
"Pghr13", pp)
393 vk: IVerificationKey,
394 pp: PairingParameters) -> List[int]:
405 for ic
in vk.ic], [])
410 vk = vk_obj.pghr13_verification_key
419 ic=[G1Point.from_json_list(ic)
420 for ic
in json.loads(vk.ic)])
424 vk: IVerificationKey) -> snark_messages_pb2.VerificationKey:
425 raise Exception(
"not implemented")
430 return PGHR13.VerificationKey.from_json_dict(json_dict)
434 return PGHR13.Proof.from_json_dict(json_dict)
438 ext_proof_proto: snark_messages_pb2.ExtendedProof) -> ExtendedProof:
439 ext_proof = ext_proof_proto.pghr13_extended_proof
450 inputs=cast(List[str], json.loads(ext_proof.inputs)))
454 ext_proof: ExtendedProof) -> snark_messages_pb2.ExtendedProof:
455 proof = ext_proof.proof
457 extproof_proto = snark_messages_pb2.ExtendedProof()
458 proof_proto = extproof_proto.pghr13_extended_proof \
468 proof_proto.inputs = json.dumps(ext_proof.inputs)
469 return extproof_proto
473 proof: IProof, pp: PairingParameters) -> List[int]:
487 if zksnark_name == constants.PGHR13_ZKSNARK:
489 if zksnark_name == constants.GROTH16_ZKSNARK:
491 raise Exception(f
"unknown zk-SNARK name: {zksnark_name}")
494 def _contract_name(zksnark_name: str, pp: PairingParameters) -> str:
496 Given a snark name fragment (as used in contract naming conventions) and
497 pairing parameters, determine the full contract name.
499 return "Mixer" + zksnark_name + PAIRING_NAME_TO_CONTRACT_NAME[pp.name]