Browse Source

Merge of Pullrequest #17

Gustavo Banegas 2 years ago
parent
commit
1eb121b2be

+ 4 - 5
include/parameters/param.h

@@ -2,7 +2,6 @@
  * param.h
  *
  *  Created on: May 3, 2018
- *      Author: vader
  */
 
 #ifndef SRC_PARAMETERS_PARAM_H_
@@ -43,7 +42,7 @@
 #define weight 176
 #define ss_length 64
 #define k_sec (code_dimension - k_prime)
-#define n0 (code_length/signature_block_size)
+#define n0 (code_length/signature_block_size) // 50
 #endif
 
 #ifdef DAGS_3
@@ -59,7 +58,7 @@
 #define weight 176
 #define ss_length 64
 #define k_sec (code_dimension - k_prime)
-#define n0 (code_length/signature_block_size)
+#define n0 (code_length/signature_block_size) // 38
 #endif
 
 #ifdef DAGS_1
@@ -72,7 +71,7 @@
 #define weight 104
 #define ss_length 16
 #define k_sec (code_dimension - k_prime)
-#define n0 (code_length/signature_block_size)
+#define n0 (code_length/signature_block_size) // 52
 #endif
 
 #ifdef DAGS_TOY
@@ -88,7 +87,7 @@
 #define weight 176
 #define ss_length 64
 #define k_sec (code_dimension - k_prime)
-#define n0 (code_length/signature_block_size)
+#define n0 (code_length/signature_block_size) // 38
 #endif
 
 

+ 2 - 4
include/structures/matrix_operations.h

@@ -13,8 +13,6 @@
 #include <string.h>
 #include <assert.h>
 
-#include "mxm.h"
-
 #include "../definitions.h"
 #include "../gf/gf.h"
 
@@ -26,7 +24,7 @@ extern void free_matrix(matrix* m);
 
 extern void print_matrix(matrix* m);
 
-extern matrix* matrix_multiply(const matrix* restrict a, const  matrix* restrict b);
+void matrix_multiply(matrix* restrict H, const matrix* restrict H_cauchy, const gf z[n0]);
 
 extern matrix* submatrix(const matrix* m, const int star_row, const int end_row, const int start_column, const int end_column);
 
@@ -34,7 +32,7 @@ extern matrix* augment(const matrix *a, const matrix *b);
 
 extern void echelon_form(matrix *a);
 
-extern matrix* transpose_matrix(matrix *a);
+extern matrix* transpose_matrix(const matrix* restrict a);
 
 extern void multiply_vector_matrix(unsigned char* restrict u, matrix *G, gf  *c);
 

+ 0 - 13
include/structures/mxm.h

@@ -1,13 +0,0 @@
-#ifndef MXM_H
-
-#define	MXM_H
-
-#include <stdio.h>
-#include <stdlib.h>
-
-#include "../gf/gf.h"
-
-void mxm_product(gf* restrict dest,const gf* restrict a,const gf*  restrict b, const int m, const int n, const int p);
-
-#endif  /* MXM_H */
-

+ 3 - 3
makefile

@@ -9,12 +9,12 @@ CC=gcc
 #CC=clang
 #Select the desired dags you would like to build
 #DAGSVER=-DDAGS_1
-DAGSVER=-DDAGS_3
-#DAGSVER=-DDAGS_5
+#DAGSVER=-DDAGS_3
+DAGSVER=-DDAGS_5
 
 #clang CFLAGS
 #CFLAGS=-O3 -g3 -Wall -ffunction-sections -fdata-sections -march=native -DDEBUG
-CFLAGS=-O3 -s -Wall -march=native -mtune=native -fomit-frame-pointer  -ffunction-sections -fdata-sections  -fmerge-all-constants -fno-unwind-tables -fno-asynchronous-unwind-tables  -ffast-math 
+CFLAGS=-O3 -g3 -Wall -march=native -mtune=native -fomit-frame-pointer -ffast-math 
 #CFLAGS=-O3 -g3 -Wall -DDEBUG
 #CFLAGS=-O3 -g3 -Wall
 LIBS=-lcrypto -lm -lsodium

+ 99 - 107
src/keygeneration.c

@@ -11,6 +11,7 @@
 static int build_dyadic_signature_part_1(gf *signature_h);
 static int build_dyadic_signature_part2(gf *signature_h, int * block_position);
 
+
 /*
  * contains_zero:
  * 	Check to see if the array does not contain zeros
@@ -53,7 +54,7 @@ int remove_integer(int element, int *list, int size) {
  * 	EXIT_SUCCESS if the element is not present
  * 	EXIT_FAILURE if the element is present
  */
