Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/pke/include/ciphertext.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ class CiphertextImpl : public CryptoObject<Element> {
/**
* Set the number of scalings
*/
// Generic case: no multiplicativeDepth validation. SetLevel() has a specialization for DCRTPoly
void SetLevel(size_t level) {
m_level = level;
}
Expand Down Expand Up @@ -523,6 +524,9 @@ class CiphertextImpl : public CryptoObject<Element> {
MetadataMap m_metadataMap{std::make_shared<std::map<std::string, std::shared_ptr<Metadata>>>()};
};

template <>
void CiphertextImpl<DCRTPoly>::SetLevel(size_t level);

/**
* operator+ overload for Ciphertexts. Performs EvalAdd.
*
Expand Down
1 change: 1 addition & 0 deletions src/pke/include/cryptocontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -1980,6 +1980,7 @@ class CryptoContextImpl : public Serializable {
Ciphertext<Element> EvalMultNoRelin(ConstCiphertext<Element>& ciphertext1,
ConstCiphertext<Element>& ciphertext2) const {
TypeCheck(ciphertext1, ciphertext2);

return GetScheme()->EvalMult(ciphertext1, ciphertext2);
}

Expand Down
20 changes: 20 additions & 0 deletions src/pke/lib/ciphertext-impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,29 @@
//==================================================================================

#include "ciphertext.h"
#include "cryptocontext.h"
#include "scheme/ckksrns/ckksrns-cryptoparameters.h"

namespace lbcrypto {

template <>
void CiphertextImpl<DCRTPoly>::SetLevel(size_t level) {
m_level = level;

// check if the multiplication depth value is sufficient in SetLevel() as it always gets called
const auto cryptoParams = std::dynamic_pointer_cast<CryptoParametersCKKSRNS>(
CryptoObject<DCRTPoly>::GetCryptoContext()->GetCryptoParameters());
// the multDepth check applies only to CKKS, when cryptoParams is of type CryptoParametersCKKSRNS.
if (cryptoParams) {
uint32_t limbNum = m_elements[0].GetNumOfElements();
if (limbNum < GetNoiseScaleDeg()) {
uint32_t multDepth = cryptoParams->GetMultiplicativeDepth();
OPENFHE_THROW("The current multiplicative depth [" + std::to_string(multDepth) +
"] is insufficient; increase it.");
}
}
}

template class CiphertextImpl<Poly>;
template class CiphertextImpl<NativePoly>;
template class CiphertextImpl<DCRTPoly>;
Expand Down
16 changes: 9 additions & 7 deletions src/pke/lib/scheme/ckksrns/ckksrns-cryptoparameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void CryptoParametersCKKSRNS::PrecomputeCRTTables(KeySwitchTechnique ksTech, Sca
m_scalingFactorsReal[0] *= moduliQ[sizeQ - j - 1].ConvertToDouble();
}
}
if (extraBits > 0 && m_scalTechnique != COMPOSITESCALINGAUTO && m_scalTechnique != COMPOSITESCALINGMANUAL)
else if (extraBits > 0)
m_scalingFactorsReal[1] = moduliQ[sizeQ - 2].ConvertToDouble();

const double lastPresetFactor = (extraBits == 0) ? m_scalingFactorsReal[0] : m_scalingFactorsReal[1];
Expand All @@ -137,13 +137,15 @@ void CryptoParametersCKKSRNS::PrecomputeCRTTables(KeySwitchTechnique ksTech, Sca
else {
double prevSF = m_scalingFactorsReal[k - 1];
m_scalingFactorsReal[k] = prevSF * prevSF / moduliQ[sizeQ - k].ConvertToDouble();
}

if (m_scalTechnique == FLEXIBLEAUTO || m_scalTechnique == FLEXIBLEAUTOEXT) {
double ratio = m_scalingFactorsReal[k] / lastPresetFactor;
if (ratio <= 0.5 || ratio >= 2.0)
OPENFHE_THROW(
"FLEXIBLEAUTO cannot support this number of levels in this parameter setting. Please use FIXEDMANUAL or FIXEDAUTO instead.");
if (m_scalTechnique == FLEXIBLEAUTO || m_scalTechnique == FLEXIBLEAUTOEXT) {
double ratio = m_scalingFactorsReal[k] / lastPresetFactor;
if (ratio <= 0.5 || ratio >= 2.0) {
OPENFHE_THROW("FLEXIBLEAUTO scaling failed at level " + std::to_string(k) +
" with scaling factor ratio " + std::to_string(ratio) +
". Use FIXEDMANUAL or FIXEDAUTO instead.");
}
}
}
}
}
Expand Down
15 changes: 11 additions & 4 deletions src/pke/lib/scheme/ckksrns/ckksrns-schemeswitching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@

#include <iterator>
#include <limits>
#include <string>
#include <utility>
#include <map>
#include <algorithm>
#include <memory>
#include <vector>

// K = 16
static constexpr std::initializer_list<double> g_coefficientsFHEW16{
Expand Down Expand Up @@ -251,7 +257,7 @@ Plaintext SWITCHCKKSRNS::MakeAuxPlaintext(const CryptoContextImpl<DCRTPoly>& cc,
im = im64 >> (-pRemaining);
}
else {
__int128 pPowRemaining = ((int64_t)1) << pRemaining;
__int128 pPowRemaining = (static_cast<int64_t>(1)) << pRemaining;
im = pPowRemaining * im64;
}

Expand Down Expand Up @@ -446,13 +452,13 @@ Plaintext SWITCHCKKSRNS::MakeAuxPlaintext(const CryptoContextImpl<DCRTPoly>& cc,
// 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(static_cast<uint64_t>(1) << logStep);
std::vector<DCRTPoly::Integer> crtApprox(numTowers, intStep);
logApprox -= logStep;

while (logApprox > 0) {
logStep = (logApprox <= MAX_LOG_STEP) ? logApprox : MAX_LOG_STEP;
intStep = DCRTPoly::Integer(uint64_t(1) << logStep);
intStep = DCRTPoly::Integer(static_cast<uint64_t>(1) << logStep);
std::vector<DCRTPoly::Integer> crtSF(numTowers, intStep);
crtApprox = CKKSPackedEncoding::CRTMult(crtApprox, crtSF, moduli);
logApprox -= logStep;
Expand Down Expand Up @@ -1241,7 +1247,8 @@ LWEPrivateKey SWITCHCKKSRNS::EvalCKKStoFHEWSetup(const SchSwchParams& params) {
CCParams<CryptoContextCKKSRNS> parameters;
parameters.SetMultiplicativeDepth(0);
parameters.SetFirstModSize(params.GetCtxtModSizeFHEWIntermedSwch());
parameters.SetScalingModSize(params.GetScalingModSize());
// scaling mod size is not used in this case
parameters.SetScalingModSize(params.GetCtxtModSizeFHEWIntermedSwch());
// This doesn't need this to be the same scaling technique as the outer cryptocontext, since we only do a key switch
parameters.SetScalingTechnique(FIXEDMANUAL);
parameters.SetSecurityLevel(params.GetSecurityLevelCKKS());
Expand Down
21 changes: 13 additions & 8 deletions src/pke/lib/scheme/gen-cryptocontext-params-validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,17 @@ void validateParametersForCryptocontext(const Params& parameters) {
OPENFHE_THROW(
"NOISE_FLOODING_MULTIPARTY is not supported in CKKSRNS. Use NOISE_FLOODING_DECRYPT and EXEC_EVALUATION instead.");
}
if (MAX_MODULUS_SIZE <= parameters.GetScalingModSize() &&
COMPOSITESCALINGAUTO != parameters.GetScalingTechnique() &&
COMPOSITESCALINGMANUAL != parameters.GetScalingTechnique()) {
OPENFHE_THROW("scalingModSize should be less than " + std::to_string(MAX_MODULUS_SIZE));
if (COMPOSITESCALINGAUTO == parameters.GetScalingTechnique() || COMPOSITESCALINGMANUAL == parameters.GetScalingTechnique()) {
if (COMPOSITESCALING_MAX_MODULUS_SIZE <= parameters.GetScalingModSize() || 15 > parameters.GetScalingModSize()) {
OPENFHE_THROW(
"scalingModSize should be greater than 15 and less than " + std::to_string(COMPOSITESCALING_MAX_MODULUS_SIZE));
}
}
else if (COMPOSITESCALING_MAX_MODULUS_SIZE <= parameters.GetScalingModSize() &&
(COMPOSITESCALINGAUTO == parameters.GetScalingTechnique() ||
COMPOSITESCALINGMANUAL == parameters.GetScalingTechnique())) {
OPENFHE_THROW("scalingModSize should be less than " + std::to_string(COMPOSITESCALING_MAX_MODULUS_SIZE));
else {
if (MAX_MODULUS_SIZE <= parameters.GetScalingModSize() || 15 > parameters.GetScalingModSize()) {
OPENFHE_THROW(
"scalingModSize should be greater than 15 and less than " + std::to_string(MAX_MODULUS_SIZE));
}
}
if (30 != parameters.GetStatisticalSecurity()) {
if (NOISE_FLOODING_MULTIPARTY != parameters.GetMultipartyMode()) {
Expand All @@ -80,6 +82,9 @@ void validateParametersForCryptocontext(const Params& parameters) {
if (parameters.GetExecutionMode() == EXEC_NOISE_ESTIMATION && parameters.GetCKKSDataType() == COMPLEX) {
OPENFHE_THROW("EXEC_NOISE_ESTIMATION mode is not compatible with complex data types.");
}
if (parameters.GetFirstModSize() < parameters.GetScalingModSize()) {
OPENFHE_THROW("firstModSize cannot be less than scalingModSize");
}
}
else if (isBFVRNS(scheme)) {
if (0 == parameters.GetPlaintextModulus()) {
Expand Down