def blum(p, q, m): # your code here if not isinstance(m, int): print('Error(blum): Invalid value of m', end='') return '' if m <= 0: print('Error(blum): Invalid value of m', end='') return '' if not isinstance(p, int) or not isinstance(q, int): print('Error(blum): Invalid values of p,q', end='') return '' if mod.residue(p, 4) != 3 or mod.residue(q, 4) != 3: print('Error(blum): Invalid values of p,q', end='') return '' n = p * q p_file = open(primeFile, 'r') primes = p_file.read().split('\n') temp = n seed = int(primes[temp - 1]) while mod.gcd(seed, n) != 1: temp += 1 seed = int(primes[temp - 1]) bitStream = '' x = mod.residue(seed**2, n) for i in range(m): x = mod.residue(x**2, n) bitStream += str(mod.residue(x, 2)) return bitStream
def inverse(A, m): # your code here if not isinstance(m, int) or m <= 0: return 'Error(inverse): invalid mod' if not is_matrix(A): return 'Error(inverse): invalid input' if get_rowCount(A) == 0: return 'Error(inverse): invalid input' if not is_square(A): return 'Error(inverse): matrix is not invertible' else: if get_rowCount(A) == 2: if mod.gcd(det(A), m) != 1: return 'Error(inverse): matrix is not invertible' else: return 'Error(inverse): Unsupported matrix size' delta = mod.residue(det(A), m) m_i_delta = mod.mul_inv(delta, m) new = new_matrix(2, 2, 0) new[0][0] = A[1][1] new[0][1] = 0 - A[0][1] new[1][0] = 0 - A[1][0] new[1][1] = A[0][0] return matrix_mod(scalar_mul(m_i_delta, new), m)
def d_hill(ciphertext, key): # your code here if len(ciphertext) == 0: print('Error(d_hill): invalid ciphertext') return '' new_key = '' if len(key) > 4: new_key += key[:4] elif len(key) == 4: new_key += key else: new_key += key counter = 0 while len(new_key) < 4: new_key += key[counter] counter += 1 baseString = utilities_A4.get_lower() key_matrix = matrix.new_matrix(2, 2, 0) count = 0 for i in range(2): for j in range(2): key_matrix[i][j] = baseString.index(new_key[count].lower()) count += 1 if mod.gcd(matrix.det(key_matrix), 26) != 1: print('Error(d_hill): key is not invertible') return '' inverse_key_matrix = matrix.inverse(key_matrix, 26) plaintext = '' non_alpha = utilities_A4.get_nonalpha(ciphertext) blocks = utilities_A4.text_to_blocks( utilities_A4.remove_nonalpha(ciphertext), 2) for block in blocks: block_m = matrix.new_matrix(2, 1, 0) block_m[0][0] = baseString.index(block[0].lower()) block_m[1][0] = baseString.index(block[1].lower()) result_m = matrix.matrix_mod(matrix.mul(inverse_key_matrix, block_m), 26) plaintext += baseString[result_m[0][0]].lower() plaintext += baseString[result_m[1][0]].lower() plaintext = utilities_A4.insert_nonalpha(plaintext, non_alpha) while plaintext[-1] == 'q': plaintext = plaintext[:-1] return plaintext
def test_q1(): print("-------------------------------------------") print("Testing Q1: Modular Arithmetic Library") filename = 'q1_solution.txt' outFile = open(filename, 'w') print() outFile.write('1- Testing residue_set:\n') outFile.write('residue_set({}) = {}\n'.format(10, mod.residue_set(10))) outFile.write('residue_set({}) = {}\n'.format(1, mod.residue_set(1))) outFile.write('residue_set({}) = '.format(-5)) outFile.write('{}\n'.format(mod.residue_set(-5))) outFile.write('residue_set({}) = '.format([5])) outFile.write('{}\n'.format(mod.residue_set([5]))) outFile.write('\n') outFile.write('2- Testing residue:\n') outFile.write('residue({},{}) = {}\n'.format(17, 5, mod.residue(17, 5))) outFile.write('residue({},{}) = '.format(3.4, 5)) outFile.write('{}\n'.format(mod.residue(3.4, 5))) outFile.write('residue({},{}) = '.format(13, -5)) outFile.write('{}\n'.format(mod.residue(13, -5))) outFile.write('\n') outFile.write('3- Testing is_congruent:\n') outFile.write('is_congruent({},{},{})= {}\n'.format( 22, 33, 11, mod.is_congruent(22, 33, 11))) outFile.write('is_congruent({},{},{}) = {}\n'.format( 7, 9, 3, mod.is_congruent(7, 9, 3))) outFile.write('is_congruent({},{},{})= '.format(3.4, 5, 9)) outFile.write('{}\n'.format(mod.is_congruent(3.4, 5, 9))) outFile.write('is_congruent({},{},{}) = '.format(3, 5, -9)) outFile.write('{}\n'.format(mod.is_congruent(3, 5, -9))) outFile.write('\n') outFile.write('4- Testing add:\n') outFile.write('add({},{},{}) = {}\n'.format(17, 23, 7, mod.add(17, 23, 7))) outFile.write('add({},{},{}) = {}\n'.format(-17, 23, 7, mod.add(-17, 23, 7))) outFile.write('add({},{},{}) = {}\n'.format(17, -23, 7, mod.add(17, -23, 7))) outFile.write('add({},{},{}) = '.format(9, 17, 0)) outFile.write('{}\n'.format(mod.add(9, 17, 0))) outFile.write('add({},{},{}) = '.format([9], 17, 7)) outFile.write('{}\n'.format(mod.add([9], 17, 7))) outFile.write('add({},{},{}) = '.format(9, 17.1, 8)) outFile.write('{}\n'.format(mod.add(9, 17.1, 8))) outFile.write('\n') outFile.write('5- Testing sub:\n') outFile.write('sub({},{},{}) = {}\n'.format(17, 23, 7, mod.sub(17, 23, 7))) outFile.write('sub({},{},{}) = {}\n'.format(-17, 23, 7, mod.sub(-17, 23, 7))) outFile.write('sub({},{},{}) = {}\n'.format(17, -23, 7, mod.sub(17, -23, 7))) outFile.write('sub({},{},{}) = '.format(9, 17, 0)) outFile.write('{}\n'.format(mod.sub(9, 17, 0))) outFile.write('sub({},{},{}) = '.format([9], 17, 7)) outFile.write('{}\n'.format(mod.sub([9], 17, 7))) outFile.write('sub({},{},{}) = '.format(9, 17.1, 8)) outFile.write('{}\n'.format(mod.sub(9, 17.1, 8))) outFile.write('\n') outFile.write('6- Testing additive inverse:\n') outFile.write('add_inv({},{}) = {}\n'.format(3, 5, mod.add_inv(3, 5))) outFile.write('add_inv({},{}) = {}\n'.format(6, 1, mod.add_inv(6, 1))) outFile.write('add_inv({},{})= {}\n'.format(22, 10, mod.add_inv(22, 10))) outFile.write('add_inv({},{}) = '.format(6, -1)) outFile.write('{}\n'.format(mod.add_inv(6, -1))) outFile.write('add_inv({},{}) = '.format(6.2, 6)) outFile.write('{}\n'.format(mod.add_inv(6.2, 6))) a = 4 b = 2 m = 5 result = mod.sub(a, b, m) == mod.add(a, mod.add_inv(b, m), m) outFile.write( 'sub({0},{1},{2}) == add({0},add_inv({1},{2}),{2})? = {3}\n'.format( a, b, m, result)) outFile.write('\n') outFile.write('7- Testing Addition Table:\n') outFile.write('Addition Table for mode {} =\n'.format(5)) addTab = mod.add_table(5) for i in range(len(addTab)): outFile.write(str(addTab[i])) outFile.write('\n') outFile.write('Addition Table for mode {} =\n'.format(8)) addTab = mod.add_table(8) for i in range(len(addTab)): outFile.write(str(addTab[i])) outFile.write('\n') outFile.write('Addition Table for mode {} =\n'.format(0)) outFile.write(mod.add_table(0)) outFile.write('\n') outFile.write('\n') outFile.write('8- Testing Subtraction Table:\n') outFile.write('Subtraction Table for mode {} =\n'.format(5)) subTab = mod.sub_table(5) for i in range(len(subTab)): outFile.write(str(subTab[i])) outFile.write('\n') outFile.write('Subtraction Table for mode {} =\n'.format(8)) subTab = mod.sub_table(8) for i in range(len(subTab)): outFile.write(str(subTab[i])) outFile.write('\n') outFile.write('Subtraction Table for mode {} =\n'.format([5])) outFile.write(mod.sub_table([5])) outFile.write('\n') outFile.write('\n') outFile.write('9- Testing Addition Inverse Table:\n') outFile.write('Addition Inverse Table for mode {} =\n'.format(5)) addInvTab = mod.add_inv_table(5) outFile.write(str(addInvTab[0])) outFile.write('\n') outFile.write(str(addInvTab[1])) outFile.write('\n') outFile.write('Addition Inverse Table for mode {} =\n'.format(26)) addInvTab = mod.add_inv_table(26) outFile.write(str(addInvTab[0])) outFile.write('\n') outFile.write(str(addInvTab[1])) outFile.write('\n') outFile.write('Addition Inverse Table for mode {} =\n'.format(-2)) outFile.write(mod.add_inv_table(-2)) outFile.write('\n') outFile.write('\n') outFile.write('10- Testing mul:\n') outFile.write('mul({},{},{}) = {}\n'.format(3, 5, 5, mod.mul(3, 5, 5))) outFile.write('mul({},{},{}) = {}\n'.format(8, 3, 7, mod.mul(8, 3, 7))) outFile.write('mul({},{},{})= {}\n'.format(17, -3, 7, mod.mul(17, -3, 7))) outFile.write('mul({},{},{}) = '.format(9, 17, 0)) outFile.write('{}\n'.format(mod.mul(9, 17, 0))) outFile.write('mul({},{},{}) = '.format([9], 17, 7)) outFile.write('{}\n'.format(mod.mul([9], 17, 7))) outFile.write('mul({},{},{}) = '.format(9, 17.1, 8)) outFile.write('{}\n'.format(mod.mul(9, 17.1, 8))) outFile.write('\n') outFile.write('11- Testing Multiplication Table:\n') outFile.write('Multiplication Table for mode {} =\n'.format(4)) mulTab = mod.mul_table(4) for i in range(len(mulTab)): outFile.write(str(mulTab[i])) outFile.write('\n') outFile.write('Multiplication Table for mode {} =\n'.format(5)) mulTab = mod.mul_table(5) for i in range(len(mulTab)): outFile.write(str(mulTab[i])) outFile.write('\n') outFile.write('Multiplication Table for mode {} =\n'.format(-5)) outFile.write(mod.mul_table(-5)) outFile.write('\n') outFile.write('\n') outFile.write('12- Testing is_prime:\n') outFile.write('is_prime({}) = {}\n'.format(97, mod.is_prime(97))) outFile.write('is_prime({}) = {}\n'.format(479, mod.is_prime(479))) outFile.write('is_prime({})= {}\n'.format(1044, mod.is_prime(1044))) outFile.write('is_prime({}) = {}\n'.format(0, mod.is_prime(0))) outFile.write('is_prime({}) = {}\n'.format(-17, mod.is_prime(-17))) outFile.write('\n') outFile.write('13- Testing gcd:\n') outFile.write('gcd({},{}) = {}\n'.format(629, 357, mod.gcd(629, 357))) outFile.write('gcd({},{}) = {}\n'.format(440, 700, mod.gcd(440, 700))) outFile.write('gcd({},{}) = {}\n'.format(-30, 700, mod.gcd(-30, 700))) outFile.write('gcd({},{}) = {}\n'.format(540, -539, mod.gcd(540, -539))) outFile.write('gcd({},{}) = '.format(711, 0)) outFile.write(mod.gcd(711, 0)) outFile.write('\n') outFile.write('gcd({},{}) = '.format(0, 311)) outFile.write(mod.gcd(0, 311)) outFile.write('\n') outFile.write('gcd({},{}) = '.format([9], 27)) outFile.write(mod.gcd([9], 27)) outFile.write('\n') outFile.write('\n') outFile.write('14- Testing is_relatively_prime:\n') outFile.write('is_relatively_prime({},{}) = {}\n'.format( 4, 5, mod.is_relatively_prime(4, 5))) outFile.write('is_relatively_prime({},{})= {}\n'.format( 540, 539, mod.is_relatively_prime(540, 539))) outFile.write('is_relatively_prime({},{}) = {}\n'.format( 18, 26, mod.is_relatively_prime(18, 26))) outFile.write('is_relatively_prime({},{}) = {}\n'.format( 0, 26, mod.is_relatively_prime(0, 26))) outFile.write('is_relatively_prime({},{}) = '.format([1], 26)) outFile.write(mod.is_relatively_prime([1], 26)) outFile.write('\n') outFile.write('\n') outFile.write('15- Testing has_mul_inv:\n') outFile.write('has_mul_inv({},{}) = {}\n'.format( 4, 5, mod.has_mul_inv(4, 5))) outFile.write('has_mul_inv({},{}) = {}\n'.format(17, 26, mod.has_mul_inv(17, 26))) outFile.write('has_mul_inv({},{}) = {}\n'.format(18, 26, mod.has_mul_inv(18, 26))) outFile.write('has_mul_inv({},{}) = {}\n'.format( 0, 26, mod.has_mul_inv(0, 26))) outFile.write('has_mul_inv({},{}) = '.format([1], 26)) outFile.write(mod.has_mul_inv([1], 26)) outFile.write('\n') outFile.write('\n') outFile.write('16- Testing EEA:\n') outFile.write('eea({},{}) = {}\n'.format(700, 440, mod.eea(700, 440))) outFile.write('eea({},{}) = {}\n'.format(88, 35, mod.eea(88, 35))) outFile.write('eea({},{}) = {}\n'.format(35, 88, mod.eea(35, 88))) outFile.write('eea({},{}) = {}\n'.format(-88, 35, mod.eea(-88, 35))) outFile.write('eea({},{}) = {}\n'.format(88, -35, mod.eea(88, -35))) outFile.write('eea({},{}) = '.format(0, 777)) outFile.write(mod.eea(0, 777)) outFile.write('\n') outFile.write('\n') outFile.write('17- Testing mul_inv:\n') outFile.write('mul_inv({},{}) = {}\n'.format(23, 26, mod.mul_inv(23, 26))) outFile.write('mul_inv({},{}) = {}\n'.format(5, 6, mod.mul_inv(5, 6))) outFile.write('mul_inv({},{}) = {}\n'.format(24, 26, mod.mul_inv(24, 26))) outFile.write('mul_inv({},{}) = {}\n'.format(700, 440, mod.mul_inv(700, 440))) outFile.write('mul_inv({},{}) = {}\n'.format(0, 777, mod.mul_inv(700, 440))) outFile.write('mul_inv({},{}) = '.format(1, [99])) outFile.write(mod.mul_inv(1, [99])) outFile.write('\n') outFile.write('mul_inv({},{}) = '.format([1], 99)) outFile.write(mod.mul_inv([1], 99)) outFile.write('\n') outFile.write('\n') outFile.write('18- Testing Multiplicative Inverse Table:\n') outFile.write('Multiplicative Inverse Table for mode {} =\n'.format(5)) mulInvTab = mod.mul_inv_table(5) outFile.write(str(mulInvTab[0])) outFile.write('\n') outFile.write(str(mulInvTab[1])) outFile.write('\n') outFile.write('Multiplicative Inverse Table for mode {} =\n'.format(26)) mulInvTab = mod.mul_inv_table(26) outFile.write(str(mulInvTab[0])) outFile.write('\n') outFile.write(str(mulInvTab[1])) outFile.write('\n') outFile.write('Multiplicative Inverse Table for mode {} =\n'.format(-2)) outFile.write(mod.mul_inv_table(-2)) outFile.write('\n') outFile.write('\n') outFile.close() print('Comparing q1_solution with q1_sample:') print(utilities_A4.compare_files('q1_solution.txt', 'q1_sample.txt')) print() print("-------------------------------------------") return