U
    Mf{,                     @   s|   d Z ddlmZ ddlmZ ddlmZ dZdZdddZ	d	d
 Z
ddlmZ eedd ZdddZdd Zdd ZdS )zHFunctions to create and test prime numbers.

:undocumented: __package__
    )Random)Integer)
iter_range   Nc                 C   s,  t | tst| } | dkrtS |  r*tS td}t| d }|dkrPt j}t|}d}| rv|dL }|d7 }q\t|D ]}d}|||fkrtj	d| d |d}d|  kr| d ksn t
qt||| }	|	||fkrq~td|D ]2}
t|	d| }	|	|kr q~|	|krt    S qt  S q~tS )a:  Perform a Miller-Rabin primality test on an integer.

    The test is specified in Section C.3.1 of `FIPS PUB 186-4`__.

    :Parameters:
      candidate : integer
        The number to test for primality.
      iterations : integer
        The maximum number of iterations to perform before
        declaring a candidate a probable prime.
      randfunc : callable
        An RNG function where bases are taken from.

    :Returns:
      ``Primality.COMPOSITE`` or ``Primality.PROBABLY_PRIME``.

    .. __: http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf
    r            r   Nr   r   )Zmin_inclusiveZmax_inclusiverandfunc)
isinstancer   PROBABLY_PRIMEis_even	COMPOSITEr   newreadr   Zrandom_rangeAssertionErrorpow)	candidateZ
iterationsr
   ZoneZ	minus_onemaibasezj r   =/tmp/pip-unpacked-wheel-l_0d1exj/Cryptodome/Math/Primality.pymiller_rabin_test-   sD    


 

r   c                 C   s  t | tst| } | dkrtS |  s.|  r2tS dd }| D ]<}| || fkrTq@t|| }|dkrpt  S |dkr@ q~q@| d }| d }td}td}td}td}	t|d ddD ]}
|	| ||9 }|| ; }|		| |	|9 }	|	|9 }	|	
|| |	 r|	| 7 }	|	dL }	|	| ; }	||
r|	| ||	7 }| rX|| 7 }|dL }|| ; }|	|	 |
|| | r|| 7 }|dL }|| ; }q|	| |	|	 q|dkrtS tS )a_  Perform a Lucas primality test on an integer.

    The test is specified in Section C.3.3 of `FIPS PUB 186-4`__.

    :Parameters:
      candidate : integer
        The number to test for primality.

    :Returns:
      ``Primality.COMPOSITE`` or ``Primality.PROBABLY_PRIME``.

    .. __: http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf
    r   c                  s   s0   d} | V  | dkr| d7 } n| d8 } |  } qd S )Nr	   r   r   r   )valuer   r   r   	alternate   s    
zlucas_test.<locals>.alternater   r   )r   r   r   r   Zis_perfect_squarer   Zjacobi_symbolsize_in_bitsr   setZmultiply_accumulateZis_oddZget_bit)r   r   DZjsKrZU_iZV_iZU_tempZV_tempr   r   r   r   
lucas_testw   sf    












r%   )
sieve_based   c                    s   |dkrt  j}t| ts$t| } t| tkr4tS zt| j	t W n t
k
r\   t Y S X d}|   z"tt fdd|d d }W n tk
r   d}Y nX t| ||dtkrtS t| tkrtS tS )a  Test if a number is prime.

    A number is qualified as prime if it passes a certain
    number of Miller-Rabin tests (dependent on the size
    of the number, but such that probability of a false
    positive is less than 10^-30) and a single Lucas test.

    For instance, a 1024-bit candidate will need to pass
    4 Miller-Rabin tests.

    :Parameters:
      candidate : integer
        The number to test for primality.
      randfunc : callable
        The routine to draw random bytes from to select Miller-Rabin bases.
    :Returns:
      ``PROBABLE_PRIME`` if the number if prime with very high probability.
      ``COMPOSITE`` if the number is a composite.
      For efficiency reasons, ``COMPOSITE`` is also returned for small primes.
    N)
)      )i     )i     )i   
   )il     )i     )iz  r	   )i     )i  r   )it  r   c                    s    | d k S )Nr   r   xZbit_sizer   r   <lambda>      z%test_probable_prime.<locals>.<lambda>r   r   r
   )r   r   r   r   r   int_sieve_baser   mapZfail_if_divisible_by
ValueErrorr   r    listfilter
IndexErrorr   r%   )r   r
   Z	mr_rangesZmr_iterationsr   r2   r   test_probable_prime   s>    



r=   c                  K   s   |  dd}|  dd}|  ddd }| r<td|   |dkrLtd|d	k r\td
|dkrnt j}t}|tkrtj||ddB }||sqrt	||}qr|S )ax  Generate a random probable prime.

    The prime will not have any specific properties
    (e.g. it will not be a *strong* prime).

    Random numbers are evaluated for primality until one
    passes all tests, consisting of a certain number of
    Miller-Rabin tests with random bases followed by
    a single Lucas test.

    The number of Miller-Rabin iterations is chosen such that
    the probability that the output number is a non-prime is
    less than 1E-30 (roughly 2^{-100}).

    This approach is compliant to `FIPS PUB 186-4`__.

    :Keywords:
      exact_bits : integer
        The desired size in bits of the probable prime.
        It must be at least 160.
      randfunc : callable
        An RNG function where candidate primes are taken from.
      prime_filter : callable
        A function that takes an Integer as parameter and returns
        True if the number can be passed to further primality tests,
        False if it should be immediately discarded.

    :Return:
        A probable prime in the range 2^exact_bits > p > 2^(exact_bits-1).

    .. __: http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf
    
exact_bitsNr
   prime_filterc                 S   s   dS )NTr   r0   r   r   r   r3   <  r4   z)generate_probable_prime.<locals>.<lambda>Unknown parameters: zMissing exact_bits parameter   zPrime number is not big enough.r>   r
   r   )
popr9   keysr   r   r   r   r   randomr=   )kwargsr>   r
   r?   resultr   r   r   r   generate_probable_prime  s,    "
rH   c                  K   s   |  dd}|  dd}| r,td|   |dkr>t j}t}|tkrt|d |d}|d d }| |krtqBt	||d}qB|S )	a  Generate a random, probable safe prime.

    Note this operation is much slower than generating a simple prime.

    :Keywords:
      exact_bits : integer
        The desired size in bits of the probable safe prime.
      randfunc : callable
        An RNG function where candidate primes are taken from.

    :Return:
        A probable safe prime in the range
        2^exact_bits > p > 2^(exact_bits-1).
    r>   Nr
   r@   r   rB   r   r5   )
rC   r9   rD   r   r   r   r   rH   r    r=   )rF   r>   r
   rG   qr   r   r   r   generate_probable_safe_primeR  s    
rJ   )N)N)__doc__Z
Cryptodomer   ZCryptodome.Math.Numbersr   ZCryptodome.Util.py3compatr   r   r   r   r%   ZCryptodome.Util.numberr&   Z_sieve_base_larger!   r7   r=   rH   rJ   r   r   r   r   <module>   s   
Ja
::