
    E#iH,                        S r SSKJr  SSKJr   " S S\5      r " S S\5      r " S S	\5      r " S
 S\5      r	S r
S rS rS rSrSrSrSrSr\R&                  " \S\5      r\R*                  " \\\\5      rSrSrSrSrSr\R&                  " \S\5      r\R*                  " \\\\5      rSrSrSrSrSr\R&                  " \S\5      r\R*                  " \\\\5      rS rS!rS"rS#rS$r\R&                  " \S\5      r\R*                  " \\\\5      rS%rS&rS'rS(rS)r\R&                  " \S\5      r\R*                  " \\\\5      rS*rS+rS,rS-rS.rS/r\R&                  " \\\5      r \R*                  " \ \\\5      r!g0)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                       \ rS rSrSrg)RSZeroError<    N)__name__
__module____qualname____firstlineno____static_attributes__r       ^/home/james-whalen/.local/lib/python3.13/site-packages/ccxt/static_dependencies/ecdsa/ecdsa.pyr   r   <   s    r   r   c                   $    \ rS rSrSrS rS rSrg)	Signature@   zECDSA signature.
  c                 (    Xl         X l        X0l        g )N)rsrecovery_param)selfr   r   r   s       r   __init__Signature.__init__D   s    ,r   c                    UR                  5       nUR                  5       nU R                  nU R                  nUnUn[	        USUR                  5       5      UR                  5       U-  -   UR                  5       -   UR                  5       -  n	[        R                  " XR                  5       5      n
U
S-  S:X  a  U
OUR                  5       U
-
  n[        R                  " X8X5      n[        R                  " XT5      Xl-  U* U-  U-  -   -  n[        X-5      n[        R                  " X8U* U5      n[        R                  " XT5      Xo-  U* U-  U-  -   -  n[        UU5      nUU/$ )z~Returns 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_keysSignature.recover_public_keysI   s?   
 !OOFFFF Q5779%Q7%'')CuwwyP11%C1HMDuwwy4'7   10%%a+qv!a98L/LM'   A2q1%%a+qv!a98L/LMB'Szr   )r   r   r   N)r	   r
   r   r   __doc__r   r6   r   r   r   r   r   r   @   s    -
r   r   c                   $    \ rS rSrSrS rS rSrg)r'   g   zPublic key for ECDSA.
  c                 ~   UR                  5       U l         Xl        X l        UR                  5       nU(       d  [	        S5      eX2-  [
        R                  :X  d  [	        S5      eUR                  5       S:  d:  X2R                  5       ::  d'  UR                  5       S:  d  X2R                  5       ::  a  [	        S5      eg)zagenerator 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)   r<   r*   s       r   r   Public_key.__init__k   s    
 __&
"
OOABByM222>??779q=ANeggi!mqGGI~IJJ @Nr   c                 B   U R                   nUR                  5       nUR                  nUR                  nUS:  d  XTS-
  :  a  gUS:  d  XdS-
  :  a  g[        R
                  " Xd5      nX-  U-  nXW-  U-  n	X-  XR                  -  -   n
U
R                  5       U-  nX:H  $ )z[Verify that signature is a valid signature of hash.
Return True if the signature is valid.
r   F)r)   r   r   r   r   r&   r<   r,   )r   r(   	signatureGr*   r   r   cu1u2xyvs               r   verifiesPublic_key.verifies{   s     NNGGIKKKKq5AAIq5AAI$$Q*h!^eq[Vb::o%DDFQJvr   )r   r)   r<   N)r	   r
   r   r   r8   r   rH   r   r   r   r   r'   r'   g   s    K r   r'   c                   $    \ rS rSrSrS rS rSrg)Private_key   zPrivate key for ECDSA.
  c                     Xl         X l        g)zIpublic_key is of class Public_key;
secret_multiplier is a large integer.
N
public_keysecret_multiplier)r   rO   rP   s      r   r   Private_key.__init__   s    
 %!2r   c                    U R                   R                  nUR                  5       nX$-  nXS-  nUR                  5       U-  nUS:X  a  [	        S5      e[
        R                  " XT5      XR                  U-  U-  -   -  U-  nUS:X  a  [	        S5      eUR                  5       S-  =(       d    UR                  5       U:X  a  SOSn	[        XxU	5      $ )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   )
rO   r)   r   r,   r   r   r&   rP   r/   r   )
r   r(   random_krB   r*   kp1r   r   r   s
             r   signPrivate_key.sign   s     OO%%GGILUDDFQJ6ABB%%a+,,q0A557:;<6ABB!@RTTVq[a~..r   rN   N)r	   r
   r   r   r8   r   rV   r   r   r   r   rK   rK      s    3/r   rK   c                     U S:  d   eU S:X  a  g/ nU (       a9  U S-  nUR                  [        R                  USS5      5        U S-  n U (       a  M9  UR                  5         SR	                  U5      $ )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_stringrd      sl    6M6AvF
d(cll7Au56	a !
 NN88Fr   c                 l    SnU  H+  n[        U[        5      (       d  [        U5      nSU-  U-   nM-     U$ )z8Convert a string of bytes into an integer, as per X9.62.r      )
isinstancer^   ord)r   rb   rC   s      r   string_to_intri      s<    F!S!!AAv!  Mr   c                 ^    SSK Jn  [        U" [        U 5      5      R	                  5       5      $ )zhConvert an integer into a string of bytes, compute
its SHA-1 hash, and convert the result to an integer.r   )sha1)hashlibrk   ri   rd   digest)mrk   s     r   digest_integerro      s%     mA./66899r   c                     U R                  5       nU R                  5       nUS:  d  X1::  d  US:  d  X2::  a  gUR                  X5      (       d  gU[        R                  " XAU5      -  [        R
                  :X  d  gg)z=Is (x,y) a valid public key based on the specified generator?r   FT)r   r   contains_pointr   r%   r>   )r)   r,   r/   r*   r   s        r   point_is_validrr      sn    
 	AOOE1u!a%16%%}""5Q//=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)"r8    r   r   r=   r   objectr   r'   rK   rd   ri   ro   rr   _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      s  3j  	, 	$ $N( (V'/& '/T	:" @? 888!!"b"-	##IsC< JI @@@!!"b"-	##IsC< TS HHH!!"b"-	##IsC< zy hhh!!"b"-	##IsC< c c K K L!!"b"-	##IsC< HGGHHG''B3#))/3RH r   