
^Yc           @   s   d  Z  d d l Z d d g Z d   Z d   Z d   Z d   Z d	   Z e	 d
 k r d GHd d l
 Z
 xR e d  D]D Z e
 j   \ Z Z e r Pn  e ru e d d k ru d e GHqu qu Wd GHn  d S(   s   Numerical functions related to primes.

Implementation based on the book Algorithm Design by Michael T. Goodrich and
Roberto Tamassia, 2002.
iNt   getprimet   are_relatively_primec         C   s(   x! | d k r# | |  | }  } q W|  S(   sP   Returns the greatest common divisor of p and q

    >>> gcd(48, 180)
    12
    i    (    (   t   pt   q(    (    s&   /tmp/pip-build-kpPAdC/rsa/rsa/prime.pyt   gcd   s    c         C   s  |  d k  r t  S|  d } d } x" | d @sD | d 7} | d L} q# Wx t |  D] } t j j |  d  d } t | | |   } | d k sR | |  d k r qR n  xR t | d  D]< } t | d |   } | d k r t  S| |  d k r Pq q Wt  SqR Wt S(   s.  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
    i   i   i    i   (   t   Falset   ranget   rsat   randnumt   randintt   powt   True(   t   nt   kt   dt   rt   _t   at   x(    (    s&   /tmp/pip-build-kpPAdC/rsa/rsa/prime.pyt   miller_rabin_primality_testing(   s(    

c         C   s1   |  d k  r |  d k S|  d @s$ t  St |  d  S(   s  Returns True if the number is prime, and False otherwise.

    >>> is_prime(2)
    True
    >>> is_prime(42)
    False
    >>> is_prime(41)
    True
    >>> [x for x in range(901, 1000) if is_prime(x)]
    [907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]
    i
   i   i   i   i   i   (   i   i   i   i   (   R   R   (   t   number(    (    s&   /tmp/pip-build-kpPAdC/rsa/rsa/prime.pyt   is_prime]   s
    

	c         C   sE   |  d k s t   x, t r@ t j j |   } t |  r | Sq Wd S(   s  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
    i   N(   t   AssertionErrorR   R   R   t   read_random_odd_intR   (   t   nbitst   integer(    (    s&   /tmp/pip-build-kpPAdC/rsa/rsa/prime.pyR    |   s
    	c         C   s   t  |  |  } | d k S(   s   Returns 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
    i   (   R   (   R   t   bR   (    (    s&   /tmp/pip-build-kpPAdC/rsa/rsa/prime.pyR      s    
t   __main__s'   Running doctests 1000x or until failurei  id   i    s   %i timess   Doctests done(   t   __doc__t   rsa.randnumR   t   __all__R   R   R   R    R   t   __name__t   doctestR   t   countt   testmodt   failurest   tests(    (    (    s&   /tmp/pip-build-kpPAdC/rsa/rsa/prime.pyt   <module>   s"   		5			