Skip to content

Commit

Permalink
Added move semantics
Browse files Browse the repository at this point in the history
  • Loading branch information
dsuponitskiy-duality committed Sep 6, 2024
1 parent 4ea2500 commit b68fcef
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/pke/lib/cryptocontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ void CryptoContextImpl<DCRTPoly>::RecoverSharedKey(PrivateKey<DCRTPoly>& sk,
for (uint32_t i = 0; i < threshold; ++i) {
sum_of_elems += sk_shares[client_indexes[i]];
}
sk->SetPrivateElement(sum_of_elems);
sk->SetPrivateElement(std::move(sum_of_elems));
}
else if (shareType == "shamir") {
// use lagrange interpolation to recover the secret
Expand Down
4 changes: 2 additions & 2 deletions src/pke/lib/keyswitch/keyswitch-hybrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ Ciphertext<DCRTPoly> KeySwitchHYBRID::KeySwitchExt(ConstCiphertext<DCRTPoly> cip
}

Ciphertext<DCRTPoly> result = ciphertext->CloneZero();
result->SetElements(resultElements);
result->SetElements(std::move(resultElements));
return result;
}

Expand Down Expand Up @@ -287,7 +287,7 @@ Ciphertext<DCRTPoly> KeySwitchHYBRID::KeySwitchDown(ConstCiphertext<DCRTPoly> ci
cryptoParams->GettInvModpPrecon(), t, cryptoParams->GettModqPrecon());

Ciphertext<DCRTPoly> result = ciphertext->CloneZero();
result->SetElements({ct0, ct1});
result->SetElements(std::vector<DCRTPoly>{std::move(ct0), std::move(ct1)});
return result;
}

Expand Down
24 changes: 12 additions & 12 deletions src/pke/lib/scheme/ckksrns/ckksrns-fhe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,8 @@ Ciphertext<DCRTPoly> FHECKKSRNS::EvalBootstrap(ConstCiphertext<DCRTPoly> ciphert
ctxtDCRT[i] = temp;
}

raised->SetElements(ctxtDCRT);
raised->SetLevel(L0 - ctxtDCRT[0].GetNumOfElements());
raised->SetElements(std::move(ctxtDCRT));

