Skip to content

Commit 57dfb39

Browse files
authored
Deprecated Functions
This is the second part of the deprecated functions work. Added the C++ [[deprecated]] tag to deprecated functions with recommendations of alternative functions. Use of these deprecated APIs should now produce compiler warnings.
2 parents 86098b5 + bca90d8 commit 57dfb39

File tree

7 files changed

+171
-133
lines changed

7 files changed

+171
-133
lines changed

examples/BGV_packed_arithmetic/BGV_packed_arithmetic.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ int main(int argc, char* argv[])
164164
// And multiply by constants
165165
// [1] [1] [1] ... [1] [1]
166166
// -> [1*1] [1*1] [1*1] ... [1*1] [1*1] = [1] [1] [1] ... [1] [1]
167-
ctxt *= NTL::ZZX(1l);
167+
ctxt *= 1l;
168168
// Plaintext version
169-
ptxt *= NTL::ZZX(1l);
169+
ptxt *= 1l;
170170

171171
// We can also perform ciphertext-plaintext operations
172172
// ctxt = [1] [1] [1] ... [1] [1], ptxt = [1] [1] [1] ... [1] [1]

include/helib/Ctxt.h

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,8 @@ class Ctxt
611611
* `EncodedPtxt`-based API.\n
612612
* Please use `Ctxt::operator*=(const EncodedPtxt& ptxt)` instead.
613613
**/
614-
// [[deprecated]]
614+
[[deprecated(
615+
"Please use Ctxt::operator*=(const EncodedPtxt& ptxt) instead.")]]
615616
Ctxt& operator*=(const NTL::ZZX& poly);
616617

617618
//! Add a constant polynomial.
@@ -655,17 +656,21 @@ class Ctxt
655656
* @deprecated This function is deprecated in favor of a newer API.
656657
* Please use `Ctxt::operator+=(double ptxt)` instead.
657658
**/
658-
// [[deprecated]]
659+
[[deprecated("Please use Ctxt::operator+=(double ptxt) instead.")]]
659660
void addConstantCKKS(std::pair</*numerator=*/long, /*denominator=*/long>);
660661
/**
661662
* @deprecated This function is deprecated in favor of a newer API.
662663
* Please use `Ctxt::operator+=(double ptxt)` instead.
663664
**/
664-
// [[deprecated]]
665+
[[deprecated("Please use Ctxt::operator+=(double ptxt) instead.")]]
665666
void addConstantCKKS(double x)
666667
{ // FIXME: not enough precision when x is large
667-
addConstantCKKS(
668-
rationalApprox(x, /*denomBound=*/1L << getContext().getAlMod().getR()));
668+
// This function is deprecated.
669+
// addConstantCKKS(
670+
// rationalApprox(x, /*denomBound=*/1L << getContext().getAlMod().getR()));
671+
auto p =
672+
rationalApprox(x, /*denomBound=*/1L << getContext().getAlMod().getR());
673+
*this += double(p.first) / p.second;
669674
}
670675

671676
/**
@@ -681,7 +686,8 @@ class Ctxt
681686
* @deprecated This function is deprecated in favor of a newer API.
682687
* Please use `Ctxt::operator+=(const EncodedPtxt& ptxt)` instead.
683688
**/
684-
// [[deprecated]]
689+
[[deprecated(
690+
"Please use Ctxt::operator+=(const EncodedPtxt& ptxt) instead.")]]
685691
void addConstantCKKS(const NTL::ZZX& poly,
686692
NTL::xdouble size = NTL::xdouble(-1.0),
687693
NTL::xdouble factor = NTL::xdouble(-1.0));
@@ -690,7 +696,7 @@ class Ctxt
690696
* @deprecated This function is deprecated in favor of a newer API.
691697
* Please use `Ctxt::operator+=(const PtxtArray& ptxt)` instead.
692698
**/
693-
// [[deprecated]]
699+
[[deprecated("Please use Ctxt::operator+=(const PtxtArray& ptxt) instead.")]]
694700
void addConstantCKKS(const std::vector<std::complex<double>>& ptxt);
695701

