def _do_test_verification_key_proto_encode_decode( self, vk: zksnark.GenericVerificationKey, snark: zksnark.IZKSnarkProvider) -> None: vk_proto = snark.verification_key_to_proto(vk) vk_decoded = snark.verification_key_from_proto(vk_proto) # For now, compare as json to brush over tuple-list differences. self.assertEqual(json.dumps(vk), json.dumps(vk_decoded))
def _do_test_verification_key_proto_encode_decode( self, vk: IVerificationKey, snark: IZKSnarkProvider) -> None: vk_proto = snark.verification_key_to_proto(vk) vk_decoded = snark.verification_key_from_proto(vk_proto) # For now, compare as json to brush over tuple-list differences. self.assertEqual(vk.to_json_dict(), vk_decoded.to_json_dict())
def load_verification_key(zksnark: IZKSnarkProvider, vk_file: str) -> IVerificationKey: """ Load a JSON verification key from a file. """ with open(vk_file, "r") as vk_f: return zksnark.verification_key_from_json_dict(json.load(vk_f))
def get_mix_parameters_components( zeth_client: MixerClient, prover_client: ProverClient, zksnark: IZKSnarkProvider, mk_tree: MerkleTree, sender_ownership_keypair: OwnershipKeyPair, inputs: List[Tuple[int, ZethNote]], outputs: List[Tuple[ZethAddressPub, EtherValue]], v_in: EtherValue, v_out: EtherValue, compute_h_sig_cb: Optional[ComputeHSigCB] = None ) -> Tuple[ZethNote, ZethNote, ExtendedProof, JoinsplitSigKeyPair]: """ Manually create the components required for MixParameters. The tests below manipulate these to create custom MixParameters as part of attacks. """ mix_call_desc = MixCallDescription(mk_tree, sender_ownership_keypair, inputs, outputs, v_in, v_out, compute_h_sig_cb) prover_inputs, signing_keypair = zeth_client.create_prover_inputs( mix_call_desc) ext_proof_proto = prover_client.get_proof(prover_inputs) ext_proof = zksnark.extended_proof_from_proto(ext_proof_proto) return (prover_inputs.js_outputs[0], prover_inputs.js_outputs[1], ext_proof, signing_keypair)
def get_nested_verification_key_hash(self, nested_zksnark: IZKSnarkProvider, vk: IVerificationKey) -> str: with grpc.insecure_channel(self.endpoint) as channel: stub = aggregator_pb2_grpc.AggregatorStub(channel) # type: ignore vk_proto = nested_zksnark.verification_key_to_proto(vk) vk_hash_json = stub.GetNestedVerificationKeyHash(vk_proto).hash return json.loads(vk_hash_json)
def nested_transaction_to_proto( zksnark: IZKSnarkProvider, tx: NestedTransaction) -> aggregator_pb2.NestedTransaction: assert isinstance(tx, NestedTransaction) tx_proto = aggregator_pb2.NestedTransaction() tx_proto.application_name = tx.app_name tx_proto.extended_proof.CopyFrom( # pylint: disable=no-member zksnark.extended_proof_to_proto(tx.ext_proof)) tx_proto.parameters = tx.parameters tx_proto.fee_in_wei = tx.fee_in_wei return tx_proto
def _proof_and_inputs_to_bytes(snark: IZKSnarkProvider, pp: PairingParameters, extproof: ExtendedProof) -> Tuple[bytes, bytes]: """ Given a proof object, compute the byte encodings of the properties excluding "inputs", and the byte encoding of the "inputs". These are used when hashing the mixer call parameters for signing, so must match what happens in the mixer contract. """ # TODO: avoid duplicating this encoding to evm parameters proof = extproof.proof return \ message_to_bytes(snark.proof_to_contract_parameters(proof, pp)), \ message_to_bytes(hex_list_to_uint256_list(extproof.inputs))
def aggregated_transaction_from_proto( zksnark: IZKSnarkProvider, aggregated_transaction_proto: aggregator_pb2.AggregatedTransaction ) -> AggregatedTransaction: """ Convert a generic protobuf AggregatedTransactionRequest to an in-memory AggregatedTransaction """ app_name = aggregated_transaction_proto.application_name extproof = zksnark.extended_proof_from_proto( aggregated_transaction_proto.extended_proof) nested_parameters = list(aggregated_transaction_proto.nested_parameters) return AggregatedTransaction(app_name, extproof, nested_parameters)
def deploy( web3: Any, zksnark: IZKSnarkProvider, pp: PairingParameters, vk: IVerificationKey, eth_addr: str, eth_private_key: Optional[bytes] ) -> Tuple[DispatcherContract, InstanceDescription]: """ Deploy the contract, returning an instance of this wrapper, and a description (which can be saved to a file to later instantiate). """ vk_evm = zksnark.verification_key_to_contract_parameters(vk, pp) instance_desc = InstanceDescription.deploy( web3, DISPATCHER_SOURCE_FILE, "ZecaleDispatcher", eth_addr, eth_private_key, DISPATCHER_DEPLOY_GAS, {"allow_paths": CONTRACTS_DIR}, [vk_evm]) return DispatcherContract(web3, instance_desc, zksnark), instance_desc
def deposit_parameters_to_contract_arguments( zksnark: IZKSnarkProvider, pp: PairingParameters, deposit_parameters: DepositParameters) -> List[Any]: """ Convert DepositParameters to a list of eth ABI objects which can be passed to the contract's deposit method. """ proof_contract_params = zksnark.proof_to_contract_parameters( deposit_parameters.proof, pp) return [ proof_contract_params, deposit_parameters.proof, deposit_parameters.zklay_address, deposit_parameters.amount, deposit_parameters.ciphertexts ]
def register_application(self, nested_zksnark: IZKSnarkProvider, vk: IVerificationKey, app_name: str) -> None: """ Register an application. Throw an error with message if this fails for any reason. """ app_desc = aggregator_pb2.ApplicationDescription() app_desc.application_name = app_name app_desc.vk.CopyFrom(nested_zksnark.verification_key_to_proto(vk)) \ # pylint: disable=no-member with grpc.insecure_channel(self.endpoint) as channel: stub = aggregator_pb2_grpc.AggregatorStub(channel) # type: ignore stub.RegisterApplication(app_desc)
def mix_parameters_as_contract_arguments( zksnark: IZKSnarkProvider, mix_parameters: MixParameters) -> List[Any]: """ Convert MixParameters to a list of eth ABI objects which can be passed to the contract's mix method. """ proof_params: List[Any] = zksnark.mixer_proof_parameters( mix_parameters.extended_proof) proof_params.extend([ verification_key_as_mix_parameter(mix_parameters.signature_vk), signature_as_mix_parameter(mix_parameters.signature), hex_to_int(mix_parameters.extended_proof["inputs"]), mix_parameters.ciphertexts ]) return proof_params
def mix_parameters_to_contract_arguments( zksnark: IZKSnarkProvider, pp: PairingParameters, mix_parameters: MixParameters) -> List[Any]: """ Convert MixParameters to a list of eth ABI objects which can be passed to the contract's mix method. """ proof_contract_params = zksnark.proof_to_contract_parameters( mix_parameters.extended_proof.proof, pp) return [ proof_contract_params, signing.verification_key_as_mix_parameter(mix_parameters.signature_vk), signing.signature_as_mix_parameter(mix_parameters.signature), mix_parameters.public_data, mix_parameters.ciphertexts, ]
def _do_test_ext_proof_proto_encode_decode( self, proof: ExtendedProof, snark: IZKSnarkProvider) -> None: proof_proto = snark.extended_proof_to_proto(proof) proof_decoded = snark.extended_proof_from_proto(proof_proto) self.assertEqual(proof.to_json_dict(), proof_decoded.to_json_dict())
def _do_test_proof_proto_encode_decode( self, proof: zksnark.GenericProof, snark: zksnark.IZKSnarkProvider) -> None: proof_proto = snark.proof_to_proto(proof) proof_decoded = snark.proof_from_proto(proof_proto) self.assertEqual(json.dumps(proof), json.dumps(proof_decoded))
def get_verification_key( self, wrapper_zksnark: IZKSnarkProvider) -> IVerificationKey: with grpc.insecure_channel(self.endpoint) as channel: stub = aggregator_pb2_grpc.AggregatorStub(channel) # type: ignore vk_proto = stub.GetVerificationKey(empty_pb2.Empty()) return wrapper_zksnark.verification_key_from_proto(vk_proto)