Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
Classes | Functions | Variables
zeth.core.mixer_client Namespace Reference

Classes

class  MixCallDescription
 
class  MixerClient
 
class  MixOutputEvents
 
class  MixParameters
 
class  MixResult
 

Functions

List[Any] mix_parameters_to_contract_arguments (IZKSnarkProvider zksnark, PairingParameters pp, MixParameters mix_parameters)
 
bytes mix_parameters_to_dispatch_parameters (MixParameters mix_parameters)
 
MixResult event_args_to_mix_result (Any event_args)
 
api.JoinsplitInput create_api_joinsplit_input (List[str] merkle_path, int address, api.ZethNote note, OwnershipSecretKey a_sk, bytes nullifier)
 
Tuple[int, api.ZethNote] get_dummy_input_and_address (OwnershipPublicKey a_pk)
 
List[bytes] encrypt_notes (List[Tuple[api.ZethNote, EncryptionPublicKey]] notes)
 
Optional[Tuple[bytes, api.ZethNote]] receive_note (MixOutputEvents out_ev, EncryptionSecretKey receiver_k_sk)
 
Iterator[MixResultget_mix_results (Any web3, Any mixer_instance, int start_block, int end_block, Optional[int] batch_size=None)
 
int joinsplit_sign (IZKSnarkProvider zksnark, PairingParameters pp, JoinsplitSigKeyPair signing_keypair, str sender_eth_address, List[bytes] ciphertexts, ExtendedProof extproof, List[int] public_data, bool for_dispatch_call=False)
 
bytes compute_commitment (api.ZethNote zeth_note, PairingParameters pp)
 
bytes compute_nullifier (api.ZethNote zeth_note, OwnershipSecretKey spending_authority_ask)
 
bytes compute_h_sig (List[bytes] nullifiers, JoinsplitSigVerificationKey sign_vk)
 

Variables

string ZERO_UNITS_HEX = "0000000000000000"
 
string ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"
 
 JoinsplitSigVerificationKey = signing.SigningVerificationKey
 
 JoinsplitSigSecretKey = signing.SigningSecretKey
 
 JoinsplitSigKeyPair = signing.SigningKeyPair
 
 ComputeHSigCB = Callable[[List[bytes], JoinsplitSigVerificationKey], bytes]
 

Function Documentation

◆ compute_commitment()

bytes zeth.core.mixer_client.compute_commitment ( api.ZethNote  zeth_note,
PairingParameters  pp 
)
Used by the recipient of a payment to recompute the commitment and check
the membership in the tree to confirm the validity of a payment

Definition at line 701 of file mixer_client.py.

701 def compute_commitment(zeth_note: api.ZethNote, pp: PairingParameters) -> bytes:
702  """
703  Used by the recipient of a payment to recompute the commitment and check
704  the membership in the tree to confirm the validity of a payment
705  """
706  # inner_k = blake2s(r || a_pk || rho || v)
707  blake = blake2s()
708  blake.update(bytes.fromhex(zeth_note.trap_r))
709  blake.update(bytes.fromhex(zeth_note.apk))
710  blake.update(bytes.fromhex(zeth_note.rho))
711  blake.update(bytes.fromhex(zeth_note.value))
712  cm = blake.digest()
713 
714  cm_field = int.from_bytes(cm, byteorder="big") % pp.scalar_field_mod()
715  return cm_field.to_bytes(int(constants.DIGEST_LENGTH/8), byteorder="big")
716 
717 
Here is the caller graph for this function:

◆ compute_h_sig()

bytes zeth.core.mixer_client.compute_h_sig ( List[bytes]  nullifiers,
JoinsplitSigVerificationKey  sign_vk 
)
Compute h_sig = sha256(nf_1 || ... || nf_{JS_INPUTS} || sign_vk)

Definition at line 734 of file mixer_client.py.

734 def compute_h_sig(
735  nullifiers: List[bytes],
736  sign_vk: JoinsplitSigVerificationKey) -> bytes:
737  """
738  Compute h_sig = sha256(nf_1 || ... || nf_{JS_INPUTS} || sign_vk)
739  """
740  h = sha256()
741  for nf in nullifiers:
742  h.update(nf)
743  h.update(sign_vk.to_bytes())
744  return h.digest()
745 
746 
Here is the call graph for this function:
Here is the caller graph for this function:

◆ compute_nullifier()

bytes zeth.core.mixer_client.compute_nullifier ( api.ZethNote  zeth_note,
OwnershipSecretKey  spending_authority_ask 
)
Returns nf = blake2s(1110 || [a_sk]_252 || rho)

Definition at line 718 of file mixer_client.py.

719  zeth_note: api.ZethNote,
720  spending_authority_ask: OwnershipSecretKey) -> bytes:
721  """
722  Returns nf = blake2s(1110 || [a_sk]_252 || rho)
723  """
724  binary_ask = digest_to_binary_string(spending_authority_ask)
725  first_252bits_ask = binary_ask[:252]
726  left_leg_bin = "1110" + first_252bits_ask
727  left_leg = int(left_leg_bin, 2).to_bytes(32, byteorder='big')
728  blake_hash = blake2s()
729  blake_hash.update(left_leg)
730  blake_hash.update(bytes.fromhex(zeth_note.rho))
731  return blake_hash.digest()
732 
733 
Here is the call graph for this function:
Here is the caller graph for this function:

