Browse Source

FFICiphertext class added.

nkaskov 1 year ago
parent
commit
1a1c8a2cab
2 changed files with 66 additions and 23 deletions
  1. 25 0
      cpp/include/bindings.hpp
  2. 41 23
      cpp/src/bindings.cpp

+ 25 - 0
cpp/include/bindings.hpp

@@ -209,6 +209,31 @@ public:
     FFIPrivateKeyImpl GetPrivateKey() const;
 };
 
+// Ciphertext FFI
+
+class FFICiphertext {
+protected:
+    void* ciphertext_ptr;
+public:
+    FFICiphertext();
+
+    explicit FFICiphertext(void* new_ciphertext_ptr){
+        ciphertext_ptr = new_ciphertext_ptr;
+    }
+
+    std::size_t GetLevel() const;
+
+    void SetLevel(std::size_t level);
+
+   FFICiphertext Clone() const;
+
+//  TODO add RemoveElement method from wrappers
+
+    std::size_t GetSlots() const;
+
+    void SetSlots(std::size_t slots);
+};
+
 // Params FFI
 class FFIParams{
 protected:

+ 41 - 23
cpp/src/bindings.cpp

@@ -35,6 +35,10 @@ namespace {
     struct KeyPairHolder{
            std::shared_ptr<KeyPair<DCRTPoly>> ptr;
     };
+
+    struct CiphertextHolder{
+           std::shared_ptr<CiphertextImpl<DCRTPoly>> ptr;
+    };
 }
 
 FFIParams::FFIParams(){
@@ -1424,29 +1428,43 @@ FFIPrivateKeyImpl FFIKeyPair::GetPrivateKey() const{
 //         return ss.str(); });
 // }
 
-// void bind_ciphertext(py::module &m)
-// {
-//     py::class_<CiphertextImpl<DCRTPoly>, std::shared_ptr<CiphertextImpl<DCRTPoly>>>(m, "Ciphertext")
-//         .def(py::init<>())
-//         .def("__add__", [](const Ciphertext<DCRTPoly> &a, const Ciphertext<DCRTPoly> &b)
-//              {return a + b; },py::is_operator(),pybind11::keep_alive<0, 1>())
-//        // .def(py::self + py::self);
-//     // .def("GetDepth", &CiphertextImpl<DCRTPoly>::GetDepth)
-//     // .def("SetDepth", &CiphertextImpl<DCRTPoly>::SetDepth)
-//      .def("GetLevel", &CiphertextImpl<DCRTPoly>::GetLevel,
-//         ctx_GetLevel_docs)
-//      .def("SetLevel", &CiphertextImpl<DCRTPoly>::SetLevel,
-//         ctx_SetLevel_docs,
-//         py::arg("level"))
-//      .def("Clone", &CiphertextImpl<DCRTPoly>::Clone)
-//      .def("RemoveElement", &RemoveElementWrapper, cc_RemoveElement_docs)
-//     // .def("GetHopLevel", &CiphertextImpl<DCRTPoly>::GetHopLevel)
-//     // .def("SetHopLevel", &CiphertextImpl<DCRTPoly>::SetHopLevel)
-//     // .def("GetScalingFactor", &CiphertextImpl<DCRTPoly>::GetScalingFactor)
-//     // .def("SetScalingFactor", &CiphertextImpl<DCRTPoly>::SetScalingFactor)
-//      .def("GetSlots", &CiphertextImpl<DCRTPoly>::GetSlots)
-//      .def("SetSlots", &CiphertextImpl<DCRTPoly>::SetSlots);
-// }
+FFICiphertext::FFICiphertext(){
+    ciphertext_ptr = reinterpret_cast<void*>(
+        new CiphertextHolder{std::make_shared<CiphertextImpl<DCRTPoly>>()});
+}
+
+std::size_t FFICiphertext::GetLevel() const{
+    std::shared_ptr<const CiphertextImpl<DCRTPoly>> ciphertext =
+        reinterpret_cast<CiphertextHolder*>(ciphertext_ptr)->ptr;
+    return ciphertext->GetLevel();
+}
+
+void FFICiphertext::SetLevel(std::size_t level){
+    std::shared_ptr<CiphertextImpl<DCRTPoly>> ciphertext =
+        reinterpret_cast<CiphertextHolder*>(ciphertext_ptr)->ptr;
+    ciphertext->SetLevel(level);
+}
+
+FFICiphertext FFICiphertext::Clone() const{
+    std::shared_ptr<CiphertextImpl<DCRTPoly>> ciphertext =
+        reinterpret_cast<CiphertextHolder*>(ciphertext_ptr)->ptr;
+    void* new_ciphertext_ptr =
+        reinterpret_cast<void*>(
+            new CiphertextHolder{ciphertext->Clone()});
+    return FFICiphertext{new_ciphertext_ptr};
+}
+
+std::size_t FFICiphertext::GetSlots() const{
+    std::shared_ptr<const CiphertextImpl<DCRTPoly>> ciphertext =
+        reinterpret_cast<CiphertextHolder*>(ciphertext_ptr)->ptr;
+    return ciphertext->GetSlots();
+}
+
+void FFICiphertext::SetSlots(std::size_t slots){
+    std::shared_ptr<CiphertextImpl<DCRTPoly>> ciphertext =
+        reinterpret_cast<CiphertextHolder*>(ciphertext_ptr)->ptr;
+    ciphertext->SetSlots(slots);
+}
 
 // void bind_schemes(py::module &m){
 //     /*Bind schemes specific functionalities like bootstrapping functions and multiparty*/