Skip to content

Commit 78128b9

Browse files
- 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
1 parent d7acca9 commit 78128b9

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

src/binfhe/include/binfhe-base-scheme.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <map>
4545
#include <memory>
4646
#include <vector>
47+
#include <optional>
4748

4849
namespace lbcrypto {
4950

@@ -57,6 +58,20 @@ typedef struct {
5758
LWEPublicKey Pkey;
5859
} RingGSWBTKey;
5960

61+
class VecLWECiphertext {
62+
public:
63+
std::vector<LWECiphertext> m_ctvector;
64+
65+
VecLWECiphertext() = default;
66+
~VecLWECiphertext() = default;
67+
68+
bool validate() const;
69+
70+
private:
71+
mutable std::optional<bool> m_independent;
72+
};
73+
74+
6075
/**
6176
* @brief Ring GSW accumulator schemes described in
6277
* 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 {
112127
* @return a shared pointer to the resulting ciphertext
113128
*/
114129
LWECiphertext EvalBinGate(const std::shared_ptr<BinFHECryptoParams>& params, BINGATE gate, const RingGSWBTKey& EK,
115-
const std::vector<LWECiphertext>& ctvector) const;
116-
130+
const VecLWECiphertext& ctvector) const;
117131
/**
118132
* Evaluates NOT gate
119133
*

src/binfhe/lib/binfhe-base-scheme.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,12 @@ LWECiphertext BinFHEScheme::EvalBinGate(const std::shared_ptr<BinFHECryptoParams
115115

116116
// Full evaluation as described in https://eprint.iacr.org/2020/086
117117
LWECiphertext BinFHEScheme::EvalBinGate(const std::shared_ptr<BinFHECryptoParams>& params, BINGATE gate,
118-
const RingGSWBTKey& EK, const std::vector<LWECiphertext>& ctvector) const {
118+
const RingGSWBTKey& EK, const VecLWECiphertext& in_ctvector) const {
119119
// check if the ciphertexts are all independent
120-
for (size_t i = 0; i < ctvector.size(); i++) {
121-
for (size_t j = i + 1; j < ctvector.size(); j++) {
122-
if (ctvector[j] == ctvector[i]) {
120+
if ( !in_ctvector.validate() ) {
123121
OPENFHE_THROW("Input ciphertexts should be independent");
124-
}
125-
}
126122
}
123+
auto& ctvector = in_ctvector.m_ctvector;
127124

128125
NativeInteger p = ctvector[0]->GetptModulus();
129126

@@ -448,7 +445,7 @@ std::vector<LWECiphertext> BinFHEScheme::EvalDecomp(const std::shared_ptr<BinFHE
448445
while (mod > q) {
449446
auto ctq = std::make_shared<LWECiphertextImpl>(*cttmp);
450447
ctq->SetModulus(q);
451-
ret.push_back(std::move(ctq));
448+
ret.emplace_back(std::move(ctq));
452449

453450
// Floor the input sequentially to obtain the most significant bit
454451
cttmp = EvalFloor(params, curEK, cttmp, beta);
@@ -604,4 +601,23 @@ LWECiphertext BinFHEScheme::BootstrapFunc(const std::shared_ptr<BinFHECryptoPara
604601
return LWEscheme->ModSwitch(fmod, ctKS);
605602
}
606603

604+
/// VecLHECipherText
605+
bool VecLWECiphertext::validate() const {
606+
if ( m_independent.has_value() ) {
607+
return m_independent.value();
608+
}
609+
610+
for (size_t i = 0; i < m_ctvector.size(); i++) {
611+
for (size_t j = i + 1; j < m_ctvector.size(); j++) {
612+
if (m_ctvector[j] == m_ctvector[i]) {
613+
m_independent = false;
614+
break;
615+
}
616+
}
617+
}
618+
m_independent = true;
619+
return m_independent.value();
620+
}
621+
622+
607623
}; // namespace lbcrypto

src/binfhe/lib/binfhecontext.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,9 @@ LWECiphertext BinFHEContext::EvalBinGate(const BINGATE gate, ConstLWECiphertext&
301301
}
302302

303303
LWECiphertext BinFHEContext::EvalBinGate(const BINGATE gate, const std::vector<LWECiphertext>& ctvector) const {
304-
return m_binfhescheme->EvalBinGate(m_params, gate, m_BTKey, ctvector);
304+
VecLWECiphertext ctvec;
305+
ctvec.m_ctvector = std::move(ctvector);
306+
return m_binfhescheme->EvalBinGate(m_params, gate, m_BTKey, ctvec);
305307
}
306308

307309
LWECiphertext BinFHEContext::Bootstrap(ConstLWECiphertext& ct) const {

0 commit comments

Comments
 (0)