post quantum cryptography
Highly optimized implementation of the NTRUEncrypt algorithm
Functions
/home/travis/build/hasufell/pqc/src/ntru_poly.c File Reference

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>
Include dependency graph for ntru_poly.c:

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)

Detailed Description

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.


Function Documentation

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.

Parameters:
fthe fmpz value to use for comparison
gthe signed long integer to use for comparison
Returns:
negative value if f < g, positiv evalue if g < f, otherwise 0

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.

Parameters:
fresult [out]
gthe inverse
modthe 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.

Parameters:
athe polynom to apply the modulus to
modthe 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.

Parameters:
athe polynom to apply the modulus to
modthe 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.

Parameters:
polythe polynom we want to change a coefficient of
nthe coefficient we want to set
xthe 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.

Parameters:
polythe 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!

Parameters:
polythe 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().

Parameters:
poly_arraythe polynomial array

Definition at line 131 of file ntru_poly.c.

void poly_draw ( const fmpz_poly_t  poly)

Draws a polynomial to stdout.

Parameters:
polydraw this

Definition at line 574 of file ntru_poly.c.

void poly_draw_pretty ( const fmpz_poly_t  poly)

Draws a polynomial to stdout, in pretty format.

Parameters:
polydraw 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."

Parameters:
apolynomial to invert
Fppolynomial [out]
paramsNTRU parameters
Returns:
true if invertible, false if not

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."

Parameters:
apolynomial to invert (is allowed to be the same as param Fq)
Fqpolynomial [out]
paramsNTRU parameters
Returns:
true if invertible, false if not

Definition at line 297 of file ntru_poly.c.

static void poly_mod2_to_modq ( const fmpz_poly_t  a,
fmpz_poly_t  Fq,
const ntru_params params 
) [static]

Find the inverse polynomial modulo a power of 2, which is q.

Parameters:
apolynomial to invert
Fqpolynomial [out]
paramsNTRU 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.

Parameters:
new_polythe polynomial to initialize and fill with coefficients [out]
carray of polynomial coefficients, can be NULL
lensize of the coefficient array, can be 0
Returns:
newly allocated polynomial pointer, must be freed with fmpz_poly_clear()

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)

Parameters:
apolynom to multiply (can be the same as c)
bpolynom to multiply
cpolynom [out]
paramsNTRU parameters
moduluswhether we use p or q

Definition at line 239 of file ntru_poly.c.

 All Data Structures Files Functions Variables Typedefs Defines