Skip to content

Commit

Permalink
Fixed GetBootstrapDepth (openfheorg#626)
Browse files Browse the repository at this point in the history
* updated GetBootstrapDepth

* Update simple-ckks-bootstrapping.cpp

---------

Co-authored-by: Yuriy Polyakov <ypolyakod@dualitytech.com>
  • Loading branch information
yspolyakov and Yuriy Polyakov committed Dec 22, 2023
1 parent ec7a78d commit fea4dec
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/pke/examples/iterative-ckks-bootstrapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ void IterativeBootstrapExample() {

// Output the precision of bootstrapping after two iterations. It should be approximately double the original precision.
std::cout << "Bootstrapping precision after 2 iterations: " << precisionMultipleIterations << std::endl;
std::cout << "Number of levels remaining after 2 bootstrappings: " << depth - ciphertextTwoIterations->GetLevel()
std::cout << "Number of levels remaining after 2 bootstrappings: "
<< depth - ciphertextTwoIterations->GetLevel() - (ciphertextTwoIterations->GetNoiseScaleDeg() - 1)
<< std::endl;
}
6 changes: 5 additions & 1 deletion src/pke/examples/simple-ckks-bootstrapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ void SimpleBootstrapExample() {
*/
std::vector<uint32_t> levelBudget = {4, 4};

// Note that the actual number of levels avalailable after bootstrapping before next bootstrapping
// will be levelsAvailableAfterBootstrap - 1 because an additional level
// is used for scaling the ciphertext before next bootstrapping (in 64-bit CKKS bootstrapping)
uint32_t levelsAvailableAfterBootstrap = 10;
usint depth = levelsAvailableAfterBootstrap + FHECKKSRNS::GetBootstrapDepth(levelBudget, secretKeyDist);
parameters.SetMultiplicativeDepth(depth);
Expand Down Expand Up @@ -139,7 +142,8 @@ void SimpleBootstrapExample() {
// for HE computation.
auto ciphertextAfter = cryptoContext->EvalBootstrap(ciph);

std::cout << "Number of levels remaining after bootstrapping: " << depth - ciphertextAfter->GetLevel() << std::endl
std::cout << "Number of levels remaining after bootstrapping: "
<< depth - ciphertextAfter->GetLevel() - (ciphertextAfter->GetNoiseScaleDeg() - 1) << std::endl
<< std::endl;

Plaintext result;
Expand Down
22 changes: 11 additions & 11 deletions src/pke/lib/scheme/ckksrns/ckksrns-fhe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void FHECKKSRNS::EvalBootstrapSetup(const CryptoContextImpl<DCRTPoly>& cc, std::
double scaleDec = 1 / pre;

uint32_t approxModDepth = GetModDepthInternal(cryptoParams->GetSecretKeyDist());
uint32_t depthBT = approxModDepth + 1 + precom->m_paramsEnc[CKKS_BOOT_PARAMS::LEVEL_BUDGET] +
uint32_t depthBT = approxModDepth + precom->m_paramsEnc[CKKS_BOOT_PARAMS::LEVEL_BUDGET] +
precom->m_paramsDec[CKKS_BOOT_PARAMS::LEVEL_BUDGET];

// compute # of levels to remain when encoding the coefficients
Expand Down Expand Up @@ -295,7 +295,7 @@ void FHECKKSRNS::EvalBootstrapPrecompute(const CryptoContextImpl<DCRTPoly>& cc,
double scaleDec = 1 / pre;

uint32_t approxModDepth = GetModDepthInternal(cryptoParams->GetSecretKeyDist());
uint32_t depthBT = approxModDepth + 1 + precom->m_paramsEnc[CKKS_BOOT_PARAMS::LEVEL_BUDGET] +
uint32_t depthBT = approxModDepth + precom->m_paramsEnc[CKKS_BOOT_PARAMS::LEVEL_BUDGET] +
precom->m_paramsDec[CKKS_BOOT_PARAMS::LEVEL_BUDGET];

// compute # of levels to remain when encoding the coefficients
Expand Down Expand Up @@ -2107,10 +2107,10 @@ uint32_t FHECKKSRNS::GetBootstrapDepthInternal(uint32_t approxModDepth, const st

uint32_t FHECKKSRNS::GetModDepthInternal(SecretKeyDist secretKeyDist) {
if (secretKeyDist == UNIFORM_TERNARY) {
return GetMultiplicativeDepthByCoeffVector(g_coefficientsUniform, true) + R_UNIFORM;
return GetMultiplicativeDepthByCoeffVector(g_coefficientsUniform, false) + R_UNIFORM;
}
else {
return GetMultiplicativeDepthByCoeffVector(g_coefficientsSparse, true) + R_SPARSE;
return GetMultiplicativeDepthByCoeffVector(g_coefficientsSparse, false) + R_SPARSE;
}
}

Expand Down Expand Up @@ -2355,8 +2355,8 @@ Plaintext FHECKKSRNS::MakeAuxPlaintext(const CryptoContextImpl<DCRTPoly>& cc, co
if (logc < 0) {
OPENFHE_THROW(math_error, "Too small scaling factor");
}
int32_t logValid = (logc <= MAX_BITS_IN_WORD) ? logc : MAX_BITS_IN_WORD;
int32_t logApprox = logc - logValid;
int32_t logValid = (logc <= MAX_BITS_IN_WORD) ? logc : MAX_BITS_IN_WORD;
int32_t logApprox = logc - logValid;
double approxFactor = pow(2, logApprox);

std::vector<int64_t> temp(2 * slots);
Expand Down Expand Up @@ -2387,11 +2387,11 @@ Plaintext FHECKKSRNS::MakeAuxPlaintext(const CryptoContextImpl<DCRTPoly>& cc, co
double imagVal = prodFactor.imag();

if (realVal > realMax) {
realMax = realVal;
realMax = realVal;
realMaxIdx = idx;
}
if (imagVal > imagMax) {
imagMax = imagVal;
imagMax = imagVal;
imagMaxIdx = idx;
}
}
Expand All @@ -2414,11 +2414,11 @@ Plaintext FHECKKSRNS::MakeAuxPlaintext(const CryptoContextImpl<DCRTPoly>& cc, co
int64_t re = std::llround(dre);
int64_t im = std::llround(dim);

temp[i] = (re < 0) ? Max64BitValue() + re : re;
temp[i] = (re < 0) ? Max64BitValue() + re : re;
temp[i + slots] = (im < 0) ? Max64BitValue() + im : im;
}

const std::shared_ptr<ILDCRTParams<BigInteger>> bigParams = plainElement.GetParams();
const std::shared_ptr<ILDCRTParams<BigInteger>> bigParams = plainElement.GetParams();
const std::vector<std::shared_ptr<ILNativeParams>>& nativeParams = bigParams->GetParams();

for (size_t i = 0; i < nativeParams.size(); i++) {
Expand Down Expand Up @@ -2454,7 +2454,7 @@ Plaintext FHECKKSRNS::MakeAuxPlaintext(const CryptoContextImpl<DCRTPoly>& cc, co
// Scale back up by the approxFactor to get the correct encoding.
if (logApprox > 0) {
int32_t logStep = (logApprox <= MAX_LOG_STEP) ? logApprox : MAX_LOG_STEP;
auto intStep = DCRTPoly::Integer(uint64_t(1) << logStep);
auto intStep = DCRTPoly::Integer(uint64_t(1) << logStep);
std::vector<DCRTPoly::Integer> crtApprox(numTowers, intStep);
logApprox -= logStep;

Expand Down

0 comments on commit fea4dec

Please sign in to comment.