bindings.cpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. #include <pybind11/pybind11.h>
  2. #include <pybind11/stl.h>
  3. #include <pybind11/stl_bind.h>
  4. #include <pybind11/complex.h>
  5. #include <pybind11/functional.h>
  6. #include <pybind11/operators.h>
  7. #include <pybind11/iostream.h>
  8. #include <iostream>
  9. #include <map>
  10. #include "openfhe.h"
  11. #include "key/key-ser.h"
  12. #include "bindings.h"
  13. #include "cryptocontext_wrapper.h"
  14. #include "binfhe_bindings.h"
  15. #include "cryptocontext_docs.h"
  16. #include "cryptoparameters_docs.h"
  17. #include "plaintext_docs.h"
  18. #include "ciphertext_docs.h"
  19. #include "serialization.h"
  20. using namespace lbcrypto;
  21. namespace py = pybind11;
  22. PYBIND11_MAKE_OPAQUE(std::map<usint, EvalKey<DCRTPoly>>);
  23. template <typename T>
  24. void bind_parameters(py::module &m,const std::string name)
  25. {
  26. py::class_<CCParams<T>>(m, name.c_str())
  27. .def(py::init<>())
  28. // getters
  29. .def("GetPlaintextModulus", &CCParams<T>::GetPlaintextModulus)
  30. .def("GetScheme", &CCParams<T>::GetScheme)
  31. .def("GetDigitSize", &CCParams<T>::GetDigitSize)
  32. .def("GetStandardDeviation", &CCParams<T>::GetStandardDeviation)
  33. .def("GetSecretKeyDist", &CCParams<T>::GetSecretKeyDist)
  34. .def("GetMaxRelinSkDeg", &CCParams<T>::GetMaxRelinSkDeg)
  35. .def("GetPREMode", &CCParams<T>::GetPREMode)
  36. .def("GetMultipartyMode", &CCParams<T>::GetMultipartyMode)
  37. .def("GetExecutionMode", &CCParams<T>::GetExecutionMode)
  38. .def("GetDecryptionNoiseMode", &CCParams<T>::GetDecryptionNoiseMode)
  39. .def("GetNoiseEstimate", &CCParams<T>::GetNoiseEstimate)
  40. .def("GetDesiredPrecision", &CCParams<T>::GetDesiredPrecision)
  41. .def("GetStatisticalSecurity", &CCParams<T>::GetStatisticalSecurity)
  42. .def("GetNumAdversarialQueries", &CCParams<T>::GetNumAdversarialQueries)
  43. //.def("GetThresholdNumOfParties", &CCParams<T>::GetThresholdNumOfParties)
  44. .def("GetKeySwitchTechnique", &CCParams<T>::GetKeySwitchTechnique)
  45. .def("GetScalingTechnique", &CCParams<T>::GetScalingTechnique)
  46. .def("GetBatchSize", &CCParams<T>::GetBatchSize)
  47. .def("GetFirstModSize", &CCParams<T>::GetFirstModSize)
  48. .def("GetNumLargeDigits", &CCParams<T>::GetNumLargeDigits)
  49. .def("GetMultiplicativeDepth", &CCParams<T>::GetMultiplicativeDepth)
  50. .def("GetScalingModSize", &CCParams<T>::GetScalingModSize)
  51. .def("GetSecurityLevel", &CCParams<T>::GetSecurityLevel)
  52. .def("GetRingDim", &CCParams<T>::GetRingDim)
  53. .def("GetEvalAddCount", &CCParams<T>::GetEvalAddCount)
  54. .def("GetKeySwitchCount", &CCParams<T>::GetKeySwitchCount)
  55. .def("GetEncryptionTechnique", &CCParams<T>::GetEncryptionTechnique)
  56. .def("GetMultiplicationTechnique", &CCParams<T>::GetMultiplicationTechnique)
  57. .def("GetMultiHopModSize", &CCParams<T>::GetMultiHopModSize)
  58. .def("GetInteractiveBootCompressionLevel", &CCParams<T>::GetInteractiveBootCompressionLevel)
  59. // setters
  60. .def("SetPlaintextModulus", &CCParams<T>::SetPlaintextModulus)
  61. .def("SetDigitSize", &CCParams<T>::SetDigitSize)
  62. .def("SetStandardDeviation", &CCParams<T>::SetStandardDeviation)
  63. .def("SetSecretKeyDist", &CCParams<T>::SetSecretKeyDist)
  64. .def("SetMaxRelinSkDeg", &CCParams<T>::SetMaxRelinSkDeg)
  65. .def("SetPREMode", &CCParams<T>::SetPREMode)
  66. .def("SetMultipartyMode", &CCParams<T>::SetMultipartyMode)
  67. .def("SetExecutionMode", &CCParams<T>::SetExecutionMode)
  68. .def("SetDecryptionNoiseMode", &CCParams<T>::SetDecryptionNoiseMode)
  69. .def("SetNoiseEstimate", &CCParams<T>::SetNoiseEstimate)
  70. .def("SetDesiredPrecision", &CCParams<T>::SetDesiredPrecision)
  71. .def("SetStatisticalSecurity", &CCParams<T>::SetStatisticalSecurity)
  72. .def("SetNumAdversarialQueries", &CCParams<T>::SetNumAdversarialQueries)
  73. .def("SetThresholdNumOfParties", &CCParams<T>::SetThresholdNumOfParties)
  74. .def("SetKeySwitchTechnique", &CCParams<T>::SetKeySwitchTechnique)
  75. .def("SetScalingTechnique", &CCParams<T>::SetScalingTechnique)
  76. .def("SetBatchSize", &CCParams<T>::SetBatchSize)
  77. .def("SetFirstModSize", &CCParams<T>::SetFirstModSize)
  78. .def("SetNumLargeDigits", &CCParams<T>::SetNumLargeDigits)
  79. .def("SetMultiplicativeDepth", &CCParams<T>::SetMultiplicativeDepth)
  80. .def("SetScalingModSize", &CCParams<T>::SetScalingModSize)
  81. .def("SetSecurityLevel", &CCParams<T>::SetSecurityLevel)
  82. .def("SetRingDim", &CCParams<T>::SetRingDim)
  83. .def("SetEvalAddCount", &CCParams<T>::SetEvalAddCount)
  84. .def("SetKeySwitchCount", &CCParams<T>::SetKeySwitchCount)
  85. .def("SetEncryptionTechnique", &CCParams<T>::SetEncryptionTechnique)
  86. .def("SetMultiplicationTechnique", &CCParams<T>::SetMultiplicationTechnique)
  87. .def("SetMultiHopModSize", &CCParams<T>::SetMultiHopModSize)
  88. .def("SetInteractiveBootCompressionLevel", &CCParams<T>::SetInteractiveBootCompressionLevel)
  89. .def("__str__",[](const CCParams<T> &params) {
  90. std::stringstream stream;
  91. stream << params;
  92. return stream.str();
  93. });
  94. //
  95. }
  96. void bind_crypto_context(py::module &m)
  97. {
  98. py::class_<CryptoContextImpl<DCRTPoly>, std::shared_ptr<CryptoContextImpl<DCRTPoly>>>(m, "CryptoContext")
  99. .def(py::init<>())
  100. .def("GetKeyGenLevel", &CryptoContextImpl<DCRTPoly>::GetKeyGenLevel, cc_GetKeyGenLevel_docs)
  101. .def("SetKeyGenLevel", &CryptoContextImpl<DCRTPoly>::SetKeyGenLevel, cc_SetKeyGenLevel_docs,
  102. py::arg("level"))
  103. .def("get_ptr", [](const CryptoContext<DCRTPoly> &self)
  104. { std::cout << "CC shared ptr (python cc)" << self << std::endl; })
  105. //.def("GetScheme",&CryptoContextImpl<DCRTPoly>::GetScheme)
  106. //.def("GetCryptoParameters", &CryptoContextImpl<DCRTPoly>::GetCryptoParameters)
  107. .def("GetRingDimension", &CryptoContextImpl<DCRTPoly>::GetRingDimension, cc_GetRingDimension_docs)
  108. .def("GetPlaintextModulus", &GetPlaintextModulusWrapper, cc_GetPlaintextModulus_docs)
  109. .def("GetModulus", &GetModulusWrapper, cc_GetModulus_docs)
  110. .def("GetModulusCKKS", &GetModulusCKKSWrapper)
  111. .def("GetScalingFactorReal", &GetScalingFactorRealWrapper, cc_GetScalingFactorReal_docs)
  112. .def("GetScalingTechnique",&GetScalingTechniqueWrapper)
  113. .def("GetDigitSize", &GetDigitSizeWrapper)
  114. .def("GetCyclotomicOrder", &CryptoContextImpl<DCRTPoly>::GetCyclotomicOrder, cc_GetCyclotomicOrder_docs)
  115. .def("Enable", static_cast<void (CryptoContextImpl<DCRTPoly>::*)(PKESchemeFeature)>(&CryptoContextImpl<DCRTPoly>::Enable), cc_Enable_docs,
  116. py::arg("feature"))
  117. .def("KeyGen", &CryptoContextImpl<DCRTPoly>::KeyGen, cc_KeyGen_docs)
  118. .def("EvalMultKeyGen", &CryptoContextImpl<DCRTPoly>::EvalMultKeyGen,
  119. cc_EvalMultKeyGen_docs,
  120. py::arg("privateKey"))
  121. .def("EvalMultKeysGen", &CryptoContextImpl<DCRTPoly>::EvalMultKeysGen,
  122. cc_EvalMultKeysGen_docs,
  123. py::arg("privateKey"))
  124. .def("EvalRotateKeyGen", &CryptoContextImpl<DCRTPoly>::EvalRotateKeyGen,
  125. cc_EvalRotateKeyGen_docs,
  126. py::arg("privateKey"),
  127. py::arg("indexList"),
  128. py::arg("publicKey") = nullptr)
  129. .def("MakeStringPlaintext", &CryptoContextImpl<DCRTPoly>::MakeStringPlaintext,
  130. cc_MakeStringPlaintext_docs,
  131. py::arg("str"))
  132. .def("MakePackedPlaintext", &CryptoContextImpl<DCRTPoly>::MakePackedPlaintext,
  133. cc_MakePackedPlaintext_docs,
  134. py::arg("value"),
  135. py::arg("noiseScaleDeg") = 1,
  136. py::arg("level") = 0)
  137. .def("MakeCoefPackedPlaintext", &CryptoContextImpl<DCRTPoly>::MakeCoefPackedPlaintext,
  138. cc_MakeCoefPackedPlaintext_docs,
  139. py::arg("value"),
  140. py::arg("noiseScaleDeg ") = 1,
  141. py::arg("level") = 0)
  142. // TODO (Oliveira): allow user to specify different params values
  143. .def("MakeCKKSPackedPlaintext", static_cast<Plaintext (CryptoContextImpl<DCRTPoly>::*)(const std::vector<std::complex<double>> &, size_t, uint32_t, const std::shared_ptr<ParmType>, usint) const>(&CryptoContextImpl<DCRTPoly>::MakeCKKSPackedPlaintext), cc_MakeCKKSPackedPlaintextComplex_docs,
  144. py::arg("value"),
  145. py::arg("scaleDeg") = static_cast<size_t>(1),
  146. py::arg("level") = static_cast<uint32_t>(0),
  147. py::arg("params") = py::none(),
  148. py::arg("slots") = 0)
  149. .def("MakeCKKSPackedPlaintext", static_cast<Plaintext (CryptoContextImpl<DCRTPoly>::*)(const std::vector<double> &, size_t, uint32_t, const std::shared_ptr<ParmType>, usint) const>(&CryptoContextImpl<DCRTPoly>::MakeCKKSPackedPlaintext), cc_MakeCKKSPlaintextReal_docs,
  150. py::arg("value"),
  151. py::arg("scaleDeg") = static_cast<size_t>(1),
  152. py::arg("level") = static_cast<uint32_t>(0),
  153. py::arg("params") = py::none(),
  154. py::arg("slots") = 0)
  155. .def("EvalRotate", &CryptoContextImpl<DCRTPoly>::EvalRotate,
  156. cc_EvalRotate_docs,
  157. py::arg("ciphertext"),
  158. py::arg("index"))
  159. .def("EvalFastRotationPrecompute", &EvalFastRotationPrecomputeWrapper,
  160. cc_EvalFastRotationPreCompute_docs,
  161. py::arg("ciphertext"))
  162. .def("EvalFastRotation", &EvalFastRotationWrapper,
  163. cc_EvalFastRotation_docs,
  164. py::arg("ciphertext"),
  165. py::arg("index"),
  166. py::arg("m"),
  167. py::arg("digits"))
  168. .def("EvalFastRotationExt", &EvalFastRotationExtWrapper,
  169. cc_EvalFastRotationExt_docs,
  170. py::arg("ciphertext"),
  171. py::arg("index"),
  172. py::arg("digits"),
  173. py::arg("addFirst"))
  174. .def("EvalAtIndexKeyGen", &CryptoContextImpl<DCRTPoly>::EvalAtIndexKeyGen,
  175. cc_EvalAtIndexKeyGen_docs,
  176. py::arg("privateKey"),
  177. py::arg("indexList"),
  178. py::arg("publicKey") = nullptr)
  179. .def("EvalAtIndex", &CryptoContextImpl<DCRTPoly>::EvalAtIndex,
  180. cc_EvalAtIndex_docs,
  181. py::arg("ciphertext"),
  182. py::arg("index"))
  183. .def("Encrypt", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(const PublicKey<DCRTPoly>, Plaintext) const>
  184. (&CryptoContextImpl<DCRTPoly>::Encrypt),
  185. cc_Encrypt_docs,
  186. py::arg("publicKey"),
  187. py::arg("plaintext"))
  188. .def("Decrypt", static_cast<Plaintext (*)(CryptoContext<DCRTPoly> &, const PrivateKey<DCRTPoly>, ConstCiphertext<DCRTPoly>)>
  189. (&DecryptWrapper), cc_Decrypt_docs,
  190. py::arg("privateKey"),
  191. py::arg("ciphertext"))
  192. .def("Decrypt", static_cast<Plaintext (*)(CryptoContext<DCRTPoly> &, ConstCiphertext<DCRTPoly>, const PrivateKey<DCRTPoly>)>
  193. (&DecryptWrapper), cc_Decrypt_docs,
  194. py::arg("ciphertext"),
  195. py::arg("privateKey"))
  196. .def("KeySwitchGen", &CryptoContextImpl<DCRTPoly>::KeySwitchGen,
  197. cc_KeySwitchGen_docs,
  198. py::arg("oldPrivateKey"),
  199. py::arg("newPrivateKey"))
  200. .def("EvalAdd", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstCiphertext<DCRTPoly>) const>
  201. (&CryptoContextImpl<DCRTPoly>::EvalAdd),
  202. cc_EvalAdd_docs,
  203. py::arg("ciphertext1"),
  204. py::arg("ciphertext2"))
  205. .def("EvalAdd", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, double) const>
  206. (&CryptoContextImpl<DCRTPoly>::EvalAdd),
  207. cc_EvalAddfloat_docs,
  208. py::arg("ciphertext"),
  209. py::arg("constant"))
  210. //inline Ciphertext<Element> EvalAdd(ConstCiphertext<Element> ciphertext, ConstPlaintext plaintext) const
  211. .def("EvalAdd", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstPlaintext) const>
  212. (&CryptoContextImpl<DCRTPoly>::EvalAdd),
  213. cc_EvalAddPlaintext_docs,
  214. py::arg("ciphertext"),
  215. py::arg("plaintext"))
  216. .def("EvalAddInPlace", static_cast<void (CryptoContextImpl<DCRTPoly>::*)(Ciphertext<DCRTPoly> &, ConstCiphertext<DCRTPoly>) const>
  217. (&CryptoContextImpl<DCRTPoly>::EvalAddInPlace),
  218. cc_EvalAddInPlace_docs,
  219. py::arg("ciphertext1"),
  220. py::arg("ciphertext2"))
  221. .def("EvalAddInPlace", static_cast<void (CryptoContextImpl<DCRTPoly>::*)(Ciphertext<DCRTPoly> &, ConstPlaintext) const>
  222. (&CryptoContextImpl<DCRTPoly>::EvalAddInPlace),
  223. cc_EvalAddInPlacePlaintext_docs,
  224. py::arg("ciphertext"),
  225. py::arg("plaintext"))
  226. .def("EvalAddInPlace", static_cast<void (CryptoContextImpl<DCRTPoly>::*)(ConstPlaintext, Ciphertext<DCRTPoly> &) const>
  227. (&CryptoContextImpl<DCRTPoly>::EvalAddInPlace),
  228. "",
  229. py::arg("plaintext"),
  230. py::arg("ciphertext"))
  231. .def("EvalAddMutable", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(Ciphertext<DCRTPoly> &, Ciphertext<DCRTPoly> &) const>
  232. (&CryptoContextImpl<DCRTPoly>::EvalAddMutable),
  233. cc_EvalAddMutable_docs,
  234. py::arg("ciphertext1"),
  235. py::arg("ciphertext2"))
  236. .def("EvalAddMutable", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(Ciphertext<DCRTPoly> &, Plaintext) const>
  237. (&CryptoContextImpl<DCRTPoly>::EvalAddMutable),
  238. cc_EvalAddMutablePlaintext_docs,
  239. py::arg("ciphertext"),
  240. py::arg("plaintext"))
  241. .def("EvalAddMutable", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(Plaintext, Ciphertext<DCRTPoly> &) const>
  242. (&CryptoContextImpl<DCRTPoly>::EvalAddMutable),
  243. "",
  244. py::arg("plaintext"),
  245. py::arg("ciphertext"))
  246. .def("EvalAddMutableInPlace", &CryptoContextImpl<DCRTPoly>::EvalAddMutableInPlace,
  247. cc_EvalAddMutableInPlace_docs,
  248. py::arg("ciphertext1"),
  249. py::arg("ciphertext2"))
  250. .def("EvalSub", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstCiphertext<DCRTPoly>) const>
  251. (&CryptoContextImpl<DCRTPoly>::EvalSub),
  252. cc_EvalSub_docs,
  253. py::arg("ciphertext1"),
  254. py::arg("ciphertext2"))
  255. .def("EvalSub", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, double) const>
  256. (&CryptoContextImpl<DCRTPoly>::EvalSub),
  257. cc_EvalSubfloat_docs,
  258. py::arg("ciphertext"),
  259. py::arg("constant"))
  260. .def("EvalSub", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(double, ConstCiphertext<DCRTPoly>) const>
  261. (&CryptoContextImpl<DCRTPoly>::EvalSub),
  262. "",
  263. py::arg("constant"),
  264. py::arg("ciphertext"))
  265. .def("EvalSub", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstPlaintext) const>
  266. (&CryptoContextImpl<DCRTPoly>::EvalSub),
  267. cc_EvalSubPlaintext_docs,
  268. py::arg("ciphertext"),
  269. py::arg("plaintext"))
  270. .def("EvalSub", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstPlaintext, ConstCiphertext<DCRTPoly>) const>
  271. (&CryptoContextImpl<DCRTPoly>::EvalSub),
  272. "",
  273. py::arg("plaintext"),
  274. py::arg("ciphertext"))
  275. .def("EvalSubInPlace", static_cast<void (CryptoContextImpl<DCRTPoly>::*)(Ciphertext<DCRTPoly> &, ConstCiphertext<DCRTPoly>) const>
  276. (&CryptoContextImpl<DCRTPoly>::EvalSubInPlace),
  277. cc_EvalSubInPlace_docs,
  278. py::arg("ciphertext1"),
  279. py::arg("ciphertext2"))
  280. .def("EvalSubInPlace", static_cast<void (CryptoContextImpl<DCRTPoly>::*)(Ciphertext<DCRTPoly> &, double) const>
  281. (&CryptoContextImpl<DCRTPoly>::EvalSubInPlace),
  282. cc_EvalSubInPlacefloat_docs,
  283. py::arg("ciphertext"),
  284. py::arg("constant"))
  285. .def("EvalSubInPlace", static_cast<void (CryptoContextImpl<DCRTPoly>::*)(double, Ciphertext<DCRTPoly> &) const>
  286. (&CryptoContextImpl<DCRTPoly>::EvalSubInPlace),
  287. "",
  288. py::arg("constant"),
  289. py::arg("ciphertext"))
  290. .def("EvalSubMutable", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(Ciphertext<DCRTPoly> &, Ciphertext<DCRTPoly> &) const>
  291. (&CryptoContextImpl<DCRTPoly>::EvalSubMutable),
  292. cc_EvalSubMutable_docs,
  293. py::arg("ciphertext1"),
  294. py::arg("ciphertext2"))
  295. .def("EvalSubMutable", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(Ciphertext<DCRTPoly> &, Plaintext) const>
  296. (&CryptoContextImpl<DCRTPoly>::EvalSubMutable),
  297. cc_EvalSubMutablePlaintext_docs,
  298. py::arg("ciphertext"),
  299. py::arg("plaintext"))
  300. .def("EvalSubMutable", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(Plaintext, Ciphertext<DCRTPoly> &) const>
  301. (&CryptoContextImpl<DCRTPoly>::EvalSubMutable),
  302. "",
  303. py::arg("plaintext"),
  304. py::arg("ciphertext"))
  305. .def("EvalSubMutableInPlace", &CryptoContextImpl<DCRTPoly>::EvalSubMutableInPlace,
  306. cc_EvalSubMutableInPlace_docs,
  307. py::arg("ciphertext1"),
  308. py::arg("ciphertext2"))
  309. .def("EvalMult", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstCiphertext<DCRTPoly>) const>
  310. (&CryptoContextImpl<DCRTPoly>::EvalMult),
  311. cc_EvalMult_docs,
  312. py::arg("ciphertext1"),
  313. py::arg("ciphertext2"))
  314. .def("EvalMult", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, double) const>
  315. (&CryptoContextImpl<DCRTPoly>::EvalMult),
  316. cc_EvalMultfloat_docs,
  317. py::arg("ciphertext"),
  318. py::arg("constant"))
  319. .def("EvalMult", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstPlaintext) const>
  320. (&CryptoContextImpl<DCRTPoly>::EvalMult),
  321. cc_EvalMultPlaintext_docs,
  322. py::arg("ciphertext"),
  323. py::arg("plaintext"))
  324. .def("EvalMult", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstPlaintext, ConstCiphertext<DCRTPoly>) const>
  325. (&CryptoContextImpl<DCRTPoly>::EvalMult),
  326. "",
  327. py::arg("plaintext"),
  328. py::arg("ciphertext"))
  329. .def("EvalMult", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(double, ConstCiphertext<DCRTPoly>) const>
  330. (&CryptoContextImpl<DCRTPoly>::EvalMult),
  331. "",
  332. py::arg("constant"),
  333. py::arg("ciphertext"))
  334. .def("EvalMultMutable", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(Ciphertext<DCRTPoly> &, Ciphertext<DCRTPoly> &) const>
  335. (&CryptoContextImpl<DCRTPoly>::EvalMultMutable),
  336. cc_EvalMultMutable_docs,
  337. py::arg("ciphertext1"),
  338. py::arg("ciphertext2"))
  339. .def("EvalMultMutable", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(Ciphertext<DCRTPoly> &, Plaintext) const>
  340. (&CryptoContextImpl<DCRTPoly>::EvalMultMutable),
  341. cc_EvalMultMutablePlaintext_docs,
  342. py::arg("ciphertext"),
  343. py::arg("plaintext"))
  344. .def("EvalMultMutable", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(Plaintext, Ciphertext<DCRTPoly> &) const>
  345. (&CryptoContextImpl<DCRTPoly>::EvalMultMutable),
  346. "",
  347. py::arg("plaintext"),
  348. py::arg("ciphertext"))
  349. .def("EvalMultMutableInPlace", &CryptoContextImpl<DCRTPoly>::EvalMultMutableInPlace,
  350. cc_EvalMultMutableInPlace_docs,
  351. py::arg("ciphertext1"),
  352. py::arg("ciphertext2"))
  353. .def("EvalSquare", &CryptoContextImpl<DCRTPoly>::EvalSquare,
  354. cc_EvalSquare_docs,
  355. py::arg("ciphertext"))
  356. .def("EvalSquareMutable", &CryptoContextImpl<DCRTPoly>::EvalSquareMutable,
  357. cc_EvalSquareMutable_docs,
  358. py::arg("ciphertext"))
  359. .def("EvalSquareInPlace", &CryptoContextImpl<DCRTPoly>::EvalSquareInPlace,
  360. cc_EvalSquareInPlace_docs,
  361. py::arg("ciphertext"))
  362. .def("EvalMultNoRelin", &CryptoContextImpl<DCRTPoly>::EvalMultNoRelin,
  363. cc_EvalMultNoRelin_docs,
  364. py::arg("ciphertext1"),
  365. py::arg("ciphertext2"))
  366. .def("Relinearize", &CryptoContextImpl<DCRTPoly>::Relinearize,
  367. cc_Relinearize_docs,
  368. py::arg("ciphertext"))
  369. .def("RelinearizeInPlace", &CryptoContextImpl<DCRTPoly>::RelinearizeInPlace,
  370. cc_RelinearizeInPlace_docs,
  371. py::arg("ciphertext"))
  372. .def("EvalMultAndRelinearize", &CryptoContextImpl<DCRTPoly>::EvalMultAndRelinearize,
  373. cc_EvalMultAndRelinearize_docs,
  374. py::arg("ciphertext1"),
  375. py::arg("ciphertext2"))
  376. .def("EvalNegate", &CryptoContextImpl<DCRTPoly>::EvalNegate,
  377. cc_EvalNegate_docs,
  378. py::arg("ciphertext"))
  379. .def("EvalNegateInPlace", &CryptoContextImpl<DCRTPoly>::EvalNegateInPlace,
  380. cc_EvalNegateInPlace_docs,
  381. py::arg("ciphertext"))
  382. .def("EvalLogistic", &CryptoContextImpl<DCRTPoly>::EvalLogistic,
  383. cc_EvalLogistic_docs,
  384. py::arg("ciphertext"),
  385. py::arg("a"),
  386. py::arg("b"),
  387. py::arg("degree"))
  388. .def("EvalChebyshevSeries", &CryptoContextImpl<DCRTPoly>::EvalChebyshevSeries,
  389. cc_EvalChebyshevSeries_docs,
  390. py::arg("ciphertext"),
  391. py::arg("coefficients"),
  392. py::arg("a"),
  393. py::arg("b"))
  394. .def("EvalChebyshevSeriesLinear", &CryptoContextImpl<DCRTPoly>::EvalChebyshevSeriesLinear,
  395. cc_EvalChebyshevSeriesLinear_docs,
  396. py::arg("ciphertext"),
  397. py::arg("coefficients"),
  398. py::arg("a"),
  399. py::arg("b"))
  400. .def("EvalChebyshevSeriesPS", &CryptoContextImpl<DCRTPoly>::EvalChebyshevSeriesPS,
  401. cc_EvalChebyshevSeriesPS_docs,
  402. py::arg("ciphertext"),
  403. py::arg("coefficients"),
  404. py::arg("a"),
  405. py::arg("b"))
  406. .def("EvalChebyshevFunction", &CryptoContextImpl<DCRTPoly>::EvalChebyshevFunction,
  407. cc_EvalChebyshevFunction_docs,
  408. py::arg("func"),
  409. py::arg("ciphertext"),
  410. py::arg("a"),
  411. py::arg("b"),
  412. py::arg("degree"))
  413. .def("EvalSin", &CryptoContextImpl<DCRTPoly>::EvalSin,
  414. cc_EvalSin_docs,
  415. py::arg("ciphertext"),
  416. py::arg("a"),
  417. py::arg("b"),
  418. py::arg("degree"))
  419. .def("EvalCos", &CryptoContextImpl<DCRTPoly>::EvalCos,
  420. cc_EvalCos_docs,
  421. py::arg("ciphertext"),
  422. py::arg("a"),
  423. py::arg("b"),
  424. py::arg("degree"))
  425. .def("EvalDivide", &CryptoContextImpl<DCRTPoly>::EvalDivide,
  426. cc_EvalDivide_docs,
  427. py::arg("ciphertext"),
  428. py::arg("a"),
  429. py::arg("b"),
  430. py::arg("degree"))
  431. .def("EvalSumKeyGen", &CryptoContextImpl<DCRTPoly>::EvalSumKeyGen,
  432. cc_EvalSumKeyGen_docs,
  433. py::arg("privateKey"),
  434. py::arg("publicKey") = py::none())
  435. //TODO (Oliveira, R.): Solve pointer handling bug when dealing with EvalKeyMap object for the next functions
  436. .def("EvalSumRowsKeyGen", &CryptoContextImpl<DCRTPoly>::EvalSumRowsKeyGen,
  437. cc_EvalSumRowsKeyGen_docs,
  438. py::arg("privateKey"),
  439. py::arg("publicKey") = py::none(),
  440. py::arg("rowSize") = 0,
  441. py::arg("subringDim") = 0)
  442. .def("EvalSumColsKeyGen", &CryptoContextImpl<DCRTPoly>::EvalSumColsKeyGen,
  443. cc_EvalSumColsKeyGen_docs,
  444. py::arg("privateKey"),
  445. py::arg("publicKey") = py::none())
  446. .def("EvalSum", &CryptoContextImpl<DCRTPoly>::EvalSum,
  447. cc_EvalSum_docs,
  448. py::arg("ciphertext"),
  449. py::arg("batchSize"))
  450. .def("EvalSumRows", &CryptoContextImpl<DCRTPoly>::EvalSumRows,
  451. cc_EvalSumRows_docs,
  452. py::arg("ciphertext"),
  453. py::arg("rowSize"),
  454. py::arg("evalSumKeyMap"),
  455. py::arg("subringDim") = 0)
  456. .def("EvalSumCols", &CryptoContextImpl<DCRTPoly>::EvalSumCols,
  457. cc_EvalSumCols_docs,
  458. py::arg("ciphertext"),
  459. py::arg("rowSize"),
  460. py::arg("evalSumKeyMap"))
  461. .def("EvalInnerProduct", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstCiphertext<DCRTPoly>, usint) const>(&CryptoContextImpl<DCRTPoly>::EvalInnerProduct),
  462. cc_EvalInnerProduct_docs,
  463. py::arg("ciphertext1"),
  464. py::arg("ciphertext2"),
  465. py::arg("batchSize"))
  466. .def("EvalInnerProduct", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstPlaintext, usint) const>(&CryptoContextImpl<DCRTPoly>::EvalInnerProduct),
  467. cc_EvalInnerProductPlaintext_docs,
  468. py::arg("ciphertext"),
  469. py::arg("plaintext"),
  470. py::arg("batchSize"))
  471. .def("MultipartyKeyGen", static_cast<KeyPair<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(const PublicKey<DCRTPoly>, bool, bool)>(&CryptoContextImpl<DCRTPoly>::MultipartyKeyGen),
  472. cc_MultipartyKeyGen_docs,
  473. py::arg("publicKey"),
  474. py::arg("makeSparse") = false,
  475. py::arg("fresh") = false)
  476. .def("MultipartyKeyGen", static_cast<KeyPair<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(const std::vector<PrivateKey<DCRTPoly>> &)>(&CryptoContextImpl<DCRTPoly>::MultipartyKeyGen),
  477. cc_MultipartyKeyGen_vector_docs,
  478. py::arg("privateKeyVec"))
  479. .def("MultipartyDecryptLead", &CryptoContextImpl<DCRTPoly>::MultipartyDecryptLead,
  480. cc_MultipartyDecryptLead_docs,
  481. py::arg("ciphertextVec"),
  482. py::arg("privateKey"))
  483. .def("MultipartyDecryptMain", &CryptoContextImpl<DCRTPoly>::MultipartyDecryptMain,
  484. cc_MultipartyDecryptMain_docs,
  485. py::arg("ciphertextVec"),
  486. py::arg("privateKey"))
  487. .def("MultipartyDecryptFusion", &MultipartyDecryptFusionWrapper,
  488. cc_MultipartyDecryptFusion_docs,
  489. py::arg("ciphertextVec"))
  490. .def("MultiKeySwitchGen", &CryptoContextImpl<DCRTPoly>::MultiKeySwitchGen,
  491. cc_MultiKeySwitchGen_docs,
  492. py::arg("originalPrivateKey"),
  493. py::arg("newPrivateKey"),
  494. py::arg("evalKey"))
  495. .def("MultiEvalSumKeyGen", &CryptoContextImpl<DCRTPoly>::MultiEvalSumKeyGen,
  496. cc_MultiEvalSumKeyGen_docs,
  497. py::arg("privateKey"),
  498. py::arg("evalKeyMap"),
  499. py::arg("keyId") = "")
  500. .def("MultiAddEvalKeys", &CryptoContextImpl<DCRTPoly>::MultiAddEvalKeys,
  501. cc_MultiAddEvalKeys_docs,
  502. py::arg("evalKey1"),
  503. py::arg("evalKey2"),
  504. py::arg("keyId") = "")
  505. .def("MultiAddEvalMultKeys", &CryptoContextImpl<DCRTPoly>::MultiAddEvalMultKeys,
  506. cc_MultiAddEvalMultKeys_docs,
  507. py::arg("evalKey1"),
  508. py::arg("evalKey2"),
  509. py::arg("keyId") = "")
  510. .def("IntMPBootAdjustScale",&CryptoContextImpl<DCRTPoly>::IntMPBootAdjustScale,
  511. cc_IntMPBootAdjustScale_docs,
  512. py::arg("ciphertext"))
  513. .def("IntMPBootRandomElementGen", &CryptoContextImpl<DCRTPoly>::IntMPBootRandomElementGen,
  514. cc_IntMPBootRandomElementGen_docs,
  515. py::arg("publicKey"))
  516. .def("IntMPBootDecrypt", &CryptoContextImpl<DCRTPoly>::IntMPBootDecrypt,
  517. cc_IntMPBootDecrypt_docs,
  518. py::arg("privateKey"),
  519. py::arg("ciphertext"),
  520. py::arg("a"))
  521. .def("IntMPBootAdd", &CryptoContextImpl<DCRTPoly>::IntMPBootAdd,
  522. cc_IntMPBootAdd_docs,
  523. py::arg("sharePairVec"))
  524. .def("IntMPBootEncrypt", &CryptoContextImpl<DCRTPoly>::IntMPBootEncrypt,
  525. cc_IntMPBootEncrypt_docs,
  526. py::arg("publicKey"),
  527. py::arg("sharePair"),
  528. py::arg("a"),
  529. py::arg("ciphertext"))
  530. .def("MultiMultEvalKey", &CryptoContextImpl<DCRTPoly>::MultiMultEvalKey,
  531. cc_MultiMultEvalKey_docs,
  532. py::arg("privateKey"),
  533. py::arg("evalKey"),
  534. py::arg("keyId") = "")
  535. .def("MultiAddEvalSumKeys", &CryptoContextImpl<DCRTPoly>::MultiAddEvalSumKeys,
  536. cc_MultiAddEvalSumKeys_docs,
  537. py::arg("evalKeyMap1"),
  538. py::arg("evalKeyMap2"),
  539. py::arg("keyId") = "")
  540. .def("EvalMerge", &CryptoContextImpl<DCRTPoly>::EvalMerge,
  541. cc_EvalMerge_docs,
  542. py::arg("ciphertextVec"))
  543. // use static_cast: inline EvalKey<Element> ReKeyGen(const PrivateKey<Element> oldPrivateKey, const PublicKey<Element> newPublicKey) const;
  544. .def("ReKeyGen", static_cast<EvalKey<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(const PrivateKey<DCRTPoly>, const PublicKey<DCRTPoly>) const>(&CryptoContextImpl<DCRTPoly>::ReKeyGen),
  545. cc_ReKeyGen_docs,
  546. py::arg("oldPrivateKey"),
  547. py::arg("newPublicKey"))
  548. .def("ReEncrypt", &CryptoContextImpl<DCRTPoly>::ReEncrypt,
  549. cc_ReEncrypt_docs,
  550. py::arg("ciphertext"),
  551. py::arg("evalKey"),
  552. py::arg("publicKey") = nullptr)
  553. .def("EvalPoly", &CryptoContextImpl<DCRTPoly>::EvalPoly,
  554. cc_EvalPoly_docs,
  555. py::arg("ciphertext"),
  556. py::arg("coefficients"))
  557. .def("EvalPolyLinear", &CryptoContextImpl<DCRTPoly>::EvalPolyLinear,
  558. cc_EvalPolyLinear_docs,
  559. py::arg("ciphertext"),
  560. py::arg("coefficients"))
  561. .def("EvalPolyPS", &CryptoContextImpl<DCRTPoly>::EvalPolyPS,
  562. cc_EvalPolyPS_docs,
  563. py::arg("ciphertext"),
  564. py::arg("coefficients"))
  565. .def("Rescale", &CryptoContextImpl<DCRTPoly>::Rescale,
  566. cc_Rescale_docs,
  567. py::arg("ciphertext"))
  568. .def("RescaleInPlace", &CryptoContextImpl<DCRTPoly>::RescaleInPlace,
  569. cc_RescaleInPlace_docs,
  570. py::arg("ciphertext"))
  571. .def("ModReduce", &CryptoContextImpl<DCRTPoly>::ModReduce,
  572. cc_ModReduce_docs,
  573. py::arg("ciphertext"))
  574. .def("ModReduceInPlace", &CryptoContextImpl<DCRTPoly>::ModReduceInPlace,
  575. cc_ModReduceInPlace_docs,
  576. py::arg("ciphertext"))
  577. .def("EvalBootstrapSetup", &CryptoContextImpl<DCRTPoly>::EvalBootstrapSetup,
  578. cc_EvalBootstrapSetup_docs,
  579. py::arg("levelBudget") = std::vector<uint32_t>({5, 4}),
  580. py::arg("dim1") = std::vector<uint32_t>({0, 0}),
  581. py::arg("slots") = 0,
  582. py::arg("correctionFactor") = 0,
  583. py::arg("precompute")= true)
  584. .def("EvalBootstrapKeyGen", &CryptoContextImpl<DCRTPoly>::EvalBootstrapKeyGen,
  585. cc_EvalBootstrapKeyGen_docs,
  586. py::arg("privateKey"),
  587. py::arg("slots"))
  588. .def("EvalBootstrap", &CryptoContextImpl<DCRTPoly>::EvalBootstrap,
  589. cc_EvalBootstrap_docs,
  590. py::arg("ciphertext"),
  591. py::arg("numIterations") = 1,
  592. py::arg("precision") = 0)
  593. .def("EvalCKKStoFHEWSetup", &CryptoContextImpl<DCRTPoly>::EvalCKKStoFHEWSetup,
  594. cc_EvalCKKStoFHEWSetup_docs,
  595. py::arg("schswchparams"))
  596. .def("EvalCKKStoFHEWKeyGen", &CryptoContextImpl<DCRTPoly>::EvalCKKStoFHEWKeyGen,
  597. cc_EvalCKKStoFHEWKeyGen_docs,
  598. py::arg("keyPair"),
  599. py::arg("lwesk"))
  600. .def("EvalCKKStoFHEWPrecompute", &CryptoContextImpl<DCRTPoly>::EvalCKKStoFHEWPrecompute,
  601. cc_EvalCKKStoFHEWPrecompute_docs,
  602. py::arg("scale") = 1.0)
  603. .def("EvalCKKStoFHEW", &CryptoContextImpl<DCRTPoly>::EvalCKKStoFHEW,
  604. cc_EvalCKKStoFHEW_docs,
  605. py::arg("ciphertext"),
  606. py::arg("numCtxts") = 0)
  607. .def("EvalFHEWtoCKKSSetup", &CryptoContextImpl<DCRTPoly>::EvalFHEWtoCKKSSetup,
  608. cc_EvalFHEWtoCKKSSetup_docs,
  609. py::arg("ccLWE"),
  610. py::arg("numSlotsCKKS") = 0,
  611. py::arg("logQ") = 25)
  612. .def("EvalFHEWtoCKKSKeyGen", &CryptoContextImpl<DCRTPoly>::EvalFHEWtoCKKSKeyGen,
  613. cc_EvalFHEWtoCKKSKeyGen_docs,
  614. py::arg("keyPair"),
  615. py::arg("lwesk"),
  616. py::arg("numSlots") = 0,
  617. py::arg("numCtxts") = 0,
  618. py::arg("dim1") = 0,
  619. py::arg("L") = 0)
  620. .def("EvalFHEWtoCKKS", &CryptoContextImpl<DCRTPoly>::EvalFHEWtoCKKS,
  621. cc_EvalFHEWtoCKKS_docs,
  622. py::arg("LWECiphertexts"),
  623. py::arg("numCtxts") = 0,
  624. py::arg("numSlots") = 0,
  625. py::arg("p") = 4,
  626. py::arg("pmin") = 0.0,
  627. py::arg("pmax") = 2.0,
  628. py::arg("dim1") = 0)
  629. .def("EvalSchemeSwitchingSetup", &CryptoContextImpl<DCRTPoly>::EvalSchemeSwitchingSetup,
  630. cc_EvalSchemeSwitchingSetup_docs,
  631. py::arg("schswchparams"))
  632. //void EvalSchemeSwitchingKeyGen(const KeyPair<Element> &keyPair, ConstLWEPrivateKey &lwesk, uint32_t numValues = 0, bool oneHot = true, bool alt = false, uint32_t dim1CF = 0, uint32_t dim1FC = 0, uint32_t LCF = 1, uint32_t LFC = 0)
  633. .def("EvalSchemeSwitchingKeyGen", &CryptoContextImpl<DCRTPoly>::EvalSchemeSwitchingKeyGen,
  634. cc_EvalSchemeSwitchingKeyGen_docs,
  635. py::arg("keyPair"),
  636. py::arg("lwesk"))
  637. .def("EvalCompareSwitchPrecompute", &CryptoContextImpl<DCRTPoly>::EvalCompareSwitchPrecompute,
  638. cc_EvalCompareSwitchPrecompute_docs,
  639. py::arg("pLWE") = 0,
  640. py::arg("scaleSign") = 1.0,
  641. py::arg("unit") = false)
  642. .def("EvalCompareSchemeSwitching", &CryptoContextImpl<DCRTPoly>::EvalCompareSchemeSwitching,
  643. cc_EvalCompareSchemeSwitching_docs,
  644. py::arg("ciphertext1"),
  645. py::arg("ciphertext2"),
  646. py::arg("numCtxts") = 0,
  647. py::arg("numSlots") = 0,
  648. py::arg("pLWE") = 0,
  649. py::arg("scaleSign") = 1.0,
  650. py::arg("unit") = false)
  651. .def("EvalMinSchemeSwitching", &CryptoContextImpl<DCRTPoly>::EvalMinSchemeSwitching,
  652. cc_EvalMinSchemeSwitching_docs,
  653. py::arg("ciphertext"),
  654. py::arg("publicKey"),
  655. py::arg("numValues") = 0,
  656. py::arg("numSlots") = 0,
  657. py::arg("pLWE") = 0,
  658. py::arg("scaleSign") = 1.0)
  659. .def("EvalMinSchemeSwitchingAlt", &CryptoContextImpl<DCRTPoly>::EvalMinSchemeSwitchingAlt,
  660. cc_EvalMinSchemeSwitchingAlt_docs,
  661. py::arg("ciphertext"),
  662. py::arg("publicKey"),
  663. py::arg("numValues") = 0,
  664. py::arg("numSlots") = 0,
  665. py::arg("pLWE") = 0,
  666. py::arg("scaleSign") = 1.0)
  667. .def("EvalMaxSchemeSwitching", &CryptoContextImpl<DCRTPoly>::EvalMaxSchemeSwitching,
  668. cc_EvalMaxSchemeSwitching_docs,
  669. py::arg("ciphertext"),
  670. py::arg("publicKey"),
  671. py::arg("numValues") = 0,
  672. py::arg("numSlots") = 0,
  673. py::arg("pLWE") = 0,
  674. py::arg("scaleSign") = 1.0)
  675. .def("EvalMaxSchemeSwitchingAlt", &CryptoContextImpl<DCRTPoly>::EvalMaxSchemeSwitchingAlt,
  676. cc_EvalMaxSchemeSwitchingAlt_docs,
  677. py::arg("ciphertext"),
  678. py::arg("publicKey"),
  679. py::arg("numValues") = 0,
  680. py::arg("numSlots") = 0,
  681. py::arg("pLWE") = 0,
  682. py::arg("scaleSign") = 1.0)
  683. //TODO (Oliveira, R.): Solve pointer handling bug when returning EvalKeyMap objects for the next functions
  684. .def("EvalAutomorphismKeyGen", &EvalAutomorphismKeyGenWrapper,
  685. cc_EvalAutomorphismKeyGen_docs,
  686. py::arg("privateKey"),
  687. py::arg("indexList"),
  688. py::return_value_policy::reference_internal)
  689. .def("FindAutomorphismIndex", &CryptoContextImpl<DCRTPoly>::FindAutomorphismIndex,
  690. cc_FindAutomorphismIndex_docs,
  691. py::arg("idx"))
  692. .def("FindAutomorphismIndices", &CryptoContextImpl<DCRTPoly>::FindAutomorphismIndices,
  693. cc_FindAutomorphismIndices_docs,
  694. py::arg("idxList"))
  695. .def_static(
  696. "ClearEvalMultKeys", []()
  697. { CryptoContextImpl<DCRTPoly>::ClearEvalMultKeys(); },
  698. cc_ClearEvalMultKeys_docs)
  699. .def_static(
  700. "InsertEvalSumKey", &CryptoContextImpl<DCRTPoly>::InsertEvalSumKey,
  701. cc_InsertEvalSumKey_docs,
  702. py::arg("evalKeyMap"),
  703. py::arg("keyTag") = "")
  704. .def_static(
  705. "InsertEvalMultKey", &CryptoContextImpl<DCRTPoly>::InsertEvalMultKey,
  706. cc_InsertEvalMultKey_docs,
  707. py::arg("evalKeyVec"))
  708. .def_static(
  709. "ClearEvalAutomorphismKeys", []()
  710. { CryptoContextImpl<DCRTPoly>::ClearEvalAutomorphismKeys(); },
  711. cc_ClearEvalAutomorphismKeys_docs)
  712. .def("GetEvalSumKeyMap", &GetEvalSumKeyMapWrapper,
  713. cc_GetEvalSumKeyMap_docs,
  714. py::return_value_policy::reference)
  715. .def("GetBinCCForSchemeSwitch", &CryptoContextImpl<DCRTPoly>::GetBinCCForSchemeSwitch,
  716. py::return_value_policy::reference_internal)
  717. .def_static(
  718. "SerializeEvalMultKey", [](const std::string &filename, const SerType::SERBINARY &sertype, std::string id = "")
  719. {
  720. std::ofstream outfile(filename,std::ios::out | std::ios::binary);
  721. bool res;
  722. res = CryptoContextImpl<DCRTPoly>::SerializeEvalMultKey<SerType::SERBINARY>(outfile, sertype, id);
  723. outfile.close();
  724. return res; },
  725. cc_SerializeEvalMultKey_docs,
  726. py::arg("filename"), py::arg("sertype"), py::arg("id") = "")
  727. .def_static( // SerializeEvalMultKey - JSON
  728. "SerializeEvalMultKey", [](const std::string &filename, const SerType::SERJSON &sertype, std::string id = "")
  729. {
  730. std::ofstream outfile(filename,std::ios::out | std::ios::binary);
  731. bool res;
  732. res = CryptoContextImpl<DCRTPoly>::SerializeEvalMultKey<SerType::SERJSON>(outfile, sertype, id);
  733. outfile.close();
  734. return res; },
  735. cc_SerializeEvalMultKey_docs,
  736. py::arg("filename"), py::arg("sertype"), py::arg("id") = "")
  737. .def_static( // SerializeEvalAutomorphismKey - Binary
  738. "SerializeEvalAutomorphismKey", [](const std::string &filename, const SerType::SERBINARY &sertype, std::string id = "")
  739. {
  740. std::ofstream outfile(filename,std::ios::out | std::ios::binary);
  741. bool res;
  742. res = CryptoContextImpl<DCRTPoly>::SerializeEvalAutomorphismKey<SerType::SERBINARY>(outfile, sertype, id);
  743. outfile.close();
  744. return res; },
  745. cc_SerializeEvalAutomorphismKey_docs,
  746. py::arg("filename"), py::arg("sertype"), py::arg("id") = "")
  747. .def_static( // SerializeEvalAutomorphismKey - JSON
  748. "SerializeEvalAutomorphismKey", [](const std::string &filename, const SerType::SERJSON &sertype, std::string id = "")
  749. {
  750. std::ofstream outfile(filename,std::ios::out | std::ios::binary);
  751. bool res;
  752. res = CryptoContextImpl<DCRTPoly>::SerializeEvalAutomorphismKey<SerType::SERJSON>(outfile, sertype, id);
  753. outfile.close();
  754. return res; },
  755. cc_SerializeEvalAutomorphismKey_docs,
  756. py::arg("filename"), py::arg("sertype"), py::arg("id") = "")
  757. .def_static("DeserializeEvalMultKey", // DeserializeEvalMultKey - Binary
  758. [](const std::string &filename, const SerType::SERBINARY &sertype)
  759. {
  760. std::ifstream emkeys(filename, std::ios::in | std::ios::binary);
  761. if (!emkeys.is_open()) {
  762. std::cerr << "I cannot read serialization from " << filename << std::endl;
  763. }
  764. bool res;
  765. res = CryptoContextImpl<DCRTPoly>::DeserializeEvalMultKey<SerType::SERBINARY>(emkeys, sertype);
  766. return res;
  767. },
  768. cc_DeserializeEvalMultKey_docs,
  769. py::arg("filename"), py::arg("sertype"))
  770. .def_static("DeserializeEvalMultKey", // DeserializeEvalMultKey - JSON
  771. [](const std::string &filename, const SerType::SERJSON &sertype)
  772. {
  773. std::ifstream emkeys(filename, std::ios::in | std::ios::binary);
  774. if (!emkeys.is_open()) {
  775. std::cerr << "I cannot read serialization from " << filename << std::endl;
  776. }
  777. bool res;
  778. res = CryptoContextImpl<DCRTPoly>::DeserializeEvalMultKey<SerType::SERJSON>(emkeys, sertype);
  779. return res; },
  780. cc_DeserializeEvalMultKey_docs,
  781. py::arg("filename"), py::arg("sertype"))
  782. .def_static("DeserializeEvalAutomorphismKey", // DeserializeEvalAutomorphismKey - Binary
  783. [](const std::string &filename, const SerType::SERBINARY &sertype)
  784. {
  785. std::ifstream erkeys(filename, std::ios::in | std::ios::binary);
  786. if (!erkeys.is_open()) {
  787. std::cerr << "I cannot read serialization from " << filename << std::endl;
  788. }
  789. bool res;
  790. res = CryptoContextImpl<DCRTPoly>::DeserializeEvalAutomorphismKey<SerType::SERBINARY>(erkeys, sertype);
  791. return res; },
  792. cc_DeserializeEvalAutomorphismKey_docs,
  793. py::arg("filename"), py::arg("sertype"))
  794. .def_static("DeserializeEvalAutomorphismKey", // DeserializeEvalAutomorphismKey - JSON
  795. [](const std::string &filename, const SerType::SERJSON &sertype)
  796. {
  797. std::ifstream erkeys(filename, std::ios::in | std::ios::binary);
  798. if (!erkeys.is_open()) {
  799. std::cerr << "I cannot read serialization from " << filename << std::endl;
  800. }
  801. bool res;
  802. res = CryptoContextImpl<DCRTPoly>::DeserializeEvalAutomorphismKey<SerType::SERJSON>(erkeys, sertype);
  803. return res; },
  804. cc_DeserializeEvalAutomorphismKey_docs,
  805. py::arg("filename"), py::arg("sertype"));
  806. // Generator Functions
  807. m.def("GenCryptoContext", &GenCryptoContext<CryptoContextBFVRNS>,
  808. py::arg("params"));
  809. m.def("GenCryptoContext", &GenCryptoContext<CryptoContextBGVRNS>,
  810. py::arg("params"));
  811. m.def("GenCryptoContext", &GenCryptoContext<CryptoContextCKKSRNS>,
  812. py::arg("params"));
  813. m.def("ReleaseAllContexts", &CryptoContextFactory<DCRTPoly>::ReleaseAllContexts);
  814. m.def("GetAllContexts", &CryptoContextFactory<DCRTPoly>::GetAllContexts);
  815. }
  816. int get_native_int(){
  817. #if NATIVEINT == 128 && !defined(__EMSCRIPTEN__)
  818. return 128;
  819. #else
  820. return 64;
  821. #endif
  822. }
  823. void bind_enums_and_constants(py::module &m)
  824. {
  825. /* ---- PKE enums ---- */
  826. // Scheme Types
  827. py::enum_<SCHEME>(m, "SCHEME")
  828. .value("INVALID_SCHEME", SCHEME::INVALID_SCHEME)
  829. .value("CKKSRNS_SCHEME", SCHEME::CKKSRNS_SCHEME)
  830. .value("BFVRNS_SCHEME", SCHEME::BFVRNS_SCHEME)
  831. .value("BGVRNS_SCHEME", SCHEME::BGVRNS_SCHEME);
  832. m.attr("INVALID_SCHEME") = py::cast(SCHEME::INVALID_SCHEME);
  833. m.attr("CKKSRNS_SCHEME") = py::cast(SCHEME::CKKSRNS_SCHEME);
  834. m.attr("BFVRNS_SCHEME") = py::cast(SCHEME::BFVRNS_SCHEME);
  835. m.attr("BGVRNS_SCHEME") = py::cast(SCHEME::BGVRNS_SCHEME);
  836. // PKE Features
  837. py::enum_<PKESchemeFeature>(m, "PKESchemeFeature")
  838. .value("PKE", PKESchemeFeature::PKE)
  839. .value("KEYSWITCH", PKESchemeFeature::KEYSWITCH)
  840. .value("PRE", PKESchemeFeature::PRE)
  841. .value("LEVELEDSHE", PKESchemeFeature::LEVELEDSHE)
  842. .value("ADVANCEDSHE", PKESchemeFeature::ADVANCEDSHE)
  843. .value("MULTIPARTY", PKESchemeFeature::MULTIPARTY)
  844. .value("FHE", PKESchemeFeature::FHE)
  845. .value("SCHEMESWITCH", PKESchemeFeature::SCHEMESWITCH);
  846. m.attr("PKE") = py::cast(PKESchemeFeature::PKE);
  847. m.attr("KEYSWITCH") = py::cast(PKESchemeFeature::KEYSWITCH);
  848. m.attr("PRE") = py::cast(PKESchemeFeature::PRE);
  849. m.attr("LEVELEDSHE") = py::cast(PKESchemeFeature::LEVELEDSHE);
  850. m.attr("ADVANCEDSHE") = py::cast(PKESchemeFeature::ADVANCEDSHE);
  851. m.attr("MULTIPARTY") = py::cast(PKESchemeFeature::MULTIPARTY);
  852. m.attr("FHE") = py::cast(PKESchemeFeature::FHE);
  853. m.attr("SCHEMESWITCH") = py::cast(PKESchemeFeature::SCHEMESWITCH);
  854. // Plaintext enums
  855. py::enum_<Format>(m, "Format")
  856. .value("EVALUATION", Format::EVALUATION)
  857. .value("COEFFICIENT", Format::COEFFICIENT);
  858. m.attr("EVALUATION") = py::cast(Format::EVALUATION);
  859. m.attr("COEFFICIENT") = py::cast(Format::COEFFICIENT);
  860. // Serialization Types
  861. py::class_<SerType::SERJSON>(m, "SERJSON");
  862. py::class_<SerType::SERBINARY>(m, "SERBINARY");
  863. m.attr("JSON") = py::cast(SerType::JSON);
  864. m.attr("BINARY") = py::cast(SerType::BINARY);
  865. // Scaling Techniques
  866. py::enum_<ScalingTechnique>(m, "ScalingTechnique")
  867. .value("FIXEDMANUAL", ScalingTechnique::FIXEDMANUAL)
  868. .value("FIXEDAUTO", ScalingTechnique::FIXEDAUTO)
  869. .value("FLEXIBLEAUTO", ScalingTechnique::FLEXIBLEAUTO)
  870. .value("FLEXIBLEAUTOEXT", ScalingTechnique::FLEXIBLEAUTOEXT)
  871. .value("NORESCALE", ScalingTechnique::NORESCALE)
  872. .value("INVALID_RS_TECHNIQUE", ScalingTechnique::INVALID_RS_TECHNIQUE);
  873. m.attr("FIXEDMANUAL") = py::cast(ScalingTechnique::FIXEDMANUAL);
  874. m.attr("FIXEDAUTO") = py::cast(ScalingTechnique::FIXEDAUTO);
  875. m.attr("FLEXIBLEAUTO") = py::cast(ScalingTechnique::FLEXIBLEAUTO);
  876. m.attr("FLEXIBLEAUTOEXT") = py::cast(ScalingTechnique::FLEXIBLEAUTOEXT);
  877. m.attr("NORESCALE") = py::cast(ScalingTechnique::NORESCALE);
  878. m.attr("INVALID_RS_TECHNIQUE") = py::cast(ScalingTechnique::INVALID_RS_TECHNIQUE);
  879. // Key Switching Techniques
  880. py::enum_<KeySwitchTechnique>(m, "KeySwitchTechnique")
  881. .value("INVALID_KS_TECH", KeySwitchTechnique::INVALID_KS_TECH)
  882. .value("BV", KeySwitchTechnique::BV)
  883. .value("HYBRID", KeySwitchTechnique::HYBRID);
  884. m.attr("INVALID_KS_TECH") = py::cast(KeySwitchTechnique::INVALID_KS_TECH);
  885. m.attr("BV") = py::cast(KeySwitchTechnique::BV);
  886. m.attr("HYBRID") = py::cast(KeySwitchTechnique::HYBRID);
  887. // Secret Key Dist
  888. py::enum_<SecretKeyDist>(m, "SecretKeyDist")
  889. .value("GAUSSIAN", SecretKeyDist::GAUSSIAN)
  890. .value("UNIFORM_TERNARY", SecretKeyDist::UNIFORM_TERNARY)
  891. .value("SPARSE_TERNARY", SecretKeyDist::SPARSE_TERNARY);
  892. m.attr("GAUSSIAN") = py::cast(SecretKeyDist::GAUSSIAN);
  893. m.attr("UNIFORM_TERNARY") = py::cast(SecretKeyDist::UNIFORM_TERNARY);
  894. m.attr("SPARSE_TERNARY") = py::cast(SecretKeyDist::SPARSE_TERNARY);
  895. // ProxyReEncryptionMode
  896. py::enum_<ProxyReEncryptionMode>(m, "ProxyReEncryptionMode")
  897. .value("NOT_SET", ProxyReEncryptionMode::NOT_SET)
  898. .value("INDCPA", ProxyReEncryptionMode::INDCPA)
  899. .value("FIXED_NOISE_HRA", ProxyReEncryptionMode::FIXED_NOISE_HRA)
  900. .value("NOISE_FLOODING_HRA", ProxyReEncryptionMode::NOISE_FLOODING_HRA)
  901. .value("DIVIDE_AND_ROUND_HRA", ProxyReEncryptionMode::DIVIDE_AND_ROUND_HRA);
  902. m.attr("NOT_SET") = py::cast(ProxyReEncryptionMode::NOT_SET);
  903. m.attr("INDCPA") = py::cast(ProxyReEncryptionMode::INDCPA);
  904. m.attr("FIXED_NOISE_HRA") = py::cast(ProxyReEncryptionMode::FIXED_NOISE_HRA);
  905. m.attr("NOISE_FLOODING_HRA") = py::cast(ProxyReEncryptionMode::NOISE_FLOODING_HRA);
  906. m.attr("DIVIDE_AND_ROUND_HRA") = py::cast(ProxyReEncryptionMode::DIVIDE_AND_ROUND_HRA);
  907. // MultipartyMode
  908. py::enum_<MultipartyMode>(m, "MultipartyMode")
  909. .value("INVALID_MULTIPARTY_MODE", MultipartyMode::INVALID_MULTIPARTY_MODE)
  910. .value("FIXED_NOISE_MULTIPARTY", MultipartyMode::FIXED_NOISE_MULTIPARTY)
  911. .value("NOISE_FLOODING_MULTIPARTY", MultipartyMode::NOISE_FLOODING_MULTIPARTY);
  912. m.attr("INVALID_MULTIPARTY_MODE") = py::cast(MultipartyMode::INVALID_MULTIPARTY_MODE);
  913. m.attr("FIXED_NOISE_MULTIPARTY") = py::cast(MultipartyMode::FIXED_NOISE_MULTIPARTY);
  914. m.attr("NOISE_FLOODING_MULTIPARTY") = py::cast(MultipartyMode::NOISE_FLOODING_MULTIPARTY);
  915. // ExecutionMode
  916. py::enum_<ExecutionMode>(m, "ExecutionMode")
  917. .value("EXEC_EVALUATION", ExecutionMode::EXEC_EVALUATION)
  918. .value("EXEC_NOISE_ESTIMATION", ExecutionMode::EXEC_NOISE_ESTIMATION);
  919. m.attr("EXEC_EVALUATION") = py::cast(ExecutionMode::EXEC_EVALUATION);
  920. m.attr("EXEC_NOISE_ESTIMATION") = py::cast(ExecutionMode::EXEC_NOISE_ESTIMATION);
  921. // DecryptionNoiseMode
  922. py::enum_<DecryptionNoiseMode>(m, "DecryptionNoiseMode")
  923. .value("FIXED_NOISE_DECRYPT", DecryptionNoiseMode::FIXED_NOISE_DECRYPT)
  924. .value("NOISE_FLOODING_DECRYPT", DecryptionNoiseMode::NOISE_FLOODING_DECRYPT);
  925. m.attr("FIXED_NOISE_DECRYPT") = py::cast(DecryptionNoiseMode::FIXED_NOISE_DECRYPT);
  926. m.attr("NOISE_FLOODING_DECRYPT") = py::cast(DecryptionNoiseMode::NOISE_FLOODING_DECRYPT);
  927. // EncryptionTechnique
  928. py::enum_<EncryptionTechnique>(m, "EncryptionTechnique")
  929. .value("STANDARD", EncryptionTechnique::STANDARD)
  930. .value("EXTENDED", EncryptionTechnique::EXTENDED);
  931. m.attr("STANDARD") = py::cast(EncryptionTechnique::STANDARD);
  932. m.attr("EXTENDED") = py::cast(EncryptionTechnique::EXTENDED);
  933. // MultiplicationTechnique
  934. py::enum_<MultiplicationTechnique>(m, "MultiplicationTechnique")
  935. .value("BEHZ", MultiplicationTechnique::BEHZ)
  936. .value("HPS", MultiplicationTechnique::HPS)
  937. .value("HPSPOVERQ", MultiplicationTechnique::HPSPOVERQ)
  938. .value("HPSPOVERQLEVELED", MultiplicationTechnique::HPSPOVERQLEVELED);
  939. m.attr("BEHZ") = py::cast(MultiplicationTechnique::BEHZ);
  940. m.attr("HPS") = py::cast(MultiplicationTechnique::HPS);
  941. m.attr("HPSPOVERQ") = py::cast(MultiplicationTechnique::HPSPOVERQ);
  942. m.attr("HPSPOVERQLEVELED") = py::cast(MultiplicationTechnique::HPSPOVERQLEVELED);
  943. // Compression Leval
  944. py::enum_<COMPRESSION_LEVEL>(m,"COMPRESSION_LEVEL")
  945. .value("COMPACT", COMPRESSION_LEVEL::COMPACT)
  946. .value("SLACK", COMPRESSION_LEVEL::SLACK);
  947. m.attr("COMPACT") = py::cast(COMPRESSION_LEVEL::COMPACT);
  948. m.attr("SLACK") = py::cast(COMPRESSION_LEVEL::SLACK);
  949. /* ---- CORE enums ---- */
  950. // Security Level
  951. py::enum_<SecurityLevel>(m,"SecurityLevel")
  952. .value("HEStd_128_classic", SecurityLevel::HEStd_128_classic)
  953. .value("HEStd_192_classic", SecurityLevel::HEStd_192_classic)
  954. .value("HEStd_256_classic", SecurityLevel::HEStd_256_classic)
  955. .value("HEStd_NotSet", SecurityLevel::HEStd_NotSet);
  956. m.attr("HEStd_128_classic") = py::cast(SecurityLevel::HEStd_128_classic);
  957. m.attr("HEStd_192_classic") = py::cast(SecurityLevel::HEStd_192_classic);
  958. m.attr("HEStd_256_classic") = py::cast(SecurityLevel::HEStd_256_classic);
  959. m.attr("HEStd_NotSet") = py::cast(SecurityLevel::HEStd_NotSet);
  960. //Parameters Type
  961. /*TODO (Oliveira): If we expose Poly's and ParmType, this block will go somewhere else */
  962. using ParmType = typename DCRTPoly::Params;
  963. py::class_<ParmType, std::shared_ptr<ParmType>>(m, "ParmType");
  964. //NATIVEINT function
  965. m.def("get_native_int", &get_native_int);
  966. }
  967. void bind_keys(py::module &m)
  968. {
  969. py::class_<PublicKeyImpl<DCRTPoly>, std::shared_ptr<PublicKeyImpl<DCRTPoly>>>(m, "PublicKey")
  970. .def(py::init<>())
  971. .def("GetKeyTag", &PublicKeyImpl<DCRTPoly>::GetKeyTag)
  972. .def("SetKeyTag", &PublicKeyImpl<DCRTPoly>::SetKeyTag);
  973. py::class_<PrivateKeyImpl<DCRTPoly>, std::shared_ptr<PrivateKeyImpl<DCRTPoly>>>(m, "PrivateKey")
  974. .def(py::init<>())
  975. .def("GetKeyTag", &PrivateKeyImpl<DCRTPoly>::GetKeyTag)
  976. .def("SetKeyTag", &PrivateKeyImpl<DCRTPoly>::SetKeyTag);
  977. py::class_<KeyPair<DCRTPoly>>(m, "KeyPair")
  978. .def_readwrite("publicKey", &KeyPair<DCRTPoly>::publicKey)
  979. .def_readwrite("secretKey", &KeyPair<DCRTPoly>::secretKey)
  980. .def("good", &KeyPair<DCRTPoly>::good,kp_good_docs);
  981. py::class_<EvalKeyImpl<DCRTPoly>, std::shared_ptr<EvalKeyImpl<DCRTPoly>>>(m, "EvalKey")
  982. .def(py::init<>())
  983. .def("GetKeyTag", &EvalKeyImpl<DCRTPoly>::GetKeyTag)
  984. .def("SetKeyTag", &EvalKeyImpl<DCRTPoly>::SetKeyTag);
  985. py::class_<std::map<usint, EvalKey<DCRTPoly>>, std::shared_ptr<std::map<usint, EvalKey<DCRTPoly>>>>(m, "EvalKeyMap")
  986. .def(py::init<>());
  987. }
  988. void bind_encodings(py::module &m)
  989. {
  990. py::class_<PlaintextImpl, std::shared_ptr<PlaintextImpl>>(m, "Plaintext")
  991. .def("GetScalingFactor", &PlaintextImpl::GetScalingFactor,
  992. ptx_GetScalingFactor_docs)
  993. .def("SetScalingFactor", &PlaintextImpl::SetScalingFactor,
  994. ptx_SetScalingFactor_docs,
  995. py::arg("sf"))
  996. .def("GetSchemeID", &PlaintextImpl::GetSchemeID,
  997. ptx_GetSchemeID_docs)
  998. .def("GetLength", &PlaintextImpl::GetLength,
  999. ptx_GetLength_docs)
  1000. .def("GetSchemeID", &PlaintextImpl::GetSchemeID,
  1001. ptx_GetSchemeID_docs)
  1002. .def("SetLength", &PlaintextImpl::SetLength,
  1003. ptx_SetLength_docs,
  1004. py::arg("newSize"))
  1005. .def("IsEncoded", &PlaintextImpl::IsEncoded,
  1006. ptx_IsEncoded_docs)
  1007. .def("GetLogPrecision", &PlaintextImpl::GetLogPrecision,
  1008. ptx_GetLogPrecision_docs)
  1009. .def("Encode", &PlaintextImpl::Encode,
  1010. ptx_Encode_docs)
  1011. .def("Decode", &PlaintextImpl::Decode,
  1012. ptx_Decode_docs)
  1013. .def("LowBound", &PlaintextImpl::LowBound,
  1014. ptx_LowBound_docs)
  1015. .def("HighBound", &PlaintextImpl::HighBound,
  1016. ptx_HighBound_docs)
  1017. .def("SetFormat", &PlaintextImpl::SetFormat,
  1018. ptx_SetFormat_docs,
  1019. py::arg("fmt"))
  1020. .def("GetPackedValue", &PlaintextImpl::GetPackedValue)
  1021. .def("GetCoefPackedValue", &PlaintextImpl::GetCoefPackedValue)
  1022. .def("GetCKKSPackedValue", &PlaintextImpl::GetCKKSPackedValue,
  1023. ptx_GetCKKSPackedValue_docs)
  1024. .def("GetRealPackedValue", &PlaintextImpl::GetRealPackedValue,
  1025. ptx_GetRealPackedValue_docs)
  1026. .def("GetLevel", &PlaintextImpl::GetLevel)
  1027. .def("SetLevel", &PlaintextImpl::SetLevel)
  1028. .def("GetNoiseScaleDeg", &PlaintextImpl::GetNoiseScaleDeg)
  1029. .def("SetNoiseScaleDeg", &PlaintextImpl::SetNoiseScaleDeg)
  1030. .def("GetSlots", &PlaintextImpl::GetSlots)
  1031. .def("SetSlots", &PlaintextImpl::SetSlots)
  1032. .def("GetLogError", &PlaintextImpl::GetLogError)
  1033. .def("GetLogPrecision", &PlaintextImpl::GetLogPrecision)
  1034. .def("GetStringValue", &PlaintextImpl::GetStringValue)
  1035. .def("SetStringValue", &PlaintextImpl::SetStringValue)
  1036. .def("SetIntVectorValue", &PlaintextImpl::SetIntVectorValue)
  1037. .def("__repr__", [](const PlaintextImpl &p)
  1038. {
  1039. std::stringstream ss;
  1040. ss << "<Plaintext Object: ";
  1041. p.PrintValue(ss);
  1042. ss << ">";
  1043. return ss.str(); })
  1044. .def("__str__", [](const PlaintextImpl &p)
  1045. {
  1046. std::stringstream ss;
  1047. p.PrintValue(ss);
  1048. return ss.str(); });
  1049. }
  1050. void bind_ciphertext(py::module &m)
  1051. {
  1052. py::class_<CiphertextImpl<DCRTPoly>, std::shared_ptr<CiphertextImpl<DCRTPoly>>>(m, "Ciphertext")
  1053. .def(py::init<>())
  1054. .def("__add__", [](const Ciphertext<DCRTPoly> &a, const Ciphertext<DCRTPoly> &b)
  1055. {return a + b; },py::is_operator(),pybind11::keep_alive<0, 1>())
  1056. // .def(py::self + py::self);
  1057. // .def("GetDepth", &CiphertextImpl<DCRTPoly>::GetDepth)
  1058. // .def("SetDepth", &CiphertextImpl<DCRTPoly>::SetDepth)
  1059. .def("GetLevel", &CiphertextImpl<DCRTPoly>::GetLevel,
  1060. ctx_GetLevel_docs)
  1061. .def("SetLevel", &CiphertextImpl<DCRTPoly>::SetLevel,
  1062. ctx_SetLevel_docs,
  1063. py::arg("level"))
  1064. .def("Clone", &CiphertextImpl<DCRTPoly>::Clone)
  1065. .def("RemoveElement", &RemoveElementWrapper, cc_RemoveElement_docs)
  1066. // .def("GetHopLevel", &CiphertextImpl<DCRTPoly>::GetHopLevel)
  1067. // .def("SetHopLevel", &CiphertextImpl<DCRTPoly>::SetHopLevel)
  1068. // .def("GetScalingFactor", &CiphertextImpl<DCRTPoly>::GetScalingFactor)
  1069. // .def("SetScalingFactor", &CiphertextImpl<DCRTPoly>::SetScalingFactor)
  1070. .def("GetSlots", &CiphertextImpl<DCRTPoly>::GetSlots)
  1071. .def("SetSlots", &CiphertextImpl<DCRTPoly>::SetSlots);
  1072. }
  1073. void bind_schemes(py::module &m){
  1074. /*Bind schemes specific functionalities like bootstrapping functions and multiparty*/
  1075. py::class_<FHECKKSRNS>(m, "FHECKKSRNS")
  1076. .def(py::init<>())
  1077. //.def_static("GetBootstrapDepth", &FHECKKSRNS::GetBootstrapDepth)
  1078. .def_static("GetBootstrapDepth", static_cast<uint32_t (*)(uint32_t, const std::vector<uint32_t>&, SecretKeyDist)>(&FHECKKSRNS::GetBootstrapDepth))
  1079. .def_static("GetBootstrapDepth", static_cast<uint32_t (*)(const std::vector<uint32_t>&, SecretKeyDist)>(&FHECKKSRNS::GetBootstrapDepth));
  1080. }
  1081. void bind_sch_swch_params(py::module &m)
  1082. {
  1083. py::class_<SchSwchParams>(m, "SchSwchParams")
  1084. .def(py::init<>())
  1085. .def("GetSecurityLevelCKKS", &SchSwchParams::GetSecurityLevelCKKS)
  1086. .def("GetSecurityLevelFHEW", &SchSwchParams::GetSecurityLevelFHEW)
  1087. .def("GetArbitraryFunctionEvaluation", &SchSwchParams::GetArbitraryFunctionEvaluation)
  1088. .def("GetUseDynamicModeFHEW", &SchSwchParams::GetUseDynamicModeFHEW)
  1089. .def("GetComputeArgmin", &SchSwchParams::GetComputeArgmin)
  1090. .def("GetOneHotEncoding", &SchSwchParams::GetOneHotEncoding)
  1091. .def("GetUseAltArgmin", &SchSwchParams::GetUseAltArgmin)
  1092. .def("GetNumSlotsCKKS", &SchSwchParams::GetNumSlotsCKKS)
  1093. .def("GetNumValues", &SchSwchParams::GetNumValues)
  1094. .def("GetCtxtModSizeFHEWLargePrec", &SchSwchParams::GetCtxtModSizeFHEWLargePrec)
  1095. .def("GetCtxtModSizeFHEWIntermedSwch", &SchSwchParams::GetCtxtModSizeFHEWIntermedSwch)
  1096. .def("GetBStepLTrCKKStoFHEW", &SchSwchParams::GetBStepLTrCKKStoFHEW)
  1097. .def("GetBStepLTrFHEWtoCKKS", &SchSwchParams::GetBStepLTrFHEWtoCKKS)
  1098. .def("GetLevelLTrCKKStoFHEW", &SchSwchParams::GetLevelLTrCKKStoFHEW)
  1099. .def("GetLevelLTrFHEWtoCKKS", &SchSwchParams::GetLevelLTrFHEWtoCKKS)
  1100. .def("GetInitialCKKSModulus", &SchSwchParams::GetInitialCKKSModulus)
  1101. .def("GetRingDimension", &SchSwchParams::GetRingDimension)
  1102. .def("GetScalingModSize", &SchSwchParams::GetScalingModSize)
  1103. .def("GetBatchSize", &SchSwchParams::GetBatchSize)
  1104. .def("SetSecurityLevelCKKS", &SchSwchParams::SetSecurityLevelCKKS)
  1105. .def("SetSecurityLevelFHEW", &SchSwchParams::SetSecurityLevelFHEW)
  1106. .def("SetArbitraryFunctionEvaluation", &SchSwchParams::SetArbitraryFunctionEvaluation)
  1107. .def("SetUseDynamicModeFHEW", &SchSwchParams::SetUseDynamicModeFHEW)
  1108. .def("SetComputeArgmin", &SchSwchParams::SetComputeArgmin)
  1109. .def("SetOneHotEncoding", &SchSwchParams::SetOneHotEncoding)
  1110. .def("SetUseAltArgmin", &SchSwchParams::SetUseAltArgmin)
  1111. .def("SetNumSlotsCKKS", &SchSwchParams::SetNumSlotsCKKS)
  1112. .def("SetNumValues", &SchSwchParams::SetNumValues)
  1113. .def("SetCtxtModSizeFHEWLargePrec", &SchSwchParams::SetCtxtModSizeFHEWLargePrec)
  1114. .def("SetCtxtModSizeFHEWIntermedSwch", &SchSwchParams::SetCtxtModSizeFHEWIntermedSwch)
  1115. .def("SetBStepLTrCKKStoFHEW", &SchSwchParams::SetBStepLTrCKKStoFHEW)
  1116. .def("SetBStepLTrFHEWtoCKKS", &SchSwchParams::SetBStepLTrFHEWtoCKKS)
  1117. .def("SetLevelLTrCKKStoFHEW", &SchSwchParams::SetLevelLTrCKKStoFHEW)
  1118. .def("SetLevelLTrFHEWtoCKKS", &SchSwchParams::SetLevelLTrFHEWtoCKKS)
  1119. .def("SetInitialCKKSModulus", &SchSwchParams::SetInitialCKKSModulus)
  1120. .def("SetRingDimension", &SchSwchParams::SetRingDimension)
  1121. .def("SetScalingModSize", &SchSwchParams::SetScalingModSize)
  1122. .def("SetBatchSize", &SchSwchParams::SetBatchSize)
  1123. .def("__str__",[](const SchSwchParams &params) {
  1124. std::stringstream stream;
  1125. stream << params;
  1126. return stream.str();
  1127. });
  1128. }
  1129. PYBIND11_MODULE(openfhe, m)
  1130. {
  1131. m.doc() = "Open-Source Fully Homomorphic Encryption Library";
  1132. // binfhe library
  1133. bind_binfhe_enums(m);
  1134. bind_binfhe_context(m);
  1135. bind_binfhe_keys(m);
  1136. bind_binfhe_ciphertext(m);
  1137. // pke library
  1138. bind_enums_and_constants(m);
  1139. bind_parameters<CryptoContextBFVRNS>(m,"CCParamsBFVRNS");
  1140. bind_parameters<CryptoContextBGVRNS>(m,"CCParamsBGVRNS");
  1141. bind_parameters<CryptoContextCKKSRNS>(m,"CCParamsCKKSRNS");
  1142. bind_encodings(m);
  1143. bind_ciphertext(m);
  1144. bind_keys(m);
  1145. bind_crypto_context(m);
  1146. bind_serialization(m);
  1147. bind_schemes(m);
  1148. bind_sch_swch_params(m);
  1149. }