sample_error_test.cc 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 "sample_error.h"
  16. #include <cstdint>
  17. #include <vector>
  18. #include <gmock/gmock.h>
  19. #include <gtest/gtest.h>
  20. #include "context.h"
  21. #include "montgomery.h"
  22. #include "symmetric_encryption.h"
  23. #include "testing/parameters.h"
  24. #include "testing/status_matchers.h"
  25. #include "testing/status_testing.h"
  26. #include "testing/testing_prng.h"
  27. namespace {
  28. using ::rlwe::testing::StatusIs;
  29. using ::testing::HasSubstr;
  30. const int kTestingRounds = 10;
  31. const std::vector<rlwe::Uint64> variances = {8, 15, 29, 50};
  32. template <typename ModularInt>
  33. class SampleErrorTest : public ::testing::Test {};
  34. TYPED_TEST_SUITE(SampleErrorTest, rlwe::testing::ModularIntTypes);
  35. TYPED_TEST(SampleErrorTest, CheckUpperBoundOnNoise) {
  36. using Int = typename TypeParam::Int;
  37. auto prng = absl::make_unique<rlwe::testing::TestingPrng>(0);
  38. for (const auto& params :
  39. rlwe::testing::ContextParameters<TypeParam>::Value()) {
  40. ASSERT_OK_AND_ASSIGN(auto context,
  41. rlwe::RlweContext<TypeParam>::Create(params));
  42. for (auto variance : variances) {
  43. for (int i = 0; i < kTestingRounds; i++) {
  44. ASSERT_OK_AND_ASSIGN(std::vector<TypeParam> error,
  45. rlwe::SampleFromErrorDistribution<TypeParam>(
  46. context->GetN(), variance, prng.get(),
  47. context->GetModulusParams()));
  48. // Check that each coefficient is in [-2*variance, 2*variance]
  49. for (int j = 0; j < context->GetN(); j++) {
  50. Int reduced = error[j].ExportInt(context->GetModulusParams());
  51. if (reduced > (context->GetModulus() >> 1)) {
  52. EXPECT_LT(context->GetModulus() - reduced, 2 * variance + 1);
  53. } else {
  54. EXPECT_LT(reduced, 2 * variance + 1);
  55. }
  56. }
  57. }
  58. }
  59. }
  60. }
  61. TYPED_TEST(SampleErrorTest, FailOnTooLargeVariance) {
  62. auto prng = absl::make_unique<rlwe::testing::TestingPrng>(0);
  63. for (const auto& params :
  64. rlwe::testing::ContextParameters<TypeParam>::Value()) {
  65. ASSERT_OK_AND_ASSIGN(auto context,
  66. rlwe::RlweContext<TypeParam>::Create(params));
  67. rlwe::Uint64 variance = rlwe::kMaxVariance + 1;
  68. EXPECT_THAT(
  69. rlwe::SampleFromErrorDistribution<TypeParam>(
  70. context->GetN(), variance, prng.get(), context->GetModulusParams()),
  71. StatusIs(
  72. absl::StatusCode::kInvalidArgument,
  73. HasSubstr(absl::StrCat("The variance, ", variance,
  74. ", must be at most ", rlwe::kMaxVariance))));
  75. }
  76. }
  77. } // namespace