From d7acca9f42d5f1de10406c526e8b8eff70980500 Mon Sep 17 00:00:00 2001 From: Muthu Annamalai Date: Sun, 14 Apr 2024 20:04:23 -0700 Subject: [PATCH 1/3] - early exit when possible --- src/core/examples/parallel.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core/examples/parallel.cpp b/src/core/examples/parallel.cpp index f7094e5ea..c50d83491 100644 --- a/src/core/examples/parallel.cpp +++ b/src/core/examples/parallel.cpp @@ -49,6 +49,7 @@ void verify(float* foo, uint32_t array_size) { for (size_t i = 1; i < array_size; ++i) { if ((foo[i] - foo[i - 1]) != 1) { goodflag = goodflag & false; + break; } } if (goodflag) { From 0f1d31119bf33557258a6055e7f2eb9dbeca11ae Mon Sep 17 00:00:00 2001 From: Muthu Annamalai Date: Sun, 14 Apr 2024 22:23:12 -0700 Subject: [PATCH 2/3] - Introduce VecLWECipherText which holds a vector of LWE CipherText with known validation status. - This can reduce time for running binary operations potentially speedup the computations --- src/binfhe/include/binfhe-base-scheme.h | 18 ++++++++++++-- src/binfhe/lib/binfhe-base-scheme.cpp | 32 +++++++++++++++++++------ src/binfhe/lib/binfhecontext.cpp | 4 +++- 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/binfhe/include/binfhe-base-scheme.h b/src/binfhe/include/binfhe-base-scheme.h index 1dddd71ca..210a07c92 100644 --- a/src/binfhe/include/binfhe-base-scheme.h +++ b/src/binfhe/include/binfhe-base-scheme.h @@ -44,6 +44,7 @@ #include #include #include +#include namespace lbcrypto { @@ -57,6 +58,20 @@ typedef struct { LWEPublicKey Pkey; } RingGSWBTKey; +class VecLWECiphertext { + public: + std::vector m_ctvector; + + VecLWECiphertext() = default; + ~VecLWECiphertext() = default; + + bool validate() const; + + private: + mutable std::optional m_independent; +}; + + /** * @brief Ring GSW accumulator schemes described in * https://eprint.iacr.org/2014/816, https://eprint.iacr.org/2020/086 and https://eprint.iacr.org/2022/198 @@ -112,8 +127,7 @@ class BinFHEScheme { * @return a shared pointer to the resulting ciphertext */ LWECiphertext EvalBinGate(const std::shared_ptr& params, BINGATE gate, const RingGSWBTKey& EK, - const std::vector& ctvector) const; - + const VecLWECiphertext& ctvector) const; /** * Evaluates NOT gate * diff --git a/src/binfhe/lib/binfhe-base-scheme.cpp b/src/binfhe/lib/binfhe-base-scheme.cpp index a067a1274..b1597e8c4 100644 --- a/src/binfhe/lib/binfhe-base-scheme.cpp +++ b/src/binfhe/lib/binfhe-base-scheme.cpp @@ -115,15 +115,12 @@ LWECiphertext BinFHEScheme::EvalBinGate(const std::shared_ptr& params, BINGATE gate, - const RingGSWBTKey& EK, const std::vector& ctvector) const { + const RingGSWBTKey& EK, const VecLWECiphertext& in_ctvector) const { // check if the ciphertexts are all independent - for (size_t i = 0; i < ctvector.size(); i++) { - for (size_t j = i + 1; j < ctvector.size(); j++) { - if (ctvector[j] == ctvector[i]) { + if ( !in_ctvector.validate() ) { OPENFHE_THROW("Input ciphertexts should be independent"); - } - } } + auto& ctvector = in_ctvector.m_ctvector; NativeInteger p = ctvector[0]->GetptModulus(); @@ -448,7 +445,7 @@ std::vector BinFHEScheme::EvalDecomp(const std::shared_ptr q) { auto ctq = std::make_shared(*cttmp); ctq->SetModulus(q); - ret.push_back(std::move(ctq)); + ret.emplace_back(std::move(ctq)); // Floor the input sequentially to obtain the most significant bit cttmp = EvalFloor(params, curEK, cttmp, beta); @@ -604,4 +601,25 @@ LWECiphertext BinFHEScheme::BootstrapFunc(const std::shared_ptrModSwitch(fmod, ctKS); } +/// VecLHECipherText +bool VecLWECiphertext::validate() const { + if ( m_independent.has_value() ) { + return m_independent.value(); + } + + m_independent = true; + + for (size_t i = 0; i < m_ctvector.size(); i++) { + for (size_t j = i + 1; j < m_ctvector.size(); j++) { + if (m_ctvector[j] == m_ctvector[i]) { + m_independent = false; + break; + } + } + } + + return m_independent.value(); +} + + }; // namespace lbcrypto diff --git a/src/binfhe/lib/binfhecontext.cpp b/src/binfhe/lib/binfhecontext.cpp index 951b3d023..e36a4f3ed 100644 --- a/src/binfhe/lib/binfhecontext.cpp +++ b/src/binfhe/lib/binfhecontext.cpp @@ -301,7 +301,9 @@ LWECiphertext BinFHEContext::EvalBinGate(const BINGATE gate, ConstLWECiphertext& } LWECiphertext BinFHEContext::EvalBinGate(const BINGATE gate, const std::vector& ctvector) const { - return m_binfhescheme->EvalBinGate(m_params, gate, m_BTKey, ctvector); + VecLWECiphertext ctvec; + ctvec.m_ctvector = std::move(ctvector); + return m_binfhescheme->EvalBinGate(m_params, gate, m_BTKey, ctvec); } LWECiphertext BinFHEContext::Bootstrap(ConstLWECiphertext& ct) const { From 64e884975bfcb75aca9adfdbe0cc79f17c2619f0 Mon Sep 17 00:00:00 2001 From: Muthu Annamalai Date: Tue, 16 Apr 2024 16:00:48 -0700 Subject: [PATCH 3/3] - fix start state bug --- src/binfhe/include/binfhe-base-scheme.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/binfhe/include/binfhe-base-scheme.h b/src/binfhe/include/binfhe-base-scheme.h index 210a07c92..2a81c2fb0 100644 --- a/src/binfhe/include/binfhe-base-scheme.h +++ b/src/binfhe/include/binfhe-base-scheme.h @@ -68,7 +68,7 @@ class VecLWECiphertext { bool validate() const; private: - mutable std::optional m_independent; + mutable std::optional m_independent = std::nullopt; };