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. Reeds and Sloane offer an extension to handle a ring.

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

The Berlekamp–Massey algorithm is an alternate method to solve the set of linear equations described in Reed–Solomon Peterson decoder, which can be summarized as:

S
i
+
ν
+
Λ
1
S
i
+
ν
−
1
+
⋯
+
Λ
ν
−
1
S
i
+
1
+
Λ
ν
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:

C
(
x
)
=
C
L
x
L
+
C
L
−
1
x
L
−
1
+
⋯
+
C
2
x
2
+
C
1
x
+
1
or reversed:

C
(
x
)
=
1
+
C
1
x
+
C
2
x
2
+
⋯
+
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

S
n
+
C
1
S
n
−
1
+
⋯
+
C
L
S
n
−
L

being equal to 0:

S
n
+
C
1
S
n
−
1
+
⋯
+
C
L
S
n
−
L
=
0
,
L
≤
n
≤
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:

d
=
S
k
+
C
1
S
k
−
1
+
⋯
+
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:

C
(
x
)
=
C
(
x
)
−
(
d
/
b
)
x
m
B
(
x
)
.
The x^{m} 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:

d
=
S
k
+
C
1
S
k
−
1
+
⋯
−
(
d
/
b
)
(
S
j
+
B
1
S
j
−
1
+
⋯
)
.
This would change a recalculated discrepancy to:

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.

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

The following is the Berlekamp–Massey algorithm specialized for the binary finite field F_{2} (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).

- Let
s
0
,
s
1
,
s
2
⋯
s
n
−
1
be the bits of the stream.
- Initialise two arrays
b
and
c
each of length
n
to be zeroes, except
b
0
←
1
,
c
0
←
1
- assign
L
←
0
,
m
←
−
1
.
**For**
N
=
0
**step** 1 **while**
N
<
n
:
- Let discrepancy
d
be
s
N
+
c
1
s
N
−
1
+
c
2
s
N
−
2
+
⋯
+
c
L
s
N
−
L
.
**if**
d
=
0
, **then**
c
is already a polynomial which annihilates the portion of the stream from
N
−
L
to
N
.
**else**:
- Let
t
be a copy of
c
.
- Set
c
N
−
m
←
c
N
−
m
⊕
b
0
,
c
N
−
m
+
1
←
c
N
−
m
+
1
⊕
b
1
,
…
up to
c
n
−
1
←
c
n
−
1
⊕
b
n
−
N
+
m
−
1
(where
⊕
is the Exclusive or operator).
- If
L
≤
N
2
, set
L
←
N
+
1
−
L
, set
m
←
N
, and let
b
←
t
; otherwise leave
L
,
m
and
b
alone.

At the end of the algorithm,
L
is the length of the minimal LFSR for the stream, and we have
c
L
s
a
+
c
L
−
1
s
a
+
1
+
c
L
−
2
s
a
+
2
+
⋯
=
0
for all
a
.