CryptoContext.cc 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. #include "CryptoContext.h"
  2. #include "openfhe/core/utils/caller_info.h" // CALLER_INFO_ARGS_HDR
  3. #include "openfhe/pke/gen-cryptocontext.h"
  4. #include "openfhe/pke/scheme/bfvrns/gen-cryptocontext-bfvrns.h"
  5. #include "openfhe/pke/scheme/bgvrns/gen-cryptocontext-bgvrns.h"
  6. #include "openfhe/pke/scheme/ckksrns/gen-cryptocontext-ckksrns.h"
  7. #include "openfhe/src/lib.rs.h" // ComplexPair
  8. #include "AssociativeContainerOfOpaqueTypes.h"
  9. #include "Ciphertext.h"
  10. #include "CryptoParametersBase.h"
  11. #include "EncodingParams.h"
  12. #include "EvalKey.h"
  13. #include "KeyPair.h"
  14. #include "LWEPrivateKey.h"
  15. #include "Plaintext.h"
  16. #include "PrivateKey.h"
  17. #include "PublicKey.h"
  18. #include "SchemeBase.h"
  19. #include "SequenceContainerOfOpaqueTypes.h"
  20. namespace openfhe
  21. {
  22. using PlaintextImpl = lbcrypto::PlaintextImpl;
  23. CryptoContextDCRTPoly::CryptoContextDCRTPoly(const ParamsBFVRNS& params)
  24. : m_cryptoContextImplSharedPtr(lbcrypto::GenCryptoContext(params))
  25. { }
  26. CryptoContextDCRTPoly::CryptoContextDCRTPoly(const ParamsBGVRNS& params)
  27. : m_cryptoContextImplSharedPtr(lbcrypto::GenCryptoContext(params))
  28. { }
  29. CryptoContextDCRTPoly::CryptoContextDCRTPoly(const ParamsCKKSRNS& params)
  30. : m_cryptoContextImplSharedPtr(lbcrypto::GenCryptoContext(params))
  31. { }
  32. std::unique_ptr<Plaintext> CryptoContextDCRTPoly::MakeCKKSPackedPlaintextByVectorOfComplex(
  33. const std::vector<ComplexPair>& value, const size_t scaleDeg, const uint32_t level,
  34. const std::shared_ptr<DCRTPolyParams> params, const uint32_t slots) const
  35. {
  36. std::vector<std::complex<double>> v;
  37. v.reserve(value.size());
  38. for (const ComplexPair& elem : value)
  39. {
  40. v.emplace_back(elem.re, elem.im);
  41. }
  42. return std::make_unique<Plaintext>(m_cryptoContextImplSharedPtr->MakeCKKSPackedPlaintext(v,
  43. scaleDeg, level, params, slots));
  44. }
  45. void CryptoContextDCRTPoly::SetSchemeId(const SCHEME schemeTag) const
  46. {
  47. m_cryptoContextImplSharedPtr->setSchemeId(schemeTag);
  48. }
  49. SCHEME CryptoContextDCRTPoly::GetSchemeId() const
  50. {
  51. return m_cryptoContextImplSharedPtr->getSchemeId();
  52. }
  53. size_t CryptoContextDCRTPoly::GetKeyGenLevel() const
  54. {
  55. return m_cryptoContextImplSharedPtr->GetKeyGenLevel();
  56. }
  57. void CryptoContextDCRTPoly::SetKeyGenLevel(const size_t level) const
  58. {
  59. m_cryptoContextImplSharedPtr->SetKeyGenLevel(level);
  60. }
  61. void CryptoContextDCRTPoly::SetSwkFC(const CiphertextDCRTPoly& FHEWtoCKKSswk) const
  62. {
  63. m_cryptoContextImplSharedPtr->SetSwkFC(FHEWtoCKKSswk.GetInternal());
  64. }
  65. void CryptoContextDCRTPoly::EvalCompareSwitchPrecompute(const uint32_t pLWE,
  66. const double scaleSign, const bool unit) const
  67. {
  68. m_cryptoContextImplSharedPtr->EvalCompareSwitchPrecompute(pLWE, scaleSign, unit);
  69. }
  70. uint32_t CryptoContextDCRTPoly::FindAutomorphismIndex(const uint32_t idx) const
  71. {
  72. return m_cryptoContextImplSharedPtr->FindAutomorphismIndex(idx);
  73. }
  74. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::GetSwkFC() const
  75. {
  76. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->GetSwkFC());
  77. }
  78. void CryptoContextDCRTPoly::Enable(const PKESchemeFeature feature) const
  79. {
  80. m_cryptoContextImplSharedPtr->Enable(feature);
  81. }
  82. void CryptoContextDCRTPoly::EnableByMask(const uint32_t featureMask) const
  83. {
  84. m_cryptoContextImplSharedPtr->Enable(featureMask);
  85. }
  86. std::unique_ptr<KeyPairDCRTPoly> CryptoContextDCRTPoly::KeyGen() const
  87. {
  88. return std::make_unique<KeyPairDCRTPoly>(m_cryptoContextImplSharedPtr->KeyGen());
  89. }
  90. std::unique_ptr<KeyPairDCRTPoly> CryptoContextDCRTPoly::MultipartyKeyGenByPublicKey(
  91. const PublicKeyDCRTPoly& publicKey, const bool makeSparse, const bool fresh) const
  92. {
  93. return std::make_unique<KeyPairDCRTPoly>(m_cryptoContextImplSharedPtr->MultipartyKeyGen(
  94. publicKey.GetInternal(), makeSparse, fresh));
  95. }
  96. std::unique_ptr<PublicKeyDCRTPoly> CryptoContextDCRTPoly::MultiAddPubKeys(
  97. const PublicKeyDCRTPoly& publicKey1, const PublicKeyDCRTPoly& publicKey2,
  98. const std::string& keyId) const
  99. {
  100. return std::make_unique<PublicKeyDCRTPoly>(m_cryptoContextImplSharedPtr->MultiAddPubKeys(
  101. publicKey1.GetInternal(), publicKey2.GetInternal(), keyId));
  102. }
  103. std::unique_ptr<KeyPairDCRTPoly> CryptoContextDCRTPoly::SparseKeyGen() const
  104. {
  105. return std::make_unique<KeyPairDCRTPoly>(m_cryptoContextImplSharedPtr->SparseKeyGen());
  106. }
  107. void CryptoContextDCRTPoly::EvalMultKeyGen(const PrivateKeyDCRTPoly& key) const
  108. {
  109. m_cryptoContextImplSharedPtr->EvalMultKeyGen(key.GetInternal());
  110. }
  111. void CryptoContextDCRTPoly::EvalMultKeysGen(const PrivateKeyDCRTPoly& key) const
  112. {
  113. m_cryptoContextImplSharedPtr->EvalMultKeysGen(key.GetInternal());
  114. }
  115. void CryptoContextDCRTPoly::EvalRotateKeyGen(const PrivateKeyDCRTPoly& privateKey,
  116. const std::vector<int32_t>& indexList, const PublicKeyDCRTPoly& publicKey) const
  117. {
  118. m_cryptoContextImplSharedPtr->EvalRotateKeyGen(privateKey.GetInternal(), indexList,
  119. publicKey.GetInternal());
  120. }
  121. void CryptoContextDCRTPoly::EvalAtIndexKeyGen(const PrivateKeyDCRTPoly& privateKey,
  122. const std::vector<int32_t>& indexList, const PublicKeyDCRTPoly& publicKey) const
  123. {
  124. m_cryptoContextImplSharedPtr->EvalAtIndexKeyGen(privateKey.GetInternal(), indexList,
  125. publicKey.GetInternal());
  126. }
  127. void CryptoContextDCRTPoly::EvalCKKStoFHEWPrecompute(const double scale) const
  128. {
  129. m_cryptoContextImplSharedPtr->EvalCKKStoFHEWPrecompute(scale);
  130. }
  131. std::unique_ptr<Plaintext> CryptoContextDCRTPoly::MakePackedPlaintext(
  132. const std::vector<int64_t>& value, const size_t noiseScaleDeg,
  133. const uint32_t level) const
  134. {
  135. return std::make_unique<Plaintext>(m_cryptoContextImplSharedPtr->MakePackedPlaintext(value,
  136. noiseScaleDeg, level));
  137. }
  138. std::unique_ptr<Plaintext> CryptoContextDCRTPoly::MakeStringPlaintext(const std::string& s) const
  139. {
  140. return std::make_unique<Plaintext>(m_cryptoContextImplSharedPtr->MakeStringPlaintext(s));
  141. }
  142. std::unique_ptr<Plaintext> CryptoContextDCRTPoly::MakeCoefPackedPlaintext(
  143. const std::vector<int64_t>& value, const size_t noiseScaleDeg, const uint32_t level) const
  144. {
  145. return std::make_unique<Plaintext>(m_cryptoContextImplSharedPtr->MakeCoefPackedPlaintext(value,
  146. noiseScaleDeg, level));
  147. }
  148. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EncryptByPublicKey(
  149. const PublicKeyDCRTPoly& publicKey, const Plaintext& plaintext) const
  150. {
  151. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->Encrypt(
  152. publicKey.GetInternal(), plaintext.GetInternal()));
  153. }
  154. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EncryptByPrivateKey(
  155. const PrivateKeyDCRTPoly& privateKey, const Plaintext& plaintext) const
  156. {
  157. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->Encrypt(
  158. privateKey.GetInternal(), plaintext.GetInternal()));
  159. }
  160. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalAddByCiphertexts(
  161. const CiphertextDCRTPoly& ciphertext1, const CiphertextDCRTPoly& ciphertext2) const
  162. {
  163. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalAdd(
  164. ciphertext1.GetInternal(), ciphertext2.GetInternal()));
  165. }
  166. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalAddByCiphertextAndPlaintext(
  167. const CiphertextDCRTPoly& ciphertext, const Plaintext& plaintext) const
  168. {
  169. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalAdd(
  170. ciphertext.GetInternal(), plaintext.GetInternal()));
  171. }
  172. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalAddByPlaintextAndCiphertext(
  173. const Plaintext& plaintext, const CiphertextDCRTPoly& ciphertext) const
  174. {
  175. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalAdd(
  176. plaintext.GetInternal(), ciphertext.GetInternal()));
  177. }
  178. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalAddByConstAndCiphertext(
  179. const double constant, const CiphertextDCRTPoly& ciphertext) const
  180. {
  181. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalAdd(
  182. constant, ciphertext.GetInternal()));
  183. }
  184. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalAddByCiphertextAndConst(
  185. const CiphertextDCRTPoly& ciphertext, const double constant) const
  186. {
  187. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalAdd(
  188. ciphertext.GetInternal(), constant));
  189. }
  190. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalAddMutableByCiphertexts(
  191. const CiphertextDCRTPoly& ciphertext1, const CiphertextDCRTPoly& ciphertext2) const
  192. {
  193. std::shared_ptr<CiphertextImpl> c1 = ciphertext1.GetInternal();
  194. std::shared_ptr<CiphertextImpl> c2 = ciphertext2.GetInternal();
  195. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalAddMutable(c1,
  196. c2));
  197. }
  198. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalAddMutableByCiphertextAndPlaintext(
  199. const CiphertextDCRTPoly& ciphertext, const Plaintext& plaintext) const
  200. {
  201. std::shared_ptr<CiphertextImpl> c = ciphertext.GetInternal();
  202. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalAddMutable(c,
  203. plaintext.GetInternal()));
  204. }
  205. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalAddMutableByPlaintextAndCiphertext(
  206. const Plaintext& plaintext, const CiphertextDCRTPoly& ciphertext) const
  207. {
  208. std::shared_ptr<CiphertextImpl> c = ciphertext.GetInternal();
  209. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalAddMutable(
  210. plaintext.GetInternal(), c));
  211. }
  212. void CryptoContextDCRTPoly::EvalAddInPlaceByCiphertexts(const CiphertextDCRTPoly& ciphertext1,
  213. const CiphertextDCRTPoly& ciphertext2) const
  214. {
  215. std::shared_ptr<CiphertextImpl> c1 = ciphertext1.GetInternal();
  216. m_cryptoContextImplSharedPtr->EvalAddInPlace(c1, ciphertext2.GetInternal());
  217. }
  218. void CryptoContextDCRTPoly::EvalAddInPlaceByCiphertextAndPlaintext(
  219. const CiphertextDCRTPoly& ciphertext, const Plaintext& plaintext) const
  220. {
  221. std::shared_ptr<CiphertextImpl> c = ciphertext.GetInternal();
  222. m_cryptoContextImplSharedPtr->EvalAddInPlace(c, plaintext.GetInternal());
  223. }
  224. void CryptoContextDCRTPoly::EvalAddInPlaceByPlaintextAndCiphertext(const Plaintext& plaintext,
  225. const CiphertextDCRTPoly& ciphertext) const
  226. {
  227. std::shared_ptr<CiphertextImpl> c = ciphertext.GetInternal();
  228. m_cryptoContextImplSharedPtr->EvalAddInPlace(plaintext.GetInternal(), c);
  229. }
  230. void CryptoContextDCRTPoly::EvalAddInPlaceByCiphertextAndConst(
  231. const CiphertextDCRTPoly& ciphertext, const double constant) const
  232. {
  233. std::shared_ptr<CiphertextImpl> c = ciphertext.GetInternal();
  234. m_cryptoContextImplSharedPtr->EvalAddInPlace(c, constant);
  235. }
  236. void CryptoContextDCRTPoly::EvalAddInPlaceByConstAndCiphertext(const double constant,
  237. const CiphertextDCRTPoly& ciphertext) const
  238. {
  239. std::shared_ptr<CiphertextImpl> c = ciphertext.GetInternal();
  240. m_cryptoContextImplSharedPtr->EvalAddInPlace(constant, c);
  241. }
  242. void CryptoContextDCRTPoly::EvalAddMutableInPlace(const CiphertextDCRTPoly& ciphertext1,
  243. const CiphertextDCRTPoly& ciphertext2) const
  244. {
  245. std::shared_ptr<CiphertextImpl> c1 = ciphertext1.GetInternal();
  246. std::shared_ptr<CiphertextImpl> c2 = ciphertext2.GetInternal();
  247. m_cryptoContextImplSharedPtr->EvalAddMutableInPlace(c1, c2);
  248. }
  249. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalSubByCiphertexts(
  250. const CiphertextDCRTPoly& ciphertext1, const CiphertextDCRTPoly& ciphertext2) const
  251. {
  252. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalSub(
  253. ciphertext1.GetInternal(), ciphertext2.GetInternal()));
  254. }
  255. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalSubByCiphertextAndPlaintext(
  256. const CiphertextDCRTPoly& ciphertext, const Plaintext& plaintext) const
  257. {
  258. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalSub(
  259. ciphertext.GetInternal(), plaintext.GetInternal()));
  260. }
  261. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalSubByPlaintextAndCiphertext(
  262. const Plaintext& plaintext, const CiphertextDCRTPoly& ciphertext) const
  263. {
  264. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalSub(
  265. plaintext.GetInternal(), ciphertext.GetInternal()));
  266. }
  267. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalSubByConstAndCiphertext(
  268. const double constant, const CiphertextDCRTPoly& ciphertext) const
  269. {
  270. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalSub(
  271. constant, ciphertext.GetInternal()));
  272. }
  273. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalSubByCiphertextAndConst(
  274. const CiphertextDCRTPoly& ciphertext, const double constant) const
  275. {
  276. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalSub(
  277. ciphertext.GetInternal(), constant));
  278. }
  279. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalSubMutableByCiphertexts(
  280. const CiphertextDCRTPoly& ciphertext1, const CiphertextDCRTPoly& ciphertext2) const
  281. {
  282. std::shared_ptr<CiphertextImpl> c1 = ciphertext1.GetInternal();
  283. std::shared_ptr<CiphertextImpl> c2 = ciphertext2.GetInternal();
  284. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalSubMutable(c1,
  285. c2));
  286. }
  287. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalSubMutableByCiphertextAndPlaintext(
  288. const CiphertextDCRTPoly& ciphertext, const Plaintext& plaintext) const
  289. {
  290. std::shared_ptr<CiphertextImpl> c = ciphertext.GetInternal();
  291. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalSubMutable(c,
  292. plaintext.GetInternal()));
  293. }
  294. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalSubMutableByPlaintextAndCiphertext(
  295. const Plaintext& plaintext, const CiphertextDCRTPoly& ciphertext) const
  296. {
  297. std::shared_ptr<CiphertextImpl> c = ciphertext.GetInternal();
  298. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalSubMutable(
  299. plaintext.GetInternal(), c));
  300. }
  301. void CryptoContextDCRTPoly::EvalSubInPlaceByCiphertexts(const CiphertextDCRTPoly& ciphertext1,
  302. const CiphertextDCRTPoly& ciphertext2) const
  303. {
  304. std::shared_ptr<CiphertextImpl> c1 = ciphertext1.GetInternal();
  305. m_cryptoContextImplSharedPtr->EvalSubInPlace(c1, ciphertext2.GetInternal());
  306. }
  307. void CryptoContextDCRTPoly::EvalSubInPlaceByCiphertextAndConst(
  308. const CiphertextDCRTPoly& ciphertext, const double constant) const
  309. {
  310. std::shared_ptr<CiphertextImpl> c = ciphertext.GetInternal();
  311. m_cryptoContextImplSharedPtr->EvalSubInPlace(c, constant);
  312. }
  313. void CryptoContextDCRTPoly::EvalSubInPlaceByConstAndCiphertext(const double constant,
  314. const CiphertextDCRTPoly& ciphertext) const
  315. {
  316. std::shared_ptr<CiphertextImpl> c = ciphertext.GetInternal();
  317. m_cryptoContextImplSharedPtr->EvalSubInPlace(constant, c);
  318. }
  319. void CryptoContextDCRTPoly::EvalSubMutableInPlace(const CiphertextDCRTPoly& ciphertext1,
  320. const CiphertextDCRTPoly& ciphertext2) const
  321. {
  322. std::shared_ptr<CiphertextImpl> c1 = ciphertext1.GetInternal();
  323. std::shared_ptr<CiphertextImpl> c2 = ciphertext2.GetInternal();
  324. m_cryptoContextImplSharedPtr->EvalSubMutableInPlace(c1, c2);
  325. }
  326. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalMultByCiphertexts(
  327. const CiphertextDCRTPoly& ciphertext1, const CiphertextDCRTPoly& ciphertext2) const
  328. {
  329. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalMult(
  330. ciphertext1.GetInternal(), ciphertext2.GetInternal()));
  331. }
  332. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalMultByCiphertextAndPlaintext(
  333. const CiphertextDCRTPoly& ciphertext, const Plaintext& plaintext) const
  334. {
  335. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalMult(
  336. ciphertext.GetInternal(), plaintext.GetInternal()));
  337. }
  338. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalMultByPlaintextAndCiphertext(
  339. const Plaintext& plaintext, const CiphertextDCRTPoly& ciphertext) const
  340. {
  341. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalMult(
  342. plaintext.GetInternal(), ciphertext.GetInternal()));
  343. }
  344. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalMultByConstAndCiphertext(
  345. const double constant, const CiphertextDCRTPoly& ciphertext) const
  346. {
  347. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalMult(
  348. constant, ciphertext.GetInternal()));
  349. }
  350. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalMultByCiphertextAndConst(
  351. const CiphertextDCRTPoly& ciphertext, const double constant) const
  352. {
  353. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalMult(
  354. ciphertext.GetInternal(), constant));
  355. }
  356. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalMultMutableByCiphertexts(
  357. const CiphertextDCRTPoly& ciphertext1, const CiphertextDCRTPoly& ciphertext2) const
  358. {
  359. std::shared_ptr<CiphertextImpl> c1 = ciphertext1.GetInternal();
  360. std::shared_ptr<CiphertextImpl> c2 = ciphertext2.GetInternal();
  361. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalMultMutable(c1,
  362. c2));
  363. }
  364. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalMultMutableByCiphertextAndPlaintext(
  365. const CiphertextDCRTPoly& ciphertext, const Plaintext& plaintext) const
  366. {
  367. std::shared_ptr<CiphertextImpl> c = ciphertext.GetInternal();
  368. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalMultMutable(c,
  369. plaintext.GetInternal()));
  370. }
  371. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalMultMutableByPlaintextAndCiphertext(
  372. const Plaintext& plaintext, const CiphertextDCRTPoly& ciphertext) const
  373. {
  374. std::shared_ptr<CiphertextImpl> c = ciphertext.GetInternal();
  375. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalMultMutable(
  376. plaintext.GetInternal(), c));
  377. }
  378. void CryptoContextDCRTPoly::EvalMultInPlaceByCiphertextAndConst(
  379. const CiphertextDCRTPoly& ciphertext, const double constant) const
  380. {
  381. std::shared_ptr<CiphertextImpl> c = ciphertext.GetInternal();
  382. m_cryptoContextImplSharedPtr->EvalMultInPlace(c, constant);
  383. }
  384. void CryptoContextDCRTPoly::EvalMultInPlaceByConstAndCiphertext(const double constant,
  385. const CiphertextDCRTPoly& ciphertext) const
  386. {
  387. std::shared_ptr<CiphertextImpl> c = ciphertext.GetInternal();
  388. m_cryptoContextImplSharedPtr->EvalMultInPlace(constant, c);
  389. }
  390. void CryptoContextDCRTPoly::EvalMultMutableInPlace(const CiphertextDCRTPoly& ciphertext1,
  391. const CiphertextDCRTPoly& ciphertext2) const
  392. {
  393. std::shared_ptr<CiphertextImpl> c1 = ciphertext1.GetInternal();
  394. std::shared_ptr<CiphertextImpl> c2 = ciphertext2.GetInternal();
  395. m_cryptoContextImplSharedPtr->EvalMultMutableInPlace(c1, c2);
  396. }
  397. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalMultNoRelin(
  398. const CiphertextDCRTPoly& ciphertext1, const CiphertextDCRTPoly& ciphertext2) const
  399. {
  400. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalMultNoRelin(
  401. ciphertext1.GetInternal(), ciphertext2.GetInternal()));
  402. }
  403. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalMultAndRelinearize(
  404. const CiphertextDCRTPoly& ciphertext1, const CiphertextDCRTPoly& ciphertext2) const
  405. {
  406. return std::make_unique<CiphertextDCRTPoly>(
  407. m_cryptoContextImplSharedPtr->EvalMultAndRelinearize(ciphertext1.GetInternal(),
  408. ciphertext2.GetInternal()));
  409. }
  410. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalRotate(
  411. const CiphertextDCRTPoly& ciphertext, const int32_t index) const
  412. {
  413. return std::make_unique<CiphertextDCRTPoly>(
  414. m_cryptoContextImplSharedPtr->EvalRotate(ciphertext.GetInternal(), index));
  415. }
  416. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalChebyshevSeries(
  417. const CiphertextDCRTPoly& ciphertext, const std::vector<double>& coefficients,
  418. const double a, const double b) const
  419. {
  420. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalChebyshevSeries(
  421. ciphertext.GetInternal(), coefficients, a, b));
  422. }
  423. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalChebyshevFunction(
  424. rust::Fn<void(const double x, double& ret)> func, const CiphertextDCRTPoly& ciphertext,
  425. const double a, const double b, const uint32_t degree) const
  426. {
  427. return std::make_unique<CiphertextDCRTPoly>(
  428. m_cryptoContextImplSharedPtr->EvalChebyshevFunction([&](const double x){
  429. double result; func(x, result); return result; }, ciphertext.GetInternal(), a, b, degree));
  430. }
  431. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalCompareSchemeSwitching(
  432. const CiphertextDCRTPoly& ciphertext1, const CiphertextDCRTPoly& ciphertext2,
  433. const uint32_t numCtxts, const uint32_t numSlots, const uint32_t pLWE, const double scaleSign,
  434. const bool unit) const
  435. {
  436. return std::make_unique<CiphertextDCRTPoly>(
  437. m_cryptoContextImplSharedPtr->EvalCompareSchemeSwitching(ciphertext1.GetInternal(),
  438. ciphertext2.GetInternal(), numCtxts, numSlots, pLWE, scaleSign, unit));
  439. }
  440. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::Rescale(
  441. const CiphertextDCRTPoly& ciphertext) const
  442. {
  443. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->Rescale(
  444. ciphertext.GetInternal()));
  445. }
  446. void CryptoContextDCRTPoly::RescaleInPlace(const CiphertextDCRTPoly& ciphertext) const
  447. {
  448. std::shared_ptr<CiphertextImpl> c = ciphertext.GetInternal();
  449. m_cryptoContextImplSharedPtr->RescaleInPlace(c);
  450. }
  451. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::ModReduce(
  452. const CiphertextDCRTPoly& ciphertext) const
  453. {
  454. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->ModReduce(
  455. ciphertext.GetInternal()));
  456. }
  457. void CryptoContextDCRTPoly::ModReduceInPlace(const CiphertextDCRTPoly& ciphertext) const
  458. {
  459. std::shared_ptr<CiphertextImpl> c = ciphertext.GetInternal();
  460. m_cryptoContextImplSharedPtr->ModReduceInPlace(c);
  461. }
  462. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalSum(
  463. const CiphertextDCRTPoly& ciphertext, const uint32_t batchSize) const
  464. {
  465. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalSum(
  466. ciphertext.GetInternal(), batchSize));
  467. }
  468. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalPolyLinear(
  469. const CiphertextDCRTPoly& ciphertext, const std::vector<double>& coefficients) const
  470. {
  471. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalPolyLinear(
  472. ciphertext.GetInternal(), coefficients));
  473. }
  474. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalPolyPS(
  475. const CiphertextDCRTPoly& ciphertext, const std::vector<double>& coefficients) const
  476. {
  477. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalPolyPS(
  478. ciphertext.GetInternal(), coefficients));
  479. }
  480. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalChebyshevSeriesLinear(
  481. const CiphertextDCRTPoly& ciphertext, const std::vector<double>& coefficients,
  482. const double a, const double b) const
  483. {
  484. return std::make_unique<CiphertextDCRTPoly>(
  485. m_cryptoContextImplSharedPtr->EvalChebyshevSeriesLinear(ciphertext.GetInternal(),
  486. coefficients, a, b));
  487. }
  488. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalChebyshevSeriesPS(
  489. const CiphertextDCRTPoly& ciphertext, const std::vector<double>& coefficients,
  490. const double a, const double b) const
  491. {
  492. return std::make_unique<CiphertextDCRTPoly>(
  493. m_cryptoContextImplSharedPtr->EvalChebyshevSeriesPS(ciphertext.GetInternal(), coefficients,
  494. a, b));
  495. }
  496. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalDivide(
  497. const CiphertextDCRTPoly& ciphertext, const double a, const double b,
  498. const uint32_t degree) const
  499. {
  500. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalDivide(
  501. ciphertext.GetInternal(), a, b, degree));
  502. }
  503. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::IntMPBootAdjustScale(
  504. const CiphertextDCRTPoly& ciphertext) const
  505. {
  506. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->IntMPBootAdjustScale(
  507. ciphertext.GetInternal()));
  508. }
  509. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalLogistic(
  510. const CiphertextDCRTPoly& ciphertext, const double a, const double b,
  511. const uint32_t degree) const
  512. {
  513. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalLogistic(
  514. ciphertext.GetInternal(), a, b, degree));
  515. }
  516. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::IntMPBootRandomElementGen(
  517. const PublicKeyDCRTPoly& publicKey) const
  518. {
  519. return std::make_unique<CiphertextDCRTPoly>(
  520. m_cryptoContextImplSharedPtr->IntMPBootRandomElementGen(publicKey.GetInternal()));
  521. }
  522. void CryptoContextDCRTPoly::EvalBootstrapSetup(
  523. const std::vector<uint32_t>& levelBudget, const std::vector<uint32_t>& dim1,
  524. const uint32_t slots, const uint32_t correctionFactor, const bool precompute) const
  525. {
  526. m_cryptoContextImplSharedPtr->EvalBootstrapSetup(levelBudget, dim1, slots, correctionFactor,
  527. precompute);
  528. }
  529. void CryptoContextDCRTPoly::EvalBootstrapKeyGen(const PrivateKeyDCRTPoly& privateKey,
  530. const uint32_t slots) const
  531. {
  532. m_cryptoContextImplSharedPtr->EvalBootstrapKeyGen(privateKey.GetInternal(), slots);
  533. }
  534. void CryptoContextDCRTPoly::EvalBootstrapPrecompute(const uint32_t slots) const
  535. {
  536. m_cryptoContextImplSharedPtr->EvalBootstrapPrecompute(slots);
  537. }
  538. std::unique_ptr<DecryptResult> CryptoContextDCRTPoly::DecryptByPrivateKeyAndCiphertext(
  539. const PrivateKeyDCRTPoly& privateKey, const CiphertextDCRTPoly& ciphertext,
  540. Plaintext& plaintext) const
  541. {
  542. std::shared_ptr<PlaintextImpl> p;
  543. std::unique_ptr<DecryptResult> res = std::make_unique<DecryptResult>(
  544. m_cryptoContextImplSharedPtr->Decrypt(privateKey.GetInternal(), ciphertext.GetInternal(), &p));
  545. plaintext = p;
  546. return res;
  547. }
  548. std::unique_ptr<DecryptResult> CryptoContextDCRTPoly::DecryptByCiphertextAndPrivateKey(
  549. const CiphertextDCRTPoly& ciphertext, const PrivateKeyDCRTPoly& privateKey,
  550. Plaintext& plaintext) const
  551. {
  552. std::shared_ptr<PlaintextImpl> p;
  553. std::unique_ptr<DecryptResult> res = std::make_unique<DecryptResult>(
  554. m_cryptoContextImplSharedPtr->Decrypt(ciphertext.GetInternal(), privateKey.GetInternal(), &p));
  555. plaintext = p;
  556. return res;
  557. }
  558. std::unique_ptr<DecryptResult> CryptoContextDCRTPoly::MultipartyDecryptFusion(
  559. const VectorOfCiphertexts& partialCiphertextVec, Plaintext& plaintext) const
  560. {
  561. std::shared_ptr<PlaintextImpl> p;
  562. std::unique_ptr<DecryptResult> res = std::make_unique<DecryptResult>(
  563. m_cryptoContextImplSharedPtr->MultipartyDecryptFusion(partialCiphertextVec.GetInternal(), &p));
  564. plaintext = p;
  565. return res;
  566. }
  567. uint32_t CryptoContextDCRTPoly::GetRingDimension() const
  568. {
  569. return m_cryptoContextImplSharedPtr->GetRingDimension();
  570. }
  571. uint32_t CryptoContextDCRTPoly::GetCyclotomicOrder() const
  572. {
  573. return m_cryptoContextImplSharedPtr->GetCyclotomicOrder();
  574. }
  575. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalSin(
  576. const CiphertextDCRTPoly& ciphertext,
  577. const double a, const double b, const uint32_t degree) const
  578. {
  579. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalSin(
  580. ciphertext.GetInternal(), a, b, degree));
  581. }
  582. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalCos(
  583. const CiphertextDCRTPoly& ciphertext,
  584. const double a, const double b, const uint32_t degree) const
  585. {
  586. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalCos(
  587. ciphertext.GetInternal(), a, b, degree));
  588. }
  589. std::unique_ptr<Plaintext> CryptoContextDCRTPoly::MakeCKKSPackedPlaintext(
  590. const std::vector<double>& value, const size_t scaleDeg, const uint32_t level,
  591. const std::shared_ptr<DCRTPolyParams> params, const uint32_t slots) const
  592. {
  593. return std::make_unique<Plaintext>(m_cryptoContextImplSharedPtr->MakeCKKSPackedPlaintext(
  594. value, scaleDeg, level, params, slots));
  595. }
  596. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalPoly(
  597. const CiphertextDCRTPoly& ciphertext, const std::vector<double>& coefficients) const
  598. {
  599. return std::make_unique<CiphertextDCRTPoly>(
  600. m_cryptoContextImplSharedPtr->EvalPoly(ciphertext.GetInternal(), coefficients));
  601. }
  602. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::KeySwitchDown(
  603. const CiphertextDCRTPoly& ciphertext) const
  604. {
  605. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->KeySwitchDown(
  606. ciphertext.GetInternal()));
  607. }
  608. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::KeySwitchExt(
  609. const CiphertextDCRTPoly& ciphertext, const bool addFirst) const
  610. {
  611. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->KeySwitchExt(
  612. ciphertext.GetInternal(), addFirst));
  613. }
  614. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::Compress(
  615. const CiphertextDCRTPoly& ciphertext, const uint32_t towersLeft) const
  616. {
  617. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->Compress(
  618. ciphertext.GetInternal(), towersLeft));
  619. }
  620. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalNegate(
  621. const CiphertextDCRTPoly& ciphertext) const
  622. {
  623. return std::make_unique<CiphertextDCRTPoly>(
  624. m_cryptoContextImplSharedPtr->EvalNegate(ciphertext.GetInternal()));
  625. }
  626. void CryptoContextDCRTPoly::EvalNegateInPlace(const CiphertextDCRTPoly& ciphertext) const
  627. {
  628. std::shared_ptr<CiphertextImpl> c = ciphertext.GetInternal();
  629. m_cryptoContextImplSharedPtr->EvalNegateInPlace(c);
  630. }
  631. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalSquare(
  632. const CiphertextDCRTPoly& ciphertext) const
  633. {
  634. return std::make_unique<CiphertextDCRTPoly>(
  635. m_cryptoContextImplSharedPtr->EvalSquare(ciphertext.GetInternal()));
  636. }
  637. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalSquareMutable(
  638. const CiphertextDCRTPoly& ciphertext) const
  639. {
  640. std::shared_ptr<CiphertextImpl> c = ciphertext.GetInternal();
  641. return std::make_unique<CiphertextDCRTPoly>(
  642. m_cryptoContextImplSharedPtr->EvalSquareMutable(c));
  643. }
  644. void CryptoContextDCRTPoly::EvalSquareInPlace(const CiphertextDCRTPoly& ciphertext) const
  645. {
  646. std::shared_ptr<CiphertextImpl> c = ciphertext.GetInternal();
  647. m_cryptoContextImplSharedPtr->EvalSquareInPlace(c);
  648. }
  649. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalAtIndex(
  650. const CiphertextDCRTPoly& ciphertext, const uint32_t index) const
  651. {
  652. return std::make_unique<CiphertextDCRTPoly>(
  653. m_cryptoContextImplSharedPtr->EvalAtIndex(ciphertext.GetInternal(), index));
  654. }
  655. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::ComposedEvalMult(
  656. const CiphertextDCRTPoly& ciphertext1, const CiphertextDCRTPoly& ciphertext2) const
  657. {
  658. return std::make_unique<CiphertextDCRTPoly>(
  659. m_cryptoContextImplSharedPtr->ComposedEvalMult(ciphertext1.GetInternal(),
  660. ciphertext2.GetInternal()));
  661. }
  662. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::Relinearize(
  663. const CiphertextDCRTPoly& ciphertext) const
  664. {
  665. return std::make_unique<CiphertextDCRTPoly>(
  666. m_cryptoContextImplSharedPtr->Relinearize(ciphertext.GetInternal()));
  667. }
  668. void CryptoContextDCRTPoly::RelinearizeInPlace(const CiphertextDCRTPoly& ciphertext) const
  669. {
  670. std::shared_ptr<CiphertextImpl> c = ciphertext.GetInternal();
  671. m_cryptoContextImplSharedPtr->RelinearizeInPlace(c);
  672. }
  673. std::unique_ptr<std::vector<uint32_t>> CryptoContextDCRTPoly::FindAutomorphismIndices(
  674. const std::vector<uint32_t>& idxList) const
  675. {
  676. return std::make_unique<std::vector<uint32_t>>(
  677. m_cryptoContextImplSharedPtr->FindAutomorphismIndices(idxList));
  678. }
  679. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalInnerProductByCiphertexts(
  680. const CiphertextDCRTPoly& ciphertext1, const CiphertextDCRTPoly& ciphertext2,
  681. const uint32_t batchSize) const
  682. {
  683. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalInnerProduct(
  684. ciphertext1.GetInternal(), ciphertext2.GetInternal(), batchSize));
  685. }
  686. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalInnerProductByPlaintext(
  687. const CiphertextDCRTPoly& ciphertext, const Plaintext& plaintext,
  688. const uint32_t batchSize) const
  689. {
  690. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalInnerProduct(
  691. ciphertext.GetInternal(), plaintext.GetInternal(), batchSize));
  692. }
  693. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::KeySwitch(
  694. const CiphertextDCRTPoly& ciphertext, const EvalKeyDCRTPoly& evalKey) const
  695. {
  696. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->KeySwitch(
  697. ciphertext.GetInternal(), evalKey.GetInternal()));
  698. }
  699. void CryptoContextDCRTPoly::KeySwitchInPlace(const CiphertextDCRTPoly& ciphertext,
  700. const EvalKeyDCRTPoly& evalKey) const
  701. {
  702. std::shared_ptr<CiphertextImpl> c = ciphertext.GetInternal();
  703. m_cryptoContextImplSharedPtr->KeySwitchInPlace(c, evalKey.GetInternal());
  704. }
  705. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::LevelReduce(
  706. const CiphertextDCRTPoly& ciphertext, const EvalKeyDCRTPoly& evalKey,
  707. const size_t levels) const
  708. {
  709. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->LevelReduce(
  710. ciphertext.GetInternal(), evalKey.GetInternal(), levels));
  711. }
  712. void CryptoContextDCRTPoly::LevelReduceInPlace(const CiphertextDCRTPoly& ciphertext,
  713. const EvalKeyDCRTPoly& evalKey, const size_t levels) const
  714. {
  715. std::shared_ptr<CiphertextImpl> c = ciphertext.GetInternal();
  716. m_cryptoContextImplSharedPtr->LevelReduceInPlace(c, evalKey.GetInternal(), levels);
  717. }
  718. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::ReEncrypt(
  719. const CiphertextDCRTPoly& ciphertext, const EvalKeyDCRTPoly& evalKey,
  720. const PublicKeyDCRTPoly& publicKey) const
  721. {
  722. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->ReEncrypt(
  723. ciphertext.GetInternal(), evalKey.GetInternal(), publicKey.GetInternal()));
  724. }
  725. std::unique_ptr<EvalKeyDCRTPoly> CryptoContextDCRTPoly::KeySwitchGen(
  726. const PrivateKeyDCRTPoly& oldPrivateKey, const PrivateKeyDCRTPoly& newPrivateKey) const
  727. {
  728. return std::make_unique<EvalKeyDCRTPoly>(m_cryptoContextImplSharedPtr->KeySwitchGen(
  729. oldPrivateKey.GetInternal(), newPrivateKey.GetInternal()));
  730. }
  731. std::unique_ptr<EvalKeyDCRTPoly> CryptoContextDCRTPoly::ReKeyGen(
  732. const PrivateKeyDCRTPoly& oldPrivateKey, const PublicKeyDCRTPoly& newPublicKey) const
  733. {
  734. return std::make_unique<EvalKeyDCRTPoly>(m_cryptoContextImplSharedPtr->ReKeyGen(
  735. oldPrivateKey.GetInternal(), newPublicKey.GetInternal()));
  736. }
  737. std::unique_ptr<EvalKeyDCRTPoly> CryptoContextDCRTPoly::MultiKeySwitchGen(
  738. const PrivateKeyDCRTPoly& originalPrivateKey, const PrivateKeyDCRTPoly& newPrivateKey,
  739. const EvalKeyDCRTPoly& evalKey) const
  740. {
  741. return std::make_unique<EvalKeyDCRTPoly>(m_cryptoContextImplSharedPtr->MultiKeySwitchGen(
  742. originalPrivateKey.GetInternal(), newPrivateKey.GetInternal(), evalKey.GetInternal()));
  743. }
  744. std::unique_ptr<EvalKeyDCRTPoly> CryptoContextDCRTPoly::MultiAddEvalKeys(
  745. const EvalKeyDCRTPoly& evalKey1, const EvalKeyDCRTPoly& evalKey2,
  746. const std::string& keyId) const
  747. {
  748. return std::make_unique<EvalKeyDCRTPoly>(m_cryptoContextImplSharedPtr->MultiAddEvalKeys(
  749. evalKey1.GetInternal(), evalKey2.GetInternal(), keyId));
  750. }
  751. std::unique_ptr<EvalKeyDCRTPoly> CryptoContextDCRTPoly::MultiMultEvalKey(
  752. const PrivateKeyDCRTPoly& privateKey, const EvalKeyDCRTPoly& evalKey,
  753. const std::string& keyId) const
  754. {
  755. return std::make_unique<EvalKeyDCRTPoly>(m_cryptoContextImplSharedPtr->MultiMultEvalKey(
  756. privateKey.GetInternal(), evalKey.GetInternal(), keyId));
  757. }
  758. std::unique_ptr<EvalKeyDCRTPoly> CryptoContextDCRTPoly::MultiAddEvalMultKeys(
  759. const EvalKeyDCRTPoly& evalKey1, const EvalKeyDCRTPoly& evalKey2,
  760. const std::string& keyId) const
  761. {
  762. return std::make_unique<EvalKeyDCRTPoly>(m_cryptoContextImplSharedPtr->MultiAddEvalMultKeys(
  763. evalKey1.GetInternal(), evalKey2.GetInternal(), keyId));
  764. }
  765. void CryptoContextDCRTPoly::EvalSumKeyGen(const PrivateKeyDCRTPoly& privateKey,
  766. const PublicKeyDCRTPoly& publicKey) const
  767. {
  768. m_cryptoContextImplSharedPtr->EvalSumKeyGen(privateKey.GetInternal(), publicKey.GetInternal());
  769. }
  770. void CryptoContextDCRTPoly::EvalCKKStoFHEWKeyGen(const KeyPairDCRTPoly& keyPair,
  771. const LWEPrivateKey& lwesk) const
  772. {
  773. m_cryptoContextImplSharedPtr->EvalCKKStoFHEWKeyGen({keyPair.GetPublicKey()->GetInternal(),
  774. keyPair.GetPrivateKey()->GetInternal()}, lwesk.GetInternal());
  775. }
  776. void CryptoContextDCRTPoly::EvalFHEWtoCKKSKeyGen(const KeyPairDCRTPoly& keyPair,
  777. const LWEPrivateKey& lwesk, const uint32_t numSlots, const uint32_t numCtxts,
  778. const uint32_t dim1, const uint32_t L) const
  779. {
  780. m_cryptoContextImplSharedPtr->EvalFHEWtoCKKSKeyGen({keyPair.GetPublicKey()->GetInternal(),
  781. keyPair.GetPrivateKey()->GetInternal()}, lwesk.GetInternal(), numSlots, numCtxts, dim1, L);
  782. }
  783. void CryptoContextDCRTPoly::EvalSchemeSwitchingKeyGen(const KeyPairDCRTPoly& keyPair,
  784. const LWEPrivateKey& lwesk) const
  785. {
  786. m_cryptoContextImplSharedPtr->EvalSchemeSwitchingKeyGen({keyPair.GetPublicKey()->GetInternal(),
  787. keyPair.GetPrivateKey()->GetInternal()}, lwesk.GetInternal());
  788. }
  789. uint64_t CryptoContextDCRTPoly::GetModulus() const
  790. {
  791. return m_cryptoContextImplSharedPtr->GetModulus().ConvertToInt();
  792. }
  793. uint64_t CryptoContextDCRTPoly::GetRootOfUnity() const
  794. {
  795. return m_cryptoContextImplSharedPtr->GetRootOfUnity().ConvertToInt();
  796. }
  797. std::unique_ptr<VectorOfCiphertexts> CryptoContextDCRTPoly::MultipartyDecryptLead(
  798. const VectorOfCiphertexts& ciphertextVec, const PrivateKeyDCRTPoly& privateKey) const
  799. {
  800. return std::make_unique<VectorOfCiphertexts>(
  801. m_cryptoContextImplSharedPtr->MultipartyDecryptLead(ciphertextVec.GetInternal(),
  802. privateKey.GetInternal()));
  803. }
  804. std::unique_ptr<VectorOfCiphertexts> CryptoContextDCRTPoly::MultipartyDecryptMain(
  805. const VectorOfCiphertexts& ciphertextVec, const PrivateKeyDCRTPoly& privateKey) const
  806. {
  807. return std::make_unique<VectorOfCiphertexts>(
  808. m_cryptoContextImplSharedPtr->MultipartyDecryptMain(ciphertextVec.GetInternal(),
  809. privateKey.GetInternal()));
  810. }
  811. std::unique_ptr<VectorOfCiphertexts> CryptoContextDCRTPoly::IntMPBootDecrypt(
  812. const PrivateKeyDCRTPoly& privateKey, const CiphertextDCRTPoly& ciphertext,
  813. const CiphertextDCRTPoly& a) const
  814. {
  815. return std::make_unique<VectorOfCiphertexts>(m_cryptoContextImplSharedPtr->IntMPBootDecrypt(
  816. privateKey.GetInternal(), ciphertext.GetInternal(), a.GetInternal()));
  817. }
  818. std::unique_ptr<VectorOfCiphertexts> CryptoContextDCRTPoly::EvalMinSchemeSwitching(
  819. const CiphertextDCRTPoly& ciphertext, const PublicKeyDCRTPoly& publicKey,
  820. const uint32_t numValues, const uint32_t numSlots, const uint32_t pLWE,
  821. const double scaleSign) const
  822. {
  823. return std::make_unique<VectorOfCiphertexts>(
  824. m_cryptoContextImplSharedPtr->EvalMinSchemeSwitching(ciphertext.GetInternal(),
  825. publicKey.GetInternal(), numValues, numSlots, pLWE, scaleSign));
  826. }
  827. std::unique_ptr<VectorOfCiphertexts> CryptoContextDCRTPoly::EvalMinSchemeSwitchingAlt(
  828. const CiphertextDCRTPoly& ciphertext, const PublicKeyDCRTPoly& publicKey,
  829. const uint32_t numValues, const uint32_t numSlots, const uint32_t pLWE,
  830. const double scaleSign) const
  831. {
  832. return std::make_unique<VectorOfCiphertexts>(
  833. m_cryptoContextImplSharedPtr->EvalMinSchemeSwitchingAlt(ciphertext.GetInternal(),
  834. publicKey.GetInternal(), numValues, numSlots, pLWE, scaleSign));
  835. }
  836. std::unique_ptr<VectorOfCiphertexts> CryptoContextDCRTPoly::EvalMaxSchemeSwitching(
  837. const CiphertextDCRTPoly& ciphertext, const PublicKeyDCRTPoly& publicKey,
  838. const uint32_t numValues, const uint32_t numSlots, const uint32_t pLWE,
  839. const double scaleSign) const
  840. {
  841. return std::make_unique<VectorOfCiphertexts>(
  842. m_cryptoContextImplSharedPtr->EvalMaxSchemeSwitching(ciphertext.GetInternal(),
  843. publicKey.GetInternal(), numValues, numSlots, pLWE, scaleSign));
  844. }
  845. std::unique_ptr<VectorOfCiphertexts> CryptoContextDCRTPoly::EvalMaxSchemeSwitchingAlt(
  846. const CiphertextDCRTPoly& ciphertext, const PublicKeyDCRTPoly& publicKey,
  847. const uint32_t numValues, const uint32_t numSlots, const uint32_t pLWE,
  848. const double scaleSign) const
  849. {
  850. return std::make_unique<VectorOfCiphertexts>(
  851. m_cryptoContextImplSharedPtr->EvalMaxSchemeSwitchingAlt(ciphertext.GetInternal(),
  852. publicKey.GetInternal(), numValues, numSlots, pLWE, scaleSign));
  853. }
  854. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalAddMany(
  855. const VectorOfCiphertexts& ciphertextVec) const
  856. {
  857. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalAddMany(
  858. ciphertextVec.GetInternal()));
  859. }
  860. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalMultMany(
  861. const VectorOfCiphertexts& ciphertextVec) const
  862. {
  863. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalMultMany(
  864. ciphertextVec.GetInternal()));
  865. }
  866. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalMerge(
  867. const VectorOfCiphertexts& ciphertextVec) const
  868. {
  869. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalMerge(
  870. ciphertextVec.GetInternal()));
  871. }
  872. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::IntMPBootEncrypt(
  873. const PublicKeyDCRTPoly& publicKey, const VectorOfCiphertexts& sharesPair,
  874. const CiphertextDCRTPoly& a, const CiphertextDCRTPoly& ciphertext) const
  875. {
  876. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->IntMPBootEncrypt(
  877. publicKey.GetInternal(), sharesPair.GetInternal(), a.GetInternal(),
  878. ciphertext.GetInternal()));
  879. }
  880. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalAddManyInPlace(
  881. VectorOfCiphertexts& ciphertextVec) const
  882. {
  883. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalAddManyInPlace(
  884. ciphertextVec.GetInternal()));
  885. }
  886. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalLinearWSumMutable(
  887. VectorOfCiphertexts& ciphertextVec, const std::vector<double>& constantsVec) const
  888. {
  889. return std::make_unique<CiphertextDCRTPoly>(
  890. m_cryptoContextImplSharedPtr->EvalLinearWSumMutable(ciphertextVec.GetInternal(),
  891. constantsVec));
  892. }
  893. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalLinearWSumMutable(
  894. const std::vector<double>& constantsVec, VectorOfCiphertexts& ciphertextVec) const
  895. {
  896. return std::make_unique<CiphertextDCRTPoly>(
  897. m_cryptoContextImplSharedPtr->EvalLinearWSumMutable(constantsVec,
  898. ciphertextVec.GetInternal()));
  899. }
  900. std::unique_ptr<KeyPairDCRTPoly> CryptoContextDCRTPoly::MultipartyKeyGenByVectorOfPrivateKeys(
  901. const VectorOfPrivateKeys& privateKeyVec) const
  902. {
  903. return std::make_unique<KeyPairDCRTPoly>(m_cryptoContextImplSharedPtr->MultipartyKeyGen(
  904. privateKeyVec.GetInternal()));
  905. }
  906. std::unique_ptr<UnorderedMapFromIndexToDCRTPoly> CryptoContextDCRTPoly::ShareKeys(
  907. const PrivateKeyDCRTPoly& sk, const uint32_t N, const uint32_t threshold, const uint32_t index,
  908. const std::string& shareType) const
  909. {
  910. return std::make_unique<UnorderedMapFromIndexToDCRTPoly>(
  911. m_cryptoContextImplSharedPtr->ShareKeys(sk.GetInternal(), N, threshold, index, shareType));
  912. }
  913. void CryptoContextDCRTPoly::RecoverSharedKey(PrivateKeyDCRTPoly& sk,
  914. UnorderedMapFromIndexToDCRTPoly& sk_shares, const uint32_t N, const uint32_t threshold,
  915. const std::string& shareType) const
  916. {
  917. std::shared_ptr<PrivateKeyImpl> p = sk.GetInternal();
  918. m_cryptoContextImplSharedPtr->RecoverSharedKey(p, sk_shares.GetInternal(), N,
  919. threshold, shareType);
  920. }
  921. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalAutomorphism(
  922. const CiphertextDCRTPoly& ciphertext, const uint32_t i,
  923. const MapFromIndexToEvalKey& evalKeyMap) const
  924. {
  925. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalAutomorphism(
  926. ciphertext.GetInternal(), i, evalKeyMap.GetInternalMap()));
  927. }
  928. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalSumRows(
  929. const CiphertextDCRTPoly& ciphertext, const uint32_t rowSize,
  930. const MapFromIndexToEvalKey& evalSumKeyMap, const uint32_t subringDim) const
  931. {
  932. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalSumRows(
  933. ciphertext.GetInternal(), rowSize, evalSumKeyMap.GetInternalMap(), subringDim));
  934. }
  935. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalSumCols(
  936. const CiphertextDCRTPoly& ciphertext, const uint32_t rowSize,
  937. const MapFromIndexToEvalKey& evalSumKeyMap) const
  938. {
  939. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalSumCols(
  940. ciphertext.GetInternal(), rowSize, evalSumKeyMap.GetInternalMap()));
  941. }
  942. std::unique_ptr<MapFromIndexToEvalKey> CryptoContextDCRTPoly::EvalAutomorphismKeyGen(
  943. const PrivateKeyDCRTPoly& privateKey, const std::vector<uint32_t>& indexList) const
  944. {
  945. return std::make_unique<MapFromIndexToEvalKey>(
  946. m_cryptoContextImplSharedPtr->EvalAutomorphismKeyGen(privateKey.GetInternal(), indexList));
  947. }
  948. std::unique_ptr<MapFromIndexToEvalKey> CryptoContextDCRTPoly::EvalSumRowsKeyGen(
  949. const PrivateKeyDCRTPoly& privateKey, const PublicKeyDCRTPoly& publicKey,
  950. const uint32_t rowSize, const uint32_t subringDim) const
  951. {
  952. return std::make_unique<MapFromIndexToEvalKey>(m_cryptoContextImplSharedPtr->EvalSumRowsKeyGen(
  953. privateKey.GetInternal(), publicKey.GetInternal(), rowSize, subringDim));
  954. }
  955. std::unique_ptr<MapFromIndexToEvalKey> CryptoContextDCRTPoly::EvalSumColsKeyGen(
  956. const PrivateKeyDCRTPoly& privateKey, const PublicKeyDCRTPoly& publicKey) const
  957. {
  958. return std::make_unique<MapFromIndexToEvalKey>(m_cryptoContextImplSharedPtr->EvalSumColsKeyGen(
  959. privateKey.GetInternal(), publicKey.GetInternal()));
  960. }
  961. std::unique_ptr<MapFromIndexToEvalKey> CryptoContextDCRTPoly::MultiEvalAutomorphismKeyGen(
  962. const PrivateKeyDCRTPoly& privateKey, const MapFromIndexToEvalKey& evalKeyMap,
  963. const std::vector<uint32_t>& indexList, const std::string& keyId) const
  964. {
  965. return std::make_unique<MapFromIndexToEvalKey>(
  966. m_cryptoContextImplSharedPtr->MultiEvalAutomorphismKeyGen(privateKey.GetInternal(),
  967. evalKeyMap.GetInternal(), indexList, keyId));
  968. }
  969. std::unique_ptr<MapFromIndexToEvalKey> CryptoContextDCRTPoly::MultiEvalAtIndexKeyGen(
  970. const PrivateKeyDCRTPoly& privateKey, const MapFromIndexToEvalKey& evalKeyMap,
  971. const std::vector<int32_t>& indexList, const std::string& keyId) const
  972. {
  973. return std::make_unique<MapFromIndexToEvalKey>(
  974. m_cryptoContextImplSharedPtr->MultiEvalAtIndexKeyGen(privateKey.GetInternal(),
  975. evalKeyMap.GetInternal(), indexList, keyId));
  976. }
  977. std::unique_ptr<MapFromIndexToEvalKey> CryptoContextDCRTPoly::MultiEvalSumKeyGen(
  978. const PrivateKeyDCRTPoly& privateKey, const MapFromIndexToEvalKey& evalKeyMap,
  979. const std::string& keyId) const
  980. {
  981. return std::make_unique<MapFromIndexToEvalKey>(
  982. m_cryptoContextImplSharedPtr->MultiEvalSumKeyGen(privateKey.GetInternal(),
  983. evalKeyMap.GetInternal(), keyId));
  984. }
  985. std::unique_ptr<MapFromIndexToEvalKey> CryptoContextDCRTPoly::MultiAddEvalSumKeys(
  986. const MapFromIndexToEvalKey& evalKeyMap1, const MapFromIndexToEvalKey& evalKeyMap2,
  987. const std::string& keyId) const
  988. {
  989. return std::make_unique<MapFromIndexToEvalKey>(
  990. m_cryptoContextImplSharedPtr->MultiAddEvalSumKeys(evalKeyMap1.GetInternal(),
  991. evalKeyMap2.GetInternal(), keyId));
  992. }
  993. std::unique_ptr<MapFromIndexToEvalKey> CryptoContextDCRTPoly::MultiAddEvalAutomorphismKeys(
  994. const MapFromIndexToEvalKey& evalKeyMap1, const MapFromIndexToEvalKey& evalKeyMap2,
  995. const std::string& keyId) const
  996. {
  997. return std::make_unique<MapFromIndexToEvalKey>(
  998. m_cryptoContextImplSharedPtr->MultiAddEvalAutomorphismKeys(evalKeyMap1.GetInternal(),
  999. evalKeyMap2.GetInternal(), keyId));
  1000. }
  1001. std::unique_ptr<VectorOfDCRTPolys> CryptoContextDCRTPoly::EvalFastRotationPrecompute(
  1002. const CiphertextDCRTPoly& ciphertext) const
  1003. {
  1004. return std::make_unique<VectorOfDCRTPolys>(
  1005. m_cryptoContextImplSharedPtr->EvalFastRotationPrecompute(ciphertext.GetInternal()));
  1006. }
  1007. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalFastRotation(
  1008. const CiphertextDCRTPoly& ciphertext, const uint32_t index, const uint32_t m,
  1009. const VectorOfDCRTPolys& digits) const
  1010. {
  1011. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalFastRotation(
  1012. ciphertext.GetInternal(), index, m, digits.GetInternal()));
  1013. }
  1014. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalFastRotationExt(
  1015. const CiphertextDCRTPoly& ciphertext, const uint32_t index, const VectorOfDCRTPolys& digits,
  1016. const bool addFirst) const
  1017. {
  1018. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalFastRotationExt(
  1019. ciphertext.GetInternal(), index, digits.GetInternal(), addFirst));
  1020. }
  1021. std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalFHEWtoCKKS(
  1022. VectorOfLWECiphertexts& LWECiphertexts, const uint32_t numCtxts, const uint32_t numSlots,
  1023. const uint32_t p, const double pmin, const double pmax, const uint32_t dim1) const
  1024. {
  1025. return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalFHEWtoCKKS(
  1026. LWECiphertexts.GetInternal(), numCtxts, numSlots, p, pmin, pmax, dim1));
  1027. }
  1028. std::unique_ptr<VectorOfLWECiphertexts> CryptoContextDCRTPoly::EvalCKKStoFHEW(
  1029. const CiphertextDCRTPoly& ciphertext, const uint32_t numCtxts) const
  1030. {
  1031. return std::make_unique<VectorOfLWECiphertexts>(m_cryptoContextImplSharedPtr->EvalCKKStoFHEW(
  1032. ciphertext.GetInternal(), numCtxts));
  1033. }
  1034. std::unique_ptr<VectorOfCiphertexts> CryptoContextDCRTPoly::IntMPBootAdd(
  1035. VectorOfVectorOfCiphertexts& sharesPairVec) const
  1036. {
  1037. return std::make_unique<VectorOfCiphertexts>(m_cryptoContextImplSharedPtr->IntMPBootAdd(
  1038. sharesPairVec.GetInternal()));
  1039. }
  1040. std::unique_ptr<SchemeBaseDCRTPoly> CryptoContextDCRTPoly::GetScheme() const
  1041. {
  1042. return std::make_unique<SchemeBaseDCRTPoly>(m_cryptoContextImplSharedPtr->GetScheme());
  1043. }
  1044. std::unique_ptr<CryptoParametersBaseDCRTPoly> CryptoContextDCRTPoly::GetCryptoParameters() const
  1045. {
  1046. return std::make_unique<CryptoParametersBaseDCRTPoly>(
  1047. m_cryptoContextImplSharedPtr->GetCryptoParameters());
  1048. }
  1049. std::unique_ptr<EncodingParams> CryptoContextDCRTPoly::GetEncodingParams() const
  1050. {
  1051. return std::make_unique<EncodingParams>(m_cryptoContextImplSharedPtr->GetEncodingParams());
  1052. }
  1053. std::shared_ptr<CryptoContextImpl> CryptoContextDCRTPoly::GetInternal() const
  1054. {
  1055. return m_cryptoContextImplSharedPtr;
  1056. }
  1057. // cxx currently does not support static class methods
  1058. void ClearEvalMultKeys()
  1059. {
  1060. CryptoContextImpl::ClearEvalMultKeys();
  1061. }
  1062. void ClearEvalMultKeysById(const std::string& id)
  1063. {
  1064. CryptoContextImpl::ClearEvalMultKeys(id);
  1065. }
  1066. void ClearEvalMultKeysByCryptoContext(const CryptoContextDCRTPoly& cryptoContext)
  1067. {
  1068. CryptoContextImpl::ClearEvalMultKeys(cryptoContext.GetInternal());
  1069. }
  1070. void ClearEvalSumKeys()
  1071. {
  1072. CryptoContextImpl::ClearEvalSumKeys();
  1073. }
  1074. void ClearEvalSumKeysById(const std::string& id)
  1075. {
  1076. CryptoContextImpl::ClearEvalSumKeys(id);
  1077. }
  1078. void ClearEvalSumKeysByCryptoContext(const CryptoContextDCRTPoly& cryptoContext)
  1079. {
  1080. CryptoContextImpl::ClearEvalSumKeys(cryptoContext.GetInternal());
  1081. }
  1082. void ClearEvalAutomorphismKeys()
  1083. {
  1084. CryptoContextImpl::ClearEvalAutomorphismKeys();
  1085. }
  1086. void ClearEvalAutomorphismKeysById(const std::string& id)
  1087. {
  1088. CryptoContextImpl::ClearEvalAutomorphismKeys(id);
  1089. }
  1090. void ClearEvalAutomorphismKeysByCryptoContext(const CryptoContextDCRTPoly& cryptoContext)
  1091. {
  1092. CryptoContextImpl::ClearEvalAutomorphismKeys(cryptoContext.GetInternal());
  1093. }
  1094. std::unique_ptr<std::vector<uint32_t>> GetExistingEvalAutomorphismKeyIndices(
  1095. const std::string& keyTag)
  1096. {
  1097. return std::make_unique<std::vector<uint32_t>>(
  1098. CryptoContextImpl::GetExistingEvalAutomorphismKeyIndices(keyTag));
  1099. }
  1100. std::unique_ptr<std::vector<uint32_t>> GetUniqueValues(const std::vector<uint32_t>& oldValues,
  1101. const std::vector<uint32_t>& newValues)
  1102. {
  1103. return std::make_unique<std::vector<uint32_t>>(CryptoContextImpl::GetUniqueValues(oldValues,
  1104. newValues));
  1105. }
  1106. std::unique_ptr<MapFromIndexToEvalKey> GetEvalAutomorphismKeyMap(const std::string& keyID)
  1107. {
  1108. return std::make_unique<MapFromIndexToEvalKey>(
  1109. CryptoContextImpl::GetEvalAutomorphismKeyMapPtr(keyID));
  1110. }
  1111. std::unique_ptr<MapFromIndexToEvalKey> GetCopyOfEvalSumKeyMap(const std::string& id)
  1112. {
  1113. return std::make_unique<MapFromIndexToEvalKey>(
  1114. std::make_shared<std::map<uint32_t, std::shared_ptr<EvalKeyImpl>>>(
  1115. CryptoContextImpl::GetEvalSumKeyMap(id)));
  1116. }
  1117. std::unique_ptr<MapFromIndexToEvalKey> GetEvalAutomorphismKeyMapPtr(const std::string& keyID)
  1118. {
  1119. return std::make_unique<MapFromIndexToEvalKey>(CryptoContextImpl::GetEvalAutomorphismKeyMapPtr(
  1120. keyID));
  1121. }
  1122. void InsertEvalAutomorphismKey(const MapFromIndexToEvalKey& evalKeyMap, const std::string& keyTag)
  1123. {
  1124. CryptoContextImpl::InsertEvalAutomorphismKey(evalKeyMap.GetInternal(), keyTag);
  1125. }
  1126. void InsertEvalSumKey(const MapFromIndexToEvalKey& mapToInsert, const std::string& keyTag)
  1127. {
  1128. CryptoContextImpl::InsertEvalSumKey(mapToInsert.GetInternal(), keyTag);
  1129. }
  1130. std::unique_ptr<VectorOfEvalKeys> GetCopyOfEvalMultKeyVector(const std::string& keyID)
  1131. {
  1132. return std::make_unique<VectorOfEvalKeys>(CryptoContextImpl::GetEvalMultKeyVector(keyID));
  1133. }
  1134. void InsertEvalMultKey(const VectorOfEvalKeys& evalKeyVec)
  1135. {
  1136. CryptoContextImpl::InsertEvalMultKey(evalKeyVec.GetInternal());
  1137. }
  1138. std::unique_ptr<MapFromStringToVectorOfEvalKeys> GetCopyOfAllEvalMultKeys()
  1139. {
  1140. return std::make_unique<MapFromStringToVectorOfEvalKeys>(
  1141. CryptoContextImpl::GetAllEvalMultKeys());
  1142. }
  1143. std::unique_ptr<MapFromStringToMapFromIndexToEvalKey> GetCopyOfAllEvalSumKeys()
  1144. {
  1145. return std::make_unique<MapFromStringToMapFromIndexToEvalKey>(
  1146. CryptoContextImpl::GetAllEvalSumKeys());
  1147. }
  1148. std::unique_ptr<MapFromStringToMapFromIndexToEvalKey> GetCopyOfAllEvalAutomorphismKeys()
  1149. {
  1150. return std::make_unique<MapFromStringToMapFromIndexToEvalKey>(
  1151. CryptoContextImpl::GetAllEvalAutomorphismKeys());
  1152. }
  1153. // Generator functions
  1154. std::unique_ptr<CryptoContextDCRTPoly> GenNullCryptoContext()
  1155. {
  1156. return std::make_unique<CryptoContextDCRTPoly>();
  1157. }
  1158. std::unique_ptr<CryptoContextDCRTPoly> GenCryptoContextByParamsBFVRNS(const ParamsBFVRNS& params)
  1159. {
  1160. return std::make_unique<CryptoContextDCRTPoly>(params);
  1161. }
  1162. std::unique_ptr<CryptoContextDCRTPoly> GenCryptoContextByParamsBGVRNS(const ParamsBGVRNS& params)
  1163. {
  1164. return std::make_unique<CryptoContextDCRTPoly>(params);
  1165. }
  1166. std::unique_ptr<CryptoContextDCRTPoly> GenCryptoContextByParamsCKKSRNS(const ParamsCKKSRNS& params)
  1167. {
  1168. return std::make_unique<CryptoContextDCRTPoly>(params);
  1169. }
  1170. } // openfhe