Skip to content

Commit

Permalink
fixed a bug with parameter selection (#893)
Browse files Browse the repository at this point in the history
* fixed a bug with parameter selection

* Addressed review comments

---------

Co-authored-by: Yuriy Polyakov <ypolyakod@dualitytech.com>
Co-authored-by: Dmitriy Suponitskiy <dsuponitskiy@dualitytech.com>
  • Loading branch information
3 people authored Oct 28, 2024
1 parent f2012fe commit 2592fc9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/pke/lib/scheme/bgvrns/bgvrns-parametergeneration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,9 @@ bool ParameterGenerationBGVRNS::ParamsGenBGVRNS(std::shared_ptr<CryptoParameters
qBound += cryptoParamsBGVRNS->EstimateMultipartyFloodingLogQ();

// we add an extra bit to account for the special logic of selecting the RNS moduli in BGV
qBound++;
// ignore the case when there is only one max size modulus
if (qBound != auxBits)
qBound++;

uint32_t auxTowers = 0;
if (ksTech == HYBRID) {
Expand Down Expand Up @@ -482,7 +484,7 @@ bool ParameterGenerationBGVRNS::ParamsGenBGVRNS(std::shared_ptr<CryptoParameters
numPartQ, std::log2(moduliQ[0].ConvertToDouble()),
(moduliQ.size() > 1) ? std::log2(moduliQ[1].ConvertToDouble()) : 0,
(scalTech == FLEXIBLEAUTOEXT) ? std::log2(moduliQ[moduliQ.size() - 1].ConvertToDouble()) : 0,
(scalTech == FLEXIBLEAUTOEXT) ? moduliQ.size() - 1 : moduliQ.size(), auxBits, true);
(scalTech == FLEXIBLEAUTOEXT) ? moduliQ.size() - 1 : moduliQ.size(), auxBits, false);
newQBound += std::get<0>(hybridKSInfo);
}
} while (qBound < newQBound);
Expand Down
4 changes: 3 additions & 1 deletion src/pke/lib/scheme/ckksrns/ckksrns-parametergeneration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ bool ParameterGenerationCKKSRNS::ParamsGenCKKSRNS(std::shared_ptr<CryptoParamete
uint32_t qBound = firstModSize + (numPrimes - 1) * scalingModSize + extraModSize;

// we add an extra bit to account for the alternating logic of selecting the RNS moduli in CKKS
qBound++;
// ignore the case when there is only one max size modulus
if (qBound != auxBits)
qBound++;

// Estimate ciphertext modulus Q*P bound (in case of HYBRID P*Q)
if (ksTech == HYBRID) {
Expand Down
11 changes: 6 additions & 5 deletions src/pke/lib/schemerns/rns-cryptoparameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,23 +415,24 @@ std::pair<double, uint32_t> CryptoParametersRNS::EstimateLogP(uint32_t numPartQ,
qi[sizeQ - 1] = extraModulusSize;

// Compute partitions of Q into numPartQ digits
double maxBits = 0;
uint32_t maxBits = 0;
for (size_t j = 0; j < numPartQ; ++j) {
size_t startTower = j * numPerPartQ;
size_t endTower = ((j + 1) * numPerPartQ - 1 < sizeQ) ? (j + 1) * numPerPartQ - 1 : sizeQ - 1;

// sum qi elements qi[startTower] + ... + qi[endTower] inclusive. the end element should be qi.begin()+(endTower+1)
double bits = std::accumulate(qi.begin() + startTower, qi.begin() + (endTower + 1), 0.0);
uint32_t bits = static_cast<uint32_t>(std::accumulate(qi.begin() + startTower, qi.begin() + (endTower + 1), 0.0));
if (bits > maxBits)
maxBits = bits;
}

// we add an extra bit to account for for the special moduli selection logic in BGV and CKKS
if (addOne)
// we add an extra bit to account for the special moduli selection logic in BGV and CKKS
// ignore the case when there is only one max size modulus
if (addOne && (maxBits != auxBits))
maxBits++;

// Select number of primes in auxiliary CRT basis
auto sizeP = static_cast<uint32_t>(std::ceil(maxBits / auxBits));
auto sizeP = static_cast<uint32_t>(std::ceil(static_cast<double>(maxBits) / auxBits));

return std::make_pair(sizeP * auxBits, sizeP);
}
Expand Down

0 comments on commit 2592fc9

Please sign in to comment.