
    5ik,                     |    S r SSKJr  SSKJr  SSKJr  SrSrSS jr	S r
SS	KJr  \" \SS
 5      rSS jrS rS rg)zHFunctions to create and test prime numbers.

:undocumented: __package__
    )Random)Integer)
iter_range   Nc                    [        U [        5      (       d  [        U 5      n U S;   a  [        $ U R                  5       (       a  [        $ [        S5      n[        U S-
  5      nUc  [
        R                  " 5       R                  n[        U5      nSnUR                  5       (       a!  US-  nUS-  nUR                  5       (       a  M!  [        U5       H  nSnXU4;   a7  [        R                  " SU S-
  US9nSUs=::  a
  U S-
  ::  d   e   eXU4;   a  M7  [        XU 5      n	XU4;   a  MX  [        SU5       H'  n
[        U	SU 5      n	X:X  a    M|  X:X  d  M  [        s  s  $    [        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              c/var/www/html/BTCUSD/btcusdt_trading_app/venv/lib/python3.13/site-packages/Crypto/Math/Primality.pymiller_rabin_testr"   -   sr   ( i))I&	L 
!*C	A&I::<$$ 		A	A
))++	a	Q ))++ 
# I&&''a"+a-%'D -	A-.-.-	 I&& #i   Aq!AAq)$A~x   " / $4     c                 \   [        U [        5      (       d  [        U 5      n U S;   a  [        $ U R                  5       (       d  U R	                  5       (       a  [
        $ S nU" 5        H9  nXU* 4;   a  M  [        R                  " X 5      nUS:X  a  [
        s  $ US:X  d  M9    O   U S-   nUR                  5       S-
  n[        S5      n[        S5      n[        S5      n[        S5      n	[        US-
  SS5       GH  n
UR                  U5        X-  nX-  nU	R                  U5        X-  n	U	W-  n	U	R                  Xw5        U	R                  5       (       a  X-  n	U	S-  n	X-  n	UR                  U
5      (       a}  UR                  U5        Xi-  nUR                  5       (       a  X`-  nUS-  nX`-  nUR                  U	5        UR                  X5        UR                  5       (       a  Xp-  nUS-  nXp-  nM  UR                  U5        UR                  U	5        GM"     US:X  a  [        $ [
        $ )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   D   #    Sn  U v   U S:  a  U S-  n OU S-  n U * n M  7f)Nr   r   r	    )values    r!   	alternatelucas_test.<locals>.alternate   s7     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_testr:   w   s    i))I&	 L i99;; [QB""1078  	AA	1A !*C
!*CQZFQZFAr2& 	

3

3!""3,==??F199Q<<GGFOMCzz|| AICCGGFO##F.zz|| AICCGGFOGGFOC 'F axr#   )
sieve_based   c                   ^ Uc  [         R                  " 5       R                  n[        U [        5      (       d  [	        U 5      n [        U 5      [        ;   a  [        $  [        U R                  [        5        SnU R                  5       m [        [        U4S jU5      5      S   S   n[!        XUS9[        :X  a  [        $ [#        U 5      [        :X  a  [        $ [        $ ! [         a	    [        s $ f = f! [         a    Sn N[f = f)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                    > TU S   :  $ )Nr   r&   )xbit_sizes    r!   <lambda>%test_probable_prime.<locals>.<lambda>  s    h1or#   r   r   r   )r   r   r   r   r   int_sieve_baser   mapfail_if_divisible_by
ValueErrorr   r-   listfilter
IndexErrorr"   r:   )r   r   	mr_rangesmr_iterationsrH   s       @r!   test_probable_primerV      s   , ::<$$i))I&	 9~$I**K8'I %%'HV$=$-/ 0013346
 "*,/89)	)-    s$   C! C7 !C43C47DDc                     U R                  SS5      nU R                  SS5      nU R                  SS 5      nU (       a  [        SU R                  5       -   5      eUc  [        S5      eUS:  a  [        S	5      eUc  [        R                  " 5       R
                  n[        nU[        :X  a>  [        R                  " UUS
9S-  nU" U5      (       d  M1  [        XR5      nU[        :X  a  M>  W$ )a  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                     g)NTr&   )rG   s    r!   rI   )generate_probable_prime.<locals>.<lambda><  s    r#   Unknown parameters: zMissing exact_bits parameter   zPrime number is not big enough.rX   r   r   )
poprP   keysr   r   r   r   r   randomrV   )kwargsrX   r   rY   resultr   s         r!   generate_probable_primerd     s    D L$/Jzz*d+H::nn=L/&++-?@@788C:;;::<$$F
I
NNj,4689:	I&&$Y9 I
 r#   c                  x   U R                  SS5      nU R                  SS5      nU (       a  [        SU R                  5       -   5      eUc  [        R                  " 5       R
                  n[        nU[        :X  a@  [        US-
  US9nUS-  S-   nUR                  5       U:w  a  M5  [        XRS9nU[        :X  a  M@  W$ )	as  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).
rX   Nr   r\   r   r^   r	   rK   )
r_   rP   r`   r   r   r   r   rd   r-   rV   )rb   rX   r   rc   qr   s         r!   generate_probable_safe_primerg   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"   r:   Crypto.Util.numberr;   _sieve_base_larger.   rM   rV   rd   rg   r&   r#   r!   <module>rn      sW   >
  ' ,	GT^B ? #DS)*7t7tr#   