Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
ownership.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # Copyright (c) 2015-2022 Clearmatics Technologies Ltd
4 #
5 # SPDX-License-Identifier: LGPL-3.0+
6 
7 from __future__ import annotations
8 
9 from zeth.core.utils import hex_extend_32bytes, digest_to_binary_string, \
10  encode_abi
11 
12 from Crypto import Random
13 from hashlib import blake2s
14 from typing import NewType
15 
16 
17 # Secret key for proving ownership
18 OwnershipSecretKey = NewType('OwnershipSecretKey', bytes)
19 
20 
21 # Public key for proving owenership
22 OwnershipPublicKey = NewType('OwnershipPublicKey', bytes)
23 
24 
26  """
27  Key-pair for ownership proof. This represents the 'payment key' (apk)
28  from the 'payment address' and the 'spending key' (ask) from the
29  'private address'. These are components of ZethAddress, used in
30  note commitments in the joinsplit statement.
31  """
32  def __init__(self, a_sk: OwnershipSecretKey, a_pk: OwnershipPublicKey):
33  self.a_sk: OwnershipSecretKey = a_sk
34  self.a_pk: OwnershipPublicKey = a_pk
35 
36 
37 def ownership_key_as_hex(a_sk: bytes) -> str:
38  """
39  Convert either a secret or public ownership key to hex representation of the
40  underlying 32-byte object.
41  """
42  return hex_extend_32bytes(a_sk.hex())
43 
44 
45 def ownership_public_key_from_hex(key_hex: str) -> OwnershipPublicKey:
46  """
47  Read an ownership public key from a hex string.
48  """
49  return OwnershipPublicKey(bytes.fromhex(key_hex))
50 
51 
52 def ownership_secret_key_from_hex(key_hex: str) -> OwnershipSecretKey:
53  """
54  Read an ownership public key from a hex string.
55  """
56  return OwnershipSecretKey(bytes.fromhex(key_hex))
57 
58 
59 def gen_ownership_keypair() -> OwnershipKeyPair:
60  a_sk = OwnershipSecretKey(Random.get_random_bytes(32))
61  a_pk = _derive_a_pk(a_sk)
62  keypair = OwnershipKeyPair(a_sk, a_pk)
63  return keypair
64 
65 
66 def _derive_a_pk(a_sk: OwnershipSecretKey) -> OwnershipPublicKey:
67  """
68  Returns a_pk = blake2s(1100 || [a_sk]_252 || 0^256)
69  """
70  binary_a_sk = digest_to_binary_string(a_sk)
71  first_252bits_ask = binary_a_sk[:252]
72  left_leg_bin = "1100" + first_252bits_ask
73  left_leg_hex = "{0:0>4X}".format(int(left_leg_bin, 2))
74  zeroes = "0000000000000000000000000000000000000000000000000000000000000000"
75  a_pk = blake2s(
76  encode_abi(
77  ["bytes32", "bytes32"],
78  [bytes.fromhex(left_leg_hex), bytes.fromhex(zeroes)])
79  ).digest()
80  return OwnershipPublicKey(a_pk)
zeth.core.utils.hex_extend_32bytes
str hex_extend_32bytes(str element)
Definition: utils.py:210
zeth.core.ownership.ownership_key_as_hex
str ownership_key_as_hex(bytes a_sk)
Definition: ownership.py:37
zeth.cli.zeth_deploy.int
int
Definition: zeth_deploy.py:27
zeth.core.ownership.ownership_secret_key_from_hex
OwnershipSecretKey ownership_secret_key_from_hex(str key_hex)
Definition: ownership.py:52
zeth.core.ownership.OwnershipPublicKey
OwnershipPublicKey
Definition: ownership.py:22
zeth.core.ownership.gen_ownership_keypair
OwnershipKeyPair gen_ownership_keypair()
Definition: ownership.py:59
zeth.core.ownership.ownership_public_key_from_hex
OwnershipPublicKey ownership_public_key_from_hex(str key_hex)
Definition: ownership.py:45
zeth.core.utils.encode_abi
bytes encode_abi(List[str] type_names, List[Any] data)
Definition: utils.py:97
zeth.core.utils
Definition: utils.py:1
zeth.core.utils.digest_to_binary_string
str digest_to_binary_string(bytes digest)
Definition: utils.py:168
zeth.core.ownership.OwnershipKeyPair.__init__
def __init__(self, OwnershipSecretKey a_sk, OwnershipPublicKey a_pk)
Definition: ownership.py:32
zeth.core.ownership.OwnershipSecretKey
OwnershipSecretKey
Definition: ownership.py:18
zeth.core.ownership.OwnershipKeyPair
Definition: ownership.py:25