a
    db                     @   s   d Z ddlZddgZdd Zdd Zd	d
 Zdd Zdd Ze	dkre
d ddlZedD ]4Ze \ZZert qer\ed dkr\e
de  q\e
d 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_primec                 C   s   |dkr|| |  } }q | S )zPReturns the greatest common divisor of p and q

    >>> gcd(48, 180)
    12
    r    )pqr   r   Z/home/tom/ab/renpy-build/tmp/install.linux-x86_64/lib/python3.9/site-packages/rsa/prime.pygcd   s    r   c                 C   s   | dk rdS | d }d}|d@ s2|d7 }|dL }qt |D ]~}tj| d d }t||| }|dks:|| d krtq:t |d D ]0}t|d| }|dkr  dS || d kr q:q dS q: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      T)rangersarandnumrandintpow)nkdr_axr   r   r   miller_rabin_primality_testing(   s(    
r   c                 C   s&   | dk r| dv S | d@ sdS t | dS )a  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]
    
   )r	            r
   Fr   )r   )numberr   r   r   is_prime]   s
    	r   c                 C   s*   | dksJ t j| }t|r|S qd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   N)r   r   Zread_random_odd_intr   )nbitsintegerr   r   r   r   |   s    c                 C   s   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   br   r   r   r   r      s    

__main__z'Running doctests 1000x or until failurei  d   z%i timeszDoctests done)__doc__Zrsa.randnumr   __all__r   r   r   r   r   __name__printdoctestr   counttestmodZfailurestestsr   r   r   r   <module>   s"   5