
    [AhVF                        S r SSKJr  SSKrSSKrSSKrSSKrSSKJr  SSK	J
r
JrJr  SSKJr  SSKJr  S	r\R$                  " \6 rS
rSr\R,                  S:  a  S rOS r\R,                  S:  a  S rOS rS rS,S jr " S S5      rS rS rS r\4S jr\4S jr S r!S r"S r#S r$S r%S  r&S! r'S" r(S# r)S$ r*S% r+S& r, " S' S(\-5      r.S) r/S* r0S+ r1g)-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                 |    [        [        R                  U S5      5      SS R                  [	        U 5      S-  5      $ )-Convert a bytestring to string of 0's and 1'sbigr   N   )binint
from_byteszfilllenent_256s    QC:\Users\julio\Documents\inmobiliaria_backend\env\Lib\site-packages\ecdsa/util.pyentropy_to_bitsr   4   s3    3>>'512126<<S\A=MNN    c                 2    SR                  S U  5       5      $ )r    c              3   n   #    U  H+  n[        [        U5      5      S S R                  S5      v   M-     g7f)r   Nr   )r   ordr   ).0xs     r   	<genexpr>"entropy_to_bits.<locals>.<genexpr><   s,     AAs3q6{12,,Q//s   35)joinr   s    r   r   r   :   s    wwAAAAr   )r      c                 0    [        [        U 5      5      S-
  $ )Nr   )r   r   r#   s    r   
bit_lengthr*   A   s    3q6{Qr   c                 4    U R                  5       =(       d    S$ Nr   )r*   r)   s    r   r*   r*   F   s    ||~""r   c                 *    S[        SU -  5      -   S-  $ )Nr   z%xr   )r   )orders    r   orderlenr/   J   s    D5L!!a''r   c                     U S:  d   eUc  [         R                  n[        U S-
  5      nUS-  S-   n U" U5      n[        U5      n[	        USU SS9S-   nSUs=:  a  U :  a   U$   M6  )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   	randranger9   N   s     199**#G1q I
)$(uXgQ/!3x%O  	 r   c                   &    \ rS rSrS rS rS rSrg)PRNGf   c                 0    U R                  U5      U l        g N)block_generator	generator)selfseeds     r   __init__PRNG.__init__l   s    --d3r   c                     [        U5       Vs/ s H  n[        U R                  5      PM     nn[        (       a  SR	                  U5      $ [        U5      $ s  snf )Nr   )ranger   r@   r   r&   bytes)rA   numbytesias       r   __call__PRNG.__call__o   sD    +0?;?aT$..!?;3771:8O <s   Ac              #      #    Sn [        SX!4-  R                  5       5      R                  5        H  nUv   M	     US-  nM=  7f)Nr   z
prng-%d-%sr   )r   encodedigest)rA   rB   counterbytes       r   r?   PRNG.block_generatorw   sR     /779fh 
 qLG s   AA)r@   N)__name__
