
    ":h4                         d dl mZ d dlmZ d dl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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Zd ay)     )division)reduceNc                       e Zd ZdZy)Errorz)Base class for exceptions in this module.N)__name__
__module____qualname____doc__     e/var/www/html/turnos/venv/lib/python3.12/site-packages/ccxt/static_dependencies/ecdsa/numbertheory.pyr   r      s    3r   r   c                       e Zd Zy)SquareRootErrorNr   r   r	   r   r   r   r   r          r   r   c                       e Zd Zy)NegativeExponentErrorNr   r   r   r   r   r      r   r   r   c                 B    |dk  rt        d|z        t        | ||      S )z+Raise base to exponent, reducing by modulusr   z#Negative exponents (%d) not allowed)r   pow)baseexponentmoduluss      r   modular_expr       s0    !|#$I&.%/ 0 	0tXw''r   c                     |d   dk(  sJ t        |      dkD  sJ t        |       t        |      k\  r\| d   dk7  r7t        dt        |      dz         D ]  }| |    | d   ||    z  z
  |z  | | <    | dd } t        |       t        |      k\  r\| S )zReduce poly by polymod, integer arithmetic modulo p.

  Polynomials are represented as lists of coefficients
  of increasing powers of x.   r      )lenrange)polypolymodpis       r   polynomial_reduce_modr$   2   s     2;!w<!
d)s7|
#8q=1c'lQ./ C !HtBx'1"+'==BaRCAbz	 d)s7|
# Kr   c                     t        |       t        |      z   dz
  dgz  }t        t        |             D ]8  }t        t        |            D ]  }|||z      | |   ||   z  z   |z  |||z   <   ! : t        |||      S )zPolynomial multiplication modulo a polynomial over ints mod p.

  Polynomials are represented as lists of coefficients
  of increasing powers of x.r   r   )r   r   r$   )m1m2r!   r"   prodr#   js          r   polynomial_multiply_modr*   I   s     Gc"g!aS(D 3r7^ <s2w 	<AA;AA6!;DQK	<< !w22r   c                     ||k  sJ |dk(  rdgS | }|}|dz  dk(  r|}ndg}|dkD  r/|dz  }t        ||||      }|dz  dk(  rt        ||||      }|dkD  r/|S )zPolynomial exponentiation modulo a polynomial over ints mod p.

  Polynomials are represented as lists of coefficients
  of increasing powers of x.r   r   r   )r*   )r   r   r!   r"   Gkss          r   polynomial_exp_modr/   a   s     a<<1}s
AA1uzC
a%F#Aq'15q5A:'1gq9A	 a% Hr   c                    |dk\  sJ |dz  dk(  sJ | |z  } | dk(  ry| dk(  ry| d}}|dz  dk(  r|dz  |dz   }}|dz  dk(  r|dz  dk(  s|dz  dk(  s|dz  dk(  rd}nd}|dk(  r|S |dz  dk(  r|dz  dk(  r| }|t        ||z  |      z  S )	zJacobi symbol   r   r   r         r      )jacobi)ana1er.   s        r   r5   r5      s     6M6q5A::	AAAvAvqB
q&A+aQA q&A+1uzQUaZ1q5A:	Qw1uzb1fkBva"fb!!!r   c                 J   d| cxk  r|k  sJ  J d|k  sJ | dk(  ry|dk(  r| S t        | |      }|dk(  rt        d| |fz        |dz  dk(  rt        | |dz   dz  |      S |dz  d	k(  r]t        | |dz
  dz  |      }|dk(  rt        | |dz   dz  |      S ||dz
  k(  rd| z  t        d| z  |d	z
  dz  |      z  |z  S t        d
      t	        d|      D ]E  }t        ||z  d| z  z
  |      dk(  s| | df}t        d|dz   dz  ||      }|d   dk(  sJ |d   c S  t        d      )z)Modular square root of a, mod p, p prime.r   r   r   r   z%d has no square root modulo %dr4   r1   r2      zShouldn't get here.)r   r   zNo b found.)r5   r   r   RuntimeErrorr   r/   )r6   r"   jacdbfffs          r   square_root_mod_primerB      s    :A:::q5L5AvAv
