Bläddra i källkod

Adding VectorOfCiphertexts and functions

Hovsep Papoyan 6 månader sedan
förälder
incheckning
05bf7207e7
6 ändrade filer med 227 tillägg och 0 borttagningar
  1. 3 0
      build.rs
  2. 106 0
      src/CryptoContext.cc
  3. 42 0
      src/CryptoContext.h
  4. 18 0
      src/VectorOfCiphertexts.cc
  5. 22 0
      src/VectorOfCiphertexts.h
  6. 36 0
      src/lib.rs

+ 3 - 0
build.rs

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

+ 106 - 0
src/CryptoContext.cc

@@ -13,6 +13,7 @@
 #include "PublicKey.h"
 #include "EvalKey.h"
 #include "LWEPrivateKey.h"
+#include "VectorOfCiphertexts.h"
 
 namespace openfhe
 {
@@ -785,6 +786,111 @@ uint64_t CryptoContextDCRTPoly::GetRootOfUnity() const
 {
     return m_cryptoContextImplSharedPtr->GetRootOfUnity().ConvertToInt();
 }
+std::unique_ptr<VectorOfCiphertexts> CryptoContextDCRTPoly::MultipartyDecryptLead(
+    const VectorOfCiphertexts& ciphertextVec,
+    const std::shared_ptr<PrivateKeyImpl> privateKey) const
+{
+    return std::make_unique<VectorOfCiphertexts>(
+        m_cryptoContextImplSharedPtr->MultipartyDecryptLead(ciphertextVec.GetInternal(),
+        privateKey));
+}
+std::unique_ptr<VectorOfCiphertexts> CryptoContextDCRTPoly::MultipartyDecryptMain(
+    const VectorOfCiphertexts& ciphertextVec,
+    const std::shared_ptr<PrivateKeyImpl> privateKey) const
+{
+    return std::make_unique<VectorOfCiphertexts>(
+        m_cryptoContextImplSharedPtr->MultipartyDecryptMain(ciphertextVec.GetInternal(),
+        privateKey));
+}
+std::unique_ptr<VectorOfCiphertexts> CryptoContextDCRTPoly::IntMPBootDecrypt(
+    const std::shared_ptr<PrivateKeyImpl> privateKey, const CiphertextDCRTPoly& ciphertext,
+    const CiphertextDCRTPoly& a) const
+{
+    return std::make_unique<VectorOfCiphertexts>(m_cryptoContextImplSharedPtr->IntMPBootDecrypt(
+        privateKey, ciphertext.GetInternal(), a.GetInternal()));
+}
+std::unique_ptr<VectorOfCiphertexts> CryptoContextDCRTPoly::EvalMinSchemeSwitching(
+    const CiphertextDCRTPoly& ciphertext, const PublicKeyDCRTPoly& publicKey,
+    const uint32_t numValues, const uint32_t numSlots, const uint32_t pLWE,
+    const double scaleSign) const
+{
+    return std::make_unique<VectorOfCiphertexts>(
+        m_cryptoContextImplSharedPtr->EvalMinSchemeSwitching(ciphertext.GetInternal(),
+        publicKey.GetInternal(), numValues, numSlots, pLWE, scaleSign));
+}
+std::unique_ptr<VectorOfCiphertexts> CryptoContextDCRTPoly::EvalMinSchemeSwitchingAlt(
+    const CiphertextDCRTPoly& ciphertext, const PublicKeyDCRTPoly& publicKey,
+    const uint32_t numValues, const uint32_t numSlots, const uint32_t pLWE,
+    const double scaleSign) const
+{
+    return std::make_unique<VectorOfCiphertexts>(
+        m_cryptoContextImplSharedPtr->EvalMinSchemeSwitchingAlt(ciphertext.GetInternal(),
+        publicKey.GetInternal(), numValues, numSlots, pLWE, scaleSign));
+}
+std::unique_ptr<VectorOfCiphertexts> CryptoContextDCRTPoly::EvalMaxSchemeSwitching(
+    const CiphertextDCRTPoly& ciphertext, const PublicKeyDCRTPoly& publicKey,
+    const uint32_t numValues, const uint32_t numSlots, const uint32_t pLWE,
+    const double scaleSign) const
+{
+    return std::make_unique<VectorOfCiphertexts>(
+        m_cryptoContextImplSharedPtr->EvalMaxSchemeSwitching(ciphertext.GetInternal(),
+        publicKey.GetInternal(), numValues, numSlots, pLWE, scaleSign));
+}
+std::unique_ptr<VectorOfCiphertexts> CryptoContextDCRTPoly::EvalMaxSchemeSwitchingAlt(
+    const CiphertextDCRTPoly& ciphertext, const PublicKeyDCRTPoly& publicKey,
+    const uint32_t numValues, const uint32_t numSlots, const uint32_t pLWE,
+    const double scaleSign) const
+{
+    return std::make_unique<VectorOfCiphertexts>(
+        m_cryptoContextImplSharedPtr->EvalMaxSchemeSwitchingAlt(ciphertext.GetInternal(),
+        publicKey.GetInternal(), numValues, numSlots, pLWE, scaleSign));
+}
+std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalAddMany(
+    const VectorOfCiphertexts& ciphertextVec) const
+{
+    return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalAddMany(
+        ciphertextVec.GetInternal()));
+}
+std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalMultMany(
+    const VectorOfCiphertexts& ciphertextVec) const
+{
+    return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalMultMany(
+        ciphertextVec.GetInternal()));
+}
+std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalMerge(
+    const VectorOfCiphertexts& ciphertextVec) const
+{
+    return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalMerge(
+        ciphertextVec.GetInternal()));
+}
+std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::IntMPBootEncrypt(
+    const PublicKeyDCRTPoly& publicKey, const VectorOfCiphertexts& sharesPair,
+    const CiphertextDCRTPoly& a, const CiphertextDCRTPoly& ciphertext) const
+{
+    return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->IntMPBootEncrypt(
+        publicKey.GetInternal(), sharesPair.GetInternal(), a.GetInternal(),
+        ciphertext.GetInternal()));
+}
+std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalAddManyInPlace(
+    VectorOfCiphertexts& ciphertextVec) const
+{
+    return std::make_unique<CiphertextDCRTPoly>(m_cryptoContextImplSharedPtr->EvalAddManyInPlace(
+        ciphertextVec.GetInternal()));
+}
+std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalLinearWSumMutable(
+    VectorOfCiphertexts& ciphertextVec, const std::vector<double>& constantsVec) const
+{
+    return std::make_unique<CiphertextDCRTPoly>(
+        m_cryptoContextImplSharedPtr->EvalLinearWSumMutable(ciphertextVec.GetInternal(),
+        constantsVec));
+}
+std::unique_ptr<CiphertextDCRTPoly> CryptoContextDCRTPoly::EvalLinearWSumMutable(
+    const std::vector<double>& constantsVec, VectorOfCiphertexts& ciphertextVec) const
+{
+    return std::make_unique<CiphertextDCRTPoly>(
+        m_cryptoContextImplSharedPtr->EvalLinearWSumMutable(constantsVec,
+        ciphertextVec.GetInternal()));
+}
 std::shared_ptr<CryptoContextImpl> CryptoContextDCRTPoly::GetInternal() const
 {
     return m_cryptoContextImplSharedPtr;

+ 42 - 0
src/CryptoContext.h

@@ -35,6 +35,7 @@ class Plaintext;
 class CiphertextDCRTPoly;
 class EvalKeyDCRTPoly;
 class LWEPrivateKey;
+class VectorOfCiphertexts;
 
 using SCHEME = lbcrypto::SCHEME;
 using PKESchemeFeature = lbcrypto::PKESchemeFeature;
@@ -332,6 +333,47 @@ public:
         const LWEPrivateKey& lwesk) const;
     [[nodiscard]] uint64_t GetModulus() const;
     [[nodiscard]] uint64_t GetRootOfUnity() const;
