
    ":hH,                        d Z ddlmZ ddlmZ  G d de      Z G d de      Z G d d	e      Z G d
 de      Z	d Z
d Zd Zd ZdZdZdZdZdZ ej&                  ede      Z ej*                  eeee      ZdZdZdZdZdZ ej&                  ede      Z ej*                  eeee      ZdZdZdZdZdZ ej&                  ede      Z ej*                  eeee      Zd Zd!Zd"Zd#Zd$Z ej&                  ede      Z ej*                  eeee      Zd%Zd&Zd'Zd(Zd)Z ej&                  ede      Z ej*                  eeee      Zd*Zd+Zd,Zd-Zd.Zd/Z ej&                  eee      Z  ej*                  e eee      Z!y0)1a  
Implementation of Elliptic-Curve Digital Signatures.

Classes and methods for elliptic-curve signatures:
private keys, public keys, signatures,
NIST prime-modulus curves with modulus lengths of
192, 224, 256, 384, and 521 bits.

Example:

  # (In real-life applications, you would probably want to
  # protect against defects in SystemRandom.)
  from random import SystemRandom
  randrange = SystemRandom().randrange

  # Generate a public/private key pair using the NIST Curve P-192:

  g = generator_192
  n = g.order()
  secret = randrange( 1, n )
  pubkey = Public_key( g, g * secret )
  privkey = Private_key( pubkey, secret )

  # Signing a hash value:

  hash = randrange( 1, n )
  signature = privkey.sign( hash, randrange( 1, n ) )

  # Verifying a signature for a hash value:

  if pubkey.verifies( hash, signature ):
    print_("Demo verification succeeded.")
  else:
    print_("*** Demo verification failed.")

  # Verification fails if the hash value is modified:

  if pubkey.verifies( hash-1, signature ):
    print_("**** Demo verification failed to reject tampered hash.")
  else:
    print_("Demo verification correctly rejected tampered hash.")

Version of 2009.05.16.

Revision history:
      2005.12.31 - Initial version.
      2008.11.25 - Substantial revisions introducing new classes.
      2009.05.16 - Warn against using random.randrange in real applications.
      2009.05.17 - Use random.SystemRandom by default.

Written in 2005 by Peter Pearson and placed in the public domain.
   )ellipticcurve)numbertheoryc                       e Zd Zy)RSZeroErrorN)__name__
__module____qualname__     ^/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/static_dependencies/ecdsa/ecdsa.pyr   r   <   s    r   r   c                       e Zd ZdZd Zd Zy)	SignaturezECDSA signature.
  c                 .    || _         || _        || _        y )N)rsrecovery_param)selfr   r   r   s       r   __init__zSignature.__init__D   s    ,r   c                    |j                         }|j                         }| j                  }| j                  }|}|}t	        |d|j                               |j                         |z  z   |j                         z   |j                         z  }	t        j                  |	|j                               }
|
dz  dk(  r|
n|j                         |
z
  }t        j                  ||||      }t        j                  ||      ||z  | |z  |z  z   z  }t        ||      }t        j                  ||| |      }t        j                  ||      ||z  | |z  |z  z   z  }t        ||      }||gS )zReturns two public keys for which the signature is valid
    hash is signed hash
    generator is the used generator of the signature
              )curveorderr   r   powpabr   square_root_mod_primer   Pointinverse_mod
Public_key)r   hash	generatorr   nr   r   exalphabetayR1Q1Pk1R2Q2Pk2s                     r   recover_public_keyszSignature.recover_public_keysI   sQ   
 !OOFFFF Q5779%Q7%'')CuwwyP11%C1HMDuwwy4'7   1a0%%a+q2v!a98L/LMB'   A2q1%%a+q2v!a98L/LMB'Szr   N)r   r   r	   __doc__r   r1   r
   r   r   r   r   @   s    -
r   r   c                       e Zd ZdZd Zd Zy)r"   zPublic key for ECDSA.
  c                 t   |j                         | _         || _        || _        |j                         }|st	        d      ||z  t
        j                  k(  st	        d      |j                         dk  s9||j                         k  s&|j                         dk  s||j                         k  rt	        d      y)zigenerator is the Point that generates the group,
    point is the Point that defines the public key.
    z Generator point must have order.zGenerator point order is bad.r   z(Generator point has x or y out of range.N)	r   r$   pointr   RuntimeErrorr   INFINITYr'   r*   )r   r$   r5   r%   s       r   r   zPublic_key.__init__k   s    
 __&
"
OOABB5yM222>??779q=ANeggi!mqEGGI~IJJ @Nr   c                 H   | j                   }|j                         }|j                  }|j                  }|dk  s||dz
  kD  ry|dk  s||dz
  kD  ryt	        j
                  ||      }||z  |z  }||z  |z  }	||z  |	| j                  z  z   }
|
j                         |z  }||k(  S )zcVerify that signature is a valid signature of hash.
    Return True if the signature is valid.
    r   F)r$   r   r   r   r   r!   r5   r'   )r   r#   	signatureGr%   r   r   cu1u2xyvs               r   verifieszPublic_key.verifies{   s     NNGGIKKKKq5AAIq5AAI$$Q*Qh!^!eq[!Vb4::o%DDFQJAvr   N)r   r   r	   r2   r   r@   r
   r   r   r"   r"   g   s    K r   r"   c                       e Zd ZdZd Zd Zy)Private_keyzPrivate key for ECDSA.
  c                      || _         || _        y)zQpublic_key is of class Public_key;
    secret_multiplier is a large integer.
    N)