696702
/**
@@ -699,13 +705,14 @@ class Ctxt
699705
* @deprecated This function is deprecated in favor of a newer API.
700706
* Please use `Ctxt::operator+=(const PtxtArray& ptxt)` instead.
701707
**/
702-
// [[deprecated]]
708+
[[deprecated(
709+
"Please use Ctxt::operator+=(const Ptxt<Scheme>& ptxt) instead.")]]
703710
void addConstantCKKS(const Ptxt<CKKS>& ptxt);
704711
/**
705712
* @deprecated This function is deprecated in favor of a newer API.
706713
* Please use `Ctxt::operator+=(const NTL::ZZ& ptxt)` instead.
707714
**/
708-
// [[deprecated]]
715+
[[deprecated("Please use Ctxt::operator+=(const NTL::ZZ& ptxt) instead.")]]
709716
void addConstantCKKS(const NTL::ZZ& c);
710717

711718
//! Multiply-by-constant.
@@ -1094,7 +1101,7 @@ class Ctxt
10941101
* @deprecated This function is deprecated in favor of a newer API.
10951102
* Please use `Ctxt::operator*=(double ptxt)` instead.
10961103
**/
1097-
// [[deprecated]]
1104+
[[deprecated("Please use Ctxt::operator*=(double ptxt) instead.")]]
10981105
void multByConstantCKKS(double x)
10991106
{
11001107
if (this->isEmpty())
@@ -1119,10 +1126,12 @@ class Ctxt
11191126
* @deprecated This function is deprecated in favor of a newer API.
11201127
* Please use `Ctxt::operator*=(double ptxt)` instead.
11211128
**/
1122-
// [[deprecated]]
1129+
[[deprecated("Please use Ctxt::operator*=(double ptxt) instead.")]]
11231130
void multByConstantCKKS(std::pair<long, long> num) // rational number
11241131
{
1125-
multByConstantCKKS(double(num.first) / num.second);
1132+
// This function is deprecated.
1133+
// multByConstantCKKS(double(num.first) / num.second);
1134+
*this *= double(num.first) / num.second;
11261135
}
11271136

11281137
/**
@@ -1139,7 +1148,8 @@ class Ctxt
11391148
* @deprecated This function is deprecated in favor of a newer API.
11401149
* Please use `Ctxt::operator*=(const EncodedPtxt& ptxt)` instead.
11411150
**/
1142-
// [[deprecated]]
1151+
[[deprecated(
1152+
"Please use Ctxt::operator*=(const EncodedPtxt& ptxt) instead.")]]
11431153
void multByConstantCKKS(const NTL::ZZX& poly,
11441154
NTL::xdouble size = NTL::xdouble(-1.0),
11451155
NTL::xdouble factor = NTL::xdouble(-1.0),
@@ -1157,13 +1167,15 @@ class Ctxt
11571167
* @deprecated This function is deprecated in favor of a newer API.
11581168
* Please use `Ctxt::operator*=(const PtxtArray& ptxt)` instead.
11591169
**/
1160-
// [[deprecated]]
1170+
[[deprecated(
1171+
"Please use Ctxt::operator*=(const Ptxt<Scheme>& ptxt) instead.")]]
11611172
void multByConstantCKKS(const Ptxt<CKKS>& ptxt);
11621173
/**
11631174
* @deprecated This function is deprecated in favor of a newer API.
11641175
* Please use `Ctxt::operator*=(const PtxtArray& ptxt)` instead.
11651176
**/
1166-
// [[deprecated]]
1177+
[[deprecated(
1178+
"Please use Ctxt::operator*=(const PtxtArray& ptxt) instead.")]]
11671179
void multByConstantCKKS(const std::vector<std::complex<double>>& ptxt);
11681180

11691181
//! Convenience method: XOR and nXOR with arbitrary plaintext space:
@@ -1394,7 +1406,7 @@ class Ctxt
13941406
* @brief Returns log(noiseBound) - log(q)
13951407
* @deprecated This is deprecated. Please use `Ctxt::capacity()` instead.
13961408
**/
1397-
// [[deprecated]] // use capacity()
1409+
[[deprecated("Please use Ctxt::capacity() instead.")]]
13981410
double log_of_ratio() const
13991411
{
14001412
double logNoise =

src/Ctxt.cpp

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,29 +1060,34 @@ void Ctxt::addConstantCKKS(const NTL::ZZX& poly,
10601060

10611061
Ctxt& Ctxt::operator*=(const NTL::ZZX& poly)
10621062
{
1063-
if (isCKKS())
1064-
multByConstantCKKS(poly);
1065-
else
1066-
multByConstant(poly);
1067-
return *this;
1063+
// NOTE: This function has been depcreated.
1064+
// if (isCKKS())
1065+
// multByConstantCKKS(poly);
1066+
// else
1067+
// multByConstant(poly);
1068+
// return *this;
1069+
PtxtArray pa(getContext(), poly);
1070+
return *this *= pa;
10681071
}
10691072

10701073
void Ctxt::addConstantCKKS(const std::vector<std::complex<double>>& other)
10711074
{
10721075
// VJS-FIXME: this routine has a number of issues and should
10731076
// be deprecated in favor of the new EncodedPtxt-based routines
10741077

1075-
NTL::ZZX poly;
1076-
double factor = getContext().getEA().getCx().encode(poly, other);
1077-
// VJS-NOTE: maybe this encdoing routine should also return
1078-
// the rounding error...we kind of need this value
1079-
1080-
double size = Norm(other);
1081-
1082-
if (size == 0.0)
1083-
return;
1084-
1085-
addConstantCKKS(poly, NTL::xdouble{size}, NTL::xdouble{factor});
1078+
// NTL::ZZX poly;
1079+
// double factor = getContext().getEA().getCx().encode(poly, other);
1080+
// // VJS-NOTE: maybe this encdoing routine should also return
1081+
// // the rounding error...we kind of need this value
1082+
//
1083+
// double size = Norm(other);
1084+
//
1085+
// if (size == 0.0)
1086+
// return;
1087+
//
1088+
// addConstantCKKS(poly, NTL::xdouble{size}, NTL::xdouble{factor});
1089+
PtxtArray pa(getContext(), other);
1090+
*this += pa;
10861091
}
10871092

10881093
void Ctxt::addConstantCKKS(const NTL::ZZ& c)
@@ -1135,7 +1140,9 @@ void Ctxt::addConstantCKKS(std::pair<long, long> num)
11351140

11361141
void Ctxt::addConstantCKKS(const Ptxt<CKKS>& ptxt)
11371142
{
1138-
addConstantCKKS(ptxt.getSlotRepr());
1143+
// This function has been deprecated.
1144+
// addConstantCKKS(ptxt.getSlotRepr());
1145+
*this += ptxt;
11391146
}
11401147

11411148
// Add at least one prime to the primeSet of c
@@ -1866,17 +1873,19 @@ void Ctxt::multByConstantCKKS(const std::vector<std::complex<double>>& other)
18661873
// be deprecated in favor of the new EncodedPtxt-based routines
18671874

18681875
// NOTE: some replicated logic here and in addConstantCKKS...
1869-
NTL::ZZX poly;
1870-
double factor = getContext().getEA().getCx().encode(poly, other);
1871-
// VJS-NOTE: why does encode with ZZX not require a size arg?
1872-
1873-
double size = Norm(other);
1874-
1875-
// VJS-NOTE: if size==0 we should just do thus->clear()
1876-
if (size == 0.0)
1877-
size = 1.0;
1878-
1879-
multByConstantCKKS(poly, NTL::xdouble{size}, NTL::xdouble{factor});
1876+
// NTL::ZZX poly;
1877+
// double factor = getContext().getEA().getCx().encode(poly, other);
1878+
// // VJS-NOTE: why does encode with ZZX not require a size arg?
1879+
//
1880+
// double size = Norm(other);
1881+
//
1882+
// // VJS-NOTE: if size==0 we should just do thus->clear()
1883+
// if (size == 0.0)
1884+
// size = 1.0;
1885+
//
1886+
// multByConstantCKKS(poly, NTL::xdouble{size}, NTL::xdouble{factor});
1887+
PtxtArray pa(getContext(), other);
1888+
*this *= pa;
18801889
}
18811890

18821891
void Ctxt::multByConstantCKKS(const DoubleCRT& dcrt,
@@ -1916,7 +1925,9 @@ void Ctxt::multByConstantCKKS(const DoubleCRT& dcrt,
19161925

19171926
void Ctxt::multByConstantCKKS(const Ptxt<CKKS>& ptxt)
19181927
{
1919-
multByConstantCKKS(ptxt.getSlotRepr());
1928+
// This function has been deprecated.
1929+
// multByConstantCKKS(ptxt.getSlotRepr());
1930+
*this *= ptxt;
19201931
}
19211932

19221933
//=========== new multByConstant interface =========

src/EaCx.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,9 @@ void EncryptedArrayCx::random(std::vector<cx_double>& array, double rad) const
419419
void EncryptedArrayCx::extractRealPart(Ctxt& c) const
420420
{
421421
Ctxt tmp = c;
422-
tmp.complexConj(); // the complex conjugate of c
423-
c += tmp; // c + conj(c) = 2*real(c)
424-
c.multByConstantCKKS(0.5); // divide by two
422+
tmp.complexConj(); // the complex conjugate of c
423+
c += tmp; // c + conj(c) = 2*real(c)
424+
c *= 0.5; // divide by two
425425
}
426426

427427
// Note: If called with dcrt==nullptr, it will perform FFT's when
@@ -446,7 +446,7 @@ void EncryptedArrayCx::extractImPart(Ctxt& c, DoubleCRT* iDcrtPtr) const
446446
iDcrtPtr = &tmpDcrt;
447447
}
448448
c.multByConstantCKKS(*iDcrtPtr); // multiply by i
449-
c.multByConstantCKKS(0.5); // divide by two
449+
c *= 0.5; // divide by two
450450
}
451451

452452
void EncryptedArrayCx::buildLinPolyCoeffs(std::vector<zzX>& C,

tests/TestCKKS.cpp

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -225,16 +225,17 @@ TEST_P(TestCKKS, addingPolyConstantToCiphertextWorks)
225225
{
226226
helib::Ctxt c1(publicKey);
227227
std::vector<std::complex<double>> vd1, vd2, vd3;
228-
NTL::ZZX poly;
228+
helib::EncodedPtxt eptxt; // Containing holding a polynomial i.e. NTL::ZZX
229229
NTL::xdouble rf, pm;
230230

231231
ea.random(vd1);
232232
ea.random(vd2);
233233
ea.encrypt(c1, publicKey, vd1);
234234
rf = c1.getRatFactor();
235235
pm = c1.getPtxtMag();
236-
ea.encode(poly, vd2, /*size=*/1.0);
237-
c1.addConstantCKKS(poly);
236+
helib::PtxtArray pa(context, vd2);
237+
pa.encode(eptxt, /*mag=*/1.0); // Encode polynomial
238+
c1 += eptxt;
238239
ea.decrypt(c1, secretKey, vd3);
239240

240241
add(vd1, vd2);
@@ -250,7 +251,7 @@ TEST_P(TestCKKS, addingNegatedPolyConstantToCiphertextWorks)
250251
{
251252
helib::Ctxt c1(publicKey);
252253
std::vector<std::complex<double>> vd1, vd2, vd3;
253-
NTL::ZZX poly;
254+
helib::EncodedPtxt eptxt; // Containing holding a polynomial i.e. NTL::ZZX
254255
NTL::xdouble rf, pm;
255256

256257
ea.random(vd1);
@@ -259,8 +260,9 @@ TEST_P(TestCKKS, addingNegatedPolyConstantToCiphertextWorks)
259260
ea.encrypt(c1, publicKey, vd1);
260261
rf = c1.getRatFactor();
261262
pm = c1.getPtxtMag();
262-
ea.encode(poly, vd2, /*size=*/1.0);
263-
c1.addConstantCKKS(poly);
263+
helib::PtxtArray pa(context, vd2);
264+
pa.encode(eptxt, /*mag=*/1.0); // Encode polynomial
265+
c1 += eptxt;
264266
ea.decrypt(c1, secretKey, vd3);
265267

266268
add(vd1, vd2);
@@ -277,16 +279,17 @@ TEST_P(TestCKKS, multiplyingPolyConstantToCiphertextWorks)
277279
{
278280
helib::Ctxt c1(publicKey);
279281
std::vector<std::complex<double>> vd1, vd2, vd3;
280-
NTL::ZZX poly;
282+
helib::EncodedPtxt eptxt; // Container holding a polynomial i.e. NTL::ZZX
281283
NTL::xdouble rf, pm;
282284

283285
ea.random(vd1);
284286
ea.random(vd2);
285287
ea.encrypt(c1, publicKey, vd1);
286288
rf = c1.getRatFactor();
287289
pm = c1.getPtxtMag();
288-
ea.encode(poly, vd2, /*size=*/1.0);
289-
c1.multByConstantCKKS(poly);
290+
helib::PtxtArray pa(context, vd2);
291+
pa.encode(eptxt, /*mag*/ 1.0); // Encode polynomial
292+
c1 *= eptxt;
290293
ea.decrypt(c1, secretKey, vd3);
291294

292295
mul(vd1, vd2);
@@ -313,7 +316,7 @@ TEST_P(TestCKKS, addingDoubleToCiphertextWorks)
313316
ea.encrypt(c1, publicKey, vd1);
314317
rf = c1.getRatFactor();
315318
pm = c1.getPtxtMag();
316-
c1.addConstantCKKS(vd[0]);
319+
c1 += vd[0]; // Same as depcrecated c1.addConstantCKKS(vd[0]);
317320
ea.decrypt(c1, secretKey, vd2);
318321

319322
add(vd1, vd[0]);
@@ -338,8 +341,7 @@ TEST_P(TestCKKS, multiplyingDoubleToCiphertextWorks)
338341
ea.encrypt(c1, publicKey, vd1);
339342
rf = c1.getRatFactor();
340343
pm = c1.getPtxtMag();
341-
c1.multByConstantCKKS(vd[0]);
342-
// c1 *= vd[0];
344+
c1 *= vd[0]; // Same as deprecated c1.multByConstantCKKS(vd[0]);
343345
ea.decrypt(c1, secretKey, vd2);
344346

345347
mul(vd1, vd[0]);
@@ -564,7 +566,9 @@ TEST_P(
564566
ea.random(vd2);
565567
ea.encrypt(c1, publicKey, vd1);
566568
ea.encrypt(c2, publicKey, vd2);
567-
c1.multByConstantCKKS(std::make_pair<long, long>(-5, 2));
569+
// Same as deprecated function
570+
// c1.multByConstantCKKS(std::make_pair<long, long>(-5, 2));
571+
c1 *= -5.0 / 2.0;
568572
c1 += c2;
569573
ea.decrypt(c1, secretKey, vd4);
570574

@@ -585,7 +589,7 @@ TEST_P(TestCKKS, multiplyingByConstantNeverProducesNegativeRatFactor)
585589

586590
ea.random(vd1);
587591
ea.encrypt(c1, publicKey, vd1);
588-
c1.multByConstantCKKS(-2.0);
592+
c1 *= -2.0; // Same as deprecated c1.multByConstantCKKS(-2.0);
589593

590594
EXPECT_GT(c1.getRatFactor(), 0);
591595
}
@@ -599,7 +603,8 @@ TEST_P(TestCKKS, multiplyBySmallNegativeConstantFollowedByOperationWorks)
599603
ea.random(vd2);
600604
ea.encrypt(c1, publicKey, vd1);
601605
ea.encrypt(c2, publicKey, vd2);
602-
c1.multByConstantCKKS(-2.0 / 10.0);
606+
// Same as deprecated c1.multByConstantCKKS(-2.0 / 10.0);
607+
c1 *= (-2.0 / 10.0);
603608
c1 -= c2;
604609
ea.decrypt(c1, secretKey, vd3);
605610

0 commit comments

Comments
 (0)