Ë
    V.jVF  ã                   ó¦  — d Z ddlmZ ddlZddlZddlZddlZddlmZ ddl	m
Z
mZmZ ddlmZ ddlmZ d	Z ej$                  eŽ Zd
ZdZej,                  dk\  rd„ Znd„ Zej,                  dk  rd„ Znd„ Zd„ Zd,d„Z G d„ d«      Zd„ Zd„ Zd„ Zefd„Zefd„Z d„ Z!d„ Z"d„ Z#d„ Z$d„ Z%d „ Z&d!„ Z'd"„ Z(d#„ Z)d$„ Z*d%„ Z+d&„ Z, G d'„ d(e-«      Z.d)„ Z/d*„ Z0d+„ Z1y)-aü  
This module includes some utility functions.

The methods most typically used are the sigencode and sigdecode functions
to be used with :func:`~ecdsa.keys.SigningKey.sign` and
:func:`~ecdsa.keys.VerifyingKey.verify`
respectively. See the :func:`sigencode_strings`, :func:`sigdecode_string`,
:func:`sigencode_der`, :func:`sigencode_strings_canonize`,
:func:`sigencode_string_canonize`, :func:`sigencode_der_canonize`,
:func:`sigdecode_strings`, :func:`sigdecode_string`, and
:func:`sigdecode_der` functions.
é    )ÚdivisionN)Úsha256)ÚPY2Úint2byteÚnexté   )Úder)Únormalise_bytes)r   é   iH  i='  r   r   )r   é   é„   r   é   )r   r   r   r   é   )r   c                 ó|   — t        t        j                  | d«      «      dd j                  t	        | «      dz  «      S )ú-Convert a bytestring to string of 0's and 1'sÚbigr   Né   )ÚbinÚintÚ
from_bytesÚzfillÚlen©Úent_256s    úLC:\xampp\htdocs\tradingbinance\backend\.venv\Lib\site-packages\ecdsa/util.pyÚentropy_to_bitsr   4   s3   € ä”3—>‘> '¨5Ó1Ó2°1°2Ð6×<Ñ<¼SÀ»\ÈAÑ=MÓNÐNó    c                 ó2   — dj                  d„ | D «       «      S )r   Ú c              3   óh   K  — | ]*  }t        t        |«      «      d d j                  d«      –— Œ, y­w)r   Nr   )r   Úordr   )Ú.0Úxs     r   Ú	<genexpr>z"entropy_to_bits.<locals>.<genexpr><   s*   è ø€ ÐA¹°A”sœ3˜q›6“{ 1 2�×,Ñ,¨Q×/¹ùs   ‚02)Újoinr   s    r   r   r   :   s   € à�w‰wÑA¹ÓAÓAÐAr   )r   é   c                 ó0   — t        t        | «      «      dz
  S )Nr   )r   r   ©r#   s    r   Ú
bit_lengthr)   A   s   € Ü”3�q“6‹{˜Q‰Ðr   c                 ó*   — | j                  «       xs dS ©Nr   )r)   r(   s    r   r)   r)   F   s   € Ø�|‰|‹~Ò" Ð"r   c                 ó*   — dt        d| z  «      z   dz  S )Nr   z%xr   )r   )Úorders    r   Úorderlenr.   J   s   € Ø”�D˜5‘LÓ!Ñ! aÑ'Ð'r   c                 óÌ   — | dkD  sJ ‚|€t         j                  }t        | dz
  «      }|dz  dz   }	  ||«      }t        |«      }t	        |d| d¬«      dz   }d|cxk  r| k  r|S  Œ5)a  Return a random integer k such that 1 <= k < order, uniformly
    distributed across that range. Worst case should be a mean of 2 loops at
    (2**k)+2.

    Note that this function is not declared to be forwards-compatible: we may
    change the behavior in future releases. The entropy= argument (which
    should get a callable that behaves like os.urandom) can be used to
    achieve stability within a given release (for repeatable unit tests), but
    should not be used as a long-term-compatible key generation algorithm.
    r   Nr   r   )Úbaser   )ÚosÚurandomr)   r   r   )r-   ÚentropyÚupper_2Ú	upper_256r   Úent_2Úrand_nums          r   Ú	randranger8   N   s„   € ð �1Š9Ðˆ9Ø€Ü—*‘*ˆÜ˜ ™Ó#€GØ˜1‘˜qÑ €IØ
