ASCII->POLY: simplify with macros
This commit is contained in:
parent
1a1f610d38
commit
d6a2269f2f
@ -38,7 +38,7 @@ LIBS += -L. -lgmp -lmpfr -lm
|
|||||||
|
|
||||||
# objects
|
# objects
|
||||||
PQC_OBJS = poly.o mem.o encrypt.o decrypt.o keypair.o ascii_poly.o
|
PQC_OBJS = poly.o mem.o encrypt.o decrypt.o keypair.o ascii_poly.o
|
||||||
PQC_HEADERS = err.h poly.h context.h encrypt.h decrypt.h keypair.h ascii_poly.h
|
PQC_HEADERS = err.h poly.h context.h encrypt.h decrypt.h keypair.h ascii_poly.h common.h
|
||||||
# CUNIT_OBJS = cunit.o
|
# CUNIT_OBJS = cunit.o
|
||||||
|
|
||||||
# includes
|
# includes
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
* MA 02110-1301 USA
|
* MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
#include "context.h"
|
#include "context.h"
|
||||||
#include "mem.h"
|
#include "mem.h"
|
||||||
#include "poly.h"
|
#include "poly.h"
|
||||||
@ -49,15 +50,14 @@ static char *get_bin_arr_to_ascii(char *binary_rep);
|
|||||||
static char *get_int_to_bin_str(uint8_t value)
|
static char *get_int_to_bin_str(uint8_t value)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
const size_t ascii_bit_size = 8;
|
const size_t bin_string_size = ASCII_BITS + 1;
|
||||||
const size_t bin_string_size = ascii_bit_size + 1;
|
char *bin_string = ntru_malloc(sizeof(*bin_string) *
|
||||||
char *bin_string = ntru_malloc(sizeof(char) *
|
|
||||||
(bin_string_size)); /* account for trailing null-byte */
|
(bin_string_size)); /* account for trailing null-byte */
|
||||||
|
|
||||||
/* terminate properly */
|
/* terminate properly */
|
||||||
bin_string[bin_string_size - 1] = '\0';
|
bin_string[bin_string_size - 1] = '\0';
|
||||||
|
|
||||||
for (i = 7; i >= 0; --i, value >>= 1)
|
for (i = ASCII_BITS - 1; i >= 0; --i, value >>= 1)
|
||||||
bin_string[i] = (value & 1) + '0';
|
bin_string[i] = (value & 1) + '0';
|
||||||
|
|
||||||
return bin_string;
|
return bin_string;
|
||||||
@ -84,7 +84,7 @@ static char *get_bin_arr_to_ascii(char *binary_rep)
|
|||||||
|
|
||||||
while (*tmp_string) {
|
while (*tmp_string) {
|
||||||
int_arr[i] = 0;
|
int_arr[i] = 0;
|
||||||
for (uint32_t j = 0; j < 8; j++) {
|
for (uint32_t j = 0; j < ASCII_BITS; j++) {
|
||||||
if (*tmp_string == '1')
|
if (*tmp_string == '1')
|
||||||
int_arr[i] = int_arr[i] * 2 + 1;
|
int_arr[i] = int_arr[i] * 2 + 1;
|
||||||
else if (*tmp_string == '0')
|
else if (*tmp_string == '0')
|
||||||
@ -94,7 +94,7 @@ static char *get_bin_arr_to_ascii(char *binary_rep)
|
|||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
int_string = ntru_calloc(1, sizeof(char) * (i + 1));
|
int_string = ntru_calloc(1, CHAR_SIZE * (i + 1));
|
||||||
|
|
||||||
for (uint32_t j = 0; j < i; j++)
|
for (uint32_t j = 0; j < i; j++)
|
||||||
int_string[j] = (char) int_arr[j];
|
int_string[j] = (char) int_arr[j];
|
||||||
@ -114,7 +114,7 @@ fmpz_poly_t **ascii_to_poly(char *to_poly, ntru_context *ctx)
|
|||||||
uint32_t i = 0,
|
uint32_t i = 0,
|
||||||
polyc = 0;
|
polyc = 0;
|
||||||
char *cur = to_poly;
|
char *cur = to_poly;
|
||||||
size_t out_size = sizeof(char) * (strlen(to_poly) * 8 + 1);
|
size_t out_size = CHAR_SIZE * (strlen(to_poly) * 8 + 1);
|
||||||
char *out = ntru_malloc(out_size);
|
char *out = ntru_malloc(out_size);
|
||||||
fmpz_poly_t **poly_array;
|
fmpz_poly_t **poly_array;
|
||||||
|
|
||||||
@ -171,9 +171,9 @@ char *poly_to_ascii(fmpz_poly_t **poly_array, ntru_context *ctx)
|
|||||||
/*
|
/*
|
||||||
* parse the polynomial coefficients into a string
|
* parse the polynomial coefficients into a string
|
||||||
*/
|
*/
|
||||||
binary_rep = ntru_malloc(sizeof(char) * (ctx->N + 1));
|
binary_rep = ntru_malloc(CHAR_SIZE * (ctx->N + 1));
|
||||||
while ((ascii_poly = *poly_array++)) {
|
while ((ascii_poly = *poly_array++)) {
|
||||||
new_length = sizeof(char) * (ctx->N + 1);
|
new_length = CHAR_SIZE * (ctx->N + 1);
|
||||||
|
|
||||||
REALLOC(binary_rep,
|
REALLOC(binary_rep,
|
||||||
old_length +
|
old_length +
|
||||||
|
38
src/common.h
Normal file
38
src/common.h
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* 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 common.h
|
||||||
|
* This file holds common macros and functions
|
||||||
|
* shared throughout the whole codebase without
|
||||||
|
* any particular purpose.
|
||||||
|
* @brief common macros/functions
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef NTRU_COMMON_H
|
||||||
|
#define NTRU_COMMON_H
|
||||||
|
|
||||||
|
|
||||||
|
#define CHAR_SIZE sizeof(char)
|
||||||
|
#define ASCII_BITS 8
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* NTRU_COMMON_H */
|
Loading…
Reference in New Issue
Block a user