GF(2)[x] 指系数为 0、1 的多项式。之前在学校有做过有限域 GF(2^8) 上的密码算法研究,而构成有限域 GF(2^8) 的其中一个必要条件就是选取一个 GF(2)[x] 上的8次不可约多项式(使得有限域上的加减乘除和求逆在 mod 这个不可约多项式后能正常运算),下面是当时暴力求解的代码,于是记录一下。
import numpy
import math
C = []
for i in range(1, 5): # [1 2 3 4]
j = 8 - i
A = []
B = []
for row in range(1, int(math.pow(2, i)) + 1): # 从1取到2^i
tempList = []
for x in list((bin(row))[2:].zfill(i + 1)):
tempList.append(int(x))
tempList[0] = 1 # i 次多项式最高次系数必须为1
A.append(tempList)
for row in range(1, int(math.pow(2, j)) + 1): # 从1取到2^j
tempList = []
for x in list((bin(row))[2:].zfill(j + 1)):
tempList.append(int(x))
tempList[0] = 1
B.append(tempList)
for i1 in range(int(math.pow(2, i))):
for j1 in range(int(math.pow(2, j))):
p1 = numpy.poly1d(A[i1]) # 将系数列表传入,生成多项式
p2 = numpy.poly1d(B[j1])
tempCoeffs = (p1 * p2).coeffs
for x in range(len(tempCoeffs)):
tempCoeffs[x] = tempCoeffs[x] % 2 # 系数归一化
C.append(tempCoeffs) # 可分解成多项式乘积的多项式为可约多项式
for x in range(len(C)):
C[x] = list(C[x]) # 转换成常规列表
for num in range(256): # 0-255
tempList = [1]
for x in list((bin(num))[2:].zfill(8)):
tempList.append(int(x))
if tempList not in C: # 如果没在可约多项式集合,则属于不可约多项式
print(tempList)