Skip to content

Commit 7d921e3

Browse files
committed
BIP-340: fix lift_x calls in test vector generation script
The `lift_x` function in the BIP and the reference implementation expect an integer to be passed rather than a byte array. Can be tested with: ``` $ python3 test-vectors.py > expected.csv $ diff test-vectors.csv expected.csv ```
1 parent 2caa8e2 commit 7d921e3

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

bip-0340/test-vectors.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ def vector0():
2727
# we should have at least one test vector where the point reconstructed
2828
# from the public key has a square and one where it has a non-square Y
2929
# coordinate. In this one Y is non-square.
30-
pubkey_point = lift_x(pubkey)
30+
pubkey_point = lift_x(int_from_bytes(pubkey))
3131
assert(not has_square_y(pubkey_point))
3232

3333
# For historical reasons (R tiebreaker was squareness and not evenness)
3434
# we should have at least one test vector where the point reconstructed
3535
# from the R.x coordinate has a square and one where it has a non-square Y
3636
# coordinate. In this one Y is non-square.
37-
R = lift_x(sig[0:32])
37+
R = lift_x(int_from_bytes(sig[0:32]))
3838
assert(not has_square_y(R))
3939

4040
return (seckey, pubkey, aux_rand, msg, sig, "TRUE", None)
@@ -47,7 +47,7 @@ def vector1():
4747
sig = schnorr_sign(msg, seckey, aux_rand)
4848

4949
# The point reconstructed from the R.x coordinate has a square Y coordinate.
50-
R = lift_x(sig[0:32])
50+
R = lift_x(int_from_bytes(sig[0:32]))
5151
assert(has_square_y(R))
5252

5353
return (seckey, pubkey_gen(seckey), aux_rand, msg, sig, "TRUE", None)
@@ -60,12 +60,12 @@ def vector2():
6060

6161
# The point reconstructed from the public key has a square Y coordinate.
6262
pubkey = pubkey_gen(seckey)
63-
pubkey_point = lift_x(pubkey)
63+
pubkey_point = lift_x(int_from_bytes(pubkey))
6464
assert(has_square_y(pubkey_point))
6565

6666
# This signature vector would not verify if the implementer checked the
6767
# evenness of the X coordinate of R instead of the Y coordinate.
68-
R = lift_x(sig[0:32])
68+
R = lift_x(int_from_bytes(sig[0:32]))
6969
assert(R[0] % 2 == 1)
7070

7171
return (seckey, pubkey, aux_rand, msg, sig, "TRUE", None)
@@ -119,8 +119,9 @@ def vector5():
119119
msg = default_msg
120120
sig = schnorr_sign(msg, seckey, default_aux_rand)
121121

122-
pubkey = bytes_from_int(0xEEFDEA4CDB677750A420FEE807EACF21EB9898AE79B9768766E4FAA04A2D4A34)
123-
assert(lift_x(pubkey) is None)
122+
pubkey_int = 0xEEFDEA4CDB677750A420FEE807EACF21EB9898AE79B9768766E4FAA04A2D4A34
123+
pubkey = bytes_from_int(pubkey_int)
124+
assert(lift_x(pubkey_int) is None)
124125

125126
return (None, pubkey, None, msg, sig, "FALSE", "public key not on the curve")
126127

@@ -197,9 +198,9 @@ def vector11():
197198
sig = schnorr_sign(msg, seckey, default_aux_rand)
198199

199200
# Replace R's X coordinate with an X coordinate that's not on the curve
200-
x_not_on_curve = bytes_from_int(0x4A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1D)
201+
x_not_on_curve = 0x4A298DACAE57395A15D0795DDBFD1DCB564DA82B0F269BC70A74F8220429BA1D
201202
assert(lift_x(x_not_on_curve) is None)
202-
sig = x_not_on_curve + sig[32:64]
203+
sig = bytes_from_int(x_not_on_curve) + sig[32:64]
203204

204205
return (None, pubkey_gen(seckey), None, msg, sig, "FALSE", "sig[0:32] is not an X coordinate on the curve")
205206

@@ -242,10 +243,10 @@ def vector14():
242243
sig = schnorr_sign(msg, seckey, default_aux_rand)
243244
pubkey_int = p + 1
244245
pubkey = bytes_from_int(pubkey_int)
245-
assert(lift_x(pubkey) is None)
246+
assert(lift_x(pubkey_int) is None)
246247
# If an implementation would reduce a given public key modulo p then the
247248
# pubkey would be valid
248-
assert(lift_x(bytes_from_int(pubkey_int % p)) is not None)
249+
assert(lift_x(pubkey_int % p) is not None)
249250

250251
return (None, pubkey, None, msg, sig, "FALSE", "public key is not a valid X coordinate because it exceeds the field size")
251252

0 commit comments

Comments
 (0)