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

Composite Prime Moduli Sampling and Scaling Factor Calculations (#910 phase 2) #929

Merged
merged 27 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3ed5909
Initial commit: adding composite scaling parameters and scaling tech …
fdiasmor Dec 12, 2024
fb50d79
Initial commit: adding composite scaling parameters and scaling tech …
fdiasmor Dec 12, 2024
d3c1a58
Composite prime generation and adaptive scaling factors.
fdiasmor Dec 16, 2024
e397a73
Propagating composite degree to mod reduce calls.
fdiasmor Dec 17, 2024
3bf393a
Handling overflow for scaling factors over 64 bits on composite scali…
fdiasmor Dec 17, 2024
51db3ca
Bug fix of overflow handling for d > 2 and sc > 64.
fdiasmor Dec 18, 2024
44b206b
Update impl of Rescale and Mod/LevelReduce methods with composite deg…
fdiasmor Dec 18, 2024
3d2c220
Minor bug fix: else case missing.
fdiasmor Dec 19, 2024
a9c9978
Update EvalPoly functions to use composite degree.
fdiasmor Dec 19, 2024
c03c8f9
Update Compress function to raise error.
fdiasmor Dec 19, 2024
470cd11
Update/Add new composite scaling examples.
fdiasmor Dec 19, 2024
6369eaa
Recalculate sizeP and update EstimateLogP interface.
fdiasmor Dec 19, 2024
5d54a9f
Update composite-prime generation function.
fdiasmor Dec 31, 2024
a5a2b9c
Remove comments from prime gen function.
fdiasmor Jan 3, 2025
9a7ad26
Update composite scaling unittests and fix unittest runtime issue.
fdiasmor Dec 25, 2024
c8c76a8
Some review changes.
fdiasmor Jan 8, 2025
4b721d9
Update composite scaling unittests and fix unittest runtime issue.
fdiasmor Dec 25, 2024
cf685de
Disable composite scaling set methods for non-CKKS schemes.
fdiasmor Jan 9, 2025
a5a443a
Move COMPOSITESCALING support error handling.
fdiasmor Jan 10, 2025
b14472d
Disable composite scaling set methods for non-CKKS schemes.
fdiasmor Jan 9, 2025
6dd35c0
Removing additional constructor that accepts composite scaling parame…
fdiasmor Jan 10, 2025
e9cdffa
Merge/rebase review changes from (#928).
fdiasmor Jan 18, 2025
80fe03f
Merge branch 'dev' into 910-composite-prime-gen
fdiasmor Jan 18, 2025
c63d0fe
Review changes for PR 910 phase 2.
fdiasmor Feb 5, 2025
2ec7adf
Review updates and fixes.
fdiasmor Feb 6, 2025
abd4585
Review updates and fixes.
fdiasmor Feb 6, 2025
d3208fa
Review updates and fixes.
fdiasmor Feb 6, 2025
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
28 changes: 14 additions & 14 deletions src/pke/include/cryptocontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,20 @@ class CryptoContextImpl : public Serializable {
return p;
}

/**
* GetCompositeDegree: get composite degree of the current scheme crypto context.
* @return integer value corresponding to composite degree
*/
uint32_t GetCompositeDegreeFromCtxt() const {
const auto cryptoParams = std::dynamic_pointer_cast<CryptoParametersRNS>(params);
if (!cryptoParams) {
std::string errorMsg(std::string("std::dynamic_pointer_cast<CryptoParametersRNS>() failed"));
OPENFHE_THROW(errorMsg);
}

return cryptoParams->GetCompositeDegree();
}

PrivateKey<Element> privateKey;

public:
Expand Down Expand Up @@ -1052,20 +1066,6 @@ class CryptoContextImpl : public Serializable {
return params->GetElementParams()->GetRootOfUnity();
}

/**
* GetCompositeDegree: get composite degree of the current scheme crypto context.
* @return integer value corresponding to composite degree
*/
uint32_t GetCompositeDegreeFromCtxt() const {
const auto cryptoParams = std::dynamic_pointer_cast<CryptoParametersRNS>(params);
if (!cryptoParams) {
std::string errorMsg(std::string("std::dynamic_pointer_cast<CryptoParametersRNS>() failed"));
OPENFHE_THROW(errorMsg);
}

return cryptoParams->GetCompositeDegree();
}

//------------------------------------------------------------------------------
// KEYS GETTERS
//------------------------------------------------------------------------------
Expand Down
31 changes: 22 additions & 9 deletions src/pke/lib/scheme/ckksrns/ckksrns-advancedshe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,37 @@ namespace lbcrypto {

Ciphertext<DCRTPoly> AdvancedSHECKKSRNS::EvalMultMany(const std::vector<Ciphertext<DCRTPoly>>& ciphertextVec,
const std::vector<EvalKey<DCRTPoly>>& evalKeys) const {
if (ciphertextVec.size() < 1)
const size_t inSize = ciphertextVec.size();

if (inSize < 2)
OPENFHE_THROW("Input ciphertext vector size should be 1 or more");

const size_t inSize = ciphertextVec.size();
const size_t lim = inSize * 2 - 2;
if (inSize == 1)
return ciphertextVec[0];

const size_t lim = inSize * 2 - 2;
std::vector<Ciphertext<DCRTPoly>> ciphertextMultVec;
ciphertextMultVec.resize(inSize - 1);
size_t ctrIndex = 0;

auto algo = ciphertextVec[0]->GetCryptoContext()->GetScheme();
const auto cc = ciphertextVec[0]->GetCryptoContext();
const auto cryptoParams = std::dynamic_pointer_cast<CryptoParametersRNS>(ciphertextVec[0]->GetCryptoParameters());
uint32_t levelsToDrop = cryptoParams->GetCompositeDegree();

for (size_t i = 0; i < lim; i = i + 2) {
ciphertextMultVec[ctrIndex] = algo->EvalMultAndRelinearize(
i < inSize ? ciphertextVec[i] : ciphertextMultVec[i - inSize],
i + 1 < inSize ? ciphertextVec[i + 1] : ciphertextMultVec[i + 1 - inSize], evalKeys);
size_t ctrIndex = 0;
size_t i = 0;
for (; i < (inSize - 1); i += 2) {
ciphertextMultVec[ctrIndex] = algo->EvalMultAndRelinearize(ciphertextVec[i], ciphertextVec[(i + 1)], evalKeys);
algo->ModReduceInPlace(ciphertextMultVec[ctrIndex++], levelsToDrop);
}
if (i < inSize) {
ciphertextMultVec[ctrIndex] =
algo->EvalMultAndRelinearize(ciphertextVec[i], ciphertextMultVec[i + 1 - inSize], evalKeys);
algo->ModReduceInPlace(ciphertextMultVec[ctrIndex++], levelsToDrop);
i += 2;
}
for (; i < lim; i += 2) {
ciphertextMultVec[ctrIndex] =
algo->EvalMultAndRelinearize(ciphertextMultVec[i - inSize], ciphertextMultVec[i + 1 - inSize], evalKeys);
algo->ModReduceInPlace(ciphertextMultVec[ctrIndex++], levelsToDrop);
}

Expand Down
5 changes: 4 additions & 1 deletion src/pke/lib/schemebase/base-advancedshe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ template <class Element>
Ciphertext<Element> AdvancedSHEBase<Element>::EvalAddMany(const std::vector<Ciphertext<Element>>& ciphertextVec) const {
const size_t inSize = ciphertextVec.size();

if (ciphertextVec.size() < 1)
if (ciphertextVec.size() < 2)
OPENFHE_THROW("Input ciphertext vector size should be 1 or more");

if (ciphertextVec.size() == 1)
return ciphertextVec[0];

const size_t lim = inSize * 2 - 2;
std::vector<Ciphertext<Element>> ciphertextSumVec;
ciphertextSumVec.resize(inSize - 1);
Expand Down
4 changes: 1 addition & 3 deletions src/pke/lib/schemerns/rns-leveledshe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,16 +540,14 @@ void LeveledSHERNS::AdjustForMultInPlace(Ciphertext<DCRTPoly>& ciphertext1, Ciph
Ciphertext<DCRTPoly> LeveledSHERNS::ComposedEvalMult(ConstCiphertext<DCRTPoly> ciphertext1,
ConstCiphertext<DCRTPoly> ciphertext2,
const EvalKey<DCRTPoly> evalKey) const {
const auto cc = ciphertext1->GetCryptoContext();
auto algo = ciphertext1->GetCryptoContext()->GetScheme();
const auto cryptoParams = std::dynamic_pointer_cast<CryptoParametersRNS>(ciphertext1->GetCryptoParameters());
Ciphertext<DCRTPoly> ciphertext = EvalMult(ciphertext1, ciphertext2);
algo->KeySwitchInPlace(ciphertext, evalKey);
uint32_t levelsToDrop = BASE_NUM_LEVELS_TO_DROP;
if (cryptoParams->GetScalingTechnique() == COMPOSITESCALINGAUTO ||
cryptoParams->GetScalingTechnique() == COMPOSITESCALINGMANUAL) {
const auto cryptoRNSParams = std::dynamic_pointer_cast<CryptoParametersRNS>(cryptoParams);
levelsToDrop = cryptoRNSParams->GetCompositeDegree();
levelsToDrop = cryptoParams->GetCompositeDegree();
}
ModReduceInPlace(ciphertext, levelsToDrop);
return ciphertext;
Expand Down