
rUc           @   s   d  Z  d d g Z d d l Z d   Z d   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 r e d d k r d e GHq q 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.
t   getprimet   are_relatively_primeiNc         C   sD   x= | d k r? |  | k  r+ | |  }  } n  | |  | }  } q W|  S(   sP   Returns the greatest common divisor of p and q

    >>> gcd(48, 180)
    12
    i    (    (   t   pt   q(    (    s*   c:\Python27\lib\site-packages\rsa\prime.pyt   gcd   s
     c         C   s   |  d k r d Sd } x| |  d k r |  d @rg |  d | d d ?d @rS | } n  | |  |  }  } q | | d d ?d @r | } n  |  d L}  q W|  d k r d S| S(   s   Calculates the value of the Jacobi symbol (a/b) where both a and b are
    positive integers, and b is odd

    :returns: -1, 0 or 1
    i    i   i   i   (    (   t   at   bt   result(    (    s*   c:\Python27\lib\site-packages\rsa\prime.pyt   jacobi(   s    
 


 c         C   s=   t  |  |  | } t |  | d ?|  } | | k r9 t St S(   sU   Returns False if n is an Euler pseudo-prime with base x, and
    True otherwise.
    i   (   R   t   powt   Falset   True(   t   xt   nt   jt   f(    (    s*   c:\Python27\lib\site-packages\rsa\prime.pyt   jacobi_witness@   s
     c         C   sD   x= t  |  D]/ } t j j |  d  } t | |   r t Sq Wt S(   s   Calculates whether n is composite (which is always correct) or
    prime (which is incorrect with error probability 2**-k)

    Returns False if the number is composite, and True if it's
    probably prime.
    i   (   t   ranget   rsat   randnumt   randintR   R
   R   (   R   t   kt   _R   (    (    s*   c:\Python27\lib\site-packages\rsa\prime.pyt   randomized_primality_testingL   s
     c         C   s   t  |  d  S(   s|   Returns True if the number is prime, and False otherwise.

    >>> is_prime(42)
    False
    >>> is_prime(41)
    True
    i   (   R   (   t   number(    (    s*   c:\Python27\lib\site-packages\rsa\prime.pyt   is_primed   s    	c         C   s=   x6 t  r8 t j j |   } | d O} 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(   R   R   R   t   read_random_intR   (   t   nbitst   integer(    (    s*   c:\Python27\lib\site-packages\rsa\prime.pyR    o   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)
    1
    >>> are_relatively_prime(2, 4)
    0
    i   (   R   (   R   R   t   d(    (    s*   c:\Python27\lib\site-packages\rsa\prime.pyR      s    
t   __main__s'   Running doctests 1000x or until failurei  id   i    s   %i timess   Doctests done(   t   __doc__t   __all__t   rsa.randnumR   R   R   R   R   R   R    R   t   __name__t   doctestR   t   countt   testmodt   failurest   tests(    (    (    s*   c:\Python27\lib\site-packages\rsa\prime.pyt   <module>   s&   							