-
Notifications
You must be signed in to change notification settings - Fork 0
/
BMDecoder.sage
executable file
·296 lines (260 loc) · 10.2 KB
/
BMDecoder.sage
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/local/sage/default/sage -python
# coding: utf-8
import sys # for argv function
from sage.all import *
def generateRandomData(Dimension):
F = GF(2)
u = random_vector(F, Dimension)
return u
def makeCodeword(data, q, m, CorrectableBits, BaseElement, NarrowFlag):
q = BaseElement
m = MultiplicativeOrder
x = PolynomialRing(GF(q), 'x').gen()
if m == 14:
f=x**14+x**10+x**6+x+1
K = GF(q**m, name='alpha', modulus=f)
else:
K = GF(q**m, name='alpha')
Dimension = int(q**m - 1 - m*CorrectableBits)
v = 0
for i in range(0, Dimension):
v += data[i]*x**i
v = v*x**(m*CorrectableBits)
#calculate generator polynomial
generator = getBCHCodeGeneratingPolynomials(q**m - 1, 2*CorrectableBits + 1, BaseElement, NarrowFlag = 0)
# divide data bits by generator
quorem = v.quo_rem(generator)
# c = v + remainder
c = v + quorem[1]
return c
def injectErrors(codeword, CorrectableBits, CodeLength, BaseElement, MultiplicativeOrder):
q = BaseElement
m = MultiplicativeOrder
x = PolynomialRing(GF(q), 'x').gen()
if m == 14:
f=x**14+x**10+x**6+x+1
K = GF(q**m, name='alpha', modulus=f)
else:
K = GF(q**m, name='alpha')
position = []
while(len(position) != CorrectableBits):
r = ZZ.random_element(CodeLength, distribution = "uniform")
if r not in position:
position.append(r)
position.sort()
# print position
# tmp = 1*0 + 0*x
# for p in position:
# tmp += x**p
# y = codeword + tmp
# return y
return position
# end of function
def verify(pos1, pos2):
if pos1 == pos2:
return True
else:
return False
#################################################
def BCHDecoder(BaseElement, CorrectableBits, MultiplicativeOrder, CodeLength):
q = BaseElement
m = MultiplicativeOrder
x = PolynomialRing(GF(q), 'x').gen()
# generate random data
Dimension = int(CodeLength - CorrectableBits*MultiplicativeOrder)
u = generateRandomData(Dimension)
# make codeword polynomial
c = makeCodeword(u, BaseElement, MultiplicativeOrder, CorrectableBits, BaseElement, NarrowFlag)
# determin error position
errorposition = injectErrors(c, CorrectableBits, CodeLength, BaseElement, MultiplicativeOrder)
# inject errors
y = 1*0 + 0*x
tmp = 1*0 + 0*x
for pos in errorposition:
tmp += x**pos
y = c + tmp
# make syndrome
syndromes = BCHSyndrome(y, BaseElement, MultiplicativeOrder, CorrectableBits)
# create error locator polynomial
errorlocator = BMDecoder(syndromes, BaseElement, MultiplicativeOrder, CorrectableBits)
# chien search
pointout = chien(errorlocator, BaseElement, MultiplicativeOrder)
#verify
result = verify(errorposition, pointout)
if result == True:
print("Correction is successfully completed.")
else:
print("Oh my God! Correction is failed.")
print("error position = {0}".format(errorposition))
print("result = {0}".format(pointout))
print("error locator = {0}".format(errorlocator))
print("syndromes = {0}".format(syndromes))
sys.exit()
return result
#end of function
########################### Syndrome Calculation ##########################
def BCHSyndrome(input, BaseElement, MultiplicativeOrder, CorrectableBits):
q = BaseElement
m = MultiplicativeOrder
x = PolynomialRing(GF(q), 'x').gen()
if m == 14:
f=x**14+x**10+x**6+x+1
K = GF(q**m, name='alpha', modulus=f)
else:
K = GF(q**m, name='alpha')
# generate primitive element alpha
alpha = K.gen()
# print("<syndrome> codeword = {0}".format(codeword))
#calculate generator polynomial
generator = getBCHCodeGeneratingPolynomials(q**m - 1, 2*CorrectableBits + 1, BaseElement, NarrowFlag = 0)
#check errors
quorem = input.quo_rem(generator)
if quorem[1] == 0:
print("<BCHSyndrome> No Error")
sys.exit(1)
remainder = quorem[1]
#declare syndromes as a list
syndromes = []
#add syndromes
for i in range(1, int(2*CorrectableBits + 1)):
syndromes.append(remainder(alpha**i))
return syndromes
# Chien Search function
def chien(errorlocator, BaseElement, MultiplicativeOrder):
q = BaseElement
m = MultiplicativeOrder
x = PolynomialRing(GF(q), 'x').gen()
if m == 14:
f=x**14+x**10+x**6+x+1
K = GF(q**m, name='alpha', modulus=f)
else:
K = GF(q**m, name='alpha')
alpha = K.gen()
print("<chien> errorlocator = {0}".format(errorlocator))
if errorlocator == 1:
print("No Error")
sys.exit(1)
errorflag = 0
errorposition = []
for i in range(0, q**m - 1):
Ex = errorlocator(1/alpha**i)
if Ex == 0:
print("bit {0} error detected".format(i))
errorposition.append(i) #save error position for correction
errorflag += 1
if errorflag == 0 or errorflag != errorlocator.degree():
print("Logical Conflict Detected")
print("errorlocator = {0}".format(errorlocator))
print("But this program can not identify error bit")
sys.exit(1)
return errorposition
# end of function
def BMDecoder(syndromes, BaseElement, MultiplicativeOrder, CorrectableBits):
q = BaseElement
m = MultiplicativeOrder
# Declare polynomial variables
x = PolynomialRing(GF(q), 'x').gen()
z = PolynomialRing(GF(q), 'z').gen()
# Define Galois Field GF(q**m) and its primitive element is alpha
if m == 14:
f=x**14+x**10+x**6+x+1
K = GF(q**m, name='alpha', modulus=f)
else:
K = GF(q**m, name='alpha')
# generate instance of alpha
alpha = K.gen()
# print "<DEBUG> syndromepolynomial = {0}".format(syndromepolynomial)
#initialize functions and variables
D = 0 #branch decision parameter
delta = 1 #coefficient for error locator sigma
sigma = 1 + 0*z #error locator polynomial
tau = 1 + 0*z #auxiliary polynomial for sigma
#Initially, DELTA = syndromes[0]
DELTA = syndromes[0]
print("syndromes = {0}".format(syndromes))
# iterative computation for sigma and tau
for i in range(0, int(2*CorrectableBits)):
tautmp = tau
deltatmp = delta
if DELTA == 0 or 2*D >= i + 1:
tau = z*tau
else:
D = i + 1 - D
delta = DELTA
tau = sigma
print("{0:>2} D = {1} delta = {2} tau = {3} DELTA = {4}".format(i, D, delta, tau, DELTA))
#calculation sigma
sigma = deltatmp*sigma+DELTA*z*tautmp
sigmacoefficients = sigma.coefficients(sparse = False)
print("{0:>2} sigma = {1}".format(i, sigma))
#calculation DELTA
#DELTA(i+1)=S_{i+1}*sigma_{0}^{(i)}+S_{i}*sigma_{1}^{(i)}+...+S_{i+1-nu_{i}}*sigma_{nu_{i}}^{(i)}
#nu_{i} = sigma^{(i)}.degree()
if i == 2*CorrectableBits - 1:
zeta = sigma.coefficients(sparse = False)
sigma = sigma/zeta[0]
return sigma
else:
# calculate Degree of sigma
nu = sigma.degree()
print("{0:>2} nu = {1}".format(i, nu))
# calculate DELTA
DELTA = 0
for j in range(0, nu + 1):
print("{0:>2} i + 1 -j = {1} j = {2}".format(i, i + 1 - j, j))
DELTA += syndromes[i + 1 - j]*sigmacoefficients[j]
# print "{0:>2} j = {1} x = {2}".format(i, j, syndromes[i + 1 - j]*sigmacoefficients[j])
# DELTA = DELTA%2
print("{0:>2} DELTA = {1}".format(i, DELTA))
#end of function
################################ get Minimum Polynomials for BCH Code #################################
def getBCHCodeMinimumPolynomials(CodeWordLength, MinimumDistance, BaseElement, NarrowFlag = 0):
q = BaseElement
R = IntegerModRing(CodeWordLength)
m = R(q).multiplicative_order()
x = PolynomialRing(GF(q), "x").gen()
# if m == 14 then choose Mizushima's primitive polynomial
if m == 14:
f=x**14+x**10+x**6+x+1
K = GF(q**m, name='a', modulus=f)
else:
K = GF(q**m, name='a')
a = K.gen()
L0 = [a**i for i in range(NarrowFlag, int(NarrowFlag + MinimumDistance))]
L1 = [b.minpoly() for b in L0]
L1.pop(0) # L1[0] is trivial polynomial "x+1".
# return List of Minimum Polynomials
return L1
######################################## End of Function ###############################################
############################### get generator polynomial for BCH Code ##################################
def getBCHCodeGeneratingPolynomials(CodeWordLength, MinimumDistance, BaseElement, NarrowFlag = 0):
q = BaseElement
R = IntegerModRing(CodeWordLength)
m = R(q).multiplicative_order()
x = PolynomialRing(GF(q), "x").gen()
L1 = getBCHCodeMinimumPolynomials(CodeWordLength, MinimumDistance, BaseElement, NarrowFlag = 0)
generator = LCM(L1)
if not (generator.divides(x**CodeWordLength-1)):
ValueError, "BCH Codes do not exist with the given input"
else:
# print "Generating Polynomial"
return generator
######################################## End of Function ###############################################
if __name__ == '__main__':
#translate command line argument
argvs = sys.argv #argvs is a list of commandline argument
# generate Test Data
BaseElement = 2
MinimumDistance = 5
CorrectableBits = (MinimumDistance - 1)/2
MultiplicativeOrder = 4
NarrowFlag = 0
CodeLength = BaseElement**MultiplicativeOrder - 1
# u = generateRandomData(7)
# c = makeCodeword(u, BaseElement, MultiplicativeOrder, CorrectableBits, BaseElement, NarrowFlag)
# y = injectErrors(c, CorrectableBits, CodeLength, BaseElement, MultiplicativeOrder)
# syndromes = BCHSyndrome(y, BaseElement, MultiplicativeOrder, CorrectableBits)
# errorlocator = BMDecoder(syndromes, BaseElement, MultiplicativeOrder, CorrectableBits)
# errorposition = chien(errorlocator, BaseElement, MultiplicativeOrder)
for i in range(0, 10000):
result = BCHDecoder(BaseElement, CorrectableBits, MultiplicativeOrder, CodeLength)