POLY->ASCII: cleanup, rm REALLOCs
This commit is contained in:
parent
7a7072675e
commit
b52690d88e
@ -113,7 +113,8 @@ ntru_decrypt_string(
|
|||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
decr_msg = bin_poly_arr_to_ascii(poly_array, params);
|
decr_msg = bin_poly_arr_to_ascii((const fmpz_poly_t **)poly_array,
|
||||||
|
i, params);
|
||||||
|
|
||||||
poly_delete_array(poly_array);
|
poly_delete_array(poly_array);
|
||||||
|
|
||||||
|
@ -95,7 +95,8 @@ ntru_encrypt_string(
|
|||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
enc_msg = poly_arr_to_base64(poly_array, params);
|
enc_msg = poly_arr_to_base64((const fmpz_poly_t **)poly_array,
|
||||||
|
i, params);
|
||||||
|
|
||||||
poly_delete_array(poly_array);
|
poly_delete_array(poly_array);
|
||||||
|
|
||||||
|
@ -118,8 +118,8 @@ bin_poly_to_ascii(const fmpz_poly_t poly,
|
|||||||
char *binary_rep = ntru_malloc(CHAR_SIZE * (params->N));
|
char *binary_rep = ntru_malloc(CHAR_SIZE * (params->N));
|
||||||
uint32_t i = 0;
|
uint32_t i = 0;
|
||||||
|
|
||||||
for (uint32_t j = 0; j < params->N; j++) {
|
for (i = 0; i < params->N; i++) {
|
||||||
fmpz *coeff = fmpz_poly_get_coeff_ptr(poly, j);
|
fmpz *coeff = fmpz_poly_get_coeff_ptr(poly, i);
|
||||||
|
|
||||||
if (coeff) {
|
if (coeff) {
|
||||||
if (!fmpz_cmp_si(coeff, 1))
|
if (!fmpz_cmp_si(coeff, 1))
|
||||||
@ -129,8 +129,6 @@ bin_poly_to_ascii(const fmpz_poly_t poly,
|
|||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result_string->ptr = binary_rep;
|
result_string->ptr = binary_rep;
|
||||||
@ -142,33 +140,22 @@ bin_poly_to_ascii(const fmpz_poly_t poly,
|
|||||||
/*------------------------------------------------------------------------*/
|
/*------------------------------------------------------------------------*/
|
||||||
|
|
||||||
string *
|
string *
|
||||||
bin_poly_arr_to_ascii(fmpz_poly_t **bin_poly_arr,
|
bin_poly_arr_to_ascii(const fmpz_poly_t **bin_poly_arr,
|
||||||
|
const uint32_t poly_c,
|
||||||
const ntru_params *params)
|
const ntru_params *params)
|
||||||
{
|
{
|
||||||
fmpz_poly_t *ascii_poly;
|
|
||||||
char *binary_rep = NULL;
|
char *binary_rep = NULL;
|
||||||
size_t string_len = 0;
|
size_t string_len = 0;
|
||||||
string *ascii_string = NULL;
|
string *ascii_string = NULL;
|
||||||
size_t old_length = 0,
|
|
||||||
new_length;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* parse the polynomial coefficients into a string
|
* parse the polynomial coefficients into a string
|
||||||
*/
|
*/
|
||||||
binary_rep = ntru_calloc(1, CHAR_SIZE * (params->N + 1));
|
binary_rep = ntru_malloc(CHAR_SIZE * (params->N * poly_c + 1));
|
||||||
while ((ascii_poly = (fmpz_poly_t *)*bin_poly_arr++)) {
|
for (uint32_t i = 0; i < poly_c; i++) {
|
||||||
string *single_poly_string = NULL;
|
string *single_poly_string = NULL;
|
||||||
|
|
||||||
new_length = CHAR_SIZE * (params->N);
|
single_poly_string = bin_poly_to_ascii(*bin_poly_arr[i], params);
|
||||||
|
|
||||||
REALLOC(binary_rep,
|
|
||||||
old_length +
|
|
||||||
new_length +
|
|
||||||
1); /* trailing null byte */
|
|
||||||
|
|
||||||
old_length += new_length;
|
|
||||||
|
|
||||||
single_poly_string = bin_poly_to_ascii(*ascii_poly, params);
|
|
||||||
|
|
||||||
memcpy(binary_rep + string_len,
|
memcpy(binary_rep + string_len,
|
||||||
single_poly_string->ptr,
|
single_poly_string->ptr,
|
||||||
@ -213,30 +200,22 @@ poly_to_ascii(const fmpz_poly_t poly,
|
|||||||
/*------------------------------------------------------------------------*/
|
/*------------------------------------------------------------------------*/
|
||||||
|
|
||||||
string *
|
string *
|
||||||
poly_arr_to_ascii(fmpz_poly_t **poly_array,
|
poly_arr_to_ascii(const fmpz_poly_t **poly_array,
|
||||||
|
const uint32_t poly_c,
|
||||||
const ntru_params *params)
|
const ntru_params *params)
|
||||||
{
|
{
|
||||||
fmpz_poly_t *ascii_poly;
|
|
||||||
char *string_rep = NULL;
|
char *string_rep = NULL;
|
||||||
size_t string_len = 0;
|
size_t string_len = 0;
|
||||||
string *result_string = ntru_malloc(sizeof(*result_string));
|
string *result_string = ntru_malloc(sizeof(*result_string));
|
||||||
size_t old_length = 0,
|
|
||||||
new_length;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* parse the polynomial coefficients into a string
|
* parse the polynomial coefficients into a string
|
||||||
*/
|
*/
|
||||||
string_rep = ntru_calloc(1, CHAR_SIZE * (params->N + 1));
|
string_rep = ntru_malloc(CHAR_SIZE * (params->N * poly_c + 1));
|
||||||
while ((ascii_poly = *poly_array++)) {
|
for (uint32_t i = 0; i < poly_c; i++) {
|
||||||
string *poly_str;
|
string *poly_str;
|
||||||
|
|
||||||
poly_str = poly_to_ascii(*ascii_poly, params);
|
poly_str = poly_to_ascii(*poly_array[i], params);
|
||||||
|
|
||||||
new_length = CHAR_SIZE * poly_str->len;
|
|
||||||
REALLOC(string_rep,
|
|
||||||
old_length +
|
|
||||||
new_length);
|
|
||||||
old_length += new_length;
|
|
||||||
|
|
||||||
memcpy(string_rep + string_len,
|
memcpy(string_rep + string_len,
|
||||||
poly_str->ptr,
|
poly_str->ptr,
|
||||||
@ -282,7 +261,8 @@ poly_to_base64(const fmpz_poly_t poly,
|
|||||||
/*------------------------------------------------------------------------*/
|
/*------------------------------------------------------------------------*/
|
||||||
|
|
||||||
string *
|
string *
|
||||||
poly_arr_to_base64(fmpz_poly_t **poly_array,
|
poly_arr_to_base64(const fmpz_poly_t **poly_array,
|
||||||
|
const uint32_t poly_c,
|
||||||
const ntru_params *params)
|
const ntru_params *params)
|
||||||
{
|
{
|
||||||
string *string_rep;
|
string *string_rep;
|
||||||
@ -291,7 +271,7 @@ poly_arr_to_base64(fmpz_poly_t **poly_array,
|
|||||||
gchar *base64_string = NULL,
|
gchar *base64_string = NULL,
|
||||||
*tmp = NULL;
|
*tmp = NULL;
|
||||||
|
|
||||||
string_rep = poly_arr_to_ascii(poly_array, params);
|
string_rep = poly_arr_to_ascii(poly_array, poly_c, params);
|
||||||
|
|
||||||
tmp = g_base64_encode((const guchar *)string_rep->ptr, string_rep->len);
|
tmp = g_base64_encode((const guchar *)string_rep->ptr, string_rep->len);
|
||||||
base64_string = g_base64_encode((const guchar *)tmp,
|
base64_string = g_base64_encode((const guchar *)tmp,
|
||||||
|
@ -76,11 +76,13 @@ bin_poly_to_ascii(const fmpz_poly_t poly,
|
|||||||
* the result.
|
* the result.
|
||||||
*
|
*
|
||||||
* @param bin_poly_arr the array of polynomials
|
* @param bin_poly_arr the array of polynomials
|
||||||
|
* @param poly_c the amount of polynomials in bin_poly_arr
|
||||||
* @param params the NTRU parameters
|
* @param params the NTRU parameters
|
||||||
* @return the real string, newly allocated
|
* @return the real string, newly allocated
|
||||||
*/
|
*/
|
||||||
string *
|
string *
|
||||||
bin_poly_arr_to_ascii(fmpz_poly_t **bin_poly_arr,
|
bin_poly_arr_to_ascii(const fmpz_poly_t **bin_poly_arr,
|
||||||
|
const uint32_t poly_c,
|
||||||
const ntru_params *params);
|
const ntru_params *params);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -109,11 +111,13 @@ poly_to_ascii(const fmpz_poly_t poly,
|
|||||||
* a polynomial with '\0', so they will not confuse the result.
|
* a polynomial with '\0', so they will not confuse the result.
|
||||||
*
|
*
|
||||||
* @param poly_array the array of polynomials
|
* @param poly_array the array of polynomials
|
||||||
|
* @param poly_c the amount of polynomials in poly_arr
|
||||||
* @param params the NTRU parameters
|
* @param params the NTRU parameters
|
||||||
* @return the real string, newly allocated
|
* @return the real string, newly allocated
|
||||||
*/
|
*/
|
||||||
string *
|
string *
|
||||||
poly_arr_to_ascii(fmpz_poly_t **poly_array,
|
poly_arr_to_ascii(const fmpz_poly_t **poly_array,
|
||||||
|
const uint32_t poly_c,
|
||||||
const ntru_params *params);
|
const ntru_params *params);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -143,11 +147,14 @@ poly_to_base64(const fmpz_poly_t poly,
|
|||||||
* a polynomial with '\0', so they will not confuse the result.
|
* a polynomial with '\0', so they will not confuse the result.
|
||||||
*
|
*
|
||||||
* @param poly_arr the array of polynomials
|
* @param poly_arr the array of polynomials
|
||||||
|
* @param poly_c the amount of polynomials in poly_arr
|
||||||
* @param params the NTRU parameters
|
* @param params the NTRU parameters
|
||||||
* @return the real string, newly allocated
|
* @return the real string, newly allocated
|
||||||
*/
|
*/
|
||||||
string *
|
string *
|
||||||
poly_arr_to_base64(fmpz_poly_t **poly_arr, const ntru_params *params);
|
poly_arr_to_base64(const fmpz_poly_t **poly_arr,
|
||||||
|
const uint32_t poly_c,
|
||||||
|
const ntru_params *params);
|
||||||
|
|
||||||
|
|
||||||
#endif /* NTRU_POLY_ASCII_H_ */
|
#endif /* NTRU_POLY_ASCII_H_ */
|
||||||
|
Loading…
Reference in New Issue
Block a user