bindings.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. typedef std::uint32_t usint;
  25. typedef std::uint64_t FFIPlaintextModulus;
  26. // enums from constants.h
  27. enum FFIMultipartyMode {
  28. INVALID_MULTIPARTY_MODE = 0,
  29. FIXED_NOISE_MULTIPARTY,
  30. NOISE_FLOODING_MULTIPARTY,
  31. };
  32. enum FFIProxyReEncryptionMode {
  33. NOT_SET = 0,
  34. INDCPA,
  35. FIXED_NOISE_HRA,
  36. NOISE_FLOODING_HRA,
  37. DIVIDE_AND_ROUND_HRA,
  38. };
  39. enum FFIExecutionMode {
  40. EXEC_EVALUATION = 0,
  41. EXEC_NOISE_ESTIMATION,
  42. };
  43. enum FFIDecryptionNoiseMode {
  44. FIXED_NOISE_DECRYPT = 0,
  45. NOISE_FLOODING_DECRYPT,
  46. };
  47. enum FFIKeySwitchTechnique {
  48. INVALID_KS_TECH = 0,
  49. BV,
  50. HYBRID,
  51. };
  52. enum FFIScalingTechnique {
  53. FIXEDMANUAL = 0,
  54. FIXEDAUTO,
  55. FLEXIBLEAUTO,
  56. FLEXIBLEAUTOEXT,
  57. NORESCALE,
  58. INVALID_RS_TECHNIQUE, // TODO (dsuponit): make this the first value
  59. };
  60. enum FFIEncryptionTechnique {
  61. STANDARD = 0,
  62. EXTENDED,
  63. };
  64. enum FFIMultiplicationTechnique {
  65. BEHZ = 0,
  66. HPS,
  67. HPSPOVERQ,
  68. HPSPOVERQLEVELED,
  69. };
  70. enum FFICOMPRESSION_LEVEL {
  71. // we don't support 0 or 1 compression levels
  72. // do not change values here
  73. COMPACT = 2, // more efficient with stronger security assumption
  74. SLACK = 3 // less efficient with weaker security assumption
  75. };
  76. enum FFIPKESchemeFeature {
  77. PKE = 0x01,
  78. KEYSWITCH = 0x02,
  79. PRE = 0x04,
  80. LEVELEDSHE = 0x08,
  81. ADVANCEDSHE = 0x10,
  82. MULTIPARTY = 0x20,
  83. FHE = 0x40,
  84. SCHEMESWITCH = 0x80,
  85. };
  86. // enums from stdlatticeparms.h
  87. enum FFISecurityLevel {
  88. HEStd_128_classic,
  89. HEStd_192_classic,
  90. HEStd_256_classic,
  91. HEStd_128_quantum,
  92. HEStd_192_quantum,
  93. HEStd_256_quantum,
  94. HEStd_NotSet,
  95. };
  96. // enums from constants-lattice.h
  97. enum FFISecretKeyDist {
  98. GAUSSIAN = 0,
  99. UNIFORM_TERNARY = 1, // Default value, all schemes support this key distribution
  100. SPARSE_TERNARY = 2,
  101. // BINARY = 3, // Future implementation
  102. };
  103. // enums from scheme-id.h
  104. enum FFISCHEME {
  105. INVALID_SCHEME = 0,
  106. CKKSRNS_SCHEME,
  107. BFVRNS_SCHEME,
  108. BGVRNS_SCHEME,
  109. };
  110. enum CryptoContextType {
  111. BFVRNS,
  112. BGVRNS,
  113. // Add other types as needed
  114. };
  115. // PublicKeyImpl FFI
  116. class FFIPublicKeyImpl {
  117. protected:
  118. void* pubkey_ptr;
  119. public:
  120. FFIPublicKeyImpl();
  121. void SetKeyTag(const std::string& tag);
  122. const std::string GetKeyTag() const;
  123. };
  124. using FFIPublicKey = std::shared_ptr<FFIPublicKeyImpl>;
  125. // PrivateKeyImpl FFI
  126. class FFIPrivateKeyImpl {
  127. protected:
  128. void* privkey_ptr;
  129. public:
  130. FFIPrivateKeyImpl();
  131. void SetKeyTag(const std::string& tag);
  132. const std::string GetKeyTag() const;
  133. };
  134. using FFIPrivateKey = std::shared_ptr<FFIPrivateKeyImpl>;
  135. // Params FFI
  136. class FFIParams{
  137. protected:
  138. void* params_ptr;
  139. public:
  140. FFIParams();
  141. FFIParams(FFISCHEME scheme);
  142. virtual FFIPlaintextModulus GetPlaintextModulus() const;
  143. virtual FFISCHEME GetScheme() const;
  144. usint GetDigitSize() const;
  145. float GetStandardDeviation() const;
  146. FFISecretKeyDist GetSecretKeyDist() const;
  147. usint GetMaxRelinSkDeg() const;
  148. FFIProxyReEncryptionMode GetPREMode() const;
  149. FFIMultipartyMode GetMultipartyMode() const;
  150. FFIExecutionMode GetExecutionMode() const;
  151. FFIDecryptionNoiseMode GetDecryptionNoiseMode() const;
  152. double GetNoiseEstimate() const;
  153. double GetDesiredPrecision() const;
  154. double GetStatisticalSecurity() const;
  155. double GetNumAdversarialQueries() const;
  156. usint GetThresholdNumOfParties() const;
  157. FFIKeySwitchTechnique GetKeySwitchTechnique() const;
  158. FFIScalingTechnique GetScalingTechnique() const;
  159. usint GetBatchSize() const;
  160. usint GetFirstModSize() const;
  161. uint32_t GetNumLargeDigits() const;
  162. usint GetMultiplicativeDepth() const;
  163. usint GetScalingModSize() const;
  164. FFISecurityLevel GetSecurityLevel() const;
  165. usint GetRingDim() const;
  166. usint GetEvalAddCount() const;
  167. usint GetKeySwitchCount() const;
  168. FFIEncryptionTechnique GetEncryptionTechnique() const;
  169. FFIMultiplicationTechnique GetMultiplicationTechnique() const;
  170. usint GetMultiHopModSize() const;
  171. FFICOMPRESSION_LEVEL GetInteractiveBootCompressionLevel() const;
  172. void SetPlaintextModulus(FFIPlaintextModulus ptModulus);
  173. void SetDigitSize(usint digitSize);
  174. void SetStandardDeviation(float standardDeviation);
  175. void SetSecretKeyDist(FFISecretKeyDist secretKeyDist);
  176. void SetMaxRelinSkDeg(usint maxRelinSkDeg);
  177. void SetPREMode(FFIProxyReEncryptionMode preMode);
  178. void SetMultipartyMode(FFIMultipartyMode multipartyMode);
  179. void SetExecutionMode(FFIExecutionMode executionMode);
  180. void SetDecryptionNoiseMode(FFIDecryptionNoiseMode decryptionNoiseMode);
  181. void SetNoiseEstimate(double noiseEstimate);
  182. void SetDesiredPrecision(double desiredPrecision);
  183. void SetStatisticalSecurity(uint32_t statisticalSecurity);
  184. void SetNumAdversarialQueries(uint32_t numAdversarialQueries);
  185. void SetThresholdNumOfParties(uint32_t thresholdNumOfParties);
  186. void SetKeySwitchTechnique(FFIKeySwitchTechnique keySwitchTechnique);
  187. void SetScalingTechnique(FFIScalingTechnique scalingTechnique);
  188. void SetBatchSize(usint batchSize);
  189. void SetFirstModSize(usint firstModSize);
  190. void SetNumLargeDigits(uint32_t numLargeDigits);
  191. void SetMultiplicativeDepth(usint multiplicativeDepth);
  192. void SetScalingModSize(usint scalingModSize);
  193. void SetSecurityLevel(FFISecurityLevel securityLevel);
  194. void SetRingDim(usint ringDim);
  195. void SetEvalAddCount(usint evalAddCount);
  196. void SetKeySwitchCount(usint keySwitchCount);
  197. void SetEncryptionTechnique(FFIEncryptionTechnique encryptionTechnique);
  198. void SetMultiplicationTechnique(FFIMultiplicationTechnique multiplicationTechnique);
  199. void SetMultiHopModSize(usint multiHopModSize);
  200. void SetInteractiveBootCompressionLevel(FFICOMPRESSION_LEVEL interactiveBootCompressionLevel);
  201. // std::stream str();
  202. };
  203. class CryptoContextBFVRNSCCParams : public FFIParams {
  204. public:
  205. // CryptoContextBFVRNSCCParams();
  206. CryptoContextBFVRNSCCParams():FFIParams(BFVRNS_SCHEME){};
  207. };
  208. class CryptoContextBGVRNSCCParams : public FFIParams {
  209. public:
  210. // CryptoContextBGVRNSCCParams();
  211. CryptoContextBGVRNSCCParams():FFIParams(BGVRNS_SCHEME){};
  212. };
  213. // CryptoContext FFI
  214. class FFICryptoContextImpl {
  215. protected:
  216. void* cc_ptr;
  217. public:
  218. FFICryptoContextImpl();
  219. std::size_t GetKeyGenLevel() const;
  220. void SetKeyGenLevel(std::size_t level);
  221. usint GetRingDimension() const;
  222. FFIPlaintextModulus GetPlaintextModulus() const;
  223. double GetModulus() const;
  224. const uint64_t GetModulusCKKS() const;
  225. double GetScalingFactorReal(uint32_t level) const;
  226. FFIScalingTechnique GetScalingTechnique() const;
  227. usint GetDigitSize() const;
  228. usint GetCyclotomicOrder() const;
  229. void Enable(FFIPKESchemeFeature feature);
  230. // KeyPair<Element> KeyGen();
  231. // void EvalMultKeyGen(const FFIPrivateKey key);
  232. // void EvalMultKeysGen(const FFIPrivateKey key);
  233. // void EvalRotateKeyGen(const FFIPrivateKey privateKey, const std::vector<int32_t>& indexList,
  234. // const FFIPublicKey publicKey = nullptr);
  235. // FFIPlaintext MakeStringPlaintext(const std::string& str) const;
  236. // FFIPlaintext MakePackedPlaintext(const std::vector<int64_t>& value, std::size_t noiseScaleDeg = 1,
  237. // uint32_t level = 0) const;
  238. // FFIPlaintext MakeCoefPackedPlaintext(const std::vector<int64_t>& value, std::size_t noiseScaleDeg = 1,
  239. // uint32_t level = 0) const;
  240. // Plaintext MakeCKKSPackedPlaintext(const std::vector<std::complex<double>>& value, std::size_t scaleDeg = 1,
  241. // uint32_t level = 0, const std::shared_ptr<ParmType> params = nullptr,
  242. // usint slots = 0) const;
  243. // Plaintext MakeCKKSPackedPlaintext(const std::vector<double>& value, std::size_t scaleDeg = 1, uint32_t level = 0,
  244. // const std::shared_ptr<ParmType> params = nullptr, usint slots = 0) const;
  245. // Ciphertext<Element> EvalRotate(ConstCiphertext<Element> ciphertext, int32_t index) const;
  246. // // const?
  247. // Ciphertext<DCRTPoly> EvalFastRotationPrecompute(ConstCiphertext<DCRTPoly> ciphertext);
  248. // // const?
  249. // Ciphertext<DCRTPoly> EvalFastRotation(ConstCiphertext<DCRTPoly> ciphertext, const usint index, const usint m,ConstCiphertext<DCRTPoly> digits);
  250. // // const?
  251. // Ciphertext<DCRTPoly> EvalFastRotationExt(ConstCiphertext<DCRTPoly> ciphertext, const usint index, ConstCiphertext<DCRTPoly> digits, bool addFirst);
  252. // void EvalAtIndexKeyGen(const PrivateKey<Element> privateKey, const std::vector<int32_t>& indexList,
  253. // const PublicKey<Element> publicKey = nullptr);
  254. // Ciphertext<Element> EvalAtIndex(ConstCiphertext<Element> ciphertext, int32_t index) const;
  255. // Ciphertext<Element> Encrypt(const PublicKey<Element> publicKey, Plaintext plaintext) const;
  256. // DecryptResult Decrypt(ConstCiphertext<Element> ciphertext, const PrivateKey<Element> privateKey,
  257. // Plaintext* plaintext);
  258. // inline DecryptResult Decrypt(const PrivateKey<Element> privateKey, ConstCiphertext<Element> ciphertext,
  259. // Plaintext* plaintext);
  260. // EvalKey<Element> KeySwitchGen(const PrivateKey<Element> oldPrivateKey,
  261. // const PrivateKey<Element> newPrivateKey) const;
  262. // Ciphertext<Element> EvalAdd(ConstCiphertext<Element> ciphertext, double constant) const;
  263. // Ciphertext<Element> EvalAdd(ConstCiphertext<Element> ciphertext1, ConstCiphertext<Element> ciphertext2) const;
  264. // Ciphertext<Element> EvalAdd(ConstCiphertext<Element> ciphertext, ConstPlaintext plaintext) const;
  265. // void EvalAddInPlace(Ciphertext<Element>& ciphertext, ConstPlaintext plaintext) const;
  266. // void EvalAddInPlace(ConstPlaintext plaintext, Ciphertext<Element>& ciphertext) const;
  267. // void EvalAddInPlace(Ciphertext<Element>& ciphertext1, ConstCiphertext<Element> ciphertext2) const;
  268. // Ciphertext<Element> EvalAddMutable(Ciphertext<Element>& ciphertext1, Ciphertext<Element>& ciphertext2) const;
  269. // Ciphertext<Element> EvalAddMutable(Ciphertext<Element>& ciphertext, Plaintext plaintext) const;
  270. // Ciphertext<Element> EvalAddMutable(Plaintext plaintext, Ciphertext<Element>& ciphertext) const;
  271. // void EvalAddMutableInPlace(Ciphertext<Element>& ciphertext1, Ciphertext<Element>& ciphertext2) const;
  272. // Ciphertext<Element> EvalSub(ConstCiphertext<Element> ciphertext1, ConstCiphertext<Element> ciphertext2) const;
  273. // Ciphertext<Element> EvalSub(ConstCiphertext<Element> ciphertext, ConstPlaintext plaintext) const;
  274. // Ciphertext<Element> EvalSub(ConstPlaintext plaintext, ConstCiphertext<Element> ciphertext) const;
  275. // Ciphertext<Element> EvalSub(ConstCiphertext<Element> ciphertext, double constant) const;
  276. // Ciphertext<Element> EvalSub(double constant, ConstCiphertext<Element> ciphertext) const;
  277. // void EvalSubInPlace(Ciphertext<Element>& ciphertext, double constant) const;
  278. // void EvalSubInPlace(double constant, Ciphertext<Element>& ciphertext) const;
  279. // void EvalSubInPlace(Ciphertext<Element>& ciphertext1, ConstCiphertext<Element> ciphertext2) const;
  280. // Ciphertext<Element> EvalSubMutable(Ciphertext<Element>& ciphertext1, Ciphertext<Element>& ciphertext2) const;
  281. // Ciphertext<Element> EvalSubMutable(Ciphertext<Element>& ciphertext, Plaintext plaintext) const;
  282. // Ciphertext<Element> EvalSubMutable(Plaintext plaintext, Ciphertext<Element>& ciphertext) const;
  283. // void EvalSubMutableInPlace(Ciphertext<Element>& ciphertext1, Ciphertext<Element>& ciphertext2) const;
  284. };
  285. #endif // OPENFHE_BINDINGS_H