# 16.6

Module: `sage.crypto.stream_cipher`

Class: `LFSRCipher`

class LFSRCipher
LFSR cipher class
 LFSRCipher( self, parent, poly, IS)
Create a LFSR cipher.

```INPUT: Parent, connection polynomial, and initial state
```

```sage: FF = FiniteField(2)
sage: P.<x> = PolynomialRing(FF)
sage: E = LFSRCryptosystem(FF)
sage: E
LFSR cryptosystem over Finite Field of size 2
sage: IS = [ FF(a) for a in [0,1,1,1,0,1,1] ]
sage: g = x^7 + x + 1
sage: e = E((g,IS))
sage: B = BinaryStrings()
sage: m = B.encoding("THECATINTHEHAT")
sage: e(m)
001000110111101011101010101000110000000011010001010101110000101111001001000
0011111100100100011001101101000001111
sage: FF = FiniteField(2)
sage: P.<x> = PolynomialRing(FF)
sage: LFSR = LFSRCryptosystem(FF)
sage: e = LFSR((x^2+x+1,[FF(0),FF(1)]))
sage: B = e.domain()
sage: m = B.encoding("The cat in the hat.")
sage: e(m)
001110011101111010111110010011011101010110111010000110011001011010110010000
000111001011010101111000001011101001111111011000001011101011110101111010000
11
sage: m == e(e(m))
True
```

TESTS:

```sage: FF = FiniteField(2)
sage: P.<x> = PolynomialRing(FF)
sage: E = LFSRCryptosystem(FF)
True
```

Functions: connection_polynomial, initial_state

 connection_polynomial( self)
The connection polynomial defining the LFSR of the cipher.

 initial_state( self)
The initial state of the LFSR cipher.

Special Functions: __call__

Class: `ShrinkingGeneratorCipher`

class ShrinkingGeneratorCipher
A shrinking generator cipher.
 ShrinkingGeneratorCipher( self, parent, e1, e2)
Create a shrinking generator cipher.

```INPUT: Parent, key stream cipher, decimation cipher
```

```       sage: FF = FiniteField(2)
sage: P.<x> = PolynomialRing(FF)
sage: LFSR = LFSRCryptosystem(FF)
sage: IS_1 = [ FF(a) for a in [0,1,0,1,0,0,0] ]
sage: e1 = LFSR((x^7 + x + 1,IS_1))
sage: IS_2 = [ FF(a) for a in [0,0,1,0,0,0,1,0,1] ]
sage: e2 = LFSR((x^9 + x^3 + 1,IS_2))
sage: E = ShrinkingGeneratorCryptosystem()
sage: e = E((e1,e2))
sage: e
((x^7 + x + 1, [0, 1, 0, 1, 0, 0, 0]), (x^9 + x^3 + 1, [0, 0, 1, 0, 0, 0,
1, 0, 1]))
```

Functions: decimating_cipher, keystream_cipher

 decimating_cipher( self)
The LFSR cipher generating the decimating key stream.

 keystream_cipher( self)
The LFSR cipher generating the output key stream.

Special Functions: __call__

 __call__( self, M, [mode=ECB])

```sage: FF = FiniteField(2)
sage: P.<x> = PolynomialRing(FF)
sage: LFSR = LFSRCryptosystem(FF)
sage: IS_1 = [ FF(a) for a in [0,1,0,1,0,0,0] ]
sage: e1 = LFSR((x^7 + x + 1,IS_1))
sage: IS_2 = [ FF(a) for a in [0,0,1,0,0,0,1,0,1] ]
sage: e2 = LFSR((x^9 + x^3 + 1,IS_2))
sage: E = ShrinkingGeneratorCryptosystem()
sage: e = E((e1,e2))
sage: B = BinaryStrings()
sage: m = B.encoding("THECATINTHEHAT")
sage: c = e(m)
sage: c.decoding()
'\xac\xfa\xfd\xc6\xa7\xe5\x16\x8f\xa2Q\xb8\xb7\xbe\xab'
sage: e(e(m)) == m
True
sage: m.decoding()
'THECATINTHEHAT'
```