|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 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 | +// http://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 "shell_encryption/rns/lazy_rns_polynomial.h" |
| 16 | + |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +#include "absl/numeric/int128.h" |
| 20 | +#include "absl/status/status.h" |
| 21 | +#include "absl/types/span.h" |
| 22 | +#include "shell_encryption/integral_types.h" |
| 23 | +#include "shell_encryption/montgomery.h" |
| 24 | +#include "shell_encryption/rns/rns_modulus.h" |
| 25 | +#include "shell_encryption/rns/rns_polynomial.h" |
| 26 | +#include "shell_encryption/rns/rns_polynomial_hwy.h" |
| 27 | +#include "shell_encryption/status_macros.h" |
| 28 | + |
| 29 | +namespace rlwe { |
| 30 | + |
| 31 | +using ModularInt32 = MontgomeryInt<Uint32>; |
| 32 | +using ModularInt64 = MontgomeryInt<Uint64>; |
| 33 | + |
| 34 | +template <typename ModularInt> |
| 35 | +absl::Status LazyRnsPolynomial<ModularInt>::CheckFusedMulAddInPlaceParameters( |
| 36 | + const RnsPolynomial<ModularInt>& a, const RnsPolynomial<ModularInt>& b, |
| 37 | + absl::Span<const PrimeModulus<ModularInt>* const> moduli) { |
| 38 | + if (!a.IsNttForm() || !b.IsNttForm()) { |
| 39 | + return absl::InvalidArgumentError( |
| 40 | + "Polynomials `a` and `b` must be in NTT form."); |
| 41 | + } |
| 42 | + int num_moduli = moduli.size(); |
| 43 | + if (a.NumModuli() != num_moduli || b.NumModuli() != num_moduli || |
| 44 | + coeff_vectors_.size() != num_moduli) { |
| 45 | + return absl::InvalidArgumentError( |
| 46 | + "Polynomials `a`, `b`, and this must all be defined wrt `moduli`"); |
| 47 | + } |
| 48 | + int num_coeffs = coeff_vectors_[0].size(); |
| 49 | + if (a.NumCoeffs() != num_coeffs || b.NumCoeffs() != num_coeffs) { |
| 50 | + return absl::InvalidArgumentError( |
| 51 | + "Polynomials `a` and `b` must have the same number of coefficients as " |
| 52 | + "this lazy polynomial."); |
| 53 | + } |
| 54 | + return absl::OkStatus(); |
| 55 | +} |
| 56 | + |
| 57 | +template <typename ModularInt> |
| 58 | +absl::Status LazyRnsPolynomial<ModularInt>::FusedMulAddInPlace( |
| 59 | + const RnsPolynomial<ModularInt>& a, const RnsPolynomial<ModularInt>& b, |
| 60 | + absl::Span<const PrimeModulus<ModularInt>* const> moduli) { |
| 61 | + RLWE_RETURN_IF_ERROR(CheckFusedMulAddInPlaceParameters(a, b, moduli)); |
| 62 | + if (current_level_ == maximum_level_) { |
| 63 | + Refresh(moduli); |
| 64 | + } |
| 65 | + |
| 66 | + int num_moduli = moduli.size(); |
| 67 | + int num_coeffs = coeff_vectors_[0].size(); |
| 68 | + const auto& a_coeff_vectors = a.Coeffs(); |
| 69 | + const auto& b_coeff_vectors = b.Coeffs(); |
| 70 | + for (int i = 0; i < num_moduli; ++i) { |
| 71 | + for (int j = 0; j < num_coeffs; ++j) { |
| 72 | + coeff_vectors_[i][j] += |
| 73 | + static_cast<BigInt>( |
| 74 | + a_coeff_vectors[i][j].GetMontgomeryRepresentation()) * |
| 75 | + b_coeff_vectors[i][j].GetMontgomeryRepresentation(); |
| 76 | + } |
| 77 | + } |
| 78 | + current_level_++; |
| 79 | + return absl::OkStatus(); |
| 80 | +} |
| 81 | + |
| 82 | +template <> |
| 83 | +absl::Status LazyRnsPolynomial<ModularInt32>::FusedMulAddInPlace( |
| 84 | + const RnsPolynomial<ModularInt32>& a, const RnsPolynomial<ModularInt32>& b, |
| 85 | + absl::Span<const PrimeModulus<ModularInt32>* const> moduli) { |
| 86 | + RLWE_RETURN_IF_ERROR(CheckFusedMulAddInPlaceParameters(a, b, moduli)); |
| 87 | + if (current_level_ == maximum_level_) { |
| 88 | + Refresh(moduli); |
| 89 | + } |
| 90 | + |
| 91 | + int num_moduli = moduli.size(); |
| 92 | + const auto& a_coeff_vectors = a.Coeffs(); |
| 93 | + const auto& b_coeff_vectors = b.Coeffs(); |
| 94 | + for (int i = 0; i < num_moduli; ++i) { |
| 95 | + internal::BatchFusedMulAddMontgomeryRep<Uint32>( |
| 96 | + a_coeff_vectors[i], b_coeff_vectors[i], coeff_vectors_[i]); |
| 97 | + } |
| 98 | + current_level_++; |
| 99 | + return absl::OkStatus(); |
| 100 | +} |
| 101 | + |
| 102 | +template <> |
| 103 | +absl::Status LazyRnsPolynomial<ModularInt64>::FusedMulAddInPlace( |
| 104 | + const RnsPolynomial<ModularInt64>& a, const RnsPolynomial<ModularInt64>& b, |
| 105 | + absl::Span<const PrimeModulus<ModularInt64>* const> moduli) { |
| 106 | + RLWE_RETURN_IF_ERROR(CheckFusedMulAddInPlaceParameters(a, b, moduli)); |
| 107 | + if (current_level_ == maximum_level_) { |
| 108 | + Refresh(moduli); |
| 109 | + } |
| 110 | + int num_moduli = moduli.size(); |
| 111 | + const auto& a_coeff_vectors = a.Coeffs(); |
| 112 | + const auto& b_coeff_vectors = b.Coeffs(); |
| 113 | + for (int i = 0; i < num_moduli; ++i) { |
| 114 | + internal::BatchFusedMulAddMontgomeryRep<Uint64>( |
| 115 | + a_coeff_vectors[i], b_coeff_vectors[i], coeff_vectors_[i]); |
| 116 | + } |
| 117 | + current_level_++; |
| 118 | + return absl::OkStatus(); |
| 119 | +} |
| 120 | + |
| 121 | +template class LazyRnsPolynomial<MontgomeryInt<Uint16>>; |
| 122 | +template class LazyRnsPolynomial<MontgomeryInt<Uint32>>; |
| 123 | +template class LazyRnsPolynomial<MontgomeryInt<Uint64>>; |
| 124 | +template class LazyRnsPolynomial<MontgomeryInt<absl::uint128>>; |
| 125 | +#ifdef ABSL_HAVE_INTRINSIC_INT128 |
| 126 | +template class LazyRnsPolynomial<MontgomeryInt<unsigned __int128>>; |
| 127 | +#endif |
| 128 | + |
| 129 | +} // namespace rlwe |
0 commit comments