Click here to Skip to main content
13,298,965 members (39,753 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 15 Sep 2005

Implementation of Berlekamp-Massey Algorithm

, 15 Sep 2005
Rate this:
Please Sign up or sign in to vote.
Implementation of Berlekamp-Massey algorithm


The Berlekamp-Massey algorithm is an efficient algorithm for determining the linear complexity of a finite binary sequence s<sub>n</sub> of length n. The algorithm takes n iterations, with the Nth iteration computing the linear complexity of the subsequence s<sub>N</sub> consisting of the first N terms of s<sub>n</sub>. Returned value (L) is the length of the shortest linear feedback shift register sequence that generates all bits in sequence s<sub>n</sub>.
(Used in NIST statistical test suite - "Linear Complexity Test".)

The Code

    // Implementation of Berlekamp-Massey algorithm for calculating linear 
    // complexity of binary sequence
    // s = byte array with binary sequence
    // returns Length of LFSR with smallest length which generates s
    // for an example: int L=BerlekampMassey(new byte[] {1,0,1,0,1,1,1,0,1,0})
    //        reference: "Handbook of Applied Cryptography", p201

    public static int BerlekampMassey(byte[] s)                
        int L, N, m, d;
        int n=s.Length;
        byte[] c=new byte[n];
        byte[] b=new byte[n];
        byte[] t=new byte[n];

        //Algorithm core
        while (N<n)
            for (int i=1; i<=L; i++)
            d^=c[i]&s[N-i];            //(d+=c[i]*s[N-i] mod 2)
            if (d==1)
                Array.Copy(c, t, n);    //T(D)<-C(D)
                for (int i=0; (i+N-m)<n; i++)
                if (L<=(N>>1))
                    Array.Copy(t, b, n);    //B(D)<-T(D)
        return L;


Sorry, but this might only be useful to those freaks (like me:) that work on statistical tests and/or cryptanalysis. I just needed to put this on the Web because I couldn't find some useful implementation out there.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Miroslav Stampar
Software Developer (Senior)
Croatia Croatia
No Biography provided

You may also be interested in...

Comments and Discussions

Generalberelekamp massey Pin
raza817-May-09 12:37
memberraza817-May-09 12:37 
GeneralJust Curious Pin
JohnDeHope315-Sep-05 5:18
memberJohnDeHope315-Sep-05 5:18 
GeneralRe: Just Curious Pin
vadivhere15-Sep-05 8:10
membervadivhere15-Sep-05 8:10 
GeneralRe: Just Curious Pin
Miroslav Stampar15-Sep-05 23:47
memberMiroslav Stampar15-Sep-05 23:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 15 Sep 2005
Article Copyright 2005 by Miroslav Stampar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid