-
Notifications
You must be signed in to change notification settings - Fork 0
/
bm-lfsr-solver.py
executable file
·237 lines (201 loc) · 6.11 KB
/
bm-lfsr-solver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env python3
import itertools as it
import functools as ft
import operator as op
# step an LFSR
def step(l, s):
return ft.reduce(
op.__xor__,
[l_ & s_ for l_, s_ in zip(l[1:], s)],
0)
# generate a random LFSR
def randomlfsr(n):
import random
# generate a random LFSR
l = [0] + [random.getrandbits(1) for _ in range(n)]
# generate a random initial state
s = [random.getrandbits(1) for _ in range(n)]
# run the lfsr for an additional n steps
for _ in range(n):
s = [step(l, s)] + s
return s
# some helpers for printing things
def sequence(l):
return " ".join("%01x" % l_ for l_ in l)
def recurrence(l):
if not any(l):
return "0"
return " + ".join([
"s_i" if i == 0 else "s_i-%d" % i
for i, b in enumerate(l)
if b])
def lfsr(l, s):
l = l[1:]
wire = max(
(i if l_ else 0 for i, l_ in enumerate(l)),
default=0)
yield "".join([
".----" if any(l) else " ",
"".join("-. " if l_ and i == wire
else " " if i >= wire
else " + <-" if l_
else "-----"
for i, l_ in enumerate(l)),
])
yield "".join([
"| " if any(l) else " ",
"".join(" | " if l_ and i == wire
else " ^ " if l_
else " "
for i, l_ in enumerate(l)),
])
yield "".join([
"| " if any(l) else " ",
"." if l else "",
"".join("-|--." if l_ else "----."
for l_ in l),
])
yield "".join([
"'-> " if any(l) else "0-> ",
"|" if l else "",
"".join(" %01x |" % s[len(s)-len(l)+i]
for i, _ in enumerate(l)),
"-> " if l else "",
])
yield "".join([
" ",
"'" if l else "",
"".join("----'" for _ in l),
" " if l else "",
])
yield "".join([
" ",
" " if l else "",
"".join(" " for _ in l),
" " if l else "",
])
def main(bits, *,
random=None):
# generate random bits?
if random is not None:
s = randomlfsr(random)
# reparse to accept bits with and without spaces
else:
s = []
for b in bits:
for c in b:
if c == '1':
s.append(1)
elif c == '0':
s.append(0)
print("Solving: %s" % sequence(s))
print()
# guess an initial LFSR
n = 0
e = 0
l = [0]
c = [1]
print("|L%d| = %d" % (n, e))
print("L%d(i) = %s" % (n, recurrence(l)))
print("C%d(i) = %s" % (n, recurrence(c)))
print()
while n < len(s):
# calculate the discrepancy d
next = step(l, s[len(s)-n:])
d = next ^ s[len(s)-(n+1)]
for i, line in enumerate(lfsr(l, s)):
if i == 3:
print("L%d = " % n, end='')
else:
print(" "*len("L%d = " % n), end='')
print(line, end='')
if i == 3:
print("Output: %s" % (
sequence([next] + s[len(s)-n:])),
end='')
elif i == 4:
print("Expected: %s" % (
sequence(s[len(s)-(n+1):])),
end='')
elif i == 5:
print(" d = %01x" % d, end='')
print()
print()
# no discrepancy? keep going
if d == 0:
e_ = e
l_ = l
# let C'(i) = C(i-1)
c_ = [0] + c
print("|L%d| = |L%d| = %d" % (n+1, n, e_))
print("L%d(i) = L%d(i) = %s" % (n+1, n, recurrence(l_)))
print("C%d(i) = C%d(i-1) = %s" % (n+1, n, recurrence(c_)))
print()
# found discrepancy?
else:
# fits in current LFSR?
if n < 2*e:
e_ = e
# let L'(i) = L(i) + C(i-1)
l_ = [l_ ^ c_
for l_, c_ in it.zip_longest(
l, [0] + c, fillvalue=0)]
# let C'(i) = C(i-1)
c_ = [0] + c
print("|L%d| = |L%d| = %d" % (n+1, n, e))
print("L%d(i) = L%d(i) + C%d(i-1) = %s" % (
n+1, n, n, recurrence(l_)))
print("C%d(i) = C%d(i-1) = %s" % (
n+1, n, recurrence(c_)))
print()
# need a bigger LFSR?
else:
# let |L'| = n+1-|L|
e_ = n+1-e
# let L'(i) = L(i) + C(i-1)
l_ = [l_ ^ c_
for l_, c_ in it.zip_longest(
l, [0] + c, fillvalue=0)]
# let C'(i) = s_i + L(i)
c_ = [1] + l[1:]
print("|L%d| = %d+1-|L%d| = %d" % (n+1, n, n, e_))
print("L%d(i) = L%d(i) + C%d(i-1) = %s" % (
n+1, n, n, recurrence(l_)))
print("C%d(i) = L%d(i) = %s" % (
n+1, n, recurrence(c_)))
print()
e = e_
l = l_
c = c_
n += 1
# print final LFSR
for i, line in enumerate(it.islice(lfsr(l, s), 5)):
if i == 3:
print("L%d = " % n, end='')
else:
print(" "*len("L%d = " % n), end='')
print(line, end='')
if i == 3:
print("Output: %s" % sequence(s), end='')
elif i == 4:
print("Expected: %s" % sequence(s), end='')
print()
print()
if __name__ == "__main__":
import sys
import argparse
parser = argparse.ArgumentParser(
description="Find the minimal LFSR for a sequence of bits using the "
"Berlekamp-Massey algorithm.",
allow_abbrev=False)
parser.add_argument(
'bits',
nargs='*',
help="Bit sequence to find an LFSR from.")
parser.add_argument(
'-r', '--random',
type=lambda x: int(x, 0),
help="Use a bit sequence generated by a random LFSR of this size.")
sys.exit(main(**{k: v
for k, v in vars(parser.parse_args()).items()
if v is not None}))