Berlekamp–Massey algorithm

This is an old revision of this page, as edited by 93.73.130.221 (talk) at 20:05, 20 September 2009. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

The Berlekamp–Massey algorithm is an algorithm for finding the shortest linear feedback shift register (LFSR) for a given output sequence. Equivalently, it is an algorithm for finding the minimal polynomial of a linearly recurrent sequence.

The algorithm was invented by Elwyn Berlekamp in 1968. Its connection to linear codes was observed by James Massey the following year. It became the key to practical application of the now ubiquitous Reed–Solomon code.

The algorithm

  1. Let   be the bits of the stream.
  2. Initialise three arrays  ,   and   each of length   to be zeroes, except  ; assign  .
  3. While   is less than  :
    • Let   be  .
    • If   is zero, then   is already a polynomial which annihilates the portion of the stream from   to  ; increase   by 1 and continue.
    • If   is 1, then:
      • Let   be a copy of  .
      • Set   up to   (where   is the Exclusive or operator).
      • If  , set  , set  , and let  ; otherwise leave  ,   and   alone.
      • Increment   and continue.

At the end of the algorithm,   is the length of the minimal LFSR for the stream, and we have   for all  .

Code Sample in C#

        byte[] b, c, t, s;
        int N, L, m;
        int position = 0;

        public BerlekampRegistryTester(int sequenceLenght)
        {
            b = new byte[sequenceLenght];
            c = new byte[sequenceLenght];
            t = new byte[sequenceLenght];
            s = new byte[sequenceLenght];
            for (int i = 0; i < sequenceLenght; i++) 
            {
                b[i] = c[i] = t[i] = 0;
            }
            b[0] = c[0] = 1;
            N = L = 0;
            m = -1;
        }

        private void runTest() 
        {
            int d;
            while (N < s.Length)
            {
                //STEP 1
                d = 0;
                for (int i = 0; i <= L; i++) 
                {
                    d += s[N-i] * c[i];
                }
                d = d % 2;

                //STEP 2
                if (d == 0)
                {
                    N++;
                    continue;
                }
                //STEP 3
                else
                {
                    //3.1
                    t = (byte[])c.Clone();
                    //3.2
                    for (int i = 0; i <= s.Length + m - 1 - N; i++)
                    {
                        c[N - m + i] = (byte)(c[N - m + i] ^ b[i]);
                    }
                    //3.3
                    if (L <= (N / 2))
                    {
                        L = N + 1 - L;
                        m = N;
                        b = (byte[])t.Clone();
                    }
                    else
                    {
                        //leave m,l,b alone
                    } 
                    N++;
                }
                
            }
        }

See also

  • Berlekamp–Massey algorithm at PlanetMath.
  • Weisstein, Eric W. "Berlekamp–Massey Algorithm". MathWorld.
  • Implementation in Mathematica
  • Template:De icon Applet Berlekamp–Massey algorithm