From 92dfe3c169ff3e6e7916bca5f997087307b67ac8 Mon Sep 17 00:00:00 2001 From: Weiqi <weltch1997@gmail.com> Date: Tue, 14 Nov 2023 14:49:54 -0500 Subject: [PATCH] Refactor --- CMakeLists.txt | 2 +- Dockerfile | 4 +- include/asym_field.h | 37 ++++++++++ include/asym_group.h | 22 ++++++ include/field.h | 37 ---------- include/group.h | 17 ----- include/ipre.h | 4 +- include/sym_field.h | 37 ++++++++++ include/sym_group.h | 17 +++++ include/{matrix.h => sym_matrix.h} | 10 +-- include/{vector.h => sym_vector.h} | 12 ++-- src/CMakeLists.txt | 7 +- src/asym_field.cpp | 75 ++++++++++++++++++++ src/asym_group.cpp | 26 +++++++ src/ipre.cpp | 2 +- src/{field.cpp => sym_field.cpp} | 42 +++++------ src/{group.cpp => sym_group.cpp} | 6 +- src/{matrix.cpp => sym_matrix.cpp} | 20 +++--- src/{vector.cpp => sym_vector.cpp} | 10 +-- tests/CMakeLists.txt | 19 ++++- tests/test_field_asym.cpp | 72 +++++++++++++++++++ tests/{test_field.cpp => test_field_sym.cpp} | 36 +++++----- tests/test_group.cpp | 6 +- tests/test_group_asym.cpp | 50 +++++++++++++ tests/test_matrix.cpp | 2 +- tests/test_vector.cpp | 2 +- 26 files changed, 438 insertions(+), 136 deletions(-) create mode 100644 include/asym_field.h create mode 100644 include/asym_group.h delete mode 100644 include/field.h delete mode 100644 include/group.h create mode 100644 include/sym_field.h create mode 100644 include/sym_group.h rename include/{matrix.h => sym_matrix.h} (79%) rename include/{vector.h => sym_vector.h} (71%) create mode 100644 src/asym_field.cpp create mode 100644 src/asym_group.cpp rename src/{field.cpp => sym_field.cpp} (67%) rename src/{group.cpp => sym_group.cpp} (60%) rename src/{matrix.cpp => sym_matrix.cpp} (89%) rename src/{vector.cpp => sym_vector.cpp} (82%) create mode 100644 tests/test_field_asym.cpp rename tests/{test_field.cpp => test_field_sym.cpp} (64%) create mode 100644 tests/test_group_asym.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ecb5396..7f9d435 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ # Set cmake version. -cmake_minimum_required(VERSION 3.24) +cmake_minimum_required(VERSION 3.22) # Project name and language. project( diff --git a/Dockerfile b/Dockerfile index a77c9ee..340f23a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM ubuntu:latest +FROM ubuntu:23.10 # Update libraries. RUN apt update && apt upgrade -y @@ -7,6 +7,8 @@ RUN apt install -y wget unzip build-essential libgmp-dev libssl-dev cmake # Clean up. RUN apt clean +RUN cmake --version + # Work in the home directory. WORKDIR "/home" diff --git a/include/asym_field.h b/include/asym_field.h new file mode 100644 index 0000000..c932030 --- /dev/null +++ b/include/asym_field.h @@ -0,0 +1,37 @@ +#ifndef PPANN_ASYM_FIELD_H +#define PPANN_ASYM_FIELD_H + +#include <gmp.h> + +extern "C" { +#include "relic_asym/relic.h" +} + +struct ZP_ASYM { + bn_t point{}; + bn_t modular{}; +}; + +ZP_ASYM rand_zp(bn_t modular); + +ZP_ASYM zp_zero(bn_t modular); + +ZP_ASYM zp_one(bn_t modular); + +ZP_ASYM zp_copy(ZP_ASYM x); + +ZP_ASYM zp_from_int(int x, bn_t modular); + +ZP_ASYM zp_add(ZP_ASYM x, ZP_ASYM y); + +ZP_ASYM zp_neg(ZP_ASYM x); + +ZP_ASYM zp_mul(ZP_ASYM x, ZP_ASYM y); + +ZP_ASYM zp_inv(ZP_ASYM x); + +int zp_cmp(ZP_ASYM x, ZP_ASYM y); + +int zp_cmp_int(ZP_ASYM x, int y); + +#endif //PPANN_ASYM_FIELD_H diff --git a/include/asym_group.h b/include/asym_group.h new file mode 100644 index 0000000..9278d8a --- /dev/null +++ b/include/asym_group.h @@ -0,0 +1,22 @@ +#ifndef PPANN_ASYM_GROUP_H +#define PPANN_ASYM_GROUP_H + +#include "asym_field.h" + +typedef g1_t g1_asym; +typedef g2_t g2_asym; +typedef gt_t gt_asym; + +void g1_gen(g1_asym x); + +void g2_gen(g2_asym x); + +void g1_mul(g1_asym r, g1_asym x, ZP_ASYM y); + +void g2_mul(g2_asym r, g2_asym x, ZP_ASYM y); + +void gt_raise(gt_asym r, gt_asym x, ZP_ASYM y); + +void bp_map(gt_asym r, g1_asym x, g2_asym y); + +#endif //PPANN_ASYM_GROUP_H \ No newline at end of file diff --git a/include/field.h b/include/field.h deleted file mode 100644 index 603b800..0000000 --- a/include/field.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef PPANN_FIELD_H -#define PPANN_FIELD_H - -#include <gmp.h> - -extern "C" { -#include "relic_sym/relic.h" -} - -struct ZP { - bn_t point{}; - bn_t modular{}; -}; - -ZP rand_zp(bn_t modular); - -ZP zp_zero(bn_t modular); - -ZP zp_one(bn_t modular); - -ZP zp_copy(ZP x); - -ZP zp_from_int(int x, bn_t modular); - -ZP zp_add(ZP x, ZP y); - -ZP zp_neg(ZP x); - -ZP zp_mul(ZP x, ZP y); - -ZP zp_inv(ZP x); - -int zp_cmp(ZP x, ZP y); - -int zp_cmp_int(ZP x, int y); - -#endif //PPANN_FIELD_H \ No newline at end of file diff --git a/include/group.h b/include/group.h deleted file mode 100644 index 8cb84e4..0000000 --- a/include/group.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef PPANN_GROUP_H -#define PPANN_GROUP_H - -#include "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 y); - -void gt_raise(gt_sym r, gt_sym x, ZP y); - -void bp_map(g_sym a, g_sym b, gt_sym r); - -#endif //PPANN_GROUP_H \ No newline at end of file diff --git a/include/ipre.h b/include/ipre.h index 78c73ff..542f16f 100644 --- a/include/ipre.h +++ b/include/ipre.h @@ -1,8 +1,8 @@ #ifndef PPANN_IPRE_H #define PPANN_IPRE_H -#include "vector.h" -#include "matrix.h" +#include "sym_vector.h" +#include "sym_matrix.h" const int B_SIZE = 6; diff --git a/include/sym_field.h b/include/sym_field.h new file mode 100644 index 0000000..59206c9 --- /dev/null +++ b/include/sym_field.h @@ -0,0 +1,37 @@ +#ifndef PPANN_SYM_FIELD_H +#define PPANN_SYM_FIELD_H + +#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); + +#endif //PPANN_SYM_FIELD_H \ No newline at end of file diff --git a/include/sym_group.h b/include/sym_group.h new file mode 100644 index 0000000..14bfe7c --- /dev/null +++ b/include/sym_group.h @@ -0,0 +1,17 @@ +#ifndef PPANN_SYM_GROUP_H +#define PPANN_SYM_GROUP_H + +#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); + +#endif //PPANN_SYM_GROUP_H \ No newline at end of file diff --git a/include/matrix.h b/include/sym_matrix.h similarity index 79% rename from include/matrix.h rename to include/sym_matrix.h index 4694036..fb31d61 100644 --- a/include/matrix.h +++ b/include/sym_matrix.h @@ -1,9 +1,9 @@ -#ifndef PPANN_MATRIX_H -#define PPANN_MATRIX_H +#ifndef PPANN_SYM_MATRIX_H +#define PPANN_SYM_MATRIX_H -#include "field.h" +#include "sym_field.h" -typedef ZP *zp_mat; +typedef ZP_SYM *zp_mat; zp_mat matrix_zp_from_int(const int *int_mat, int row, int col, bn_t modular); @@ -21,4 +21,4 @@ zp_mat matrix_multiply(zp_mat x, zp_mat y, int row_x, int row_y, int col_y, bn_t zp_mat matrix_inverse(zp_mat x, int size, bn_t modular); -#endif //PPANN_MATRIX_H \ No newline at end of file +#endif //PPANN_SYM_MATRIX_H \ No newline at end of file diff --git a/include/vector.h b/include/sym_vector.h similarity index 71% rename from include/vector.h rename to include/sym_vector.h index 038bf27..4f016e3 100644 --- a/include/vector.h +++ b/include/sym_vector.h @@ -1,10 +1,10 @@ -#ifndef PPANN_VECTOR_H -#define PPANN_VECTOR_H +#ifndef PPANN_SYM_VECTOR_H +#define PPANN_SYM_VECTOR_H -#include "field.h" -#include "group.h" +#include "sym_field.h" +#include "sym_group.h" -typedef ZP *zp_vec; +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); @@ -19,4 +19,4 @@ 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); -#endif //PPANN_VECTOR_H \ No newline at end of file +#endif //PPANN_SYM_VECTOR_H \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4b327cf..40a5780 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,7 +2,12 @@ set(HEADER_LIST "${CMAKE_SOURCE_DIR}/include/") # Add all files to the library. -add_library(ppann_lib field.cpp group.cpp vector.cpp matrix.cpp ipre.cpp hnsw.cpp helper.cpp ${HEADER_LIST}) +add_library(ppann_lib + sym_field.cpp sym_group.cpp sym_vector.cpp sym_matrix.cpp + asym_field.cpp asym_group.cpp + ipre.cpp hnsw.cpp helper.cpp + ${HEADER_LIST} +) # We need this directory, and users of our library will need it too target_include_directories(ppann_lib PUBLIC ../include) diff --git a/src/asym_field.cpp b/src/asym_field.cpp new file mode 100644 index 0000000..b7f0fa2 --- /dev/null +++ b/src/asym_field.cpp @@ -0,0 +1,75 @@ +#include "asym_field.h" + +ZP_ASYM rand_zp(bn_st *modular) { + ZP_ASYM result; + bn_rand_mod(result.point, modular); + bn_copy(result.modular, modular); + return result; +} + +ZP_ASYM zp_zero(bn_st *modular) { + ZP_ASYM result; + bn_set_dig(result.point, 0); + bn_copy(result.modular, modular); + return result; +} + +ZP_ASYM zp_one(bn_st *modular) { + ZP_ASYM result; + bn_set_dig(result.point, 1); + bn_copy(result.modular, modular); + return result; +} + +ZP_ASYM zp_copy(ZP_ASYM x) { + ZP_ASYM result; + bn_copy(result.point, x.point); + bn_copy(result.modular, x.modular); + return result; +} + +ZP_ASYM zp_from_int(int x, bn_st *modular) { + ZP_ASYM result; + bn_set_dig(result.point, x); + bn_copy(result.modular, modular); + return result; +} + +ZP_ASYM zp_add(ZP_ASYM x, ZP_ASYM y) { + ZP_ASYM 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_ASYM zp_neg(ZP_ASYM x) { + ZP_ASYM result; + bn_neg(result.point, x.point); + bn_mod(result.point, result.point, x.modular); + bn_copy(result.modular, x.modular); + return result; +} + +ZP_ASYM zp_mul(ZP_ASYM x, ZP_ASYM y) { + ZP_ASYM 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_ASYM zp_inv(ZP_ASYM x) { + ZP_ASYM result; + bn_mod_inv(result.point, x.point, x.modular); + bn_copy(result.modular, x.modular); + return result; +} + +int zp_cmp(ZP_ASYM x, ZP_ASYM y) { + return bn_cmp(x.point, y.point) == RLC_EQ; +} + +int zp_cmp_int(ZP_ASYM x, int y) { + return bn_cmp_dig(x.point, y) == RLC_EQ; +} \ No newline at end of file diff --git a/src/asym_group.cpp b/src/asym_group.cpp new file mode 100644 index 0000000..5fd16ec --- /dev/null +++ b/src/asym_group.cpp @@ -0,0 +1,26 @@ +#include "asym_group.h" + +void g1_gen(asym_ep_st *x) { + g1_get_gen(x); +} + +void g2_gen(asym_ep2_st *x) { + g2_get_gen(x); +} + +void g1_mul(asym_ep_st *r, asym_ep_st *x, ZP_ASYM y) { + g1_mul(r, x, y.point); +} + +void g2_mul(asym_ep2_st *r, asym_ep2_st *x, ZP_ASYM y) { + g2_mul(r, x, y.point); +} + +void gt_raise(asym_fp6_t *r, asym_fp6_t *x, ZP_ASYM y) { + gt_exp(r, x, y.point); +} + +void bp_map(asym_fp6_t *r, asym_ep_st *x, asym_ep2_st *y) { + pc_map(r, x, y); +} + diff --git a/src/ipre.cpp b/src/ipre.cpp index ebd09f2..cfb015c 100644 --- a/src/ipre.cpp +++ b/src/ipre.cpp @@ -12,7 +12,7 @@ Key setup(int size) { } Ct enc(Key key, const int *message, int size) { - // Declare the returned ciphertext and convert message to ZP. + // Declare the returned ciphertext and convert message to ZP_ASYM. Ct ct{}; zp_vec x = vector_zp_from_int(message, size, key.modular); diff --git a/src/field.cpp b/src/sym_field.cpp similarity index 67% rename from src/field.cpp rename to src/sym_field.cpp index 8ef78ff..5034c13 100644 --- a/src/field.cpp +++ b/src/sym_field.cpp @@ -1,75 +1,75 @@ -#include "field.h" +#include "sym_field.h" -ZP rand_zp(bn_st *modular) { - ZP result; +ZP_SYM rand_zp(bn_st *modular) { + ZP_SYM result; bn_rand_mod(result.point, modular); bn_copy(result.modular, modular); return result; } -ZP zp_zero(bn_st *modular) { - ZP result; +ZP_SYM zp_zero(bn_st *modular) { + ZP_SYM result; bn_set_dig(result.point, 0); bn_copy(result.modular, modular); return result; } -ZP zp_one(bn_st *modular) { - ZP result; +ZP_SYM zp_one(bn_st *modular) { + ZP_SYM result; bn_set_dig(result.point, 1); bn_copy(result.modular, modular); return result; } -ZP zp_copy(ZP x) { - ZP result; +ZP_SYM zp_copy(ZP_SYM x) { + ZP_SYM result; bn_copy(result.point, x.point); bn_copy(result.modular, x.modular); return result; } -ZP zp_from_int(int x, bn_st *modular) { - ZP result; +ZP_SYM zp_from_int(int x, bn_st *modular) { + ZP_SYM result; bn_set_dig(result.point, x); bn_copy(result.modular, modular); return result; } -ZP zp_add(ZP x, ZP y) { - ZP result; +ZP_SYM zp_add(ZP_SYM x, ZP_SYM y) { + ZP_SYM 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 zp_neg(ZP x) { - ZP result; +ZP_SYM zp_neg(ZP_SYM x) { + ZP_SYM result; bn_neg(result.point, x.point); bn_mod(result.point, result.point, x.modular); bn_copy(result.modular, x.modular); return result; } -ZP zp_mul(ZP x, ZP y) { - ZP result; +ZP_SYM zp_mul(ZP_SYM x, ZP_SYM y) { + ZP_SYM 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 zp_inv(ZP x) { - ZP result; +ZP_SYM zp_inv(ZP_SYM x) { + ZP_SYM result; bn_mod_inv(result.point, x.point, x.modular); bn_copy(result.modular, x.modular); return result; } -int zp_cmp(ZP x, ZP y) { +int zp_cmp(ZP_SYM x, ZP_SYM y) { return bn_cmp(x.point, y.point) == RLC_EQ; } -int zp_cmp_int(ZP x, int y) { +int zp_cmp_int(ZP_SYM x, int y) { return bn_cmp_dig(x.point, y) == RLC_EQ; } \ No newline at end of file diff --git a/src/group.cpp b/src/sym_group.cpp similarity index 60% rename from src/group.cpp rename to src/sym_group.cpp index e358225..7eaf29f 100644 --- a/src/group.cpp +++ b/src/sym_group.cpp @@ -1,14 +1,14 @@ -#include "group.h" +#include "sym_group.h" void gen(ep_st *x) { g1_get_gen(x); } -void g_mul(ep_st *r, ep_st *x, ZP y) { +void g_mul(ep_st *r, ep_st *x, ZP_SYM y) { g1_mul(r, x, y.point); } -void gt_raise(fp_t *r, fp_t *x, ZP y) { +void gt_raise(fp_t *r, fp_t *x, ZP_SYM y) { gt_exp(r, x, y.point); } diff --git a/src/matrix.cpp b/src/sym_matrix.cpp similarity index 89% rename from src/matrix.cpp rename to src/sym_matrix.cpp index bf66e09..f17e3d3 100644 --- a/src/matrix.cpp +++ b/src/sym_matrix.cpp @@ -1,8 +1,8 @@ -#include "matrix.h" +#include "sym_matrix.h" 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) * row * col); + x = (zp_mat) malloc(sizeof(ZP_SYM) * 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); @@ -13,7 +13,7 @@ zp_mat matrix_zp_from_int(const int *int_mat, int row, int col, bn_st *modular) zp_mat matrix_zp_rand(int row, int col, bn_st *modular) { zp_mat x; - x = (zp_mat) malloc(sizeof(ZP) * row * col); + x = (zp_mat) malloc(sizeof(ZP_SYM) * row * col); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { x[i * col + j] = rand_zp(modular); @@ -24,7 +24,7 @@ zp_mat matrix_zp_rand(int row, int col, bn_st *modular) { zp_mat matrix_identity(int size, bn_st *modular) { zp_mat x; - x = (zp_mat) malloc(sizeof(ZP) * size * size); + x = (zp_mat) malloc(sizeof(ZP_SYM) * 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); @@ -46,7 +46,7 @@ int matrix_is_identity(zp_mat x, int size) { zp_mat matrix_transpose(zp_mat x, int row, int col) { zp_mat xt; - xt = (zp_mat) malloc(sizeof(ZP) * row * col); + xt = (zp_mat) malloc(sizeof(ZP_SYM) * 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]); @@ -57,7 +57,7 @@ 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 xy; - xy = (zp_mat) malloc(sizeof(ZP) * row * (col_x + col_y)); + xy = (zp_mat) malloc(sizeof(ZP_SYM) * 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]); @@ -70,7 +70,7 @@ 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_st *modular) { - auto xy = (zp_mat) malloc(sizeof(ZP) * row_x * col_y); + auto xy = (zp_mat) malloc(sizeof(ZP_SYM) * row_x * col_y); for (int i = 0; i < row_x; i++) { for (int j = 0; j < col_y; j++) { @@ -89,8 +89,8 @@ zp_mat matrix_inverse(zp_mat x, int size, bn_st *modular) { zp_mat row_echelon = matrix_merge(x, identity, size, size, size); // Declare temp value. - ZP temp_multiplier; - ZP temp_neg; + ZP_SYM temp_multiplier; + ZP_SYM temp_neg; // Bottom left half to all zeros. for (int i = 0; i < size; i++) { @@ -129,7 +129,7 @@ zp_mat matrix_inverse(zp_mat x, int size, bn_st *modular) { // Copy over the output. zp_mat xi; - xi = (zp_mat) malloc(sizeof(ZP) * size * size); + xi = (zp_mat) malloc(sizeof(ZP_SYM) * 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]); diff --git a/src/vector.cpp b/src/sym_vector.cpp similarity index 82% rename from src/vector.cpp rename to src/sym_vector.cpp index 56015e5..ab67b27 100644 --- a/src/vector.cpp +++ b/src/sym_vector.cpp @@ -1,22 +1,22 @@ -#include "vector.h" +#include "sym_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(ZP) * size); + x = (zp_vec) malloc(sizeof(ZP_SYM) * 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(ZP) * size); + x = (zp_vec) malloc(sizeof(ZP_SYM) * 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(ZP) * (size_a + size_b)); + 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]); return r; @@ -24,7 +24,7 @@ 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) { zp_vec r; - r = (zp_vec) malloc(sizeof(ZP) * size); + r = (zp_vec) malloc(sizeof(ZP_SYM) * size); for (int i = 0; i < size; i++) r[i] = zp_add(a[i], b[i]); return r; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a5e1b85..5e70670 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,26 +1,39 @@ # Add tests as separate executables. -add_executable(test_field test_field.cpp) +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 PRIVATE ppann_lib) +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 COMMAND test_field) +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_asym.cpp b/tests/test_field_asym.cpp new file mode 100644 index 0000000..0c94261 --- /dev/null +++ b/tests/test_field_asym.cpp @@ -0,0 +1,72 @@ +#include "asym_field.h" + +int test_zp_zero(bn_st *N) { + ZP_ASYM x = zp_zero(N); + return zp_cmp_int(x, 0); +} + +int test_zp_one(bn_st *N) { + ZP_ASYM x = zp_one(N); + return zp_cmp_int(x, 1); +} + +int test_zp_copy(bn_st *N) { + ZP_ASYM x = zp_from_int(10, N); + ZP_ASYM y = zp_copy(x); + return zp_cmp(x, y); +} + +int test_zp_from_int(bn_st *N) { + ZP_ASYM x = zp_from_int(3, N); + return zp_cmp_int(x, 3); +} + +int test_zp_add(bn_st *N) { + ZP_ASYM x = zp_from_int(10, N); + ZP_ASYM y = zp_from_int(20, N); + ZP_ASYM z = zp_add(x, y); + return zp_cmp_int(z, 30); +} + +int test_zp_neg(bn_st *N) { + ZP_ASYM x = rand_zp(N); + ZP_ASYM y = zp_neg(x); + ZP_ASYM z = zp_add(x, y); + return zp_cmp_int(z, 0); +} + +int test_zp_mul(bn_st *N) { + ZP_ASYM x = zp_from_int(10, N); + ZP_ASYM y = zp_from_int(20, N); + ZP_ASYM z = zp_mul(x, y); + return zp_cmp_int(z, 200); +} + +int test_zp_inv(bn_st *N) { + ZP_ASYM x = rand_zp(N); + ZP_ASYM y = zp_inv(x); + ZP_ASYM 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_field.cpp b/tests/test_field_sym.cpp similarity index 64% rename from tests/test_field.cpp rename to tests/test_field_sym.cpp index c86c2c1..92cc6c7 100644 --- a/tests/test_field.cpp +++ b/tests/test_field_sym.cpp @@ -1,51 +1,51 @@ -#include "field.h" +#include "sym_field.h" int test_zp_zero(bn_st *N) { - ZP x = zp_zero(N); + ZP_SYM x = zp_zero(N); return zp_cmp_int(x, 0); } int test_zp_one(bn_st *N) { - ZP x = zp_one(N); + ZP_SYM x = zp_one(N); return zp_cmp_int(x, 1); } int test_zp_copy(bn_st *N) { - ZP x = zp_from_int(10, N); - ZP y = zp_copy(x); + 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 x = zp_from_int(3, N); + ZP_SYM x = zp_from_int(3, N); return zp_cmp_int(x, 3); } int test_zp_add(bn_st *N) { - ZP x = zp_from_int(10, N); - ZP y = zp_from_int(20, N); - ZP z = zp_add(x, y); + 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 x = rand_zp(N); - ZP y = zp_neg(x); - ZP z = zp_add(x, y); + 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 x = zp_from_int(10, N); - ZP y = zp_from_int(20, N); - ZP z = zp_mul(x, y); + 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 x = rand_zp(N); - ZP y = zp_inv(x); - ZP z = zp_mul(x, y); + 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); } diff --git a/tests/test_group.cpp b/tests/test_group.cpp index d73f4fc..7cdc268 100644 --- a/tests/test_group.cpp +++ b/tests/test_group.cpp @@ -1,4 +1,4 @@ -#include "group.h" +#include "sym_group.h" int test_generator() { g_sym x; @@ -8,8 +8,8 @@ int test_generator() { int test_all(bn_st *N) { // Set integers. - ZP m = zp_from_int(5, N); - ZP n = zp_from_int(25, N); + ZP_SYM m = zp_from_int(5, N); + ZP_SYM n = zp_from_int(25, N); // Declare variables. g_sym a, b; diff --git a/tests/test_group_asym.cpp b/tests/test_group_asym.cpp new file mode 100644 index 0000000..236cdda --- /dev/null +++ b/tests/test_group_asym.cpp @@ -0,0 +1,50 @@ + +#include "asym_group.h" +#include "relic_sym/relic.h" + +int test_all(bn_st *N) { + // Set integers. + ZP_ASYM a = zp_from_int(5, N); + ZP_ASYM b = zp_from_int(6, N); + ZP_ASYM c = zp_from_int(30, N); + + // Declare variables. + g1_asym j, k; + g2_asym m, n; + gt_asym x, y, z; + + core_init(); + + // + g1_gen(j); + g2_gen(m); + + asym_bn_t test; + asym_bn_set_dig(test, 20); + + + g1_mul(k, j, test); + + + // Compare e(g_sym^5, g_sym^5) with e(g_sym, g_sym)^25. + return RLC_EQ; +} + +int main() { + core_clean(); + asym_core_clean(); + + // Init core and setup. + asym_core_init(); + asym_pc_core_init(); + pc_param_set_any(); + + // Get order. + bn_t N; + pc_get_ord(N); + + // Perform tests. + if (test_all(N) != RLC_EQ) return 1; + + return 0; +} \ No newline at end of file diff --git a/tests/test_matrix.cpp b/tests/test_matrix.cpp index 93eca2a..72a5956 100644 --- a/tests/test_matrix.cpp +++ b/tests/test_matrix.cpp @@ -1,4 +1,4 @@ -#include "matrix.h" +#include "sym_matrix.h" int test_zp_from_int(bn_st *N) { int int_mat[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; diff --git a/tests/test_vector.cpp b/tests/test_vector.cpp index e9ab483..4d78d10 100644 --- a/tests/test_vector.cpp +++ b/tests/test_vector.cpp @@ -1,4 +1,4 @@ -#include "vector.h" +#include "sym_vector.h" int test_zp_from_int(bn_st *N) { int int_vec[4] = {1, 2, 3, 4}; -- GitLab