bindings.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. // BSD 2-Clause License
  2. // Copyright (c) 2023, OpenFHE
  3. // All rights reserved.
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are met:
  6. // 1. Redistributions of source code must retain the above copyright notice, this
  7. // list of conditions and the following disclaimer.
  8. // 2. Redistributions in binary form must reproduce the above copyright notice,
  9. // this list of conditions and the following disclaimer in the documentation
  10. // and/or other materials provided with the distribution.
  11. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  12. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  13. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  15. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  16. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  18. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  19. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  20. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. #ifndef OPENFHE_BINDINGS_H
  22. #define OPENFHE_BINDINGS_H
  23. #include <cstdint>
  24. #include "constants-fwd.h"
  25. // forward declarations
  26. class FFIPublicKeyImpl;
  27. class FFIPrivateKeyImpl;
  28. class FFIKeyPair;
  29. class FFIParams;
  30. class FFICryptoContextImpl;
  31. typedef std::uint32_t usint;
  32. typedef std::uint64_t FFIPlaintextModulus;
  33. // enums from stdlatticeparms.h
  34. enum FFISecurityLevel {
  35. HEStd_128_classic,
  36. HEStd_192_classic,
  37. HEStd_256_classic,
  38. HEStd_128_quantum,
  39. HEStd_192_quantum,
  40. HEStd_256_quantum,
  41. HEStd_NotSet,
  42. };
  43. // enums from constants-lattice.h
  44. enum FFISecretKeyDist {
  45. GAUSSIAN = 0,
  46. UNIFORM_TERNARY = 1, // Default value, all schemes support this key distribution
  47. SPARSE_TERNARY = 2,
  48. // BINARY = 3, // Future implementation
  49. };
  50. // enums from scheme-id.h
  51. enum FFISCHEME {
  52. INVALID_SCHEME = 0,
  53. CKKSRNS_SCHEME,
  54. BFVRNS_SCHEME,
  55. BGVRNS_SCHEME,
  56. };
  57. enum CryptoContextType {
  58. BFVRNS,
  59. BGVRNS,
  60. // Add other types as needed
  61. };
  62. // KeyPair FFI forwad declaration
  63. class FFIKeyPair;
  64. // PublicKeyImpl FFI
  65. class FFIPublicKeyImpl {
  66. protected:
  67. void* pubkey_ptr;
  68. public:
  69. FFIPublicKeyImpl();
  70. explicit FFIPublicKeyImpl(void* new_pubkey_ptr);
  71. void SetKeyTag(const char*& tag);
  72. const char* GetKeyTag() const;
  73. friend class FFIKeyPair;
  74. friend class FFICryptoContextImpl;
  75. };
  76. // PrivateKeyImpl FFI
  77. class FFIPrivateKeyImpl {
  78. protected:
  79. void* privkey_ptr;
  80. public:
  81. FFIPrivateKeyImpl();
  82. explicit FFIPrivateKeyImpl(void* new_privkey_ptr);
  83. void SetKeyTag(const char*& tag);
  84. const char* GetKeyTag() const;
  85. friend class FFIKeyPair;
  86. friend class FFICryptoContextImpl;
  87. };
  88. // KeyPair FFI
  89. class FFIKeyPair {
  90. protected:
  91. void* keypair_ptr;
  92. public:
  93. FFIKeyPair();
  94. explicit FFIKeyPair(void* new_keypair_ptr){
  95. keypair_ptr = new_keypair_ptr;
  96. }
  97. explicit FFIKeyPair(const FFIPublicKeyImpl& publicKey, const FFIPrivateKeyImpl& privateKey);
  98. bool is_allocated() const;
  99. FFIPublicKeyImpl GetPublicKey() const;
  100. FFIPrivateKeyImpl GetPrivateKey() const;
  101. };
  102. // Plaintext FFI
  103. class FFIPlaintext {
  104. protected:
  105. void* plaintext_ptr;
  106. public:
  107. explicit FFIPlaintext(void* new_plaintext_ptr){
  108. plaintext_ptr = new_plaintext_ptr;
  109. }
  110. double GetScalingFactor() const;
  111. void SetScalingFactor(double sf);
  112. FFISCHEME GetSchemeID() const;
  113. std::size_t GetLength() const;
  114. void SetLength(std::size_t newSize);
  115. bool IsEncoded() const;
  116. double GetLogPrecision() const;
  117. void Encode();
  118. void Decode();
  119. std::int64_t LowBound() const;
  120. std::int64_t HighBound() const;
  121. };
  122. // Ciphertext FFI
  123. class FFICiphertext {
  124. protected:
  125. void* ciphertext_ptr;
  126. public:
  127. FFICiphertext();
  128. explicit FFICiphertext(void* new_ciphertext_ptr){
  129. ciphertext_ptr = new_ciphertext_ptr;
  130. }
  131. std::size_t GetLevel() const;
  132. void SetLevel(std::size_t level);
  133. FFICiphertext Clone() const;
  134. // TODO add RemoveElement method from wrappers
  135. std::size_t GetSlots() const;
  136. void SetSlots(std::size_t slots);
  137. friend class FFICryptoContextImpl;
  138. };
  139. // Params FFI
  140. class FFIParams{
  141. protected:
  142. void* params_ptr;
  143. public:
  144. FFIParams();
  145. FFIParams(FFISCHEME scheme);
  146. FFIPlaintextModulus GetPlaintextModulus() const;
  147. FFISCHEME GetScheme() const;
  148. usint GetDigitSize() const;
  149. float GetStandardDeviation() const;
  150. FFISecretKeyDist GetSecretKeyDist() const;
  151. usint GetMaxRelinSkDeg() const;
  152. lbcrypto::ProxyReEncryptionMode GetPREMode() const;
  153. lbcrypto::MultipartyMode GetMultipartyMode() const;
  154. lbcrypto::ExecutionMode GetExecutionMode() const;
  155. lbcrypto::DecryptionNoiseMode GetDecryptionNoiseMode() const;
  156. double GetNoiseEstimate() const;
  157. double GetDesiredPrecision() const;
  158. double GetStatisticalSecurity() const;
  159. double GetNumAdversarialQueries() const;
  160. usint GetThresholdNumOfParties() const;
  161. lbcrypto::KeySwitchTechnique GetKeySwitchTechnique() const;
  162. lbcrypto::ScalingTechnique GetScalingTechnique() const;
  163. usint GetBatchSize() const;
  164. usint GetFirstModSize() const;
  165. uint32_t GetNumLargeDigits() const;
  166. usint GetMultiplicativeDepth() const;
  167. usint GetScalingModSize() const;
  168. FFISecurityLevel GetSecurityLevel() const;
  169. usint GetRingDim() const;
  170. usint GetEvalAddCount() const;
  171. usint GetKeySwitchCount() const;
  172. lbcrypto::EncryptionTechnique GetEncryptionTechnique() const;
  173. lbcrypto::MultiplicationTechnique GetMultiplicationTechnique() const;
  174. usint GetMultiHopModSize() const;
  175. lbcrypto::COMPRESSION_LEVEL GetInteractiveBootCompressionLevel() const;
  176. void SetPlaintextModulus(FFIPlaintextModulus ptModulus);
  177. void SetDigitSize(usint digitSize);
  178. void SetStandardDeviation(float standardDeviation);
  179. void SetSecretKeyDist(FFISecretKeyDist secretKeyDist);
  180. void SetMaxRelinSkDeg(usint maxRelinSkDeg);
  181. void SetPREMode(lbcrypto::ProxyReEncryptionMode preMode);
  182. void SetMultipartyMode(lbcrypto::MultipartyMode multipartyMode);
  183. void SetExecutionMode(lbcrypto::ExecutionMode executionMode);
  184. void SetDecryptionNoiseMode(lbcrypto::DecryptionNoiseMode decryptionNoiseMode);
  185. void SetNoiseEstimate(double noiseEstimate);
  186. void SetDesiredPrecision(double desiredPrecision);
  187. void SetStatisticalSecurity(uint32_t statisticalSecurity);
  188. void SetNumAdversarialQueries(uint32_t numAdversarialQueries);
  189. void SetThresholdNumOfParties(uint32_t thresholdNumOfParties);
  190. void SetKeySwitchTechnique(lbcrypto::KeySwitchTechnique keySwitchTechnique);
  191. void SetScalingTechnique(lbcrypto::ScalingTechnique scalingTechnique);
  192. void SetBatchSize(usint batchSize);
  193. void SetFirstModSize(usint firstModSize);
  194. void SetNumLargeDigits(uint32_t numLargeDigits);
  195. void SetMultiplicativeDepth(usint multiplicativeDepth);
  196. void SetScalingModSize(usint scalingModSize);
  197. void SetSecurityLevel(FFISecurityLevel securityLevel);
  198. void SetRingDim(usint ringDim);
  199. void SetEvalAddCount(usint evalAddCount);
  200. void SetKeySwitchCount(usint keySwitchCount);
  201. void SetEncryptionTechnique(lbcrypto::EncryptionTechnique encryptionTechnique);
  202. void SetMultiplicationTechnique(lbcrypto::MultiplicationTechnique multiplicationTechnique);
  203. void SetMultiHopModSize(usint multiHopModSize);
  204. void SetInteractiveBootCompressionLevel(lbcrypto::COMPRESSION_LEVEL interactiveBootCompressionLevel);
  205. // std::stream str();
  206. friend FFICryptoContextImpl GenCryptoContext(FFIParams params);
  207. };
  208. // class CryptoContextBFVRNSCCParams : public FFIParams {
  209. // public:
  210. // // CryptoContextBFVRNSCCParams();
  211. // CryptoContextBFVRNSCCParams():FFIParams(BFVRNS_SCHEME){};
  212. // };
  213. // class CryptoContextBGVRNSCCParams : public FFIParams {
  214. // public:
  215. // // CryptoContextBGVRNSCCParams();
  216. // CryptoContextBGVRNSCCParams():FFIParams(BGVRNS_SCHEME){};
  217. // };
  218. // CryptoContext FFI
  219. class FFICryptoContextImpl {
  220. protected:
  221. void* cc_ptr;
  222. public:
  223. FFICryptoContextImpl();
  224. std::size_t GetKeyGenLevel() const;
  225. void SetKeyGenLevel(std::size_t level);
  226. usint GetRingDimension() const;
  227. FFIPlaintextModulus GetPlaintextModulus() const;
  228. double GetModulus() const;
  229. const uint64_t GetModulusCKKS() const;
  230. double GetScalingFactorReal(uint32_t level) const;
  231. lbcrypto::ScalingTechnique GetScalingTechnique() const;
  232. usint GetDigitSize() const;
  233. usint GetCyclotomicOrder() const;
  234. void Enable(lbcrypto::PKESchemeFeature feature);
  235. FFIKeyPair KeyGen() const;
  236. void EvalMultKeyGen(const FFIPrivateKeyImpl key);
  237. void EvalMultKeysGen(const FFIPrivateKeyImpl key);
  238. // void EvalRotateKeyGen(const FFIPrivateKeyImpl privateKey, const std::vector<int32_t>& indexList,
  239. // const FFIPublicKey publicKey = nullptr);
  240. // FFIPlaintext MakeStringPlaintext(const std::string& str) const;
  241. // FFIPlaintext MakePackedPlaintext(const std::vector<int64_t>& value, std::size_t noiseScaleDeg = 1,
  242. // uint32_t level = 0) const;
  243. // FFIPlaintext MakeCoefPackedPlaintext(const std::vector<int64_t>& value, std::size_t noiseScaleDeg = 1,
  244. // uint32_t level = 0) const;
  245. // Plaintext MakeCKKSPackedPlaintext(const std::vector<std::complex<double>>& value, std::size_t scaleDeg = 1,
  246. // uint32_t level = 0, const std::shared_ptr<ParmType> params = nullptr,
  247. // usint slots = 0) const;
  248. // Plaintext MakeCKKSPackedPlaintext(const std::vector<double>& value, std::size_t scaleDeg = 1, uint32_t level = 0,
  249. // const std::shared_ptr<ParmType> params = nullptr, usint slots = 0) const;
  250. FFICiphertext EvalRotate(const FFICiphertext ciphertext, std::int32_t index) const;
  251. // // const?
  252. // Ciphertext<DCRTPoly> EvalFastRotationPrecompute(ConstCiphertext<DCRTPoly> ciphertext);
  253. // // const?
  254. // Ciphertext<DCRTPoly> EvalFastRotation(ConstCiphertext<DCRTPoly> ciphertext, const usint index, const usint m,ConstCiphertext<DCRTPoly> digits);
  255. // // const?
  256. // Ciphertext<DCRTPoly> EvalFastRotationExt(ConstCiphertext<DCRTPoly> ciphertext, const usint index, ConstCiphertext<DCRTPoly> digits, bool addFirst);
  257. // void EvalAtIndexKeyGen(const PrivateKey<Element> privateKey, const std::vector<int32_t>& indexList,
  258. // const PublicKey<Element> publicKey = nullptr);
  259. // Ciphertext<Element> EvalAtIndex(ConstCiphertext<Element> ciphertext, int32_t index) const;
  260. // FFICiphertext Encrypt(const FFIPublicKeyImpl publicKey, FFIPlaintext plaintext) const;
  261. // DecryptResult Decrypt(ConstCiphertext<Element> ciphertext, const PrivateKey<Element> privateKey,
  262. // Plaintext* plaintext);
  263. // inline DecryptResult Decrypt(const PrivateKey<Element> privateKey, ConstCiphertext<Element> ciphertext,
  264. // Plaintext* plaintext);
  265. // EvalKey<Element> KeySwitchGen(const PrivateKey<Element> oldPrivateKey,
  266. // const PrivateKey<Element> newPrivateKey) const;
  267. // Ciphertext<Element> EvalAdd(ConstCiphertext<Element> ciphertext, double constant) const;
  268. // Ciphertext<Element> EvalAdd(ConstCiphertext<Element> ciphertext1, ConstCiphertext<Element> ciphertext2) const;
  269. // Ciphertext<Element> EvalAdd(ConstCiphertext<Element> ciphertext, ConstPlaintext plaintext) const;
  270. // void EvalAddInPlace(Ciphertext<Element>& ciphertext, ConstPlaintext plaintext) const;
  271. // void EvalAddInPlace(ConstPlaintext plaintext, Ciphertext<Element>& ciphertext) const;
  272. // void EvalAddInPlace(Ciphertext<Element>& ciphertext1, ConstCiphertext<Element> ciphertext2) const;
  273. // Ciphertext<Element> EvalAddMutable(Ciphertext<Element>& ciphertext1, Ciphertext<Element>& ciphertext2) const;
  274. // Ciphertext<Element> EvalAddMutable(Ciphertext<Element>& ciphertext, Plaintext plaintext) const;
  275. // Ciphertext<Element> EvalAddMutable(Plaintext plaintext, Ciphertext<Element>& ciphertext) const;
  276. // void EvalAddMutableInPlace(Ciphertext<Element>& ciphertext1, Ciphertext<Element>& ciphertext2) const;
  277. // Ciphertext<Element> EvalSub(ConstCiphertext<Element> ciphertext1, ConstCiphertext<Element> ciphertext2) const;
  278. // Ciphertext<Element> EvalSub(ConstCiphertext<Element> ciphertext, ConstPlaintext plaintext) const;
  279. // Ciphertext<Element> EvalSub(ConstPlaintext plaintext, ConstCiphertext<Element> ciphertext) const;
  280. // Ciphertext<Element> EvalSub(ConstCiphertext<Element> ciphertext, double constant) const;
  281. // Ciphertext<Element> EvalSub(double constant, ConstCiphertext<Element> ciphertext) const;
  282. // void EvalSubInPlace(Ciphertext<Element>& ciphertext, double constant) const;
  283. // void EvalSubInPlace(double constant, Ciphertext<Element>& ciphertext) const;
  284. // void EvalSubInPlace(Ciphertext<Element>& ciphertext1, ConstCiphertext<Element> ciphertext2) const;
  285. // Ciphertext<Element> EvalSubMutable(Ciphertext<Element>& ciphertext1, Ciphertext<Element>& ciphertext2) const;
  286. // Ciphertext<Element> EvalSubMutable(Ciphertext<Element>& ciphertext, Plaintext plaintext) const;
  287. // Ciphertext<Element> EvalSubMutable(Plaintext plaintext, Ciphertext<Element>& ciphertext) const;
  288. // void EvalSubMutableInPlace(Ciphertext<Element>& ciphertext1, Ciphertext<Element>& ciphertext2) const;
  289. friend FFICryptoContextImpl GenCryptoContext(FFIParams params);
  290. };
  291. FFICryptoContextImpl GenCryptoContext(FFIParams params);
  292. #endif // OPENFHE_BINDINGS_H