ASCII->POLY: improve performance wrt #6

It appears that the strcat call was the culprit here and
caused non-linear run-time growth of the encryption algorithm.

Replacing it with memcpy fixed this.
This commit is contained in:
hasufell 2014-06-08 16:17:01 +02:00
부모 3bce63f96a
커밋 5ff7c1bd1a
No known key found for this signature in database
GPG 키 ID: 220CD1C5BDEED020
1개의 변경된 파일6개의 추가작업 그리고 3개의 파일을 삭제

파일 보기

@ -103,26 +103,29 @@ ascii_to_bin_poly_arr(const string *to_poly, const ntru_params *params)
char *cur = to_poly->ptr; char *cur = to_poly->ptr;
char *out = ntru_malloc(CHAR_SIZE * (to_poly->len * ASCII_BITS + 1)); char *out = ntru_malloc(CHAR_SIZE * (to_poly->len * ASCII_BITS + 1));
uint32_t polyc = 0; uint32_t polyc = 0;
size_t out_len = 0;
fmpz_poly_t **poly_array; fmpz_poly_t **poly_array;
*out = '\0'; *out = '\0';
for (uint32_t i = 0; i < to_poly->len; i++) { for (uint32_t i = 0; i < to_poly->len; i++) {
char *tmp_string = get_int_to_bin_str((int)(*cur)); char *tmp_string = get_int_to_bin_str((int)(*cur));
strcat(out, tmp_string); memcpy(out + out_len, tmp_string, ASCII_BITS);
out_len += ASCII_BITS;
cur++; cur++;
free(tmp_string); free(tmp_string);
} }
out[out_len] = '\0';
poly_array = ntru_malloc(sizeof(**poly_array) * poly_array = ntru_malloc(sizeof(**poly_array) *
(strlen(out) / params->N + 1)); (strlen(out) / params->N + 1));
for (uint32_t i = 0; i < strlen(out); i += params->N) { for (uint32_t i = 0; i < out_len; i += params->N) {
char chunk[params->N + 1]; char chunk[params->N + 1];
size_t real_chunk_size; size_t real_chunk_size;
real_chunk_size = real_chunk_size =
(strlen(out + i) > params->N) ? params->N : strlen(out + i); (out_len - i > params->N) ? params->N : out_len - i;
memcpy(chunk, out + i, real_chunk_size); memcpy(chunk, out + i, real_chunk_size);
chunk[real_chunk_size] = '\0'; chunk[real_chunk_size] = '\0';