Browse Source

changed name of functions and added commentaries

Gustavo Banegas 2 years ago
parent
commit
85e160b039
4 changed files with 57 additions and 94 deletions
  1. 4 4
      include/util/util.h
  2. 0 10
      src/keygeneration.c
  3. 25 34
      src/main.c
  4. 28 46
      src/util/util.c

+ 4 - 4
include/util/util.h

@@ -23,9 +23,9 @@
 #include "../gf/gf.h"
 #include "../api.h"
 
-extern void store(matrix *src, unsigned char *dst);
+extern void store_public_key(matrix *src, unsigned char *dst);
 
-extern void store_v_y(const gf *v,const gf *y, unsigned char *sk);
+extern void store_secret_key(const gf *v,const gf *y, unsigned char *sk);
 
 extern void random_m(unsigned char *m);
 
@@ -33,11 +33,11 @@ extern void random_e(const unsigned char *sigma, unsigned char *error_array);
 
 extern void recover_public_key(const unsigned char *public_key, matrix *G);
 
-void recover_G(const unsigned char *public_key, matrix *G);
+void recover_public_key_from_array(const unsigned char *public_key, matrix *G);
 
 extern void set_vy_from_sk(gf* v, gf * y, const unsigned char * sk);
 
-extern void recover_sk(const unsigned char * sk, gf* v, gf * y);
+extern void recover_secret_key(const unsigned char * sk, gf* v, gf * y);
 
 extern int compute_weight(unsigned char *r, int size);
 

+ 0 - 10
src/keygeneration.c

