PALISADE Lattice Crypto Library  1.11.9
A lattice crypto library for software engineers by software engineers.
ckks.h
1 // @file ckks.h -- Operations for the CKKS cryptoscheme.
2 // @author TPOC: contact@palisade-crypto.org
3 //
4 // @copyright Copyright (c) 2019, Duality Technologies Inc.
5 // All rights reserved.
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution. THIS SOFTWARE IS
13 // PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
14 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
16 // EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
17 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 
24 /*
25  * This code implements the CKKS homomorphic encryption scheme.
26  */
27 
28 #ifndef LBCRYPTO_CRYPTO_CKKS_H
29 #define LBCRYPTO_CRYPTO_CKKS_H
30 
31 #include <map>
32 #include <memory>
33 #include <string>
34 #include <utility>
35 #include <vector>
36 
37 #include "math/dftransfrm.h"
38 #include "palisade.h"
39 #include "utils/caller_info.h"
40 
45 namespace lbcrypto {
46 
52 template <class Element>
54  using ParmType = typename Element::Params;
55  using IntType = typename Element::Integer;
56  using DugType = typename Element::DugType;
57  using DggType = typename Element::DggType;
58  using TugType = typename Element::TugType;
59 
60  public:
65  : LPCryptoParametersRLWE<Element>(),
66  m_ksTechnique(BV),
67  m_rsTechnique(APPROXRESCALE),
68  m_numPartQ(0),
69  m_numPerPartQ(0),
70  m_approxSF(0) {}
71 
78  : LPCryptoParametersRLWE<Element>(rhs),
79  m_ksTechnique(BV),
80  m_rsTechnique(APPROXRESCALE),
81  m_numPartQ(0),
82  m_numPerPartQ(0),
83  m_approxSF(0) {}
84 
109  LPCryptoParametersCKKS(shared_ptr<ParmType> params,
110  const PlaintextModulus &plaintextModulus,
111  float distributionParameter, float assuranceMeasure,
112  float securityLevel, usint relinWindow, MODE mode,
113  int depth = 1, int maxDepth = 2,
114  KeySwitchTechnique ksTech = BV,
115  RescalingTechnique rsTech = APPROXRESCALE)
116  : LPCryptoParametersRLWE<Element>(
117  params,
118  EncodingParams(
119  std::make_shared<EncodingParamsImpl>(plaintextModulus)),
120  distributionParameter, assuranceMeasure, securityLevel, relinWindow,
121  depth, maxDepth, mode) {
122  m_ksTechnique = ksTech;
123  m_rsTechnique = rsTech;
124  m_numPartQ = 0;
125  m_numPerPartQ = 0;
126  m_approxSF = 0;
127  }
128 
145  LPCryptoParametersCKKS(shared_ptr<ParmType> params,
146  EncodingParams encodingParams,
147  float distributionParameter, float assuranceMeasure,
148  float securityLevel, usint relinWindow, MODE mode,
149  int depth = 1, int maxDepth = 2,
150  KeySwitchTechnique ksTech = BV,
151  RescalingTechnique rsTech = APPROXRESCALE)
152  : LPCryptoParametersRLWE<Element>(
153  params, encodingParams, distributionParameter, assuranceMeasure,
154  securityLevel, relinWindow, depth, maxDepth, mode) {
155  m_ksTechnique = ksTech;
156  m_rsTechnique = rsTech;
157  m_numPartQ = 0;
158  m_numPerPartQ = 0;
159  m_approxSF = 0;
160  }
161 
166 
167  template <class Archive>
168  void save(Archive &ar, std::uint32_t const version) const {
169  ar(cereal::base_class<LPCryptoParametersRLWE<Element>>(this));
170  ar(cereal::make_nvp("ks", m_ksTechnique));
171  ar(cereal::make_nvp("rs", m_rsTechnique));
172  ar(cereal::make_nvp("dnum", m_numPartQ));
173  }
174 
175  template <class Archive>
176  void load(Archive &ar, std::uint32_t const version) {
177  if (version > SerializedVersion()) {
178  PALISADE_THROW(deserialize_error,
179  "serialized object version " + std::to_string(version) +
180  " is from a later version of the library");
181  }
182  ar(cereal::base_class<LPCryptoParametersRLWE<Element>>(this));
183  ar(cereal::make_nvp("ks", m_ksTechnique));
184  ar(cereal::make_nvp("rs", m_rsTechnique));
185  ar(cereal::make_nvp("dnum", m_numPartQ));
186 
187  if (SERIALIZE_PRECOMPUTE) {
188  this->PrecomputeCRTTables(m_ksTechnique, m_rsTechnique, m_numPartQ);
189  }
190  }
191 
192  std::string SerializedObjectName() const { return "CKKSSchemeParameters"; }
193  static uint32_t SerializedVersion() { return 1; }
194 
203  bool PrecomputeCRTTables(KeySwitchTechnique ksTech, RescalingTechnique rsTech,
204  uint32_t numLargeDigits = 0);
205 
211  bool operator==(const LPCryptoParameters<Element> &rhs) const {
212  const auto *el =
213  dynamic_cast<const LPCryptoParametersCKKS<Element> *>(&rhs);
214 
215  if (el == nullptr) return false;
216 
218  m_rsTechnique == el->GetRescalingTechnique() &&
219  m_ksTechnique == el->GetKeySwitchTechnique() &&
220  m_numPartQ == el->GetNumPartQ();
221  }
222 
223  void PrintParameters(std::ostream &os) const {
225  }
226 
233  const std::vector<NativeInteger> &GetQlQlInvModqlDivqlModq(size_t i) const {
234  return m_QlQlInvModqlDivqlModq[i];
235  }
236 
243  const std::vector<NativeInteger> &GetQlQlInvModqlDivqlModqPrecon(
244  size_t i) const {
245  return m_QlQlInvModqlDivqlModqPrecon[i];
246  }
247 
253  const std::vector<NativeInteger> &GetqInvModq(size_t i) const {
254  return m_qInvModq[i];
255  }
256 
262  const std::vector<NativeInteger> &GetqInvModqPrecon(size_t i) const {
263  return m_qInvModqPrecon[i];
264  }
265 
272  const shared_ptr<ILDCRTParams<BigInteger>> GetParamsP() const {
273  return m_paramsP;
274  }
275 
282  const BigInteger &GetAuxModulus() const { return m_modulusP; }
283 
291  const shared_ptr<ILDCRTParams<BigInteger>> GetParamsQP() const {
292  return m_paramsQP;
293  }
294 
304  const vector<NativeInteger> &GetPInvModq() const { return m_PInvModq; }
305 
312  const vector<NativeInteger> &GetPInvModqPrecon() const {
313  return m_PInvModqPrecon;
314  }
315 
325  const vector<NativeInteger> &GetPHatInvModp() const { return m_PHatInvModp; }
326 
332  const vector<NativeInteger> &GetPHatInvModpPrecon() const {
333  return m_PHatInvModpPrecon;
334  }
335 
345  const vector<NativeInteger> &GetQlHatInvModq(uint32_t l) const {
346  return m_LvlQHatInvModq[l];
347  }
348 
354  const vector<NativeInteger> &GetQlHatInvModqPrecon(uint32_t l) const {
355  return m_LvlQHatInvModqPrecon[l];
356  }
357 
367  const vector<vector<NativeInteger>> &GetPHatModq() const {
368  return m_PHatModq;
369  }
370 
380  const vector<vector<NativeInteger>> &GetQlHatModp(uint32_t l) const {
381  return m_LvlQHatModp[l];
382  }
383 
393  const vector<NativeInteger> &GetPModq() const { return m_PModq; }
394 
400  const vector<DoubleNativeInt> &GetModqBarrettMu() const {
401  return m_modqBarrettMu;
402  }
403 
409  const vector<DoubleNativeInt> &GetModpBarrettMu() const {
410  return m_modpBarrettMu;
411  }
412 
418  enum KeySwitchTechnique GetKeySwitchTechnique() const {
419  return m_ksTechnique;
420  }
421 
427  enum RescalingTechnique GetRescalingTechnique() const {
428  return m_rsTechnique;
429  }
430 
440  double GetScalingFactorOfLevel(uint32_t l = 0) const {
441  if (m_rsTechnique == EXACTRESCALE) {
442  if (l >= m_scalingFactors.size()) {
443  PALISADE_THROW(
444  math_error,
445  "LPCryptoParametersCKKS::GetScalingFactorOfLevel - Cannot "
446  "return scaling factor of level " +
447  std::to_string(l) + ". Current settings have up to " +
448  std::to_string(m_scalingFactors.size()) +
449  " levels, starting from 0.");
450  }
451 
452  return m_scalingFactors[l];
453  }
454 
455  return m_approxSF;
456  }
457 
467  double GetModReduceFactor(uint32_t l = 0) const {
468  if (m_rsTechnique == EXACTRESCALE) {
469  return m_dmoduliQ[l];
470  }
471 
472  return m_approxSF;
473  }
474 
481  const vector<vector<NativeInteger>> &GetPartQHatModq() const {
482  return m_PartQHatModq;
483  }
484 
492  const shared_ptr<ILDCRTParams<BigInteger>> &GetParamsPartQ(
493  uint32_t part) const {
494  return m_paramsPartQ[part];
495  }
496 
497  /*
498  * Method that returns the element parameters corresponding to the
499  * complementary basis of a single digit j, i.e., the basis consisting of
500  * all other digits plus the special primes. Note that numTowers should be
501  * up to l (where l is the number of towers).
502  *
503  * @param numTowers is the total number of towers there are in the
504  * ciphertext.
505  * @param digit is the index of the digit we want to get the complementary
506  * partition from.
507  * @return the partitions.
508  */
509  const shared_ptr<ILDCRTParams<BigInteger>> &GetParamsComplPartQ(
510  uint32_t numTowers, uint32_t digit) const {
511  return m_paramsComplPartQ[numTowers][digit];
512  }
513 
514  /*
515  * Method that returns the number of partitions.
516  *
517  * @return the number of partitions.
518  */
519  uint32_t GetNumberOfQPartitions() const { return m_paramsPartQ.size(); }
520 
527  const vector<NativeInteger> &GetPartQHatInvModq(uint32_t part) const {
528  return m_PartQHatInvModq[part];
529  }
530 
536  uint32_t GetNumPartQ() const { return m_numPartQ; }
537 
545  uint32_t GetNumPerPartQ() const { return m_numPerPartQ; }
546 
553  const vector<NativeInteger> &GetPartQlHatInvModq(uint32_t part,
554  uint32_t sublvl) const {
555  if (part < m_LvlPartQHatInvModq.size() &&
556  sublvl < m_LvlPartQHatInvModq[part].size())
557  return m_LvlPartQHatInvModq[part][sublvl];
558 
559  PALISADE_THROW(math_error,
560  "LPCryptoParametersCKKS::GetPartitionQHatInvModQTable - "
561  "index out of bounds.");
562  }
563 
570  const vector<NativeInteger> &GetPartQlHatInvModqPrecon(
571  uint32_t part, uint32_t sublvl) const {
572  if (part < m_LvlPartQHatInvModqPrecon.size() &&
573  sublvl < m_LvlPartQHatInvModqPrecon[part].size())
574  return m_LvlPartQHatInvModqPrecon[part][sublvl];
575 
576  PALISADE_THROW(math_error,
577  "LPCryptoParametersCKKS::"
578  "GetPartitionQHatInvModQPreconTable - index "
579  "out of bounds.");
580  }
581 
588  const vector<vector<NativeInteger>> &GetPartQlHatModp(uint32_t lvl,
589  uint32_t part) const {
590  if (lvl < m_LvlPartQHatModp.size() && part < m_LvlPartQHatModp[lvl].size())
591  return m_LvlPartQHatModp[lvl][part];
592 
593  PALISADE_THROW(math_error,
594  "LPCryptoParametersCKKS::GetPartitionQHatModPTable - "
595  "index out of bounds.");
596  }
597 
604  const vector<DoubleNativeInt> &GetmodComplPartqBarrettMu(
605  uint32_t lvl, uint32_t part) const {
606  if (lvl < m_modComplPartqBarrettMu.size() &&
607  part < m_modComplPartqBarrettMu[lvl].size())
608  return m_modComplPartqBarrettMu[lvl][part];
609 
610  PALISADE_THROW(math_error,
611  "LPCryptoParametersCKKS::GetPartitionPrecon - index out "
612  "of bounds.");
613  }
614 
615  private:
616  // Stores the technique to use for key switching
617  enum KeySwitchTechnique m_ksTechnique;
618 
619  // Stores the technique to use for rescaling
620  enum RescalingTechnique m_rsTechnique;
621 
622  // Stores the partition size {PartQ} = {Q_1,...,Q_l}
623  // where each Q_i is the product of q_j
624  uint32_t m_numPartQ;
625 
626  // Stores the number of towers per Q_i
627  uint32_t m_numPerPartQ;
628 
629  // Stores the composite moduli Q_i
630  vector<BigInteger> m_moduliPartQ;
631 
632  // Stores the partition of the moduli that correspond to digit j
633  vector<shared_ptr<ILDCRTParams<BigInteger>>> m_paramsPartQ;
634 
635  // Stores the complementary partition of each digit, which is
636  // used in HYBRID key switching
637  vector<vector<shared_ptr<ILDCRTParams<BigInteger>>>> m_paramsComplPartQ;
638 
639  // Stores the Barrett multiplication precomputation
640  vector<vector<vector<DoubleNativeInt>>> m_modComplPartqBarrettMu;
641 
642  // Stores [Q/Q_j] for HYBRID
643  vector<BigInteger> m_PartQHat;
644 
645  // Stores [Q/Q_j]_{q_i} for HYBRID
646  vector<vector<NativeInteger>> m_PartQHatModq;
647 
648  // Stores [{Q/Q_j}^{-1}]_{q_i} for HYBRID
649  vector<vector<NativeInteger>> m_PartQHatInvModq;
650 
651  // Stores [{(Q_k)^(l)/q_i}^{-1}]_{q_i} for HYBRID
652  vector<vector<vector<NativeInteger>>> m_LvlPartQHatInvModq;
653 
654  // Stores NTL precomputations for [{(Q_k)^(l)/q_i}^{-1}]_{q_i}
655  vector<vector<vector<NativeInteger>>> m_LvlPartQHatInvModqPrecon;
656 
657  // Stores [QHat_i]_{p_j}
658  vector<vector<vector<vector<NativeInteger>>>> m_LvlPartQHatModp;
659 
660  // Q^(l) = \prod_{j=0}^{l-1}
661  // Stores [Q^(l)*[Q^(l)^{-1}]_{q_l}/q_l]_{q_i}
662  std::vector<std::vector<NativeInteger>> m_QlQlInvModqlDivqlModq;
663 
664  // Q^(l) = \prod_{j=0}^{l-1}
665  // Stores NTL precomputations for [Q^(l)*[Q^(l)^{-1}]_{q_l}/q_l]_{q_i}
666  std::vector<std::vector<NativeInteger>> m_QlQlInvModqlDivqlModqPrecon;
667 
668  // Stores [q_i]_{q_j}
669  std::vector<std::vector<NativeInteger>> m_qInvModq;
670 
671  // Stores NTL precomputations for [q_i]_{q_j}
672  std::vector<std::vector<NativeInteger>> m_qInvModqPrecon;
673 
674  // Params for Auxiliary CRT basis {P} = {p_1,...,p_k}
675  // used in GHS key switching
676  shared_ptr<ILDCRTParams<BigInteger>> m_paramsP;
677 
678  // Params for Extended CRT basis {QP} = {q_1...q_l,p_1,...,p_k}
679  // used in GHS key switching
680  shared_ptr<ILDCRTParams<BigInteger>> m_paramsQP;
681 
682  // Moduli product P (P=p1*p2*..pk) of the auxiliary CRT basis for GHS key
683  // switching
684  BigInteger m_modulusP;
685 
686  // Stores [P]_{q_i}, used in GHS key switching
687  vector<NativeInteger> m_PModq;
688 
689  // Stores [P^{-1}]_{q_i}, required for GHS key switching
690  vector<NativeInteger> m_PInvModq;
691 
692  // Stores NTL precomputations for [P^{-1}]_{q_i}
693  vector<NativeInteger> m_PInvModqPrecon;
694 
695  // Stores [(P/p_j)^{-1}]_{p_j}, required for GHS key switching
696  vector<NativeInteger> m_PHatInvModp;
697 
698  // Stores NTL precomputations for [(P/p_j)^{-1}]_{p_j}
699  vector<NativeInteger> m_PHatInvModpPrecon;
700 
701  // Stores [(Q/q_i)^{-1}]_{q_i}
702  // required for GHS key switching
703  vector<vector<NativeInteger>> m_LvlQHatInvModq;
704 
705  // Stores NTL precomputations for [(Q/q_i)^{-1}]_{q_i}
706  vector<vector<NativeInteger>> m_LvlQHatInvModqPrecon;
707 
708  // Stores [P/p_j]_{q_i}, required for GHS key switching
709  vector<vector<NativeInteger>> m_PHatModq;
710 
711  // Stores [Q^(l)/q_i]_{p_j}, required for GHS key switching
712  vector<vector<vector<NativeInteger>>> m_LvlQHatModp;
713 
714  // Stores the Barrett multiplication precomputation for p_j
715  vector<DoubleNativeInt> m_modpBarrettMu;
716 
717  // Stores the Barrett multiplication precomputation for q_i
718  vector<DoubleNativeInt> m_modqBarrettMu;
719 
720  // A vector holding the doubles that correspond to the exact
721  // scaling factor of each level, when EXACTRESCALE is used.
722  vector<double> m_scalingFactors;
723 
724  // Stores q_i as doubles
725  vector<double> m_dmoduliQ;
726 
727  // Stores 2^ptm where ptm - plaintext modulus
728  double m_approxSF;
729 };
730 
734 template <class Element>
736  : public LPParameterGenerationAlgorithm<Element> {
737  public:
742 
759  bool ParamsGen(shared_ptr<LPCryptoParameters<Element>> cryptoParams,
760  int32_t evalAddCount = 0, int32_t evalMultCount = 0,
761  int32_t keySwitchCount = 0, size_t dcrtBits = 0,
762  uint32_t n = 0) const {
763  std::string errMsg = "This ParamsGen method is not implemented for CKKS.";
764  PALISADE_THROW(not_implemented_error, errMsg);
765  }
766 
783  bool ParamsGen(shared_ptr<LPCryptoParameters<Element>> cryptoParams,
784  usint cyclOrder, usint numPrimes, usint scaleExp,
785  usint relinWindow, MODE mode, KeySwitchTechnique ksTech = BV,
786  usint firstModSize = 60, RescalingTechnique = APPROXRESCALE,
787  uint32_t numLargeDigits = 4) const;
788 
790 
791  template <class Archive>
792  void save(Archive &ar) const {
793  ar(cereal::base_class<LPParameterGenerationAlgorithm<Element>>(this));
794  }
795 
796  template <class Archive>
797  void load(Archive &ar) {
798  ar(cereal::base_class<LPParameterGenerationAlgorithm<Element>>(this));
799  }
800 
801  std::string SerializedObjectName() const { return "CKKSParamsGen"; }
802 };
803 
808 template <class Element>
809 class LPAlgorithmCKKS : public LPEncryptionAlgorithm<Element> {
810  using ParmType = typename Element::Params;
811  using IntType = typename Element::Integer;
812  using DugType = typename Element::DugType;
813  using DggType = typename Element::DggType;
814  using TugType = typename Element::TugType;
815 
816  public:
821 
831  Ciphertext<Element> Encrypt(const LPPublicKey<Element> publicKey,
832  Element plaintext) const;
833 
843  Ciphertext<Element> Encrypt(const LPPrivateKey<Element> privateKey,
844  Element plaintext) const;
845 
854  DecryptResult Decrypt(const LPPrivateKey<Element> privateKey,
855  ConstCiphertext<Element> ciphertext,
856  NativePoly *plaintext) const;
857 
866  DecryptResult Decrypt(const LPPrivateKey<Element> privateKey,
867  ConstCiphertext<Element> ciphertext,
868  Poly *plaintext) const;
869 
878  LPKeyPair<Element> KeyGen(CryptoContext<Element> cc, bool makeSparse = false);
879 
880  template <class Archive>
881  void save(Archive &ar) const {
882  ar(cereal::base_class<LPEncryptionAlgorithm<Element>>(this));
883  }
884 
885  template <class Archive>
886  void load(Archive &ar) {
887  ar(cereal::base_class<LPEncryptionAlgorithm<Element>>(this));
888  }
889 
890  std::string SerializedObjectName() const { return "CKKSEncryption"; }
891 };
892 
898 template <class Element>
899 class LPAlgorithmSHECKKS : public LPSHEAlgorithm<Element> {
900  using ParmType = typename Element::Params;
901  using IntType = typename Element::Integer;
902  using DugType = typename Element::DugType;
903  using DggType = typename Element::DggType;
904  using TugType = typename Element::TugType;
905 
906  public:
911 
915  virtual ~LPAlgorithmSHECKKS() {}
916 
925  void EvalAddInPlace(Ciphertext<Element> &ciphertext1,
926  ConstCiphertext<Element> ciphertext2) const override;
927 
936  Ciphertext<Element> EvalAddMutable(
937  Ciphertext<Element> &ciphertext1,
938  Ciphertext<Element> &ciphertext2) const override {
939  std::string errMsg =
940  "LPAlgorithmSHECKKS::EvalAddMutable is only supported for DCRTPoly.";
941  PALISADE_THROW(not_implemented_error, errMsg);
942  }
943 
951  Ciphertext<Element> EvalAdd(ConstCiphertext<Element> ciphertext,
952  ConstPlaintext plaintext) const override;
953 
962  Ciphertext<Element> EvalAddMutable(Ciphertext<Element> &ciphertext,
963  Plaintext plaintext) const override {
964  std::string errMsg =
965  "LPAlgorithmSHECKKS::EvalAddMutable is only supported for DCRTPoly.";
966  PALISADE_THROW(not_implemented_error, errMsg);
967  }
968 
976  Ciphertext<Element> EvalAdd(ConstCiphertext<Element> ciphertext,
977  double constant) const override;
978 
987  virtual Ciphertext<Element> EvalAddMutable(Ciphertext<Element> &ciphertext,
988  double constant) const {
989  std::string errMsg =
990  "LPAlgorithmSHECKKS::EvalAddMutable is only supported for DCRTPoly.";
991  PALISADE_THROW(not_implemented_error, errMsg);
992  }
993 
1003  Ciphertext<Element> EvalLinearWSum(vector<Ciphertext<Element>> ciphertexts,
1004  vector<double> constants) const override {
1005  std::string errMsg =
1006  "LPAlgorithmSHECKKS::EvalLinearWSum is only supported for DCRTPoly.";
1007  PALISADE_THROW(not_implemented_error, errMsg);
1008  }
1009 
1019  Ciphertext<Element> EvalLinearWSumMutable(
1020  vector<Ciphertext<Element>> ciphertexts,
1021  vector<double> constants) const override {
1022  std::string errMsg =
1023  "LPAlgorithmSHECKKS::EvalLinearWSumMutable is only supported for "
1024  "DCRTPoly.";
1025  PALISADE_THROW(not_implemented_error, errMsg);
1026  }
1027 
1035  Ciphertext<Element> EvalSub(
1036  ConstCiphertext<Element> ciphertext1,
1037  ConstCiphertext<Element> ciphertext2) const override;
1038 
1047  Ciphertext<Element> EvalSubMutable(
1048  Ciphertext<Element> &ciphertext1,
1049  Ciphertext<Element> &ciphertext2) const override {
1050  std::string errMsg =
1051  "LPAlgorithmSHECKKS::EvalSubMutable is only supported for DCRTPoly.";
1052  PALISADE_THROW(not_implemented_error, errMsg);
1053  }
1054 
1062  Ciphertext<Element> EvalSub(ConstCiphertext<Element> ciphertext1,
1063  ConstPlaintext plaintext) const override;
1064 
1073  Ciphertext<Element> EvalSubMutable(Ciphertext<Element> &ciphertext1,
1074  Plaintext plaintext) const override {
1075  std::string errMsg =
1076  "LPAlgorithmSHECKKS::EvalSubMutable is only supported for DCRTPoly.";
1077  PALISADE_THROW(not_implemented_error, errMsg);
1078  }
1079 
1087  Ciphertext<Element> EvalSub(ConstCiphertext<Element> ciphertext,
1088  double constant) const override;
1089 
1098  virtual Ciphertext<Element> EvalSubMutable(Ciphertext<Element> &ciphertext,
1099  double constant) const {
1100  std::string errMsg =
1101  "LPAlgorithmSHECKKS::EvalSubMutable is only supported for DCRTPoly.";
1102  PALISADE_THROW(not_implemented_error, errMsg);
1103  }
1104 
1115  Ciphertext<Element> EvalMult(
1116  ConstCiphertext<Element> ciphertext1,
1117  ConstCiphertext<Element> ciphertext2) const override;
1118 
1128  Ciphertext<Element> EvalMultMutable(
1129  Ciphertext<Element> &ciphertext1,
1130  Ciphertext<Element> &ciphertext2) const override {
1131  std::string errMsg =
1132  "LPAlgorithmSHECKKS::EvalMultMutable is only supported for DCRTPoly.";
1133  PALISADE_THROW(not_implemented_error, errMsg);
1134  }
1135 
1143  Ciphertext<Element> EvalMult(ConstCiphertext<Element> ciphertext,
1144  ConstPlaintext plaintext) const override;
1145 
1154  Ciphertext<Element> EvalMultMutable(Ciphertext<Element> &ciphertext,
1155  Plaintext plaintext) const override {
1156  std::string errMsg =
1157  "LPAlgorithmSHECKKS::EvalMultMutable is only supported for DCRTPoly.";
1158  PALISADE_THROW(not_implemented_error, errMsg);
1159  }
1160 
1168  Ciphertext<Element> EvalMult(ConstCiphertext<Element> ciphertext,
1169  double constant) const override;
1170 
1179  Ciphertext<Element> EvalMultMutable(Ciphertext<Element> &ciphertext,
1180  double constant) const override {
1181  std::string errMsg =
1182  "LPAlgorithmSHECKKS::EvalMultMutable is only supported for DCRTPoly.";
1183  PALISADE_THROW(not_implemented_error, errMsg);
1184  }
1185 
1198  Ciphertext<Element> EvalMult(ConstCiphertext<Element> ciphertext1,
1199  ConstCiphertext<Element> ciphertext2,
1200  const LPEvalKey<Element> ek) const override;
1201 
1213  Ciphertext<Element> EvalMultMutable(
1214  Ciphertext<Element> &ciphertext1, Ciphertext<Element> &ciphertext2,
1215  const LPEvalKey<Element> ek) const override;
1216 
1227  Ciphertext<Element> EvalMultAndRelinearize(
1228  ConstCiphertext<Element> ciphertext1,
1229  ConstCiphertext<Element> ciphertext2,
1230  const vector<LPEvalKey<Element>> &ek) const override {
1231  std::string errMsg =
1232  "LPAlgorithmSHECKKS::EvalMultAndRelinearize is not implemented for "
1233  "the "
1234  "CKKS Scheme.";
1235  PALISADE_THROW(not_implemented_error, errMsg);
1236  }
1237 
1238  /*
1239  * Relinearize a ciphertext.
1240  *
1241  * @param ciphertext input ciphertext to be relinearized
1242  * @param ek The evaluation key input.
1243  * @return the relinearized ciphertext
1244  */
1245  Ciphertext<Element> Relinearize(
1246  ConstCiphertext<Element> ciphertext,
1247  const vector<LPEvalKey<Element>> &ek) const override {
1248  std::string errMsg =
1249  "LPAlgorithmSHECKKS::Relinearize is not implemented for the non "
1250  "Double-CRT variant of the CKKS Scheme.";
1251  PALISADE_THROW(not_implemented_error, errMsg);
1252  }
1253 
1254  /*
1255  * Relinearize a ciphertext in place.
1256  *
1257  * @param ciphertext input ciphertext to be relinearized
1258  * @param ek The evaluation key input.
1259  * @return the relinearized ciphertext
1260  */
1262  Ciphertext<Element> &ciphertext,
1263  const vector<LPEvalKey<Element>> &ek) const override {
1264  std::string errMsg =
1265  "LPAlgorithmSHECKKS::RelinearizeInPlace is not implemented for the non "
1266  "Double-CRT variant of the CKKS Scheme.";
1267  PALISADE_THROW(not_implemented_error, errMsg);
1268  }
1269 
1276  Ciphertext<Element> EvalNegate(ConstCiphertext<Element> ct) const override;
1277 
1288  LPEvalKey<Element> KeySwitchHybridGen(
1289  const LPPrivateKey<Element> oldKey, const LPPrivateKey<Element> newKey,
1290  const LPEvalKey<DCRTPoly> ek = nullptr) const {
1291  std::string errMsg =
1292  "LPAlgorithmSHECKKS::KeySwitchHybridGen is not implemented for the "
1293  "non "
1294  "Double-CRT variant of the CKKS Scheme.";
1295  PALISADE_THROW(not_implemented_error, errMsg);
1296  }
1297 
1305  void KeySwitchHybridInPlace(const LPEvalKey<Element> keySwitchHint,
1306  Ciphertext<Element> &ciphertext) const {
1307  std::string errMsg =
1308  "LPAlgorithmSHECKKS::KeySwitchHybridInPlace is not implemented for the "
1309  "non Double-CRT variant of the CKKS Scheme.";
1310  PALISADE_THROW(not_implemented_error, errMsg);
1311  }
1312 
1325  LPEvalKey<Element> KeySwitchGHSGen(
1326  const LPPrivateKey<DCRTPoly> oldKey, const LPPrivateKey<DCRTPoly> newKey,
1327  const LPEvalKey<DCRTPoly> ek = nullptr) const;
1328 
1339  void KeySwitchGHSInPlace(const LPEvalKey<Element> keySwitchHint,
1340  Ciphertext<Element> &ciphertext) const;
1341 
1354  LPEvalKey<Element> KeySwitchBVGen(
1355  const LPPrivateKey<Element> oldKey, const LPPrivateKey<Element> newKey,
1356  const LPEvalKey<DCRTPoly> ek = nullptr) const;
1357 
1367  void KeySwitchBVInPlace(const LPEvalKey<Element> keySwitchHint,
1368  Ciphertext<Element> &ciphertext) const;
1369 
1377  LPEvalKey<Element> KeySwitchGen(
1378  const LPPrivateKey<Element> oldKey,
1379  const LPPrivateKey<Element> newKey) const override;
1380 
1388  void KeySwitchInPlace(const LPEvalKey<Element> keySwitchHint,
1389  Ciphertext<Element> &ciphertext) const override;
1390 
1398  LPEvalKey<Element> EvalMultKeyGen(
1399  const LPPrivateKey<Element> privateKey) const override;
1400 
1409  vector<LPEvalKey<Element>> EvalMultKeysGen(
1410  const LPPrivateKey<Element> privateKey) const override;
1411 
1421  Ciphertext<Element> EvalAutomorphism(
1422  ConstCiphertext<Element> ciphertext, usint i,
1423  const std::map<usint, LPEvalKey<Element>> &evalKeys,
1424  CALLER_INFO_ARGS_HDR) const override;
1425 
1434  shared_ptr<std::map<usint, LPEvalKey<Element>>> EvalAutomorphismKeyGen(
1435  const LPPrivateKey<Element> privateKey,
1436  const std::vector<usint> &indexList) const override;
1437 
1447  shared_ptr<std::map<usint, LPEvalKey<Element>>> EvalAutomorphismKeyGen(
1448  const LPPublicKey<Element> publicKey,
1449  const LPPrivateKey<Element> privateKey,
1450  const std::vector<usint> &indexList) const override {
1451  std::string errMsg =
1452  "LPAlgorithmSHECKKS::EvalAutomorphismKeyGen is not implemented for "
1453  "CKKS SHE Scheme.";
1454  PALISADE_THROW(not_implemented_error, errMsg);
1455  }
1456 
1464  shared_ptr<vector<Element>> EvalFastRotationPrecompute(
1465  ConstCiphertext<Element> ciphertext) const override;
1466 
1480  Ciphertext<Element> EvalFastRotation(
1481  ConstCiphertext<Element> ciphertext, const usint index, const usint m,
1482  const shared_ptr<vector<Element>> precomp) const override;
1483 
1549  Ciphertext<Element> AdjustLevelWithRescale(
1550  Ciphertext<Element> &ciphertext, uint32_t targetLevel) const override {
1551  std::string errMsg =
1552  "LPAlgorithmSHECKKS::AdjustLevelWithoutRescale is not implemented "
1553  "for "
1554  "the non Double-CRT variant of the CKKS Scheme.";
1555  PALISADE_THROW(not_implemented_error, errMsg);
1556  }
1557 
1575  Ciphertext<Element> AdjustLevelWithoutRescale(Ciphertext<Element> &ciphertext,
1576  uint32_t targetLevel) const {
1577  std::string errMsg =
1578  "LPAlgorithmSHECKKS::AdjustLevelWithoutRescale is not implemented "
1579  "for "
1580  "the non Double-CRT variant of the CKKS Scheme.";
1581  PALISADE_THROW(not_implemented_error, errMsg);
1582  }
1583 
1584  protected:
1593  virtual Ciphertext<DCRTPoly> EvalMultApprox(
1594  ConstCiphertext<DCRTPoly> ciphertext, ConstPlaintext plaintext) const {
1595  std::string errMsg =
1596  "LPAlgorithmSHECKKS::EvalMultApprox with plaintext is only supported "
1597  "for DCRTPoly.";
1598  PALISADE_THROW(not_implemented_error, errMsg);
1599  }
1600 
1611  Ciphertext<Element> EvalAddCorePlaintext(ConstCiphertext<Element> ciphertext,
1612  Element ptElement,
1613  usint ptDepth) const;
1614 
1625  Ciphertext<Element> EvalSubCorePlaintext(ConstCiphertext<Element> ciphertext,
1626  Element ptElement,
1627  usint ptDepth) const;
1628 
1637  std::pair<shared_ptr<ConstCiphertext<Element>>, Element> AutomaticLevelReduce(
1638  ConstCiphertext<Element> ciphertext, ConstPlaintext plaintext) const;
1639 
1668  shared_ptr<vector<Element>> EvalFastRotationPrecomputeBV(
1669  ConstCiphertext<Element> ciphertext) const;
1670 
1710  Ciphertext<Element> EvalFastRotationBV(
1711  ConstCiphertext<Element> ciphertext, const usint index, const usint m,
1712  const shared_ptr<vector<Element>> digits,
1713  LPEvalKey<DCRTPoly> evalKey) const;
1714 
1730  shared_ptr<vector<Element>> EvalFastRotationPrecomputeGHS(
1731  ConstCiphertext<Element> ciphertext) const;
1732 
1752  Ciphertext<Element> EvalFastRotationGHS(
1753  ConstCiphertext<Element> ciphertext, const usint index, const usint m,
1754  const shared_ptr<vector<Element>> expandedCiphertext,
1755  LPEvalKey<DCRTPoly> evalKey) const;
1756 
1770  shared_ptr<vector<Element>> EvalFastRotationPrecomputeHybrid(
1771  ConstCiphertext<Element> ciphertext) const;
1772 
1790  Ciphertext<Element> EvalFastRotationHybrid(
1791  ConstCiphertext<Element> ciphertext, const usint index, const usint m,
1792  const shared_ptr<vector<Element>> expandedCiphertext,
1793  LPEvalKey<DCRTPoly> evalKey) const;
1794 
1795  private:
1805  Ciphertext<Element> EvalAddCore(ConstCiphertext<Element> ciphertext1,
1806  ConstCiphertext<Element> ciphertext2) const;
1807 
1818  void EvalAddCoreInPlace(Ciphertext<Element> &ciphertext1,
1819  ConstCiphertext<Element> ciphertext2) const;
1820 
1829  virtual Ciphertext<Element> EvalAddApprox(
1830  ConstCiphertext<Element> ciphertext1,
1831  ConstCiphertext<Element> ciphertext2) const {
1832  std::string errMsg =
1833  "LPAlgorithmSHECKKS::EvalAddApprox is only supported for DCRTPoly.";
1834  PALISADE_THROW(not_implemented_error, errMsg);
1835  }
1836 
1845  virtual void EvalAddApproxInPlace(
1846  Ciphertext<Element> &ciphertext1,
1847  ConstCiphertext<Element> ciphertext2) const {
1848  std::string errMsg =
1849  "LPAlgorithmSHECKKS::EvalAddApproxInPlace is only supported for "
1850  "DCRTPoly.";
1851  PALISADE_THROW(not_implemented_error, errMsg);
1852  }
1853 
1863  Ciphertext<Element> EvalSubCore(ConstCiphertext<Element> ciphertext1,
1864  ConstCiphertext<Element> ciphertext2) const;
1865 
1874  Ciphertext<Element> EvalSubApprox(ConstCiphertext<Element> ciphertext1,
1875  ConstCiphertext<Element> ciphertext2) const;
1876 
1886  Ciphertext<Element> EvalMultCore(ConstCiphertext<Element> ciphertext1,
1887  ConstCiphertext<Element> ciphertext2) const;
1888 
1897  virtual Ciphertext<Element> EvalMultApprox(
1898  ConstCiphertext<Element> ciphertext1,
1899  ConstCiphertext<Element> ciphertext2) const {
1900  std::string errMsg =
1901  "LPAlgorithmSHECKKS::EvalMultApprox is only supported for DCRTPoly.";
1902  PALISADE_THROW(not_implemented_error, errMsg);
1903  }
1904 
1913  virtual Ciphertext<Element> EvalMultApprox(
1914  ConstCiphertext<Element> ciphertext, double constant) const;
1915 
1923  vector<shared_ptr<ConstCiphertext<Element>>> AutomaticLevelReduce(
1924  ConstCiphertext<Element> ciphertext1,
1925  ConstCiphertext<Element> ciphertext2) const;
1926 
1935  void AutomaticLevelReduceInPlace(Ciphertext<Element> &ciphertext1,
1936  ConstCiphertext<Element> ciphertext2) const;
1937 
1947  Ciphertext<DCRTPoly> EvalLinearWSumInternalMutable(
1948  vector<Ciphertext<DCRTPoly>> ciphertexts, vector<double> constants) const;
1949 
1957  std::vector<DCRTPoly::Integer> GetElementForEvalAddOrSub(
1958  ConstCiphertext<DCRTPoly> ciphertext, double constant) const;
1959 
1960  public:
1961  template <class Archive>
1962  void save(Archive &ar) const {
1963  ar(cereal::base_class<LPSHEAlgorithm<Element>>(this));
1964  }
1965 
1966  template <class Archive>
1967  void load(Archive &ar) {
1968  ar(cereal::base_class<LPSHEAlgorithm<Element>>(this));
1969  }
1970 
1971  std::string SerializedObjectName() const { return "CKKSSHE"; }
1972 };
1973 
1974 // LPAlgorithmSHECKKS' function template specializations for DCRTPoly
1975 template <>
1977  Ciphertext<DCRTPoly>& ciphertext, uint32_t targetLevel) const;
1978 template <>
1980  Ciphertext<DCRTPoly>& ciphertext, uint32_t targetLevel) const;
1981 template <>
1982 Ciphertext<DCRTPoly> LPAlgorithmSHECKKS<DCRTPoly>::EvalAddApprox(
1983  ConstCiphertext<DCRTPoly> ciphertext1, ConstCiphertext<DCRTPoly> ciphertext2) const;
1984 template <>
1986  Ciphertext<DCRTPoly>& ciphertext1, ConstCiphertext<DCRTPoly> ciphertext2) const;
1987 template <>
1989  Ciphertext<DCRTPoly>& ciphertext1, Ciphertext<DCRTPoly>& ciphertext2) const;
1990 template <>
1992  Ciphertext<DCRTPoly>& ciphertext, Plaintext plaintext) const;
1993 template <>
1995  vector<Ciphertext<DCRTPoly>> ciphertexts, vector<double> constants) const;
1996 template <>
1998  vector<Ciphertext<DCRTPoly>> ciphertexts, vector<double> constants) const;
1999 template <>
2001  vector<Ciphertext<DCRTPoly>> ciphertexts, vector<double> constants) const;
2002 template <>
2004  vector<Ciphertext<DCRTPoly>> ciphertexts, vector<double> constants) const;
2005 template <>
2007  ConstCiphertext<DCRTPoly> ciphertext1, ConstCiphertext<DCRTPoly> ciphertext2, const vector<LPEvalKey<DCRTPoly>>& ek) const;
2008 template <>
2010  ConstCiphertext<DCRTPoly> ciphertext1, ConstCiphertext<DCRTPoly> ciphertext2) const;
2011 template <>
2013  ConstCiphertext<DCRTPoly> ciphertext, ConstPlaintext plaintext) const;
2014 template <>
2016  ConstCiphertext<DCRTPoly> ciphertext, double constant) const;
2017 template <>
2019  Ciphertext<DCRTPoly>& ciphertext, double constant) const;
2020 template <>
2022  Ciphertext<DCRTPoly>& ciphertext1, Ciphertext<DCRTPoly>& ciphertext2) const;
2023 template <>
2025  Ciphertext<DCRTPoly>& ciphertext, Plaintext plaintext) const;
2026 template <>
2028  Ciphertext<DCRTPoly>& ciphertext1, Ciphertext<DCRTPoly>& ciphertext2) const;
2029 template <>
2031  Ciphertext<DCRTPoly>& ciphertext, Plaintext plaintext) const;
2032 template <>
2034  const LPPrivateKey<DCRTPoly> oldKey, const LPPrivateKey<DCRTPoly> newKey, const LPEvalKey<DCRTPoly> ekPrev) const;
2035 template <>
2037  const LPEvalKey<DCRTPoly> ek, Ciphertext<DCRTPoly>& ciphertext) const;
2038 template <>
2039 Ciphertext<DCRTPoly> LPAlgorithmSHECKKS<DCRTPoly>::Relinearize(
2040  ConstCiphertext<DCRTPoly> ciphertext, const vector<LPEvalKey<DCRTPoly>>& ek) const;
2041 template <>
2043  Ciphertext<DCRTPoly>& ciphertext, const vector<LPEvalKey<DCRTPoly>>& ek) const;
2044 
2070 template <class Element>
2071 class LPAlgorithmPRECKKS : public LPPREAlgorithm<Element> {
2072  using ParmType = typename Element::Params;
2073  using IntType = typename Element::Integer;
2074  using DugType = typename Element::DugType;
2075  using DggType = typename Element::DggType;
2076  using TugType = typename Element::TugType;
2077 
2078  public:
2083 
2094  LPEvalKey<Element> ReKeyGen(const LPPrivateKey<Element> newKey,
2095  const LPPrivateKey<Element> oldKey) const;
2096 
2123  LPEvalKey<Element> ReKeyGen(const LPPublicKey<Element> newKey,
2124  const LPPrivateKey<Element> oldKey) const;
2125 
2152  Ciphertext<Element> ReEncrypt(
2153  const LPEvalKey<Element> EK, ConstCiphertext<Element> ciphertext,
2154  const LPPublicKey<Element> publicKey = nullptr) const;
2155 
2156  private:
2170  LPEvalKey<Element> ReKeyGenBV(const LPPublicKey<Element> newKey,
2171  const LPPrivateKey<Element> oldKey) const;
2172 
2186  LPEvalKey<Element> ReKeyGenGHS(const LPPublicKey<Element> newKey,
2187  const LPPrivateKey<Element> oldKey) const;
2188 
2189  public:
2190  template <class Archive>
2191  void save(Archive &ar) const {
2192  ar(cereal::base_class<LPPREAlgorithm<Element>>(this));
2193  }
2194 
2195  template <class Archive>
2196  void load(Archive &ar) {
2197  ar(cereal::base_class<LPPREAlgorithm<Element>>(this));
2198  }
2199 
2200  std::string SerializedObjectName() const { return "CKKSPRE"; }
2201 };
2202 
2229 template <class Element>
2231  using ParmType = typename Element::Params;
2232  using IntType = typename Element::Integer;
2233  using DugType = typename Element::DugType;
2234  using DggType = typename Element::DggType;
2235  using TugType = typename Element::TugType;
2236 
2237  public:
2242 
2257  LPKeyPair<Element> MultipartyKeyGen(CryptoContext<Element> cc,
2258  const LPPublicKey<Element> pk1,
2259  bool makeSparse = false,
2260  bool fresh = false);
2261 
2273  LPKeyPair<Element> MultipartyKeyGen(
2274  CryptoContext<Element> cc,
2275  const vector<LPPrivateKey<Element>> &secretKeys, bool makeSparse = false);
2276 
2284  Ciphertext<Element> MultipartyDecryptMain(
2285  const LPPrivateKey<Element> privateKey,
2286  ConstCiphertext<Element> ciphertext) const;
2287 
2295  Ciphertext<Element> MultipartyDecryptLead(
2296  const LPPrivateKey<Element> privateKey,
2297  ConstCiphertext<Element> ciphertext) const;
2298 
2307  DecryptResult MultipartyDecryptFusion(
2308  const vector<Ciphertext<Element>> &ciphertextVec,
2309  NativePoly *plaintext) const;
2310 
2319  DecryptResult MultipartyDecryptFusion(
2320  const vector<Ciphertext<Element>> &ciphertextVec, Poly *plaintext) const;
2321 
2332  LPEvalKey<Element> MultiKeySwitchGen(
2333  const LPPrivateKey<Element> originalPrivateKey,
2334  const LPPrivateKey<Element> newPrivateKey,
2335  const LPEvalKey<Element> ek) const;
2336 
2347  shared_ptr<std::map<usint, LPEvalKey<Element>>> MultiEvalAutomorphismKeyGen(
2348  const LPPrivateKey<Element> privateKey,
2349  const shared_ptr<std::map<usint, LPEvalKey<Element>>> eAuto,
2350  const std::vector<usint> &indexList) const;
2351 
2361  shared_ptr<std::map<usint, LPEvalKey<Element>>> MultiEvalSumKeyGen(
2362  const LPPrivateKey<Element> privateKey,
2363  const shared_ptr<std::map<usint, LPEvalKey<Element>>> eSum) const;
2364 
2374  LPEvalKey<Element> MultiMultEvalKey(LPEvalKey<Element> evalKey,
2375  LPPrivateKey<Element> sk) const;
2376 
2377  template <class Archive>
2378  void save(Archive &ar) const {
2379  ar(cereal::base_class<LPMultipartyAlgorithm<Element>>(this));
2380  }
2381 
2382  template <class Archive>
2383  void load(Archive &ar) {
2384  ar(cereal::base_class<LPMultipartyAlgorithm<Element>>(this));
2385  }
2386 
2387  std::string SerializedObjectName() const { return "CKKSMultiparty"; }
2388 };
2389 
2403 template <class Element>
2405  using ParmType = typename Element::Params;
2406  using IntType = typename Element::Integer;
2407  using DugType = typename Element::DugType;
2408  using DggType = typename Element::DggType;
2409  using TugType = typename Element::TugType;
2410 
2411  public:
2416 
2417  virtual ~LPLeveledSHEAlgorithmCKKS() {}
2418 
2432  void ModReduceInPlace(Ciphertext<Element> &ciphertext,
2433  size_t levels = 1) const override;
2434 
2441  Ciphertext<Element> ModReduceInternal(ConstCiphertext<Element> ciphertext,
2442  size_t levels = 1) const override;
2443 
2444  void ModReduceInternalInPlace(Ciphertext<Element> &ciphertext,
2445  size_t levels = 1) const override;
2446 
2454  Ciphertext<Element> Compress(ConstCiphertext<Element> ciphertext,
2455  size_t towersLeft = 1) const override;
2456 
2469  Ciphertext<Element> ComposedEvalMult(
2470  ConstCiphertext<Element> cipherText1,
2471  ConstCiphertext<Element> cipherText2,
2472  const LPEvalKey<Element> quadKeySwitchHint) const override {
2473  std::string errMsg =
2474  "LPLeveledSHEAlgorithmCKKS::ComposedEvalMult is not currently "
2475  "implemented "
2476  "for "
2477  "the CKKS/CKKS Scheme.";
2478  PALISADE_THROW(not_implemented_error, errMsg);
2479  }
2480 
2492  Ciphertext<Element> LevelReduce(ConstCiphertext<Element> ciphertext,
2493  const LPEvalKey<Element> linearKeySwitchHint,
2494  size_t levels) const override;
2495 
2506  Ciphertext<Element> LevelReduceInternal(
2507  ConstCiphertext<Element> ciphertext,
2508  const LPEvalKey<Element> linearKeySwitchHint,
2509  size_t levels) const override;
2510 
2521  void LevelReduceInternalInPlace(Ciphertext<Element> &ciphertext,
2522  const LPEvalKey<Element> linearKeySwitchHint,
2523  size_t levels) const override;
2524 
2534  Ciphertext<Element> EvalPoly(
2535  ConstCiphertext<Element> cipherText,
2536  const std::vector<double> &coefficients) const override {
2537  std::string errMsg =
2538  "LPLeveledSHEAlgorithmCKKS::EvalPoly is only supported for DCRTPoly.";
2539  PALISADE_THROW(not_implemented_error, errMsg);
2540  }
2541 
2542  template <class Archive>
2543  void save(Archive &ar) const {
2544  ar(cereal::base_class<LPLeveledSHEAlgorithm<Element>>(this));
2545  }
2546 
2547  template <class Archive>
2548  void load(Archive &ar) {
2549  ar(cereal::base_class<LPLeveledSHEAlgorithm<Element>>(this));
2550  }
2551 
2552  std::string SerializedObjectName() const { return "CKKSLeveledSHE"; }
2553 };
2554 
2559 template <class Element>
2561  : public LPPublicKeyEncryptionScheme<Element> {
2562  public:
2564  this->m_algorithmParamsGen =
2565  std::make_shared<LPAlgorithmParamsGenCKKS<Element>>();
2566  }
2567 
2568  bool operator==(const LPPublicKeyEncryptionScheme<Element> &sch) const {
2569  return dynamic_cast<const LPPublicKeyEncryptionSchemeCKKS<Element> *>(
2570  &sch) != nullptr;
2571  }
2572 
2573  void Enable(PKESchemeFeature feature);
2574 
2575  template <class Archive>
2576  void save(Archive &ar, std::uint32_t const version) const {
2577  ar(cereal::base_class<LPPublicKeyEncryptionScheme<Element>>(this));
2578  }
2579 
2580  template <class Archive>
2581  void load(Archive &ar, std::uint32_t const version) {
2582  ar(cereal::base_class<LPPublicKeyEncryptionScheme<Element>>(this));
2583  }
2584 
2585  std::string SerializedObjectName() const { return "CKKSScheme"; }
2586 };
2587 
2588 } // namespace lbcrypto
2589 #endif
const vector< NativeInteger > & GetPInvModq() const
Definition: ckks.h:304
const vector< NativeInteger > & GetPartQlHatInvModqPrecon(uint32_t part, uint32_t sublvl) const
Definition: ckks.h:570
const vector< NativeInteger > & GetQlHatInvModqPrecon(uint32_t l) const
Definition: ckks.h:354
Ciphertext< Element > EvalMultMutable(Ciphertext< Element > &ciphertext, double constant) const override
Definition: ckks.h:1179
Ciphertext< Element > EvalMultMutable(Ciphertext< Element > &ciphertext1, Ciphertext< Element > &ciphertext2) const override
Definition: ckks.h:1128
const vector< DoubleNativeInt > & GetModqBarrettMu() const
Definition: ckks.h:400
Ciphertext< Element > EvalSubMutable(Ciphertext< Element > &ciphertext1, Plaintext plaintext) const override
Definition: ckks.h:1073
const vector< NativeInteger > & GetPModq() const
Definition: ckks.h:393
Abstract interface for parameter generation algorithm.
Definition: pubkeylp.h:899
PRE scheme based on CKKS. The basic scheme is described here:
Definition: ckks.h:2071
shared_ptr< std::map< usint, LPEvalKey< Element > > > EvalAutomorphismKeyGen(const LPPublicKey< Element > publicKey, const LPPrivateKey< Element > privateKey, const std::vector< usint > &indexList) const override
Definition: ckks.h:1447
The multiparty homomorphic encryption capability for the CKKS scheme. A version of this multiparty sc...
Definition: ckks.h:2230
Ciphertext< Element > EvalLinearWSumMutable(vector< Ciphertext< Element >> ciphertexts, vector< double > constants) const override
Definition: ckks.h:1019
double GetModReduceFactor(uint32_t l=0) const
Definition: ckks.h:467
uint32_t GetNumPartQ() const
Definition: ckks.h:536
Decryption result. This represents whether the decryption of a cipheretext was performed correctly...
Definition: pubkeylp.h:105
Ciphertext< Element > Relinearize(ConstCiphertext< Element > ciphertext, const vector< LPEvalKey< Element >> &ek) const override
Definition: ckks.h:1245
enum KeySwitchTechnique GetKeySwitchTechnique() const
Definition: ckks.h:418
const vector< vector< NativeInteger > > & GetPartQHatModq() const
Definition: ckks.h:481
Ciphertext< Element > AdjustLevelWithRescale(Ciphertext< Element > &ciphertext, uint32_t targetLevel) const override
Definition: ckks.h:1549
Ciphertext< Element > EvalLinearWSum(vector< Ciphertext< Element >> ciphertexts, vector< double > constants) const override
Definition: ckks.h:1003
const vector< NativeInteger > & GetQlHatInvModq(uint32_t l) const
Definition: ckks.h:345
Concrete feature class for Leveled SHECKKS operations. This class adds leveled (CKKS scheme) features...
Definition: ckks.h:2404
Ciphertext< Element > EvalPoly(ConstCiphertext< Element > cipherText, const std::vector< double > &coefficients) const override
Definition: ckks.h:2534
Template for crypto parameters.
Definition: rlwe.h:47
Definition: stl_allocator.h:124
All parameters for plaintext encodings into ciphertext space.
Definition: encodingparams.h:44
LPAlgorithmPRECKKS()
Definition: ckks.h:2082
bool operator==(const LPCryptoParameters< Element > &rhs) const
Definition: rlwe.h:284
Abstract interface class for LBC Multiparty algorithms based on threshold FHE. A version of this mult...
Definition: pubkeylp.h:1299
LPEvalKey< Element > KeySwitchHybridGen(const LPPrivateKey< Element > oldKey, const LPPrivateKey< Element > newKey, const LPEvalKey< DCRTPoly > ek=nullptr) const
Definition: ckks.h:1288
Ciphertext< Element > EvalMultAndRelinearize(ConstCiphertext< Element > ciphertext1, ConstCiphertext< Element > ciphertext2, const vector< LPEvalKey< Element >> &ek) const override
Definition: ckks.h:1227
Definition: exception.h:119
LPAlgorithmMultipartyCKKS()
Definition: ckks.h:2241
const vector< NativeInteger > & GetPInvModqPrecon() const
Definition: ckks.h:312
const vector< vector< NativeInteger > > & GetQlHatModp(uint32_t l) const
Definition: ckks.h:380
LPAlgorithmParamsGenCKKS()
Definition: ckks.h:741
virtual Ciphertext< Element > EvalSubMutable(Ciphertext< Element > &ciphertext, double constant) const
Definition: ckks.h:1098
Definition: exception.h:147
Definition: exception.h:113
const std::vector< NativeInteger > & GetqInvModq(size_t i) const
Definition: ckks.h:253
LPLeveledSHEAlgorithmCKKS()
Definition: ckks.h:2415
const vector< vector< NativeInteger > > & GetPartQlHatModp(uint32_t lvl, uint32_t part) const
Definition: ckks.h:588
Abstract interface for encryption algorithm.
Definition: pubkeylp.h:1016
Definition: ckks.h:809
virtual Ciphertext< DCRTPoly > EvalMultApprox(ConstCiphertext< DCRTPoly > ciphertext, ConstPlaintext plaintext) const
Definition: ckks.h:1593
const shared_ptr< ILDCRTParams< BigInteger > > GetParamsQP() const
Definition: ckks.h:291
const std::vector< NativeInteger > & GetQlQlInvModqlDivqlModq(size_t i) const
Definition: ckks.h:233
const BigInteger & GetAuxModulus() const
Definition: ckks.h:282
bool SERIALIZE_PRECOMPUTE
Definition: cryptocontext.cpp:30
Ciphertext< Element > AdjustLevelWithoutRescale(Ciphertext< Element > &ciphertext, uint32_t targetLevel) const
Definition: ckks.h:1575
LPCryptoParametersCKKS(const LPCryptoParametersCKKS &rhs)
Definition: ckks.h:77
Ciphertext< Element > EvalAddMutable(Ciphertext< Element > &ciphertext1, Ciphertext< Element > &ciphertext2) const override
Definition: ckks.h:936
void KeySwitchHybridInPlace(const LPEvalKey< Element > keySwitchHint, Ciphertext< Element > &ciphertext) const
Definition: ckks.h:1305
Parameter generation for CKKS.
Definition: ckks.h:735
Main class for big integers represented as an array of native (primitive) unsigned integers...
Definition: ubintfxd.h:219
Ciphertext< Element > EvalSubMutable(Ciphertext< Element > &ciphertext1, Ciphertext< Element > &ciphertext2) const override
Definition: ckks.h:1047
virtual Ciphertext< Element > EvalAddMutable(Ciphertext< Element > &ciphertext, double constant) const
Definition: ckks.h:987
Main public key encryption scheme for the CKKS/CKKS implementation.
Definition: ckks.h:2560
LPCryptoParametersCKKS()
Definition: ckks.h:64
const std::vector< NativeInteger > & GetQlQlInvModqlDivqlModqPrecon(size_t i) const
Definition: ckks.h:243
Abstract interface for Leveled SHE operations.
Definition: pubkeylp.h:1088
LPAlgorithmSHECKKS()
Definition: ckks.h:910
const vector< vector< NativeInteger > > & GetPHatModq() const
Definition: ckks.h:367
const vector< NativeInteger > & GetPartQlHatInvModq(uint32_t part, uint32_t sublvl) const
Definition: ckks.h:553
enum RescalingTechnique GetRescalingTechnique() const
Definition: ckks.h:427
Abstract interface class for LBC PRE algorithms.
Definition: pubkeylp.h:1239
Ciphertext< Element > EvalAddMutable(Ciphertext< Element > &ciphertext, Plaintext plaintext) const override
Definition: ckks.h:962
const vector< DoubleNativeInt > & GetmodComplPartqBarrettMu(uint32_t lvl, uint32_t part) const
Definition: ckks.h:604
LPCryptoParametersCKKS(shared_ptr< ParmType > params, const PlaintextModulus &plaintextModulus, float distributionParameter, float assuranceMeasure, float securityLevel, usint relinWindow, MODE mode, int depth=1, int maxDepth=2, KeySwitchTechnique ksTech=BV, RescalingTechnique rsTech=APPROXRESCALE)
Definition: ckks.h:109
const shared_ptr< ILDCRTParams< BigInteger > > & GetParamsPartQ(uint32_t part) const
Definition: ckks.h:492
const vector< NativeInteger > & GetPHatInvModp() const
Definition: ckks.h:325
Ciphertext< Element > ComposedEvalMult(ConstCiphertext< Element > cipherText1, ConstCiphertext< Element > cipherText2, const LPEvalKey< Element > quadKeySwitchHint) const override
Definition: ckks.h:2469
uint32_t GetNumPerPartQ() const
Definition: ckks.h:545
main implementation class to capture essential cryptoparameters of any LBC system ...
Definition: pubkeylp.h:73
LPCryptoParametersCKKS(shared_ptr< ParmType > params, EncodingParams encodingParams, float distributionParameter, float assuranceMeasure, float securityLevel, usint relinWindow, MODE mode, int depth=1, int maxDepth=2, KeySwitchTechnique ksTech=BV, RescalingTechnique rsTech=APPROXRESCALE)
Definition: ckks.h:145
const vector< DoubleNativeInt > & GetModpBarrettMu() const
Definition: ckks.h:409
bool ParamsGen(shared_ptr< LPCryptoParameters< Element >> cryptoParams, int32_t evalAddCount=0, int32_t evalMultCount=0, int32_t keySwitchCount=0, size_t dcrtBits=0, uint32_t n=0) const
Definition: ckks.h:759
virtual ~LPCryptoParametersCKKS()
Definition: ckks.h:165
const vector< NativeInteger > & GetPHatInvModpPrecon() const
Definition: ckks.h:332
const std::vector< NativeInteger > & GetqInvModqPrecon(size_t i) const
Definition: ckks.h:262
Definition: pubkeylp.h:879
void RelinearizeInPlace(Ciphertext< Element > &ciphertext, const vector< LPEvalKey< Element >> &ek) const override
Definition: ckks.h:1261
Crypto parameters class for RLWE-based schemes.
Definition: ckks.h:53
Abstract interface class for LBC SHE algorithms.
Definition: pubkeylp.h:1679
double GetScalingFactorOfLevel(uint32_t l=0) const
Definition: ckks.h:440
Definition: binfhecontext.h:36
Ciphertext< Element > EvalMultMutable(Ciphertext< Element > &ciphertext, Plaintext plaintext) const override
Definition: ckks.h:1154
const vector< NativeInteger > & GetPartQHatInvModq(uint32_t part) const
Definition: ckks.h:527
bool operator==(const LPCryptoParameters< Element > &rhs) const
Definition: ckks.h:211
Abstract interface for public key encryption schemes.
Definition: pubkeylp.h:3181
const shared_ptr< ILDCRTParams< BigInteger > > GetParamsP() const
Definition: ckks.h:272
bool PrecomputeCRTTables(KeySwitchTechnique ksTech, RescalingTechnique rsTech, uint32_t numLargeDigits=0)
virtual ~LPAlgorithmSHECKKS()
Definition: ckks.h:915
Definition: ckks.h:899
LPAlgorithmCKKS()
Definition: ckks.h:820