Berlekamp-Massey Algorithm
- •
What it is
- •
Given a finite bit sequence, Berlekamp–Massey finds a shortest linear recurrence consistent with it. For a sequence of linear complexity L, 2L consecutive bits suffice to recover its minimal recurrence under the usual linear-sequence assumptions. This need not identify the physical register’s exact taps or make a nonlinear filtered sequence easy to predict.
- •
- •
When to apply
- •
You have a sufficiently long bit sequence (at least bits if you suspect an -bit LFSR) believed to come from an LFSR with unknown feedback taps.
- •
- •
Symbols and assumptions
- •
are observed bits. A connection polynomial represents . L is the minimal recurrence length for the sequence.
- •
- •
Math — step by step
- •
- At position i, compute discrepancy . Zero means the current recurrence predicts this bit correctly.
- •
- For nonzero discrepancy, XOR a shifted saved connection polynomial into C to cancel it. If , increase L to and save the previous C as the next correction reference.
- •
- For a sequence of linear complexity L, 2L consecutive bits suffice to recover its minimal recurrence. The physical register may be longer or use a nonminimal realization; the algorithm does not uniquely identify that hardware.
- •
- Connection-polynomial coefficients describe delays. Translate delays into the implementation’s shift direction and tap convention before generating bits. Test on held-out output. Nonlinear filtering can make the visible complexity far exceed the register length.
- •
- •
- •
Python
- •
def berlekamp_massey(seq): n = len(seq) C, B = [1]+[0]*n, [1]+[0]*n L, m, b = 0, 1, 1 for i in range(n): d = seq[i] ^ sum(C[j] & seq[i-j] for j in range(1, L+1)) % 2 if d == 0: m += 1 elif 2*L <= i: T = C[:] for j in range(n+1-m): C[j+m] ^= d & B[j] L, B, m, b = i+1-L, T, 1, d else: for j in range(n+1-m): C[j+m] ^= d & B[j] m += 1 return C[:L+1], L
- •
- •
SageMath
- •
berlekamp_massey()is available directly, operating over a sequence ofGF(2)elements - •
from sage.matrix.berlekamp_massey import berlekamp_massey poly = berlekamp_massey([GF(2)(b) for b in bits]) # Translate the returned polynomial convention to your shift/tap indexing. # Regenerate held-out bits before accepting the recurrence.
- •
- •
- •
Cards
- •
What does the usual 2L-bit recovery statement identify?
- •
The minimal recurrence of a sequence of linear complexity L. A physical register can be longer, nonminimal, or hidden behind a nonlinear filter.
- •
- •
What does the algorithm actually compute?
- •
The shortest LFSR (its exact feedback polynomial) capable of producing the given bit sequence — the sequence's linear complexity.
- •
- •