Browse Source

added store and recover for sk

Gustavo Banegas 2 years ago
parent
commit
9c4be93d2a
2 changed files with 18 additions and 4 deletions
  1. 3 1
      include/util/util.h
  2. 15 3
      src/util/util.c

+ 3 - 1
include/util/util.h

@@ -25,7 +25,7 @@
 
 extern void store(matrix *src, unsigned char *dst);
 
-extern void store_u_y(gf *v, gf *y, unsigned char *sk);
+extern void store_u_y(const gf *v,const gf *y, unsigned char *sk);
 
 extern void random_m(unsigned char *m);
 
@@ -37,6 +37,8 @@ void recover_G(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 int compute_weight(unsigned char *r, int size);
 
 extern void permute(gf *array, int i, int length);

+ 15 - 3
src/util/util.c

@@ -26,7 +26,7 @@ void store(matrix *src, unsigned char *dst) {
 	 printf("\n");*/
 }
 
-void store_u_y(gf *v, gf *y, unsigned char *sk) {
+void store_u_y(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;
@@ -34,8 +34,8 @@ void store_u_y(gf *v, gf *y, unsigned char *sk) {
 		sk[i + 1] = b;
 	}
 	for (int i = 2 * code_length; i < 4 * code_length; i = i + 2) {
-		gf a = y[i / 4] >> 8;
-		gf b = y[i / 4] & 0xFF;
+		gf a = y[(i / 2) - code_length] >> 8;
+		gf b = y[(i / 2) - code_length] & 0xFF;
 		sk[i] = a;
 		sk[i + 1] = b;
 	}
@@ -75,6 +75,18 @@ void store_u_y(gf *v, gf *y, unsigned char *sk) {
 
 }
 
+void recover_sk(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);
+	}
+	for (int i = 2 * code_length; i < 4 * code_length; i = i + 2) {
+		y[(i / 2) - code_length] = sk[i + 1] | (sk[i] << 8);
+
+	}
+
+}
+
 void recover_G(const unsigned char *public_key, matrix *G) {
 	gf z[code_dimension] = { 0 };
 	for (int i = 0; i < code_dimension; i++) {