bindings.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include <pybind11/pybind11.h>
  2. #include <pybind11/stl.h>
  3. #include <openfhe/pke/openfhe.h>
  4. #include "bindings.h"
  5. using namespace lbcrypto;
  6. namespace py = pybind11;
  7. void bind_parameters(py::module &m){
  8. py::class_<Params>(m, "Params");
  9. py::class_<CCParams<CryptoContextBFVRNS>, Params>(m, "CCParamsBFVRNS")
  10. .def(py::init<>())
  11. // setters
  12. .def("SetPlaintextModulus", &CCParams<CryptoContextBFVRNS>::SetPlaintextModulus)
  13. .def("SetMultiplicativeDepth",&CCParams<CryptoContextBFVRNS>::SetMultiplicativeDepth)
  14. // getters
  15. .def("GetPlaintextModulus", &CCParams<CryptoContextBFVRNS>::GetPlaintextModulus)
  16. .def("GetMultiplicativeDepth", &CCParams<CryptoContextBFVRNS>::GetMultiplicativeDepth);
  17. }
  18. void bind_crypto_context(py::module &m){
  19. py::class_<CryptoContextImpl<DCRTPoly>,std::shared_ptr<CryptoContextImpl<DCRTPoly>>>(m,"CryptoContextDCRTPoly")
  20. .def("GetKeyGenLevel",&CryptoContextImpl<DCRTPoly>::GetKeyGenLevel)
  21. .def("SetKeyGenLevel",&CryptoContextImpl<DCRTPoly>::SetKeyGenLevel)
  22. .def("Enable",static_cast<void (CryptoContextImpl<DCRTPoly>::*)(PKESchemeFeature)>(&CryptoContextImpl<DCRTPoly>::Enable), "Enable a feature for the CryptoContext")
  23. .def("KeyGen",&CryptoContextImpl<DCRTPoly>::KeyGen,"Generate a key pair with public and private keys")
  24. .def("EvalMultKeyGen",&CryptoContextImpl<DCRTPoly>::EvalMultKeyGen,"Generate the evaluation key for multiplication")
  25. .def("EvalRotateKeyGen",&CryptoContextImpl<DCRTPoly>::EvalRotateKeyGen,"Generate the evaluation key for rotation",
  26. py::arg("privateKey"),py::arg("indexList"),py::arg("publicKey")=nullptr)
  27. .def("MakePackedPlaintext",&CryptoContextImpl<DCRTPoly>::MakePackedPlaintext,"Make a plaintext from a vector of integers",
  28. py::arg("value"),py::arg("depth")=1,py::arg("level")=0)
  29. .def("EvalRotate",&CryptoContextImpl<DCRTPoly>::EvalRotate,"Rotate a ciphertext")
  30. .def("Encrypt",static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(const PublicKey<DCRTPoly>, Plaintext) const>(&CryptoContextImpl<DCRTPoly>::Encrypt),"Encrypt a plaintext using public key")
  31. .def("EvalAdd", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstCiphertext<DCRTPoly>) const>(&CryptoContextImpl<DCRTPoly>::EvalAdd),"Add two ciphertexts")
  32. .def("EvalMult", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstCiphertext<DCRTPoly>) const>(&CryptoContextImpl<DCRTPoly>::EvalMult),"Multiply two ciphertexts");
  33. // Generator Functions
  34. m.def("GenCryptoContext", &GenCryptoContext<CryptoContextBFVRNS>);
  35. m.def("GenCryptoContext", &GenCryptoContext<CryptoContextBGVRNS>);
  36. }
  37. void bind_enums(py::module &m){
  38. // Scheme Types
  39. py::enum_<SCHEME>(m, "SCHEME")
  40. .value("INVALID_SCHEME", SCHEME::INVALID_SCHEME)
  41. .value("CKKSRNS_SCHEME", SCHEME::CKKSRNS_SCHEME)
  42. .value("BFVRNS_SCHEME", SCHEME::BFVRNS_SCHEME)
  43. .value("BGVRNS_SCHEME", SCHEME::BGVRNS_SCHEME);
  44. // PKE Features
  45. py::enum_<PKESchemeFeature>(m, "PKESchemeFeature")
  46. .value("PKE", PKESchemeFeature::PKE)
  47. .value("KEYSWITCH", PKESchemeFeature::KEYSWITCH)
  48. .value("PRE", PKESchemeFeature::PRE)
  49. .value("LEVELEDSHE", PKESchemeFeature::LEVELEDSHE)
  50. .value("ADVANCEDSHE", PKESchemeFeature::ADVANCEDSHE)
  51. .value("MULTIPARTY", PKESchemeFeature::MULTIPARTY)
  52. .value("FHE", PKESchemeFeature::FHE);
  53. }
  54. void bind_keys(py::module &m){
  55. py::class_<PublicKeyImpl<DCRTPoly>,std::shared_ptr<PublicKeyImpl<DCRTPoly>>>(m,"PublicKey");
  56. py::class_<PrivateKeyImpl<DCRTPoly>,std::shared_ptr<PrivateKeyImpl<DCRTPoly>>>(m,"PrivateKey");
  57. py::class_<KeyPair<DCRTPoly>>(m,"KeyPair")
  58. .def_readwrite("publicKey", &KeyPair<DCRTPoly>::publicKey)
  59. .def_readwrite("secretKey", &KeyPair<DCRTPoly>::secretKey);
  60. }
  61. void bind_encodings(py::module &m){
  62. py::class_<PlaintextImpl,std::shared_ptr<PlaintextImpl>>(m,"Plaintext")
  63. .def("GetScalingFactor", &PlaintextImpl::GetScalingFactor)
  64. .def("SetScalingFactor", &PlaintextImpl::SetScalingFactor)
  65. .def("GetLength", &PlaintextImpl::GetLength)
  66. .def("GetSchemeID", &PlaintextImpl::GetSchemeID)
  67. .def("SetLength", &PlaintextImpl::SetLength)
  68. .def("IsEncoded", &PlaintextImpl::IsEncoded)
  69. //.def("GetEncondingParams", &PlaintextImpl::GetEncondingParams)
  70. .def("Encode", &PlaintextImpl::Encode)
  71. .def("Decode", &PlaintextImpl::Decode)
  72. .def("__repr__", [] (const PlaintextImpl& p) {
  73. std::stringstream ss;
  74. ss << "<Plaintext Object: ";
  75. p.PrintValue(ss);
  76. ss << ">";
  77. return ss.str();
  78. })
  79. .def("__str__", [] (const PlaintextImpl& p) {
  80. std::stringstream ss;
  81. p.PrintValue(ss);
  82. return ss.str();
  83. });
  84. }
  85. void bind_ciphertext(py::module &m){
  86. py::class_<CiphertextImpl<DCRTPoly>,std::shared_ptr<CiphertextImpl<DCRTPoly>>>(m,"Ciphertext")
  87. .def(py::init<>());
  88. // .def("GetDepth", &CiphertextImpl<DCRTPoly>::GetDepth)
  89. // .def("SetDepth", &CiphertextImpl<DCRTPoly>::SetDepth)
  90. // .def("GetLevel", &CiphertextImpl<DCRTPoly>::GetLevel)
  91. // .def("SetLevel", &CiphertextImpl<DCRTPoly>::SetLevel)
  92. // .def("GetHopLevel", &CiphertextImpl<DCRTPoly>::GetHopLevel)
  93. // .def("SetHopLevel", &CiphertextImpl<DCRTPoly>::SetHopLevel)
  94. // .def("GetScalingFactor", &CiphertextImpl<DCRTPoly>::GetScalingFactor)
  95. // .def("SetScalingFactor", &CiphertextImpl<DCRTPoly>::SetScalingFactor)
  96. // .def("GetSlots", &CiphertextImpl<DCRTPoly>::GetSlots)
  97. // .def("SetSlots", &CiphertextImpl<DCRTPoly>::SetSlots);
  98. }
  99. PYBIND11_MODULE(openfhe, m) {
  100. m.doc() = "Open-Source Fully Homomorphic Encryption Library";
  101. bind_parameters(m);
  102. bind_crypto_context(m);
  103. bind_enums(m);
  104. bind_keys(m);
  105. bind_encodings(m);
  106. bind_ciphertext(m);
  107. bind_decryption(m);
  108. }