diff --git a/include/field.h b/include/field.h index 2ffd0e0541069ba643fa710e13b6cabdfedb068d..527654316e50511e0fd25ea2e41d77be5886dc9f 100644 --- a/include/field.h +++ b/include/field.h @@ -3,26 +3,33 @@ #include "relic/relic.h" -typedef fp_t zp; +struct zp { + bn_t point; + bn_t modular; +}; -void rand_zp(zp x); +struct zp new_zp(); -void zp_zero(zp x); +struct zp rand_zp(bn_t modular); -void zp_one(zp x); +struct zp zp_zero(bn_t modular); -void zp_copy(zp x_copy, zp x); +struct zp zp_one(bn_t modular); -void zp_from_int(zp x, int x_int); +struct zp zp_copy(struct zp x); -void zp_add(zp r, zp x, zp y); +struct zp zp_from_int(int x, bn_t modular); -void zp_neg(zp nx, zp x); +struct zp zp_add(struct zp x, struct zp y); -void zp_multiply(zp p, zp x, zp y); +struct zp zp_neg(struct zp x); -void zp_inverse(zp xi, zp x); +struct zp zp_mul(struct zp x, struct zp y); -int zp_is_int(zp x, int x_int); +struct zp zp_inv(struct zp x); + +int zp_cmp(struct zp x, struct zp y); + +int zp_cmp_int(struct zp x, int y); #endif //PPANN_FIELD_H \ No newline at end of file diff --git a/include/group.h b/include/group.h index 392fc7613295a542d6c010d9d718792af28939a2..eca51ea3624f1ef91cb38e7c0d096ca386728460 100644 --- a/include/group.h +++ b/include/group.h @@ -1,17 +1,17 @@ -#ifndef PPANN_GROUP_H -#define PPANN_GROUP_H - -#include "field.h" - -typedef g1_t g; -typedef gt_t gt; - -void generator(g x); - -void multiply(g r, g x, zp y); - -void exponentiation(gt r, gt x, zp y); - -void map(g a, g b, gt r); - -#endif //PPANN_GROUP_H +//#ifndef PPANN_GROUP_H +//#define PPANN_GROUP_H +// +//#include "field.h" +// +//typedef g1_t g; +//typedef gt_t gt; +// +//void generator(g x); +// +//void multiply(g r, g x, zp y); +// +//void exponentiation(gt r, gt x, zp y); +// +//void map(g a, g b, gt r); +// +//#endif //PPANN_GROUP_H diff --git a/include/matrix.h b/include/matrix.h index 62d2977b8affaf55dbbd0c346734fb03d283c640..8a6a2ecfbe775ae1aeac61d05befa48385dff4d1 100644 --- a/include/matrix.h +++ b/include/matrix.h @@ -1,25 +1,25 @@ -#ifndef PPANN_MATRIX_H -#define PPANN_MATRIX_H - -#include "field.h" -#include "group.h" - -typedef zp* zp_mat; - -zp_mat matrix_zp_from_int(int *int_mat, int row, int col); - -zp_mat matrix_zp_rand(int row, int col); - -zp_mat matrix_identity(int size); - -int matrix_is_identity(zp_mat x, int size); - -zp_mat matrix_transpose(zp_mat x, int row, int col); - -zp_mat matrix_merge(zp_mat x, zp_mat y, int row, int col_x, int col_y); - -zp_mat matrix_multiply(zp_mat x, zp_mat y, int row_x, int row_y, int col_y); - -zp_mat matrix_inverse(zp_mat x, int size); - -#endif //PPANN_MATRIX_H +//#ifndef PPANN_MATRIX_H +//#define PPANN_MATRIX_H +// +//#include "field.h" +//#include "group.h" +// +//typedef zp* zp_mat; +// +//zp_mat matrix_zp_from_int(int *int_mat, int row, int col); +// +//zp_mat matrix_zp_rand(int row, int col); +// +//zp_mat matrix_identity(int size); +// +//int matrix_is_identity(zp_mat x, int size); +// +//zp_mat matrix_transpose(zp_mat x, int row, int col); +// +//zp_mat matrix_merge(zp_mat x, zp_mat y, int row, int col_x, int col_y); +// +//zp_mat matrix_multiply(zp_mat x, zp_mat y, int row_x, int row_y, int col_y); +// +//zp_mat matrix_inverse(zp_mat x, int size); +// +//#endif //PPANN_MATRIX_H diff --git a/include/vector.h b/include/vector.h index f55dd9ccc3dec6f496c3ccbeb75d43f43b65c8a0..528f6962ad6d02999b6607dd6998fae06cbd8948 100644 --- a/include/vector.h +++ b/include/vector.h @@ -1,24 +1,24 @@ -#ifndef PPANN_VECTOR_H -#define PPANN_VECTOR_H - -#include "field.h" -#include "group.h" - -typedef zp* zp_vec; -typedef g* g_vec; - -zp_vec vector_zp_from_int(int *int_vec, int size); - -zp_vec vector_zp_rand(int size); - -zp_vec vector_merge(zp_vec a, zp_vec b, int size_a, int size_b); - -zp_vec vector_add(zp_vec a, zp_vec b, int size); - -void zp_inner_product(zp r, zp_vec a, zp_vec b, int size); - -g_vec vector_raise(g base, zp_vec x, int size); - -void inner_product(gt r, g_vec a, g_vec b, int size); - -#endif //PPANN_VECTOR_H \ No newline at end of file +//#ifndef PPANN_VECTOR_H +//#define PPANN_VECTOR_H +// +//#include "field.h" +//#include "group.h" +// +//typedef zp* zp_vec; +//typedef g* g_vec; +// +//zp_vec vector_zp_from_int(int *int_vec, int size); +// +//zp_vec vector_zp_rand(int size); +// +//zp_vec vector_merge(zp_vec a, zp_vec b, int size_a, int size_b); +// +//zp_vec vector_add(zp_vec a, zp_vec b, int size); +// +//void zp_inner_product(zp r, zp_vec a, zp_vec b, int size); +// +//g_vec vector_raise(g base, zp_vec x, int size); +// +//void inner_product(gt r, g_vec a, g_vec b, int size); +// +//#endif //PPANN_VECTOR_H \ No newline at end of file diff --git a/src/field.c b/src/field.c index 5f5aaae617a0baa3d439a70b64d32cfbc93ec039..fe727080e23220046ac5f3ce13be30b4b90b440e 100644 --- a/src/field.c +++ b/src/field.c @@ -1,41 +1,82 @@ #include "field.h" -void rand_zp(dig_t *x) { - fp_rand(x); +struct zp new_zp() { + struct zp result; + bn_new(result.point); + bn_new(result.modular); + return result; } -void zp_zero(dig_t *x) { - fp_zero(x); +struct zp rand_zp(bn_st *modular) { + struct zp result = new_zp(); + bn_rand_mod(result.point, modular); + bn_copy(result.modular, modular); + return result; } -void zp_one(dig_t *x) { - zp_from_int(x, 1); +struct zp zp_zero(bn_st *modular) { + struct zp result = new_zp(); + bn_set_dig(result.point, 0); + bn_copy(result.modular, modular); + return result; } -void zp_copy(dig_t *x_copy, dig_t *x) { - fp_copy(x_copy, x); +struct zp zp_one(bn_st *modular) { + struct zp result = new_zp(); + bn_set_dig(result.point, 1); + bn_copy(result.modular, modular); + return result; } -void zp_from_int(dig_t *x, int y) { - fp_set_dig(x, y); +struct zp zp_copy(struct zp x) { + struct zp result = new_zp(); + bn_copy(result.point, x.point); + bn_copy(result.modular, x.modular); + return result; } -void zp_add(dig_t *r, dig_t *x, dig_t *y) { - fp_add(r, x, y); +struct zp zp_from_int(int x, bn_st *modular) { + struct zp result = new_zp(); + bn_set_dig(result.point, x); + bn_copy(result.modular, modular); + return result; } -void zp_neg(dig_t *nx, dig_t *x) { - fp_neg(nx, x); +struct zp zp_add(struct zp x, struct zp y) { + struct zp result = new_zp(); + bn_add(result.point, x.point, y.point); + bn_mod(result.point, result.point, x.modular); + bn_copy(result.modular, x.modular); + return result; } -void zp_multiply(dig_t *p, dig_t *x, dig_t *y) { - fp_mul(p, x, y); +struct zp zp_neg(struct zp x) { + struct zp result = new_zp(); + bn_neg(result.point, x.point); + bn_mod(result.point, result.point, x.modular); + bn_copy(result.modular, x.modular); + return result; } -void zp_inverse(dig_t *xi, dig_t *x) { - fp_inv(xi, x); +struct zp zp_mul(struct zp x, struct zp y) { + struct zp result = new_zp(); + bn_mul(result.point, x.point, y.point); + bn_mod(result.point, result.point, x.modular); + bn_copy(result.modular, x.modular); + return result; } -int zp_is_int(dig_t *x, int x_int) { - return fp_cmp_dig(x, x_int) == RLC_EQ; +struct zp zp_inv(struct zp x) { + struct zp result = new_zp(); + bn_mod_inv(result.point, x.point, x.modular); + bn_copy(result.modular, x.modular); + return result; +} + +int zp_cmp(struct zp x, struct zp y) { + return bn_cmp(x.point, y.point) == RLC_EQ; +} + +int zp_cmp_int(struct zp x, int y) { + return bn_cmp_dig(x.point, y) == RLC_EQ; } \ No newline at end of file diff --git a/src/group.c b/src/group.c index 2581badc6ca0c57153cc42f5beb62a648e559999..0f79697c6a3a04de8d094ad197a9c57172234188 100644 --- a/src/group.c +++ b/src/group.c @@ -1,21 +1,21 @@ -#include "group.h" - -void generator(ep_st *x) { - g1_get_gen(x); -} - -void multiply(ep_st *r, ep_st *x, dig_t *y) { - bn_t new_y; - fp_prime_back(new_y, y); - g1_mul(r, x, new_y); -} - -void exponentiation(fp_t *r, fp_t *x, dig_t *y) { - bn_t new_y; - fp_prime_back(new_y, y); - gt_exp(r, x, new_y); -} - -void map(ep_st *a, ep_st *b, fp_t *r) { - pc_map(r, a, b); -} +//#include "group.h" +// +//void generator(ep_st *x) { +// g1_get_gen(x); +//} +// +//void multiply(ep_st *r, ep_st *x, dig_t *y) { +// bn_t new_y; +// fp_prime_back(new_y, y); +// g1_mul(r, x, new_y); +//} +// +//void exponentiation(fp_t *r, fp_t *x, dig_t *y) { +// bn_t new_y; +// fp_prime_back(new_y, y); +// gt_exp(r, x, new_y); +//} +// +//void map(ep_st *a, ep_st *b, fp_t *r) { +// pc_map(r, a, b); +//} diff --git a/src/matrix.c b/src/matrix.c index f13b7945a82df7ed6e88820b3a98b4e892c2940c..78001e6513db5b14a1f7ea3431c509a55ecedeb0 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -1,143 +1,143 @@ -#include "matrix.h" - -zp_mat matrix_zp_from_int(int *int_mat, int row, int col) { - zp_mat x; - x = (zp_mat) malloc(sizeof(zp) * row * col); - for (int i = 0; i < row; i++) { - for (int j = 0; j < col; j++) { - zp_from_int(x[i * col + j], int_mat[i * col + j]); - } - } - return x; -} - -zp_mat matrix_zp_rand(int row, int col) { - zp_mat x; - x = (zp_mat) malloc(sizeof(zp) * row * col); - for (int i = 0; i < row; i++) { - for (int j = 0; j < col; j++) { - rand_zp(x[i * col + j]); - } - } - return x; -} - -zp_mat matrix_identity(int size) { - zp_mat x; - x = (zp_mat) malloc(sizeof(zp) * size * size); - for (int i = 0; i < size; i++) { - for (int j = 0; j < size; j++) { - if (i == j) zp_from_int(x[i * size + j], 1); - else zp_zero(x[i * size + j]); - } - } - return x; -} - -int matrix_is_identity(zp_mat x, int size) { - for (int i = 0; i < size; i++) { - for (int j = 0; j < size; j++) { - if (i == j && fp_cmp_dig(x[i * size + j], 1) != RLC_EQ) return 0; - if (i != j && fp_cmp_dig(x[i * size + j], 0) != RLC_EQ) return 0; - } - } - return 1; -} - -zp_mat matrix_transpose(zp_mat x, int row, int col) { - zp_mat xt; - xt = (zp_mat) malloc(sizeof(zp) * row * col); - for (int i = 0; i < row; i++) { - for (int j = 0; j < col; j++) { - zp_copy(xt[j * row + i], x[i * col + j]); - } - } - return xt; -} - -zp_mat matrix_merge(zp_mat x, zp_mat y, int row, int col_x, int col_y) { - zp_mat xy; - xy = (zp_mat) malloc(sizeof(zp) * row * (col_x + col_y)); - for (int i = 0; i < row; i++) { - for (int j = 0; j < col_x; j++) { - zp_copy(xy[i * (col_x + col_y) + j], x[i * col_x + j]); - } - for (int j = 0; j < col_y; j++) { - zp_copy(xy[i * (col_x + col_y) + col_x + j], y[i * col_y + j]); - } - } - return xy; -} - -zp_mat matrix_multiply(zp_mat x, zp_mat y, int row_x, int row_y, int col_y) { - zp temp; - zp_mat xy; - xy = (zp_mat) malloc(sizeof(zp) * row_x * col_y); - - for (int i = 0; i < row_x; i++) { - for (int j = 0; j < col_y; j++) { - zp_zero(xy[i * row_y + j]); - for (int k = 0; k < row_y; k++) { - zp_multiply(temp, x[i * row_y + k], y[k * col_y + j]); - zp_add(xy[i * col_y + j], xy[i * col_y + j], temp); - } - } - } - return xy; -} - -zp_mat matrix_inverse(zp_mat x, int size) { - // Declare the row echelon matrix and generate it. - zp_mat identity, row_echelon; - identity = matrix_identity(size); - row_echelon = matrix_merge(x, identity, size, size, size); - - // Declare temp value. - zp temp_multiplier, temp_neg; - - // Bottom left half to all zeros. - for (int i = 0; i < size; i++) { - for (int j = i; j < size; j++) { - if (i == j && !zp_is_int(row_echelon[i * 2 * size + j], 1)) { - zp_inverse(temp_multiplier, row_echelon[i * 2 * size + i]); - for (int k = i; k < size * 2; k++) { - zp_multiply(row_echelon[j * 2 * size + k], row_echelon[j * 2 * size + k], temp_multiplier); - } - } - - if (i == j && zp_is_int(row_echelon[i * 2 * size + j], 0)) break; - - if (i != j) { - zp_copy(temp_multiplier, row_echelon[j * 2 * size + i]); - for (int k = i; k < size * 2; k++) { - zp_multiply(temp_neg, temp_multiplier, row_echelon[i * 2 * size + k]); - zp_neg(temp_neg, temp_neg); - zp_add(row_echelon[j * 2 * size + k], row_echelon[j * 2 * size + k], temp_neg); - } - } - } - } - - // Top right half to all zeros. - for (int i = size - 1; i > 0; i--) { - for (int j = i - 1; j >= 0; j--) { - zp_copy(temp_multiplier, row_echelon[j * 2 * size + i]); - for (int k = i; k < size * 2; k++) { - zp_multiply(temp_neg, temp_multiplier, row_echelon[i * 2 * size + k]); - zp_neg(temp_neg, temp_neg); - zp_add(row_echelon[j * 2 * size + k], row_echelon[j * 2 * size + k], temp_neg); - } - } - } - - - // Copy over the output. - zp_mat xi; - xi = (zp_mat) malloc(sizeof(zp) * size * size); - for (int i = 0; i < size; i++) { - for (int j = 0; j < size; j++) { - zp_copy(xi[i * size + j], row_echelon[i * 2 * size + size + j]); - } - } - return xi; -} \ No newline at end of file +//#include "matrix.h" +// +//zp_mat matrix_zp_from_int(int *int_mat, int row, int col) { +// zp_mat x; +// x = (zp_mat) malloc(sizeof(zp) * row * col); +// for (int i = 0; i < row; i++) { +// for (int j = 0; j < col; j++) { +// zp_from_int(x[i * col + j], int_mat[i * col + j]); +// } +// } +// return x; +//} +// +//zp_mat matrix_zp_rand(int row, int col) { +// zp_mat x; +// x = (zp_mat) malloc(sizeof(zp) * row * col); +// for (int i = 0; i < row; i++) { +// for (int j = 0; j < col; j++) { +// rand_zp(x[i * col + j]); +// } +// } +// return x; +//} +// +//zp_mat matrix_identity(int size) { +// zp_mat x; +// x = (zp_mat) malloc(sizeof(zp) * size * size); +// for (int i = 0; i < size; i++) { +// for (int j = 0; j < size; j++) { +// if (i == j) zp_from_int(x[i * size + j], 1); +// else zp_zero(x[i * size + j]); +// } +// } +// return x; +//} +// +//int matrix_is_identity(zp_mat x, int size) { +// for (int i = 0; i < size; i++) { +// for (int j = 0; j < size; j++) { +// if (i == j && fp_cmp_dig(x[i * size + j], 1) != RLC_EQ) return 0; +// if (i != j && fp_cmp_dig(x[i * size + j], 0) != RLC_EQ) return 0; +// } +// } +// return 1; +//} +// +//zp_mat matrix_transpose(zp_mat x, int row, int col) { +// zp_mat xt; +// xt = (zp_mat) malloc(sizeof(zp) * row * col); +// for (int i = 0; i < row; i++) { +// for (int j = 0; j < col; j++) { +// zp_copy(xt[j * row + i], x[i * col + j]); +// } +// } +// return xt; +//} +// +//zp_mat matrix_merge(zp_mat x, zp_mat y, int row, int col_x, int col_y) { +// zp_mat xy; +// xy = (zp_mat) malloc(sizeof(zp) * row * (col_x + col_y)); +// for (int i = 0; i < row; i++) { +// for (int j = 0; j < col_x; j++) { +// zp_copy(xy[i * (col_x + col_y) + j], x[i * col_x + j]); +// } +// for (int j = 0; j < col_y; j++) { +// zp_copy(xy[i * (col_x + col_y) + col_x + j], y[i * col_y + j]); +// } +// } +// return xy; +//} +// +//zp_mat matrix_multiply(zp_mat x, zp_mat y, int row_x, int row_y, int col_y) { +// zp temp; +// zp_mat xy; +// xy = (zp_mat) malloc(sizeof(zp) * row_x * col_y); +// +// for (int i = 0; i < row_x; i++) { +// for (int j = 0; j < col_y; j++) { +// zp_zero(xy[i * row_y + j]); +// for (int k = 0; k < row_y; k++) { +// zp_multiply(temp, x[i * row_y + k], y[k * col_y + j]); +// zp_add(xy[i * col_y + j], xy[i * col_y + j], temp); +// } +// } +// } +// return xy; +//} +// +//zp_mat matrix_inverse(zp_mat x, int size) { +// // Declare the row echelon matrix and generate it. +// zp_mat identity, row_echelon; +// identity = matrix_identity(size); +// row_echelon = matrix_merge(x, identity, size, size, size); +// +// // Declare temp value. +// zp temp_multiplier, temp_neg; +// +// // Bottom left half to all zeros. +// for (int i = 0; i < size; i++) { +// for (int j = i; j < size; j++) { +// if (i == j && !zp_is_int(row_echelon[i * 2 * size + j], 1)) { +// zp_inverse(temp_multiplier, row_echelon[i * 2 * size + i]); +// for (int k = i; k < size * 2; k++) { +// zp_multiply(row_echelon[j * 2 * size + k], row_echelon[j * 2 * size + k], temp_multiplier); +// } +// } +// +// if (i == j && zp_is_int(row_echelon[i * 2 * size + j], 0)) break; +// +// if (i != j) { +// zp_copy(temp_multiplier, row_echelon[j * 2 * size + i]); +// for (int k = i; k < size * 2; k++) { +// zp_multiply(temp_neg, temp_multiplier, row_echelon[i * 2 * size + k]); +// zp_neg(temp_neg, temp_neg); +// zp_add(row_echelon[j * 2 * size + k], row_echelon[j * 2 * size + k], temp_neg); +// } +// } +// } +// } +// +// // Top right half to all zeros. +// for (int i = size - 1; i > 0; i--) { +// for (int j = i - 1; j >= 0; j--) { +// zp_copy(temp_multiplier, row_echelon[j * 2 * size + i]); +// for (int k = i; k < size * 2; k++) { +// zp_multiply(temp_neg, temp_multiplier, row_echelon[i * 2 * size + k]); +// zp_neg(temp_neg, temp_neg); +// zp_add(row_echelon[j * 2 * size + k], row_echelon[j * 2 * size + k], temp_neg); +// } +// } +// } +// +// +// // Copy over the output. +// zp_mat xi; +// xi = (zp_mat) malloc(sizeof(zp) * size * size); +// for (int i = 0; i < size; i++) { +// for (int j = 0; j < size; j++) { +// zp_copy(xi[i * size + j], row_echelon[i * 2 * size + size + j]); +// } +// } +// return xi; +//} \ No newline at end of file diff --git a/src/vector.c b/src/vector.c index 8aa18bcf7f4ade68f1108f72ba1d01c3dd4b8ffc..78b28eb304b8db74b84f08f2b3d78ea168cc0736 100644 --- a/src/vector.c +++ b/src/vector.c @@ -1,64 +1,64 @@ -#include "vector.h" - -zp_vec vector_zp_from_int(int *int_vec, int size) { - zp_vec x; - x = (zp_vec) malloc(sizeof(zp) * size); - for (int i = 0; i < size; i++) zp_from_int(x[i], int_vec[i]); - return x; -} - -zp_vec vector_zp_rand(int size) { - zp_vec x; - x = (zp_vec) malloc(sizeof(zp) * size); - for (int i = 0; i < size; i++) rand_zp(x[i]); - return x; -} - -zp_vec vector_merge(zp_vec a, zp_vec b, int size_a, int size_b) { - zp_vec r; - r = (zp_vec) malloc(sizeof(zp) * (size_a + size_b)); - for (int i = 0; i < size_a; i++) zp_copy(r[i], a[i]); - for (int i = 0; i < size_b; i++) zp_copy(r[i + size_a], b[i]); - return r; -} - -zp_vec vector_add(zp_vec a, zp_vec b, int size) { - zp_vec r; - r = (zp_vec) malloc(sizeof(zp) * size); - for (int i = 0; i < size; i++) zp_add(r[i], a[i], b[i]); - return r; -} - -void zp_inner_product(dig_t *r, zp_vec a, zp_vec b, int size) { - zp_zero(r); - zp temp; - for (int i = 0; i < size; i++) { - zp_multiply(temp, a[i], b[i]); - zp_add(r, r, temp); - } -} - -g_vec vector_raise(g base, zp_vec x, int size) { - g_vec r; - r = (g_vec) malloc(sizeof(g) * size); - for (int i = 0; i < size; i++) multiply(r[i], base, x[i]); - return r; -} - -void inner_product(gt r, g_vec a, g_vec b, int size) { - gt temp; - gt_set_unity(r); - for (int i = 0; i < size; i++) { - pc_map(temp, a[i], b[i]); - gt_mul(r, r, temp); - } -} - - - - - - - - - +//#include "vector.h" +// +//zp_vec vector_zp_from_int(int *int_vec, int size) { +// zp_vec x; +// x = (zp_vec) malloc(sizeof(zp) * size); +// for (int i = 0; i < size; i++) zp_from_int(x[i], int_vec[i]); +// return x; +//} +// +//zp_vec vector_zp_rand(int size) { +// zp_vec x; +// x = (zp_vec) malloc(sizeof(zp) * size); +// for (int i = 0; i < size; i++) rand_zp(x[i]); +// return x; +//} +// +//zp_vec vector_merge(zp_vec a, zp_vec b, int size_a, int size_b) { +// zp_vec r; +// r = (zp_vec) malloc(sizeof(zp) * (size_a + size_b)); +// for (int i = 0; i < size_a; i++) zp_copy(r[i], a[i]); +// for (int i = 0; i < size_b; i++) zp_copy(r[i + size_a], b[i]); +// return r; +//} +// +//zp_vec vector_add(zp_vec a, zp_vec b, int size) { +// zp_vec r; +// r = (zp_vec) malloc(sizeof(zp) * size); +// for (int i = 0; i < size; i++) zp_add(r[i], a[i], b[i]); +// return r; +//} +// +//void zp_inner_product(dig_t *r, zp_vec a, zp_vec b, int size) { +// zp_zero(r); +// zp temp; +// for (int i = 0; i < size; i++) { +// zp_multiply(temp, a[i], b[i]); +// zp_add(r, r, temp); +// } +//} +// +//g_vec vector_raise(g base, zp_vec x, int size) { +// g_vec r; +// r = (g_vec) malloc(sizeof(g) * size); +// for (int i = 0; i < size; i++) multiply(r[i], base, x[i]); +// return r; +//} +// +//void inner_product(gt r, g_vec a, g_vec b, int size) { +// gt temp; +// gt_set_unity(r); +// for (int i = 0; i < size; i++) { +// pc_map(temp, a[i], b[i]); +// gt_mul(r, r, temp); +// } +//} +// +// +// +// +// +// +// +// +// diff --git a/tests/test_field.c b/tests/test_field.c index d6e4c93e8e88ae9815075cab1b985844b7ff3ee1..8a13422c5adf726b4a9166b3bc85768af125a5ae 100644 --- a/tests/test_field.c +++ b/tests/test_field.c @@ -1,77 +1,72 @@ #include "field.h" -int test_zp_zero() { - zp x; - zp_zero(x); - return zp_is_int(x, 0); +int test_zp_zero(bn_st *N) { + struct zp x = zp_zero(N); + return zp_cmp_int(x, 0); } -int test_zp_one() { - zp x; - zp_one(x); - return zp_is_int(x, 1); +int test_zp_one(bn_st *N) { + struct zp x = zp_one(N); + return zp_cmp_int(x, 1); } -int test_zp_copy() { - zp x, y; - zp_from_int(x, 3); - zp_copy(y, x); - return zp_is_int(y, 3); +int test_zp_copy(bn_st *N) { + struct zp x = zp_from_int(10, N); + struct zp y = zp_copy(x); + return zp_cmp(x, y); } -int test_zp_from_int() { - zp x; - zp_from_int(x, 3); - return zp_is_int(x, 3); +int test_zp_from_int(bn_st *N) { + struct zp x = zp_from_int(3, N); + return zp_cmp_int(x, 3); } -int test_zp_add() { - zp x, y, r; - zp_from_int(x, 10); - zp_from_int(y, 20); - zp_add(r, x, y); - return zp_is_int(r, 30); +int test_zp_add(bn_st *N) { + struct zp x = zp_from_int(10, N); + struct zp y = zp_from_int(20, N); + struct zp z = zp_add(x, y); + return zp_cmp_int(z, 30); } -int test_zp_neg() { - zp x, y, r; - rand_zp(x); - zp_neg(y, x); - zp_add(r, x, y); - return zp_is_int(r, 0); +int test_zp_neg(bn_st *N) { + struct zp x = rand_zp(N); + struct zp y = zp_neg(x); + struct zp z = zp_add(x, y); + return zp_cmp_int(z, 0); } -int test_zp_multiply() { - zp x, y, r; - zp_from_int(x, 10); - zp_from_int(y, 20); - zp_multiply(r, x, y); - return zp_is_int(r, 200); +int test_zp_mul(bn_st *N) { + struct zp x = zp_from_int(10, N); + struct zp y = zp_from_int(20, N); + struct zp z = zp_mul(x, y); + return zp_cmp_int(z, 200); } -int test_zp_inverse() { - zp x, xi, r; - rand_zp(x); - zp_inverse(xi, x); - zp_multiply(r, x, xi); - return zp_is_int(r, 1); +int test_zp_inv(bn_st *N) { + struct zp x = rand_zp(N); + struct zp y = zp_inv(x); + struct zp z = zp_mul(x, y); + return zp_cmp_int(z, 1); } - int main() { // Init core and setup. core_init(); pc_param_set_any(); + // Get order. + bn_t N; + pc_get_ord(N); + // Perform tests. - if (test_zp_zero() != 1) return 1; - if (test_zp_one() != 1) return 1; - if (test_zp_copy() != 1) return 1; - if (test_zp_from_int() != 1) return 1; - if (test_zp_add() != 1) return 1; - if (test_zp_neg() != 1) return 1; - if (test_zp_multiply() != 1) return 1; - if (test_zp_inverse() != 1) return 1; + if (test_zp_zero(N) != 1) return 1; + if (test_zp_one(N) != 1) return 1; + if (test_zp_copy(N) != 1) return 1; + if (test_zp_from_int(N) != 1) return 1; + if (test_zp_add(N) != 1) return 1; + if (test_zp_neg(N) != 1) return 1; + if (test_zp_mul(N) != 1) return 1; + if (test_zp_inv(N) != 1) return 1; return 0; } \ No newline at end of file diff --git a/tests/test_group.c b/tests/test_group.c index 0e6b9d1b040dea4aef906ed8cb4dfeb9a8b312bb..5a0ce85e3202d5a41bf84dbbdcbe55103b0d51cf 100644 --- a/tests/test_group.c +++ b/tests/test_group.c @@ -1,45 +1,45 @@ -#include "group.h" - -int test_generator() { - g x; - generator(x); - return g1_is_valid(x); -} - -int test_all() { - // Set integers. - zp m, n; - zp_from_int(m, 5); - zp_from_int(n, 25); - - // Declare variables. - g a, b; - gt x, y, z; - - // Get generator g and find g^5. - generator(a); - multiply(b, a, m); - - // Get e(g, g) and e(g^5, g^5). - map(a, a, x); - map(b, b, y); - - // Get e(g, g)^25. - exponentiation(z, x, n); - - // Compare e(g^5, g^5) with e(g, g)^25. - return gt_cmp(y, z); -} - -int main(){ - // Init core and setup. - core_init(); - fp_prime_init(); - pc_param_set_any(); - - // Perform tests. - if (test_generator() != 1) return 1; - if (test_all() != RLC_EQ) return 1; - - return 0; -} \ No newline at end of file +//#include "group.h" +// +//int test_generator() { +// g x; +// generator(x); +// return g1_is_valid(x); +//} +// +//int test_all() { +// // Set integers. +// zp m, n; +// zp_from_int(m, 5); +// zp_from_int(n, 25); +// +// // Declare variables. +// g a, b; +// gt x, y, z; +// +// // Get generator g and find g^5. +// generator(a); +// multiply(b, a, m); +// +// // Get e(g, g) and e(g^5, g^5). +// map(a, a, x); +// map(b, b, y); +// +// // Get e(g, g)^25. +// exponentiation(z, x, n); +// +// // Compare e(g^5, g^5) with e(g, g)^25. +// return gt_cmp(y, z); +//} +// +//int main(){ +// // Init core and setup. +// core_init(); +// fp_prime_init(); +// pc_param_set_any(); +// +// // Perform tests. +// if (test_generator() != 1) return 1; +// if (test_all() != RLC_EQ) return 1; +// +// return 0; +//} \ No newline at end of file diff --git a/tests/test_matrix.c b/tests/test_matrix.c index 24857d3205dc3f5312b243c4077c0bc2aada10c4..9c8e054538e0e6a810b01820c0a4f4949a6c1040 100644 --- a/tests/test_matrix.c +++ b/tests/test_matrix.c @@ -1,74 +1,74 @@ -#include "matrix.h" - -int test_zp_from_int() { - zp_mat x; - int int_mat[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; - x = matrix_zp_from_int(int_mat, 3, 3); - return zp_is_int(x[8], 9); -} - -int test_transpose() { - int row = 3, col = 3; - zp_mat x, xt; - x = matrix_zp_rand(row, col); - xt = matrix_transpose(x, row, col); - return fp_cmp(xt[col - 1], x[2 * row]); -} - -int test_identity() { - int size = 100; - zp_mat x; - x = matrix_identity(size); - return matrix_is_identity(x, size); -} - -int test_merge() { - int size = 10; - zp_mat xy, x, y; - x = matrix_zp_rand(size, size); - y = matrix_identity(size); - xy = matrix_merge(x, y, size, size, size); - return fp_cmp(x[2 * size + 1], xy[4 * size + 1]); -} - -int test_multiply_vector() { - int mat_x[5] = {1, 2, 3, 4, 5}; - int mat_y[15] = {10, 20, 30, - 10, 20, 30, - 10, 20, 30, - 10, 20, 30, - 10, 20, 30}; - - zp_mat x, y, xy; - x = matrix_zp_from_int(mat_x, 1, 5); - y = matrix_zp_from_int(mat_y, 5, 3); - xy = matrix_multiply(x, y, 1, 5, 3); - - return fp_cmp_dig(xy[1], 300); -} - -int test_inverse() { - int size = 100; - // Allocate space. - zp_mat x, xi, r; - x = matrix_zp_rand(size, size); - xi = matrix_inverse(x, size); - r = matrix_multiply(xi, x, size, size, size); - return matrix_is_identity(r, size); -} - -int main() { - // Init core and setup. - core_init(); - pc_param_set_any(); - - // Perform tests. - if (test_zp_from_int() != 1) return 1; - if (test_transpose() != RLC_EQ) return 1; - if (test_identity() != 1) return 1; - if (test_merge() != RLC_EQ) return 1; - if (test_multiply_vector() != RLC_EQ) return 1; - if (test_inverse() != 1) return 1; - - return 0; -} \ No newline at end of file +//#include "matrix.h" +// +//int test_zp_from_int() { +// zp_mat x; +// int int_mat[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; +// x = matrix_zp_from_int(int_mat, 3, 3); +// return zp_is_int(x[8], 9); +//} +// +//int test_transpose() { +// int row = 3, col = 3; +// zp_mat x, xt; +// x = matrix_zp_rand(row, col); +// xt = matrix_transpose(x, row, col); +// return fp_cmp(xt[col - 1], x[2 * row]); +//} +// +//int test_identity() { +// int size = 100; +// zp_mat x; +// x = matrix_identity(size); +// return matrix_is_identity(x, size); +//} +// +//int test_merge() { +// int size = 10; +// zp_mat xy, x, y; +// x = matrix_zp_rand(size, size); +// y = matrix_identity(size); +// xy = matrix_merge(x, y, size, size, size); +// return fp_cmp(x[2 * size + 1], xy[4 * size + 1]); +//} +// +//int test_multiply_vector() { +// int mat_x[5] = {1, 2, 3, 4, 5}; +// int mat_y[15] = {10, 20, 30, +// 10, 20, 30, +// 10, 20, 30, +// 10, 20, 30, +// 10, 20, 30}; +// +// zp_mat x, y, xy; +// x = matrix_zp_from_int(mat_x, 1, 5); +// y = matrix_zp_from_int(mat_y, 5, 3); +// xy = matrix_multiply(x, y, 1, 5, 3); +// +// return fp_cmp_dig(xy[1], 300); +//} +// +//int test_inverse() { +// int size = 100; +// // Allocate space. +// zp_mat x, xi, r; +// x = matrix_zp_rand(size, size); +// xi = matrix_inverse(x, size); +// r = matrix_multiply(xi, x, size, size, size); +// return matrix_is_identity(r, size); +//} +// +//int main() { +// // Init core and setup. +// core_init(); +// pc_param_set_any(); +// +// // Perform tests. +// if (test_zp_from_int() != 1) return 1; +// if (test_transpose() != RLC_EQ) return 1; +// if (test_identity() != 1) return 1; +// if (test_merge() != RLC_EQ) return 1; +// if (test_multiply_vector() != RLC_EQ) return 1; +// if (test_inverse() != 1) return 1; +// +// return 0; +//} \ No newline at end of file diff --git a/tests/test_vector.c b/tests/test_vector.c index 767660ec6cc7e8c5aa372acd912352f84feb333f..759f5de33a3a55e904f7b230ae409b716360a91b 100644 --- a/tests/test_vector.c +++ b/tests/test_vector.c @@ -1,77 +1,77 @@ -#include "vector.h" - -int test_zp_from_int() { - zp_vec x; - int int_vec[4] = {1, 2, 3, 4}; - x = vector_zp_from_int(int_vec, 4); - return zp_is_int(x[3], 4); -} - -int test_merge_vector() { - zp_vec x, y, z; - int int_vec_x[3] = {1, 2, 3}; - int int_vec_y[3] = {11, 22, 33}; - x = vector_zp_from_int(int_vec_x, 3); - y = vector_zp_from_int(int_vec_y, 3); - z = vector_merge(x, y, 3, 3); - return zp_is_int(z[5], 33); -} - -int test_add_vector() { - zp_vec x, y, z; - int int_vec_x[3] = {1, 2, 3}; - int int_vec_y[3] = {11, 22, 33}; - x = vector_zp_from_int(int_vec_x, 3); - y = vector_zp_from_int(int_vec_y, 3); - z = vector_add(x, y, 3); - return zp_is_int(z[2], 36); -} - -int test_zp_inner_product() { - zp_vec x, y; - int int_vec_x[3] = {1, 2, 3}; - int int_vec_y[3] = {11, 22, 33}; - x = vector_zp_from_int(int_vec_x, 3); - y = vector_zp_from_int(int_vec_y, 3); - - zp r; - zp_inner_product(r, x, y, 3); - return zp_is_int(r, 154); -} - -int test_inner_product() { - zp_vec x, y; - int int_vec_x[3] = {1, 2, 3}; - int int_vec_y[3] = {4, 5, 6}; - x = vector_zp_from_int(int_vec_x, 3); - y = vector_zp_from_int(int_vec_y, 3); - - g base; - g_vec gx, gy; - generator(base); - gx = vector_raise(base, x, 3); - gy = vector_raise(base, y, 3); - - gt b, r; - inner_product(r, gx, gy, 3); - map(base, base, b); - gt_exp_dig(b, b, 32); - - return gt_cmp(b, r); -} - - -int main() { - // Init core and setup. - core_init(); - pc_param_set_any(); - - // Perform tests. - if (test_zp_from_int() != 1) return 1; - if (test_merge_vector() != 1) return 1; - if (test_add_vector() != 1) return 1; - if (test_zp_inner_product() != 1) return 1; - if (test_inner_product() != RLC_EQ) return 1; - - return 0; -} \ No newline at end of file +//#include "vector.h" +// +//int test_zp_from_int() { +// zp_vec x; +// int int_vec[4] = {1, 2, 3, 4}; +// x = vector_zp_from_int(int_vec, 4); +// return zp_is_int(x[3], 4); +//} +// +//int test_merge_vector() { +// zp_vec x, y, z; +// int int_vec_x[3] = {1, 2, 3}; +// int int_vec_y[3] = {11, 22, 33}; +// x = vector_zp_from_int(int_vec_x, 3); +// y = vector_zp_from_int(int_vec_y, 3); +// z = vector_merge(x, y, 3, 3); +// return zp_is_int(z[5], 33); +//} +// +//int test_add_vector() { +// zp_vec x, y, z; +// int int_vec_x[3] = {1, 2, 3}; +// int int_vec_y[3] = {11, 22, 33}; +// x = vector_zp_from_int(int_vec_x, 3); +// y = vector_zp_from_int(int_vec_y, 3); +// z = vector_add(x, y, 3); +// return zp_is_int(z[2], 36); +//} +// +//int test_zp_inner_product() { +// zp_vec x, y; +// int int_vec_x[3] = {1, 2, 3}; +// int int_vec_y[3] = {11, 22, 33}; +// x = vector_zp_from_int(int_vec_x, 3); +// y = vector_zp_from_int(int_vec_y, 3); +// +// zp r; +// zp_inner_product(r, x, y, 3); +// return zp_is_int(r, 154); +//} +// +//int test_inner_product() { +// zp_vec x, y; +// int int_vec_x[3] = {1, 2, 3}; +// int int_vec_y[3] = {4, 5, 6}; +// x = vector_zp_from_int(int_vec_x, 3); +// y = vector_zp_from_int(int_vec_y, 3); +// +// g base; +// g_vec gx, gy; +// generator(base); +// gx = vector_raise(base, x, 3); +// gy = vector_raise(base, y, 3); +// +// gt b, r; +// inner_product(r, gx, gy, 3); +// map(base, base, b); +// gt_exp_dig(b, b, 32); +// +// return gt_cmp(b, r); +//} +// +// +//int main() { +// // Init core and setup. +// core_init(); +// pc_param_set_any(); +// +// // Perform tests. +// if (test_zp_from_int() != 1) return 1; +// if (test_merge_vector() != 1) return 1; +// if (test_add_vector() != 1) return 1; +// if (test_zp_inner_product() != 1) return 1; +// if (test_inner_product() != RLC_EQ) return 1; +// +// return 0; +//} \ No newline at end of file