Skip to content

Commit a6301c5

Browse files
Optionally print intermediate values in reference code
and make reference code and pseudocode more consistent with each other
1 parent 39ba507 commit a6301c5

File tree

2 files changed

+56
-18
lines changed

2 files changed

+56
-18
lines changed

bip-0340.mediawiki

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ Input:
136136
* The secret key ''sk'': a 32-byte array, freshly generated uniformly at random
137137
138138
The algorithm ''PubKey(sk)'' is defined as:
139-
* Let ''d = int(sk)''.
140-
* Fail if ''d = 0'' or ''d ≥ n''.
141-
* Return ''bytes(d⋅G)''.
139+
* Let ''d' = int(sk)''.
140+
* Fail if ''d' = 0'' or ''d' ≥ n''.
141+
* Return ''bytes(d'⋅G)''.
142142
143143
Note that we use a very different public key format (32 bytes) than the ones used by existing systems (which typically use elliptic curve points as public keys, or 33-byte or 65-byte encodings of them). A side effect is that ''PubKey(sk) = PubKey(bytes(n - int(sk))'', so every public key has two corresponding secret keys.
144144

bip-0340/reference.py

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import hashlib
22
import binascii
33

4+
# Set DEBUG to True to get a detailed debug output including
5+
# intermediate values during key generation, signing, and
6+
# verification. This is implemented via calls to the
7+
# debug_print_vars() function.
8+
#
9+
# If you want to print values on an individual basis, use
10+
# the pretty() function, e.g., print(pretty(foo)).
11+
DEBUG = False
12+
413
p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
514
n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
615

@@ -62,7 +71,7 @@ def lift_x_square_y(b):
6271
y = pow(y_sq, (p + 1) // 4, p)
6372
if pow(y, 2, p) != y_sq:
6473
return None
65-
return [x, y]
74+
return (x, y)
6675

6776
def lift_x_even_y(b):
6877
P = lift_x_square_y(b)
@@ -87,32 +96,37 @@ def has_even_y(P):
8796
return y(P) % 2 == 0
8897

8998
def pubkey_gen(seckey):
90-
x = int_from_bytes(seckey)
91-
if not (1 <= x <= n - 1):
99+
d0 = int_from_bytes(seckey)
100+
if not (1 <= d0 <= n - 1):
101+
debug_print_vars()
92102
raise ValueError('The secret key must be an integer in the range 1..n-1.')
93-
P = point_mul(G, x)
103+
P = point_mul(G, d0)
94104
return bytes_from_point(P)
95105

96-
def schnorr_sign(msg, seckey0, aux_rand):
106+
def schnorr_sign(msg, seckey, aux_rand):
97107
if len(msg) != 32:
108+
debug_print_vars()
98109
raise ValueError('The message must be a 32-byte array.')
99-
seckey0 = int_from_bytes(seckey0)
100-
if not (1 <= seckey0 <= n - 1):
110+
d0 = int_from_bytes(seckey)
111+
if not (1 <= d0 <= n - 1):
101112
raise ValueError('The secret key must be an integer in the range 1..n-1.')
102113
if len(aux_rand) != 32:
103114
raise ValueError('aux_rand must be 32 bytes instead of %i.' % len(aux_rand))
104-
P = point_mul(G, seckey0)
105-
seckey = seckey0 if has_even_y(P) else n - seckey0
106-
t = xor_bytes(bytes_from_int(seckey), tagged_hash("BIP340/aux", aux_rand))
115+
P = point_mul(G, d0)
116+
d = d0 if has_even_y(P) else n - d0
117+
t = xor_bytes(bytes_from_int(d), tagged_hash("BIP340/aux", aux_rand))
107118
k0 = int_from_bytes(tagged_hash("BIP340/nonce", t + bytes_from_point(P) + msg)) % n
108119
if k0 == 0:
120+
debug_print_vars()
109121
raise RuntimeError('Failure. This happens only with negligible probability.')
110122
R = point_mul(G, k0)
111123
k = n - k0 if not has_square_y(R) else k0
112124
e = int_from_bytes(tagged_hash("BIP340/challenge", bytes_from_point(R) + bytes_from_point(P) + msg)) % n
113-
sig = bytes_from_point(R) + bytes_from_int((k + e * seckey) % n)
125+
sig = bytes_from_point(R) + bytes_from_int((k + e * d) % n)
114126
if not schnorr_verify(msg, bytes_from_point(P), sig):
127+
debug_print_vars()
115128
raise RuntimeError('The signature does not pass verification.')
129+
debug_print_vars()
116130
return sig
117131

118132
def schnorr_verify(msg, pubkey, sig):
@@ -123,26 +137,29 @@ def schnorr_verify(msg, pubkey, sig):
123137
if len(sig) != 64:
124138
raise ValueError('The signature must be a 64-byte array.')
125139
P = lift_x_even_y(pubkey)
126-
if (P is None):
127-
return False
128140
r = int_from_bytes(sig[0:32])
129141
s = int_from_bytes(sig[32:64])
130-
if (r >= p or s >= n):
142+
if (P is None) or (r >= p) or (s >= n):
143+
debug_print_vars()
131144
return False
132145
e = int_from_bytes(tagged_hash("BIP340/challenge", sig[0:32] + pubkey + msg)) % n
133146
R = point_add(point_mul(G, s), point_mul(P, n - e))
134147
if R is None or not has_square_y(R) or x(R) != r:
148+
debug_print_vars()
135149
return False
150+
debug_print_vars()
136151
return True
137152

138153
#
139154
# The following code is only used to verify the test vectors.
140155
#
141156
import csv
157+
import os
158+
import sys
142159

143160
def test_vectors():
144161
all_passed = True
145-
with open('test-vectors.csv', newline='') as csvfile:
162+
with open(os.path.join(sys.path[0], 'test-vectors.csv'), newline='') as csvfile:
146163
reader = csv.reader(csvfile)
147164
reader.__next__()
148165
for row in reader:
@@ -185,5 +202,26 @@ def test_vectors():
185202
print('Some test vectors failed.')
186203
return all_passed
187204

205+
#
206+
# The following code is only used for debugging
207+
#
208+
import inspect
209+
210+
def pretty(v):
211+
if isinstance(v, bytes):
212+
return '0x' + v.hex()
213+
if isinstance(v, int):
214+
return pretty(bytes_from_int(v))
215+
if isinstance(v, tuple):
216+
return tuple(map(pretty, v))
217+
return v
218+
219+
def debug_print_vars():
220+
if DEBUG:
221+
frame = inspect.currentframe().f_back
222+
print(' Variables in function ', frame.f_code.co_name, ' at line ', frame.f_lineno, ':', sep='')
223+
for var_name, var_val in frame.f_locals.items():
224+
print(' ' + var_name.rjust(11, ' '), '==', pretty(var_val))
225+
188226
if __name__ == '__main__':
189227
test_vectors()

0 commit comments

Comments
 (0)