cryptocontext_wrapper.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //==================================================================================
  2. // BSD 2-Clause License
  3. //
  4. // Copyright (c) 2014-2025, NJIT, Duality Technologies Inc. and other contributors
  5. //
  6. // All rights reserved.
  7. //
  8. // Author TPOC: contact@openfhe.org
  9. //
  10. // Redistribution and use in source and binary forms, with or without
  11. // modification, are permitted provided that the following conditions are met:
  12. //
  13. // 1. Redistributions of source code must retain the above copyright notice, this
  14. // list of conditions and the following disclaimer.
  15. //
  16. // 2. Redistributions in binary form must reproduce the above copyright notice,
  17. // this list of conditions and the following disclaimer in the documentation
  18. // and/or other materials provided with the distribution.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. //==================================================================================
  31. #include "cryptocontext_wrapper.h"
  32. Ciphertext<DCRTPoly> EvalFastRotationPrecomputeWrapper(CryptoContext<DCRTPoly> &self,ConstCiphertext<DCRTPoly> ciphertext) {
  33. std::shared_ptr<std::vector<DCRTPoly>> precomp = self->EvalFastRotationPrecompute(ciphertext);
  34. std::vector<DCRTPoly> elements = *(precomp.get());
  35. CiphertextImpl<DCRTPoly> cipherdigits = CiphertextImpl<DCRTPoly>(self);
  36. std::shared_ptr<CiphertextImpl<DCRTPoly>> cipherdigitsPtr = std::make_shared<CiphertextImpl<DCRTPoly>>(cipherdigits);
  37. cipherdigitsPtr->SetElements(elements);
  38. return cipherdigitsPtr;
  39. }
  40. Ciphertext<DCRTPoly> EvalFastRotationWrapper(CryptoContext<DCRTPoly>& self,ConstCiphertext<DCRTPoly> ciphertext, const usint index, const usint m,ConstCiphertext<DCRTPoly> digits) {
  41. std::vector<DCRTPoly> digitsElements = digits->GetElements();
  42. std::shared_ptr<std::vector<DCRTPoly>> digitsElementsPtr = std::make_shared<std::vector<DCRTPoly>>(digitsElements);
  43. return self->EvalFastRotation(ciphertext, index, m, digitsElementsPtr);
  44. }
  45. Ciphertext<DCRTPoly> EvalFastRotationExtWrapper(CryptoContext<DCRTPoly>& self,ConstCiphertext<DCRTPoly> ciphertext, const usint index, ConstCiphertext<DCRTPoly> digits, bool addFirst) {
  46. std::vector<DCRTPoly> digitsElements = digits->GetElements();
  47. std::shared_ptr<std::vector<DCRTPoly>> digitsElementsPtr = std::make_shared<std::vector<DCRTPoly>>(digitsElements);
  48. return self->EvalFastRotationExt(ciphertext, index, digitsElementsPtr, addFirst);
  49. }
  50. Plaintext DecryptWrapper(CryptoContext<DCRTPoly>& self,ConstCiphertext<DCRTPoly> ciphertext,const PrivateKey<DCRTPoly> privateKey){
  51. Plaintext plaintextDecResult;
  52. self->Decrypt(privateKey, ciphertext,&plaintextDecResult);
  53. return plaintextDecResult;
  54. }
  55. Plaintext DecryptWrapper(CryptoContext<DCRTPoly>& self,const PrivateKey<DCRTPoly> privateKey,ConstCiphertext<DCRTPoly> ciphertext){
  56. Plaintext plaintextDecResult;
  57. self->Decrypt(privateKey, ciphertext,&plaintextDecResult);
  58. return plaintextDecResult;
  59. }
  60. Plaintext MultipartyDecryptFusionWrapper(CryptoContext<DCRTPoly>& self,const std::vector<Ciphertext<DCRTPoly>>& partialCiphertextVec){
  61. Plaintext plaintextDecResult;
  62. self->MultipartyDecryptFusion(partialCiphertextVec,&plaintextDecResult);
  63. return plaintextDecResult;
  64. }
  65. const std::shared_ptr<std::map<usint, EvalKey<DCRTPoly>>> GetEvalSumKeyMapWrapper(CryptoContext<DCRTPoly>& self,const std::string &id){
  66. return std::make_shared<std::map<usint, EvalKey<DCRTPoly>>>(CryptoContextImpl<DCRTPoly>::GetEvalSumKeyMap(id));;
  67. }
  68. const PlaintextModulus GetPlaintextModulusWrapper(CryptoContext<DCRTPoly>& self){
  69. return self->GetCryptoParameters()->GetPlaintextModulus();
  70. }
  71. const double GetModulusWrapper(CryptoContext<DCRTPoly>& self){
  72. return self->GetCryptoParameters()->GetElementParams()->GetModulus().ConvertToDouble();
  73. }
  74. void RemoveElementWrapper(Ciphertext<DCRTPoly> &self, usint index){
  75. self->GetElements().erase(self->GetElements().begin()+index);
  76. }
  77. const usint GetDigitSizeWrapper(CryptoContext<DCRTPoly>& self){
  78. return self->GetCryptoParameters()->GetDigitSize();
  79. }
  80. const double GetScalingFactorRealWrapper(CryptoContext<DCRTPoly>& self, uint32_t l){
  81. if(self->getSchemeId()==SCHEME::CKKSRNS_SCHEME){
  82. const auto cryptoParams = std::dynamic_pointer_cast<CryptoParametersCKKSRNS>(self->GetCryptoParameters());
  83. double scFactor = cryptoParams->GetScalingFactorReal(l);
  84. return scFactor;
  85. }
  86. else if(self->getSchemeId()==SCHEME::BFVRNS_SCHEME){
  87. const auto cryptoParams = std::dynamic_pointer_cast<CryptoParametersBFVRNS>(self->GetCryptoParameters());
  88. double scFactor = cryptoParams->GetScalingFactorReal(l);
  89. return scFactor;
  90. }
  91. else if(self->getSchemeId()==SCHEME::BGVRNS_SCHEME){
  92. const auto cryptoParams = std::dynamic_pointer_cast<CryptoParametersBGVRNS>(self->GetCryptoParameters());
  93. double scFactor = cryptoParams->GetScalingFactorReal(l);
  94. return scFactor;
  95. }
  96. else{
  97. OPENFHE_THROW("Invalid scheme");
  98. return 0;
  99. }
  100. }
  101. const uint64_t GetModulusCKKSWrapper(CryptoContext<DCRTPoly> &self)
  102. {
  103. const auto cryptoParams = std::dynamic_pointer_cast<CryptoParametersCKKSRNS>(self->GetCryptoParameters());
  104. ILDCRTParams<DCRTPoly::Integer> elementParams = *(cryptoParams->GetElementParams());
  105. auto paramsQ = elementParams.GetParams();
  106. uint64_t modulus_CKKS_from = paramsQ[0]->GetModulus().ConvertToInt<uint64_t>();
  107. return modulus_CKKS_from;
  108. }
  109. const ScalingTechnique GetScalingTechniqueWrapper(CryptoContext<DCRTPoly> & self){
  110. if(self->getSchemeId()==SCHEME::CKKSRNS_SCHEME){
  111. const auto cryptoParams = std::dynamic_pointer_cast<CryptoParametersCKKSRNS>(self->GetCryptoParameters());
  112. return cryptoParams->GetScalingTechnique();
  113. }
  114. else if(self->getSchemeId()==SCHEME::BFVRNS_SCHEME){
  115. const auto cryptoParams = std::dynamic_pointer_cast<CryptoParametersBFVRNS>(self->GetCryptoParameters());
  116. return cryptoParams->GetScalingTechnique();
  117. }
  118. else if(self->getSchemeId()==SCHEME::BGVRNS_SCHEME){
  119. const auto cryptoParams = std::dynamic_pointer_cast<CryptoParametersBGVRNS>(self->GetCryptoParameters());
  120. return cryptoParams->GetScalingTechnique();
  121. }
  122. else{
  123. OPENFHE_THROW("Invalid scheme");
  124. }
  125. }
  126. void ClearEvalMultKeysWrapper() {
  127. CryptoContextImpl<DCRTPoly>::ClearEvalMultKeys();
  128. }