## 27 March 2018

### scrambling and descrambling

(The polynomial theory which scramblers are based on is beyond the scope of this post, for those who want to deepen, google offers a lot of documentation about it. The aim is just to show their operation and the results obtained by manipulating an incoming stream of data with a scrambler, say a little introduction to this topic.)
In telecommunications a scrambler, also referred to as a randomizer, is a block that manipulates a data stream before transmitting. The manipulations are reversed by a descrambler at the receiving side. A scrambler can be placed just before a FEC coder, or it can be placed after the FEC, just before the modulator to give the transmitted data useful engineering properties as to reduce the length of consecutive 0s or 1s [1] (long sequences of 0s or 1s can cause transmission synchronization problems at receive modem).
In brief, scramblers are often constructed using linear-feedback shift registers (LFSRs) which consist of clocked storage elements (say "registers") and a feedback network and are defined similarly by a polynomial: the number N of registers gives the degree of the polynomial, the "taps" in the feedback network are modulo-2 additions (equivalent to exclusive-OR, or XOR) and give the used monomial with their relative degree. The registers are initially pre-loaded to the 0 state. The schematic in Figure 1 shows the so-called multiplicative (or "self-synchronizing") scrambler.

 Fig. 1 - scrambler/descrambler schematic
As example, the schematic in Figure 2 shows a  x^8+x^5+x+1 scrambler (for simplicity the connections between the registers are not indicated).

 Fig. 2 - x^8 + x^5 +x + 1 scrambler

That said, removing the scrambler from a demodulated stream (if scrambled), offers some more chances to understand the original data format and allows to take a step forward in signals analysis. That's why I coded some functions in LUA to study the operation of scramblers and the examples I post here refer to the scrambler described by the plynomial x^10+x+1 and depicted in Figure 3

 Fig. 3 - x^10+x+1 scrambler

A good way to observe the "randomizer" effect of a scrambler is to use an input bitstream composed of all 1s, the operation of the x^10+x+1 scrambler is illustrated in Figure 4. The function I wrote also prints out the scrambler and descrambler tables which report at each step (clock) the values of the input and output bits and the internal state of the registers.

 Fig. 4
The scrambled stream just appears as a random sequence of 1s and 0s: this means that what looks like a ciphertext could actually be a scrambled plain text. It's interesting to note the first ten bits of the scrambled stream: in this case the first "1" takes ten clock cycles to pass through the descrambler and reach the last register, during this time the last register output remains to zero therefore the two XORs in the feedback path produce the sequence "1010101010".
The initial part of the descrambler table is shown in Figure 5.

 Fig. 5

An example with a ASCII text stream is shown in figures 6,7

 Fig.6
 Fig.7
Figure 8 shows the first thirty steps of the scrambler and descrambler tables, note that the two set of registers assume the same states.

 Fig.8
Another example of the use of  the x^10+x+1 descrambler can be observed in radioscanner forum: here a GFSK transmission has preamble and data blocks wich are scrambled using two different polynomials (x^10+x+1 and x^11+x^9+1).

 Fig. 9
Removing the tap from the first register the scrambler x^10+1 is obtained: the feedback simply consists of the modulo-2 add between the input bit and the output bit. Notice how the first 10 bits of the scrambled stream follow those of the input stream: this happens because during the first 10 clocks the output remains at zero and is XORed with the input.

 Fig. 10 - x^10+1 scrambler
 Fig. 11

Quoting wikipedia "a scrambler has nothing to do with encrypting as its intent is not to render the message unintelligible", anyway scramblers are also used in the stream ciphers: in this case the initial states of the registers are actually the secret keys.