1、基础知识
1.1 有限域
有限域_百度百科
伽罗华域(Galois Field)上的四则运算_模2的伽罗华域乘法-CSDN博客
1.2 RS(255,251)
里德-所罗门码(一种前向错误更正的信道编码)_百度百科
本原多项式: p(x) = x^8 + x^4 + x^3+ x^2 + 1
生成多项式: g(x) = x^4 + 15* x^3 + 54*x^2 + 120*x+64
根据对1.1的理解,GF(256) 为GF(2^8)
RS_m = 8
RS_n = 255
RS_k = 251
RS_t = (RS_n - RS_k)/2 = 2
根据四则运算,该域中的每个元素的数值为:
a(0) = x^0 =>0b
a(1) = x^1 =>1b
a(2) = x^2 =>10b
a(3) = x^3 =>11b
...
a(8) = x^8 = x^4 + x^3+ x^2 + 1 =>11101b(由 P(x)推导)
a(9) = x^9 = x^8* x= (x^4 + x^3+ x^2 + 1)*x = x^5+x^4+x^3+x =>111010b
a(10) = x^10 = x^8*x^2 = (x^4 + x^3+ x^2 + 1)*x^2 =x^6+x^5+x^4+x^2 =>1110100b
a(11) = x^11 = x^8*x^3 = (x^4 + x^3+ x^2 + 1)*x^3 = x^7+x^6+x^5+ x^3 => 11101000b
a(12) = x^12 = x^8*x^4 = (x^4 + x^3+ x^2 + 1)*x^4 = x^8 + x^7+ x^6+ x^4 = (x^4 + x^3+ x^2 + 1)+ x^7+ x^6+ x^4 (modp(x))= x^7+ x^6 + x^3+ x^2 + 1 => 11001101b
...
只要a(n)中的n大于8或者推导出来的指数大于8就可以modp(x)推导出来指数小于8
2、python实现(基础版)
# parameter
rs_n = 255
rs_k = 251
rs_m = 8
# rs_t = 2
p_x = int('100011101', 2) # primitive polynomial GF(256)
# x_0_7 = [1, 2, 4, 8, 16, 32, 64, 128]
x8 = p_x ^ (2 ** rs_m)
# GF function
# GF_xn(n) : element a(n); GF_add(a,b): add; GF_mult(a,b): multiply
def gf_xn(n):
if n < rs_m:
return 2 ** n
elif n == rs_m:
return x8
else:
tmp_xn = gf_xn(n - 1) << 1
while tmp_xn > 2 ** rs_m:
tmp_xn_low = tmp_xn & rs_n
tmp_xn = tmp_xn_low ^ x8
return tmp_xn
def gf_add(a, b):
r = a ^ b
return r
def gf_mult(a, b):
if a == 0 or b == 0:
r = 0
else:
r_mul = 0
r_mul_high = 0
for i in range(rs_m):
if a & 2 ** i:
b_i = b << i
r_mul = r_mul ^ b_i
for j in range(rs_n):
if j >= rs_m:
if r_mul & 2 ** j:
r_mul_high = r_mul_high ^ gf_xn(j)
r_mul_low = r_mul & rs_n
r = r_mul_low ^ r_mul_high
return r
def rs_encoder(mx):
rx_pre = [0, 0, 0, 0]
rx = [0, 0, 0, 0]
for mx_i in mx:
fec_i = gf_add(rx_pre[3], mx_i)
rx[0] = gf_mult(64, mx_i)
rx[1] = gf_add(gf_mult(120, fec_i), rx_pre[0])
rx[2] = gf_add(gf_mult(54, fec_i), rx_pre[1])
rx[3] = gf_add(gf_mult(15, fec_i), rx_pre[2])
return rx
if __name__ == '__main__':
mx = [0 for i in range(rs_k)]
with open('mx.txt', 'rb') as mx_f:
mx_all = mx_f.read()
mx_list = mx_all.split("\n")
for i in range(rs_k):
mx[i] = int(mx_list[i])
print(mx)
rx = rs_encoder(mx)
print(rx)
其中px是本原多项式,xn就是GF(256)中的各个元素
mx 是 RS(255,251) 生成多项式 对应的 移位寄存器的输入B0-B250
rx 是mx 编码后得到的4 位 RS0-RS3
gf_xn 以及gf_mult 都是根据推导一步步写的,比较笨拙的写法(数学不好,算法不好没办法)
3、其他
输入法也不知道怎么了,shift也调不回来了,懒得管了,看起来可能有点费眼睛(我输入也挺费眼的。。。)
之前的文章为啥变成vip免费了?我什么也没干,又去一篇一篇重新编辑成全部可见了。。。。。费人