+
    oi                     P   R t ^ RIt^ RItRR.tR R ltR R ltR R	 ltR
 R ltR R lt	R R lt
]R8X  dm   ]! R4       ^ RIt]! R4       FJ  t]P                   ! 4       w  tt]'       d    M-]^d,          ^ 8X  g   K1  ]'       g   K;  ]! R],          4       KL  	  ]! R4       R# R# )zNumerical functions related to primes.

Implementation based on the book Algorithm Design by Michael T. Goodrich and
Roberto Tamassia, 2002.
Ngetprimeare_relatively_primec                <    V ^8  d   QhR\         R\         R\         /# )   pqreturnint)formats   "l/Users/tonyclaw/.openclaw/workspace/scripts/youtube-playlists/venv/lib/python3.14/site-packages/rsa/prime.py__annotate__r      s!     	 	3 	3 	3 	    c                *    V^ 8w  d   YV,          rK  V # )zDReturns the greatest common divisor of p and q

>>> gcd(48, 180)
12
 )r   r   s   &&r   gcdr      s     q&UAHr   c                0    V ^8  d   QhR\         R\         /# r   numberr   r	   )r   s   "r   r   r   '   s        r   c                z    \         P                  P                  V 4      pVR8  d   ^# VR8  d   ^# VR8  d   ^# ^
# )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     jj!!&)G$$#~r   c                <    V ^8  d   QhR\         R\         R\        /# )r   nkr   r
   bool)r   s   "r   r   r   A   s!     2 2c 2c 2d 2r   c                   V ^8  d   R# V ^,
          p^ pV^,          '       g   V^,          pV^,          pK#  \        V4       F  p\        P                  P                  V ^,
          4      ^,           p\	        WRV 4      pV^8X  g   W`^,
          8X  d   KR  \        V^,
          4       F+  p\	        V^V 4      pV^8X  d     R# W`^,
          8X  g   K*   K  	   R# 	  R# )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
FT)ranger   randnumrandintpow)r   r   dr_axs   &&     r   miller_rabin_primality_testingr*   A   s    " 	1u 	
AA	A1uu	Q	a 1XKKA&*aL6Qa%Zq1uAAq!AAvEz  % ( r   c                0    V ^8  d   QhR\         R\        /# r   r   )r   s   "r   r   r   v   s     9 9S 9T 9r   c                v    V ^
8  d   V R9   # V ^,          '       g   R# \        V 4      p\        W^,           4      # )z}Returns True if the number is prime, and False otherwise.

>>> is_prime(2)
True
>>> is_prime(42)
False
>>> is_prime(41)
True
F>   r            )r   r*   )r   r   s   & r   is_primer0   v   sA     {%% QJJ 	%V,A *&a%88r   c                0    V ^8  d   QhR\         R\         /# )r   nbitsr   r	   )r   s   "r   r   r      s      C C r   c                ~    V ^8  g   Q h \         P                  P                  V 4      p\        V4      '       g   K3  V# )zReturns 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   r"   read_random_odd_intr0   )r2   integers   & r   r   r      s9      199
++11%8 GNr   c                <    V ^8  d   QhR\         R\         R\        /# )r   r(   br   r   )r   s   "r   r   r      s!      C C D r   c                "    \        W4      pV^8H  # )zReturns 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(   r7   r%   s   && r   r   r      s     	A	A6Mr   __main__z'Running doctests 1000x or until failurei  z%i timeszDoctests done)__doc__
rsa.commonr   rsa.randnum__all__r   r   r*   r0   r   r   __name__printdoctestr!   counttestmodfailurestestsr   r   r   <module>rE      s     -
.	42j948 z	
34t#OO-53;!*u$%  
/ r   