Ù˜)Ó$ˆÜ Ó(ˆÜ�u˜X˜g�¨QÔ/°!Ñ3ˆØˆxÔ˜%ÒØˆOð  ð	 r   c                   ó   — e Zd Zd„ Zd„ Zd„ Zy)ÚPRNGc                 ó0   — | j                  |«      | _        y ©N)Úblock_generatorÚ	generator)ÚselfÚseeds     r   Ú__init__zPRNG.__init__l   s   € Ø×-Ñ-¨dÓ3ˆ�r   c                 ó¦   — t        |«      D �cg c]  }t        | j                  «      ‘Œ }}t        rdj	                  |«      S t        |«      S c c}w )Nr   )Úranger   r>   r   r%   Úbytes)r?   ÚnumbytesÚiÚas       r   Ú__call__zPRNG.__call__o   sB   € Ü+0°¬?Ó;©? aŒT�$—.‘.Õ!¨?ˆÐ;åØ—7‘7˜1“:Ðä˜“8ˆOùò <s   ŽAc              #   ó†   K  — d}	 t        d||fz  j                  «       «      j                  «       D ]  }|–— Œ |dz  }Œ;­w)Nr   z
prng-%d-%sr   )r   ÚencodeÚdigest)r?   r@   ÚcounterÚbytes       r   r=   zPRNG.block_generatorw   sU   è ø€ ØˆØÜØ ¨$ Ñ/×7Ñ7Ó9óç‰f‹hò�ð “
ðð �q‰LˆGð ùs   ‚?AN)Ú__name__Ú
__module__Ú__qualname__rA   rH   r=   © r   r   r:   r:   f   s   „ ò4òór   r:   c                 ó¾   —  t        | «      dt        |«      z  «      }t        t        j                  |«      d«      |dz
  z  dz   }d|cxk  r|k  sn J d||f«       ‚|S )Nr   é   r   )r:   r.   r   ÚbinasciiÚhexlify)r@   r-   r0   Únumbers       r   Ú%randrange_from_seed__overshoot_modulorW   �   sf   € ð Œ4�‹:�aœ( 5›/Ñ)Ó*€DÜ”(×"Ñ" 4Ó(¨"Ó-°¸±Ñ;¸qÑ@€FØ�Ô˜ÔÐ2  F¨EÐ 2Ó2ÐØ€Mr   c                 ó   — d| z  dz
  S r+   rQ   )Únumbitss    r   Úlsb_of_onesrZ   Ž   s   € Ø�‰L˜AÑÐr   c                 ój   — t        t        j                  | dz
  d«      dz   «      }|dz  }|dz  }|||fS )Nr   r   r   )r   ÚmathÚlog)r-   ÚbitsrD   Ú	extrabitss       r   Úbits_and_bytesr`   ’   s?   € ÜŒt�x‰x˜ ™	 1Ó%¨Ñ)Ó*€DØ�A‰I€EØ�q‘€IØ�˜	Ð!Ð!r   c                 óð   — t        |«      \  }}}|r|dz  } || «      j                  «       d | }d|t        |«      z
  z  |z   }dt        t	        j
                  |«      d«      z   }d|cxk  r|k  sJ ‚ J ‚|S )Nr   Ú rS   )r`   rK   r   r   rT   rU   )r@   r-   Úhashmodr^   Ú_bytesr_   r0   rV   s           r   Ú#randrange_from_seed__truncate_bytesre   ¥   sŒ   € ô -¨UÓ3Ñ€Dˆ&�)ÙØ�!‰ˆÙ�4‹=×ÑÓ! ' 6Ð*€DØ�Vœc $›iÑ'Ñ(¨4Ñ/€DØ””X×%Ñ% dÓ+¨RÓ0Ñ0€FØ�Ô˜ÒÐÑÐÐØ€Mr   c                 ó„  — t        t        j                  |dz
  d«      dz   «      }|dz   dz  } || «      j                  «       d | }d|t	        |«      z
  z  |z   }d|z  |z
  }|r)t        t        |d   «      t        |«      z  «      |dd  z   }dt        t        j                  |«      d«      z   }d|cxk  r|k  sJ ‚ J ‚|S )Nr   r   r&   r   rb   r   rS   )
