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

Classes

class  SigningKeyPair
 
class  SigningSecretKey
 
class  SigningVerificationKey
 

Functions

SigningKeyPair gen_signing_keypair ()
 
bytes signature_to_bytes (Signature signature)
 
Signature signature_from_bytes (bytes sig_bytes)
 
Signature sign (SigningSecretKey sk, bytes m)
 
bool verify (SigningVerificationKey vk, bytes m, int sigma)
 
List[int] verification_key_as_mix_parameter (SigningVerificationKey vk)
 
SigningVerificationKey verification_key_from_mix_parameter (List[int] param)
 
int signature_as_mix_parameter (Signature signature)
 
Signature signature_from_mix_parameter (int param)
 
bytes fq_to_bytes (FQ fq_element)
 
FQ fq_from_bytes (bytes fq_bytes)
 
str fq_to_hex (FQ fq_element)
 
FQ fq_from_hex (str fq_hex)
 
bytes g1_to_bytes (G1 group_el)
 
Dict[str, Any] g1_to_json_dict (G1 group_el)
 
G1 g1_from_json_dict (Dict[str, Any] json_dict)
 

Variables

 FQ = ec.FQ
 
 G1 = Tuple[ec.FQ, ec.FQ]
 
 Signature = int
 

Function Documentation

◆ fq_from_bytes()

FQ zeth.core.signing.fq_from_bytes ( bytes  fq_bytes)

Definition at line 205 of file signing.py.

205 def fq_from_bytes(fq_bytes: bytes) -> FQ:
206  return FQ(int.from_bytes(fq_bytes, byteorder='big'))
207 
208 
Here is the caller graph for this function:

◆ fq_from_hex()

FQ zeth.core.signing.fq_from_hex ( str  fq_hex)

Definition at line 213 of file signing.py.

213 def fq_from_hex(fq_hex: str) -> FQ:
214  return fq_from_bytes(bytes.fromhex(fq_hex))
215 
216 
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fq_to_bytes()

bytes zeth.core.signing.fq_to_bytes ( FQ  fq_element)

Definition at line 201 of file signing.py.

201 def fq_to_bytes(fq_element: FQ) -> bytes:
202  return int(fq_element.n).to_bytes(32, byteorder='big')
203 
204 
Here is the caller graph for this function:

◆ fq_to_hex()

str zeth.core.signing.fq_to_hex ( FQ  fq_element)

Definition at line 209 of file signing.py.

209 def fq_to_hex(fq_element: FQ) -> str:
210  return fq_to_bytes(fq_element).hex()
211 
212 
Here is the call graph for this function:
Here is the caller graph for this function:

◆ g1_from_json_dict()

G1 zeth.core.signing.g1_from_json_dict ( Dict[str, Any]  json_dict)

Definition at line 235 of file signing.py.

235 def g1_from_json_dict(json_dict: Dict[str, Any]) -> G1:
236  return (fq_from_hex(json_dict["x"]), fq_from_hex(json_dict["y"]))
Here is the call graph for this function:
Here is the caller graph for this function:

◆ g1_to_bytes()

bytes zeth.core.signing.g1_to_bytes ( G1  group_el)
Encode a group element into a byte string
We assume here the group prime $p$ is written in less than 256 bits
to conform with Ethereum bytes32 type.

Definition at line 217 of file signing.py.

217 def g1_to_bytes(group_el: G1) -> bytes:
218  """
219  Encode a group element into a byte string
220  We assume here the group prime $p$ is written in less than 256 bits
221  to conform with Ethereum bytes32 type.
222  """
223  return \
224  int(group_el[0]).to_bytes(32, byteorder='big') + \
225  int(group_el[1]).to_bytes(32, byteorder='big')
226 
227 
Here is the caller graph for this function:

◆ g1_to_json_dict()

Dict[str, Any] zeth.core.signing.g1_to_json_dict ( G1  group_el)

Definition at line 228 of file signing.py.

228 def g1_to_json_dict(group_el: G1) -> Dict[str, Any]:
229  return {
230  "x": fq_to_hex(group_el[0]),
231  "y": fq_to_hex(group_el[1]),
232  }
233 
234 
Here is the call graph for this function:
Here is the caller graph for this function:

◆ gen_signing_keypair()

