/usr/lib/python2.6/site-packages/rsa
Edit: /usr/lib/python2.6/site-packages/rsa/prime.pyo (3618B)
|~Vc @ s d Z d d k Z d d g Z d Z d Z d Z d Z d Z e d
j ot d GHd d k
Z
xW e d D]I Z e
i
\ Z Z e o Pn e o e d
d j o
d e GHqv qv Wd GHn d S( s Numerical functions related to primes.
Implementation based on the book Algorithm Design by Michael T. Goodrich and
Roberto Tamassia, 2002.
iNt getprimet are_relatively_primec C s* x# | d j o | | | } } q W| S( sP Returns the greatest common divisor of p and q
>>> gcd(48, 180)
12
i ( ( t pt q( ( s- /usr/lib/python2.6/site-packages/rsa/prime.pyt gcd s
c C s | d j o t S| d } d } x$ | d @p | d 7} | d L} q% Wx t | D] } t i i | d d } t | | | } | d j p | | d j o qV n xV t | d D]@ } t | d | } | d j o t S| | d j o Pq q Wt SqV Wt S( s. 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
i i i i ( t Falset ranget rsat randnumt randintt powt True( t nt kt dt rt _t at x( ( s- /usr/lib/python2.6/site-packages/rsa/prime.pyt miller_rabin_primality_testing( s.
c C s5 | d j o | d j S| d @p t St | d S( s Returns True if the number is prime, and False otherwise.
>>> is_prime(2)
True
>>> is_prime(42)
False
>>> is_prime(41)
True
>>> [x for x in range(901, 1000) if is_prime(x)]
[907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]
i
i i i i i ( i i i i ( R R ( t number( ( s- /usr/lib/python2.6/site-packages/rsa/prime.pyt is_prime] s
c C s7 x0 t o( t i i | } t | o | Sq Wd S( s Returns 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
N( R R R t read_random_odd_intR ( t nbitst integer( ( s- /usr/lib/python2.6/site-packages/rsa/prime.pyR | s
c C s t | | } | d j S( s 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
i ( R ( R t bR ( ( s- /usr/lib/python2.6/site-packages/rsa/prime.pyR s
t __main__s' Running doctests 1000x or until failurei id i s %i timess
Doctests done( t __doc__t rsa.randnumR t __all__R R R R R t __name__t doctestR t countt testmodt failurest tests( ( ( s- /usr/lib/python2.6/site-packages/rsa/prime.pyt
s$ 5