U
    ¾śg{,  ć                   @   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rĘtj	d| d |d}d|  kr¾| d ksn t
qt||| }	|	||fkrąq~td|D ]2}
t|	d| }	|	|kr q~|	|krźt    S qźt  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-_q8s9isk/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rĘt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_baseéd   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r¼tS t| tkrĢt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r¢tj||ddB }||sqrt	||}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rt|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
::