#ifdef BOOTSTRAPTIMING
std::cerr << "\nNumber of levels at the beginning of bootstrapping: "
Expand Down Expand Up @@ -1616,7 +1616,7 @@ Ciphertext<DCRTPoly> FHECKKSRNS::EvalLinearTransform(const std::vector<ConstPlai
first = cc->KeySwitchDownFirstElement(inner);
auto elements = inner->GetElements();
elements[0].SetValuesToZero();
inner->SetElements(elements);
inner->SetElements(std::move(elements));
result = inner;
}
else {
Expand All @@ -1636,7 +1636,7 @@ Ciphertext<DCRTPoly> FHECKKSRNS::EvalLinearTransform(const std::vector<ConstPlai
result = cc->KeySwitchDown(result);
auto elements = result->GetElements();
elements[0] += first;
result->SetElements(elements);
result->SetElements(std::move(elements));

return result;
}
Expand Down Expand Up @@ -1756,7 +1756,7 @@ Ciphertext<DCRTPoly> FHECKKSRNS::EvalCoeffsToSlots(const std::vector<std::vector
first = cc->KeySwitchDownFirstElement(inner);
auto elements = inner->GetElements();
elements[0].SetValuesToZero();
inner->SetElements(elements);
inner->SetElements(std::move(elements));
outer = inner;
}
else {
Expand All @@ -1774,7 +1774,7 @@ Ciphertext<DCRTPoly> FHECKKSRNS::EvalCoeffsToSlots(const std::vector<std::vector
first += cc->KeySwitchDownFirstElement(inner);
auto elements = inner->GetElements();
elements[0].SetValuesToZero();
inner->SetElements(elements);
inner->SetElements(std::move(elements));
EvalAddExtInPlace(outer, inner);
}
}
Expand Down Expand Up @@ -1819,7 +1819,7 @@ Ciphertext<DCRTPoly> FHECKKSRNS::EvalCoeffsToSlots(const std::vector<std::vector
first = cc->KeySwitchDownFirstElement(inner);
auto elements = inner->GetElements();
elements[0].SetValuesToZero();
inner->SetElements(elements);
inner->SetElements(std::move(elements));
outer = inner;
}
else {
Expand All @@ -1837,7 +1837,7 @@ Ciphertext<DCRTPoly> FHECKKSRNS::EvalCoeffsToSlots(const std::vector<std::vector
first += cc->KeySwitchDownFirstElement(inner);
auto elements = inner->GetElements();
elements[0].SetValuesToZero();
inner->SetElements(elements);
inner->SetElements(std::move(elements));
EvalAddExtInPlace(outer, inner);
}
}
Expand Down Expand Up @@ -1969,7 +1969,7 @@ Ciphertext<DCRTPoly> FHECKKSRNS::EvalSlotsToCoeffs(const std::vector<std::vector
first = cc->KeySwitchDownFirstElement(inner);
auto elements = inner->GetElements();
elements[0].SetValuesToZero();
inner->SetElements(elements);
inner->SetElements(std::move(elements));
outer = inner;
}
else {
Expand All @@ -1987,7 +1987,7 @@ Ciphertext<DCRTPoly> FHECKKSRNS::EvalSlotsToCoeffs(const std::vector<std::vector
first += cc->KeySwitchDownFirstElement(inner);
auto elements = inner->GetElements();
elements[0].SetValuesToZero();
inner->SetElements(elements);
inner->SetElements(std::move(elements));
EvalAddExtInPlace(outer, inner);
}
}
Expand Down Expand Up @@ -2032,7 +2032,7 @@ Ciphertext<DCRTPoly> FHECKKSRNS::EvalSlotsToCoeffs(const std::vector<std::vector
first = cc->KeySwitchDownFirstElement(inner);
auto elements = inner->GetElements();
elements[0].SetValuesToZero();
inner->SetElements(elements);
inner->SetElements(std::move(elements));
outer = inner;
}
else {
Expand All @@ -2050,7 +2050,7 @@ Ciphertext<DCRTPoly> FHECKKSRNS::EvalSlotsToCoeffs(const std::vector<std::vector
first += cc->KeySwitchDownFirstElement(inner);
auto elements = inner->GetElements();
elements[0].SetValuesToZero();
inner->SetElements(elements);
inner->SetElements(std::move(elements));
EvalAddExtInPlace(outer, inner);
}
}
Expand Down Expand Up @@ -2503,7 +2503,7 @@ EvalKey<DCRTPoly> FHECKKSRNS::ConjugateKeyGen(const PrivateKey<DCRTPoly> private

DCRTPoly sPermuted = s.AutomorphismTransform(index, vec);

privateKeyPermuted->SetPrivateElement(sPermuted);
privateKeyPermuted->SetPrivateElement(std::move(sPermuted));
privateKeyPermuted->SetKeyTag(privateKey->GetKeyTag());

return algo->KeySwitchGen(privateKey, privateKeyPermuted);
Expand Down
9 changes: 5 additions & 4 deletions src/pke/lib/scheme/ckksrns/ckksrns-leveledshe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,13 @@ Ciphertext<DCRTPoly> LeveledSHECKKSRNS::EvalFastRotationExt(ConstCiphertext<DCRT
Ciphertext<DCRTPoly> LeveledSHECKKSRNS::MultByInteger(ConstCiphertext<DCRTPoly> ciphertext, uint64_t integer) const {
const std::vector<DCRTPoly>& cv = ciphertext->GetElements();

std::vector<DCRTPoly> resultDCRT(cv.size());
for (usint i = 0; i < cv.size(); i++)
resultDCRT[i] = cv[i].Times(NativeInteger(integer));
std::vector<DCRTPoly> resultDCRT;
resultDCRT.reserve(cv.size());
for (const auto& elem : cv)
resultDCRT.push_back(elem.Times(NativeInteger(integer)));

Ciphertext<DCRTPoly> result = ciphertext->CloneZero();
result->SetElements(resultDCRT);
result->SetElements(std::move(resultDCRT));
return result;
}

Expand Down
20 changes: 10 additions & 10 deletions src/pke/lib/scheme/ckksrns/ckksrns-schemeswitching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ EvalKey<DCRTPoly> SWITCHCKKSRNS::ConjugateKeyGen(const PrivateKey<DCRTPoly> priv

DCRTPoly sPermuted = s.AutomorphismTransform(index, vec);

privateKeyPermuted->SetPrivateElement(sPermuted);
privateKeyPermuted->SetPrivateElement(std::move(sPermuted));
privateKeyPermuted->SetKeyTag(privateKey->GetKeyTag());

return algo->KeySwitchGen(privateKey, privateKeyPermuted);
Expand Down Expand Up @@ -615,7 +615,7 @@ EvalKey<DCRTPoly> switchingKeyGenRLWE(
skelementsPlain[j] = skelementsPlain.GetModulus() - 1;
}
}
skelements.SetElementAtIndex(i, skelementsPlain);
skelements.SetElementAtIndex(i, std::move(skelementsPlain));
}

skelements.SetFormat(Format::EVALUATION);
Expand Down Expand Up @@ -648,7 +648,7 @@ void ModSwitch(ConstCiphertext<DCRTPoly> ctxt, Ciphertext<DCRTPoly>& ctxtKS, Nat
ref.SetFormat(Format::EVALUATION);
}

ctxtKS->SetElements(resultElements);
ctxtKS->SetElements(std::move(resultElements));
}

EvalKey<DCRTPoly> switchingKeyGen(const PrivateKey<DCRTPoly>& ckksSKto, const PrivateKey<DCRTPoly>& ckksSKfrom) {
Expand All @@ -670,7 +670,7 @@ EvalKey<DCRTPoly> switchingKeyGen(const PrivateKey<DCRTPoly>& ckksSKto, const Pr
else
skElementsPlain[j] = skElementsPlain.GetModulus() - 1;
}
skElements.SetElementAtIndex(i, skElementsPlain);
skElements.SetElementAtIndex(i, std::move(skElementsPlain));
}

skElements.SetFormat(Format::EVALUATION);
Expand Down Expand Up @@ -720,8 +720,8 @@ EvalKey<DCRTPoly> switchingKeyGenRLWEcc(const PrivateKey<DCRTPoly>& ckksSKto, co
skElementsPlainLWE[j] = skElementsPlain.GetModulus() - 1;
}
}
skElements.SetElementAtIndex(i, skElementsPlain);
skElements2.SetElementAtIndex(i, skElementsPlainLWE);
skElements.SetElementAtIndex(i, std::move(skElementsPlain));
skElements2.SetElementAtIndex(i, std::move(skElementsPlainLWE));
}