+    [[nodiscard]] std::unique_ptr<VectorOfCiphertexts> MultipartyDecryptLead(
+        const VectorOfCiphertexts& ciphertextVec,
+        const std::shared_ptr<PrivateKeyImpl> privateKey) const;
+    [[nodiscard]] std::unique_ptr<VectorOfCiphertexts> MultipartyDecryptMain(
+        const VectorOfCiphertexts& ciphertextVec,
+        const std::shared_ptr<PrivateKeyImpl> privateKey) const;
+    [[nodiscard]] std::unique_ptr<VectorOfCiphertexts> IntMPBootDecrypt(
+        const std::shared_ptr<PrivateKeyImpl> privateKey, const CiphertextDCRTPoly& ciphertext,
+        const CiphertextDCRTPoly& a) const;
+    [[nodiscard]] std::unique_ptr<VectorOfCiphertexts> EvalMinSchemeSwitching(
+        const CiphertextDCRTPoly& ciphertext, const PublicKeyDCRTPoly& publicKey,
+        const uint32_t numValues /* 0 */, const uint32_t numSlots /* 0 */,
+        const uint32_t pLWE /* 0 */, const double scaleSign /* 1.0 */) const;
+    [[nodiscard]] std::unique_ptr<VectorOfCiphertexts> EvalMinSchemeSwitchingAlt(
+        const CiphertextDCRTPoly& ciphertext, const PublicKeyDCRTPoly& publicKey,
+        const uint32_t numValues /* 0 */, const uint32_t numSlots /* 0 */,
+        const uint32_t pLWE /* 0 */, const double scaleSign /* 1.0 */) const;
+    [[nodiscard]] std::unique_ptr<VectorOfCiphertexts> EvalMaxSchemeSwitching(
+        const CiphertextDCRTPoly& ciphertext, const PublicKeyDCRTPoly& publicKey,
+        const uint32_t numValues /* 0 */, const uint32_t numSlots /* 0 */,
+        const uint32_t pLWE /* 0 */, const double scaleSign /* 1.0 */) const;
+    [[nodiscard]] std::unique_ptr<VectorOfCiphertexts> EvalMaxSchemeSwitchingAlt(
+        const CiphertextDCRTPoly& ciphertext, const PublicKeyDCRTPoly& publicKey,
+        const uint32_t numValues /* 0 */, const uint32_t numSlots /* 0 */,
+        const uint32_t pLWE /* 0 */, const double scaleSign /* 1.0 */) const;
+    [[nodiscard]] std::unique_ptr<CiphertextDCRTPoly> EvalAddMany(
+        const VectorOfCiphertexts& ciphertextVec) const;
+    [[nodiscard]] std::unique_ptr<CiphertextDCRTPoly> EvalMultMany(
+        const VectorOfCiphertexts& ciphertextVec) const;
+    [[nodiscard]] std::unique_ptr<CiphertextDCRTPoly> EvalMerge(
+        const VectorOfCiphertexts& ciphertextVec) const;
+    [[nodiscard]] std::unique_ptr<CiphertextDCRTPoly> IntMPBootEncrypt(
+        const PublicKeyDCRTPoly& publicKey, const VectorOfCiphertexts& sharesPair,
+        const CiphertextDCRTPoly& a, const CiphertextDCRTPoly& ciphertext) const;
+    [[nodiscard]] std::unique_ptr<CiphertextDCRTPoly> EvalAddManyInPlace(
+        VectorOfCiphertexts& ciphertextVec) const;
+    [[nodiscard]] std::unique_ptr<CiphertextDCRTPoly> EvalLinearWSumMutable(
+        VectorOfCiphertexts& ciphertextVec, const std::vector<double>& constantsVec) const;
+    [[nodiscard]] std::unique_ptr<CiphertextDCRTPoly> EvalLinearWSumMutable(
+        const std::vector<double>& constantsVec, VectorOfCiphertexts& ciphertextVec) const;
+
     [[nodiscard]] std::shared_ptr<CryptoContextImpl> GetInternal() const;
 };
 // cxx currently does not support static class methods

+ 18 - 0
src/VectorOfCiphertexts.cc

@@ -0,0 +1,18 @@
+#include "VectorOfCiphertexts.h"
+
+namespace openfhe
+{
+
+VectorOfCiphertexts::VectorOfCiphertexts(std::vector<std::shared_ptr<CiphertextImpl>> ciphertexts)
+    : m_ciphertexts(std::move(ciphertexts))
+{ }
+const std::vector<std::shared_ptr<CiphertextImpl>>& VectorOfCiphertexts::GetInternal() const
+{
+    return m_ciphertexts;
+}
+std::vector<std::shared_ptr<CiphertextImpl>>& VectorOfCiphertexts::GetInternal()
+{
+    return m_ciphertexts;
+}
+
+} // openfhe

+ 22 - 0
src/VectorOfCiphertexts.h

@@ -0,0 +1,22 @@
+#pragma once
+
+#include "openfhe/core/lattice/hal/lat-backend.h"
+#include "openfhe/pke/ciphertext-fwd.h"
+
+#include <vector>
+
+namespace openfhe
+{
+
+using CiphertextImpl = lbcrypto::CiphertextImpl<lbcrypto::DCRTPoly>;
+
+class VectorOfCiphertexts final
+{
+    std::vector<std::shared_ptr<CiphertextImpl>> m_ciphertexts;
+public:
+    VectorOfCiphertexts(std::vector<std::shared_ptr<CiphertextImpl>> ciphertexts);
+    [[nodiscard]] const std::vector<std::shared_ptr<CiphertextImpl>>& GetInternal() const;
+    [[nodiscard]] std::vector<std::shared_ptr<CiphertextImpl>>& GetInternal();
+};
+
+} // openfhe