◆ create_api_joinsplit_input()

api.JoinsplitInput zeth.core.mixer_client.create_api_joinsplit_input ( List[str]  merkle_path,
int  address,
api.ZethNote  note,
OwnershipSecretKey  a_sk,
bytes  nullifier 
)

Definition at line 227 of file mixer_client.py.

228  merkle_path: List[str],
229  address: int,
230  note: api.ZethNote,
231  a_sk: OwnershipSecretKey,
232  nullifier: bytes) -> api.JoinsplitInput:
233  return api.JoinsplitInput(
234  merkle_path=merkle_path,
235  address=address,
236  note=note,
237  spending_ask=ownership_key_as_hex(a_sk),
238  nullifier=nullifier.hex())
239 
240 
Here is the call graph for this function:
Here is the caller graph for this function:

◆ encrypt_notes()

List[bytes] zeth.core.mixer_client.encrypt_notes ( List[Tuple[api.ZethNote, EncryptionPublicKey]]  notes)
Encrypts a set of output notes to be decrypted by the respective receivers.
Returns the ciphertexts corresponding to each note.

Definition at line 601 of file mixer_client.py.

601 def encrypt_notes(
602  notes: List[Tuple[api.ZethNote, EncryptionPublicKey]]) -> List[bytes]:
603  """
604  Encrypts a set of output notes to be decrypted by the respective receivers.
605  Returns the ciphertexts corresponding to each note.
606  """
607 
608  def _encrypt_note(
609  out_note: api.ZethNote, pub_key: EncryptionPublicKey) -> bytes:
610  out_note_bytes = proto_utils.zeth_note_to_bytes(out_note)
611 
612  return encrypt(out_note_bytes, pub_key)
613 
614  ciphertexts = [_encrypt_note(note, pk) for (note, pk) in notes]
615  return ciphertexts
616 
617 
Here is the call graph for this function:
Here is the caller graph for this function:

◆ event_args_to_mix_result()

MixResult zeth.core.mixer_client.event_args_to_mix_result ( Any  event_args)

Definition at line 218 of file mixer_client.py.

218 def event_args_to_mix_result(event_args: Any) -> MixResult:
219  mix_out_args = zip(event_args.commitments, event_args.ciphertexts)
220  out_events = [MixOutputEvents(c, ciph) for (c, ciph) in mix_out_args]
221  return MixResult(
222  new_merkle_root=event_args.root,
223  nullifiers=event_args.nullifiers,
224  output_events=out_events)
225 
226 
Here is the caller graph for this function:

◆ get_dummy_input_and_address()

Tuple[int, api.ZethNote] zeth.core.mixer_client.get_dummy_input_and_address ( OwnershipPublicKey  a_pk)
Create a zeth note and address, for use as circuit inputs where there is no
real input.

Definition at line 241 of file mixer_client.py.

