
    ¿ig{,                         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dS )zHFunctions to create and test prime numbers.

:undocumented: __package__
    )Random)Integer)
iter_range   Nc                    t          | t                    st          |           } | dv rt          S |                                 rt          S t          d          }t          | dz
            }|t          j                    j        }t          |          }d}|                                r|dz  }|dz  }|                                t          |          D ]}d}|||fv r4t          j	        d| dz
  |          }d|cxk    r	| dz
  k    sn J |||fv 4t          |||           }	|	||fv rVt          d|          D ],}
t          |	d|           }	|	|k    r n|	|k    r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   Nr   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              U/var/www/html/afkarena/venv/lib/python3.11/site-packages/Cryptodome/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~~Cxx        	      c                    t          | t                    st          |           } | dv rt          S |                                 s|                                 rt
          S d } |            D ]6}| || fv r
t          j        ||           }|dk    r	t
          c S |dk    r n7| dz   }|                                dz
  }t          d          }t          d          }t          d          }t          d          }	t          |dz
  dd          D ]F}
|	                    |           ||z  }|| z  }|		                    |           |	|z  }	|	|z  }	|	
                    ||           |	                                r|	| z  }	|	dz  }	|	| z  }	|                    |
          r|	                    |           ||	z  }|                                r|| z  }|dz  }|| z  }|	                    |	           |
                    ||           |                                r|| z  }|dz  }|| z  }|	                    |           |	                    |	           H|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} 	 | V  | dk    r| dz  } n| dz  } |  } )Nr   Tr   r	    )values    r!   	alternatezlucas_test.<locals>.alternate   sB      	KKKqyy

FE	r#   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;;    Y[[  QB"1i007788E  	AA	1A !**C
!**CQZZFQZZFAr2&& ! ! 	

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

3#!""3,,,==?? 	 iF1)99Q<< 	GGFOOO6MCzz|| !y AIC9CGGFOOO##FA...zz|| !y AIC9CCGGFOOOGGFOOOO
axxr#   )
sieve_based   c                 \   |t          j                    j        }t          | t                    st	          |           } t          |           t          v rt          S 	 t          | j	        t                     n# t          $ r
 t          cY S w xY wd}|                                 	 t          t          fd|                    d         d         }n# t          $ r d}Y nw xY wt!          | ||          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                     | 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888   'I %%''HV$=$=$=$=$-/ / 0 0013346    M"*, , ,/89 9)	))s$   A9 9BB',C C#"C#c                     |                      dd          }|                      dd          }|                      dd           }| r$t          d|                                 z             |t          d          |dk     rt          d	          |t          j                    j        }t          }|t          k    r@t          j        ||
          dz  } ||          s0t          ||          }|t          k    @|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                     dS )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::nnn==L A/&++--?@@@7888C:;;;:<<$F
I

Nj,46 6 689:	|I&& 	$Y99 I

 r#   c                     |                      dd          }|                      dd          }| r$t          d|                                 z             |t          j                    j        }t          }|t          k    rQt          |dz
  |          }|dz  dz   }|                                |k    r@t          ||          }|t          k    Q|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 A/&++--?@@@:<<$F
I

#zA~QQQEAI	!!##z11$YBBB I

 r#   )N)__doc__
Cryptodomer   Cryptodome.Math.Numbersr   Cryptodome.Util.py3compatr   r   r   r"   r9   Cryptodome.Util.numberr:   _sieve_base_larger-   rK   rT   ra   rd   r&   r#   r!   <module>rk      s   > 
       + + + + + + 0 0 0 0 0 0	G G G GT^ ^ ^B C B B B B B c#DSD)**7 7 7 7t7 7 7t    r#   