Skip to content
Merged
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
134 changes: 48 additions & 86 deletions benchmark/src/lib-benchmark.cpp

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions src/pke/include/keyswitch/keyswitch-base.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ class KeySwitchBase {
// CORE OPERATIONS
/////////////////////////////////////////

virtual std::shared_ptr<std::vector<Element>> KeySwitchCore(const Element& a,
const EvalKey<Element> evalKey) const {
virtual std::vector<Element> KeySwitchCore(const Element& a, const EvalKey<Element> evalKey) const {
OPENFHE_THROW(NOT_SUPPORTED_ERROR);
}

Expand All @@ -123,15 +122,15 @@ class KeySwitchBase {
OPENFHE_THROW(NOT_SUPPORTED_ERROR);
}

virtual std::shared_ptr<std::vector<Element>> EvalFastKeySwitchCore(
const std::shared_ptr<std::vector<Element>> digits, const EvalKey<Element> evalKey,
const std::shared_ptr<ParmType> paramsQl) const {
virtual std::vector<Element> EvalFastKeySwitchCore(const std::shared_ptr<std::vector<Element>> digits,
const EvalKey<Element> evalKey,
const std::shared_ptr<ParmType> paramsQl) const {
OPENFHE_THROW(NOT_SUPPORTED_ERROR);
}

virtual std::shared_ptr<std::vector<Element>> EvalFastKeySwitchCoreExt(
const std::shared_ptr<std::vector<Element>> digits, const EvalKey<Element> evalKey,
const std::shared_ptr<ParmType> paramsQl) const {
virtual std::vector<Element> EvalFastKeySwitchCoreExt(const std::shared_ptr<std::vector<Element>> digits,
const EvalKey<Element> evalKey,
const std::shared_ptr<ParmType> paramsQl) const {
OPENFHE_THROW(NOT_SUPPORTED_ERROR);
}
};
Expand Down
9 changes: 4 additions & 5 deletions src/pke/include/keyswitch/keyswitch-bv.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,14 @@ class KeySwitchBV : public KeySwitchRNS {
// CORE OPERATIONS
/////////////////////////////////////////

std::shared_ptr<std::vector<DCRTPoly>> KeySwitchCore(const DCRTPoly& a,
const EvalKey<DCRTPoly> evalKey) const override;
std::vector<DCRTPoly> KeySwitchCore(const DCRTPoly& a, const EvalKey<DCRTPoly> evalKey) const override;

std::shared_ptr<std::vector<DCRTPoly>> EvalKeySwitchPrecomputeCore(
const DCRTPoly& c, std::shared_ptr<CryptoParametersBase<DCRTPoly>> cryptoParamsBase) const override;

std::shared_ptr<std::vector<DCRTPoly>> EvalFastKeySwitchCore(
const std::shared_ptr<std::vector<DCRTPoly>> digits, const EvalKey<DCRTPoly> evalKey,
const std::shared_ptr<ParmType> paramsQl) const override;
std::vector<DCRTPoly> EvalFastKeySwitchCore(const std::shared_ptr<std::vector<DCRTPoly>> digits,
const EvalKey<DCRTPoly> evalKey,
const std::shared_ptr<ParmType> paramsQl) const override;

/////////////////////////////////////////
// SERIALIZATION
Expand Down
15 changes: 7 additions & 8 deletions src/pke/include/keyswitch/keyswitch-hybrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,18 @@ class KeySwitchHYBRID : public KeySwitchRNS {
// CORE OPERATIONS
/////////////////////////////////////////

std::shared_ptr<std::vector<DCRTPoly>> KeySwitchCore(const DCRTPoly& a,
const EvalKey<DCRTPoly> evalKey) const override;
std::vector<DCRTPoly> KeySwitchCore(const DCRTPoly& a, const EvalKey<DCRTPoly> evalKey) const override;

std::shared_ptr<std::vector<DCRTPoly>> EvalKeySwitchPrecomputeCore(
const DCRTPoly& c, std::shared_ptr<CryptoParametersBase<DCRTPoly>> cryptoParamsBase) const override;

std::shared_ptr<std::vector<DCRTPoly>> EvalFastKeySwitchCore(
const std::shared_ptr<std::vector<DCRTPoly>> digits, const EvalKey<DCRTPoly> evalKey,
const std::shared_ptr<ParmType> paramsQl) const override;
std::vector<DCRTPoly> EvalFastKeySwitchCore(const std::shared_ptr<std::vector<DCRTPoly>> digits,
const EvalKey<DCRTPoly> evalKey,
const std::shared_ptr<ParmType> paramsQl) const override;

std::shared_ptr<std::vector<DCRTPoly>> EvalFastKeySwitchCoreExt(
const std::shared_ptr<std::vector<DCRTPoly>> digits, const EvalKey<DCRTPoly> evalKey,
const std::shared_ptr<ParmType> paramsQl) const override;
std::vector<DCRTPoly> EvalFastKeySwitchCoreExt(const std::shared_ptr<std::vector<DCRTPoly>> digits,
const EvalKey<DCRTPoly> evalKey,
const std::shared_ptr<ParmType> paramsQl) const override;

/////////////////////////////////////////
// SERIALIZATION
Expand Down
15 changes: 7 additions & 8 deletions src/pke/include/schemebase/base-scheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,9 @@ class SchemeBase {
return m_KeySwitch->EvalKeySwitchPrecomputeCore(c, cryptoParamsBase);
}

virtual std::shared_ptr<std::vector<Element>> EvalFastKeySwitchCoreExt(
const std::shared_ptr<std::vector<Element>> digits, const EvalKey<Element> evalKey,
const std::shared_ptr<ParmType> params) const {
virtual std::vector<Element> EvalFastKeySwitchCoreExt(const std::shared_ptr<std::vector<Element>> digits,
const EvalKey<Element> evalKey,
const std::shared_ptr<ParmType> params) const {
VerifyKeySwitchEnabled(__func__);
if (nullptr == digits)
OPENFHE_THROW("Input digits is nullptr");
Expand All @@ -306,9 +306,9 @@ class SchemeBase {
return m_KeySwitch->EvalFastKeySwitchCoreExt(digits, evalKey, params);
}

virtual std::shared_ptr<std::vector<Element>> EvalFastKeySwitchCore(
const std::shared_ptr<std::vector<Element>> digits, const EvalKey<Element> evalKey,
const std::shared_ptr<ParmType> params) const {
virtual std::vector<Element> EvalFastKeySwitchCore(const std::shared_ptr<std::vector<Element>> digits,
const EvalKey<Element> evalKey,
const std::shared_ptr<ParmType> params) const {
VerifyKeySwitchEnabled(__func__);
if (nullptr == digits)
OPENFHE_THROW("Input digits is nullptr");
Expand All @@ -321,8 +321,7 @@ class SchemeBase {
return m_KeySwitch->EvalFastKeySwitchCore(digits, evalKey, params);
}

virtual std::shared_ptr<std::vector<Element>> KeySwitchCore(const Element& a,
const EvalKey<Element> evalKey) const {
virtual std::vector<Element> KeySwitchCore(const Element& a, const EvalKey<Element> evalKey) const {
VerifyKeySwitchEnabled(__func__);
if (!evalKey)
OPENFHE_THROW("Input evaluation key is nullptr");
Expand Down
29 changes: 13 additions & 16 deletions src/pke/lib/keyswitch/keyswitch-bv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,23 +227,19 @@ EvalKey<DCRTPoly> KeySwitchBV::KeySwitchGenInternal(const PrivateKey<DCRTPoly> o
void KeySwitchBV::KeySwitchInPlace(Ciphertext<DCRTPoly>& ciphertext, const EvalKey<DCRTPoly> ek) const {
auto& cv = ciphertext->GetElements();
auto ba = KeySwitchCore(cv.back(), ek);

cv[0].SetFormat(Format::EVALUATION);
cv[0] += (*ba)[0];

cv[0] += ba[0];
if (cv.size() > 2) {
cv[1].SetFormat(Format::EVALUATION);
cv[1] += (*ba)[1];
cv[1] += ba[1];
}
else {
cv[1] = (*ba)[1];
cv[1] = ba[1];
}

cv.resize(2);
}

std::shared_ptr<std::vector<DCRTPoly>> KeySwitchBV::KeySwitchCore(const DCRTPoly& a,
const EvalKey<DCRTPoly> evalKey) const {
std::vector<DCRTPoly> KeySwitchBV::KeySwitchCore(const DCRTPoly& a, const EvalKey<DCRTPoly> evalKey) const {
return EvalFastKeySwitchCore(EvalKeySwitchPrecomputeCore(a, evalKey->GetCryptoParameters()), evalKey,
a.GetParams());
}
Expand All @@ -254,9 +250,9 @@ std::shared_ptr<std::vector<DCRTPoly>> KeySwitchBV::EvalKeySwitchPrecomputeCore(
return std::make_shared<std::vector<DCRTPoly>>(c.CRTDecompose(cryptoParams->GetDigitSize()));
}

std::shared_ptr<std::vector<DCRTPoly>> KeySwitchBV::EvalFastKeySwitchCore(
const std::shared_ptr<std::vector<DCRTPoly>> digits, const EvalKey<DCRTPoly> evalKey,
const std::shared_ptr<ParmType> paramsQl) const {
std::vector<DCRTPoly> KeySwitchBV::EvalFastKeySwitchCore(const std::shared_ptr<std::vector<DCRTPoly>> digits,
const EvalKey<DCRTPoly> evalKey,
const std::shared_ptr<ParmType> paramsQl) const {
std::vector<DCRTPoly> bv(evalKey->GetBVector());
std::vector<DCRTPoly> av(evalKey->GetAVector());
const auto diffQl = bv[0].GetParams()->GetParams().size() - paramsQl->GetParams().size();
Expand All @@ -268,13 +264,14 @@ std::shared_ptr<std::vector<DCRTPoly>> KeySwitchBV::EvalFastKeySwitchCore(
av[i].DropLastElements(diffQl);
av[i] *= (*digits)[i];
}

std::vector<DCRTPoly> res{std::move(bv[0]), std::move(av[0])};
for (uint32_t i = 1; i < limit; ++i) {
res[0] += bv[i];
res[1] += av[i];
bv[0] += bv[i];
av[0] += av[i];
}
return std::make_shared<std::vector<DCRTPoly>>(std::move(res));
std::vector<DCRTPoly> res;
res.emplace_back(std::move(bv[0]));
res.emplace_back(std::move(av[0]));
return res;
}

} // namespace lbcrypto
58 changes: 26 additions & 32 deletions src/pke/lib/keyswitch/keyswitch-hybrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,15 @@ EvalKey<DCRTPoly> KeySwitchHYBRID::KeySwitchGenInternal(const PrivateKey<DCRTPol
void KeySwitchHYBRID::KeySwitchInPlace(Ciphertext<DCRTPoly>& ciphertext, const EvalKey<DCRTPoly> ek) const {
auto& cv = ciphertext->GetElements();
auto ba = KeySwitchCore(cv.back(), ek);

cv[0].SetFormat(Format::EVALUATION);
cv[0] += (*ba)[0];

cv[0] += ba[0];
if (cv.size() > 2) {
cv[1].SetFormat(Format::EVALUATION);
cv[1] += (*ba)[1];
cv[1] += ba[1];
}
else {
cv[1] = (*ba)[1];
cv[1] = ba[1];
}

cv.resize(2);
}

Expand Down Expand Up @@ -305,8 +302,7 @@ DCRTPoly KeySwitchHYBRID::KeySwitchDownFirstElement(ConstCiphertext<DCRTPoly> ci
cryptoParams->GettInvModpPrecon(), t, cryptoParams->GettModqPrecon());
}

std::shared_ptr<std::vector<DCRTPoly>> KeySwitchHYBRID::KeySwitchCore(const DCRTPoly& a,
const EvalKey<DCRTPoly> evalKey) const {
std::vector<DCRTPoly> KeySwitchHYBRID::KeySwitchCore(const DCRTPoly& a, const EvalKey<DCRTPoly> evalKey) const {
return EvalFastKeySwitchCore(EvalKeySwitchPrecomputeCore(a, evalKey->GetCryptoParameters()), evalKey,
a.GetParams());
}
Expand Down Expand Up @@ -378,30 +374,30 @@ std::shared_ptr<std::vector<DCRTPoly>> KeySwitchHYBRID::EvalKeySwitchPrecomputeC
return result;
}

std::shared_ptr<std::vector<DCRTPoly>> KeySwitchHYBRID::EvalFastKeySwitchCore(
const std::shared_ptr<std::vector<DCRTPoly>> digits, const EvalKey<DCRTPoly> evalKey,
const std::shared_ptr<ParmType> paramsQl) const {
std::vector<DCRTPoly> KeySwitchHYBRID::EvalFastKeySwitchCore(const std::shared_ptr<std::vector<DCRTPoly>> digits,
const EvalKey<DCRTPoly> evalKey,
const std::shared_ptr<ParmType> paramsQl) const {
const auto cryptoParams = std::dynamic_pointer_cast<CryptoParametersRNS>(evalKey->GetCryptoParameters());

const PlaintextModulus t = (cryptoParams->GetNoiseScale() == 1) ? 0 : cryptoParams->GetPlaintextModulus();

auto result = EvalFastKeySwitchCoreExt(digits, evalKey, paramsQl);
(*result)[0] = (*result)[0].ApproxModDown(paramsQl, cryptoParams->GetParamsP(), cryptoParams->GetPInvModq(),
cryptoParams->GetPInvModqPrecon(), cryptoParams->GetPHatInvModp(),
cryptoParams->GetPHatInvModpPrecon(), cryptoParams->GetPHatModq(),
cryptoParams->GetModqBarrettMu(), cryptoParams->GettInvModp(),
cryptoParams->GettInvModpPrecon(), t, cryptoParams->GettModqPrecon());
(*result)[1] = (*result)[1].ApproxModDown(paramsQl, cryptoParams->GetParamsP(), cryptoParams->GetPInvModq(),
cryptoParams->GetPInvModqPrecon(), cryptoParams->GetPHatInvModp(),
cryptoParams->GetPHatInvModpPrecon(), cryptoParams->GetPHatModq(),
cryptoParams->GetModqBarrettMu(), cryptoParams->GettInvModp(),
cryptoParams->GettInvModpPrecon(), t, cryptoParams->GettModqPrecon());
auto result = EvalFastKeySwitchCoreExt(digits, evalKey, paramsQl);
result[0] = result[0].ApproxModDown(paramsQl, cryptoParams->GetParamsP(), cryptoParams->GetPInvModq(),
cryptoParams->GetPInvModqPrecon(), cryptoParams->GetPHatInvModp(),
cryptoParams->GetPHatInvModpPrecon(), cryptoParams->GetPHatModq(),
cryptoParams->GetModqBarrettMu(), cryptoParams->GettInvModp(),
cryptoParams->GettInvModpPrecon(), t, cryptoParams->GettModqPrecon());
result[1] = result[1].ApproxModDown(paramsQl, cryptoParams->GetParamsP(), cryptoParams->GetPInvModq(),
cryptoParams->GetPInvModqPrecon(), cryptoParams->GetPHatInvModp(),
cryptoParams->GetPHatInvModpPrecon(), cryptoParams->GetPHatModq(),
cryptoParams->GetModqBarrettMu(), cryptoParams->GettInvModp(),
cryptoParams->GettInvModpPrecon(), t, cryptoParams->GettModqPrecon());
return result;
}

std::shared_ptr<std::vector<DCRTPoly>> KeySwitchHYBRID::EvalFastKeySwitchCoreExt(
const std::shared_ptr<std::vector<DCRTPoly>> digits, const EvalKey<DCRTPoly> evalKey,
const std::shared_ptr<ParmType> paramsQl) const {
std::vector<DCRTPoly> KeySwitchHYBRID::EvalFastKeySwitchCoreExt(const std::shared_ptr<std::vector<DCRTPoly>> digits,
const EvalKey<DCRTPoly> evalKey,
const std::shared_ptr<ParmType> paramsQl) const {
const auto paramsQlP = (*digits)[0].GetParams();
const uint32_t sizeQlP = paramsQlP->GetParams().size();

Expand All @@ -413,11 +409,9 @@ std::shared_ptr<std::vector<DCRTPoly>> KeySwitchHYBRID::EvalFastKeySwitchCoreExt
const auto& av = evalKey->GetAVector();
const auto& bv = evalKey->GetBVector();

auto result = std::make_shared<std::vector<DCRTPoly>>();
result->reserve(2);
result->emplace_back(paramsQlP, Format::EVALUATION, true);
result->emplace_back(paramsQlP, Format::EVALUATION, true);
auto& elements = (*result);
std::vector<DCRTPoly> result;
result.emplace_back(paramsQlP, Format::EVALUATION, true);
result.emplace_back(paramsQlP, Format::EVALUATION, true);

for (uint32_t j = 0; j < limit; ++j) {
#pragma omp parallel for num_threads(OpenFHEParallelControls.GetThreadLimit(sizeQlP))
Expand All @@ -426,8 +420,8 @@ std::shared_ptr<std::vector<DCRTPoly>> KeySwitchHYBRID::EvalFastKeySwitchCoreExt
const auto& cji = (*digits)[j].GetElementAtIndex(i);
const auto& bji = bv[j].GetElementAtIndex(idx);
const auto& aji = av[j].GetElementAtIndex(idx);
elements[0].SetElementAtIndex(i, elements[0].GetElementAtIndex(i) + cji * bji);
elements[1].SetElementAtIndex(i, elements[1].GetElementAtIndex(i) + cji * aji);
result[0].SetElementAtIndex(i, result[0].GetElementAtIndex(i) + cji * bji);
result[1].SetElementAtIndex(i, result[1].GetElementAtIndex(i) + cji * aji);
}
}

Expand Down
Loading