A,C
by?!"A' ( 	( 	1uz1q1ulA..1uzAEa<+6q1q5Q,22A:EKAA!|Q??1DD0111a[ !a%!a%-#r)QB
A#FQUqL!Q?Ba5A::a5L }
%%r   c                     | dk  s|| k  r| |z  } | |}}d\  }}}}|dk7  r1t        ||      |fz   \  }}}|||z  z
  |||z  z
  ||f\  }}}}|dk7  r1|dk(  sJ |dkD  r|S ||z   S )zInverse of a mod m.r   )r   r   r   r   r   )divmod)	r6   mcr>   ucvcudvdqs	            r   inverse_modrL      s     	1uQE aqANBB
q&A,!%1aa"fb1r6k2r9BB q& 6M6	Av	Avr   c                     | r
|| z  | }} | r
|S )z1Greatest common divisor using Euclid's algorithm.r   r6   r?   s     r   gcd2rO      s    
1ua1 Hr   c                      t        |       dkD  rt        t        |       S t        | d   d      rt        t        | d         S | d   S )zLGreatest common divisor.

  Usage: gcd([ 2, 4, 6 ])
  or:    gcd(2, 4, 6)
  r   r   __iter__)r   r   rO   hasattrr6   s    r   gcdrT      C     1vzdAqtZ dAaD!!Q4Kr   c                 &    | |z  t        | |      z  S )z&Least common multiple of two integers.rT   rN   s     r   lcm2rX      s     Ec!Qir   c                      t        |       dkD  rt        t        |       S t        | d   d      rt        t        | d         S | d   S )zJLeast common multiple.

  Usage: lcm([ 3, 4, 5 ])
  or:    lcm(3, 4, 5)
  r   r   rQ   )r   r   rX   rR   rS   s    r   lcmrZ      rU   r   c                 ^   t        | t              sJ | dk  rg S g }d}t        D ]Z  }|| kD  r nSt        | |      \  }}|dk(  sd}|| k  r"|} t        | |      \  }}|dk7  rn|dz   }|| k  r"|j	                  ||f       \ | t        d   kD  rt        |       r|j	                  | df       |S t        d   }	 |dz   }t        | |      \  }}||k  rnD|dk(  r>d}|} || k  r"t        | |      \  }}|dk7  rn|} |dz   }|| k  r"|j	                  ||f       ^| dkD  r|j	                  | df       |S )z2Decompose n into a list of (prime,exponent) pairs.r   r   r   r   )
isinstanceintsmallprimesrD   appendis_prime)r7   resultr>   rK   rcounts         r   factorizationrd   	  s    a1u	F	A  &q5a|16Eq&a|16	 q& MM1e*%&" 	;r?A;MM1a&!* M' BAEa|1q56EAq&%a|16! %	 q& MM1e*-  1uq!f%Mr   c                     t        | t              sJ | dk  ryd}t        |       }|D ]/  }|d   }|dkD  r||d   |dz
  z  z  |d   dz
  z  }%||d   dz
  z  }1 |S )z'Return the Euler totient function of n.r1   r   r   )r\   r]   rd   )r7   ra   rA   r@   r9   s        r   phirf   A  s     a1uF	q	B )aDq5adq1uo-1:Fqtax(F) Mr   c                 *    t        t        |             S )zReturn Carmichael function of n.

  Carmichael(n) is the smallest integer x such that
  m**x = 1 mod n for all m relatively prime to n.
  )carmichael_of_factorizedrd   )r7   s    r   
carmichaelri   T  s     $M!$455r   c                     t        |       dk  ryt        | d         }t        dt        |             D ]  }t        |t        | |               } |S )zhReturn the Carmichael function of a number that is
  represented as a list of (prime,exponent) pairs.
  r   r   )r   carmichael_of_ppowerr   rZ   )f_listra   r#   s      r   rh   rh   ^  sW    
 6{Q!&),F1c&k" >V1&)<=> Mr   c                 L    | \  }}|dk(  r|dkD  rd|dz
  z  S |dz
  ||dz
  z  z  S )z=Carmichael function of the given power of the given prime.
  r   r   r   )ppr"   r6   s      r   rk   rk   m  s?     DAqAv!a%QU|Aq1u%%r   c                 l    |dk  ryt        | |      dk(  sJ | }d}|dk7  r|| z  |z  }|dz   }|dk7  r|S )z;Return the order of x in the multiplicative group mod m.
  r   r   rW   )xrE   zra   s       r   	order_modrr   x  sW     	Avq!9>>	AF
