def test_gcd_coprime(): assert gcd(9, 28) == 1 primes = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271 ] for i_max in range(0, len(primes), 2): n1 = product([primes[j] for j in range(0, i_max, 2)]) n2 = product([primes[j] for j in range(1, i_max - 1, 2)]) assert gcd(n1, n2) == 1
def hill_decrypt(chipertext, key): if type(chipertext) != str or type(key) != list: return chipertext = word_filter(chipertext) m = len(key) a = check(chipertext, key, m) #checking if paramteres are good if a == False: return #if lenght of chipertext is not divisible with m we need to add random letters if (len(chipertext) % m) != 0: while ((len(chipertext) % m) != 0): chipertext += alphabet[randint(0, 25)] #adding random letter #calculate inverse of matrix key(if it exists) d = int(round(det(key)) % 26) if (gcd(d, 26) != 1): #if determinant of matrix is not invertible in Z26 print("Key is not invertible matrix!") return inverse = matrixInverse_modn(key, 26) #inverse mod26 of key matrix num_vector = [alphabet.index(l) for l in chipertext] #numerical representation of chipertext plaintext = "" a = len(chipertext) for i in range(0, a - m + 1, m): x = num_vector[i:i + m] #calculate x*key_inverse (matrix multiplication) y = list(matmul(x, inverse) % 26) for n in y: plaintext += alphabet[n] return plaintext
def verify_e(self): coprimes = [] for i in range(3, self.n): if gcd(i, self.totient): coprimes.append(i) if self.e not in coprimes: raise Exception(f"{self.e} is not coprime with {self.n}")
def eulerTotient(n): # """ A simple method used for checking the Carmichael Totient, as the Euler Totient is a multiple of the Carmichael Totient. Returns the Euler Totient of an integer n. """ result = 1 for i in range(2, n): if (nt.gcd(i, n) == 1): result+=1 return result
def gcdTest(): " A method of checking GCD calculations." assert (nt.gcd(0, 0) == None) # Checks correct definitions for 0,0 case. assert (nt.gcd(0, 1) == 1) # Checks 0,1 case. assert (nt.gcd(1, 3) == 1) # Checks 1,(any other number) case. assert (nt.gcd(2, 6) == 2) # Checks prime, composite case. assert (nt.gcd(4, 9) == 1) # Checks coprime case. assert (nt.gcd(8, 36) == 4) # Checks 2 composite number case. assert (nt.gcd(4, 16) == 4 ) # Check number, square number case.
from number_theory import gcd from check_primes import is_prime q = 10**6 totient = lambda x: len(list(filter(lambda y: gcd(x, y) == 1, range(1, x)))) """ ans = 0 n_ans = 0 for n in range(2,q+1): totient_value = totient(n) if n/totient_value > ans: ans = n/totient_value n_ans = n """ # You can run the above if you want, might take a lot of time. # But think about this, what if we want to minimize totient(n)? # Then we must as many prime factors as possible! # So it's simply 2 x 3 x 5 x 7 x ... until it's still less then 1 million. p = 0 result = 1 while result <= q: if is_prime(p): if result * p <= q: result *= p else: break p += 1 print(result)
def test_other(self): self.assertEqual(gcd(1071, 1029), 21)
def test_zero(self): self.assertEqual(gcd(0, 5), 5) self.assertEqual(gcd(7, 0), 7)
def test_gcd_big_numbers(): assert gcd(2 * 3 * 5 * 7, 2 * 3 * 5 * 7) == 2 * 3 * 5 * 7 assert gcd(2 * 3 * 5 * 7 * 9 * 11, 2 * 3 * 5 * 7 * 9 * 11 * 13) == 2 * 3 * 5 * 7 * 9 * 11
def test_gcd_small_numbers(): assert gcd(1, 0) == 1 assert gcd(1, 1) == 1 assert gcd(0, 0) == 0 assert gcd(2 * 3, 2 * 3 * 5) == 6 assert gcd(2 * 3 * 5, 2 * 3) == 6
def choose_public_exponent(phi_n): for e in range(3, phi_n): if gcd(e, phi_n) == 1: return e