Browse Source

FFIPublicKeyImpl and FFIPrivateKeyImpl bindings added.

nkaskov 1 year ago
parent
commit
db6f4210b1
2 changed files with 78 additions and 12 deletions
  1. 34 4
      cpp/include/bindings.hpp
  2. 44 8
      cpp/src/bindings.cpp

+ 34 - 4
cpp/include/bindings.hpp

@@ -141,6 +141,36 @@ enum CryptoContextType {
     // Add other types as needed
 };
 
+// PublicKeyImpl FFI
+
+class FFIPublicKeyImpl {
+protected:
+    void* pubkey_ptr;
+public:
+    FFIPublicKeyImpl();
+
+    void SetKeyTag(const std::string& tag);
+
+    const std::string GetKeyTag() const;
+};
+
+using FFIPublicKey = std::shared_ptr<FFIPublicKeyImpl>;
+
+// PrivateKeyImpl FFI
+
+class FFIPrivateKeyImpl {
+protected:
+    void* privkey_ptr;
+public:
+    FFIPrivateKeyImpl();
+
+    void SetKeyTag(const std::string& tag);
+
+    const std::string GetKeyTag() const;
+};
+
+using FFIPrivateKey = std::shared_ptr<FFIPrivateKeyImpl>;
+
 // Params FFI
 class FFIParams{
 protected:
@@ -315,12 +345,12 @@ public:
 
 //     KeyPair<Element> KeyGen();
 
-//     void EvalMultKeyGen(const PrivateKey<Element> key);
+//     void EvalMultKeyGen(const FFIPrivateKey key);
 
-//     void EvalMultKeysGen(const PrivateKey<Element> key);
+//     void EvalMultKeysGen(const FFIPrivateKey key);
 
-//     void EvalRotateKeyGen(const PrivateKey<Element> privateKey, const std::vector<int32_t>& indexList,
-//                           const PublicKey<Element> publicKey = nullptr);
+//     void EvalRotateKeyGen(const FFIPrivateKey privateKey, const std::vector<int32_t>& indexList,
+//                           const FFIPublicKey publicKey = nullptr);
 
 //     FFIPlaintext MakeStringPlaintext(const std::string& str) const;
 

+ 44 - 8
cpp/src/bindings.cpp

@@ -23,6 +23,14 @@ namespace {
     struct CryptoContextImplHolder{
            std::shared_ptr<CryptoContextImpl<DCRTPoly>> ptr;
     };
+
+    struct PubkeyHolder{
+           std::shared_ptr<PublicKeyImpl<DCRTPoly>> ptr;
+    };
+
+    struct PrivkeyHolder{
+           std::shared_ptr<PrivateKeyImpl<DCRTPoly>> ptr;
+    };
 }
 
 FFIParams::FFIParams(){
@@ -1204,16 +1212,44 @@ void FFICryptoContextImpl::Enable(FFIPKESchemeFeature feature) {
     
 // }
 
+FFIPublicKeyImpl::FFIPublicKeyImpl(){
+    pubkey_ptr = reinterpret_cast<void*>(
+        new PubkeyHolder{std::make_shared<PublicKeyImpl<DCRTPoly>>()});
+}
+
+void FFIPublicKeyImpl::SetKeyTag(const std::string& tag){
+    std::shared_ptr<PublicKeyImpl<DCRTPoly>> pubkey =
+        reinterpret_cast<PubkeyHolder*>(pubkey_ptr)->ptr;
+    pubkey->SetKeyTag(tag);
+}
+
+const std::string FFIPublicKeyImpl::GetKeyTag() const{
+    std::shared_ptr<const PublicKeyImpl<DCRTPoly>> pubkey =
+        reinterpret_cast<PubkeyHolder*>(pubkey_ptr)->ptr;
+    return pubkey->GetKeyTag();
+}
+
+
+FFIPrivateKeyImpl::FFIPrivateKeyImpl(){
+    privkey_ptr = reinterpret_cast<void*>(
+        new PrivkeyHolder{std::make_shared<PrivateKeyImpl<DCRTPoly>>()});
+}
+
+void FFIPrivateKeyImpl::SetKeyTag(const std::string& tag){
+    std::shared_ptr<PrivateKeyImpl<DCRTPoly>> privkey =
+        reinterpret_cast<PrivkeyHolder*>(privkey_ptr)->ptr;
+    privkey->SetKeyTag(tag);
+}
+
+const std::string FFIPrivateKeyImpl::GetKeyTag() const{
+    std::shared_ptr<const PrivateKeyImpl<DCRTPoly>> privkey =
+        reinterpret_cast<PrivkeyHolder*>(privkey_ptr)->ptr;
+    return privkey->GetKeyTag();
+}
+
+
 // void bind_keys(py::module &m)
 // {
-//     py::class_<PublicKeyImpl<DCRTPoly>, std::shared_ptr<PublicKeyImpl<DCRTPoly>>>(m, "PublicKey")
-//         .def(py::init<>())
-//         .def("GetKeyTag", &PublicKeyImpl<DCRTPoly>::GetKeyTag)
-//         .def("SetKeyTag", &PublicKeyImpl<DCRTPoly>::SetKeyTag);
-//     py::class_<PrivateKeyImpl<DCRTPoly>, std::shared_ptr<PrivateKeyImpl<DCRTPoly>>>(m, "PrivateKey")
-//         .def(py::init<>())
-//         .def("GetKeyTag", &PrivateKeyImpl<DCRTPoly>::GetKeyTag)
-//         .def("SetKeyTag", &PrivateKeyImpl<DCRTPoly>::SetKeyTag);
 //     py::class_<KeyPair<DCRTPoly>>(m, "KeyPair")
 //         .def_readwrite("publicKey", &KeyPair<DCRTPoly>::publicKey)
 //         .def_readwrite("secretKey", &KeyPair<DCRTPoly>::secretKey)