Ctxt.h
1 /* Copyright (C) 2012-2020 IBM Corp.
2  * This program is Licensed under the Apache License, Version 2.0
3  * (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software
7  * distributed under the License is distributed on an "AS IS" BASIS,
8  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9  * See the License for the specific language governing permissions and
10  * limitations under the License. See accompanying LICENSE file.
11  */
12 #ifndef HELIB_CTXT_H
13 #define HELIB_CTXT_H
57 #include <cfloat> // DBL_MAX
58 #include <helib/DoubleCRT.h>
59 #include <helib/EncodedPtxt.h>
60 #include <helib/apiAttributes.h>
61 
62 namespace helib {
63 struct CKKS;
64 struct BGV;
65 
66 template <typename Scheme>
67 class Ptxt;
68 
69 class KeySwitch;
70 class PubKey;
71 class SecKey;
72 
73 class PtxtArray;
74 
80 class SKHandle
81 {
82  long powerOfS, powerOfX, secretKeyID;
83 
84 public:
85  friend class Ctxt;
86 
87  SKHandle(long newPowerOfS = 0, long newPowerOfX = 1, long newSecretKeyID = 0)
88  {
89  powerOfS = newPowerOfS;
90  powerOfX = newPowerOfX;
91  secretKeyID = newSecretKeyID;
92  }
93 
95  void setBase(long newSecretKeyID = -1)
96  {
97  powerOfS = 1;
98  powerOfX = 1;
99  if (newSecretKeyID >= 0)
100  secretKeyID = newSecretKeyID;
101  }
102 
104  bool isBase(long ofKeyID = 0) const
105  {
106  // If ofKeyID<0, only check that this is base of some key,
107  // otherwise check that this is base of the given key
108  return powerOfS == 1 && powerOfX == 1 &&
109  (ofKeyID < 0 || secretKeyID == ofKeyID);
110  }
111 
113  void setOne(long newSecretKeyID = -1)
114  {
115  powerOfS = 0;
116  powerOfX = 1;
117  if (newSecretKeyID >= 0)
118  secretKeyID = newSecretKeyID;
119  }
120 
122  bool isOne() const { return powerOfS == 0; }
123 
124  bool operator==(const SKHandle& other) const
125  {
126  if (powerOfS == 0 && other.powerOfS == 0)
127  return true;
128  else
129  return powerOfS == other.powerOfS && powerOfX == other.powerOfX &&
130  secretKeyID == other.secretKeyID;
131  }
132 
133  bool operator!=(const SKHandle& other) const { return !(*this == other); }
134 
135  /* access functions */
136 
137  long getPowerOfS() const { return powerOfS; }
138  long getPowerOfX() const { return powerOfX; }
139  long getSecretKeyID() const { return secretKeyID; }
140 
153  bool mul(const SKHandle& a, const SKHandle& b)
154  {
155  // If either of the inputs is one, the output equals to the other input
156  if (a.isOne()) {
157  *this = b;
158  return (b.secretKeyID >= 0);
159  }
160  if (b.isOne()) {
161  *this = a;
162  return (a.secretKeyID >= 0);
163  }
164 
165  if (a.secretKeyID == -1 || b.secretKeyID == -1) {
166  secretKeyID = -1; // -1 will be used to indicate an "error state"
167  return false;
168  }
169 
170  if (a.secretKeyID != b.secretKeyID) {
171  secretKeyID = -1;
172  return false;
173  }
174 
175  if (a.powerOfX != b.powerOfX) {
176  secretKeyID = -1;
177  return false;
178  }
179 
180  secretKeyID = a.secretKeyID;
181  powerOfX = a.powerOfX;
182  powerOfS = a.powerOfS + b.powerOfS;
183  return true;
184  }
185 
186  friend std::istream& operator>>(std::istream& s, SKHandle& handle);
187 
188  // Raw IO
189 
194  void writeTo(std::ostream& str) const;
195 
202  static SKHandle readFrom(std::istream& str);
203 
209  void writeToJSON(std::ostream& str) const;
210 
216  JsonWrapper writeToJSON() const;
217 
224  static SKHandle readFromJSON(std::istream& str);
225 
232  static SKHandle readFromJSON(const JsonWrapper& j);
233 
239  void readJSON(std::istream& str);
240 
246  void readJSON(const JsonWrapper& j);
247 };
248 
249 inline std::ostream& operator<<(std::ostream& s, const SKHandle& handle)
250 {
251  handle.writeToJSON(s);
252  return s;
253 }
254 
262 class CtxtPart : public DoubleCRT
263 {
264 public:
266  SKHandle skHandle; // The secret-key polynomial corresponding to this part
267 
268  bool operator==(const CtxtPart& other) const;
269  bool operator!=(const CtxtPart& other) const { return !(*this == other); }
270 
271  // Constructors
272 
273  CtxtPart(const Context& _context, const IndexSet& s) : DoubleCRT(_context, s)
274  {
275  skHandle.setOne();
276  }
277 
278  CtxtPart(const Context& _context,
279  const IndexSet& s,
280  const SKHandle& otherHandle) :
281  DoubleCRT(_context, s), skHandle(otherHandle)
282  {}
283 
284  // Copy constructors from the base class
285  explicit CtxtPart(const DoubleCRT& other) : DoubleCRT(other)
286  {
287  skHandle.setOne();
288  }
289 
290  CtxtPart(const DoubleCRT& other, const SKHandle& otherHandle) :
291  DoubleCRT(other), skHandle(otherHandle)
292  {}
293 
298  void writeTo(std::ostream& str) const;
299 
306  static CtxtPart readFrom(std::istream& str, const Context& context);
307 
313  void read(std::istream& str);
314 
320  void writeToJSON(std::ostream& str) const;
321 
327  JsonWrapper writeToJSON() const;
328 
336  static CtxtPart readFromJSON(std::istream& str, const Context& context);
337 
345  static CtxtPart readFromJSON(const JsonWrapper& j, const Context& context);
346 
352  void readJSON(std::istream& str);
353 
359  void readJSON(const JsonWrapper& j);
360 };
361 
362 std::istream& operator>>(std::istream& s, CtxtPart& p);
363 std::ostream& operator<<(std::ostream& s, const CtxtPart& p);
364 
366 struct ZeroCtxtLike_type
367 {}; // used to select a constructor
368 
369 const ZeroCtxtLike_type ZeroCtxtLike = ZeroCtxtLike_type();
371 
395 class Ctxt
396 {
397  friend class PubKey;
398  friend class SecKey;
399  friend class BasicAutomorphPrecon;
400 
401  const Context& context; // points to the parameters of this FHE instance
402  const PubKey& pubKey; // points to the public encryption key;
403  std::vector<CtxtPart> parts; // the ciphertext parts
404  IndexSet primeSet; // the primes relative to which the parts are defined
405  long ptxtSpace; // plaintext space for this ciphertext (either p or p^r)
406 
407  // a high-probability bound on the noise magnitude
408  NTL::xdouble noiseBound;
409 
410  long intFactor; // an integer factor to divide by on decryption (for BGV)
411  NTL::xdouble ratFactor; // rational factor to divide on decryption (for CKKS)
412  NTL::xdouble ptxtMag; // bound on the plaintext size (for CKKS)
413 
414  // Create a tensor product of c1,c2. It is assumed that *this,c1,c2
415  // are defined relative to the same set of primes and plaintext space,
416  // and that *this DOES NOT point to the same object as c1,c2
417  void tensorProduct(const Ctxt& c1, const Ctxt& c2);
418 
419  // Add/subtract a ciphertext part to/from a ciphertext. These are private
420  // methods, they cannot update the noiseBound so they must be called
421  // from a procedure that will eventually update that estimate.
422  Ctxt& operator-=(const CtxtPart& part)
423  {
424  subPart(part);
425  return *this;
426  }
427 
428  Ctxt& operator+=(const CtxtPart& part)
429  {
430  addPart(part);
431  return *this;
432  }
433 
434  // NOTE: the matchPrimeSets business in the following routines
435  // is DEPRECATED. The requirement is that the prime set of part
436  // must contain the prime set of *this.
437  // If not, an exception is raised.
438  // Also, if matchPrimeSets == true and the prime set of *this does
439  // not contain the prime set of part, an exception is also raised.
440  // Also, note that the prime set of *this will not change.
441 
442  // Procedural versions with additional parameter
443  void subPart(const CtxtPart& part, bool matchPrimeSet = false)
444  {
445  subPart(part, part.skHandle, matchPrimeSet);
446  }
447 
448  void addPart(const CtxtPart& part, bool matchPrimeSet = false)
449  {
450  addPart(part, part.skHandle, matchPrimeSet);
451  }
452 
453  void subPart(const DoubleCRT& part,
454  const SKHandle& handle,
455  bool matchPrimeSet = false)
456  {
457  addPart(part, handle, matchPrimeSet, true);
458  }
459 
460  void addPart(const DoubleCRT& part,
461  const SKHandle& handle,
462  bool matchPrimeSet = false,
463  bool negative = false);
464 
465  // convenient to avoid dealing with the deprecated matchPrimeSet
466  // parameter
467  void addSignedPart(const DoubleCRT& part,
468  const SKHandle& handle,
469  bool negative = false)
470  {
471  addPart(part, handle, false, negative);
472  }
473 
474  // Takes as arguments a ciphertext-part p relative to s' and a key-switching
475  // matrix W = W[s'->s], use W to switch p relative to (1,s), and add the
476  // result to *this.
477  void keySwitchPart(const CtxtPart& p, const KeySwitch& W);
478 
479  // internal procedure used in key-switching
480  void keySwitchDigits(const KeySwitch& W, std::vector<DoubleCRT>& digits);
481 
482  long getPartIndexByHandle(const SKHandle& handle) const
483  {
484  for (size_t i = 0; i < parts.size(); i++)
485  if (parts[i].skHandle == handle)
486  return i;
487  return -1;
488  }
489 
490  // Sanity-check: Check that prime-set is "valid", i.e. that it
491  // contains either all the special primes or none of them
492  bool verifyPrimeSet() const;
493 
494  // A private assignment method that does not check equality of context or
495  // public key, this is needed when we copy the pubEncrKey member between
496  // different public keys.
497  Ctxt& privateAssign(const Ctxt& other);
498 
499  // explicitly multiply intFactor by e, which should be
500  // in the interval [0, ptxtSpace)
501  void mulIntFactor(long e);
502 
503 public:
508  static constexpr std::string_view typeName = "Ctxt";
509 
510  // Default copy-constructor
511  Ctxt(const Ctxt& other) = default;
512 
513  // VJS-FIXME: this was really a messy design choice to not
514  // have ciphertext constructors that specify prime sets.
515  // The default value of ctxtPrimes is kind of pointless.
516 
517  //__attribute__((deprecated))
518  explicit Ctxt(const PubKey& newPubKey, long newPtxtSpace = 0); // constructor
519 
520  //__attribute__((deprecated))
521  Ctxt(ZeroCtxtLike_type, const Ctxt& ctxt);
522  // constructs a zero ciphertext with same public key and
523  // plaintext space as ctxt
524 
528  void DummyEncrypt(const NTL::ZZX& ptxt, double size = -1.0);
529 
530  Ctxt& operator=(const Ctxt& other)
531  { // public assignment operator
532  assertEq(&context,
533  &other.context,
534  "Cannot assign Ctxts with different context");
535  assertEq(&pubKey,
536  &other.pubKey,
537  "Cannot assign Ctxts with different pubKey");
538  return privateAssign(other);
539  }
540 
541  bool operator==(const Ctxt& other) const { return equalsTo(other); }
542  bool operator!=(const Ctxt& other) const { return !equalsTo(other); }
543 
544  // a procedural variant with an additional parameter
545  bool equalsTo(const Ctxt& other, bool comparePkeys = true) const;
546 
547  // Encryption and decryption are done by the friends [Pub|Sec]Key
548 
551  void negate();
552 
553  // Add/subtract another ciphertext
554  Ctxt& operator+=(const Ctxt& other)
555  {
556  addCtxt(other);
557  return *this;
558  }
559 
560  Ctxt& operator-=(const Ctxt& other)
561  {
562  addCtxt(other, true);
563  return *this;
564  }
565 
566  void addCtxt(const Ctxt& other, bool negative = false);
567 
568  // Multiply by another ciphertext
569  void multLowLvl(const Ctxt& other, bool destructive = false);
570 
571 #if 0
572  [[deprecated]]
573  Ctxt& operator*=(const Ctxt& other)
574  {
575  multLowLvl(other);
576  return *this;
577  }
578 #else
579  // we now do the high-level mul
580  Ctxt& operator*=(const Ctxt& other)
581  {
582  multiplyBy(other);
583  return *this;
584  }
585 
586 #endif
587 
588  void automorph(long k); // Apply automorphism F(X) -> F(X^k) (gcd(k,m)=1)
589  Ctxt& operator>>=(long k)
590  {
591  automorph(k);
592  return *this;
593  }
594 
595  void complexConj(); // Complex conjugate, same as automorph(m-1)
596 
598  void smartAutomorph(long k);
599  // Apply F(X)->F(X^k) followed by re-linearization. The automorphism is
600  // possibly evaluated via a sequence of steps, to ensure that we can
601  // re-linearize the result of every step.
602 
604  void frobeniusAutomorph(long j);
605 
614  // [[deprecated]]
615  Ctxt& operator*=(const NTL::ZZX& poly);
616 
624  void addConstant(const DoubleCRT& dcrt, double size = -1.0);
625  void addConstant(const NTL::ZZX& poly, double size = -1.0);
626 
631  template <typename Scheme>
632  void addConstant(const Ptxt<Scheme>& ptxt, bool neg = false)
633  {
634  EncodedPtxt eptxt;
635  ptxt.encode(eptxt);
636  addConstant(eptxt, neg);
637  }
638 
639  template <typename Scheme>
641  {
642  addConstant(ptxt);
643  return *this;
644  }
645 
646  template <typename Scheme>
648  {
649  addConstant(ptxt, true);
650  return *this;
651  }
652 
654 
658  // [[deprecated]]
659  void addConstantCKKS(std::pair</*numerator=*/long, /*denominator=*/long>);
664  // [[deprecated]]
665  void addConstantCKKS(double x)
666  { // FIXME: not enough precision when x is large
668  rationalApprox(x, /*denomBound=*/1L << getContext().getAlMod().getR()));
669  }
670 
675  // [[deprecated]]
676  void addConstantCKKS(const DoubleCRT& dcrt,
677  NTL::xdouble size = NTL::xdouble(-1.0),
678  NTL::xdouble factor = NTL::xdouble(-1.0));
679 
684  // [[deprecated]]
685  void addConstantCKKS(const NTL::ZZX& poly,
686  NTL::xdouble size = NTL::xdouble(-1.0),
687  NTL::xdouble factor = NTL::xdouble(-1.0));
688 
693  // [[deprecated]]
694  void addConstantCKKS(const std::vector<std::complex<double>>& ptxt);
695 
702  // [[deprecated]]
703  void addConstantCKKS(const Ptxt<CKKS>& ptxt);
708  // [[deprecated]]
709  void addConstantCKKS(const NTL::ZZ& c);
710 
716  void multByConstant(const DoubleCRT& dcrt, double size = -1.0);
717 
718  void multByConstant(const NTL::ZZX& poly, double size = -1.0);
719  void multByConstant(const zzX& poly, double size = -1.0);
720 
721  //=========== new multByConstant interface =========
722 
727  void multByConstant(const PtxtArray& ptxt);
733  void multByConstant(const EncodedPtxt& ptxt);
740  void multByConstant(const FatEncodedPtxt& ptxt);
741 
746  void multByConstant(const NTL::ZZ& ptxt);
751  void multByConstant(long ptxt);
756  void multByConstant(double ptxt);
761  void multByConstant(NTL::xdouble ptxt);
762 
768  Ctxt& operator*=(const PtxtArray& ptxt)
769  {
770  multByConstant(ptxt);
771  return *this;
772  }
773 
781  {
782  multByConstant(ptxt);
783  return *this;
784  }
785 
794  {
795  multByConstant(ptxt);
796  return *this;
797  }
798 
804  Ctxt& operator*=(const NTL::ZZ& ptxt)
805  {
806  multByConstant(ptxt);
807  return *this;
808  }
809 
815  Ctxt& operator*=(long ptxt)
816  {
817  multByConstant(ptxt);
818  return *this;
819  }
820 
826  Ctxt& operator*=(double ptxt)
827  {
828  multByConstant(ptxt);
829  return *this;
830  }
831 
837  Ctxt& operator*=(NTL::xdouble ptxt)
838  {
839  multByConstant(ptxt);
840  return *this;
841  }
842 
843 private: // impl only
844  void multByConstant(const FatEncodedPtxt_BGV& ptxt);
845  void multByConstant(const FatEncodedPtxt_CKKS& ptxt);
846 
847 public:
848  //=========== new addConstant interface ============
849 
856  void addConstant(const PtxtArray& ptxt, bool neg = false);
864  void addConstant(const EncodedPtxt& ptxt, bool neg = false);
873  void addConstant(const FatEncodedPtxt& ptxt, bool neg = false);
874 
881  void addConstant(const NTL::ZZ& ptxt, bool neg = false);
888  void addConstant(long ptxt, bool neg = false);
895  void addConstant(double ptxt, bool neg = false);
902  void addConstant(NTL::xdouble ptxt, bool neg = false);
903 
909  Ctxt& operator+=(const PtxtArray& ptxt)
910  {
911  addConstant(ptxt);
912  return *this;
913  }
914 
922  {
923  addConstant(ptxt);
924  return *this;
925  }
926 
935  {
936  addConstant(ptxt);
937  return *this;
938  }
939 
945  Ctxt& operator+=(const NTL::ZZ& ptxt)
946  {
947  addConstant(ptxt);
948  return *this;
949  }
950 
956  Ctxt& operator+=(long ptxt)
957  {
958  addConstant(ptxt);
959  return *this;
960  }
961 
967  Ctxt& operator+=(double ptxt)
968  {
969  addConstant(ptxt);
970  return *this;
971  }
972 
978  Ctxt& operator+=(NTL::xdouble ptxt)
979  {
980  addConstant(ptxt);
981  return *this;
982  }
983 
989  Ctxt& operator-=(const PtxtArray& ptxt)
990  {
991  addConstant(ptxt, true);
992  return *this;
993  }
994 
1002  {
1003  addConstant(ptxt, true);
1004  return *this;
1005  }
1006 
1015  {
1016  addConstant(ptxt, true);
1017  return *this;
1018  }
1019 
1025  Ctxt& operator-=(const NTL::ZZ& ptxt)
1026  {
1027  addConstant(ptxt, true);
1028  return *this;
1029  }
1030 
1036  Ctxt& operator-=(long ptxt)
1037  {
1038  addConstant(ptxt, true);
1039  return *this;
1040  }
1041 
1047  Ctxt& operator-=(double ptxt)
1048  {
1049  addConstant(ptxt, true);
1050  return *this;
1051  }
1052 
1058  Ctxt& operator-=(NTL::xdouble ptxt)
1059  {
1060  addConstant(ptxt, true);
1061  return *this;
1062  }
1063 
1064 private: // impl only
1065  void addConstant(const FatEncodedPtxt_BGV& ptxt, bool neg = false);
1066  void addConstant(const EncodedPtxt_BGV& ptxt, bool neg = false);
1067 
1068  void addConstant(const FatEncodedPtxt_CKKS& ptxt, bool neg = false);
1069 
1070 public:
1071  //==================================================
1072 
1077  template <typename Scheme>
1078  void multByConstant(const Ptxt<Scheme>& ptxt)
1079  {
1080  EncodedPtxt eptxt;
1081  ptxt.encode(eptxt);
1082  multByConstant(eptxt);
1083  }
1084 
1085  template <typename Scheme>
1087  {
1088  multByConstant(ptxt);
1089  return *this;
1090  }
1091 
1093 
1097  // [[deprecated]]
1098  void multByConstantCKKS(double x)
1099  {
1100  if (this->isEmpty())
1101  return;
1102 
1103  if (x == 1)
1104  return;
1105 
1106  if (x == 0) {
1107  clear();
1108  return;
1109  }
1110 
1111  double size = std::abs(x);
1112  ptxtMag *= size;
1113  ratFactor /= size;
1114  if (x < 0)
1115  this->negate();
1116  }
1117 
1122  // [[deprecated]]
1123  void multByConstantCKKS(std::pair<long, long> num) // rational number
1124  {
1125  multByConstantCKKS(double(num.first) / num.second);
1126  }
1127 
1132  // [[deprecated]]
1133  void multByConstantCKKS(const DoubleCRT& dcrt,
1134  NTL::xdouble size = NTL::xdouble(-1.0),
1135  NTL::xdouble factor = NTL::xdouble(-1.0),
1136  double roundingErr = -1.0);
1137 
1142  // [[deprecated]]
1143  void multByConstantCKKS(const NTL::ZZX& poly,
1144  NTL::xdouble size = NTL::xdouble(-1.0),
1145  NTL::xdouble factor = NTL::xdouble(-1.0),
1146  double roundingErr = -1.0)
1147  {
1148  DoubleCRT dcrt(poly, context, primeSet);
1149  multByConstantCKKS(dcrt, size, factor, roundingErr);
1150  }
1151 
1152  // TODO: Ptxt: refactor into single function for getting the
1153  // std::vector<cx_double> repr of slots
1160  // [[deprecated]]
1161  void multByConstantCKKS(const Ptxt<CKKS>& ptxt);
1166  // [[deprecated]]
1167  void multByConstantCKKS(const std::vector<std::complex<double>>& ptxt);
1168 
1172  void xorConstant(const DoubleCRT& poly, UNUSED double size = -1.0)
1173  {
1174  DoubleCRT tmp = poly;
1175  tmp *= -2;
1176  tmp += 1; // tmp = 1-2*poly
1177  multByConstant(tmp);
1178  addConstant(poly);
1179  }
1180 
1181  void xorConstant(const NTL::ZZX& poly, double size = -1.0)
1182  {
1183  xorConstant(DoubleCRT(poly, context, primeSet), size);
1184  }
1185 
1186  void nxorConstant(const DoubleCRT& poly, UNUSED double size = -1.0)
1187  {
1188  DoubleCRT tmp = poly;
1189  tmp *= 2;
1190  tmp -= 1; // 2a-1
1191  addConstant(NTL::to_ZZX(-1L)); // b11
1192  multByConstant(tmp); // (b-1)(2a-1)
1193  addConstant(poly); // (b-1)(2a-1)+a = 1-a-b+2ab
1194  }
1195 
1196  void nxorConstant(const NTL::ZZX& poly, double size = -1.0)
1197  {
1198  nxorConstant(DoubleCRT(poly, context, primeSet), size);
1199  }
1200 
1205  void divideByP();
1206 
1209  void multByP(long e = 1)
1210  {
1211  long p2e = NTL::power_long(context.getP(), e);
1212  ptxtSpace *= p2e;
1213  multByConstant(NTL::to_ZZ(p2e));
1214  }
1215 
1216  // For backward compatibility
1217  void divideBy2();
1218  void extractBits(std::vector<Ctxt>& bits, long nBits2extract = 0);
1219 
1220  // Higher-level multiply routines
1221  void multiplyBy(const Ctxt& other);
1222  void multiplyBy2(const Ctxt& other1, const Ctxt& other2);
1223  void square() { multiplyBy(*this); }
1224  void cube() { multiplyBy2(*this, *this); }
1225 
1227  void power(long e); // in polyEval.cpp
1228 
1230 
1233 
1235  void reducePtxtSpace(long newPtxtSpace);
1236 
1237  // This method can be used to increase the plaintext space, but the
1238  // high-order digits that you get this way are noise. Do not use it
1239  // unless you know what you are doing.
1240  void hackPtxtSpace(long newPtxtSpace) { ptxtSpace = newPtxtSpace; }
1241 
1242  void bumpNoiseBound(double factor) { noiseBound *= factor; }
1243 
1244  // CKKS adjustment to protect precision
1245  void relin_CKKS_adjust();
1246 
1247  void reLinearize(long keyIdx = 0);
1248  // key-switch to (1,s_i), s_i is the base key with index keyIdx
1249 
1250  Ctxt& cleanUp();
1251  // relinearize, then reduce, then drop special primes and small primes.
1252 
1253  // void reduce() const;
1254 
1256  void blindCtxt(const NTL::ZZX& poly);
1257 
1259  NTL::xdouble modSwitchAddedNoiseBound() const;
1260 
1264  void modUpToSet(const IndexSet& s);
1265 
1269  void modDownToSet(const IndexSet& s);
1270 
1273  void bringToSet(const IndexSet& s);
1274 
1275  // Finding the "natural" state of a ciphertext
1276  double naturalSize() const;
1277  IndexSet naturalPrimeSet() const;
1278 
1283 
1286  NTL::xdouble totalNoiseBound() const
1287  {
1288  if (isCKKS())
1289  return ptxtMag * ratFactor + noiseBound;
1290  else
1291  return noiseBound;
1292  }
1293 
1296  double errorBound() const
1297  {
1298  if (isCKKS())
1299  return convert<double>(noiseBound / ratFactor);
1300  else
1301  return 0;
1302  }
1303 
1307  double capacity() const
1308  {
1309  return (logOfPrimeSet() -
1310  NTL::log(std::max(totalNoiseBound(), NTL::to_xdouble(1.0)))) /
1311  std::log(2.0);
1312  }
1313 
1315  double logOfPrimeSet() const { return context.logOfProduct(getPrimeSet()); }
1316 
1318  long bitCapacity() const { return long(capacity()); }
1319 
1328  double rawModSwitch(std::vector<NTL::ZZX>& zzParts, long toModulus) const;
1329 
1331  // void computePowers(std::vector<Ctxt>& v, long nPowers) const;
1332 
1334  void evalPoly(const NTL::ZZX& poly);
1336 
1339 
1340  void clear()
1341  { // set as an empty ciphertext
1342  parts.clear();
1343  primeSet = context.getCtxtPrimes();
1344  noiseBound = 0.0;
1345  intFactor = 1;
1346  ratFactor = ptxtMag = 1.0;
1347  }
1348 
1350  bool isEmpty() const { return (parts.size() == 0); }
1351 
1353  bool inCanonicalForm(long keyID = 0) const
1354  {
1355  if (parts.size() > 2)
1356  return false;
1357  if (parts.size() > 0 && !parts[0].skHandle.isOne())
1358  return false;
1359  if (parts.size() > 1 && !parts[1].skHandle.isBase(keyID))
1360  return false;
1361  return true;
1362  }
1363 
1365  bool isCorrect() const;
1366 
1367  const Context& getContext() const { return context; }
1368  const PubKey& getPubKey() const { return pubKey; }
1369  const IndexSet& getPrimeSet() const { return primeSet; }
1370  long getPtxtSpace() const { return ptxtSpace; }
1371  const NTL::xdouble& getNoiseBound() const { return noiseBound; }
1372  const NTL::xdouble& getRatFactor() const { return ratFactor; }
1373  const NTL::xdouble& getPtxtMag() const { return ptxtMag; }
1374  void setPtxtMag(const NTL::xdouble& z) { ptxtMag = z; }
1375  long getKeyID() const;
1376 
1377  bool isCKKS() const { return getContext().isCKKS(); }
1378 
1379  // Return r such that p^r = ptxtSpace
1380  long effectiveR() const
1381  {
1382  long p = context.getP();
1383  for (long r = 1, p2r = p; r < NTL_SP_NBITS; r++, p2r *= p) {
1384  if (p2r == ptxtSpace)
1385  return r;
1386  if (p2r > ptxtSpace)
1387  throw RuntimeError("ctxt.ptxtSpace is not of the form p^r");
1388  }
1389  throw RuntimeError("ctxt.ptxtSpace is not of the form p^r");
1390  return 0; // just to keep the compiler happy
1391  }
1392 
1397  // [[deprecated]] // use capacity()
1398  double log_of_ratio() const
1399  {
1400  double logNoise =
1401  (getNoiseBound() <= 0.0) ? -DBL_MAX : log(getNoiseBound());
1402  double logMod =
1403  empty(getPrimeSet()) ? -DBL_MAX : context.logOfProduct(getPrimeSet());
1404  return logNoise - logMod;
1405  }
1407  friend std::istream& operator>>(std::istream& str, Ctxt& ctxt);
1408  friend std::ostream& operator<<(std::ostream& str, const Ctxt& ctxt);
1409 
1410  // Raw IO
1411 
1416  void writeTo(std::ostream& str) const;
1417 
1423  static Ctxt readFrom(std::istream& str, const PubKey& pubKey);
1424 
1430  void read(std::istream& str);
1431 
1437  void writeToJSON(std::ostream& str) const;
1438 
1443  JsonWrapper writeToJSON() const;
1444 
1452  static Ctxt readFromJSON(std::istream& str, const PubKey& pubKey);
1453 
1461  static Ctxt readFromJSON(const JsonWrapper& j, const PubKey& pubKey);
1462 
1468  void readJSON(std::istream& str);
1469 
1475  void readJSON(const JsonWrapper& j);
1476 
1477  // scale up c1, c2 so they have the same ratFactor
1478  static void equalizeRationalFactors(Ctxt& c1, Ctxt& c2);
1479 
1480  // adds noise for CKKS deryption to mitigate against
1481  // plaintext leakage attacks, as in the paper
1482  // "On the Security of Homomorphic Encryption on Approximate Numbers",
1483  // by Li and Micciancio.
1484  void addedNoiseForCKKSDecryption(const SecKey& sk,
1485  double eps,
1486  NTL::ZZX& noise) const;
1487 };
1488 
1489 // set out=prod_{i=0}^{n-1} v[j], takes depth log n and n-1 products
1490 // out could point to v[0], but having it pointing to any other v[i]
1491 // will make the result unpredictable.
1492 void totalProduct(Ctxt& out, const std::vector<Ctxt>& v);
1493 
1496 void incrementalProduct(std::vector<Ctxt>& v);
1497 
1498 void innerProduct(Ctxt& result,
1499  const std::vector<Ctxt>& v1,
1500  const std::vector<Ctxt>& v2);
1501 inline Ctxt innerProduct(const std::vector<Ctxt>& v1,
1502  const std::vector<Ctxt>& v2)
1503 {
1504  Ctxt ret(v1[0].getPubKey());
1505  innerProduct(ret, v1, v2);
1506  return ret;
1507 }
1508 
1510 void innerProduct(Ctxt& result,
1511  const std::vector<Ctxt>& v1,
1512  const std::vector<DoubleCRT>& v2);
1513 inline Ctxt innerProduct(const std::vector<Ctxt>& v1,
1514  const std::vector<DoubleCRT>& v2)
1515 {
1516  Ctxt ret(v1[0].getPubKey());
1517  innerProduct(ret, v1, v2);
1518  return ret;
1519 }
1520 
1521 void innerProduct(Ctxt& result,
1522  const std::vector<Ctxt>& v1,
1523  const std::vector<NTL::ZZX>& v2);
1524 inline Ctxt innerProduct(const std::vector<Ctxt>& v1,
1525  const std::vector<NTL::ZZX>& v2)
1526 {
1527  Ctxt ret(v1[0].getPubKey());
1528  innerProduct(ret, v1, v2);
1529  return ret;
1530 }
1531 
1533 inline void frobeniusAutomorph(Ctxt& ctxt, long j)
1534 {
1535  ctxt.frobeniusAutomorph(j);
1536 }
1537 
1539 inline void conjugate(Ctxt& ctxt) { frobeniusAutomorph(ctxt, 1); }
1540 
1544 void extractRealPart(Ctxt& c);
1545 void extractImPart(Ctxt& c);
1546 
1548 inline void power(Ctxt& ctxt, long e) { ctxt.power(e); }
1549 
1551 inline void negate(Ctxt& ctxt) { ctxt.negate(); }
1552 
1554 void CheckCtxt(const Ctxt& c, const char* label);
1555 
1577 void extractDigits(std::vector<Ctxt>& digits, const Ctxt& c, long r = 0);
1578 // implemented in extractDigits.cpp
1579 
1580 inline void extractDigits(std::vector<Ctxt>& digits,
1581  const Ctxt& c,
1582  long r,
1583  bool shortCut)
1584 {
1585  if (shortCut)
1586  std::cerr << "extractDigits: the shortCut flag is disabled\n";
1587  extractDigits(digits, c, r);
1588 }
1589 
1590 inline void Ctxt::extractBits(std::vector<Ctxt>& bits, long nBits2extract)
1591 {
1592  extractDigits(bits, *this, nBits2extract);
1593 }
1594 
1595 // @brief Extract the mod-p digits of a mod-p^{r+e} ciphertext.
1596 
1597 // extendExtractDigits assumes that the slots of *this contains integers mod
1598 // p^{r+e} i.e., that only the free terms are nonzero. (If that assumptions
1599 // does not hold then the result will not be a valid ciphertext anymore.)
1600 //
1601 // It returns in the slots of digits[j] the j'th-lowest digits from the
1602 // integers in the slots of the input. Namely, the i'th slot of digits[j]
1603 // contains the j'th digit in the p-base expansion of the integer in the i'th
1604 // slot of the *this. The plaintext space of digits[j] is mod p^{e+r-j}.
1605 
1606 void extendExtractDigits(std::vector<Ctxt>& digits,
1607  const Ctxt& c,
1608  long r,
1609  long e);
1610 // implemented in extractDigits.cpp
1611 
1612 } // namespace helib
1613 
1614 #endif // ifndef HELIB_CTXT_H
Pre-computation to speed many automorphism on the same ciphertext.
Definition: matmul.cpp:61
Maintaining the HE scheme parameters.
Definition: Context.h:100
double logOfProduct(const IndexSet &s) const
Calculate the natural logarithm of productOfPrimes(s) for a given set of primes s.
Definition: Context.h:840
long getP() const
Getter method for the p used to create this context.
Definition: Context.h:258
const IndexSet & getCtxtPrimes() const
Getter method to the index set to the ciphertext primes.
Definition: Context.h:344
bool isCKKS() const
Return whether this is a CKKS context or not Context.
Definition: Context.h:379
A Ctxt object holds a single ciphertext.
Definition: Ctxt.h:396
void xorConstant(const NTL::ZZX &poly, double size=-1.0)
Definition: Ctxt.h:1181
void automorph(long k)
Definition: Ctxt.cpp:2412
void blindCtxt(const NTL::ZZX &poly)
Add a high-noise encryption of the given constant.
Definition: Ctxt.cpp:550
Ctxt & operator-=(long ptxt)
Minus equals operator with a long scalar.
Definition: Ctxt.h:1036
void multByConstantCKKS(const NTL::ZZX &poly, NTL::xdouble size=NTL::xdouble(-1.0), NTL::xdouble factor=NTL::xdouble(-1.0), double roundingErr=-1.0)
Definition: Ctxt.h:1143
Ctxt & operator-=(const NTL::ZZ &ptxt)
Minus equals operator with an NTL::ZZ scalar.
Definition: Ctxt.h:1025
Ctxt & operator*=(const Ctxt &other)
Definition: Ctxt.h:580
void negate()
Definition: Ctxt.cpp:1169
const NTL::xdouble & getNoiseBound() const
Definition: Ctxt.h:1371
const IndexSet & getPrimeSet() const
Definition: Ctxt.h:1369
bool isEmpty() const
Is this an empty ciphertext without any parts.
Definition: Ctxt.h:1350
void multByConstant(const Ptxt< Scheme > &ptxt)
Multiply a plaintext to this Ctxt.
Definition: Ctxt.h:1078
const Context & getContext() const
Definition: Ctxt.h:1367
void addedNoiseForCKKSDecryption(const SecKey &sk, double eps, NTL::ZZX &noise) const
Definition: Ctxt.cpp:3026
void writeTo(std::ostream &str) const
Write out the Ctxt object in binary format.
Definition: Ctxt.cpp:2559
void addConstantCKKS(std::pair< long, long >)
add a rational number in the form a/b, a,b are long
Definition: Ctxt.cpp:1107
const NTL::xdouble & getPtxtMag() const
Definition: Ctxt.h:1373
Ctxt & operator-=(const FatEncodedPtxt &ptxt)
Minus equals operator with plaintext constant.
Definition: Ctxt.h:1014
void readJSON(std::istream &str)
In-place read from the str std::istream the serialized ciphertext (Ctxt) object.
Definition: Ctxt.cpp:2655
void reLinearize(long keyIdx=0)
Definition: Ctxt.cpp:706
Ctxt & operator+=(NTL::xdouble ptxt)
Plus equals operator with an NTL::xdouble scalar.
Definition: Ctxt.h:978
NTL::xdouble modSwitchAddedNoiseBound() const
Estimate the added noise.
Definition: Ctxt.cpp:2535
void complexConj()
Definition: Ctxt.cpp:2493
void DummyEncrypt(const NTL::ZZX &ptxt, double size=-1.0)
Definition: Ctxt.cpp:119
Ctxt & operator+=(const FatEncodedPtxt &ptxt)
Plus equals operator with plaintext constant.
Definition: Ctxt.h:934
Ctxt & operator-=(NTL::xdouble ptxt)
Minus equals operator with an NTL::xdouble scalar.
Definition: Ctxt.h:1058
void modDownToSet(const IndexSet &s)
Modulus-switching down (to a smaller modulus). mod-switch down to primeSet \intersect s,...
Definition: Ctxt.cpp:379
void multByConstant(const DoubleCRT &dcrt, double size=-1.0)
Definition: Ctxt.cpp:1811
Ctxt & operator-=(const Ptxt< Scheme > &ptxt)
Definition: Ctxt.h:647
void dropSmallAndSpecialPrimes()
the corresponding primeSet
Definition: Ctxt.cpp:575
double log_of_ratio() const
Returns log(noiseBound) - log(q)
Definition: Ctxt.h:1398
long effectiveR() const
Definition: Ctxt.h:1380
Ctxt & operator=(const Ctxt &other)
Definition: Ctxt.h:530
void xorConstant(const DoubleCRT &poly, UNUSED double size=-1.0)
Definition: Ctxt.h:1172
void extractBits(std::vector< Ctxt > &bits, long nBits2extract=0)
Definition: Ctxt.h:1590
void multiplyBy(const Ctxt &other)
Definition: Ctxt.cpp:1736
void cube()
Definition: Ctxt.h:1224
static void equalizeRationalFactors(Ctxt &c1, Ctxt &c2)
Definition: Ctxt.cpp:1191
long bitCapacity() const
the capacity in bits, returned as an integer
Definition: Ctxt.h:1318
Ctxt & operator+=(double ptxt)
Plus equals operator with a double scalar.
Definition: Ctxt.h:967
Ctxt & operator*=(const Ptxt< Scheme > &ptxt)
Definition: Ctxt.h:1086
const PubKey & getPubKey() const
Definition: Ctxt.h:1368
double logOfPrimeSet() const
returns the log of the prime set
Definition: Ctxt.h:1315
void nxorConstant(const DoubleCRT &poly, UNUSED double size=-1.0)
Definition: Ctxt.h:1186
void multiplyBy2(const Ctxt &other1, const Ctxt &other2)
Definition: Ctxt.cpp:1755
void multByP(long e=1)
Definition: Ctxt.h:1209
const NTL::xdouble & getRatFactor() const
Definition: Ctxt.h:1372
void hackPtxtSpace(long newPtxtSpace)
Definition: Ctxt.h:1240
double errorBound() const
for CKKS, returns a bound on the absolute error (which is noiseBound/ratFactor); for BGV,...
Definition: Ctxt.h:1296
Ctxt & operator*=(const EncodedPtxt &ptxt)
Times equals operator with a plaintext constant.
Definition: Ctxt.h:780
void square()
Definition: Ctxt.h:1223
NTL::xdouble totalNoiseBound() const
returns the total noise bound, which for CKKS is ptxtMag*ratFactor + noiseBound
Definition: Ctxt.h:1286
void addConstantCKKS(double x)
Definition: Ctxt.h:665
Ctxt & operator*=(double ptxt)
Times equals operator with a double scalar.
Definition: Ctxt.h:826
JsonWrapper writeToJSON() const
Write out the ciphertext (Ctxt) object to a JsonWrapper.
Definition: Ctxt.cpp:2623
Ctxt & operator-=(const PtxtArray &ptxt)
Minus equals operator with plaintext constant.
Definition: Ctxt.h:989
void modUpToSet(const IndexSet &s)
Modulus-switching up (to a larger modulus). Must have primeSet <= s, and s must contain either all th...
Definition: Ctxt.cpp:332
void reducePtxtSpace(long newPtxtSpace)
Reduce plaintext space to a divisor of the original plaintext space.
Definition: Ctxt.cpp:562
Ctxt & operator+=(const EncodedPtxt &ptxt)
Plus equals operator with plaintext constant.
Definition: Ctxt.h:921
void clear()
Definition: Ctxt.h:1340
void read(std::istream &str)
In-place read from the stream the serialized Ctxt object in binary format.
Definition: Ctxt.cpp:2590
void bumpNoiseBound(double factor)
Definition: Ctxt.h:1242
static constexpr std::string_view typeName
Class label to be added to JSON serialization as object type information.
Definition: Ctxt.h:508
IndexSet naturalPrimeSet() const
"natural size" is size before squaring
Definition: Ctxt.cpp:1652
bool operator==(const Ctxt &other) const
Definition: Ctxt.h:541
bool inCanonicalForm(long keyID=0) const
A canonical ciphertext has (at most) handles pointing to (1,s)
Definition: Ctxt.h:1353
void multByConstantCKKS(std::pair< long, long > num)
Definition: Ctxt.h:1123
Ctxt & operator+=(const Ctxt &other)
Definition: Ctxt.h:554
friend std::istream & operator>>(std::istream &str, Ctxt &ctxt)
Definition: Ctxt.cpp:2774
void power(long e)
raise ciphertext to some power
Definition: polyEval.cpp:392
Ctxt(const Ctxt &other)=default
static Ctxt readFromJSON(std::istream &str, const PubKey &pubKey)
Read from the stream the serialized ciphertext (Ctxt) object using JSON format.
Definition: Ctxt.cpp:2639
double capacity() const
returns the "capacity" of a ciphertext, which is the log2 of the ratio of the modulus to the total no...
Definition: Ctxt.h:1307
void setPtxtMag(const NTL::xdouble &z)
Definition: Ctxt.h:1374
void nxorConstant(const NTL::ZZX &poly, double size=-1.0)
Definition: Ctxt.h:1196
Ctxt & cleanUp()
Definition: Ctxt.cpp:774
long getKeyID() const
Definition: Ctxt.cpp:2525
void evalPoly(const NTL::ZZX &poly)
compute the power X,X^2,...,X^n
bool isCorrect() const
Would this ciphertext be decrypted without errors?
Definition: Ctxt.cpp:102
void addCtxt(const Ctxt &other, bool negative=false)
Definition: Ctxt.cpp:1385
Ctxt & operator*=(long ptxt)
Times equals operator with a long scalar.
Definition: Ctxt.h:815
double rawModSwitch(std::vector< NTL::ZZX > &zzParts, long toModulus) const
Special-purpose modulus-switching for bootstrapping.
Definition: Ctxt.cpp:2924
friend std::ostream & operator<<(std::ostream &str, const Ctxt &ctxt)
Definition: Ctxt.cpp:2768
void divideBy2()
Definition: Ctxt.cpp:2365
Ctxt & operator*=(const PtxtArray &ptxt)
Times equals operator with a plaintext constant.
Definition: Ctxt.h:768
bool equalsTo(const Ctxt &other, bool comparePkeys=true) const
Definition: Ctxt.cpp:228
Ctxt & operator+=(long ptxt)
Plus equals operator with a long scalar.
Definition: Ctxt.h:956
bool isCKKS() const
Definition: Ctxt.h:1377
Ctxt & operator+=(const PtxtArray &ptxt)
Plus equals operator with plaintext constant.
Definition: Ctxt.h:909
void addConstant(const DoubleCRT &dcrt, double size=-1.0)
Definition: Ctxt.cpp:882
long getPtxtSpace() const
Definition: Ctxt.h:1370
void multByConstantCKKS(double x)
multiply by a rational number or floating point
Definition: Ctxt.h:1098
bool operator!=(const Ctxt &other) const
Definition: Ctxt.h:542
void smartAutomorph(long k)
automorphism with re-linearization
Definition: Ctxt.cpp:2437
Ctxt & operator-=(const Ctxt &other)
Definition: Ctxt.h:560
Ctxt & operator*=(const FatEncodedPtxt &ptxt)
Times equals operator with a plaintext constant.
Definition: Ctxt.h:793
Ctxt & operator-=(double ptxt)
Minus equals operator with a double scalar.
Definition: Ctxt.h:1047
Ctxt & operator>>=(long k)
Definition: Ctxt.h:589
Ctxt & operator*=(NTL::xdouble ptxt)
Times equals operator with an NTL::xdouble scalar.
Definition: Ctxt.h:837
Ctxt & operator*=(const NTL::ZZ &ptxt)
Times equals operator with an NTL::ZZ scalar.
Definition: Ctxt.h:804
Ctxt & operator+=(const Ptxt< Scheme > &ptxt)
Definition: Ctxt.h:640
Ctxt & operator-=(const EncodedPtxt &ptxt)
Minus equals operator with plaintext constant.
Definition: Ctxt.h:1001
void relin_CKKS_adjust()
Definition: Ctxt.cpp:650
void bringToSet(const IndexSet &s)
make the primeSet equal to newPrimeSet, via modUpToSet and modDownToSet
Definition: Ctxt.cpp:359
static Ctxt readFrom(std::istream &str, const PubKey &pubKey)
Read from the stream the serialized Ctxt object in binary format.
Definition: Ctxt.cpp:2582
double naturalSize() const
Definition: Ctxt.cpp:1642
void frobeniusAutomorph(long j)
applies the automorphism p^j using smartAutomorphism
Definition: Ctxt.cpp:2501
void addConstant(const Ptxt< Scheme > &ptxt, bool neg=false)
Add a plaintext to this Ctxt.
Definition: Ctxt.h:632
void divideByP()
Definition: Ctxt.cpp:2390
void multLowLvl(const Ctxt &other, bool destructive=false)
Definition: Ctxt.cpp:1660
Ctxt & operator+=(const NTL::ZZ &ptxt)
Plus equals operator with an NTL::ZZ scalar.
Definition: Ctxt.h:945
One entry in a ciphertext std::vector.
Definition: Ctxt.h:263
bool operator!=(const CtxtPart &other) const
Definition: Ctxt.h:269
CtxtPart(const Context &_context, const IndexSet &s)
Definition: Ctxt.h:273
SKHandle skHandle
The handle is a public data member.
Definition: Ctxt.h:266
CtxtPart(const DoubleCRT &other)
Definition: Ctxt.h:285
JsonWrapper writeToJSON() const
Write out the ciphertext part (CtxtPart) object to a JsonWrapper.
Definition: Ctxt.cpp:2714
void readJSON(std::istream &str)
Read from the stream the serialized ciphertext part (CtxtPart) object using JSON format.
Definition: Ctxt.cpp:2735
CtxtPart(const DoubleCRT &other, const SKHandle &otherHandle)
Definition: Ctxt.h:290
bool operator==(const CtxtPart &other) const
Definition: Ctxt.cpp:218
static CtxtPart readFrom(std::istream &str, const Context &context)
Read from the stream the serialized CtxtPart object in binary format.
Definition: Ctxt.cpp:2696
static CtxtPart readFromJSON(std::istream &str, const Context &context)
Read from the stream the serialized ciphertext part (CtxtPart) object using JSON format.
Definition: Ctxt.cpp:2721
void read(std::istream &str)
In-place read from the stream the serialized CtxtPart object in binary format.
Definition: Ctxt.cpp:2703
void writeTo(std::ostream &str) const
Write out the CtxtPart object in binary format.
Definition: Ctxt.cpp:2690
CtxtPart(const Context &_context, const IndexSet &s, const SKHandle &otherHandle)
Definition: Ctxt.h:278
Implementing polynomials (elements in the ring R_Q) in double-CRT form.
Definition: DoubleCRT.h:76
Definition: EncodedPtxt.h:21
Definition: EncodedPtxt.h:143
Definition: EncodedPtxt.h:186
Definition: EncodedPtxt.h:212
Definition: EncodedPtxt.h:313
A dynamic set of non-negative integers.
Definition: IndexSet.h:33
Definition: EncryptedArray.h:2167
An object that mimics the functionality of the Ctxt object, and acts as a convenient entry point for ...
Definition: Ptxt.h:188
void encode(EncodedPtxt &eptxt, double mag=-1, OptLong prec=OptLong()) const
Converts the slot data in this to a corresponding EncodedPtxt object. mag,prec must be defaulted for ...
The public key.
Definition: keys.h:45
Inherits from Exception and std::runtime_error.
Definition: exceptions.h:105
A handle, describing the secret-key element that "matches" a part, of the form s^r(X^t).
Definition: Ctxt.h:81
long getPowerOfS() const
Definition: Ctxt.h:137
void writeToJSON(std::ostream &str) const
Write out the secret key handle (SKHandle) object to the output stream using JSON format.
Definition: Ctxt.cpp:55
static SKHandle readFromJSON(std::istream &str)
Read from the stream the serialized secret key handle (SKHandle) object using JSON format.
Definition: Ctxt.cpp:69
bool mul(const SKHandle &a, const SKHandle &b)
Computes the "product" of two handles.
Definition: Ctxt.h:153
SKHandle(long newPowerOfS=0, long newPowerOfX=1, long newSecretKeyID=0)
Definition: Ctxt.h:87
friend std::istream & operator>>(std::istream &s, SKHandle &handle)
Definition: Ctxt.cpp:2750
JsonWrapper writeToJSON() const
Write out the secret key handle (SKHandle) object to a JsonWrapper.
Definition: Ctxt.cpp:60
long getPowerOfX() const
Definition: Ctxt.h:138
void setBase(long newSecretKeyID=-1)
Set powerOfS=powerOfX=1.
Definition: Ctxt.h:95
bool isOne() const
Is powerOfS==0?
Definition: Ctxt.h:122
void writeTo(std::ostream &str) const
Write out the SKHandle object in binary format.
Definition: Ctxt.cpp:48
bool operator!=(const SKHandle &other) const
Definition: Ctxt.h:133
void setOne(long newSecretKeyID=-1)
Set powerOfS=0, powerOfX=1.
Definition: Ctxt.h:113
long getSecretKeyID() const
Definition: Ctxt.h:139
static SKHandle readFrom(std::istream &str)
Read from the stream the serialized SKHandle object in binary format.
Definition: Ctxt.cpp:40
void readJSON(std::istream &str)
Read from the stream the serialized secret key handle (SKHandle) object using JSON format.
Definition: Ctxt.cpp:83
bool operator==(const SKHandle &other) const
Definition: Ctxt.h:124
bool isBase(long ofKeyID=0) const
Is powerOfS==powerOfX==1?
Definition: Ctxt.h:104
The secret key.
Definition: keys.h:311
Definition: apiAttributes.h:21
std::pair< long, long > rationalApprox(double x, long denomBound=0)
Definition: NumbTh.cpp:1598
void innerProduct(Ctxt &result, const CtPtrs &v1, const CtPtrs &v2)
Definition: Ctxt.cpp:2853
void extendExtractDigits(std::vector< Ctxt > &digits, const Ctxt &c, long r, long e)
Definition: extractDigits.cpp:225
NTL::Vec< long > zzX
Definition: zzX.h:24
void extractImPart(Ctxt &c)
Definition: Ctxt.cpp:3100
void extractRealPart(Ctxt &c)
Definition: Ctxt.cpp:3092
void incrementalProduct(std::vector< Ctxt > &v)
Definition: Ctxt.cpp:2809
void negate(Ctxt &ctxt)
negate: free function version of negate method
Definition: Ctxt.h:1551
void conjugate(Ctxt &ctxt)
conjugate: free function that is equivalent to frobeniusAutomorph(ctxt, 1)
Definition: Ctxt.h:1539
std::istream & operator>>(std::istream &s, CtxtPart &p)
Definition: Ctxt.cpp:2762
void CheckCtxt(const Ctxt &c, const char *label)
print to cerr some info about ciphertext
Definition: debugging.cpp:158
void totalProduct(Ctxt &out, const std::vector< Ctxt > &v)
Definition: Ctxt.cpp:2844
void extractDigits(std::vector< Ctxt > &digits, const Ctxt &c, long r=0)
Extract the mod-p digits of a mod-p^r ciphertext.
Definition: extractDigits.cpp:70
void frobeniusAutomorph(Ctxt &ctxt, long j)
frobeniusAutomorph: free function version of frobeniusAutomorph method
Definition: Ctxt.h:1533
std::ostream & operator<<(std::ostream &os, const ContextBuilder< SCHEME > &cb)
ostream operator for serializing the ContextBuilder object.
void power(Ctxt &ctxt, long e)
power: free function version of power method
Definition: Ctxt.h:1548
bool empty(const IndexSet &s)
Definition: IndexSet.h:222
void assertEq(const T &a, const T &b, const std::string &message)
Definition: assertions.h:108
Definition: JsonWrapper.h:9