Skip to content

Instantly share code, notes, and snippets.

@hiterm
Last active September 30, 2016 15:21
Show Gist options
  • Save hiterm/b694556002b24569b406e8e21201a382 to your computer and use it in GitHub Desktop.
Save hiterm/b694556002b24569b406e8e21201a382 to your computer and use it in GitHub Desktop.
︠ef71f22d-2e92-4007-b961-4e8fd2cc5023s︠
class MyRSCode:
def __init__(self, n, k, F, a, Fx, x):
self.n = n
self.k = k
self.field = F
self.field_generator = a
self.polynomial_ring = Fx
self.x = x
self.gen_poly = generate_gen_poly(self.n - k + 1, F, a, Fx, x)
def list_to_poly(self, ls):
return list_to_poly(ls, self.field, self.field_generator, self.polynomial_ring, self.x)
def poly_to_list(self, poly, degree):
length = log(self.field.order(), 2)
ls = reversed(poly.coefficients(sparse=False))
ret = []
for c in ls:
tmpls = c.polynomial().coefficients(sparse=False)
for _ in range(length - len(tmpls)):
tmpls.append(0)
tmpls.reverse()
ret.append(tmpls)
for _ in range(degree + 1 - len(ret)):
ret = [[0] * length] + ret
return ret
def encode_poly_to_poly(self, msg):
x = self.x
return x^(self.n - self.k) * msg + (x^(self.n - self.k) % self.gen_poly)
def encode_poly_to_list(self, msg):
return self.poly_to_list(self.encode_poly_to_poly(msg), self.n - 1)
def encode_list_to_poly(self, msg):
msg_poly = self.list_to_poly(msg)
return self.encode_poly_to_poly(msg_poly)
def encode_list_to_list(self, msg):
return self.poly_to_list(self.encode_list_to_poly(msg), self.n - 1)
class MyShorteningRSCode:
def __init__(self, C, shortening_length):
self.base_code = C
self.shortening_length = shortening_length
self.n = C.n - shortening_length
self.k = C.k - shortening_length
self.field = C.field
self.field_generator = C.field_generator
self.polynomial_ring = C.polynomial_ring
self.x = C.x
self.gen_poly = C.gen_poly
def encode_poly_to_list(self, msg):
shl = self.shortening_length
code_ext = self.base_code.encode_poly_to_list(msg)
return code_ext[shl:]
def encode_list_to_list(self, msg):
msg_poly = self.base_code.list_to_poly(msg)
return self.encode_poly_to_list(msg_poly)
def generate_gen_poly(d, F, a, Fx, x):
f = Fx(1)
for i in range(d - 1):
f *= (x - a^i)
return f
def list_to_poly(ls, F, a, Fx, x):
f = Fx(0)
for i, l1 in enumerate(reversed(ls)):
for j, elem in enumerate(reversed(l1)):
f += elem * a^j * x^i
return f
def mask011(ls, F):
length = log(F.order(), 2)
mask = \
[[0,1,1,0,0,0,0,1], [1,0,0,0,0,1,1,0],[0,0,0,1,1,0,0,0], [1,0,0,1,0,0,1,0],
[0,1,0,0,1,0,0,1], [0,0,1,0,0,1,0,0], [1,0,0,0,0,1,1,0], [0,0,0,1,1,0,0,0],
[0,1,1,0,0,0,0,1], [0,0,1,0,0,1,0,0], [1,0,0,1,0,0,1,0], [0,1,0,0,1,0,0,1],
[0,0,0,1,1,0,0,0], [0,1,1,0,0,0,0,1], [1,0,0,0,0,1,1,0], [0,0,0,1,0,0,0,1],
[1,0,0,0,0,1,1,0], [0,1,0,0,1,0,0,1], [0,0,1,0,0,0,1,0], [0,1,0,0,1,0,0,1],
[0,0,1,0,0,1,0,0], [1,0,0,1,0,0,1,0], [0,0,0,1,1,0,0,0], [0,0,1,0,0,1,0,0],
[1,0,0,1,0,0,1,0], [0,1,0,0,1,0,0,1]]
mask = [[F(elem) for elem in l1] for l1 in mask]
zipls = zip(ls, mask)
ret = [[elem[0] + elem[1] for elem in zip(l1[0], l1[1])]
for l1 in zipls]
return ret
︡460f4e48-f584-4cc7-9e0e-923394e8d0a3︡{"done":true}︡
︠f451eebc-9f66-4d7c-b158-77d4c5e3075fs︠
F.<a> = GF(2^8, modulus="primitive") # 有限体GF(2^8)の作成
F.modulus() # Fの原始多項式を表示
Fx = F['x'] # 多項式環を作成
︡a4dcf8f1-7449-413d-b9d7-64e4f886ae29︡{"stdout":"x^8 + x^4 + x^3 + x^2 + 1\n"}︡{"done":true}︡
︠4164d4ed-309e-4d18-aca8-31bdd3446480s︠
C0 = MyRSCode(31, 18, F, a, Fx, Fx(x)) # (31、18、14)符号の作成
C = MyShorteningRSCode(C0, 5) # 短縮符号の作成
C.n; C.k # (n, k)の表示
︡75330e1c-74a3-476d-b71e-901a3034779d︡{"stdout":"26\n13\n"}︡{"done":true}︡
︠6104cc24-cd54-4add-be22-4d7e6fe59307s︠
# メッセージ
msg = \
[[1, 0, 0, 0, 0, 0, 0, 0], #D1
[0, 1, 0, 0, 0, 0, 1, 0],
[1, 1, 1, 0, 1, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 0, 1],
[0, 1, 0, 1, 1, 1, 0, 1], #D5
[1, 0, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0], #D9
[1, 1, 1, 0, 1, 1, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 1],
[1, 1, 1, 0, 1, 1, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 1]] #D13
ls = C.encode_list_to_list(msg); ls # 符号化
ls = mask011(ls, F) # マスク
print "\n結果の表示"
for l1 in ls:
print l1
︡a5fd5b7f-7046-451b-b0d3-00742af1b6ee︡{"stdout":"[[1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 1, 0], [1, 1, 1, 0, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 0, 1], [0, 1, 0, 1, 1, 1, 0, 1], [1, 0, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 0, 1, 1, 0, 0], [0, 0, 0, 1, 0, 0, 0, 1], [1, 1, 1, 0, 1, 1, 0, 0], [0, 0, 0, 1, 0, 0, 0, 1], [1, 0, 0, 0, 1, 0, 0, 1], [0, 1, 0, 0, 1, 0, 0, 1], [1, 1, 1, 0, 0, 0, 1, 1], [0, 0, 0, 1, 0, 0, 0, 1], [1, 0, 1, 1, 0, 0, 0, 1], [0, 0, 0, 1, 0, 0, 0, 1], [0, 0, 1, 1, 0, 1, 0, 0], [0, 0, 0, 0, 1, 1, 0, 1], [0, 0, 1, 0, 1, 1, 1, 0], [0, 0, 1, 0, 1, 0, 1, 1], [0, 1, 0, 1, 0, 0, 1, 1], [1, 0, 0, 0, 0, 1, 0, 0], [0, 1, 1, 1, 1, 0, 0, 0]]\n"}︡{"stdout":"\n結果の表示\n"}︡{"stdout":"[1, 1, 1, 0, 0, 0, 0, 1]\n[1, 1, 0, 0, 0, 1, 0, 0]\n[1, 1, 1, 1, 0, 0, 0, 0]\n[1, 0, 1, 0, 1, 1, 1, 1]\n[0, 0, 0, 1, 0, 1, 0, 0]\n[1, 0, 0, 1, 1, 0, 1, 0]\n[1, 0, 0, 0, 0, 1, 1, 0]\n[0, 0, 0, 0, 0, 0, 0, 0]\n[0, 1, 1, 0, 0, 0, 0, 1]\n[1, 1, 0, 0, 1, 0, 0, 0]\n[1, 0, 0, 0, 0, 0, 1, 1]\n[1, 0, 1, 0, 0, 1, 0, 1]\n[0, 0, 0, 0, 1, 0, 0, 1]\n[1, 1, 1, 0, 1, 0, 0, 0]\n[1, 1, 0, 0, 1, 1, 1, 1]\n[1, 1, 1, 1, 0, 0, 1, 0]\n[1, 0, 0, 1, 0, 1, 1, 1]\n[1, 1, 1, 1, 1, 0, 0, 0]\n[0, 0, 1, 1, 0, 0, 1, 1]\n[0, 1, 1, 1, 1, 1, 0, 1]\n[0, 0, 1, 0, 1, 0, 0, 1]\n[1, 0, 1, 1, 1, 1, 0, 0]\n[0, 0, 1, 1, 0, 0, 1, 1]\n[0, 1, 1, 1, 0, 1, 1, 1]\n[0, 0, 0, 1, 0, 1, 1, 0]\n[0, 0, 1, 1, 0, 0, 0, 1]\n"}︡{"done":true}︡
︠200f50bf-1e69-41b7-af51-24064df3cd43︠
︡8e0e59e2-5923-46b0-b376-0483865ceac7︡
︠b1bb325c-5115-4ec4-bb49-641e3184c21bs︠
# 短縮しない場合
D = MyRSCode(26, 13, F, a, Fx, Fx(x))
D.gen_poly
msg = \
[[1, 0, 0, 0, 0, 0, 0, 0], #D1
[0, 1, 0, 0, 0, 0, 1, 0],
[1, 1, 1, 0, 1, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 0, 1],
[0, 1, 0, 1, 1, 1, 0, 1], #D5
[1, 0, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0], #D9
[1, 1, 1, 0, 1, 1, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 1],
[1, 1, 1, 0, 1, 1, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 1]] #D13
D.list_to_poly(msg)
D.encode_list_to_poly(msg)
ls = D.encode_list_to_list(msg); ls
ls = mask011(ls, F)
for elem in ls:
print elem
︡fc6d5d7f-9efd-4660-94fc-6eb5c24fb8d0︡{"stdout":"x^13 + (a^7 + a^3 + 1)*x^12 + (a^6 + a^3 + 1)*x^11 + (a^7 + a^6 + a^5 + a + 1)*x^10 + (a^4 + 1)*x^9 + (a^7 + a^5 + a^4 + 1)*x^8 + (a^4 + 1)*x^7 + (a^5 + a^4 + a^2)*x^6 + (a^3 + a^2 + 1)*x^5 + (a^5 + a^3 + a^2 + a)*x^4 + (a^5 + a^3 + a + 1)*x^3 + (a^6 + a^4 + a + 1)*x^2 + (a^7 + a^2)*x + a^6 + a^5 + a^4 + a^3\n"}︡{"stdout":"a^7*x^12 + (a^6 + a)*x^11 + (a^7 + a^6 + a^5 + a^3)*x^10 + (a^5 + a^4 + a^3 + a^2 + 1)*x^9 + (a^6 + a^4 + a^3 + a^2 + 1)*x^8 + (a^7 + a^5 + a^4 + a^3 + a^2 + a)*x^7 + (a^4 + a^3)*x^5 + (a^7 + a^6 + a^5 + a^3 + a^2)*x^3 + (a^4 + 1)*x^2 + (a^7 + a^6 + a^5 + a^3 + a^2)*x + a^4 + 1\n"}︡{"stdout":"a^7*x^25 + (a^6 + a)*x^24 + (a^7 + a^6 + a^5 + a^3)*x^23 + (a^5 + a^4 + a^3 + a^2 + 1)*x^22 + (a^6 + a^4 + a^3 + a^2 + 1)*x^21 + (a^7 + a^5 + a^4 + a^3 + a^2 + a)*x^20 + (a^4 + a^3)*x^18 + (a^7 + a^6 + a^5 + a^3 + a^2)*x^16 + (a^4 + 1)*x^15 + (a^7 + a^6 + a^5 + a^3 + a^2)*x^14 + (a^4 + 1)*x^13 + (a^7 + a^3 + 1)*x^12 + (a^6 + a^3 + 1)*x^11 + (a^7 + a^6 + a^5 + a + 1)*x^10 + (a^4 + 1)*x^9 + (a^7 + a^5 + a^4 + 1)*x^8 + (a^4 + 1)*x^7 + (a^5 + a^4 + a^2)*x^6 + (a^3 + a^2 + 1)*x^5 + (a^5 + a^3 + a^2 + a)*x^4 + (a^5 + a^3 + a + 1)*x^3 + (a^6 + a^4 + a + 1)*x^2 + (a^7 + a^2)*x + a^6 + a^5 + a^4 + a^3\n"}︡{"stdout":"[[1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 1, 0], [1, 1, 1, 0, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 0, 1], [0, 1, 0, 1, 1, 1, 0, 1], [1, 0, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 0, 1, 1, 0, 0], [0, 0, 0, 1, 0, 0, 0, 1], [1, 1, 1, 0, 1, 1, 0, 0], [0, 0, 0, 1, 0, 0, 0, 1], [1, 0, 0, 0, 1, 0, 0, 1], [0, 1, 0, 0, 1, 0, 0, 1], [1, 1, 1, 0, 0, 0, 1, 1], [0, 0, 0, 1, 0, 0, 0, 1], [1, 0, 1, 1, 0, 0, 0, 1], [0, 0, 0, 1, 0, 0, 0, 1], [0, 0, 1, 1, 0, 1, 0, 0], [0, 0, 0, 0, 1, 1, 0, 1], [0, 0, 1, 0, 1, 1, 1, 0], [0, 0, 1, 0, 1, 0, 1, 1], [0, 1, 0, 1, 0, 0, 1, 1], [1, 0, 0, 0, 0, 1, 0, 0], [0, 1, 1, 1, 1, 0, 0, 0]]\n"}︡{"stdout":"[1, 1, 1, 0, 0, 0, 0, 1]\n[1, 1, 0, 0, 0, 1, 0, 0]\n[1, 1, 1, 1, 0, 0, 0, 0]\n[1, 0, 1, 0, 1, 1, 1, 1]\n[0, 0, 0, 1, 0, 1, 0, 0]\n[1, 0, 0, 1, 1, 0, 1, 0]\n[1, 0, 0, 0, 0, 1, 1, 0]\n[0, 0, 0, 0, 0, 0, 0, 0]\n[0, 1, 1, 0, 0, 0, 0, 1]\n[1, 1, 0, 0, 1, 0, 0, 0]\n[1, 0, 0, 0, 0, 0, 1, 1]\n[1, 0, 1, 0, 0, 1, 0, 1]\n[0, 0, 0, 0, 1, 0, 0, 1]\n[1, 1, 1, 0, 1, 0, 0, 0]\n[1, 1, 0, 0, 1, 1, 1, 1]\n[1, 1, 1, 1, 0, 0, 1, 0]\n[1, 0, 0, 1, 0, 1, 1, 1]\n[1, 1, 1, 1, 1, 0, 0, 0]\n[0, 0, 1, 1, 0, 0, 1, 1]\n[0, 1, 1, 1, 1, 1, 0, 1]\n[0, 0, 1, 0, 1, 0, 0, 1]\n[1, 0, 1, 1, 1, 1, 0, 0]\n[0, 0, 1, 1, 0, 0, 1, 1]\n[0, 1, 1, 1, 0, 1, 1, 1]\n[0, 0, 0, 1, 0, 1, 1, 0]\n[0, 0, 1, 1, 0, 0, 0, 1]\n"}︡{"done":true}︡
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment