The one-sentence version
An LFSR is a row of registers that XORs a few positions together and feeds the result back into the end of the queue, producing a long stream of "random-looking" bits. If the feedback polynomial is a primitive polynomial over , an -bit register reaches the maximum period of . But it is linear: after seeing just output bits, the Berlekamp–Massey algorithm can solve for the entire register. So an LFSR alone is insecure, and nonlinearity must be added.
This article has three layers:
- The intuition: a row of cells passing a message along
- Real-world use: GPS, Wi-Fi scrambling, CRC, and the failures of GSM and DVD encryption
- The mathematics: linear recurrences, primitive polynomials and maximal period, Berlekamp–Massey and worked examples
Layer 1: The Intuition
1.1 A row of cells passing a message along
Imagine 4 cells in a row, each holding 0 or 1. On each tick:
- The leftmost cell "spits out" its digit as output;
- All cells shift left by one;
- The cell left empty on the right is filled with the XOR of certain cells.
In the diagram above, the new value . The circuit needs only a few flip-flops and one XOR gate, so it is extremely cheap and extremely fast.
1.2 How long before it repeats
4 cells have states in all. The all-zero state stays at 0 forever, so at most 15 states can take turns. Choose the feedback positions correctly and it visits all 15 nonzero states before repeating; choose badly and the period is much shorter. Which positions are "right" is decided by whether a certain polynomial is primitive.
1.3 Why it is tempting and dangerous for encryption
The one-time pad needs a random key as long as the message. An LFSR offers a temptation: remember just the initial few dozen bits (the key), and you can generate a nearly unlimited "random" keystream to XOR with the plaintext.
The problem is that an LFSR is linear: its output bits satisfy a simple linear equation. An attacker who knows a small piece of plaintext (and therefore a small piece of keystream) can recover the whole register by solving a system of linear equations.
Layer 2: Real-World Use
Non-cryptographic uses (where LFSRs excel)
| Use | Explanation |
|---|---|
| GPS satellite signals | Each satellite's C/A code is generated by combining two 10-bit LFSRs (a Gold code), with period 1023. Different satellites' codes have low cross-correlation, so receivers can tell them apart |
| Scrambling | Wi-Fi, Ethernet, USB 3.0 and PCIe use LFSR sequences to shuffle data and avoid long runs of 0s or 1s |
| CRC checks | Essentially polynomial division over using an LFSR circuit; see "Casting Out Nines and Check Digits" |
| Chip testing, pseudorandom test data | Cheap and reproducible |
Cryptographic uses (more lessons than successes)
| System | Outcome |
|---|---|
| GSM phones, A5/1 | 3 LFSRs stepped irregularly by "majority vote"; broken in the 2000s, and rainbow tables later allowed real-time cracking on ordinary hardware |
| CSS on DVDs | Two LFSRs with an effective key of only 40 bits; broken by DeCSS in 1999 |
| Bluetooth E0 | 4 LFSRs plus a nonlinear combiner; several theoretical attacks exist |
Modern stream ciphers (ChaCha20, AES-CTR) no longer center on LFSRs. However, SNOW 3G and ZUC (one of the 4G/5G standards) still wrap strong nonlinear components around an LFSR.
Layer 3: The Mathematics
3.1 Linear recurrences
The output of an -bit LFSR satisfies
and the corresponding characteristic polynomial is .
3.2 An LFSR is "multiplying by "
Treat the state as an element of the quotient ring . Then each LFSR step amounts to multiplying by .
- If is irreducible, this quotient ring is the finite field (abstract algebra: is a field if and only if is irreducible);
- The period is the order of in the multiplicative group ;
- is a cyclic group of order . If happens to be a generator, the period reaches the maximum . Such an is called a primitive polynomial.
The number of primitive polynomials is (where is Euler's function), so they always exist, and there are plenty of them.
A worked example: (primitive)
The recurrence is , with initial state .
It repeats from bit 16, with period . These 15 bits contain 8 ones and 7 zeros, and a window of length 4 passes through each of the 15 nonzero 4-bit strings exactly once. These are typical properties of an "m-sequence".
By contrast: is irreducible but not primitive (it divides , so has order only ), and starting from the period is only . (Both checked with Python.)
3.3 Berlekamp–Massey: bits are enough
Suppose an attacker knows the register length and sees consecutive output bits. Then they can write down linear equations in and solve them over .
Recovering a 4-bit LFSR from 8 bits
Intercept the first 8 bits: . Let :
| Equation | Result | |
|---|---|---|
| 0 | , i.e. | |
| 1 | , i.e. | |
| 2 | , i.e. | |
| 3 | , i.e. |
This gives , that is . The attacker can now predict all future keystream.
The Berlekamp–Massey algorithm goes further: without knowing in advance, it finds the shortest LFSR generating a given sequence in time. This shortest length is called the sequence's linear complexity. An LFSR with a 128-bit key has linear complexity at most 128, and is seen through with just 256 bits of known plaintext.
Interestingly, this algorithm was originally invented for decoding Reed–Solomon and BCH codes.
3.4 How to fix it: add nonlinearity
- Nonlinear combination: merge the outputs of several LFSRs through a nonlinear Boolean function (such as the Geffe generator, though a poor design falls to correlation attacks);
- Nonlinear filtering: take values from several positions of one LFSR and feed them into a nonlinear function;
- Irregular stepping: use one LFSR to control whether another advances (the approach of A5/1).
The goal in every case is to push the linear complexity close to the sequence length, while avoiding statistical correlation between the output and any single LFSR.
Summary
| Layer | Key points |
|---|---|
| Intuition | Cells shift left and XOR feeds back; a few gates generate a long sequence |
| Algebra | Each step multiplying by in ; a primitive polynomial gives the maximum period |
| Weakness | Linear: bits solve the entire register (Berlekamp–Massey) |
| Reality | Excellent for GPS, scrambling and CRC; encryption must add nonlinearity, with A5/1 and CSS as cautionary tales |