41 template <
typename T,
int N,
int NP,
int DP,
typename TGF,
int GFTRUNCGEN>
49 for (
int i = 0;
i < npolys; ++
i)
54 for (
int i = 0;
i < npolys; ++
i)
55 truncpolys[
i].copy(polys[
i]);
59 syndpolys =
new int[2 * npolys];
60 for (
int i = 0; i < 2 * npolys; ++
i)
63 for (j = 0; j < npolys; ++j)
64 if (!eval_poly(truncpolys[j],
true, 1 + i))
67 fail(
"Bad polynomials/root");
78 for (
int i = N / 8;
i--; ++out)
79 *out = parity.
v[
i /
sizeof(T)] >> ((
i & (
sizeof(T) - 1)) * 8);
88 bool corrupted =
false;
92 for (
int j = 0; j < npolys; ++j)
94 rem[j] =
divmod(cw, cwbytes, truncpolys[j]);
97 TGF *S =
new TGF[2 * npolys];
98 for (
int i = 0;
i < 2 * npolys; ++
i)
104 S[
i] = eval_poly(rem[syndpolys[
i]],
false, 1 + i);
115 fprintf(stderr,
"synd:");
116 for (
int i=0;
i<2*npolys; ++
i ) fprintf(stderr,
" %04x", S[
i]);
117 fprintf(stderr,
"\n");
129 TGF *C =
new TGF[NN];
131 TGF *B =
new TGF[NN];
141 for (
int n = 0; n < NN; ++n)
144 for (
int i = 1; i <= L; ++
i)
145 d = GF.add(d, GF.mul(C[i], S[n - i]));
150 TGF d_div_b = GF.mul(d, GF.inv(b));
153 TGF *tmp =
new TGF[NN];
154 memcpy(tmp, C,
sizeof(tmp));
155 for (
int i = 0; i < NN - m; ++
i)
156 C[m + i] = GF.sub(C[m + i], GF.mul(d_div_b, B[i]));
158 memcpy(B, tmp,
sizeof(B));
165 for (
int i = 0; i < NN - m; ++
i)
166 C[m + i] = GF.sub(C[m + i], GF.mul(d_div_b, B[i]));
175 fprintf(stderr,
"C[%d]=", L);
176 for (
int i=0; i<NN; ++
i ) fprintf(stderr,
" %04x", C[i]);
177 fprintf(stderr,
"\n");
187 for (
int i = 0; i < (1 << DP) - 1; ++
i)
190 TGF v = eval_poly(C, L, i);
194 int loc = (i ? (1 << DP) - 1 - i : 0);
196 int rloc = cwbytes * 8 - 1 - loc;
206 cw[rloc / 8] ^= 128 >> (rloc & 7);
208 if (roots_found == L)
218 if (roots_found != L)
230 for (
int i = 0;
i < DP; ++
i)
233 acc = GF.add(acc, GF.exp(re));
235 if (re >= (1 << DP) - 1)
239 acc = GF.add(acc, GF.exp(re));
249 for (
int i = 0;
i <= deg; ++
i)
251 acc = GF.add(acc, GF.mul(poly[
i], GF.exp(re)));
253 if (re >= (1 << DP) - 1)
269 #endif // LEANSDR_BCH_H virtual int decode(uint8_t *cw, size_t cwbytes)=0
bitvect< T, DP > * truncpolys
int decode(uint8_t *cw, size_t cwbytes)
void encode(const uint8_t *msg, size_t msgbytes, uint8_t *out)
virtual void encode(const uint8_t *msg, size_t msgbytes, uint8_t *out)=0
bitvect< T, N > divmod(const Tm *m, size_t nm, const bitvect< T, N > &p)
gf2n< TGF, DP, 2, GFTRUNCGEN > GF
TGF eval_poly(const bitvect< T, DP > &poly, bool is_trunc, int rootexp)
unsigned char parity(uint8_t x)
TGF eval_poly(const TGF *poly, int deg, int rootexp)
bch_engine(const bitvect< T, NP > *polys, int _npolys)
bitvect< T, N > shiftdivmod(const Tm *m, size_t nm, const bitvect< T, N > &p, T init=0)