skElements.SetFormat(Format::EVALUATION);
Expand Down Expand Up @@ -998,7 +998,7 @@ Ciphertext<DCRTPoly> SWITCHCKKSRNS::EvalLTWithPrecomputeSwitch(const CryptoConte
first = cc.KeySwitchDownFirstElement(inner);
auto elements = inner->GetElements();
elements[0].SetValuesToZero();
inner->SetElements(elements);
inner->SetElements(std::move(elements));
result = inner;
}
else {
Expand All @@ -1018,7 +1018,7 @@ Ciphertext<DCRTPoly> SWITCHCKKSRNS::EvalLTWithPrecomputeSwitch(const CryptoConte
result = cc.KeySwitchDown(result);
auto elements = result->GetElements();
elements[0] += first;
result->SetElements(elements);
result->SetElements(std::move(elements));

return result;
}
Expand Down Expand Up @@ -1104,7 +1104,7 @@ Ciphertext<DCRTPoly> SWITCHCKKSRNS::EvalLTRectWithPrecomputeSwitch(
first = cc.KeySwitchDownFirstElement(inner);
auto elements = inner->GetElements();
elements[0].SetValuesToZero();
inner->SetElements(elements);
inner->SetElements(std::move(elements));
result = inner;
}
else {
Expand All @@ -1123,7 +1123,7 @@ Ciphertext<DCRTPoly> SWITCHCKKSRNS::EvalLTRectWithPrecomputeSwitch(
result = cc.KeySwitchDown(result);
auto elements = result->GetElements();
elements[0] += first;
result->SetElements(elements);
result->SetElements(std::move(elements));

// A represents the diagonals, which lose the information whether the initial matrix is tall or wide
if (wide) {
Expand Down
7 changes: 3 additions & 4 deletions src/pke/lib/schemebase/base-multiparty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ std::shared_ptr<std::map<usint, EvalKey<Element>>> MultipartyBase<Element>::Mult
PrecomputeAutoMap(N, index, &vec);

Element sPermuted = s.AutomorphismTransform(index, vec);
privateKeyPermuted->SetPrivateElement(sPermuted);
privateKeyPermuted->SetPrivateElement(std::move(sPermuted));

// verify if the key indexList[i] exists in the evalKeyMap
auto evalKeyIterator = evalKeyMap->find(indexList[i]);
Expand All @@ -185,9 +185,8 @@ std::shared_ptr<std::map<usint, EvalKey<Element>>> MultipartyBase<Element>::Mult
std::vector<uint32_t> autoIndices(indexList.size());

for (size_t i = 0; i < indexList.size(); i++) {
autoIndices[i] = (isCKKS(cc->getSchemeId())) ?
FindAutomorphismIndex2nComplex(indexList[i], M) :
FindAutomorphismIndex2n(indexList[i], M);
autoIndices[i] = (isCKKS(cc->getSchemeId())) ? FindAutomorphismIndex2nComplex(indexList[i], M) :
FindAutomorphismIndex2n(indexList[i], M);
}

return MultiEvalAutomorphismKeyGen(privateKey, evalKeyMap, autoIndices);
Expand Down
11 changes: 3 additions & 8 deletions src/pke/lib/schemerns/rns-leveledshe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,22 +478,17 @@ void LeveledSHERNS::AdjustForAddOrSubInPlace(Ciphertext<DCRTPoly>& ciphertext1,
crtPowSF = CKKSPackedEncoding::CRTMult(crtPowSF, crtSF, moduli);
}

ptxt = ptxt.Times(crtPowSF);

if (ptxtIndex == 1) {
ciphertext1->SetElements({ptxt});
ciphertext1->SetElements(std::vector<DCRTPoly>{ptxt.Times(crtPowSF)});
ciphertext1->SetNoiseScaleDeg(ctxtDepth);
}
else {
ciphertext2->SetElements({ptxt});
ciphertext2->SetElements(std::vector<DCRTPoly>{ptxt.Times(crtPowSF)});
ciphertext2->SetNoiseScaleDeg(ctxtDepth);
}
}
else if (ptxtDepth > ctxtDepth) {
OPENFHE_THROW(
"LPAlgorithmSHERNS<DCRTPoly>::AdjustForAddOrSubInPlace "
"- plaintext cannot be encoded at a larger depth than that "
"of the ciphertext.");
OPENFHE_THROW("plaintext cannot be encoded at a larger depth than that of the ciphertext.");
}
}
else if (cryptoParams->GetScalingTechnique() != NORESCALE) {
Expand Down

0 comments on commit b68fcef

Please sign in to comment.