Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ecdsa/ecdsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ def verifies(self, hash, signature):
xy = G.mul_add(u1, self.point, u2)
else:
xy = u1 * G + u2 * self.point
if xy == ellipticcurve.INFINITY:
return False
v = xy.x() % n
return v == r

Expand Down
13 changes: 13 additions & 0 deletions src/ecdsa/test_ecdsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ def test_verification_with_regular_point(self):

assert pubk.verifies(self.msg, self.sig)

def _assert_rejects_sum_at_infinity(self, generator):
# with r = s = 1 and Q = -msg * G, u1 * G + u2 * Q is the point
# at infinity
n = generator.order()
pubk = Public_key(generator, generator * (-self.msg % n))
assert not pubk.verifies(self.msg, Signature(1, 1))

def test_rejection_when_sum_is_point_at_infinity(self):
self._assert_rejects_sum_at_infinity(generator_192)

def test_rejection_when_sum_is_point_at_infinity_regular_point(self):
self._assert_rejects_sum_at_infinity(generator_192.to_affine())


class TestPublicKey(unittest.TestCase):
def test_equality_public_keys(self):
Expand Down
14 changes: 14 additions & 0 deletions src/ecdsa/test_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,20 @@ def test_VerifyingKey_verify(
assert vrf_mthd(sig, fun(vrf_data), sigdecode=decoder)


def test_VerifyingKey_verify_rejects_sum_at_point_at_infinity():
# with r = s = 1 and Q = -e * G, u1 * G + u2 * Q is the point at
# infinity, which must be a BadSignatureError, not a TypeError
data = b"point at infinity during verify"
n = NIST256p.order
e = int(hashlib.sha256(data).hexdigest(), 16)
vk = VerifyingKey.from_public_point(
NIST256p.generator * (-e % n), curve=NIST256p, hashfunc=hashlib.sha256
)

with pytest.raises(BadSignatureError):
vk.verify(sigencode_der(1, 1, n), data, sigdecode=sigdecode_der)


# test SigningKey.from_string()
prv_key_bytes = (
b"^\xc8B\x0b\xd6\xef\x92R\xa9B\xe9\x89\x04<\xa2"
Expand Down