SigningKeyPair zeth.core.signing.gen_signing_keypair ( )
Return a one-time signature key-pair
composed of elements of F_q and G1.

Definition at line 93 of file signing.py.

93 def gen_signing_keypair() -> SigningKeyPair:
94  """
95  Return a one-time signature key-pair
96  composed of elements of F_q and G1.
97  """
98  key_size_byte = ceil(len("{0:b}".format(ec.curve_order)) / 8)
99  x = FQ(
100  int(bytes(urandom(key_size_byte)).hex(), 16) % ec.curve_order)
101  y = FQ(
102  int(bytes(urandom(key_size_byte)).hex(), 16) % ec.curve_order)
103  X = ec.multiply(ec.G1, x.n)
104  Y = ec.multiply(ec.G1, y.n)
105 
106  # We include y_g1 in the signing key
107  sk = SigningSecretKey(x, y, Y)
108  vk = SigningVerificationKey(X, Y)
109  return SigningKeyPair(sk, vk)
110 
111 

◆ sign()

Signature zeth.core.signing.sign ( SigningSecretKey  sk,
bytes  m 
)
Generate a Schnorr signature on a message m.
We assume here that the message fits in an Ethereum word (i.e. bit_len(m)
<= 256), so that it can be represented by a single bytes32 on the smart-
contract during the signature verification.

Definition at line 123 of file signing.py.

123 def sign(
124  sk: SigningSecretKey,
125  m: bytes) -> Signature:
126  """
127  Generate a Schnorr signature on a message m.
128  We assume here that the message fits in an Ethereum word (i.e. bit_len(m)
129  <= 256), so that it can be represented by a single bytes32 on the smart-
130  contract during the signature verification.
131  """
132 
133  # Encode and hash the verifying key and input hashes
134  challenge_to_hash = g1_to_bytes(sk.ssk[1]) + m
135 
136  # Convert the hex digest into a field element
137  challenge = int(sha256(challenge_to_hash).hexdigest(), 16)
138  challenge = challenge % ec.curve_order
139 
140  # Compute the signature sigma
141  sigma = (sk.ssk[0].n + challenge * sk.psk.n) % ec.curve_order
142  return sigma
143 
144 
Here is the call graph for this function:

◆ signature_as_mix_parameter()

int zeth.core.signing.signature_as_mix_parameter ( Signature  signature)
Transform a signature to the format required by the mix function.

Definition at line 183 of file signing.py.

183 def signature_as_mix_parameter(signature: Signature) -> int:
184  """
185  Transform a signature to the format required by the mix function.
186  """
187  # This function happens to be the identity but in the general case some
188  # transform will be required.
189  return signature
190 
191 

◆ signature_from_bytes()

Signature zeth.core.signing.signature_from_bytes ( bytes  sig_bytes)

Definition at line 119 of file signing.py.

119 def signature_from_bytes(sig_bytes: bytes) -> Signature:
120  return int.from_bytes(sig_bytes, byteorder='big')
121 
122 

◆ signature_from_mix_parameter()

Signature zeth.core.signing.signature_from_mix_parameter ( int  param)
Transform mix function parameters to a signature.

Definition at line 192 of file signing.py.

192 def signature_from_mix_parameter(param: int) -> Signature:
193  """
194  Transform mix function parameters to a signature.
195  """
196  return param
197 
198 # Low level encoding / decoding functions
199 
200 

◆ signature_to_bytes()

bytes zeth.core.signing.signature_to_bytes ( Signature  signature)

Definition at line 115 of file signing.py.

115 def signature_to_bytes(signature: Signature) -> bytes:
116  return signature.to_bytes(32, byteorder='big')
117 
118 

◆ verification_key_as_mix_parameter()

List[int] zeth.core.signing.verification_key_as_mix_parameter ( SigningVerificationKey  vk)
Transform a verification key to the format required by the mix function.

Definition at line 166 of file signing.py.

166 def verification_key_as_mix_parameter(vk: SigningVerificationKey) -> List[int]:
167  """
168  Transform a verification key to the format required by the mix function.
169  """
170  return [int(vk.ppk[0]), int(vk.ppk[1]), int(vk.spk[0]), int(vk.spk[1])]
171 
172 

◆ verification_key_from_mix_parameter()

