Browse Source

Adding some new functions

Hovsep Papoyan 8 months ago
parent
commit
aabd0ecdd7
6 changed files with 82 additions and 7 deletions
  1. 3 0
      build.rs
  2. 26 7
      src/CryptoContext.cc
  3. 8 0
      src/CryptoContext.h
  4. 19 0
      src/LWEPrivateKey.cc
  5. 24 0
      src/LWEPrivateKey.h
  6. 2 0
      src/lib.rs

+ 3 - 0
build.rs

@@ -9,6 +9,7 @@ fn main()
         .file("src/PublicKey.cc")
         .file("src/SerialDeserial.cc")
         .file("src/EvalKey.cc")
+        .file("src/LWEPrivateKey.cc")
         .include("/usr/local/include/openfhe")
         .include("/usr/local/include/openfhe/third-party/include")
         .include("/usr/local/include/openfhe/core")
@@ -42,6 +43,8 @@ fn main()
     println!("cargo::rerun-if-changed=src/SerialDeserial.cc");
     println!("cargo::rerun-if-changed=src/EvalKey.h");
     println!("cargo::rerun-if-changed=src/EvalKey.cc");
+    println!("cargo::rerun-if-changed=src/LWEPrivateKey.h");
+    println!("cargo::rerun-if-changed=src/LWEPrivateKey.cc");
 
     // linking openFHE
     println!("cargo::rustc-link-arg=-L/usr/local/lib");

+ 26 - 7
src/CryptoContext.cc

@@ -12,6 +12,7 @@
 #include "Plaintext.h"
 #include "PublicKey.h"
 #include "EvalKey.h"
+#include "LWEPrivateKey.h"
 
 namespace openfhe
 {
@@ -728,30 +729,49 @@ std::unique_ptr<EvalKeyDCRTPoly> CryptoContextDCRTPoly::MultiKeySwitchGen(
 }
 std::unique_ptr<EvalKeyDCRTPoly> CryptoContextDCRTPoly::MultiAddEvalKeys(
     const EvalKeyDCRTPoly& evalKey1, const EvalKeyDCRTPoly& evalKey2,
-    const std::string& keyId /* "" */) const
+    const std::string& keyId) const
 {
     return std::make_unique<EvalKeyDCRTPoly>(m_cryptoContextImplSharedPtr->MultiAddEvalKeys(
         evalKey1.GetInternal(), evalKey2.GetInternal(), keyId));
 }
 std::unique_ptr<EvalKeyDCRTPoly> CryptoContextDCRTPoly::MultiMultEvalKey(
     const std::shared_ptr<PrivateKeyImpl> privateKey, const EvalKeyDCRTPoly& evalKey,
-    const std::string& keyId /* "" */) const
+    const std::string& keyId) const
 {
     return std::make_unique<EvalKeyDCRTPoly>(m_cryptoContextImplSharedPtr->MultiMultEvalKey(
         privateKey, evalKey.GetInternal(), keyId));
 }
 std::unique_ptr<EvalKeyDCRTPoly> CryptoContextDCRTPoly::MultiAddEvalMultKeys(
     const EvalKeyDCRTPoly& evalKey1, const EvalKeyDCRTPoly& evalKey2,
-    const std::string& keyId /* "" */) const
+    const std::string& keyId) const
 {
     return std::make_unique<EvalKeyDCRTPoly>(m_cryptoContextImplSharedPtr->MultiAddEvalMultKeys(
         evalKey1.GetInternal(), evalKey2.GetInternal(), keyId));
 }
 void CryptoContextDCRTPoly::EvalSumKeyGen(const std::shared_ptr<PrivateKeyImpl> privateKey,
-    const std::shared_ptr<PublicKeyImpl> publicKey /* nullptr */) const
+    const std::shared_ptr<PublicKeyImpl> publicKey) const
 {
     m_cryptoContextImplSharedPtr->EvalSumKeyGen(privateKey, publicKey);
 }
+void CryptoContextDCRTPoly::EvalCKKStoFHEWKeyGen(const KeyPairDCRTPoly& keyPair,
+    const LWEPrivateKey& lwesk) const
+{
+    m_cryptoContextImplSharedPtr->EvalCKKStoFHEWKeyGen({keyPair.GetPublicKey(),
+        keyPair.GetPrivateKey()}, lwesk.GetInternal());
+}
+void CryptoContextDCRTPoly::EvalFHEWtoCKKSKeyGen(const KeyPairDCRTPoly& keyPair,
+    const LWEPrivateKey& lwesk, const uint32_t numSlots, const uint32_t numCtxts,
+    const uint32_t dim1, const uint32_t L) const
+{
+    m_cryptoContextImplSharedPtr->EvalFHEWtoCKKSKeyGen({keyPair.GetPublicKey(),
+        keyPair.GetPrivateKey()}, lwesk.GetInternal(), numSlots, numCtxts, dim1, L);
+}
+void CryptoContextDCRTPoly::EvalSchemeSwitchingKeyGen(const KeyPairDCRTPoly& keyPair,
+    const LWEPrivateKey& lwesk) const
+{
+    m_cryptoContextImplSharedPtr->EvalSchemeSwitchingKeyGen({keyPair.GetPublicKey(),
+        keyPair.GetPrivateKey()}, lwesk.GetInternal());
+}
 std::shared_ptr<CryptoContextImpl> CryptoContextDCRTPoly::GetInternal() const
 {
     return m_cryptoContextImplSharedPtr;
@@ -803,10 +823,9 @@ std::unique_ptr<std::vector<uint32_t>> GetExistingEvalAutomorphismKeyIndices(
 std::unique_ptr<std::vector<uint32_t>> GetUniqueValues(const std::vector<uint32_t>& oldValues,
     const std::vector<uint32_t>& newValues)
 {
-    return std::make_unique<std::vector<uint32_t>>(
-        CryptoContextImpl::GetUniqueValues(oldValues, newValues));
+    return std::make_unique<std::vector<uint32_t>>(CryptoContextImpl::GetUniqueValues(oldValues,
+        newValues));
 }
-
 std::unique_ptr<CryptoContextDCRTPoly> GenEmptyCryptoContext()
 {
     return std::make_unique<CryptoContextDCRTPoly>();

+ 8 - 0
src/CryptoContext.h

@@ -34,6 +34,7 @@ class PublicKeyDCRTPoly;
 class Plaintext;
 class CiphertextDCRTPoly;
 class EvalKeyDCRTPoly;
+class LWEPrivateKey;
 
 using SCHEME = lbcrypto::SCHEME;
 using PKESchemeFeature = lbcrypto::PKESchemeFeature;
@@ -320,6 +321,13 @@ public:
         const std::string& keyId /* "" */) const;
     void EvalSumKeyGen(const std::shared_ptr<PrivateKeyImpl> privateKey,
         const std::shared_ptr<PublicKeyImpl> publicKey /* nullptr */) const;
+    void EvalCKKStoFHEWKeyGen(const KeyPairDCRTPoly& keyPair, const LWEPrivateKey& lwesk) const;
+    void EvalFHEWtoCKKSKeyGen(const KeyPairDCRTPoly& keyPair, const LWEPrivateKey& lwesk,
+        const uint32_t numSlots /* 0 */, const uint32_t numCtxts /* 0 */,
+        const uint32_t dim1 /* 0 */, const uint32_t L /* 0 */) const;
+    void EvalSchemeSwitchingKeyGen(const KeyPairDCRTPoly& keyPair,
+        const LWEPrivateKey& lwesk) const;
+
     [[nodiscard]] std::shared_ptr<CryptoContextImpl> GetInternal() const;
 };
 // cxx currently does not support static class methods

+ 19 - 0
src/LWEPrivateKey.cc

@@ -0,0 +1,19 @@
+#include "LWEPrivateKey.h"
+
+#include "openfhe/binfhe/lwe-privatekey.h"
+
+namespace openfhe
+{
+
+LWEPrivateKey::LWEPrivateKey()
+    : m_lwePrivateKey(std::make_shared<LWEPrivateKeyImpl>())
+{ }
+LWEPrivateKey::LWEPrivateKey(const std::shared_ptr<LWEPrivateKeyImpl>& lwePrivateKey)
+    : m_lwePrivateKey(lwePrivateKey)
+{ }
+std::shared_ptr<LWEPrivateKeyImpl> LWEPrivateKey::GetInternal() const
+{
+    return m_lwePrivateKey;
+}
+
+} // openfhe

+ 24 - 0
src/LWEPrivateKey.h

@@ -0,0 +1,24 @@
+#pragma once
+
+#include "openfhe/binfhe/lwe-privatekey-fwd.h"
+
+namespace openfhe
+{
+
+using LWEPrivateKeyImpl = lbcrypto::LWEPrivateKeyImpl;
+
+class LWEPrivateKey final
+{
+    std::shared_ptr<LWEPrivateKeyImpl> m_lwePrivateKey;
+public:
+    explicit LWEPrivateKey();
+    explicit LWEPrivateKey(const std::shared_ptr<LWEPrivateKeyImpl>& lwePrivateKey);
+    LWEPrivateKey(const LWEPrivateKey&) = delete;
+    LWEPrivateKey(LWEPrivateKey&&) = delete;
+    LWEPrivateKey& operator=(const LWEPrivateKey&) = delete;
+    LWEPrivateKey& operator=(LWEPrivateKey&&) = delete;
+
+    [[nodiscard]] std::shared_ptr<LWEPrivateKeyImpl> GetInternal() const;
+};
+
+} // openfhe

+ 2 - 0
src/lib.rs

@@ -154,6 +154,7 @@ pub mod ffi
         include!("openfhe/src/PublicKey.h");
         include!("openfhe/src/SerialDeserial.h");
         include!("openfhe/src/EvalKey.h");
+        include!("openfhe/src/LWEPrivateKey.h");
 
         type COMPRESSION_LEVEL;
         type DecryptionNoiseMode;
@@ -185,6 +186,7 @@ pub mod ffi
         type PublicKeyDCRTPoly;
         type PublicKeyImpl;
         type EvalKeyDCRTPoly;
+        type LWEPrivateKey;
     }
 
     // Params