示例#1
0
    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))
示例#3
0
    def decompose_inner_sets(self, generator):

        cw_num = 80 * 2**(self.m - 5)
        threshold = 100

        eps = (1 - 2**(-self.r)) * sqrt(1 - 2**(self.r - 1) / self.d)
        min_weight = self.d
        max_weight = floor(2 * self.d * eps)

        low_wt_cws = []

        while len(low_wt_cws) < cw_num:
            cols_sample = sample(range(generator.ncolumns), generator.nrows)
            new_generator = matrix.nonsingular(generator.nrows) * generator
            gaussed_g = new_generator.gaussian_elimination(cols_sample)

            for row in gaussed_g:
                wt = row.hamming_weight
                if row.support not in low_wt_cws and wt >= min_weight and wt <= max_weight:
                    low_wt_cws.append(row.support)

        for i in range(2**self.m):
            for j in range(2**self.m):
                self.pair_ctrs[i][j] = 0

        for codeword in low_wt_cws:
            for i in range(2**self.m):
                for j in range(i + 1, 2**self.m):
                    if i in codeword and j in codeword:
                        self.pair_ctrs[i][j] += 1

        G = nx.Graph()
        G.add_nodes_from(range(2**self.m))
        cliques = []

        iteration = 0
        max_iterations = 1000

        while not len(cliques) and iteration < max_iterations:

            for i in range(2**self.m):
                for j in range(i + 1, 2**self.m):
                    if (self.pair_ctrs[i][j] * (iteration + 1)) >= threshold:
                        G.add_edge(i, j)

            cliques = self.get_cliques(G)

            iteration += 1

        return cliques
示例#4
0
 def test_inverse(self):
     """Test evaluating of inverse matrix."""
     matr_non_max_rank1_diagonal = [
         0b10010,
         0b01011,
         0b00101,
         0b00000,
     ]
     matr_non_max_rank2_diagonal = [
         0b10000,
         0b01000,
         0b00100,
         0b00010,
         0b00001,
         0b00000,
         0b00000,
     ]
     self.assertTrue(
         (matrix.Matrix(self.matr_upper,
                        4).inverse * matrix.Matrix(self.matr_upper,
                                                   4)).is_identity())
     self.assertTrue(
         (matrix.Matrix(self.matr_max_rank,
                        4).inverse * matrix.Matrix(self.matr_max_rank,
                                                   4)).is_identity())
     self.assertEqual(
         matrix.Matrix(self.matr_non_max_rank1,
                       5).inverse * matrix.Matrix(self.matr_non_max_rank1,
                                                  5),
         matrix.Matrix(matr_non_max_rank1_diagonal, 5))
     self.assertEqual(
         matrix.Matrix(self.matr_non_max_rank2,
                       5).inverse * matrix.Matrix(self.matr_non_max_rank2,
                                                  5),
         matrix.Matrix(matr_non_max_rank2_diagonal, 5))
     self.assertEqual(matrix.Matrix().inverse, matrix.Matrix())
     self.assertTrue(matrix.Matrix([0] * 10, 20).inverse.is_identity())
     for _ in range(10):
         matr = matrix.nonsingular(20)
         self.assertTrue((matr * matr.inverse).is_identity())
示例#5
0
 def test_generate_nonsingular(self):
     """Test generating random non-singular matrix."""
     for _ in range(10):
         self.assertEqual(matrix.nonsingular(20).rank, 20)