Skip to content
Snippets Groups Projects
Commit 44941b4c authored by Weiqi's avatar Weiqi
Browse files

Done with the helpers

parent 16d880a6
Branches
No related tags found
No related merge requests found
......@@ -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
......@@ -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
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment