Linearly recurrent sequences and LFSRs
Wildon's Weblog 2019-09-17
Part B of my Cipher Systems course is on stream ciphers. Modern stream ciphers, such as Trivium, are typically constructed by judiciously introducing non-linearity into a long-period linear stream cipher. The linear case is therefore still important.
By one mathematical definition, a Linear Feedback Shift Register (LFSR) is a function of the form
where the feedback function is linear. If
then we say that has taps
and that
is the width of
. The keystream generated by
for a key
is the sequence
, where each
for
is defined recursively by
Equivalently, for ,
The equation is close to the hardware implementation of an LFSR: fill
registers with the key
and then repeatedly step by putting the sum of the bits
for each
into the rightmost register, shifting the other registers to the left. (In hardware, these bits would be ‘tapped’ by physical wires.) This interpretation makes it clear that an LFSR of width
is invertible if and only if it taps the bit about to leave the register for good: that is
must be a tap.
The period of an invertible LFSR is the least
such that
. One way to find
uses that in the canonical basis of
, the matrix (acting on column vectors) representing
is the companion matrix of the polynomial
. Thus
if and only if
divides
. This may seem concise written here, but in practice it takes well over a lecture to remind students of matrices, find the matrix representing
and determine its minimal polynomial. All this adds up to a substantial diversion from cryptography; I fear it lost most students in my Cipher Systems course.
Instead, this year I’m going to give up on linear algebra and use the theory of linearly recurrent sequences and infinite power series. My plan is to subsume all or the required ring-theory into the following definition.
Definition. A polynomial annihilates a power series
if
is a polynomial.
For example, the LFSR of width with taps
generates the keystream
of period
. The corresponding power series is
Clearly the power series is annihilated by . But since
for
, it is also annihilated by
. Correspondingly,
More generally, the keystream generated by an LFSR with taps is annihilated by the feedback polynomial
. Indeed, the coefficient of
in the product is
, and so
is a polynomial of degree at most
. Hence the period of
is the minimum
such that
is divisible by
. (This is equivalent to the condition found before by taking reciprocal polynomials.)
One quickly gets further non-trivial results.
Keystream of maximum period
Given , we claim that the equation
defines a corresponding keystream. To avoid the need to define inverses in the ring of power series, let’s agree to interpret this equation as equivalent to
.
In turn, this is equivalent to the linear equations
for ; clearly these have a unique solution. In particular, there is a keystream such that
. (This holds even if the LFSR is not invertible.) It’s obvious that
is annihilated only by the multiples of
. In the invertible case we have seen that
divides
if and only if
is a multiple of the period
of the LFSR. Therefore there is a keystream of period
.
(For comparison, using the linear algebra method, one argues that the matrix representing an LFSR is cyclic and that, as is clear from the left-shift in the definition, the corresponding -module is generated by
. The period of the keystream for
is therefore the period of
. Again this is an argument that becomes far more long-winded when enough detail is included to make it accessible to students.)
Sum of two keystreams
Suppose, in any attempt to increase the linear complexity, we add the keystream of the LFSR of width
with taps
to the keystream with period
in the example above. The new keystream has period
and is annihilated by
It is therefore a keystream of the LFSR with taps . I plan to get students to find these taps by the slow method of solving the linear equations
, and then, I hope, impress them with how much faster things are with a bit more algebra.
(Incidentally one can have endless fun with the two competing conventions for taps. While less standard, this example shows that our choice in which is a natural fit for the annihilator method. It is also clearly best for the Berlekamp—Massey algorithm.)
Length of all keystreams
One computer demonstration that I think worked quite well last year used a Mathematica notebook to compute the lengths of all keystreams for all invertible LFSRs of width . I then showed that the keystreams of maximum possible period
came from the LFSRs whose minimal polynomial is primitive. Of course most, quite reasonably, didn’t know what ‘primitive’ meant, but they could see Mathematica did, and that there was some underlying pattern related to the factorization of the minimal polynomial.
Since the matrix representing an LFSR with taps in the canonical basis of
is the companion matrix of
, the corresponding
-module is
Switching to the reciprocal polynomial, the lengths of the keystreams are the orbit sizes of in its action on
. When
is irreducible and primitive, we get a single non-zero orbit. The general case is quite intricate, and stated in Theorem 6.33 in Lidl and Niederreiter Finite fields.
For this retelling, we need the following function. Given with
, let
. Thus the sequence
starts
and is the number of bits in the binary form of
.
In the next two results, let be an irreducible polynomial of degree
and let
be the order of
in the finite field
.
Proposition. Let . The orbits of
on
not contained in the unique maximal ideal
have size
and there are
of them.
Proof. Let be the complement of the maximal ideal. By hypothesis,
is divisible by
. Since
is odd,
is divisible by if and only if
. Therefore
has order
in its action on
. Since
, the elements of
are invertible. Hence if
then
mod
if and only if
mod
. It follows that all the orbits of
on
have size
. Since
has size
, the number of orbits is as claimed.
Theorem. The non-zero orbits of on
have sizes
for
. If
then the number of orbits of size
is
The number of orbits of size is
Proof. Let . Suppose that
. For each
such that
we have
and the count of orbits of size
in the previous proposition telescopes as required. A similar telescoping works to count the orbits of size
. Finally we count the zero orbit
and
orbits of size
, which together form the minimal ideal
isomorphic to the finite field
.
For example, take . Then
,
and the orbits of
have sizes
;
;
;
, where the semicolons indicate the split according to
used in the theorem, and the exponents indicate multiplicities. These orbit sizes form the multiset
.
Just for the next corollary, we define the product of two multisets to be the multiset obtained by multiplying the elements pairwise, respecting multiplicities. For example
Corollary. Let be an invertible LFSR with taps
. Let
be the irreducible factorization of its feedback polynomial. Then the multiset of orbit sizes of
is the product of the multisets of orbit sizes for each factor
, as given by the previous theorem.
Proof. This is routine from the Chinese Remainder Theorem.