我使用Wikipedia上的说明在 Python 中编写了一个伽罗瓦线性反馈移位寄存器:
def lfsr(coefficients, state):
while 1:
lsb = state.pop()
state.insert(0, 0)
if lsb:
state = app(coefficients, state)
yield lsb
def app(coefficients, state):
return [ (coefficients[i]^state[i]) for i in range(len(state)) ]
L = lfsr([1,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,1], [0]*15+[1])
seq = [ str(L.next()) for i in range(2**16+16) ]
这工作正常。我现在想做的是编写一个通用版本,可以处理 GF(2) 以外的 Galois 字段,但我不明白关于non-binary LFSRs的部分。这部分对我来说没有意义:“反馈位(输出位)乘以(模 q)乘以 q 进制值,该值对于每个特定的抽头点都是恒定的。” 如何将单个输出位乘以每个抽头点的值?
这是我想出的,但不是给出循环序列,而是输出迅速恶化为全 0:
# Multiplication table for GF(4)
mult_4 = [[0, 0, 0, 0],
[0, 1, 2, 3],
[0, 2, 3, 1],
[0, 3, 1, 2]]
def lfsr(coefficients, state):
while 1:
lsb = state.pop()
state.insert(0, 0)
state = app(coefficients, state)
yield lsb
def app(coefficients, state):
return [ mult_4[coefficients[i]][state[i]] for i in range(len(state)) ]
L = lfsr([ 1, 0, 0, 0, 1, 2, 3 ], [1]*6)
seq = [ str(L.next()) for i in range(4**6+6) ]