def test_generate_permutation(self): """Test generating permutation matrix.""" perm_matrix = [ 0b10000000, 0b00010000, 0b00000010, 0b01000000, 0b00000100, 0b00001000, 0b00100000, 0b00000001, ] self.assertEqual(matrix.permutation([]), matrix.Matrix()) perm = matrix.permutation([0, 3, 6, 1, 5, 4, 2, 7]) self.assertEqual(perm, matrix.Matrix(perm_matrix, 8)) perm = matrix.permutation([0, 3, 6, 1, 5, 4, 2, 7], by_rows=True) self.assertEqual(perm, matrix.Matrix(perm_matrix, 8).transpose())
def pubkey_gen(self): rm_generator = rm.generator(self.r, self.m) M = matrix.nonsingular(rm_generator.nrows) perm = [i for i in range(rm_generator.ncolumns)] shuffle(perm) P = matrix.permutation(perm) return M * rm_generator * P
def generate_keys(self): logger.info('generating pair of keys...') G = rm_code.generator(self.r, self.m) M = matrix.nonsingular(G.nrows) permutation = list(range(G.ncolumns)) shuffle(permutation) P = matrix.permutation(permutation) self.private_key = (M, G, P) self.public_key = (M * G * P) logger.debug(self.stringify_matrix('Public Key', self.public_key))
def find_permutation(matrix, m): a = matrix.T.solve(vector.from_support_supplement(2**m))[1] removing_num = a.support[0] if len(a.support) else 0 logger.debug(f'removing {removing_num}...') a_rows = [a] for i in range(m + 1): if i != removing_num: a_rows.append(a ^ vector.from_support(m + 1, [i])) a_rows = (mx.from_vectors(a_rows) * matrix)[1:] return mx.permutation([row.value for row in a_rows.T])
def find_permutation(self, generator): onev = vector.from_support_supplement(2**m) a = generator.T.solve(onev)[1] removing_num = 0 if len(a.support): removing_num = a.support[0] A_rows = [a] for i in range(0, m + 1): if i != removing_num: A_rows.append(a ^ vector.from_support(m + 1, [i])) ag = matrix.from_vectors(A_rows) * generator A_rows = ag[1:] return matrix.permutation([row.value for row in A_rows.T])