From 782ccf05244eff14e9bc8f3a258c8f3210e85331 Mon Sep 17 00:00:00 2001 From: hasufell Date: Tue, 13 May 2014 00:20:46 +0200 Subject: [PATCH] POLY: improve error handling --- src/poly.c | 13 +++++++++---- src/poly.h | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/poly.c b/src/poly.c index 7f3aabd..5a152a5 100644 --- a/src/poly.c +++ b/src/poly.c @@ -300,11 +300,11 @@ void pb_xor(pb_poly *a, * Get the degree of the polynomial. * * @param poly the polynomial - * @return the degree + * @return the degree, -1 if polynom is empty */ -unsigned int get_degree(pb_poly const * const poly) +int get_degree(pb_poly const * const poly) { - unsigned int count = 0; + int count = -1; for (int i = 0; i < poly->alloc; i++) if (mp_iszero(&(poly->terms[i])) == MP_NO) @@ -354,7 +354,7 @@ static void pb_mod2_to_modq(pb_poly * const a, * @param a polynomial to invert (is allowed to be the same as param Fq) * @param Fq polynomial [out] * @param ctx NTRU context - * @return true/false for success/failure + * @return true if invertible, false if not */ bool pb_inverse_poly_q(pb_poly * const a, pb_poly *Fq, @@ -395,6 +395,8 @@ bool pb_inverse_poly_q(pb_poly * const a, MP_SET(&(f->terms[ctx->N]), 0); MP_SET(&(c->terms[0]), 0); k++; + if (get_degree(f) == -1) + return false; } if (get_degree(f) == 0) @@ -411,6 +413,9 @@ bool pb_inverse_poly_q(pb_poly * const a, k = k % ctx->N; + if (mp_cmp_d(&(b->terms[ctx->N]), 0) != MP_EQ) + return false; + /* Fq(x) = x^(N-k) * b(x) */ for (int i = ctx->N - 1; i >= 0; i--) { j = i - k; diff --git a/src/poly.h b/src/poly.h index 7eacb21..6f675c6 100644 --- a/src/poly.h +++ b/src/poly.h @@ -202,7 +202,7 @@ void pb_xor(pb_poly *a, pb_poly *c, const size_t len); -unsigned int get_degree(pb_poly const * const poly); +int get_degree(pb_poly const * const poly); bool pb_inverse_poly_q(pb_poly *a, pb_poly *Fq,