diff --git a/benchmark/CMakeLists.txt b/benchmark/CMakeLists.txt index 7b00073..86e205f 100644 --- a/benchmark/CMakeLists.txt +++ b/benchmark/CMakeLists.txt @@ -18,8 +18,7 @@ cpmaddpackage(URI "gh:google/benchmark@1.9.4" OPTIONS "BENCHMARK_ENABLE_TESTING Off") add_executable(deb-benchmark benchmark.cpp) -target_link_libraries(deb-benchmark PRIVATE deb_obj deb_DevHeader - benchmark::benchmark_main) +target_link_libraries(deb-benchmark PRIVATE deb benchmark::benchmark_main) enable_language(C) cpmaddpackage( @@ -33,6 +32,5 @@ cpmaddpackage( c) add_executable(deb-benchmark-blake3 benchmark_blake3.cpp) -target_link_libraries( - deb-benchmark-blake3 PRIVATE deb_obj deb_DevHeader benchmark::benchmark_main - BLAKE3::blake3) +target_link_libraries(deb-benchmark-blake3 + PRIVATE deb benchmark::benchmark_main BLAKE3::blake3) diff --git a/benchmark/benchmark.cpp b/benchmark/benchmark.cpp index e967039..0acf374 100644 --- a/benchmark/benchmark.cpp +++ b/benchmark/benchmark.cpp @@ -113,6 +113,30 @@ template static void bm_decryption(benchmark::State &state) { } } +template static void bm_decryption_inplace(benchmark::State &state) { + const Preset preset = T; + const auto ns = get_num_secret(preset); + std::vector msg_v; + for (Size i = 0; i < ns; ++i) { + msg_v.push_back(gen_random_message(get_num_slots(preset))); + } + + SecretKey sk = SecretKeyGenerator::GenSecretKey(preset); + EncryptorT encryptor; + DecryptorT decryptor; + Ciphertext ctxt(preset); + encryptor.encrypt(msg_v, sk, ctxt); + + std::optional ctxt_copy; + for (auto _ : state) { + state.PauseTiming(); + ctxt_copy.emplace(ctxt.deepCopy()); + state.ResumeTiming(); + decryptor.decryptInplace(ctxt_copy.value(), sk, msg_v.data()); + benchmark::DoNotOptimize(msg_v.data()); + benchmark::ClobberMemory(); + } +} template static void bm_seckey_coeff_encryption(benchmark::State &state) { const Preset preset = T; @@ -176,6 +200,31 @@ static void bm_coeff_decryption(benchmark::State &state) { } } +template +static void bm_coeff_decryption_inplace(benchmark::State &state) { + const Preset preset = T; + const auto ns = get_num_secret(preset); + std::vector msg_v; + for (Size i = 0; i < ns; ++i) { + msg_v.push_back(gen_random_coeff(get_degree(preset))); + } + SecretKey sk = SecretKeyGenerator::GenSecretKey(preset); + EncryptorT encryptor(preset); + DecryptorT decryptor(preset); + + Ciphertext ctxt(preset); + encryptor.encrypt(msg_v, sk, ctxt); + + std::optional ctxt_copy; + for (auto _ : state) { + state.PauseTiming(); + ctxt_copy.emplace(ctxt.deepCopy()); + state.ResumeTiming(); + decryptor.decryptInplace(ctxt_copy.value(), sk, msg_v.data()); + benchmark::DoNotOptimize(msg_v.data()); + benchmark::ClobberMemory(); + } +} template static void bm_forward_ntt(benchmark::State &state) { utils::NTT ntt(degree, prime); @@ -218,11 +267,15 @@ static void bm_backward_ntt(benchmark::State &state) { ->Unit(benchmark::kMicrosecond); \ BENCHMARK_TEMPLATE(bm_decryption, Preset::PRESET_##PRESET) \ ->Unit(benchmark::kMicrosecond); \ + BENCHMARK_TEMPLATE(bm_decryption_inplace, Preset::PRESET_##PRESET) \ + ->Unit(benchmark::kMicrosecond); \ BENCHMARK_TEMPLATE(bm_seckey_coeff_encryption, Preset::PRESET_##PRESET) \ ->Unit(benchmark::kMicrosecond); \ BENCHMARK_TEMPLATE(bm_enckey_coeff_encryption, Preset::PRESET_##PRESET) \ ->Unit(benchmark::kMicrosecond); \ BENCHMARK_TEMPLATE(bm_coeff_decryption, Preset::PRESET_##PRESET) \ + ->Unit(benchmark::kMicrosecond); \ + BENCHMARK_TEMPLATE(bm_coeff_decryption_inplace, Preset::PRESET_##PRESET)\ ->Unit(benchmark::kMicrosecond); PRESET_LIST diff --git a/include/deb/Decryptor.hpp b/include/deb/Decryptor.hpp index f3988b0..0d8ada6 100644 --- a/include/deb/Decryptor.hpp +++ b/include/deb/Decryptor.hpp @@ -23,8 +23,6 @@ #include namespace deb { -// TODO: make template for Decryptor -// to support constexpr functions with various presets /** * @brief Provides CKKS decryption and decoding utilities. */ @@ -38,10 +36,11 @@ class DecryptorT : public PresetTraits { public: /** * @brief Creates a decryptor for the given preset. - * @param preset Target preset that defines polynomial sizes and moduli. + * @param target_preset Target preset that defines polynomial sizes and + * moduli. */ explicit DecryptorT(); - explicit DecryptorT(const Preset preset); + explicit DecryptorT(const Preset target_preset); template >, int> = 0> @@ -69,6 +68,10 @@ class DecryptorT : public PresetTraits { void decrypt(const CiphertextT &ctxt, const SecretKeyT &sk, MSG *msg, Real scale = 0) const; + template + void decryptInplace(CiphertextT &ctxt, const SecretKeyT &sk, MSG *msg, + Real scale = 0) const; + template /** * @brief Decrypts into a vector-like container, validating secret-unit @@ -86,21 +89,47 @@ class DecryptorT : public PresetTraits { decrypt(ctxt, sk, msg.data(), scale); } -private: + /** + * @brief Internal decryption function that produces a decrypted polynomial + * plaintext. + * @param ctxt Ciphertext input. + * @param sx Secret key polynomial used for decryption. + * @param ax Optional auxiliary polynomial for decryption (e.g., from + * switching key). + * @return Decrypted polynomial plaintext. + */ PolynomialT innerDecrypt(const CiphertextT &ctxt, const PolynomialT &sx, const std::optional> &ax = std::nullopt) const; + + /** + * @brief Internal decode function that handles both single-poly and + * poly-pair cases. + * @tparam CMSG Coefficient message type (e.g., CoeffMessage, + * FCoeffMessage). + * @param ptxt Decrypted polynomial plaintext. + * @param coeff Coefficient message that receives the decoded coefficients. + * @param scale Scaling factor for decoding. + */ + template + void innerDecode(const PolynomialT &ptxt, CMSG &coeff, Real scale) const; + /** + * @brief Decodes a decrypted polynomial into a message-like object. + * @tparam MSG Message container or view type. + * @param ptxt Decrypted polynomial plaintext. + * @param msg Message object that receives decoded values. + * @param scale Scaling factor for decoding. + */ + template + void decode(const PolynomialT &ptxt, MSG &msg, Real scale) const; + +private: template void decodeWithSinglePoly(const PolynomialT &ptxt, CMSG &coeff, Real scale) const; template void decodeWithPolyPair(const PolynomialT &ptxt, CMSG &coeff, Real scale) const; - template - void decodeWithoutFFT(const PolynomialT &ptxt, CMSG &coeff, - Real scale) const; - template - void decode(const PolynomialT &ptxt, MSG &msg, Real scale) const; utils::FFT fft_; }; @@ -112,6 +141,9 @@ class DecryptorT : public PresetTraits { prefix template void DecryptorT::decrypt( \ const CiphertextT &ctxt, const SecretKeyT &sk, \ msg_t *msg, Real scale) const; \ + prefix template void DecryptorT::decryptInplace( \ + CiphertextT & ctxt, const SecretKeyT &sk, msg_t *msg, \ + Real scale) const; \ prefix template void DecryptorT::decrypt( \ const CiphertextT &ctxt, const SecretKeyT &sk, \ std::vector &msg, Real scale) const; @@ -134,11 +166,11 @@ class DecryptorT : public PresetTraits { const PolynomialT &ptxt, FCoeffMessage &coeff, Real scale) \ const; \ prefix template void \ - DecryptorT::decodeWithoutFFT( \ + DecryptorT::innerDecode( \ const PolynomialT &ptxt, CoeffMessage &coeff, Real scale) \ const; \ prefix template void \ - DecryptorT::decodeWithoutFFT( \ + DecryptorT::innerDecode( \ const PolynomialT &ptxt, FCoeffMessage &coeff, Real scale) \ const; \ prefix template void DecryptorT::decode( \ diff --git a/include/deb/Encryptor.hpp b/include/deb/Encryptor.hpp index 1419c56..0230b61 100644 --- a/include/deb/Encryptor.hpp +++ b/include/deb/Encryptor.hpp @@ -82,28 +82,29 @@ class EncryptorT : public PresetTraits { public: /** * @brief Constructs an encryptor bound to a preset and optional RNG seed. - * @param preset Target preset. + * @param target_preset Target preset. * @param seeds Optional deterministic seed. */ explicit EncryptorT(std::optional seeds = std::nullopt); - explicit EncryptorT(Preset actual_preset, + explicit EncryptorT(Preset target_preset, std::optional seeds = std::nullopt); /** * @brief Constructs an encryptor with a custom random generator. - * @param actual_preset Target preset. + * @param target_preset Target preset. * @param rng Custom random generator instance. */ - explicit EncryptorT(Preset actual_preset, + explicit EncryptorT(Preset target_preset, std::shared_ptr rng); template >, int> = 0> /** * @brief Encrypts a message-like object reference with the provided key. - * @tparam MSG Message representation type. + * @tparam MSG Message representation type (e.g., Message, FMessage, + * CoeffMessage, FCoeffMessage). * @tparam KEY Secret or switching key type. * @param msg Input message object. - * @param key Encryption key or switch key. + * @param key Secret key or Switching(Encryption) key. * @param ctxt Ciphertext that receives the encryption result. * @param opt Optional encryption options. */ @@ -112,9 +113,10 @@ class EncryptorT : public PresetTraits { template /** - * @brief Encrypts a vector of messages element-wise. + * @brief Encrypts a vector of messages with multi-secret parameters. The + * message vector size must match num_secret. * @param msg Vector with input messages. - * @param key Encryption key. + * @param key Secret key or Switching(Encryption) key. * @param ctxt Ciphertext result container. * @param opt Optional encryption options. */ @@ -124,32 +126,70 @@ class EncryptorT : public PresetTraits { template /** - * @brief Encrypts raw message arrays. + * @brief Encrypts a raw array of messages with multi-secret parameters. The + * array size must match num_secret. * @param msg Pointer to message sequence. - * @param key Encryption key. + * @param key Secret key or Switching(Encryption) key. * @param ctxt Ciphertext result container. * @param opt Optional encryption options. */ void encrypt(const MSG *msg, const KEY &key, CiphertextT &ctxt, const EncryptOptions &opt = default_opt) const; -private: template + /** + * @brief Core encryption routine that produces a ciphertext from a + * plaintext polynomial and key. + * @tparam KEY Secret or switching key type. + * @param ptxt Encoded plaintext polynomial. + * @param key Secret or Switching(Encryption) key. + * @param num_polyunit Number of PolyUnitT entries to encrypt. + * @param ctxt Ciphertext result container. + */ void innerEncrypt([[maybe_unused]] const PolynomialT &ptxt, [[maybe_unused]] const KEY &key, [[maybe_unused]] Size num_polyunit, [[maybe_unused]] CiphertextT &ctxt) const; template - void embeddingToN(const MSG &msg, const Real &delta, PolynomialT &ptxt, - const Size size) const; + /** + * @brief Core encode routine that embeds a message into a plaintext + * polynomial. + * @tparam MSG Message representation type (e.g., Message, FMessage, + * CoeffMessage, FCoeffMessage). + * @param msg Input message object. + * @param delta Scaling factor for embedding. + * @param ptxt Output plaintext polynomial. + * @param size Number of PolyUnitT entries to embed. + */ + void innerEncode(const MSG &msg, const Real &delta, PolynomialT &ptxt, + const Size size) const; template - void encodeWithoutNTT(const MSG &msg, PolynomialT &ptxt, const Size size, - const Real scale) const; + /** + * @brief Encodes a message into a plaintext polynomial. + * @tparam MSG Message representation type (e.g., Message, FMessage, + * CoeffMessage, FCoeffMessage). + * @param msg Input message object. + * @param ptxt Output plaintext polynomial. + * @param size Number of PolyUnitT entries to encode. + * @param scale Scaling factor for embedding. + */ + void encode(const MSG &msg, PolynomialT &ptxt, const Size size, + const Real scale) const; +private: + /** + * @brief Samples a zero-one polynomial. + * @param num_polyunit Number of PolyUnitT entries to sample. + */ void sampleZO(const Size num_polyunit) const; + /** + * @brief Samples a Gaussian polynomial. + * @param num_polyunit Number of PolyUnitT entries to sample. + * @param do_ntt Whether to apply NTT to the sampled polynomial. + */ void sampleGaussian(const Size num_polyunit, const bool do_ntt) const; std::shared_ptr rng_; @@ -182,10 +222,10 @@ class EncryptorT : public PresetTraits { #define DECL_ENCRYPT_TEMPLATE_MSG(preset, u_type, msg_t, prefix) \ DECL_ENCRYPT_TEMPLATE_MSG_KEY(preset, u_type, msg_t, SecretKeyT, prefix) \ DECL_ENCRYPT_TEMPLATE_MSG_KEY(preset, u_type, msg_t, SwitchKeyT, prefix) \ - prefix template void EncryptorT::embeddingToN( \ + prefix template void EncryptorT::innerEncode( \ const msg_t &msg, const Real &delta, PolynomialT &ptxt, \ const Size size) const; \ - prefix template void EncryptorT::encodeWithoutNTT( \ + prefix template void EncryptorT::encode( \ const msg_t &msg, PolynomialT &ptxt, const Size size, \ const Real scale) const; diff --git a/include/deb/KeyGenerator.hpp b/include/deb/KeyGenerator.hpp index e0d09c6..3084ca9 100644 --- a/include/deb/KeyGenerator.hpp +++ b/include/deb/KeyGenerator.hpp @@ -41,19 +41,19 @@ class KeyGeneratorT : public PresetTraits { /** * @brief Builds a key generator for a preset when no secret key is * provided. An external secret key must be given for key generation calls. - * @param preset Target preset whose parameters drive key sizes. + * @param target_preset Target preset whose parameters drive key sizes. * @param seeds Optional deterministic RNG seed material used when new * samples are required. */ explicit KeyGeneratorT(std::optional seeds = std::nullopt); - explicit KeyGeneratorT(const Preset preset, + explicit KeyGeneratorT(const Preset target_preset, std::optional seeds = std::nullopt); /** * @brief Builds a key generator with a custom random generator. - * @param preset Target preset whose parameters drive key sizes. + * @param target_preset Target preset whose parameters drive key sizes. * @param rng Custom random generator instance. */ - explicit KeyGeneratorT(const Preset preset, + explicit KeyGeneratorT(const Preset target_preset, std::shared_ptr rng); KeyGeneratorT(const KeyGeneratorT &) = delete; diff --git a/include/deb/SeedGenerator.hpp b/include/deb/SeedGenerator.hpp index e8f9313..32d5d33 100644 --- a/include/deb/SeedGenerator.hpp +++ b/include/deb/SeedGenerator.hpp @@ -39,14 +39,14 @@ class SeedGenerator { * @return Reference to the singleton instance. */ static SeedGenerator & - GetInstance(std::optional seeds = std::nullopt); + GetInstance(const std::optional &seeds = std::nullopt); /** * @brief Reinitializes the underlying RNG with the provided seed. * @param seeds Optional deterministic seed; when empty a random seed is * chosen. */ - static void Reseed(const std::optional &seeds); + static void Reseed(const std::optional &seeds); /** * @brief Generates a new random seed suitable for deterministic APIs. * @return Fresh RNG seed. @@ -54,7 +54,7 @@ class SeedGenerator { static RNGSeed Gen(); private: - SeedGenerator(std::optional seeds); + SeedGenerator(const std::optional &seeds); /** * @brief Internal helper that produces a new seed from the RNG state. diff --git a/src/Decryptor.cpp b/src/Decryptor.cpp index 8f03919..8764599 100644 --- a/src/Decryptor.cpp +++ b/src/Decryptor.cpp @@ -40,8 +40,8 @@ DecryptorT::DecryptorT() : PresetTraits(preset), fft_(degree) { } template -DecryptorT::DecryptorT(const Preset preset) - : PresetTraits(preset), fft_(degree) { +DecryptorT::DecryptorT(const Preset target_preset) + : PresetTraits(target_preset), fft_(degree) { for (Size i = 0; i < MAX_DECRYPT_SIZE; ++i) { modarith.emplace_back(degree, primes[i]); } @@ -61,6 +61,18 @@ template void DecryptorT::decrypt(const CiphertextT &ctxt, const SecretKeyT &sk, MSG *msg, Real scale) const { + if (scale == 0) + scale = std::pow(2.0, scale_factors[ctxt[0].size() - 1]); + CiphertextT ctxt_copy = + ctxt.deepCopy(std::min(ctxt[0].size(), MAX_DECRYPT_SIZE)); + decryptInplace(ctxt_copy, sk, msg, scale); +} + +template +template +void DecryptorT::decryptInplace(CiphertextT &ctxt, + const SecretKeyT &sk, MSG *msg, + Real scale) const { deb_assert(ctxt.numPoly() > 0, "[Decryptor::decrypt] Ciphertext size is zero"); deb_assert(sk.numPoly() > 0, @@ -77,14 +89,16 @@ void DecryptorT::decrypt(const CiphertextT &ctxt, static_cast(ctxt[0].size() * (degree >> 10)); utils::setOmpThreadLimit(max_num_threads); - CiphertextT ctxt_copy = - ctxt.deepCopy(std::min(ctxt[0].size(), MAX_DECRYPT_SIZE)); - PolynomialT &ax = ctxt_copy[ctxt_copy.numPoly() - 1]; + const Size num_polyunit = std::min(ctxt[0].size(), MAX_DECRYPT_SIZE); + PolynomialT ax(ctxt[ctxt.numPoly() - 1]); + ax.setSize(preset, num_polyunit); + if (!ax[0].isNTT()) { forwardNTT(modarith, ax); } for (Size i = 0; i < num_secret; ++i) { - CiphertextT ctxt_tmp(ctxt_copy, i); + CiphertextT ctxt_tmp(ctxt, i); + ctxt_tmp.setNumPolyunit(num_polyunit); for (Size j = 0; j < ctxt_tmp.numPoly(); ++j) { if (!ctxt_tmp[j][0].isNTT()) { forwardNTT(modarith, ctxt_tmp[j]); @@ -97,7 +111,7 @@ void DecryptorT::decrypt(const CiphertextT &ctxt, } else if constexpr (std::is_same_v || std::is_same_v) { PolynomialT ptxt_tmp = innerDecrypt(ctxt_tmp, sk[i], ax); - decodeWithoutFFT(ptxt_tmp, msg[i], scale); + innerDecode(ptxt_tmp, msg[i], scale); } else { throw std::runtime_error( "[Decryptor::decrypt] Unsupported message type"); @@ -133,6 +147,41 @@ DecryptorT::innerDecrypt(const CiphertextT &ctxt, return ptxt; } +template +template +void DecryptorT::innerDecode(const PolynomialT &ptxt, CMSG &coeff, + Real scale) const { + if (ptxt.size() != 1) { + decodeWithPolyPair(ptxt, coeff, scale); + } else { + decodeWithSinglePoly(ptxt, coeff, scale); + } +} + +template +template +void DecryptorT::decode(const PolynomialT &ptxt, MSG &msg, + Real scale) const { + + deb_assert(msg.size() >= num_slots, + "[Decryptor::decode] Message size is too small"); + if constexpr (std::is_same_v || + std::is_same_v) { + CoeffMessage coeff(preset); + innerDecode(ptxt, coeff, scale); + + const auto half_degree = num_slots; + for (Size i = 0; i < msg.size(); ++i) { + msg[i].real(coeff[i]); + msg[i].imag(coeff[i + half_degree]); + } + fft_.forwardFFT(msg); + } else { + throw std::runtime_error( + "[Decryptor::decode] Unsupported message type"); + } +} + template template void DecryptorT::decodeWithSinglePoly(const PolynomialT &ptxt, @@ -225,50 +274,6 @@ void DecryptorT::decodeWithPolyPair(const PolynomialT &ptxt, } } -template -template -void DecryptorT::decodeWithoutFFT(const PolynomialT &ptxt, CMSG &coeff, - Real scale) const { - if (ptxt.size() != 1) { - decodeWithPolyPair(ptxt, coeff, scale); - } else { - decodeWithSinglePoly(ptxt, coeff, scale); - } -} - -template -template -void DecryptorT::decode(const PolynomialT &ptxt, MSG &msg, - Real scale) const { - - deb_assert(msg.size() >= num_slots, - "[Decryptor::decode] Message size is too small"); - if constexpr (std::is_same_v) { - CoeffMessage coeff(preset); - decodeWithoutFFT(ptxt, coeff, scale); - - const auto half_degree = num_slots; - for (Size i = 0; i < msg.size(); ++i) { - msg[i].real(coeff[i]); - msg[i].imag(coeff[i + half_degree]); - } - fft_.forwardFFT(msg); - } else if constexpr (std::is_same_v) { - FCoeffMessage coeff(preset); - decodeWithoutFFT(ptxt, coeff, scale); - - const auto half_degree = num_slots; - for (Size i = 0; i < msg.size(); ++i) { - msg[i].real(coeff[i]); - msg[i].imag(coeff[i + half_degree]); - } - fft_.forwardFFT(msg); - } else { - throw std::runtime_error( - "[Decryptor::decode] Unsupported message type"); - } -} - #ifdef DEB_U64 #define X(preset) DECRYPT_TYPE_TEMPLATE(PRESET_##preset, u64, ) PRESET_LIST_WITH_EMPTY diff --git a/src/Encryptor.cpp b/src/Encryptor.cpp index f94d56b..de3cc7c 100644 --- a/src/Encryptor.cpp +++ b/src/Encryptor.cpp @@ -53,11 +53,11 @@ EncryptorT::EncryptorT(std::optional seeds) } template -EncryptorT::EncryptorT(Preset actual_preset, +EncryptorT::EncryptorT(Preset target_preset, std::optional seeds) - : PresetTraits(actual_preset), - ptxt_buffer_(actual_preset, num_p * num_secret), - vx_buffer_(actual_preset, true), ex_buffer_(actual_preset, true), + : PresetTraits(target_preset), + ptxt_buffer_(target_preset, num_p * num_secret), + vx_buffer_(target_preset, true), ex_buffer_(target_preset, true), samples_(buffer_size(degree)), mask_(degree), i_samples_(degree), fft_(degree) { @@ -72,11 +72,11 @@ EncryptorT::EncryptorT(Preset actual_preset, } template -EncryptorT::EncryptorT(Preset actual_preset, +EncryptorT::EncryptorT(Preset target_preset, std::shared_ptr rng) - : PresetTraits(actual_preset), - ptxt_buffer_(actual_preset, num_p * num_secret), - vx_buffer_(actual_preset, true), ex_buffer_(actual_preset, true), + : PresetTraits(target_preset), + ptxt_buffer_(target_preset, num_p * num_secret), + vx_buffer_(target_preset, true), ex_buffer_(target_preset, true), samples_(degree + (sizeof(u64) / sizeof(U)) * div_ceil_32(degree)), mask_(degree), i_samples_(degree), rng_(std::move(rng)), fft_(degree) { @@ -137,10 +137,10 @@ void EncryptorT::encrypt(const MSG *msg, const KEY &key, for (Size i = 0; i < num_secret; ++i) { PolynomialT ptxt_tmp(ptxt, single_num_polyunit * i, single_num_polyunit); - encodeWithoutNTT(msg[i], ptxt_tmp, single_num_polyunit, opt.scale); + encode(msg[i], ptxt_tmp, single_num_polyunit, opt.scale); } } else { - encodeWithoutNTT(msg[0], ptxt, single_num_polyunit, opt.scale); + encode(msg[0], ptxt, single_num_polyunit, opt.scale); } innerEncrypt(ptxt, key, single_num_polyunit, ctxt); @@ -282,9 +282,9 @@ void EncryptorT::innerEncrypt(const PolynomialT &ptxt, const KEY &key, template template -void EncryptorT::embeddingToN(const MSG &msg, const Real &delta, - PolynomialT &ptxt, - const Size size) const { +void EncryptorT::innerEncode(const MSG &msg, const Real &delta, + PolynomialT &ptxt, + const Size size) const { const auto msg_size = msg.size(); Size gap = degree / msg_size; if constexpr (std::is_same_v) { @@ -338,19 +338,18 @@ void EncryptorT::embeddingToN(const MSG &msg, const Real &delta, template template -void EncryptorT::encodeWithoutNTT(const MSG &msg, PolynomialT &ptxt, - const Size size, - const Real scale) const { +void EncryptorT::encode(const MSG &msg, PolynomialT &ptxt, + const Size size, const Real scale) const { const Real delta{scale == 0 ? std::pow(static_cast(2), scale_factors[ptxt.size() - 1]) : scale}; if constexpr (std::is_same_v || std::is_same_v) { - embeddingToN(msg, delta, ptxt, size); + innerEncode(msg, delta, ptxt, size); } else if constexpr (std::is_same_v) { Message tmp(msg.size(), msg.data()); fft_.backwardFFT(tmp); - embeddingToN(tmp, delta, ptxt, size); + innerEncode(tmp, delta, ptxt, size); } else if constexpr (std::is_same_v) { Message tmp(msg.size()); for (Size i = 0; i < msg.size(); ++i) { @@ -358,10 +357,10 @@ void EncryptorT::encodeWithoutNTT(const MSG &msg, PolynomialT &ptxt, static_cast(msg[i].imag())); } fft_.backwardFFT(tmp); - embeddingToN(tmp, delta, ptxt, size); + innerEncode(tmp, delta, ptxt, size); } else { throw std::runtime_error( - "[Encryptor::encodeWithoutNTT] Unsupported message type"); + "[Encryptor::encode] Unsupported message type"); } } diff --git a/src/KeyGenerator.cpp b/src/KeyGenerator.cpp index 25f3a52..eadf805 100644 --- a/src/KeyGenerator.cpp +++ b/src/KeyGenerator.cpp @@ -21,8 +21,8 @@ namespace { template -inline void checkSecretKey(const deb::Preset preset, - const deb::SecretKeyT &sk) { +inline void checkSecretKey([[maybe_unused]] const deb::Preset preset, + [[maybe_unused]] const deb::SecretKeyT &sk) { deb_assert(preset == sk.preset(), "[KeyGenerator] Preset mismatch between KeyGenerator and " "SecretKey."); @@ -34,7 +34,8 @@ inline void checkSecretKey(const deb::Preset preset, }; template -inline void checkSwk(const deb::Preset &preset, const deb::SwitchKeyT &swk, +inline void checkSwk([[maybe_unused]] const deb::Preset &preset, + [[maybe_unused]] const deb::SwitchKeyT &swk, const deb::SwitchKeyKind expected_type) { deb_assert(preset == swk.preset(), "[KeyGenerator] Preset mismatch between KeyGenerator and " @@ -43,9 +44,10 @@ inline void checkSwk(const deb::Preset &preset, const deb::SwitchKeyT &swk, "[KeyGenerator] The provided switching key has invalid type."); }; -inline void checkModPackKeyBundleCondition(const deb::Preset &preset, - const deb::Preset &preset_from, - const deb::Preset &preset_to) { +inline void +checkModPackKeyBundleCondition([[maybe_unused]] const deb::Preset &preset, + [[maybe_unused]] const deb::Preset &preset_from, + [[maybe_unused]] const deb::Preset &preset_to) { [[maybe_unused]] const deb::Size from_degree = get_degree(preset_from); [[maybe_unused]] const deb::Size from_rank = get_rank(preset_from); @@ -97,9 +99,9 @@ KeyGeneratorT::KeyGeneratorT(std::optional seeds) } template -KeyGeneratorT::KeyGeneratorT(const Preset preset, +KeyGeneratorT::KeyGeneratorT(const Preset target_preset, std::optional seeds) - : PresetTraits(preset), fft_(degree) { + : PresetTraits(target_preset), fft_(degree) { for (u64 i = 0; i < num_p; ++i) { modarith.emplace_back(degree, primes[i]); } @@ -112,9 +114,9 @@ KeyGeneratorT::KeyGeneratorT(const Preset preset, } template -KeyGeneratorT::KeyGeneratorT(const Preset preset, +KeyGeneratorT::KeyGeneratorT(const Preset target_preset, std::shared_ptr rng) - : PresetTraits(preset), rng_(std::move(rng)), fft_(degree) { + : PresetTraits(target_preset), rng_(std::move(rng)), fft_(degree) { for (u64 i = 0; i < num_p; ++i) { modarith.emplace_back(degree, primes[i]); } diff --git a/src/SeedGenerator.cpp b/src/SeedGenerator.cpp index efa561f..b426215 100644 --- a/src/SeedGenerator.cpp +++ b/src/SeedGenerator.cpp @@ -22,11 +22,11 @@ namespace deb { -SeedGenerator &SeedGenerator::GetInstance(std::optional seeds) { +SeedGenerator &SeedGenerator::GetInstance(const std::optional &seeds) { static SeedGenerator instance(seeds); return instance; } -void SeedGenerator::Reseed(const std::optional &seeds) { +void SeedGenerator::Reseed(const std::optional &seeds) { const auto &s = seeds.value(); GetInstance().rng_->reseed(reinterpret_cast(s.data()), DEB_RNG_SEED_BYTE_SIZE); @@ -34,23 +34,25 @@ void SeedGenerator::Reseed(const std::optional &seeds) { RNGSeed SeedGenerator::Gen() { return GetInstance().genSeed(); } -SeedGenerator::SeedGenerator(std::optional seeds) { +SeedGenerator::SeedGenerator(const std::optional &seeds) { if (!seeds) { std::random_device rd; - RNGSeed nseeds; + RNGSeed nseeds = {}; for (size_t i = 0; i < nseeds.size(); ++i) { auto ptr = reinterpret_cast(&nseeds[i]); for (size_t j = 0; j < sizeof(u64) / sizeof(unsigned int); ++j) { ptr[j] = rd(); } } - seeds.emplace(nseeds); + // seeds.emplace(nseeds); + rng_ = createRandomGenerator(nseeds); + } else { + rng_ = createRandomGenerator(seeds.value()); } - rng_ = createRandomGenerator(seeds.value()); } RNGSeed SeedGenerator::genSeed() { - RNGSeed seeds; + RNGSeed seeds = {}; rng_->getRandomUint64Array(seeds.data(), DEB_U64_SEED_SIZE); return seeds; }