1+ """
2+ This module includes some utility functions.
3+
4+ The methods most typically used are the sigencode and sigdecode functions
5+ to be used with :func:`~ecdsa.keys.SigningKey.sign` and
6+ :func:`~ecdsa.keys.VerifyingKey.verify`
7+ respectively. See the :func:`sigencode_strings`, :func:`sigdecode_string`,
8+ :func:`sigencode_der`, :func:`sigencode_strings_canonize`,
9+ :func:`sigencode_string_canonize`, :func:`sigencode_der_canonize`,
10+ :func:`sigdecode_strings`, :func:`sigdecode_string`, and
11+ :func:`sigdecode_der` functions.
12+ """
13+
114from __future__ import division
215
316import os
922from . import der
1023from ._compat import normalise_bytes
1124
25+
1226# RFC5480:
1327# The "unrestricted" algorithm identifier is:
1428# id-ecPublicKey OBJECT IDENTIFIER ::= {
@@ -221,12 +235,24 @@ def string_to_number_fixedlen(string, order):
221235 return int (binascii .hexlify (string ), 16 )
222236
223237
224- # these methods are useful for the sigencode= argument to SK.sign() and the
225- # sigdecode= argument to VK.verify(), and control how the signature is packed
226- # or unpacked.
238+ def sigencode_strings (r , s , order ):
239+ """
240+ Encode the signature to a pair of strings in a tuple
241+
242+ Encodes signature into raw encoding (:term:`raw encoding`) with the
243+ ``r`` and ``s`` parts of the signature encoded separately.
227244
245+ It's expected that this function will be used as a ``sigencode=`` parameter
246+ in :func:`ecdsa.keys.SigningKey.sign` method.
228247
229- def sigencode_strings (r , s , order ):
248+ :param int r: first parameter of the signature
249+ :param int s: second parameter of the signature
250+ :param int order: the order of the curve over which the signature was
251+ computed
252+
253+ :return: raw encoding of ECDSA signature
254+ :rtype: tuple(bytes, bytes)
255+ """
230256 r_str = number_to_string (r , order )
231257 s_str = number_to_string (s , order )
232258 return (r_str , s_str )
@@ -236,7 +262,7 @@ def sigencode_string(r, s, order):
236262 """
237263 Encode the signature to raw format (:term:`raw encoding`)
238264
239- It's expected that this function will be used as a `sigencode=` parameter
265+ It's expected that this function will be used as a `` sigencode=` ` parameter
240266 in :func:`ecdsa.keys.SigningKey.sign` method.
241267
242268 :param int r: first parameter of the signature
@@ -264,7 +290,7 @@ def sigencode_der(r, s, order):
264290 s INTEGER
265291 }
266292
267- It's expected that this function will be used as a `sigencode=` parameter
293+ It's expected that this function will be used as a `` sigencode=` ` parameter
268294 in :func:`ecdsa.keys.SigningKey.sign` method.
269295
270296 :param int r: first parameter of the signature
@@ -278,23 +304,83 @@ def sigencode_der(r, s, order):
278304 return der .encode_sequence (der .encode_integer (r ), der .encode_integer (s ))
279305
280306
281- # canonical versions of sigencode methods
282- # these enforce low S values, by negating the value (modulo the order) if
283- # above order/2 see CECKey::Sign()
284- # https://github.com/bitcoin/bitcoin/blob/master/src/key.cpp#L214
285307def sigencode_strings_canonize (r , s , order ):
308+ """
309+ Encode the signature to a pair of strings in a tuple
310+
311+ Encodes signature into raw encoding (:term:`raw encoding`) with the
312+ ``r`` and ``s`` parts of the signature encoded separately.
313+
314+ Makes sure that the signature is encoded in the canonical format, where
315+ the ``s`` parameter is always smaller than ``order / 2``.
316+ Most commonly used in bitcoin.
317+
318+ It's expected that this function will be used as a ``sigencode=`` parameter
319+ in :func:`ecdsa.keys.SigningKey.sign` method.
320+
321+ :param int r: first parameter of the signature
322+ :param int s: second parameter of the signature
323+ :param int order: the order of the curve over which the signature was
324+ computed
325+
326+ :return: raw encoding of ECDSA signature
327+ :rtype: tuple(bytes, bytes)
328+ """
286329 if s > order / 2 :
287330 s = order - s
288331 return sigencode_strings (r , s , order )
289332
290333
291334def sigencode_string_canonize (r , s , order ):
335+ """
336+ Encode the signature to raw format (:term:`raw encoding`)
337+
338+ Makes sure that the signature is encoded in the canonical format, where
339+ the ``s`` parameter is always smaller than ``order / 2``.
340+ Most commonly used in bitcoin.
341+
342+ It's expected that this function will be used as a ``sigencode=`` parameter
343+ in :func:`ecdsa.keys.SigningKey.sign` method.
344+
345+ :param int r: first parameter of the signature
346+ :param int s: second parameter of the signature
347+ :param int order: the order of the curve over which the signature was
348+ computed
349+
350+ :return: raw encoding of ECDSA signature
351+ :rtype: bytes
352+ """
292353 if s > order / 2 :
293354 s = order - s
294355 return sigencode_string (r , s , order )
295356
296357
297358def sigencode_der_canonize (r , s , order ):
359+ """
360+ Encode the signature into the ECDSA-Sig-Value structure using :term:`DER`.
361+
362+ Makes sure that the signature is encoded in the canonical format, where
363+ the ``s`` parameter is always smaller than ``order / 2``.
364+ Most commonly used in bitcoin.
365+
366+ Encodes the signature to the following :term:`ASN.1` structure::
367+
368+ Ecdsa-Sig-Value ::= SEQUENCE {
369+ r INTEGER,
370+ s INTEGER
371+ }
372+
373+ It's expected that this function will be used as a ``sigencode=`` parameter
374+ in :func:`ecdsa.keys.SigningKey.sign` method.
375+
376+ :param int r: first parameter of the signature
377+ :param int s: second parameter of the signature
378+ :param int order: the order of the curve over which the signature was
379+ computed
380+
381+ :return: DER encoding of ECDSA signature
382+ :rtype: bytes
383+ """
298384 if s > order / 2 :
299385 s = order - s
300386 return sigencode_der (r , s , order )
@@ -321,7 +407,7 @@ def sigdecode_string(signature, order):
321407 the signature, with each encoded using the same amount of bytes depending
322408 on curve size/order.
323409
324- It's expected that this function will be used as the `sigdecode=`
410+ It's expected that this function will be used as the `` sigdecode=` `
325411 parameter to the :func:`ecdsa.keys.VerifyingKey.verify` method.
326412
327413 :param signature: encoded signature
@@ -331,7 +417,7 @@ def sigdecode_string(signature, order):
331417
332418 :raises MalformedSignature: when the encoding of the signature is invalid
333419
334- :return: tuple with decoded 'r' and 's' values of signature
420+ :return: tuple with decoded ``r`` and ``s`` values of signature
335421 :rtype: tuple of ints
336422 """
337423 signature = normalise_bytes (signature )
@@ -350,10 +436,10 @@ def sigdecode_strings(rs_strings, order):
350436 """
351437 Decode the signature from two strings.
352438
353- First string needs to be a big endian encoding of 'r' , second needs to
354- be a big endian encoding of the 's' parameter of an ECDSA signature.
439+ First string needs to be a big endian encoding of ``r`` , second needs to
440+ be a big endian encoding of the ``s`` parameter of an ECDSA signature.
355441
356- It's expected that this function will be used as the `sigdecode=`
442+ It's expected that this function will be used as the `` sigdecode=` `
357443 parameter to the :func:`ecdsa.keys.VerifyingKey.verify` method.
358444
359445 :param list rs_strings: list of two bytes-like objects, each encoding one
@@ -362,7 +448,7 @@ def sigdecode_strings(rs_strings, order):
362448
363449 :raises MalformedSignature: when the encoding of the signature is invalid
364450
365- :return: tuple with decoded 'r' and 's' values of signature
451+ :return: tuple with decoded ``r`` and ``s`` values of signature
366452 :rtype: tuple of ints
367453 """
368454 if not len (rs_strings ) == 2 :
@@ -404,7 +490,7 @@ def sigdecode_der(sig_der, order):
404490 s INTEGER
405491 }
406492
407- It's expected that this function will be used as as the `sigdecode=`
493+ It's expected that this function will be used as as the `` sigdecode=` `
408494 parameter to the :func:`ecdsa.keys.VerifyingKey.verify` method.
409495
410496 :param sig_der: encoded signature
@@ -414,7 +500,7 @@ def sigdecode_der(sig_der, order):
414500
415501 :raises UnexpectedDER: when the encoding of signature is invalid
416502
417- :return: tuple with decoded 'r' and 's' values of signature
503+ :return: tuple with decoded ``r`` and ``s`` values of signature
418504 :rtype: tuple of ints
419505 """
420506 sig_der = normalise_bytes (sig_der )
0 commit comments