diff --git a/CMakeLists.txt b/CMakeLists.txt index ecb5396bca45e725e74fb516341572bbc5c83a62..7f9d435255de7c72f1f0832f31e30b40aa926599 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 a77c9eecc0c71574616a8d3168111a9d7a739d08..340f23aeaef6586789737c008bf9f7b27f6488d6 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 0000000000000000000000000000000000000000..c932030b9076ffb3ed2c67e5f47bbe6ac4ddd84e --- /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 0000000000000000000000000000000000000000..9278d8a1d3ad581ae7843ac86617c2e480882128 --- /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 603b8006b66700c80794a0681cb3240958d61f90..0000000000000000000000000000000000000000 --- 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 8cb84e417bcc978d1159d12291f1d4028e24cd03..0000000000000000000000000000000000000000 --- 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 78c73ff0d140d9260c2d741cbb8472cebe0b4a50..542f16fc3e4915db1b116b9977ea28075526fe1f 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 0000000000000000000000000000000000000000..59206c9f69d95f0cbcaac82b52b412b22c2f78ee --- /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 0000000000000000000000000000000000000000..14bfe7c5176aa92c255560de0808a70c9061cfa4 --- /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 4694036a74b8af6a6572d1ed4e2ed0a9aa7136d1..fb31d616002ab9e4a5fee12ca27e30109f22fc25 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 038bf2728e751645fe55e19e3f2a6c4bd1862566..4f016e32a97a52db7458663f2d0aea46a2344bbc 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 4b327cfceadb76907395bc88a6d1c7e976f36351..40a57808a0296038d841ff26c454e1b963308829 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 0000000000000000000000000000000000000000..b7f0fa2fe1e371bd74a0cb8146fc3be46b582ff1 --- /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 0000000000000000000000000000000000000000..5fd16eceb93f9da88d1a7111ccd2eafb719a08a7 --- /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 ebd09f2143579ad96243f86d840ec615e3842bf4..cfb015c5ffee8d8cb6d0b8d5e9f98df9cd6c5716 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 8ef78ffb01ff2b04ccab4cfdea16ff7ea754c327..5034c13200cff36b7a4a7a5e8a66826de0262863 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 e3582257f144e2077a9c57e503828269dc5a7fad..7eaf29f1a77d073b4895064875bf50ec8708839d 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 bf66e090792662d3af5b912fa99abe2fe972ad9d..f17e3d326f8fd85b92f57e9d8fa08f0ba6c84288 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 56015e51faad13b992835c738de0f6d4c2107107..ab67b27926d9d9b097a1d0128bf4b2262d6562f3 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 a5e1b8578a897302bafe3dac1b6b7717f0626a44..5e70670c4b8c53d49e780a6e4a286095199235a1 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 0000000000000000000000000000000000000000..0c942611b710782b8a7319b9c1441ec5e4407de8 --- /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 c86c2c148d35010a042806574e8bc97ed239d67e..92cc6c73b2163c6cbb0e8fd91d9f8d2c0dbfe71c 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 d73f4fcd220903f2cdfb35e6776a450e21454870..7cdc2687f4fa13d195ca58ceb3f0dc5abee80d34 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 0000000000000000000000000000000000000000..236cdda82a6f01dc8448956e5f0bc8ef1c542b79 --- /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 93eca2a32281718062639aa9fece43cb1d3db0c8..72a5956483fab8a1a04a1d0d799534cba03deb04 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 e9ab4835803350629aa565c77e95b60b71dfd48e..4d78d1095b98064ac710a6033d48578ef5f8a66e 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};