Skip to content
Snippets Groups Projects
Commit 3027b95d authored by Dmytro Bogatov's avatar Dmytro Bogatov :two_hearts:
Browse files

Add test suite and benchmark.

parent c12f38e5
No related branches found
No related tags found
No related merge requests found
Showing
with 732 additions and 634 deletions
...@@ -4,7 +4,8 @@ AllowShortCaseLabelsOnASingleLine: false ...@@ -4,7 +4,8 @@ AllowShortCaseLabelsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: true AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true AllowShortLoopsOnASingleLine: true
DerivePointerBinding: false DerivePointerBinding: false
TabWidth: 2 TabWidth: 4
IndentWidth: 4
ForEachMacros: ["foreach"] ForEachMacros: ["foreach"]
SpaceBeforeParens: ControlStatementsExceptForEachMacros SpaceBeforeParens: ControlStatementsExceptForEachMacros
......
{
"cSpell.words": [
"Asym",
"IPRE",
"PPANN"
]
}
...@@ -40,7 +40,7 @@ TESTS = $(ENTITIES) ...@@ -40,7 +40,7 @@ TESTS = $(ENTITIES)
TESTBIN = $(addprefix $(BDIR)/test-, $(TESTS)) TESTBIN = $(addprefix $(BDIR)/test-, $(TESTS))
JUNITS= $(foreach test, $(TESTS), bin/test-$(test)?--gtest_output=xml:junit-$(test).xml) JUNITS= $(foreach test, $(TESTS), bin/test-$(test)?--gtest_output=xml:junit-$(test).xml)
BENCHMARKS = BENCHMARKS = ipre
BENCHMARKSBIN = $(addprefix $(BDIR)/benchmark-, $(BENCHMARKS)) BENCHMARKSBIN = $(addprefix $(BDIR)/benchmark-, $(BENCHMARKS))
INTEGRATION = INTEGRATION =
...@@ -109,10 +109,12 @@ run-tests: LDLIBS += $(LDTESTLIBS) ...@@ -109,10 +109,12 @@ run-tests: LDLIBS += $(LDTESTLIBS)
run-tests: $(TESTBIN) run-tests: $(TESTBIN)
$(addsuffix &&, $(TESTBIN)) echo Tests passed! $(addsuffix &&, $(TESTBIN)) echo Tests passed!
run-benchmarks: LDLIBS += $(LDTESTLIBS)
run-benchmarks: $(BENCHMARKSBIN) run-benchmarks: $(BENCHMARKSBIN)
$(addsuffix &&, $(BENCHMARKSBIN)) echo Benchmarks completed! $(addsuffix &&, $(BENCHMARKSBIN)) echo Benchmarks completed!
run-integration: CPPFLAGS += -DTESTING run-integration: CPPFLAGS += -DTESTING
run-integration: LDLIBS += $(LDTESTLIBS)
run-integration: $(INTEGRATIONBIN) run-integration: $(INTEGRATIONBIN)
$(addsuffix &&, $(INTEGRATIONBIN)) echo Tests passed! $(addsuffix &&, $(INTEGRATIONBIN)) echo Tests passed!
......
#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();
...@@ -41,15 +41,16 @@ void HNSWGraph::insert(Item& q) { ...@@ -41,15 +41,16 @@ void HNSWGraph::insert(Item& q) {
for (int i = min(l, maxLayer); i >= 0; i--) { for (int i = min(l, maxLayer); i >= 0; i--) {
int MM = l == 0 ? MNZ : MN; int MM = l == 0 ? MNZ : MN;
vector<int> neighbors = searchLayer(q, ep, SN, i); vector<int> neighbors = searchLayer(q, ep, SN, i);
vector<int> selectedNeighbors = vector<int>( vector<int> selectedNeighbors =
neighbors.begin(), neighbors.begin() + min(int(neighbors.size()), NN)); vector<int>(neighbors.begin(),
neighbors.begin() + min(int(neighbors.size()), NN));
for (int n : selectedNeighbors) addEdge(n, nid, i); for (int n : selectedNeighbors) addEdge(n, nid, i);
for (int n : selectedNeighbors) { for (int n : selectedNeighbors) {
if (layerEdgeLists[i][n].size() > MM) { if (layerEdgeLists[i][n].size() > MM) {
vector<pair<double, int>> distPairs; vector<pair<double, int>> distPairs;
for (int nn : layerEdgeLists[i][n]) for (int nn : layerEdgeLists[i][n])
distPairs.emplace_back(items[n].dist(items[nn], key, size, bound), distPairs.emplace_back(
nn); items[n].dist(items[nn], key, size, bound), nn);
sort(distPairs.begin(), distPairs.end()); sort(distPairs.begin(), distPairs.end());
layerEdgeLists[i][n].clear(); layerEdgeLists[i][n].clear();
for (int d = 0; d < min(int(distPairs.size()), MM); d++) for (int d = 0; d < min(int(distPairs.size()), MM); d++)
......
...@@ -44,8 +44,8 @@ Ct enc(Key key, const int* message, int size) { ...@@ -44,8 +44,8 @@ Ct enc(Key key, const int* message, int size) {
symZpVec sxATsAAT = sym::vector_merge(s, xATsAAT, 2, 2); symZpVec sxATsAAT = sym::vector_merge(s, xATsAAT, 2, 2);
symZpVec sxATsAAT1 = sym::vector_merge(sxATsAAT, one_vec, 4, 1); symZpVec sxATsAAT1 = sym::vector_merge(sxATsAAT, one_vec, 4, 1);
symZpVec sxATsAAT10 = sym::vector_merge(sxATsAAT1, zero_vec, 5, 1); symZpVec sxATsAAT10 = sym::vector_merge(sxATsAAT1, zero_vec, 5, 1);
symZpVec sxATsAAT10_Bi = symZpVec sxATsAAT10_Bi = sym::matrix_multiply(sxATsAAT10, key.Bi, 1, B_SIZE,
sym::matrix_multiply(sxATsAAT10, key.Bi, 1, B_SIZE, B_SIZE, key.modular); B_SIZE, key.modular);
ct.ctk = sym::vector_raise(key.base, sxATsAAT10_Bi, B_SIZE); ct.ctk = sym::vector_raise(key.base, sxATsAAT10_Bi, B_SIZE);
return ct; return ct;
......
...@@ -67,7 +67,8 @@ symZpMat sym::matrix_merge(symZpMat x, symZpMat y, int row, int col_x, ...@@ -67,7 +67,8 @@ symZpMat sym::matrix_merge(symZpMat x, symZpMat y, int row, int col_x,
xy[i * (col_x + col_y) + j] = sym::zp_copy(x[i * 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++) { 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]); xy[i * (col_x + col_y) + col_x + j] =
sym::zp_copy(y[i * col_y + j]);
} }
} }
return xy; return xy;
...@@ -82,7 +83,8 @@ symZpMat sym::matrix_multiply(symZpMat x, symZpMat y, int row_x, int row_y, ...@@ -82,7 +83,8 @@ symZpMat sym::matrix_multiply(symZpMat x, symZpMat y, int row_x, int row_y,
xy[i * row_y + j] = sym::zp_zero(modular); xy[i * row_y + j] = sym::zp_zero(modular);
for (int k = 0; k < row_y; k++) { for (int k = 0; k < row_y; k++) {
xy[i * col_y + j] = sym::zp_add( 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])); xy[i * col_y + j],
sym::zp_mul(x[i * row_y + k], y[k * col_y + j]));
} }
} }
} }
...@@ -104,18 +106,19 @@ symZpMat sym::matrix_inverse(symZpMat x, int size, symPoint modular) { ...@@ -104,18 +106,19 @@ symZpMat sym::matrix_inverse(symZpMat x, int size, symPoint modular) {
if (i == j && !sym::zp_cmp_int(row_echelon[i * 2 * size + j], 1)) { 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]); temp_multiplier = sym::zp_inv(row_echelon[i * 2 * size + i]);
for (int k = i; k < size * 2; k++) { for (int k = i; k < size * 2; k++) {
row_echelon[j * 2 * size + k] = row_echelon[j * 2 * size + k] = sym::zp_mul(
sym::zp_mul(row_echelon[j * 2 * size + k], temp_multiplier); 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) { if (i != j) {
temp_multiplier = sym::zp_copy(row_echelon[j * 2 * size + i]); temp_multiplier = sym::zp_copy(row_echelon[j * 2 * size + i]);
for (int k = i; k < size * 2; k++) { for (int k = i; k < size * 2; k++) {
temp_neg = temp_neg = sym::zp_mul(temp_multiplier,
sym::zp_mul(temp_multiplier, row_echelon[i * 2 * size + k]); row_echelon[i * 2 * size + k]);
temp_neg = sym::zp_neg(temp_neg); temp_neg = sym::zp_neg(temp_neg);
row_echelon[j * 2 * size + k] = row_echelon[j * 2 * size + k] =
sym::zp_add(row_echelon[j * 2 * size + k], temp_neg); sym::zp_add(row_echelon[j * 2 * size + k], temp_neg);
...@@ -129,7 +132,8 @@ symZpMat sym::matrix_inverse(symZpMat x, int size, symPoint modular) { ...@@ -129,7 +132,8 @@ symZpMat sym::matrix_inverse(symZpMat x, int size, symPoint modular) {
for (int j = i - 1; j >= 0; j--) { for (int j = i - 1; j >= 0; j--) {
temp_multiplier = sym::zp_copy(row_echelon[j * 2 * size + i]); temp_multiplier = sym::zp_copy(row_echelon[j * 2 * size + i]);
for (int k = i; k < size * 2; k++) { 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_mul(temp_multiplier, row_echelon[i * 2 * size + k]);
temp_neg = sym::zp_neg(temp_neg); temp_neg = sym::zp_neg(temp_neg);
row_echelon[j * 2 * size + k] = row_echelon[j * 2 * size + k] =
sym::zp_add(row_echelon[j * 2 * size + k], temp_neg); sym::zp_add(row_echelon[j * 2 * size + k], temp_neg);
...@@ -142,7 +146,8 @@ symZpMat sym::matrix_inverse(symZpMat x, int size, symPoint modular) { ...@@ -142,7 +146,8 @@ symZpMat sym::matrix_inverse(symZpMat x, int size, symPoint modular) {
xi = (symZpMat)malloc(sizeof(symZp) * size * size); xi = (symZpMat)malloc(sizeof(symZp) * size * size);
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) { for (int j = 0; j < size; j++) {
xi[i * size + j] = sym::zp_copy(row_echelon[i * 2 * size + size + j]); xi[i * size + j] =
sym::zp_copy(row_echelon[i * 2 * size + size + j]);
} }
} }
return xi; return xi;
......
#include "asym-field.hpp" #include "asym-field.hpp"
#include "gtest/gtest.h"
int test_zp_zero(asymPoint N) { namespace PPANN {
asymZp x = asym::zp_zero(N);
return asym::zp_cmp_int(x, 0); class AsymFieldTest : public ::testing::Test {
protected:
bn_t N_;
AsymFieldTest() {
// Init core and setup.
core_init();
pc_param_set_any();
// Get order.
pc_get_ord(N_);
} }
};
int test_zp_one(asymPoint N) { TEST_F(AsymFieldTest, ZpZero) {
asymZp x = asym::zp_one(N); asymZp x = asym::zp_zero(N_);
return asym::zp_cmp_int(x, 1); ASSERT_EQ(1, asym::zp_cmp_int(x, 0));
} }
int test_zp_copy(asymPoint N) { TEST_F(AsymFieldTest, ZpOne) {
asymZp x = asym::zp_from_int(10, N); asymZp x = asym::zp_one(N_);
ASSERT_EQ(1, asym::zp_cmp_int(x, 1));
}
TEST_F(AsymFieldTest, ZpCopy) {
asymZp x = asym::zp_from_int(10, N_);
asymZp y = asym::zp_copy(x); asymZp y = asym::zp_copy(x);
return asym::zp_cmp(x, y); ASSERT_EQ(1, asym::zp_cmp(x, y));
} }
int test_zp_from_int(asymPoint N) { TEST_F(AsymFieldTest, ZpFromInt) {
asymZp x = asym::zp_from_int(3, N); asymZp x = asym::zp_from_int(3, N_);
return asym::zp_cmp_int(x, 3); ASSERT_EQ(1, asym::zp_cmp_int(x, 3));
} }
int test_zp_add(asymPoint N) { TEST_F(AsymFieldTest, ZpAdd) {
asymZp x = asym::zp_from_int(10, N); asymZp x = asym::zp_from_int(10, N_);
asymZp y = asym::zp_from_int(20, N); asymZp y = asym::zp_from_int(20, N_);
asymZp z = asym::zp_add(x, y); asymZp z = asym::zp_add(x, y);
return asym::zp_cmp_int(z, 30); ASSERT_EQ(1, asym::zp_cmp_int(z, 30));
} }
int test_zp_neg(asymPoint N) { TEST_F(AsymFieldTest, ZpNeg) {
asymZp x = asym::rand_zp(N); asymZp x = asym::rand_zp(N_);
asymZp y = asym::zp_neg(x); asymZp y = asym::zp_neg(x);
asymZp z = asym::zp_add(x, y); asymZp z = asym::zp_add(x, y);
return asym::zp_cmp_int(z, 0); ASSERT_EQ(1, asym::zp_cmp_int(z, 0));
} }
int test_zp_mul(asymPoint N) { TEST_F(AsymFieldTest, ZpMul) {
asymZp x = asym::zp_from_int(10, N); asymZp x = asym::zp_from_int(10, N_);
asymZp y = asym::zp_from_int(20, N); asymZp y = asym::zp_from_int(20, N_);
asymZp z = asym::zp_mul(x, y); asymZp z = asym::zp_mul(x, y);
return asym::zp_cmp_int(z, 200); ASSERT_EQ(1, asym::zp_cmp_int(z, 200));
} }
int test_zp_inv(asymPoint N) { TEST_F(AsymFieldTest, ZpInv) {
asymZp x = asym::rand_zp(N); asymZp x = asym::rand_zp(N_);
asymZp y = asym::zp_inv(x); asymZp y = asym::zp_inv(x);
asymZp z = asym::zp_mul(x, y); asymZp z = asym::zp_mul(x, y);
return asym::zp_cmp_int(z, 1); ASSERT_EQ(1, asym::zp_cmp_int(z, 1));
} }
int main() { } // namespace PPANN
// 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; int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
} }
...@@ -3,7 +3,8 @@ ...@@ -3,7 +3,8 @@
int test_g1_generator() { int test_g1_generator() {
asymG1 x; asymG1 x;
asym::g1_gen(x); asym::g1_gen(x);
// Specifically this line goes wrong... I don't know how to further debug it. // Specifically this line goes wrong... I don't know how to further debug
// it.
return g1_is_valid(x); return g1_is_valid(x);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment