Computation

A memory of four bits

A register holding four bits, shifting them along and adding two of them back, runs through all fifteen nonzero states before it repeats. Which two are added back is a question about a polynomial, and getting it wrong costs fourteen of the fifteen.

Worth reading first: Every necklace, in order · The field with four elements.

The two constructions below this rung both hold the whole sequence in mind while building it — a graph with 2n12^{n-1} vertices, or a list of necklaces in alphabetical order. Neither is a machine. This one is: four flip-flops, one exclusive-or gate, and a clock.

A 4-bit register that visits all 15 nonzero states. The first 15 states of a 4-bit linear feedback shift register with taps at 4 and 1, with the bit that leaves the register at each step; the output shows every nonzero window of 4 bits exactly once.
Fig. 1 A four-bit register with taps at positions four and one, started at 0001. Each step shifts the bits along and puts the sum of the two tapped bits back at the end. It visits all fifteen nonzero states and then repeats, and the bit leaving the register spells a sequence in which every nonzero four-bit window appears exactly once.

The register holds nn bits. At each tick it shifts them along by one, drops the bit at the end, and puts a new bit in at the start: the sum, modulo two, of the bits at a chosen few positions. The chosen positions are the taps, and everything about the machine’s behaviour is decided by them.

A 3-bit register that visits all 7 nonzero states. The first 7 states of a 3-bit linear feedback shift register with taps at 3 and 1, with the bit that leaves the register at each step; the output shows every nonzero window of 3 bits exactly once.
Fig. 2 The smallest case worth drawing: three bits, taps at three and one, seven states and an output of seven letters holding every nonzero three-bit word. Writing one more zero into the run of two gives 00010111, which is the same eight-letter sequence the necklace list produces — arrived at by a machine rather than by an alphabetical order.

What the taps decide

There are 2n2^n possible states and the all-zero state is a trap — every tap sums to zero there, so the register stays put forever. The best a register can do is visit the other 2n12^n - 1 states in one cycle, and a register that does is called maximum-length.

Most tap choices do far worse. A four-bit register has fifteen useful tap sets and only two of them are maximum-length; the rest split the fifteen nonzero states into shorter cycles, and a register that has fallen into a cycle of length three produces a sequence with a period of three.

The rule for which taps work is not about the register at all. Write the taps as a polynomial: taps at positions nn and jj become xn+xj+1x^n + x^j + 1. The recurrence is then multiplication by xx in the ring of polynomials modulo that polynomial, and the state cycles through every nonzero element exactly when xx generates the multiplicative group — which is what primitive means, and which requires the polynomial to be irreducible first.

The register is running arithmetic in a field of 2n2^n elements, and the state is a field element being multiplied by xx over and over.

The non-zero elements of GF(16) as the powers of one of them. A ring of the field's non-zero elements in the order the powers of a primitive element produce them, beside a table of exponents.
Fig. 3 The same arithmetic without the hardware: each nonzero element of a field of sixteen written as a power of one primitive element, all fifteen of them before the powers return to one. The register’s state at step tt is that element’s tt-th power, and the fifteen states are these fifteen elements written in the basis the register’s bits provide.

That the multiplicative group of a finite field is cyclic is the theorem doing all the work, and it is the same theorem that makes every nonzero residue a power of a single one. A generator exists; the question is only whether xx happens to be one, and for each degree there are φ(2n1)/n\varphi(2^n-1)/n primitive polynomials to choose from.

Every window but one

Run the register and read the bit that falls off the end at each step. That output has period 2n12^n - 1, and its windows are the states themselves: the window of nn consecutive output bits starting at step tt is the state at step tt, read off.

So the output contains every nonzero window of width nn exactly once and the all-zero window never. That is a de Bruijn sequence with exactly one word missing, and the missing word is the one the machine cannot represent.

The repair is a single character. In the output there is exactly one run of n1n-1 zeros — it is the state 00001000\ldots01 passing through — and writing one more zero into it produces a cyclic sequence of length 2n2^n in which the all-zero window now appears once and nothing else has changed. Every other window is untouched, because the only windows that see the edit are the ones overlapping the run, and those were the ones spelling 0n110^{n-1}1 and 10n110^{n-1}, which shift by one place each.

A 5-bit register that visits all 31 nonzero states. The first 16 states of a 5-bit linear feedback shift register with taps at 5 and 2, with the bit that leaves the register at each step; the output shows every nonzero window of 5 bits exactly once.
Fig. 4 Five bits, taps at five and two: thirty-one states, and an output whose thirty-one windows are the thirty-one nonzero five-bit words. The patched sequence has thirty-two letters and is a de Bruijn sequence of order five, checked window by window against the same test the graph construction is put through.

