
    (ph                     d   S r SSKrSSKrSS/rS\S\S\4S jrS	\S\4S
 jrS\S\S\4S jr	S	\S\4S jr
S\S\4S jrS\S\S\4S jr\S:X  a`  \" S5        SSKr\" S5       H?  r\R$                  " 5       u  rr\(       a    O#\S-  S:X  d  M+  \(       d  M4  \" S\-  5        MA     \" S5        gg)zNumerical functions related to primes.

Implementation based on the book Algorithm Design by Michael T. Goodrich and
Roberto Tamassia, 2002.
    Ngetprimeare_relatively_primepqreturnc                 ,    US:w  a  XU-  pUS:w  a  M  U $ )zDReturns the greatest common divisor of p and q

>>> gcd(48, 180)
12
r    )r   r   s     </var/www/html/venv/lib/python3.13/site-packages/rsa/prime.pygcdr      s"     q&UA q&H    numberc                 l    [         R                  R                  U 5      nUS:  a  gUS:  a  gUS:  a  gg)a  Returns minimum number of rounds for Miller-Rabing primality testing,
based on number bitsize.

According to NIST FIPS 186-4, Appendix C, Table C.3, minimum number of
rounds of M-R testing, using an error probability of 2 ** (-100), for
different p, q bitsizes are:
  * p, q bitsize: 512; rounds: 7
  * p, q bitsize: 1024; rounds: 4
  * p, q bitsize: 1536; rounds: 3
See: http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf
i      i      i      
   )rsacommonbit_size)r   bitsizes     r
   get_primality_testing_roundsr   '   s9     jj!!&)G$$#~r   nkc                 r   U S:  a  gU S-
  nSnUS-  (       d  US-  nUS-  nUS-  (       d  M  [        U5       Hz  n[        R                  R                  U S-
  5      S-   n[	        XRU 5      nUS:X  d  X`S-
  :X  a  MD  [        US-
  5       H$  n[	        USU 5      nUS:X  a      gX`S-
  :X  d  M#    Mx       g   g)a  Calculates whether n is composite (which is always correct) or prime
(which theoretically is incorrect with error probability 4**-k), by
applying Miller-Rabin primality testing.

For reference and implementation example, see:
https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test

:param n: Integer to be tested for primality.
:type n: int
:param k: Number of rounds (witnesses) of Miller-Rabin testing.
:type k: int
:return: False if the number is composite, True if it's probably prime.
:rtype: bool
   F   r   r   T)ranger   randnumrandintpow)r   r   dr_axs          r
   miller_rabin_primality_testingr&   A   s    " 	1u 	
AA	A1u	Q	a 1uu
 1XKKA&*aL6Qa%Zq1uAAq!AAvEz  % ( r   c                 `    U S:  a  U S;   $ U S-  (       d  g[        U 5      n[        XS-   5      $ )z}Returns True if the number is prime, and False otherwise.

>>> is_prime(2)
True
>>> is_prime(42)
False
>>> is_prime(41)
True
r   >   r   r      r   r   F)r   r&   )r   r   s     r
   is_primer)   v   s?     {%% QJ 	%V,A *&a%88r   nbitsc                 z    U S:  d   e [         R                  R                  U 5      n[        U5      (       a  U$ M3  )zReturns a prime number that can be stored in 'nbits' bits.

>>> p = getprime(128)
>>> is_prime(p-1)
False
>>> is_prime(p)
True
>>> is_prime(p+1)
False

>>> from rsa import common
>>> common.bit_size(p) == 128
True
r   )r   r   read_random_odd_intr)   )r*   integers     r
   r   r      s>      199
++11%8 GN r   r$   bc                 "    [        X5      nUS:H  $ )zReturns True if a and b are relatively prime, and False if they
are not.

>>> are_relatively_prime(2, 3)
True
>>> are_relatively_prime(2, 4)
False
r   )r   )r$   r.   r!   s      r
   r   r      s     	A	A6Mr   __main__z'Running doctests 1000x or until failurei  d   z%i timeszDoctests done)__doc__
rsa.commonr   rsa.randnum__all__intr   r   boolr&   r)   r   r   __name__printdoctestr   counttestmodfailurestestsr	   r   r
   <module>r?      s    -
.	3 	3 	3 	  42c 2c 2d 2j9S 9T 94C C 8C C D  z	
34t#OO-53;!*u$%  
/ r   