From 3d39f324ffa7f8ee6aff17c47b8682ca6a708b90 Mon Sep 17 00:00:00 2001 From: NIRAJAN KOIRALA <31283818+n7koirala@users.noreply.github.com> Date: Fri, 29 Jul 2022 08:00:13 -0700 Subject: [PATCH 01/14] Added test for totalSums Tests added for totalSums for BGV and CKKS: 1) Input: Positive, Negative, Zeros, and Positive and Negative Values 2) Parameters: For BGV, tested p values include Fermat and non-Fermat primes. --- tests/TestEncryptedArray.cpp | 323 +++++++++++++++++++++++++++++++++++ 1 file changed, 323 insertions(+) create mode 100644 tests/TestEncryptedArray.cpp diff --git a/tests/TestEncryptedArray.cpp b/tests/TestEncryptedArray.cpp new file mode 100644 index 000000000..e5b53740b --- /dev/null +++ b/tests/TestEncryptedArray.cpp @@ -0,0 +1,323 @@ +/* Copyright (C) 2022 IBM Corp. + * This program is Licensed under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. See accompanying LICENSE file. + */ + +#include +#include +#include +#include + +#include "test_common.h" +#include "gtest/gtest.h" + +struct BGVParameters +{ + const long m; + const long p; + const long r; + const long bits; + + BGVParameters(long m, + long p, + long r, + long bits) : m(m), p(p), r(r), bits(bits){}; + + friend std::ostream& operator<<(std::ostream& os, const BGVParameters& params) + { + return os << "{" + << "m = " << params.m << ", " + << "p = " << params.p << ", " + << "r = " << params.r << ", " + << "bits = " << params.bits << "}"; + } +}; + +struct CKKSParameters +{ + const long m; + const long precision; + const long bits; + + CKKSParameters(long m, long precision, long bits) : + m(m), precision(precision), bits(bits){}; + + friend std::ostream& operator<<(std::ostream& os, + const CKKSParameters& params) + { + return os << "{" + << "m = " << params.m << ", " + << "precision = " << params.precision << ", " + << "bits = " << params.bits << "}"; + } +}; + +class TestTotalSums_BGV : public ::testing::TestWithParam +{ +protected: + const long m; + const long p; + const long r; + const long bits; + + helib::Context context; + helib::SecKey secretKey; + helib::PubKey publicKey; + const helib::EncryptedArray& ea; + + TestTotalSums_BGV() : + m(GetParam().m), + p(GetParam().p), + r(GetParam().r), + bits(GetParam().bits), + context(helib::ContextBuilder() + .m(m) + .p(p) + .r(r) + .bits(bits) + .build()), + secretKey(context), + publicKey( + (secretKey.GenSecKey(), addSome1DMatrices(secretKey), secretKey)), + ea(context.getEA()) + {} + + virtual void SetUp() override + { + helib::setupDebugGlobals(&secretKey, context.shareEA()); + } + + virtual void TearDown() override { helib::cleanupDebugGlobals(); } +}; + + +class TestTotalSums_CKKS : public ::testing::TestWithParam +{ +protected: + const long m; + const long precision; + const long bits; + + helib::Context context; + helib::SecKey secretKey; + helib::PubKey publicKey; + const helib::EncryptedArray& ea; + + TestTotalSums_CKKS() : + m(GetParam().m), + precision(GetParam().precision), + bits(GetParam().bits), + context(helib::ContextBuilder() + .m(m) + .precision(precision) + .bits(bits) + .build()), + secretKey(context), + publicKey( + (secretKey.GenSecKey(), addSome1DMatrices(secretKey), secretKey)), + ea(context.getEA()) + {} + + virtual void SetUp() override + { + helib::setupDebugGlobals(&secretKey, context.shareEA()); + } + + virtual void TearDown() override { helib::cleanupDebugGlobals(); } +}; + +//tests here + +TEST_P(TestTotalSums_BGV, TSumsWorkCorrForPosValBGV) +{ + std::vector data(context.getEA().size()); + std::iota(data.begin(), data.end(), 1); + + helib::Ptxt ptxt(context, data); + ptxt.totalSums(); + + helib::Ctxt ctxt(publicKey); + publicKey.Encrypt(ctxt, ptxt); + totalSums(context.getEA(), ctxt); + + helib::Ptxt post_decryption(context); + secretKey.Decrypt(post_decryption, ctxt); + + + for (std::size_t i = 0; i < ptxt.size(); ++i) { + EXPECT_EQ(post_decryption[i], ptxt[i]); + } +} + +TEST_P(TestTotalSums_BGV, TSumsWorkCorrForNegValBGV) +{ + std::vector data(context.getEA().size()); + std::iota(data.begin(), data.end(), -context.getEA().size()); + + helib::Ptxt ptxt(context, data); + ptxt.totalSums(); + + helib::Ctxt ctxt(publicKey); + publicKey.Encrypt(ctxt, ptxt); + totalSums(context.getEA(), ctxt); + + helib::Ptxt post_decryption(context); + secretKey.Decrypt(post_decryption, ctxt); + + + for (std::size_t i = 0; i < ptxt.size(); ++i) { + EXPECT_EQ(post_decryption[i], ptxt[i]); + } +} + +TEST_P(TestTotalSums_BGV, TSumsWorkCorrForPosNegValBGV) +{ + std::vector data(context.getEA().size()); + + for (std::size_t i=0; i ptxt(context, data); + ptxt.totalSums(); + + helib::Ctxt ctxt(publicKey); + publicKey.Encrypt(ctxt, ptxt); + totalSums(context.getEA(), ctxt); + + helib::Ptxt post_decryption(context); + secretKey.Decrypt(post_decryption, ctxt); + + for (std::size_t i = 0; i < ptxt.size(); ++i) { + EXPECT_EQ(post_decryption[i], ptxt[i]); + } +} + +TEST_P(TestTotalSums_BGV, TSumsWorkCorrForZeroValBGV) +{ + std::vector data(context.getEA().size(), 0); + + helib::Ptxt ptxt(context, data); + ptxt.totalSums(); + + helib::Ctxt ctxt(publicKey); + publicKey.Encrypt(ctxt, ptxt); + totalSums(context.getEA(), ctxt); + + helib::Ptxt post_decryption(context); + secretKey.Decrypt(post_decryption, ctxt); + + for (std::size_t i = 0; i < ptxt.size(); ++i) { + EXPECT_EQ(post_decryption[i], ptxt[i]); + } +} + +TEST_P(TestTotalSums_CKKS, TSumsWorkCorrForZeroValCKKS) +{ + std::vector> data(context.getEA().size(), 0.0); + + helib::Ptxt ptxt(context, data); + ptxt.totalSums(); + + helib::Ctxt ctxt(publicKey); + publicKey.Encrypt(ctxt, ptxt); + totalSums(context.getEA(), ctxt); + + helib::Ptxt post_decryption(context); + secretKey.Decrypt(post_decryption, ctxt); + + COMPARE_CXDOUBLE_VECS(ptxt.getSlotRepr(), post_decryption.getSlotRepr()); +} + +TEST_P(TestTotalSums_CKKS, TSumsWorkCorrForPosValCKKS) +{ + std::vector> data(context.getEA().size()); + for (std::size_t i = 0; i < data.size(); ++i) { + data[i] = {i / 1.0, (i * i) / 1.0}; + } + + helib::Ptxt ptxt(context, data); + ptxt.totalSums(); + + helib::Ctxt ctxt(publicKey); + publicKey.Encrypt(ctxt, ptxt); + totalSums(context.getEA(), ctxt); + + helib::Ptxt post_decryption(context); + secretKey.Decrypt(post_decryption, ctxt); + + COMPARE_CXDOUBLE_VECS(ptxt.getSlotRepr(), post_decryption.getSlotRepr()); +} + +TEST_P(TestTotalSums_CKKS, TSumsWorkCorrForNegValCKKS) +{ + std::vector> data(context.getEA().size()); + for (std::size_t i = 0; i < data.size(); ++i) { + data[i] = {-i / 1.0, -(i * i) / 1.0}; + } + + helib::Ptxt ptxt(context, data); + ptxt.totalSums(); + + helib::Ctxt ctxt(publicKey); + publicKey.Encrypt(ctxt, ptxt); + totalSums(context.getEA(), ctxt); + + helib::Ptxt post_decryption(context); + secretKey.Decrypt(post_decryption, ctxt); + + COMPARE_CXDOUBLE_VECS(ptxt.getSlotRepr(), post_decryption.getSlotRepr()); +} + +TEST_P(TestTotalSums_CKKS, TSumsWorkCorrForPosNegValCKKS) +{ + std::vector> data(context.getEA().size()); + for (std::size_t i = 0; i < data.size(); ++i) { + if(i%2 == 0){ + data[i] = {-i / 1.0, -(i * i) / 1.0}; + } + else{ + data[i] = {i / 1.0, (i * i) / 1.0}; + } + } + + helib::Ptxt ptxt(context, data); + ptxt.totalSums(); + + helib::Ctxt ctxt(publicKey); + publicKey.Encrypt(ctxt, ptxt); + totalSums(context.getEA(), ctxt); + + helib::Ptxt post_decryption(context); + secretKey.Decrypt(post_decryption, ctxt); + + COMPARE_CXDOUBLE_VECS(ptxt.getSlotRepr(), post_decryption.getSlotRepr()); +} + + +// parameters initialization +INSTANTIATE_TEST_SUITE_P(Parameters, + TestTotalSums_BGV, + ::testing::Values(BGVParameters(/*m=*/45, + /*p=*/317, + /*r=*/1, + /*bits=*/500), BGVParameters(512, /*fermat_prime=*/257, 1, 500), BGVParameters(45, 317, 1, 500), BGVParameters(288, /*fermat_prime=*/17, 1, 500), BGVParameters(45, 367, 1, 500))); + +INSTANTIATE_TEST_SUITE_P(Parameters, + TestTotalSums_CKKS, + ::testing::Values(CKKSParameters(/*m=*/64, + /*precision=*/30, + /*bits=*/500), CKKSParameters(128, 35, 500), CKKSParameters(256, 40, 500), CKKSParameters(512, 50, 500), CKKSParameters(1024, 45, 500))); + From 1522b8762295f19495eac75bc9296580adcaa304 Mon Sep 17 00:00:00 2001 From: NIRAJAN KOIRALA <31283818+n7koirala@users.noreply.github.com> Date: Fri, 29 Jul 2022 08:47:27 -0700 Subject: [PATCH 02/14] Fix totalSums Fix totalSums for non-power of 2 slot-size --- src/EncryptedArray.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/EncryptedArray.cpp b/src/EncryptedArray.cpp index 1d551a97b..e4f1242e4 100644 --- a/src/EncryptedArray.cpp +++ b/src/EncryptedArray.cpp @@ -707,32 +707,32 @@ void runningSums(const EncryptedArray& ea, Ctxt& ctxt) void totalSums(const EncryptedArray& ea, Ctxt& ctxt) { - long n = ea.size(); + long n = ea.size(); // slot-count if (n == 1) return; Ctxt orig = ctxt; - - long k = NTL::NumBits(n); + + long b = NTL::NumBits(n); long e = 1; - for (long i = k - 2; i >= 0; i--) { - Ctxt tmp1 = ctxt; - ea.rotate(tmp1, e); - ctxt += tmp1; // ctxt = ctxt + (ctxt >>> e) + for (long i = b - 2; i >= 0; i--) { + + Ctxt tmp1 = orig; + helib::rotate(tmp1, e); + orig += tmp1; e = 2 * e; if (NTL::bit(n, i)) { Ctxt tmp2 = orig; - ea.rotate(tmp2, e); - ctxt += tmp2; // ctxt = ctxt + (orig >>> e) - // NOTE: we could have also computed - // ctxt = (ctxt >>> e) + orig, however, - // this would give us greater depth/noise + helib::rotate(tmp2, 1); + tmp2 += ctxt; + orig = tmp2; e += 1; } } + ctxt = orig; } // Linearized polynomials. From 466c7d6dfca244350eb89abeafdbd95290ee4fab Mon Sep 17 00:00:00 2001 From: NIRAJAN KOIRALA <31283818+n7koirala@users.noreply.github.com> Date: Fri, 29 Jul 2022 09:42:45 -0700 Subject: [PATCH 03/14] Added TestEncryptedArray test TestEncryptedArray test added --- tests/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e0bf08879..e0b52243b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -30,6 +30,7 @@ if (NOT ONLY_ADD_TEST) "TestClonedPtr.cpp" "TestContext.cpp" "TestCtxt.cpp" + "TestEncryptedArray.cpp" "TestErrorHandling.cpp" "TestHEXL.cpp" "TestLogging.cpp" @@ -148,6 +149,7 @@ set(TEST_NAMES "TestClonedPtr" "TestContext" "TestCtxt" + "TestEncryptedArray" "TestErrorHandling" "TestFatBootstrappingWithMultiplications" "TestHEXL" From 051ffd6ff252e1dd5024bf68b5a948e89b9f63d3 Mon Sep 17 00:00:00 2001 From: Flavio Bergamaschi Date: Mon, 1 Aug 2022 15:53:49 +0100 Subject: [PATCH 04/14] Update tests/TestEncryptedArray.cpp Correcting Copyright message --- tests/TestEncryptedArray.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TestEncryptedArray.cpp b/tests/TestEncryptedArray.cpp index e5b53740b..ada3dcd66 100644 --- a/tests/TestEncryptedArray.cpp +++ b/tests/TestEncryptedArray.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2022 IBM Corp. +/* Copyright (C) 2022 Intel Corporation * This program is Licensed under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at From 5eb99dd2a0e5a9ff9bb1934381b58255749c1c4b Mon Sep 17 00:00:00 2001 From: NIRAJAN KOIRALA <31283818+n7koirala@users.noreply.github.com> Date: Mon, 1 Aug 2022 08:07:54 -0700 Subject: [PATCH 05/14] Update EncryptedArray.cpp --- src/EncryptedArray.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EncryptedArray.cpp b/src/EncryptedArray.cpp index e4f1242e4..692761ccf 100644 --- a/src/EncryptedArray.cpp +++ b/src/EncryptedArray.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2012-2020 IBM Corp. +/* Copyright (C) 2012-2022 Intel Corp. * This program is Licensed under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at From c303b6ef4780b530bdcb6e495429599f168ec0cd Mon Sep 17 00:00:00 2001 From: Akshaya Jagannadharao Date: Mon, 1 Aug 2022 14:08:57 -0700 Subject: [PATCH 06/14] test if CI fails when HEXL ON --- .github/workflows/github-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/github-ci.yml b/.github/workflows/github-ci.yml index e34c76dca..5335501fb 100644 --- a/.github/workflows/github-ci.yml +++ b/.github/workflows/github-ci.yml @@ -37,7 +37,7 @@ jobs: matrix: # TODO: shared builds package_build: [ON, OFF] - hexl: [ON, OFF] + hexl: [ON] c_compiler: [gcc, clang] os: [macos-latest, ubuntu-20.04] include: # Use g++ with gcc only and clang++ with clang only From 42f01b425ec5c34b85239402b58c02ed70f37b76 Mon Sep 17 00:00:00 2001 From: Akshaya Jagannadharao Date: Mon, 1 Aug 2022 14:23:20 -0700 Subject: [PATCH 07/14] test CI fails with gcc - Add ON/OFF matrix flag for HEXL --- .github/workflows/github-ci.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/github-ci.yml b/.github/workflows/github-ci.yml index 5335501fb..e2c06e761 100644 --- a/.github/workflows/github-ci.yml +++ b/.github/workflows/github-ci.yml @@ -34,11 +34,12 @@ jobs: run: shell: bash strategy: + fail-fast: false matrix: # TODO: shared builds package_build: [ON, OFF] - hexl: [ON] - c_compiler: [gcc, clang] + hexl: [ON, OFF] + c_compiler: [gcc] os: [macos-latest, ubuntu-20.04] include: # Use g++ with gcc only and clang++ with clang only - c_compiler: gcc From a0244a63d99241303dd7390c0fb2d35b2a2d7056 Mon Sep 17 00:00:00 2001 From: Akshaya Jagannadharao Date: Mon, 1 Aug 2022 14:25:11 -0700 Subject: [PATCH 08/14] remove clang flag from 'include' in matrix strat --- .github/workflows/github-ci.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/github-ci.yml b/.github/workflows/github-ci.yml index e2c06e761..b76a43454 100644 --- a/.github/workflows/github-ci.yml +++ b/.github/workflows/github-ci.yml @@ -44,8 +44,6 @@ jobs: include: # Use g++ with gcc only and clang++ with clang only - c_compiler: gcc cxx_compiler: g++ - - c_compiler: clang - cxx_compiler: clang++ exclude: # Skip HEXL package build - package_build: ON hexl: ON From 442ef8a67b457cbd3a52d84b610d68836540aab3 Mon Sep 17 00:00:00 2001 From: Akshaya Jagannadharao Date: Mon, 1 Aug 2022 14:44:46 -0700 Subject: [PATCH 09/14] leave fail-fast false in matrix strategy --- .github/workflows/github-ci.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/github-ci.yml b/.github/workflows/github-ci.yml index b76a43454..57626530a 100644 --- a/.github/workflows/github-ci.yml +++ b/.github/workflows/github-ci.yml @@ -39,11 +39,13 @@ jobs: # TODO: shared builds package_build: [ON, OFF] hexl: [ON, OFF] - c_compiler: [gcc] + c_compiler: [gcc, clang] os: [macos-latest, ubuntu-20.04] include: # Use g++ with gcc only and clang++ with clang only - c_compiler: gcc cxx_compiler: g++ + - c_compiler: clang + cxx_compiler: clang++ exclude: # Skip HEXL package build - package_build: ON hexl: ON From 13b519de6b9d9af38e8f23cbe23bc253227b4ccd Mon Sep 17 00:00:00 2001 From: Akshaya Jagannadharao Date: Mon, 1 Aug 2022 15:18:15 -0700 Subject: [PATCH 10/14] temp install valgrind valgrind to be removed before merging --- .github/workflows/github-ci.yml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/workflows/github-ci.yml b/.github/workflows/github-ci.yml index 57626530a..e4bb2cf5d 100644 --- a/.github/workflows/github-ci.yml +++ b/.github/workflows/github-ci.yml @@ -51,6 +51,14 @@ jobs: hexl: ON steps: - uses: actions/checkout@v2 + - name: System Check + run: | + clang --version + gcc -v + - name: Install Valgrind [REMOVE BEFORE MERGE] + run: + sudo apt-get update + sudo apt install -y valgrind - run: | set -x env @@ -59,6 +67,6 @@ jobs: ./ci/build_install_lib.sh "${{ matrix.package_build }}" ${{ env.INSTALL_PREFIX }} \ ${{ matrix.c_compiler }} ${{ matrix.cxx_compiler }} \ ${{ matrix.hexl }} "./hexl/lib/cmake/hexl-1.2.4" - ./ci/test_lib.sh + valgrind --leak-check=yes --track-origins=yes --verbose ./ci/test_lib.sh ./ci/build_test_consumer.sh "examples" "${{ matrix.package_build }}" ${{ env.INSTALL_PREFIX }} ./ci/build_test_consumer.sh "utils" "${{ matrix.package_build }}" ${{ env.INSTALL_PREFIX }} From 0e4d18091865a41943f800acb1c6e83e791b0151 Mon Sep 17 00:00:00 2001 From: Akshaya Jagannadharao Date: Mon, 1 Aug 2022 15:19:34 -0700 Subject: [PATCH 11/14] CI error - use semicolon --- .github/workflows/github-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/github-ci.yml b/.github/workflows/github-ci.yml index e4bb2cf5d..c844b4c53 100644 --- a/.github/workflows/github-ci.yml +++ b/.github/workflows/github-ci.yml @@ -57,7 +57,7 @@ jobs: gcc -v - name: Install Valgrind [REMOVE BEFORE MERGE] run: - sudo apt-get update + sudo apt-get update; sudo apt install -y valgrind - run: | set -x From 0927f650e2e208078b4bf3ce85ccc5739d0fd90e Mon Sep 17 00:00:00 2001 From: Akshaya Jagannadharao Date: Mon, 1 Aug 2022 16:26:41 -0700 Subject: [PATCH 12/14] remove valgrind debugging keeping System Check step and 'fail-fast: false' --- .github/workflows/github-ci.yml | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.github/workflows/github-ci.yml b/.github/workflows/github-ci.yml index c844b4c53..8d8874552 100644 --- a/.github/workflows/github-ci.yml +++ b/.github/workflows/github-ci.yml @@ -55,10 +55,6 @@ jobs: run: | clang --version gcc -v - - name: Install Valgrind [REMOVE BEFORE MERGE] - run: - sudo apt-get update; - sudo apt install -y valgrind - run: | set -x env @@ -67,6 +63,6 @@ jobs: ./ci/build_install_lib.sh "${{ matrix.package_build }}" ${{ env.INSTALL_PREFIX }} \ ${{ matrix.c_compiler }} ${{ matrix.cxx_compiler }} \ ${{ matrix.hexl }} "./hexl/lib/cmake/hexl-1.2.4" - valgrind --leak-check=yes --track-origins=yes --verbose ./ci/test_lib.sh + ./ci/test_lib.sh ./ci/build_test_consumer.sh "examples" "${{ matrix.package_build }}" ${{ env.INSTALL_PREFIX }} ./ci/build_test_consumer.sh "utils" "${{ matrix.package_build }}" ${{ env.INSTALL_PREFIX }} From 9953cc4cf8b5615185dd309f043c0c26d13b9c5f Mon Sep 17 00:00:00 2001 From: Nirajan Koirala <31283818+n7koirala@users.noreply.github.com> Date: Fri, 5 Aug 2022 15:36:35 -0700 Subject: [PATCH 13/14] Names changed Names changed for struct and class --- tests/TestEncryptedArray.cpp | 59 ++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/tests/TestEncryptedArray.cpp b/tests/TestEncryptedArray.cpp index ada3dcd66..aabf3d3a4 100644 --- a/tests/TestEncryptedArray.cpp +++ b/tests/TestEncryptedArray.cpp @@ -18,19 +18,19 @@ #include "test_common.h" #include "gtest/gtest.h" -struct BGVParameters +struct BGVParametersT { const long m; const long p; const long r; const long bits; - BGVParameters(long m, + BGVParametersT(long m, long p, long r, long bits) : m(m), p(p), r(r), bits(bits){}; - friend std::ostream& operator<<(std::ostream& os, const BGVParameters& params) + friend std::ostream& operator<<(std::ostream& os, const BGVParametersT& params) { return os << "{" << "m = " << params.m << ", " @@ -40,17 +40,17 @@ struct BGVParameters } }; -struct CKKSParameters +struct CKKSParametersT { const long m; const long precision; const long bits; - CKKSParameters(long m, long precision, long bits) : + CKKSParametersT(long m, long precision, long bits) : m(m), precision(precision), bits(bits){}; friend std::ostream& operator<<(std::ostream& os, - const CKKSParameters& params) + const CKKSParametersT& params) { return os << "{" << "m = " << params.m << ", " @@ -59,7 +59,7 @@ struct CKKSParameters } }; -class TestTotalSums_BGV : public ::testing::TestWithParam +class TestTotalSums_BGVT : public ::testing::TestWithParam { protected: const long m; @@ -72,7 +72,7 @@ class TestTotalSums_BGV : public ::testing::TestWithParam helib::PubKey publicKey; const helib::EncryptedArray& ea; - TestTotalSums_BGV() : + TestTotalSums_BGVT() : m(GetParam().m), p(GetParam().p), r(GetParam().r), @@ -98,7 +98,7 @@ class TestTotalSums_BGV : public ::testing::TestWithParam }; -class TestTotalSums_CKKS : public ::testing::TestWithParam +class TestTotalSums_CKKST : public ::testing::TestWithParam { protected: const long m; @@ -110,7 +110,7 @@ class TestTotalSums_CKKS : public ::testing::TestWithParam helib::PubKey publicKey; const helib::EncryptedArray& ea; - TestTotalSums_CKKS() : + TestTotalSums_CKKST() : m(GetParam().m), precision(GetParam().precision), bits(GetParam().bits), @@ -135,7 +135,7 @@ class TestTotalSums_CKKS : public ::testing::TestWithParam //tests here -TEST_P(TestTotalSums_BGV, TSumsWorkCorrForPosValBGV) +TEST_P(TestTotalSums_BGVT, TSumsWorkCorrForPosValBGV) { std::vector data(context.getEA().size()); std::iota(data.begin(), data.end(), 1); @@ -156,11 +156,11 @@ TEST_P(TestTotalSums_BGV, TSumsWorkCorrForPosValBGV) } } -TEST_P(TestTotalSums_BGV, TSumsWorkCorrForNegValBGV) +TEST_P(TestTotalSums_BGVT, TSumsWorkCorrForNegValBGV) { std::vector data(context.getEA().size()); std::iota(data.begin(), data.end(), -context.getEA().size()); - + helib::Ptxt ptxt(context, data); ptxt.totalSums(); @@ -177,10 +177,10 @@ TEST_P(TestTotalSums_BGV, TSumsWorkCorrForNegValBGV) } } -TEST_P(TestTotalSums_BGV, TSumsWorkCorrForPosNegValBGV) +TEST_P(TestTotalSums_BGVT, TSumsWorkCorrForPosNegValBGV) { std::vector data(context.getEA().size()); - + for (std::size_t i=0; i data(context.getEA().size(), 0); - + helib::Ptxt ptxt(context, data); ptxt.totalSums(); @@ -224,10 +224,10 @@ TEST_P(TestTotalSums_BGV, TSumsWorkCorrForZeroValBGV) } } -TEST_P(TestTotalSums_CKKS, TSumsWorkCorrForZeroValCKKS) +TEST_P(TestTotalSums_CKKST, TSumsWorkCorrForZeroValCKKS) { std::vector> data(context.getEA().size(), 0.0); - + helib::Ptxt ptxt(context, data); ptxt.totalSums(); @@ -241,7 +241,7 @@ TEST_P(TestTotalSums_CKKS, TSumsWorkCorrForZeroValCKKS) COMPARE_CXDOUBLE_VECS(ptxt.getSlotRepr(), post_decryption.getSlotRepr()); } -TEST_P(TestTotalSums_CKKS, TSumsWorkCorrForPosValCKKS) +TEST_P(TestTotalSums_CKKST, TSumsWorkCorrForPosValCKKS) { std::vector> data(context.getEA().size()); for (std::size_t i = 0; i < data.size(); ++i) { @@ -261,7 +261,7 @@ TEST_P(TestTotalSums_CKKS, TSumsWorkCorrForPosValCKKS) COMPARE_CXDOUBLE_VECS(ptxt.getSlotRepr(), post_decryption.getSlotRepr()); } -TEST_P(TestTotalSums_CKKS, TSumsWorkCorrForNegValCKKS) +TEST_P(TestTotalSums_CKKST, TSumsWorkCorrForNegValCKKS) { std::vector> data(context.getEA().size()); for (std::size_t i = 0; i < data.size(); ++i) { @@ -281,12 +281,12 @@ TEST_P(TestTotalSums_CKKS, TSumsWorkCorrForNegValCKKS) COMPARE_CXDOUBLE_VECS(ptxt.getSlotRepr(), post_decryption.getSlotRepr()); } -TEST_P(TestTotalSums_CKKS, TSumsWorkCorrForPosNegValCKKS) +TEST_P(TestTotalSums_CKKST, TSumsWorkCorrForPosNegValCKKS) { std::vector> data(context.getEA().size()); for (std::size_t i = 0; i < data.size(); ++i) { if(i%2 == 0){ - data[i] = {-i / 1.0, -(i * i) / 1.0}; + data[i] = {-i / 1.0, -(i * i) / 1.0}; } else{ data[i] = {i / 1.0, (i * i) / 1.0}; @@ -309,15 +309,14 @@ TEST_P(TestTotalSums_CKKS, TSumsWorkCorrForPosNegValCKKS) // parameters initialization INSTANTIATE_TEST_SUITE_P(Parameters, - TestTotalSums_BGV, - ::testing::Values(BGVParameters(/*m=*/45, + TestTotalSums_BGVT, + ::testing::Values(BGVParametersT(/*m=*/45, /*p=*/317, /*r=*/1, - /*bits=*/500), BGVParameters(512, /*fermat_prime=*/257, 1, 500), BGVParameters(45, 317, 1, 500), BGVParameters(288, /*fermat_prime=*/17, 1, 500), BGVParameters(45, 367, 1, 500))); + /*bits=*/500), BGVParametersT(512, /*fermat_prime=*/257, 1, 500), BGVParametersT(45, 317, 1, 500), BGVParametersT(288, /*fermat_prime=*/17, 1, 500), BGVParametersT(45, 367, 1, 500))); INSTANTIATE_TEST_SUITE_P(Parameters, - TestTotalSums_CKKS, - ::testing::Values(CKKSParameters(/*m=*/64, + TestTotalSums_CKKST, + ::testing::Values(CKKSParametersT(/*m=*/64, /*precision=*/30, - /*bits=*/500), CKKSParameters(128, 35, 500), CKKSParameters(256, 40, 500), CKKSParameters(512, 50, 500), CKKSParameters(1024, 45, 500))); - + /*bits=*/500), CKKSParametersT(128, 35, 500), CKKSParametersT(256, 40, 500), CKKSParametersT(512, 50, 500), CKKSParametersT(1024, 45, 500))); From 5ea5e62141826fb16563de868c591ae4f9814e65 Mon Sep 17 00:00:00 2001 From: Nirajan Koirala <31283818+n7koirala@users.noreply.github.com> Date: Mon, 8 Aug 2022 09:25:22 -0700 Subject: [PATCH 14/14] Added namespace and removed unnecessary headers --- tests/TestEncryptedArray.cpp | 54 +++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/tests/TestEncryptedArray.cpp b/tests/TestEncryptedArray.cpp index aabf3d3a4..3f1f90b5b 100644 --- a/tests/TestEncryptedArray.cpp +++ b/tests/TestEncryptedArray.cpp @@ -12,25 +12,25 @@ #include #include -#include -#include #include "test_common.h" #include "gtest/gtest.h" -struct BGVParametersT +namespace { + +struct BGVParameters { const long m; const long p; const long r; const long bits; - BGVParametersT(long m, + BGVParameters(long m, long p, long r, long bits) : m(m), p(p), r(r), bits(bits){}; - friend std::ostream& operator<<(std::ostream& os, const BGVParametersT& params) + friend std::ostream& operator<<(std::ostream& os, const BGVParameters& params) { return os << "{" << "m = " << params.m << ", " @@ -40,17 +40,17 @@ struct BGVParametersT } }; -struct CKKSParametersT +struct CKKSParameters { const long m; const long precision; const long bits; - CKKSParametersT(long m, long precision, long bits) : + CKKSParameters(long m, long precision, long bits) : m(m), precision(precision), bits(bits){}; friend std::ostream& operator<<(std::ostream& os, - const CKKSParametersT& params) + const CKKSParameters& params) { return os << "{" << "m = " << params.m << ", " @@ -59,7 +59,7 @@ struct CKKSParametersT } }; -class TestTotalSums_BGVT : public ::testing::TestWithParam +class TestTotalSums_BGV : public ::testing::TestWithParam { protected: const long m; @@ -72,7 +72,7 @@ class TestTotalSums_BGVT : public ::testing::TestWithParam helib::PubKey publicKey; const helib::EncryptedArray& ea; - TestTotalSums_BGVT() : + TestTotalSums_BGV() : m(GetParam().m), p(GetParam().p), r(GetParam().r), @@ -98,7 +98,7 @@ class TestTotalSums_BGVT : public ::testing::TestWithParam }; -class TestTotalSums_CKKST : public ::testing::TestWithParam +class TestTotalSums_CKKS : public ::testing::TestWithParam { protected: const long m; @@ -110,7 +110,7 @@ class TestTotalSums_CKKST : public ::testing::TestWithParam helib::PubKey publicKey; const helib::EncryptedArray& ea; - TestTotalSums_CKKST() : + TestTotalSums_CKKS() : m(GetParam().m), precision(GetParam().precision), bits(GetParam().bits), @@ -135,7 +135,7 @@ class TestTotalSums_CKKST : public ::testing::TestWithParam //tests here -TEST_P(TestTotalSums_BGVT, TSumsWorkCorrForPosValBGV) +TEST_P(TestTotalSums_BGV, TSumsWorkCorrForPosValBGV) { std::vector data(context.getEA().size()); std::iota(data.begin(), data.end(), 1); @@ -156,7 +156,7 @@ TEST_P(TestTotalSums_BGVT, TSumsWorkCorrForPosValBGV) } } -TEST_P(TestTotalSums_BGVT, TSumsWorkCorrForNegValBGV) +TEST_P(TestTotalSums_BGV, TSumsWorkCorrForNegValBGV) { std::vector data(context.getEA().size()); std::iota(data.begin(), data.end(), -context.getEA().size()); @@ -177,7 +177,7 @@ TEST_P(TestTotalSums_BGVT, TSumsWorkCorrForNegValBGV) } } -TEST_P(TestTotalSums_BGVT, TSumsWorkCorrForPosNegValBGV) +TEST_P(TestTotalSums_BGV, TSumsWorkCorrForPosNegValBGV) { std::vector data(context.getEA().size()); @@ -205,7 +205,7 @@ TEST_P(TestTotalSums_BGVT, TSumsWorkCorrForPosNegValBGV) } } -TEST_P(TestTotalSums_BGVT, TSumsWorkCorrForZeroValBGV) +TEST_P(TestTotalSums_BGV, TSumsWorkCorrForZeroValBGV) { std::vector data(context.getEA().size(), 0); @@ -224,7 +224,7 @@ TEST_P(TestTotalSums_BGVT, TSumsWorkCorrForZeroValBGV) } } -TEST_P(TestTotalSums_CKKST, TSumsWorkCorrForZeroValCKKS) +TEST_P(TestTotalSums_CKKS, TSumsWorkCorrForZeroValCKKS) { std::vector> data(context.getEA().size(), 0.0); @@ -241,7 +241,7 @@ TEST_P(TestTotalSums_CKKST, TSumsWorkCorrForZeroValCKKS) COMPARE_CXDOUBLE_VECS(ptxt.getSlotRepr(), post_decryption.getSlotRepr()); } -TEST_P(TestTotalSums_CKKST, TSumsWorkCorrForPosValCKKS) +TEST_P(TestTotalSums_CKKS, TSumsWorkCorrForPosValCKKS) { std::vector> data(context.getEA().size()); for (std::size_t i = 0; i < data.size(); ++i) { @@ -261,7 +261,7 @@ TEST_P(TestTotalSums_CKKST, TSumsWorkCorrForPosValCKKS) COMPARE_CXDOUBLE_VECS(ptxt.getSlotRepr(), post_decryption.getSlotRepr()); } -TEST_P(TestTotalSums_CKKST, TSumsWorkCorrForNegValCKKS) +TEST_P(TestTotalSums_CKKS, TSumsWorkCorrForNegValCKKS) { std::vector> data(context.getEA().size()); for (std::size_t i = 0; i < data.size(); ++i) { @@ -281,7 +281,7 @@ TEST_P(TestTotalSums_CKKST, TSumsWorkCorrForNegValCKKS) COMPARE_CXDOUBLE_VECS(ptxt.getSlotRepr(), post_decryption.getSlotRepr()); } -TEST_P(TestTotalSums_CKKST, TSumsWorkCorrForPosNegValCKKS) +TEST_P(TestTotalSums_CKKS, TSumsWorkCorrForPosNegValCKKS) { std::vector> data(context.getEA().size()); for (std::size_t i = 0; i < data.size(); ++i) { @@ -309,14 +309,16 @@ TEST_P(TestTotalSums_CKKST, TSumsWorkCorrForPosNegValCKKS) // parameters initialization INSTANTIATE_TEST_SUITE_P(Parameters, - TestTotalSums_BGVT, - ::testing::Values(BGVParametersT(/*m=*/45, + TestTotalSums_BGV, + ::testing::Values(BGVParameters(/*m=*/45, /*p=*/317, /*r=*/1, - /*bits=*/500), BGVParametersT(512, /*fermat_prime=*/257, 1, 500), BGVParametersT(45, 317, 1, 500), BGVParametersT(288, /*fermat_prime=*/17, 1, 500), BGVParametersT(45, 367, 1, 500))); + /*bits=*/500), BGVParameters(512, /*fermat_prime=*/257, 1, 500), BGVParameters(45, 317, 1, 500), BGVParameters(288, /*fermat_prime=*/17, 1, 500), BGVParameters(45, 367, 1, 500))); INSTANTIATE_TEST_SUITE_P(Parameters, - TestTotalSums_CKKST, - ::testing::Values(CKKSParametersT(/*m=*/64, + TestTotalSums_CKKS, + ::testing::Values(CKKSParameters(/*m=*/64, /*precision=*/30, - /*bits=*/500), CKKSParametersT(128, 35, 500), CKKSParametersT(256, 40, 500), CKKSParametersT(512, 50, 500), CKKSParametersT(1024, 45, 500))); + /*bits=*/500), CKKSParameters(128, 35, 500), CKKSParameters(256, 40, 500), CKKSParameters(512, 50, 500), CKKSParameters(1024, 45, 500))); + +} // namespace