-
Notifications
You must be signed in to change notification settings - Fork 2
/
reed-solomon.c
401 lines (359 loc) · 8.99 KB
/
reed-solomon.c
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/**
* @file reed-solomon.c
* This is a reed-solomon codec.
*/
#include <stdint.h>
#include "reed-solomon.h"
#include "galois.h"
#include "utils.h"
static gf_t field;
static uint8_t gen[D+1];
static void rs_generate_generator_polynomial();
static uint32_t rs_calculate_syndromes(const uint8_t msg[N], uint8_t syndromes[D]);
static uint32_t rs_calculate_error_locator_polynomial(const uint8_t syndromes[D], uint8_t errpoly[D + 1]);
static int32_t rs_calculate_error_values(uint32_t errdeg, const uint8_t errpoly[D + 1], uint8_t roots[D + 1], uint8_t locpoly[E]);
static uint32_t rs_generate_error_evaluator_polynomial(const uint8_t syndromes[D], uint32_t errdeg, const uint8_t *errpoly, uint8_t evalpoly[D]);
/**
* Main program function.
*/
int32_t
rs_init(void)
{
if(gf_generate_field(&field, M, GF_PRIMPOLY_2_8)) {
return -1;
}
rs_generate_generator_polynomial();
return 0;
}
/* Find the generator polynomial for the BCH/RS code. */
static void
rs_generate_generator_polynomial()
{
uint32_t i, j;
gen[0] = 1;
for(i = 0; i < D; i++)
{
gen[i + 1] = 1;
j = i;
for(; j > 0; j--)
{
if(gen[j])
{
gen[j] = gen[j - 1] ^ field.exp[(field.log[gen[j]] + i) % N];
}
else
{
gen[j] = gen[j - 1];
}
}
gen[0] = field.exp[(field.log[gen[0]] + i) % N];
}
for(i = 0; i <= D; i++)
{
gen[i] = field.log[gen[i]];
}
}
/* Encode a message.
* Parity information is computed from msg and stored in parity in-place.
* Messages of length < K will be zero-padded.
* Returns 0 on completion or -1 on error.
*/
int32_t
rs_encode(const uint8_t msg[], uint32_t len, uint8_t parity[D])
{
int32_t i, j;
uint8_t fb;
/* Message must be length <= K. */
if(len > K)
{
return -1;
}
/* Zero-fill the parity bytes. */
for(i = 0; i < D; i++)
{
parity[i] = 0;
}
/* Linear feedback shift register. */
for(i = K - 1; i >= 0; i--)
{
fb = field.log[((i >= len) ? 0 : msg[i]) ^ parity[D-1]];
if(fb != A0)
{
for(j = D - 1; j > 0; j--)
{
parity[j] = parity[j - 1];
if(gen[j] != A0)
{
parity[j] ^= field.exp[(gen[j] + fb) % N];
}
}
parity[0] = field.exp[(gen[0] + fb) % N];
}
else
{
for(j = D - 1; j > 0; j--)
{
parity[j] = parity[j - 1];
}
parity[0] = 0;
}
}
return 0;
}
/* Decode a message.
* msg will be modified in-place if there are recoverable errors.
* Returns the number of errors in the message or -1 if it was unrecoverable.
*/
int32_t
rs_decode(uint8_t msg[N])
{
int32_t i, j, cnt;
uint8_t syndromes[D];
uint8_t errpoly[D + 1];
uint8_t roots[D + 1];
uint8_t locpoly[E];
uint8_t evalpoly[D];
uint32_t errdeg;
int32_t rootscnt;
uint32_t evaldeg;
uint8_t n1, n2, tmp;
if(!rs_calculate_syndromes(msg, syndromes))
{
return 0;
}
/* FIXME: Berlekamp-Massey algorithm implementation is BROKEN! */
errdeg = rs_calculate_error_locator_polynomial(syndromes, errpoly);
/* FIXME: Chien search might be broken, too. */
rootscnt = rs_calculate_error_values(errdeg, errpoly, roots, locpoly);
if(rootscnt < 0)
{
return -1;
}
evaldeg = rs_generate_error_evaluator_polynomial(syndromes, errdeg, errpoly, evalpoly);
cnt = rootscnt;
for(j = cnt - 1; j >= 0; j--)
{
n1 = 0;
for(i = evaldeg; i >= 0; i--)
{
if(evalpoly[i] != A0)
{
n1 ^= field.exp[(evalpoly[i] + i * roots[j]) % N];
}
}
n2 = field.exp[(N - roots[j]) % N];
tmp = 0;
for(i = MIN(errdeg, D - 1) & (-1 << 1); i >= 0; i -= 2)
{
if(errpoly[i + 1] != A0)
{
tmp ^= field.exp[(evalpoly[i] + i * roots[j]) % N];
}
}
if(!tmp)
{
return (cnt = -1);
}
if(n1 && j < E)
{
msg[locpoly[j]] ^= field.exp[(field.log[n1] + field.log[n2] + N - field.log[tmp]) % N];
}
}
return cnt;
}
/* Calculate the syndromes of the message.
* Returns zero if there are no errors in the message.
*/
static uint32_t
rs_calculate_syndromes(const uint8_t msg[N], uint8_t syndromes[D])
{
int32_t i, j, err;
uint8_t tmp;
for(i = 0; i < D; i++)
{
syndromes[i] = msg[0];
}
for(i = 1; i < N; i++)
{
if(!msg[i])
{
continue;
}
tmp = field.log[msg[i]];
for(j = 0; j < D; j++)
{
syndromes[j] ^= field.exp[(tmp + j * i) % N];
}
}
err = 0;
for(i = 0; i < D; i++)
{
err += !!syndromes[i];
syndromes[i] = field.log[syndromes[i]];
}
return err;
}
/* Calculate the error locator polynomial for the BCH/RS code.
* Uses the Berlekamp-Massey Algorithm.
* Returns the degree of the error polynomial (the number of errors in the message).
*/
static uint32_t
rs_calculate_error_locator_polynomial(const uint8_t syndromes[D], uint8_t errpoly[D + 1])
{
int32_t i, r, el, discr_r;
uint32_t deg = 0;
uint8_t b[D + 1];
uint8_t t[D + 1];
errpoly[0] = 1;
b[0] = field.log[1];
for(i = 1; i <= D; i++)
{
errpoly[i] = 0;
b[i] = field.log[0];
}
el = 0;
for(r = 0; r < D; r++)
{
discr_r = 0;
for(i = 0; i < r; i++)
{
if(errpoly[i] && syndromes[r - i] != A0)
{
discr_r ^= field.exp[(errpoly[i] + syndromes[r - i]) % N];
}
}
discr_r = field.log[discr_r];
if(discr_r == A0)
{
for(i = D - 1; i >= 0; i--)
{
b[i + 1] = b[i];
}
b[0] = A0;
}
else
{
t[0] = errpoly[0];
for(i = 0; i < D; i++)
{
t[i + 1] = errpoly[i + 1];
if(b[i] != A0)
{
t[i + 1] ^= field.exp[(discr_r + b[i]) % N];
}
}
if(2 * el <= r)
{
el = r - el;
for(i = 0; i <= D; i++)
{
if(errpoly[i])
{
b[i] = (field.log[errpoly[i]] - discr_r + N) % N;
}
else
{
b[i] = A0;
}
}
}
else
{
for(i = D - 1; i >= 0; i--)
{
b[i + 1] = b[i];
}
b[0] = A0;
}
for(i = 0; i <= D; i++)
{
errpoly[i] = t[i];
}
}
}
for(i = 0; i <= D; i++)
{
if(errpoly[i])
{
deg = i;
}
errpoly[i] = field.log[errpoly[i]];
}
return deg;
}
/* Calculate the roots of the error locator polynomial given an error locator polynomial and its degree.
* Also calculate the error values at each location.
* Uses the Chien search and Forney algoritms.
* Returns the number of roots of the error polynomial.
*/
static int32_t
rs_calculate_error_values(uint32_t errdeg, const uint8_t errpoly[D + 1], uint8_t roots[D + 1], uint8_t locpoly[E])
{
int32_t i, j, k, q;
uint8_t reg[D + 1];
int32_t rootscnt = 0;
for(i = 1; i <= D; i++)
{
reg[i] = errpoly[i];
}
k = N - 1;
for(i = 1; i <= N; i++)
{
q = 1;
for(j = errdeg; j > 0; j--)
{
if(reg[j] != A0)
{
reg[j] = (reg[j] + j) % N;
q ^= field.exp[reg[j]];
}
}
if(q)
{
k = (N + k - 1) % N;
continue;
}
roots[rootscnt] = i;
if(rootscnt < E)
{
locpoly[rootscnt] = k;
}
if(++rootscnt == errdeg)
{
break;
}
k = (N + k - 1) % N;
}
if(rootscnt != errdeg)
{
rootscnt = -1;
}
return rootscnt;
}
/* Generate the error evaluator polynomial.
* Returns the degree of the error evaluator polynomial.
*/
static uint32_t
rs_generate_error_evaluator_polynomial(const uint8_t syndromes[D], uint32_t errdeg, const uint8_t *errpoly, uint8_t evalpoly[D])
{
int32_t i, j;
uint32_t deg = 0;
uint8_t tmp;
for(i = 0; i < D; i++)
{
tmp = 0;
for(j = (deg < i) ? deg : i; j >= 0; j--)
{
if(syndromes[i-j] != A0 && errpoly[j] != A0)
{
tmp ^= field.exp[(syndromes[i-j] + errpoly[j]) % N];
}
}
if(tmp)
{
deg = i;
}
evalpoly[i] = field.log[tmp];
}
return deg;
}