diff --git a/.clang-format b/.clang-format index 7c75acc63432c4aa9a8d6c5063bc0933a834c6fe..714255e46d82b4dfc443855326cc783e09a005a4 100644 --- a/.clang-format +++ b/.clang-format @@ -4,7 +4,8 @@ AllowShortCaseLabelsOnASingleLine: false AllowShortIfStatementsOnASingleLine: true AllowShortLoopsOnASingleLine: true DerivePointerBinding: false -TabWidth: 2 +TabWidth: 4 +IndentWidth: 4 ForEachMacros: ["foreach"] SpaceBeforeParens: ControlStatementsExceptForEachMacros diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000000000000000000000000000000000000..8fc0397ad9a8286dbe270cee252c7c548b696448 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "cSpell.words": [ + "Asym", + "IPRE", + "PPANN" + ] +} diff --git a/ppann/Makefile b/ppann/Makefile index d892821f260d71168603dd3f5923dd7ea46ef84d..8c97908e529c6755567ef4e3ad535323356855cd 100644 --- a/ppann/Makefile +++ b/ppann/Makefile @@ -40,7 +40,7 @@ TESTS = $(ENTITIES) TESTBIN = $(addprefix $(BDIR)/test-, $(TESTS)) JUNITS= $(foreach test, $(TESTS), bin/test-$(test)?--gtest_output=xml:junit-$(test).xml) -BENCHMARKS = +BENCHMARKS = ipre BENCHMARKSBIN = $(addprefix $(BDIR)/benchmark-, $(BENCHMARKS)) INTEGRATION = @@ -109,10 +109,12 @@ run-tests: LDLIBS += $(LDTESTLIBS) run-tests: $(TESTBIN) $(addsuffix &&, $(TESTBIN)) echo Tests passed! +run-benchmarks: LDLIBS += $(LDTESTLIBS) run-benchmarks: $(BENCHMARKSBIN) $(addsuffix &&, $(BENCHMARKSBIN)) echo Benchmarks completed! run-integration: CPPFLAGS += -DTESTING +run-integration: LDLIBS += $(LDTESTLIBS) run-integration: $(INTEGRATIONBIN) $(addsuffix &&, $(INTEGRATIONBIN)) echo Tests passed! diff --git a/ppann/benchmark/benchmark-ipre.cpp b/ppann/benchmark/benchmark-ipre.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c24d33e273f92021e172e882c322749e9d4a1db --- /dev/null +++ b/ppann/benchmark/benchmark-ipre.cpp @@ -0,0 +1,79 @@ +#include "ipre.hpp" + +#include <benchmark/benchmark.h> + +namespace PPANN { +class IPREBenchmark : public ::benchmark::Fixture { + protected: + IPREBenchmark() { + // Init core and setup. + core_init(); + pc_param_set_any(); + } +}; + +BENCHMARK_DEFINE_F(IPREBenchmark, Setup) +(benchmark::State& state) { + auto size = (int)state.range(0); + for (auto _ : state) { + benchmark::DoNotOptimize(setup(size)); + } +} + +BENCHMARK_DEFINE_F(IPREBenchmark, Encrypt) +(benchmark::State& state) { + auto size = (int)state.range(0); + + Key key = setup(size); + int* x = new int[size]; + for (size_t i = 0; i < size; i++) { + x[i] = i; + } + + for (auto _ : state) { + benchmark::DoNotOptimize(enc(key, x, size)); + } +} + +BENCHMARK_DEFINE_F(IPREBenchmark, Eval) +(benchmark::State& state) { + // TODO(weiqi): figure out how to properly benchmark given the `bound` + // argument. + + int size = 10; + int bound = 150; + + int x[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; + int y[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 100}; + // Initialize the scheme. + Key key = setup(size); + // Encrypt the messages. + Ct ct_x = enc(key, x, size); + Ct ct_y = enc(key, y, size); + + for (auto _ : state) { + benchmark::DoNotOptimize(eval(key, ct_x, ct_y, size, bound)); + } +} + +BENCHMARK_REGISTER_F(IPREBenchmark, Setup) + ->RangeMultiplier(2) + ->Range(1, 1 << 10) + ->Complexity() + ->Iterations(1 << 10) + ->Unit(benchmark::kMicrosecond); + +BENCHMARK_REGISTER_F(IPREBenchmark, Encrypt) + ->RangeMultiplier(2) + ->Range(1, 1 << 10) + ->Complexity() + ->MinTime(10) + ->Unit(benchmark::kMillisecond); + +BENCHMARK_REGISTER_F(IPREBenchmark, Eval) + ->MinTime(10) + ->Unit(benchmark::kMillisecond); + +} // namespace PPANN + +BENCHMARK_MAIN(); diff --git a/ppann/include/asym-field.hpp b/ppann/include/asym-field.hpp index 4db83ada7f0b71ad56c3bddb49329a040c303ae9..9547fb2716782645beb8a76dcf924cc4759b9f07 100644 --- a/ppann/include/asym-field.hpp +++ b/ppann/include/asym-field.hpp @@ -9,8 +9,8 @@ extern "C" { // Set specific asym data types. typedef bn_t asymPoint; struct asymZp { - asymPoint point{}; - asymPoint modular{}; + asymPoint point{}; + asymPoint modular{}; }; namespace asym { diff --git a/ppann/include/asym_field.hpp b/ppann/include/asym_field.hpp index 4db83ada7f0b71ad56c3bddb49329a040c303ae9..9547fb2716782645beb8a76dcf924cc4759b9f07 100644 --- a/ppann/include/asym_field.hpp +++ b/ppann/include/asym_field.hpp @@ -9,8 +9,8 @@ extern "C" { // Set specific asym data types. typedef bn_t asymPoint; struct asymZp { - asymPoint point{}; - asymPoint modular{}; + asymPoint point{}; + asymPoint modular{}; }; namespace asym { diff --git a/ppann/include/hnsw.hpp b/ppann/include/hnsw.hpp index 5a69f18e4f09ace71f185a917937533e01ca4351..8cc98490b73341f7f3ea4391e65675a46b9543cb 100644 --- a/ppann/include/hnsw.hpp +++ b/ppann/include/hnsw.hpp @@ -12,51 +12,51 @@ using namespace std; struct Item { - // The ciphertext as value. - Ct value; + // The ciphertext as value. + Ct value; - // Compute distance between item with something else. - int dist(Item& other, Key key, int size, int bound) const; + // Compute distance between item with something else. + int dist(Item& other, Key key, int size, int bound) const; }; struct HNSWGraph { - // Constructor. - HNSWGraph(int NN, int MN, int MNZ, int SN, int ML, Key key, int size, - int bound); - - /* HNSW related settings. */ - // Number of neighbors. - int NN; - // Max number of neighbors in layers >= 1. - int MN; - // Max number of neighbors in layers 0. - int MNZ; - // search numbers in construction (efConstruction). - int SN; - // Max number of layers. - int ML; - // number of items - int numItem; - // enter node id - int enterNode{}; - // actual vector of the items - vector<Item> items; - // adjacent edge lists in each layer - vector<unordered_map<int, vector<int>>> layerEdgeLists; - // The default generator. - default_random_engine generator; - - /* For the IPRE scheme. */ - Key key; - int size; - int bound; - - /* Methods. */ - void insert(Item& q); - - void addEdge(int st, int ed, int lc); - - vector<int> search(Item& q, int K); - - vector<int> searchLayer(Item& q, int ep, int ef, int lc); + // Constructor. + HNSWGraph(int NN, int MN, int MNZ, int SN, int ML, Key key, int size, + int bound); + + /* HNSW related settings. */ + // Number of neighbors. + int NN; + // Max number of neighbors in layers >= 1. + int MN; + // Max number of neighbors in layers 0. + int MNZ; + // search numbers in construction (efConstruction). + int SN; + // Max number of layers. + int ML; + // number of items + int numItem; + // enter node id + int enterNode{}; + // actual vector of the items + vector<Item> items; + // adjacent edge lists in each layer + vector<unordered_map<int, vector<int>>> layerEdgeLists; + // The default generator. + default_random_engine generator; + + /* For the IPRE scheme. */ + Key key; + int size; + int bound; + + /* Methods. */ + void insert(Item& q); + + void addEdge(int st, int ed, int lc); + + vector<int> search(Item& q, int K); + + vector<int> searchLayer(Item& q, int ep, int ef, int lc); }; diff --git a/ppann/include/ipre.hpp b/ppann/include/ipre.hpp index 3ec0e8b82e3f3947c2dc930b445fbe983f258dc7..e374f9db2314f5655d64a85f0cee6201334a6fd0 100644 --- a/ppann/include/ipre.hpp +++ b/ppann/include/ipre.hpp @@ -6,18 +6,18 @@ const int B_SIZE = 6; struct Key { - symZpMat A; - symZpMat B; - symZpMat Bi; - symG base; - symGt t_base; - bn_t modular; + symZpMat A; + symZpMat B; + symZpMat Bi; + symG base; + symGt t_base; + bn_t modular; }; struct Ct { - symGVec ctx; - symGVec ctk; - symGVec ctc; + symGVec ctx; + symGVec ctk; + symGVec ctc; }; Key setup(int size); diff --git a/ppann/include/sym-field.hpp b/ppann/include/sym-field.hpp index a3892ebe948fb8561dcad921fc5e88ad6f9d93b0..f27db04bd5f95e7a81e5e00bb0cf68a0f7d7f7ef 100644 --- a/ppann/include/sym-field.hpp +++ b/ppann/include/sym-field.hpp @@ -9,8 +9,8 @@ extern "C" { // Set specific sym data types. typedef bn_t symPoint; struct symZp { - symPoint point{}; - symPoint modular{}; + symPoint point{}; + symPoint modular{}; }; namespace sym { diff --git a/ppann/src/asym-field.cpp b/ppann/src/asym-field.cpp index 0560d5c0abae24e0ab5721a9c8b0800c674813d5..2a91b06c2154f266fad2a66723a7ad4960a3aa62 100644 --- a/ppann/src/asym-field.cpp +++ b/ppann/src/asym-field.cpp @@ -1,75 +1,75 @@ #include "asym-field.hpp" asymZp asym::rand_zp(asymPoint modular) { - asymZp result; - bn_rand_mod(result.point, modular); - bn_copy(result.modular, modular); - return result; + asymZp result; + bn_rand_mod(result.point, modular); + bn_copy(result.modular, modular); + return result; } asymZp asym::zp_zero(asymPoint modular) { - asymZp result; - bn_set_dig(result.point, 0); - bn_copy(result.modular, modular); - return result; + asymZp result; + bn_set_dig(result.point, 0); + bn_copy(result.modular, modular); + return result; } asymZp asym::zp_one(asymPoint modular) { - asymZp result; - bn_set_dig(result.point, 1); - bn_copy(result.modular, modular); - return result; + asymZp result; + bn_set_dig(result.point, 1); + bn_copy(result.modular, modular); + return result; } asymZp asym::zp_copy(asymZp x) { - asymZp result; - bn_copy(result.point, x.point); - bn_copy(result.modular, x.modular); - return result; + asymZp result; + bn_copy(result.point, x.point); + bn_copy(result.modular, x.modular); + return result; } asymZp asym::zp_from_int(int x, asymPoint modular) { - asymZp result; - bn_set_dig(result.point, x); - bn_copy(result.modular, modular); - return result; + asymZp result; + bn_set_dig(result.point, x); + bn_copy(result.modular, modular); + return result; } asymZp asym::zp_add(asymZp x, asymZp y) { - asymZp 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; + asymZp 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; } asymZp asym::zp_neg(asymZp x) { - asymZp result; - bn_neg(result.point, x.point); - bn_mod(result.point, result.point, x.modular); - bn_copy(result.modular, x.modular); - return result; + asymZp result; + bn_neg(result.point, x.point); + bn_mod(result.point, result.point, x.modular); + bn_copy(result.modular, x.modular); + return result; } asymZp asym::zp_mul(asymZp x, asymZp y) { - asymZp 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; + asymZp 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; } asymZp asym::zp_inv(asymZp x) { - asymZp result; - bn_mod_inv(result.point, x.point, x.modular); - bn_copy(result.modular, x.modular); - return result; + asymZp result; + bn_mod_inv(result.point, x.point, x.modular); + bn_copy(result.modular, x.modular); + return result; } int asym::zp_cmp(asymZp x, asymZp y) { - return bn_cmp(x.point, y.point) == RLC_EQ; + return bn_cmp(x.point, y.point) == RLC_EQ; } int asym::zp_cmp_int(asymZp x, int y) { - return bn_cmp_dig(x.point, y) == RLC_EQ; + return bn_cmp_dig(x.point, y) == RLC_EQ; } diff --git a/ppann/src/helper.cpp b/ppann/src/helper.cpp index f053ea8565facb0d936d5bdccfd91bd2775e0ea1..09bb307242334ea9e3b77d59599bdad7cfbce5ef 100644 --- a/ppann/src/helper.cpp +++ b/ppann/src/helper.cpp @@ -1,68 +1,68 @@ #include "helper.hpp" float* fvecs_read(const char* file_path, size_t* d_out, size_t* n_out) { - // Open up the file. - FILE* file = fopen(file_path, "r"); - if (file == NULL) { - printf("\n The file could not be opened: %s\n", file_path); - exit(1); - } - - // Get the dimension. - int d; - fread(&d, 1, sizeof(int), file); - - // Get the number of vectors. - fseek(file, 0, SEEK_SET); - struct stat st {}; - fstat(fileno(file), &st); - - // Compute number of vectors. - size_t sz = st.st_size; - size_t n = sz / ((d + 1) * 4); - - // Update parameters. - *d_out = d; - *n_out = n; - - // Save the data. - auto* x = new float[n * (d + 1)]; - fread(x, sizeof(float), n * (d + 1), file); - - // shift array to remove row headers - for (size_t i = 0; i < n; i++) - memmove(x + i * d, x + 1 + i * (d + 1), d * sizeof(*x)); - - // Close file. - fclose(file); - - return x; + // Open up the file. + FILE* file = fopen(file_path, "r"); + if (file == NULL) { + printf("\n The file could not be opened: %s\n", file_path); + exit(1); + } + + // Get the dimension. + int d; + fread(&d, 1, sizeof(int), file); + + // Get the number of vectors. + fseek(file, 0, SEEK_SET); + struct stat st {}; + fstat(fileno(file), &st); + + // Compute number of vectors. + size_t sz = st.st_size; + size_t n = sz / ((d + 1) * 4); + + // Update parameters. + *d_out = d; + *n_out = n; + + // Save the data. + auto* x = new float[n * (d + 1)]; + fread(x, sizeof(float), n * (d + 1), file); + + // shift array to remove row headers + for (size_t i = 0; i < n; i++) + memmove(x + i * d, x + 1 + i * (d + 1), d * sizeof(*x)); + + // Close file. + fclose(file); + + return x; } int* ivecs_read(const char* file_path, size_t* d_out, size_t* n_out) { - // Cast the float results to integers. - return (int*)fvecs_read(file_path, d_out, n_out); + // Cast the float results to integers. + return (int*)fvecs_read(file_path, d_out, n_out); } int* float_to_int(const float* data, size_t size) { - // Get a new list for integer data. - auto int_data = new int[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]); + // 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; + // Return pointer of the list. + return int_data; } Item* encrypt_data(const int* data, Key key, size_t d, size_t n) { - // Get a new list for encrypted data. - auto* encrypted_data = new Item[n]; + // Get a new list for encrypted data. + auto* encrypted_data = new Item[n]; - // Encrypt each vector. - for (int i = 0; i < n; i++) - encrypted_data[i].value = enc(key, &data[i * d], static_cast<int>(d)); + // Encrypt each vector. + for (int i = 0; i < n; i++) + encrypted_data[i].value = enc(key, &data[i * d], static_cast<int>(d)); - // Return pointer of the list. - return encrypted_data; + // Return pointer of the list. + return encrypted_data; } diff --git a/ppann/src/hnsw.cpp b/ppann/src/hnsw.cpp index ed1abeed8ae828adeac3b0eb87a280282dd3d0b7..852105f73b9efbd846c8daacc25ab68715f22315 100644 --- a/ppann/src/hnsw.cpp +++ b/ppann/src/hnsw.cpp @@ -1,7 +1,7 @@ #include "hnsw.hpp" int Item::dist(Item& other, Key key, int size, int bound) const { - return eval(key, value, other.value, size, bound); + return eval(key, value, other.value, size, bound); } HNSWGraph::HNSWGraph(int NN, int MN, int MNZ, int SN, int ML, Key key, int size, @@ -14,97 +14,98 @@ HNSWGraph::HNSWGraph(int NN, int MN, int MNZ, int SN, int ML, Key key, int size, key(key), size(size), bound(bound) { - numItem = 0; - enterNode = 0; - layerEdgeLists.emplace_back(); + numItem = 0; + enterNode = 0; + layerEdgeLists.emplace_back(); } void HNSWGraph::insert(Item& q) { - int nid = static_cast<int>(items.size()); - numItem++; - items.push_back(q); - // sample layer - int maxLayer = static_cast<int>(layerEdgeLists.size()) - 1; - int l = 0; - uniform_real_distribution<double> distribution(0.0, 1.0); - while (l < ML && (1.0 / ML <= distribution(generator))) { - l++; - if (layerEdgeLists.size() <= l) layerEdgeLists.emplace_back(); - } - if (nid == 0) { - enterNode = nid; - return; - } - // search up layer entrance - int ep = enterNode; - for (int i = maxLayer; i > l; i--) ep = searchLayer(q, ep, 1, i)[0]; - for (int i = min(l, maxLayer); i >= 0; i--) { - int MM = l == 0 ? MNZ : MN; - vector<int> neighbors = searchLayer(q, ep, SN, i); - vector<int> selectedNeighbors = vector<int>( - neighbors.begin(), neighbors.begin() + min(int(neighbors.size()), NN)); - for (int n : selectedNeighbors) addEdge(n, nid, i); - for (int n : selectedNeighbors) { - if (layerEdgeLists[i][n].size() > MM) { - vector<pair<double, int>> distPairs; - for (int nn : layerEdgeLists[i][n]) - distPairs.emplace_back(items[n].dist(items[nn], key, size, bound), - nn); - sort(distPairs.begin(), distPairs.end()); - layerEdgeLists[i][n].clear(); - for (int d = 0; d < min(int(distPairs.size()), MM); d++) - layerEdgeLists[i][n].push_back(distPairs[d].second); - } + int nid = static_cast<int>(items.size()); + numItem++; + items.push_back(q); + // sample layer + int maxLayer = static_cast<int>(layerEdgeLists.size()) - 1; + int l = 0; + uniform_real_distribution<double> distribution(0.0, 1.0); + while (l < ML && (1.0 / ML <= distribution(generator))) { + l++; + if (layerEdgeLists.size() <= l) layerEdgeLists.emplace_back(); } - ep = selectedNeighbors[0]; - } - if (l == layerEdgeLists.size() - 1) enterNode = nid; + if (nid == 0) { + enterNode = nid; + return; + } + // search up layer entrance + int ep = enterNode; + for (int i = maxLayer; i > l; i--) ep = searchLayer(q, ep, 1, i)[0]; + for (int i = min(l, maxLayer); i >= 0; i--) { + int MM = l == 0 ? MNZ : MN; + vector<int> neighbors = searchLayer(q, ep, SN, i); + vector<int> selectedNeighbors = + vector<int>(neighbors.begin(), + neighbors.begin() + min(int(neighbors.size()), NN)); + for (int n : selectedNeighbors) addEdge(n, nid, i); + for (int n : selectedNeighbors) { + if (layerEdgeLists[i][n].size() > MM) { + vector<pair<double, int>> distPairs; + for (int nn : layerEdgeLists[i][n]) + distPairs.emplace_back( + items[n].dist(items[nn], key, size, bound), nn); + sort(distPairs.begin(), distPairs.end()); + layerEdgeLists[i][n].clear(); + for (int d = 0; d < min(int(distPairs.size()), MM); d++) + layerEdgeLists[i][n].push_back(distPairs[d].second); + } + } + ep = selectedNeighbors[0]; + } + if (l == layerEdgeLists.size() - 1) enterNode = nid; } vector<int> HNSWGraph::search(Item& q, int K) { - int maxLayer = static_cast<int>(layerEdgeLists.size()) - 1; - int ep = enterNode; - for (auto l = maxLayer; l >= 1; l--) ep = searchLayer(q, ep, 1, l)[0]; - return searchLayer(q, ep, K, 0); + int maxLayer = static_cast<int>(layerEdgeLists.size()) - 1; + int ep = enterNode; + for (auto l = maxLayer; l >= 1; l--) ep = searchLayer(q, ep, 1, l)[0]; + return searchLayer(q, ep, K, 0); } void HNSWGraph::addEdge(int st, int ed, int lc) { - if (st == ed) return; - layerEdgeLists[lc][st].push_back(ed); - layerEdgeLists[lc][ed].push_back(st); + if (st == ed) return; + layerEdgeLists[lc][st].push_back(ed); + layerEdgeLists[lc][ed].push_back(st); } vector<int> HNSWGraph::searchLayer(Item& q, int ep, int ef, int lc) { - unordered_set<int> isVisited; - set<pair<double, int>> candidates; - set<pair<double, int>> nearestNeighbors; + unordered_set<int> isVisited; + set<pair<double, int>> candidates; + set<pair<double, int>> nearestNeighbors; - double td = q.dist(items[ep], key, size, bound); - candidates.insert(make_pair(td, ep)); - nearestNeighbors.insert(make_pair(td, ep)); - isVisited.insert(ep); - while (!candidates.empty()) { - auto ci = candidates.begin(); - candidates.erase(candidates.begin()); - int nid = ci->second; - auto fi = nearestNeighbors.end(); - fi--; - if (ci->first > fi->first) break; - for (int ed : layerEdgeLists[lc][nid]) { - if (isVisited.find(ed) != isVisited.end()) continue; - fi = nearestNeighbors.end(); - fi--; - isVisited.insert(ed); - td = q.dist(items[ed], key, size, bound); - if ((td < fi->first) || nearestNeighbors.size() < ef) { - candidates.insert(make_pair(td, ed)); - nearestNeighbors.insert(make_pair(td, ed)); - if (nearestNeighbors.size() > ef) nearestNeighbors.erase(fi); - } + double td = q.dist(items[ep], key, size, bound); + candidates.insert(make_pair(td, ep)); + nearestNeighbors.insert(make_pair(td, ep)); + isVisited.insert(ep); + while (!candidates.empty()) { + auto ci = candidates.begin(); + candidates.erase(candidates.begin()); + int nid = ci->second; + auto fi = nearestNeighbors.end(); + fi--; + if (ci->first > fi->first) break; + for (int ed : layerEdgeLists[lc][nid]) { + if (isVisited.find(ed) != isVisited.end()) continue; + fi = nearestNeighbors.end(); + fi--; + isVisited.insert(ed); + td = q.dist(items[ed], key, size, bound); + if ((td < fi->first) || nearestNeighbors.size() < ef) { + candidates.insert(make_pair(td, ed)); + nearestNeighbors.insert(make_pair(td, ed)); + if (nearestNeighbors.size() > ef) nearestNeighbors.erase(fi); + } + } } - } - vector<int> results; - results.reserve(nearestNeighbors.size()); - for (auto& p : nearestNeighbors) results.push_back(p.second); - return results; + vector<int> results; + results.reserve(nearestNeighbors.size()); + for (auto& p : nearestNeighbors) results.push_back(p.second); + return results; } diff --git a/ppann/src/ipre.cpp b/ppann/src/ipre.cpp index ce68ce574cd807d7ec424f95cc319dd97164c044..b43313ea3517a767a6d769cde4cd521b670e9bb5 100644 --- a/ppann/src/ipre.cpp +++ b/ppann/src/ipre.cpp @@ -1,75 +1,75 @@ #include "ipre.hpp" Key setup(int size) { - Key key{}; - pc_get_ord(key.modular); - sym::g_gen(key.base); - sym::bp_map(key.t_base, key.base, key.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; + Key key{}; + pc_get_ord(key.modular); + sym::g_gen(key.base); + sym::bp_map(key.t_base, key.base, key.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 asymZp. - Ct ct{}; - symZpVec x = sym::vector_zp_from_int(message, size, key.modular); + // Declare the returned ciphertext and convert message to asymZp. + Ct ct{}; + symZpVec x = sym::vector_zp_from_int(message, size, key.modular); - // Helper values. - int one[] = {1}, zero[] = {0}; - symZpVec one_vec = sym::vector_zp_from_int(one, 1, key.modular); - symZpVec zero_vec = sym::vector_zp_from_int(zero, 1, key.modular); + // Helper values. + int one[] = {1}, zero[] = {0}; + 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. - 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 generate s and compute sA + x. + 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. - 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 Key. + 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. - 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); + // We compute the function hiding inner product encryption ciphertext. + 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; + return ct; } int eval(Key key, Ct x, Ct y, int size, int bound) { - // Decrypt components. - symGt xy, ct; - sym::inner_product(xy, x.ctx, y.ctx, size); - sym::inner_product(ct, x.ctc, y.ctk, B_SIZE); + // Decrypt components. + 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); + // Decrypt final result. + gt_inv(ct, ct); + gt_mul(xy, xy, ct); - // Get a target group element holder. - symGt output; + // Get a target group element holder. + symGt output; - // Iterate through a loop to find correct answer. - for (int i = 1; i <= bound; i++) { - gt_exp_dig(output, key.t_base, i); - if (gt_cmp(output, xy) == RLC_EQ) return i; - } + // Iterate through a loop to find correct answer. + for (int i = 1; i <= bound; i++) { + gt_exp_dig(output, key.t_base, i); + if (gt_cmp(output, xy) == RLC_EQ) return i; + } - // Otherwise return 0 as the output. - return 0; + // Otherwise return 0 as the output. + return 0; } diff --git a/ppann/src/sym-field.cpp b/ppann/src/sym-field.cpp index 3f0b6129aebf746078574e2cc06620a2098510f6..46ca038ac9a8bfbe8a5242eb81a07771bbc1c3c9 100644 --- a/ppann/src/sym-field.cpp +++ b/ppann/src/sym-field.cpp @@ -1,69 +1,69 @@ #include "sym-field.hpp" symZp sym::zp_rand(symPoint modular) { - symZp result; - bn_rand_mod(result.point, modular); - bn_copy(result.modular, modular); - return result; + symZp result; + bn_rand_mod(result.point, modular); + bn_copy(result.modular, modular); + return result; } symZp sym::zp_zero(symPoint modular) { - symZp result; - bn_set_dig(result.point, 0); - bn_copy(result.modular, modular); - return result; + symZp result; + bn_set_dig(result.point, 0); + bn_copy(result.modular, modular); + return result; } symZp sym::zp_one(symPoint modular) { - symZp result; - bn_set_dig(result.point, 1); - bn_copy(result.modular, modular); - return result; + symZp result; + bn_set_dig(result.point, 1); + bn_copy(result.modular, modular); + return result; } symZp sym::zp_copy(symZp x) { - symZp result; - bn_copy(result.point, x.point); - bn_copy(result.modular, x.modular); - return result; + symZp result; + bn_copy(result.point, x.point); + bn_copy(result.modular, x.modular); + return 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; + symZp result; + bn_set_dig(result.point, x); + bn_copy(result.modular, modular); + return 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; + 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; } 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; + symZp result; + bn_neg(result.point, x.point); + bn_mod(result.point, result.point, x.modular); + bn_copy(result.modular, x.modular); + return 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; + 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; } 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; + symZp result; + bn_mod_inv(result.point, x.point, x.modular); + bn_copy(result.modular, x.modular); + return result; } int sym::zp_cmp(symZp x, symZp y) { return bn_cmp(x.point, y.point) == RLC_EQ; } diff --git a/ppann/src/sym-matrix.cpp b/ppann/src/sym-matrix.cpp index 2b1da0c783d9742161e32602fc7662bdecc774e9..9012e172ddd68e52cedc30ea40822ba12f25824d 100644 --- a/ppann/src/sym-matrix.cpp +++ b/ppann/src/sym-matrix.cpp @@ -2,148 +2,153 @@ 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] = sym::zp_from_int(int_mat[i * col + j], 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] = sym::zp_from_int(int_mat[i * col + j], modular); + } } - } - return x; + return x; } 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] = sym::zp_rand(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] = sym::zp_rand(modular); + } } - } - return x; + return x; } 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] = sym::zp_one(modular); - else - x[i * size + j] = sym::zp_zero(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] = sym::zp_one(modular); + else + x[i * size + j] = sym::zp_zero(modular); + } } - } - return x; + return x; } 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 && !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; + for (int i = 0; i < size; i++) { + for (int j = 0; j < size; j++) { + 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; + return 1; } 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] = sym::zp_copy(x[i * col + j]); + 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] = sym::zp_copy(x[i * col + j]); + } } - } - return xt; + return xt; } 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] = 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] = sym::zp_copy(y[i * col_y + j]); + 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] = 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] = + sym::zp_copy(y[i * col_y + j]); + } } - } - return xy; + return xy; } 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); + 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] = sym::zp_zero(modular); - for (int k = 0; k < row_y; k++) { - 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])); - } + for (int i = 0; i < row_x; i++) { + for (int j = 0; j < col_y; j++) { + xy[i * row_y + j] = sym::zp_zero(modular); + for (int k = 0; k < row_y; k++) { + 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; + return xy; } symZpMat sym::matrix_inverse(symZpMat x, int size, symPoint modular) { - // Declare the row echelon matrix and generate it. - symZpMat identity = matrix_identity(size, modular); - symZpMat row_echelon = matrix_merge(x, identity, size, size, size); + // Declare the row echelon matrix and generate it. + symZpMat identity = matrix_identity(size, modular); + symZpMat row_echelon = matrix_merge(x, identity, size, size, size); - // Declare temp value. - symZp temp_multiplier; - symZp temp_neg; + // Declare temp value. + 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 && !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] = - sym::zp_mul(row_echelon[j * 2 * size + k], temp_multiplier); - } - } + // Bottom left half to all zeros. + for (int i = 0; i < size; i++) { + for (int j = i; j < size; j++) { + 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] = sym::zp_mul( + row_echelon[j * 2 * size + k], temp_multiplier); + } + } - if (i == j && sym::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 = sym::zp_copy(row_echelon[j * 2 * size + i]); - for (int k = i; k < size * 2; k++) { - 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); + if (i != j) { + temp_multiplier = sym::zp_copy(row_echelon[j * 2 * size + i]); + for (int k = i; k < size * 2; k++) { + 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); + } + } } - } } - } - // Top right half to all zeros. - for (int i = size - 1; i > 0; i--) { - for (int j = i - 1; j >= 0; j--) { - temp_multiplier = sym::zp_copy(row_echelon[j * 2 * size + i]); - for (int k = i; k < size * 2; k++) { - 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); - } + // Top right half to all zeros. + for (int i = size - 1; i > 0; i--) { + for (int j = i - 1; j >= 0; j--) { + temp_multiplier = sym::zp_copy(row_echelon[j * 2 * size + i]); + for (int k = i; k < size * 2; k++) { + 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. - 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] = sym::zp_copy(row_echelon[i * 2 * size + size + j]); + // Copy over the output. + 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] = + sym::zp_copy(row_echelon[i * 2 * size + size + j]); + } } - } - return xi; + return xi; } diff --git a/ppann/src/sym-vector.cpp b/ppann/src/sym-vector.cpp index 7b8fe4e29ee7d8aed116051dec1bd05e57bb1c45..d05e3d222659073e781d23447cce3b1f6f04f607 100644 --- a/ppann/src/sym-vector.cpp +++ b/ppann/src/sym-vector.cpp @@ -2,46 +2,46 @@ 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; + 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; } 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; + symZpVec x; + x = (symZpVec)malloc(sizeof(symZp) * size); + for (int i = 0; i < size; i++) x[i] = sym::zp_rand(modular); + return x; } 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; + 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; } 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; + 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; } 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; + 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 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]); - gt_mul(r, r, temp); - } + symGt temp; + gt_set_unity(r); + for (int i = 0; i < size; i++) { + pc_map(temp, a[i], b[i]); + gt_mul(r, r, temp); + } } diff --git a/ppann/test/test-asym-field.cpp b/ppann/test/test-asym-field.cpp index 476459277c8af3c020a793cb0f5b299f3a946f78..9082a867be313a1c9824f06e1cac2b665ad621a9 100644 --- a/ppann/test/test-asym-field.cpp +++ b/ppann/test/test-asym-field.cpp @@ -1,72 +1,74 @@ #include "asym-field.hpp" +#include "gtest/gtest.h" -int test_zp_zero(asymPoint N) { - asymZp x = asym::zp_zero(N); - return asym::zp_cmp_int(x, 0); -} +namespace PPANN { -int test_zp_one(asymPoint N) { - asymZp x = asym::zp_one(N); - return asym::zp_cmp_int(x, 1); -} +class AsymFieldTest : public ::testing::Test { + protected: + bn_t N_; + + AsymFieldTest() { + // Init core and setup. + core_init(); + pc_param_set_any(); -int test_zp_copy(asymPoint N) { - asymZp x = asym::zp_from_int(10, N); - asymZp y = asym::zp_copy(x); - return asym::zp_cmp(x, y); + // Get order. + pc_get_ord(N_); + } +}; + +TEST_F(AsymFieldTest, ZpZero) { + asymZp x = asym::zp_zero(N_); + ASSERT_EQ(1, asym::zp_cmp_int(x, 0)); } -int test_zp_from_int(asymPoint N) { - asymZp x = asym::zp_from_int(3, N); - return asym::zp_cmp_int(x, 3); +TEST_F(AsymFieldTest, ZpOne) { + asymZp x = asym::zp_one(N_); + ASSERT_EQ(1, asym::zp_cmp_int(x, 1)); } -int test_zp_add(asymPoint N) { - asymZp x = asym::zp_from_int(10, N); - asymZp y = asym::zp_from_int(20, N); - asymZp z = asym::zp_add(x, y); - return asym::zp_cmp_int(z, 30); +TEST_F(AsymFieldTest, ZpCopy) { + asymZp x = asym::zp_from_int(10, N_); + asymZp y = asym::zp_copy(x); + ASSERT_EQ(1, asym::zp_cmp(x, y)); } -int test_zp_neg(asymPoint N) { - asymZp x = asym::rand_zp(N); - asymZp y = asym::zp_neg(x); - asymZp z = asym::zp_add(x, y); - return asym::zp_cmp_int(z, 0); +TEST_F(AsymFieldTest, ZpFromInt) { + asymZp x = asym::zp_from_int(3, N_); + ASSERT_EQ(1, asym::zp_cmp_int(x, 3)); } -int test_zp_mul(asymPoint N) { - asymZp x = asym::zp_from_int(10, N); - asymZp y = asym::zp_from_int(20, N); - asymZp z = asym::zp_mul(x, y); - return asym::zp_cmp_int(z, 200); +TEST_F(AsymFieldTest, ZpAdd) { + asymZp x = asym::zp_from_int(10, N_); + asymZp y = asym::zp_from_int(20, N_); + asymZp z = asym::zp_add(x, y); + ASSERT_EQ(1, asym::zp_cmp_int(z, 30)); } -int test_zp_inv(asymPoint N) { - asymZp x = asym::rand_zp(N); - asymZp y = asym::zp_inv(x); - asymZp z = asym::zp_mul(x, y); - return asym::zp_cmp_int(z, 1); +TEST_F(AsymFieldTest, ZpNeg) { + asymZp x = asym::rand_zp(N_); + asymZp y = asym::zp_neg(x); + asymZp z = asym::zp_add(x, y); + ASSERT_EQ(1, asym::zp_cmp_int(z, 0)); } -int main() { - // Init core and setup. - core_init(); - pc_param_set_any(); +TEST_F(AsymFieldTest, ZpMul) { + asymZp x = asym::zp_from_int(10, N_); + asymZp y = asym::zp_from_int(20, N_); + asymZp z = asym::zp_mul(x, y); + ASSERT_EQ(1, asym::zp_cmp_int(z, 200)); +} - // Get order. - bn_t N; - pc_get_ord(N); +TEST_F(AsymFieldTest, ZpInv) { + asymZp x = asym::rand_zp(N_); + asymZp y = asym::zp_inv(x); + asymZp z = asym::zp_mul(x, y); + ASSERT_EQ(1, asym::zp_cmp_int(z, 1)); +} - // 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; +} // namespace PPANN - return 0; +int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); } diff --git a/ppann/test/test-asym-group.cpp b/ppann/test/test-asym-group.cpp index 9004ab1bd7cddb76ceeb1b12fcaa17abde123cb8..933a4ada62d091df7f14fc4b1c217b6ae215c505 100644 --- a/ppann/test/test-asym-group.cpp +++ b/ppann/test/test-asym-group.cpp @@ -1,26 +1,27 @@ #include "asym-group.hpp" int test_g1_generator() { - asymG1 x; - asym::g1_gen(x); - // Specifically this line goes wrong... I don't know how to further debug it. - return g1_is_valid(x); + asymG1 x; + asym::g1_gen(x); + // Specifically this line goes wrong... I don't know how to further debug + // it. + return g1_is_valid(x); } int main() { - // Init core and setup. - core_init(); - pc_param_set_any(); + // Init core and setup. + core_init(); + pc_param_set_any(); - // Print the param used. - pc_param_print(); + // Print the param used. + pc_param_print(); - // Get order. - bn_t N; - pc_get_ord(N); + // Get order. + bn_t N; + pc_get_ord(N); - // Perform tests. - // if (test_g1_generator() != 1) return 1; + // Perform tests. + // if (test_g1_generator() != 1) return 1; - return 0; + return 0; } diff --git a/ppann/test/test-helper.cpp b/ppann/test/test-helper.cpp index ee88012d7c51d46cefe2a1864c52fc0829b85c8e..b2eb356210ff9b19aa4263455ba75029916d0fc6 100644 --- a/ppann/test/test-helper.cpp +++ b/ppann/test/test-helper.cpp @@ -1,85 +1,85 @@ #include "helper.hpp" int test_read_fvecs() { - // Set dimensions holders and get the data. - size_t d, n; - float* xd = fvecs_read("../data/sift_query.fvecs", &d, &n); - - // Check for whether the data is correct. - if (d != 128) return 0; - if (n != 10000) return 0; - if (xd[1] != 3) return 0; - if (xd[1279872] != 23) return 0; - - // If everything passes, return 1. - return 1; + // Set dimensions holders and get the data. + size_t d, n; + float* xd = fvecs_read("../data/sift_query.fvecs", &d, &n); + + // Check for whether the data is correct. + if (d != 128) return 0; + if (n != 10000) return 0; + if (xd[1] != 3) return 0; + if (xd[1279872] != 23) return 0; + + // If everything passes, return 1. + return 1; } int test_read_ivecs() { - // Set dimensions holders and get the data. - size_t d, n; - int* gt = ivecs_read("../data/sift_groundtruth.ivecs", &d, &n); - - // Check for whether the data is correct. - if (d != 100) return 0; - if (n != 10000) return 0; - if (gt[1] != 934876) return 0; - if (gt[999900] != 874343) return 0; - - // If everything passes, return 1. - return 1; + // Set dimensions holders and get the data. + size_t d, n; + int* gt = ivecs_read("../data/sift_groundtruth.ivecs", &d, &n); + + // Check for whether the data is correct. + if (d != 100) return 0; + if (n != 10000) return 0; + if (gt[1] != 934876) return 0; + if (gt[999900] != 874343) return 0; + + // If everything passes, return 1. + return 1; } int test_float_to_int() { - // Set dimensions holders and get the data. - size_t d, n; - float* xd = fvecs_read("../data/sift_query.fvecs", &d, &n); + // Set dimensions holders and get the data. + size_t d, n; + float* xd = fvecs_read("../data/sift_query.fvecs", &d, &n); - // Conversion and test. - int* data = float_to_int(xd, 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; + // 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; + // 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("../data/sift_query.fvecs", &d, &n); + // Set dimensions holders and get the data. + size_t d, n; + float* xd = fvecs_read("../data/sift_query.fvecs", &d, &n); - // Conversion. - int* data = float_to_int(xd, d * n); + // Conversion. + int* data = float_to_int(xd, d * n); - // Cast d to integer. - int d_int = static_cast<int>(d); + // Cast d to integer. + int d_int = static_cast<int>(d); - // Encrypt the first two vectors. - Key key = setup(d_int); - Item* encrypted_data = encrypt_data(data, key, d, 2); + // Encrypt the first two vectors. + Key key = setup(d_int); + Item* encrypted_data = encrypt_data(data, key, d, 2); - // Get inner product of the first two vectors. - int result = eval(key, encrypted_data[0].value, encrypted_data[1].value, - d_int, 200000); + // Get inner product of the first two vectors. + int result = eval(key, encrypted_data[0].value, encrypted_data[1].value, + d_int, 200000); - // Check for whether the data is correct. - if (result != 184094) return 0; + // Check for whether the data is correct. + if (result != 184094) return 0; - return 1; + return 1; } int main() { - // Init core and setup. - core_init(); - pc_param_set_any(); - - // 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; + // Init core and setup. + core_init(); + pc_param_set_any(); + + // 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; } diff --git a/ppann/test/test-hnsw.cpp b/ppann/test/test-hnsw.cpp index 820619da14fd0ab3f29e483f906a5fbbdbcbd6b2..84a7116ed2d6b7fc3768b31aae9b8d703dce1fb7 100644 --- a/ppann/test/test-hnsw.cpp +++ b/ppann/test/test-hnsw.cpp @@ -3,84 +3,84 @@ #include "ipre.hpp" int test_constructor() { - // Get the key. - Key key = setup(10); + // Get the key. + Key key = setup(10); - // Get the HNSW object. - HNSWGraph index(10, 30, 10, 40, 4, key, 10, 100); + // Get the HNSW object. + HNSWGraph index(10, 30, 10, 40, 4, key, 10, 100); - // Check for whether the data is correct. - if (index.NN != 10) return 0; + // Check for whether the data is correct. + if (index.NN != 10) return 0; - // If everything passes, return 1. - return 1; + // If everything passes, return 1. + return 1; } int test_insert() { - // Set dimensions holders and get the data. - size_t d, n; - float* xd = fvecs_read("../data/sift_query.fvecs", &d, &n); + // Set dimensions holders and get the data. + size_t d, n; + float* xd = fvecs_read("../data/sift_query.fvecs", &d, &n); - // Conversion. - int* data = float_to_int(xd, d * n); + // Conversion. + int* data = float_to_int(xd, d * n); - // Cast d to integer. - int d_int = static_cast<int>(d); + // Cast d to integer. + int d_int = static_cast<int>(d); - // Encrypt the first three vectors. - Key key = setup(d_int); - Item* encrypted_data = encrypt_data(data, key, d, 3); + // Encrypt the first three vectors. + Key key = setup(d_int); + Item* encrypted_data = encrypt_data(data, key, d, 3); - // Get the HNSW object. - HNSWGraph index(10, 30, 10, 40, 4, key, 10, 100); + // Get the HNSW object. + HNSWGraph index(10, 30, 10, 40, 4, key, 10, 100); - // Do the insert. - for (int i = 0; i < 3; i++) index.insert(encrypted_data[i]); + // Do the insert. + for (int i = 0; i < 3; i++) index.insert(encrypted_data[i]); - // Check for whether the data is correct. - if (index.numItem != 3) return 0; + // Check for whether the data is correct. + if (index.numItem != 3) return 0; - return 1; + return 1; } int test_search() { - // Set dimensions holders and get the data. - size_t d, n; - float* xd = fvecs_read("../data/sift_query.fvecs", &d, &n); + // Set dimensions holders and get the data. + size_t d, n; + float* xd = fvecs_read("../data/sift_query.fvecs", &d, &n); - // Conversion. - int* data = float_to_int(xd, d * n); + // Conversion. + int* data = float_to_int(xd, d * n); - // Cast d to integer. - int d_int = static_cast<int>(d); + // Cast d to integer. + int d_int = static_cast<int>(d); - // Encrypt the first three vectors. - Key key = setup(d_int); - Item* encrypted_data = encrypt_data(data, key, d, 6); + // Encrypt the first three vectors. + Key key = setup(d_int); + Item* encrypted_data = encrypt_data(data, key, d, 6); - // Get the HNSW object. - HNSWGraph index(10, 30, 10, 40, 4, key, 10, 100); + // Get the HNSW object. + HNSWGraph index(10, 30, 10, 40, 4, key, 10, 100); - // Do the insert. - for (int i = 0; i < 5; i++) index.insert(encrypted_data[i]); + // Do the insert. + for (int i = 0; i < 5; i++) index.insert(encrypted_data[i]); - // Check for whether the data is correct. - vector<int> result = index.search(encrypted_data[5], 3); + // Check for whether the data is correct. + vector<int> result = index.search(encrypted_data[5], 3); - if (result.size() != 3) return 0; + if (result.size() != 3) return 0; - return 1; + return 1; } int main() { - // Init core and setup. - core_init(); - pc_param_set_any(); + // Init core and setup. + core_init(); + pc_param_set_any(); - // Perform tests. - if (test_constructor() != 1) return 1; - if (test_insert() != 1) return 1; - if (test_search() != 1) return 1; + // Perform tests. + if (test_constructor() != 1) return 1; + if (test_insert() != 1) return 1; + if (test_search() != 1) return 1; - return 0; + return 0; } diff --git a/ppann/test/test-ipre.cpp b/ppann/test/test-ipre.cpp index 549a2d9f333231de2f95a219e0b39126a69e4042..5db7946f56e21b12e95bd1973189ebc998ff2c7a 100644 --- a/ppann/test/test-ipre.cpp +++ b/ppann/test/test-ipre.cpp @@ -1,27 +1,27 @@ #include "ipre.hpp" int test_scheme() { - // Set x, y vectors. - int x[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; - int y[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 100}; - // Initialize the scheme. - Key key = setup(10); - // Encrypt the messages. - Ct ct_x = enc(key, x, 10); - Ct ct_y = enc(key, y, 10); - // Evaluate the two ciphertexts. - int output = eval(key, ct_x, ct_y, 10, 150); + // Set x, y vectors. + int x[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; + int y[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 100}; + // Initialize the scheme. + Key key = setup(10); + // Encrypt the messages. + Ct ct_x = enc(key, x, 10); + Ct ct_y = enc(key, y, 10); + // Evaluate the two ciphertexts. + int output = eval(key, ct_x, ct_y, 10, 150); - return output == 145; + return output == 145; } int main() { - // Init core and setup. - core_init(); - pc_param_set_any(); + // Init core and setup. + core_init(); + pc_param_set_any(); - // Perform tests. - if (test_scheme() != 1) return 1; + // Perform tests. + if (test_scheme() != 1) return 1; - return 0; + return 0; } diff --git a/ppann/test/test-sym-field.cpp b/ppann/test/test-sym-field.cpp index 91acc38c50990df239db8a98ac0a036abb8e55ed..5308f085bd54e8adb1b297a4e5e5f817abb1deec 100644 --- a/ppann/test/test-sym-field.cpp +++ b/ppann/test/test-sym-field.cpp @@ -1,72 +1,72 @@ #include "sym-field.hpp" int test_zp_zero(symPoint N) { - symZp x = sym::zp_zero(N); - return sym::zp_cmp_int(x, 0); + symZp x = sym::zp_zero(N); + return sym::zp_cmp_int(x, 0); } int test_zp_one(symPoint N) { - symZp x = sym::zp_one(N); - return sym::zp_cmp_int(x, 1); + symZp x = sym::zp_one(N); + return sym::zp_cmp_int(x, 1); } int test_zp_copy(symPoint N) { - symZp x = sym::zp_from_int(10, N); - symZp y = sym::zp_copy(x); - return sym::zp_cmp(x, y); + 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(symPoint N) { - symZp x = sym::zp_from_int(3, N); - return sym::zp_cmp_int(x, 3); + symZp x = sym::zp_from_int(3, N); + return sym::zp_cmp_int(x, 3); } int test_zp_add(symPoint 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); + 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(symPoint 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); + 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(symPoint 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); + 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(symPoint 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); + 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(); + // Init core and setup. + core_init(); + pc_param_set_any(); - // Get order. - bn_t N; - pc_get_ord(N); + // 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; + // 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; + return 0; } diff --git a/ppann/test/test-sym-group.cpp b/ppann/test/test-sym-group.cpp index c8b695cf63063a729435ea75e2ff0b237cb01333..37a2c2ac4af70854e7ecf855e99fa57f6870ffe4 100644 --- a/ppann/test/test-sym-group.cpp +++ b/ppann/test/test-sym-group.cpp @@ -1,47 +1,47 @@ #include "sym-group.hpp" int test_generator() { - symG x; - sym::g_gen(x); - return g1_is_valid(x); + symG x; + sym::g_gen(x); + return g1_is_valid(x); } int test_all(symPoint N) { - // Set integers. - symZp m = sym::zp_from_int(5, N); - symZp n = sym::zp_from_int(25, 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; + // 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 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(x, a, a); - sym::bp_map(y, b, b); + // Get e(symG, symG) and e(symG^5, symG^5). + sym::bp_map(x, a, a); + sym::bp_map(y, b, b); - // Get e(symG, symG)^25. - sym::gt_raise(z, x, n); + // 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); + // 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(); + // Init core and setup. + core_init(); + pc_param_set_any(); - // Get order. - bn_t N; - pc_get_ord(N); + // 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; + // Perform tests. + if (test_generator() != 1) return 1; + if (test_all(N) != RLC_EQ) return 1; - return 0; + return 0; } diff --git a/ppann/test/test-sym-matrix.cpp b/ppann/test/test-sym-matrix.cpp index 3e7e2f81d29533887ccacd98884b430a08812e16..18e00fa166e65ab298f47a07e0f931735812b771 100644 --- a/ppann/test/test-sym-matrix.cpp +++ b/ppann/test/test-sym-matrix.cpp @@ -1,66 +1,67 @@ #include "sym-matrix.hpp" int test_zp_from_int(symPoint 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 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(symPoint 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 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(symPoint N) { - int size = 10; - symZpMat x = sym::matrix_identity(size, N); - return sym::matrix_is_identity(x, size); + int size = 10; + symZpMat x = sym::matrix_identity(size, N); + return sym::matrix_is_identity(x, size); } int test_merge(symPoint 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 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(symPoint 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}; + 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); + 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(symPoint 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 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(); + // Init core and setup. + core_init(); + pc_param_set_any(); - // Get order. - bn_t N; - pc_get_ord(N); + // 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; + // 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; + return 0; } diff --git a/ppann/test/test-sym-vector.cpp b/ppann/test/test-sym-vector.cpp index dc127aaa7b0401ce1b0b17938b002135a0cec59c..87502b988ae214a633251324b96a4c60deefeac9 100644 --- a/ppann/test/test-sym-vector.cpp +++ b/ppann/test/test-sym-vector.cpp @@ -1,63 +1,63 @@ #include "sym-vector.hpp" int test_zp_from_int(symPoint 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 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(symPoint 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 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(symPoint 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 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(symPoint 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(b, base, base); - gt_exp_dig(b, b, 32); - - return gt_cmp(b, r); + 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(b, base, base); + gt_exp_dig(b, b, 32); + + return gt_cmp(b, r); } int main() { - // Init core and setup. - core_init(); - pc_param_set_any(); + // Init core and setup. + core_init(); + pc_param_set_any(); - // Get order. - bn_t N; - pc_get_ord(N); + // 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; + // 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; + return 0; } diff --git a/ppann/test/test-sym_field.cpp b/ppann/test/test-sym_field.cpp index 91acc38c50990df239db8a98ac0a036abb8e55ed..5308f085bd54e8adb1b297a4e5e5f817abb1deec 100644 --- a/ppann/test/test-sym_field.cpp +++ b/ppann/test/test-sym_field.cpp @@ -1,72 +1,72 @@ #include "sym-field.hpp" int test_zp_zero(symPoint N) { - symZp x = sym::zp_zero(N); - return sym::zp_cmp_int(x, 0); + symZp x = sym::zp_zero(N); + return sym::zp_cmp_int(x, 0); } int test_zp_one(symPoint N) { - symZp x = sym::zp_one(N); - return sym::zp_cmp_int(x, 1); + symZp x = sym::zp_one(N); + return sym::zp_cmp_int(x, 1); } int test_zp_copy(symPoint N) { - symZp x = sym::zp_from_int(10, N); - symZp y = sym::zp_copy(x); - return sym::zp_cmp(x, y); + 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(symPoint N) { - symZp x = sym::zp_from_int(3, N); - return sym::zp_cmp_int(x, 3); + symZp x = sym::zp_from_int(3, N); + return sym::zp_cmp_int(x, 3); } int test_zp_add(symPoint 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); + 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(symPoint 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); + 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(symPoint 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); + 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(symPoint 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); + 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(); + // Init core and setup. + core_init(); + pc_param_set_any(); - // Get order. - bn_t N; - pc_get_ord(N); + // 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; + // 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; + return 0; }