diff --git a/CMakeLists.txt b/CMakeLists.txt index 2e5b590f5289fc0743a37b71c1ac7b334750e835..af2ac200dbf68e0d90e357feafeb84f3b418d075 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,7 +12,6 @@ if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) include(CTest) endif() - find_library(RELIC_LIB relic) add_subdirectory(src) diff --git a/apps/scheme.c b/apps/scheme.c index 59ee1d56d5222cb1d5307da5fe6c4c9c5c1b9a27..e41a24ef835722cb747b1fd9166dfe3fcaa13f75 100644 --- a/apps/scheme.c +++ b/apps/scheme.c @@ -1,118 +1,118 @@ #include "vector.h" #include "matrix.h" - -const int B_SIZE = 6; - -struct key { - zp_mat A; - zp_mat B; - zp_mat Bi; - g base; - gt t_base; -}; - -struct ct { - g_vec ctx; - g_vec ctk; - g_vec ctc; -}; - -void initialize_relic() { - core_init(); - pc_param_set_any(); - -} - -struct key setup(int size) { - struct key key; - key.A = matrix_zp_rand(2, size); - key.B = matrix_zp_rand(B_SIZE, B_SIZE); - key.Bi = matrix_inverse(key.B, B_SIZE); - key.Bi = matrix_transpose(key.Bi, B_SIZE, B_SIZE); - generator(key.base); - map(key.base, key.base, key.t_base); - return key; -} - -struct ct enc(struct key key, int *message, int size) { - // Declare the returned ciphertext and convert message to zp. - struct ct ct; - zp_vec x = vector_zp_from_int(message, size); - - // Helper values. - int one[] = {1}, zero[] = {0}; - zp_vec one_vec = vector_zp_from_int(one, 1); - zp_vec zero_vec = vector_zp_from_int(zero, 1); - - // We generate s and compute sA + x. - zp_vec s = vector_zp_rand(2); - zp_vec sA = matrix_multiply(s, key.A, 1, 2, size); - zp_vec sAx = vector_add(sA, x, size); - ct.ctx = vector_raise(key.base, sAx, size); - - // We compute the function hiding inner product encryption key. - zp_mat AT = matrix_transpose(key.A, 2, size); - zp_vec xAT = matrix_multiply(x, AT, 1, size, 2); - zp_vec xATs = vector_merge(xAT, s, 2, 2); - zp_vec xATs0 = vector_merge(xATs, zero_vec, 4, 1); - zp_vec xATs01 = vector_merge(xATs0, zero_vec, 5, 1); - zp_vec xATs01_B = matrix_multiply(xATs01, key.B, 1, B_SIZE, B_SIZE); - ct.ctc = vector_raise(key.base, xATs01_B, B_SIZE); - - // We compute the function hiding inner product encryption ciphertext. - zp_vec sAAT = matrix_multiply(sA, AT, 1, size, 2); - zp_vec xATsAAT = vector_add(xAT, sAAT, 2); - zp_vec sxATsAAT = vector_merge(s, xATsAAT, 2, 2); - zp_vec sxATsAAT1 = vector_merge(sxATsAAT, zero_vec, 4, 1); - zp_vec sxATsAAT10 = vector_merge(sxATsAAT1, zero_vec, 5, 1); - zp_vec sxATsAAT10_Bi = matrix_multiply(sxATsAAT10, key.Bi, 1, B_SIZE, B_SIZE); - ct.ctk = vector_raise(key.base, sxATsAAT10_Bi, B_SIZE); - - return ct; -} - -void eval(struct key key, struct ct x, struct ct y, int size) { - // - gt z, xy, ct; - inner_product(xy, x.ctx, y.ctx, size); - inner_product(ct, x.ctc, y.ctk, B_SIZE); - - // Decrypt. - gt_inv(ct, ct); - gt_mul(z, xy, ct); - - // Check correctness. - gt desired_output; - gt_exp_dig(desired_output, key.t_base, 10); - - if (gt_cmp(desired_output, z) == RLC_EQ) printf("Magic happened."); - else printf("Fuck my life."); -} - -int main() { - initialize_relic(); - -// int x[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; - struct key key = setup(10); -// struct ct ct_x = enc(key, x, 10); -// eval(key, ct_x, ct_x, 10); - - bn_t a, b, c, n; - pc_get_ord(n); - - bn_rand_mod(a, n); - bn_rand_mod(b, n); - bn_mul(c, a, a); - bn_mod(c, c, n); - - g g1, g2; - g1_mul(g1, key.base, a); - g1_mul(g2, key.base, b); - - gt gt1, gt2;pc_map(gt1, g1, g2); - gt_exp(gt2, key.t_base, c); - - printf("Works if 1: %i", gt_cmp(gt1, gt2) == RLC_EQ); - return 0; -} +// +//const int B_SIZE = 6; +// +//struct key { +// zp_mat A; +// zp_mat B; +// zp_mat Bi; +// g base; +// gt t_base; +//}; +// +//struct ct { +// g_vec ctx; +// g_vec ctk; +// g_vec ctc; +//}; +// +//void initialize_relic() { +// core_init(); +// pc_param_set_any(); +// +//} +// +//struct key setup(int size) { +// struct key key; +// key.A = matrix_zp_rand(2, size); +// key.B = matrix_zp_rand(B_SIZE, B_SIZE); +// key.Bi = matrix_inverse(key.B, B_SIZE); +// key.Bi = matrix_transpose(key.Bi, B_SIZE, B_SIZE); +// generator(key.base); +// map(key.base, key.base, key.t_base); +// return key; +//} +// +//struct ct enc(struct key key, int *message, int size) { +// // Declare the returned ciphertext and convert message to zp. +// struct ct ct; +// zp_vec x = vector_zp_from_int(message, size); +// +// // Helper values. +// int one[] = {1}, zero[] = {0}; +// zp_vec one_vec = vector_zp_from_int(one, 1); +// zp_vec zero_vec = vector_zp_from_int(zero, 1); +// +// // We generate s and compute sA + x. +// zp_vec s = vector_zp_rand(2); +// zp_vec sA = matrix_multiply(s, key.A, 1, 2, size); +// zp_vec sAx = vector_add(sA, x, size); +// ct.ctx = vector_raise(key.base, sAx, size); +// +// // We compute the function hiding inner product encryption key. +// zp_mat AT = matrix_transpose(key.A, 2, size); +// zp_vec xAT = matrix_multiply(x, AT, 1, size, 2); +// zp_vec xATs = vector_merge(xAT, s, 2, 2); +// zp_vec xATs0 = vector_merge(xATs, zero_vec, 4, 1); +// zp_vec xATs01 = vector_merge(xATs0, zero_vec, 5, 1); +// zp_vec xATs01_B = matrix_multiply(xATs01, key.B, 1, B_SIZE, B_SIZE); +// ct.ctc = vector_raise(key.base, xATs01_B, B_SIZE); +// +// // We compute the function hiding inner product encryption ciphertext. +// zp_vec sAAT = matrix_multiply(sA, AT, 1, size, 2); +// zp_vec xATsAAT = vector_add(xAT, sAAT, 2); +// zp_vec sxATsAAT = vector_merge(s, xATsAAT, 2, 2); +// zp_vec sxATsAAT1 = vector_merge(sxATsAAT, zero_vec, 4, 1); +// zp_vec sxATsAAT10 = vector_merge(sxATsAAT1, zero_vec, 5, 1); +// zp_vec sxATsAAT10_Bi = matrix_multiply(sxATsAAT10, key.Bi, 1, B_SIZE, B_SIZE); +// ct.ctk = vector_raise(key.base, sxATsAAT10_Bi, B_SIZE); +// +// return ct; +//} +// +//void eval(struct key key, struct ct x, struct ct y, int size) { +// // +// gt z, xy, ct; +// inner_product(xy, x.ctx, y.ctx, size); +// inner_product(ct, x.ctc, y.ctk, B_SIZE); +// +// // Decrypt. +// gt_inv(ct, ct); +// gt_mul(z, xy, ct); +// +// // Check correctness. +// gt desired_output; +// gt_exp_dig(desired_output, key.t_base, 10); +// +// if (gt_cmp(desired_output, z) == RLC_EQ) printf("Magic happened."); +// else printf("Fuck my life."); +//} +// +//int main() { +// initialize_relic(); +// +//// int x[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; +// struct key key = setup(10); +//// struct ct ct_x = enc(key, x, 10); +//// eval(key, ct_x, ct_x, 10); +// +// bn_t a, b, c, n; +// pc_get_ord(n); +// +// bn_rand_mod(a, n); +// bn_rand_mod(b, n); +// bn_mul(c, a, a); +// bn_mod(c, c, n); +// +// g g1, g2; +// g1_mul(g1, key.base, a); +// g1_mul(g2, key.base, b); +// +// gt gt1, gt2;pc_map(gt1, g1, g2); +// gt_exp(gt2, key.t_base, c); +// +// printf("Works if 1: %i", gt_cmp(gt1, gt2) == RLC_EQ); +// return 0; +//} diff --git a/include/group.h b/include/group.h index eca51ea3624f1ef91cb38e7c0d096ca386728460..9a94d5943f47ffe11efe731979bebaa060f8ad0e 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 gen(g x); + +void g_mul(g r, g x, struct zp y); + +void gt_raise(gt r, gt x, struct zp y); + +void bp_map(g a, g b, gt r); + +#endif //PPANN_GROUP_H \ No newline at end of file diff --git a/include/vector.h b/include/vector.h index 528f6962ad6d02999b6607dd6998fae06cbd8948..21e0f7c2c94ad14182fc8e303a572c6ca773ba47 100644 --- a/include/vector.h +++ b/include/vector.h @@ -1,24 +1,22 @@ -//#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 struct zp *zp_vec; +typedef g *g_vec; + +zp_vec vector_zp_from_int(const int *int_vec, int size, bn_t modular); + +zp_vec vector_zp_rand(int size, bn_t modular); + +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); + +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/group.c b/src/group.c index 0f79697c6a3a04de8d094ad197a9c57172234188..e6307a5e0f9226982e87f210a92836875bd0628b 100644 --- a/src/group.c +++ b/src/group.c @@ -1,21 +1,17 @@ -//#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 gen(ep_st *x) { + g1_get_gen(x); +} + +void g_mul(ep_st *r, ep_st *x, struct zp y) { + g1_mul(r, x, y.point); +} + +void gt_raise(fp_t *r, fp_t *x, struct zp y) { + gt_exp(r, x, y.point); +} + +void bp_map(ep_st *a, ep_st *b, fp_t *r) { + pc_map(r, a, b); +} \ No newline at end of file diff --git a/src/vector.c b/src/vector.c index 78b28eb304b8db74b84f08f2b3d78ea168cc0736..24f234470b9d553a257488d0723d0d685a988b6f 100644 --- a/src/vector.c +++ b/src/vector.c @@ -1,64 +1,45 @@ -//#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(const int *int_vec, int size, bn_st *modular) { + zp_vec x; + x = (zp_vec) malloc(sizeof(struct zp) * size); + for (int i = 0; i < size; i++) x[i] = zp_from_int(int_vec[i], modular); + return x; +} + +zp_vec vector_zp_rand(int size, bn_st *modular) { + zp_vec x; + x = (zp_vec) malloc(sizeof(struct zp) * size); + for (int i = 0; i < size; i++) x[i] = rand_zp(modular); + 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(struct zp) * (size_a + size_b)); + for (int i = 0; i < size_a; i++) r[i] = zp_copy(a[i]); + for (int i = 0; i < size_b; i++) r[i + size_a] = zp_copy(b[i]); + return r; +} + +zp_vec vector_add(zp_vec a, zp_vec b, int size) { + zp_vec r; + r = (zp_vec) malloc(sizeof(struct zp) * size); + for (int i = 0; i < size; i++) r[i] = zp_add(a[i], b[i]); + return r; +} + +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++) g_mul(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); + } +} \ No newline at end of file diff --git a/tests/test_group.c b/tests/test_group.c index 5a0ce85e3202d5a41bf84dbbdcbe55103b0d51cf..b3c0d220575bb794d28c6569bbb2ce99e30f559a 100644 --- a/tests/test_group.c +++ b/tests/test_group.c @@ -1,45 +1,47 @@ -//#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; + gen(x); + return g1_is_valid(x); +} + +int test_all(bn_st *N) { + // Set integers. + struct zp m = zp_from_int(5, N); + struct zp n = zp_from_int(25, N); + + // Declare variables. + g a, b; + gt x, y, z; + + // Get generator g and find g^5. + gen(a); + g_mul(b, a, m); + + // Get e(g, g) and e(g^5, g^5). + bp_map(a, a, x); + bp_map(b, b, y); + + // Get e(g, g)^25. + gt_raise(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(); + pc_param_set_any(); + + // Get order. + bn_t N; + pc_get_ord(N); + + // Perform tests. + if (test_generator() != 1) return 1; + if (test_all(N) != 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 9c8e054538e0e6a810b01820c0a4f4949a6c1040..60c81f3450a8bc588f18f97c7ce38ac71cffdbe0 100644 --- a/tests/test_matrix.c +++ b/tests/test_matrix.c @@ -61,6 +61,10 @@ // // Init core and setup. // core_init(); // pc_param_set_any(); + +//// Get order. +//bn_t N; +//pc_get_ord(N); // // // Perform tests. // if (test_zp_from_int() != 1) return 1; diff --git a/tests/test_vector.c b/tests/test_vector.c index 759f5de33a3a55e904f7b230ae409b716360a91b..f74581ec83db382a7d19cab92e88b121b0ba6f9d 100644 --- a/tests/test_vector.c +++ b/tests/test_vector.c @@ -1,77 +1,63 @@ -//#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(bn_st *N) { + int int_vec[4] = {1, 2, 3, 4}; + zp_vec x = vector_zp_from_int(int_vec, 4, N); + return zp_cmp_int(x[3], 4); +} + +int test_merge_vector(bn_st *N) { + int int_vec_x[3] = {1, 2, 3}; + int int_vec_y[3] = {11, 22, 33}; + zp_vec x = vector_zp_from_int(int_vec_x, 3, N); + zp_vec y = vector_zp_from_int(int_vec_y, 3, N); + zp_vec z = vector_merge(x, y, 3, 3); + return zp_cmp_int(z[5], 33); +} + +int test_add_vector(bn_st *N) { + int int_vec_x[3] = {1, 2, 3}; + int int_vec_y[3] = {11, 22, 33}; + zp_vec x = vector_zp_from_int(int_vec_x, 3, N); + zp_vec y = vector_zp_from_int(int_vec_y, 3, N); + zp_vec z = vector_add(x, y, 3); + return zp_cmp_int(z[2], 36); +} + +int test_inner_product(bn_st *N) { + int int_vec_x[3] = {1, 2, 3}; + int int_vec_y[3] = {4, 5, 6}; + zp_vec x = vector_zp_from_int(int_vec_x, 3, N); + zp_vec y = vector_zp_from_int(int_vec_y, 3, N); + + g base; + g_vec gx, gy; + gen(base); + gx = vector_raise(base, x, 3); + gy = vector_raise(base, y, 3); + + gt b, r; + inner_product(r, gx, gy, 3); + bp_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(); + + // Get order. + bn_t N; + pc_get_ord(N); + + // Perform tests. + if (test_zp_from_int(N) != 1) return 1; + if (test_merge_vector(N) != 1) return 1; + if (test_add_vector(N) != 1) return 1; + if (test_inner_product(N) != RLC_EQ) return 1; + + return 0; +} \ No newline at end of file