Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use integer division for canonicalization of signatures #358

Merged
merged 1 commit into from
Mar 13, 2025
Merged
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
17 changes: 17 additions & 0 deletions src/ecdsa/test_pyecdsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,23 @@ def test_sigencode_der_canonize(self):
self.assertEqual(r, new_r)
self.assertEqual(order - s, new_s)

def test_sigencode_der_canonize_with_close_to_half_order(self):
r = 13
order = SECP112r1.order
s = order // 2 + 1

regular_encode = sigencode_der(r, s, order)
canonical_encode = sigencode_der_canonize(r, s, order)

self.assertNotEqual(regular_encode, canonical_encode)

new_r, new_s = sigdecode_der(
sigencode_der_canonize(r, s, order), order
)

self.assertEqual(r, new_r)
self.assertEqual(order - s, new_s)

def test_sig_decode_strings_with_invalid_count(self):
with self.assertRaises(MalformedSignature):
sigdecode_strings([b"one", b"two", b"three"], 0xFF)
Expand Down
26 changes: 20 additions & 6 deletions src/ecdsa/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,23 @@ def sigencode_der(r, s, order):
return der.encode_sequence(der.encode_integer(r), der.encode_integer(s))


def _canonize(s, order):
"""
Internal function for ensuring that the ``s`` value of a signature is in
the "canonical" format.

:param int s: the second parameter of ECDSA signature
:param int order: the order of the curve over which the signatures was
computed

:return: canonical value of s
:rtype: int
"""
if s > order // 2:
s = order - s
return s


def sigencode_strings_canonize(r, s, order):
"""
Encode the signature to a pair of strings in a tuple
Expand All @@ -326,8 +343,7 @@ def sigencode_strings_canonize(r, s, order):
:return: raw encoding of ECDSA signature
:rtype: tuple(bytes, bytes)
"""
if s > order / 2:
s = order - s
s = _canonize(s, order)
return sigencode_strings(r, s, order)


Expand All @@ -350,8 +366,7 @@ def sigencode_string_canonize(r, s, order):
:return: raw encoding of ECDSA signature
:rtype: bytes
"""
if s > order / 2:
s = order - s
s = _canonize(s, order)
return sigencode_string(r, s, order)


Expand Down Expand Up @@ -381,8 +396,7 @@ def sigencode_der_canonize(r, s, order):
:return: DER encoding of ECDSA signature
:rtype: bytes
"""
if s > order / 2:
s = order - s
s = _canonize(s, order)
return sigencode_der(r, s, order)


Expand Down
Loading