242  a_pk: OwnershipPublicKey) -> Tuple[int, api.ZethNote]:
243  """
244  Create a zeth note and address, for use as circuit inputs where there is no
245  real input.
246  """
247  dummy_note = api.ZethNote(
248  apk=ownership_key_as_hex(a_pk),
249  value=ZERO_UNITS_HEX,
250  rho=_get_dummy_rho(),
251  trap_r=_trap_r_randomness())
252  # Note that the Merkle path is not fully checked against the root by the
253  # circuit since the note value is 0. Hence the address used here is
254  # arbitrary.
255  dummy_note_address = 0
256  return (dummy_note_address, dummy_note)
257 
258 
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_mix_results()

Iterator[MixResult] zeth.core.mixer_client.get_mix_results ( Any  web3,
Any  mixer_instance,
int  start_block,
int  end_block,
Optional[int]   batch_size = None 
)
Iterator for all events generated by 'mix' executions, over some block
range (inclusive of `end_block`). Batch eth RPC calls to avoid too many
calls, and holding huge lists of events in memory.

Definition at line 640 of file mixer_client.py.

640 def get_mix_results(
641  web3: Any,
642  mixer_instance: Any,
643  start_block: int,
644  end_block: int,
645  batch_size: Optional[int] = None) -> Iterator[MixResult]:
646  """
647  Iterator for all events generated by 'mix' executions, over some block
648  range (inclusive of `end_block`). Batch eth RPC calls to avoid too many
649  calls, and holding huge lists of events in memory.
650  """
651  logs = contracts.get_event_logs(
652  web3, mixer_instance, "LogMix", start_block, end_block, batch_size)
653  for event_data in logs:
654  yield event_args_to_mix_result(event_data.args)
655 
656 
Here is the call graph for this function:
Here is the caller graph for this function:

◆ joinsplit_sign()

int zeth.core.mixer_client.joinsplit_sign ( IZKSnarkProvider  zksnark,
PairingParameters  pp,
JoinsplitSigKeyPair  signing_keypair,
str  sender_eth_address,
List[bytes]  ciphertexts,
ExtendedProof  extproof,
List[int]  public_data,
bool   for_dispatch_call = False 
)
Generate a signature on the hash of the ciphertexts, proofs and primary
inputs. This is used to solve transaction malleability. We chose to sign
the hash and not the values themselves for modularity (to use the same code
regardless of whether GROTH16 or PGHR13 proof system is chosen), and sign
the hash of the ciphers and inputs for consistency. If for_dispatch_call is
set, the parameters are to be passed to the Mixer's `dispatch` call in a
later operation (in which proof data is not available), hence proof is
omitted from the signature.

Definition at line 657 of file mixer_client.py.

657 def joinsplit_sign(
658  zksnark: IZKSnarkProvider,
659  pp: PairingParameters,
660  signing_keypair: JoinsplitSigKeyPair,
661  sender_eth_address: str,
662  ciphertexts: List[bytes],
663  extproof: ExtendedProof,
664  public_data: List[int],
665  for_dispatch_call: bool = False) -> int:
666  """
667  Generate a signature on the hash of the ciphertexts, proofs and primary
668  inputs. This is used to solve transaction malleability. We chose to sign
669  the hash and not the values themselves for modularity (to use the same code
670  regardless of whether GROTH16 or PGHR13 proof system is chosen), and sign
671  the hash of the ciphers and inputs for consistency. If for_dispatch_call is
672  set, the parameters are to be passed to the Mixer's `dispatch` call in a
673  later operation (in which proof data is not available), hence proof is
674  omitted from the signature.
675  """
676  assert len(ciphertexts) == constants.JS_INPUTS
677 
678  # The message to sign consists of (in order):
679  # - senders Ethereum address
680  # - ciphertexts
681  # - proof elements (if for_dispatch_call is False)
682  # - public input elements
683  h = sha256()
684  h.update(eth_address_to_bytes32(sender_eth_address))
685  for ciphertext in ciphertexts:
686  h.update(ciphertext)
687 
688  proof_bytes, pub_inputs_bytes = _proof_and_inputs_to_bytes(
689  zksnark, pp, extproof, public_data)
690 
691  # If for_dispatch_call is set, omit proof from the signature. See
692  # AbstractMixer.sol.
693  if not for_dispatch_call:
694  h.update(proof_bytes)
695 
696  h.update(pub_inputs_bytes)
697  message_digest = h.digest()
698  return signing.sign(signing_keypair.sk, message_digest)
699 
700 
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mix_parameters_to_contract_arguments()

