62 #include <NTL/xdouble.h>
74 long powerOfS, powerOfX, secretKeyID;
79 SKHandle(
long newPowerOfS=0,
long newPowerOfX=1,
long newSecretKeyID=0)
81 powerOfS = newPowerOfS;
82 powerOfX = newPowerOfX;
83 secretKeyID = newSecretKeyID;
91 if (newSecretKeyID >= 0) secretKeyID = newSecretKeyID;
99 return powerOfS == 1 && powerOfX == 1 &&
100 (ofKeyID<0 || secretKeyID == ofKeyID);
108 if (newSecretKeyID >= 0) secretKeyID = newSecretKeyID;
114 return powerOfS == 0;
117 bool operator==(
const SKHandle& other)
const
119 if (powerOfS==0 && other.powerOfS==0)
return true;
120 return powerOfS==other.powerOfS &&
121 powerOfX==other.powerOfX &&
122 secretKeyID==other.secretKeyID;
125 bool operator!=(
const SKHandle& other)
const {
return !(*
this==other);}
129 long getPowerOfS()
const {
return powerOfS; }
130 long getPowerOfX()
const {
return powerOfX; }
131 long getSecretKeyID()
const {
return secretKeyID; }
150 return (b.secretKeyID >= 0);
154 return (a.secretKeyID >= 0);
157 if (a.secretKeyID == -1 || b.secretKeyID == -1) {
162 if (a.secretKeyID != b.secretKeyID) {
167 if (a.powerOfX != b.powerOfX) {
172 secretKeyID = a.secretKeyID;
173 powerOfX = a.powerOfX;
174 powerOfS = a.powerOfS + b.powerOfS;
178 friend istream& operator>>(istream& s,
SKHandle& handle);
180 inline ostream& operator<<(ostream& s,
const SKHandle& handle)
182 return s <<
"[" << handle.getPowerOfS() <<
" " << handle.getPowerOfX()
183 <<
" " << handle.getSecretKeyID() <<
"]";
198 bool operator==(
const CtxtPart& other)
const;
199 bool operator!=(
const CtxtPart& other)
const {
return !(*
this==other);}
220 istream& operator>>(istream& s,
CtxtPart& p);
221 ostream& operator<<(ostream& s,
const CtxtPart& p);
255 vector<CtxtPart> parts;
263 void tensorProduct(
const Ctxt& c1,
const Ctxt& c2);
268 Ctxt& operator-=(
const CtxtPart& part) { subPart(part);
return *
this; }
269 Ctxt& operator+=(
const CtxtPart& part) { addPart(part);
return *
this; }
272 void subPart(
const CtxtPart& part,
bool matchPrimeSet=
false)
273 { subPart(part, part.
skHandle, matchPrimeSet); }
274 void addPart(
const CtxtPart& part,
bool matchPrimeSet=
false)
275 { addPart(part, part.
skHandle, matchPrimeSet); }
278 const SKHandle& handle,
bool matchPrimeSet=
false)
279 { addPart(part, handle, matchPrimeSet,
true); }
281 bool matchPrimeSet=
false,
bool negative=
false);
288 long getPartIndexByHandle(
const SKHandle& hanle)
const {
289 for (
size_t i=0; i<parts.size(); i++)
290 if (parts[i].skHandle==hanle)
return i;
298 bool operator==(
const Ctxt& other)
const {
return equalsTo(other); }
299 bool operator!=(
const Ctxt& other)
const {
return !equalsTo(other); }
302 bool equalsTo(
const Ctxt& other,
bool comparePkeys=
true)
const;
307 noiseVar = to_xdouble(0.0);
317 Ctxt& operator+=(
const Ctxt& other) { addCtxt(other);
return *
this; }
318 Ctxt& operator-=(
const Ctxt& other) { addCtxt(other,
true);
return *
this; }
319 void addCtxt(
const Ctxt& other,
bool negative=
false);
321 Ctxt& operator*=(
const Ctxt& other);
322 void automorph(
long k);
323 Ctxt& operator>>=(
long k) { automorph(k);
return *
this; }
333 void addConstant(
const DoubleCRT& dcrt,
double size=0.0);
334 void addConstant(
const ZZX& poly,
double size=0.0)
335 { addConstant(
DoubleCRT(poly,context,primeSet),size); }
339 void multByConstant(
const DoubleCRT& dcrt,
double size=0.0);
340 void multByConstant(
const ZZX& poly,
double size=0.0)
341 { multByConstant(
DoubleCRT(poly,context,primeSet),size); }
344 void multiplyBy(
const Ctxt& other);
345 void multiplyBy2(
const Ctxt& other1,
const Ctxt& other2);
346 void square() { multiplyBy(*
this); }
347 void cube() { multiplyBy2(*
this, *
this); }
352 void reLinearize(
long keyIdx=0);
379 return (parts.size()==2 &&
380 parts[0].skHandle.isOne() && parts[1].skHandle.isBase(keyID));
386 return (to_xdouble(q) > sqrt(noiseVar)*2);
388 const FHEcontext& getContext()
const {
return context; }
389 const FHEPubKey& getPubKey()
const {
return pubKey; }
390 const IndexSet& getPrimeSet()
const {
return primeSet; }
391 const xdouble& getNoiseVar()
const {
return noiseVar; }
392 const long getPtxtSpace()
const {
return ptxtSpace;}
393 const long getKeyID()
const;
404 {
return (log(getNoiseVar())/2 - context.
logOfProduct(getPrimeSet()));}
406 friend istream& operator>>(istream& str,
Ctxt& ctxt);
407 friend ostream& operator<<(ostream& str,
const Ctxt& ctxt);
416 void CheckCtxt(
const Ctxt& c,
const char* label);
418 #endif // ifndef _Ctxt_H_