33#define RS_NPAR (RS_N - RS_K)
47static uint8_t
gf_mul(uint8_t a, uint8_t b) {
48 if (a == 0 || b == 0)
return 0;
61 for (
int i = 0; i <
RS_N; i++) {
79 for (
int i = 1; i <=
RS_NPAR; i++) {
82 for (
int j = deg + 1; j > 0; j--) {
83 gpoly[j] =
gf_mul(gpoly[j], root) ^ gpoly[j - 1];
85 gpoly[0] =
gf_mul(gpoly[0], root);
103 int info_exp = (
RS_K - 1) - col;
105 int div_deg = info_exp +
RS_NPAR;
110 memset(rem, 0,
sizeof(rem));
112 for (
int d = div_deg; d >=
RS_NPAR; d--) {
113 uint8_t lead = rem[d];
114 if (lead == 0)
continue;
117 for (
int j = 0; j <=
RS_NPAR; j++) {
128 for (
int r = 0; r <
RS_N; r++) {
131 sym = (r -
RS_NPAR == info_exp) ? 1 : 0;
135 G[
RS_N - 1 - r][col] = sym;
143 for (
int i = 0; i < k; i++) {
144 for (
int j = 0; j < k; j++) inv[i][j] = (i == j) ? 1 : 0;
146 for (
int col = 0; col < k; col++) {
149 for (
int r = col; r < k; r++) {
150 if (a[r][col] != 0) {
155 if (piv < 0)
return -1;
157 for (
int j = 0; j < k; j++) {
158 uint8_t t = a[col][j];
159 a[col][j] = a[piv][j];
162 inv[col][j] = inv[piv][j];
167 uint8_t pinv =
gf_inv(a[col][col]);
168 for (
int j = 0; j < k; j++) {
169 a[col][j] =
gf_mul(a[col][j], pinv);
170 inv[col][j] =
gf_mul(inv[col][j], pinv);
173 for (
int r = 0; r < k; r++) {
174 if (r == col)
continue;
175 uint8_t f = a[r][col];
176 if (f == 0)
continue;
177 for (
int j = 0; j < k; j++) {
178 a[r][j] ^=
gf_mul(f, a[col][j]);
179 inv[r][j] ^=
gf_mul(f, inv[col][j]);
197 if (!pages || !pids || !out)
return -1;
198 if (k < 1 || k >
RS_K)
return -1;
204 memset(seen, 0,
sizeof(seen));
205 for (
int i = 0; i < k; i++) {
207 if (p < 1 || p >
RS_N)
return -1;
208 if (seen[p])
return -1;
214 for (
int i = 0; i < k; i++) {
215 int row = pids[i] - 1;
216 for (
int j = 0; j < k; j++) D[i][j] =
G[row][j];
218 if (
gf_invert(k, D, Dinv) != 0)
return -1;
223 for (
int oct = 0; oct <
PAGE_OCT; oct++) {
224 for (
int i = 0; i < k; i++) {
226 for (
int r = 0; r < k; r++) {
227 acc ^=
gf_mul(Dinv[i][r], pages[r][oct]);
static uint8_t gf_mul(uint8_t a, uint8_t b)
static uint8_t gf_inv(uint8_t a)
static void gf_build_genpoly(uint8_t gpoly[(255 - 32)+1])
static void gf_build_tables(void)
static uint8_t gf_log[256]
int has_rs_init(void)
Build the GF(256) log/antilog tables and the systematic generator matrix G. Idempotent: safe to call ...
static void rs_encode_column(int col, const uint8_t gpoly[(255 - 32)+1])
static uint8_t gf_exp[2 *255]
static uint8_t G[255][32]
static int gf_invert(int k, uint8_t a[32][32], uint8_t inv[32][32])
int has_rs_decode(const uint8_t pages[][53], const uint8_t *pids, int k, uint8_t *out)
Decode a k-page HAS message from k distinct received encoded pages using the RS(255,...