42 #include <fmpz_poly.h>
71 size_t int_arr_size = 0;
72 uint8_t *int_arr = NULL;
74 char *int_string = NULL;
77 if (!binary_rep || !*binary_rep)
80 int_arr_size = strlen(binary_rep) /
ASCII_BITS + 1;
81 int_arr =
ntru_malloc(
sizeof(*int_arr) * int_arr_size);
87 for (uint32_t j = 0; j <
ASCII_BITS && *binary_rep; j++) {
88 if (*binary_rep ==
'1')
89 int_arr[i] = int_arr[i] * 2 + 1;
90 else if (*binary_rep ==
'0')
100 for (uint32_t j = 0; j < i; j++)
101 int_string[j] = (
char) int_arr[j];
103 result->
ptr = int_string;
117 string *result_string =
ntru_malloc(
sizeof(*result_string));
121 for (i = 0; i < params->
N; i++) {
122 fmpz *coeff = fmpz_poly_get_coeff_ptr(poly, i);
125 if (!fmpz_cmp_si(coeff, 1))
127 else if (!fmpz_cmp_si(coeff, -1))
134 result_string->
ptr = binary_rep;
135 result_string->
len = i;
137 return result_string;
144 const uint32_t poly_c,
147 char *binary_rep = NULL;
148 size_t string_len = 0;
149 string *ascii_string = NULL;
155 for (uint32_t i = 0; i < poly_c; i++) {
156 string *single_poly_string = NULL;
160 memcpy(binary_rep + string_len,
161 single_poly_string->
ptr,
162 single_poly_string->
len);
164 string_len += single_poly_string->
len;
168 binary_rep[string_len] =
'\0';
183 string *result_string =
ntru_malloc(
sizeof(*result_string));
186 for (uint32_t j = 0; j < params->
N; j++) {
187 uint8_t coeff = fmpz_poly_get_coeff_ui(poly, j);
188 if (coeff == params->
q)
189 string_rep[j] =
'\0';
191 string_rep[j] = (char)coeff;
194 result_string->
ptr = string_rep;
195 result_string->
len = params->
N;
197 return result_string;
204 const uint32_t poly_c,
207 char *string_rep = NULL;
208 size_t string_len = 0;
209 string *result_string =
ntru_malloc(
sizeof(*result_string));
215 for (uint32_t i = 0; i < poly_c; i++) {
220 memcpy(string_rep + string_len,
223 string_len += poly_str->
len;
228 result_string->
ptr = string_rep;
229 result_string->
len = string_len;
231 return result_string;
240 string *result_string =
ntru_malloc(
sizeof(*result_string));
241 string *string_rep = NULL;
242 gchar *base64_string = NULL,
247 tmp = g_base64_encode((
const guchar *)string_rep->
ptr,
249 base64_string = g_base64_encode((
const guchar *)tmp,
252 result_string->
ptr = base64_string;
253 result_string->
len = strlen(base64_string);
258 return result_string;
265 const uint32_t poly_c,
269 string *result_string =
ntru_malloc(
sizeof(*result_string));
271 gchar *base64_string = NULL,
276 tmp = g_base64_encode((
const guchar *)string_rep->
ptr, string_rep->
len);
277 base64_string = g_base64_encode((
const guchar *)tmp,
280 result_string->
ptr = base64_string;
281 result_string->
len = strlen(base64_string);
286 return result_string;
void string_delete(string *del_string)
string * poly_to_ascii(const fmpz_poly_t poly, const ntru_params *params)
string * poly_arr_to_base64(const fmpz_poly_t **poly_array, const uint32_t poly_c, const ntru_params *params)
static string * get_bin_arr_to_ascii(const char *binary_rep)
header for ntru_poly_ascii.c
string * poly_to_base64(const fmpz_poly_t poly, const ntru_params *params)
string * bin_poly_arr_to_ascii(const fmpz_poly_t **bin_poly_arr, const uint32_t poly_c, const ntru_params *params)
string * bin_poly_to_ascii(const fmpz_poly_t poly, const ntru_params *params)
void * ntru_malloc(size_t size)
string * poly_arr_to_ascii(const fmpz_poly_t **poly_array, const uint32_t poly_c, const ntru_params *params)