bindings.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // .def_property("multiplicativeDepth",
  18. // &CCParams<CryptoContextBFVRNS>::GetMultiplicativeDepth,
  19. // &CCParams<CryptoContextBFVRNS>::SetMultiplicativeDepth)
  20. // .def_property("ptModulus",
  21. // &CCParams<CryptoContextBFVRNS>::GetPlaintextModulus,
  22. // &CCParams<CryptoContextBFVRNS>::SetPlaintextModulus);
  23. }
  24. void bind_crypto_context(py::module &m){
  25. py::class_<CryptoContextImpl<DCRTPoly>,std::shared_ptr<CryptoContextImpl<DCRTPoly>>>(m,"CryptoContextDCRTPoly")
  26. .def("GetKeyGenLevel",&CryptoContextImpl<DCRTPoly>::GetKeyGenLevel)
  27. .def("SetKeyGenLevel",&CryptoContextImpl<DCRTPoly>::SetKeyGenLevel)
  28. .def("Enable",static_cast<void (CryptoContextImpl<DCRTPoly>::*)(PKESchemeFeature)>(&CryptoContextImpl<DCRTPoly>::Enable), "Enable a feature for the CryptoContext")
  29. .def("KeyGen",&CryptoContextImpl<DCRTPoly>::KeyGen,"Generate a key pair with public and private keys")
  30. .def("EvalMultKeyGen",&CryptoContextImpl<DCRTPoly>::EvalMultKeyGen,"Generate the evaluation key for multiplication")
  31. .def("EvalRotateKeyGen",&CryptoContextImpl<DCRTPoly>::EvalRotateKeyGen,"Generate the evaluation key for rotation",
  32. py::arg("privateKey"),py::arg("indexList"),py::arg("publicKey")=nullptr)
  33. .def("MakePackedPlaintext",&CryptoContextImpl<DCRTPoly>::MakePackedPlaintext,"Make a plaintext from a vector of integers",
  34. py::arg("value"),py::arg("depth")=1,py::arg("level")=0)
  35. .def("EvalRotate",&CryptoContextImpl<DCRTPoly>::EvalRotate,"Rotate a ciphertext")
  36. .def("Encrypt",static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(const PublicKey<DCRTPoly>, Plaintext) const>(&CryptoContextImpl<DCRTPoly>::Encrypt),"Encrypt a plaintext using public key");
  37. //.def("MakePackedPlaintext",static_cast<Plaintext (CryptoContextImpl<DCRTPoly>::*)(const std::vector<int64_t>&)>(&CryptoContextImpl<DCRTPoly>::MakePackedPlaintext), "Make a plaintext from a vector of integers")
  38. // Generator Functions
  39. m.def("GenCryptoContext", &GenCryptoContext<CryptoContextBFVRNS>);
  40. m.def("GenCryptoContext", &GenCryptoContext<CryptoContextBGVRNS>);
  41. }
  42. void bind_enums(py::module &m){
  43. // Scheme Types
  44. py::enum_<SCHEME>(m, "SCHEME")
  45. .value("INVALID_SCHEME", SCHEME::INVALID_SCHEME)
  46. .value("CKKSRNS_SCHEME", SCHEME::CKKSRNS_SCHEME)
  47. .value("BFVRNS_SCHEME", SCHEME::BFVRNS_SCHEME)
  48. .value("BGVRNS_SCHEME", SCHEME::BGVRNS_SCHEME);
  49. // PKE Features
  50. py::enum_<PKESchemeFeature>(m, "PKESchemeFeature")
  51. .value("PKE", PKESchemeFeature::PKE)
  52. .value("KEYSWITCH", PKESchemeFeature::KEYSWITCH)
  53. .value("PRE", PKESchemeFeature::PRE)
  54. .value("LEVELEDSHE", PKESchemeFeature::LEVELEDSHE)
  55. .value("ADVANCEDSHE", PKESchemeFeature::ADVANCEDSHE)
  56. .value("MULTIPARTY", PKESchemeFeature::MULTIPARTY)
  57. .value("FHE", PKESchemeFeature::FHE);
  58. }
  59. void bind_keys(py::module &m){
  60. py::class_<PublicKeyImpl<DCRTPoly>,std::shared_ptr<PublicKeyImpl<DCRTPoly>>>(m,"PublicKey");
  61. py::class_<PrivateKeyImpl<DCRTPoly>,std::shared_ptr<PrivateKeyImpl<DCRTPoly>>>(m,"PrivateKey");
  62. py::class_<KeyPair<DCRTPoly>>(m,"KeyPair")
  63. .def_readwrite("publicKey", &KeyPair<DCRTPoly>::publicKey)
  64. .def_readwrite("secretKey", &KeyPair<DCRTPoly>::secretKey);
  65. }
  66. void bind_encodings(py::module &m){
  67. py::class_<PlaintextImpl,std::shared_ptr<PlaintextImpl>>(m,"Plaintext")
  68. .def("GetScalingFactor", &PlaintextImpl::GetScalingFactor)
  69. .def("SetScalingFactor", &PlaintextImpl::SetScalingFactor)
  70. .def("GetSchemeID", &PlaintextImpl::GetSchemeID)
  71. .def("IsEncoded", &PlaintextImpl::IsEncoded)
  72. //.def("GetEncondingParams", &PlaintextImpl::GetEncondingParams)
  73. .def("Encode", &PlaintextImpl::Encode)
  74. .def("Decode", &PlaintextImpl::Decode);
  75. }
  76. void bind_ciphertext(py::module &m){
  77. py::class_<CiphertextImpl<DCRTPoly>,std::shared_ptr<CiphertextImpl<DCRTPoly>>>(m,"Ciphertext")
  78. .def(py::init<>());
  79. // .def("GetDepth", &CiphertextImpl<DCRTPoly>::GetDepth)
  80. // .def("SetDepth", &CiphertextImpl<DCRTPoly>::SetDepth)
  81. // .def("GetLevel", &CiphertextImpl<DCRTPoly>::GetLevel)
  82. // .def("SetLevel", &CiphertextImpl<DCRTPoly>::SetLevel)
  83. // .def("GetHopLevel", &CiphertextImpl<DCRTPoly>::GetHopLevel)
  84. // .def("SetHopLevel", &CiphertextImpl<DCRTPoly>::SetHopLevel)
  85. // .def("GetScalingFactor", &CiphertextImpl<DCRTPoly>::GetScalingFactor)
  86. // .def("SetScalingFactor", &CiphertextImpl<DCRTPoly>::SetScalingFactor)
  87. // .def("GetSlots", &CiphertextImpl<DCRTPoly>::GetSlots)
  88. // .def("SetSlots", &CiphertextImpl<DCRTPoly>::SetSlots);
  89. }
  90. PYBIND11_MODULE(openfhe, m) {
  91. m.doc() = "Open-Source Fully Homomorphic Encryption Library";
  92. bind_parameters(m);
  93. bind_crypto_context(m);
  94. bind_enums(m);
  95. bind_keys(m);
  96. bind_encodings(m);
  97. bind_ciphertext(m);
  98. bind_decryption(m);
  99. }