# Berlekamp–Massey algorithm

Jump to navigation Jump to search

The Berlekamp–Massey algorithm is an algorithm that will find the shortest linear feedback shift register (LFSR) for a given binary output sequence. The algorithm will also find the minimal polynomial of a linearly recurrent sequence in an arbitrary field. The field requirement means that the Berlekamp–Massey algorithm requires all non-zero elements to have a multiplicative inverse.[1] Reeds and Sloane offer an extension to handle a ring.[2]

Elwyn Berlekamp invented an algorithm for decoding Bose–Chaudhuri–Hocquenghem (BCH) codes.[3][4] James Massey recognized its application to linear feedback shift registers and simplified the algorithm.[5][6] Massey termed the algorithm the LFSR Synthesis Algorithm (Berlekamp Iterative Algorithm),[7] but it is now known as the Berlekamp–Massey algorithm.

## Description of algorithm

The Berlekamp–Massey algorithm is an alternative to the Reed–Solomon Peterson decoder for solving the set of linear equations. It can be summarized as:

${\displaystyle S_{i+\nu }+\Lambda _{1}S_{i+\nu -1}+\cdots +\Lambda _{\nu -1}S_{i+1}+\Lambda _{\nu }S_{i}=0.}$

In the code examples below, C(x) is a potential instance of Λ(x). The error locator polynomial C(x) for L errors is defined as:

${\displaystyle C(x)=C_{L}\ x^{L}+C_{L-1}\ x^{L-1}+\cdots +C_{2}\ x^{2}+C_{1}\ x+1}$

or reversed:

${\displaystyle C(x)=1+C_{1}\ x+C_{2}\ x^{2}+\cdots +C_{L-1}\ x^{L-1}+C_{L}\ x^{L}.}$

The goal of the algorithm is to determine the minimal degree L and C(x) which results in all syndromes

${\displaystyle S_{n}+C_{1}\ S_{n-1}+\cdots +C_{L}\ S_{n-L}}$

being equal to 0:

${\displaystyle S_{n}+C_{1}\ S_{n-1}+\cdots +C_{L}\ S_{n-L}=0,\qquad L\leq n\leq N-1.}$

Algorithm: C(x) is initialized to 1, L is the current number of assumed errors, and initialized to zero. N is the total number of syndromes. n is used as the main iterator and to index the syndromes from 0 to (N-1). B(x) is a copy of the last C(x) since L was updated and initialized to 1. b is a copy of the last discrepancy d (explained below) since L was updated and initialized to 1. m is the number of iterations since L, B(x), and b were updated and initialized to 1.

Each iteration of the algorithm calculates a discrepancy d. At iteration k this would be:

${\displaystyle d=S_{k}+C_{1}\ S_{k-1}+\cdots +C_{L}\ S_{k-L}.}$

If d is zero, the algorithm assumes that C(x) and L are correct for the moment, increments m, and continues.

If d is not zero, the algorithm adjusts C(x) so that a recalculation of d would be zero:

${\displaystyle C(x)=C(x)\ -\ (d/b)\ x^{m}\ B(x).}$

The xm term shifts B(x) so it follows the syndromes corresponding to 'b'. If the previous update of L occurred on iteration j, then m = k - j, and a recalculated discrepancy would be:

${\displaystyle d=S_{k}+C_{1}\ S_{k-1}+\cdots -(d/b)(S_{j}+B_{1}\ S_{j-1}+\cdots ).}$

This would change a recalculated discrepancy to:

${\displaystyle d=d-(d/b)b=d-d=0.\ }$

The algorithm also needs to increase L (number of errors) as needed. If L equals the actual number of errors, then during the iteration process, the discrepancies will become zero before n becomes greater than or equal to (2 L). Otherwise L is updated and algorithm will update B(x), b, increase L, and reset m = 1. The formula L = (n + 1 - L) limits L to the number of available syndromes used to calculate discrepancies, and also handles the case where L increases by more than 1.

## Code sample

The algorithm from Massey (1969, p. 124) for an arbitrary field:

  polynomial(field K) s(x) = ... /* coeffs are s_j; output sequence as N-1 degree polynomial) */
/* connection polynomial */
polynomial(field K) C(x) = 1;  /* coeffs are c_j */
polynomial(field K) B(x) = 1;
int L = 0;
int m = 1;
field K b = 1;
int n;

