58 #include <helib/DoubleCRT.h>
59 #include <helib/apiAttributes.h>
65 template <
typename Scheme>
79 long powerOfS, powerOfX, secretKeyID;
84 SKHandle(
long newPowerOfS = 0,
long newPowerOfX = 1,
long newSecretKeyID = 0)
86 powerOfS = newPowerOfS;
87 powerOfX = newPowerOfX;
88 secretKeyID = newSecretKeyID;
96 if (newSecretKeyID >= 0)
97 secretKeyID = newSecretKeyID;
105 return powerOfS == 1 && powerOfX == 1 &&
106 (ofKeyID < 0 || secretKeyID == ofKeyID);
114 if (newSecretKeyID >= 0)
115 secretKeyID = newSecretKeyID;
119 bool isOne()
const {
return powerOfS == 0; }
123 if (powerOfS == 0 && other.powerOfS == 0)
126 return powerOfS == other.powerOfS && powerOfX == other.powerOfX &&
127 secretKeyID == other.secretKeyID;
155 return (b.secretKeyID >= 0);
159 return (a.secretKeyID >= 0);
162 if (a.secretKeyID == -1 || b.secretKeyID == -1) {
167 if (a.secretKeyID != b.secretKeyID) {
172 if (a.powerOfX != b.powerOfX) {
177 secretKeyID = a.secretKeyID;
178 powerOfX = a.powerOfX;
179 powerOfS = a.powerOfS + b.powerOfS;
186 void read(std::istream& str);
187 void write(std::ostream& str)
const;
235 void read(std::istream& str);
236 void write(std::ostream& str)
const;
239 std::istream&
operator>>(std::istream& s, CtxtPart& p);
240 std::ostream&
operator<<(std::ostream& s,
const CtxtPart& p);
243 struct ZeroCtxtLike_type
246 const ZeroCtxtLike_type ZeroCtxtLike = ZeroCtxtLike_type();
280 std::vector<CtxtPart> parts;
285 NTL::xdouble noiseBound;
288 NTL::xdouble ratFactor;
289 NTL::xdouble ptxtMag;
294 void tensorProduct(
const Ctxt& c1,
const Ctxt& c2);
320 void subPart(
const CtxtPart& part,
bool matchPrimeSet =
false)
322 subPart(part, part.skHandle, matchPrimeSet);
325 void addPart(
const CtxtPart& part,
bool matchPrimeSet =
false)
327 addPart(part, part.skHandle, matchPrimeSet);
330 void subPart(
const DoubleCRT& part,
331 const SKHandle& handle,
332 bool matchPrimeSet =
false)
334 addPart(part, handle, matchPrimeSet,
true);
337 void addPart(
const DoubleCRT& part,
338 const SKHandle& handle,
339 bool matchPrimeSet =
false,
340 bool negative =
false);
345 void keySwitchPart(
const CtxtPart& p,
const KeySwitch& W);
348 void keySwitchDigits(
const KeySwitch& W, std::vector<DoubleCRT>& digits);
350 long getPartIndexByHandle(
const SKHandle& handle)
const
352 for (
size_t i = 0; i < parts.size(); i++)
353 if (parts[i].skHandle == handle)
360 bool verifyPrimeSet()
const;
365 Ctxt& privateAssign(
const Ctxt& other);
369 void mulIntFactor(
long e);
380 explicit Ctxt(
const PubKey& newPubKey,
long newPtxtSpace = 0);
383 Ctxt(ZeroCtxtLike_type,
const Ctxt& ctxt);
390 void DummyEncrypt(
const NTL::ZZX& ptxt,
double size = -1.0);
396 "Cannot assign Ctxts with different context");
399 "Cannot assign Ctxts with different pubKey");
400 return privateAssign(other);
407 bool equalsTo(
const Ctxt& other,
bool comparePkeys =
true)
const;
428 void addCtxt(
const Ctxt& other,
bool negative =
false);
524 void addConstant(
const NTL::ZZX& poly,
double size = -1.0);
530 template <
typename Scheme>
538 void addConstantCKKS(std::pair</*numerator=*/long, /*denominator=*/long>);
546 NTL::xdouble size = NTL::xdouble(-1.0),
547 NTL::xdouble factor = NTL::xdouble(-1.0));
550 NTL::xdouble size = NTL::xdouble(-1.0),
551 NTL::xdouble factor = NTL::xdouble(-1.0));
576 template <
typename Scheme>
597 double size = std::abs(x);
610 NTL::xdouble size = NTL::xdouble(-1.0),
611 NTL::xdouble factor = NTL::xdouble(-1.0),
612 double roundingErr = -1.0);
615 NTL::xdouble size = NTL::xdouble(-1.0),
616 NTL::xdouble factor = NTL::xdouble(-1.0),
617 double roundingErr = -1.0)
674 long p2e = NTL::power_long(context.
zMStar.
getP(), e);
681 void extractBits(std::vector<Ctxt>& bits,
long nBits2extract = 0);
749 if (noiseBound <= 1.0)
776 double rawModSwitch(std::vector<NTL::ZZX>& zzParts,
long toModulus)
const;
792 noiseBound = NTL::to_xdouble(0.0);
798 bool isEmpty()
const {
return (parts.size() == 0); }
803 if (parts.size() > 2)
805 if (parts.size() > 0 && !parts[0].skHandle.isOne())
807 if (parts.size() > 1 && !parts[1].skHandle.isBase(keyID))
816 return NTL::to_xdouble(q) > noiseBound * 2;
835 for (
long r = 1, p2r = p; r < NTL_SP_NBITS; r++, p2r *= p) {
836 if (p2r == ptxtSpace)
839 throw RuntimeError(
"ctxt.ptxtSpace is not of the form p^r");
841 throw RuntimeError(
"ctxt.ptxtSpace is not of the form p^r");
852 return logNoise - logMod;
856 friend std::ostream&
operator<<(std::ostream& str,
const Ctxt& ctxt);
859 void write(std::ostream& str)
const;
860 void read(std::istream& str);
869 void totalProduct(Ctxt& out,
const std::vector<Ctxt>& v);
876 const std::vector<Ctxt>& v1,
877 const std::vector<Ctxt>& v2);
879 const std::vector<Ctxt>& v2)
881 Ctxt ret(v1[0].getPubKey());
888 const std::vector<Ctxt>& v1,
889 const std::vector<DoubleCRT>& v2);
891 const std::vector<DoubleCRT>& v2)
893 Ctxt ret(v1[0].getPubKey());
899 const std::vector<Ctxt>& v1,
900 const std::vector<NTL::ZZX>& v2);
902 const std::vector<NTL::ZZX>& v2)
904 Ctxt ret(v1[0].getPubKey());
910 void CheckCtxt(
const Ctxt& c,
const char* label);
933 void extractDigits(std::vector<Ctxt>& digits,
const Ctxt& c,
long r = 0);
942 std::cerr <<
"extractDigits: the shortCut flag is disabled\n";
970 #endif // ifndef HELIB_CTXT_H
An object that mimics the functionality of the Ctxt object, and acts as a convenient entry point for ...
Definition: Ptxt.h:280
void setOne(long newSecretKeyID=-1)
Set powerOfS=0, powerOfX=1.
Definition: Ctxt.h:110
Pre-computation to speed many automorphism on the same ciphertext.
Definition: matmul.cpp:61
const Context & getContext() const
Definition: Ctxt.h:819
long effectiveR() const
Definition: Ctxt.h:832
void setBase(long newSecretKeyID=-1)
Set powerOfS=powerOfX=1.
Definition: Ctxt.h:92
double log_of_ratio() const
Returns log(noiseBound) - log(q)
Definition: Ctxt.h:846
Inherits from Exception and std::runtime_error.
Definition: exceptions.h:105
CtxtPart(const Context &_context, const IndexSet &s, const SKHandle &otherHandle)
Definition: Ctxt.h:219
void frobeniusAutomorph(long j)
applies the automorphism p^j using smartAutomorphism
Definition: Ctxt.cpp:2082
long getKeyID() const
Definition: Ctxt.cpp:2106
bool equalsTo(const Ctxt &other, bool comparePkeys=true) const
Definition: Ctxt.cpp:165
void multiplyBy2(const Ctxt &other1, const Ctxt &other2)
Definition: Ctxt.cpp:1700
void multByConstantCKKS(double x)
multiply by a rational number or floating point
Definition: Ctxt.h:584
NTL::Vec< long > zzX
Definition: zzX.h:24
bool empty(const IndexSet &s)
Definition: IndexSet.h:182
long getPowerOfX() const
Definition: Ctxt.h:135
Ctxt & operator+=(const Ctxt &other)
Definition: Ctxt.h:416
void setPtxtMag(const NTL::xdouble &z)
Definition: Ctxt.h:826
void DummyEncrypt(const NTL::ZZX &ptxt, double size=-1.0)
Definition: Ctxt.cpp:56
Implementing polynomials (elements in the ring R_Q) in double-CRT form.
Definition: DoubleCRT.h:76
CtxtPart(const DoubleCRT &other)
Definition: Ctxt.h:226
const IndexSet & getPrimeSet() const
Definition: Ctxt.h:821
bool isOne() const
Is powerOfS==0?
Definition: Ctxt.h:119
Ctxt & operator-=(const Ctxt &other)
Definition: Ctxt.h:422
Ctxt & operator>>=(long k)
Definition: Ctxt.h:439
bool operator!=(const CtxtPart &other) const
Definition: Ctxt.h:210
void CheckCtxt(const Ctxt &c, const char *label)
print to cerr some info about ciphertext
Definition: debugging.cpp:165
const NTL::xdouble & getNoiseBound() const
Definition: Ctxt.h:823
void multByConstant(const DoubleCRT &dcrt, double size=-1.0)
Definition: Ctxt.cpp:1832
void reLinearize(long keyIdx=0)
Definition: Ctxt.cpp:591
void totalProduct(Ctxt &out, const std::vector< Ctxt > &v)
Definition: Ctxt.cpp:2309
void assertEq(const T &a, const T &b, const std::string &message)
Definition: assertions.h:108
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:614
NTL::xdouble modSwitchAddedNoiseBound() const
Estimate the added noise.
Definition: Ctxt.cpp:2116
long getP() const
Returns p.
Definition: PAlgebra.h:165
static void equalizeRationalFactors(Ctxt &c1, Ctxt &c2)
Definition: Ctxt.cpp:1151
void xorConstant(const DoubleCRT &poly, UNUSED double size=-1.0)
Definition: Ctxt.h:635
void reducePtxtSpace(long newPtxtSpace)
Reduce plaintext space to a divisor of the original plaintext space.
Definition: Ctxt.cpp:503
void multByConstantCKKS(std::pair< long, long > num)
Definition: Ctxt.h:604
bool operator!=(const Ctxt &other) const
Definition: Ctxt.h:404
A handle, describing the secret-key element that "matches" a part, of the form s^r(X^t).
Definition: Ctxt.h:78
void complexConj()
Definition: Ctxt.cpp:2012
void divideByP()
Definition: Ctxt.cpp:1969
Ctxt & operator=(const Ctxt &other)
Definition: Ctxt.h:392
std::pair< long, long > rationalApprox(double x, long denomBound=0)
Definition: NumbTh.cpp:1709
bool isCorrect() const
Would this ciphertext be decrypted without errors?
Definition: Ctxt.h:813
void addConstant(const DoubleCRT &dcrt, double size=-1.0)
Definition: Ctxt.cpp:793
bool mul(const SKHandle &a, const SKHandle &b)
Computes the "product" of two handles.
Definition: Ctxt.h:150
void read(std::istream &str)
Definition: Ctxt.cpp:2185
A dynamic set of non-negative integers.
Definition: IndexSet.h:31
void bumpNoiseBound(double factor)
Definition: Ctxt.h:705
void extendExtractDigits(std::vector< Ctxt > &digits, const Ctxt &c, long r, long e)
Definition: extractDigits.cpp:225
One entry in a ciphertext std::vector.
Definition: Ctxt.h:204
void evalPoly(const NTL::ZZX &poly)
compute the power X,X^2,...,X^n
void clear()
Definition: Ctxt.h:788
const PubKey & getPubKey() const
Definition: Ctxt.h:820
SKHandle skHandle
The handle is a public data member.
Definition: Ctxt.h:207
bool operator==(const CtxtPart &other) const
Definition: Ctxt.cpp:155
bool inCanonicalForm(long keyID=0) const
A canonical ciphertext has (at most) handles pointing to (1,s)
Definition: Ctxt.h:801
double logOfPrimeSet() const
returns the log of the prime set
Definition: Ctxt.h:766
void multByP(long e=1)
Definition: Ctxt.h:672
void read(std::istream &str)
Definition: Ctxt.cpp:2161
IndexSet naturalPrimeSet() const
"natural size" is size before squaring
Definition: Ctxt.cpp:1599
void write(std::ostream &str) const
Definition: Ctxt.cpp:2179
void multByConstant(const Ptxt< Scheme > &ptxt)
Multiply a BGV plaintext to this Ctxt.
Definition: Ctxt.h:577
long getSecretKeyID() const
Definition: Ctxt.h:136
friend std::istream & operator>>(std::istream &str, Ctxt &ctxt)
Definition: Ctxt.cpp:2225
long getPtxtSpace() const
Definition: Ctxt.h:822
SKHandle(long newPowerOfS=0, long newPowerOfX=1, long newSecretKeyID=0)
Definition: Ctxt.h:84
bool isBase(long ofKeyID=0) const
Is powerOfS==powerOfX==1?
Definition: Ctxt.h:101
Ctxt & cleanUp()
Definition: Ctxt.cpp:654
Ctxt & operator*=(const Ctxt &other)
Definition: Ctxt.h:432
void nxorConstant(const DoubleCRT &poly, UNUSED double size=-1.0)
Definition: Ctxt.h:649
void square()
Definition: Ctxt.h:686
double logOfProduct(const IndexSet &s) const
Returns the natural logarithm of productOfPrimes(s)
Definition: Context.h:467
void power(long e)
raise ciphertext to some power
Definition: polyEval.cpp:392
void multiplyBy(const Ctxt &other)
Definition: Ctxt.cpp:1681
bool operator==(const Ctxt &other) const
Definition: Ctxt.h:403
The public key.
Definition: keys.h:47
void divideBy2()
Definition: Ctxt.cpp:1944
void innerProduct(Ctxt &result, const CtPtrs &v1, const CtPtrs &v2)
Definition: Ctxt.cpp:2318
void negate()
Definition: Ctxt.cpp:1129
friend std::istream & operator>>(std::istream &s, SKHandle &handle)
Definition: Ctxt.cpp:2191
Definition: apiAttributes.h:21
void addConstantCKKS(std::pair< long, long >)
add a rational number in the form a/b, a,b are long
Definition: Ctxt.cpp:1070
void incrementalProduct(std::vector< Ctxt > &v)
Definition: Ctxt.cpp:2274
void smartAutomorph(long k)
automorphism with re-linearization
Definition: Ctxt.cpp:2031
void clear()
Set to the empty set.
Definition: IndexSet.cpp:120
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:275
void extractBits(std::vector< Ctxt > &bits, long nBits2extract=0)
Definition: Ctxt.h:946
void read(std::istream &str)
Definition: Ctxt.cpp:34
void productOfPrimes(NTL::ZZ &p, const IndexSet &s) const
The product of all the primes in the given set.
Definition: Context.cpp:178
void bringToSet(const IndexSet &s)
make the primeSet equal to newPrimeSet, via modUpToSet and modDownToSet
Definition: Ctxt.cpp:302
void automorph(long k)
Definition: Ctxt.cpp:1991
std::ostream & operator<<(std::ostream &s, const SKHandle &handle)
Definition: Ctxt.h:190
void addCtxt(const Ctxt &other, bool negative=false)
Definition: Ctxt.cpp:1333
void modDownToSet(const IndexSet &s)
Modulus-switching down (to a smaller modulus). mod-switch down to primeSet \intersect s,...
Definition: Ctxt.cpp:326
CtxtPart(const DoubleCRT &other, const SKHandle &otherHandle)
Definition: Ctxt.h:231
bool operator!=(const SKHandle &other) const
Definition: Ctxt.h:130
@ PA_cx_tag
Definition: PAlgebra.h:300
void addConstantCKKS(double x)
Definition: Ctxt.h:539
double rawModSwitch(std::vector< NTL::ZZX > &zzParts, long toModulus) const
Special-purpose modulus-switching for bootstrapping.
Definition: Ctxt.cpp:2389
const NTL::xdouble & getRatFactor() const
Definition: Ctxt.h:824
void multLowLvl(const Ctxt &other, bool destructive=false)
Definition: Ctxt.cpp:1609
void nxorConstant(const NTL::ZZX &poly, double size=-1.0)
Definition: Ctxt.h:659
friend std::ostream & operator<<(std::ostream &str, const Ctxt &ctxt)
Definition: Ctxt.cpp:2215
void dropSmallAndSpecialPrimes()
the corresponding primeSet
Definition: Ctxt.cpp:516
The secret key.
Definition: keys.h:241
Maintaining the parameters.
Definition: Context.h:121
NTL::ZZX getPolyRepr() const
Converts the slot data in this to its single polynomial representation.
long bitCapacity() const
the capacity in bits, returned as an integer
Definition: Ctxt.h:763
void addConstant(const Ptxt< Scheme > &ptxt)
Add a BGV plaintext to this Ctxt.
Definition: Ctxt.h:531
Ctxt(const Ctxt &other)=default
void write(std::ostream &str) const
Definition: Ctxt.cpp:2139
const NTL::xdouble & getPtxtMag() const
Definition: Ctxt.h:825
bool isEmpty() const
Is this an empty ciphertext without any parts.
Definition: Ctxt.h:798
void write(std::ostream &str) const
Definition: Ctxt.cpp:41
long getPowerOfS() const
Definition: Ctxt.h:134
void xorConstant(const NTL::ZZX &poly, double size=-1.0)
Definition: Ctxt.h:644
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
A Ctxt object holds a single ciphertext.
Definition: Ctxt.h:273
CtxtPart(const Context &_context, const IndexSet &s)
Definition: Ctxt.h:214
std::istream & operator>>(std::istream &s, CtxtPart &p)
Definition: Ctxt.cpp:2206
void cube()
Definition: Ctxt.h:687
void blindCtxt(const NTL::ZZX &poly)
Add a high-noise encryption of the given constant.
Definition: Ctxt.cpp:491
IndexSet ctxtPrimes
Definition: Context.h:343
bool isCKKS() const
Definition: Ctxt.h:829
bool operator==(const SKHandle &other) const
Definition: Ctxt.h:121
double naturalSize() const
Definition: Ctxt.cpp:1589
double capacity() const
returns the "capacity" of a ciphertext, which is the log of the ratio of the modulus to the noise bou...
Definition: Ctxt.h:747
PAlgebra zMStar
The structure of Zm*.
Definition: Context.h:131
void hackPtxtSpace(long newPtxtSpace)
Definition: Ctxt.h:703