
    g                     8   d Z ddlZddlZddgZdededefdZd	edefd
ZdededefdZ	d	edefdZ
dedefdZdededefdZedk(  rQ ed       ddlZ ed      D ]1  Z ej$                         \  ZZer nedz  dk(  s$es' edez         3  ed       yy)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                 *    |dk7  r|| |z  }} |dk7  r| S )zPReturns the greatest common divisor of p and q

    >>> gcd(48, 180)
    12
    r    )r   r   s     >/var/www/openai/venv/lib/python3.12/site-packages/rsa/prime.pygcdr      s&     q&QUA q&H    numberc                 f    t         j                  j                  |       }|dk\  ry|dk\  ry|dk\  ryy)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                 N   | dk  ry| dz
  }d}|dz  s|dz  }|dz  }|dz  st        |      D ]u  }t        j                  j                  | dz
        dz   }t	        |||       }|dk(  s|| dz
  k(  rCt        |dz
        D ]!  }t	        |d|       }|dk(  r  y|| dz
  k(  s! t  y y)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 1u
 1XKKA&*1aL6Q!a%Zq1uAAq!AAvAEz  % ( r   c                 T    | dk  r| dv S | dz  syt        |       }t        | |dz         S )zReturns 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   sA     {%% QJ 	%V,A *&!a%88r   nbitsc                 l    | dkD  sJ 	 t         j                  j                  |       }t        |      r|S -)a  Returns 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                 $    t        | |      }|dk(  S )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     	Aq	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+GOO-53;!*u$%  
/ r   