The result is a de Bruijn sequence produced by nn bits of memory and a handful of gates. Nothing is stored, nothing is searched, and the sequence can be generated indefinitely from any point in it — which is the property that makes the construction useful rather than merely elegant.

The sequence looks random and is not

A maximum-length sequence passes the tests one would apply to a coin.

Balance. In each period there are 2n12^{n-1} ones and 2n112^{n-1}-1 zeros — as close to equal as an odd-length sequence can be, and the imbalance is exactly the missing all-zero state.

Runs. The runs of consecutive equal bits are distributed as they would be for a fair coin: half the runs have length one, a quarter have length two, and so on down to the single run of n1n-1 zeros and the single run of nn ones.

Autocorrelation. Comparing the sequence with any nontrivial shift of itself gives agreement in exactly 2n112^{n-1}-1 places and disagreement in 2n12^{n-1} — a correlation of 1/(2n1)-1/(2^n-1), the same for every shift. This is the sharpest of the three: the sequence is uncorrelated with every shift of itself by the same tiny amount, which is a stronger and more uniform property than a random sequence would have.

The autocorrelation property is worth a sentence more, because it is the one that gets these sequences used. A signal correlating sharply with itself at zero shift and almost not at all at every other shift is exactly what a ranging system needs: transmit the sequence, correlate the echo against every shift, and the shift that spikes is the delay. The sequence is doing the work a single sharp pulse would do, spread over a long interval, so the transmitter never needs high peak power. That is why maximum-length sequences are in satellite navigation, in radar, and in the impulse-response measurement of a room.

Golomb collected these as the postulates a pseudorandom sequence ought to satisfy, and maximum-length sequences satisfy them exactly rather than on average. That is the tell.

Which generators visit all 16 values. A table of multipliers and increments with the three conditions that decide whether the generator has the full period, beside the period found by running it out.
Fig. 5 The same question asked of a different family of recurrences: which multipliers give a linear congruential generator its full period, and which give a short one. The criterion is a condition on the arithmetic rather than on the output, exactly as primitivity is here — and in both cases a generator with the wrong constants produces a short cycle that looks fine until it repeats.
1500 triples from a generator, on 15 planes. Consecutive triples of outputs of a linear congruential generator plotted in a cube, seen from an angle that shows them lying on a small number of parallel planes.
Fig. 6 What a linear recurrence looks like when its output is plotted rather than tested. Consecutive triples from a linear congruential generator lie on a small family of parallel planes, and no statistical test of the individual values reports it. The same kind of structure is present in a maximum-length sequence and is found by a different question — not are the bits balanced but what recurrence generates them.

And a sequence that satisfies them exactly is predictable. Given any 2n2n consecutive output bits, the taps can be recovered by solving nn linear equations, after which the entire past and future of the sequence is determined. The linear complexity of a maximum-length sequence is nn — the smallest register that can produce it is the one that did — and the Berlekamp–Massey algorithm finds it from 2n2n bits in time O(n2)O(n^2).

This is the same lesson the planes a recurrence cannot leave teaches about linear congruential generators, in a different disguise. A generator whose next value is a linear function of the previous ones has a structure that shows up as soon as the right question is asked, and passing statistical tests is not evidence of the absence of structure. It is evidence about which tests were run.

There is a second structural fact the tests cannot see, and it is the reason the linear complexity is exactly nn. The sequence satisfies a linear recurrence of order nn — that is what the register is — so any nn consecutive bits determine the next one, and a recurrence with a short description is compressible however random its output looks. Kolmogorov’s notion of randomness is the absence of a short description, and by that standard a maximum-length sequence of length four billion is not random at all: it is thirty-two bits of state and two tap positions.

Where each construction is the right one

Three constructions now produce the same kind of object, and they are good at different things.

The Eulerian walk produces any of them and needs the whole graph. It is the existence proof, and it is what shows the object exists for every alphabet and every order.

The necklace list produces the alphabetically first, in constant time per letter and O(n)O(n) space, for any alphabet. It is what to use when the sequence is wanted as data.

The shift register produces one particular sequence, from nn bits of state, and can be started anywhere in it and run forward or backward. It is what to use when the sequence is wanted as a process, and it is the only one of the three that a circuit can be.

The register’s other distinguishing property is the one the rung below flagged: locating a window. In a maximum-length sequence the window at step tt is the field element xtx^t, so finding where a given window sits means solving xt=wx^t = w for tt — a discrete logarithm in a field of 2n2^n elements. For small nn that is a table lookup; for large nn it is a problem believed to be hard, and the whole of a branch of cryptography rests on it being so.

