Caloogy Article
中文

Linear Feedback Shift Registers: Primitive Polynomials over F2[x]\mathbb{F}_2[x] and Stream Ciphers

Date
May 5, 2025
Series
Cryptography
Reading
5 min
Topics
LFSRStream cipherPrimitive polynomialFinite field

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 F2\mathbb{F}_2, an nn-bit register reaches the maximum period of 2n−12^n - 1. But it is linear: after seeing just 2n2n 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:

  1. The intuition: a row of cells passing a message along
  2. Real-world use: GPS, Wi-Fi scrambling, CRC, and the failures of GSM and DVD encryption
  3. 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:

  1. The leftmost cell "spits out" its digit as output;
  2. All cells shift left by one;
  3. The cell left empty on the right is filled with the XOR of certain cells.

new value

s0

Output

s1

s2

s3

⊕

In the diagram above, the new value =s0⊕s1= s_0 \oplus s_1. 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 24=162^4 = 16 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 F2[x]\mathbb{F}_2[x] 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 nn-bit LFSR satisfies

st+n=c0st+c1st+1+⋯+cn−1st+n−1(mod2),s_{t+n} = c_0 s_t + c_1 s_{t+1} + \cdots + c_{n-1} s_{t+n-1} \pmod 2,

and the corresponding characteristic polynomial is f(x)=xn+cn−1xn−1+⋯+c1x+c0∈F2[x]f(x) = x^n + c_{n-1}x^{n-1} + \cdots + c_1 x + c_0 \in \mathbb{F}_2[x].

3.2 An LFSR is "multiplying by xx"

Treat the state as an element of the quotient ring F2[x]/(f(x))\mathbb{F}_2[x]/(f(x)). Then each LFSR step amounts to multiplying by xx.

The number of primitive polynomials is φ(2n−1)/n\varphi(2^n - 1)/n (where φ\varphi is Euler's function), so they always exist, and there are plenty of them.

A worked example: f(x)=x4+x+1f(x) = x^4 + x + 1 (primitive)

The recurrence is st+4=st+st+1s_{t+4} = s_t + s_{t+1}, with initial state 00010001.

0 0 0 1 0 0 1 1 0 1 0 1 1 1 1⏟15 bits 0 0 0 1 ⋯\underbrace{0\,0\,0\,1\ 0\,0\,1\,1\ 0\,1\,0\,1\ 1\,1\,1}_{15\ \text{bits}}\ 0\,0\,0\,1\ \cdots

It repeats from bit 16, with period 15=24−115 = 2^4 - 1. 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: f(x)=x4+x3+x2+x+1f(x) = x^4 + x^3 + x^2 + x + 1 is irreducible but not primitive (it divides x5−1x^5 - 1, so xx has order only 55), and starting from 00010001 the period is only 55. (Both checked with Python.)

3.3 Berlekamp–Massey: 2n2n bits are enough

Suppose an attacker knows the register length nn and sees 2n2n consecutive output bits. Then they can write down nn linear equations in c0,…,cn−1c_0, \dots, c_{n-1} and solve them over F2\mathbb{F}_2.

Recovering a 4-bit LFSR from 8 bits

Intercept the first 8 bits: s0…s7=0 0 0 1 0 0 1 1s_0 \dots s_7 = 0\,0\,0\,1\,0\,0\,1\,1. Let st+4=c0st+c1st+1+c2st+2+c3st+3s_{t+4} = c_0 s_t + c_1 s_{t+1} + c_2 s_{t+2} + c_3 s_{t+3}:

tt Equation Result
0 s4=c0⋅0+c1⋅0+c2⋅0+c3⋅1s_4 = c_0 \cdot 0 + c_1 \cdot 0 + c_2 \cdot 0 + c_3 \cdot 1, i.e. 0=c30 = c_3 c3=0c_3 = 0
1 s5=c2⋅1+c3⋅0s_5 = c_2 \cdot 1 + c_3 \cdot 0, i.e. 0=c20 = c_2 c2=0c_2 = 0
2 s6=c1⋅1+c2s4+c3s5s_6 = c_1 \cdot 1 + c_2 s_4 + c_3 s_5, i.e. 1=c11 = c_1 c1=1c_1 = 1
3 s7=c0⋅1+c1s4+c2s5+c3s6s_7 = c_0 \cdot 1 + c_1 s_4 + c_2 s_5 + c_3 s_6, i.e. 1=c01 = c_0 c0=1c_0 = 1

This gives st+4=st+st+1s_{t+4} = s_t + s_{t+1}, that is f(x)=x4+x+1f(x) = x^4 + x + 1. The attacker can now predict all future keystream.

The Berlekamp–Massey algorithm goes further: without knowing nn in advance, it finds the shortest LFSR generating a given sequence in O(N2)O(N^2) 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

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 xx in F2[x]/(f)\mathbb{F}_2[x]/(f); a primitive polynomial gives the maximum period 2n−12^n - 1
Weakness Linear: 2n2n 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