public_keysecret_multiplier)r   rD   rE   s      r   r   zPrivate_key.__init__   s    
 %!2r   c                    | j                   j                  }|j                         }||z  }||z  }|j                         |z  }|dk(  rt	        d      t        j                  ||      || j                  |z  |z  z   z  |z  }|dk(  rt	        d      |j                         dz  xs |j                         |k(  rdnd}	t        |||	      S )a  Return a signature for the provided hash, using the provided
    random nonce.  It is absolutely vital that random_k be an unpredictable
    number in the range [1, self.public_key.point.order()-1].  If
    an attacker can guess random_k, he can compute our private key from a
    single signature.  Also, if an attacker knows a few high-order
    bits (or a few low-order bits) of random_k, he can compute our private
    key from many signatures.  The generation of nonces with adequate
    cryptographic strength is very difficult and far beyond the scope
    of this comment.

    May raise RuntimeError, in which case retrying with a new
    random value k is in order.
    r   z!amazingly unlucky random number rz!amazingly unlucky random number sr   )
rD   r$   r   r'   r   r   r!   rE   r*   r   )
r   r#   random_kr:   r%   kp1r   r   r   s
             r   signzPrivate_key.sign   s     OO%%GGIqLUDDFQJ6ABB%%a+d,,q0A557:;<6ABB!@RTTVq[aA~..r   N)r   r   r	   r2   r   rJ   r
   r   r   rB   rB      s    3/r   rB   c                     | dk\  sJ | dk(  ryg }| r3| dz  }|j                  t        j                  |dd             | dz  } | r3|j                          dj	                  |      S )z7Convert integer x into a string of bytes, as per X9.62.r          r   big   r   )appendintto_bytesreversejoin)r'   resultordinals      r   int_to_stringrW      sj    6M6AvF
d(cll7Au56	a 
 NN88Fr   c                 ^    d}| D ]%  }t        |t              st        |      }d|z  |z   }' |S )z8Convert a string of bytes into an integer, as per X9.62.r      )
isinstancerQ   ord)r   rU   r;   s      r   string_to_intr\      s>    F "!S!AAv!" Mr   c                 ^    ddl m} t         |t        |             j	                               S )zmConvert an integer into a string of bytes, compute
     its SHA-1 hash, and convert the result to an integer.r   )sha1)hashlibr^   r\   rW   digest)mr^   s     r   digest_integerrb      s%     mA./66899r   c                     | j                         }| j                         }|dk  s||k  s
|dk  s||k  ry|j                  ||      sy|t        j                  |||      z  t        j
                  k(  syy)z=Is (x,y) a valid public key based on the specified generator?r   FT)r   r   contains_pointr   r    r7   )r$   r'   r*   r%   r   s        r   point_is_validre      ss    
 	AOOE1uQ!a%161%}""5!Q//=3I3IIr   l   l   1(i&^#a;l   9{uDjSg9g(Bl   +' 1t:_|v!a:@ml   H<^W]dZ{cxW\Iq l              ?l   =*8%(?l   FeY8w-X"PVd/%PP!-l   !"X!#BXtJ9!'|%VA-l   4~ 
f&Dv@h!fE0m9_qlM/l   ?               @ l   Q%x+Ohbi+}s   @ l   K`Opq^cv
3,e<1U]>{|R*Zl   B11e	%:f=K`wrH7gHK8hkl   Q~o]l+fUg+<)Z?8O?q!Ol          ~l   s)e`gwlX_[nlv|l   *'#.TEbc+Z'@=D 1 "(?7N2Z_+|S/1fl   
dxRjoyU8T(	:ss"nZL8k&"_Ul   _!uR/sX0
@qaNQNB&JxS8KJEY	K%l l#   l#   	dp"z\}[z3"nZ;PK#
`7roCQl#    ?VQ(zO%b95~cte1oR{V;LHw>l-rZE]"Sr&Ga9}*Fl#   f=xK)H-apY$3^Q	n%k{;/K!u{4-{?$Od8V1l3s:l#   Pf?QE$XN!85aZUWL9YLhzf$Du13otc!%pMxjRr`r      l   /| l   -lO96M:WZEuw>?3yl   T!v?B>bAP)("8p'UxI;m:Hl   AAl 3z~i9nW~ N)"r2    r   r   r6   r   objectr   r"   rB   rW   r\   rb   re   _p_r_b_Gx_GyCurveFp	curve_192r    generator_192	curve_224generator_224	curve_256generator_256	curve_384generator_384	curve_521generator_521_acurve_secp256k1generator_secp256k1r
   r   r   <module>r}      s3  3j  	, 	$ $N( (V'/& '/T	:" @? 888!M!!"b"-	###IsC< JI @@@!M!!"b"-	###IsC< TS HHH!M!!"b"-	###IsC< zy hhh!M!!"b"-	###IsC< c c K K L!M!!"b"-	###IsC< HGGHHG'-''B3)m))/3RH r   