def __init__(self, n, t): self.n = n self.t = t all_polynoms = numpy.loadtxt('primpoly.txt', dtype=int, delimiter=',') for poly in all_polynoms: poly = int(poly) cur_poly_pow = poly.bit_length() - 1 if 2**cur_poly_pow - 1 == n: self.polynom = poly break self.pm = gf.gen_pow_matrix(self.polynom) roots = list() alpha = 2 cur = 2 for i in range(2 * self.t): roots.append(cur) cur = gf.prod_zero_dim(cur, alpha, self.pm) self.R_for_pgz = numpy.array(roots) roots.reverse() self.R = numpy.array(roots) roots.reverse() self.g = gf.minpoly(numpy.array(roots), self.pm)[0] for i in range(2 * self.t, self.n): roots.append(cur) cur = gf.prod_zero_dim(cur, alpha, self.pm) self.all_roots = numpy.array(roots) self.m = self.g.shape[0] - 1 self.k = self.n - self.m
def test_linsolve_5(self): pm = gf.gen_pow_matrix(19) A1 = np.array([[3, 7], [12, 1]]) A2 = np.array([[3, 7], [12, 15]]) b = np.array([8, 13]) assert_array_equal(np.array([13, 14]), gf.linsolve(A1, b, pm)) self.assertTrue(gf.linsolve(A2, b, pm) is np.nan)
def test_polyval(): pm = gf.gen_pow_matrix(37) p = np.array([11, 29, 26, 31, 3]) x = np.array([19, 25, 31, 3, 14, 29]) right_answer = np.array([ 3, 12, 19, 26, 22, 1]) assert_equal(right_answer, gf.polyval(p, x, 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_polyprod(): pm = gf.gen_pow_matrix(37) p1 = np.array([11, 29, 26, 31, 3]) p2 = np.array([19, 25, 31, 3, 14, 29]) right_answer = np.array([28, 25, 29, 30, 27, 17, 14, 1, 27, 2]) assert_equal(right_answer, gf.polyprod(p1, p2, pm))
def test_gen_pow_matrix_2(self): assert_array_equal( [[31, 2], [1, 4], [13, 8], [2, 16], [26, 27], [14, 13], [10, 26], [3, 15], [23, 30], [27, 7], [17, 14], [15, 28], [6, 3], [11, 6], [8, 12], [4, 24], [21, 11], [24, 22], [29, 23], [28, 21], [20, 17], [18, 25], [19, 9], [16, 18], [22, 31], [7, 5], [5, 10], [12, 20], [30, 19], [9, 29], [25, 1]], gf.gen_pow_matrix(59))
def speed(): all_polynoms = numpy.loadtxt('primpoly.txt', dtype=int, delimiter=',') for n in [7, 15, 31, 63]: for poly in all_polynoms: poly = int(poly) cur_poly_pow = poly.bit_length() - 1 if 2**cur_poly_pow - 1 == n: polynom = poly break pm = gf.gen_pow_matrix(polynom) roots = list() alpha = 2 cur = 2 r = numpy.zeros((n - 1) >> 1) all_t_for_n = [t for t in range(1, ((n - 1) >> 1) + 1)] for t in all_t_for_n: roots = [] for j in range(2 * t): roots.append(cur) cur = gf.prod_zero_dim(cur, alpha, pm) cur = 2 g = gf.minpoly(numpy.array(roots), pm)[0] k = n - (g.shape[0] - 1) r[t - 1] = k / n plt.plot(all_t_for_n, r) plt.title('code length, n = ' + str(n)) plt.xlabel('number of correctioned errors, t') plt.ylabel('code speed, r') plt.grid(color='k', linestyle='-', linewidth=0.5) plt.show() return
def test_linsolve(self): pm = gf.gen_pow_matrix(130207) A = np.array([[pm[5, 1], pm[20, 1], pm[6, 1]], [0, 0, 0], [pm[-1, 1], pm[2, 1], pm[9, 1]]]) self.assertTrue( gf.linsolve(A, np.array([pm[1, 1], pm[5, 1], pm[-3, 1]]), pm) is np.nan)
def test_linsolve_4(self): pm = gf.gen_pow_matrix(87341) A = np.array([[pm[20, 1], pm[-20, 1], pm[10, 1], pm[8, 1]], [pm[198, 1], pm[30, 1], pm[89, 1], pm[-30, 1]], [pm[298, 1], pm[32, 1], pm[86, 1], pm[-24, 1]], [pm[5, 1], pm[-67, 1], pm[94, 1], pm[43, 1]]]) b = np.array([pm[67, 1], pm[-39, 1], pm[49, 1], pm[87, 1]]) assert_array_equal([35048, 24262, 65502, 26384], gf.linsolve(A, b, pm))
def test_linsolve_7_with_zero(self): pm = gf.gen_pow_matrix(87341) A = np.array([[0, pm[-20, 1], pm[10, 1], pm[8, 1]], [pm[198, 1], pm[30, 1], pm[89, 1], pm[-30, 1]], [pm[298, 1], pm[32, 1], pm[86, 1], pm[-24, 1]], [pm[5, 1], pm[-67, 1], pm[94, 1], pm[43, 1]]]) b = np.array([pm[67, 1], pm[-39, 1], pm[49, 1], pm[87, 1]]) assert_array_equal([21320, 18899, 5953, 57137], gf.linsolve(A, b, pm))
def test_linsolve_8_with_zero(self): pm = gf.gen_pow_matrix(87341) A = np.array([[1, pm[-20, 1], pm[10, 1], pm[8, 1]], [1, pm[-20, 1], pm[89, 1], pm[-30, 1]], [pm[298, 1], pm[32, 1], pm[86, 1], pm[-24, 1]], [pm[5, 1], pm[-67, 1], pm[94, 1], pm[43, 1]]]) b = np.array([pm[67, 1], pm[-39, 1], pm[49, 1], pm[87, 1]]) assert_array_equal([49980, 29479, 12587, 62413], gf.linsolve(A, b, pm))
def test_polydivmod(): pm = gf.gen_pow_matrix(37) p1 = np.array([13, 11, 29, 26, 31, 3]) p2 = np.array([19, 25, 31, 3, 14, 29]) right_answer = (np.array([21]), np.array([16, 23, 0, 23, 6])) result = gf.polydiv(p1, p2, pm) assert_equal(right_answer[0], result[0]) assert_equal(right_answer[1], result[1])
def __init__(self, n, t): self.n = n self.t = t prim = gf.find_prim(n + 1) self.pm = gf.gen_pow_matrix(prim) self.R = self.pm[0:2 * t, 1] self.g = gf.minpoly(self.R, self.pm)[0] self.m = gf.deg(self.g) self.k = self.n - self.m # word lenght
def test_linsolve_3(self): pm = gf.gen_pow_matrix(108851) A = np.array([[pm[5, 1], pm[20, 1], pm[6, 1]], [pm[8, 1], pm[1, 1], pm[6, 1]], [pm[-1, 1], pm[2, 1], pm[9, 1]]]) assert_array_equal([3009, 23136, 63822], gf.linsolve( A, np.array([pm[1, 1], pm[5, 1], pm[-3, 1]]), pm))
def test_minpoly(): pm = gf.gen_pow_matrix(37) x = np.array([19, 25, 31, 3, 14, 29]) right_answer = (np.array([1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1]), np.array([ 3, 5, 6, 8, 9, 10, 11, 12, 14, 15, 17, 18, 19, 20, 22, 25, 26, 29, 30, 31])) result = gf.minpoly(x, pm) assert_equal(right_answer[0], result[0]) assert_equal(right_answer[1], result[1])
def test_primpoly(): primpoly = 11 right_primpoly = np.array([[7, 2], [1, 4], [3, 3], [2, 6], [6, 7], [4, 5], [5, 1]]) assert_equal(right_primpoly, gf.gen_pow_matrix(primpoly))
def __init__(self, n, t, prim_choice=0, ref=None): """ Построение БЧХ кода с параметрами n, t prim_choice - задаёт, который из возможных неприводимых многочленов (отсортированных по двоичной записи) выбрать для поля GF. В качестве ref можно задать уже построенный объект BCH с теми же n и prim_choice, но меньшим t, чтобы облегчить построение данного BCH (если эти условия не выполняются, ref игнорируется) """ if '0' in bin(n)[2:]: raise ValueError("invalid n") self.n = n if 2 * t + 1 > n: raise ValueError("too big t") self.t = t self.prim_choice = prim_choice if ref is None or (ref.n, ref.prim_choice) != ( self.n, self.prim_choice) or self.t < ref.t: primpolys = sorted( list(filter(lambda x: n + 1 < x <= n * 2 + 1, primpoly_list))) primpoly = primpolys[self.prim_choice] self.pm = gf.gen_pow_matrix(primpoly) self.R = self.pm[:2 * self.t, 1] self.g, self.g_roots = gf.minpoly(self.R, self.pm) else: self.pm = ref.pm self.R = self.pm[:2 * self.t, 1] if self.t > ref.t: new_roots = set(self.R) - set(ref.g_roots) if new_roots: g_rem, new_g_roots = gf.minpoly(_np.array(list(new_roots)), pm=self.pm) self.g = gf.binpolyprod(ref.g, g_rem) self.g_roots = _np.array( sorted(list(set(ref.g_roots) | set(new_g_roots)))) else: self.g = ref.g self.g_roots = ref.g_roots else: # self.t == ref.t self.g = ref.g self.g_roots = ref.g_roots # поделим x^n - 1 на g(x) binome = _np.pad(_np.zeros(n - 1, dtype=int), 1, mode="constant", constant_values=1) _, r = gf.binpolydiv(binome, self.g) if not gf.isnull(r): raise Exception("programmer error") self.m = gf.polydeg(self.g) self.k = self.n - self.m self.dist_ = None
def test_euclid(): pm = gf.gen_pow_matrix(37) p1 = np.array([2, 14, 22, 23, 8, 17, 31, 11, 26, 3]) p2 = np.array([31, 23, 30, 31, 11, 9]) right_answer = (np.array([24, 8, 11]), np.array([1, 23, 14]), np.array([19, 14, 2, 21, 7, 12, 11])) max_deg = 2 result = gf.euclid(p1, p2, pm, max_deg=max_deg) assert_equal(right_answer[0], result[0]) assert_equal(right_answer[1], result[1]) assert_equal(right_answer[2], result[2])
def test_linsolve_6(self): pm = gf.gen_pow_matrix(87341) A = np.array([[pm[20, 1], pm[-20, 1], 0, pm[8, 1]], [pm[298, 1], pm[30, 1], 0, pm[-30, 1]], [pm[20, 1], pm[32, 1], 0, pm[-24, 1]], [pm[20, 1], pm[-67, 1], 0, pm[43, 1]]]) self.assertTrue( gf.linsolve( A, np.array([pm[67, 1], pm[-39, 1], pm[87, 1], pm[49, 1]]), pm) is np.nan)
def genpoly(n, t): # Very-very bad style: prim_poly_array = np.array([ 0, 0, 7, 11, 19, 37, 67, 131, 285, 529, 1033, 2053, 4179, 8219, 16427, 32771, 65581 ]) q = np.log2(n + 1).astype(int) if q < 2 or q > 16: raise ValueError("log2(n + 1) should be in [2, 16]") pm = gf.gen_pow_matrix(prim_poly_array[q]) bch_zeros = pm[:(2 * t), 1] g = gf.minpoly(bch_zeros, pm)[0] return (g, bch_zeros, pm)
def __init__(self, n, t): assert t <= int((n - 1) / 2) q = int(np.log2(n + 1)) assert (q >= 2) and (q <= 16) assert 2**q - 1 == n self.pm = gf.gen_pow_matrix(self.primpolies[q]) self.R = self.pm[:2 * t, 1] self.g, _ = gf.minpoly(self.R, self.pm) check_poly = np.zeros(n + 1, dtype=np.int64) check_poly[0] = 1 check_poly[-1] = 1 assert gf.polydivmod(check_poly, self.g, self.pm)[1] == 0 mask = (self.g == 0) | (self.g == 1) assert mask.all()
def __init__(self, n, t): self.n, self.t, self.q = n, t, int(np.log2(n + 1)) with open('./primpoly.txt', 'r') as file: primpolys = np.array( list(map(int, file.readline().strip().split(',')))) primpolys = primpolys[np.argwhere( np.log2(primpolys).astype(np.int) == self.q)].reshape(-1) self.pm = gf.gen_pow_matrix(primpolys[0]) self.R = self.pm[0:2 * t, 1] self.g, _ = gf.minpoly(self.R, self.pm) self.m, self.k = self.g.shape[0] - 1, self.n - (self.g.shape[0] - 1)
def __init__(self, n, t): file = open('primpoly.txt', 'r') self.pm = np.empty(0, int) for line in file: for val in line.split(','): val = int(val) if (val > n): self.pm = gf.gen_pow_matrix(val) break if self.pm.size: break self.R = self.pm[:t * 2, 1] self.g = gf.minpoly(self.R, self.pm)[0] file.close() return
def test_linsolve(): pm = gf.gen_pow_matrix(37) A = np.array([[30, 15, 13, 2, 17, 10, 27, 16, 7, 12], [ 1, 15, 30, 2, 17, 4, 19, 9, 1, 11], [29, 26, 7, 1, 27, 2, 2, 15, 15, 18], [29, 12, 5, 6, 26, 18, 23, 15, 24, 4], [10, 24, 22, 19, 3, 31, 18, 29, 24, 30], [15, 8, 7, 3, 8, 22, 13, 1, 16, 4], [19, 25, 31, 3, 14, 29, 27, 1, 29, 12], [ 8, 25, 20, 17, 5, 13, 31, 7, 23, 1], [14, 15, 7, 26, 14, 3, 31, 16, 5, 7], [19, 28, 9, 11, 30, 7, 25, 2, 3, 26]]) b = np.array([19, 2, 3, 11, 8, 27, 9, 4, 21, 5]) right_answer = np.array([ 6, 22, 14, 8, 20, 21, 20, 23, 30, 27]) assert_equal(right_answer, gf.linsolve(A, b, pm))
def __init__(self, n, t): self.n = n self.t = t q = P(n + 1).pw data = np.loadtxt("primpoly.txt", delimiter=', ', dtype=np.int) for i in data: if P(i).pw == q: self.pm = gen_pow_matrix(i) break zeros = [] for i in range(1, 2 * t + 1): zeros.append(self.pm[i][1]) self.R = np.asarray(zeros, dtype=int) p, roots = minpoly(self.R, self.pm) self.g = p self.k = self.n - self.g.size + 1
def __init__(self, n, t): self.n = n self.t = t q = int(np.log2(n + 1)) primpoly = open("primpoly.txt").readlines() primpoly = primpoly[0].replace(" ", "").split(',') primpoly = [int(poly) for poly in primpoly] primpoly_deg = np.log2(np.array(primpoly)).astype('int') prim_poly = primpoly[np.where(primpoly_deg >= q)[0][0]] self.pm = gf.gen_pow_matrix(prim_poly) self.R = [self.pm[i % self.pm.shape[0] - 1, 1] for i in range(1, 2 * self.t + 1)] self.R = np.array(self.R) self.g, _ = gf.minpoly(self.R, self.pm) self.m = self.g.shape[0] - 1
def __init__(self, n, t): dc = 2 * t + 1 q = 1 while (2 ** q < n + 1): q += 1 f = open('primpoly.txt') poly = 0 c = 0 for s in f: for i in s.split(', '): a = int(i) if (a > 2**q): poly = a break self.pm = gf.gen_pow_matrix(poly) self.g, self.R = gf.minpoly(self.pm.T[1][:dc-1], self.pm) self.k = n - self.g.shape[0] + 1 self.t = t
def __init__(self, n, t): q = n.bit_length() if (n+1).bit_length() <= q: raise ValueError("n != 2^q - 1") if t > (n-1)//2: raise ValueError("t > (n-1)/2") poly = open("primpoly.txt").readline() poly = poly.replace(" ", "").split(',') poly = list(map(int, poly)) i = np.array(list(map(int.bit_length,poly))) i = np.flatnonzero(i-1 == q) if i.size == 0: raise ValueError("No matching primitive polynomial found in primpoly.txt") poly = poly[random.choice(i)] # print("Primitive polynomial: " + str(poly) + " ~ " + np.binary_repr(poly)) self.pm = gf.gen_pow_matrix(poly) self.R = self.pm[:(2*t), 1] self.g = gf.minpoly(self.R, self.pm)[0]
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 __init__(self, n, t): primpoly = [ 7, 11, 19, 37, 67, 131, 285, 529, 1033, 2053, 4179, 8219, 16427, 32771, 65581 ] q = int(np.log2(n + 1)) for i in range(len(primpoly)): if primpoly[i] >= 2**q: prim_poly = primpoly[i] break self.pm = gf.gen_pow_matrix(prim_poly) arr = [2] for i in range(2, 2 * t + 1): arr = arr + [self.pm[i % self.pm.shape[0] - 1, 1]] self.R = np.array(arr) self.g = gf.minpoly(self.R, self.pm)[0] self.n = n self.t = t