
    tZhVF                        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    f/var/www/html/wine-match-dev/backend/winematch-backend/venv/lib/python3.12/site-packages/ecdsa/util.pyentropy_to_bitsr   4   s3    3>>'51212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)     AAs3q6{12,,Q/As   02)joinr   s    r   r   r   :   s    wwAAAAr   )r      c                 0    t        t        |             dz
  S )Nr   )r   r   r#   s    r   
bit_lengthr)   A   s    3q6{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    D5L!!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     199**#G1q I
)$(uXg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   sA    +0?;aT$..!;;771:8O <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fh 
 qLG 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FE 22Mr   c                     d| z  dz
  S r+   rQ   )numbitss    r   lsb_of_onesrZ      s    LA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?    txx	1%)*DAIE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Vc$i'(4/DX%%d+R00F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     txx	1%)*DqQH4=!)8,DXD	)*T1D(lT!GDG{7';;<tABxGX%%d+R00F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     199+E2D%DzH
	 Xa[!1K	4J!JKI Xe_!<=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9Fv;!-c&k1--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9F"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&k1--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1LE5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!4c6H6H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     	5A:~AI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Aq%((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Au%%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" 006q1uc)n0M
 	
 	")BQ-7A!)AB-7Aa4Kr   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
 	

  NUEE"EE"EAu:? 3u:.
 	

 u:? 3u:.
 	

 	"%/A!%/Aa4Kr   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
 	
   ,GAt!!$'HAu|1H4D4DU4KK
 	
 a4Kr   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   