Linear-feedback shift register
In computing, a linear-feedback shift register (LFSR) is a shift register whose input bit is a linear function of its previous state.
≈ 7 min read
Linear-feedback shift register. encyclopegia.org, snapshot enwiki-001375542776. /a/linear-feedback-shift-register
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
In computing, a linear-feedback shift register (LFSR) is a shift register whose input bit is a linear function of its previous state. The most commonly used linear function of single bits is exclusive-or (XOR). Thus, an LFSR is most often a shift register whose input bit is driven by the XOR of some bits of the overall shift register value.
The initial value of the LFSR is called the seed, and because the operation of the register is deterministic, the stream of values produced by the register is completely determined by its current (or previous) state. Likewise, because the register has a finite number of possible states, it must eventually enter a repeating cycle.
However, an LFSR with a well-chosen feedback function can produce a sequence of bits that appears random and has a very long cycle. Applications of LFSRs include generating pseudo-random numbers, pseudo-noise sequences, fast digital counters, and whitening sequences. Both hardware and software implementations of LFSRs are common.
The mathematics of a cyclic redundancy check, used to provide a quick check against transmission errors, are closely related to those of an LFSR. In general, the arithmetics behind LFSRs makes them very elegant as an object to study and implement. One can produce relatively complex logics with simple building blocks.
However, other methods, that are less elegant but perform better, should be considered as well. There can be more than one maximum-length tap sequence for a given LFSR length. Also, once one maximum-length tap sequence has been found, another automatically follows.
If the tap sequence in an n-bit LFSR is , where the 0 corresponds to the x 0 = 1 term, then the corresponding "mirror" sequence is . So the tap sequence has as its counterpart . Both give a maximum-length sequence.
An example in C is below: include unsigned lfsr_fib(void) { uint16_t start_state = 0xACE1u; /* Any nonzero start state will work. */ uint16_t lfsr = start_state; uint16_t bit; /* Must be 16-bit to allow bit > 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 5)) & 1u; lfsr = (lfsr >> 1) | (bit If a fast parity or popcount operation is available, the feedback bit can be computed more efficiently as the dot product of the register with the characteristic polynomial: bit = parity(lfsr & 0x002Du); , or equivalently bit = popcnt(lfsr & 0x002Du) /* & 1u */; . (The & 1u turns the popcnt into a true parity function, but the bitshift later bit makes higher bits irrelevant.)
If a rotation operation is available, the new state can be computed as lfsr = rotateright((lfsr & ~1u) | (bit & 1u), 1); , or equivalently lfsr = rotateright(((bit ^ lfsr) & 1u) ^ lfsr, 1); This LFSR configuration is also known as standard, many-to-one or external XOR gates. The alternative Galois configuration is described in the next section. start_state = 1 > 1) ^ (lfsr >> 3) ^ (lfsr >> 12)) & 1 lfsr = (lfsr >> 1) | (bit Where a register of 16 bits is used and the xor tap at the fourth, 13th, 15th and sixteenth bit establishes a maximum sequence length. include unsigned lfsr_galois(void) { uint16_t start_state = 0xACE1u; /* Any nonzero start state will work. */ uint16_t lfsr = start_state; unsigned period = 0; do { ifndef LEFT unsigned lsb = lfsr & 1u; /* Get LSB (i.e., the output bit). */ lfsr >>= 1; /* Shift register */ if (lsb) /* If the output bit is 1, */ lfsr ^= 0xB400u; /* apply toggle mask. */ else unsigned msb = (int16_t) lfsr The branch if (lsb) lfsr ^= 0xB400u; can also be written as lfsr ^= (-lsb) & 0xB400u; which may produce more efficient code on some compilers.
In addition, the left-shifting variant may produce even better code, as the msb is the carry from the addition of lfsr to itself (equivalent to, and often generated from, the left shift). include uint64_t prsg63(uint64_t lfsr) { lfsr = lfsr > 32; lfsr = lfsr > 32; return lfsr; } Binary Galois LFSRs like the ones shown above can be generalized to any q-ary alphabet {0, 1, ..., q − 1} (e.g., for binary, q = 2, and the alphabet is simply {0, 1}). In this case, the exclusive-or component is generalized to addition modulo-q (note that XOR is addition modulo 2), and the feedback bit (output bit) is multiplied (modulo-q) by a q-ary value, which is constant for each specific tap point.
Note that this is also a generalization of the binary case, where the feedback is multiplied by either 0 (no feedback, i.e., no tap) or 1 (feedback is present). Given an appropriate tap configuration, such LFSRs can be used to generate Galois fields for arbitrary prime values of q. include unsigned lfsr_xorshift(void) { uint16_t start_state = 0xACE1u; /* Any nonzero start state will work. */ uint16_t lfsr = start_state; unsigned period = 0; do { // 7,9,13 triplet from http://www.retroprogramming.com/2017/07/xorshift-pseudorandom-numbers-in-z80.html lfsr ^= lfsr >> 7; lfsr ^= lfsr > 13; ++period; } while (lfsr != start_state); return period; } \begin{pmatrix} a_{k} \\ a_{k+1} \\ a_{k+2} \\ \vdots \\ a_{k+n-1} \end{pmatrix} = \begin{pmatrix} 0 & 1 & 0 & \cdots & 0 \\ 0 & 0 & 1 & \ddots & \vdots \\ \vdots & \vdots & \ddots & \ddots & 0\\ 0 & 0 & \cdots & 0& 1\\ c_{0} & c_{1} & \cdots & \cdots & c_{n-1} \end{pmatrix} \begin{pmatrix} a_{k-1} \\ a_{k} \\ a_{k+1} \\ \vdots \\ a_{k+n-2} \end{pmatrix} = \begin{pmatrix} 0 & 1 & 0 & \cdots & 0 \\ 0 & 0 & 1 & \ddots & \vdots \\ \vdots & \vdots & \ddots & \ddots & 0\\ 0 & 0 & \cdots & 0& 1\\ c_{0} & c_{1} & \cdots & \cdots & c_{n-1} \end{pmatrix}^k \begin{pmatrix} a_0 \\ a_1 \\ a_2 \\ \vdots \\ a_{n-1} \end{pmatrix} Matrix for the corresponding Galois form is : \begin{pmatrix} c_{n-1} & 1 & 0 & \cdots & 0 \\ c_{n-2} & 0 & 1 & \ddots & \vdots \\ \vdots & \vdots & \ddots & \ddots & 0\\ c_1 & 0 & \cdots & 0& 1\\ c_0 & 0 & \cdots & \cdots & 0 \end{pmatrix} For a suitable initialisation, a'_i=\sum_{j=0}^ia_{i-j}c_{i},\ 0\leq i the top coefficient of the column vector : \begin{pmatrix} c_{n-1} & 1 & 0 & \cdots & 0 \\ c_{n-2} & 0 & 1 & \ddots & \vdots \\ \vdots & \vdots & \ddots & \ddots & 0\\ c_1 & 0 & \cdots & 0& 1\\ c_0 & 0 & \cdots & \cdots & 0 \end{pmatrix}^k \begin{pmatrix} a'_0 \\ a'_1 \\ a'_2 \\ \vdots \\ a'_{n-1} \end{pmatrix} gives the term of the original sequence.
These forms generalize naturally to arbitrary fields. Maximal Length LFSR table with length from 2 to 67.
Pseudo-Random Number Generation Routine for the MAX765x Microprocessor https://www.ece.ualberta.ca/~elliott/ee552/studentAppNotes/1999f/Drivers_Ed/lfsr.html http://www.quadibloc.com/crypto/co040801.htm Simple explanation of LFSRs for Engineers Feedback terms General LFSR Theory An implementation of LFSR in VHDL. Simple VHDL coding for Galois and Fibonacci LFSR. mlpolygen: A Maximal Length polynomial generator LFSR and Intrinsic Generation of Randomness: Notes From NKS
Text derived from https://en.wikipedia.org/w/index.php?oldid=1375542776 under CC BY-SA 4.0; modifications have been made.