|~Vc@sdZddlZddgZdZdZdZdZd Ze d krd GHddl Z xRe d D]DZ e j \ZZerPne rue d dkrude GHququWdGHndS(sNumerical functions related to primes. Implementation based on the book Algorithm Design by Michael T. Goodrich and Roberto Tamassia, 2002. iNtgetprimetare_relatively_primecCs(x!|dkr#|||}}qW|S(sPReturns the greatest common divisor of p and q >>> gcd(48, 180) 12 i((tptq((s4/opt/awscli/lib/python2.7/site-packages/rsa/prime.pytgcdscCs|dkrtS|d}d}x"|d@sD|d7}|dL}q#Wxt|D]}tjj|dd}t|||}|dksR||dkrqRnxRt|dD]<}t|d|}|dkrtS||dkrPqqWtSqRWtS(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 iiii(tFalsetrangetrsatrandnumtrandinttpowtTrue(tntktdtrt_tatx((s4/opt/awscli/lib/python2.7/site-packages/rsa/prime.pytmiller_rabin_primality_testing(s(     cCs1|dkr|dkS|d@s$tSt|dS(sReturns 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 iiiii(iiii(RR(tnumber((s4/opt/awscli/lib/python2.7/site-packages/rsa/prime.pytis_prime]s     cCsE|dkstx,tr@tjj|}t|r|SqWdS(sReturns 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 iN(tAssertionErrorR RRtread_random_odd_intR(tnbitstinteger((s4/opt/awscli/lib/python2.7/site-packages/rsa/prime.pyR|s   cCst||}|dkS(sReturns 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(RtbR((s4/opt/awscli/lib/python2.7/site-packages/rsa/prime.pyRs t__main__s'Running doctests 1000x or until failureiidis%i timess Doctests done(t__doc__t rsa.randnumRt__all__RRRRRt__name__tdoctestRtcountttestmodtfailuresttests(((s4/opt/awscli/lib/python2.7/site-packages/rsa/prime.pyts"   5