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

Fixed GetBootstrapDepth #626

Merged
merged 2 commits into from
Dec 22, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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