post quantum cryptography
Highly optimized implementation of the NTRUEncrypt algorithm
|
operations on polynomials More...
#include "ntru_err.h"
#include "ntru_mem.h"
#include "ntru_params.h"
#include "ntru_poly.h"
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fmpz_poly.h>
#include <fmpz.h>
Go to the source code of this file.
Functions | |
static void | poly_mod2_to_modq (const fmpz_poly_t a, fmpz_poly_t Fq, const ntru_params *params) |
int | fmpz_cmp_si_n (const fmpz_t f, slong g) |
void | poly_new (fmpz_poly_t new_poly, int const *const c, const size_t len) |
void | poly_delete (fmpz_poly_t poly) |
void | poly_delete_array (fmpz_poly_t **poly_array) |
void | poly_delete_all (fmpz_poly_t poly,...) |
void | fmpz_poly_mod_unsigned (fmpz_poly_t a, const uint32_t mod) |
void | fmpz_poly_mod (fmpz_poly_t a, const uint32_t mod) |
void | fmpz_poly_set_coeff_fmpz_n (fmpz_poly_t poly, slong n, const fmpz_t x) |
int | fmpz_invmod_ui (fmpz_t f, const fmpz_t g, const uint32_t mod) |
void | fmpz_add_n (fmpz_t f, const fmpz_t g, const fmpz_t h) |
void | poly_starmultiply (const fmpz_poly_t a, const fmpz_poly_t b, fmpz_poly_t c, const ntru_params *params, uint32_t modulus) |
bool | poly_inverse_poly_q (const fmpz_poly_t a, fmpz_poly_t Fq, const ntru_params *params) |
bool | poly_inverse_poly_p (const fmpz_poly_t a, fmpz_poly_t Fp, const ntru_params *params) |
void | poly_draw (const fmpz_poly_t poly) |
void | poly_draw_pretty (const fmpz_poly_t poly) |
operations on polynomials
This files purpose is to handle polynomials in general, allowing modification, arithmetic and common algorithms like inverting them.
Definition in file ntru_poly.c.
void fmpz_add_n | ( | fmpz_t | f, |
const fmpz_t | g, | ||
const fmpz_t | h | ||
) |
The same as fmpz_add() except that it handles NULL pointer for g and h.
Definition at line 222 of file ntru_poly.c.
int fmpz_cmp_si_n | ( | const fmpz_t | f, |
slong | g | ||
) |
The same as fmpz_cmp_si except that it will interpret f as a 0-coefficient if it is a NULL pointer.
f | the fmpz value to use for comparison |
g | the signed long integer to use for comparison |
Definition at line 93 of file ntru_poly.c.
int fmpz_invmod_ui | ( | fmpz_t | f, |
const fmpz_t | g, | ||
const uint32_t | mod | ||
) |
Wrapper around fmpz_invmod() where we convert mod to an fmpz_t implicitly.
f | result [out] |
g | the inverse |
mod | the modulo |
Definition at line 210 of file ntru_poly.c.
void fmpz_poly_mod | ( | fmpz_poly_t | a, |
const uint32_t | mod | ||
) |
Calls fmpz_poly_get_nmod_poly() and fmpz_poly_set_nmod_poly() in a row, so we don't have to deal with the intermediate nmod_poly_t type if we don't need it.
This also normalises the coefficients to the interval -m/2 <= r < m/2.
a | the polynom to apply the modulus to |
mod | the modulus |
Definition at line 182 of file ntru_poly.c.
void fmpz_poly_mod_unsigned | ( | fmpz_poly_t | a, |
const uint32_t | mod | ||
) |
Calls fmpz_poly_get_nmod_poly() and fmpz_poly_set_nmod_poly_unsigned() in a row, so we don't have to deal with the intermediate nmod_poly_t type if we don't need it.
This also normalises the coefficients to the interval 0 <= r < m.
a | the polynom to apply the modulus to |
mod | the modulus |
Definition at line 166 of file ntru_poly.c.
void fmpz_poly_set_coeff_fmpz_n | ( | fmpz_poly_t | poly, |
slong | n, | ||
const fmpz_t | x | ||
) |
The same as fmpz_poly_set_coeff_fmpz() except that it will take care of null-pointer coefficients and use fmpz_poly_set_coeff_si() in that case.
poly | the polynom we want to change a coefficient of |
n | the coefficient we want to set |
x | the value to assign to the coefficient |
Definition at line 198 of file ntru_poly.c.
void poly_delete | ( | fmpz_poly_t | poly | ) |
This deletes the internal structure of a polynomial, and frees the pointer.
poly | the polynomial to delete |
Definition at line 123 of file ntru_poly.c.
void poly_delete_all | ( | fmpz_poly_t | poly, |
... | |||
) |
This deletes the internal structure of all polynomials, and frees the pointers. You must call this with NULL as last argument!
poly | the polynomial to delete |
... | follow up polynomials |
Definition at line 149 of file ntru_poly.c.
void poly_delete_array | ( | fmpz_poly_t ** | poly_array | ) |
Delete the internal structure of a polynomial array which must be NULL terminated. It is expected that poly_array is not on the stack and was obtained by a function like ascii_to_poly().
poly_array | the polynomial array |
Definition at line 131 of file ntru_poly.c.
void poly_draw | ( | const fmpz_poly_t | poly | ) |
void poly_draw_pretty | ( | const fmpz_poly_t | poly | ) |
Draws a polynomial to stdout, in pretty format.
poly | draw this |
Definition at line 583 of file ntru_poly.c.
bool poly_inverse_poly_p | ( | const fmpz_poly_t | a, |
fmpz_poly_t | Fp, | ||
const ntru_params * | params | ||
) |
Compute the inverse of a polynomial in (Z/pZ)[X]/(X^N - 1). See NTRU Cryptosystems Tech Report #014 "Almost Inverses and Fast NTRU Key Creation."
a | polynomial to invert |
Fp | polynomial [out] |
params | NTRU parameters |
Definition at line 409 of file ntru_poly.c.
bool poly_inverse_poly_q | ( | const fmpz_poly_t | a, |
fmpz_poly_t | Fq, | ||
const ntru_params * | params | ||
) |
Compute the inverse of a polynomial in modulo a power of 2, which is q. This is based off the pseudo-code for "Inversion in (Z/2Z)[X](X^N - 1)" and "Inversion in (Z/p^r Z)[X](X^N - 1)". See NTRU Cryptosystems Tech Report #014 "Almost Inverses and Fast NTRU Key Creation."
a | polynomial to invert (is allowed to be the same as param Fq) |
Fq | polynomial [out] |
params | NTRU parameters |
Definition at line 297 of file ntru_poly.c.
|
static |
Find the inverse polynomial modulo a power of 2, which is q.
a | polynomial to invert |
Fq | polynomial [out] |
params | NTRU parameters |
Definition at line 63 of file ntru_poly.c.
void poly_new | ( | fmpz_poly_t | new_poly, |
int const *const | c, | ||
const size_t | len | ||
) |
Initializes and builds a polynomial with the coefficient values of c[] of size len within NTRU parameters and returns a newly allocated polynomial. For an empty polynom, both c and len can be NULL/0.
new_poly | the polynomial to initialize and fill with coefficients [out] |
c | array of polynomial coefficients, can be NULL |
len | size of the coefficient array, can be 0 |
Definition at line 110 of file ntru_poly.c.
void poly_starmultiply | ( | const fmpz_poly_t | a, |
const fmpz_poly_t | b, | ||
fmpz_poly_t | c, | ||
const ntru_params * | params, | ||
uint32_t | modulus | ||
) |
Starmultiplication, as follows: c = a * b mod (x^N − 1)
a | polynom to multiply (can be the same as c) |
b | polynom to multiply |
c | polynom [out] |
params | NTRU parameters |
modulus | whether we use p or q |
Definition at line 239 of file ntru_poly.c.