47 template <
typename Te,
typename Tp, Tp P,
int N, Te ALPHA>
53 fail(
"alpha!=2 not implemented");
56 for (Tp
i = 0;
i < (1 << N); ++
i)
59 lut_exp[((1 << N) - 1) +
i] = alpha_i;
62 if (alpha_i & (1 << N))
67 inline Te
add(Te x, Te y) {
return x ^ y; }
68 inline Te
sub(Te x, Te y) {
return x ^ y; }
69 inline Te
mul(Te x, Te y)
75 inline Te
div(Te x, Te y)
109 for (
int i = 0;
i <= 16; ++
i)
110 G[
i] = (
i == 16) ? 1 : 0;
111 for (
int d = 0; d < 16; ++d)
115 for (
int i = 0;
i <= 16; ++
i)
116 G[
i] = gf.
sub((
i == 16) ? 0 : G[
i + 1], gf.
mul(gf.
exp(d), G[
i]));
119 fprintf(stderr,
"RS generator:");
120 for (
int i = 0;
i <= 16; ++
i)
121 fprintf(stderr,
" %02x", G[
i]);
122 fprintf(stderr,
"\n");
134 bool corrupted =
false;
135 for (
int i = 0;
i < 16; ++
i)
137 synd[
i] = eval_poly_rev(poly, 204, gf.
exp(
i));
147 for (
int i = 0;
i < n; ++
i)
148 acc = gf.
add(gf.
mul(acc, x), poly[
i]);
157 for (; deg >= 0; --deg)
158 acc = gf.
add(gf.
mul(acc, x), poly[deg]);
169 memset(p + 188, 0, 16);
172 fprintf(stderr,
"uncoded:");
173 for (
int i = 0;
i < 204; ++
i)
174 fprintf(stderr,
" %d", p[
i]);
175 fprintf(stderr,
"\n");
178 for (
int d = 0; d < 188; ++d)
183 u8 k = gf.
div(p[d], G[0]);
185 for (
int i = 0; i <= 16; ++
i)
186 p[d + i] = gf.
sub(p[d + i], gf.
mul(k, G[i]));
189 fprintf(stderr,
"coded:");
190 for (
int i = 0; i < 204; ++
i)
191 fprintf(stderr,
" %d", p[i]);
192 fprintf(stderr,
"\n");
194 memcpy(msg + 188, p + 188, 16);
202 u8 pin[204] = NULL,
int *bits_corrected = NULL)
206 u8 C[16] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
207 u8 B[16] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
211 for (
int n = 0; n < 16; ++n)
214 for (
int i = 1;
i <= L; ++
i)
215 d ^= gf.
mul(C[
i], synd[n - i]);
223 memcpy(T, C,
sizeof(T));
224 for (
int i = 0; i < 16 - m; ++
i)
225 C[m + i] ^= gf.
mul(d, gf.
mul(gf.
inv(b), B[
i]));
227 memcpy(B, T,
sizeof(B));
233 for (
int i = 0; i < 16 - m; ++
i)
234 C[m + i] ^= gf.
mul(d, gf.
mul(gf.
inv(b), B[
i]));
241 fprintf(stderr,
"[L=%d C=", L);
242 for (
int i = 0;
i < 16; ++
i)
243 fprintf(stderr,
" %d", C[
i]);
244 fprintf(stderr,
"]\n");
245 fprintf(stderr,
"[S=");
246 for (
int i = 0; i < 16; ++
i)
247 fprintf(stderr,
" %d", synd[i]);
248 fprintf(stderr,
"]\n");
256 memset(omega, 0,
sizeof(omega));
258 for (
int i = 0; i < 16; ++
i)
259 for (
int j = 0; j < 16; ++j)
261 omega[i + j] ^= gf.
mul(synd[i], C[j]);
263 fprintf(stderr,
"omega=");
264 for (
int i = 0; i < 16; ++
i)
265 fprintf(stderr,
" %d", omega[i]);
266 fprintf(stderr,
"\n");
271 for (
int i = 0; i < 15; ++
i)
272 Cprime[i] = (i & 1) ? 0 : C[i + 1];
274 fprintf(stderr,
"Cprime=");
275 for (
int i = 0; i < 15; ++
i)
276 fprintf(stderr,
" %d", Cprime[i]);
277 fprintf(stderr,
"\n");
283 for (
int i = 0; i < 255; ++
i)
286 u8 v = eval_poly(C, L, r);
291 int loc = (255 -
i) % 255;
293 fprintf(stderr,
"found root=%d, inv=%d, loc=%d\n", r, xk, loc);
298 u8 num = gf.
mul(xk, eval_poly(omega, L, r));
299 u8 den = eval_poly(Cprime, 14, r);
300 u8 e = gf.
div(num, den);
306 pout[203 - loc] ^= e;
310 if (++roots_found == L)
315 return syndromes(pin, synd);
323 #endif // LEANSDR_RS_H
gf2x_p< unsigned char, unsigned short, 0x11d, 8, 2 > gf
int hamming_weight(uint8_t x)
bool correct(u8 synd[16], u8 pout[188], u8 pin[204]=NULL, int *bits_corrected=NULL)
u8 eval_poly(const u8 *poly, int deg, u8 x)
u8 eval_poly_rev(const u8 *poly, int n, u8 x)
bool syndromes(const u8 *poly, u8 *synd)