So the same sequence is cheap to generate and expensive to index, and which of those is the feature depends entirely on the application. A position-encoding strip wants both: cheap to print, and indexable by the reader — which is why such strips use small nn and a stored table rather than the arithmetic.

What a wrong tap set does

It is worth seeing the failure, because it is quiet.

Take a four-bit register and tap positions four and two instead of four and one. The polynomial is x4+x2+1x^4 + x^2 + 1, which factors as (x2+x+1)2(x^2+x+1)^2 — reducible, so certainly not primitive. Started at 0001 the register visits six states and returns; started elsewhere it visits three, or one. The fifteen nonzero states break into cycles of six, six and three.

Nothing about the register announces this. It clocks, it produces bits, the bits are not obviously patterned, and the failure shows only when the output is watched for long enough to repeat — six bits, in a machine whose designer expected fifteen. A register of width thirty-two with badly chosen taps can have a period of a few thousand while the designer expects four billion, and the difference is invisible in any short sample.

That is the practical reason primitivity is checked by arithmetic rather than by running the machine. Running it settles the question only after a full period, which is exactly the quantity in doubt, and a short period is discovered by the thing that goes wrong rather than by the test.

The same shape of error appears wherever a recurrence is trusted for its period: a modulus and multiplier that fail one of the conditions in the criterion for full period give a generator that works and repeats early, and the symptom arrives long after the decision that caused it.

What the taps cost to find

A last practical note, because it is a good example of a search that looks harder than it is.

To find a maximum-length register of width nn, one needs a primitive polynomial of degree nn over the two-element field. There are φ(2n1)/n\varphi(2^n - 1)/n of them, which for n=32n = 32 is about 67 million out of 2322^{32} candidate polynomials — roughly one in sixty. So random search finds one quickly, provided each candidate can be tested quickly.

Testing is where the work is, and it needs the factorisation of 2n12^n - 1: a polynomial is primitive when xx has order exactly 2n12^n-1, which is checked by verifying that x(2n1)/p1x^{(2^n-1)/p} \ne 1 for each prime pp dividing 2n12^n-1. So the difficulty of finding a good register of width nn is the difficulty of factoring one specific number — and for the widths where 2n12^n - 1 is a Mersenne prime, there is nothing to factor and every irreducible polynomial of that degree is primitive.

That is a pleasing inversion. The primes that are one less than a power of two are a subject with no obvious application, and they turn out to be exactly the register widths for which the design problem disappears.

What the pictures cannot show

The register is drawn as a table of states, not as a circuit. What a circuit would add is the gate count and the propagation delay, which are the reasons the construction is used and are facts about hardware rather than about the sequence.

The statistical properties are described and not measured. The balance, run and correlation statements are theorems about maximum-length sequences; the figures here show one register at four bits and one at five, where the statistics are visible but the sample is far too small to be evidence.

The tap search is not run. The claim that about one polynomial in sixty is primitive at degree thirty-two is arithmetic, and generating sixty-seven million polynomials is not something a figure does.

And the field picture is a different notation for the same object. The powers of a primitive element and the states of the register are the same fifteen things written two ways; the figure showing both does not prove they correspond, it displays the correspondence the text argues for.

Where the ladder goes next

The rung above takes the property into two dimensions. An array can be printed so that every small rectangular patch of it appears exactly once — a de Bruijn torus — and a reader that sees a patch knows where on the page it is. The one-dimensional constructions do not simply lift: the counting is tighter, the existence conditions are more delicate, and a row of a torus is generally not a de Bruijn sequence at all.

Above that: which other families of objects admit such a cycle. Words are the easy case because the transition structure is a graph with equal degrees; subsets and permutations are not, and the question of which admit a universal cycle has a counting obstruction and a good deal of open ground.

What is worth carrying away

A machine that produces a sequence and a description of that sequence are different objects, and knowing which one is in hand decides what questions are easy.

The register makes the sequence cheap to produce and cheap to continue from anywhere, and it makes the sequence’s position hard to determine. The necklace list makes the position obvious and continuation from the middle awkward. Both produce de Bruijn sequences and neither is a better construction than the other. The useful question about a construction is not whether it works but which of the object’s properties it makes visible — and here the answer is decided, both times, by what has to be held in memory while it runs.

What links here

Computed from the collection, not written here: the essays that point at this one.

Reads more easily once this is understood

Essays that name this one as worth reading first.

Shares its objects with

Essays that name at least two of the same things, and that neither author linked.

Named objects

A dashed tag is an object no other essay names yet.

De bruijn sequenceDiscrete logarithmFinite fieldGroupPeriodicityPolynomialPrimitive elementPseudorandomnessRecurrenceShift register