HEX
Server: Apache/2.4.58 (Ubuntu)
System: Linux ip-172-31-18-78 6.17.0-1017-aws #17~24.04.1-Ubuntu SMP Tue May 26 21:09:53 UTC 2026 aarch64
User: ubuntu (1000)
PHP: 8.2.31
Disabled: NONE
Upload Files
File: //lib/python3/dist-packages/rsa/__pycache__/prime.cpython-312.pyc
�

%
�a���8�dZddlZddlZddgZdededefd�Zd	edefd
�Zdededefd
�Z	d	edefd�Z
dedefd�Zdededefd�Ze
dk(rQed�ddlZed�D]1Zej$�\ZZernedzdk(s�$es�'edez��3ed�yy)z�Numerical functions related to primes.

Implementation based on the book Algorithm Design by Michael T. Goodrich and
Roberto Tamassia, 2002.
�N�getprime�are_relatively_prime�p�q�returnc�*�|dk7r
|||z}}|dk7r�
|S)zPReturns the greatest common divisor of p and q

    >>> gcd(48, 180)
    12
    r�)rrs  �+/usr/lib/python3/dist-packages/rsa/prime.py�gcdrs&���q�&��Q��U�A���q�&��H��numberc�f�tjj|�}|dk\ry|dk\ry|dk\ryy)a�Returns minimum number of rounds for Miller-Rabing primality testing,
    based on number bitsize.

    According to NIST FIPS 186-4, Appendix C, Table C.3, minimum number of
    rounds of M-R testing, using an error probability of 2 ** (-100), for
    different p, q bitsizes are:
      * p, q bitsize: 512; rounds: 7
      * p, q bitsize: 1024; rounds: 4
      * p, q bitsize: 1536; rounds: 3
    See: http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.186-4.pdf
    i�i�i��
)�rsa�common�bit_size)r
�bitsizes  r
�get_primality_testing_roundsr's9���j�j�!�!�&�)�G��$����$����#�~��
r�n�kc�N�|dkry|dz
}d}|dzs|dz
}|dz}|dzs�t|�D]u}tjj|dz
�dz}t	|||�}|dk(s||dz
k(r�Ct|dz
�D]!}t	|d|�}|dk(ry||dz
k(s�!�tyy)a.Calculates whether n is composite (which is always correct) or prime
    (which theoretically is incorrect with error probability 4**-k), by
    applying Miller-Rabin primality testing.

    For reference and implementation example, see:
    https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test

    :param n: Integer to be tested for primality.
    :type n: int
    :param k: Number of rounds (witnesses) of Miller-Rabin testing.
    :type k: int
    :return: False if the number is composite, True if it's probably prime.
    :rtype: bool
    �F�rrT)�ranger�randnum�randint�pow)rr�d�r�_�a�xs       r
�miller_rabin_primality_testingr&As���"	�1�u��	
�A��A�	�A��1�u�	�Q���	�a����1�u�
�1�X����K�K����A��&��*����1�a�L����6�Q�!�a�%�Z���q�1�u��
	�A��A�q�!��A��A�v���A��E�z��
	��%�(rc�T�|dkr|dvS|dzsyt|�}t||dz�S)z�Returns True if the number is prime, and False otherwise.

    >>> is_prime(2)
    True
    >>> is_prime(42)
    False
    >>> is_prime(41)
    True
    r>rr�rrF)rr&)r
rs  r
�is_primer)vsA����{���%�%�
�Q�J��	%�V�,�A�*�&�!�a�%�8�8r�nbitsc�l�|dkDsJ�	tjj|�}t|�r|S�-)aReturns a prime number that can be stored in 'nbits' bits.

    >>> p = getprime(128)
    >>> is_prime(p-1)
    False
    >>> is_prime(p)
    True
    >>> is_prime(p+1)
    False

    >>> from rsa import common
    >>> common.bit_size(p) == 128
    True
    r)rr�read_random_odd_intr))r*�integers  r
rr�s;�� �1�9��9�
��+�+�1�1�%�8���G���N�rr$�bc�$�t||�}|dk(S)z�Returns True if a and b are relatively prime, and False if they
    are not.

    >>> are_relatively_prime(2, 3)
    True
    >>> are_relatively_prime(2, 4)
    False
    r)r)r$r.r!s   r
rr�s��	�A�q�	�A���6�Mr�__main__z'Running doctests 1000x or until failurei��dz%i timesz
Doctests done)�__doc__�
rsa.commonr�rsa.randnum�__all__�intrr�boolr&r)rr�__name__�print�doctestr�count�testmod�failures�testsr	rr
�<module>r?s������-�
.��	
�3�	
�3�	
�3�	
������42�c�2�c�2�d�2�j9�S�9�T�9�4�C��C��8�C��C��D���z��	�
3�4���t��&��+�G�O�O�-���5����3�;�!����*�u�$�%�
&�
�/��r