a
    !fk,                     @   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v rtS |  r*tS td}t| d }|du rPt j}t|}d}| rv|dL }|d7 }q\t|D ]}d}|||fv rtj	d| d |d}d|  kr| d ksn J qt
||| }	|	||fv 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pow)	candidateZ
iterationsr
   oneZ	minus_onemaibasezj r   V/var/www/html/python-backend/venv/lib/python3.9/site-packages/Crypto/Math/Primality.pymiller_rabin_test-   sD    


 

r   c                 C   s  t | tst| } | dv rtS |  s.|  r2tS dd }| D ]<}| || fv 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   DjsKrZU_iZV_iZU_tempZV_tempr   r   r   r   
lucas_testw   sf    












r&   )
sieve_based   c                    s   |du rt  j}t| ts$t| } t| tv r4tS zt| j	t W n t
yZ   t Y S 0 d}|   z"tt fdd|d d }W n ty   d}Y n0 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   r3   r   test_probable_prime   s>    



r>   c                  K   s   |  dd}|  dd}|  ddd }| r<td|   |du rLtd|d	k r\td
|du 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   r1   r   r   r   r4   <  r5   z)generate_probable_prime.<locals>.<lambda>Unknown parameters: zMissing exact_bits parameter   zPrime number is not big enough.r?   r
   r   )
popr:   keysr   r   r   r   r   randomr>   )kwargsr?   r
   r@   resultr   r   r   r   generate_probable_prime  s,    "
rI   c                  K   s   |  dd}|  dd}| r,td|   |du 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
   rA   r   rC   r   r6   )
rD   r:   rE   r   r   r   r   rI   r    r>   )rG   r?   r
   rH   qr   r   r   r   generate_probable_safe_primeR  s    
rK   )N)N)__doc__ZCryptor   ZCrypto.Math.Numbersr   ZCrypto.Util.py3compatr   r   r   r   r&   ZCrypto.Util.numberr'   Z_sieve_base_larger!   r8   r>   rI   rK   r   r   r   r   <module>   s   
Ja
::