-int vector_contains(gf *signature_h, gf random_e, int length) {
+int vector_contains(const gf *signature_h, const gf random_e, int length) {
 	int i;
 	for (i = 0; i < length; i++) {
 		if (signature_h[i] == random_e)
@@ -62,6 +63,7 @@ int vector_contains(gf *signature_h, gf random_e, int length) {
 	return EXIT_SUCCESS;
 }
 
+
 /*
  * build_dyadic_signature function
  *   This function is used to create the dyadic matrix required for the key_gen.
@@ -80,13 +82,14 @@ int build_dyadic_signature(gf *dyadic_signature) {
 	gf signature_h[F_q_m_size] = { 0 };
 	int i, aux_count_transfer_block = 0;
 
-	if (EXIT_SUCCESS != build_dyadic_signature_part_1(signature_h)) {
+	if (EXIT_SUCCESS != build_dyadic_signature_part_1(signature_h))
+	{
 		PRINT_DEBUG("build_dyadic_signature_part_1 failed\n");
 		return EXIT_FAILURE;
 	}
 
-	if (EXIT_SUCCESS
-			!= build_dyadic_signature_part2(signature_h, block_position)) {
+	if(EXIT_SUCCESS != build_dyadic_signature_part2(signature_h, block_position))
+	{
 		PRINT_DEBUG("build_dyadic_signature_part_2 failed\n");
 		return EXIT_FAILURE;
 	}
@@ -101,7 +104,8 @@ int build_dyadic_signature(gf *dyadic_signature) {
 	return EXIT_SUCCESS;
 }
 
-static int build_dyadic_signature_part_1(gf *signature_h) {
+static int build_dyadic_signature_part_1(gf *signature_h)
+{
 	gf h0 = 0, h0_inverse, i_inverse;
 	int t, i, j; //for loop variables
 	gf random_e, temp, temp_inv;
@@ -142,22 +146,26 @@ static int build_dyadic_signature_part_1(gf *signature_h) {
 			}
 		}
 	}
-	if (EXIT_SUCCESS == contains_zero(signature_h, signature_block_size)) {
+	if (EXIT_SUCCESS == contains_zero(signature_h, signature_block_size))
+	{
 		return EXIT_SUCCESS;
-	} else {
+	}
+	else
+	{
 		print_vector(signature_h, signature_block_size);
 		return EXIT_FAILURE;
 	}
 }
 
-int build_dyadic_signature_part2(gf *signature_h, int * block_position) {
+int build_dyadic_signature_part2(gf *signature_h, int * block_position)
+{
 	int i, j; //for loop variables
 	int ll = 0;
 	int size_part = (F_q_m_size / signature_block_size) - 1;
 	int part[(F_q_m_size / signature_block_size) - 1] = { 0 };
 	int count_part = 0;
 	gf aux_list[signature_block_size] = { 0 };
-	gf temp_list[signature_block_size]; // = { 0 };
+	gf temp_list[signature_block_size];// = { 0 };
 
 	memcpy(temp_list, signature_h, signature_block_size * sizeof(gf));
 
@@ -166,32 +174,33 @@ int build_dyadic_signature_part2(gf *signature_h, int * block_position) {
 	}
 
 	//Check to see if temp_list does not contain zeros
-	block_position[0] = ll;
-	ll++;
-	while (ll * signature_block_size != code_length) {
-		j = 0;
-		do {
-			j = part[count_part];
-			count_part++;
-		} while (j == 0 && count_part < size_part);
-
-		if (count_part >= size_part) {
-			PRINT_DEBUG("Invalid index into part array\n");
-			return EXIT_FAILURE;
-		}
+		block_position[0] = ll;
+		ll++;
+		while (ll * signature_block_size != code_length) {
+			j = 0;
+			do {
+				j = part[count_part];
+				count_part++;
+			} while (j == 0 && count_part < size_part);
+
+			if (count_part >= size_part)
+			{
+				PRINT_DEBUG("Invalid index into part array\n");
+				return EXIT_FAILURE;
+			}
 
-		if (EXIT_FAILURE == remove_integer(j, part, size_part)) {
-			PRINT_DEBUG("Failed to remove int likely already occurred\n");
-		}
+			if (EXIT_FAILURE == remove_integer(j, part, size_part))
+			{
+				PRINT_DEBUG("Failed to remove int likely already occurred\n");
+			}
 
-		memcpy(aux_list, &signature_h[(j * signature_block_size)],
-				signature_block_size * sizeof(gf));
+			memcpy(aux_list, &signature_h[(j * signature_block_size)], signature_block_size * sizeof(gf));
 
-		if (EXIT_SUCCESS == contains_zero(aux_list, signature_block_size)) {
-			block_position[ll] = j;
-			ll++;
+			if (EXIT_SUCCESS == contains_zero(aux_list, signature_block_size)) {
+				block_position[ll] = j;
+				ll++;
+			}
 		}
-	}
 	return EXIT_SUCCESS;
 }
 
@@ -240,6 +249,7 @@ int is_vector_disjoint(gf *list, int size) { //TODO consider wrapping these test
 	return EXIT_SUCCESS;
 }
 
+
 /*
  * For loop through both arrays and if remove elements from the to_remove array
  * exists in they elements array then remove it.
@@ -285,17 +295,20 @@ void build_support(gf *signature_h, gf *u, gf *v, gf *elements) {
 	}
 	remove_elements(elements_in_F_q_m, aux, code_length);
 
+
 	do {
 		omega = elements_in_F_q_m[randombytes_uniform(code_length - 1)];
 	} while (omega == 0);
 
 	for (i = 0; i < code_length; i++) {
 		if (signature_h[i] != 0) {
-			if (i < signature_block_size) {
+			if (i < signature_block_size){
 				u[i] = signature_h_inv[i] ^ omega;
 				v[i] = u[i] ^ h0_inv;
-			} else {
-				v[i] = signature_h_inv[i] ^ h0_inv ^ omega;
+			}
+			else
+			{
+				v[i] =  signature_h_inv[i] ^ h0_inv ^ omega;
 			}
 		}
 	}
@@ -341,12 +354,13 @@ void build_cauchy_matrix(gf *u, gf *v, matrix *H_cauchy) {
  * 	EXIT_SUCCES if trapdoor is build otherwise EXIT_FAILURE
  */
 int build_trapdoor(const matrix* restrict H_cauchy, const gf* restrict v,
-		const gf* restrict u, gf* restrict y, matrix* restrict H) {
-	gf z[code_length] = { 0 };
+		const gf* restrict u,  gf* restrict y,	matrix* restrict H) {
 	gf random_el = 0;
 	gf pol, sum_u_v, result;
 	int i, j, ret_val = EXIT_FAILURE;
-	matrix *diagonal_z_matrix = NULL, *H3 = NULL;
+
+	gf z_short[n0] = {0};
+	gf z[code_length] = {0};
 
 	PRINT_DEBUG("build_trapdoor start\n");
 
@@ -357,23 +371,15 @@ int build_trapdoor(const matrix* restrict H_cauchy, const gf* restrict v,
 			// only need to check every signature_block_size element to see if it matches
 		} while (random_el == 0 || vector_contains(z, random_el, code_length));
 
-		// This cannot be shortened with a memset because gf not full byte size;
 		for (j = 0; j < signature_block_size; j++) {
 			z[(i * signature_block_size) + j] = random_el;
 		}
+		z_short[i] = random_el;
 	}
 
-	if (NULL
-			== (diagonal_z_matrix = diagonal_matrix(z, code_length, code_length))) {
-		PRINT_DEBUG("Failed to create diagonal_matrix\n");
-		goto failout;
-	}
 	PRINT_DEBUG("Critical section\n");
-	H3 = matrix_multiply(H_cauchy, diagonal_z_matrix);
+	matrix_multiply(H, H_cauchy, z_short);
 	PRINT_DEBUG("DONE Critical section\n");
-	memcpy(H->data, H3->data, sizeof(gf) * H3->rows * H3->cols);
-	PRINT_DEBUG("H3 row cols = %d %d \n", H3->rows, H3->cols);
-	PRINT_DEBUG("free 1 \n");
 
 	for (i = 0; i < code_length; i++) {
 		pol = 1;
@@ -385,13 +391,11 @@ int build_trapdoor(const matrix* restrict H_cauchy, const gf* restrict v,
 		y[i] = gf_div_f_q_m(z[i], pol);
 	}
 	ret_val = EXIT_SUCCESS;
-	failout: free_matrix(H3);
-	PRINT_DEBUG("free 2 \n");
-	free_matrix(diagonal_z_matrix);
+
 	return ret_val;
 }
 
-void project_H_on_F_q(const matrix *H, matrix *Hbase) {
+void project_H_on_F_q(const matrix* restrict H, matrix* restrict Hbase) {
 	int k, i, j;
 	int nr_rows = H->rows;
 	int nr_cols = H->cols;
@@ -400,8 +404,7 @@ void project_H_on_F_q(const matrix *H, matrix *Hbase) {
 		for (i = 0; i < nr_rows; i++) {
 			for (j = 0; j < nr_cols; j++) {
 				Hbase->data[(k * nr_rows + i) * nr_cols + j] =
-						relative_field_representation(H->data[i * nr_cols + j],
-								k);
+						relative_field_representation(H->data[i * nr_cols + j], k);
 			}
 		}
 	}
@@ -426,12 +429,9 @@ int generate_systematic_matrix(const matrix* Hbase) {
 				if (Hbase->data[l * num_cols + j]) {
 					//printf("Find Pivot\n");
 					for (mm = 0; mm < num_cols; mm++) {
-						Hbase->data[(l * num_cols) + mm] ^= Hbase->data[(i
-								* num_cols) + mm];
-						Hbase->data[(i * num_cols) + mm] ^= Hbase->data[(l
-								* num_cols) + mm];
-						Hbase->data[(l * num_cols) + mm] ^= Hbase->data[(i
-								* num_cols) + mm];
+						Hbase->data[(l * num_cols) + mm] ^= Hbase->data[(i * num_cols) + mm];
+						Hbase->data[(i * num_cols) + mm] ^= Hbase->data[(l * num_cols) + mm];
+						Hbase->data[(l * num_cols) + mm] ^= Hbase->data[(i * num_cols) + mm];
 					}
 					break;
 				}
@@ -448,8 +448,10 @@ int generate_systematic_matrix(const matrix* Hbase) {
 		// 	//P[i+n-num_rows]=P[j];
 		// 	//P[j]=temp;
 
+
 		// }
 
+
 		//   Matrix standardization
 		if (Hbase->data[(i * num_cols) + j] != 1) {
 			invPiv = gf_inv(Hbase->data[(i * num_cols) + j]);
@@ -463,50 +465,34 @@ int generate_systematic_matrix(const matrix* Hbase) {
 			// 	}
 			// }
 			for (mm = 0; mm < j; mm++) {
-				Hbase->data[(i * num_cols) + mm] = gf_mult(
-						Hbase->data[(i * num_cols) + mm], invPiv);
+				Hbase->data[(i * num_cols) + mm] = gf_mult(Hbase->data[(i * num_cols) + mm], invPiv);
 			}
-			for (mm = j + 1; mm < num_cols; mm++) {
-				Hbase->data[(i * num_cols) + mm] = gf_mult(
-						Hbase->data[(i * num_cols) + mm], invPiv);
+			for (mm = j+1; mm < num_cols; mm++) {
+				Hbase->data[(i * num_cols) + mm] = gf_mult(Hbase->data[(i * num_cols) + mm], invPiv);
 			}
 
 		}
 
-		//Here we do the elimination on column i + num_cols-num_rows
-		// TODO: BLOCKING LOOP
-
-		// for (l = 0; l < num_rows; l++) {
-		// 	if (l != i) {				
-		// 		if (Hbase->data[(l * num_cols) + j]) {
-		// 			piv_align = Hbase->data[(l * num_cols) + j];
-		// 			for (mm = 0; mm < num_cols; mm++) {
-		// 				Hbase->data[(l * num_cols) + mm] ^= gf_mult(piv_align,Hbase->data[(i * num_cols) + mm]);
-		// 			}
-		// 		}
-		// 	}
-		// }
 		for (l = 0; l < i; l++) {
 			if (Hbase->data[(l * num_cols) + j]) {
 				piv_align = Hbase->data[(l * num_cols) + j];
 				for (mm = 0; mm < num_cols; mm++) {
-					Hbase->data[(l * num_cols) + mm] ^= gf_mult(piv_align,
-							Hbase->data[(i * num_cols) + mm]);
+					Hbase->data[(l * num_cols) + mm] ^= gf_mult(piv_align,Hbase->data[(i * num_cols) + mm]);
 				}
 			}
 		}
-		for (l = i + 1; l < num_rows; l++) {
+		for (l = i+1; l < num_rows; l++) {
 			if (Hbase->data[(l * num_cols) + j]) {
 				piv_align = Hbase->data[(l * num_cols) + j];
 				for (mm = 0; mm < num_cols; mm++) {
-					Hbase->data[(l * num_cols) + mm] ^= gf_mult(piv_align,
-							Hbase->data[(i * num_cols) + mm]);
+					Hbase->data[(l * num_cols) + mm] ^= gf_mult(piv_align,Hbase->data[(i * num_cols) + mm]);
 				}
 			}
 		}
 	}
 	return 0;
 
+
 }
 
 /*
@@ -517,7 +503,7 @@ int generate_public_key(const matrix *Hbase, matrix *G) {
 	int num_cols, num_rows, i;
 	matrix *M, *m_temp, *m_transposed, *final;
 
-	if (EXIT_FAILURE == generate_systematic_matrix(Hbase)) {
+	if(EXIT_FAILURE == generate_systematic_matrix(Hbase)){
 		PRINT_DEBUG("Failed to generate_systematic_matrix\n");
 		return EXIT_FAILURE;
 	}
@@ -525,16 +511,19 @@ int generate_public_key(const matrix *Hbase, matrix *G) {
 	num_cols = Hbase->cols;
 	num_rows = Hbase->rows;
 
+
 	M = submatrix(Hbase, 0, 0, num_rows, (num_cols - num_rows));
 
 	m_transposed = transpose_matrix(M);
 
 	free_matrix(M);
 
+
 	m_temp = make_matrix((num_cols - num_rows), (num_cols - num_rows));
 	for (i = 0; i < (num_cols - num_rows); i++)
 		m_temp->data[i * (num_cols - num_rows) + i] = 1;
 
+
 	// TODO augment is only used here. We could save memory usages by passing in
 	// G to this function
 	final = augment(m_temp, m_transposed);
@@ -565,14 +554,16 @@ int key_pair_generation(unsigned char *pk, unsigned char *sk) {
 	return ret;
 }
 
+
+
 int key_gen(gf *v, gf *y, matrix *G) {
 	int ret_value = 0;
-	gf signature_h[code_length];
+	gf signature_h[code_length] = {0};
+	gf u[signature_block_size] = {0};
 	gf *elements_in_F_q_m = NULL;
 	gf *elements_in_F_q_m_constant = NULL;
-	gf u[signature_block_size];
 	long build_support_failures = 0;
-	long build_dyadic_sig_failures = 0;
+	long build_dyadic_sig_failures =0;
 
 	matrix H_cauchy;
 	gf data_cauchy[signature_block_size * pol_deg * code_length] = { 0 };
@@ -585,18 +576,20 @@ int key_gen(gf *v, gf *y, matrix *G) {
 	H.rows = signature_block_size * pol_deg;
 	H.cols = code_length;
 	H.data = data_H;
-	PRINT_DEBUG("H row col = %d %d\n", H.rows, H.cols);
+	// PRINT_DEBUG("H row col = %d %d\n", H.rows, H.cols);
 
 	matrix Hbase;
 	Hbase.rows = (signature_block_size * pol_deg) * extension;
 	Hbase.cols = code_length;
-	gf data_Hbase[signature_block_size * pol_deg * code_length * extension] = {
-			0 };
+	gf data_Hbase[signature_block_size * pol_deg * code_length * extension] =
+			{ 0 };
 	Hbase.data = data_Hbase;
 
+
 	PRINT_DEBUG("Key Gen start\n");
 
-	if (NULL == (elements_in_F_q_m_constant = calloc(F_q_m_size, sizeof(gf)))) {
+	if (NULL == (elements_in_F_q_m_constant = calloc(F_q_m_size, sizeof(gf))))
+	{
 		PRINT_DEBUG("Failed to allocate memory for elements_in_F_q_m");
 		goto failout;
 	}
@@ -605,30 +598,28 @@ int key_gen(gf *v, gf *y, matrix *G) {
 	generate_elements_in_order(elements_in_F_q_m_constant, 1, F_q_m_size);
 
 	do {
-		if (NULL == (elements_in_F_q_m = calloc(F_q_m_size, sizeof(gf)))) {
+		if (NULL == (elements_in_F_q_m = calloc(F_q_m_size, sizeof(gf))))
+		{
 			PRINT_DEBUG("Failed to allocate memory for elements_in_F_q_m");
 			goto failout;
 		}
-		memcpy(elements_in_F_q_m, elements_in_F_q_m_constant,
-		F_q_m_size * sizeof(gf));
-		memset(signature_h, 0, code_length * sizeof(gf));
-		memset(u, 0, signature_block_size * sizeof(gf));
+		memcpy(elements_in_F_q_m, elements_in_F_q_m_constant, F_q_m_size * sizeof(gf));
 		do {
-			build_support_failures++;
+			build_support_failures ++;
 			ret_value = build_dyadic_signature(signature_h);
-			if (ret_value == EXIT_SUCCESS) {
+			if (ret_value == EXIT_SUCCESS){
 				build_support(signature_h, u, v, elements_in_F_q_m);
-			} else {
-				build_dyadic_sig_failures++;
+			}
+			else
+			{
+				build_dyadic_sig_failures ++;
 				build_support_failures--;
 			}
-			if (build_support_failures % 100 == 0) {
-				PRINT_DEBUG("interations %ld vs %ld\n", build_support_failures,
-						build_dyadic_sig_failures);
+			if (build_support_failures %100 == 0){
+				PRINT_DEBUG("interations %ld vs %ld\n",build_support_failures, build_dyadic_sig_failures);
 			}
 
-		} while (ret_value != EXIT_SUCCESS || is_vectors_disjoint(u, v)
-				|| is_vector_disjoint(v, code_length)
+		} while (ret_value != EXIT_SUCCESS || is_vectors_disjoint(u, v) || is_vector_disjoint(v, code_length)
 				|| is_vector_disjoint(u, signature_block_size));
 
 		free(elements_in_F_q_m);
@@ -636,8 +627,7 @@ int key_gen(gf *v, gf *y, matrix *G) {
 
 		build_cauchy_matrix(u, v, &H_cauchy);
 
-		if (EXIT_FAILURE
-				== (ret_value = build_trapdoor(&H_cauchy, v, u, y, &H))) {
+		if (EXIT_FAILURE == (ret_value = build_trapdoor(&H_cauchy, v, u, y, &H))){
 			PRINT_DEBUG("Failed to build_trapdoor\n");
 			continue;
 		}
@@ -648,7 +638,9 @@ int key_gen(gf *v, gf *y, matrix *G) {
 		ret_value = generate_public_key(&Hbase, G);
 
 	} while (ret_value);
-	failout: free(elements_in_F_q_m);
+	return ret_value;
+failout:
+	free(elements_in_F_q_m);
 	free(elements_in_F_q_m_constant);
 	return ret_value;
 }

+ 64 - 27
src/structures/matrix_operations.c

@@ -31,41 +31,78 @@ matrix* make_matrix(const int n_rows, const int n_cols) {
 	return m;
 }
 
-matrix* diagonal_matrix(gf* z, const int n_rows, const int n_cols) {
-	int i;
-	matrix *ret_val = NULL;
-	matrix *m;
 
-	if (NULL == (m = (matrix *) malloc(sizeof(matrix)))){
-		PRINT_DEBUG("Failed to allocate memory for diagonal_matrix\n");
-	}
-
-	// set dimensions
-	m->rows = n_rows;
-	m->cols = n_cols;
 
-	// allocate a double array of length rows * cols
-	if (NULL == (m->data = (gf *)calloc(n_rows * n_cols, sizeof(gf)))){
-		PRINT_DEBUG("Failed to allocate memory for diagonal_matrix");
-		goto failout;
+void gf_q_m_mult_block( gf* restrict C,  const  gf* restrict B, const gf A) {
+	gf reduction[signature_block_size];
+	uint32_t tmp[signature_block_size] = {0};
+
+	int i,j;
+
+	//Multiplication
+	for (i = 0; i < 18; i++)	tmp[0] ^= (A * (B[0] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[1] ^= (A * (B[1] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[2] ^= (A * (B[2] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[3] ^= (A * (B[3] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[4] ^= (A * (B[4] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[5] ^= (A * (B[5] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[6] ^= (A * (B[6] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[7] ^= (A * (B[7] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[8] ^= (A * (B[8] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[9] ^= (A * (B[9] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[10] ^= (A * (B[10] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[11] ^= (A * (B[11] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[12] ^= (A * (B[12] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[13] ^= (A * (B[13] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[14] ^= (A * (B[14] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[15] ^= (A * (B[15] & (1 << i)));
+#if defined(DAGS_3) || defined(DAGS_5) || defined(DAGS_TOY)
+	for (i = 0; i < 18; i++)	tmp[16] ^= (A * (B[16] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[17] ^= (A * (B[17] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[18] ^= (A * (B[18] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[19] ^= (A * (B[19] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[20] ^= (A * (B[20] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[21] ^= (A * (B[21] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[22] ^= (A * (B[22] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[23] ^= (A * (B[23] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[24] ^= (A * (B[24] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[25] ^= (A * (B[25] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[26] ^= (A * (B[26] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[27] ^= (A * (B[27] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[28] ^= (A * (B[28] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[29] ^= (A * (B[29] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[30] ^= (A * (B[30] & (1 << i)));
+	for (i = 0; i < 18; i++)	tmp[31] ^= (A * (B[31] & (1 << i)));
+#endif
+
+	for (j = 0; j < signature_block_size; j++){
+		for (i = 0; i < 2; i++){
+            reduction[j] = (tmp[j] >> 16) &0x7fff;
+            tmp[j] = tmp[j] & 0xFFFF;
+            tmp[j] ^= reduction[j];
+            tmp[j] ^= reduction[j] << 2;
+            tmp[j] ^= reduction[j] << 3;
+            tmp[j] ^= reduction[j] << 5;
+        }
 	}
 
-	for (i = 0; i < min(n_rows, n_cols); i++) {
-		m->data[i * n_cols + i] = z[i];
+	for (j = 0; j < signature_block_size; j++){
+		C[j] =  tmp[j];
 	}
-
-	ret_val = m;
-failout:
-	return ret_val;
 }
 
-matrix* matrix_multiply(const matrix* restrict a, const  matrix* restrict b) {
-	matrix *m = make_matrix(a->rows, b->cols);
-	mxm_product(m->data, a->data, b->data, a->rows, a->cols, b->cols);
-	return m;
 
-}
+void matrix_multiply(matrix* restrict H, const matrix* restrict H_cauchy, const gf z[n0]) {
+	int row, col;
+	int tmp_index;
+	for (row = 0; row < H_cauchy->rows; row++){
+		for (col = 0; col < n0; col++){
+			tmp_index = row*code_length + col*signature_block_size;
+			gf_q_m_mult_block(&H->data[tmp_index], &H_cauchy->data[tmp_index], z[col]);
+		}
+	}
 
+}
 /*
  * free_matrix
  * 	Frees the memory that was allocated by make_matrix
@@ -147,7 +184,7 @@ void echelon_form(matrix *a) {
 	}
 }
 
-matrix * transpose_matrix(matrix *a) {
+matrix * transpose_matrix(const matrix* restrict a) {
 
 	int n_a_cols = a->cols;
 	int n_a_rows = a->rows;

+ 0 - 177
src/structures/mxm.c

@@ -1,177 +0,0 @@
-#include "../../include/structures/mxm.h"
-#include "../cpucycles.h"
-
-#define BLK_SIZE 128
-#define min(a,b) (((a)<(b))?(a):(b))
-
-#define bound 32
-
-
-#if defined(DAGS_3) || defined(DAGS_5) || defined(DAGS_TOY)
-void gf_q_m_mult_bound(const gf A, const  gf* restrict B, gf* restrict C) {
-	gf reduction[bound]; 
-	uint32_t tmp[bound] = {0};
-	
-	int i,j; 
-
-	//Multiplication
-	for (i = 0; i < 18; i++)	tmp[0] ^= (A * (B[0] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[1] ^= (A * (B[1] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[2] ^= (A * (B[2] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[3] ^= (A * (B[3] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[4] ^= (A * (B[4] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[5] ^= (A * (B[5] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[6] ^= (A * (B[6] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[7] ^= (A * (B[7] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[8] ^= (A * (B[8] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[9] ^= (A * (B[9] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[10] ^= (A * (B[10] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[11] ^= (A * (B[11] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[12] ^= (A * (B[12] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[13] ^= (A * (B[13] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[14] ^= (A * (B[14] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[15] ^= (A * (B[15] & (1 << i))); 
-	// /*
-	for (i = 0; i < 18; i++)	tmp[16+0]  ^= (A * (B[16+0] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+1]  ^= (A * (B[16+1] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+2]  ^= (A * (B[16+2] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+3]  ^= (A * (B[16+3] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+4]  ^= (A * (B[16+4] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+5]  ^= (A * (B[16+5] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+6]  ^= (A * (B[16+6] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+7]  ^= (A * (B[16+7] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+8]  ^= (A * (B[16+8] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+9]  ^= (A * (B[16+9] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+10] ^= (A * (B[16+10] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+11] ^= (A * (B[16+11] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+12] ^= (A * (B[16+12] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+13] ^= (A * (B[16+13] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+14] ^= (A * (B[16+14] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+15] ^= (A * (B[16+15] & (1 << i))); 
-
-	// */
-	for (j = 0; j < bound; j++){
-		for (i = 0; i < 2; i++){
-            reduction[j] = (tmp[j] >> 16) &0x7fff;
-            tmp[j] = tmp[j] & 0xFFFF;
-            tmp[j] ^= reduction[j];
-            tmp[j] ^= reduction[j] << 2;
-            tmp[j] ^= reduction[j] << 3;
-            tmp[j] ^= reduction[j] << 5;
-        }
-	}
-
-	for (j = 0; j < bound; j++){
-		C[j] ^=  tmp[j];
-	}
-}
-
-#endif
-
-#ifdef DAGS_1
-void gf_q_m_mult_bound(const gf A, const  gf* restrict B, gf* restrict C) {
-	gf reduction[bound]; 
-	uint32_t tmp[bound] = {0};
-	
-	int i,j; 
-	
-	//Multiplication
-	for (i = 0; i < 18; i++)	tmp[0] ^= (A * (B[0] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[1] ^= (A * (B[1] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[2] ^= (A * (B[2] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[3] ^= (A * (B[3] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[4] ^= (A * (B[4] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[5] ^= (A * (B[5] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[6] ^= (A * (B[6] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[7] ^= (A * (B[7] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[8] ^= (A * (B[8] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[9] ^= (A * (B[9] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[10] ^= (A * (B[10] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[11] ^= (A * (B[11] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[12] ^= (A * (B[12] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[13] ^= (A * (B[13] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[14] ^= (A * (B[14] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[15] ^= (A * (B[15] & (1 << i))); 
-	// /*
-	for (i = 0; i < 18; i++)	tmp[16+0]  ^= (A * (B[16+0] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+1]  ^= (A * (B[16+1] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+2]  ^= (A * (B[16+2] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+3]  ^= (A * (B[16+3] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+4]  ^= (A * (B[16+4] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+5]  ^= (A * (B[16+5] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+6]  ^= (A * (B[16+6] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+7]  ^= (A * (B[16+7] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+8]  ^= (A * (B[16+8] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+9]  ^= (A * (B[16+9] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+10] ^= (A * (B[16+10] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+11] ^= (A * (B[16+11] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+12] ^= (A * (B[16+12] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+13] ^= (A * (B[16+13] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+14] ^= (A * (B[16+14] & (1 << i))); 
-	for (i = 0; i < 18; i++)	tmp[16+15] ^= (A * (B[16+15] & (1 << i))); 
-
-	//reduction
-	//first step of reduction
-	//second step of reduction
-
-	for (j = 0; j < bound; j++){
-		for (i = 0; i < 2; i++){
-            reduction[j] = (tmp[j] >> 12) &0x7FF;
-            tmp[j] = tmp[j] & 0xFFF;
-            tmp[j] ^= reduction[j];
-            tmp[j] ^= reduction[j] << 1;
-            tmp[j] ^= reduction[j] << 4;
-            tmp[j] ^= reduction[j] << 6;
-        }
-	}
-	
-	for (j = 0; j < bound; j++){
-		C[j] ^=  (tmp[j] & 0xFFF);
-	}
-}
-
-#endif
-
-
-void mxm_block_reorder_reuse(gf* restrict C, const gf* restrict A, const gf* restrict B, const int m, const int n, const int p) {
-	int i, j, k, ii, jj, kk, Aik, bs = BLK_SIZE;
-
-	for (ii = 0; ii < m; ii += bs)
-		for (kk = 0; kk < n; kk += bs)
-			for (jj = 0; jj < p; jj += bs)
-				for (i = ii; i < min(m, ii + bs); i++)
-					for (k = kk; k < min(n, kk + bs); k++) {
-						Aik = A[n * i + k];
-						for (j = jj; j < min(p, jj + bs); j++)
-							C[p * i + j] ^= gf_q_m_mult(Aik, B[p * k + j]);
-					}
-}
-
-void mxm_block_reorder_reuse_unroll_bound(gf* restrict C, const gf* restrict A, const gf* restrict B, const int m, const int n, const int p) {
-	int i, j, k, ii, jj, kk, Aik, bs = BLK_SIZE;
-
-	for (ii = 0; ii < m; ii += bs)
-		for (kk = 0; kk < n; kk += bs)
-			for (jj = 0; jj < p; jj += bs)
-				for (i = ii; i < min(m, ii + bs); i++)
-					for (k = kk; k < min(n, kk + bs); k++) {
-						Aik = A[n * i + k];
-						for (j = jj; j < min(p, jj + bs); j += bound) {
-							gf_q_m_mult_bound(Aik, &B[p * k + j], &C[p * i + j]);
-							
-						}
-					}
-}
-
-// Cmxp = Amxn * Bnxp
-void mxm_product(gf* restrict dest,const gf* restrict a, const gf* restrict b, const int m, const int n, const int p) {
-	long long start, end; 
-	start = cpucycles();
-	// Option for GCC 
-	mxm_block_reorder_reuse_unroll_bound(dest, a, b, m, n, p);
-	// Option for CLANG
-	// mxm_block_reorder_reuse(dest, a, b, m, n, p);
-	end = cpucycles() - start;
-	PRINT_DEBUG("[++++] timming %lld \n", end/1000000);
-	
-}