POLY: improve error handling
This commit is contained in:
parent
30e18177e8
commit
782ccf0524
13
src/poly.c
13
src/poly.c
@ -300,11 +300,11 @@ void pb_xor(pb_poly *a,
|
|||||||
* Get the degree of the polynomial.
|
* Get the degree of the polynomial.
|
||||||
*
|
*
|
||||||
* @param poly 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++)
|
for (int i = 0; i < poly->alloc; i++)
|
||||||
if (mp_iszero(&(poly->terms[i])) == MP_NO)
|
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 a polynomial to invert (is allowed to be the same as param Fq)
|
||||||
* @param Fq polynomial [out]
|
* @param Fq polynomial [out]
|
||||||
* @param ctx NTRU context
|
* @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,
|
bool pb_inverse_poly_q(pb_poly * const a,
|
||||||
pb_poly *Fq,
|
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(&(f->terms[ctx->N]), 0);
|
||||||
MP_SET(&(c->terms[0]), 0);
|
MP_SET(&(c->terms[0]), 0);
|
||||||
k++;
|
k++;
|
||||||
|
if (get_degree(f) == -1)
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (get_degree(f) == 0)
|
if (get_degree(f) == 0)
|
||||||
@ -411,6 +413,9 @@ bool pb_inverse_poly_q(pb_poly * const a,
|
|||||||
|
|
||||||
k = k % ctx->N;
|
k = k % ctx->N;
|
||||||
|
|
||||||
|
if (mp_cmp_d(&(b->terms[ctx->N]), 0) != MP_EQ)
|
||||||
|
return false;
|
||||||
|
|
||||||
/* Fq(x) = x^(N-k) * b(x) */
|
/* Fq(x) = x^(N-k) * b(x) */
|
||||||
for (int i = ctx->N - 1; i >= 0; i--) {
|
for (int i = ctx->N - 1; i >= 0; i--) {
|
||||||
j = i - k;
|
j = i - k;
|
||||||
|
@ -202,7 +202,7 @@ void pb_xor(pb_poly *a,
|
|||||||
pb_poly *c,
|
pb_poly *c,
|
||||||
const size_t len);
|
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,
|
bool pb_inverse_poly_q(pb_poly *a,
|
||||||
pb_poly *Fq,
|
pb_poly *Fq,
|
||||||
|
Loading…
Reference in New Issue
Block a user