| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- //==================================================================================
- // BSD 2-Clause License
- //
- // Copyright (c) 2014-2025, NJIT, Duality Technologies Inc. and other contributors
- //
- // All rights reserved.
- //
- // Author TPOC: contact@openfhe.org
- //
- // Redistribution and use in source and binary forms, with or without
- // modification, are permitted provided that the following conditions are met:
- //
- // 1. Redistributions of source code must retain the above copyright notice, this
- // list of conditions and the following disclaimer.
- //
- // 2. Redistributions in binary form must reproduce the above copyright notice,
- // this list of conditions and the following disclaimer in the documentation
- // and/or other materials provided with the distribution.
- //
- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- //==================================================================================
- #include "cryptocontext_wrapper.h"
- Ciphertext<DCRTPoly> EvalFastRotationPrecomputeWrapper(CryptoContext<DCRTPoly> &self,ConstCiphertext<DCRTPoly> ciphertext) {
- std::shared_ptr<std::vector<DCRTPoly>> precomp = self->EvalFastRotationPrecompute(ciphertext);
- std::vector<DCRTPoly> elements = *(precomp.get());
- CiphertextImpl<DCRTPoly> cipherdigits = CiphertextImpl<DCRTPoly>(self);
- std::shared_ptr<CiphertextImpl<DCRTPoly>> cipherdigitsPtr = std::make_shared<CiphertextImpl<DCRTPoly>>(cipherdigits);
- cipherdigitsPtr->SetElements(elements);
- return cipherdigitsPtr;
- }
- Ciphertext<DCRTPoly> EvalFastRotationWrapper(CryptoContext<DCRTPoly>& self,ConstCiphertext<DCRTPoly> ciphertext, const usint index, const usint m,ConstCiphertext<DCRTPoly> digits) {
-
- std::vector<DCRTPoly> digitsElements = digits->GetElements();
- std::shared_ptr<std::vector<DCRTPoly>> digitsElementsPtr = std::make_shared<std::vector<DCRTPoly>>(digitsElements);
- return self->EvalFastRotation(ciphertext, index, m, digitsElementsPtr);
- }
- Ciphertext<DCRTPoly> EvalFastRotationExtWrapper(CryptoContext<DCRTPoly>& self,ConstCiphertext<DCRTPoly> ciphertext, const usint index, ConstCiphertext<DCRTPoly> digits, bool addFirst) {
- std::vector<DCRTPoly> digitsElements = digits->GetElements();
- std::shared_ptr<std::vector<DCRTPoly>> digitsElementsPtr = std::make_shared<std::vector<DCRTPoly>>(digitsElements);
- return self->EvalFastRotationExt(ciphertext, index, digitsElementsPtr, addFirst);
- }
- Plaintext DecryptWrapper(CryptoContext<DCRTPoly>& self,ConstCiphertext<DCRTPoly> ciphertext,const PrivateKey<DCRTPoly> privateKey){
- Plaintext plaintextDecResult;
- self->Decrypt(privateKey, ciphertext,&plaintextDecResult);
- return plaintextDecResult;
- }
- Plaintext DecryptWrapper(CryptoContext<DCRTPoly>& self,const PrivateKey<DCRTPoly> privateKey,ConstCiphertext<DCRTPoly> ciphertext){
- Plaintext plaintextDecResult;
- self->Decrypt(privateKey, ciphertext,&plaintextDecResult);
- return plaintextDecResult;
- }
- Plaintext MultipartyDecryptFusionWrapper(CryptoContext<DCRTPoly>& self,const std::vector<Ciphertext<DCRTPoly>>& partialCiphertextVec){
- Plaintext plaintextDecResult;
- self->MultipartyDecryptFusion(partialCiphertextVec,&plaintextDecResult);
- return plaintextDecResult;
- }
- const std::shared_ptr<std::map<usint, EvalKey<DCRTPoly>>> GetEvalSumKeyMapWrapper(CryptoContext<DCRTPoly>& self,const std::string &id){
- return std::make_shared<std::map<usint, EvalKey<DCRTPoly>>>(CryptoContextImpl<DCRTPoly>::GetEvalSumKeyMap(id));;
- }
- const PlaintextModulus GetPlaintextModulusWrapper(CryptoContext<DCRTPoly>& self){
- return self->GetCryptoParameters()->GetPlaintextModulus();
- }
- const double GetModulusWrapper(CryptoContext<DCRTPoly>& self){
- return self->GetCryptoParameters()->GetElementParams()->GetModulus().ConvertToDouble();
- }
- void RemoveElementWrapper(Ciphertext<DCRTPoly> &self, usint index){
- self->GetElements().erase(self->GetElements().begin()+index);
- }
- const usint GetDigitSizeWrapper(CryptoContext<DCRTPoly>& self){
- return self->GetCryptoParameters()->GetDigitSize();
- }
- const double GetScalingFactorRealWrapper(CryptoContext<DCRTPoly>& self, uint32_t l){
- if(self->getSchemeId()==SCHEME::CKKSRNS_SCHEME){
- const auto cryptoParams = std::dynamic_pointer_cast<CryptoParametersCKKSRNS>(self->GetCryptoParameters());
- double scFactor = cryptoParams->GetScalingFactorReal(l);
- return scFactor;
- }
- else if(self->getSchemeId()==SCHEME::BFVRNS_SCHEME){
- const auto cryptoParams = std::dynamic_pointer_cast<CryptoParametersBFVRNS>(self->GetCryptoParameters());
- double scFactor = cryptoParams->GetScalingFactorReal(l);
- return scFactor;
- }
- else if(self->getSchemeId()==SCHEME::BGVRNS_SCHEME){
- const auto cryptoParams = std::dynamic_pointer_cast<CryptoParametersBGVRNS>(self->GetCryptoParameters());
- double scFactor = cryptoParams->GetScalingFactorReal(l);
- return scFactor;
- }
- else{
- OPENFHE_THROW("Invalid scheme");
- return 0;
- }
- }
- const uint64_t GetModulusCKKSWrapper(CryptoContext<DCRTPoly> &self)
- {
- const auto cryptoParams = std::dynamic_pointer_cast<CryptoParametersCKKSRNS>(self->GetCryptoParameters());
- ILDCRTParams<DCRTPoly::Integer> elementParams = *(cryptoParams->GetElementParams());
- auto paramsQ = elementParams.GetParams();
- uint64_t modulus_CKKS_from = paramsQ[0]->GetModulus().ConvertToInt<uint64_t>();
- return modulus_CKKS_from;
- }
- const ScalingTechnique GetScalingTechniqueWrapper(CryptoContext<DCRTPoly> & self){
- if(self->getSchemeId()==SCHEME::CKKSRNS_SCHEME){
- const auto cryptoParams = std::dynamic_pointer_cast<CryptoParametersCKKSRNS>(self->GetCryptoParameters());
- return cryptoParams->GetScalingTechnique();
- }
- else if(self->getSchemeId()==SCHEME::BFVRNS_SCHEME){
- const auto cryptoParams = std::dynamic_pointer_cast<CryptoParametersBFVRNS>(self->GetCryptoParameters());
- return cryptoParams->GetScalingTechnique();
- }
- else if(self->getSchemeId()==SCHEME::BGVRNS_SCHEME){
- const auto cryptoParams = std::dynamic_pointer_cast<CryptoParametersBGVRNS>(self->GetCryptoParameters());
- return cryptoParams->GetScalingTechnique();
- }
- else{
- OPENFHE_THROW("Invalid scheme");
- }
- }
- void ClearEvalMultKeysWrapper() {
- CryptoContextImpl<DCRTPoly>::ClearEvalMultKeys();
- }
|