diff --git a/include/helper.h b/include/helper.h index 0525ee30482c54422ebd448a159a79a99ec2b9b2..6d76777f51633ea877c70bb2d71f43373d4e04b5 100644 --- a/include/helper.h +++ b/include/helper.h @@ -11,6 +11,8 @@ float *fvecs_read(const char *file_path, size_t *d_out, size_t *n_out); int *ivecs_read(const char *file_path, size_t *d_out, size_t *n_out); -Ct *encrypt_data(int *data, Key key, int d, int n); +int *float_to_int(const float *data, size_t size); + +Ct *encrypt_data(const int *data, Key key, size_t d, size_t n); #endif //PPANN_HELPER_H \ No newline at end of file diff --git a/src/helper.cpp b/src/helper.cpp index e9fdb5d440fae99cd320ca6e965f127207e973e8..3723185eb15547745cc5238b9550ce5cd760a9a0 100644 --- a/src/helper.cpp +++ b/src/helper.cpp @@ -39,10 +39,24 @@ int *ivecs_read(const char *file_path, size_t *d_out, size_t *n_out) { return (int *) fvecs_read(file_path, d_out, n_out); } -Ct *encrypt_data(int *data, Key key, int d, int n) { +int *float_to_int(const float *data, size_t size) { + // Get a new list for integer data. + auto int_data = new int[size]; + + // Cast float to integers. + for (int i = 0; i < size; i++) int_data[i] = static_cast<int>(data[i]); + + // Return pointer of the list. + return int_data; +} + +Ct *encrypt_data(const int *data, Key key, size_t d, size_t n) { + // Get a new list for encrypted data. auto *encrypted_data = new Ct[n]; - for (int i = 0; i < n; i++) encrypted_data[i] = enc(key, &data[i * d], d); + // Encrypt each vector. + for (int i = 0; i < n; i++) encrypted_data[i] = enc(key, &data[i * d], static_cast<int>(d)); + // Return pointer of the list. return encrypted_data; -} +} \ No newline at end of file diff --git a/tests/test_helper.cpp b/tests/test_helper.cpp index 74d520579d0e1519cf6e3bb8831e8bb7f28f32c9..1ea3e2305b74d09606147be55eecde978476cf49 100644 --- a/tests/test_helper.cpp +++ b/tests/test_helper.cpp @@ -1,8 +1,5 @@ #include "helper.h" -#include "iostream" -using namespace std; - int test_read_fvecs() { // Set dimensions holders and get the data. size_t d, n; @@ -33,20 +30,43 @@ int test_read_ivecs() { return 1; } +int test_float_to_int() { + // Set dimensions holders and get the data. + size_t d, n; + float *xd = fvecs_read("sift_query.fvecs", &d, &n); + + // Conversion and test. + int *data = float_to_int(xd, d * n); + + // Check for whether the data is correct. + if (data[1] != 3) return 0; + if (data[1279872] != 23) return 0; + + // If everything passes, return 1. + return 1; +} + int test_encrypt() { // Set dimensions holders and get the data. size_t d, n; float *xd = fvecs_read("sift_query.fvecs", &d, &n); - cout << static_cast<int>(xd[1]) << endl; - // - int d_int, n_int; - d_int = static_cast<int>(d); - n_int = static_cast<int>(n); + // Conversion. + int *data = float_to_int(xd, d * n); + + // Cast d to integer. + int d_int = static_cast<int>(d); + + // Encrypt the first two vectors. + Key key = setup(d_int); + Ct *encrypted_data = encrypt_data(data, key, d, 2); + + // Get inner product of the first two vectors. + int result = eval(key, encrypted_data[0], encrypted_data[1], d_int, 200000); + + // Check for whether the data is correct. + if (result != 184094) return 0; - // -// Key key = setup(d_int); -// Ct *encrypted_data = encrypt_data(gt, key, d_int, 1); return 1; } @@ -55,8 +75,10 @@ int main() { core_init(); pc_param_set_any(); -// if (test_read_fvecs() != 1) return 1; -// if (test_read_ivecs() != 1) return 1; + // Perform tests. + if (test_read_fvecs() != 1) return 1; + if (test_read_ivecs() != 1) return 1; + if (test_float_to_int() != 1) return 1; if (test_encrypt() != 1) return 1; return 0; } \ No newline at end of file