
    &g                     L   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    rY ed           ddlZ ed          D ]1Z ej                    \  ZZer nedz  dk    rer edez             2 ed           dS dS )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k    r|| |z  }} |dk    | S )zPReturns the greatest common divisor of p and q

    >>> gcd(48, 180)
    12
    r    )r   r   s     ;/var/www/api/venv/lib/python3.11/site-packages/rsa/prime.pygcdr      s*     q&&QUA q&&H    numberc                 t    t           j                            |           }|dk    rdS |dk    rdS |dk    rdS dS )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   '   sH     j!!&))G$q$q#~~q2r   nkc                 x   | dk     rdS | dz
  }d}|dz  s|dz  }|dz  }|dz  t          |          D ]}t          j                            | dz
            dz   }t	          |||           }|dk    s	|| dz
  k    rHt          |dz
            D ](}t	          |d|           }|dk    r  dS || dz
  k    r n) dS dS )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   " 	1uuu 	
AA	A1u 	Q	a 1u 
 1XX  KA&&*1aLL66Q!a%ZZq1u 
	 
	AAq!AAvvuuuAEzz 
 55 
 4r   c                 h    | dk     r| dv S | dz  sdS 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   sP     {{%% QJ u 	%V,,A *&!a%888r   nbitsc                 v    | dk    sJ 	 t           j                            |           }t          |          r|S 1)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      sG      19999+11%8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 2 2 2j9S 9T 9 9 9 94C C    8C C D     z	E
3444NNNt & &+GO--5 	E3;!E*u$%%%	E/ r   