py_raise_fec_error("Precondition violation: second argument is required to be greater than or equal to 1, but it was %d", self->mm);
return -1;
}
- if (self->mm > 255) {
- py_raise_fec_error("Precondition violation: second argument is required to be less than or equal to 255, but it was %d", self->mm);
+ if (self->mm > 256) {
+ py_raise_fec_error("Precondition violation: second argument is required to be less than or equal to 256, but it was %d", self->mm);
return -1;
}
if (self->kk > self->mm) {
gf* check_shares_produced[self->mm - self->kk]; /* This is an upper bound -- we will actually use only num_check_shares_produced of these elements (see below). */
PyObject* pystrs_produced[self->mm - self->kk]; /* This is an upper bound -- we will actually use only num_check_shares_produced of these elements (see below). */
- unsigned char num_check_shares_produced = 0; /* The first num_check_shares_produced elements of the check_shares_produced array and of the pystrs_produced array will be used. */
+ unsigned num_check_shares_produced = 0; /* The first num_check_shares_produced elements of the check_shares_produced array and of the pystrs_produced array will be used. */
const gf* incshares[self->kk];
- unsigned char num_desired_shares;
+ unsigned num_desired_shares;
PyObject* fast_desired_shares_ids = NULL;
PyObject** fast_desired_shares_ids_items;
- unsigned char c_desired_shares_ids[self->mm];
- unsigned char c_desired_checkshares_ids[self->mm - self->kk];
- unsigned char i;
+ unsigned c_desired_shares_ids[self->mm];
+ unsigned c_desired_checkshares_ids[self->mm - self->kk];
+ unsigned i;
if (desired_shares_ids) {
fast_desired_shares_ids = PySequence_Fast(desired_shares_ids, "Second argument (optional) was not a sequence.");
num_desired_shares = PySequence_Fast_GET_SIZE(fast_desired_shares_ids);
py_raise_fec_error("Precondition violation: second argument is required to be greater than or equal to 1, but it was %d", self->mm);
return -1;
}
- if (self->mm > 255) {
- py_raise_fec_error("Precondition violation: second argument is required to be less than or equal to 255, but it was %d", self->mm);
+ if (self->mm > 256) {
+ py_raise_fec_error("Precondition violation: second argument is required to be less than or equal to 256, but it was %d", self->mm);
return -1;
}
if (self->kk > self->mm) {
return NULL;
const gf*restrict cshares[self->kk];
- unsigned char cshareids[self->kk];
+ unsigned cshareids[self->kk];
gf*restrict recoveredcstrs[self->kk]; /* self->kk is actually an upper bound -- we probably won't need all of this space. */
PyObject*restrict recoveredpystrs[self->kk]; /* self->kk is actually an upper bound -- we probably won't need all of this space. */
- unsigned char i;
+ unsigned i;
for (i=0; i<self->kk; i++)
recoveredpystrs[i] = NULL;
PyObject*restrict fastshares = PySequence_Fast(shares, "First argument was not a sequence.");
}
/* Construct a C array of gf*'s of the data and another of C ints of the shareids. */
- unsigned char needtorecover=0;
+ unsigned needtorecover=0;
PyObject** fastshareidsitems = PySequence_Fast_ITEMS(fastshareids);
if (!fastshareidsitems)
goto err;
if (!PyInt_Check(fastshareidsitems[i]))
goto err;
long tmpl = PyInt_AsLong(fastshareidsitems[i]);
- if (tmpl < 0 || tmpl >= UCHAR_MAX) {
+ if (tmpl < 0 || tmpl > 255) {
py_raise_fec_error("Precondition violation: Share ids can't be less than zero or greater than 255. %ld\n", tmpl);
goto err;
}
- cshareids[i] = (unsigned char)tmpl;
+ cshareids[i] = (unsigned)tmpl;
if (cshareids[i] >= self->kk)
needtorecover+=1;
i++;
else {
/* put pkt in the right position. */
- unsigned char c = cshareids[i];
+ unsigned c = cshareids[i];
SWAP (cshareids[i], cshareids[c], int);
SWAP (cshares[i], cshares[c], const gf*);
fec_decode(self->fec_matrix, cshares, recoveredcstrs, cshareids, sz);
/* Wrap up both original primary shares and decoded shares into a Python list of Python strings. */
- unsigned char nextrecoveredix=0;
+ unsigned nextrecoveredix=0;
result = PyList_New(self->kk);
if (result == NULL)
goto err;
* many numbers by the same constant. In this case the first call sets the
* constant, and others perform the multiplications. A value related to the
* multiplication is held in a local variable declared with USE_GF_MULC . See
- * usage in addmul1().
+ * usage in _addmul1().
*/
static gf gf_mul_table[256][256];
}
#define NEW_GF_MATRIX(rows, cols) \
- (gf *)my_malloc(rows * cols * sizeof(gf), " ## __LINE__ ## " )
+ (gf*)my_malloc(rows * cols, " ## __LINE__ ## " )
/*
* initialize the data structures used for computations in GF.
* calls are unfrequent in my typical apps so I did not bother.
*/
#define addmul(dst, src, c, sz) \
- if (c != 0) addmul1(dst, src, c, sz)
+ if (c != 0) _addmul1(dst, src, c, sz)
#define UNROLL 16 /* 1, 4, 8, 16 */
static void
-addmul1 (gf * dst1, const gf * src1, gf c, int sz) {
+_addmul1(register gf*restrict dst, const register gf*restrict src, gf c, size_t sz) {
USE_GF_MULC;
- register gf *dst = dst1;
- register const gf *src = src1;
- gf *lim = &dst[sz - UNROLL + 1];
+ const gf* lim = &dst[sz - UNROLL + 1];
GF_MULC0 (c);
* computes C = AB where A is n*k, B is k*m, C is n*m
*/
static void
-matmul (gf * a, gf * b, gf * c, int n, int k, int m) {
- int row, col, i;
+_matmul(gf * a, gf * b, gf * c, unsigned n, unsigned k, unsigned m) {
+ unsigned row, col, i;
for (row = 0; row < n; row++) {
for (col = 0; col < m; col++) {
}
/*
- * invert_mat() takes a matrix and produces its inverse
+ * _invert_mat() takes a matrix and produces its inverse
* k is the size of the matrix.
* (Gauss-Jordan, adapted from Numerical Recipes in C)
* Return non-zero if singular.
*/
-static int
-invert_mat (gf * src, int k) {
+static void
+_invert_mat(gf* src, unsigned k) {
gf c, *p;
- int irow, icol, row, col, i, ix;
+ unsigned irow = 0;
+ unsigned icol = 0;
+ unsigned row, col, i, ix;
- int error = -1;
- int *indxc = (int *) my_malloc (k * sizeof (int), "indxc");
- int *indxr = (int *) my_malloc (k * sizeof (int), "indxr");
- int *ipiv = (int *) my_malloc (k * sizeof (int), "ipiv");
+ unsigned* indxc = (unsigned*) my_malloc (k * sizeof(unsigned), "indxc");
+ unsigned* indxr = (unsigned*) my_malloc (k * sizeof(unsigned), "indxr");
+ unsigned* ipiv = (unsigned*) my_malloc (k * sizeof(unsigned), "ipiv");
gf *id_row = NEW_GF_MATRIX (1, k);
gf *temp_row = NEW_GF_MATRIX (1, k);
* Zeroing column 'col', look for a non-zero element.
* First try on the diagonal, if it fails, look elsewhere.
*/
- irow = icol = -1;
if (ipiv[col] != 1 && src[col * k + col] != 0) {
irow = col;
icol = col;
}
}
}
- if (icol == -1) {
- ERR("Pivot not found!");
- goto fail;
- }
found_piv:
++(ipiv[icol]);
/*
}
id_row[icol] = 0;
} /* done all columns */
- for (col = k - 1; col >= 0; col--) {
- if (indxr[col] < 0 || indxr[col] >= k) {
- ERR("AARGH, indxr[col] %d\n", indxr[col]);
- goto fail;
- } else if (indxc[col] < 0 || indxc[col] >= k) {
- ERR("AARGH, indxc[col] %d\n", indxc[col]);
- goto fail;
- } else if (indxr[col] != indxc[col]) {
+ for (col = k; col > 0; col--)
+ if (indxr[col-1] != indxc[col-1])
for (row = 0; row < k; row++)
- SWAP (src[row * k + indxr[col]], src[row * k + indxc[col]], gf);
- }
- }
- error = 0;
+ SWAP (src[row * k + indxr[col-1]], src[row * k + indxc[col-1]], gf);
fail:
free (indxc);
free (indxr);
free (ipiv);
free (id_row);
free (temp_row);
- return error;
+ return;
}
/*
* p = coefficients of the matrix (p_i)
* q = values of the polynomial (known)
*/
-int
-invert_vdm (gf * src, int k) {
- int i, j, row, col;
+void
+_invert_vdm (gf* src, unsigned k) {
+ unsigned i, j, row, col;
gf *b, *c, *p;
gf t, xx;
if (k == 1) /* degenerate case, matrix must be p^0 = 1 */
- return 0;
+ return;
/*
* c holds the coefficient of P(x) = Prod (x - p_i), i=0..k-1
* b holds the coefficient for the matrix inversion
xx = p[row];
t = 1;
b[k - 1] = 1; /* this is in fact c[k] */
- for (i = k - 2; i >= 0; i--) {
- b[i] = c[i + 1] ^ gf_mul (xx, b[i + 1]);
- t = gf_mul (xx, t) ^ b[i];
+ for (i = k - 1; i > 0; i--) {
+ b[i-1] = c[i] ^ gf_mul (xx, b[i]);
+ t = gf_mul (xx, t) ^ b[i-1];
}
for (col = 0; col < k; col++)
src[col * k + row] = gf_mul (inverse[t], b[col]);
free (c);
free (b);
free (p);
- return 0;
+ return;
}
static int fec_initialized = 0;
free (p);
}
-/*
- * create a new encoder, returning a descriptor. This contains k,n and
- * the encoding matrix.
- */
fec_t *
-fec_new (unsigned char k, unsigned char n) {
- unsigned char row, col;
+fec_new(unsigned k, unsigned n) {
+ unsigned row, col;
gf *p, *tmp_m;
fec_t *retval;
* k*k vandermonde matrix, multiply right the bottom n-k rows
* by the inverse, and construct the identity matrix at the top.
*/
- invert_vdm (tmp_m, k); /* much faster than invert_mat */
- matmul (tmp_m + k * k, tmp_m, retval->enc_matrix + k * k, n - k, k, k);
+ _invert_vdm (tmp_m, k); /* much faster than _invert_mat */
+ _matmul(tmp_m + k * k, tmp_m, retval->enc_matrix + k * k, n - k, k, k);
/*
* the upper matrix is I so do not bother with a slow multiply
*/
}
void
-fec_encode(const fec_t* code, const gf*restrict const*restrict const src, gf*restrict const*restrict const fecs, const unsigned char*restrict const share_ids, unsigned char num_share_ids, size_t sz) {
+fec_encode(const fec_t* code, const gf*restrict const*restrict const src, gf*restrict const*restrict const fecs, const unsigned*restrict const share_ids, size_t num_share_ids, size_t sz) {
unsigned i, j;
- unsigned char fecnum;
+ unsigned fecnum;
gf* p;
for (i=0; i<num_share_ids; i++) {
memset(fecs[i], 0, sz);
p = &(code->enc_matrix[fecnum * code->k]);
for (j = 0; j < code->k; j++)
- addmul (fecs[i], src[j], p[j], sz);
+ addmul(fecs[i], src[j], p[j], sz);
}
}
* @param matrix a space allocated for a k by k matrix
*/
void
-build_decode_matrix_into_space(const fec_t*restrict const code, const unsigned char*const restrict index, const unsigned char k, gf*restrict const matrix) {
+build_decode_matrix_into_space(const fec_t*restrict const code, const unsigned*const restrict index, const unsigned k, gf*restrict const matrix) {
unsigned i;
gf* p;
for (i=0, p=matrix; i < k; i++, p += k) {
memcpy(p, &(code->enc_matrix[index[i] * code->k]), k);
}
}
- invert_mat (matrix, k);
+ _invert_mat (matrix, k);
}
void
-fec_decode(const fec_t* code, const gf*restrict const*restrict const inpkts, gf*restrict const*restrict const outpkts, const unsigned char*restrict const index, size_t sz) {
+fec_decode(const fec_t* code, const gf*restrict const*restrict const inpkts, gf*restrict const*restrict const outpkts, const unsigned*restrict const index, size_t sz) {
gf m_dec[code->k * code->k];
build_decode_matrix_into_space(code, index, code->k, m_dec);