|
| 1 | +/* Copyright (C) 2020 IBM Corp. |
| 2 | + * This program is Licensed under the Apache License, Version 2.0 |
| 3 | + * (the "License"); you may not use this file except in compliance |
| 4 | + * with the License. You may obtain a copy of the License at |
| 5 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * Unless required by applicable law or agreed to in writing, software |
| 7 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | + * See the License for the specific language governing permissions and |
| 10 | + * limitations under the License. See accompanying LICENSE file. |
| 11 | + */ |
| 12 | + |
| 13 | +// This test file does not fully cover Ctxt, and is intended as a starting |
| 14 | +// point for testing new functionality of Ctxt going forward. |
| 15 | +// The older tests with more extensive coverage can be found in the files |
| 16 | +// with names matching "GTest*". |
| 17 | + |
| 18 | +#include <helib/helib.h> |
| 19 | + |
| 20 | +#include "test_common.h" |
| 21 | +#include "gtest/gtest.h" |
| 22 | + |
| 23 | +namespace { |
| 24 | + |
| 25 | +struct BGVParameters |
| 26 | +{ |
| 27 | + BGVParameters(unsigned m, unsigned p, unsigned r, unsigned bits) : |
| 28 | + m(m), |
| 29 | + p(p), |
| 30 | + r(r), |
| 31 | + bits(bits){}; |
| 32 | + |
| 33 | + const unsigned m; |
| 34 | + const unsigned p; |
| 35 | + const unsigned r; |
| 36 | + const unsigned bits; |
| 37 | + |
| 38 | + friend std::ostream& operator<<(std::ostream& os, const BGVParameters& params) |
| 39 | + { |
| 40 | + return os << "{" |
| 41 | + << "m = " << params.m << ", " |
| 42 | + << "p = " << params.p << ", " |
| 43 | + << "r = " << params.r << ", " |
| 44 | + << "bits = " << params.bits << "}"; |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +class TestCtxt : public ::testing::TestWithParam<BGVParameters> |
| 49 | +{ |
| 50 | +protected: |
| 51 | + const unsigned long m; |
| 52 | + const unsigned long p; |
| 53 | + const unsigned long r; |
| 54 | + const unsigned long bits; |
| 55 | + helib::Context context; |
| 56 | + helib::SecKey secretKey; |
| 57 | + helib::PubKey publicKey; |
| 58 | + const helib::EncryptedArray& ea; |
| 59 | + |
| 60 | + TestCtxt() : |
| 61 | + m(GetParam().m), |
| 62 | + p(GetParam().p), |
| 63 | + r(GetParam().r), |
| 64 | + bits(GetParam().bits), |
| 65 | + context(m, p, r), |
| 66 | + secretKey((buildModChain(context, bits), context)), |
| 67 | + publicKey((secretKey.GenSecKey(), secretKey)), |
| 68 | + ea(*(context.ea)) |
| 69 | + {} |
| 70 | +}; |
| 71 | + |
| 72 | +TEST_P(TestCtxt, timesEqualsWithLongWorks) |
| 73 | +{ |
| 74 | + helib::Ptxt<helib::BGV> ptxt(context, std::vector<long>(ea.size(), 5)); |
| 75 | + helib::Ctxt ctxt(publicKey); |
| 76 | + publicKey.Encrypt(ctxt, ptxt); |
| 77 | + ctxt *= 2l; |
| 78 | + |
| 79 | + helib::Ptxt<helib::BGV> expected_result(context, |
| 80 | + std::vector<long>(ea.size(), 10)); |
| 81 | + helib::Ptxt<helib::BGV> decrypted_result(context); |
| 82 | + secretKey.Decrypt(decrypted_result, ctxt); |
| 83 | + |
| 84 | + EXPECT_EQ(decrypted_result, expected_result); |
| 85 | +} |
| 86 | + |
| 87 | +TEST_P(TestCtxt, timesEqualsWithZZXWorks) |
| 88 | +{ |
| 89 | + helib::Ptxt<helib::BGV> ptxt(context, std::vector<long>(ea.size(), 5)); |
| 90 | + helib::Ctxt ctxt(publicKey); |
| 91 | + publicKey.Encrypt(ctxt, ptxt); |
| 92 | + ctxt *= NTL::ZZX(2l); |
| 93 | + |
| 94 | + helib::Ptxt<helib::BGV> expected_result(context, |
| 95 | + std::vector<long>(ea.size(), 10)); |
| 96 | + helib::Ptxt<helib::BGV> decrypted_result(context); |
| 97 | + secretKey.Decrypt(decrypted_result, ctxt); |
| 98 | + |
| 99 | + EXPECT_EQ(decrypted_result, expected_result); |
| 100 | +} |
| 101 | + |
| 102 | +INSTANTIATE_TEST_SUITE_P(variousParameters, |
| 103 | + TestCtxt, |
| 104 | + ::testing::Values(BGVParameters(17, 2, 1, 100))); |
| 105 | + |
| 106 | +} // namespace |
0 commit comments