diff --git a/src/ascii_poly.c b/src/ascii_poly.c index 33a129f..da27c9c 100644 --- a/src/ascii_poly.c +++ b/src/ascii_poly.c @@ -40,13 +40,6 @@ #include -/* - * static function declaration - */ -static char *get_int_to_bin_str(uint8_t value); -static char *get_bin_arr_to_ascii(char *binary_rep); - - /** * Convert an integer to it's binary representation * as a string and return it. @@ -54,7 +47,25 @@ static char *get_bin_arr_to_ascii(char *binary_rep); * @param value the integer to convert * @return the binary representation as a newly allocated string */ -static char *get_int_to_bin_str(uint8_t value) +static char * +get_int_to_bin_str(uint8_t value); + +/** + * Converts a binary representation of multiple concatenated + * integers to the corresponding array of ascii chars, which + * is NULL-terminated. + * + * @param binary_rep the binary representation of multiple + * integers concatenated + * @return NULL-terminated array of corresponding ascii-chars, + * newly allocated + */ +static char * +get_bin_arr_to_ascii(char *binary_rep); + + +static char * +get_int_to_bin_str(uint8_t value) { int i; const size_t bin_string_size = ASCII_BITS + 1; @@ -70,19 +81,10 @@ static char *get_int_to_bin_str(uint8_t value) return bin_string; } -/** - * Converts a binary representation of multiple concatenated - * integers to the corresponding array of ascii chars, which - * is NULL-terminated. - * - * @param binary_rep the binary representation of multiple - * integers concatenated - * @return NULL-terminated array of corresponding ascii-chars, - * newly allocated - */ -static char *get_bin_arr_to_ascii(char *binary_rep) +static char * +get_bin_arr_to_ascii(char *binary_rep) { - const size_t int_arr_size = strlen(binary_rep) / 8; + const size_t int_arr_size = strlen(binary_rep) / ASCII_BITS; uint8_t int_arr[int_arr_size]; char *tmp_string = binary_rep; uint32_t i = 0; @@ -109,19 +111,13 @@ static char *get_bin_arr_to_ascii(char *binary_rep) return int_string; } -/** - * Convert an ascii string to an array of polyomials. - * - * @param to_poly the string to get into polynomial format - * @param ctx the NTRUEncrypt context - * @return newly allocated array of polynomials - */ -fmpz_poly_t **ascii_to_poly(char *to_poly, ntru_context *ctx) +fmpz_poly_t ** +ascii_to_poly(char *to_poly, ntru_context *ctx) { uint32_t i = 0, polyc = 0; char *cur = to_poly; - size_t out_size = CHAR_SIZE * (strlen(to_poly) * 8 + 1); + size_t out_size = CHAR_SIZE * (strlen(to_poly) * ASCII_BITS + 1); char *out = ntru_malloc(out_size); fmpz_poly_t **poly_array; @@ -159,14 +155,8 @@ fmpz_poly_t **ascii_to_poly(char *to_poly, ntru_context *ctx) return poly_array; } -/** - * Convert an array of polynomials back to a real string. - * - * @param poly_array the array of polynomials - * @param ctx the NTRUEncrypt context - * @return the real string - */ -char *poly_to_ascii(fmpz_poly_t **poly_array, ntru_context *ctx) +char * +poly_to_ascii(fmpz_poly_t **poly_array, ntru_context *ctx) { fmpz_poly_t *ascii_poly; char *binary_rep = NULL; diff --git a/src/ascii_poly.h b/src/ascii_poly.h index 59c32f3..4acdf89 100644 --- a/src/ascii_poly.h +++ b/src/ascii_poly.h @@ -35,8 +35,25 @@ #include -fmpz_poly_t **ascii_to_poly(char *to_poly, ntru_context *ctx); -char *poly_to_ascii(fmpz_poly_t **poly_array, ntru_context *ctx); +/** + * Convert an ascii string to an array of polyomials. + * + * @param to_poly the string to get into polynomial format + * @param ctx the NTRUEncrypt context + * @return newly allocated array of polynomials + */ +fmpz_poly_t ** +ascii_to_poly(char *to_poly, ntru_context *ctx); + +/** + * Convert an array of polynomials back to a real string. + * + * @param poly_array the array of polynomials + * @param ctx the NTRUEncrypt context + * @return the real string + */ +char * +poly_to_ascii(fmpz_poly_t **poly_array, ntru_context *ctx); #endif /* NTRU_ASCII_POLY_H_ */ diff --git a/src/context.h b/src/context.h index d534431..42bba7a 100644 --- a/src/context.h +++ b/src/context.h @@ -29,6 +29,10 @@ #ifndef NTRU_CONTEXT_H #define NTRU_CONTEXT_H + +#include + + /** * NTRU cryptosystem is specified by * the following triple. @@ -38,15 +42,15 @@ typedef struct { * maximal degree N - 1 for * all polynomials */ - unsigned int N; + uint32_t N; /** * large modulus */ - unsigned int q; + uint32_t q; /** * small modulus */ - unsigned int p; + uint32_t p; } ntru_context; #endif /* NTRU_CONTEXT_H */ diff --git a/src/decrypt.c b/src/decrypt.c index 185fff8..a0026c4 100644 --- a/src/decrypt.c +++ b/src/decrypt.c @@ -32,19 +32,8 @@ #include -/** - * Decryption of the given Polynom with the private key, its inverse - * and the fitting ntru_context - * - * @param encr_msg encrypted polynom with maximum length of N from - * the given context - * @param priv_key the polynom containing the private key to decrypt - * the message - * @param priv_key_inv the inverse polynome to the private key - * @param out the result polynom is written in here [out] - * @param ctx the ntru_context - */ -void ntru_decrypt_poly( +void +ntru_decrypt_poly( fmpz_poly_t encr_msg, fmpz_poly_t priv_key, fmpz_poly_t priv_key_inv, diff --git a/src/decrypt.h b/src/decrypt.h index e4447f6..01a9f67 100644 --- a/src/decrypt.h +++ b/src/decrypt.h @@ -35,7 +35,20 @@ #include -void ntru_decrypt_poly( +/** + * Decryption of the given Polynom with the private key, its inverse + * and the fitting ntru_context + * + * @param encr_msg encrypted polynom with maximum length of N from + * the given context + * @param priv_key the polynom containing the private key to decrypt + * the message + * @param priv_key_inv the inverse polynome to the private key + * @param out the result polynom is written in here [out] + * @param ctx the ntru_context + */ +void +ntru_decrypt_poly( fmpz_poly_t encr_msg, fmpz_poly_t priv_key, fmpz_poly_t priv_key_inv, diff --git a/src/encrypt.c b/src/encrypt.c index 9a366d3..388a61d 100644 --- a/src/encrypt.c +++ b/src/encrypt.c @@ -32,23 +32,8 @@ #include -/** - * encrypt the msg, using the math: - * e = (h ∗ r) + m (mod q) - * - * e = the encrypted poly - * h = the public key - * r = the random poly - * m = the message poly - * q = large mod - * - * @param msg pb_poly* the message to encrypt - * @param pub_key pb_poly* the public key - * @param rnd pb_poly* the random poly - * @param out pb_poly* the output poly [out] - * @param ctx ntru_context* the ntru context - */ -void ntru_encrypt_poly( +void +ntru_encrypt_poly( fmpz_poly_t msg, fmpz_poly_t pub_key, fmpz_poly_t rnd, diff --git a/src/encrypt.h b/src/encrypt.h index 886c3b8..b90254f 100644 --- a/src/encrypt.h +++ b/src/encrypt.h @@ -36,7 +36,24 @@ #include -void ntru_encrypt_poly( +/** + * encrypt the msg, using the math: + * e = (h ∗ r) + m (mod q) + * + * e = the encrypted poly + * h = the public key + * r = the random poly + * m = the message poly + * q = large mod + * + * @param msg pb_poly* the message to encrypt + * @param pub_key pb_poly* the public key + * @param rnd pb_poly* the random poly + * @param out pb_poly* the output poly [out] + * @param ctx ntru_context* the ntru context + */ +void +ntru_encrypt_poly( fmpz_poly_t msg, fmpz_poly_t pub_key, fmpz_poly_t rnd, diff --git a/src/keypair.c b/src/keypair.c index 82952ad..8abb6e6 100644 --- a/src/keypair.c +++ b/src/keypair.c @@ -36,17 +36,8 @@ #include -/** - * Creates an NTRU key pair, - * consisting of public and private - * components. - * - * @param f a random polynomial - * @param g a random polynomial - * @param pair store private and public components here [out] - * @param ctx the NTRU context - */ -bool ntru_create_keypair( +bool +ntru_create_keypair( fmpz_poly_t f, fmpz_poly_t g, keypair *pair, @@ -91,14 +82,8 @@ cleanup: return retval; } -/** - * Used to free the inner structure - * of a keypair. This will not call free() - * on the pair itself. - * - * @param pair the pair to free the inner structure of - */ -void ntru_delete_keypair(keypair *pair) +void +ntru_delete_keypair(keypair *pair) { fmpz_poly_clear(pair->priv_inv); fmpz_poly_clear(pair->priv); diff --git a/src/keypair.h b/src/keypair.h index 71f0154..307e65f 100644 --- a/src/keypair.h +++ b/src/keypair.h @@ -62,13 +62,32 @@ struct keypair { }; -bool ntru_create_keypair( +/** + * Creates an NTRU key pair, + * consisting of public and private + * components. + * + * @param f a random polynomial + * @param g a random polynomial + * @param pair store private and public components here [out] + * @param ctx the NTRU context + */ +bool +ntru_create_keypair( fmpz_poly_t f, fmpz_poly_t g, keypair *pair, ntru_context *ctx); -void ntru_delete_keypair(keypair *pair); +/** + * Used to free the inner structure + * of a keypair. This will not call free() + * on the pair itself. + * + * @param pair the pair to free the inner structure of + */ +void +ntru_delete_keypair(keypair *pair); #endif /* NTRU_KEYPAIR_H */ diff --git a/src/mem.c b/src/mem.c index f17fa61..73e032c 100644 --- a/src/mem.c +++ b/src/mem.c @@ -32,14 +32,8 @@ #include -/** - * Allocate memory of size and return - * a void pointer. - * - * @param size of the memory to allocate in bytes - * @return void pointer to the beginning of the allocated memory block - */ -void *ntru_malloc(size_t size) +void * +ntru_malloc(size_t size) { void *ptr; @@ -54,15 +48,8 @@ void *ntru_malloc(size_t size) return ptr; } -/** - * Allocate memory of size and return - * a void pointer. The memory is zeroed. - * - * @param nmemb amount of blocks to allocate - * @param size of the memory blocks to allocate in bytes - * @return void pointer to the beginning of the allocated memory block - */ -void *ntru_calloc(size_t nmemb, size_t size) +void * +ntru_calloc(size_t nmemb, size_t size) { void *ptr; diff --git a/src/mem.h b/src/mem.h index 42e9a50..0fea006 100644 --- a/src/mem.h +++ b/src/mem.h @@ -47,8 +47,26 @@ } -void *ntru_malloc(size_t size); -void *ntru_calloc(size_t nmemb, size_t size); +/** + * Allocate memory of size and return + * a void pointer. + * + * @param size of the memory to allocate in bytes + * @return void pointer to the beginning of the allocated memory block + */ +void * +ntru_malloc(size_t size); + +/** + * Allocate memory of size and return + * a void pointer. The memory is zeroed. + * + * @param nmemb amount of blocks to allocate + * @param size of the memory blocks to allocate in bytes + * @return void pointer to the beginning of the allocated memory block + */ +void * +ntru_calloc(size_t nmemb, size_t size); #endif /* NTRU_MEM_H */ diff --git a/src/poly.c b/src/poly.c index be9f522..f82a7a0 100644 --- a/src/poly.c +++ b/src/poly.c @@ -34,6 +34,7 @@ #include #include +#include #include #include #include @@ -42,14 +43,6 @@ #include -/* - * static declarations - */ -static void poly_mod2_to_modq(fmpz_poly_t a, - fmpz_poly_t Fq, - ntru_context *ctx); - - /** * Find the inverse polynomial modulo a power of 2, * which is q. @@ -58,7 +51,14 @@ static void poly_mod2_to_modq(fmpz_poly_t a, * @param Fq polynomial [out] * @param ctx NTRU context */ -static void poly_mod2_to_modq(fmpz_poly_t a, +static +void poly_mod2_to_modq(fmpz_poly_t a, + fmpz_poly_t Fq, + ntru_context *ctx); + + +static void +poly_mod2_to_modq(fmpz_poly_t a, fmpz_poly_t Fq, ntru_context *ctx) { @@ -85,51 +85,27 @@ static void poly_mod2_to_modq(fmpz_poly_t a, } -/** - * Initializes and builds a polynomial with the - * coefficient values of c[] of size len within NTRU - * context ctx and returns a newly allocated polynomial. - * For an empty polynom, both parameters can be NULL/0. - * - * @param new_poly the polynomial to initialize and - * fill with coefficients - * @param c array of polynomial coefficients, can be NULL - * @param len size of the coefficient array, can be 0 - * @return newly allocated polynomial pointer, must be freed - * with fmpz_poly_clear() - */ -void poly_new(fmpz_poly_t new_poly, +void +poly_new(fmpz_poly_t new_poly, int const * const c, const size_t len) { fmpz_poly_init(new_poly); - for (unsigned int i = 0; i < len; i++) + for (uint32_t i = 0; i < len; i++) fmpz_poly_set_coeff_si(new_poly, i, c[i]); } -/** - * This deletes the internal structure of a polynomial, - * and frees the pointer. - * - * @param poly the polynomial to delete - */ -void poly_delete(fmpz_poly_t poly) +void +poly_delete(fmpz_poly_t poly) { fmpz_poly_clear(poly); } -/** - * 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(). - * - * @param poly_array the polynomial array - */ -void poly_delete_array(fmpz_poly_t **poly_array) +void +poly_delete_array(fmpz_poly_t **poly_array) { - unsigned int i = 0; + uint32_t i = 0; while(poly_array[i]) { poly_delete(*(poly_array[i])); @@ -139,15 +115,8 @@ void poly_delete_array(fmpz_poly_t **poly_array) free(poly_array); } -/** - * This deletes the internal structure of all polynomials, - * and frees the pointers. - * You must call this with NULL as last argument! - * - * @param poly the polynomial to delete - * @param ... follow up polynomials - */ -void poly_delete_all(fmpz_poly_t poly, ...) +void +poly_delete_all(fmpz_poly_t poly, ...) { fmpz_poly_struct *next_poly; va_list args; @@ -161,20 +130,9 @@ void poly_delete_all(fmpz_poly_t poly, ...) va_end(args); } -/** - * 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. - * - * @param a the polynom to apply the modulus to - * @param mod the modulus - */ -void fmpz_poly_mod_unsigned(fmpz_poly_t a, - unsigned int mod) +void +fmpz_poly_mod_unsigned(fmpz_poly_t a, + uint32_t mod) { nmod_poly_t nmod_tmp; @@ -186,20 +144,9 @@ void fmpz_poly_mod_unsigned(fmpz_poly_t a, nmod_poly_clear(nmod_tmp); } -/** - * 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. - * - * @param a the polynom to apply the modulus to - * @param mod the modulus - */ -void fmpz_poly_mod(fmpz_poly_t a, - unsigned int mod) +void +fmpz_poly_mod(fmpz_poly_t a, + uint32_t mod) { nmod_poly_t nmod_tmp; @@ -211,16 +158,8 @@ void fmpz_poly_mod(fmpz_poly_t a, nmod_poly_clear(nmod_tmp); } -/** - * 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. - * - * @param poly the polynom we want to change a coefficient of - * @param n the coefficient we want to set - * @param x the value to assign to the coefficient - */ -void fmpz_poly_set_coeff_fmpz_n(fmpz_poly_t poly, slong n, +void +fmpz_poly_set_coeff_fmpz_n(fmpz_poly_t poly, slong n, const fmpz_t x) { if (x) @@ -229,15 +168,8 @@ void fmpz_poly_set_coeff_fmpz_n(fmpz_poly_t poly, slong n, fmpz_poly_set_coeff_si(poly, n, 0); } -/** - * Wrapper around fmpz_invmod() where we convert - * mod to an fmpz_t implicitly. - * - * @param f result [out] - * @param g the inverse - * @param mod the modulo - */ -int fmpz_invmod_ui(fmpz_t f, const fmpz_t g, unsigned int mod) +int +fmpz_invmod_ui(fmpz_t f, const fmpz_t g, uint32_t mod) { fmpz_t modulus; @@ -246,11 +178,8 @@ int fmpz_invmod_ui(fmpz_t f, const fmpz_t g, unsigned int mod) return fmpz_invmod(f, g, modulus); } -/** - * The same as fmpz_add() except that it handles NULL - * pointer for g and h. - */ -void fmpz_add_n(fmpz_t f, const fmpz_t g, const fmpz_t h) +void +fmpz_add_n(fmpz_t f, const fmpz_t g, const fmpz_t h) { if (!g && !h) { fmpz_zero(f); @@ -264,21 +193,12 @@ void fmpz_add_n(fmpz_t f, const fmpz_t g, const fmpz_t h) } } -/** - * Starmultiplication, as follows: - * c = a * b mod (x^N − 1) - * - * @param a polynom to multiply (can be the same as c) - * @param b polynom to multiply - * @param c polynom [out] - * @param ctx NTRU context - * @param modulus whether we use p or q - */ -void poly_starmultiply(fmpz_poly_t a, +void +poly_starmultiply(fmpz_poly_t a, fmpz_poly_t b, fmpz_poly_t c, ntru_context *ctx, - unsigned int modulus) + uint32_t modulus) { fmpz_poly_t a_tmp; fmpz_t c_coeff_k; @@ -329,19 +249,8 @@ void poly_starmultiply(fmpz_poly_t a, fmpz_poly_clear(a_tmp); } -/** - * 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." - * - * @param a polynomial to invert (is allowed to be the same as param Fq) - * @param Fq polynomial [out] - * @param ctx NTRU context - * @return true if invertible, false if not - */ -bool poly_inverse_poly_q(fmpz_poly_t a, +bool +poly_inverse_poly_q(fmpz_poly_t a, fmpz_poly_t Fq, ntru_context *ctx) { @@ -374,7 +283,7 @@ bool poly_inverse_poly_q(fmpz_poly_t a, while (1) { while (fmpz_is_zero(fmpz_poly_get_coeff_ptr(f, 0))) { - for (unsigned int i = 1; i <= ctx->N; i++) { + for (uint32_t i = 1; i <= ctx->N; i++) { fmpz *f_coeff = fmpz_poly_get_coeff_ptr(f, i); fmpz *c_coeff = fmpz_poly_get_coeff_ptr(c, ctx->N - i); @@ -455,16 +364,8 @@ cleanup: return retval; } -/** - * 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." - * - * @param a polynomial to invert - * @param Fp polynomial [out] - * @param ctx NTRU context - */ -bool poly_inverse_poly_p(fmpz_poly_t a, +bool +poly_inverse_poly_p(fmpz_poly_t a, fmpz_poly_t Fp, ntru_context *ctx) { @@ -497,7 +398,7 @@ bool poly_inverse_poly_p(fmpz_poly_t a, while (1) { while (fmpz_is_zero(fmpz_poly_get_coeff_ptr(f, 0))) { - for (unsigned int i = 1; i <= ctx->N; i++) { + for (uint32_t i = 1; i <= ctx->N; i++) { fmpz *f_coeff_tmp = fmpz_poly_get_coeff_ptr(f, i); fmpz *c_coeff_tmp = fmpz_poly_get_coeff_ptr(c, ctx->N - i); @@ -631,24 +532,15 @@ cleanup: return retval; } -/** - * Draws a polynomial to stdout. - * - * @param poly draw this - */ -void poly_draw(fmpz_poly_t poly) +void +poly_draw(fmpz_poly_t poly) { fmpz_poly_print(poly); flint_printf("\n"); } -/** - * Draws a polynomial to stdout, - * in pretty format. - * - * @param poly draw this - */ -void poly_draw_pretty(fmpz_poly_t poly) +void +poly_draw_pretty(fmpz_poly_t poly) { fmpz_poly_print_pretty(poly, "x"); flint_printf("\n"); diff --git a/src/poly.h b/src/poly.h index 50d3600..ff2891f 100644 --- a/src/poly.h +++ b/src/poly.h @@ -33,54 +33,191 @@ #include #include +#include #include #include -void poly_new(fmpz_poly_t new_poly, +/** + * Initializes and builds a polynomial with the + * coefficient values of c[] of size len within NTRU + * context ctx and returns a newly allocated polynomial. + * For an empty polynom, both parameters can be NULL/0. + * + * @param new_poly the polynomial to initialize and + * fill with coefficients + * @param c array of polynomial coefficients, can be NULL + * @param len size of the coefficient array, can be 0 + * @return newly allocated polynomial pointer, must be freed + * with fmpz_poly_clear() + */ +void +poly_new(fmpz_poly_t new_poly, int const * const c, const size_t len); -void poly_delete(fmpz_poly_t poly); +/** + * This deletes the internal structure of a polynomial, + * and frees the pointer. + * + * @param poly the polynomial to delete + */ +void +poly_delete(fmpz_poly_t poly); -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(). + * + * @param poly_array the polynomial array + */ +void +poly_delete_array(fmpz_poly_t **poly_array); -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! + * + * @param poly the polynomial to delete + * @param ... follow up polynomials + */ +void +poly_delete_all(fmpz_poly_t poly, ...); -void fmpz_poly_mod_unsigned(fmpz_poly_t a, - unsigned int 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. + * + * @param a the polynom to apply the modulus to + * @param mod the modulus + */ +void +fmpz_poly_mod_unsigned(fmpz_poly_t a, + uint32_t mod); -void fmpz_poly_mod(fmpz_poly_t a, - unsigned int 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. + * + * @param a the polynom to apply the modulus to + * @param mod the modulus + */ +void +fmpz_poly_mod(fmpz_poly_t a, + uint32_t mod); -void fmpz_poly_set_coeff_fmpz_n(fmpz_poly_t poly, +/** + * 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. + * + * @param poly the polynom we want to change a coefficient of + * @param n the coefficient we want to set + * @param x the value to assign to the coefficient + */ +void +fmpz_poly_set_coeff_fmpz_n(fmpz_poly_t poly, slong n, const fmpz_t x); -int fmpz_invmod_ui(fmpz_t f, +/** + * Wrapper around fmpz_invmod() where we convert + * mod to an fmpz_t implicitly. + * + * @param f result [out] + * @param g the inverse + * @param mod the modulo + */ +int +fmpz_invmod_ui(fmpz_t f, const fmpz_t g, - unsigned int mod); + uint32_t mod); -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. + */ +void +fmpz_add_n(fmpz_t f, const fmpz_t g, const fmpz_t h); -void poly_starmultiply(fmpz_poly_t a, +/** + * Starmultiplication, as follows: + * c = a * b mod (x^N − 1) + * + * @param a polynom to multiply (can be the same as c) + * @param b polynom to multiply + * @param c polynom [out] + * @param ctx NTRU context + * @param modulus whether we use p or q + */ +void +poly_starmultiply(fmpz_poly_t a, fmpz_poly_t b, fmpz_poly_t c, ntru_context *ctx, - unsigned int modulus); + uint32_t modulus); -bool poly_inverse_poly_q(fmpz_poly_t a, +/** + * 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." + * + * @param a polynomial to invert (is allowed to be the same as param Fq) + * @param Fq polynomial [out] + * @param ctx NTRU context + * @return true if invertible, false if not + */ +bool +poly_inverse_poly_q(fmpz_poly_t a, fmpz_poly_t Fq, ntru_context *ctx); -bool poly_inverse_poly_p(fmpz_poly_t a, +/** + * 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." + * + * @param a polynomial to invert + * @param Fp polynomial [out] + * @param ctx NTRU context + */ +bool +poly_inverse_poly_p(fmpz_poly_t a, fmpz_poly_t Fp, ntru_context *ctx); -void poly_draw(fmpz_poly_t poly); +/** + * Draws a polynomial to stdout. + * + * @param poly draw this + */ +void +poly_draw(fmpz_poly_t poly); -void poly_draw_pretty(fmpz_poly_t poly); +/** + * Draws a polynomial to stdout, + * in pretty format. + * + * @param poly draw this + */ +void +poly_draw_pretty(fmpz_poly_t poly); #endif /* NTRU_POLY_H */ diff --git a/src/rand.c b/src/rand.c index 2e8f602..7e7986e 100644 --- a/src/rand.c +++ b/src/rand.c @@ -46,7 +46,7 @@ static mp_digit get_random_ternary(mp_digit random_int, int* sign); static mp_int *get_random_bigint(mp_int *upper_bound, mp_int *lower_bound, int entropy_source); -static unsigned int check_allowed_zeros(pb_poly *polynom); +static uint32_t check_allowed_zeros(pb_poly *polynom); /** * Reads a single mp_digit out of /dev/random and returns this mp_digit @@ -149,9 +149,9 @@ static mp_int *get_random_bigint(mp_int *upper_bound, * -1 if the polynom zero coefficients are over * PERCENTAGE_OF_ZERO_ALLOWED percent */ -static unsigned int check_allowed_zeros(pb_poly *polynom) +static uint32_t check_allowed_zeros(pb_poly *polynom) { - unsigned int result = -1; + uint32_t result = -1; //TODO return result; } @@ -175,7 +175,7 @@ pb_poly *ntru_get_random_poly_ternary(size_t length, int entropy_source) init_polynom_size(poly, &chara, length); mp_clear(&chara); - for (unsigned int i = 0; i < length; i++) { + for (uint32_t i = 0; i < length; i++) { int sign; if (entropy_source == GET_INT_FROM_RRAND) { coefficient = get_int_dev_random();