List[Any] zeth.core.mixer_client.mix_parameters_to_contract_arguments ( IZKSnarkProvider  zksnark,
PairingParameters  pp,
MixParameters  mix_parameters 
)
Convert MixParameters to a list of eth ABI objects which can be passed to
the contract's mix method.

Definition at line 157 of file mixer_client.py.

158  zksnark: IZKSnarkProvider,
159  pp: PairingParameters,
160  mix_parameters: MixParameters) -> List[Any]:
161  """
162  Convert MixParameters to a list of eth ABI objects which can be passed to
163  the contract's mix method.
164  """
165  proof_contract_params = zksnark.proof_to_contract_parameters(
166  mix_parameters.extended_proof.proof, pp)
167  return [
168  proof_contract_params,
169  signing.verification_key_as_mix_parameter(mix_parameters.signature_vk),
170  signing.signature_as_mix_parameter(mix_parameters.signature),
171  mix_parameters.public_data,
172  mix_parameters.ciphertexts,
173  ]
174 
175 
Here is the caller graph for this function:

◆ mix_parameters_to_dispatch_parameters()

bytes zeth.core.mixer_client.mix_parameters_to_dispatch_parameters ( MixParameters  mix_parameters)
Encode parameters from mix_parameters into an array of uint256 values,
compatible with the `dispatch` method on Mixer. This conforms to the
`IZecaleApplication` solidity interface of Zecale
(https://github.com/clearmatics/zecale)

Definition at line 176 of file mixer_client.py.

176 def mix_parameters_to_dispatch_parameters(mix_parameters: MixParameters) -> bytes:
177  """
178  Encode parameters from mix_parameters into an array of uint256 values,
179  compatible with the `dispatch` method on Mixer. This conforms to the
180  `IZecaleApplication` solidity interface of Zecale
181  (https://github.com/clearmatics/zecale)
182  """
183  vk_param = signing.verification_key_as_mix_parameter(
184  mix_parameters.signature_vk)
185  sigma_param = signing.signature_as_mix_parameter(mix_parameters.signature)
186  public_data = mix_parameters.public_data
187  ciphertexts = mix_parameters.ciphertexts
188  return eth_abi.encode_abi(
189  ['uint256[4]', 'uint256', 'uint256[]', 'bytes[]'],
190  [vk_param, sigma_param, public_data, ciphertexts]) # type: ignore
191 
192 

◆ receive_note()

Optional[Tuple[bytes, api.ZethNote]] zeth.core.mixer_client.receive_note ( MixOutputEvents  out_ev,
EncryptionSecretKey   receiver_k_sk 
)
Given the receivers secret key, and the event data from a transaction
(encrypted notes), decrypt any that are intended for the receiver. Return
tuples `(<address-in-merkle-tree>, ZethNote)`. Callers should record the
address-in-merkle-tree along with ZethNote information, for convenience
when spending the notes.

Definition at line 618 of file mixer_client.py.

618 def receive_note(
619  out_ev: MixOutputEvents,
620  receiver_k_sk: EncryptionSecretKey
621 ) -> Optional[Tuple[bytes, api.ZethNote]]:
622  """
623  Given the receivers secret key, and the event data from a transaction
624  (encrypted notes), decrypt any that are intended for the receiver. Return
625  tuples `(<address-in-merkle-tree>, ZethNote)`. Callers should record the
626  address-in-merkle-tree along with ZethNote information, for convenience
627  when spending the notes.
628  """
629  try:
630  plaintext = decrypt(out_ev.ciphertext, receiver_k_sk)
631  return (
632  out_ev.commitment,
633  proto_utils.zeth_note_from_bytes(plaintext))
634  except InvalidSignature:
635  return None
636  except ValueError:
637  return None
638 
639 
Here is the call graph for this function:

Variable Documentation

◆ ComputeHSigCB

zeth.core.mixer_client.ComputeHSigCB = Callable[[List[bytes], JoinsplitSigVerificationKey], bytes]

Definition at line 46 of file mixer_client.py.

◆ JoinsplitSigKeyPair

zeth.core.mixer_client.JoinsplitSigKeyPair = signing.SigningKeyPair

Definition at line 44 of file mixer_client.py.

◆ JoinsplitSigSecretKey

zeth.core.mixer_client.JoinsplitSigSecretKey = signing.SigningSecretKey

Definition at line 43 of file mixer_client.py.

◆ JoinsplitSigVerificationKey

zeth.core.mixer_client.JoinsplitSigVerificationKey = signing.SigningVerificationKey

Definition at line 42 of file mixer_client.py.

◆ ZERO_ADDRESS

string zeth.core.mixer_client.ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"

Definition at line 39 of file mixer_client.py.

◆ ZERO_UNITS_HEX

string zeth.core.mixer_client.ZERO_UNITS_HEX = "0000000000000000"

Definition at line 38 of file mixer_client.py.

zeth.core.mixer_client.mix_parameters_to_contract_arguments
List[Any] mix_parameters_to_contract_arguments(IZKSnarkProvider zksnark, PairingParameters pp, MixParameters mix_parameters)
Definition: mixer_client.py:157
zeth.core.mixer_client.create_api_joinsplit_input
api.JoinsplitInput create_api_joinsplit_input(List[str] merkle_path, int address, api.ZethNote note, OwnershipSecretKey a_sk, bytes nullifier)
Definition: mixer_client.py:227
zeth.core.ownership.ownership_key_as_hex
str ownership_key_as_hex(bytes a_sk)
Definition: ownership.py:37
zeth.core.mixer_client.receive_note
Optional[Tuple[bytes, api.ZethNote]] receive_note(MixOutputEvents out_ev, EncryptionSecretKey receiver_k_sk)
Definition: mixer_client.py:618
zeth.cli.zeth_deploy.int
int
Definition: zeth_deploy.py:27
zeth.core.mixer_client.encrypt_notes
List[bytes] encrypt_notes(List[Tuple[api.ZethNote, EncryptionPublicKey]] notes)
Definition: mixer_client.py:601
zeth.core.mixer_client.get_dummy_input_and_address
Tuple[int, api.ZethNote] get_dummy_input_and_address(OwnershipPublicKey a_pk)
Definition: mixer_client.py:241
zeth.core.encryption.encrypt
bytes encrypt(bytes message, EncryptionPublicKey pk_receiver)
Definition: encryption.py:168
zeth.core.mixer_client.mix_parameters_to_dispatch_parameters
bytes mix_parameters_to_dispatch_parameters(MixParameters mix_parameters)
Definition: mixer_client.py:176
zeth.core.utils.eth_address_to_bytes32
bytes eth_address_to_bytes32(str eth_addr)
Definition: utils.py:114
zeth.core.utils.digest_to_binary_string
str digest_to_binary_string(bytes digest)
Definition: utils.py:168
zeth.core.mixer_client.joinsplit_sign
int joinsplit_sign(IZKSnarkProvider zksnark, PairingParameters pp, JoinsplitSigKeyPair signing_keypair, str sender_eth_address, List[bytes] ciphertexts, ExtendedProof extproof, List[int] public_data, bool for_dispatch_call=False)
Definition: mixer_client.py:657
zeth.core.mixer_client.compute_h_sig
bytes compute_h_sig(List[bytes] nullifiers, JoinsplitSigVerificationKey sign_vk)
Definition: mixer_client.py:734
zeth.core.mixer_client.compute_nullifier
bytes compute_nullifier(api.ZethNote zeth_note, OwnershipSecretKey spending_authority_ask)
Definition: mixer_client.py:718
zeth.core.encryption.decrypt
bytes decrypt(bytes encrypted_message, EncryptionSecretKey sk_receiver)
Definition: encryption.py:203
zeth.core.mixer_client.event_args_to_mix_result
MixResult event_args_to_mix_result(Any event_args)
Definition: mixer_client.py:218
zeth.core.mixer_client.compute_commitment
bytes compute_commitment(api.ZethNote zeth_note, PairingParameters pp)
Definition: mixer_client.py:701
zeth.core.mixer_client.get_mix_results
Iterator[MixResult] get_mix_results(Any web3, Any mixer_instance, int start_block, int end_block, Optional[int] batch_size=None)
Definition: mixer_client.py:640