__module____qualname____firstlineno__rC   rK   r?   __static_attributes__ r   r   r;   r;   f   s    4r   r;   c                     [        U 5      " S[        U5      -  5      n[        [        R                  " U5      S5      US-
  -  S-   nSUs=::  a  U:  d  O   SX145       eU$ )Nr      r   )r;   r/   r   binasciihexlify)rB   r.   r1   numbers       r   %randrange_from_seed__overshoot_modulor^      sb     :a(5/)*D(""4("-;q@F2F 22Mr   c                     SU -  S-
  $ r,   rX   )numbitss    r   lsb_of_onesra      s    LAr   c                 j    [        [        R                  " U S-
  S5      S-   5      nUS-  nUS-  nXU4$ )Nr   r   r   )r   mathlog)r.   bitsrG   	extrabitss       r   bits_and_bytesrg      s=    txx	1%)*DAIEqI	!!r   c                     [        U5      u  p4nU(       a  US-  nU" U 5      R                  5       S U nSU[        U5      -
  -  U-   nS[        [        R
                  " U5      S5      -   nSUs=::  a  U:  d   e   eU$ )Nr    rZ   )rg   rO   r   r   r[   r\   )rB   r.   hashmodre   _bytesrf   r1   r]   s           r   #randrange_from_seed__truncate_bytesrl      s     -U3D)!4=!'6*DVc$i'(4/DX%%d+R00FMr   c                    [        [        R                  " US-
  S5      S-   5      nUS-   S-  nU" U 5      R                  5       S U nSU[	        U5      -
  -  U-   nSU-  U-
  nU(       a)  [        [        US   5      [        U5      -  5      USS  -   nS[        [        R                  " U5      S5      -   nSUs=::  a  U:  d   e   eU$ )Nr   r   r'   r   ri   r   rZ   )
r   rc   rd   rO   r   r   r!   ra   r[   r\   )rB   r.   rj   re   maxbytesr1   topbitsr]   s           r   "randrange_from_seed__truncate_bitsrp      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                     US:  d   e[        U5      u  p#n[        U 5      n SnU(       a&  [        [        U" S5      5      [	        U5      -  5      n[        Xe" U5      -   5      S-   nSUs=::  a  U:  a   U$   MX  )Nr   r   )rg   r;   r   r!   ra   string_to_number)rB   r.   re   rG   rf   generate	extrabyteguesss           r    randrange_from_seed__trytryagainrv      s     199+E2DDzH
	 Xa[!1K	4J!JKI Xe_!<=AL  r   c                     [        U5      nS[        SU-  5      -   S-   n[        R                  " X0-  R	                  5       5      n[        U5      U:X  d   [        U5      U45       eU$ Nz%0r   r#   )r/   strr[   	unhexlifyrN   r   numr.   lfmt_strstrings        r   number_to_stringr      sb    ASQZ#%G 6 6 89Fv;!-c&k1--Mr   c                     [        U5      nS[        SU-  5      -   S-   n[        R                  " X0-  R	                  5       5      nUS U $ rx   )r/   ry   r[   rz   rN   r{   s        r   number_to_string_cropr      sI    ASQZ#%G 6 6 89F"1:r   c                 B    [        [        R                  " U 5      S5      $ NrZ   )r   r[   r\   )r   s    r   rr   rr      s    x',,r   c                     [        U5      n[        U 5      U:X  d   [        U 5      U45       e[        [        R                  " U 5      S5      $ r   )r/   r   r   r[   r\   )r   r.   r}   s      r   string_to_number_fixedlenr      sC    Av;!-c&k1--x',,r   c                 4    [        X5      n[        X5      nX44$ )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.

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sr.   r_strs_strs        r   sigencode_stringsr      s     $ Q&EQ&E>r   c                 &    [        XU5      u  p4X4-   $ )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    " %Q51LE=r   c                     [         R                  " [         R                  " U 5      [         R                  " U5      5      $ )aO  
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                     XS-  :  a  X-
  n U $ )a  
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   rX   )r   r.   s     r   	_canonizer   3  s     	A:~IHr   c                 0    [        X5      n[        XU5      $ )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    , 	!AQ5))r   c                 0    [        X5      n[        XU5      $ )aC  
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    & 	!AA%((r   c                 0    [        X5      n[        XU5      $ )a  
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 	!Au%%r   c                       \ rS rSrSrSrg)MalformedSignaturei  a*  
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.
rX   N)rS   rT   rU   rV   __doc__rW   rX   r   r   r   r     s     	r   r   c                     [        U 5      n [        U5      n[        U 5      SU-  :X  d'  [        SR	                  SU-  [        U 5      5      5      e[        U SU U5      n[        XS U5      nX44$ )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.   r}   r   r   s        r   sigdecode_stringr     sx    *  	*IAy>QU" 006q1uc)n0M
 	
 	")BQ-7A!)B-7A4Kr   c                    [        U 5      S:X  d#  [        SR                  [        U 5      5      5      eU u  p#[        U5      n[        U5      n[	        U5      n[        U5      U:X  d$  [        SR                  U[        U5      5      5      e[        U5      U:X  d$  [        SR                  U[        U5      5      5      e[        X!5      n[        X15      nXV4$ )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   r}   r   r   s          r   sigdecode_stringsr     s    & z?a AHHJ
 	

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

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

 	"%/A!%/A4Kr   c                 z   [        U 5      n [        R                  " U 5      u  p#US:w  a-  [        R                  " S[        R
                  " U5      -  5      e[        R                  " U5      u  pE[        R                  " U5      u  pcUS:w  a-  [        R                  " S[        R
                  " U5      -  5      eXF4$ )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UnexpectedDERr[   r\   remove_integer)sig_derr.   r   emptyr   restr   s          r   sigdecode_derr     s    2 g&G++G4J|-0@0@0GG
 	
   ,GA!!$'HA|1H4D4DU4KK
 	
 4Kr   r>   )2r   
__future__r   r2   rc   r[   syshashlibr   sixr   r   r   r   r	   _compatr
   oid_ecPublicKey
encode_oidencoded_oid_ecPublicKeyoid_ecDH	oid_ecMQVversion_infor   r*   r/   r9   r;   r^   ra   rg   rl   rp   rv   r   r   rr   r   r   r   r   r   r   r   r   	Exceptionr   r   r   r   rX   r   r   <module>r      s     	   
  # #  $ +../:   	tOB
 f
#(0 6
"& >D  =C &--.*M2"*4).&<
	 
	B+\&r   