symmetric_encryption_with_prng_test.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright 2018 Google LLC.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * https://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include "symmetric_encryption_with_prng.h"
  16. #include <vector>
  17. #include <gmock/gmock.h>
  18. #include <gtest/gtest.h>
  19. #include "context.h"
  20. #include "montgomery.h"
  21. #include "ntt_parameters.h"
  22. #include "polynomial.h"
  23. #include "prng/integral_prng_types.h"
  24. #include "status_macros.h"
  25. #include "testing/parameters.h"
  26. #include "testing/status_matchers.h"
  27. #include "testing/status_testing.h"
  28. #include "testing/testing_utils.h"
  29. namespace rlwe {
  30. namespace {
  31. // Set constants.
  32. const unsigned int kTestingRounds = 10;
  33. template <typename ModularInt>
  34. class SymmetricEncryptionWithPrngTest : public ::testing::Test {
  35. public:
  36. using Key = SymmetricRlweKey<ModularInt>;
  37. // Sample a random key.
  38. rlwe::StatusOr<Key> SampleKey(const rlwe::RlweContext<ModularInt>* context) {
  39. RLWE_ASSIGN_OR_RETURN(std::string prng_seed,
  40. rlwe::SingleThreadPrng::GenerateSeed());
  41. RLWE_ASSIGN_OR_RETURN(auto prng, rlwe::SingleThreadPrng::Create(prng_seed));
  42. return Key::Sample(context->GetLogN(), context->GetVariance(),
  43. context->GetLogT(), context->GetModulusParams(),
  44. context->GetNttParams(), prng.get());
  45. }
  46. rlwe::StatusOr<std::vector<Polynomial<ModularInt>>> ConvertPlaintextsToNtt(
  47. const std::vector<std::vector<typename ModularInt::Int>>& coeffs,
  48. const rlwe::RlweContext<ModularInt>* context) {
  49. std::vector<Polynomial<ModularInt>> ntt_plaintexts;
  50. for (int i = 0; i < coeffs.size(); ++i) {
  51. RLWE_ASSIGN_OR_RETURN(auto mont,
  52. rlwe::testing::ConvertToMontgomery<ModularInt>(
  53. coeffs[i], context->GetModulusParams()));
  54. ntt_plaintexts.push_back(Polynomial<ModularInt>::ConvertToNtt(
  55. mont, context->GetNttParams(), context->GetModulusParams()));
  56. }
  57. return ntt_plaintexts;
  58. }
  59. void TestCompressedEncryptionDecryption(
  60. const std::vector<std::vector<typename ModularInt::Int>>& plaintexts,
  61. const rlwe::RlweContext<ModularInt>* context) {
  62. ASSERT_OK_AND_ASSIGN(auto key, SampleKey(context));
  63. ASSERT_OK_AND_ASSIGN(std::string prng_seed,
  64. SingleThreadPrng::GenerateSeed());
  65. ASSERT_OK_AND_ASSIGN(auto prng, SingleThreadPrng::Create(prng_seed));
  66. ASSERT_OK_AND_ASSIGN(std::string prng_encryption_seed,
  67. SingleThreadPrng::GenerateSeed());
  68. ASSERT_OK_AND_ASSIGN(auto prng_encryption,
  69. SingleThreadPrng::Create(prng_encryption_seed));
  70. ASSERT_OK_AND_ASSIGN(std::vector<Polynomial<ModularInt>> ntt_plaintexts,
  71. ConvertPlaintextsToNtt(plaintexts, context));
  72. ASSERT_OK_AND_ASSIGN(
  73. auto compressed_ciphertexts,
  74. EncryptWithPrng<ModularInt>(key, ntt_plaintexts, prng.get(),
  75. prng_encryption.get()));
  76. EXPECT_EQ(plaintexts.size(), compressed_ciphertexts.size());
  77. ASSERT_OK_AND_ASSIGN(auto another_prng,
  78. SingleThreadPrng::Create(prng_seed));
  79. ASSERT_OK_AND_ASSIGN(auto ciphertexts,
  80. ExpandFromPrng<ModularInt>(compressed_ciphertexts,
  81. context->GetModulusParams(),
  82. context->GetNttParams(),
  83. context->GetErrorParams(),
  84. another_prng.get()));
  85. EXPECT_EQ(plaintexts.size(), ciphertexts.size());
  86. for (int i = 0; i < ciphertexts.size(); ++i) {
  87. // Expect that the error of an expanded ciphertext is of a fresh
  88. // encryption.
  89. EXPECT_EQ(ciphertexts[i].Error(),
  90. context->GetErrorParams()->B_encryption());
  91. ASSERT_OK_AND_ASSIGN(auto decrypted,
  92. Decrypt<ModularInt>(key, ciphertexts[i]));
  93. EXPECT_EQ(plaintexts[i], decrypted);
  94. }
  95. }
  96. };
  97. TYPED_TEST_SUITE(SymmetricEncryptionWithPrngTest,
  98. rlwe::testing::ModularIntTypes);
  99. // Ensure that the encryption scheme can encrypt and decrypt a single compressed
  100. // ciphertext.
  101. TYPED_TEST(SymmetricEncryptionWithPrngTest, EncryptDecryptSingleCompressed) {
  102. for (const auto& params :
  103. rlwe::testing::ContextParameters<TypeParam>::Value()) {
  104. ASSERT_OK_AND_ASSIGN(auto context,
  105. rlwe::RlweContext<TypeParam>::Create(params));
  106. for (unsigned int i = 0; i < kTestingRounds; ++i) {
  107. this->TestCompressedEncryptionDecryption(
  108. {rlwe::testing::SamplePlaintext<TypeParam>(context->GetN(),
  109. context->GetT())},
  110. context.get());
  111. }
  112. }
  113. }
  114. // Ensure that the encryption scheme can encrypt and decrypt multiple compressed
  115. // ciphertexts.
  116. TYPED_TEST(SymmetricEncryptionWithPrngTest, EncryptDecryptMultipleCompressed) {
  117. for (const auto& params :
  118. rlwe::testing::ContextParameters<TypeParam>::Value()) {
  119. ASSERT_OK_AND_ASSIGN(auto context,
  120. rlwe::RlweContext<TypeParam>::Create(params));
  121. for (unsigned int i = 0; i < kTestingRounds; ++i) {
  122. std::vector<std::vector<typename TypeParam::Int>> plaintexts;
  123. for (int j = 0; j < i + 2; ++j) {
  124. plaintexts.push_back(rlwe::testing::SamplePlaintext<TypeParam>(
  125. context->GetN(), context->GetT()));
  126. }
  127. this->TestCompressedEncryptionDecryption(plaintexts, context.get());
  128. }
  129. }
  130. }
  131. } // namespace
  132. } // namespace rlwe