o
    n~b{,                     @   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 r(t j}t|}d}| r>|dL }|d7 }| s2t|D ]Y}d}|||fv rltj	d| d |d}d|  krc| d ksfJ  J |||fv sLt
||| }	|	||fv ryqBtd|D ]}
t
|	d| }	|	|kr n|	|krt    S q~t  S qBt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   D/usr/local/lib/python3.10/dist-packages/Cryptodome/Math/Primality.pymiller_rabin_test-   sL   

 r   c                 C   s  t | ts	t| } | dv rtS |  s|  rtS dd }| D ]}| || fv r*q t|| }|dkr8t  S |dkr> nq | d }| d }td}td}td}td}	t|d ddD ]v}
|	| ||9 }|| ; }|		| |	|9 }	|	|9 }	|	
|| |	 r|	| 7 }	|	dL }	|	| ; }	||
r|	| ||	7 }| r|| 7 }|dL }|| ; }|	|	 |
|| | r|| 7 }|dL }|| ; }qa|	| |	|	 qa|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)Nr	   Tr   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   sh   









r&   )
sieve_based   c                    s   |du r	t  j}t| tst| } t| tv rtS zt| j	t W n t
y-   t Y S w d}|   ztt fdd|d d }W n tyP   d}Y nw t| ||dtkr\tS t| tkrdt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>  s    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   sB   

r=   c                  K   s   |  dd}|  dd}|  ddd }| rtd|   |du r&td|d	k r.td
|du r7t j}t}|tkrTtj||ddB }||sKq9t	||}|tks=|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   <  s    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u rt j}t}|tkrDt|d |d}|d d }| |kr:q!t	||d}|tks%|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)__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
::