Berlekamp-Massey算法在二元域上的Python实现

Berlekamp-Massey算法

本文介绍了Berlekamp-Massey算法流程以及对应的一个二元域上的Python实现。
注:本文的实现不适用于 a 0 ≠ 0 a_0\neq0 a0=0的序列

前置知识

线性综合解
在这里插入图片描述

如何理解退化的线性移位寄存器?

在非退化的线性移位寄存器中,我们有 deg ⁡ ( f N ( x ) ) = l N \deg(f_N(x))=l_N deg(fN(x))=lN,即生成线性移位寄存器序列时,新生成的位一定依赖当前状态中的第一位。

例:0011101的线性综合解为 ( 1 + x + x 3 , 3 ) (1+x+x^3,3) (1+x+x3,3)

给0011101编为 a 0 a 1 a 2 a 3 a 4 a 5 a 6 a_0a_1a_2a_3a_4a_5a_6 a0a1a2a3a4a5a6,则初态应该有3位,是 a 0 a 1 a 2 a_0a_1a_2 a0a1a2即001

注意线性综合解中的多项式是联接多项式(也叫反馈多项式),通过对其求互反多项式可以求得线性移位寄存器序列的特征多项式。

求互反多项式:将联接多项式中的 x x x换成 1 x 1\over x x1,再乘以 x l N x^{l_N} xlN

( 1 + 1 x + 1 x 3 ) x 3 = x 3 + x 2 + 1 (1+{1\over x}+{1\over x^3})x^3=x^3+x^2+1 (1+x1+x31)x3=x3+x2+1,所以依据特征多项式的定义知 a n + 3 = a n + 2 + a n a_{n+3}=a_{n+2}+a_{n} an+3=an+2+an

后面生成新的位的过程应该是: a 3 = a 2 + a 0 = 1 + 0 = 1 a_3=a_2+a_0=1+0=1 a3=a2+a0=1+0=1,依此类推

而退化的线性移位寄存器序列的生成不依赖当前状态的第一位。

例:00101101的线性综合解为 ( 1 + x + x 2 , 4 ) (1+x+x^2,4) (1+x+x2,4)

初态为 a 0 a 1 a 2 a 3 a_0a_1a_2a_3 a0a1a2a3即0010

同理, a 4 = a 3 + a 2 = 0 + 1 = 1 , a 5 = a 4 + a 3 = 1 + 0 = 1 a_4=a_3+a_2=0+1=1,a_5=a_4+a_3=1+0=1 a4=a3+a2=0+1=1,a5=a4+a3=1+0=1,依此类推

算法流程

该算法可以求出关于给定有限长序列 a ( N ) a^{(N)} a(N)的线性综合解。

在这里插入图片描述
在这里插入图片描述

算法实现思路

给定一个0-1序列(二元域,考虑取余)(采用0,1列表表示)

(1)初始化:

找到第一个不等于0的位作为 a n 0 a_{n_0} an0,顺序查找

a 0 = a 1 = . . . = a n 0 − 1 = 0 a_0=a_1=...=a_{n_0-1}=0 a0=a1=...=an01=0

d 0 = d 1 = . . . = d n 0 − 1 = 0 , d n 0 = a n 0 d_0=d_1=...=d_{n_0-1}=0,d_{n_0}=a_{n_0} d0=d1=...=dn01=0,dn0=an0,需要存储

f 1 ( x ) = f 2 ( x ) = . . . = f n 0 ( x ) = 1 f_1(x)=f_2(x)=...=f_{n_0}(x)=1 f1(x)=f2(x)=...=fn0(x)=1

l 1 = l 2 = . . = l n 0 = 0 l_1=l_2=..=l_{n_0}=0 l1=l2=..=ln0=0,需要存储

f n 0 + 1 ( x ) = 1 − d n 0 x n 0 + 1 , l n 0 + 1 = n 0 + 1 f_{n_0+1}(x)=1-d_{n_0}x^{n_0+1},l_{n_0+1}=n_0+1 fn0+1(x)=1dn0xn0+1,ln0+1=n0+1,

设计多项式类,设计线性综合解二元组类

(2)迭代计算:(for循环)

假设 ( f i ( x ) , l i ) , 1 ≤ i ≤ n (f_i(x),l_i),1\le i\le n (fi(x),li),1in已经求得。

f n ( x ) = 1 + c n , 1 x + . . . + c n , l n x l n f_n(x)=1+c_{n,1}x+...+c_{n,l_n}x^{l_n} fn(x)=1+cn,1x+...+cn,lnxln

