Browse Source

TODO: stop inline gf_q_m_mult

Duc Nguyen 2 years ago
parent
commit
b8d55682c7

+ 2 - 2
include/keygeneration.h

@@ -39,8 +39,8 @@ extern void build_support(gf *signature_h, gf *u, gf *v, gf *elements);
 
 extern void build_cauchy_matrix(gf *u, gf *v, matrix *H_cauchy);
 
-extern int build_trapdoor(matrix* restrict H_cauchy, const gf *v, const gf *u,
-		gf *y, matrix *H);
+extern int build_trapdoor(const matrix* restrict H_cauchy, const gf* restrict v,
+		const gf* restrict u,  gf* restrict y,	matrix* restrict H) ;
 
 extern void project_H_on_F_q(const matrix *H, matrix *Hbase);
 

+ 1 - 1
include/structures/matrix_operations.h

@@ -26,7 +26,7 @@ extern void free_matrix(matrix* m);
 
 extern void print_matrix(matrix* m);
 
-extern matrix* matrix_multiply(matrix* restrict a, matrix* restrict b);
+extern matrix* matrix_multiply(const matrix* restrict a, const  matrix* restrict b);
 
 extern matrix* submatrix(const matrix* m, const int star_row, const int end_row, const int start_column, const int end_column);
 

+ 1 - 1
include/structures/mxm.h

@@ -7,7 +7,7 @@
 
 #include "../gf/gf.h"
 
-void mxm_product(gf* restrict dest, gf* restrict a, gf*  restrict b, const int m, const int n, const int p);
+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 */
 

+ 2 - 2
makefile

@@ -5,8 +5,8 @@
 -include ../makefile.init
 
 RM := rm -rf
-CC=gcc
-#CC=clang
+#CC=gcc
+CC=clang
 #Select the desired dags you would like to build
 #DAGSVER=-DDAGS_1
 DAGSVER=-DDAGS_3

+ 63 - 39
src/keygeneration.c

@@ -353,13 +353,13 @@ void build_cauchy_matrix(gf *u, gf *v, matrix *H_cauchy) {
  * Return:
  * 	EXIT_SUCCES if trapdoor is build otherwise EXIT_FAILURE
  */
-int build_trapdoor(matrix* restrict H_cauchy, const gf *v,
-		const gf *u, gf *y,	matrix *H) {
+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 };
 	gf random_el = 0;
 	gf pol, sum_u_v, result;
 	int i, j, ret_val = EXIT_FAILURE;
-	matrix *diagonal_z_matrix = NULL, *H3 = NULL;
+	matrix *diagonal_z_matrix =NULL , *H3 =NULL ;
 
 	PRINT_DEBUG("build_trapdoor start\n");
 
@@ -380,14 +380,13 @@ int build_trapdoor(matrix* restrict H_cauchy, const gf *v,
 		PRINT_DEBUG("Failed to create diagonal_matrix\n");
 		goto failout;
 	}
+	PRINT_DEBUG("Critical section\n");
 	H3 = matrix_multiply(H_cauchy, diagonal_z_matrix);
-
+	PRINT_DEBUG("DONE Critical section\n");
 	memcpy(H->data, H3->data, sizeof(gf) * H3->rows * H3->cols);
-	free_matrix(H3);
-	free_matrix(diagonal_z_matrix);
-	H3 = NULL;
-	diagonal_z_matrix = NULL;
-
+	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;
 		for (j = 0; j < signature_block_size; j++) {
@@ -400,6 +399,7 @@ int build_trapdoor(matrix* restrict H_cauchy, const gf *v,
 	ret_val = EXIT_SUCCESS;
 failout:
 	free_matrix(H3);
+	PRINT_DEBUG("free 2 \n");
 	free_matrix(diagonal_z_matrix);
 	return ret_val;
 }
@@ -426,6 +426,7 @@ int generate_systematic_matrix(const matrix* Hbase) {
 	int num_cols = Hbase->cols;
 	int num_rows = Hbase->rows;
 	gf invPiv = 1;
+	gf piv_align;
 
 	for (i = 0; i < num_rows; i++) {
 		test = 0;
@@ -437,6 +438,11 @@ int generate_systematic_matrix(const matrix* Hbase) {
 			for (l = i + 1; l < num_rows; l++) {
 				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];
+					}
 					break;
 				}
 			}
@@ -445,19 +451,15 @@ int generate_systematic_matrix(const matrix* Hbase) {
 			printf("Non systematic Matrix %d\num_cols", l);
 			return EXIT_FAILURE;
 		}
-		if (test == 1) { // We switches the lines l and i
-			test = 0;
-			//printf("Permut line\n");
-			//temp=P[i+n-num_rows];
-			//P[i+n-num_rows]=P[j];
-			//P[j]=temp;
-
-			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];
-			}
-		}
+		// if (test == 1) { // We switches the lines l and i
+		// 	test = 0;
+		// 	//printf("Permut line\n");
+		// 	//temp=P[i+n-num_rows];
+		// 	//P[i+n-num_rows]=P[j];
+		// 	//P[j]=temp;
+
+			
+		// }
 
 
 		//   Matrix standardization
