ALL: rename context to params

This commit is contained in:
hasufell 2014-06-05 15:33:57 +02:00
parent 516b14414a
commit 6dccef4dc9
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020
16 changed files with 233 additions and 175 deletions

View File

@ -17,12 +17,12 @@ PQC_OBJS = $(patsubst %.c, %.o, $(PQC_SOURCES))
PQC_HEADERS = err.h \
poly.h \
context.h \
params.h \
encrypt.h \
decrypt.h \
keypair.h \
ascii_poly.h \
common.h \
params.h \
file.h \
ntru_string.h \
poly_ascii.h \

View File

@ -28,9 +28,9 @@
#include "ascii_poly.h"
#include "common.h"
#include "context.h"
#include "mem.h"
#include "ntru_string.h"
#include "params.h"
#include "poly.h"
#include <glib.h>
@ -78,14 +78,14 @@ get_int_to_bin_str(uint8_t value)
/*------------------------------------------------------------------------*/
fmpz_poly_t *
ascii_bin_to_bin_poly(const char *to_poly, const ntru_context *ctx)
ascii_bin_to_bin_poly(const char *to_poly, const ntru_params *params)
{
uint32_t i = 0;
fmpz_poly_t *new_poly = ntru_malloc(sizeof(*new_poly));
fmpz_poly_init(*new_poly);
while (to_poly[i] && i < ctx->N) {
while (to_poly[i] && i < params->N) {
fmpz_poly_set_coeff_si(*new_poly,
i,
(to_poly[i] == '0') ? -1 : 1);
@ -98,7 +98,7 @@ ascii_bin_to_bin_poly(const char *to_poly, const ntru_context *ctx)
/*------------------------------------------------------------------------*/
fmpz_poly_t **
ascii_to_bin_poly_arr(const string *to_poly, const ntru_context *ctx)
ascii_to_bin_poly_arr(const string *to_poly, const ntru_params *params)
{
char *cur = to_poly->ptr;
char *out = ntru_malloc(CHAR_SIZE * (to_poly->len * ASCII_BITS + 1));
@ -115,19 +115,19 @@ ascii_to_bin_poly_arr(const string *to_poly, const ntru_context *ctx)
}
poly_array = ntru_malloc(sizeof(**poly_array) *
(strlen(out) / ctx->N + 1));
(strlen(out) / params->N + 1));
for (uint32_t i = 0; i < strlen(out); i += ctx->N) {
char chunk[ctx->N + 1];
for (uint32_t i = 0; i < strlen(out); i += params->N) {
char chunk[params->N + 1];
size_t real_chunk_size;
real_chunk_size =
(strlen(out + i) > ctx->N) ? ctx->N : strlen(out + i);
(strlen(out + i) > params->N) ? params->N : strlen(out + i);
memcpy(chunk, out + i, real_chunk_size);
chunk[real_chunk_size] = '\0';
poly_array[polyc] = ascii_bin_to_bin_poly(chunk, ctx);
poly_array[polyc] = ascii_bin_to_bin_poly(chunk, params);
polyc++;
}
@ -142,7 +142,7 @@ ascii_to_bin_poly_arr(const string *to_poly, const ntru_context *ctx)
/*------------------------------------------------------------------------*/
fmpz_poly_t **
base64_to_poly_arr(const string *to_poly, const ntru_context *ctx)
base64_to_poly_arr(const string *to_poly, const ntru_params *params)
{
uint32_t i = 0,
polyc = 0;
@ -170,7 +170,7 @@ base64_to_poly_arr(const string *to_poly, const ntru_context *ctx)
new_string->len = (unsigned long)(out_len);
poly_array = ntru_malloc(sizeof(**poly_array) *
(new_string->len / ctx->N));
(new_string->len / params->N));
while (i < new_string->len) {
uint32_t j = 0;
@ -178,7 +178,7 @@ base64_to_poly_arr(const string *to_poly, const ntru_context *ctx)
fmpz_poly_init(*new_poly);
while (j < ctx->N) {
while (j < params->N) {
fmpz_poly_set_coeff_si(*new_poly,
j,
(uint8_t)(base64_decoded[i]));
@ -188,10 +188,10 @@ base64_to_poly_arr(const string *to_poly, const ntru_context *ctx)
/* fill the last poly with q (which is a non-standard
* coefficient) */
for (uint32_t k = j; k < ctx->N; k++) {
for (uint32_t k = j; k < params->N; k++) {
fmpz_poly_set_coeff_si(*new_poly,
k,
ctx->q);
params->q);
}
poly_array[polyc] = new_poly;

View File

@ -30,8 +30,8 @@
#include "common.h"
#include "context.h"
#include "ntru_string.h"
#include "params.h"
#include <fmpz_poly.h>
#include <fmpz.h>
@ -50,11 +50,11 @@
* be filled with trailing 2's for later use in bin_poly_to_ascii().
*
* @param to_poly the string to get into binary polynomial format
* @param ctx the NTRUEncrypt context
* @param params the NTRUEncrypt context
* @return newly allocated array of binary polynomials
*/
fmpz_poly_t *
ascii_bin_to_bin_poly(const char *to_poly, const ntru_context *ctx);
ascii_bin_to_bin_poly(const char *to_poly, const ntru_params *params);
/**
* Convert an ascii string to an array of binary polyomials.
@ -69,11 +69,11 @@ ascii_bin_to_bin_poly(const char *to_poly, const ntru_context *ctx);
* be filled with trailing 2's for later use in bin_poly_arr_to_ascii().
*
* @param to_poly the string to get into binary polynomial format
* @param ctx the NTRUEncrypt context
* @param params the NTRUEncrypt context
* @return newly allocated array of binary polynomials
*/
fmpz_poly_t **
ascii_to_bin_poly_arr(const string *to_poly, const ntru_context *ctx);
ascii_to_bin_poly_arr(const string *to_poly, const ntru_params *params);
/**
* Convert an base64 encoded string to an array of polyomials with
@ -87,11 +87,11 @@ ascii_to_bin_poly_arr(const string *to_poly, const ntru_context *ctx);
* @param to_poly the string to get into polynomial format,
* which is of type string, so we can iterate safely over it
* (the string might have null-bytes in the middle of it)
* @param ctx the NTRUEncrypt context
* @param params the NTRUEncrypt context
* @return newly allocated array of polynomials
*/
fmpz_poly_t **
base64_to_poly_arr(const string *to_poly, const ntru_context *ctx);
base64_to_poly_arr(const string *to_poly, const ntru_params *params);
#endif /* NTRU_ASCII_POLY_H_ */

View File

@ -29,6 +29,7 @@
#include "ascii_poly.h"
#include "decrypt.h"
#include "ntru_string.h"
#include "params.h"
#include "poly_ascii.h"
#include <stdbool.h>
@ -46,14 +47,14 @@ ntru_decrypt_poly(
const fmpz_poly_t priv_key,
const fmpz_poly_t priv_key_inv,
fmpz_poly_t out_bin,
const ntru_context *ctx)
const ntru_params *params)
{
fmpz_poly_t a,
priv_key_tmp,
priv_key_inv_tmp,
encr_msg_tmp;
if (!encr_msg || !priv_key || !priv_key_inv || !out_bin || !ctx)
if (!encr_msg || !priv_key || !priv_key_inv || !out_bin || !params)
return false;
fmpz_poly_init(a);
@ -69,14 +70,14 @@ ntru_decrypt_poly(
fmpz_poly_set(priv_key_tmp, priv_key);
fmpz_poly_set(priv_key_inv_tmp, priv_key_inv);
fmpz_poly_set(encr_msg_tmp, encr_msg);
fmpz_poly_mod(priv_key_tmp, ctx->q);
fmpz_poly_mod(priv_key_inv_tmp, ctx->q);
fmpz_poly_mod(encr_msg_tmp, ctx->q);
fmpz_poly_mod(priv_key_tmp, params->q);
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, ctx, ctx->q);
fmpz_poly_mod(a, ctx->q);
poly_starmultiply(a, priv_key_inv_tmp, out_bin, ctx, ctx->p);
fmpz_poly_mod(out_bin, ctx->p);
poly_starmultiply(priv_key_tmp, encr_msg_tmp, a, params, params->q);
fmpz_poly_mod(a, params->q);
poly_starmultiply(a, priv_key_inv_tmp, out_bin, params, params->p);
fmpz_poly_mod(out_bin, params->p);
fmpz_poly_clear(a);
fmpz_poly_clear(priv_key_tmp);
@ -93,7 +94,7 @@ ntru_decrypt_string(
const string *encr_msg,
const fmpz_poly_t priv_key,
const fmpz_poly_t priv_key_inv,
const ntru_context *ctx)
const ntru_params *params)
{
uint32_t i = 0;
string *decr_msg;
@ -102,19 +103,19 @@ ntru_decrypt_string(
if (!encr_msg || !encr_msg->len)
return NULL;
poly_array = base64_to_poly_arr(encr_msg, ctx);
poly_array = base64_to_poly_arr(encr_msg, params);
while (*poly_array[i]) {
if (!ntru_decrypt_poly(*poly_array[i],
priv_key,
priv_key_inv,
*poly_array[i],
ctx))
params))
NTRU_ABORT("failed encrypting string!\n");
i++;
}
decr_msg = bin_poly_arr_to_ascii(poly_array, ctx);
decr_msg = bin_poly_arr_to_ascii(poly_array, params);
poly_delete_array(poly_array);

View File

@ -28,8 +28,8 @@
#ifndef NTRU_DECRYPT_H
#define NTRU_DECRYPT_H
#include "context.h"
#include "ntru_string.h"
#include "params.h"
#include "poly.h"
#include <stdbool.h>
@ -40,7 +40,7 @@
/**
* Decryption of the given Polynom with the private key, its inverse
* and the fitting ntru_context
* and the fitting ntru_params
*
* @param encr_msg encrypted polynom with maximum length of N from
* the given context
@ -48,7 +48,7 @@
* the message
* @param priv_key_inv the inverse polynome to the private key
* @param out_tern the resulting ternary polynom [out]
* @param ctx the ntru_context
* @param params the ntru_params
* @return true/false for success/failure
*/
bool
@ -57,7 +57,7 @@ ntru_decrypt_poly(
const fmpz_poly_t priv_key,
const fmpz_poly_t priv_key_inv,
fmpz_poly_t out_tern,
const ntru_context *ctx);
const ntru_params *params);
/**
* Decryption of a given encrypted string.
@ -66,7 +66,7 @@ ntru_decrypt_poly(
* @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 ctx the ntru_context
* @param params the ntru_params
* @return the decrypted string or NULL on failure
*/
string *
@ -74,7 +74,7 @@ ntru_decrypt_string(
const string *encr_msg,
const fmpz_poly_t priv_key,
const fmpz_poly_t priv_key_inv,
const ntru_context *ctx);
const ntru_params *params);
#endif /* NTRU_DECRYPT */

View File

@ -30,6 +30,7 @@
#include "encrypt.h"
#include "mem.h"
#include "ntru_string.h"
#include "params.h"
#include "poly_ascii.h"
#include <string.h>
@ -46,11 +47,11 @@ ntru_encrypt_poly(
const fmpz_poly_t pub_key,
const fmpz_poly_t rnd,
fmpz_poly_t out,
const ntru_context *ctx)
const ntru_params *params)
{
fmpz_poly_t tmp_poly_msg;
if (!msg_bin || !pub_key || !rnd || !out || !ctx)
if (!msg_bin || !pub_key || !rnd || !out || !params)
return false;
/* allow aliasing */
@ -58,10 +59,10 @@ ntru_encrypt_poly(
fmpz_poly_set(tmp_poly_msg, msg_bin);
fmpz_poly_zero(out);
poly_starmultiply(pub_key, rnd, out, ctx, ctx->q);
poly_starmultiply(pub_key, rnd, out, params, params->q);
fmpz_poly_add(out, out, tmp_poly_msg);
fmpz_poly_mod_unsigned(out, ctx->q);
fmpz_poly_mod_unsigned(out, params->q);
fmpz_poly_clear(tmp_poly_msg);
@ -75,7 +76,7 @@ ntru_encrypt_string(
const string *msg,
const fmpz_poly_t pub_key,
const fmpz_poly_t rnd,
const ntru_context *ctx)
const ntru_params *params)
{
uint32_t i = 0;
string *enc_msg;
@ -84,19 +85,19 @@ ntru_encrypt_string(
if (!msg || !msg->len)
return NULL;
poly_array = ascii_to_bin_poly_arr(msg, ctx);
poly_array = ascii_to_bin_poly_arr(msg, params);
while (*poly_array[i]) {
if (!ntru_encrypt_poly(*poly_array[i],
pub_key,
rnd,
*poly_array[i],
ctx))
params))
NTRU_ABORT("failed encrypting string!\n");
i++;
}
enc_msg = poly_arr_to_base64(poly_array, ctx);
enc_msg = poly_arr_to_base64(poly_array, params);
poly_delete_array(poly_array);

View File

@ -29,8 +29,8 @@
#define PQC_ENCRYPT_H
#include "context.h"
#include "ntru_string.h"
#include "params.h"
#include "poly.h"
#include <stdbool.h>
@ -59,7 +59,7 @@
* 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 ctx ntru_context the ntru context
* @param params ntru_params the ntru context
* @return true/false for success/failure
*/
bool
@ -68,7 +68,7 @@ ntru_encrypt_poly(
const fmpz_poly_t pub_key,
const fmpz_poly_t rnd,
fmpz_poly_t out,
const ntru_context *ctx);
const ntru_params *params);
/**
* Encrypt a message in the form of a null-terminated char array and
@ -78,7 +78,7 @@ ntru_encrypt_poly(
* @param pub_key the public key
* @param rnd the random poly (should have relatively small
* coefficients, but not restricted to {-1, 0, 1})
* @param ctx ntru_context the ntru context
* @param params ntru_params the ntru context
* @return the newly allocated encrypted string, NULL on failure
*/
string *
@ -86,7 +86,7 @@ ntru_encrypt_string(
const string *msg,
const fmpz_poly_t pub_key,
const fmpz_poly_t rnd,
const ntru_context *ctx);
const ntru_params *params);
#endif /* PQC_ENCRYPT_H */

View File

@ -27,10 +27,10 @@
*/
#include "ascii_poly.h"
#include "context.h"
#include "file.h"
#include "keypair.h"
#include "ntru_string.h"
#include "params.h"
#include "poly.h"
#include "poly_ascii.h"
@ -48,29 +48,29 @@ ntru_create_keypair(
fmpz_poly_t f,
fmpz_poly_t g,
keypair *pair,
ntru_context *ctx)
ntru_params *params)
{
bool retval = false;
fmpz_poly_t Fq,
Fp,
pub;
if (!f || !g || !ctx)
if (!f || !g || !params)
goto _return;
fmpz_poly_init(Fq);
fmpz_poly_init(Fp);
fmpz_poly_init(pub);
if (!poly_inverse_poly_q(f, Fq, ctx))
if (!poly_inverse_poly_q(f, Fq, params))
goto _cleanup;
if (!poly_inverse_poly_p(f, Fp, ctx))
if (!poly_inverse_poly_p(f, Fp, params))
goto _cleanup;
poly_starmultiply(Fq, g, pub, ctx, ctx->q);
fmpz_poly_scalar_mul_ui(pub, pub, ctx->p);
fmpz_poly_mod_unsigned(pub, ctx->q);
poly_starmultiply(Fq, g, pub, params, params->q);
fmpz_poly_scalar_mul_ui(pub, pub, params->p);
fmpz_poly_mod_unsigned(pub, params->q);
fmpz_poly_init(pair->priv);
fmpz_poly_init(pair->priv_inv);
@ -95,11 +95,11 @@ _return:
void
export_public_key(char const * const filename,
fmpz_poly_t pub,
ntru_context *ctx)
ntru_params *params)
{
string *pub_string;
pub_string = poly_to_base64(pub, ctx);
pub_string = poly_to_base64(pub, params);
write_file(pub_string, filename);
string_delete(pub_string);
@ -110,16 +110,16 @@ export_public_key(char const * const filename,
void
export_priv_key(char const * const filename,
fmpz_poly_t priv,
ntru_context *ctx)
ntru_params *params)
{
string *priv_string;
fmpz_poly_t priv_u;
fmpz_poly_init(priv_u);
fmpz_poly_set(priv_u, priv);
fmpz_poly_mod_unsigned(priv_u, ctx->p);
fmpz_poly_mod_unsigned(priv_u, params->p);
priv_string = poly_to_base64(priv_u, ctx);
priv_string = poly_to_base64(priv_u, params);
write_file(priv_string, filename);
fmpz_poly_clear(priv_u);
@ -131,13 +131,13 @@ export_priv_key(char const * const filename,
void
import_public_key(char const * const filename,
fmpz_poly_t pub,
ntru_context *ctx)
ntru_params *params)
{
string *pub_string;
fmpz_poly_t **imported;
pub_string = read_file(filename);
imported = base64_to_poly_arr(pub_string, ctx);
imported = base64_to_poly_arr(pub_string, params);
/* if the array exceeds one element, then something
* went horribly wrong */
@ -157,7 +157,7 @@ void
import_priv_key(char const * const filename,
fmpz_poly_t priv,
fmpz_poly_t priv_inv,
ntru_context *ctx)
ntru_params *params)
{
string *pub_string;
fmpz_poly_t **imported,
@ -167,8 +167,8 @@ import_priv_key(char const * const filename,
pub_string = read_file(filename);
imported = base64_to_poly_arr(pub_string, ctx);
fmpz_poly_mod(**imported, ctx->p);
imported = base64_to_poly_arr(pub_string, params);
fmpz_poly_mod(**imported, params->p);
/* if the array exceeds one element, then something
* went horribly wrong */
@ -177,10 +177,10 @@ import_priv_key(char const * const filename,
fmpz_poly_set(priv, **imported);
if (!poly_inverse_poly_p(priv, Fp, ctx))
if (!poly_inverse_poly_p(priv, Fp, params))
goto cleanup;
fmpz_poly_mod(Fp, ctx->p);
fmpz_poly_mod(Fp, params->p);
fmpz_poly_set(priv_inv, Fp);
fmpz_poly_clear(Fp);

View File

@ -29,7 +29,7 @@
#define NTRU_KEYPAIR_H
#include "context.h"
#include "params.h"
#include <fmpz_poly.h>
#include <fmpz.h>
@ -70,49 +70,49 @@ struct keypair {
* @param f a random polynomial
* @param g a random polynomial
* @param pair store private and public components here [out]
* @param ctx the NTRU context
* @param params the NTRU context
*/
bool
ntru_create_keypair(
fmpz_poly_t f,
fmpz_poly_t g,
keypair *pair,
ntru_context *ctx);
ntru_params *params);
/**
* Export the public key to a file.
*
* @param filename the file to save the public key into
* @param pub the public key
* @param ctx the NTRU context
* @param params the NTRU context
*/
void
export_public_key(char const * const filename,
fmpz_poly_t pub,
ntru_context *ctx);
ntru_params *params);
/**
* Export the private key to a file.
*
* @param filename the file to save the private key into
* @param priv the private key
* @param ctx the NTRU context
* @param params the NTRU context
*/
void
export_priv_key(char const * const filename,
fmpz_poly_t priv,
ntru_context *ctx);
ntru_params *params);
/**
* 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 ctx the NTRU context
* @param params the NTRU context
*/
void
import_public_key(char const * const filename,
fmpz_poly_t pub,
ntru_context *ctx);
ntru_params *params);
/**
* Import the private key from a file and store him
@ -121,13 +121,13 @@ import_public_key(char const * const filename,
* @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 ctx the NTRU context
* @param params the NTRU context
*/
void
import_priv_key(char const * const filename,
fmpz_poly_t priv,
fmpz_poly_t priv_inv,
ntru_context *ctx);
ntru_params *params);
/**
* Used to free the inner structure

56
src/params.h Normal file
View File

@ -0,0 +1,56 @@
/*
* Copyright (C) 2014 FH Bielefeld
*
* This file is part of a FH Bielefeld project.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
/**
* @file params.h
* This file defines the ntru_params
* and related data types.
* @brief NTRU parameters
*/
#ifndef NTRU_PARAMS_H
#define NTRU_PARAMS_H
#include <stdint.h>
/**
* NTRU cryptosystem is specified by
* the following triple.
*/
typedef struct {
/**
* maximal degree N - 1 for
* all polynomials
*/
uint32_t N;
/**
* large modulus
*/
uint32_t q;
/**
* small modulus
*/
uint32_t p;
} ntru_params;
#endif /* NTRU_PARAMS_H */

View File

@ -27,9 +27,9 @@
* @brief operations on polynomials
*/
#include "context.h"
#include "err.h"
#include "mem.h"
#include "params.h"
#include "poly.h"
#include <stdarg.h>
@ -49,12 +49,12 @@
*
* @param a polynomial to invert
* @param Fq polynomial [out]
* @param ctx NTRU context
* @param params NTRU context
*/
static
void poly_mod2_to_modq(const fmpz_poly_t a,
fmpz_poly_t Fq,
const ntru_context *ctx);
const ntru_params *params);
/*------------------------------------------------------------------------*/
@ -62,7 +62,7 @@ void poly_mod2_to_modq(const fmpz_poly_t a,
static void
poly_mod2_to_modq(const fmpz_poly_t a,
fmpz_poly_t Fq,
const ntru_context *ctx)
const ntru_params *params)
{
int v = 2;
fmpz_poly_t poly_tmp, two;
@ -72,13 +72,13 @@ poly_mod2_to_modq(const fmpz_poly_t a,
fmpz_poly_init(two);
fmpz_poly_set_coeff_ui(two, 0, 2);
while (v < (int)(ctx->q)) {
while (v < (int)(params->q)) {
v = v * 2;
poly_starmultiply(a, Fq, poly_tmp, ctx, v);
poly_starmultiply(a, Fq, poly_tmp, params, v);
fmpz_poly_sub(poly_tmp, two, poly_tmp);
fmpz_poly_mod_unsigned(poly_tmp, v);
poly_starmultiply(Fq, poly_tmp, Fq, ctx, v);
poly_starmultiply(Fq, poly_tmp, Fq, params, v);
}
@ -239,7 +239,7 @@ void
poly_starmultiply(const fmpz_poly_t a,
const fmpz_poly_t b,
fmpz_poly_t c,
const ntru_context *ctx,
const ntru_params *params,
uint32_t modulus)
{
fmpz_poly_t a_tmp;
@ -252,18 +252,18 @@ poly_starmultiply(const fmpz_poly_t a,
fmpz_poly_set(a_tmp, a);
fmpz_poly_zero(c);
for (int k = ctx->N - 1; k >= 0; k--) {
for (int k = params->N - 1; k >= 0; k--) {
int j;
j = k + 1;
fmpz_set_si(c_coeff_k, 0);
for (int i = ctx->N - 1; i >= 0; i--) {
for (int i = params->N - 1; i >= 0; i--) {
fmpz *a_tmp_coeff_i,
*b_coeff_j;
if (j == (int)(ctx->N))
if (j == (int)(params->N))
j = 0;
a_tmp_coeff_i = fmpz_poly_get_coeff_ptr(a_tmp, i);
@ -296,7 +296,7 @@ poly_starmultiply(const fmpz_poly_t a,
bool
poly_inverse_poly_q(const fmpz_poly_t a,
fmpz_poly_t Fq,
const ntru_context *ctx)
const ntru_params *params)
{
bool retval = false;
int k = 0,
@ -318,7 +318,7 @@ poly_inverse_poly_q(const fmpz_poly_t a,
/* set g(x) = x^N 1 */
fmpz_poly_init(g);
fmpz_poly_set_coeff_si(g, 0, -1);
fmpz_poly_set_coeff_si(g, ctx->N, 1);
fmpz_poly_set_coeff_si(g, params->N, 1);
/* avoid side effects */
fmpz_poly_init(a_tmp);
@ -327,20 +327,20 @@ poly_inverse_poly_q(const fmpz_poly_t a,
while (1) {
while (fmpz_is_zero(fmpz_poly_get_coeff_ptr(f, 0))) {
for (uint32_t i = 1; i <= ctx->N; i++) {
for (uint32_t i = 1; i <= params->N; i++) {
fmpz *f_coeff = fmpz_poly_get_coeff_ptr(f, i);
fmpz *c_coeff = fmpz_poly_get_coeff_ptr(c, ctx->N - i);
fmpz *c_coeff = fmpz_poly_get_coeff_ptr(c, params->N - i);
/* f(x) = f(x) / x */
fmpz_poly_set_coeff_fmpz_n(f, i - 1,
f_coeff);
/* c(x) = c(x) * x */
fmpz_poly_set_coeff_fmpz_n(c, ctx->N + 1 - i,
fmpz_poly_set_coeff_fmpz_n(c, params->N + 1 - i,
c_coeff);
}
fmpz_poly_set_coeff_si(f, ctx->N, 0);
fmpz_poly_set_coeff_si(f, params->N, 0);
fmpz_poly_set_coeff_si(c, 0, 0);
k++;
@ -364,30 +364,30 @@ poly_inverse_poly_q(const fmpz_poly_t a,
fmpz_poly_mod_unsigned(b, 2);
}
k = k % ctx->N;
k = k % params->N;
b_last = fmpz_poly_get_coeff_ptr(b, ctx->N);
b_last = fmpz_poly_get_coeff_ptr(b, params->N);
if (fmpz_cmp_si_n(b_last, 0))
goto _cleanup;
/* Fq(x) = x^(N-k) * b(x) */
for (int i = ctx->N - 1; i >= 0; i--) {
for (int i = params->N - 1; i >= 0; i--) {
fmpz *b_i;
j = i - k;
if (j < 0)
j = j + ctx->N;
j = j + params->N;
b_i = fmpz_poly_get_coeff_ptr(b, i);
fmpz_poly_set_coeff_fmpz_n(Fq, j, b_i);
}
poly_mod2_to_modq(a_tmp, Fq, ctx);
poly_mod2_to_modq(a_tmp, Fq, 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, ctx, ctx->q);
poly_starmultiply(a_tmp, Fq, a_tmp, params, params->q);
if (fmpz_poly_is_one(a_tmp))
retval = true;
else
@ -408,7 +408,7 @@ _cleanup:
bool
poly_inverse_poly_p(const fmpz_poly_t a,
fmpz_poly_t Fp,
const ntru_context *ctx)
const ntru_params *params)
{
bool retval = false;
int k = 0,
@ -430,7 +430,7 @@ poly_inverse_poly_p(const fmpz_poly_t a,
/* set g(x) = x^N 1 */
fmpz_poly_init(g);
fmpz_poly_set_coeff_si(g, 0, -1);
fmpz_poly_set_coeff_si(g, ctx->N, 1);
fmpz_poly_set_coeff_si(g, params->N, 1);
/* avoid side effects */
fmpz_poly_init(a_tmp);
@ -439,20 +439,20 @@ poly_inverse_poly_p(const fmpz_poly_t a,
while (1) {
while (fmpz_is_zero(fmpz_poly_get_coeff_ptr(f, 0))) {
for (uint32_t i = 1; i <= ctx->N; i++) {
for (uint32_t i = 1; i <= params->N; i++) {
fmpz *f_coeff = fmpz_poly_get_coeff_ptr(f, i);
fmpz *c_coeff = fmpz_poly_get_coeff_ptr(c, ctx->N - i);
fmpz *c_coeff = fmpz_poly_get_coeff_ptr(c, params->N - i);
/* f(x) = f(x) / x */
fmpz_poly_set_coeff_fmpz_n(f, i - 1,
f_coeff);
/* c(x) = c(x) * x */
fmpz_poly_set_coeff_fmpz_n(c, ctx->N + 1 - i,
fmpz_poly_set_coeff_fmpz_n(c, params->N + 1 - i,
c_coeff);
}
fmpz_poly_set_coeff_si(f, ctx->N, 0);
fmpz_poly_set_coeff_si(f, params->N, 0);
fmpz_poly_set_coeff_si(c, 0, 0);
k++;
@ -491,20 +491,20 @@ poly_inverse_poly_p(const fmpz_poly_t a,
/* = (f[0] mod p) * (g[0] inverse mod p) mod p */
fmpz_invmod_ui(u,
fmpz_poly_get_coeff_ptr(g, 0),
ctx->p);
fmpz_mod_ui(mp_tmp, mp_tmp, ctx->p);
params->p);
fmpz_mod_ui(mp_tmp, mp_tmp, params->p);
fmpz_mul(u, mp_tmp, u);
fmpz_mod_ui(u, u, ctx->p);
fmpz_mod_ui(u, u, params->p);
/* f = f - u * g mod p */
fmpz_poly_scalar_mul_fmpz(g_tmp, g_tmp, u);
fmpz_poly_sub(f, g_tmp, f);
fmpz_poly_mod_unsigned(f, ctx->p);
fmpz_poly_mod_unsigned(f, params->p);
/* b = b - u * c mod p */
fmpz_poly_scalar_mul_fmpz(c_tmp, c_tmp, u);
fmpz_poly_sub(b, c_tmp, b);
fmpz_poly_mod_unsigned(b, ctx->p);
fmpz_poly_mod_unsigned(b, params->p);
fmpz_clear(u);
fmpz_poly_clear(g_tmp);
@ -512,14 +512,14 @@ poly_inverse_poly_p(const fmpz_poly_t a,
}
}
k = k % ctx->N;
k = k % params->N;
b_last = fmpz_poly_get_coeff_ptr(b, ctx->N);
b_last = fmpz_poly_get_coeff_ptr(b, params->N);
if (fmpz_cmp_si_n(b_last, 0))
goto cleanup;
/* Fp(x) = x^(N-k) * b(x) */
for (int i = ctx->N - 1; i >= 0; i--) {
for (int i = params->N - 1; i >= 0; i--) {
fmpz *b_i;
/* b(X) = f[0]^(-1) * b(X) (mod p) */
@ -530,7 +530,7 @@ poly_inverse_poly_p(const fmpz_poly_t a,
fmpz_invmod_ui(mp_tmp,
fmpz_poly_get_coeff_ptr(f, 0),
ctx->p);
params->p);
if (fmpz_poly_get_coeff_ptr(b, i)) {
fmpz_mul(fmpz_poly_get_coeff_ptr(b, i),
@ -538,13 +538,13 @@ poly_inverse_poly_p(const fmpz_poly_t a,
mp_tmp);
fmpz_mod_ui(fmpz_poly_get_coeff_ptr(b, i),
fmpz_poly_get_coeff_ptr(b, i),
ctx->p);
params->p);
}
}
j = i - k;
if (j < 0)
j = j + ctx->N;
j = j + params->N;
b_i = fmpz_poly_get_coeff_ptr(b, i);
fmpz_poly_set_coeff_fmpz_n(Fp, j, b_i);
@ -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, ctx, ctx->p);
poly_starmultiply(a_tmp, Fp, a_tmp, params, params->p);
if (fmpz_poly_is_one(a_tmp))
retval = true;
else

View File

@ -28,8 +28,8 @@
#ifndef NTRU_POLY_H
#define NTRU_POLY_H
#include "context.h"
#include "err.h"
#include "params.h"
#include <stdarg.h>
#include <stdbool.h>
@ -53,7 +53,7 @@ fmpz_cmp_si_n(const fmpz_t f, slong g);
/**
* Initializes and builds a polynomial with the
* coefficient values of c[] of size len within NTRU
* context ctx and returns a newly allocated polynomial.
* parameters and returns a newly allocated polynomial.
* For an empty polynom, both parameters can be NULL/0.
*
* @param new_poly the polynomial to initialize and
@ -172,14 +172,14 @@ fmpz_add_n(fmpz_t f, const fmpz_t g, const fmpz_t h);
* @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 params NTRU parameters
* @param modulus whether we use p or q
*/
void
poly_starmultiply(const fmpz_poly_t a,
const fmpz_poly_t b,
fmpz_poly_t c,
const ntru_context *ctx,
const ntru_params *params,
uint32_t modulus);
/**
@ -191,13 +191,13 @@ poly_starmultiply(const fmpz_poly_t a,
*
* @param a polynomial to invert (is allowed to be the same as param Fq)
* @param Fq polynomial [out]
* @param ctx NTRU context
* @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,
const ntru_context *ctx);
const ntru_params *params);
/**
* Compute the inverse of a polynomial in (Z/pZ)[X]/(X^N - 1).
@ -206,12 +206,12 @@ poly_inverse_poly_q(const fmpz_poly_t a,
*
* @param a polynomial to invert
* @param Fp polynomial [out]
* @param ctx NTRU context
* @param params NTRU parameters
*/
bool
poly_inverse_poly_p(const fmpz_poly_t a,
fmpz_poly_t Fp,
const ntru_context *ctx);
const ntru_params *params);
/**
* Draws a polynomial to stdout.

View File

@ -28,9 +28,9 @@
#include "poly_ascii.h"
#include "common.h"
#include "context.h"
#include "mem.h"
#include "ntru_string.h"
#include "params.h"
#include "poly.h"
#include <glib.h>
@ -112,13 +112,13 @@ get_bin_arr_to_ascii(const char *binary_rep)
string *
bin_poly_to_ascii(const fmpz_poly_t poly,
const ntru_context *ctx)
const ntru_params *params)
{
string *result_string = ntru_malloc(sizeof(*result_string));
char *binary_rep = ntru_malloc(CHAR_SIZE * (ctx->N));
char *binary_rep = ntru_malloc(CHAR_SIZE * (params->N));
uint32_t i = 0;
for (uint32_t j = 0; j < ctx->N; j++) {
for (uint32_t j = 0; j < params->N; j++) {
fmpz *coeff = fmpz_poly_get_coeff_ptr(poly, j);
if (coeff) {
@ -143,7 +143,7 @@ bin_poly_to_ascii(const fmpz_poly_t poly,
string *
bin_poly_arr_to_ascii(fmpz_poly_t **bin_poly_arr,
const ntru_context *ctx)
const ntru_params *params)
{
fmpz_poly_t *ascii_poly;
char *binary_rep = NULL;
@ -155,11 +155,11 @@ bin_poly_arr_to_ascii(fmpz_poly_t **bin_poly_arr,
/*
* parse the polynomial coefficients into a string
*/
binary_rep = ntru_calloc(1, CHAR_SIZE * (ctx->N + 1));
binary_rep = ntru_calloc(1, CHAR_SIZE * (params->N + 1));
while ((ascii_poly = (fmpz_poly_t *)*bin_poly_arr++)) {
string *single_poly_string = NULL;
new_length = CHAR_SIZE * (ctx->N);
new_length = CHAR_SIZE * (params->N);
REALLOC(binary_rep,
old_length +
@ -168,7 +168,7 @@ bin_poly_arr_to_ascii(fmpz_poly_t **bin_poly_arr,
old_length += new_length;
single_poly_string = bin_poly_to_ascii(*ascii_poly, ctx);
single_poly_string = bin_poly_to_ascii(*ascii_poly, params);
memcpy(binary_rep + string_len,
single_poly_string->ptr,
@ -191,21 +191,21 @@ bin_poly_arr_to_ascii(fmpz_poly_t **bin_poly_arr,
string *
poly_to_ascii(const fmpz_poly_t poly,
const ntru_context *ctx)
const ntru_params *params)
{
string *result_string = ntru_malloc(sizeof(*result_string));
char *string_rep = ntru_malloc(CHAR_SIZE * (ctx->N));
char *string_rep = ntru_malloc(CHAR_SIZE * (params->N));
for (uint32_t j = 0; j < ctx->N; j++) {
for (uint32_t j = 0; j < params->N; j++) {
uint8_t coeff = fmpz_poly_get_coeff_ui(poly, j);
if (coeff == ctx->q)
if (coeff == params->q)
string_rep[j] = '\0';
else
string_rep[j] = (char)coeff;
}
result_string->ptr = string_rep;
result_string->len = ctx->N;
result_string->len = params->N;
return result_string;
}
@ -214,7 +214,7 @@ poly_to_ascii(const fmpz_poly_t poly,
string *
poly_arr_to_ascii(fmpz_poly_t **poly_array,
const ntru_context *ctx)
const ntru_params *params)
{
fmpz_poly_t *ascii_poly;
char *string_rep = NULL;
@ -226,11 +226,11 @@ poly_arr_to_ascii(fmpz_poly_t **poly_array,
/*
* parse the polynomial coefficients into a string
*/
string_rep = ntru_calloc(1, CHAR_SIZE * (ctx->N + 1));
string_rep = ntru_calloc(1, CHAR_SIZE * (params->N + 1));
while ((ascii_poly = *poly_array++)) {
string *poly_str;
poly_str = poly_to_ascii(*ascii_poly, ctx);
poly_str = poly_to_ascii(*ascii_poly, params);
new_length = CHAR_SIZE * poly_str->len;
REALLOC(string_rep,
@ -256,14 +256,14 @@ poly_arr_to_ascii(fmpz_poly_t **poly_array,
string *
poly_to_base64(const fmpz_poly_t poly,
const ntru_context *ctx)
const ntru_params *params)
{
string *result_string = ntru_malloc(sizeof(*result_string));
string *string_rep = NULL;
gchar *base64_string = NULL,
*tmp = NULL;
string_rep = poly_to_ascii(poly, ctx);
string_rep = poly_to_ascii(poly, params);
tmp = g_base64_encode((const guchar *)string_rep->ptr,
string_rep->len);
@ -283,7 +283,7 @@ poly_to_base64(const fmpz_poly_t poly,
string *
poly_arr_to_base64(fmpz_poly_t **poly_array,
const ntru_context *ctx)
const ntru_params *params)
{
string *string_rep;
string *result_string = ntru_malloc(sizeof(*result_string));
@ -291,7 +291,7 @@ poly_arr_to_base64(fmpz_poly_t **poly_array,
gchar *base64_string = NULL,
*tmp = NULL;
string_rep = poly_arr_to_ascii(poly_array, ctx);
string_rep = poly_arr_to_ascii(poly_array, params);
tmp = g_base64_encode((const guchar *)string_rep->ptr, string_rep->len);
base64_string = g_base64_encode((const guchar *)tmp,

View File

@ -30,8 +30,8 @@
#include "common.h"
#include "context.h"
#include "ntru_string.h"
#include "params.h"
#include <fmpz_poly.h>
#include <fmpz.h>
@ -53,12 +53,12 @@
* not confuse the result.
*
* @param poly the binary polynomial to convert
* @param ctx the NTRUEncrypt context
* @param params the NTRU parameters
* @return the real string, newly allocated
*/
string *
bin_poly_to_ascii(const fmpz_poly_t poly,
const ntru_context *ctx);
const ntru_params *params);
/**
* Convert an array of binary polynomials back to a real string.
@ -76,12 +76,12 @@ bin_poly_to_ascii(const fmpz_poly_t poly,
* the result.
*
* @param bin_poly_arr the array of polynomials
* @param ctx the NTRUEncrypt context
* @param params the NTRU parameters
* @return the real string, newly allocated
*/
string *
bin_poly_arr_to_ascii(fmpz_poly_t **bin_poly_arr,
const ntru_context *ctx);
const ntru_params *params);
/**
* Convert a single polynom back to a real string which is
@ -93,12 +93,12 @@ bin_poly_arr_to_ascii(fmpz_poly_t **bin_poly_arr,
* a polynomial with '\0', so they will not confuse the result.
*
* @param poly the polynomial to convert
* @param ctx the NTRUEncrypt context
* @param params the NTRU parameters
* @return the real string, newly allocated
*/
string *
poly_to_ascii(const fmpz_poly_t poly,
const ntru_context *ctx);
const ntru_params *params);
/**
* Convert an array of polynomials back to a real string.
@ -109,12 +109,12 @@ poly_to_ascii(const fmpz_poly_t poly,
* a polynomial with '\0', so they will not confuse the result.
*
* @param poly_array the array of polynomials
* @param ctx the NTRUEncrypt context
* @param params the NTRU parameters
* @return the real string, newly allocated
*/
string *
poly_arr_to_ascii(fmpz_poly_t **poly_array,
const ntru_context *ctx);
const ntru_params *params);
/**
* Convert a single polynom back to a real string which is
@ -126,12 +126,12 @@ poly_arr_to_ascii(fmpz_poly_t **poly_array,
* a polynomial with '\0', so they will not confuse the result.
*
* @param poly the polynomial to convert
* @param ctx the NTRUEncrypt context
* @param params the NTRU parameters
* @return the real string, newly allocated
*/
string *
poly_to_base64(const fmpz_poly_t poly,
const ntru_context *ctx);
const ntru_params *params);
/**
* Convert an array of polynomials back to a real string which
@ -143,11 +143,11 @@ poly_to_base64(const fmpz_poly_t poly,
* a polynomial with '\0', so they will not confuse the result.
*
* @param poly_arr the array of polynomials
* @param ctx the NTRUEncrypt context
* @param params the NTRU parameters
* @return the real string, newly allocated
*/
string *
poly_arr_to_base64(fmpz_poly_t **poly_arr, const ntru_context *ctx);
poly_arr_to_base64(fmpz_poly_t **poly_arr, const ntru_params *params);
#endif /* NTRU_POLY_ASCII_H_ */

View File

@ -25,9 +25,9 @@
* @brief random polynomials
*/
#include "context.h"
#include "err.h"
#include "math.h"
#include "params.h"
#include "poly.h"
#include <fmpz_poly.h>
@ -84,19 +84,19 @@ get_urnd_int(void)
void
ntru_get_rnd_tern_poly_num(fmpz_poly_t poly,
const ntru_context *ctx,
const ntru_params *params,
uint32_t num_ones,
uint32_t num_neg_ones,
int (*rnd_int)(void))
{
if (!poly || ! ctx)
if (!poly || ! params)
NTRU_ABORT("unexpected NULL parameters in"
"ntru_get_rnd_tern_poly_num()!\n");
fmpz_poly_zero(poly);
while (num_ones != 0 || num_neg_ones != 0) {
int32_t pos = rnd_int() % ctx->N;
int32_t pos = rnd_int() % params->N;
if (!fmpz_cmp_si_n(fmpz_poly_get_coeff_ptr(poly, pos), 0)) {
if (num_ones > 0) {

View File

@ -28,7 +28,7 @@
#ifndef NTRU_RND_H
#define NTRU_RND_H
#include "context.h"
#include "params.h"
#include <stdlib.h>
@ -56,7 +56,7 @@ get_urnd_int(void);
* of 1 coefficients and -1 coefficients.
*
* @param poly the resulting random polynomial [out]
* @param ctx the NTRU context
* @param params the NTRU context
* @param num_ones the number of 1 coefficients
* @param num_neg_ones the number of -1 coefficients
* @param rnd_int function callback which should return
@ -64,7 +64,7 @@ get_urnd_int(void);
*/
void
ntru_get_rnd_tern_poly_num(fmpz_poly_t poly,
const ntru_context *ctx,
const ntru_params *params,
uint32_t num_ones,
uint32_t num_neg_ones,
int (*rnd_int)(void));