From a265a89a0d409bb028feb17fd33298b8c5ccaba5 Mon Sep 17 00:00:00 2001 From: hasufell Date: Sun, 1 Jun 2014 20:41:29 +0200 Subject: [PATCH] ALL: use const modifiers on parameters where possible --- src/ascii_poly.c | 29 +++++++++++++++-------------- src/ascii_poly.h | 23 ++++++++++++----------- src/decrypt.c | 16 ++++++++-------- src/decrypt.h | 16 ++++++++-------- src/encrypt.c | 16 ++++++++-------- src/encrypt.h | 16 ++++++++-------- src/ntru_string.c | 2 +- src/ntru_string.h | 2 +- src/poly.c | 32 ++++++++++++++++---------------- src/poly.h | 24 ++++++++++++------------ 10 files changed, 89 insertions(+), 87 deletions(-) diff --git a/src/ascii_poly.c b/src/ascii_poly.c index 4b245b6..1c171f0 100644 --- a/src/ascii_poly.c +++ b/src/ascii_poly.c @@ -75,7 +75,7 @@ get_int_to_bin_str(uint8_t value); * newly allocated */ static string * -get_bin_arr_to_ascii(char *binary_rep); +get_bin_arr_to_ascii(const char *binary_rep); /*------------------------------------------------------------------------*/ @@ -100,7 +100,7 @@ get_int_to_bin_str(uint8_t value) /*------------------------------------------------------------------------*/ static string * -get_bin_arr_to_ascii(char *binary_rep) +get_bin_arr_to_ascii(const char *binary_rep) { size_t int_arr_size = 0; uint8_t *int_arr = NULL; @@ -145,7 +145,7 @@ get_bin_arr_to_ascii(char *binary_rep) /*------------------------------------------------------------------------*/ fmpz_poly_t * -ascii_bin_to_bin_poly(char *to_poly, ntru_context *ctx) +ascii_bin_to_bin_poly(const char *to_poly, const ntru_context *ctx) { uint32_t i = 0; fmpz_poly_t *new_poly = ntru_malloc(sizeof(*new_poly)); @@ -165,7 +165,7 @@ ascii_bin_to_bin_poly(char *to_poly, ntru_context *ctx) /*------------------------------------------------------------------------*/ fmpz_poly_t ** -ascii_to_bin_poly_arr(string *to_poly, ntru_context *ctx) +ascii_to_bin_poly_arr(const string *to_poly, const ntru_context *ctx) { char *cur = to_poly->ptr; char *out = ntru_malloc(CHAR_SIZE * (to_poly->len * ASCII_BITS + 1)); @@ -209,8 +209,8 @@ ascii_to_bin_poly_arr(string *to_poly, ntru_context *ctx) /*------------------------------------------------------------------------*/ string * -bin_poly_to_ascii(fmpz_poly_t poly, - ntru_context *ctx) +bin_poly_to_ascii(const fmpz_poly_t poly, + const ntru_context *ctx) { string *result_string = ntru_malloc(sizeof(*result_string)); char *binary_rep = ntru_malloc(CHAR_SIZE * (ctx->N)); @@ -240,7 +240,8 @@ bin_poly_to_ascii(fmpz_poly_t poly, /*------------------------------------------------------------------------*/ string * -bin_poly_arr_to_ascii(fmpz_poly_t **bin_poly_arr, ntru_context *ctx) +bin_poly_arr_to_ascii(fmpz_poly_t **bin_poly_arr, + const ntru_context *ctx) { fmpz_poly_t *ascii_poly; char *binary_rep = NULL; @@ -253,7 +254,7 @@ bin_poly_arr_to_ascii(fmpz_poly_t **bin_poly_arr, ntru_context *ctx) * parse the polynomial coefficients into a string */ binary_rep = ntru_calloc(1, CHAR_SIZE * (ctx->N + 1)); - while ((ascii_poly = *bin_poly_arr++)) { + while ((ascii_poly = (fmpz_poly_t *)*bin_poly_arr++)) { string *single_poly_string = NULL; new_length = CHAR_SIZE * (ctx->N); @@ -287,8 +288,8 @@ bin_poly_arr_to_ascii(fmpz_poly_t **bin_poly_arr, ntru_context *ctx) /*------------------------------------------------------------------------*/ string * -poly_to_ascii(fmpz_poly_t poly, - ntru_context *ctx) +poly_to_ascii(const fmpz_poly_t poly, + const ntru_context *ctx) { string *result_string = ntru_malloc(sizeof(*result_string)); char *string_rep = ntru_malloc(CHAR_SIZE * (ctx->N)); @@ -310,7 +311,7 @@ poly_to_ascii(fmpz_poly_t poly, /*------------------------------------------------------------------------*/ fmpz_poly_t ** -base64_to_poly_arr(string *to_poly, ntru_context *ctx) +base64_to_poly_arr(const string *to_poly, const ntru_context *ctx) { uint32_t i = 0, polyc = 0; @@ -378,8 +379,8 @@ base64_to_poly_arr(string *to_poly, ntru_context *ctx) /*------------------------------------------------------------------------*/ string * -poly_to_base64(fmpz_poly_t poly, - ntru_context *ctx) +poly_to_base64(const fmpz_poly_t poly, + const ntru_context *ctx) { string *result_string = ntru_malloc(sizeof(*result_string)); string *string_rep = NULL; @@ -407,7 +408,7 @@ poly_to_base64(fmpz_poly_t poly, string * poly_arr_to_base64(fmpz_poly_t **poly_array, - ntru_context *ctx) + const ntru_context *ctx) { fmpz_poly_t *ascii_poly; char *string_rep = NULL; diff --git a/src/ascii_poly.h b/src/ascii_poly.h index 80aafd3..43d54e4 100644 --- a/src/ascii_poly.h +++ b/src/ascii_poly.h @@ -54,7 +54,7 @@ * @return newly allocated array of binary polynomials */ fmpz_poly_t * -ascii_bin_to_bin_poly(char *to_poly, ntru_context *ctx); +ascii_bin_to_bin_poly(const char *to_poly, const ntru_context *ctx); /** * Convert an ascii string to an array of binary polyomials. @@ -73,7 +73,7 @@ ascii_bin_to_bin_poly(char *to_poly, ntru_context *ctx); * @return newly allocated array of binary polynomials */ fmpz_poly_t ** -ascii_to_bin_poly_arr(string *to_poly, ntru_context *ctx); +ascii_to_bin_poly_arr(const string *to_poly, const ntru_context *ctx); /** * Convert a single binary polynomial back to a real string. @@ -95,8 +95,8 @@ ascii_to_bin_poly_arr(string *to_poly, ntru_context *ctx); * @return the real string, newly allocated */ string * -bin_poly_to_ascii(fmpz_poly_t poly, - ntru_context *ctx); +bin_poly_to_ascii(const fmpz_poly_t poly, + const ntru_context *ctx); /** * Convert an array of binary polynomials back to a real string. @@ -118,7 +118,8 @@ bin_poly_to_ascii(fmpz_poly_t poly, * @return the real string, newly allocated */ string * -bin_poly_arr_to_ascii(fmpz_poly_t **bin_poly_arr, ntru_context *ctx); +bin_poly_arr_to_ascii(fmpz_poly_t **bin_poly_arr, + const ntru_context *ctx); /** * Convert a single polynom back to a real string which is @@ -134,8 +135,8 @@ bin_poly_arr_to_ascii(fmpz_poly_t **bin_poly_arr, ntru_context *ctx); * @return the real string, newly allocated */ string * -poly_to_ascii(fmpz_poly_t poly, - ntru_context *ctx); +poly_to_ascii(const fmpz_poly_t poly, + const ntru_context *ctx); /** * Convert an base64 encoded string to an array of polyomials with @@ -153,7 +154,7 @@ poly_to_ascii(fmpz_poly_t poly, * @return newly allocated array of polynomials */ fmpz_poly_t ** -base64_to_poly_arr(string *to_poly, ntru_context *ctx); +base64_to_poly_arr(const string *to_poly, const ntru_context *ctx); /** * Convert a single polynom back to a real string which is @@ -169,8 +170,8 @@ base64_to_poly_arr(string *to_poly, ntru_context *ctx); * @return the real string, newly allocated */ string * -poly_to_base64(fmpz_poly_t poly, - ntru_context *ctx); +poly_to_base64(const fmpz_poly_t poly, + const ntru_context *ctx); /** * Convert an array of polynomials back to a real string which @@ -186,7 +187,7 @@ poly_to_base64(fmpz_poly_t poly, * @return the real string, newly allocated */ string * -poly_arr_to_base64(fmpz_poly_t **poly_arr, ntru_context *ctx); +poly_arr_to_base64(fmpz_poly_t **poly_arr, const ntru_context *ctx); #endif /* NTRU_ASCII_POLY_H_ */ diff --git a/src/decrypt.c b/src/decrypt.c index af2b39e..4d31092 100644 --- a/src/decrypt.c +++ b/src/decrypt.c @@ -41,11 +41,11 @@ bool ntru_decrypt_poly( - fmpz_poly_t encr_msg, - fmpz_poly_t priv_key, - fmpz_poly_t priv_key_inv, + const fmpz_poly_t encr_msg, + const fmpz_poly_t priv_key, + const fmpz_poly_t priv_key_inv, fmpz_poly_t out_bin, - ntru_context *ctx) + const ntru_context *ctx) { fmpz_poly_t a, priv_key_tmp, @@ -89,10 +89,10 @@ ntru_decrypt_poly( string * ntru_decrypt_string( - string *encr_msg, - fmpz_poly_t priv_key, - fmpz_poly_t priv_key_inv, - ntru_context *ctx) + const string *encr_msg, + const fmpz_poly_t priv_key, + const fmpz_poly_t priv_key_inv, + const ntru_context *ctx) { uint32_t i = 0; string *decr_msg; diff --git a/src/decrypt.h b/src/decrypt.h index beeb8ff..11a2452 100644 --- a/src/decrypt.h +++ b/src/decrypt.h @@ -53,11 +53,11 @@ */ bool ntru_decrypt_poly( - fmpz_poly_t encr_msg, - fmpz_poly_t priv_key, - fmpz_poly_t priv_key_inv, + const fmpz_poly_t encr_msg, + const fmpz_poly_t priv_key, + const fmpz_poly_t priv_key_inv, fmpz_poly_t out_tern, - ntru_context *ctx); + const ntru_context *ctx); /** * Decryption of a given encrypted string. @@ -71,10 +71,10 @@ ntru_decrypt_poly( */ string * ntru_decrypt_string( - string *encr_msg, - fmpz_poly_t priv_key, - fmpz_poly_t priv_key_inv, - ntru_context *ctx); + const string *encr_msg, + const fmpz_poly_t priv_key, + const fmpz_poly_t priv_key_inv, + const ntru_context *ctx); #endif /* NTRU_DECRYPT */ diff --git a/src/encrypt.c b/src/encrypt.c index 541ac6c..504b329 100644 --- a/src/encrypt.c +++ b/src/encrypt.c @@ -41,11 +41,11 @@ bool ntru_encrypt_poly( - fmpz_poly_t msg_bin, - fmpz_poly_t pub_key, - fmpz_poly_t rnd, + const fmpz_poly_t msg_bin, + const fmpz_poly_t pub_key, + const fmpz_poly_t rnd, fmpz_poly_t out, - ntru_context *ctx) + const ntru_context *ctx) { fmpz_poly_t tmp_poly_msg; @@ -73,10 +73,10 @@ ntru_encrypt_poly( string * ntru_encrypt_string( - string *msg, - fmpz_poly_t pub_key, - fmpz_poly_t rnd, - ntru_context *ctx) + const string *msg, + const fmpz_poly_t pub_key, + const fmpz_poly_t rnd, + const ntru_context *ctx) { uint32_t i = 0; string *enc_msg; diff --git a/src/encrypt.h b/src/encrypt.h index 700e36d..b4eeacf 100644 --- a/src/encrypt.h +++ b/src/encrypt.h @@ -64,11 +64,11 @@ */ bool ntru_encrypt_poly( - fmpz_poly_t msg_tern, - fmpz_poly_t pub_key, - fmpz_poly_t rnd, + const fmpz_poly_t msg_tern, + const fmpz_poly_t pub_key, + const fmpz_poly_t rnd, fmpz_poly_t out, - ntru_context *ctx); + const ntru_context *ctx); /** * Encrypt a message in the form of a null-terminated char array and @@ -83,10 +83,10 @@ ntru_encrypt_poly( */ string * ntru_encrypt_string( - string *msg, - fmpz_poly_t pub_key, - fmpz_poly_t rnd, - ntru_context *ctx); + const string *msg, + const fmpz_poly_t pub_key, + const fmpz_poly_t rnd, + const ntru_context *ctx); #endif /* PQC_ENCRYPT_H */ diff --git a/src/ntru_string.c b/src/ntru_string.c index b3d133a..a0b850e 100644 --- a/src/ntru_string.c +++ b/src/ntru_string.c @@ -34,7 +34,7 @@ /*------------------------------------------------------------------------*/ void -prints(string *print_string) +prints(const string *print_string) { for (size_t i = 0; i < print_string->len; i++) printf("%c", print_string->ptr[i]); diff --git a/src/ntru_string.h b/src/ntru_string.h index 69126ef..15d9be8 100644 --- a/src/ntru_string.h +++ b/src/ntru_string.h @@ -55,7 +55,7 @@ struct string { void -prints(string *print_string); +prints(const string *print_string); /** * Delete the inner structure diff --git a/src/poly.c b/src/poly.c index 481051e..7500802 100644 --- a/src/poly.c +++ b/src/poly.c @@ -52,17 +52,17 @@ * @param ctx NTRU context */ static -void poly_mod2_to_modq(fmpz_poly_t a, +void poly_mod2_to_modq(const fmpz_poly_t a, fmpz_poly_t Fq, - ntru_context *ctx); + const ntru_context *ctx); /*------------------------------------------------------------------------*/ static void -poly_mod2_to_modq(fmpz_poly_t a, +poly_mod2_to_modq(const fmpz_poly_t a, fmpz_poly_t Fq, - ntru_context *ctx) + const ntru_context *ctx) { int v = 2; fmpz_poly_t poly_tmp, two; @@ -147,7 +147,7 @@ poly_delete_all(fmpz_poly_t poly, ...) void fmpz_poly_mod_unsigned(fmpz_poly_t a, - uint32_t mod) + const uint32_t mod) { nmod_poly_t nmod_tmp; @@ -163,7 +163,7 @@ fmpz_poly_mod_unsigned(fmpz_poly_t a, void fmpz_poly_mod(fmpz_poly_t a, - uint32_t mod) + const uint32_t mod) { nmod_poly_t nmod_tmp; @@ -190,7 +190,7 @@ fmpz_poly_set_coeff_fmpz_n(fmpz_poly_t poly, slong n, /*------------------------------------------------------------------------*/ int -fmpz_invmod_ui(fmpz_t f, const fmpz_t g, uint32_t mod) +fmpz_invmod_ui(fmpz_t f, const fmpz_t g, const uint32_t mod) { fmpz_t modulus; @@ -219,10 +219,10 @@ fmpz_add_n(fmpz_t f, const fmpz_t g, const fmpz_t h) /*------------------------------------------------------------------------*/ void -poly_starmultiply(fmpz_poly_t a, - fmpz_poly_t b, +poly_starmultiply(const fmpz_poly_t a, + const fmpz_poly_t b, fmpz_poly_t c, - ntru_context *ctx, + const ntru_context *ctx, uint32_t modulus) { fmpz_poly_t a_tmp; @@ -277,9 +277,9 @@ poly_starmultiply(fmpz_poly_t a, /*------------------------------------------------------------------------*/ bool -poly_inverse_poly_q(fmpz_poly_t a, +poly_inverse_poly_q(const fmpz_poly_t a, fmpz_poly_t Fq, - ntru_context *ctx) + const ntru_context *ctx) { bool retval = false; int k = 0, @@ -389,9 +389,9 @@ _cleanup: /*------------------------------------------------------------------------*/ bool -poly_inverse_poly_p(fmpz_poly_t a, +poly_inverse_poly_p(const fmpz_poly_t a, fmpz_poly_t Fp, - ntru_context *ctx) + const ntru_context *ctx) { bool retval = false; int k = 0, @@ -554,7 +554,7 @@ cleanup: /*------------------------------------------------------------------------*/ void -poly_draw(fmpz_poly_t poly) +poly_draw(const fmpz_poly_t poly) { fmpz_poly_print(poly); flint_printf("\n"); @@ -563,7 +563,7 @@ poly_draw(fmpz_poly_t poly) /*------------------------------------------------------------------------*/ void -poly_draw_pretty(fmpz_poly_t poly) +poly_draw_pretty(const fmpz_poly_t poly) { fmpz_poly_print_pretty(poly, "x"); flint_printf("\n"); diff --git a/src/poly.h b/src/poly.h index ff2891f..964ad14 100644 --- a/src/poly.h +++ b/src/poly.h @@ -102,7 +102,7 @@ poly_delete_all(fmpz_poly_t poly, ...); */ void fmpz_poly_mod_unsigned(fmpz_poly_t a, - uint32_t mod); + const uint32_t mod); /** * Calls fmpz_poly_get_nmod_poly() and @@ -118,7 +118,7 @@ fmpz_poly_mod_unsigned(fmpz_poly_t a, */ void fmpz_poly_mod(fmpz_poly_t a, - uint32_t mod); + const uint32_t mod); /** * The same as fmpz_poly_set_coeff_fmpz() except that it @@ -145,7 +145,7 @@ fmpz_poly_set_coeff_fmpz_n(fmpz_poly_t poly, int fmpz_invmod_ui(fmpz_t f, const fmpz_t g, - uint32_t mod); + const uint32_t mod); /** * The same as fmpz_add() except that it handles NULL @@ -165,10 +165,10 @@ fmpz_add_n(fmpz_t f, const fmpz_t g, const fmpz_t h); * @param modulus whether we use p or q */ void -poly_starmultiply(fmpz_poly_t a, - fmpz_poly_t b, +poly_starmultiply(const fmpz_poly_t a, + const fmpz_poly_t b, fmpz_poly_t c, - ntru_context *ctx, + const ntru_context *ctx, uint32_t modulus); /** @@ -184,9 +184,9 @@ poly_starmultiply(fmpz_poly_t a, * @return true if invertible, false if not */ bool -poly_inverse_poly_q(fmpz_poly_t a, +poly_inverse_poly_q(const fmpz_poly_t a, fmpz_poly_t Fq, - ntru_context *ctx); + const ntru_context *ctx); /** * Compute the inverse of a polynomial in (Z/pZ)[X]/(X^N - 1). @@ -198,9 +198,9 @@ poly_inverse_poly_q(fmpz_poly_t a, * @param ctx NTRU context */ bool -poly_inverse_poly_p(fmpz_poly_t a, +poly_inverse_poly_p(const fmpz_poly_t a, fmpz_poly_t Fp, - ntru_context *ctx); + const ntru_context *ctx); /** * Draws a polynomial to stdout. @@ -208,7 +208,7 @@ poly_inverse_poly_p(fmpz_poly_t a, * @param poly draw this */ void -poly_draw(fmpz_poly_t poly); +poly_draw(const fmpz_poly_t poly); /** * Draws a polynomial to stdout, @@ -217,7 +217,7 @@ poly_draw(fmpz_poly_t poly); * @param poly draw this */ void -poly_draw_pretty(fmpz_poly_t poly); +poly_draw_pretty(const fmpz_poly_t poly); #endif /* NTRU_POLY_H */