
    ,hk,                     x    d Z ddlmZ ddlmZ ddlmZ dZdZddZ	d Z
dd	lmZ  eedd
       ZddZd Zd Zy)zHFunctions to create and test prime numbers.

:undocumented: __package__
    )Random)Integer)
iter_range   Nc                    t        | t              st        |       } | dv rt        S | j                         rt        S t        d      }t        | dz
        }|t        j                         j                  }t        |      }d}|j                         r|dz  }|dz  }|j                         rt        |      D ]  }d}|||fv r5t        j                  d| dz
  |      }d|cxk  r	| dz
  k  sJ  J |||fv r5t        |||       }	|	||fv rTt        d|      D ]%  }
t        |	d|       }	|	|k(  r w|	|k(  st        c c S  t        c S  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   r   r	   )min_inclusivemax_inclusiverandfunc)
isinstancer   PROBABLY_PRIMEis_even	COMPOSITEr   newreadr   random_rangepow)	candidate
iterationsr   one	minus_onemaibasezjs              W/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/Crypto/Math/Primality.pymiller_rabin_testr"   -   s{   ( i)I&	L 
!*C	A&I::<$$ 		A	A
))+	a	Q ))+ 
#  sI&&''a"+a-%'D -	A-.-.	 sI&& a#i   Aq! 	AAq)$AI~Cx  	 /4     c                    t        | t              st        |       } | dv rt        S | j                         s| j	                         rt
        S d } |       D ]4  }| || fv rt        j                  ||       }|dk(  rt
        c S |dk(  s4 n | dz   }|j                         dz
  }t        d      }t        d      }t        d      }t        d      }	t        |dz
  dd      D ]  }
|j                  |       ||z  }|| z  }|	j                  |       |	|z  }	|	z  }	|	j                  ||       |	j                         r|	| z  }	|	dz  }	|	| z  }	|j                  |
      rx|j                  |       ||	z  }|j                         r|| z  }|dz  }|| z  }|j                  |	       |j                  ||       |j                         r|| z  }|dz  }|| z  }|j                  |       |j                  |	        |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               3   @   K   d} 	 |  | dkD  r| dz  } n| dz  } |  } w)Nr   r   r	    )values    r!   	alternatezlucas_test.<locals>.alternate   s8     Kqy

FE s   r   r   )r   r   r   r   is_perfect_squarer   jacobi_symbolsize_in_bitsr   setmultiply_accumulateis_oddget_bit)r   r(   DjsKrU_iV_iU_tempV_tempr   s              r!   
lucas_testr9   w   s    i)I&	 L i99; [ QB""1i078 	AA	1A !*C
!*CQZFQZFAr2& ! 	

3#)

3#!""3,==?iF1)99Q<GGFO6MCzz|y AIC9CGGFO##FA.zz|y AIC9CGGFOGGFOC!F axr#   )
sieve_based   c                    |t        j                         j                  }t        | t              st	        |       } t        |       t        v rt        S 	 t        | j                  t               d}| j                         	 t        t        fd|            d   d   }t!        | ||      t        k(  rt        S t#        |       t        k(  rt        S t        S # t        $ r	 t        cY S w xY w# t        $ r d}Y [w xY w)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.
    )
)      )i     )i     )i   
   )il     )i     )iz  r   )i     )i  r
   )it  r	   c                     | d   k  S )Nr   r&   )xbit_sizes    r!   <lambda>z%test_probable_prime.<locals>.<lambda>  s    h1o r#   r   r   r   )r   r   r   r   r   int_sieve_baser   mapfail_if_divisible_by
ValueErrorr   r,   listfilter
IndexErrorr"   r9   )r   r   	mr_rangesmr_iterationsrG   s       @r!   test_probable_primerT      s    , ::<$$i)I&	 9~$I**K8'I %%'HV$=$-/ 0013346
 M"*,/89)	)-    s$   C C. C+*C+.C<;C<c                     | j                  dd      }| j                  dd      }| j                  dd       }| rt        d| j                         z         |t        d      |dk  rt        d	      |t        j                         j
                  }t        }|t        k(  r9t        j                  ||
      dz  } ||      s,t        ||      }|t        k(  r9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                      y)NTr&   )rF   s    r!   rH   z)generate_probable_prime.<locals>.<lambda><  s    r#   Unknown parameters: zMissing exact_bits parameter   zPrime number is not big enough.rV   r   r   )
poprN   keysr   r   r   r   r   randomrT   )kwargsrV   r   rW   resultr   s         r!   generate_probable_primera     s    D L$/Jzz*d+H::nn=L/&++-?@@788C:;;::<$$F
I
NNj,4689:	I&$Y9 I
 r#   c                  n   | j                  dd      }| j                  dd      }| rt        d| j                         z         |t        j                         j
                  }t        }|t        k(  rCt        |dz
  |      }|dz  dz   }|j                         |k7  r5t        ||      }|t        k(  rC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).
    rV   Nr   rY   r   r[   r	   rI   )
r\   rN   r]   r   r   r   r   ra   r,   rT   )r_   rV   r   r`   qr   s         r!   generate_probable_safe_primerd   R  s      L$/Jzz*d+H/&++-?@@::<$$F
I
#zA~QEAI	!!#z1$YB I
 r#   )N)__doc__Cryptor   Crypto.Math.Numbersr   Crypto.Util.py3compatr   r   r   r"   r9   Crypto.Util.numberr:   _sieve_base_larger-   rK   rT   ra   rd   r&   r#   r!   <module>rk      sW   >
  ' ,	GT^B ? #DS)*7t7tr#   