POLY: first try of pb_starmultiply()
This commit is contained in:
parent
0d179a0e7d
commit
2d2ccfbf3f
54
src/poly.c
54
src/poly.c
@ -146,6 +146,60 @@ void delete_polynom(pb_poly *poly)
|
|||||||
free(poly);
|
free(poly);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starmultiplication, as follows:
|
||||||
|
* c = a * b mod x^(N − 1)
|
||||||
|
*
|
||||||
|
* @param a polynom to multiply
|
||||||
|
* @param b polynom to multiply
|
||||||
|
* @param c polynom [out]
|
||||||
|
* @param ctx NTRU context
|
||||||
|
* @param modulus whether we use p or q
|
||||||
|
*/
|
||||||
|
void pb_starmultiply(pb_poly *a,
|
||||||
|
pb_poly *b,
|
||||||
|
pb_poly *c,
|
||||||
|
ntru_context *ctx,
|
||||||
|
unsigned int modulus)
|
||||||
|
{
|
||||||
|
for (int k = ctx->N - 1; k >= 0; k--) {
|
||||||
|
int j;
|
||||||
|
j = k + 1;
|
||||||
|
|
||||||
|
for (int i = ctx->N - 1; i >= 0; i--) {
|
||||||
|
if (j == (int)(ctx->N))
|
||||||
|
j = 0;
|
||||||
|
if (mp_cmp_d(&(a->terms[i]), (mp_digit)0) != MP_EQ &&
|
||||||
|
mp_cmp_d(&(b->terms[j]), (mp_digit)0) != MP_EQ) {
|
||||||
|
int result;
|
||||||
|
mp_int mp_modulus;
|
||||||
|
mp_int mp_tmp;
|
||||||
|
|
||||||
|
init_integer(&mp_tmp);
|
||||||
|
init_integer(&mp_modulus);
|
||||||
|
mp_set_int(&mp_modulus, (unsigned long)(modulus));
|
||||||
|
|
||||||
|
if ((result = mp_mul(&(a->terms[i]),
|
||||||
|
&(b->terms[j]), &mp_tmp)) != MP_OKAY)
|
||||||
|
NTRU_ABORT("Error multiplying terms. %s",
|
||||||
|
mp_error_to_string(result));
|
||||||
|
if ((result = mp_add(&(c->terms[k]),
|
||||||
|
&mp_tmp, &(c->terms[k]))) != MP_OKAY)
|
||||||
|
NTRU_ABORT("Error multiplying terms. %s",
|
||||||
|
mp_error_to_string(result));
|
||||||
|
if ((result = mp_mod(&(c->terms[k]),
|
||||||
|
&mp_modulus, &(c->terms[k]))) != MP_OKAY)
|
||||||
|
NTRU_ABORT("Error multiplying terms. %s",
|
||||||
|
mp_error_to_string(result));
|
||||||
|
|
||||||
|
mp_clear(&mp_modulus);
|
||||||
|
mp_clear(&mp_tmp);
|
||||||
|
}
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Print the polynomial in a human readable format to stdout.
|
* Print the polynomial in a human readable format to stdout.
|
||||||
*
|
*
|
||||||
|
@ -41,6 +41,12 @@ pb_poly *build_polynom(int const * const c,
|
|||||||
|
|
||||||
void delete_polynom(pb_poly *new_poly);
|
void delete_polynom(pb_poly *new_poly);
|
||||||
|
|
||||||
|
void pb_starmultiply(pb_poly *a,
|
||||||
|
pb_poly *b,
|
||||||
|
pb_poly *c,
|
||||||
|
ntru_context *ctx,
|
||||||
|
unsigned int modulus);
|
||||||
|
|
||||||
void draw_polynom(pb_poly * const poly);
|
void draw_polynom(pb_poly * const poly);
|
||||||
|
|
||||||
#endif /* NTRU_POLY_H */
|
#endif /* NTRU_POLY_H */
|
||||||
|
Loading…
Reference in New Issue
Block a user