Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce VectorLWECipherText with cached validation flag #737

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/binfhe/include/binfhe-base-scheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <map>
#include <memory>
#include <vector>
#include <optional>

namespace lbcrypto {

Expand All @@ -57,6 +58,20 @@ typedef struct {
LWEPublicKey Pkey;
} RingGSWBTKey;

class VecLWECiphertext {
public:
std::vector<LWECiphertext> m_ctvector;

VecLWECiphertext() = default;
~VecLWECiphertext() = default;

bool validate() const;

private:
mutable std::optional<bool> m_independent = std::nullopt;
};


/**
* @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
Expand Down Expand Up @@ -112,8 +127,7 @@ class BinFHEScheme {
* @return a shared pointer to the resulting ciphertext
*/
LWECiphertext EvalBinGate(const std::shared_ptr<BinFHECryptoParams>& params, BINGATE gate, const RingGSWBTKey& EK,
const std::vector<LWECiphertext>& ctvector) const;

const VecLWECiphertext& ctvector) const;
/**
* Evaluates NOT gate
*
Expand Down
32 changes: 25 additions & 7 deletions src/binfhe/lib/binfhe-base-scheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,12 @@ LWECiphertext BinFHEScheme::EvalBinGate(const std::shared_ptr<BinFHECryptoParams

// Full evaluation as described in https://eprint.iacr.org/2020/086
LWECiphertext BinFHEScheme::EvalBinGate(const std::shared_ptr<BinFHECryptoParams>& params, BINGATE gate,
const RingGSWBTKey& EK, const std::vector<LWECiphertext>& 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();

Expand Down Expand Up @@ -448,7 +445,7 @@ std::vector<LWECiphertext> BinFHEScheme::EvalDecomp(const std::shared_ptr<BinFHE
while (mod > q) {
auto ctq = std::make_shared<LWECiphertextImpl>(*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);
Expand Down Expand Up @@ -604,4 +601,25 @@ LWECiphertext BinFHEScheme::BootstrapFunc(const std::shared_ptr<BinFHECryptoPara
return LWEscheme->ModSwitch(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
4 changes: 3 additions & 1 deletion src/binfhe/lib/binfhecontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,9 @@ LWECiphertext BinFHEContext::EvalBinGate(const BINGATE gate, ConstLWECiphertext&
}

LWECiphertext BinFHEContext::EvalBinGate(const BINGATE gate, const std::vector<LWECiphertext>& 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 {
Expand Down
1 change: 1 addition & 0 deletions src/core/examples/parallel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down