From 471521c68356c7c844dedec04bb8aa9814422e9f Mon Sep 17 00:00:00 2001 From: Sakamoto Souta Date: Mon, 31 Jul 2023 02:55:36 +0000 Subject: [PATCH 1/9] refactoring math --- .../computation_container/math/math.cpp | 109 ++++++------------ 1 file changed, 33 insertions(+), 76 deletions(-) diff --git a/packages/server/computation_container/math/math.cpp b/packages/server/computation_container/math/math.cpp index acc7bcd3c..b1bc86656 100644 --- a/packages/server/computation_container/math/math.cpp +++ b/packages/server/computation_container/math/math.cpp @@ -5,111 +5,68 @@ namespace qmpc::Math { Share sum(const std::vector &v) { - Share ret; - for (const auto &a : v) - { - ret += a; - } - return ret; + Share e(FixedPoint(0)); + return std::accumulate(v.begin(), v.end(), e); } -Share smean(const std::vector &v) +Share smean(const std::vector &v) { return sum(v) / FixedPoint(std::size(v)); } + +std::vector deviation(std::vector v) { - // Share avg(FixedPoint(0.0)); - // for (int i = 0; i < size; i++) - // { - // avg = avg + v[i]; - // } - Share ret{}; - for (auto a : v) + Share avg = smean(v); + for (auto &s : v) { - ret += a; + s -= avg; } - int size = std::size(v); - ret /= FixedPoint(size); - - return ret; + return v; } -Share variance(std::vector &v) +Share variance(const std::vector &v) { - Share avg; - - avg = smean(v); - std::vector var; - for (auto &a : v) - { - var.emplace_back(a - avg); - } - auto varVec = var * var; - Share ret{}; - for (auto &a : varVec) - { - ret += a; - } - // FPのresolutionの制約により、FPだと1000より大きい数で割れないためdoubleで割っている - // double var_d = var.getDoubleVal(); - int size = std::size(v); - ret /= FixedPoint(size); - - return ret; + auto dev = deviation(v); + auto var = dev * dev; + return smean(var); } -FixedPoint stdev(std::vector &v) +FixedPoint stdev(const std::vector &v) { - Share var; - var = variance(v); + Share var = variance(v); + FixedPoint var_val = open_and_recons(var); - FixedPoint stdev = open_and_recons(var); - auto value = stdev.getDoubleVal(); + auto value = var_val.getDoubleVal(); if (value < 0) { value = 0; } - auto r = sqrt(value); - FixedPoint ret{r}; - return ret; + return FixedPoint(sqrt(value)); } -Share correl(std::vector &x, std::vector &y) + +Share covariance(const std::vector &x, const std::vector &y) +{ + auto devX = deviation(x); + auto devY = deviation(y); + auto devXY = devX * devY; + return smean(devXY); +} + +Share correl(const std::vector &x, const std::vector &y) { - int sizex = (int)x.size(); - int sizey = (int)y.size(); + size_t n = x.size(); - if (sizex != sizey) + if (n != y.size()) { qmpc::Log::throw_with_trace(std::runtime_error("input Size is not Equal")); } - Share aveX = smean(x); - Share aveY = smean(y); FixedPoint stdeX = stdev(x); FixedPoint stdeY = stdev(y); + // 0除算 if (stdeX == FixedPoint(0) || stdeY == FixedPoint(0)) { - QMPC_LOG_ERROR("correl returns 0 when stdev is 0"); - return Share(FixedPoint(0)); + qmpc::Log::throw_with_trace(std::runtime_error("correl Div0 error")); } - int n = sizex; - std::vector tmpX; - tmpX.reserve(n); - std::vector tmpY; - tmpY.reserve(n); - for (int i = 0; i < n; ++i) - { - tmpX.emplace_back(x[i] - aveX); - tmpY.emplace_back(y[i] - aveY); - } - auto tmpVec = tmpX * tmpY; - Share ret{}; - for (auto &r : tmpVec) - { - ret += r; - } - ret /= stdeX; - ret /= stdeY; - ret /= FixedPoint(n); - return ret; + return covariance(x, y) / (stdeX * stdeY); } Share exp(const Share &x) From be5358a1318f1ec9eb16056253b66fd27e23af99 Mon Sep 17 00:00:00 2001 From: Sakamoto Souta Date: Mon, 31 Jul 2023 02:57:21 +0000 Subject: [PATCH 2/9] del unused --- .../computation_container/math/math.cpp | 27 ------------------- .../computation_container/math/math.hpp | 4 --- .../test/integration_test/math_test.hpp | 24 ----------------- 3 files changed, 55 deletions(-) diff --git a/packages/server/computation_container/math/math.cpp b/packages/server/computation_container/math/math.cpp index b1bc86656..4c0791db0 100644 --- a/packages/server/computation_container/math/math.cpp +++ b/packages/server/computation_container/math/math.cpp @@ -68,31 +68,4 @@ Share correl(const std::vector &x, const std::vector &y) return covariance(x, y) / (stdeX * stdeY); } - -Share exp(const Share &x) -{ - // Nはマクローリン展開時の項数 - // 1+x+x^2 ... x^N-1 - constexpr int N = 100; - auto *conf = Config::getInstance(); - Share ret; - if (conf->sp_id == conf->party_id) - { - ret += 1; - } - std::vector px(N); - std::vector k(N); - px[0] = ret; - k[0] = 1; - for (int i = 1; i < N; ++i) - { - k[i] = k[i - 1] * i; - px[i] = px[i - 1] * x; - } - for (int i = 1; i < N; ++i) - { - px[i] /= k[i]; - } - return std::accumulate(px.begin(), px.end(), Share{0}); -} } // namespace qmpc::Math \ No newline at end of file diff --git a/packages/server/computation_container/math/math.hpp b/packages/server/computation_container/math/math.hpp index d33b0da9c..aae3b1895 100644 --- a/packages/server/computation_container/math/math.hpp +++ b/packages/server/computation_container/math/math.hpp @@ -17,8 +17,4 @@ Share variance(std::vector &v); FixedPoint stdev(std::vector &v); // 相関係数 Share correl(std::vector &x, std::vector &y); -Share exp(const Share &x); -Share sigmoid(const Share &x, const FixedPoint &a = 1); -Share open_sigmoid(const Share &x_s, const FixedPoint &a = 1); -Share open_sigmoid_vector(const std::vector &v_s, const FixedPoint &a = 1); } // namespace qmpc::Math \ No newline at end of file diff --git a/packages/server/computation_container/test/integration_test/math_test.hpp b/packages/server/computation_container/test/integration_test/math_test.hpp index b60edf22e..a69c76dfd 100644 --- a/packages/server/computation_container/test/integration_test/math_test.hpp +++ b/packages/server/computation_container/test/integration_test/math_test.hpp @@ -225,30 +225,6 @@ TEST(MathTest, Correl_large) QMPC_LOG_INFO(correl_rec.getStrVal()); } -TEST(MathTest, ExpTest) -{ - Config *conf = Config::getInstance(); - int n_parties = conf->n_parties; - - // x = n_parties; - Share x{1}; - auto start = std::chrono::system_clock::now(); - - auto exp_n = qmpc::Math::exp(x); - double expect = std::exp(n_parties); - auto exp_n_rec = open_and_recons(exp_n); - auto end = std::chrono::system_clock::now(); - auto dur = end - start; - - // 計算に要した時間をミリ秒(1/1000秒)に変換して表示 - auto msec = std::chrono::duration_cast(dur).count(); - - QMPC_LOG_INFO("share exp time is {}", msec); - QMPC_LOG_INFO("share exp_n is {}", exp_n_rec); - QMPC_LOG_INFO("expect exp_n is {}", expect); - - EXPECT_NEAR(expect, exp_n_rec.getDoubleVal(), 0.001); -} TEST(MathTest, correlVecExceptionTest) { constexpr int N = 28000; From a0fad25a88e4f4f8340ac732b02ff078fdc75c6a Mon Sep 17 00:00:00 2001 From: Sakamoto Souta Date: Mon, 31 Jul 2023 03:17:35 +0000 Subject: [PATCH 3/9] try catch --- .../test/integration_test/math_test.hpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/server/computation_container/test/integration_test/math_test.hpp b/packages/server/computation_container/test/integration_test/math_test.hpp index a69c76dfd..404099ba6 100644 --- a/packages/server/computation_container/test/integration_test/math_test.hpp +++ b/packages/server/computation_container/test/integration_test/math_test.hpp @@ -163,11 +163,21 @@ TEST(MathTest, Correl) TEST(MathTest, Correl_0div) { std::vector x = {FixedPoint("2.0"), FixedPoint("2.0"), FixedPoint("2.0")}; - std::vector y = {FixedPoint("10.0"), FixedPoint("10.0"), FixedPoint("10.0")}; + std::vector y = {FixedPoint("9.0"), FixedPoint("10.0"), FixedPoint("11.0")}; - Share correl_rec = qmpc::Math::correl(x, y); - FixedPoint target = open_and_recons(correl_rec); - EXPECT_NEAR(target.getDoubleVal(), 0.0, 0.001); + try + { + Share correl = qmpc::Math::correl(x, y); + } + catch (std::exception e) + { + QMPC_LOG_INFO(e.what()); + EXPECT_TRUE(true); + QMPC_LOG_INFO("TestCorrel_0div \033[32m Succeed \033[m"); + return; + } + EXPECT_TRUE(false); + QMPC_LOG_INFO("TestCorrel_0div \033[32m Succeed \033[m"); } TEST(MathTest, Correl_size) From 414e4482475d1962550398b21f7e09f6cf103257 Mon Sep 17 00:00:00 2001 From: Sakamoto Souta Date: Mon, 31 Jul 2023 03:31:36 +0000 Subject: [PATCH 4/9] refactoring math.hpp --- .../server/computation_container/math/math.hpp | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/packages/server/computation_container/math/math.hpp b/packages/server/computation_container/math/math.hpp index aae3b1895..4592fc048 100644 --- a/packages/server/computation_container/math/math.hpp +++ b/packages/server/computation_container/math/math.hpp @@ -1,8 +1,5 @@ #pragma once -#include -#include -#include -#include +#include #include #include "share/share.hpp" @@ -12,9 +9,9 @@ namespace qmpc::Math using Share = qmpc::Share::Share; Share sum(const std::vector &v); Share smean(const std::vector &v); -Share variance(std::vector &v); -// 標準偏差 -FixedPoint stdev(std::vector &v); -// 相関係数 -Share correl(std::vector &x, std::vector &y); +std::vector deviation(std::vector v); +Share variance(const std::vector &v); +FixedPoint stdev(const std::vector &v); +Share covariance(const std::vector &x, const std::vector &y); +Share correl(const std::vector &x, const std::vector &y); } // namespace qmpc::Math \ No newline at end of file From 123c95f19f089ca130cbf2fddf01511465f6d5a3 Mon Sep 17 00:00:00 2001 From: Sakamoto Souta Date: Wed, 9 Aug 2023 07:25:14 +0000 Subject: [PATCH 5/9] fix --- packages/server/computation_container/math/math.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/server/computation_container/math/math.cpp b/packages/server/computation_container/math/math.cpp index 4c0791db0..77130f04f 100644 --- a/packages/server/computation_container/math/math.cpp +++ b/packages/server/computation_container/math/math.cpp @@ -8,7 +8,11 @@ Share sum(const std::vector &v) Share e(FixedPoint(0)); return std::accumulate(v.begin(), v.end(), e); } -Share smean(const std::vector &v) { return sum(v) / FixedPoint(std::size(v)); } +Share smean(const std::vector &v) +{ + assert(std::size(v)); + return sum(v) / FixedPoint(std::size(v)); +} std::vector deviation(std::vector v) { @@ -63,7 +67,8 @@ Share correl(const std::vector &x, const std::vector &y) // 0除算 if (stdeX == FixedPoint(0) || stdeY == FixedPoint(0)) { - qmpc::Log::throw_with_trace(std::runtime_error("correl Div0 error")); + QMPC_LOG_ERROR("correl returns 0 when stdev is 0"); + return Share(FixedPoint(0)); } return covariance(x, y) / (stdeX * stdeY); From 592bf94877b3e15eafbb3db7d9573e9288f0a281 Mon Sep 17 00:00:00 2001 From: Sakamoto Souta Date: Wed, 9 Aug 2023 07:26:10 +0000 Subject: [PATCH 6/9] fix space --- packages/server/computation_container/math/math.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/server/computation_container/math/math.cpp b/packages/server/computation_container/math/math.cpp index 77130f04f..2f547fa3f 100644 --- a/packages/server/computation_container/math/math.cpp +++ b/packages/server/computation_container/math/math.cpp @@ -8,6 +8,7 @@ Share sum(const std::vector &v) Share e(FixedPoint(0)); return std::accumulate(v.begin(), v.end(), e); } + Share smean(const std::vector &v) { assert(std::size(v)); @@ -55,7 +56,6 @@ Share covariance(const std::vector &x, const std::vector &y) Share correl(const std::vector &x, const std::vector &y) { size_t n = x.size(); - if (n != y.size()) { qmpc::Log::throw_with_trace(std::runtime_error("input Size is not Equal")); From 3f37d506192c7a7013e6ebf8da2b48323ba66a66 Mon Sep 17 00:00:00 2001 From: Sakamoto Souta Date: Wed, 9 Aug 2023 07:48:10 +0000 Subject: [PATCH 7/9] del n --- packages/server/computation_container/math/math.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/server/computation_container/math/math.cpp b/packages/server/computation_container/math/math.cpp index 2f547fa3f..57256b824 100644 --- a/packages/server/computation_container/math/math.cpp +++ b/packages/server/computation_container/math/math.cpp @@ -55,8 +55,7 @@ Share covariance(const std::vector &x, const std::vector &y) Share correl(const std::vector &x, const std::vector &y) { - size_t n = x.size(); - if (n != y.size()) + if (x.size() != y.size()) { qmpc::Log::throw_with_trace(std::runtime_error("input Size is not Equal")); } From c0b735997d9c882001ee3a2b7c3b8ff0a18669ac Mon Sep 17 00:00:00 2001 From: Sakamoto Souta Date: Wed, 9 Aug 2023 09:27:08 +0000 Subject: [PATCH 8/9] change 10**-8 to 10**-7 --- scripts/libclient/src/tests/test_sum.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/libclient/src/tests/test_sum.py b/scripts/libclient/src/tests/test_sum.py index c678d1088..ccf8f817d 100644 --- a/scripts/libclient/src/tests/test_sum.py +++ b/scripts/libclient/src/tests/test_sum.py @@ -22,9 +22,9 @@ pd.DataFrame([2.0*10**18])), # small data case - (data_frame([[10**-8], [10**-8]], columns=["s1"]), + (data_frame([[10**-7], [10**-7]], columns=["s1"]), [1], - pd.DataFrame([2.0*10**-8])), + pd.DataFrame([2.0*10**-7])), # duplicated src case (data_frame([[1, 2, 3], [4, 5, 6]], columns=["s1", "s2", "s3"]), From ceafd0631f813dddd18db4dc7a1398864c341da2 Mon Sep 17 00:00:00 2001 From: Sakamoto Souta Date: Thu, 10 Aug 2023 00:42:41 +0000 Subject: [PATCH 9/9] 0div --- .../test/integration_test/math_test.hpp | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/packages/server/computation_container/test/integration_test/math_test.hpp b/packages/server/computation_container/test/integration_test/math_test.hpp index 404099ba6..7e4787591 100644 --- a/packages/server/computation_container/test/integration_test/math_test.hpp +++ b/packages/server/computation_container/test/integration_test/math_test.hpp @@ -165,19 +165,9 @@ TEST(MathTest, Correl_0div) std::vector x = {FixedPoint("2.0"), FixedPoint("2.0"), FixedPoint("2.0")}; std::vector y = {FixedPoint("9.0"), FixedPoint("10.0"), FixedPoint("11.0")}; - try - { - Share correl = qmpc::Math::correl(x, y); - } - catch (std::exception e) - { - QMPC_LOG_INFO(e.what()); - EXPECT_TRUE(true); - QMPC_LOG_INFO("TestCorrel_0div \033[32m Succeed \033[m"); - return; - } - EXPECT_TRUE(false); - QMPC_LOG_INFO("TestCorrel_0div \033[32m Succeed \033[m"); + Share correl_rec = qmpc::Math::correl(x, y); + FixedPoint target = open_and_recons(correl_rec); + EXPECT_NEAR(target.getDoubleVal(), 0.0, 0.001); } TEST(MathTest, Correl_size)