U
    6³d¸  ã                   @   sä   d Z ddlZddlZddgZeeedœdd„Zeedœd	d
„Zeeedœdd„Z	eedœdd„Z
eedœdd„Zeeedœdd„Zedkràedƒ ddlZedƒD ]4Ze ¡ \ZZerº qØed dkr¢e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_prime)ÚpÚqÚreturnc                 C   s   |dkr|| |  } }q | S )zPReturns the greatest common divisor of p and q

    >>> gcd(48, 180)
    12
    r   © )r   r   r   r   ú-/tmp/pip-unpacked-wheel-dgov8y_p/rsa/prime.pyÚgcd   s    r	   )Únumberr   c                 C   s4   t j | ¡}|dkrdS |dkr$dS |dkr0d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ÚcommonZbit_size)r
   Zbitsizer   r   r   Úget_primality_testing_rounds'   s    r   )ÚnÚkr   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   r   T)Úranger   ÚrandnumÚrandintÚpow)r   r   ÚdÚrÚ_ÚaÚxr   r   r   Úmiller_rabin_primality_testingA   s(    
r   c                 C   s2   | dk r| dkS | d@ sdS t | ƒ}t| |d ƒS )z™Returns 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   r   r   r   Úis_primev   s    r!   )Únbitsr   c                 C   s*   | dkst ‚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)ÚAssertionErrorr   r   Zread_random_odd_intr!   )r"   Úintegerr   r   r   r      s    )r   Úbr   c                 C   s   t | |ƒ}|dkS )z«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
    r   )r	   )r   r%   r   r   r   r   r   ¬   s    

Ú__main__z'Running doctests 1000x or until failureiè  éd   z%i timeszDoctests done)Ú__doc__Z
rsa.commonr   Zrsa.randnumÚ__all__Úintr	   r   Úboolr   r!   r   r   Ú__name__ÚprintÚdoctestr   ÚcountÚtestmodZfailuresÚtestsr   r   r   r   Ú<module>   s&   5