bindings.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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 "plaintext_docs.h"
  17. #include "ciphertext_docs.h"
  18. using namespace lbcrypto;
  19. namespace py = pybind11;
  20. PYBIND11_MAKE_OPAQUE(std::map<usint, EvalKey<DCRTPoly>>);
  21. template <typename T>
  22. void bind_parameters(py::module &m,const std::string name)
  23. {
  24. py::class_<CCParams<T>>(m, name.c_str())
  25. .def(py::init<>())
  26. // getters
  27. .def("GetPlaintextModulus", &CCParams<T>::GetPlaintextModulus)
  28. .def("GetScheme", &CCParams<T>::GetScheme)
  29. .def("GetDigitSize", &CCParams<T>::GetDigitSize)
  30. .def("GetStandardDeviation", &CCParams<T>::GetStandardDeviation)
  31. .def("GetSecretKeyDist", &CCParams<T>::GetSecretKeyDist)
  32. .def("GetMaxRelinSkDeg", &CCParams<T>::GetMaxRelinSkDeg)
  33. .def("GetPREMode", &CCParams<T>::GetPREMode)
  34. .def("GetMultipartyMode", &CCParams<T>::GetMultipartyMode)
  35. .def("GetExecutionMode", &CCParams<T>::GetExecutionMode)
  36. .def("GetDecryptionNoiseMode", &CCParams<T>::GetDecryptionNoiseMode)
  37. .def("GetNoiseEstimate", &CCParams<T>::GetNoiseEstimate)
  38. .def("GetDesiredPrecision", &CCParams<T>::GetDesiredPrecision)
  39. .def("GetStatisticalSecurity", &CCParams<T>::GetStatisticalSecurity)
  40. .def("GetNumAdversarialQueries", &CCParams<T>::GetNumAdversarialQueries)
  41. //.def("GetThresholdNumOfParties", &CCParams<T>::GetThresholdNumOfParties)
  42. .def("GetKeySwitchTechnique", &CCParams<T>::GetKeySwitchTechnique)
  43. .def("GetScalingTechnique", &CCParams<T>::GetScalingTechnique)
  44. .def("GetBatchSize", &CCParams<T>::GetBatchSize)
  45. .def("GetFirstModSize", &CCParams<T>::GetFirstModSize)
  46. .def("GetNumLargeDigits", &CCParams<T>::GetNumLargeDigits)
  47. .def("GetMultiplicativeDepth", &CCParams<T>::GetMultiplicativeDepth)
  48. .def("GetScalingModSize", &CCParams<T>::GetScalingModSize)
  49. .def("GetSecurityLevel", &CCParams<T>::GetSecurityLevel)
  50. .def("GetRingDim", &CCParams<T>::GetRingDim)
  51. .def("GetEvalAddCount", &CCParams<T>::GetEvalAddCount)
  52. .def("GetKeySwitchCount", &CCParams<T>::GetKeySwitchCount)
  53. .def("GetEncryptionTechnique", &CCParams<T>::GetEncryptionTechnique)
  54. .def("GetMultiplicationTechnique", &CCParams<T>::GetMultiplicationTechnique)
  55. .def("GetMultiHopModSize", &CCParams<T>::GetMultiHopModSize)
  56. // setters
  57. .def("SetPlaintextModulus", &CCParams<T>::SetPlaintextModulus)
  58. .def("SetDigitSize", &CCParams<T>::SetDigitSize)
  59. .def("SetStandardDeviation", &CCParams<T>::SetStandardDeviation)
  60. .def("SetSecretKeyDist", &CCParams<T>::SetSecretKeyDist)
  61. .def("SetMaxRelinSkDeg", &CCParams<T>::SetMaxRelinSkDeg)
  62. .def("SetPREMode", &CCParams<T>::SetPREMode)
  63. .def("SetMultipartyMode", &CCParams<T>::SetMultipartyMode)
  64. .def("SetExecutionMode", &CCParams<T>::SetExecutionMode)
  65. .def("SetDecryptionNoiseMode", &CCParams<T>::SetDecryptionNoiseMode)
  66. .def("SetNoiseEstimate", &CCParams<T>::SetNoiseEstimate)
  67. .def("SetDesiredPrecision", &CCParams<T>::SetDesiredPrecision)
  68. .def("SetStatisticalSecurity", &CCParams<T>::SetStatisticalSecurity)
  69. .def("SetNumAdversarialQueries", &CCParams<T>::SetNumAdversarialQueries)
  70. //.def("SetThresholdNumOfParties", &CCParams<T>::SetThresholdNumOfParties)
  71. .def("SetKeySwitchTechnique", &CCParams<T>::SetKeySwitchTechnique)
  72. .def("SetScalingTechnique", &CCParams<T>::SetScalingTechnique)
  73. .def("SetBatchSize", &CCParams<T>::SetBatchSize)
  74. .def("SetFirstModSize", &CCParams<T>::SetFirstModSize)
  75. .def("SetNumLargeDigits", &CCParams<T>::SetNumLargeDigits)
  76. .def("SetMultiplicativeDepth", &CCParams<T>::SetMultiplicativeDepth)
  77. .def("SetScalingModSize", &CCParams<T>::SetScalingModSize)
  78. .def("SetSecurityLevel", &CCParams<T>::SetSecurityLevel)
  79. .def("SetRingDim", &CCParams<T>::SetRingDim)
  80. .def("SetEvalAddCount", &CCParams<T>::SetEvalAddCount)
  81. .def("SetKeySwitchCount", &CCParams<T>::SetKeySwitchCount)
  82. .def("SetEncryptionTechnique", &CCParams<T>::SetEncryptionTechnique)
  83. .def("SetMultiplicationTechnique", &CCParams<T>::SetMultiplicationTechnique)
  84. .def("SetMultiHopModSize", &CCParams<T>::SetMultiHopModSize)
  85. .def("__str__",[](const CCParams<T> &params) {
  86. std::stringstream stream;
  87. stream << params;
  88. return stream.str();
  89. });
  90. //
  91. }
  92. void bind_crypto_context(py::module &m)
  93. {
  94. py::class_<CryptoContextImpl<DCRTPoly>, std::shared_ptr<CryptoContextImpl<DCRTPoly>>>(m, "CryptoContext")
  95. .def(py::init<>())
  96. .def("GetKeyGenLevel", &CryptoContextImpl<DCRTPoly>::GetKeyGenLevel, cc_GetKeyGenLevel_docs)
  97. .def("SetKeyGenLevel", &CryptoContextImpl<DCRTPoly>::SetKeyGenLevel, cc_SetKeyGenLevel_docs,
  98. py::arg("level"))
  99. //.def("GetScheme",&CryptoContextImpl<DCRTPoly>::GetScheme)
  100. //.def("GetCryptoParameters", &CryptoContextImpl<DCRTPoly>::GetCryptoParameters)
  101. .def("GetRingDimension", &CryptoContextImpl<DCRTPoly>::GetRingDimension, cc_GetRingDimension_docs)
  102. .def("Enable", static_cast<void (CryptoContextImpl<DCRTPoly>::*)(PKESchemeFeature)>(&CryptoContextImpl<DCRTPoly>::Enable), cc_Enable_docs,
  103. py::arg("feature"))
  104. .def("KeyGen", &CryptoContextImpl<DCRTPoly>::KeyGen, cc_KeyGen_docs)
  105. .def("EvalMultKeyGen", &CryptoContextImpl<DCRTPoly>::EvalMultKeyGen,
  106. cc_EvalMultKeyGen_docs,
  107. py::arg("privateKey"))
  108. .def("EvalMultKeysGen", &CryptoContextImpl<DCRTPoly>::EvalMultKeysGen,
  109. cc_EvalMultKeysGen_docs,
  110. py::arg("privateKey"))
  111. .def("EvalRotateKeyGen", &CryptoContextImpl<DCRTPoly>::EvalRotateKeyGen,
  112. cc_EvalRotateKeyGen_docs,
  113. py::arg("privateKey"),
  114. py::arg("indexList"),
  115. py::arg("publicKey") = nullptr)
  116. .def("MakeStringPlaintext", &CryptoContextImpl<DCRTPoly>::MakeStringPlaintext,
  117. cc_MakeStringPlaintext_docs,
  118. py::arg("str"))
  119. .def("MakePackedPlaintext", &CryptoContextImpl<DCRTPoly>::MakePackedPlaintext,
  120. cc_MakePackedPlaintext_docs,
  121. py::arg("value"),
  122. py::arg("depth") = 1,
  123. py::arg("level") = 0)
  124. .def("MakeCoefPackedPlaintext", &CryptoContextImpl<DCRTPoly>::MakeCoefPackedPlaintext,
  125. cc_MakeCoefPackedPlaintext_docs,
  126. py::arg("value"),
  127. py::arg("depth") = 1,
  128. py::arg("level") = 0)
  129. // TODO (Oliveira): allow user to specify different params values
  130. .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,
  131. py::arg("value"),
  132. py::arg("depth") = static_cast<size_t>(1),
  133. py::arg("level") = static_cast<uint32_t>(0),
  134. py::arg("params") = py::none(),
  135. py::arg("slots") = 0)
  136. .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,
  137. py::arg("value"),
  138. py::arg("depth") = static_cast<size_t>(1),
  139. py::arg("level") = static_cast<uint32_t>(0),
  140. py::arg("params") = py::none(),
  141. py::arg("slots") = 0)
  142. .def("EvalRotate", &CryptoContextImpl<DCRTPoly>::EvalRotate,
  143. cc_EvalRotate_docs,
  144. py::arg("ciphertext"),
  145. py::arg("index"))
  146. .def("EvalFastRotationPrecompute", &EvalFastRotationPrecomputeWrapper,
  147. cc_EvalFastRotationPreCompute_docs,
  148. py::arg("ciphertext"))
  149. .def("EvalFastRotation", &EvalFastRotationWrapper,
  150. cc_EvalFastRotation_docs,
  151. py::arg("ciphertext"),
  152. py::arg("index"),
  153. py::arg("m"),
  154. py::arg("digits"))
  155. .def("EvalFastRotationExt", &EvalFastRotationExtWrapper,
  156. cc_EvalFastRotationExt_docs,
  157. py::arg("ciphertext"),
  158. py::arg("index"),
  159. py::arg("digits"),
  160. py::arg("addFirst"))
  161. .def("EvalAtIndexKeyGen", &CryptoContextImpl<DCRTPoly>::EvalAtIndexKeyGen,
  162. cc_EvalAtIndexKeyGen_docs,
  163. py::arg("privateKey"),
  164. py::arg("indexList"),
  165. py::arg("publicKey") = nullptr)
  166. .def("EvalAtIndex", &CryptoContextImpl<DCRTPoly>::EvalAtIndex,
  167. cc_EvalAtIndex_docs,
  168. py::arg("ciphertext"),
  169. py::arg("index"))
  170. .def("Encrypt", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(const PublicKey<DCRTPoly>, Plaintext) const>
  171. (&CryptoContextImpl<DCRTPoly>::Encrypt),
  172. cc_Encrypt_docs,
  173. py::arg("publicKey"),
  174. py::arg("plaintext"))
  175. .def("Decrypt", static_cast<Plaintext (*)(CryptoContext<DCRTPoly> &, const PrivateKey<DCRTPoly>, ConstCiphertext<DCRTPoly>)>
  176. (&DecryptWrapper), cc_Decrypt_docs,
  177. py::arg("privateKey"),
  178. py::arg("ciphertext"))
  179. .def("Decrypt", static_cast<Plaintext (*)(CryptoContext<DCRTPoly> &, ConstCiphertext<DCRTPoly>, const PrivateKey<DCRTPoly>)>
  180. (&DecryptWrapper), cc_Decrypt_docs,
  181. py::arg("ciphertext"),
  182. py::arg("privateKey"))
  183. .def("EvalAdd", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstCiphertext<DCRTPoly>) const>
  184. (&CryptoContextImpl<DCRTPoly>::EvalAdd),
  185. cc_EvalAdd_docs,
  186. py::arg("ciphertext1"),
  187. py::arg("ciphertext2"))
  188. .def("EvalAdd", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, double) const>
  189. (&CryptoContextImpl<DCRTPoly>::EvalAdd),
  190. cc_EvalAddfloat_docs,
  191. py::arg("ciphertext"),
  192. py::arg("scalar"))
  193. .def("EvalAddInPlace", static_cast<void (CryptoContextImpl<DCRTPoly>::*)(Ciphertext<DCRTPoly> &, ConstCiphertext<DCRTPoly>) const>
  194. (&CryptoContextImpl<DCRTPoly>::EvalAddInPlace),
  195. cc_EvalAddInPlace_docs,
  196. py::arg("ciphertext1"),
  197. py::arg("ciphertext2"))
  198. .def("EvalAddInPlace", static_cast<void (CryptoContextImpl<DCRTPoly>::*)(Ciphertext<DCRTPoly> &, ConstPlaintext) const>
  199. (&CryptoContextImpl<DCRTPoly>::EvalAddInPlace),
  200. cc_EvalAddInPlacePlaintext_docs,
  201. py::arg("ciphertext"),
  202. py::arg("plaintext"))
  203. .def("EvalAddInPlace", static_cast<void (CryptoContextImpl<DCRTPoly>::*)(ConstPlaintext, Ciphertext<DCRTPoly> &) const>
  204. (&CryptoContextImpl<DCRTPoly>::EvalAddInPlace),
  205. "",
  206. py::arg("plaintext"),
  207. py::arg("ciphertext"))
  208. .def("EvalAddMutable", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(Ciphertext<DCRTPoly> &, Ciphertext<DCRTPoly> &) const>
  209. (&CryptoContextImpl<DCRTPoly>::EvalAddMutable),
  210. cc_EvalAddMutable_docs,
  211. py::arg("ct1"),
  212. py::arg("ct2"))
  213. .def("EvalAddMutable", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(Ciphertext<DCRTPoly> &, Plaintext) const>
  214. (&CryptoContextImpl<DCRTPoly>::EvalAddMutable),
  215. cc_EvalAddMutablePlaintext_docs,
  216. py::arg("ciphertext"),
  217. py::arg("plaintext"))
  218. .def("EvalAddMutable", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(Plaintext, Ciphertext<DCRTPoly> &) const>
  219. (&CryptoContextImpl<DCRTPoly>::EvalAddMutable),
  220. "",
  221. py::arg("plaintext"),
  222. py::arg("ciphertext"))
  223. .def("EvalAddMutableInPlace", &CryptoContextImpl<DCRTPoly>::EvalAddMutableInPlace,
  224. cc_EvalAddMutableInPlace_docs,
  225. py::arg("ciphertext1"),
  226. py::arg("ciphertext2"))
  227. .def("EvalSub", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstCiphertext<DCRTPoly>) const>
  228. (&CryptoContextImpl<DCRTPoly>::EvalSub),
  229. cc_EvalSub_docs,
  230. py::arg("ct1"),
  231. py::arg("ct2"))
  232. .def("EvalSub", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, double) const>
  233. (&CryptoContextImpl<DCRTPoly>::EvalSub),
  234. cc_EvalSubfloat_docs,
  235. py::arg("ciphertext"),
  236. py::arg("constant"))
  237. .def("EvalSub", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(double, ConstCiphertext<DCRTPoly>) const>
  238. (&CryptoContextImpl<DCRTPoly>::EvalSub),
  239. "",
  240. py::arg("constant"),
  241. py::arg("ciphertext"))
  242. .def("EvalSub", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstPlaintext) const>
  243. (&CryptoContextImpl<DCRTPoly>::EvalSub),
  244. cc_EvalSubPlaintext_docs,
  245. py::arg("ciphertext"),
  246. py::arg("plaintext"))
  247. .def("EvalSub", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstPlaintext, ConstCiphertext<DCRTPoly>) const>
  248. (&CryptoContextImpl<DCRTPoly>::EvalSub),
  249. "",
  250. py::arg("plaintext"),
  251. py::arg("ciphertext"))
  252. .def("EvalSubInPlace", static_cast<void (CryptoContextImpl<DCRTPoly>::*)(Ciphertext<DCRTPoly> &, ConstCiphertext<DCRTPoly>) const>
  253. (&CryptoContextImpl<DCRTPoly>::EvalSubInPlace),
  254. cc_EvalSubInPlace_docs,
  255. py::arg("ct1"),
  256. py::arg("ct2"))
  257. .def("EvalSubInPlace", static_cast<void (CryptoContextImpl<DCRTPoly>::*)(Ciphertext<DCRTPoly> &, double) const>
  258. (&CryptoContextImpl<DCRTPoly>::EvalSubInPlace),
  259. cc_EvalSubInPlacefloat_docs,
  260. py::arg("ciphertext"),
  261. py::arg("constant"))
  262. .def("EvalSubInPlace", static_cast<void (CryptoContextImpl<DCRTPoly>::*)(double, Ciphertext<DCRTPoly> &) const>
  263. (&CryptoContextImpl<DCRTPoly>::EvalSubInPlace),
  264. "",
  265. py::arg("constant"),
  266. py::arg("ciphertext"))
  267. .def("EvalSubMutable", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(Ciphertext<DCRTPoly> &, Ciphertext<DCRTPoly> &) const>
  268. (&CryptoContextImpl<DCRTPoly>::EvalSubMutable),
  269. cc_EvalSubMutable_docs,
  270. py::arg("ct1"),
  271. py::arg("ct2"))
  272. .def("EvalSubMutable", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(Ciphertext<DCRTPoly> &, Plaintext) const>
  273. (&CryptoContextImpl<DCRTPoly>::EvalSubMutable),
  274. cc_EvalSubMutablePlaintext_docs,
  275. py::arg("ciphertext"),
  276. py::arg("plaintext"))
  277. .def("EvalSubMutable", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(Plaintext, Ciphertext<DCRTPoly> &) const>
  278. (&CryptoContextImpl<DCRTPoly>::EvalSubMutable),
  279. "",
  280. py::arg("plaintext"),
  281. py::arg("ciphertext"))
  282. .def("EvalSubMutableInPlace", &CryptoContextImpl<DCRTPoly>::EvalSubMutableInPlace,
  283. cc_EvalSubMutableInPlace_docs,
  284. py::arg("ciphertext1"),
  285. py::arg("ciphertext2"))
  286. .def("EvalMult", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstCiphertext<DCRTPoly>) const>
  287. (&CryptoContextImpl<DCRTPoly>::EvalMult),
  288. cc_EvalMult_docs,
  289. py::arg("ct1"),
  290. py::arg("ct2"))
  291. .def("EvalMult", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, double) const>
  292. (&CryptoContextImpl<DCRTPoly>::EvalMult),
  293. cc_EvalMultfloat_docs,
  294. py::arg("ciphertext"),
  295. py::arg("constant"))
  296. .def("EvalMult", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstPlaintext) const>
  297. (&CryptoContextImpl<DCRTPoly>::EvalMult),
  298. cc_EvalMultPlaintext_docs,
  299. py::arg("ciphertext"),
  300. py::arg("plaintext"))
  301. .def("EvalMult", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstPlaintext, ConstCiphertext<DCRTPoly>) const>
  302. (&CryptoContextImpl<DCRTPoly>::EvalMult),
  303. "",
  304. py::arg("plaintext"),
  305. py::arg("ciphertext"))
  306. .def("EvalMult", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(double, ConstCiphertext<DCRTPoly>) const>
  307. (&CryptoContextImpl<DCRTPoly>::EvalMult),
  308. "",
  309. py::arg("constant"),
  310. py::arg("ciphertext"))
  311. .def("EvalMultMutable", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(Ciphertext<DCRTPoly> &, Ciphertext<DCRTPoly> &) const>
  312. (&CryptoContextImpl<DCRTPoly>::EvalMultMutable),
  313. cc_EvalMultMutable_docs,
  314. py::arg("ct1"),
  315. py::arg("ct2"))
  316. .def("EvalMultMutable", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(Ciphertext<DCRTPoly> &, Plaintext) const>
  317. (&CryptoContextImpl<DCRTPoly>::EvalMultMutable),
  318. cc_EvalMultMutablePlaintext_docs,
  319. py::arg("ciphertext"),
  320. py::arg("plaintext"))
  321. .def("EvalMultMutable", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(Plaintext, Ciphertext<DCRTPoly> &) const>
  322. (&CryptoContextImpl<DCRTPoly>::EvalMultMutable),
  323. "",
  324. py::arg("plaintext"),
  325. py::arg("ciphertext"))
  326. .def("EvalMultMutableInPlace", &CryptoContextImpl<DCRTPoly>::EvalMultMutableInPlace,
  327. cc_EvalMultMutableInPlace_docs,
  328. py::arg("ct1"),
  329. py::arg("ct2"))
  330. .def("EvalSquare", &CryptoContextImpl<DCRTPoly>::EvalSquare,
  331. cc_EvalSquare_docs,
  332. py::arg("ct"))
  333. .def("EvalSquareMutable", &CryptoContextImpl<DCRTPoly>::EvalSquareMutable,
  334. cc_EvalSquareMutable_docs,
  335. py::arg("ct"))
  336. .def("EvalSquareInPlace", &CryptoContextImpl<DCRTPoly>::EvalSquareInPlace,
  337. cc_EvalSquareInPlace_docs,
  338. py::arg("ct"))
  339. .def("EvalMultNoRelin", &CryptoContextImpl<DCRTPoly>::EvalMultNoRelin,
  340. cc_EvalMultNoRelin_docs,
  341. py::arg("ct1"),
  342. py::arg("ct2"))
  343. .def("Relinearize", &CryptoContextImpl<DCRTPoly>::Relinearize,
  344. cc_Relinearize_docs,
  345. py::arg("ciphertext"))
  346. .def("RelinearizeInPlace", &CryptoContextImpl<DCRTPoly>::RelinearizeInPlace,
  347. cc_RelinearizeInPlace_docs,
  348. py::arg("ciphertext"))
  349. .def("EvalMultAndRelinearize", &CryptoContextImpl<DCRTPoly>::EvalMultAndRelinearize,
  350. cc_EvalMultAndRelinearize_docs,
  351. py::arg("ct1"),
  352. py::arg("ct2"))
  353. .def("EvalNegate", &CryptoContextImpl<DCRTPoly>::EvalNegate,
  354. cc_EvalNegate_docs,
  355. py::arg("ct"))
  356. .def("EvalNegateInPlace", &CryptoContextImpl<DCRTPoly>::EvalNegateInPlace,
  357. cc_EvalNegateInPlace_docs,
  358. py::arg("ct"))
  359. .def("EvalLogistic", &CryptoContextImpl<DCRTPoly>::EvalLogistic,
  360. cc_EvalLogistic_docs,
  361. py::arg("ciphertext"),
  362. py::arg("a"),
  363. py::arg("b"),
  364. py::arg("degree"))
  365. .def("EvalChebyshevSeries", &CryptoContextImpl<DCRTPoly>::EvalChebyshevSeries,
  366. cc_EvalChebyshevSeries_docs,
  367. py::arg("ciphertext"),
  368. py::arg("coefficients"),
  369. py::arg("a"),
  370. py::arg("b"))
  371. .def("EvalChebyshevSeriesLinear", &CryptoContextImpl<DCRTPoly>::EvalChebyshevSeriesLinear,
  372. cc_EvalChebyshevSeriesLinear_docs,
  373. py::arg("ciphertext"),
  374. py::arg("coefficients"),
  375. py::arg("a"),
  376. py::arg("b"))
  377. .def("EvalChebyshevSeriesPS", &CryptoContextImpl<DCRTPoly>::EvalChebyshevSeriesPS,
  378. cc_EvalChebyshevSeriesPS_docs,
  379. py::arg("ciphertext"),
  380. py::arg("coefficients"),
  381. py::arg("a"),
  382. py::arg("b"))
  383. .def("EvalChebyshevFunction", &CryptoContextImpl<DCRTPoly>::EvalChebyshevFunction,
  384. cc_EvalChebyshevFunction_docs,
  385. py::arg("func"),
  386. py::arg("ciphertext"),
  387. py::arg("a"),
  388. py::arg("b"),
  389. py::arg("degree"))
  390. .def("EvalSin", &CryptoContextImpl<DCRTPoly>::EvalSin,
  391. cc_EvalSin_docs,
  392. py::arg("ciphertext"),
  393. py::arg("a"),
  394. py::arg("b"),
  395. py::arg("degree"))
  396. .def("EvalCos", &CryptoContextImpl<DCRTPoly>::EvalCos,
  397. cc_EvalCos_docs,
  398. py::arg("ciphertext"),
  399. py::arg("a"),
  400. py::arg("b"),
  401. py::arg("degree"))
  402. .def("EvalDivide", &CryptoContextImpl<DCRTPoly>::EvalDivide,
  403. cc_EvalDivide_docs,
  404. py::arg("ciphertext"),
  405. py::arg("a"),
  406. py::arg("b"),
  407. py::arg("degree"))
  408. .def("EvalSumKeyGen", &CryptoContextImpl<DCRTPoly>::EvalSumKeyGen,
  409. cc_EvalSumKeyGen_docs,
  410. py::arg("privateKey"),
  411. py::arg("publicKey") = py::none())
  412. //TODO (Oliveira, R.): Solve pointer handling bug when dealing with EvalKeyMap object for the next functions
  413. .def("EvalSumRowsKeyGen", &CryptoContextImpl<DCRTPoly>::EvalSumRowsKeyGen,
  414. cc_EvalSumRowsKeyGen_docs,
  415. py::arg("privateKey"),
  416. py::arg("publicKey") = py::none(),
  417. py::arg("rowSize") = 0,
  418. py::arg("subringDim") = 0)
  419. .def("EvalSumColsKeyGen", &CryptoContextImpl<DCRTPoly>::EvalSumColsKeyGen,
  420. cc_EvalSumColsKeyGen_docs,
  421. py::arg("privateKey"),
  422. py::arg("publicKey") = py::none())
  423. .def("EvalSumRows", &CryptoContextImpl<DCRTPoly>::EvalSumRows,
  424. cc_EvalSumRows_docs,
  425. py::arg("ciphertext"),
  426. py::arg("rowSize"),
  427. py::arg("evalSumKeyMap"),
  428. py::arg("subringDim") = 0)
  429. .def("EvalSumCols", &CryptoContextImpl<DCRTPoly>::EvalSumCols,
  430. cc_EvalSumCols_docs,
  431. py::arg("ciphertext"),
  432. py::arg("rowSize"),
  433. py::arg("evalSumKeyMap"))
  434. .def("EvalInnerProduct", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstCiphertext<DCRTPoly>, usint) const>(&CryptoContextImpl<DCRTPoly>::EvalInnerProduct),
  435. cc_EvalInnerProduct_docs,
  436. py::arg("ciphertext1"),
  437. py::arg("ciphertext2"),
  438. py::arg("batchSize"))
  439. .def("EvalInnerProduct", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstPlaintext, usint) const>(&CryptoContextImpl<DCRTPoly>::EvalInnerProduct),
  440. cc_EvalInnerProductPlaintext_docs,
  441. py::arg("ciphertext"),
  442. py::arg("plaintext"),
  443. py::arg("batchSize"))
  444. .def("EvalMerge", &CryptoContextImpl<DCRTPoly>::EvalMerge,
  445. cc_EvalMerge_docs,
  446. py::arg("ciphertextVec"))
  447. .def("EvalPoly", &CryptoContextImpl<DCRTPoly>::EvalPoly,
  448. cc_EvalPoly_docs,
  449. py::arg("ciphertext"),
  450. py::arg("coefficients"))
  451. .def("EvalPolyLinear", &CryptoContextImpl<DCRTPoly>::EvalPolyLinear,
  452. cc_EvalPolyLinear_docs,
  453. py::arg("ciphertext"),
  454. py::arg("coefficients"))
  455. .def("EvalPolyPS", &CryptoContextImpl<DCRTPoly>::EvalPolyPS,
  456. cc_EvalPolyPS_docs,
  457. py::arg("ciphertext"),
  458. py::arg("coefficients"))
  459. .def("Rescale", &CryptoContextImpl<DCRTPoly>::Rescale,
  460. cc_Rescale_docs,
  461. py::arg("ciphertext"))
  462. .def("EvalBootstrapSetup", &CryptoContextImpl<DCRTPoly>::EvalBootstrapSetup,
  463. cc_EvalBootstrapSetup_docs,
  464. py::arg("levelBudget") = std::vector<uint32_t>({5, 4}),
  465. py::arg("dim1") = std::vector<uint32_t>({0, 0}),
  466. py::arg("slots") = 0,
  467. py::arg("correctionFactor") = 0)
  468. .def("EvalBootstrapKeyGen", &CryptoContextImpl<DCRTPoly>::EvalBootstrapKeyGen,
  469. cc_EvalBootstrapKeyGen_docs,
  470. py::arg("privateKey"),
  471. py::arg("slots"))
  472. .def("EvalBootstrap", &CryptoContextImpl<DCRTPoly>::EvalBootstrap,
  473. cc_EvalBootstrap_docs,
  474. py::arg("ciphertext"),
  475. py::arg("numIterations") = 1,
  476. py::arg("precision") = 0)
  477. //TODO (Oliveira, R.): Solve pointer handling bug when returning EvalKeyMap objects for the next functions
  478. .def("EvalAutomorphismKeyGen", &EvalAutomorphismKeyGenWrapper,
  479. cc_EvalAutomorphismKeyGen_docs,
  480. py::arg("privateKey"),
  481. py::arg("indexList"),
  482. py::return_value_policy::reference_internal)
  483. .def("EvalAutomorphismKeyGen", &EvalAutomorphismKeyGenWrapper_PublicKey,
  484. cc_EvalAutomorphismKeyGenPublic_docs,
  485. py::arg("publicKey"),
  486. py::arg("privateKey"),
  487. py::arg("indexList"),
  488. py::return_value_policy::reference_internal)
  489. .def("FindAutomorphismIndex", &CryptoContextImpl<DCRTPoly>::FindAutomorphismIndex,
  490. cc_FindAutomorphismIndex_docs,
  491. py::arg("idx"))
  492. .def("FindAutomorphismIndices", &CryptoContextImpl<DCRTPoly>::FindAutomorphismIndices,
  493. cc_FindAutomorphismIndices_docs,
  494. py::arg("idxList"))
  495. .def_static(
  496. "ClearEvalMultKeys", []()
  497. { CryptoContextImpl<DCRTPoly>::ClearEvalMultKeys(); },
  498. cc_ClearEvalMultKeys_docs)
  499. .def_static(
  500. "ClearEvalAutomorphismKeys", []()
  501. { CryptoContextImpl<DCRTPoly>::ClearEvalAutomorphismKeys(); },
  502. cc_ClearEvalAutomorphismKeys_docs)
  503. .def_static(
  504. "SerializeEvalMultKey", [](const std::string &filename, const SerType::SERBINARY &sertype, std::string id = "")
  505. {
  506. std::ofstream outfile(filename,std::ios::out | std::ios::binary);
  507. bool res;
  508. res = CryptoContextImpl<DCRTPoly>::SerializeEvalMultKey<SerType::SERBINARY>(outfile, sertype, id);
  509. outfile.close();
  510. return res; },
  511. cc_SerializeEvalMultKey_docs,
  512. py::arg("filename"), py::arg("sertype"), py::arg("id") = "")
  513. .def_static(
  514. "SerializeEvalAutomorphismKey", [](const std::string &filename, const SerType::SERBINARY &sertype, std::string id = "")
  515. {
  516. std::ofstream outfile(filename,std::ios::out | std::ios::binary);
  517. bool res;
  518. res = CryptoContextImpl<DCRTPoly>::SerializeEvalAutomorphismKey<SerType::SERBINARY>(outfile, sertype, id);
  519. outfile.close();
  520. return res; },
  521. cc_SerializeEvalAutomorphismKey_docs,
  522. py::arg("filename"), py::arg("sertype"), py::arg("id") = "")
  523. .def_static("DeserializeEvalMultKey", [](std::shared_ptr<CryptoContextImpl<DCRTPoly>> &self, const std::string &filename, const SerType::SERBINARY &sertype)
  524. {
  525. std::ifstream emkeys(filename, std::ios::in | std::ios::binary);
  526. if (!emkeys.is_open()) {
  527. std::cerr << "I cannot read serialization from " << filename << std::endl;
  528. }
  529. bool res;
  530. res = self->DeserializeEvalMultKey<SerType::SERBINARY>(emkeys, sertype);
  531. return res; },
  532. cc_DeserializeEvalMultKey_docs,
  533. py::arg("self"),
  534. py::arg("filename"), py::arg("sertype"))
  535. .def_static("DeserializeEvalAutomorphismKey", [](std::shared_ptr<CryptoContextImpl<DCRTPoly>> &self, const std::string &filename, const SerType::SERBINARY &sertype)
  536. {
  537. std::ifstream erkeys(filename, std::ios::in | std::ios::binary);
  538. if (!erkeys.is_open()) {
  539. std::cerr << "I cannot read serialization from " << filename << std::endl;
  540. }
  541. bool res;
  542. res = self->DeserializeEvalAutomorphismKey<SerType::SERBINARY>(erkeys, sertype);
  543. return res; },
  544. cc_DeserializeEvalAutomorphismKey_docs,
  545. py::arg("self"),
  546. py::arg("filename"), py::arg("sertype"));
  547. // Generator Functions
  548. m.def("GenCryptoContext", &GenCryptoContext<CryptoContextBFVRNS>,
  549. py::arg("params"));
  550. m.def("GenCryptoContext", &GenCryptoContext<CryptoContextBGVRNS>,
  551. py::arg("params"));
  552. m.def("GenCryptoContext", &GenCryptoContext<CryptoContextCKKSRNS>,
  553. py::arg("params"));
  554. m.def("ReleaseAllContexts", &CryptoContextFactory<DCRTPoly>::ReleaseAllContexts);
  555. }
  556. int get_native_int(){
  557. #if NATIVEINT == 128 && !defined(__EMSCRIPTEN__)
  558. return 128;
  559. #else
  560. return 64;
  561. #endif
  562. }
  563. void bind_enums_and_constants(py::module &m)
  564. {
  565. /* ---- PKE enums ---- */
  566. // Scheme Types
  567. py::enum_<SCHEME>(m, "SCHEME")
  568. .value("INVALID_SCHEME", SCHEME::INVALID_SCHEME)
  569. .value("CKKSRNS_SCHEME", SCHEME::CKKSRNS_SCHEME)
  570. .value("BFVRNS_SCHEME", SCHEME::BFVRNS_SCHEME)
  571. .value("BGVRNS_SCHEME", SCHEME::BGVRNS_SCHEME);
  572. m.attr("INVALID_SCHEME") = py::cast(SCHEME::INVALID_SCHEME);
  573. m.attr("CKKSRNS_SCHEME") = py::cast(SCHEME::CKKSRNS_SCHEME);
  574. m.attr("BFVRNS_SCHEME") = py::cast(SCHEME::BFVRNS_SCHEME);
  575. m.attr("BGVRNS_SCHEME") = py::cast(SCHEME::BGVRNS_SCHEME);
  576. // PKE Features
  577. py::enum_<PKESchemeFeature>(m, "PKESchemeFeature")
  578. .value("PKE", PKESchemeFeature::PKE)
  579. .value("KEYSWITCH", PKESchemeFeature::KEYSWITCH)
  580. .value("PRE", PKESchemeFeature::PRE)
  581. .value("LEVELEDSHE", PKESchemeFeature::LEVELEDSHE)
  582. .value("ADVANCEDSHE", PKESchemeFeature::ADVANCEDSHE)
  583. .value("MULTIPARTY", PKESchemeFeature::MULTIPARTY)
  584. .value("FHE", PKESchemeFeature::FHE);
  585. m.attr("PKE") = py::cast(PKESchemeFeature::PKE);
  586. m.attr("KEYSWITCH") = py::cast(PKESchemeFeature::KEYSWITCH);
  587. m.attr("PRE") = py::cast(PKESchemeFeature::PRE);
  588. m.attr("LEVELEDSHE") = py::cast(PKESchemeFeature::LEVELEDSHE);
  589. m.attr("ADVANCEDSHE") = py::cast(PKESchemeFeature::ADVANCEDSHE);
  590. m.attr("MULTIPARTY") = py::cast(PKESchemeFeature::MULTIPARTY);
  591. m.attr("FHE") = py::cast(PKESchemeFeature::FHE);
  592. // Serialization Types
  593. py::class_<SerType::SERJSON>(m, "SERJSON");
  594. py::class_<SerType::SERBINARY>(m, "SERBINARY");
  595. m.attr("JSON") = py::cast(SerType::JSON);
  596. m.attr("BINARY") = py::cast(SerType::BINARY);
  597. // Scaling Techniques
  598. py::enum_<ScalingTechnique>(m, "ScalingTechnique")
  599. .value("FIXEDMANUAL", ScalingTechnique::FIXEDMANUAL)
  600. .value("FIXEDAUTO", ScalingTechnique::FIXEDAUTO)
  601. .value("FLEXIBLEAUTO", ScalingTechnique::FLEXIBLEAUTO)
  602. .value("FLEXIBLEAUTOEXT", ScalingTechnique::FLEXIBLEAUTOEXT)
  603. .value("NORESCALE", ScalingTechnique::NORESCALE)
  604. .value("INVALID_RS_TECHNIQUE", ScalingTechnique::INVALID_RS_TECHNIQUE);
  605. m.attr("FIXEDMANUAL") = py::cast(ScalingTechnique::FIXEDMANUAL);
  606. m.attr("FIXEDAUTO") = py::cast(ScalingTechnique::FIXEDAUTO);
  607. m.attr("FLEXIBLEAUTO") = py::cast(ScalingTechnique::FLEXIBLEAUTO);
  608. m.attr("FLEXIBLEAUTOEXT") = py::cast(ScalingTechnique::FLEXIBLEAUTOEXT);
  609. m.attr("NORESCALE") = py::cast(ScalingTechnique::NORESCALE);
  610. m.attr("INVALID_RS_TECHNIQUE") = py::cast(ScalingTechnique::INVALID_RS_TECHNIQUE);
  611. // Key Switching Techniques
  612. py::enum_<KeySwitchTechnique>(m, "KeySwitchTechnique")
  613. .value("INVALID_KS_TECH", KeySwitchTechnique::INVALID_KS_TECH)
  614. .value("BV", KeySwitchTechnique::BV)
  615. .value("HYBRID", KeySwitchTechnique::HYBRID);
  616. m.attr("INVALID_KS_TECH") = py::cast(KeySwitchTechnique::INVALID_KS_TECH);
  617. m.attr("BV") = py::cast(KeySwitchTechnique::BV);
  618. m.attr("HYBRID") = py::cast(KeySwitchTechnique::HYBRID);
  619. // Secret Key Dist
  620. py::enum_<SecretKeyDist>(m, "SecretKeyDist")
  621. .value("GAUSSIAN", SecretKeyDist::GAUSSIAN)
  622. .value("UNIFORM_TERNARY", SecretKeyDist::UNIFORM_TERNARY)
  623. .value("SPARSE_TERNARY", SecretKeyDist::SPARSE_TERNARY);
  624. m.attr("GAUSSIAN") = py::cast(SecretKeyDist::GAUSSIAN);
  625. m.attr("UNIFORM_TERNARY") = py::cast(SecretKeyDist::UNIFORM_TERNARY);
  626. m.attr("SPARSE_TERNARY") = py::cast(SecretKeyDist::SPARSE_TERNARY);
  627. // ProxyReEncryptionMode
  628. py::enum_<ProxyReEncryptionMode>(m, "ProxyReEncryptionMode")
  629. .value("NOT_SET", ProxyReEncryptionMode::NOT_SET)
  630. .value("INDCPA", ProxyReEncryptionMode::INDCPA)
  631. .value("FIXED_NOISE_HRA", ProxyReEncryptionMode::FIXED_NOISE_HRA)
  632. .value("NOISE_FLOODING_HRA", ProxyReEncryptionMode::NOISE_FLOODING_HRA)
  633. .value("DIVIDE_AND_ROUND_HRA", ProxyReEncryptionMode::DIVIDE_AND_ROUND_HRA);
  634. m.attr("NOT_SET") = py::cast(ProxyReEncryptionMode::NOT_SET);
  635. m.attr("INDCPA") = py::cast(ProxyReEncryptionMode::INDCPA);
  636. m.attr("FIXED_NOISE_HRA") = py::cast(ProxyReEncryptionMode::FIXED_NOISE_HRA);
  637. m.attr("NOISE_FLOODING_HRA") = py::cast(ProxyReEncryptionMode::NOISE_FLOODING_HRA);
  638. m.attr("DIVIDE_AND_ROUND_HRA") = py::cast(ProxyReEncryptionMode::DIVIDE_AND_ROUND_HRA);
  639. // MultipartyMode
  640. py::enum_<MultipartyMode>(m, "MultipartyMode")
  641. .value("INVALID_MULTIPARTY_MODE", MultipartyMode::INVALID_MULTIPARTY_MODE)
  642. .value("FIXED_NOISE_MULTIPARTY", MultipartyMode::FIXED_NOISE_MULTIPARTY)
  643. .value("NOISE_FLOODING_MULTIPARTY", MultipartyMode::NOISE_FLOODING_MULTIPARTY);
  644. m.attr("INVALID_MULTIPARTY_MODE") = py::cast(MultipartyMode::INVALID_MULTIPARTY_MODE);
  645. m.attr("FIXED_NOISE_MULTIPARTY") = py::cast(MultipartyMode::FIXED_NOISE_MULTIPARTY);
  646. m.attr("NOISE_FLOODING_MULTIPARTY") = py::cast(MultipartyMode::NOISE_FLOODING_MULTIPARTY);
  647. // ExecutionMode
  648. py::enum_<ExecutionMode>(m, "ExecutionMode")
  649. .value("EXEC_EVALUATION", ExecutionMode::EXEC_EVALUATION)
  650. .value("EXEC_NOISE_ESTIMATION", ExecutionMode::EXEC_NOISE_ESTIMATION);
  651. m.attr("EXEC_EVALUATION") = py::cast(ExecutionMode::EXEC_EVALUATION);
  652. m.attr("EXEC_NOISE_ESTIMATION") = py::cast(ExecutionMode::EXEC_NOISE_ESTIMATION);
  653. // DecryptionNoiseMode
  654. py::enum_<DecryptionNoiseMode>(m, "DecryptionNoiseMode")
  655. .value("FIXED_NOISE_DECRYPT", DecryptionNoiseMode::FIXED_NOISE_DECRYPT)
  656. .value("NOISE_FLOODING_DECRYPT", DecryptionNoiseMode::NOISE_FLOODING_DECRYPT);
  657. m.attr("FIXED_NOISE_DECRYPT") = py::cast(DecryptionNoiseMode::FIXED_NOISE_DECRYPT);
  658. m.attr("NOISE_FLOODING_DECRYPT") = py::cast(DecryptionNoiseMode::NOISE_FLOODING_DECRYPT);
  659. // EncryptionTechnique
  660. py::enum_<EncryptionTechnique>(m, "EncryptionTechnique")
  661. .value("STANDARD", EncryptionTechnique::STANDARD)
  662. .value("EXTENDED", EncryptionTechnique::EXTENDED);
  663. m.attr("STANDARD") = py::cast(EncryptionTechnique::STANDARD);
  664. m.attr("EXTENDED") = py::cast(EncryptionTechnique::EXTENDED);
  665. // MultiplicationTechnique
  666. py::enum_<MultiplicationTechnique>(m, "MultiplicationTechnique")
  667. .value("BEHZ", MultiplicationTechnique::BEHZ)
  668. .value("HPS", MultiplicationTechnique::HPS)
  669. .value("HPSPOVERQ", MultiplicationTechnique::HPSPOVERQ)
  670. .value("HPSPOVERQLEVELED", MultiplicationTechnique::HPSPOVERQLEVELED);
  671. m.attr("BEHZ") = py::cast(MultiplicationTechnique::BEHZ);
  672. m.attr("HPS") = py::cast(MultiplicationTechnique::HPS);
  673. m.attr("HPSPOVERQ") = py::cast(MultiplicationTechnique::HPSPOVERQ);
  674. m.attr("HPSPOVERQLEVELED") = py::cast(MultiplicationTechnique::HPSPOVERQLEVELED);
  675. /* ---- CORE enums ---- */
  676. // Security Level
  677. py::enum_<SecurityLevel>(m,"SecurityLevel")
  678. .value("HEStd_128_classic", SecurityLevel::HEStd_128_classic)
  679. .value("HEStd_192_classic", SecurityLevel::HEStd_192_classic)
  680. .value("HEStd_256_classic", SecurityLevel::HEStd_256_classic)
  681. .value("HEStd_NotSet", SecurityLevel::HEStd_NotSet);
  682. m.attr("HEStd_128_classic") = py::cast(SecurityLevel::HEStd_128_classic);
  683. m.attr("HEStd_192_classic") = py::cast(SecurityLevel::HEStd_192_classic);
  684. m.attr("HEStd_256_classic") = py::cast(SecurityLevel::HEStd_256_classic);
  685. m.attr("HEStd_NotSet") = py::cast(SecurityLevel::HEStd_NotSet);
  686. //Parameters Type
  687. /*TODO (Oliveira): If we expose Poly's and ParmType, this block will go somewhere else */
  688. using ParmType = typename DCRTPoly::Params;
  689. py::class_<ParmType, std::shared_ptr<ParmType>>(m, "ParmType");
  690. //NATIVEINT function
  691. m.def("get_native_int", &get_native_int);
  692. // EvalKeyMap
  693. py::bind_map<std::map<usint, EvalKey<DCRTPoly>>>(m, "EvalKeyMap");
  694. }
  695. void bind_keys(py::module &m)
  696. {
  697. py::class_<PublicKeyImpl<DCRTPoly>, std::shared_ptr<PublicKeyImpl<DCRTPoly>>>(m, "PublicKey")
  698. .def(py::init<>());
  699. py::class_<PrivateKeyImpl<DCRTPoly>, std::shared_ptr<PrivateKeyImpl<DCRTPoly>>>(m, "PrivateKey");
  700. py::class_<KeyPair<DCRTPoly>>(m, "KeyPair")
  701. .def_readwrite("publicKey", &KeyPair<DCRTPoly>::publicKey)
  702. .def_readwrite("secretKey", &KeyPair<DCRTPoly>::secretKey);
  703. py::class_<EvalKeyImpl<DCRTPoly>, std::shared_ptr<EvalKeyImpl<DCRTPoly>>>(m, "EvalKey")
  704. .def(py::init<>());
  705. }
  706. void bind_encodings(py::module &m)
  707. {
  708. py::class_<PlaintextImpl, std::shared_ptr<PlaintextImpl>>(m, "Plaintext")
  709. .def("GetScalingFactor", &PlaintextImpl::GetScalingFactor,
  710. ptx_GetScalingFactor_docs)
  711. .def("SetScalingFactor", &PlaintextImpl::SetScalingFactor,
  712. ptx_SetScalingFactor_docs,
  713. py::arg("sf"))
  714. .def("GetLength", &PlaintextImpl::GetLength,
  715. ptx_GetLength_docs)
  716. .def("GetSchemeID", &PlaintextImpl::GetSchemeID,
  717. ptx_GetSchemeID_docs)
  718. .def("SetLength", &PlaintextImpl::SetLength,
  719. ptx_SetLength_docs,
  720. py::arg("newSize"))
  721. .def("IsEncoded", &PlaintextImpl::IsEncoded,
  722. ptx_IsEncoded_docs)
  723. .def("GetLogPrecision", &PlaintextImpl::GetLogPrecision,
  724. ptx_GetLogPrecision_docs)
  725. .def("Encode", &PlaintextImpl::Encode,
  726. ptx_Encode_docs)
  727. .def("Decode", &PlaintextImpl::Decode,
  728. ptx_Decode_docs)
  729. .def("GetCKKSPackedValue", &PlaintextImpl::GetCKKSPackedValue,
  730. ptx_GetCKKSPackedValue_docs)
  731. .def("__repr__", [](const PlaintextImpl &p)
  732. {
  733. std::stringstream ss;
  734. ss << "<Plaintext Object: ";
  735. p.PrintValue(ss);
  736. ss << ">";
  737. return ss.str(); })
  738. .def("__str__", [](const PlaintextImpl &p)
  739. {
  740. std::stringstream ss;
  741. p.PrintValue(ss);
  742. return ss.str(); });
  743. }
  744. void bind_ciphertext(py::module &m)
  745. {
  746. py::class_<CiphertextImpl<DCRTPoly>, std::shared_ptr<CiphertextImpl<DCRTPoly>>>(m, "Ciphertext")
  747. .def(py::init<>())
  748. .def("__add__", [](const Ciphertext<DCRTPoly> &a, const Ciphertext<DCRTPoly> &b)
  749. {return a + b; },py::is_operator(),pybind11::keep_alive<0, 1>())
  750. // .def(py::self + py::self);
  751. // .def("GetDepth", &CiphertextImpl<DCRTPoly>::GetDepth)
  752. // .def("SetDepth", &CiphertextImpl<DCRTPoly>::SetDepth)
  753. .def("GetLevel", &CiphertextImpl<DCRTPoly>::GetLevel,
  754. ctx_GetLevel_docs)
  755. .def("SetLevel", &CiphertextImpl<DCRTPoly>::SetLevel,
  756. ctx_SetLevel_docs,
  757. py::arg("level"));
  758. // .def("GetHopLevel", &CiphertextImpl<DCRTPoly>::GetHopLevel)
  759. // .def("SetHopLevel", &CiphertextImpl<DCRTPoly>::SetHopLevel)
  760. // .def("GetScalingFactor", &CiphertextImpl<DCRTPoly>::GetScalingFactor)
  761. // .def("SetScalingFactor", &CiphertextImpl<DCRTPoly>::SetScalingFactor)
  762. // .def("GetSlots", &CiphertextImpl<DCRTPoly>::GetSlots)
  763. // .def("SetSlots", &CiphertextImpl<DCRTPoly>::SetSlots);
  764. }
  765. void bind_schemes(py::module &m){
  766. /*Bind schemes specific functionalities like bootstrapping functions and multiparty*/
  767. py::class_<FHECKKSRNS>(m, "FHECKKSRNS")
  768. .def(py::init<>())
  769. //.def_static("GetBootstrapDepth", &FHECKKSRNS::GetBootstrapDepth)
  770. .def_static("GetBootstrapDepth", static_cast<uint32_t (*)(uint32_t, const std::vector<uint32_t>&, SecretKeyDist)>(&FHECKKSRNS::GetBootstrapDepth));
  771. }
  772. PYBIND11_MODULE(openfhe, m)
  773. {
  774. m.doc() = "Open-Source Fully Homomorphic Encryption Library";
  775. // pke library
  776. bind_enums_and_constants(m);
  777. bind_parameters<CryptoContextBFVRNS>(m,"CCParamsBFVRNS");
  778. bind_parameters<CryptoContextBGVRNS>(m,"CCParamsBGVRNS");
  779. bind_parameters<CryptoContextCKKSRNS>(m,"CCParamsCKKSRNS");
  780. bind_encodings(m);
  781. bind_ciphertext(m);
  782. bind_keys(m);
  783. bind_crypto_context(m);
  784. bind_serialization(m);
  785. bind_schemes(m);
  786. // binfhe library
  787. bind_binfhe_enums(m);
  788. bind_binfhe_context(m);
  789. bind_binfhe_keys(m);
  790. bind_binfhe_ciphertext(m);
  791. }