Skip to content

Commit 07d938a

Browse files
fixup! Optionally print intermediate values in reference code
1 parent 003d38c commit 07d938a

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

bip-0340/reference.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def lift_x_even_y(b):
7878
if P is None:
7979
return None
8080
else:
81-
return [x(P), y(P) if y(P) % 2 == 0 else p - y(P)]
81+
return (x(P), y(P) if y(P) % 2 == 0 else p - y(P))
8282

8383
def int_from_bytes(b):
8484
return int.from_bytes(b, byteorder="big")
@@ -90,22 +90,20 @@ def is_square(x):
9090
return pow(x, (p - 1) // 2, p) == 1
9191

9292
def has_square_y(P):
93-
return (not is_infinity(P)) and (is_square(y(P)))
93+
return (not is_infinity(P)) and is_square(y(P))
9494

9595
def has_even_y(P):
9696
return y(P) % 2 == 0
9797

9898
def pubkey_gen(seckey):
9999
d0 = int_from_bytes(seckey)
100100
if not (1 <= d0 <= n - 1):
101-
debug_print_vars()
102101
raise ValueError('The secret key must be an integer in the range 1..n-1.')
103102
P = point_mul(G, d0)
104103
return bytes_from_point(P)
105104

106105
def schnorr_sign(msg, seckey, aux_rand):
107106
if len(msg) != 32:
108-
debug_print_vars()
109107
raise ValueError('The message must be a 32-byte array.')
110108
d0 = int_from_bytes(seckey)
111109
if not (1 <= d0 <= n - 1):
@@ -117,16 +115,14 @@ def schnorr_sign(msg, seckey, aux_rand):
117115
t = xor_bytes(bytes_from_int(d), tagged_hash("BIP340/aux", aux_rand))
118116
k0 = int_from_bytes(tagged_hash("BIP340/nonce", t + bytes_from_point(P) + msg)) % n
119117
if k0 == 0:
120-
debug_print_vars()
121118
raise RuntimeError('Failure. This happens only with negligible probability.')
122119
R = point_mul(G, k0)
123120
k = n - k0 if not has_square_y(R) else k0
124121
e = int_from_bytes(tagged_hash("BIP340/challenge", bytes_from_point(R) + bytes_from_point(P) + msg)) % n
125122
sig = bytes_from_point(R) + bytes_from_int((k + e * d) % n)
123+
debug_print_vars()
126124
if not schnorr_verify(msg, bytes_from_point(P), sig):
127-
debug_print_vars()
128125
raise RuntimeError('The signature does not pass verification.')
129-
debug_print_vars()
130126
return sig
131127

132128
def schnorr_verify(msg, pubkey, sig):

0 commit comments

Comments
 (0)