r   r\   r]   rK   r   r   r!   rZ   rT   rU   )r@   r-   rc   r^   Úmaxbytesr0   ÚtopbitsrV   s           r   Ú"randrange_from_seed__truncate_bitsri   ³   sÒ   € ô Œt�x‰x˜ ™	 1Ó%¨Ñ)Ó*€DØ�q‘˜Q‰€HÙ�4‹=×ÑÓ! ) 8Ð,€DØ�X¤ D£	Ñ)Ñ*¨TÑ1€DØ�(‰l˜TÑ!€GÙÜœ˜D ™G›¤{°7Ó';Ñ;Ó<¸tÀAÀB¸xÑGˆØ””X×%Ñ% dÓ+¨RÓ0Ñ0€FØ�Ô˜ÒÐÑÐÐØ€Mr   c                 óæ   — |dkD  sJ ‚t        |«      \  }}}t        | «      }	 d}|r&t        t         |d«      «      t	        |«      z  «      }t        | ||«      z   «      dz   }d|cxk  r|k  r|S  ŒP)Nr   r   )r`   r:   r   r!   rZ   Ústring_to_number)r@   r-   r^   rD   r_   ÚgenerateÚ	extrabyteÚguesss           r   Ú randrange_from_seed__trytryagainro   Â   s†   € ð �1Š9Ðˆ9Ü+¨EÓ2Ñ€Dˆ%�Ü�D‹z€HØ
Øˆ	ÙÜ ¤¡X¨a£[Ó!1´KÀ	Ó4JÑ!JÓKˆIÜ  ©X°e«_Ñ!<Ó=ÀÑAˆØ�Ô˜ÒØˆLð ð r   c                 óÐ   — t        |«      }dt        d|z  «      z   dz   }t        j                  || z  j	                  «       «      }t        |«      |k(  sJ t        |«      |f«       ‚|S ©Nz%0r   r#   )r.   ÚstrrT   Ú	unhexlifyrJ   r   ©Únumr-   ÚlÚfmt_strÚstrings        r   Únumber_to_stringry   Õ   sd   € Ü�‹€AØ”S˜˜Q™“ZÑ #Ñ%€GÜ×Ñ ¨3¡× 6Ñ 6Ó 8Ó9€FÜˆv‹;˜!ÒÐ-œc &›k¨1Ð-Ó-ÐØ€Mr   c                 ó–   — t        |«      }dt        d|z  «      z   dz   }t        j                  || z  j	                  «       «      }|d | S rq   )r.   rr   rT   rs   rJ   rt   s        r   Únumber_to_string_cropr{   Ý   sK   € Ü�‹€AØ”S˜˜Q™“ZÑ #Ñ%€GÜ×Ñ ¨3¡× 6Ñ 6Ó 8Ó9€FØ�"�1ˆ:Ðr   c                 ó@   — t        t        j                  | «      d«      S ©NrS   )r   rT   rU   )rx   s    r   rk   rk   ä   s   € ÜŒx×Ñ Ó'¨Ó,Ð,r   c                 ó–   — t        |«      }t        | «      |k(  sJ t        | «      |f«       ‚t        t        j                  | «      d«      S r}   )r.   r   r   rT   rU   )rx   r-   rv   s      r   Ústring_to_number_fixedlenr   è   sC   € Ü�‹€AÜˆv‹;˜!ÒÐ-œc &›k¨1Ð-Ó-ÐÜŒx×Ñ Ó'¨Ó,Ð,r   c                 ó:   — t        | |«      }t        ||«      }||fS )aZ  
    Encode the signature to a pair of strings in a tuple

    Encodes signature into raw encoding (:term:`raw encoding`) with the
    ``r`` and ``s`` parts of the signature encoded separately.

    It's expected that this function will be used as a ``sigencode=`` parameter
    in :func:`ecdsa.keys.SigningKey.sign` method.

    :param int r: first parameter of the signature
    :param int s: second parameter of the signature
    :param int order: the order of the curve over which the signature was
        computed

    :return: raw encoding of ECDSA signature
    :rtype: tuple(bytes, bytes)
    )ry   ©ÚrÚsr-   Úr_strÚs_strs        r   Úsigencode_stringsr†   î   s&   € ô$ ˜Q Ó&€EÜ˜Q Ó&€EØ�5ˆ>Ðr   c                 ó,   — t        | ||«      \  }}||z   S )aÉ  
    Encode the signature to raw format (:term:`raw encoding`)

    It's expected that this function will be used as a ``sigencode=`` parameter
    in :func:`ecdsa.keys.SigningKey.sign` method.

    :param int r: first parameter of the signature
    :param int s: second parameter of the signature
    :param int order: the order of the curve over which the signature was
        computed

    :return: raw encoding of ECDSA signature
    :rtype: bytes
    )r†   r�   s        r   Úsigencode_stringrˆ     s    € ô" % Q¨¨5Ó1�L€Eˆ5Ø�5‰=Ðr   c                 óz   — t        j                  t        j                  | «      t        j                  |«      «      S )a‹  
    Encode the signature into the ECDSA-Sig-Value structure using :term:`DER`.

    Encodes the signature to the following :term:`ASN.1` structure::

        Ecdsa-Sig-Value ::= SEQUENCE {
            r       INTEGER,
            s       INTEGER
        }

    It's expected that this function will be used as a ``sigencode=`` parameter
    in :func:`ecdsa.keys.SigningKey.sign` method.

    :param int r: first parameter of the signature
    :param int s: second parameter of the signature
    :param int order: the order of the curve over which the signature was
        computed

    :return: DER encoding of ECDSA signature
    :rtype: bytes
    )r	   Úencode_sequenceÚencode_integer©r‚   rƒ   r-   s      r   Úsigencode_derr�     s-   € ô, ×Ñœs×1Ñ1°!Ó4´c×6HÑ6HÈÓ6KÓLÐLr   c                 ó    — | |dz  kD  r|| z
  } | S )a8  
    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
    r   rQ   )rƒ   r-   s     r   Ú	_canonizer�   3  s   € ð 	ˆ5�A‰:‚~Ø�A‰IˆØ€Hr   c                 ó4   — t        ||«      }t        | ||«      S )a  
    Encode the signature to a pair of strings in a tuple

    Encodes signature into raw encoding (:term:`raw encoding`) with the
    ``r`` and ``s`` parts of the signature encoded separately.

    Makes sure that the signature is encoded in the canonical format, where
    the ``s`` parameter is always smaller than ``order / 2``.
    Most commonly used in bitcoin.

    It's expected that this function will be used as a ``sigencode=`` parameter
    in :func:`ecdsa.keys.SigningKey.sign` method.

    :param int r: first parameter of the signature
    :param int s: second parameter of the signature
    :param int order: the order of the curve over which the signature was
        computed

    :return: raw encoding of ECDSA signature
    :rtype: tuple(bytes, bytes)
    )r�   r†   rŒ   s      r   Úsigencode_strings_canonizer‘   D  s   € ô, 	�!�UÓ€AÜ˜Q  5Ó)Ð)r   c                 ó4   — t        ||«      }t        | ||«      S )aw  
    Encode the signature to raw format (:term:`raw encoding`)

    Makes sure that the signature is encoded in the canonical format, where
    the ``s`` parameter is always smaller than ``order / 2``.
    Most commonly used in bitcoin.

    It's expected that this function will be used as a ``sigencode=`` parameter
    in :func:`ecdsa.keys.SigningKey.sign` method.

    :param int r: first parameter of the signature
    :param int s: second parameter of the signature
    :param int order: the order of the curve over which the signature was
        computed

    :return: raw encoding of ECDSA signature
    :rtype: bytes
    )r�   rˆ   rŒ   s      r   Úsigencode_string_canonizer“   ^  s   € ô& 	�!�UÓ€AÜ˜A˜q %Ó(Ð(r   c                 ó4   — t        ||«      }t        | ||«      S )a9  
    Encode the signature into the ECDSA-Sig-Value structure using :term:`DER`.

    Makes sure that the signature is encoded in the canonical format, where
    the ``s`` parameter is always smaller than ``order / 2``.
    Most commonly used in bitcoin.

    Encodes the signature to the following :term:`ASN.1` structure::

        Ecdsa-Sig-Value ::= SEQUENCE {
            r       INTEGER,
            s       INTEGER
        }

    It's expected that this function will be used as a ``sigencode=`` parameter
    in :func:`ecdsa.keys.SigningKey.sign` method.

    :param int r: first parameter of the signature
    :param int s: second parameter of the signature
    :param int order: the order of the curve over which the signature was
        computed

    :return: DER encoding of ECDSA signature
    :rtype: bytes
    )r�   r�   rŒ   s      r   Úsigencode_der_canonizer•   u  s   € ô4 	�!�UÓ€AÜ˜˜A˜uÓ%Ð%r   c                   ó   — e Zd ZdZy)ÚMalformedSignatureaB  
    Raised by decoding functions when the signature is malformed.

    Malformed in this context means that the relevant strings or integers
    do not match what a signature over provided curve would create. Either
    because the byte strings have incorrect lengths or because the encoded
    values are too large.
    N)rN   rO   rP   Ú__doc__rQ   r   r   r—   r—   “  s   „ ñð 	r   r—   c                 óâ   — t        | «      } t        |«      }t        | «      d|z  k(  s't        dj	                  d|z  t        | «      «      «      ‚t        | d| |«      }t        | |d |«      }||fS )aà  
    Decoder for :term:`raw encoding`  of ECDSA signatures.

    raw encoding is a simple concatenation of the two integers that comprise
    the signature, with each encoded using the same amount of bytes depending
    on curve size/order.

    It's expected that this function will be used as the ``sigdecode=``
    parameter to the :func:`ecdsa.keys.VerifyingKey.verify` method.

    :param signature: encoded signature
    :type signature: bytes like object
    :param order: order of the curve over which the signature was computed
    :type order: int

    :raises MalformedSignature: when the encoding of the signature is invalid

    :return: tuple with decoded ``r`` and ``s`` values of signature
    :rtype: tuple of ints
    r   zWInvalid length of signature, expected {0} bytes long, provided string is {1} bytes longN)r
   r.   r   r—   Úformatr   )Ú	signaturer-   rv   r‚   rƒ   s        r   Úsigdecode_stringrœ      s|   € ô*   	Ó*€IÜ�‹€AÜˆy‹>˜Q ™UÒ"Ü ð0ß06±°q¸1±u¼cÀ)»nÓ0Mó