@@ -465,25 +467,48 @@ int generate_systematic_matrix(const matrix* Hbase) {
 			invPiv = gf_inv(Hbase->data[(i * num_cols) + j]);
 			Hbase->data[(i * num_cols) + j] = 1;
 
-			for (mm = 0; mm < num_cols; mm++) {
-				if (mm != j) {
-					// continue;
-					Hbase->data[(i * num_cols) + mm] = gf_mult(
-							Hbase->data[(i * num_cols) + mm], invPiv);
-				}
+			// for (mm = 0; mm < num_cols; mm++) {
+			// 	if (mm != j) {
+			// 		// continue;
+			// 		Hbase->data[(i * num_cols) + mm] = gf_mult(
+			// 				Hbase->data[(i * num_cols) + mm], invPiv);
+			// 	}
+			// }
+			for (mm = 0; mm < j; 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
-		gf piv_align;
-		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 < 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]);
+				}
+			}
+		}
+		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]);
 				}
 			}
 		}
@@ -541,9 +566,7 @@ int key_pair_generation(unsigned char *pk, unsigned char *sk) {
 	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 };
+	gf data_G[code_length * (code_length - ((signature_block_size * pol_deg) * extension))] = { 0 };
 	G.data = data_G;
 	key_gen(v, y, &G);
 	store(&G, pk);
@@ -574,6 +597,7 @@ void 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);
 
 	matrix Hbase;
 	Hbase.rows = (signature_block_size * pol_deg) * extension;

+ 4 - 9
src/structures/matrix_operations.c

@@ -59,7 +59,7 @@ failout:
 	return ret_val;
 }
 