/* steps 2. and 6. */
for (n = 0; n < N; n++)
{
/* step 2. calculate discrepancy */
field K d = s_n + \Sigma_{i=1}^L c_i * s_{n-i};

if (d == 0)
{
/* step 3. discrepancy is zero; annihilation continues */
m = m + 1;
}
else if (2 * L <= n)
{
/* step 5. */
/* temporary copy of C(x) */
polynomial(field K) T(x) = C(x);

C(x) = C(x) - d b^{-1} x^m B(x);
L = n + 1 - L;
B(x) = T(x);
b = d;
m = 1;
}
else
{
/* step 4. */
C(x) = C(x) - d b^{-1} x^m B(x);
m = m + 1;
}
}
return L;


## The algorithm for the binary field

The following is the Berlekamp–Massey algorithm specialized for the binary finite field F2 (also written GF(2)). The field elements are '0' and '1'. The field operations '+' and '−' are identical and are equivalent to the 'exclusive or' operation, XOR. The multiplication operator '*' becomes the logical AND operation. The division operator reduces to the identity operation (i.e., field division is only defined for dividing by 1, and x/1 = x).

1. Let ${\displaystyle s_{0},s_{1},s_{2}\cdots s_{n-1}}$ be the bits of the stream.
2. Initialise two arrays ${\displaystyle b}$ and ${\displaystyle c}$ each of length ${\displaystyle n}$ to be zeroes, except ${\displaystyle b_{0}\leftarrow 1,c_{0}\leftarrow 1}$
3. assign ${\displaystyle L\leftarrow 0,m\leftarrow -1}$.
4. For ${\displaystyle N=0}$ step 1 while ${\displaystyle N:
• Let discrepancy ${\displaystyle d}$ be ${\displaystyle s_{N}\oplus c_{1}s_{N-1}\oplus c_{2}s_{N-2}\oplus \cdots \oplus c_{L}s_{N-L}}$.
• if ${\displaystyle d=0}$, then ${\displaystyle c}$ is already a polynomial which annihilates the portion of the stream from ${\displaystyle N-L}$ to ${\displaystyle N}$.
• else:
• Let ${\displaystyle t}$ be a copy of ${\displaystyle c}$.
• Set ${\displaystyle c_{N-m}\leftarrow c_{N-m}\oplus b_{0},c_{N-m+1}\leftarrow c_{N-m+1}\oplus b_{1},\dots }$ up to ${\displaystyle c_{n-1}\leftarrow c_{n-1}\oplus b_{n-N+m-1}}$ (where ${\displaystyle \oplus }$ is the Exclusive or operator).
• If ${\displaystyle L\leq {\frac {N}{2}}}$, set ${\displaystyle L\leftarrow N+1-L}$, set ${\displaystyle m\leftarrow N}$, and let ${\displaystyle b\leftarrow t}$; otherwise leave ${\displaystyle L}$, ${\displaystyle m}$ and ${\displaystyle b}$ alone.

At the end of the algorithm, ${\displaystyle L}$ is the length of the minimal LFSR for the stream, and we have ${\displaystyle c_{L}s_{a}\oplus c_{L-1}s_{a+1}\oplus c_{L-2}s_{a+2}\oplus \cdots =0}$ for all ${\displaystyle a}$.

## References

1. ^ Reeds & Sloane 1985, p. 2
2. ^ Reeds, J. A.; Sloane, N. J. A. (1985), "Shift-Register Synthesis (Modulo n)" (PDF), SIAM Journal on Computing, 14 (3): 505–513, doi:10.1137/0214038
3. ^ Berlekamp, Elwyn R. (1967), Nonbinary BCH decoding, International Symposium on Information Theory, San Remo, Italy
4. ^ Berlekamp, Elwyn R. (1984) [1968], Algebraic Coding Theory (Revised ed.), Laguna Hills, CA: Aegean Park Press, ISBN 0-89412-063-8. Previous publisher McGraw-Hill, New York, NY.
5. ^ Massey, J. L. (1969), "Shift-register synthesis and BCH decoding" (PDF), IEEE Trans. Inf. Theory, IT-15 (1): 122–127
6. ^ Ben Atti, Nadia; Diaz-Toca, Gema M.; Lombardi, Henri, The Berlekamp–Massey Algorithm revisited, CiteSeerX
7. ^ Massey 1969, p. 124