ð 	
ô 	" )¨B¨Q -°Ó7€AÜ! )¨A¨B -°Ó7€AØˆaˆ4€Kr   c                 ó°  — t        | «      dk(  s#t        dj                  t        | «      «      «      ‚| \  }}t        |«      }t        |«      }t	        |«      }t        |«      |k(  s$t        dj                  |t        |«      «      «      ‚t        |«      |k(  s$t        dj                  |t        |«      «      «      ‚t        ||«      }t        ||«      }||fS )aÁ  
    Decode the signature from two strings.

    First string needs to be a big endian encoding of ``r``, second needs to
    be a big endian encoding of the ``s`` parameter of an ECDSA signature.

    It's expected that this function will be used as the ``sigdecode=``
    parameter to the :func:`ecdsa.keys.VerifyingKey.verify` method.

    :param list rs_strings: list of two bytes-like objects, each encoding one
        parameter of signature
    :param int order: order of the curve over which the signature was computed

    :raises MalformedSignature: when the encoding of the signature is invalid

    :return: tuple with decoded ``r`` and ``s`` values of signature
    :rtype: tuple of ints
    r   z3Invalid number of strings provided: {0}, expected 2zjInvalid length of first string ('r' parameter), expected {0} bytes long, provided string is {1} bytes longzkInvalid length of second string ('s' parameter), expected {0} bytes long, provided string is {1} bytes long)r   r—   rš   r
   r.   r   )Ú
rs_stringsr-   r„   r…   rv   r‚   rƒ   s          r   Úsigdecode_stringsrŸ   Á  sÙ   € ô& ˆz‹?˜aÒÜ ØA×HÑHÜ�J“óó
ð 	
ð
  �N€UˆEÜ˜EÓ"€EÜ˜EÓ"€EÜ�‹€AÜˆu‹:˜Š?Ü ðç™ ¤3 u£:Ó.ó
ð 	
ô
 ˆu‹:˜Š?Ü ðç™ ¤3 u£:Ó.ó
ð 	
ô
 	" %¨Ó/€AÜ! %¨Ó/€AØˆaˆ4€Kr   c                 óp  — t        | «      } t        j                  | «      \  }}|dk7  r+t        j                  dt	        j
                  |«      z  «      ‚t        j                  |«      \  }}t        j                  |«      \  }}|dk7  r+t        j                  dt	        j
                  |«      z  «      ‚||fS )a  
    Decoder for DER format of ECDSA signatures.

    DER format of signature is one that uses the :term:`ASN.1` :term:`DER`
    rules to encode it as a sequence of two integers::

        Ecdsa-Sig-Value ::= SEQUENCE {
            r       INTEGER,
            s       INTEGER
        }

    It's expected that this function will be used as as the ``sigdecode=``
    parameter to the :func:`ecdsa.keys.VerifyingKey.verify` method.

    :param sig_der: encoded signature
    :type sig_der: bytes like object
    :param order: order of the curve over which the signature was computed
    :type order: int

    :raises UnexpectedDER: when the encoding of signature is invalid

    :return: tuple with decoded ``r`` and ``s`` values of signature
    :rtype: tuple of ints
    r   ztrailing junk after DER sig: %sz#trailing junk after DER numbers: %s)r
   r	   Úremove_sequenceÚUnexpectedDERrT   rU   Úremove_integer)Úsig_derr-   rž   Úemptyr‚   Úrestrƒ   s          r   Úsigdecode_derr§   ï  s²   € ô2 ˜gÓ&€Gä×+Ñ+¨GÓ4Ñ€J�Ø�‚|Ü×ÑØ-´×0@Ñ0@ÀÓ0GÑGó
