POLY: fix pb_inverse_poly_q()

Should be correct now. Had to add get_degree(), because
pb_clamp() in conjunction with pb_cmp() does not give
expected results, see https://github.com/libtom/libtompoly/issues/3
...so don't use it.
This commit is contained in:
hasufell 2014-04-17 23:43:29 +02:00 committed by malte
parent f98be67dd3
commit 99ebda181a
2 changed files with 35 additions and 19 deletions

View File

@ -29,6 +29,13 @@
#include <tommath.h>
#include <stdbool.h>
/*
* static declarations
*/
static unsigned int get_degree(pb_poly const * const poly);
/**
* Initialize a mp_int and check if this was successful, the
* caller must free new_int with mp_clear().
@ -233,6 +240,23 @@ void pb_xor(pb_poly *a,
MP_XOR(&(a->terms[i]), &(b->terms[i]), &(c->terms[i]));
}
/**
* Get the degree of the polynomial.
*
* @param poly the polynomial
* @return the degree
*/
static unsigned int get_degree(pb_poly const * const poly)
{
unsigned int count = 0;
for (int i = 0; i < poly->alloc; i++)
if (mp_iszero(&(poly->terms[i])) == MP_NO)
count = i;
return count;
}
/**
* Invert the polynomial a modulo q.
*
@ -248,20 +272,17 @@ bool pb_inverse_poly_q(pb_poly * const a,
int k = 0,
j = 0,
v = 2;
int zero_poly_val = 1;
pb_poly *a_tmp, *b, *c, *f, *g, *degree_zero_poly;
pb_poly *a_tmp, *b, *c, *f, *g;
degree_zero_poly = build_polynom(&zero_poly_val, 1, ctx);
b = build_polynom(NULL, ctx->N, ctx);
b = build_polynom(NULL, ctx->N + 1, ctx);
mp_set(&(b->terms[0]), 1);
c = build_polynom(NULL, ctx->N, ctx);
f = build_polynom(NULL, ctx->N, ctx);
c = build_polynom(NULL, ctx->N + 1, ctx);
f = build_polynom(NULL, ctx->N + 1, ctx);
PB_COPY(a, f);
g = build_polynom(NULL, ctx->N, ctx);
g = build_polynom(NULL, ctx->N + 1, ctx);
mp_set(&(g->terms[0]), 1);
mp_neg(&(g->terms[0]), &(g->terms[0]));
mp_set(&(g->terms[ctx->N]), 1);
/* avoid side effects */
a_tmp = build_polynom(NULL, ctx->N, ctx);
PB_COPY(a, a_tmp);
@ -278,14 +299,10 @@ bool pb_inverse_poly_q(pb_poly * const a,
k++;
}
/* hope this does not blow up in our face */
pb_clamp(degree_zero_poly);
pb_clamp(f);
if (pb_cmp(f, degree_zero_poly) == PB_DEG_EQ)
if (get_degree(f) == 0)
goto OUT_OF_LOOP;
pb_clamp(g);
if (pb_cmp(f, g) == PB_DEG_LT) {
if (get_degree(f) < get_degree(g)) {
pb_exch(f, g);
pb_exch(b, c);
}
@ -318,7 +335,7 @@ OUT_OF_LOOP:
/* hope this does not blow up in our face */
pb_starmultiply(a_tmp, Fq, pb_tmp, ctx, v);
PB_SUB(pb_tmp2, pb_tmp, pb_tmp);
PB_MOD(pb_tmp, &tmp_v, pb_tmp, ctx);
PB_MOD(pb_tmp, &tmp_v, pb_tmp, ctx->N);
pb_starmultiply(Fq, pb_tmp, Fq, ctx, v);
mp_clear(&tmp_v);
@ -340,7 +357,6 @@ OUT_OF_LOOP:
delete_polynom(c);
delete_polynom(f);
delete_polynom(g);
delete_polynom(degree_zero_poly);
/* TODO: check if the f * Fq = 1 (mod p) condition holds true */

View File

@ -110,10 +110,10 @@
mp_error_to_string(result)); \
}
#define PB_MOD(poly_a, mp_int, poly_out, context) \
#define PB_MOD(poly_a, mp_int, poly_out, len) \
{ \
for (unsigned int i = 0; i < context->N; i++) \
MP_MOD(&(poly_a->terms[i]), mp_int, &(poly_out->terms[i])); \
for (unsigned int i = 0; i < len; i++) \
MP_DIV(&(poly_a->terms[i]), mp_int, NULL, &(poly_out->terms[i])); \
}
#define PB_COPY(...) \