POLY->ASCII: Added a function to get a string out of a given polynom.

This commit is contained in:
malte 2014-05-18 18:08:36 +02:00
parent d498ddf1a8
commit 3bf66ce274
2 changed files with 63 additions and 19 deletions

View File

@ -19,18 +19,21 @@
* MA 02110-1301 USA * MA 02110-1301 USA
*/ */
#include "ascii_poly.h"
#include "context.h"
#include "err.h"
#include "mem.h" #include "mem.h"
#include "poly.h" #include "poly.h"
#include "ascii_poly.h"
#include <math.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include <tompoly.h>
#include <tommath.h> #include <tommath.h>
#include <tompoly.h>
#define ASCII_DIGIETS 7 #define ASCII_DIGITS 7
/** /**
* Converts a string into a pb_poly of the size strlen(to_poly) * 7. * Converts a string into a pb_poly of the size strlen(to_poly) * 7.
@ -41,34 +44,74 @@
*/ */
pb_poly *ascii_to_poly(char *to_poly) pb_poly *ascii_to_poly(char *to_poly)
{ {
size_t length = (strlen(to_poly) * ASCII_DIGIETS); size_t length = (strlen(to_poly) * ASCII_DIGITS);
char *tmp_ptr = to_poly; char *tmp_ptr = to_poly;
u_int8_t quotient, u_int8_t binary_Number[ASCII_DIGITS + 1];
i,
k,
binary_Number[ASCII_DIGIETS + 1];
mp_int *chara = ntru_malloc(sizeof(mp_int)); if (!to_poly) {
init_integer(chara); return NULL;
}
mp_int chara;
init_integer(&chara);
pb_poly *poly = ntru_malloc(sizeof(pb_poly)); pb_poly *poly = ntru_malloc(sizeof(pb_poly));
init_polynom_size(poly, chara, length); init_polynom_size(poly, &chara, length);
/* for every char */
for (u_int32_t j = 0; j < strlen(to_poly); j++) { for (u_int32_t j = 0; j < strlen(to_poly); j++) {
quotient = (u_int8_t) *tmp_ptr++; u_int8_t quotient = (u_int8_t) *tmp_ptr++;
k = ASCII_DIGIETS; u_int8_t k = ASCII_DIGITS;
for (i = 1; i <= ASCII_DIGIETS; i++) { for (u_int8_t i = 1; i <= ASCII_DIGITS; i++) {
/* gets the least significant bit in an array*/
binary_Number[k--] = quotient % 2; binary_Number[k--] = quotient % 2;
/* bitshift so the next bit becomes the lsb*/
quotient >>= 1; quotient >>= 1;
} }
for (i = 1; i <= ASCII_DIGIETS; i++) { for (u_int8_t i = 1; i <= ASCII_DIGITS; i++) {
mp_set(&(poly->terms[((i - 1) + (j * ASCII_DIGIETS))]), /* the actual position of the bit in the polynom */
binary_Number[i]); u_int32_t coefficient = (i - 1) + (j * ASCII_DIGITS);
MP_SET(&(poly->terms[coefficient]), binary_Number[i]);
/* set the array to 0 so the next run is garbage free */
binary_Number[i] = 0; binary_Number[i] = 0;
poly->terms[i].sign = 0; poly->terms[i].sign = 0;
} }
} }
poly->used = (int) length; poly->used = (int) length;
mp_clear(chara); mp_clear(&chara);
return poly; return poly;
} }
/**
* Converts a polynom into a newly allocated string.
*
* @param to_ascii the polynom you want to make a string of.
* @return a pointer to the string ore a NULL pointer in the error case
*/
char *polynom_to_ascii(pb_poly *to_ascii)
{
if (!to_ascii) {
return NULL;
}
size_t length_poly = (size_t) to_ascii->used;
size_t length_string = (size_t) (length_poly / ASCII_DIGITS);
char *string = (char*) ntru_malloc(length_string);
char bit_buffer;
char *tmp_ptr = string;
u_int8_t ascii_value = 0;
for (u_int32_t i = 0; i < length_poly; i += ASCII_DIGITS) {
for (u_int32_t j = 0; j < ASCII_DIGITS; j++) {
if (mp_toradix(&(to_ascii->terms[i + j]), &bit_buffer, 2)) {
return NULL;
}
u_int8_t bit = atoi(&bit_buffer);
ascii_value <<= 1;
ascii_value |= bit;
}
*tmp_ptr++ = ascii_value;
ascii_value = 0;
}
return string;
}

View File

@ -23,5 +23,6 @@
#define ASCII_POLY_H_ #define ASCII_POLY_H_
pb_poly *ascii_to_poly(char *to_poly); pb_poly *ascii_to_poly(char *to_poly);
char *polynom_to_ascii(pb_poly *to_ascii);
#endif /* ASCII_POLY_H_ */ #endif /* ASCII_POLY_H_ */