A binary linear-feedback shift register of length stores bits. At each clock it outputs one bit, shifts the state, and inserts a fixed linear combination over of the stored bits. Equivalently, its output obeys a recurrence
The Berlekamp-Massey algorithm processes the observed symbols from left to right. It maintains the shortest current connection polynomial , its degree , and a saved polynomial from the most recent degree increase. At step it computes the discrepancy
If , the current recurrence already predicts the new symbol. If , a shifted multiple of the saved polynomial is added to to cancel the discrepancy; when , the algorithm also replaces by and saves the old polynomial.
For the observed binary sequence, the nonzero-discrepancy updates are
All discrepancies through then vanish. Thus the shortest recurrence is
or, equivalently, . The connection polynomial is , and with the reciprocal convention the feedback polynomial is
A general binary feedback shift register of length has state
and a feedback function . One update outputs the oldest bit, shifts the state, and inserts
The initial fill is . It is a linear-feedback shift register when
for fixed .
The Berlekamp-Massey algorithm reads an intercepted sequence from left to right while maintaining the shortest connection polynomial that reproduces the prefix. At each new symbol it computes the discrepancy between the observed bit and that predicted by the current recurrence. A zero discrepancy leaves the polynomial unchanged; a nonzero discrepancy adds a suitably shifted copy of the connection polynomial saved at the previous increase in linear complexity. After at least twice the unknown register length, it recovers the shortest recurrence, after which the entire keystream can be predicted.
Applying those discrepancy updates to
returns the connection polynomial
Equivalently, the sequence obeys
Indeed this predicts successively . No recurrence of length one or two fits the prefix, so its linear complexity is three.