Skip to content

Commit

Permalink
Merge pull request #1729 from theStack/fix-bip0340-test-vector-gen_li…
Browse files Browse the repository at this point in the history
…ft_x

BIP-340: fix `lift_x` calls in test vector generation script
  • Loading branch information
murchandamus authored Dec 23, 2024
2 parents a0d4fb1 + 7d921e3 commit 665712c
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions bip-0340/test-vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ def vector0():
# we should have at least one test vector where the point reconstructed
# from the public key has a square and one where it has a non-square Y
# coordinate. In this one Y is non-square.
pubkey_point = lift_x(pubkey)
pubkey_point = lift_x(int_from_bytes(pubkey))
assert(not has_square_y(pubkey_point))

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

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

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

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

# The point reconstructed from the public key has a square Y coordinate.
pubkey = pubkey_gen(seckey)
pubkey_point = lift_x(pubkey)
pubkey_point = lift_x(int_from_bytes(pubkey))
assert(has_square_y(pubkey_point))

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

return (seckey, pubkey, aux_rand, msg, sig, "TRUE", None)
Expand Down Expand Up @@ -119,8 +119,9 @@ def vector5():
msg = default_msg
sig = schnorr_sign(msg, seckey, default_aux_rand)

pubkey = bytes_from_int(0xEEFDEA4CDB677750A420FEE807EACF21EB9898AE79B9768766E4FAA04A2D4A34)
assert(lift_x(pubkey) is None)
pubkey_int = 0xEEFDEA4CDB677750A420FEE807EACF21EB9898AE79B9768766E4FAA04A2D4A34
pubkey = bytes_from_int(pubkey_int)
assert(lift_x(pubkey_int) is None)

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

Expand Down Expand Up @@ -197,9 +198,9 @@ def vector11():
sig = schnorr_sign(msg, seckey, default_aux_rand)

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

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

Expand Down Expand Up @@ -242,10 +243,10 @@ def vector14():
sig = schnorr_sign(msg, seckey, default_aux_rand)
pubkey_int = p + 1
pubkey = bytes_from_int(pubkey_int)
assert(lift_x(pubkey) is None)
assert(lift_x(pubkey_int) is None)
# If an implementation would reduce a given public key modulo p then the
# pubkey would be valid
assert(lift_x(bytes_from_int(pubkey_int % p)) is not None)
assert(lift_x(pubkey_int % p) is not None)

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

Expand Down

0 comments on commit 665712c

Please sign in to comment.