+ 36 - 0
src/lib.rs

@@ -155,6 +155,7 @@ pub mod ffi
         include!("openfhe/src/SerialDeserial.h");
         include!("openfhe/src/EvalKey.h");
         include!("openfhe/src/LWEPrivateKey.h");
+        include!("openfhe/src/VectorOfCiphertexts.h");
 
         type COMPRESSION_LEVEL;
         type DecryptionNoiseMode;
@@ -187,6 +188,7 @@ pub mod ffi
         type PublicKeyImpl;
         type EvalKeyDCRTPoly;
         type LWEPrivateKey;
+        type VectorOfCiphertexts;
     }
 
     // Params
@@ -843,6 +845,40 @@ pub mod ffi
                                      lwesk: &LWEPrivateKey);
         fn GetModulus(self: &CryptoContextDCRTPoly) -> u64;
         fn GetRootOfUnity(self: &CryptoContextDCRTPoly) -> u64;
+        fn MultipartyDecryptLead(self: &CryptoContextDCRTPoly, ciphertextVec: &VectorOfCiphertexts,
+                                 privateKey: SharedPtr<PrivateKeyImpl>)
+                                 -> UniquePtr<VectorOfCiphertexts>;
+        fn MultipartyDecryptMain(self: &CryptoContextDCRTPoly, ciphertextVec: &VectorOfCiphertexts,
+                                 privateKey: SharedPtr<PrivateKeyImpl>)
+                                 -> UniquePtr<VectorOfCiphertexts>;
+        fn IntMPBootDecrypt(self: &CryptoContextDCRTPoly, privateKey: SharedPtr<PrivateKeyImpl>,
+                            ciphertext: &CiphertextDCRTPoly, a: &CiphertextDCRTPoly)
+                            -> UniquePtr<VectorOfCiphertexts>;
+        fn EvalMinSchemeSwitching(self: &CryptoContextDCRTPoly, ciphertext: &CiphertextDCRTPoly,
+                                  publicKey: &PublicKeyDCRTPoly, numValues: /* 0 */ u32,
+                                  numSlots: /* 0 */ u32, pLWE: /* 0 */ u32,
+                                  scaleSign: /* 1.0 */ f64) -> UniquePtr<VectorOfCiphertexts>;
+        fn EvalMinSchemeSwitchingAlt(self: &CryptoContextDCRTPoly, ciphertext: &CiphertextDCRTPoly,
+                                     publicKey: &PublicKeyDCRTPoly, numValues: /* 0 */ u32,
+                                     numSlots: /* 0 */ u32, pLWE: /* 0 */ u32,
+                                     scaleSign: /* 1.0 */ f64) -> UniquePtr<VectorOfCiphertexts>;
+        fn EvalMaxSchemeSwitching(self: &CryptoContextDCRTPoly, ciphertext: &CiphertextDCRTPoly,
+                                  publicKey: &PublicKeyDCRTPoly, numValues: /* 0 */ u32,
+                                  numSlots: /* 0 */ u32, pLWE: /* 0 */ u32,
+                                  scaleSign: /* 1.0 */ f64) -> UniquePtr<VectorOfCiphertexts>;
+        fn EvalMaxSchemeSwitchingAlt(self: &CryptoContextDCRTPoly, ciphertext: &CiphertextDCRTPoly,
+                                     publicKey: &PublicKeyDCRTPoly, numValues: /* 0 */ u32,
+                                     numSlots: /* 0 */ u32, pLWE: /* 0 */ u32,
+                                     scaleSign: /* 1.0 */ f64) -> UniquePtr<VectorOfCiphertexts>;
+        fn EvalAddMany(self: &CryptoContextDCRTPoly, ciphertextVec: &VectorOfCiphertexts)
+                       -> UniquePtr<CiphertextDCRTPoly>;
+        fn EvalMultMany(self: &CryptoContextDCRTPoly, ciphertextVec: &VectorOfCiphertexts)
+                        -> UniquePtr<CiphertextDCRTPoly>;
+        fn EvalMerge(self: &CryptoContextDCRTPoly, ciphertextVec: &VectorOfCiphertexts)
+                     -> UniquePtr<CiphertextDCRTPoly>;
+        fn IntMPBootEncrypt(self: &CryptoContextDCRTPoly, publicKey: &PublicKeyDCRTPoly,
+                            sharesPair: &VectorOfCiphertexts, a: &CiphertextDCRTPoly,
+                            ciphertext: &CiphertextDCRTPoly) -> UniquePtr<CiphertextDCRTPoly>;
 
         // cxx currently does not support static class methods
         fn ClearEvalMultKeys();