-matrix* matrix_multiply(matrix* restrict a, matrix* restrict b) {
+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;
@@ -99,19 +99,14 @@ matrix* submatrix(const matrix* m, const int i, const int j, const int nr_row, c
 
 }
 
-matrix* augment(const matrix *a, const matrix *b) {
+matrix* augment(const matrix* restrict a, const matrix* restrict b) {
 	const int n_rows = a->rows;
 	const int n_cols = a->cols + b->cols;
 	matrix *result = make_matrix(n_rows, n_cols);
 
 	for (int i = 0; i < n_rows; i++) {
-		for (int j = 0; j < a->cols; j++) {
-			result->data[result->cols * i + j] = a->data[a->cols * i + j];
-		}
-		for (int j = a->cols; j < n_cols; j++) {
-			result->data[result->cols * i + j] 
-								= b->data[(b->cols * i) + (j - a->cols)];
-		}
+		memcpy(&result->data[result->cols * i], &a->data[a->cols * i], a->cols*sizeof(gf));
+		memcpy(&result->data[result->cols * i + a->cols], &b->data[b->cols * i], (n_cols-a->cols)*sizeof(gf) );
 	}
 	return result;
 }

+ 12 - 15
src/structures/mxm.c

@@ -40,7 +40,7 @@ void mxm_block_reorder(gf *C, const gf *A, const gf *B, int m, int n, int p) {
 
 }
 
-void mxm_block_reorder_reuse(gf* restrict C,  gf* restrict A,  gf* restrict B, const int m, const int n, const int p) {
+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)
@@ -54,8 +54,7 @@ void mxm_block_reorder_reuse(gf* restrict C,  gf* restrict A,  gf* restrict B, c
 					}
 }
 
-void mxm_block_reorder_reuse_unroll_2(gf *C, const gf *A, const gf *B, int m,
-		int n, int p) {
+void mxm_block_reorder_reuse_unroll_2(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)
@@ -71,8 +70,7 @@ void mxm_block_reorder_reuse_unroll_2(gf *C, const gf *A, const gf *B, int m,
 					}
 }
 
-void mxm_block_reorder_reuse_unroll_4(gf *C, const gf *A, const gf *B, int m,
-		int n, int p) {
+void mxm_block_reorder_reuse_unroll_4(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)
@@ -90,8 +88,7 @@ void mxm_block_reorder_reuse_unroll_4(gf *C, const gf *A, const gf *B, int m,
 					}
 }
 
-void mxm_block_reorder_reuse_unroll_8(gf *C, const gf *A, const gf *B, int m,
-		int n, int p) {
+void mxm_block_reorder_reuse_unroll_8(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)
@@ -113,8 +110,7 @@ void mxm_block_reorder_reuse_unroll_8(gf *C, const gf *A, const gf *B, int m,
 					}
 }
 
-void mxm_block_reorder_reuse_unroll_16(gf *C, const gf *A, const gf *B, int m,
-		int n, int p) {
+void mxm_block_reorder_reuse_unroll_16(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)
@@ -124,6 +120,7 @@ void mxm_block_reorder_reuse_unroll_16(gf *C, const gf *A, const gf *B, int m,
 					for (k = kk; k < min(n, kk + bs); k++) {
 						Aik = A[n * i + k];
 						for (j = jj; j < min(p, jj + bs); j += 16) {
+						// PRINT_DEBUG("i j = %d %d %d\n",i, j, 0);
 							C[p * i + j] ^= gf_q_m_mult(Aik , B[p * k + j]);
 							C[p * i + j + 1] ^= gf_q_m_mult(Aik , B[p * k + j + 1]);
 							C[p * i + j + 2] ^= gf_q_m_mult(Aik , B[p * k + j + 2]);
@@ -144,10 +141,11 @@ void mxm_block_reorder_reuse_unroll_16(gf *C, const gf *A, const gf *B, int m,
 					}
 }
 
-void mxm(gf *C, const gf *A, const gf *B, int m, int n, int p, int uf) {
+void mxm(gf* restrict C, const gf* restrict A, const gf* restrict B, const int m, const int n, const int p, const int uf) {
 	//mxm_naive(C, A, B, m, n, p);
 	//mxm_block(C, A, B, m, n, p);
 	//mxm_block_reorder(C, A, B, m, n, p);
+	PRINT_DEBUG("m n p = %d %d %d\n", m, n, p);
 	switch (uf) {
 	case 16:
 		mxm_block_reorder_reuse_unroll_16(C, A, B, m, n, p);
@@ -168,9 +166,9 @@ void mxm(gf *C, const gf *A, const gf *B, int m, int n, int p, int uf) {
 }
 
 // Cmxp = Amxn * Bnxp
-void mxm_product(gf* restrict dest, gf* restrict a, gf*  restrict b, const int m, const int n, const int p) {
-	mxm_block_reorder_reuse(dest, a, b, m, n, p);
-	/*
+void mxm_product(gf* restrict dest,const gf* restrict a, const gf* restrict b, const int m, const int n, const int p) {
+	// mxm_block_reorder_reuse(dest, a, b, m, n, p);
+	
 	int uf;
 
 	if (p % 16 == 0)
@@ -188,7 +186,6 @@ void mxm_product(gf* restrict dest, gf* restrict a, gf*  restrict b, const int m
 	printf("Block size: %d\n", BLK_SIZE);
 	printf("Unrolls: %d\n", uf);
 
-	//mxm_naive(dest, a, b, m, n, p);
 	mxm(dest, a, b, m, n, p, uf);
-	*/
+	
 }