def decode(self, W, method='euclid'): alpha = np.array([2], np.int) curr_deg = alpha alpha_list_t = [alpha[0]] for i in range(2 * self.t - 1): curr_deg = gf.prod(curr_deg, alpha, self.pm) alpha_list_t.append(curr_deg[0]) alpha_list_n = alpha_list_t.copy() for i in range(self.n - 2 * self.t): curr_deg = gf.prod(curr_deg, alpha, self.pm) alpha_list_n.append(curr_deg[0]) alpha_list_t = np.array(alpha_list_t) alpha_list_n = np.array(alpha_list_n) result = np.zeros(W.shape, np.int) if method == 'pgz': for row in range(W.shape[0]): deg_list = gf.polyval(W[row], alpha_list_t, self.pm) if np.all(deg_list == 0): result[row] = W[row] else: curr_dim = self.t while (curr_dim > 0): A = np.zeros((curr_dim, curr_dim), np.int) for i in range(curr_dim): A[i] = deg_list[i:curr_dim + i] b = deg_list[curr_dim:2 * curr_dim] value = gf.linsolve(A, b, self.pm) if value is not np.nan: value = np.concatenate((value, np.ones(1, np.int))) break curr_dim -= 1 if curr_dim == 0: result[row] = np.nan else: roots = gf.polyval(value, alpha_list_n, self.pm) roots = np.nonzero(np.logical_not(roots)) err_pos = (roots[0]) % self.n result[row] = W[row] result[row, err_pos] = np.logical_not( result[row, err_pos]).astype(np.int) if np.any( gf.polyval(result[row], alpha_list_t, self.pm) != 0): result[row, :] = np.nan elif method == 'euclid': alpha_list_t = alpha_list_t[::-1] for row in range(W.shape[0]): S = gf.polyval(W[row], alpha_list_t, self.pm) S = np.concatenate((S, np.ones(1, np.int))) z = np.array([1] + [0 for x in range(2 * self.t + 1)], np.int) r, A, Lambda = gf.euclid(z, S, self.pm, self.t) roots = gf.polyval(Lambda, alpha_list_n, self.pm) roots = np.nonzero(np.logical_not(roots)) err_pos = (roots[0]) % self.n result[row] = W[row] result[row, err_pos] = np.logical_not( result[row, err_pos]).astype(np.int) if np.any(gf.polyval(result[row], alpha_list_t, self.pm) != 0): result[row, :] = np.nan return result
def test_prod(): X = np.array([[1, 5], [14, 9], [13, 5]]) Y = np.array([[2, 3], [5, 4], [3, 11]]) pm = np.array([[12, 2], [13, 4], [ 0, 8], [14, 1], [ 0, 2], [ 0, 4], [ 0, 8], [15, 1], [ 0, 2], [ 0, 4], [ 0, 8], [ 0, 1], [ 0, 2], [ 0, 4], [ 0, 8]]) right_prod = np.array([[4, 8], [8, 4], [8, 8]]) assert_equal(right_prod, gf.prod(X, Y, pm))
def __init__(self, n, t): self.n = n self.t = t q = -1 n_copy = n + 1 while n_copy: n_copy = n_copy // 2 q += 1 if 2**q - 1 != n: raise ValueError('n is not 2^{q} - 1') with open('primpoly.txt', 'r') as file: primpoly_list = file.read().split(', ') for primpoly in primpoly_list: if int(primpoly) // 2**(q): # if degree == q break primpoly = int(primpoly) self.pm = gf.gen_pow_matrix(primpoly) alpha = np.array([2], np.int) curr_poly = alpha degrees_list = [alpha[0]] for i in range(2 * t - 1): curr_poly = gf.prod(curr_poly, alpha, self.pm) degrees_list.append(curr_poly[0]) self.g, self.R = gf.minpoly(np.array(degrees_list), self.pm) self.deg_g = self.g.shape[0] - 1
def test_linsolve_random(self): while True: pm = gf.gen_pow_matrix(92127) pm_len = len(pm) n = 50 A = np.array( [[pm[random.randint(0, pm_len - 1), 1] for _ in range(n)] for _ in range(n)]) b = np.array( [pm[random.randint(0, pm_len - 1), 1] for _ in range(n)]) solution = gf.linsolve(A, b, pm) if not (solution is np.nan): subst = [ gf.sum( gf.prod(A[i].reshape(A[i].size, 1), solution.reshape(solution.size, 1), pm))[0][0] for i in range(n) ] assert_array_equal(subst, b) break
def test_divide_all(self): pm = gf.gen_pow_matrix(357) for elem1 in pm[:, 1]: for elem2 in pm[:, 1]: a = gf.divide(np.array([[elem1]]), np.array([[elem2]]), pm) self.assertEqual(elem1, gf.prod(a, np.array([[elem2]]), pm))
def test_divide_inverse(self): pm = gf.gen_pow_matrix(88479) for elem in pm[:, 1]: inverse = gf.divide(np.array([[1]]), np.array([[elem]]), pm) self.assertEqual(1, gf.prod(inverse, np.array([[elem]]), pm))