From 5c007cbb39028aeec6d5619826afe43e4c0a2c79 Mon Sep 17 00:00:00 2001 From: Weiqi <weltch1997@gmail.com> Date: Tue, 14 Nov 2023 23:31:40 -0500 Subject: [PATCH] Refactor with better naming conventions --- include/helper.h | 2 +- include/hnsw.h | 2 +- include/{ipre.h => ipre.hpp} | 20 +++---- include/sym_field.h | 34 ------------ include/sym_field.hpp | 38 +++++++++++++ include/sym_group.h | 14 ----- include/sym_group.hpp | 17 ++++++ include/sym_matrix.h | 21 -------- include/sym_matrix.hpp | 23 ++++++++ include/sym_vector.h | 19 ------- include/sym_vector.hpp | 21 ++++++++ src/ipre.cpp | 62 +++++++++++----------- src/sym_field.cpp | 42 +++++++-------- src/sym_group.cpp | 10 ++-- src/sym_matrix.cpp | 100 +++++++++++++++++------------------ src/sym_vector.cpp | 51 +++++++++--------- tests/CMakeLists.txt | 100 +++++++++++++++++++++-------------- tests/test_field_sym.cpp | 72 ------------------------- tests/test_group.cpp | 47 ---------------- tests/test_group_asym.cpp | 2 +- tests/test_hnsw.cpp | 2 +- tests/test_ipre.cpp | 2 +- tests/test_matrix.cpp | 69 ------------------------ tests/test_sym_field.cpp | 72 +++++++++++++++++++++++++ tests/test_sym_group.cpp | 47 ++++++++++++++++ tests/test_sym_matrix.cpp | 72 +++++++++++++++++++++++++ tests/test_sym_vector.cpp | 63 ++++++++++++++++++++++ tests/test_vector.cpp | 63 ---------------------- 28 files changed, 562 insertions(+), 525 deletions(-) rename include/{ipre.h => ipre.hpp} (53%) delete mode 100644 include/sym_field.h create mode 100644 include/sym_field.hpp delete mode 100644 include/sym_group.h create mode 100644 include/sym_group.hpp delete mode 100644 include/sym_matrix.h create mode 100644 include/sym_matrix.hpp delete mode 100644 include/sym_vector.h create mode 100644 include/sym_vector.hpp delete mode 100644 tests/test_field_sym.cpp delete mode 100644 tests/test_group.cpp delete mode 100644 tests/test_matrix.cpp create mode 100644 tests/test_sym_field.cpp create mode 100644 tests/test_sym_group.cpp create mode 100644 tests/test_sym_matrix.cpp create mode 100644 tests/test_sym_vector.cpp delete mode 100644 tests/test_vector.cpp diff --git a/include/helper.h b/include/helper.h index 9fc23b1..2b38ef8 100644 --- a/include/helper.h +++ b/include/helper.h @@ -4,7 +4,7 @@ #include <cstring> #include <sys/stat.h> #include "hnsw.h" -#include "ipre.h" +#include "ipre.hpp" float *fvecs_read(const char *file_path, size_t *d_out, size_t *n_out); diff --git a/include/hnsw.h b/include/hnsw.h index fcd6da7..d90328f 100644 --- a/include/hnsw.h +++ b/include/hnsw.h @@ -6,7 +6,7 @@ #include <algorithm> #include <unordered_map> #include <unordered_set> -#include "ipre.h" +#include "ipre.hpp" using namespace std; diff --git a/include/ipre.h b/include/ipre.hpp similarity index 53% rename from include/ipre.h rename to include/ipre.hpp index 65695b9..7754382 100644 --- a/include/ipre.h +++ b/include/ipre.hpp @@ -1,23 +1,23 @@ #pragma once -#include "sym_vector.h" -#include "sym_matrix.h" +#include "sym_vector.hpp" +#include "sym_matrix.hpp" const int B_SIZE = 6; struct Key { - zp_mat A; - zp_mat B; - zp_mat Bi; - g_sym base; - gt_sym t_base; + symZpMat A; + symZpMat B; + symZpMat Bi; + symG base; + symGt t_base; bn_t modular; }; struct Ct { - g_vec ctx; - g_vec ctk; - g_vec ctc; + symGVec ctx; + symGVec ctk; + symGVec ctc; }; Key setup(int size); diff --git a/include/sym_field.h b/include/sym_field.h deleted file mode 100644 index b4dc474..0000000 --- a/include/sym_field.h +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include <gmp.h> - -extern "C" { -#include "relic_sym/relic.h" -} - -struct ZP_SYM { - bn_t point{}; - bn_t modular{}; -}; - -ZP_SYM rand_zp(bn_t modular); - -ZP_SYM zp_zero(bn_t modular); - -ZP_SYM zp_one(bn_t modular); - -ZP_SYM zp_copy(ZP_SYM x); - -ZP_SYM zp_from_int(int x, bn_t modular); - -ZP_SYM zp_add(ZP_SYM x, ZP_SYM y); - -ZP_SYM zp_neg(ZP_SYM x); - -ZP_SYM zp_mul(ZP_SYM x, ZP_SYM y); - -ZP_SYM zp_inv(ZP_SYM x); - -int zp_cmp(ZP_SYM x, ZP_SYM y); - -int zp_cmp_int(ZP_SYM x, int y); \ No newline at end of file diff --git a/include/sym_field.hpp b/include/sym_field.hpp new file mode 100644 index 0000000..7eb5f32 --- /dev/null +++ b/include/sym_field.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include <gmp.h> + +extern "C" { +#include "relic_sym/relic.h" +} + +// Set specific sym data types. +typedef bn_t symPoint; +struct symZp { + symPoint point{}; + symPoint modular{}; +}; + +namespace sym { + symZp zp_rand(symPoint modular); + + symZp zp_zero(symPoint modular); + + symZp zp_one(symPoint modular); + + symZp zp_copy(symZp x); + + symZp zp_from_int(int x, symPoint modular); + + symZp zp_add(symZp x, symZp y); + + symZp zp_neg(symZp x); + + symZp zp_mul(symZp x, symZp y); + + symZp zp_inv(symZp x); + + int zp_cmp(symZp x, symZp y); + + int zp_cmp_int(symZp x, int y); +} diff --git a/include/sym_group.h b/include/sym_group.h deleted file mode 100644 index 95a3ca7..0000000 --- a/include/sym_group.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include "sym_field.h" - -typedef g1_t g_sym; -typedef gt_t gt_sym; - -void gen(g_sym x); - -void g_mul(g_sym r, g_sym x, ZP_SYM y); - -void gt_raise(gt_sym r, gt_sym x, ZP_SYM y); - -void bp_map(g_sym a, g_sym b, gt_sym r); diff --git a/include/sym_group.hpp b/include/sym_group.hpp new file mode 100644 index 0000000..92d6bb5 --- /dev/null +++ b/include/sym_group.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include "sym_field.hpp" + +typedef g1_t symG; +typedef gt_t symGt; + +namespace sym { + void g_gen(symG x); + + void g_mul(symG r, symG x, symZp y); + + void gt_raise(symGt r, symGt x, symZp y); + + void bp_map(symG a, symG b, symGt r); +} + diff --git a/include/sym_matrix.h b/include/sym_matrix.h deleted file mode 100644 index 5e330ae..0000000 --- a/include/sym_matrix.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include "sym_field.h" - -typedef ZP_SYM *zp_mat; - -zp_mat matrix_zp_from_int(const int *int_mat, int row, int col, bn_t modular); - -zp_mat matrix_zp_rand(int row, int col, bn_t modular); - -zp_mat matrix_identity(int size, bn_t modular); - -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, bn_t modular); - -zp_mat matrix_inverse(zp_mat x, int size, bn_t modular); \ No newline at end of file diff --git a/include/sym_matrix.hpp b/include/sym_matrix.hpp new file mode 100644 index 0000000..8255ae4 --- /dev/null +++ b/include/sym_matrix.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "sym_field.hpp" + +typedef symZp *symZpMat; + +namespace sym { + symZpMat matrix_zp_from_int(const int *int_mat, int row, int col, symPoint modular); + + symZpMat matrix_zp_rand(int row, int col, symPoint modular); + + symZpMat matrix_identity(int size, symPoint modular); + + int matrix_is_identity(symZpMat x, int size); + + symZpMat matrix_transpose(symZpMat x, int row, int col); + + symZpMat matrix_merge(symZpMat x, symZpMat y, int row, int col_x, int col_y); + + symZpMat matrix_multiply(symZpMat x, symZpMat y, int row_x, int row_y, int col_y, symPoint modular); + + symZpMat matrix_inverse(symZpMat x, int size, symPoint modular); +} diff --git a/include/sym_vector.h b/include/sym_vector.h deleted file mode 100644 index 8bd92e8..0000000 --- a/include/sym_vector.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include "sym_field.h" -#include "sym_group.h" - -typedef ZP_SYM *zp_vec; -typedef g_sym *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_sym base, zp_vec x, int size); - -void inner_product(gt_sym r, g_vec a, g_vec b, int size); \ No newline at end of file diff --git a/include/sym_vector.hpp b/include/sym_vector.hpp new file mode 100644 index 0000000..191589f --- /dev/null +++ b/include/sym_vector.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include "sym_field.hpp" +#include "sym_group.hpp" + +typedef symZp *symZpVec; +typedef symG *symGVec; + +namespace sym { + symZpVec vector_zp_from_int(const int *int_vec, int size, symPoint modular); + + symZpVec vector_zp_rand(int size, symPoint modular); + + symZpVec vector_merge(symZpVec a, symZpVec b, int size_a, int size_b); + + symZpVec vector_add(symZpVec a, symZpVec b, int size); + + symGVec vector_raise(symG base, symZpVec x, int size); + + void inner_product(symGt r, symGVec a, symGVec b, int size); +} diff --git a/src/ipre.cpp b/src/ipre.cpp index cfb015c..a3bb5ef 100644 --- a/src/ipre.cpp +++ b/src/ipre.cpp @@ -1,65 +1,65 @@ -#include "ipre.h" +#include "ipre.hpp" Key setup(int size) { Key key{}; pc_get_ord(key.modular); - gen(key.base); - bp_map(key.base, key.base, key.t_base); - key.A = matrix_zp_rand(2, size, key.modular); - key.B = matrix_zp_rand(B_SIZE, B_SIZE, key.modular); - key.Bi = matrix_transpose(matrix_inverse(key.B, B_SIZE, key.modular), B_SIZE, B_SIZE); + sym::g_gen(key.base); + sym::bp_map(key.base, key.base, key.t_base); + key.A = sym::matrix_zp_rand(2, size, key.modular); + key.B = sym::matrix_zp_rand(B_SIZE, B_SIZE, key.modular); + key.Bi = sym::matrix_transpose(sym::matrix_inverse(key.B, B_SIZE, key.modular), B_SIZE, B_SIZE); return key; } Ct enc(Key key, const int *message, int size) { // Declare the returned ciphertext and convert message to ZP_ASYM. Ct ct{}; - zp_vec x = vector_zp_from_int(message, size, key.modular); + symZpVec x = sym::vector_zp_from_int(message, size, key.modular); // Helper values. int one[] = {1}, zero[] = {0}; - zp_vec one_vec = vector_zp_from_int(one, 1, key.modular); - zp_vec zero_vec = vector_zp_from_int(zero, 1, key.modular); + symZpVec one_vec = sym::vector_zp_from_int(one, 1, key.modular); + symZpVec zero_vec = sym::vector_zp_from_int(zero, 1, key.modular); // We generate s and compute sA + x. - zp_vec s = vector_zp_rand(2, key.modular); - zp_vec sA = matrix_multiply(s, key.A, 1, 2, size, key.modular); - zp_vec sAx = vector_add(sA, x, size); - ct.ctx = vector_raise(key.base, sAx, size); + symZpVec s = sym::vector_zp_rand(2, key.modular); + symZpVec sA = sym::matrix_multiply(s, key.A, 1, 2, size, key.modular); + symZpVec sAx = sym::vector_add(sA, x, size); + ct.ctx = sym::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, key.modular); - 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, one_vec, 5, 1); - zp_vec xATs01_B = matrix_multiply(xATs01, key.B, 1, B_SIZE, B_SIZE, key.modular); - ct.ctc = vector_raise(key.base, xATs01_B, B_SIZE); + symZpMat AT = sym::matrix_transpose(key.A, 2, size); + symZpVec xAT = sym::matrix_multiply(x, AT, 1, size, 2, key.modular); + symZpVec xATs = sym::vector_merge(xAT, s, 2, 2); + symZpVec xATs0 = sym::vector_merge(xATs, zero_vec, 4, 1); + symZpVec xATs01 = sym::vector_merge(xATs0, one_vec, 5, 1); + symZpVec xATs01_B = sym::matrix_multiply(xATs01, key.B, 1, B_SIZE, B_SIZE, key.modular); + ct.ctc = sym::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, key.modular); - zp_vec xATsAAT = vector_add(xAT, sAAT, 2); - zp_vec sxATsAAT = vector_merge(s, xATsAAT, 2, 2); - zp_vec sxATsAAT1 = vector_merge(sxATsAAT, one_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, key.modular); - ct.ctk = vector_raise(key.base, sxATsAAT10_Bi, B_SIZE); + symZpVec sAAT = sym::matrix_multiply(sA, AT, 1, size, 2, key.modular); + symZpVec xATsAAT = sym::vector_add(xAT, sAAT, 2); + symZpVec sxATsAAT = sym::vector_merge(s, xATsAAT, 2, 2); + symZpVec sxATsAAT1 = sym::vector_merge(sxATsAAT, one_vec, 4, 1); + symZpVec sxATsAAT10 = sym::vector_merge(sxATsAAT1, zero_vec, 5, 1); + symZpVec sxATsAAT10_Bi = sym::matrix_multiply(sxATsAAT10, key.Bi, 1, B_SIZE, B_SIZE, key.modular); + ct.ctk = sym::vector_raise(key.base, sxATsAAT10_Bi, B_SIZE); return ct; } int eval(Key key, Ct x, Ct y, int size, int bound) { // Decrypt components. - gt_sym xy, ct; - inner_product(xy, x.ctx, y.ctx, size); - inner_product(ct, x.ctc, y.ctk, B_SIZE); + symGt xy, ct; + sym::inner_product(xy, x.ctx, y.ctx, size); + sym::inner_product(ct, x.ctc, y.ctk, B_SIZE); // Decrypt final result. gt_inv(ct, ct); gt_mul(xy, xy, ct); // Get a target group element holder. - gt_sym output; + symGt output; // Iterate through a loop to find correct answer. for (int i = 1; i <= bound; i++) { diff --git a/src/sym_field.cpp b/src/sym_field.cpp index 5034c13..470fc6e 100644 --- a/src/sym_field.cpp +++ b/src/sym_field.cpp @@ -1,75 +1,75 @@ -#include "sym_field.h" +#include "sym_field.hpp" -ZP_SYM rand_zp(bn_st *modular) { - ZP_SYM result; +symZp sym::zp_rand(symPoint modular) { + symZp result; bn_rand_mod(result.point, modular); bn_copy(result.modular, modular); return result; } -ZP_SYM zp_zero(bn_st *modular) { - ZP_SYM result; +symZp sym::zp_zero(symPoint modular) { + symZp result; bn_set_dig(result.point, 0); bn_copy(result.modular, modular); return result; } -ZP_SYM zp_one(bn_st *modular) { - ZP_SYM result; +symZp sym::zp_one(symPoint modular) { + symZp result; bn_set_dig(result.point, 1); bn_copy(result.modular, modular); return result; } -ZP_SYM zp_copy(ZP_SYM x) { - ZP_SYM result; +symZp sym::zp_copy(symZp x) { + symZp result; bn_copy(result.point, x.point); bn_copy(result.modular, x.modular); return result; } -ZP_SYM zp_from_int(int x, bn_st *modular) { - ZP_SYM result; +symZp sym::zp_from_int(int x, symPoint modular) { + symZp result; bn_set_dig(result.point, x); bn_copy(result.modular, modular); return result; } -ZP_SYM zp_add(ZP_SYM x, ZP_SYM y) { - ZP_SYM result; +symZp sym::zp_add(symZp x, symZp y) { + symZp result; bn_add(result.point, x.point, y.point); bn_mod(result.point, result.point, x.modular); bn_copy(result.modular, x.modular); return result; } -ZP_SYM zp_neg(ZP_SYM x) { - ZP_SYM result; +symZp sym::zp_neg(symZp x) { + symZp result; bn_neg(result.point, x.point); bn_mod(result.point, result.point, x.modular); bn_copy(result.modular, x.modular); return result; } -ZP_SYM zp_mul(ZP_SYM x, ZP_SYM y) { - ZP_SYM result; +symZp sym::zp_mul(symZp x, symZp y) { + symZp result; bn_mul(result.point, x.point, y.point); bn_mod(result.point, result.point, x.modular); bn_copy(result.modular, x.modular); return result; } -ZP_SYM zp_inv(ZP_SYM x) { - ZP_SYM result; +symZp sym::zp_inv(symZp x) { + symZp result; bn_mod_inv(result.point, x.point, x.modular); bn_copy(result.modular, x.modular); return result; } -int zp_cmp(ZP_SYM x, ZP_SYM y) { +int sym::zp_cmp(symZp x, symZp y) { return bn_cmp(x.point, y.point) == RLC_EQ; } -int zp_cmp_int(ZP_SYM x, int y) { +int sym::zp_cmp_int(symZp x, int y) { return bn_cmp_dig(x.point, y) == RLC_EQ; } \ No newline at end of file diff --git a/src/sym_group.cpp b/src/sym_group.cpp index 7eaf29f..d8b7301 100644 --- a/src/sym_group.cpp +++ b/src/sym_group.cpp @@ -1,17 +1,17 @@ -#include "sym_group.h" +#include "sym_group.hpp" -void gen(ep_st *x) { +void sym::g_gen(symG x) { g1_get_gen(x); } -void g_mul(ep_st *r, ep_st *x, ZP_SYM y) { +void sym::g_mul(ep_st *r, ep_st *x, symZp y) { g1_mul(r, x, y.point); } -void gt_raise(fp_t *r, fp_t *x, ZP_SYM y) { +void sym::gt_raise(fp_t *r, fp_t *x, symZp y) { gt_exp(r, x, y.point); } -void bp_map(ep_st *a, ep_st *b, fp_t *r) { +void sym::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/sym_matrix.cpp b/src/sym_matrix.cpp index f17e3d3..b0e7580 100644 --- a/src/sym_matrix.cpp +++ b/src/sym_matrix.cpp @@ -1,115 +1,115 @@ -#include "sym_matrix.h" +#include "sym_matrix.hpp" -zp_mat matrix_zp_from_int(const int *int_mat, int row, int col, bn_st *modular) { - zp_mat x; - x = (zp_mat) malloc(sizeof(ZP_SYM) * row * col); +symZpMat sym::matrix_zp_from_int(const int *int_mat, int row, int col, symPoint modular) { + symZpMat x; + x = (symZpMat) malloc(sizeof(symZp) * row * col); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { - x[i * col + j] = zp_from_int(int_mat[i * col + j], modular); + x[i * col + j] = sym::zp_from_int(int_mat[i * col + j], modular); } } return x; } -zp_mat matrix_zp_rand(int row, int col, bn_st *modular) { - zp_mat x; - x = (zp_mat) malloc(sizeof(ZP_SYM) * row * col); +symZpMat sym::matrix_zp_rand(int row, int col, symPoint modular) { + symZpMat x; + x = (symZpMat) malloc(sizeof(symZp) * row * col); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { - x[i * col + j] = rand_zp(modular); + x[i * col + j] = sym::zp_rand(modular); } } return x; } -zp_mat matrix_identity(int size, bn_st *modular) { - zp_mat x; - x = (zp_mat) malloc(sizeof(ZP_SYM) * size * size); +symZpMat sym::matrix_identity(int size, symPoint modular) { + symZpMat x; + x = (symZpMat) malloc(sizeof(symZp) * size * size); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { - if (i == j) x[i * size + j] = zp_one(modular); - else x[i * size + j] = zp_zero(modular); + if (i == j) x[i * size + j] = sym::zp_one(modular); + else x[i * size + j] = sym::zp_zero(modular); } } return x; } -int matrix_is_identity(zp_mat x, int size) { +int sym::matrix_is_identity(symZpMat x, int size) { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { - if (i == j && !zp_cmp_int(x[i * size + j], 1)) return 0; - if (i != j && !zp_cmp_int(x[i * size + j], 0)) return 0; + if (i == j && !sym::zp_cmp_int(x[i * size + j], 1)) return 0; + if (i != j && !sym::zp_cmp_int(x[i * size + j], 0)) return 0; } } return 1; } -zp_mat matrix_transpose(zp_mat x, int row, int col) { - zp_mat xt; - xt = (zp_mat) malloc(sizeof(ZP_SYM) * row * col); +symZpMat sym::matrix_transpose(symZpMat x, int row, int col) { + symZpMat xt; + xt = (symZpMat) malloc(sizeof(symZp) * row * col); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { - xt[j * row + i] = zp_copy(x[i * col + j]); + xt[j * row + i] = sym::zp_copy(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_SYM) * row * (col_x + col_y)); +symZpMat sym::matrix_merge(symZpMat x, symZpMat y, int row, int col_x, int col_y) { + symZpMat xy; + xy = (symZpMat) malloc(sizeof(symZp) * row * (col_x + col_y)); for (int i = 0; i < row; i++) { for (int j = 0; j < col_x; j++) { - xy[i * (col_x + col_y) + j] = zp_copy(x[i * col_x + j]); + xy[i * (col_x + col_y) + j] = sym::zp_copy(x[i * col_x + j]); } for (int j = 0; j < col_y; j++) { - xy[i * (col_x + col_y) + col_x + j] = zp_copy(y[i * col_y + j]); + xy[i * (col_x + col_y) + col_x + j] = sym::zp_copy(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, bn_st *modular) { - auto xy = (zp_mat) malloc(sizeof(ZP_SYM) * row_x * col_y); +symZpMat sym::matrix_multiply(symZpMat x, symZpMat y, int row_x, int row_y, int col_y, symPoint modular) { + auto xy = (symZpMat) malloc(sizeof(symZp) * row_x * col_y); for (int i = 0; i < row_x; i++) { for (int j = 0; j < col_y; j++) { - xy[i * row_y + j] = zp_zero(modular); + xy[i * row_y + j] = sym::zp_zero(modular); for (int k = 0; k < row_y; k++) { - xy[i * col_y + j] = zp_add(xy[i * col_y + j], zp_mul(x[i * row_y + k], y[k * col_y + j])); + xy[i * col_y + j] = sym::zp_add(xy[i * col_y + j], sym::zp_mul(x[i * row_y + k], y[k * col_y + j])); } } } return xy; } -zp_mat matrix_inverse(zp_mat x, int size, bn_st *modular) { +symZpMat sym::matrix_inverse(symZpMat x, int size, symPoint modular) { // Declare the row echelon matrix and generate it. - zp_mat identity = matrix_identity(size, modular); - zp_mat row_echelon = matrix_merge(x, identity, size, size, size); + symZpMat identity = matrix_identity(size, modular); + symZpMat row_echelon = matrix_merge(x, identity, size, size, size); // Declare temp value. - ZP_SYM temp_multiplier; - ZP_SYM temp_neg; + symZp temp_multiplier; + symZp 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_cmp_int(row_echelon[i * 2 * size + j], 1)) { - temp_multiplier = zp_inv(row_echelon[i * 2 * size + i]); + if (i == j && !sym::zp_cmp_int(row_echelon[i * 2 * size + j], 1)) { + temp_multiplier = sym::zp_inv(row_echelon[i * 2 * size + i]); for (int k = i; k < size * 2; k++) { - row_echelon[j * 2 * size + k] = zp_mul(row_echelon[j * 2 * size + k], temp_multiplier); + row_echelon[j * 2 * size + k] = sym::zp_mul(row_echelon[j * 2 * size + k], temp_multiplier); } } - if (i == j && zp_cmp_int(row_echelon[i * 2 * size + j], 0)) break; + if (i == j && sym::zp_cmp_int(row_echelon[i * 2 * size + j], 0)) break; if (i != j) { - temp_multiplier = zp_copy(row_echelon[j * 2 * size + i]); + temp_multiplier = sym::zp_copy(row_echelon[j * 2 * size + i]); for (int k = i; k < size * 2; k++) { - temp_neg = zp_mul(temp_multiplier, row_echelon[i * 2 * size + k]); - temp_neg = zp_neg(temp_neg); - row_echelon[j * 2 * size + k] = zp_add(row_echelon[j * 2 * size + k], temp_neg); + temp_neg = sym::zp_mul(temp_multiplier, row_echelon[i * 2 * size + k]); + temp_neg = sym::zp_neg(temp_neg); + row_echelon[j * 2 * size + k] = sym::zp_add(row_echelon[j * 2 * size + k], temp_neg); } } } @@ -118,21 +118,21 @@ zp_mat matrix_inverse(zp_mat x, int size, bn_st *modular) { // Top right half to all zeros. for (int i = size - 1; i > 0; i--) { for (int j = i - 1; j >= 0; j--) { - temp_multiplier = zp_copy(row_echelon[j * 2 * size + i]); + temp_multiplier = sym::zp_copy(row_echelon[j * 2 * size + i]); for (int k = i; k < size * 2; k++) { - temp_neg = zp_mul(temp_multiplier, row_echelon[i * 2 * size + k]); - temp_neg = zp_neg(temp_neg); - row_echelon[j * 2 * size + k] = zp_add(row_echelon[j * 2 * size + k], temp_neg); + temp_neg = sym::zp_mul(temp_multiplier, row_echelon[i * 2 * size + k]); + temp_neg = sym::zp_neg(temp_neg); + row_echelon[j * 2 * size + k] = sym::zp_add(row_echelon[j * 2 * size + k], temp_neg); } } } // Copy over the output. - zp_mat xi; - xi = (zp_mat) malloc(sizeof(ZP_SYM) * size * size); + symZpMat xi; + xi = (symZpMat) malloc(sizeof(symZp) * size * size); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { - xi[i * size + j] = zp_copy(row_echelon[i * 2 * size + size + j]); + xi[i * size + j] = sym::zp_copy(row_echelon[i * 2 * size + size + j]); } } return xi; diff --git a/src/sym_vector.cpp b/src/sym_vector.cpp index ab67b27..617c71d 100644 --- a/src/sym_vector.cpp +++ b/src/sym_vector.cpp @@ -1,45 +1,46 @@ -#include "sym_vector.h" +#include "sym_vector.hpp" -zp_vec vector_zp_from_int(const int *int_vec, int size, bn_st *modular) { - zp_vec x; - x = (zp_vec) malloc(sizeof(ZP_SYM) * size); - for (int i = 0; i < size; i++) x[i] = zp_from_int(int_vec[i], modular); +symZpVec sym::vector_zp_from_int(const int *int_vec, int size, symPoint modular) { + symZpVec x; + x = (symZpVec) malloc(sizeof(symZp) * size); + for (int i = 0; i < size; i++) x[i] = sym::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(ZP_SYM) * size); - for (int i = 0; i < size; i++) x[i] = rand_zp(modular); +symZpVec sym::vector_zp_rand(int size, symPoint modular) { + symZpVec x; + x = (symZpVec) malloc(sizeof(symZp) * size); + for (int i = 0; i < size; i++) x[i] = sym::zp_rand(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(ZP_SYM) * (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]); +symZpVec sym::vector_merge(symZpVec a, symZpVec b, int size_a, int size_b) { + symZpVec r; + r = (symZpVec) malloc(sizeof(symZp) * (size_a + size_b)); + for (int i = 0; i < size_a; i++) r[i] = sym::zp_copy(a[i]); + for (int i = 0; i < size_b; i++) r[i + size_a] = sym::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(ZP_SYM) * size); - for (int i = 0; i < size; i++) r[i] = zp_add(a[i], b[i]); +symZpVec sym::vector_add(symZpVec a, symZpVec b, int size) { + symZpVec r; + r = (symZpVec) malloc(sizeof(symZp) * size); + for (int i = 0; i < size; i++) r[i] = sym::zp_add(a[i], b[i]); return r; } -g_vec vector_raise(g_sym base, zp_vec x, int size) { - g_vec r; - r = (g_vec) malloc(sizeof(g_sym) * size); - for (int i = 0; i < size; i++) g_mul(r[i], base, x[i]); +symGVec sym::vector_raise(symG base, symZpVec x, int size) { + symGVec r; + r = (symGVec) malloc(sizeof(symG) * size); + for (int i = 0; i < size; i++) sym::g_mul(r[i], base, x[i]); return r; } -void inner_product(gt_sym r, g_vec a, g_vec b, int size) { - gt_sym temp; +void sym::inner_product(symGt r, symGVec a, symGVec b, int size) { + symGt temp; gt_set_unity(r); - for (int i = 0; i < size; i++) { pc_map(temp, a[i], b[i]); + 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/CMakeLists.txt b/tests/CMakeLists.txt index 5e70670..dd4d491 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,39 +1,61 @@ -# Add tests as separate executables. -add_executable(test_field_sym test_field_sym.cpp) -add_executable(test_group test_group.cpp) -add_executable(test_vector test_vector.cpp) -add_executable(test_matrix test_matrix.cpp) - -add_executable(test_field_asym test_field_asym.cpp) -add_executable(test_group_asym test_group_asym.cpp) - - -add_executable(test_ipre test_ipre.cpp) -add_executable(test_helper test_helper.cpp) -add_executable(test_hnsw test_hnsw.cpp) - -# Link tests to the main library. -target_link_libraries(test_field_sym PRIVATE ppann_lib) -target_link_libraries(test_group PRIVATE ppann_lib) -target_link_libraries(test_vector PRIVATE ppann_lib) -target_link_libraries(test_matrix PRIVATE ppann_lib) - -target_link_libraries(test_field_asym PRIVATE ppann_lib) -target_link_libraries(test_group_asym PRIVATE ppann_lib) - -target_link_libraries(test_ipre PRIVATE ppann_lib) -target_link_libraries(test_helper PRIVATE ppann_lib) -target_link_libraries(test_hnsw PRIVATE ppann_lib) - -# Register the previous tests. -add_test(NAME test_field_sym COMMAND test_field_sym) -add_test(NAME test_group COMMAND test_group) -add_test(NAME test_vector COMMAND test_vector) -add_test(NAME test_matrix COMMAND test_matrix) - -add_test(NAME test_field_asym COMMAND test_field_asym) -add_test(NAME test_group_asym COMMAND test_group_asym) - -add_test(NAME test_ipre COMMAND test_ipre) -add_test(NAME test_helper COMMAND test_helper WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/data") -add_test(NAME test_hnsw COMMAND test_hnsw WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/data") \ No newline at end of file +# ----------------------------------------------------- +add_executable(test_sym_field test_sym_field.cpp) +target_link_libraries(test_sym_field PRIVATE ppann_lib) +add_test(NAME test_sym_field COMMAND test_sym_field) + +# ----------------------------------------------------- +add_executable(test_sym_group test_sym_group.cpp) +target_link_libraries(test_sym_group PRIVATE ppann_lib) +add_test(NAME test_sym_group COMMAND test_sym_group) + +# ----------------------------------------------------- +add_executable(test_sym_matrix test_sym_matrix.cpp) +target_link_libraries(test_sym_matrix PRIVATE ppann_lib) +add_test(NAME test_sym_matrix COMMAND test_sym_matrix) + +# ----------------------------------------------------- +add_executable(test_sym_vector test_sym_vector.cpp) +target_link_libraries(test_sym_vector PRIVATE ppann_lib) +add_test(NAME test_sym_vector COMMAND test_sym_vector) + + + +## Add tests as separate executables. +#add_executable(test_field_sym test_field_sym.cpp) +#add_executable(test_group test_group.cpp) +#add_executable(test_vector test_vector.cpp) +#add_executable(test_matrix test_matrix.cpp) +# +#add_executable(test_field_asym test_field_asym.cpp) +#add_executable(test_group_asym test_group_asym.cpp) +# +# +#add_executable(test_ipre test_ipre.cpp) +#add_executable(test_helper test_helper.cpp) +#add_executable(test_hnsw test_hnsw.cpp) +# +## Link tests to the main library. +#target_link_libraries(test_field_sym PRIVATE ppann_lib) +#target_link_libraries(test_group PRIVATE ppann_lib) +#target_link_libraries(test_vector PRIVATE ppann_lib) +#target_link_libraries(test_matrix PRIVATE ppann_lib) +# +#target_link_libraries(test_field_asym PRIVATE ppann_lib) +#target_link_libraries(test_group_asym PRIVATE ppann_lib) +# +#target_link_libraries(test_ipre PRIVATE ppann_lib) +#target_link_libraries(test_helper PRIVATE ppann_lib) +#target_link_libraries(test_hnsw PRIVATE ppann_lib) +# +## Register the previous tests. +#add_test(NAME test_field_sym COMMAND test_field_sym) +#add_test(NAME test_group COMMAND test_group) +#add_test(NAME test_vector COMMAND test_vector) +#add_test(NAME test_matrix COMMAND test_matrix) +# +#add_test(NAME test_field_asym COMMAND test_field_asym) +#add_test(NAME test_group_asym COMMAND test_group_asym) +# +#add_test(NAME test_ipre COMMAND test_ipre) +#add_test(NAME test_helper COMMAND test_helper WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/data") +#add_test(NAME test_hnsw COMMAND test_hnsw WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/data") \ No newline at end of file diff --git a/tests/test_field_sym.cpp b/tests/test_field_sym.cpp deleted file mode 100644 index 92cc6c7..0000000 --- a/tests/test_field_sym.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include "sym_field.h" - -int test_zp_zero(bn_st *N) { - ZP_SYM x = zp_zero(N); - return zp_cmp_int(x, 0); -} - -int test_zp_one(bn_st *N) { - ZP_SYM x = zp_one(N); - return zp_cmp_int(x, 1); -} - -int test_zp_copy(bn_st *N) { - ZP_SYM x = zp_from_int(10, N); - ZP_SYM y = zp_copy(x); - return zp_cmp(x, y); -} - -int test_zp_from_int(bn_st *N) { - ZP_SYM x = zp_from_int(3, N); - return zp_cmp_int(x, 3); -} - -int test_zp_add(bn_st *N) { - ZP_SYM x = zp_from_int(10, N); - ZP_SYM y = zp_from_int(20, N); - ZP_SYM z = zp_add(x, y); - return zp_cmp_int(z, 30); -} - -int test_zp_neg(bn_st *N) { - ZP_SYM x = rand_zp(N); - ZP_SYM y = zp_neg(x); - ZP_SYM z = zp_add(x, y); - return zp_cmp_int(z, 0); -} - -int test_zp_mul(bn_st *N) { - ZP_SYM x = zp_from_int(10, N); - ZP_SYM y = zp_from_int(20, N); - ZP_SYM z = zp_mul(x, y); - return zp_cmp_int(z, 200); -} - -int test_zp_inv(bn_st *N) { - ZP_SYM x = rand_zp(N); - ZP_SYM y = zp_inv(x); - ZP_SYM 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(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.cpp b/tests/test_group.cpp deleted file mode 100644 index 7cdc268..0000000 --- a/tests/test_group.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include "sym_group.h" - -int test_generator() { - g_sym x; - gen(x); - return g1_is_valid(x); -} - -int test_all(bn_st *N) { - // Set integers. - ZP_SYM m = zp_from_int(5, N); - ZP_SYM n = zp_from_int(25, N); - - // Declare variables. - g_sym a, b; - gt_sym x, y, z; - - // Get generator g_sym and find g_sym^5. - gen(a); - g_mul(b, a, m); - - // Get e(g_sym, g_sym) and e(g_sym^5, g_sym^5). - bp_map(a, a, x); - bp_map(b, b, y); - - // Get e(g_sym, g_sym)^25. - gt_raise(z, x, n); - - // Compare e(g_sym^5, g_sym^5) with e(g_sym, g_sym)^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_group_asym.cpp b/tests/test_group_asym.cpp index 619a5a2..8272552 100644 --- a/tests/test_group_asym.cpp +++ b/tests/test_group_asym.cpp @@ -21,7 +21,7 @@ // g1_mul(k, j, test); // // -// // Compare e(g_sym^5, g_sym^5) with e(g_sym, g_sym)^25. +// // Compare e(symG^5, symG^5) with e(symG, symG)^25. // return RLC_EQ; //} diff --git a/tests/test_hnsw.cpp b/tests/test_hnsw.cpp index 914fb52..f97b7c4 100644 --- a/tests/test_hnsw.cpp +++ b/tests/test_hnsw.cpp @@ -1,4 +1,4 @@ -#include "ipre.h" +#include "ipre.hpp" #include "hnsw.h" #include "helper.h" diff --git a/tests/test_ipre.cpp b/tests/test_ipre.cpp index 6f90891..17f49f3 100644 --- a/tests/test_ipre.cpp +++ b/tests/test_ipre.cpp @@ -1,4 +1,4 @@ -#include "ipre.h" +#include "ipre.hpp" int test_scheme() { // Set x, y vectors. diff --git a/tests/test_matrix.cpp b/tests/test_matrix.cpp deleted file mode 100644 index 72a5956..0000000 --- a/tests/test_matrix.cpp +++ /dev/null @@ -1,69 +0,0 @@ -#include "sym_matrix.h" - -int test_zp_from_int(bn_st *N) { - int int_mat[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; - zp_mat x = matrix_zp_from_int(int_mat, 3, 3, N); - return zp_cmp_int(x[8], 9); -} - -int test_transpose(bn_st *N) { - int row = 3, col = 3; - zp_mat x = matrix_zp_rand(row, col, N); - zp_mat xt = matrix_transpose(x, row, col); - return zp_cmp(xt[col - 1], x[2 * row]); -} - -int test_identity(bn_st *N) { - int size = 10; - zp_mat x = matrix_identity(size, N); - return matrix_is_identity(x, size); -} - -int test_merge(bn_st *N) { - int size = 10; - zp_mat x = matrix_zp_rand(size, size, N); - zp_mat y = matrix_identity(size, N); - zp_mat xy = matrix_merge(x, y, size, size, size); - return zp_cmp(x[2 * size + 1], xy[4 * size + 1]); -} - -int test_multiply_vector(bn_st *N) { - 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 = matrix_zp_from_int(mat_x, 1, 5, N); - zp_mat y = matrix_zp_from_int(mat_y, 5, 3, N); - zp_mat xy = matrix_multiply(x, y, 1, 5, 3, N); - return zp_cmp_int(xy[2], 450); -} - -int test_inverse(bn_st *N) { - int size = 128; - zp_mat x = matrix_zp_rand(size, size, N); - zp_mat xi = matrix_inverse(x, size, N); - zp_mat r = matrix_multiply(x, xi, size, size, size, N); - return matrix_is_identity(r, size); -} - -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_transpose(N) != 1) return 1; - if (test_identity(N) != 1) return 1; - if (test_merge(N) != 1) return 1; - if (test_multiply_vector(N) != 1) return 1; - if (test_inverse(N) != 1) return 1; - - return 0; -} \ No newline at end of file diff --git a/tests/test_sym_field.cpp b/tests/test_sym_field.cpp new file mode 100644 index 0000000..981227d --- /dev/null +++ b/tests/test_sym_field.cpp @@ -0,0 +1,72 @@ +#include "sym_field.hpp" + +int test_zp_zero(bn_st *N) { + symZp x = sym::zp_zero(N); + return sym::zp_cmp_int(x, 0); +} + +int test_zp_one(bn_st *N) { + symZp x = sym::zp_one(N); + return sym::zp_cmp_int(x, 1); +} + +int test_zp_copy(bn_st *N) { + symZp x = sym::zp_from_int(10, N); + symZp y = sym::zp_copy(x); + return sym::zp_cmp(x, y); +} + +int test_zp_from_int(bn_st *N) { + symZp x = sym::zp_from_int(3, N); + return sym::zp_cmp_int(x, 3); +} + +int test_zp_add(bn_st *N) { + symZp x = sym::zp_from_int(10, N); + symZp y = sym::zp_from_int(20, N); + symZp z = sym::zp_add(x, y); + return sym::zp_cmp_int(z, 30); +} + +int test_zp_neg(bn_st *N) { + symZp x = sym::zp_rand(N); + symZp y = sym::zp_neg(x); + symZp z = sym::zp_add(x, y); + return sym::zp_cmp_int(z, 0); +} + +int test_zp_mul(bn_st *N) { + symZp x = sym::zp_from_int(10, N); + symZp y = sym::zp_from_int(20, N); + symZp z = sym::zp_mul(x, y); + return sym::zp_cmp_int(z, 200); +} + +int test_zp_inv(bn_st *N) { + symZp x = sym::zp_rand(N); + symZp y = sym::zp_inv(x); + symZp z = sym::zp_mul(x, y); + return sym::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(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_sym_group.cpp b/tests/test_sym_group.cpp new file mode 100644 index 0000000..04ae5bf --- /dev/null +++ b/tests/test_sym_group.cpp @@ -0,0 +1,47 @@ +#include "sym_group.hpp" + +int test_generator() { + symG x; + sym::g_gen(x); + return g1_is_valid(x); +} + +int test_all(bn_st *N) { + // Set integers. + symZp m = sym::zp_from_int(5, N); + symZp n = sym::zp_from_int(25, N); + + // Declare variables. + symG a, b; + symGt x, y, z; + + // Get generator symG and find symG^5. + sym::g_gen(a); + sym::g_mul(b, a, m); + + // Get e(symG, symG) and e(symG^5, symG^5). + sym::bp_map(a, a, x); + sym::bp_map(b, b, y); + + // Get e(symG, symG)^25. + sym::gt_raise(z, x, n); + + // Compare e(symG^5, symG^5) with e(symG, symG)^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_sym_matrix.cpp b/tests/test_sym_matrix.cpp new file mode 100644 index 0000000..45447a3 --- /dev/null +++ b/tests/test_sym_matrix.cpp @@ -0,0 +1,72 @@ +#include "sym_matrix.hpp" + +int test_zp_from_int(bn_st *N) { + int int_mat[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + symZpMat x = sym::matrix_zp_from_int(int_mat, 3, 3, N); + return sym::zp_cmp_int(x[8], 9); +} + +int test_transpose(bn_st *N) { + int row = 3, col = 3; + symZpMat x = sym::matrix_zp_rand(row, col, N); + symZpMat xt = sym::matrix_transpose(x, row, col); + return sym::zp_cmp(xt[col - 1], x[2 * row]); +} + +int test_identity(bn_st *N) { + int size = 10; + symZpMat x = sym::matrix_identity(size, N); + return sym::matrix_is_identity(x, size); +} + +int test_merge(bn_st *N) { + int size = 10; + symZpMat x = sym::matrix_zp_rand(size, size, N); + symZpMat y = sym::matrix_identity(size, N); + symZpMat xy = sym::matrix_merge(x, y, size, size, size); + return sym::zp_cmp(x[2 * size + 1], xy[4 * size + 1]); +} + +int test_multiply_vector(bn_st *N) { + 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 + }; + + symZpMat x = sym::matrix_zp_from_int(mat_x, 1, 5, N); + symZpMat y = sym::matrix_zp_from_int(mat_y, 5, 3, N); + symZpMat xy = sym::matrix_multiply(x, y, 1, 5, 3, N); + return sym::zp_cmp_int(xy[2], 450); +} + +int test_inverse(bn_st *N) { + int size = 10; + symZpMat x = sym::matrix_zp_rand(size, size, N); + symZpMat xi = sym::matrix_inverse(x, size, N); + symZpMat r = sym::matrix_multiply(x, xi, size, size, size, N); + return sym::matrix_is_identity(r, size); +} + +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_transpose(N) != 1) return 1; + if (test_identity(N) != 1) return 1; + if (test_merge(N) != 1) return 1; + if (test_multiply_vector(N) != 1) return 1; + if (test_inverse(N) != 1) return 1; + + return 0; +} \ No newline at end of file diff --git a/tests/test_sym_vector.cpp b/tests/test_sym_vector.cpp new file mode 100644 index 0000000..50040bb --- /dev/null +++ b/tests/test_sym_vector.cpp @@ -0,0 +1,63 @@ +#include "sym_vector.hpp" + +int test_zp_from_int(bn_st *N) { + int int_vec[4] = {1, 2, 3, 4}; + symZpVec x = sym::vector_zp_from_int(int_vec, 4, N); + return sym::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}; + symZpVec x = sym::vector_zp_from_int(int_vec_x, 3, N); + symZpVec y = sym::vector_zp_from_int(int_vec_y, 3, N); + symZpVec z = sym::vector_merge(x, y, 3, 3); + return sym::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}; + symZpVec x = sym::vector_zp_from_int(int_vec_x, 3, N); + symZpVec y = sym::vector_zp_from_int(int_vec_y, 3, N); + symZpVec z = sym::vector_add(x, y, 3); + return sym::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}; + symZpVec x = sym::vector_zp_from_int(int_vec_x, 3, N); + symZpVec y = sym::vector_zp_from_int(int_vec_y, 3, N); + + symG base; + symGVec gx, gy; + sym::g_gen(base); + gx = sym::vector_raise(base, x, 3); + gy = sym::vector_raise(base, y, 3); + + symGt b, r; + sym::inner_product(r, gx, gy, 3); + sym::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 diff --git a/tests/test_vector.cpp b/tests/test_vector.cpp deleted file mode 100644 index 4d78d10..0000000 --- a/tests/test_vector.cpp +++ /dev/null @@ -1,63 +0,0 @@ -#include "sym_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_sym base; - g_vec gx, gy; - gen(base); - gx = vector_raise(base, x, 3); - gy = vector_raise(base, y, 3); - - gt_sym 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 -- GitLab