q&UaK! q& Mr   c                 d    	 t        | |      }|dk  r	 | S |}	 t        | |      \  }}|dkD  rn|} 0)z8Return the largest factor of a relatively prime to b.
  r   r   )rT   rD   )r6   r?   r>   rK   rb   s        r   largest_factor_relatively_primert     sU     1I6 H !Q<DAq1uA	  r   c                 .    t        | t        ||             S )zyReturn the order of x in the multiplicative group mod m',
  where m' is the largest factor of m relatively prime to x.
  )rr   rt   )rp   rE   s     r   kinda_order_modrv     s    
 Q71=>>r   c                    da | t        d   k  r
| t        v ryyt        | d      dk7  ryd}dt        t	        j
                  | d            z   }d	D ]  \  }}||k  r n|} d}| dz
  }|dz  dk(  r|dz   }|dz  }|dz  dk(  rt        |      D ]w  }t        |   }t        |||       }	|	dk7  s|	| dz
  k7  s(d}
|
|dz
  k  r7|	| dz
  k7  r/t        |	d|       }	|	dk(  r|dz   a  y|
dz   }
|
|dz
  k  r	|	| dz
  k7  r/|	| dz
  k7  sr|dz   a  y y)
a*  Return True if x is prime, False otherwise.

  We use the Miller-Rabin test, as given in Menezes et al. p. 138.
  This test is not exact: there are composite values n for which
  it returns True.

  In testing the odd numbers from 10000001 to 19999999,
  about 66 composites got past the first test,
  5 got past the second test, and none got past the third.
  Since factors of 2, 3, 5, 7, and 11 were detected during
  preliminary screening, the number of numbers tested by
  Miller-Rabin was (19999999 - 10000001)*(2/3)*(4/5)*(6/7)
  = 4.57 million.
  r   r   TFi	  r   (   r   ))d      )      )      )      )i,  	   )i^  r2   )i  r3   )i     )i&  r;   )i  r4   )iR  r1   )i  r   )miller_rabin_test_countr^   rT   r]   mathlogr   r   )r7   tn_bitsr-   ttr.   rb   r#   r6   yr)   s              r   r`   r`     so   &  KO
1 !Q& 	ATXXa^$$F 2 A:& 	
A	AAq5Q,EF q5Q, 1X N1a 6a1q5jAq1u*a!e1a(6./!e+ E q1u*a!e AEz*+a%' r   c                 Z    | dk  ry| dz   dz  }t        |      s|dz   }t        |      s|S )z9Return the smallest prime larger than the starting value.r   r   )r`   )starting_valuera   s     r   
next_primer     s>     q A%Fv! vMr   )r   r1   r;   r3                        %   )   +   /   5   ;   =   C   G   I   O   S   Y   a   e   g   k   m   q                                                                           i  i  i  i  i  i  i  i%  i3  i7  i9  i=  iK  iQ  i[  i]  ia  ig  io  iu  i{  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i	  i  i  i#  i-  i3  i9  i;  iA  iK  iQ  iW  iY  i_  ie  ii  ik  iw  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i)  i+  i5  i7  i;  i=  iG  iU  iY  i[  i_  im  iq  is  iw  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i  i	  i  i  i  i%  i'  i-  i?  iC  iE  iI  iO  iU  i]  ic  ii  i  i  i  i  i  i  i  i  i  i  i  i  ) 
__future__r   	functoolsr   r   	Exceptionr   r   r   r   r$   r*   r/   r5   rB   rL   rO   rT   rX   rZ   rd   rf   ri   rh   rk   rr   rt   rv   r`   r   r^   r   r   r   r   <module>r      s       	I 	
	e 		E 	($.30@"<&&R0 5p&6&("?JZE*  r   