ffk,dZddlmZddlmZddlmZdZdZddZ dZ dd l m Z ee dd Zdd Zd Zd ZdS)zHFunctions to create and test prime numbers. :undocumented: __package__ )Random)Integer) iter_rangeNct|tst|}|dvrtS|rtStd}t|dz }|t jj}t|}d}|r|dz}|dz }|t|D]}d}|||fvr4tj d|dz |}d|cxkr |dz ksnJ|||fv4t|||} | ||fvrVtd|D],} t| d|} | |krn| |kr tccS-tcStS)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 rrNrr ) min_inclusive max_inclusiverandfunc) isinstancerPROBABLY_PRIMEis_even COMPOSITErnewreadr random_rangepow) candidate iterationsrone minus_onemaibasezjs w/builddir/build/BUILD/imunify360-venv-2.3.5/opt/imunify360/venv/lib64/python3.11/site-packages/Crypto/Math/Primality.pymiller_rabin_testr"-s( i ) )'I&& L   !**C A &&I:<<$  A A ))++ a Q ))++ # #sI&&&'a"+a-%'''D---- A ------ sI&&& a # # i Aq!!  AAq)$$AI~~Cxx           ct|tst|}|dvrtS|s|rt Sd}|D]6}||| fvr tj||}|dkr t cS|dkrn7|dz}|dz }td}td}td}td} t|dz ddD]F} | |||z}||z}| || |z} | |z} | ||| r| |z } | dz} | |z} | | r| ||| z }| r||z }|dz}||z}| | | ||| r||z }|dz}||z}| || | H|dkrtSt 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 rc3>Kd} |V|dkr|dz }n|dz}| })Nr Trr )values r! alternatezlucas_test..alternatesB KKKqyy  FE  r#rr) rrrris_perfect_squarer jacobi_symbol size_in_bitsrsetmultiply_accumulateis_oddget_bit) rr(DjsKrU_iV_iU_tempV_temprs r! lucas_testr9ws i ) )'I&& L  i99;;Y[[ QB     "1i 0 0 77    88 E  A A 1A !**C !**C QZZF QZZF Ar2 & &!!  3# ) 3# ! ""3,,, ==?? i F1 ) 99Q<<  GGFOOO 6MCzz|| !y  AIC 9 C GGFOOO  # #FA . . .zz|| !y  AIC 9 CC GGFOOO GGFOOOO axx r#) sieve_basedc\|tjj}t|tst |}t |t vrtS t|j t n#t$r tcYSwxYwd}|  ttfd|dd}n#t$rd}YnwxYwt!|||tkrtSt#|tkrtStS)aTest 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)izr )i)ir )itr c|dkS)Nrr&)xbit_sizes r!z%test_probable_prime.. sh1or#rrr)rrrrrint _sieve_basermapfail_if_divisible_by ValueErrorrr,listfilter IndexErrorr"r9)rr mr_ranges mr_iterationsrGs @r!test_probable_primerTs_,:<<$ i ) )'I&&  9~~$$ I *K8888  'I%%''HV$=$=$=$=$-//00013346  M"*,,,/899) )) s$A99B  B ',C C#"C#c |dd}|dd}|dd}|r$td|z|td|dkrtd |tjj}t }|t kr@tj|| d z}||s0t||}|t k@|S) axGenerate 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_filtercdS)NTr&)rFs r!rHz)generate_probable_prime..<sr#Unknown parameters: zMissing exact_bits parameterzPrime number is not big enough.rVrr) poprNkeysrrrrrrandomrT)kwargsrVrrWresultrs r!generate_probable_primerasDL$//Jzz*d++H::nnn==L A/&++--?@@@7888C:;;;:<<$ F I  Nj,466689: |I&&  $Y99 I   r#c |dd}|dd}|r$td|z|tjj}t }|t krQt|dz |}|dzdz}||kr@t||}|t kQ|S) aGenerate 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). rVNrrYrr[r rI) r\rNr]rrrrrar,rT)r_rVrr`qrs r!generate_probable_safe_primerdRs L$//Jzz*d++H A/&++--?@@@:<<$ F I   #zA~ Q Q QEAI  ! ! # #z 1 1 $YBBB I   r#)N)__doc__CryptorCrypto.Math.NumbersrCrypto.Util.py3compatrrrr"r9Crypto.Util.numberr:_sieve_base_larger-rKrTrardr&r#r!rks> '''''',,,,,, GGGGT^^^B?>>>>>c#DSD)** 7777t777tr#