Ë
    (üàgk,  ã                   ó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              úS/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/Crypto/Math/Primality.pyÚmiller_rabin_testr"   -   s~  € ô( �i¤Ô)Ü˜IÓ&ˆ	à�LÑ ÜÐà×ÑÔÜÐä
�!‹*€CÜ˜	 A™Ó&€IàÐÜ—:‘:“<×$Ñ$ˆô 	�	Ó€AØ	€AØ
�)‰)Œ+Ø	ˆa‰ˆØ	ˆQ‰ˆð �)‰)�+ô ˜
Ó#ò ˆð ˆØ�s˜IÐ&Ñ&Ü×'Ñ'°aØ"+¨a¡-Ø%ô'ˆDð ˜Ô- 	¨A¡Ò-Ð.Ñ-Ð.Ð-ð	 �s˜IÐ&Ò&ô ��a˜Ó#ˆØ��iÐ Ñ Øô ˜A˜qÓ!ò 	ˆAÜ�A�q˜)Ó$ˆAØ�IŠ~ÙØ�C‹xÜ Ô ð	ô Òð/ô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Ø�qŠyØ˜‘
‘à˜‘
�Ø�FˆEð ù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Ô;ÜÐòñ ‹[ò ˆØ˜˜Q˜B˜ÑØÜ×"Ñ" 1 iÓ0ˆØ�Š7ÜÒØ�‹8Ùðð 	�A‰€Aà	�‰Ó˜1Ñ€Aô �!‹*€CÜ
�!‹*€CÜ�Q‹Z€FÜ�Q‹Z€Fä˜˜A™˜r 2Ó&ó !ˆð 	�
‰
�3ŒØ�#‰ˆØ�)Ñˆà�
‰
�3ŒØ�#‰ˆØ�!‰ˆØ×"Ñ" 3¨Ô,Ø�=‰=Œ?Ø�iÑˆFØ�1‰ˆØ�)Ñˆà�9‰9�QŒ<à�G‰G�FŒOØ�6‰MˆCØ�z‰zŒ|Ø�yÑ �Ø�A‰IˆCØ�9ÑˆCà�G‰G�FŒOØ×#Ñ# F¨AÔ.Ø�z‰zŒ|Ø�yÑ �Ø�A‰IˆCØ�9Ñ‰Cà�G‰G�FŒOØ�G‰G�FŽOðC!ðF ˆa‚xÜÐÜÐr#   )Ú
sieve_baseéd   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Ó$=Ø$-ó/ó 0Ø01ñ3Ø34ñ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Ø�z‰z˜* dÓ+€HØ—:‘:˜n©nÓ=€LÙÜÐ/°&·+±+³-Ñ?Ó@Ð@àÐÜÐ7Ó8Ð8Ø�CÒÜÐ:Ó;Ð;àÐÜ—:‘:“<×$Ñ$ˆä€FØ
”IÒ
Ü—N‘N¨jØ,4ô6Ø89ñ:ˆ	á˜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Ø�z‰z˜* dÓ+€HÙÜÐ/°&·+±+³-Ñ?Ó@Ð@àÐÜ—:‘:“<×$Ñ$ˆä€FØ
”IÒ
Ü#¨z¸A©~ÈÔQˆØ˜‘E˜A‘Iˆ	Ø×!Ñ!Ó# 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 ?ñ Ð# D SÐ)Ó*€ó7òt7ótr#   