Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/crypto/lfsr.py
4049 views
1
r"""
2
Linear feedback shift register (LFSR) sequence commands
3
4
Stream ciphers have been used for a long time as a source of
5
pseudo-random number generators.
6
7
S. Golomb [G]_ gives a list of three statistical properties a
8
sequence of numbers `{\bf a}=\{a_n\}_{n=1}^\infty`,
9
`a_n\in \{0,1\}`, should display to be considered
10
"random". Define the autocorrelation of `{\bf a}` to be
11
12
.. math::
13
14
C(k)=C(k,{\bf a})=\lim_{N\rightarrow \infty} {1\over N}\sum_{n=1}^N (-1)^{a_n+a_{n+k}}.
15
16
17
In the case where `{\bf a}` is periodic with period
18
`P` then this reduces to
19
20
.. math::
21
22
C(k)={1\over P}\sum_{n=1}^P (-1)^{a_n+a_{n+k}}.
23
24
25
Assume `{\bf a}` is periodic with period `P`.
26
27
28
- balance: `|\sum_{n=1}^P(-1)^{a_n}|\leq 1`.
29
30
- low autocorrelation:
31
32
.. math::
33
34
C(k)= \left\{ \begin{array}{cc} 1,& k=0,\\ \epsilon, & k\not= 0. \end{array} \right.
35
36
37
(For sequences satisfying these first two properties, it is known
38
that `\epsilon=-1/P` must hold.)
39
40
- proportional runs property: In each period, half the runs have
41
length `1`, one-fourth have length `2`, etc.
42
Moreover, there are as many runs of `1`'s as there are of
43
`0`'s.
44
45
46
A general feedback shift register is a map
47
`f:{\bf F}_q^d\rightarrow {\bf F}_q^d` of the form
48
49
.. math::
50
51
\begin{array}{c} f(x_0,...,x_{n-1})=(x_1,x_2,...,x_n),\\ x_n=C(x_0,...,x_{n-1}), \end{array}
52
53
54
where `C:{\bf F}_q^d\rightarrow {\bf F}_q` is a given
55
function. When `C` is of the form
56
57
.. math::
58
59
C(x_0,...,x_{n-1})=a_0x_0+...+a_{n-1}x_{n-1},
60
61
62
for some given constants `a_i\in {\bf F}_q`, the map is
63
called a linear feedback shift register (LFSR).
64
65
Example of a LFSR Let
66
67
.. math::
68
69
f(x)=a_{{0}}+a_{{1}}x+...+a_{{n}}{x}^n+...,
70
71
72
73
.. math::
74
75
g(x)=b_{{0}}+b_{{1}}x+...+b_{{n}}{x}^n+...,
76
77
78
be given polynomials in `{\bf F}_2[x]` and let
79
80
.. math::
81
82
h(x)={f(x)\over g(x)}=c_0+c_1x+...+c_nx^n+... \ .
83
84
85
We can compute a recursion formula which allows us to rapidly
86
compute the coefficients of `h(x)` (take `f(x)=1`):
87
88
.. math::
89
90
c_{n}=\sum_{i=1}^n {{-b_i\over b_0}c_{n-i}}.
91
92
93
94
The coefficients of `h(x)` can, under certain conditions on
95
`f(x)` and `g(x)`, be considered "random" from
96
certain statistical points of view.
97
98
Example: For instance, if
99
100
.. math::
101
102
f(x)=1,\ \ \ \ g(x)=x^4+x+1,
103
104
then
105
106
.. math::
107
108
h(x)=1+x+x^2+x^3+x^5+x^7+x^8+...\ .
109
110
The coefficients of `h` are
111
112
.. math::
113
114
\begin{array}{c} 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, \\ 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, ...\ . \end{array}
115
116
117
The sequence of `0,1`'s is periodic with period
118
`P=2^4-1=15` and satisfies Golomb's three randomness
119
conditions. However, this sequence of period 15 can be "cracked"
120
(i.e., a procedure to reproduce `g(x)`) by knowing only 8
121
terms! This is the function of the Berlekamp-Massey algorithm [M]_,
122
implemented as ``berlekamp_massey.py``.
123
124
.. [G] Solomon Golomb, Shift register sequences, Aegean Park Press,
125
Laguna Hills, Ca, 1967
126
127
.. [M] James L. Massey, "Shift-Register Synthesis and BCH Decoding."
128
IEEE Trans. on Information Theory, vol. 15(1), pp. 122-127, Jan
129
1969.
130
131
AUTHORS:
132
133
- Timothy Brock
134
135
Created 11-24-2005 by wdj. Last updated 12-02-2005.
136
"""
137
138
###########################################################################
139
# Copyright (C) 2006 Timothy Brock
140
# and William Stein <[email protected]>
141
#
142
# Distributed under the terms of the GNU General Public License (GPL)
143
#
144
# http://www.gnu.org/licenses/
145
###########################################################################
146
147
import copy
148
149
from sage.structure.all import Sequence
150
from sage.rings.all import is_FiniteField, Integer, PolynomialRing
151
152
def lfsr_sequence(key, fill, n):
153
r"""
154
This function creates an lfsr sequence.
155
156
INPUT:
157
158
159
- ``key`` - a list of finite field elements,
160
[c_0,c_1,...,c_k].
161
162
- ``fill`` - the list of the initial terms of the lfsr
163
sequence, [x_0,x_1,...,x_k].
164
165
- ``n`` - number of terms of the sequence that the
166
function returns.
167
168
169
OUTPUT: The lfsr sequence defined by
170
`x_{n+1} = c_kx_n+...+c_0x_{n-k}`, for
171
`n \leq k`.
172
173
EXAMPLES::
174
175
sage: F = GF(2); l = F(1); o = F(0)
176
sage: F = GF(2); S = LaurentSeriesRing(F,'x'); x = S.gen()
177
sage: fill = [l,l,o,l]; key = [1,o,o,l]; n = 20
178
sage: L = lfsr_sequence(key,fill,20); L
179
[1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0]
180
sage: g = berlekamp_massey(L); g
181
x^4 + x^3 + 1
182
sage: (1)/(g.reverse()+O(x^20))
183
1 + x + x^2 + x^3 + x^5 + x^7 + x^8 + x^11 + x^15 + x^16 + x^17 + x^18 + O(x^20)
184
sage: (1+x^2)/(g.reverse()+O(x^20))
185
1 + x + x^4 + x^8 + x^9 + x^10 + x^11 + x^13 + x^15 + x^16 + x^19 + O(x^20)
186
sage: (1+x^2+x^3)/(g.reverse()+O(x^20))
187
1 + x + x^3 + x^5 + x^6 + x^9 + x^13 + x^14 + x^15 + x^16 + x^18 + O(x^20)
188
sage: fill = [l,l,o,l]; key = [l,o,o,o]; n = 20
189
sage: L = lfsr_sequence(key,fill,20); L
190
[1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1]
191
sage: g = berlekamp_massey(L); g
192
x^4 + 1
193
sage: (1+x)/(g.reverse()+O(x^20))
194
1 + x + x^4 + x^5 + x^8 + x^9 + x^12 + x^13 + x^16 + x^17 + O(x^20)
195
sage: (1+x+x^3)/(g.reverse()+O(x^20))
196
1 + x + x^3 + x^4 + x^5 + x^7 + x^8 + x^9 + x^11 + x^12 + x^13 + x^15 + x^16 + x^17 + x^19 + O(x^20)
197
198
AUTHORS:
199
200
- Timothy Brock (2005-11): with code modified from Python
201
Cookbook, http://aspn.activestate.com/ASPN/Python/Cookbook/
202
"""
203
if not isinstance(key, list):
204
raise TypeError, "key must be a list"
205
key = Sequence(key)
206
F = key.universe()
207
if not is_FiniteField(F):
208
raise TypeError, "universe of sequence must be a finite field"
209
210
s = fill
211
k = len(fill)
212
L = []
213
for i in range(n):
214
s0 = copy.copy(s)
215
L.append(s[0])
216
s = s[1:k]
217
s.append(sum([key[i]*s0[i] for i in range(k)]))
218
return L
219
220
def lfsr_autocorrelation(L, p, k):
221
"""
222
INPUT:
223
224
225
- ``L`` - is a periodic sequence of elements of ZZ or
226
GF(2). L must have length = p
227
228
- ``p`` - the period of L
229
230
- ``k`` - k is an integer (0 k p)
231
232
233
OUTPUT: autocorrelation sequence of L
234
235
EXAMPLES::
236
237
sage: F = GF(2)
238
sage: o = F(0)
239
sage: l = F(1)
240
sage: key = [l,o,o,l]; fill = [l,l,o,l]; n = 20
241
sage: s = lfsr_sequence(key,fill,n)
242
sage: lfsr_autocorrelation(s,15,7)
243
4/15
244
sage: lfsr_autocorrelation(s,int(15),7)
245
4/15
246
247
AUTHORS:
248
249
- Timothy Brock (2006-04-17)
250
"""
251
if not isinstance(L, list):
252
raise TypeError, "L (=%s) must be a list"%L
253
p = Integer(p)
254
_p = int(p)
255
k = int(k)
256
L0 = L[:_p] # slices makes a copy
257
L0 = L0 + L0[:k]
258
L1 = [int(L0[i])*int(L0[i + k])/p for i in range(_p)]
259
return sum(L1)
260
261
def lfsr_connection_polynomial(s):
262
"""
263
INPUT:
264
265
266
- ``s`` - a sequence of elements of a finite field (F)
267
of even length
268
269
270
OUTPUT:
271
272
273
- ``C(x)`` - the connection polynomial of the minimal
274
LFSR.
275
276
277
This implements the algorithm in section 3 of J. L. Massey's
278
article [M]_.
279
280
EXAMPLE::
281
282
sage: F = GF(2)
283
sage: F
284
Finite Field of size 2
285
sage: o = F(0); l = F(1)
286
sage: key = [l,o,o,l]; fill = [l,l,o,l]; n = 20
287
sage: s = lfsr_sequence(key,fill,n); s
288
[1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0]
289
sage: lfsr_connection_polynomial(s)
290
x^4 + x + 1
291
sage: berlekamp_massey(s)
292
x^4 + x^3 + 1
293
294
Notice that ``berlekamp_massey`` returns the reverse
295
of the connection polynomial (and is potentially must faster than
296
this implementation).
297
298
AUTHORS:
299
300
- Timothy Brock (2006-04-17)
301
"""
302
# Initialization:
303
FF = s[0].base_ring()
304
R = PolynomialRing(FF, "x")
305
x = R.gen()
306
C = R(1); B = R(1); m = 1; b = FF(1); L = 0; N = 0
307
308
while N < len(s):
309
if L > 0:
310
r = min(L+1,C.degree()+1)
311
d = s[N] + sum([(C.list())[i]*s[N-i] for i in range(1,r)])
312
if L == 0:
313
d = s[N]
314
if d == 0:
315
m += 1
316
N += 1
317
if d > 0:
318
if 2*L > N:
319
C = C - d*b**(-1)*x**m*B
320
m += 1
321
N += 1
322
else:
323
T = C
324
C = C - d*b**(-1)*x**m*B
325
L = N + 1 - L
326
m = 1
327
b = d
328
B = T
329
N += 1
330
return C
331
332