context.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright 2020 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. #ifndef RLWE_CONTEXT_H_
  16. #define RLWE_CONTEXT_H_
  17. #include <memory>
  18. #include "absl/memory/memory.h"
  19. #include "error_params.h"
  20. #include "ntt_parameters.h"
  21. #include "status_macros.h"
  22. #include "statusor.h"
  23. namespace rlwe {
  24. // Defines and holds the context of the RLWE encryption scheme.
  25. // Thread safe..
  26. template <typename ModularInt>
  27. class RlweContext {
  28. using Int = typename ModularInt::Int;
  29. using ModulusParams = typename ModularInt::Params;
  30. public:
  31. // Structure to hold parameters for the RLWE encryption scheme. The parameters
  32. // include:
  33. // - modulus, an Int which needs to be congruent to 1 modulo 2 * (1 << log_n);
  34. // - log_n: the logarithm of the number of coefficients of the polynomials;
  35. // - log_t: the number of bits of the plaintext space, which will be equal to
  36. // (1 << log_t) + 1;
  37. // - variance, the error variance to use when sampling noises or secrets.
  38. struct Parameters {
  39. Int modulus;
  40. size_t log_n;
  41. size_t log_t;
  42. size_t variance;
  43. };
  44. // Factory function to create a context from a context_params.
  45. static rlwe::StatusOr<std::unique_ptr<const RlweContext>> Create(
  46. Parameters context_params) {
  47. // Create the modulus parameters.
  48. RLWE_ASSIGN_OR_RETURN(
  49. std::unique_ptr<const ModulusParams> modulus_parameters,
  50. ModulusParams::Create(context_params.modulus));
  51. // Create the NTT parameters.
  52. RLWE_ASSIGN_OR_RETURN(NttParameters<ModularInt> ntt_params_temp,
  53. InitializeNttParameters<ModularInt>(
  54. context_params.log_n, modulus_parameters.get()));
  55. std::unique_ptr<const NttParameters<ModularInt>> ntt_params =
  56. std::make_unique<const NttParameters<ModularInt>>(
  57. std::move(ntt_params_temp));
  58. // Create the error parameters.
  59. RLWE_ASSIGN_OR_RETURN(ErrorParams<ModularInt> error_params_temp,
  60. ErrorParams<ModularInt>::Create(
  61. context_params.log_t, context_params.variance,
  62. modulus_parameters.get(), ntt_params.get()));
  63. std::unique_ptr<const ErrorParams<ModularInt>> error_params =
  64. std::make_unique<const ErrorParams<ModularInt>>(
  65. std::move(error_params_temp));
  66. return absl::WrapUnique<const RlweContext>(
  67. new RlweContext(std::move(modulus_parameters), std::move(ntt_params),
  68. std::move(error_params), std::move(context_params)));
  69. }
  70. // Disallow copy and copy-assign, allow move and move-assign.
  71. RlweContext(const RlweContext&) = delete;
  72. RlweContext& operator=(const RlweContext&) = delete;
  73. RlweContext(RlweContext&&) = default;
  74. RlweContext& operator=(RlweContext&&) = default;
  75. ~RlweContext() = default;
  76. // Getters.
  77. const ModulusParams* GetModulusParams() const {
  78. return modulus_parameters_.get();
  79. }
  80. const NttParameters<ModularInt>* GetNttParams() const {
  81. return ntt_parameters_.get();
  82. }
  83. const ErrorParams<ModularInt>* GetErrorParams() const {
  84. return error_parameters_.get();
  85. }
  86. const Int GetModulus() const { return context_params_.modulus; }
  87. const size_t GetLogN() const { return context_params_.log_n; }
  88. const size_t GetN() const { return 1 << context_params_.log_n; }
  89. const size_t GetLogT() const { return context_params_.log_t; }
  90. const Int GetT() const {
  91. return (static_cast<Int>(1) << context_params_.log_t) + static_cast<Int>(1);
  92. }
  93. const size_t GetVariance() const { return context_params_.variance; }
  94. private:
  95. RlweContext(std::unique_ptr<const ModulusParams> modulus_parameters,
  96. std::unique_ptr<const NttParameters<ModularInt>> ntt_parameters,
  97. std::unique_ptr<const ErrorParams<ModularInt>> error_parameters,
  98. Parameters context_params)
  99. : modulus_parameters_(std::move(modulus_parameters)),
  100. ntt_parameters_(std::move(ntt_parameters)),
  101. error_parameters_(std::move(error_parameters)),
  102. context_params_(std::move(context_params)) {}
  103. const std::unique_ptr<const ModulusParams> modulus_parameters_;
  104. const std::unique_ptr<const NttParameters<ModularInt>> ntt_parameters_;
  105. const std::unique_ptr<const ErrorParams<ModularInt>> error_parameters_;
  106. const Parameters context_params_;
  107. };
  108. } // namespace rlwe
  109. #endif // RLWE_CONTEXT_H_