计算 d n = a n + c n , 1 a n − 1 + . . . + c n , l n a n − l n d_n=a_n+c_{n,1}a_{n-1}+...+c_{n,l_n}a_{n-l_n} dn=an+cn,1an1+...+cn,lnanln

if判断

如果 d n = 0 d_n=0 dn=0,取 f n + 1 ( x ) = f n ( x ) , l n + 1 = l n f_{n+1}(x)=f_n(x),l_{n+1}=l_n fn+1(x)=fn(x),ln+1=ln

如果 d n ≠ 0 d_n\neq 0 dn=0,存在 1 ≤ m < n 1\le m\lt n 1m<n

使 l m < l m + 1 = l m + 2 = . . . = l n l_m\lt l_{m+1}=l_{m+2}=...=l_n lm<lm+1=lm+2=...=ln

找到最后一个 m m m,顺序查找

f n + 1 ( x ) = f n ( x ) − d n d m − 1 x n − m f m ( x ) f_{n+1}(x)=f_n(x)-d_nd_m^{-1}x^{n-m}f_m(x) fn+1(x)=fn(x)dndm1xnmfm(x)

l n + 1 = m a x { l n , n + 1 − l n } l_{n+1}=max\{l_n,n+1-l_n\} ln+1=max{ln,n+1ln}

需要实现多项式的加法

因为只考虑二元域,所以这里的系数的加法都模2取余,且不进位(采用异或实现),算法中的减法统一替换成加法。

算法代码

# author:njw
# 二元域上的Berlekamp-Massey算法

# 二元域上的多项式类
class Polynomial:
    def __init__(self, coefficients):
        self.coefficients = coefficients
        self.degree = len(coefficients) - 1

    # 二元域上的多项式的加法,做异或运算
    def __add__(self, other):
        if self.degree > other.degree:
            return Polynomial(
                [
                    self.coefficients[i] ^ other.coefficients[i]
                    for i in range(other.degree + 1)
                ]
                + self.coefficients[other.degree + 1 :]
            )
        else:
            return Polynomial(
                [
                    self.coefficients[i] ^ other.coefficients[i]
                    for i in range(self.degree + 1)
                ]
                + other.coefficients[self.degree + 1 :]
            )


# 线性综合解类(用于存储线性综合解)(二元组)
class LinearCombinationSolution:
    def __init__(self, polynomial, order):
        self.polynomial = polynomial
        self.order = order


# 二元域上的Berlekamp-Massey算法
# 输入:0-1序列(采取0-1列表表示)
def Berlekamp_Massey_algorithm(sequence):
    # 初始化
    n = 0
    # 找到第一个不等于0的位
    for i in range(len(sequence)):
        if sequence[i] == 1:
            n = i
            break

    d = [0 for i in range(n)]
    d.extend([1])

    linear_combination_solutions = []
    for i in range(n):
        linear_combination_solutions.append(
            LinearCombinationSolution(Polynomial([1]), 0)
        )

    linear_combination_solutions.append(
        LinearCombinationSolution(Polynomial([1] + [0 for i in range(n)] + [1]), n + 1)
    )

    # 迭代
    for i in range(n, len(sequence) - 1):
        # 计算新的d
        temp_d = 0
        for j in range(linear_combination_solutions[-1].polynomial.degree + 1):
            temp_d += (
                linear_combination_solutions[-1].polynomial.coefficients[j]
                * sequence[i - j + 1]
            )

        temp_d = temp_d % 2
        d.append(temp_d)
        # 如果d为0,那么线性综合解不变
        if temp_d == 0:
            linear_combination_solutions.append(linear_combination_solutions[-1])
        # 如果d不为0,那么线性综合解更新
        else:
            m = len(linear_combination_solutions) - 1
            # 寻找当前最后一个跳跃的l
            for k in range(len(linear_combination_solutions)):
                if (
                    linear_combination_solutions[m - k].order
                    < linear_combination_solutions[-1].order
                ):
                    # 更新线性综合解
                    linear_combination_solutions.append(
                        LinearCombinationSolution(
                            # 更新线性综合解的联接多项式
                            linear_combination_solutions[-1].polynomial.__add__(
                                Polynomial(
                                    [0 for l in range(i - m + k)]
                                    + linear_combination_solutions[
                                        m - k
                                    ].polynomial.coefficients
                                )
                            ),
                            # 更新线性综合解的阶
                            max(
                                linear_combination_solutions[-1].order,
                                i + 2 - linear_combination_solutions[-1].order,
                            ),
                        )
                    )
                    break
    return linear_combination_solutions[-1]
# 返回线性综合解的联接多项式和阶

# 测试
if __name__ == "__main__":
    # test1
    # sequence = [0,0,1,0,1,1,0,1]
    # test2
    sequence = [0,0,1,1,1,0,1]
    # test3 都是用之前编写的LFSR产生的序列
    # sequence = [0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1]
    # test4
    # sequence = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1]
    # test5
    # sequence = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1]
    # sequence =[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,1,1,1]
    linear_combination_solution = Berlekamp_Massey_algorithm(sequence)
    print(linear_combination_solution.polynomial.coefficients)
    print(linear_combination_solution.order)
    # 结果说明:[1,1,0,1],3是指联接多项式为1+x+x^3,阶为3
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Berlekamp-Massey算法是一种线性复杂度算法,用于在一个序列中求出最短的线性递推序列。它可以用来检测线性级别的纠错码,并在检测到错误时纠正这些错误。该算法于1967年由Elwyn Berlekamp和James Massey首先提出。 ### 回答2: Berlekamp-Massey算法是线性递推序列的一个求解算法,主要用于加密算法、编码和错误校验码等。 对于一个由$a_0, a_1, a_2, ..., a_n$组成的序列,如果它是一个线性递推序列,则存在$f(x)=f_0+f_1x+f_2x^2+...+f_nx^n$和$g(x)=g_0+g_1x+g_2x^2+...+g_nx^n$满足以下条件: 1. $f(x)a_n+f_1(x)a_{n-1}+f_2(x)a_{n-2}+...+f_n(x)a_0=0$ 2. $f(x)g(x)=1+0x+0x^2+...+0x^{2n}$ 其中$f(x)$和$g(x)$都是多项式,系数都属于$GF(2)$,即所有系数都为$0$或$1$。 Berlekamp-Massey算法的核心思想是通过不断更新推导出$f(x)$多项式,从而确定序列是否为线性递推序列。具体操作如下: 1. 初始化$f(x)=a_0$和$g(x)=1$ 2. 设$i=0$,继续下面的步骤。 3. 如果$f(x)$使得$f(x)a_i+f_1(x)a_{i-1}+f_2(x)a_{i-2}+...+f_i(x)a_{i-i}=0$,则跳过下一步。 4. 否则更新$f(x)$和$g(x)$为:$f(x)=f(x)-f_i(x)x^{i-ld}$,$g(x)=g(x)+f_i(x)x^{i-ld}$,其中$ld$是最低位的非零项指数。 5. 如果$i$等于序列长度$n$,则停止,否则将$i$增加1,返回步骤3。 当算法执行结束后,如果$deg(f(x))<n$,则序列是线性递推序列。否则,序列不是线性递推序列。 Berlekamp-Massey算法具有高效的时间复杂度和空间复杂度,并且能够在有限时间内判断序列是否为线性递推序列。由于其可靠性和适用性,该算法在加密、编码、校验等方面得到广泛应用。 ### 回答3: Berlekamp-Massey算法是一种线性复杂度扫描算法,用于寻找给定有限上的线性递推序列的最短线性递推关系。在密码学、纠错码、伪随机序列等应用中有着广泛的应用。 该算法的基本思想是利用一个长度为m的寄存器序列和一个长度为m的系数序列,去逐步生成原序列,通过比较原序列和生成序列之间的差异,逐步解决递推关系。当序列长度超过m时,就可以使用修改寄存器序列和系数序列来更新序列。在任意时刻,算法都会保持当前序列前r个元素的线性关系,直到找到整个递推式。因此,它可以通过线性时间求解整个递推式。 Berlekamp-Massey算法对于极大伪随机序列具有特别的重要性,因为它可以判断一个序列是否为线性复杂度生成,并且可以通过线性时间求出其线性递推关系。具体来说,一个序列为线性复杂度生成当且仅当它的线性递推关系的位数达到了序列的长度。在密码学中,这意味着一个暴力破解程序所需要的运算次数会达到指数级。因此,一些密码学应用需要使用Berlekamp-Massey算法对生成的伪随机序列进行测试,以保证其没有线性递推关系,从而更好地保证密码的安全性。 总的来说,Berlekamp-Massey算法是一种重要的算法,在密码学和其他应用中都有广泛的应用和重要性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值