GF(2^8) 多项式的欧几里德算法

问题描述

我正在尝试为 GF(2^8) 中的 2 个多项式创建欧几里得算法(以解决 Bezout 关系)。

我目前有这个代码用于我的不同操作


class ReedSolomon:   
    gfSize = 256
    genPoly = 285 
    log = [0]*gfSize
    antilog = [0]*gfSize


    def _genLogAntilogArrays(self):
        self.antilog[0] = 1
        self.log[0] = 0
        self.antilog[255] = 1
        for i in range(1,255):
            self.antilog[i] = self.antilog[i-1] << 1 
            if self.antilog[i] >= self.gfSize:
                self.antilog[i] = self.antilog[i] ^ self.genPoly
            self.log[self.antilog[i]] = i

def __init__(self):
        self._genLogAntilogArrays()

def _galPolynomialDivision(self,dividend,divisor):
        result = dividend.copy()
        for i in range(len(dividend) - (len(divisor)-1)):
            coef = result[i]
            if coef != 0:
                for j in range(1,len(divisor)):
                    if divisor[j] != 0: 
                        result[i + j] ^= self._galMult(divisor[j],coef) # équivalent result[i + j] += -divisor[j] * coef car dans un champ GF(2) addition <=> substraction <=> XOR

        remainderIndex = -(len(divisor)-1)
        return result[:remainderIndex],result[remainderIndex:]

def _galMultiplicationPolynomiale(self,x,y):
        result = [0]*(len(x)+len(y)-1)
        for i in range(len(x)):
            for j in range(len(y)):
                result[i+j] ^= self._galMult(x[i],y[j])
        return result

def _galMult(self,y):
        if ((x == 0) or (y == 0)):
            val = 0
        else:
            val = self.antilog[(self.log[x] + self.log[y])%255]
        return val

def _galPolynomialAddition(self,a,b):
        polSum = [0] * max(len(a),len(b))
        for index in range(0,len(a)):
            polSum[index + len(polSum) - len(a)] = a[index]
        for index in range(0,len(b)):
            polSum[index + len(polSum) - len(b)] ^= b[index]
        return (polSum)

这是我的欧几里得算法:

def _galEuclideanAlgorithm(self,b):
        r0 = a.copy()
        r1 = b.copy()

        u0 = [1]
        u1 = [0]

        v0 = [0]
        v1 = [1]
        while max(r1) != 0:
            print(r1)
            q,r = self._galPolynomialDivision(r0,r1)
            r0 = self._galPolynomialAddition(self._galMultiplicationPolynomiale(q,r1),r)
            r1,r0 = self._galPolynomialAddition(r0,self._galMultiplicationPolynomiale(q,r1)),r1.copy()
            u1,u0 = self._galPolynomialAddition(u0,u1)),u1.copy()
            v1,v0 = self._galPolynomialAddition(v0,v1)),v1.copy()
        return r1,u1,v1

我不明白我的算法循环的问题,这是我测试的剩余输出:

rs = ReedSolomon()
a = [1,15,7,8,11]

b = [1,0]

print(rs._galEuclideanAlgorithm(b,a))

#Console output
'''
[1,11]
[0,82,37,120,11,105]
[1,11]
'''

我知道我似乎在抛出一些代码只是在期待答案,但我真的是在寻找错误。

提前致谢!

解决方法

我创建了一个名为 galois 的 Python 包来执行此操作。 galois 扩展了 NumPy 数组以对 Galois 字段进行操作。代码是用 Python 编写的,但为了速度使用 Numba 进行了 JIT 编译。除了数组算术,它还支持伽罗瓦域上的多项式。 ...而且 Reed-Solomon codes 也实现了 :)

用于求解 GF(2^8) 中两个多项式的 Bezout 恒等式的扩展欧几里得算法将以这种方式求解。下面是一段简短的源代码。您可以查看我的完整源代码 here

def poly_egcd(a,b):
    field = a.field
    zero = Poly.Zero(field)
    one = Poly.One(field)

    r2,r1 = a,b
    s2,s1 = one,zero
    t2,t1 = zero,one

    while r1 != zero:
        q = r2 / r1
        r2,r1 = r1,r2 - q*r1
        s2,s1 = s1,s2 - q*s1
        t2,t1 = t1,t2 - q*t1

    # Make the GCD polynomial monic
    c = r2.coeffs[0]  # The leading coefficient
    if c > 1:
        r2 /= c
        s2 /= c
        t2 /= c

    return r2,s2,t2

这是一个使用 galois 库和示例中的多项式的完整示例。 (我假设最高阶系数是第一?)

In [1]: import galois                                                                                                                                                                          

In [2]: GF = galois.GF(2**8)                                                                                                                                                                   

In [3]: print(GF.properties)                                                                                                                                                                   
GF(2^8):
  characteristic: 2
  degree: 8
  order: 256
  irreducible_poly: x^8 + x^4 + x^3 + x^2 + 1
  is_primitive_poly: True
  primitive_element: x

In [4]: a = galois.Poly([1,15,7,8,11],field=GF); a                                                                                                                                          
Out[4]: Poly(x^5 + 15x^4 + 7x^3 + 8x^2 + 11,GF(2^8))

In [5]: b = galois.Poly([1,0],field=GF); b                                                                                                                                          
Out[5]: Poly(x^6,GF(2^8))

In [6]: d,s,t = galois.poly_egcd(a,b); d,t                                                                                                                                              
Out[6]: 
(Poly(1,GF(2^8)),Poly(78x^5 + 7x^4 + 247x^3 + 74x^2 + 152,Poly(78x^4 + 186x^3 + 45x^2 + x + 70,GF(2^8)))

In [7]: a*s + b*t == d                                                                                                                                                                         
Out[7]: True

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...