ð 	
ô × Ñ  Ó,�G€A€tÜ×!Ñ! $Ó'�H€A€uØ�‚|Ü×ÑØ1´H×4DÑ4DÀUÓ4KÑKó
ð 	
ð ˆaˆ4€Kr   r<   )2r˜   Ú
__future__r   r1   r\   rT   ÚsysÚhashlibr   Úsixr   r   r   r   r	   Ú_compatr
   Úoid_ecPublicKeyÚ
encode_oidÚencoded_oid_ecPublicKeyÚoid_ecDHÚ	oid_ecMQVÚversion_infor   r)   r.   r8   r:   rW   rZ   r`   re   ri   ro   ry   r{   rk   r   r†   rˆ   r�   r�   r‘   r“   r•   Ú	Exceptionr—   rœ   rŸ   r§   rQ   r   r   Ú<module>r´      s  ðñõ  ã 	Û Û Û 
Ý ß #Ñ #Ý Ý $ð +€Ø(˜#Ÿ.™.¨/Ð:Ð ð €ð €	à×Ñ�tÒóOòBð
 ×Ñ�fÒóò
#ò(ó÷0ñ ò6
òò"ð& >Dó ð =Có òò&òò-ò-òò.ò*Mò2ò"*ò4)ò.&ô<
	˜ô 
	òòB+ó\&r   