@@ -560,16 +560,6 @@ int key_pair_generation(unsigned char *pk, unsigned char *sk) {
 	G.data = data_G;
 	key_gen(v, y, &G);
 	store_public_key(&G, pk);
-	printf("[ ");
-	for (int i = 0; i < code_length; i++) {
-		printf(" %" PRIu16 " ,", v[i]);
-	}
-	printf("]\n");
-	printf("[ ");
-	for (int i = 0; i < code_length; i++) {
-		printf(" %" PRIu16 " ,", y[i]);
-	}
-	printf("]\n");
 	store_secret_key(v, y, sk);
 
 	return 0;

+ 25 - 34
src/main.c

@@ -20,6 +20,7 @@
 #include "../include/decapsulation.h"
 #include "../include/gf/gf.h"
 #include "../include/structures/polynomial.h"
+#include "../include/definitions.h"
 
 #include "cpucycles.h"
 
@@ -76,9 +77,9 @@ int main(void) {
 	unsigned char entropy_input[48];
 
 	int count = 0;
-	//unsigned char pk[CRYPTO_PUBLICKEYBYTES];
-	unsigned char ct[CRYPTO_CIPHERTEXTBYTES], ss[CRYPTO_BYTES];//, ss1[CRYPTO_BYTES];
-	//unsigned char *sk;
+	unsigned char pk[CRYPTO_PUBLICKEYBYTES] = { 0 };
+	unsigned char ct[CRYPTO_CIPHERTEXTBYTES], ss[CRYPTO_BYTES]; //, ss1[CRYPTO_BYTES];
+	unsigned char sk[CRYPTO_SECRETKEYBYTES] = { 0 };
 	int byte_count = 48;
 	int counter = 0;
 	int i;
@@ -98,8 +99,7 @@ int main(void) {
 
 	FILE *fp_urandom;
 	fp_urandom = fopen("/dev/urandom", "r");
-	if (0 == fread(&entropy_input, 1, byte_count, fp_urandom))
-	{
+	if (0 == fread(&entropy_input, 1, byte_count, fp_urandom)) {
 		PRINT_DEBUG("Failed to read in entropy input\n");
 		return EXIT_FAILURE;
 	}
@@ -127,8 +127,7 @@ int main(void) {
 
 	//sk = malloc(CRYPTO_SECRETKEYBYTES);
 
-	if(EOF == fscanf(fp_req, "%d", &count))
-	{
+	if (EOF == fscanf(fp_req, "%d", &count)) {
 		PRINT_DEBUG("Failed to fscanf %d:%s\n", errno, strerror(errno));
 		return EXIT_FAILURE;
 	}
@@ -145,44 +144,35 @@ int main(void) {
 	int fails = 0;
 
 	do {
-		gf v[code_length] = { 0 };
-		gf y[code_length] = { 0 };
-		matrix G;
-		G.cols = code_length;
-		G.rows = code_length - ((signature_block_size * pol_deg) * extension);
-		gf data_G[code_length
-				* (code_length
-						- ((signature_block_size * pol_deg) * extension))] = {
-				0 };
-		G.data = data_G;
 		int ret_val = 0;
 		printf("Starting iteration: %d\n", counter);
+
 		long long start = cpucycles();
-		key_gen(v, y, &G);
+		//key_gen(v, y, &G);
+		ret_val = key_pair_generation(pk, sk);
 		//int ret_val = key_pair_generation(pk, sk);
 		long long final = cpucycles();
-		t_gen_avg = t_gen_avg + (final-start);
-		printf("Key pair generation: %lld\n", (final - start)/1000000);
+		t_gen_avg = t_gen_avg + (final - start);
+
 		start = cpucycles();
-		//ret_val = encapsulation(ct, ss, pk);
-		encrypt(ct, ss, &G);
+		ret_val = encapsulation(ct, ss, pk);
+		//encrypt(ct, ss, &G);
 		final = cpucycles();
-		
-		t_enc_avg = t_enc_avg + (final-start);
 
-		printf("Encaps: %lld\n", (final - start)/1000000);
+		t_enc_avg = t_enc_avg + (final - start);
+
+		printf("Encaps: %lld\n", (final - start) / 1000000);
 		if (ret_val != 0) {
 			printf("fail encrypt");
 			return KAT_CRYPTO_FAILURE;
 		}
-
 		start = cpucycles();
-		//ret_val = decapsulation(ct, ss, pk);
-		ret_val = decrypt(ss, ct, v, y);
+		ret_val = decapsulation(ss, ct, sk);
+		//ret_val = decrypt(ss, ct, v_k, y_k);
 		final = cpucycles();
-		t_dec_avg = t_dec_avg + (final-start);
+		t_dec_avg = t_dec_avg + (final - start);
 
-		printf("decaps: %lld\n", (final - start)/1000000);
+		printf("decaps: %lld\n", (final - start) / 1000000);
 		if (ret_val != 0) {
 			printf("FAIL\n");
 			fails++;
@@ -192,14 +182,15 @@ int main(void) {
 			success++;
 		}
 		counter++;
+
 	} while (counter < 100);
 	printf("SUCCESS: %d\n", success);
 	printf("fails: %d\n", fails);
-	printf("gen_avg: %lld\n", (t_gen_avg/counter));
-	printf("enc_avg: %lld\n", (t_enc_avg/counter));
-	printf("dec_avg: %lld\n", (t_dec_avg/counter));
+	printf("gen_avg: %lld\n", (t_gen_avg / counter));
+	printf("enc_avg: %lld\n", (t_enc_avg / counter));
+	printf("dec_avg: %lld\n", (t_dec_avg / counter));
 
-	//free(sk);
+//free(sk);
 	return 0;
 }
 

+ 28 - 46
src/util/util.c

@@ -9,25 +9,29 @@
 
 #define min(a,b) (((a)<(b))?(a):(b))
 
-void store(matrix *src, unsigned char *dst) {
+
+/*
+ * store_public_key:
+ * Store matrix src that is the public key into array.
+ */
+
+void store_public_key(matrix *src, unsigned char *dst) {
 	int counter = 0;
 	for (int i = 0; i < src->rows; i++) {
 		for (int j = code_dimension; j < src->cols; j++) {
 			dst[counter] = src->data[i * src->cols + j];
 			counter++;
-			//printf(" %d, \n", counter);
 		}
 	}
-	printf("counter: %d, \n", counter);
-	//printf(" %d, \n", counter);
-	/*print_matrix(src);
-	 for (int i = 0; i < CRYPTO_PUBLICKEYBYTES; i++) {
-	 printf(" %" PRIu16 ", ", dst[i]);
-	 }
-	 printf("\n");*/
 }
 
-void store_v_y(const gf *v, const gf *y, unsigned char *sk) {
+/*
+ * store_secret_key:
+ * Store two vectors that is the secret key into array.
+ * The vectors are 16bits and the array is 8 bits.
+ */
+
+void store_secret_key(const gf *v, const gf *y, unsigned char *sk) {
 	for (int i = 0; i < 2 * code_length; i = i + 2) {
 		gf a = v[i / 2] >> 8;
 		gf b = v[i / 2] & 0xFF;
@@ -41,42 +45,16 @@ void store_v_y(const gf *v, const gf *y, unsigned char *sk) {
 		sk[i + 1] = b;
 	}
 
-	/*int i, a = 0;
-	 gf c1, c2;
-	 unsigned char c;
-
-	 for (i = 0; i < (code_length / 2); i++) {
-	 c1 = v[2 * i];
-	 c2 = v[2 * i + 1];
-	 c = c1 >> 4;
-	 sk[a] = c;
-	 a += 1;
-	 c1 = c1 & 15;
-	 c = (c1 << 4) ^ (c2 >> 8);
-	 sk[a] = c;
-	 a += 1;
-	 c = c2 & 255;
-	 sk[a] = c;
-	 a += 1;
-	 }
-	 for (i = 0; i < (code_length / 2); i++) {
-	 c1 = y[2 * i];
-	 c2 = y[2 * i + 1];
-	 c = c1 >> 4;
-	 sk[a] = c;
-	 a += 1;
-	 c1 = c1 & 15;
-	 c = (c1 << 4) ^ (c2 >> 8);
-	 sk[a] = c;
-	 a += 1;
-	 c = c2 & 255;
-	 sk[a] = c;
-	 a += 1;
-	 }*/
+
 
 }
 
-void recover_sk(const unsigned char* sk, gf* v, gf* y) {
+/*
+ * recover_sk:
+ * Recover two vectors that is the secret key from array.
+ * The vectors are 16bits and the array is 8 bits.
+ */
+void recover_secret_key(const unsigned char* sk, gf* v, gf* y) {
 
 	for (int i = 0; i < 2 * code_length; i = i + 2) {
 		v[i / 2] = sk[i + 1] | (sk[i] << 8);
@@ -88,7 +66,11 @@ void recover_sk(const unsigned char* sk, gf* v, gf* y) {
 
 }
 
-void recover_G(const unsigned char *public_key, matrix *G) {
+/*
+ * recover_public_key_from_array:
+ * Recover the matrix G (that is the public key) from an array.
+ */
+void recover_public_key_from_array(const unsigned char *public_key, matrix *G) {
 	gf z[code_dimension] = { 0 };
 	for (int i = 0; i < code_dimension; i++) {
 		z[i] = 1;
@@ -112,7 +94,7 @@ void recover_G(const unsigned char *public_key, matrix *G) {
 
 }
 
-void recover_public_key(const unsigned char *public_key, matrix *G) {
+/*void recover_public_key(const unsigned char *public_key, matrix *G) {
 	int a = 0;
 	int i, j, k, p, l, m, q;
 	matrix *M = make_matrix(code_dimension, code_length - code_dimension);
@@ -161,7 +143,7 @@ void recover_public_key(const unsigned char *public_key, matrix *G) {
 		}
 	}
 	free_matrix(M);
-}
+}*/
 
 void generate_int_list_of_size(int *list, int length) {
 	for (int i = 0; i < length; i++) {