SigningVerificationKey zeth.core.signing.verification_key_from_mix_parameter ( List[int]  param)
Transform mix function parameter to verification key.

Definition at line 173 of file signing.py.

174  param: List[int]) -> SigningVerificationKey:
175  """
176  Transform mix function parameter to verification key.
177  """
178  return SigningVerificationKey(
179  (FQ(param[0]), FQ(param[1])),
180  (FQ(param[2]), FQ(param[3])))
181 
182 

◆ verify()

bool zeth.core.signing.verify ( SigningVerificationKey  vk,
bytes  m,
int  sigma 
)
Return true if the signature sigma is valid on message m and vk.
We assume here that the message is an hexadecimal string written in
less than 256 bits to conform with Ethereum bytes32 type.

Definition at line 145 of file signing.py.

145 def verify(
146  vk: SigningVerificationKey,
147  m: bytes,
148  sigma: int) -> bool:
149  """
150  Return true if the signature sigma is valid on message m and vk.
151  We assume here that the message is an hexadecimal string written in
152  less than 256 bits to conform with Ethereum bytes32 type.
153  """
154  # Encode and hash the verifying key and input hashes
155  challenge_to_hash = g1_to_bytes(vk.spk) + m
156 
157  challenge = int(sha256(challenge_to_hash).hexdigest(), 16)
158  challenge = challenge % ec.curve_order
159 
160  left_part = ec.multiply(ec.G1, FQ(sigma).n)
161  right_part = ec.add(vk.spk, ec.multiply(vk.ppk, FQ(challenge).n))
162 
163  return ec.eq(left_part, right_part)
164 
165 
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ FQ

zeth.core.signing.FQ = ec.FQ

Definition at line 21 of file signing.py.

◆ G1

zeth.core.signing.G1 = Tuple[ec.FQ, ec.FQ]

Definition at line 22 of file signing.py.

◆ Signature

zeth.core.signing.Signature = int

Definition at line 112 of file signing.py.

zeth.core.signing.g1_from_json_dict
G1 g1_from_json_dict(Dict[str, Any] json_dict)
Definition: signing.py:235
zeth.cli.zeth_deploy.int
int
Definition: zeth_deploy.py:27
zeth.core.signing.signature_to_bytes
bytes signature_to_bytes(Signature signature)
Definition: signing.py:115
zeth.core.signing.gen_signing_keypair
SigningKeyPair gen_signing_keypair()
Definition: signing.py:93
zeth.core.encryption.bytes
bytes
Definition: encryption.py:87
zeth.core.signing.fq_to_hex
str fq_to_hex(FQ fq_element)
Definition: signing.py:209
zeth.core.signing.verify
bool verify(SigningVerificationKey vk, bytes m, int sigma)
Definition: signing.py:145
zeth.core.signing.signature_from_mix_parameter
Signature signature_from_mix_parameter(int param)
Definition: signing.py:192
zeth.core.signing.g1_to_bytes
bytes g1_to_bytes(G1 group_el)
Definition: signing.py:217
zeth.core.signing.FQ
FQ
Definition: signing.py:21
zeth.core.signing.signature_from_bytes
Signature signature_from_bytes(bytes sig_bytes)
Definition: signing.py:119
zeth.core.signing.signature_as_mix_parameter
int signature_as_mix_parameter(Signature signature)
Definition: signing.py:183
zeth.core.signing.g1_to_json_dict
Dict[str, Any] g1_to_json_dict(G1 group_el)
Definition: signing.py:228
zeth.core.signing.fq_from_bytes
FQ fq_from_bytes(bytes fq_bytes)
Definition: signing.py:205
zeth.core.signing.fq_to_bytes
bytes fq_to_bytes(FQ fq_element)
Definition: signing.py:201
zeth.core.signing.verification_key_as_mix_parameter
List[int] verification_key_as_mix_parameter(SigningVerificationKey vk)
Definition: signing.py:166
zeth.core.signing.fq_from_hex
FQ fq_from_hex(str fq_hex)
Definition: signing.py:213
zeth.core.signing.verification_key_from_mix_parameter
SigningVerificationKey verification_key_from_mix_parameter(List[int] param)
Definition: signing.py:173
zeth.core.signing.sign
Signature sign(SigningSecretKey sk, bytes m)
Definition: signing.py:123