24 #ifndef SRC_PKE_CRYPTOCONTEXT_H_ 25 #define SRC_PKE_CRYPTOCONTEXT_H_ 34 #include "scheme/allscheme.h" 36 #include "cryptocontexthelper.h" 38 #include "utils/caller_info.h" 39 #include "utils/serial.h" 45 const size_t FIRSTMODSIZE = 105;
46 const enum RescalingTechnique DEFAULTRSTECH = APPROXAUTO;
48 const size_t FIRSTMODSIZE = 60;
49 const enum RescalingTechnique DEFAULTRSTECH = EXACTRESCALE;
52 template <
typename Element>
55 template <
typename Element>
58 template <
typename Element>
59 using CryptoContext = shared_ptr<CryptoContextImpl<Element>>;
77 template <
typename Element>
79 using IntType =
typename Element::Integer;
80 using ParmType =
typename Element::Params;
86 shared_ptr<LPCryptoParameters<Element>> params;
88 shared_ptr<LPPublicKeyEncryptionScheme<Element>> scheme;
90 static std::map<string, std::vector<LPEvalKey<Element>>>& evalMultKeyMap() {
92 static std::map<string, std::vector<LPEvalKey<Element>>> s_evalMultKeyMap;
93 return s_evalMultKeyMap;
96 static std::map<string, shared_ptr<std::map<usint, LPEvalKey<Element>>>>&
99 static std::map<string, shared_ptr<std::map<usint, LPEvalKey<Element>>>>
101 return s_evalSumKeyMap;
104 static std::map<string, shared_ptr<std::map<usint, LPEvalKey<Element>>>>&
105 evalAutomorphismKeyMap() {
107 static std::map<string, shared_ptr<std::map<usint, LPEvalKey<Element>>>>
108 s_evalAutomorphismKeyMap;
109 return s_evalAutomorphismKeyMap;
114 uint32_t m_keyGenLevel;
121 void TypeCheck(ConstCiphertext<Element> a, ConstCiphertext<Element> b,
122 CALLER_INFO_ARGS_HDR)
const {
123 if (a ==
nullptr || b ==
nullptr) {
124 std::string errorMsg(std::string(
"Null Ciphertext") + CALLER_INFO);
127 if (a->GetCryptoContext().get() !=
this) {
128 std::string errorMsg(
129 std::string(
"Ciphertext was not created in this CryptoContext") +
133 if (a->GetCryptoContext() != b->GetCryptoContext()) {
134 std::string errorMsg(
136 "Ciphertexts were not created in the same CryptoContext") +
140 if (a->GetKeyTag() != b->GetKeyTag()) {
141 std::string errorMsg(
142 std::string(
"Ciphertexts were not encrypted with same keys") +
146 if (a->GetEncodingType() != b->GetEncodingType()) {
147 std::stringstream ss;
148 ss <<
"Ciphertext encoding types " << a->GetEncodingType();
149 ss <<
" and " << b->GetEncodingType();
150 ss <<
" do not match";
207 void TypeCheck(ConstCiphertext<Element> a, ConstPlaintext b,
208 CALLER_INFO_ARGS_HDR)
const {
210 std::string errorMsg(std::string(
"Null Ciphertext") + CALLER_INFO);
214 std::string errorMsg(std::string(
"Null Plaintext") + CALLER_INFO);
217 if (a->GetCryptoContext().get() !=
this) {
218 std::string errorMsg(
219 std::string(
"Ciphertext was not created in this CryptoContext") +
223 if (a->GetEncodingType() != b->GetEncodingType()) {
224 std::stringstream ss;
225 ss <<
"Ciphertext encoding type " << a->GetEncodingType();
226 ss <<
" and Plaintext encoding type " << b->GetEncodingType();
227 ss <<
" do not match";
233 bool Mismatched(
const CryptoContext<Element> a)
const {
234 if (a.get() !=
this) {
241 LPPrivateKey<Element> privateKey;
269 cerr <<
"Warning - SetPrivateKey is only intended to be used for debugging " 270 "purposes - not for production systems." 272 this->privateKey = sk;
276 "SetPrivateKey is only allowed if DEBUG_KEY is set in palisade.h");
306 return this->privateKey;
310 "GetPrivateKey is only allowed if DEBUG_KEY is set in palisade.h");
314 void setSchemeId(
string schemeTag) { this->m_schemeId = schemeTag; }
316 string getSchemeId()
const {
return this->m_schemeId; }
325 const string& schemeId =
"Not") {
326 this->params.reset(params);
327 this->scheme.reset(scheme);
328 this->m_keyGenLevel = 0;
329 this->m_schemeId = schemeId;
339 const string& schemeId =
"Not") {
340 this->params = params;
341 this->scheme = scheme;
342 this->m_keyGenLevel = 0;
343 this->m_schemeId = schemeId;
353 this->m_keyGenLevel = 0;
354 this->m_schemeId = c.m_schemeId;
365 m_keyGenLevel = rhs.m_keyGenLevel;
366 m_schemeId = rhs.m_schemeId;
373 operator bool()
const {
return params && scheme; }
386 if (a.params.get() == b.params.get()) {
389 if (
typeid(*a.params.get()) !=
typeid(*b.params.get())) {
392 if (*a.params.get() != *b.params.get())
return false;
395 if (a.scheme.get() == b.scheme.get()) {
398 if (
typeid(*a.scheme.get()) !=
typeid(*b.scheme.get())) {
401 if (*a.scheme.get() != *b.scheme.get())
return false;
420 template <
typename ST>
432 template <
typename ST>
434 const CryptoContext<Element> cc) {
435 std::map<string, std::vector<LPEvalKey<Element>>> omap;
437 if (k.second[0]->GetCryptoContext() == cc) {
438 omap[k.first] = k.second;
442 if (omap.size() == 0)
return false;
444 Serial::Serialize(omap, ser, sertype);
456 template <
typename ST>
458 std::map<string, std::vector<LPEvalKey<Element>>> evalMultKeys;
495 const std::vector<LPEvalKey<Element>>& vectorToInsert);
505 template <
typename ST>
508 std::map<string, shared_ptr<std::map<usint, LPEvalKey<Element>>>>* smap;
509 std::map<string, shared_ptr<std::map<usint, LPEvalKey<Element>>>> omap;
511 if (
id.length() == 0) {
512 smap = &GetAllEvalSumKeys();
514 auto k = GetAllEvalSumKeys().find(
id);
516 if (k == GetAllEvalSumKeys().end())
return false;
519 omap[k->first] = k->second;
521 Serial::Serialize(*smap, ser, sertype);
533 template <
typename ST>
535 const CryptoContext<Element> cc) {
536 std::map<string, shared_ptr<std::map<usint, LPEvalKey<Element>>>> omap;
537 for (
const auto& k : GetAllEvalSumKeys()) {
538 if (k.second->begin()->second->GetCryptoContext() == cc) {
539 omap[k.first] = k.second;
543 if (omap.size() == 0)
return false;
545 Serial::Serialize(omap, ser, sertype);
559 template <
typename ST>
561 std::map<string, shared_ptr<std::map<usint, LPEvalKey<Element>>>>
564 Serial::Deserialize(evalSumKeys, ser, sertype);
569 for (
auto k : evalSumKeys) {
570 GetAllEvalSumKeys()[k.first] = k.second;
599 const shared_ptr<std::map<usint, LPEvalKey<Element>>> mapToInsert);
610 template <
typename ST>
613 std::map<string, shared_ptr<std::map<usint, LPEvalKey<Element>>>>* smap;
614 std::map<string, shared_ptr<std::map<usint, LPEvalKey<Element>>>> omap;
615 if (
id.length() == 0) {
616 smap = &GetAllEvalAutomorphismKeys();
618 auto k = GetAllEvalAutomorphismKeys().find(
id);
620 if (k == GetAllEvalAutomorphismKeys().end())
return false;
623 omap[k->first] = k->second;
625 Serial::Serialize(*smap, ser, sertype);
637 template <
typename ST>
639 const CryptoContext<Element> cc) {
640 std::map<string, shared_ptr<std::map<usint, LPEvalKey<Element>>>> omap;
641 for (
const auto& k : GetAllEvalAutomorphismKeys()) {
642 if (k.second->begin()->second->GetCryptoContext() == cc) {
643 omap[k.first] = k.second;
647 if (omap.size() == 0)
return false;
649 Serial::Serialize(omap, ser, sertype);
662 template <
typename ST>
665 std::map<string, shared_ptr<std::map<usint, LPEvalKey<Element>>>>
668 Serial::Deserialize(evalSumKeys, ser, sertype);
673 for (
auto k : evalSumKeys) {
674 GetAllEvalAutomorphismKeys()[k.first] = k.second;
704 const shared_ptr<std::map<usint, LPEvalKey<Element>>> mapToInsert);
711 void Enable(PKESchemeFeature feature) { scheme->Enable(feature); }
717 void Enable(usint featureMask) { scheme->Enable(featureMask); }
724 const shared_ptr<LPPublicKeyEncryptionScheme<Element>>
737 size_t GetKeyGenLevel()
const {
return m_keyGenLevel; }
739 void SetKeyGenLevel(
size_t level) { m_keyGenLevel = level; }
746 return params->GetElementParams();
754 return params->GetEncodingParams();
763 return params->GetElementParams()->GetCyclotomicOrder();
772 return params->GetElementParams()->GetRingDimension();
781 return params->GetElementParams()->GetModulus();
790 return params->GetElementParams()->GetRootOfUnity();
817 bool makeSparse =
false,
818 bool fresh =
false) {
819 if (!pk) PALISADE_THROW(
config_error,
"Input public key is empty");
835 const vector<LPPrivateKey<Element>>& secretKeys) {
836 if (!secretKeys.size())
837 PALISADE_THROW(
config_error,
"Input private key vector is empty");
852 const LPPrivateKey<Element> privateKey,
853 const vector<Ciphertext<Element>>& ciphertext)
const {
854 if (privateKey ==
nullptr || Mismatched(privateKey->GetCryptoContext()))
856 "Information passed to MultipartyDecryptLead was not " 857 "generated with this crypto context");
859 vector<Ciphertext<Element>> newCiphertext;
861 for (
size_t i = 0; i < ciphertext.size(); i++) {
862 if (ciphertext[i] ==
nullptr ||
863 Mismatched(ciphertext[i]->GetCryptoContext()))
865 "A ciphertext passed to MultipartyDecryptLead was not " 866 "generated with this crypto context");
869 privateKey, ciphertext[i]));
872 return newCiphertext;
883 const LPPrivateKey<Element> privateKey,
884 const vector<Ciphertext<Element>>& ciphertext)
const {
885 if (privateKey ==
nullptr || Mismatched(privateKey->GetCryptoContext()))
887 "Information passed to MultipartyDecryptMain was not " 888 "generated with this crypto context");
890 vector<Ciphertext<Element>> newCiphertext;
892 for (
size_t i = 0; i < ciphertext.size(); i++) {
893 if (ciphertext[i] ==
nullptr ||
894 Mismatched(ciphertext[i]->GetCryptoContext()))
896 "A ciphertext passed to MultipartyDecryptMain was not " 897 "generated with this crypto context");
900 privateKey, ciphertext[i]));
903 return newCiphertext;
915 const vector<Ciphertext<Element>>& partialCiphertextVec,
916 Plaintext* plaintext)
const;
929 const LPPrivateKey<Element> originalPrivateKey,
930 const LPPrivateKey<Element> newPrivateKey,
931 const LPEvalKey<Element> ek)
const {
932 if (!originalPrivateKey)
933 PALISADE_THROW(
config_error,
"Input first private key is nullptr");
935 PALISADE_THROW(
config_error,
"Input second private key is nullptr");
936 if (!ek) PALISADE_THROW(
config_error,
"Input evaluation key is nullptr");
954 const LPPrivateKey<Element> privateKey,
955 const shared_ptr<std::map<usint, LPEvalKey<Element>>> eAuto,
956 const std::vector<usint>& indexList,
const std::string& keyId =
"") {
958 PALISADE_THROW(
config_error,
"Input private key is nullptr");
960 PALISADE_THROW(
config_error,
"Input evaluation key map is nullptr");
961 if (!indexList.size())
962 PALISADE_THROW(
config_error,
"Input index vector is empty");
964 privateKey, eAuto, indexList, keyId);
980 const LPPrivateKey<Element> privateKey,
981 const shared_ptr<std::map<usint, LPEvalKey<Element>>> eAuto,
982 const std::vector<int32_t>& indexList,
const std::string& keyId =
"") {
984 PALISADE_THROW(
config_error,
"Input private key is nullptr");
986 PALISADE_THROW(
config_error,
"Input evaluation key map is nullptr");
987 if (!indexList.size())
988 PALISADE_THROW(
config_error,
"Input index vector is empty");
1005 const LPPrivateKey<Element> privateKey,
1006 const shared_ptr<std::map<usint, LPEvalKey<Element>>> eSum,
1007 const std::string& keyId =
"") {
1009 PALISADE_THROW(
config_error,
"Input private key is nullptr");
1011 PALISADE_THROW(
config_error,
"Input evaluation key map is nullptr");
1026 LPEvalKey<Element> b,
1027 const std::string& keyId =
"") {
1029 PALISADE_THROW(
config_error,
"Input first evaluation key is nullptr");
1031 PALISADE_THROW(
config_error,
"Input second evaluation key is nullptr");
1047 LPPrivateKey<Element> sk,
1048 const std::string& keyId =
"") {
1050 PALISADE_THROW(
config_error,
"Input evaluation key is nullptr");
1051 if (!sk) PALISADE_THROW(
config_error,
"Input private key is nullptr");
1065 const shared_ptr<std::map<usint, LPEvalKey<Element>>> es1,
1066 const shared_ptr<std::map<usint, LPEvalKey<Element>>> es2,
1067 const std::string& keyId =
"") {
1069 PALISADE_THROW(
config_error,
"Input first evaluation key map is nullptr");
1072 "Input second evaluation key map is nullptr");
1086 const shared_ptr<std::map<usint, LPEvalKey<Element>>> es1,
1087 const shared_ptr<std::map<usint, LPEvalKey<Element>>> es2,
1088 const std::string& keyId =
"") {
1090 PALISADE_THROW(
config_error,
"Input first evaluation key map is nullptr");
1093 "Input second evaluation key map is nullptr");
1108 LPPublicKey<Element> pubKey2,
1109 const std::string& keyId =
"") {
1111 PALISADE_THROW(
config_error,
"Input first public key is nullptr");
1113 PALISADE_THROW(
config_error,
"Input second public key is nullptr");
1128 LPEvalKey<Element> evalKey2,
1129 const std::string& keyId =
"") {
1131 PALISADE_THROW(
config_error,
"Input first evaluation key is nullptr");
1133 PALISADE_THROW(
config_error,
"Input second evaluation key is nullptr");
1156 LPEvalKey<Element>
ReKeyGen(
const LPPublicKey<Element> newKey,
1157 const LPPrivateKey<Element> oldKey)
const {
1158 if (newKey ==
nullptr || oldKey ==
nullptr ||
1159 Mismatched(newKey->GetCryptoContext()) ||
1160 Mismatched(oldKey->GetCryptoContext()))
1162 "Keys passed to ReKeyGen were not generated with this " 1176 LPEvalKey<Element>
ReKeyGen(
const LPPrivateKey<Element> newKey,
1177 const LPPrivateKey<Element> oldKey)
const 1178 __attribute__((deprecated(
"functionality removed from PALISADE")));
1205 const string& keyID);
1211 static std::map<string, std::vector<LPEvalKey<Element>>>&
1222 const LPPrivateKey<Element> key2)
const {
1223 if (key1 ==
nullptr || key2 ==
nullptr ||
1224 Mismatched(key1->GetCryptoContext()) ||
1225 Mismatched(key2->GetCryptoContext()))
1227 "Keys passed to KeySwitchGen were not generated with this " 1240 Ciphertext<Element>
Encrypt(
const LPPublicKey<Element> publicKey,
1241 Plaintext plaintext) {
1242 if (publicKey ==
nullptr)
1243 PALISADE_THROW(
type_error,
"null key passed to Encrypt");
1245 if (plaintext ==
nullptr)
1246 PALISADE_THROW(
type_error,
"Input plaintext is nullptr");
1248 if (Mismatched(publicKey->GetCryptoContext()))
1251 "key passed to Encrypt was not generated with this crypto context");
1254 publicKey, plaintext->GetElement<Element>());
1257 ciphertext->SetEncodingType(plaintext->GetEncodingType());
1258 ciphertext->SetScalingFactor(plaintext->GetScalingFactor());
1259 ciphertext->SetDepth(plaintext->GetDepth());
1260 ciphertext->SetLevel(plaintext->GetLevel());
1272 Ciphertext<Element>
Encrypt(
const LPPrivateKey<Element> privateKey,
1273 Plaintext plaintext)
const {
1274 if (privateKey ==
nullptr || Mismatched(privateKey->GetCryptoContext()))
1277 "key passed to Encrypt was not generated with this crypto context");
1278 if (plaintext ==
nullptr)
1279 PALISADE_THROW(
type_error,
"Input plaintext is nullptr");
1282 privateKey, plaintext->GetElement<Element>());
1285 ciphertext->SetEncodingType(plaintext->GetEncodingType());
1286 ciphertext->SetScalingFactor(plaintext->GetScalingFactor());
1287 ciphertext->SetDepth(plaintext->GetDepth());
1288 ciphertext->SetLevel(plaintext->GetLevel());
1313 auto p = PlaintextFactory::MakePlaintext(
1336 template <
typename Value1>
1338 CryptoContext<Element> cc,
1339 const Value1& value) {
1340 return PlaintextFactory::MakePlaintext(encoding, cc->GetElementParams(),
1341 cc->GetEncodingParams(), value);
1344 template <
typename Value1,
typename Value2>
1346 CryptoContext<Element> cc,
const Value1& value,
1347 const Value2& value2) {
1348 return PlaintextFactory::MakePlaintext(encoding, cc->GetElementParams(),
1349 cc->GetEncodingParams(), value,
1365 const std::vector<std::complex<double>>& value,
size_t depth = 1,
1366 uint32_t level = 0,
const shared_ptr<ParmType> params =
nullptr)
const {
1368 const auto cryptoParamsCKKS =
1374 if (params ==
nullptr) {
1375 shared_ptr<ILDCRTParams<DCRTPoly::Integer>> elemParamsPtr;
1378 *(cryptoParamsCKKS->GetElementParams());
1379 for (uint32_t i = 0; i < level; i++) {
1383 std::make_shared<ILDCRTParams<DCRTPoly::Integer>>(elemParams);
1385 elemParamsPtr = cryptoParamsCKKS->GetElementParams();
1388 p = Plaintext(std::make_shared<CKKSPackedEncoding>(
1392 p = Plaintext(std::make_shared<CKKSPackedEncoding>(
1410 const std::vector<double>& value,
size_t depth = 1, uint32_t level = 0,
1411 const shared_ptr<ParmType> params =
nullptr)
const {
1412 std::vector<std::complex<double>> complexValue(value.size());
1413 std::transform(value.begin(), value.end(), complexValue.begin(),
1414 [](
double da) {
return std::complex<double>(da); });
1428 shared_ptr<ParmType> evp,
1441 ConstCiphertext<Element> ciphertext,
1442 Plaintext* plaintext);
1453 LPEvalKey<Element> evalKey, ConstCiphertext<Element> ciphertext,
1454 const LPPublicKey<Element> publicKey =
nullptr)
const {
1455 if (evalKey ==
nullptr || Mismatched(evalKey->GetCryptoContext()))
1457 "Information passed to ReEncrypt was not generated with " 1458 "this crypto context");
1460 if (ciphertext ==
nullptr || Mismatched(ciphertext->GetCryptoContext()))
1462 "The ciphertext passed to ReEncrypt was not generated " 1463 "with this crypto context");
1465 Ciphertext<Element> newCiphertext =
1468 return newCiphertext;
1477 Ciphertext<Element>
EvalAdd(ConstCiphertext<Element> ct1,
1478 ConstCiphertext<Element> ct2)
const {
1492 ConstCiphertext<Element> ct2)
const {
1508 Ciphertext<Element>& ct2)
const {
1521 Ciphertext<Element>
EvalSub(ConstCiphertext<Element> ct1,
1522 ConstCiphertext<Element> ct2)
const {
1539 Ciphertext<Element>& ct2)
const {
1552 Ciphertext<Element>
EvalAdd(ConstCiphertext<Element> ciphertext,
1553 ConstPlaintext plaintext)
const {
1556 plaintext->SetFormat(EVALUATION);
1572 Plaintext plaintext)
const {
1573 TypeCheck((ConstCiphertext<Element>)ciphertext, (ConstPlaintext)plaintext);
1575 plaintext->SetFormat(EVALUATION);
1587 Ciphertext<Element>
EvalAdd(ConstCiphertext<Element> ciphertext,
1588 double constant)
const {
1589 Ciphertext<Element> rv;
1591 if (constant >= 0) {
1609 vector<double> constants)
const {
1624 vector<Ciphertext<Element>> ciphertexts, vector<double> constants)
const {
1631 vector<double> constants, vector<Ciphertext<Element>> ciphertexts)
const {
1636 vector<double> constants, vector<Ciphertext<Element>> ciphertexts)
const {
1640 inline Ciphertext<Element>
EvalAdd(
1641 ConstPlaintext plaintext, ConstCiphertext<Element> ciphertext)
const {
1642 return EvalAdd(ciphertext, plaintext);
1646 Plaintext plaintext, Ciphertext<Element>& ciphertext)
const {
1650 inline Ciphertext<Element>
EvalAdd(
1651 double constant, ConstCiphertext<Element> ciphertext)
const {
1652 return EvalAdd(ciphertext, constant);
1661 Ciphertext<Element>
EvalSub(ConstCiphertext<Element> ciphertext,
1662 ConstPlaintext plaintext)
const {
1679 Plaintext plaintext)
const {
1680 TypeCheck((ConstCiphertext<Element>)ciphertext, (ConstPlaintext)plaintext);
1692 Ciphertext<Element>
EvalSub(ConstCiphertext<Element> ciphertext,
1693 double constant)
const {
1694 Ciphertext<Element> rv;
1696 if (constant >= 0) {
1705 inline Ciphertext<Element>
EvalSub(
1706 ConstPlaintext plaintext, ConstCiphertext<Element> ciphertext)
const {
1711 Plaintext plaintext, Ciphertext<Element>& ciphertext)
const {
1712 Ciphertext<Element> negated =
EvalNegate(ciphertext);
1718 inline Ciphertext<Element>
EvalSub(
1719 double constant, ConstCiphertext<Element> ciphertext)
const {
1730 Ciphertext<Element>
EvalMult(ConstCiphertext<Element> ct1,
1731 ConstCiphertext<Element> ct2)
const {
1737 "Evaluation key has not been generated for EvalMult");
1754 Ciphertext<Element>& ct2)
const {
1760 "Evaluation key has not been generated for EvalMult");
1775 ConstCiphertext<Element> ct2)
const {
1796 const vector<Ciphertext<Element>>& ct)
const {
1798 if (!ct.size()) PALISADE_THROW(
type_error,
"Empty input ciphertext vector");
1800 if (ct.size() == 1){
1805 if (ek.size() < (ct[0]->GetElements().size() - 2)) {
1807 "Insufficient value was used for maxDepth to generate " 1808 "keys for EvalMult");
1824 const vector<Ciphertext<Element>>& ctList)
const {
1827 PALISADE_THROW(
type_error,
"Empty input ciphertext vector");
1829 if (ctList.size() == 1){
1848 vector<Ciphertext<Element>>& ctList)
const {
1851 PALISADE_THROW(
type_error,
"Empty input ciphertext vector");
1869 ConstCiphertext<Element> ct1, ConstCiphertext<Element> ct2)
const {
1871 if (!ct1 || !ct2) PALISADE_THROW(
type_error,
"Input ciphertext is nullptr");
1875 (ct1->GetElements().size() + ct2->GetElements().size() - 3)) {
1877 "Insufficient value was used for maxDepth to generate " 1878 "keys for EvalMult");
1894 if (!ct) PALISADE_THROW(
type_error,
"Input ciphertext is nullptr");
1898 if (ek.size() < (ct->GetElements().size() - 2)) {
1900 "Insufficient value was used for maxDepth to generate " 1901 "keys for EvalMult");
1918 PALISADE_THROW(
type_error,
"Input ciphertext is nullptr");
1921 if (ek.size() < (ct->GetElements().size() - 2)) {
1923 "Insufficient value was used for maxDepth to generate " 1924 "keys for EvalMult");
1936 inline Ciphertext<Element>
EvalMult(ConstPlaintext pt2,
1937 ConstCiphertext<Element> ct1)
const {
1948 Ciphertext<Element>& ct1)
const {
1959 ConstCiphertext<Element> ct1)
const {
1964 Ciphertext<Element>& ct1)
const {
1974 Ciphertext<Element>
EvalMult(ConstCiphertext<Element> ct1,
1975 ConstPlaintext pt2)
const {
1992 Plaintext pt2)
const {
2005 Ciphertext<Element>
EvalMult(ConstCiphertext<Element> ciphertext,
2006 double constant)
const {
2009 PALISADE_THROW(
type_error,
"Input ciphertext is nullptr");
2026 double constant)
const {
2029 PALISADE_THROW(
type_error,
"Input ciphertext is nullptr");
2041 Ciphertext<Element>
EvalNegate(ConstCiphertext<Element> ct)
const {
2042 if (ct ==
nullptr || Mismatched(ct->GetCryptoContext()))
2044 "Information passed to EvalNegate was not generated with " 2045 "this crypto context");
2061 const LPPublicKey<Element> publicKey,
2062 const LPPrivateKey<Element> origPrivateKey,
2063 const std::vector<usint>& indexList)
const {
2064 if (publicKey ==
nullptr || origPrivateKey ==
nullptr)
2066 if (!indexList.size())
2067 PALISADE_THROW(
config_error,
"Input index vector is empty");
2068 if (publicKey->GetCryptoContext().get() !=
this)
2070 "Key was not created in this CryptoContextImpl");
2071 if (publicKey->GetCryptoContext() != origPrivateKey->GetCryptoContext())
2073 "Keys were not created in the same CryptoContextImpl");
2076 publicKey, origPrivateKey, indexList);
2090 ConstCiphertext<Element> ciphertext, usint i,
2091 const std::map<usint, LPEvalKey<Element>>& evalKeys,
2092 CALLER_INFO_ARGS_HDR)
const {
2093 if (
nullptr == ciphertext) {
2094 std::string errorMsg(std::string(
"Input ciphertext is nullptr") +
2099 if (evalKeys.empty()) {
2100 std::string errorMsg(std::string(
"Empty input key map") + CALLER_INFO);
2103 auto tk = evalKeys.begin()->second;
2104 if (
nullptr == tk) {
2105 std::string errorMsg(std::string(
"Invalid evalKey") + CALLER_INFO);
2109 if (ciphertext->GetCryptoContext().get() !=
this) {
2110 std::string errorMsg(
2111 std::string(
"Ciphertext was not created in this CryptoContextImpl") +
2116 if (ciphertext->GetCryptoContext() != tk->GetCryptoContext()) {
2117 std::string errorMsg(
2118 std::string(
"Items were not created in the same CryptoContextImpl") +
2123 if (ciphertext->GetKeyTag() != tk->GetKeyTag()) {
2124 std::string errorMsg(
2125 std::string(
"Items were not encrypted with same keys") + CALLER_INFO);
2143 const LPPrivateKey<Element> privateKey,
2144 const std::vector<usint>& indexList)
const {
2145 if (privateKey ==
nullptr) PALISADE_THROW(
type_error,
"Null input");
2146 if (!indexList.size())
2147 PALISADE_THROW(
config_error,
"Input index vector is empty");
2148 if (privateKey->GetCryptoContext().get() !=
this)
2150 "Key was not created in this CryptoContextImpl");
2164 const LPPublicKey<Element> publicKey =
nullptr);
2166 shared_ptr<std::map<usint, LPEvalKey<Element>>> EvalSumRowsKeyGen(
2167 const LPPrivateKey<Element> privateKey,
2168 const LPPublicKey<Element> publicKey =
nullptr, usint rowSize = 0,
2169 usint subringDim = 0);
2171 shared_ptr<std::map<usint, LPEvalKey<Element>>> EvalSumColsKeyGen(
2172 const LPPrivateKey<Element> privateKey,
2173 const LPPublicKey<Element> publicKey =
nullptr);
2183 static std::map<string, shared_ptr<std::map<usint, LPEvalKey<Element>>>>&
2184 GetAllEvalSumKeys();
2193 Ciphertext<Element>
EvalSum(ConstCiphertext<Element> ciphertext,
2194 usint batchSize)
const;
2196 Ciphertext<Element> EvalSumRows(
2197 ConstCiphertext<Element> ciphertext, usint rowSize,
2198 const std::map<usint, LPEvalKey<Element>>& evalKeys,
2199 usint subringDim = 0)
const;
2201 Ciphertext<Element> EvalSumCols(
2202 ConstCiphertext<Element> ciphertext, usint rowSize,
2203 const std::map<usint, LPEvalKey<Element>>& evalKeys)
const;
2213 const std::vector<int32_t>& indexList,
2214 const LPPublicKey<Element> publicKey =
nullptr);
2244 ConstCiphertext<Element> ct)
const {
2286 ConstCiphertext<Element> ct,
const usint index,
const usint m,
2287 const shared_ptr<vector<Element>> digits)
const {
2303 const vector<Ciphertext<Element>>& ciphertextVector)
const;
2313 static std::map<string, shared_ptr<std::map<usint, LPEvalKey<Element>>>>&
2314 GetAllEvalAutomorphismKeys();
2323 Ciphertext<Element>
EvalAtIndex(ConstCiphertext<Element> ciphertext,
2324 int32_t index)
const;
2335 ConstCiphertext<Element> ciphertext2,
2336 usint batchSize)
const;
2347 ConstPlaintext plaintext,
2348 usint batchSize)
const;
2360 ConstCiphertext<Element> ciphertext,
2361 const std::vector<double>& coefficients)
const {
2362 if (ciphertext ==
nullptr ||
2363 this->Mismatched(ciphertext->GetCryptoContext()))
2364 throw std::logic_error(
2365 "Information passed to EvalPoly was not generated with this crypto " 2370 ->
EvalPoly(ciphertext, coefficients);
2380 Ciphertext<Element>
KeySwitch(
const LPEvalKey<Element> keySwitchHint,
2381 ConstCiphertext<Element> ciphertext)
const {
2382 if (keySwitchHint ==
nullptr ||
2383 Mismatched(keySwitchHint->GetCryptoContext()))
2386 "Key passed to KeySwitch was not generated with this crypto context");
2388 if (ciphertext ==
nullptr || Mismatched(ciphertext->GetCryptoContext()))
2390 "Ciphertext passed to KeySwitch was not generated with " 2391 "this crypto context");
2403 Ciphertext<Element>& ciphertext)
const {
2404 if (keySwitchHint ==
nullptr ||
2405 Mismatched(keySwitchHint->GetCryptoContext()))
2407 "Key passed to KeySwitchInPlace was not generated with " 2408 "this crypto context");
2410 if (ciphertext ==
nullptr || Mismatched(ciphertext->GetCryptoContext()))
2413 "Ciphertext passed to KeySwitchInPlace was not generated with " 2414 "this crypto context");
2426 Ciphertext<Element>
Rescale(ConstCiphertext<Element> ciphertext)
const {
2427 if (ciphertext ==
nullptr || Mismatched(ciphertext->GetCryptoContext()))
2429 "Information passed to Rescale was not generated with " 2430 "this crypto context");
2443 if (ciphertext ==
nullptr || Mismatched(ciphertext->GetCryptoContext()))
2446 "Information passed to RescaleInPlace was not generated with " 2447 "this crypto context");
2457 Ciphertext<Element>
ModReduce(ConstCiphertext<Element> ciphertext)
const {
2458 if (ciphertext ==
nullptr || Mismatched(ciphertext->GetCryptoContext()))
2461 "Information passed to ModReduce was not generated with this crypto " 2473 if (ciphertext ==
nullptr || Mismatched(ciphertext->GetCryptoContext()))
2476 "Information passed to ModReduce was not generated with this crypto " 2489 const LPEvalKey<Element> linearKeySwitchHint,
2490 size_t levels = 1)
const {
2491 const auto cryptoParams =
2493 cipherText1->GetCryptoParameters());
2495 if (cipherText1 ==
nullptr || Mismatched(cipherText1->GetCryptoContext())) {
2497 "Information passed to LevelReduce was not generated with " 2498 "this crypto context");
2502 cipherText1, linearKeySwitchHint, levels);
2514 ConstCiphertext<Element> ciphertext1,
2515 ConstCiphertext<Element> ciphertext2)
const {
2516 if (ciphertext1 ==
nullptr || ciphertext2 ==
nullptr ||
2517 ciphertext1->GetKeyTag() != ciphertext2->GetKeyTag() ||
2518 Mismatched(ciphertext1->GetCryptoContext()))
2520 "Ciphertexts passed to ComposedEvalMult were not " 2521 "generated with this crypto context");
2526 "Evaluation key has not been generated for EvalMult");
2530 ciphertext2, ek[0]);
2541 Ciphertext<Element>
Compress(ConstCiphertext<Element> ciphertext1,
2542 uint32_t numTowers = 1)
const {
2543 if (ciphertext1 ==
nullptr)
2544 PALISADE_THROW(
config_error,
"input ciphertext is invalid (has no data)");
2550 template <
class Archive>
2551 void save(Archive& ar, std::uint32_t
const version)
const {
2552 ar(cereal::make_nvp(
"cc", params));
2553 ar(cereal::make_nvp(
"kt", scheme));
2554 ar(cereal::make_nvp(
"si", m_schemeId));
2557 template <
class Archive>
2558 void load(Archive& ar, std::uint32_t
const version) {
2559 if (version > SerializedVersion()) {
2561 "serialized object version " + std::to_string(version) +
2562 " is from a later version of the library");
2564 ar(cereal::make_nvp(
"cc", params));
2565 ar(cereal::make_nvp(
"kt", scheme));
2566 ar(cereal::make_nvp(
"si", m_schemeId));
2576 virtual std::string SerializedObjectName()
const {
return "CryptoContext"; }
2577 static uint32_t SerializedVersion() {
return 1; }
2585 template <
typename Element>
2588 CryptoContext<Element> context;
2594 explicit CryptoObject(CryptoContext<Element> cc =
nullptr,
2595 const string& tag =
"")
2596 : context(cc), keyTag(tag) {}
2599 context = rhs.context;
2600 keyTag = rhs.keyTag;
2604 context = std::move(rhs.context);
2605 keyTag = std::move(rhs.keyTag);
2611 this->context = rhs.context;
2612 this->keyTag = rhs.keyTag;
2617 this->context = std::move(rhs.context);
2618 this->keyTag = std::move(rhs.keyTag);
2623 return context.get() == rhs.context.get() && keyTag == rhs.keyTag;
2626 CryptoContext<Element> GetCryptoContext()
const {
return context; }
2629 return context->GetCryptoParameters();
2632 const EncodingParams GetEncodingParameters()
const {
2633 return context->GetCryptoParameters()->GetEncodingParams();
2636 const string GetKeyTag()
const {
return keyTag; }
2638 void SetKeyTag(
const string& tag) { keyTag = tag; }
2640 template <
class Archive>
2641 void save(Archive& ar, std::uint32_t
const version)
const {
2642 ar(::cereal::make_nvp(
"cc", context));
2643 ar(::cereal::make_nvp(
"kt", keyTag));
2646 template <
class Archive>
2647 void load(Archive& ar, std::uint32_t
const version) {
2648 if (version > SerializedVersion()) {
2650 "serialized object version " + std::to_string(version) +
2651 " is from a later version of the library");
2653 ar(::cereal::make_nvp(
"cc", context));
2654 ar(::cereal::make_nvp(
"kt", keyTag));
2657 context->GetCryptoParameters(),
2658 context->GetEncryptionAlgorithm(),
2659 context->getSchemeId()
2663 std::string SerializedObjectName()
const {
return "CryptoObject"; }
2664 static uint32_t SerializedVersion() {
return 1; }
2674 template <
typename Element>
2676 using ParmType =
typename Element::Params;
2677 using IntType =
typename Element::Integer;
2680 static vector<CryptoContext<Element>> AllContexts;
2683 static void ReleaseAllContexts();
2685 static int GetContextCount();
2687 static CryptoContext<Element> GetSingleContext();
2689 static CryptoContext<Element> GetContext(
2692 const string& schemeId =
"Not");
2694 static CryptoContext<Element> GetContextForPointer(
2697 static const vector<CryptoContext<Element>>& GetAllContexts();
2724 static CryptoContext<Element> genCryptoContextBFV(
2725 shared_ptr<ParmType> params,
const PlaintextModulus plaintextmodulus,
2726 usint relinWindow,
float stDev,
const std::string& delta,
2727 MODE mode = RLWE,
const std::string& bigmodulus =
"0",
2728 const std::string& bigrootofunity =
"0",
int depth = 0,
2729 int assuranceMeasure = 0,
float securityLevel = 0,
2730 const std::string& bigmodulusarb =
"0",
2731 const std::string& bigrootofunityarb =
"0",
int maxDepth = 2);
2758 static CryptoContext<Element> genCryptoContextBFV(
2759 shared_ptr<ParmType> params, EncodingParams encodingParams,
2760 usint relinWindow,
float stDev,
const std::string& delta,
2761 MODE mode = RLWE,
const std::string& bigmodulus =
"0",
2762 const std::string& bigrootofunity =
"0",
int depth = 0,
2763 int assuranceMeasure = 0,
float securityLevel = 0,
2764 const std::string& bigmodulusarb =
"0",
2765 const std::string& bigrootofunityarb =
"0",
int maxDepth = 2);
2791 static CryptoContext<Element> genCryptoContextBFV(
2792 const PlaintextModulus plaintextModulus,
float securityLevel,
2793 usint relinWindow,
float dist,
unsigned int numAdds,
2794 unsigned int numMults,
unsigned int numKeyswitches, MODE mode = OPTIMIZED,
2795 int maxDepth = 2, uint32_t n = 0);
2821 static CryptoContext<Element> genCryptoContextBFV(
2822 const PlaintextModulus plaintextModulus, SecurityLevel securityLevel,
2823 usint relinWindow,
float dist,
unsigned int numAdds,
2824 unsigned int numMults,
unsigned int numKeyswitches, MODE mode = OPTIMIZED,
2825 int maxDepth = 2, uint32_t n = 0);
2849 static CryptoContext<Element> genCryptoContextBFV(
2850 EncodingParams encodingParams,
float securityLevel, usint relinWindow,
2851 float dist,
unsigned int numAdds,
unsigned int numMults,
2852 unsigned int numKeyswitches, MODE mode = OPTIMIZED,
int maxDepth = 2,
2877 static CryptoContext<Element> genCryptoContextBFV(
2878 EncodingParams encodingParams, SecurityLevel securityLevel,
2879 usint relinWindow,
float dist,
unsigned int numAdds,
2880 unsigned int numMults,
unsigned int numKeyswitches, MODE mode = OPTIMIZED,
2881 int maxDepth = 2, uint32_t n = 0);
2908 static CryptoContext<Element> genCryptoContextBFVrns(
2909 const PlaintextModulus plaintextModulus,
float securityLevel,
float dist,
2910 unsigned int numAdds,
unsigned int numMults,
unsigned int numKeyswitches,
2911 MODE mode = OPTIMIZED,
int maxDepth = 2, uint32_t relinWindow = 0,
2912 size_t dcrtBits = 60, uint32_t n = 0);
2939 static CryptoContext<Element> genCryptoContextBFVrns(
2940 const PlaintextModulus plaintextModulus, SecurityLevel securityLevel,
2941 float dist,
unsigned int numAdds,
unsigned int numMults,
2942 unsigned int numKeyswitches, MODE mode = OPTIMIZED,
int maxDepth = 2,
2943 uint32_t relinWindow = 0,
size_t dcrtBits = 60, uint32_t n = 0);
2970 static CryptoContext<Element> genCryptoContextBFVrns(
2971 EncodingParams encodingParams,
float securityLevel,
float dist,
2972 unsigned int numAdds,
unsigned int numMults,
unsigned int numKeyswitches,
2973 MODE mode = OPTIMIZED,
int maxDepth = 2, uint32_t relinWindow = 0,
2974 size_t dcrtBits = 60, uint32_t n = 0);
3001 static CryptoContext<Element> genCryptoContextBFVrns(
3002 EncodingParams encodingParams, SecurityLevel securityLevel,
float dist,
3003 unsigned int numAdds,
unsigned int numMults,
unsigned int numKeyswitches,
3004 MODE mode = OPTIMIZED,
int maxDepth = 2, uint32_t relinWindow = 0,
3005 size_t dcrtBits = 60, uint32_t n = 0);
3032 static CryptoContext<Element> genCryptoContextBFVrnsB(
3033 const PlaintextModulus plaintextModulus,
float securityLevel,
float dist,
3034 unsigned int numAdds,
unsigned int numMults,
unsigned int numKeyswitches,
3035 MODE mode = OPTIMIZED,
int maxDepth = 2, uint32_t relinWindow = 0,
3036 size_t dcrtBits = 60, uint32_t n = 0);
3063 static CryptoContext<Element> genCryptoContextBFVrnsB(
3064 const PlaintextModulus plaintextModulus, SecurityLevel securityLevel,
3065 float dist,
unsigned int numAdds,
unsigned int numMults,
3066 unsigned int numKeyswitches, MODE mode = OPTIMIZED,
int maxDepth = 2,
3067 uint32_t relinWindow = 0,
size_t dcrtBits = 60, uint32_t = 0);
3094 static CryptoContext<Element> genCryptoContextBFVrnsB(
3095 EncodingParams encodingParams,
float securityLevel,
float dist,
3096 unsigned int numAdds,
unsigned int numMults,
unsigned int numKeyswitches,
3097 MODE mode = OPTIMIZED,
int maxDepth = 2, uint32_t relinWindow = 0,
3098 size_t dcrtBits = 60, uint32_t n = 0);
3125 static CryptoContext<Element> genCryptoContextBFVrnsB(
3126 EncodingParams encodingParams, SecurityLevel securityLevel,
float dist,
3127 unsigned int numAdds,
unsigned int numMults,
unsigned int numKeyswitches,
3128 MODE mode = OPTIMIZED,
int maxDepth = 2, uint32_t relinWindow = 0,
3129 size_t dcrtBits = 60, uint32_t n = 0);
3148 static CryptoContext<Element> genCryptoContextCKKS(
3149 shared_ptr<ParmType> params,
const PlaintextModulus plaintextmodulus,
3150 usint relinWindow,
float stDev, MODE mode = RLWE,
int depth = 1,
3151 int maxDepth = 2, KeySwitchTechnique ksTech = BV,
3152 RescalingTechnique rsTech = APPROXRESCALE);
3170 static CryptoContext<Element> genCryptoContextCKKS(
3171 shared_ptr<ParmType> params, EncodingParams encodingParams,
3172 usint relinWindow,
float stDev, MODE mode = RLWE,
int depth = 1,
3173 int maxDepth = 2,
enum KeySwitchTechnique ksTech = BV,
3174 RescalingTechnique rsTech = APPROXRESCALE);
3198 static CryptoContext<Element> genCryptoContextCKKSWithParamsGen(
3199 usint cyclOrder, usint numPrimes, usint scaleExp, usint relinWindow,
3200 usint batchSize, MODE mode,
int depth = 1,
int maxDepth = 2,
3201 usint firstModSize = FIRSTMODSIZE,
enum KeySwitchTechnique ksTech = BV,
3202 enum RescalingTechnique rsTech = APPROXRESCALE,
3203 uint32_t numLargeDigits = 4);
3229 static CryptoContext<Element> genCryptoContextCKKS(
3230 usint multiplicativeDepth, usint scalingFactorBits, usint batchSize,
3231 SecurityLevel stdLevel = HEStd_128_classic, usint ringDim = 0,
3232 enum RescalingTechnique rsTech = DEFAULTRSTECH,
3233 enum KeySwitchTechnique ksTech = HYBRID, uint32_t numLargeDigits = 0,
3234 int maxDepth = 2, usint firstModSize = FIRSTMODSIZE,
3235 usint relinWindow = 0, MODE mode = OPTIMIZED);
3253 static CryptoContext<Element> genCryptoContextBGVrns(
3254 shared_ptr<ParmType> params,
const PlaintextModulus plaintextmodulus,
3255 usint relinWindow,
float stDev, MODE mode = RLWE,
int depth = 1,
3256 int maxDepth = 2, KeySwitchTechnique ksTech = BV,
3257 enum ModSwitchMethod msMethod = MANUAL);
3274 static CryptoContext<Element> genCryptoContextBGVrns(
3275 shared_ptr<ParmType> params, EncodingParams encodingParams,
3276 usint relinWindow,
float stDev, MODE mode = RLWE,
int depth = 1,
3277 int maxDepth = 2,
enum KeySwitchTechnique ksTech = BV,
3278 enum ModSwitchMethod msMethod = MANUAL);
3301 static CryptoContext<Element> genCryptoContextBGVrnsWithParamsGen(
3302 usint cyclOrder, usint numPrimes, usint ptm, usint relinWindow, MODE mode,
3303 int depth = 1,
int maxDepth = 2,
enum KeySwitchTechnique ksTech = BV,
3304 usint firstModSize = 0, usint dcrtBits = 0, uint32_t numLargeDigits = 4,
3305 usint batchSize = 0,
enum ModSwitchMethod msMethod = MANUAL);
3332 static CryptoContext<Element> genCryptoContextBGVrns(
3333 usint multiplicativeDepth, usint ptm,
3334 SecurityLevel stdLevel = HEStd_128_classic,
float stdDev = 3.19,
3335 int maxDepth = 2, MODE mode = OPTIMIZED,
3336 enum KeySwitchTechnique ksTech = HYBRID, usint ringDim = 0,
3337 uint32_t numLargeDigits = 0, usint firstModSize = 0, usint dcrtBits = 0,
3338 usint relinWindow = 0, usint batchSize = 0,
3339 enum ModSwitchMethod msMethod = AUTO);
3348 static CryptoContext<Element> genCryptoContextNull(
3349 unsigned int m,
const PlaintextModulus ptModulus);
3358 static CryptoContext<Element> genCryptoContextNull(
3359 unsigned int m, EncodingParams encodingParams);
Ciphertext< Element > EvalAddManyInPlace(vector< Ciphertext< Element >> &ctList) const
Definition: cryptocontext.h:1847
Base class for PALISADE serialization.
Definition: serializable.h:76
Ciphertext< Element > EvalLinearWSumMutable(vector< Ciphertext< Element >> ciphertexts, vector< double > constants) const
Definition: cryptocontext.h:1623
CryptoContextImpl.
Definition: cryptocontext.h:56
CryptoContextImpl(LPCryptoParameters< Element > *params=nullptr, LPPublicKeyEncryptionScheme< Element > *scheme=nullptr, const string &schemeId="Not")
Definition: cryptocontext.h:323
Ciphertext< Element > Relinearize(ConstCiphertext< Element > ct) const
Definition: cryptocontext.h:1892
Ciphertext< Element > EvalAdd(ConstCiphertext< Element > ciphertext, double constant) const
Definition: cryptocontext.h:1587
Ciphertext< Element > EvalMultMutable(Ciphertext< Element > &ct1, Ciphertext< Element > &ct2) const
Definition: cryptocontext.h:1753
Decryption result. This represents whether the decryption of a cipheretext was performed correctly...
Definition: pubkeylp.h:105
Ciphertext< Element > EvalMultMutable(Plaintext pt2, Ciphertext< Element > &ct1) const
Definition: cryptocontext.h:1947
LPEvalKey< Element > KeySwitchGen(const LPPrivateKey< Element > key1, const LPPrivateKey< Element > key2) const
Definition: cryptocontext.h:1221
Ciphertext< Element > EvalSub(ConstCiphertext< Element > ciphertext, double constant) const
Definition: cryptocontext.h:1692
static const vector< LPEvalKey< Element > > & GetEvalMultKeyVector(const string &keyID)
Definition: cryptocontext.cpp:61
void RescaleInPlace(Ciphertext< Element > &ciphertext) const
Definition: cryptocontext.h:2442
Ciphertext< Element > EvalAddMany(const vector< Ciphertext< Element >> &ctList) const
Definition: cryptocontext.h:1823
CryptoContextImpl(const CryptoContextImpl< Element > &c)
Definition: cryptocontext.h:350
const shared_ptr< LPCryptoParameters< Element > > GetCryptoParameters() const
Definition: cryptocontext.h:733
void KeySwitchInPlace(const LPEvalKey< Element > keySwitchHint, Ciphertext< Element > &ciphertext) const
Definition: cryptocontext.h:2402
Ciphertext< Element > EvalMultMany(const vector< Ciphertext< Element >> &ct) const
Definition: cryptocontext.h:1795
Ciphertext< Element > EvalFastRotation(ConstCiphertext< Element > ct, const usint index, const usint m, const shared_ptr< vector< Element >> digits) const
Definition: cryptocontext.h:2285
Plaintext MakeStringPlaintext(const string &str) const
Definition: cryptocontext.h:1301
static bool DeserializeEvalAutomorphismKey(std::istream &ser, const ST &sertype)
Definition: cryptocontext.h:663
Ciphertext< Element > EvalLinearWSum(vector< Ciphertext< Element >> ciphertexts, vector< double > constants) const
Definition: cryptocontext.h:1608
static void ClearEvalAutomorphismKeys()
Definition: cryptocontext.cpp:286
Ciphertext< Element > EvalSub(ConstCiphertext< Element > ct1, ConstCiphertext< Element > ct2) const
Definition: cryptocontext.h:1521
CryptoContextFactory.
Definition: cryptocontext.h:53
static bool SerializeEvalSumKey(std::ostream &ser, const ST &sertype, string id="")
Definition: cryptocontext.h:506
shared_ptr< std::map< usint, LPEvalKey< Element > > > MultiAddEvalAutomorphismKeys(const shared_ptr< std::map< usint, LPEvalKey< Element >>> es1, const shared_ptr< std::map< usint, LPEvalKey< Element >>> es2, const std::string &keyId="")
Definition: cryptocontext.h:1085
void ModReduceInPlace(Ciphertext< Element > &ciphertext) const
Definition: cryptocontext.h:2472
LPEvalKey< Element > MultiMultEvalKey(LPEvalKey< Element > evalKey, LPPrivateKey< Element > sk, const std::string &keyId="")
Definition: cryptocontext.h:1046
Definition: exception.h:133
CryptoObject.
Definition: cryptocontext.h:2586
void RelinearizeInPlace(Ciphertext< Element > &ct) const
Definition: cryptocontext.h:1915
Ciphertext< Element > EvalSub(ConstCiphertext< Element > ciphertext, ConstPlaintext plaintext) const
Definition: cryptocontext.h:1661
shared_ptr< vector< Element > > EvalFastRotationPrecompute(ConstCiphertext< Element > ct) const
Definition: cryptocontext.h:2243
LPKeyPair< Element > KeyGen()
Definition: cryptocontext.h:797
static void InsertEvalSumKey(const shared_ptr< std::map< usint, LPEvalKey< Element >>> mapToInsert)
Definition: cryptocontext.cpp:235
friend bool operator==(const CryptoContextImpl< Element > &a, const CryptoContextImpl< Element > &b)
Definition: cryptocontext.h:382
Definition: exception.h:147
Ciphertext< Element > EvalAddMutable(Ciphertext< Element > &ciphertext, Plaintext plaintext) const
Definition: cryptocontext.h:1571
Ciphertext< Element > Encrypt(const LPPrivateKey< Element > privateKey, Plaintext plaintext) const
Definition: cryptocontext.h:1272
Ciphertext< Element > EvalMult(ConstPlaintext pt2, ConstCiphertext< Element > ct1) const
Definition: cryptocontext.h:1936
static Plaintext MakePlaintext(PlaintextEncodings encoding, CryptoContext< Element > cc, const Value1 &value)
Definition: cryptocontext.h:1337
CryptoContextImpl< Element > & operator=(const CryptoContextImpl< Element > &rhs)
Definition: cryptocontext.h:362
static std::map< string, std::vector< LPEvalKey< Element > > > & GetAllEvalMultKeys()
Definition: cryptocontext.cpp:72
static const std::map< usint, LPEvalKey< Element > > & GetEvalAutomorphismKeyMap(const string &id)
Definition: cryptocontext.cpp:270
vector< Ciphertext< Element > > MultipartyDecryptMain(const LPPrivateKey< Element > privateKey, const vector< Ciphertext< Element >> &ciphertext) const
Definition: cryptocontext.h:882
LPEvalKey< Element > MultiAddEvalMultKeys(LPEvalKey< Element > evalKey1, LPEvalKey< Element > evalKey2, const std::string &keyId="")
Definition: cryptocontext.h:1127
LPEvalKey< Element > ReKeyGen(const LPPublicKey< Element > newKey, const LPPrivateKey< Element > oldKey) const
Definition: cryptocontext.h:1156
DecryptResult Decrypt(const LPPrivateKey< Element > privateKey, ConstCiphertext< Element > ciphertext, Plaintext *plaintext)
Definition: cryptocontext.cpp:464
static void ClearEvalSumKeys()
Definition: cryptocontext.cpp:203
Ciphertext< Element > EvalSubMutable(Ciphertext< Element > &ciphertext, Plaintext plaintext) const
Definition: cryptocontext.h:1678
const IntType & GetModulus() const
Definition: cryptocontext.h:780
static bool DeserializeEvalSumKey(std::istream &ser, const ST &sertype)
Definition: cryptocontext.h:560
static bool SerializeEvalMultKey(std::ostream &ser, const ST &sertype, const CryptoContext< Element > cc)
Definition: cryptocontext.h:433
Ciphertext< Element > Encrypt(const LPPublicKey< Element > publicKey, Plaintext plaintext)
Definition: cryptocontext.h:1240
Ciphertext< Element > EvalAdd(ConstCiphertext< Element > ciphertext, ConstPlaintext plaintext) const
Definition: cryptocontext.h:1552
void EvalAddInPlace(Ciphertext< Element > &ct1, ConstCiphertext< Element > ct2) const
Definition: cryptocontext.h:1491
vector< Ciphertext< Element > > MultipartyDecryptLead(const LPPrivateKey< Element > privateKey, const vector< Ciphertext< Element >> &ciphertext) const
Definition: cryptocontext.h:851
shared_ptr< std::map< usint, LPEvalKey< Element > > > MultiEvalSumKeyGen(const LPPrivateKey< Element > privateKey, const shared_ptr< std::map< usint, LPEvalKey< Element >>> eSum, const std::string &keyId="")
Definition: cryptocontext.h:1004
Ciphertext< Element > KeySwitch(const LPEvalKey< Element > keySwitchHint, ConstCiphertext< Element > ciphertext) const
Definition: cryptocontext.h:2380
CryptoContextImpl(shared_ptr< LPCryptoParameters< Element >> params, shared_ptr< LPPublicKeyEncryptionScheme< Element >> scheme, const string &schemeId="Not")
Definition: cryptocontext.h:337
Ciphertext< Element > EvalAdd(ConstCiphertext< Element > ct1, ConstCiphertext< Element > ct2) const
Definition: cryptocontext.h:1477
void TypeCheck(ConstCiphertext< Element > a, ConstPlaintext b, CALLER_INFO_ARGS_HDR) const
Definition: cryptocontext.h:207
static void InsertEvalAutomorphismKey(const shared_ptr< std::map< usint, LPEvalKey< Element >>> mapToInsert)
Definition: cryptocontext.cpp:319
Ciphertext< Element > Compress(ConstCiphertext< Element > ciphertext1, uint32_t numTowers=1) const
Definition: cryptocontext.h:2541
LPEvalKey< Element > MultiKeySwitchGen(const LPPrivateKey< Element > originalPrivateKey, const LPPrivateKey< Element > newPrivateKey, const LPEvalKey< Element > ek) const
Definition: cryptocontext.h:928
static bool SerializeEvalSumKey(std::ostream &ser, const ST &sertype, const CryptoContext< Element > cc)
Definition: cryptocontext.h:534
const EncodingParams GetEncodingParams() const
Definition: cryptocontext.h:753
Ciphertext< Element > EvalMult(ConstCiphertext< Element > ciphertext, double constant) const
Definition: cryptocontext.h:2005
Ciphertext< Element > EvalInnerProduct(ConstCiphertext< Element > ciphertext1, ConstCiphertext< Element > ciphertext2, usint batchSize) const
Definition: cryptocontext.cpp:417
static Plaintext GetPlaintextForDecrypt(PlaintextEncodings pte, shared_ptr< ParmType > evp, EncodingParams ep)
Definition: cryptocontext.cpp:453
usint GetCyclotomicOrder() const
Definition: cryptocontext.h:762
const shared_ptr< LPPublicKeyEncryptionScheme< Element > > GetEncryptionAlgorithm() const
Definition: cryptocontext.h:725
Ciphertext< Element > EvalMultMutable(Ciphertext< Element > &ct1, Plaintext pt2) const
Definition: cryptocontext.h:1991
void SetPrivateKey(const LPPrivateKey< Element > sk)
Definition: cryptocontext.h:267
virtual Plaintext MakeCKKSPackedPlaintext(const std::vector< std::complex< double >> &value, size_t depth=1, uint32_t level=0, const shared_ptr< ParmType > params=nullptr) const
Definition: cryptocontext.h:1364
Ciphertext< Element > EvalAtIndex(ConstCiphertext< Element > ciphertext, int32_t index) const
Definition: cryptocontext.cpp:374
void EvalMultKeysGen(const LPPrivateKey< Element > key)
Definition: cryptocontext.cpp:46
const shared_ptr< ParmType > GetElementParams() const
Definition: cryptocontext.h:745
void EvalAtIndexKeyGen(const LPPrivateKey< Element > privateKey, const std::vector< int32_t > &indexList, const LPPublicKey< Element > publicKey=nullptr)
Definition: cryptocontext.cpp:245
static bool SerializeEvalMultKey(std::ostream &ser, const ST &sertype, string id="")
Definition: cryptocontext-ser.h:55
const IntType & GetRootOfUnity() const
Definition: cryptocontext.h:789
static bool SerializeEvalAutomorphismKey(std::ostream &ser, const ST &sertype, const CryptoContext< Element > cc)
Definition: cryptocontext.h:638
LPKeyPair< Element > MultipartyKeyGen(const vector< LPPrivateKey< Element >> &secretKeys)
Definition: cryptocontext.h:834
void Enable(usint featureMask)
Definition: cryptocontext.h:717
Ciphertext< Element > EvalMultAndRelinearize(ConstCiphertext< Element > ct1, ConstCiphertext< Element > ct2) const
Definition: cryptocontext.h:1868
virtual Ciphertext< Element > EvalPoly(ConstCiphertext< Element > ciphertext, const std::vector< double > &coefficients) const
Definition: cryptocontext.h:2359
LPKeyPair< Element > MultipartyKeyGen(const LPPublicKey< Element > pk, bool makeSparse=false, bool fresh=false)
Definition: cryptocontext.h:816
static const std::map< usint, LPEvalKey< Element > > & GetEvalSumKeyMap(const string &id)
Definition: cryptocontext.cpp:187
virtual Plaintext MakeCKKSPackedPlaintext(const std::vector< double > &value, size_t depth=1, uint32_t level=0, const shared_ptr< ParmType > params=nullptr) const
Definition: cryptocontext.h:1409
Ciphertext< Element > EvalSubMutable(Ciphertext< Element > &ct1, Ciphertext< Element > &ct2) const
Definition: cryptocontext.h:1538
Ciphertext< Element > EvalMult(ConstCiphertext< Element > ct1, ConstPlaintext pt2) const
Definition: cryptocontext.h:1974
static bool SerializeEvalAutomorphismKey(std::ostream &ser, const ST &sertype, string id="")
Definition: cryptocontext.h:611
void PopLastParam()
Removes the last parameter set and adjust the multiplied moduli.
Definition: ildcrtparams.h:283
void EvalSumKeyGen(const LPPrivateKey< Element > privateKey, const LPPublicKey< Element > publicKey=nullptr)
Definition: cryptocontext.cpp:115
Ciphertext< Element > ComposedEvalMult(ConstCiphertext< Element > ciphertext1, ConstCiphertext< Element > ciphertext2) const
Definition: cryptocontext.h:2513
static void ClearEvalMultKeys()
Definition: cryptocontext.cpp:77
shared_ptr< std::map< usint, LPEvalKey< Element > > > MultiEvalAtIndexKeyGen(const LPPrivateKey< Element > privateKey, const shared_ptr< std::map< usint, LPEvalKey< Element >>> eAuto, const std::vector< int32_t > &indexList, const std::string &keyId="")
Definition: cryptocontext.h:979
LPKeyPair< Element > SparseKeyGen()
Definition: cryptocontext.h:1144
Ciphertext< Element > EvalMultMutable(Ciphertext< Element > &ciphertext, double constant) const
Definition: cryptocontext.h:2025
shared_ptr< std::map< usint, LPEvalKey< Element > > > MultiAddEvalSumKeys(const shared_ptr< std::map< usint, LPEvalKey< Element >>> es1, const shared_ptr< std::map< usint, LPEvalKey< Element >>> es2, const std::string &keyId="")
Definition: cryptocontext.h:1064
DecryptResult MultipartyDecryptFusion(const vector< Ciphertext< Element >> &partialCiphertextVec, Plaintext *plaintext) const
Definition: cryptocontext.cpp:521
Ciphertext< Element > LevelReduce(ConstCiphertext< Element > cipherText1, const LPEvalKey< Element > linearKeySwitchHint, size_t levels=1) const
Definition: cryptocontext.h:2488
Ciphertext< Element > ReEncrypt(LPEvalKey< Element > evalKey, ConstCiphertext< Element > ciphertext, const LPPublicKey< Element > publicKey=nullptr) const
Definition: cryptocontext.h:1452
Plaintext MakePackedPlaintext(const vector< int64_t > &value) const
Definition: cryptocontext.h:1323
main implementation class to capture essential cryptoparameters of any LBC system ...
Definition: pubkeylp.h:73
void TypeCheck(ConstCiphertext< Element > a, ConstCiphertext< Element > b, CALLER_INFO_ARGS_HDR) const
Definition: cryptocontext.h:121
Ciphertext< Element > EvalSum(ConstCiphertext< Element > ciphertext, usint batchSize) const
Definition: cryptocontext.cpp:327
static bool DeserializeEvalMultKey(std::istream &ser, const ST &sertype)
Definition: cryptocontext.h:457
shared_ptr< std::map< usint, LPEvalKey< Element > > > EvalAutomorphismKeyGen(const LPPublicKey< Element > publicKey, const LPPrivateKey< Element > origPrivateKey, const std::vector< usint > &indexList) const
Definition: cryptocontext.h:2060
usint GetRingDimension() const
Definition: cryptocontext.h:771
const LPPrivateKey< Element > GetPrivateKey()
Definition: cryptocontext.h:304
Definition: pubkeylp.h:879
Ciphertext< Element > EvalNegate(ConstCiphertext< Element > ct) const
Definition: cryptocontext.h:2041
LPEvalKey< Element > MultiAddEvalKeys(LPEvalKey< Element > a, LPEvalKey< Element > b, const std::string &keyId="")
Definition: cryptocontext.h:1025
Ciphertext< Element > EvalMult(ConstCiphertext< Element > ct1, ConstCiphertext< Element > ct2) const
Definition: cryptocontext.h:1730
Ciphertext< Element > EvalAddMutable(Ciphertext< Element > &ct1, Ciphertext< Element > &ct2) const
Definition: cryptocontext.h:1507
Ciphertext< Element > EvalMerge(const vector< Ciphertext< Element >> &ciphertextVector) const
Definition: cryptocontext.cpp:398
Ciphertext< Element > EvalAutomorphism(ConstCiphertext< Element > ciphertext, usint i, const std::map< usint, LPEvalKey< Element >> &evalKeys, CALLER_INFO_ARGS_HDR) const
Definition: cryptocontext.h:2089
Crypto parameters class for RLWE-based schemes.
Definition: ckks.h:53
double GetScalingFactorOfLevel(uint32_t l=0) const
Definition: ckks.h:440
shared_ptr< std::map< usint, LPEvalKey< Element > > > EvalAutomorphismKeyGen(const LPPrivateKey< Element > privateKey, const std::vector< usint > &indexList) const
Definition: cryptocontext.h:2142
Definition: binfhecontext.h:36
Ciphertext< Element > EvalMult(double constant, ConstCiphertext< Element > ct1) const
Definition: cryptocontext.h:1958
shared_ptr< std::map< usint, LPEvalKey< Element > > > MultiEvalAutomorphismKeyGen(const LPPrivateKey< Element > privateKey, const shared_ptr< std::map< usint, LPEvalKey< Element >>> eAuto, const std::vector< usint > &indexList, const std::string &keyId="")
Definition: cryptocontext.h:953
Ciphertext< Element > ModReduce(ConstCiphertext< Element > ciphertext) const
Definition: cryptocontext.h:2457
Definition: exception.h:126
Abstract interface for public key encryption schemes.
Definition: pubkeylp.h:3181
void Enable(PKESchemeFeature feature)
Definition: cryptocontext.h:711
Parameters for array of ideal lattices (used for Double-CRT).
Definition: backend.h:71
Plaintext MakeCoefPackedPlaintext(const vector< int64_t > &value) const
Definition: cryptocontext.h:1312
Ciphertext< Element > EvalMultNoRelin(ConstCiphertext< Element > ct1, ConstCiphertext< Element > ct2) const
Definition: cryptocontext.h:1774
void EvalMultKeyGen(const LPPrivateKey< Element > key)
Definition: cryptocontext.cpp:33
Definition: exception.h:107
LPPublicKey< Element > MultiAddPubKeys(LPPublicKey< Element > pubKey1, LPPublicKey< Element > pubKey2, const std::string &keyId="")
Definition: cryptocontext.h:1107
Ciphertext< Element > Rescale(ConstCiphertext< Element > ciphertext) const
Definition: cryptocontext.h:2426
static void InsertEvalMultKey(const std::vector< LPEvalKey< Element >> &vectorToInsert)
Definition: cryptocontext.cpp:109