diff --git a/include/ntru_keypair.h b/include/ntru_keypair.h index 7a375d0..1dd9c96 100644 --- a/include/ntru_keypair.h +++ b/include/ntru_keypair.h @@ -70,16 +70,18 @@ struct keypair { * consisting of public and private * components. * + * @param pair store private and public components here [out] * @param f a random polynomial * @param g a random polynomial - * @param pair store private and public components here [out] * @param params the NTRU context + * @return true for success, false if f or g are not invertible + * (then the caller hast to try different ones) */ bool ntru_create_keypair( + keypair *pair, const fmpz_poly_t f, const fmpz_poly_t g, - keypair *pair, const ntru_params *params); /** @@ -108,28 +110,29 @@ export_priv_key(char const * const filename, /** * Import the public key from a file. - * @param filename the file to get the public key from + * * @param pub where to save the public key [out] + * @param filename the file to get the public key from * @param params the NTRU context */ void -import_public_key(char const * const filename, - fmpz_poly_t pub, +import_public_key(fmpz_poly_t pub, + char const * const filename, const ntru_params *params); /** * Import the private key from a file and store him * along with his inverse. * - * @param filename the file to get the private key from * @param priv where to save the private key [out] * @param priv_inv where to save the inverse of the private key [out] + * @param filename the file to get the private key from * @param params the NTRU context */ void -import_priv_key(char const * const filename, - fmpz_poly_t priv, +import_priv_key(fmpz_poly_t priv, fmpz_poly_t priv_inv, + char const * const filename, const ntru_params *params); /** diff --git a/src/ntru_decrypt.c b/src/ntru_decrypt.c index 34c0731..aedd3c5 100644 --- a/src/ntru_decrypt.c +++ b/src/ntru_decrypt.c @@ -44,10 +44,10 @@ void ntru_decrypt_poly( + fmpz_poly_t out_bin, const fmpz_poly_t encr_msg, const fmpz_poly_t priv_key, const fmpz_poly_t priv_key_inv, - fmpz_poly_t out_bin, const ntru_params *params) { fmpz_poly_t a, @@ -75,9 +75,9 @@ ntru_decrypt_poly( fmpz_poly_mod(priv_key_inv_tmp, params->q); fmpz_poly_mod(encr_msg_tmp, params->q); - poly_starmultiply(priv_key_tmp, encr_msg_tmp, a, params, params->q); + poly_starmultiply(a, priv_key_tmp, encr_msg_tmp, params, params->q); fmpz_poly_mod(a, params->q); - poly_starmultiply(a, priv_key_inv_tmp, out_bin, params, params->p); + poly_starmultiply(out_bin, a, priv_key_inv_tmp, params, params->p); fmpz_poly_mod(out_bin, params->p); fmpz_poly_clear(a); @@ -106,9 +106,9 @@ ntru_decrypt_string( while (*poly_array[i]) { ntru_decrypt_poly(*poly_array[i], + *poly_array[i], priv_key, priv_key_inv, - *poly_array[i], params); i++; } diff --git a/src/ntru_decrypt.h b/src/ntru_decrypt.h index bc224b4..4e7ade4 100644 --- a/src/ntru_decrypt.h +++ b/src/ntru_decrypt.h @@ -40,20 +40,20 @@ * Decryption of the given Polynom with the private key, its inverse * and the fitting ntru_params * + * @param out_tern the resulting ternary polynom [out] * @param encr_msg encrypted polynomial with maximum length of N from * the given context * @param priv_key the polynomial containing the private key to decrypt * the message * @param priv_key_inv the inverse polynome to the private key - * @param out_tern the resulting ternary polynom [out] * @param params the ntru_params */ void ntru_decrypt_poly( + fmpz_poly_t out_tern, const fmpz_poly_t encr_msg, const fmpz_poly_t priv_key, const fmpz_poly_t priv_key_inv, - fmpz_poly_t out_tern, const ntru_params *params); /** diff --git a/src/ntru_encrypt.c b/src/ntru_encrypt.c index a1fb81f..ca7f398 100644 --- a/src/ntru_encrypt.c +++ b/src/ntru_encrypt.c @@ -44,10 +44,10 @@ void ntru_encrypt_poly( + fmpz_poly_t out, const fmpz_poly_t msg_bin, const fmpz_poly_t pub_key, const fmpz_poly_t rnd, - fmpz_poly_t out, const ntru_params *params) { fmpz_poly_t tmp_poly_msg; @@ -60,7 +60,7 @@ ntru_encrypt_poly( fmpz_poly_set(tmp_poly_msg, msg_bin); fmpz_poly_zero(out); - poly_starmultiply(pub_key, rnd, out, params, params->q); + poly_starmultiply(out, pub_key, rnd, params, params->q); fmpz_poly_add(out, out, tmp_poly_msg); fmpz_poly_mod_unsigned(out, params->q); @@ -88,9 +88,9 @@ ntru_encrypt_string( while (*poly_array[i]) { ntru_encrypt_poly(*poly_array[i], + *poly_array[i], pub_key, rnd, - *poly_array[i], params); i++; } diff --git a/src/ntru_encrypt.h b/src/ntru_encrypt.h index 4893f6f..a02c192 100644 --- a/src/ntru_encrypt.h +++ b/src/ntru_encrypt.h @@ -51,20 +51,20 @@ * * q = large mod * + * @param out the output poly which is in the range {0, q-1} + * (not ternary!) [out] * @param msg_tern the message to encrypt, in ternary format * @param pub_key the public key * @param rnd the random poly (should have relatively small * coefficients, but not restricted to {-1, 0, 1}) - * @param out the output poly which is in the range {0, q-1} - * (not ternary!) [out] * @param params ntru_params the ntru context */ void ntru_encrypt_poly( + fmpz_poly_t out, const fmpz_poly_t msg_tern, const fmpz_poly_t pub_key, const fmpz_poly_t rnd, - fmpz_poly_t out, const ntru_params *params); /** diff --git a/src/ntru_keypair.c b/src/ntru_keypair.c index bfa1778..2fd22a9 100644 --- a/src/ntru_keypair.c +++ b/src/ntru_keypair.c @@ -45,9 +45,9 @@ bool ntru_create_keypair( + keypair *pair, const fmpz_poly_t f, const fmpz_poly_t g, - keypair *pair, const ntru_params *params) { bool retval = false; @@ -62,13 +62,13 @@ ntru_create_keypair( fmpz_poly_init(Fp); fmpz_poly_init(pub); - if (!poly_inverse_poly_q(f, Fq, params)) + if (!poly_inverse_poly_q(Fq, f, params)) goto _cleanup; - if (!poly_inverse_poly_p(f, Fp, params)) + if (!poly_inverse_poly_p(Fp, f, params)) goto _cleanup; - poly_starmultiply(Fq, g, pub, params, params->q); + poly_starmultiply(pub, Fq, g, params, params->q); fmpz_poly_scalar_mul_ui(pub, pub, params->p); fmpz_poly_mod_unsigned(pub, params->q); @@ -129,8 +129,8 @@ export_priv_key(char const * const filename, /*------------------------------------------------------------------------*/ void -import_public_key(char const * const filename, - fmpz_poly_t pub, +import_public_key(fmpz_poly_t pub, + char const * const filename, const ntru_params *params) { string *pub_string; @@ -154,9 +154,9 @@ import_public_key(char const * const filename, /*------------------------------------------------------------------------*/ void -import_priv_key(char const * const filename, - fmpz_poly_t priv, +import_priv_key(fmpz_poly_t priv, fmpz_poly_t priv_inv, + char const * const filename, const ntru_params *params) { string *pub_string; @@ -177,7 +177,7 @@ import_priv_key(char const * const filename, fmpz_poly_set(priv, **imported); - if (!poly_inverse_poly_p(priv, Fp, params)) + if (!poly_inverse_poly_p(Fp, priv, params)) goto cleanup; fmpz_poly_mod(Fp, params->p); diff --git a/src/ntru_keypair.h b/src/ntru_keypair.h index 9384c99..8bcbe32 100644 --- a/src/ntru_keypair.h +++ b/src/ntru_keypair.h @@ -67,18 +67,18 @@ struct keypair { * consisting of public and private * components. * + * @param pair store private and public components here [out] * @param f a random polynomial * @param g a random polynomial - * @param pair store private and public components here [out] * @param params the NTRU context * @return true for success, false if f or g are not invertible * (then the caller hast to try different ones) */ bool ntru_create_keypair( + keypair *pair, const fmpz_poly_t f, const fmpz_poly_t g, - keypair *pair, const ntru_params *params); /** @@ -107,28 +107,29 @@ export_priv_key(char const * const filename, /** * Import the public key from a file. - * @param filename the file to get the public key from + * * @param pub where to save the public key [out] + * @param filename the file to get the public key from * @param params the NTRU context */ void -import_public_key(char const * const filename, - fmpz_poly_t pub, +import_public_key(fmpz_poly_t pub, + char const * const filename, const ntru_params *params); /** * Import the private key from a file and store him * along with his inverse. * - * @param filename the file to get the private key from * @param priv where to save the private key [out] * @param priv_inv where to save the inverse of the private key [out] + * @param filename the file to get the private key from * @param params the NTRU context */ void -import_priv_key(char const * const filename, - fmpz_poly_t priv, +import_priv_key(fmpz_poly_t priv, fmpz_poly_t priv_inv, + char const * const filename, const ntru_params *params); /** diff --git a/src/ntru_poly.c b/src/ntru_poly.c index 367af43..ac3b60d 100644 --- a/src/ntru_poly.c +++ b/src/ntru_poly.c @@ -47,21 +47,21 @@ * Find the inverse polynomial modulo a power of 2, * which is q. * - * @param a polynomial to invert * @param Fq polynomial [out] + * @param a polynomial to invert * @param params NTRU parameters */ static -void poly_mod2_to_modq(const fmpz_poly_t a, - fmpz_poly_t Fq, +void poly_mod2_to_modq(fmpz_poly_t Fq, + const fmpz_poly_t a, const ntru_params *params); /*------------------------------------------------------------------------*/ static void -poly_mod2_to_modq(const fmpz_poly_t a, - fmpz_poly_t Fq, +poly_mod2_to_modq(fmpz_poly_t Fq, + const fmpz_poly_t a, const ntru_params *params) { int v = 2; @@ -75,10 +75,10 @@ poly_mod2_to_modq(const fmpz_poly_t a, while (v < (int)(params->q)) { v = v * 2; - poly_starmultiply(a, Fq, poly_tmp, params, v); + poly_starmultiply(poly_tmp, a, Fq, params, v); fmpz_poly_sub(poly_tmp, two, poly_tmp); fmpz_poly_mod_unsigned(poly_tmp, v); - poly_starmultiply(Fq, poly_tmp, Fq, params, v); + poly_starmultiply(Fq, Fq, poly_tmp, params, v); } @@ -236,9 +236,9 @@ fmpz_add_n(fmpz_t f, const fmpz_t g, const fmpz_t h) /*------------------------------------------------------------------------*/ void -poly_starmultiply(const fmpz_poly_t a, +poly_starmultiply(fmpz_poly_t c, + const fmpz_poly_t a, const fmpz_poly_t b, - fmpz_poly_t c, const ntru_params *params, uint32_t modulus) { @@ -294,8 +294,8 @@ poly_starmultiply(const fmpz_poly_t a, /*------------------------------------------------------------------------*/ bool -poly_inverse_poly_q(const fmpz_poly_t a, - fmpz_poly_t Fq, +poly_inverse_poly_q(fmpz_poly_t Fq, + const fmpz_poly_t a, const ntru_params *params) { bool retval = false; @@ -383,11 +383,11 @@ poly_inverse_poly_q(const fmpz_poly_t a, fmpz_poly_set_coeff_fmpz_n(Fq, j, b_i); } - poly_mod2_to_modq(a_tmp, Fq, params); + poly_mod2_to_modq(Fq, a_tmp, params); /* check if the f * Fq = 1 (mod p) condition holds true */ fmpz_poly_set(a_tmp, a); - poly_starmultiply(a_tmp, Fq, a_tmp, params, params->q); + poly_starmultiply(a_tmp, a_tmp, Fq, params, params->q); if (fmpz_poly_is_one(a_tmp)) retval = true; else @@ -406,8 +406,8 @@ _cleanup: /*------------------------------------------------------------------------*/ bool -poly_inverse_poly_p(const fmpz_poly_t a, - fmpz_poly_t Fp, +poly_inverse_poly_p(fmpz_poly_t Fp, + const fmpz_poly_t a, const ntru_params *params) { bool retval = false; @@ -552,7 +552,7 @@ poly_inverse_poly_p(const fmpz_poly_t a, /* check if the f * Fp = 1 (mod p) condition holds true */ fmpz_poly_set(a_tmp, a); - poly_starmultiply(a_tmp, Fp, a_tmp, params, params->p); + poly_starmultiply(a_tmp, a_tmp, Fp, params, params->p); if (fmpz_poly_is_one(a_tmp)) retval = true; else diff --git a/src/ntru_poly.h b/src/ntru_poly.h index f7995e0..c324952 100644 --- a/src/ntru_poly.h +++ b/src/ntru_poly.h @@ -169,16 +169,16 @@ 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 c polynom [out] * @param a polynom to multiply (can be the same as c) * @param b polynom to multiply - * @param c polynom [out] * @param params NTRU parameters * @param modulus whether we use p or q */ void -poly_starmultiply(const fmpz_poly_t a, +poly_starmultiply(fmpz_poly_t c, + const fmpz_poly_t a, const fmpz_poly_t b, - fmpz_poly_t c, const ntru_params *params, uint32_t modulus); @@ -189,14 +189,14 @@ poly_starmultiply(const fmpz_poly_t a, * 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 a polynomial to invert (is allowed to be the same as param Fq) * @param params NTRU parameters * @return true if invertible, false if not */ bool -poly_inverse_poly_q(const fmpz_poly_t a, - fmpz_poly_t Fq, +poly_inverse_poly_q(fmpz_poly_t Fq, + const fmpz_poly_t a, const ntru_params *params); /** @@ -204,14 +204,14 @@ poly_inverse_poly_q(const fmpz_poly_t a, * See NTRU Cryptosystems Tech Report #014 "Almost Inverses * and Fast NTRU Key Creation." * - * @param a polynomial to invert * @param Fp polynomial [out] + * @param a polynomial to invert * @param params NTRU parameters * @return true if invertible, false if not */ bool -poly_inverse_poly_p(const fmpz_poly_t a, - fmpz_poly_t Fp, +poly_inverse_poly_p(fmpz_poly_t Fp, + const fmpz_poly_t a, const ntru_params *params); /**