Ejemplo n.º 1
0
 def encode(self, U):
     result_matr = np.zeros((U.shape[0], U.shape[1] + self.deg_g), np.int)
     for i in range(U.shape[0]):
         result_matr[i, :U.shape[1]] = U[i, :]
         _, d = gf.polydivmod(result_matr[i], self.g, self.pm)
         result_matr[i, -d.shape[0]:] = d
     return result_matr
Ejemplo n.º 2
0
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.polydivmod(p1, p2, pm)
    assert_equal(right_answer[0], result[0])
    assert_equal(right_answer[1], result[1])
Ejemplo n.º 3
0
 def _encode(self, u):
     assert self.n - (self.g.shape[0] - 1) == u.shape[0]
     x_power = np.zeros_like(self.g, dtype=np.int)
     x_power[0] = 1
     x_power_u = gf.polyprod(x_power, u, self.pm)
     _, mod = gf.polydivmod(x_power_u, self.g, self.pm)
     result = gf.polyadd(x_power_u, mod)
     result = np.concatenate(
         [np.zeros([self.n - result.shape[0]], dtype=np.int), result])
     return result
Ejemplo n.º 4
0
 def encode(self, U):
     n = self.pm.shape[0]
     m = self.g.shape[0] - 1
     x = np.zeros(m + 1, dtype=np.int64)
     x[0] = 1
     res = np.zeros((U.shape[0], U.shape[1] + m), dtype=np.int64)
     for i in range(U.shape[0]):
         u = U[i]
         code = gf.polyprod(x, u, self.pm)
         _, mod = gf.polydivmod(code, self.g, self.pm)
         code = gf.polyadd(code, mod)
         len_code = code.shape[0]
         res[i][-len_code:] = code
     return res
Ejemplo n.º 5
0
 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()
Ejemplo n.º 6
0
 def encode(self, U):
     mesg_cnt, k = U.shape
     
     x_m = np.zeros(self.m + 1).astype('int')
     x_m[0] = 1
     
     V = np.zeros((mesg_cnt, self.n)).astype('int')
     for i in range(mesg_cnt):
         u = U[i, :]
         
         x_m_prod_u = gf.polyprod(x_m, u, self.pm)
         div, mod = gf.polydivmod(x_m_prod_u, self.g, self.pm)
         
         v = gf.polyadd(x_m_prod_u, mod)
         ind = self.n - v.shape[0]
         V[i, ind:] = gf.polyadd(x_m_prod_u, mod)
     
     return V