forked from homenc/HElib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCtxt.h
1610 lines (1393 loc) · 50.8 KB
/
Ctxt.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2012-2021 IBM Corp.
* This program is Licensed under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. See accompanying LICENSE file.
*/
#ifndef HELIB_CTXT_H
#define HELIB_CTXT_H
/**
* @file Ctxt.h
* @brief Declarations of a BGV-type ciphertext and key-switching matrices
*
* A ciphertext is a std::vector of "ciphertext parts", each part consists of
* a polynomial (element of polynomial ring R_Q) and a "handle" describing
* the secret-key polynomial that this part multiplies during decryption.
* For example:
* + A "canonical" ciphertext has two parts, the first part multiplies 1
* and the second multiplies the "base" secret key s.
* + When you multiply two canonical ciphertexts you get a 3-part
* ciphertext, with parts corresponding to 1, s, and s^2.
* + When you apply automorphism X->X^t to a generic ciphertext, then
* - the part corresponding to 1 still remains wrt 1
* - every other part corresponding to some s' will now be corresponding
* to the polynomial s'(X^t) mod Phi_m(X)
*
* This type of representation lets you in principle add ciphertexts that
* are defined with respect to different keys:
* + For parts of the two ciphertexts that point to the same secret-key
* polynomial, you just add the two Double-CRT polynomials
* + Parts in one ciphertext that do not have counter-part in the other
* ciphertext will just be included in the result intact.
* For example, you have the ciphertexts
* C1 = (a relative to 1, b relative to s)
* C2 = (u relative to 1, v relative to s(X^3))
* Then their sum will be
* C1+C2 = (a+u relative to 1, b relative to s, v relative to s(X^3))
*
* Similarly, in principle you can also multiply arbitrary ciphertexts, even
* ones that are defined with respect to different keys, and the result will
* be defined with respect to the tensor product of the two keys.
*
* The current implementation is more restrictive, however. It requires that
* a ciphertext has one part wrt 1, that for every r>=1 there is at most one
* part wrt to s^r(X^t) (for some t), and that the r's are consecutive. For
* example you cannot have parts wrt (1,s,s^3) without having a part wrt s^2.
*
* It follows that you can only add/multiply ciphertexts if one of the two
* lists of handles is a prefix of the other. For example, one can add a
* ciphertext wrt (1,s(X^2)) to another wrt (1,s(X^2),s^2(X^2)), but not
* to another ciphertext wrt (1,s).
**/
#include <helib/DoubleCRT.h>
#include <helib/EncodedPtxt.h>
#include <helib/apiAttributes.h>
#include <cfloat> // DBL_MAX
namespace helib {
struct CKKS;
struct BGV;
template <typename Scheme>
class Ptxt;
class KeySwitch;
class PubKey;
class SecKey;
class PtxtArray;
/**
* @class SKHandle
* @brief A handle, describing the secret-key element that "matches" a part, of
* the form s^r(X^t).
**/
class SKHandle
{
long powerOfS, powerOfX, secretKeyID;
public:
friend class Ctxt;
SKHandle(long newPowerOfS = 0, long newPowerOfX = 1, long newSecretKeyID = 0)
{
powerOfS = newPowerOfS;
powerOfX = newPowerOfX;
secretKeyID = newSecretKeyID;
}
//! @brief Set powerOfS=powerOfX=1
void setBase(long newSecretKeyID = -1)
{
powerOfS = 1;
powerOfX = 1;
if (newSecretKeyID >= 0)
secretKeyID = newSecretKeyID;
}
//! @brief Is powerOfS==powerOfX==1?
bool isBase(long ofKeyID = 0) const
{
// If ofKeyID<0, only check that this is base of some key,
// otherwise check that this is base of the given key
return powerOfS == 1 && powerOfX == 1 &&
(ofKeyID < 0 || secretKeyID == ofKeyID);
}
//! @brief Set powerOfS=0, powerOfX=1
void setOne(long newSecretKeyID = -1)
{
powerOfS = 0;
powerOfX = 1;
if (newSecretKeyID >= 0)
secretKeyID = newSecretKeyID;
}
//! @brief Is powerOfS==0?
bool isOne() const { return powerOfS == 0; }
bool operator==(const SKHandle& other) const
{
if (powerOfS == 0 && other.powerOfS == 0)
return true;
else
return powerOfS == other.powerOfS && powerOfX == other.powerOfX &&
secretKeyID == other.secretKeyID;
}
bool operator!=(const SKHandle& other) const { return !(*this == other); }
/* access functions */
long getPowerOfS() const { return powerOfS; }
long getPowerOfX() const { return powerOfX; }
long getSecretKeyID() const { return secretKeyID; }
/**
* @brief Computes the "product" of two handles.
*
* The key-ID's and powers of X must match, else an error state arises,
* which is represented using a key-ID of -1 and returning false. Also,
* note that inputs may alias outputs.
*
* To determine if the resulting handle can be re-linearized using
* some key-switching matrices from the public key, use the method
* pubKey.haveKeySWmatrix(handle,handle.secretKeyID), from the class
* PubKey in keys.h
*/
bool mul(const SKHandle& a, const SKHandle& b)
{
// If either of the inputs is one, the output equals to the other input
if (a.isOne()) {
*this = b;
return (b.secretKeyID >= 0);
}
if (b.isOne()) {
*this = a;
return (a.secretKeyID >= 0);
}
if (a.secretKeyID == -1 || b.secretKeyID == -1) {
secretKeyID = -1; // -1 will be used to indicate an "error state"
return false;
}
if (a.secretKeyID != b.secretKeyID) {
secretKeyID = -1;
return false;
}
if (a.powerOfX != b.powerOfX) {
secretKeyID = -1;
return false;
}
secretKeyID = a.secretKeyID;
powerOfX = a.powerOfX;
powerOfS = a.powerOfS + b.powerOfS;
return true;
}
friend std::istream& operator>>(std::istream& s, SKHandle& handle);
// Raw IO
/**
* @brief Write out the `SKHandle` object in binary format.
* @param str Output `std::ostream`.
**/
void writeTo(std::ostream& str) const;
/**
* @brief Read from the stream the serialized `SKHandle` object in binary
* format.
* @param str Input `std::istream`.
* @return The deserialized `SKHandle` object.
**/
static SKHandle readFrom(std::istream& str);
/**
* @brief Write out the secret key handle (`SKHandle`) object to the output
* stream using JSON format.
* @param str Output `std::ostream`.
**/
void writeToJSON(std::ostream& str) const;
/**
* @brief Write out the secret key handle (`SKHandle`) object to a
* `JsonWrapper`.
* @return The `JsonWrapper`.
**/
JsonWrapper writeToJSON() const;
/**
* @brief Read from the stream the serialized secret key handle (`SKHandle`)
* object using JSON format.
* @param str Input `std::istream`.
* @return The deserialized `SKHandle` object.
**/
static SKHandle readFromJSON(std::istream& str);
/**
* @brief Read from the `JsonWrapper` the serialized secret key handle
* (`SKHandle`) object.
* @param j The `JsonWrapper` containing the serialized `SKHandle` object.
* @return The deserialized `SKHandle` object.
**/
static SKHandle readFromJSON(const JsonWrapper& j);
/**
* @brief Read from the stream the serialized secret key handle (`SKHandle`)
* object using JSON format.
* @param str Input `std::istream`.
**/
void readJSON(std::istream& str);
/**
* @brief Read from the `JsonWrapper` the serialized secret key handle
* (`SKHandle`) object.
* @param j The `JsonWrapper` containing the serialized `SKHandle` object.
**/
void readJSON(const JsonWrapper& j);
};
inline std::ostream& operator<<(std::ostream& s, const SKHandle& handle)
{
handle.writeToJSON(s);
return s;
}
/**
* @class CtxtPart
* @brief One entry in a ciphertext std::vector
*
* A ciphertext part consists of a polynomial (element of the ring R_Q)
* and a handle to the corresponding secret-key polynomial.
**/
class CtxtPart : public DoubleCRT
{
public:
//! @brief The handle is a public data member
SKHandle skHandle; // The secret-key polynomial corresponding to this part
bool operator==(const CtxtPart& other) const;
bool operator!=(const CtxtPart& other) const { return !(*this == other); }
// Constructors
CtxtPart(const Context& _context, const IndexSet& s) : DoubleCRT(_context, s)
{
skHandle.setOne();
}
CtxtPart(const Context& _context,
const IndexSet& s,
const SKHandle& otherHandle) :
DoubleCRT(_context, s), skHandle(otherHandle)
{}
// Copy constructors from the base class
explicit CtxtPart(const DoubleCRT& other) : DoubleCRT(other)
{
skHandle.setOne();
}
CtxtPart(const DoubleCRT& other, const SKHandle& otherHandle) :
DoubleCRT(other), skHandle(otherHandle)
{}
/**
* @brief Write out the `CtxtPart` object in binary format.
* @param str Output `std::ostream`.
**/
void writeTo(std::ostream& str) const;
/**
* @brief Read from the stream the serialized `CtxtPart` object in binary
* format.
* @param str Input `std::istream`.
* @return The deserialized `CtxtPart` object.
**/
static CtxtPart readFrom(std::istream& str, const Context& context);
/**
* @brief In-place read from the stream the serialized `CtxtPart` object in
* binary format.
* @param str Input `std::istream`.
**/
void read(std::istream& str);
/**
* @brief Write out the ciphertext part (`CtxtPart`) object to the output
* stream using JSON format.
* @param str Output `std::ostream`.
**/
void writeToJSON(std::ostream& str) const;
/**
* @brief Write out the ciphertext part (`CtxtPart`) object to a
* `JsonWrapper`.
* @return The `JsonWrapper`.
**/
JsonWrapper writeToJSON() const;
/**
* @brief Read from the stream the serialized ciphertext part (`CtxtPart`)
* object using JSON format.
* @param str Input `std::istream`.
* @param context The `Context` to be used.
* @return The deserialized `CtxtPart` object.
**/
static CtxtPart readFromJSON(std::istream& str, const Context& context);
/**
* @brief Read from the `JsonWrapper` the serialized ciphertext part
*(`CtxtPart`) object.
* @param j The `JsonWrapper` containing the serialized `CtxtPart` object.
* @param context The `Context` to be used.
* @return The deserialized `CtxtPart` object.
**/
static CtxtPart readFromJSON(const JsonWrapper& j, const Context& context);
/**
* @brief Read from the stream the serialized ciphertext part (`CtxtPart`)
* object using JSON format.
* @param str Input `std::istream`.
**/
void readJSON(std::istream& str);
/**
* @brief Read from the `JsonWrapper` the serialized ciphertext part
*(`CtxtPart`) object.
* @param j The `JsonWrapper` containing the serialized `SKHandle` object.
**/
void readJSON(const JsonWrapper& j);
};
std::istream& operator>>(std::istream& s, CtxtPart& p);
std::ostream& operator<<(std::ostream& s, const CtxtPart& p);
//! \cond FALSE (make doxygen ignore this code)
struct ZeroCtxtLike_type
{}; // used to select a constructor
const ZeroCtxtLike_type ZeroCtxtLike = ZeroCtxtLike_type();
//! \endcond
/**
* @class Ctxt
* @brief A Ctxt object holds a single ciphertext
*
* The class Ctxt includes a std::vector<CtxtPart>: For a Ctxt c, c[i] is the
* i'th ciphertext part, which can be used also as a DoubleCRT object (since
* CtxtPart is derived from DoubleCRT). By convention, c[0], the first CtxtPart
* object in the std::vector, has skHndl that points to 1 (i.e., it is just
* added in upon decryption, without being multiplied by anything). We
* maintain the invariance that all the parts of a ciphertext are defined
* relative to the same set of primes.
*
* A ciphertext contains also pointers to the general parameters of this FHE
* instance and the public key, and a high-probability bound on the noise
* magnitude (kept in the noiseBound data member). The noise bound is a bound
* on the l-infinity norm of the canonical embedding of the noise polynomial,
* namely its evaluation in roots of the ring polynomial (which are the complex
* primitive roots of unity). The noise bound is added on addition, multiplied
* on multiplications, remains unchanged for automorphism, and is roughly
* scaled down by mod-switching with some added factor, and similarly scaled up
* by key-switching with some added factor.
*
**/
class Ctxt
{
friend class PubKey;
friend class SecKey;
friend class BasicAutomorphPrecon;
const Context& context; // points to the parameters of this FHE instance
const PubKey& pubKey; // points to the public encryption key;
std::vector<CtxtPart> parts; // the ciphertext parts
IndexSet primeSet; // the primes relative to which the parts are defined
long ptxtSpace; // plaintext space for this ciphertext (either p or p^r)
// a high-probability bound on the noise magnitude
NTL::xdouble noiseBound;
long intFactor; // an integer factor to divide by on decryption (for BGV)
NTL::xdouble ratFactor; // rational factor to divide on decryption (for CKKS)
NTL::xdouble ptxtMag; // bound on the plaintext size (for CKKS)
// Create a tensor product of c1,c2. It is assumed that *this,c1,c2
// are defined relative to the same set of primes and plaintext space,
// and that *this DOES NOT point to the same object as c1,c2
void tensorProduct(const Ctxt& c1, const Ctxt& c2);
// Add/subtract a ciphertext part to/from a ciphertext. These are private
// methods, they cannot update the noiseBound so they must be called
// from a procedure that will eventually update that estimate.
Ctxt& operator-=(const CtxtPart& part)
{
subPart(part);
return *this;
}
Ctxt& operator+=(const CtxtPart& part)
{
addPart(part);
return *this;
}
// NOTE: the matchPrimeSets business in the following routines
// is DEPRECATED. The requirement is that the prime set of part
// must contain the prime set of *this.
// If not, an exception is raised.
// Also, if matchPrimeSets == true and the prime set of *this does
// not contain the prime set of part, an exception is also raised.
// Also, note that the prime set of *this will not change.
// Procedural versions with additional parameter
void subPart(const CtxtPart& part, bool matchPrimeSet = false)
{
subPart(part, part.skHandle, matchPrimeSet);
}
void addPart(const CtxtPart& part, bool matchPrimeSet = false)
{
addPart(part, part.skHandle, matchPrimeSet);
}
void subPart(const DoubleCRT& part,
const SKHandle& handle,
bool matchPrimeSet = false)
{
addPart(part, handle, matchPrimeSet, true);
}
void addPart(const DoubleCRT& part,
const SKHandle& handle,
bool matchPrimeSet = false,
bool negative = false);
// convenient to avoid dealing with the deprecated matchPrimeSet
// parameter
void addSignedPart(const DoubleCRT& part,
const SKHandle& handle,
bool negative = false)
{
addPart(part, handle, false, negative);
}
// Takes as arguments a ciphertext-part p relative to s' and a key-switching
// matrix W = W[s'->s], use W to switch p relative to (1,s), and add the
// result to *this.
void keySwitchPart(const CtxtPart& p, const KeySwitch& W);
// internal procedure used in key-switching
void keySwitchDigits(const KeySwitch& W, std::vector<DoubleCRT>& digits);
long getPartIndexByHandle(const SKHandle& handle) const
{
for (size_t i = 0; i < parts.size(); i++)
if (parts[i].skHandle == handle)
return i;
return -1;
}
// Sanity-check: Check that prime-set is "valid", i.e. that it
// contains either all the special primes or none of them
bool verifyPrimeSet() const;
// A private assignment method that does not check equality of context or
// public key, this is needed when we copy the pubEncrKey member between
// different public keys.
Ctxt& privateAssign(const Ctxt& other);
// explicitly multiply intFactor by e, which should be
// in the interval [0, ptxtSpace)
void mulIntFactor(long e);
public:
/**
* @brief Class label to be added to JSON serialization as object type
* information.
*/
static constexpr std::string_view typeName = "Ctxt";
// Default copy-constructor
Ctxt(const Ctxt& other) = default;
// VJS-FIXME: this was really a messy design choice to not
// have ciphertext constructors that specify prime sets.
// The default value of ctxtPrimes is kind of pointless.
//__attribute__((deprecated))
explicit Ctxt(const PubKey& newPubKey, long newPtxtSpace = 0); // constructor
//__attribute__((deprecated))
Ctxt(ZeroCtxtLike_type, const Ctxt& ctxt);
// constructs a zero ciphertext with same public key and
// plaintext space as ctxt
//! Dummy encryption, just encodes the plaintext in a Ctxt object
//! If provided, size should be a high-probability bound
//! on the L-infty norm of the canonical embedding
void DummyEncrypt(const NTL::ZZX& ptxt, double size = -1.0);
Ctxt& operator=(const Ctxt& other)
{ // public assignment operator
assertEq(&context,
&other.context,
"Cannot assign Ctxts with different context");
assertEq(&pubKey,
&other.pubKey,
"Cannot assign Ctxts with different pubKey");
return privateAssign(other);
}
bool operator==(const Ctxt& other) const { return equalsTo(other); }
bool operator!=(const Ctxt& other) const { return !equalsTo(other); }
// a procedural variant with an additional parameter
bool equalsTo(const Ctxt& other, bool comparePkeys = true) const;
// Encryption and decryption are done by the friends [Pub|Sec]Key
//! @name Ciphertext arithmetic
///@{
void negate();
// Add/subtract another ciphertext
Ctxt& operator+=(const Ctxt& other)
{
addCtxt(other);
return *this;
}
Ctxt& operator-=(const Ctxt& other)
{
addCtxt(other, true);
return *this;
}
void addCtxt(const Ctxt& other, bool negative = false);
// Multiply by another ciphertext
void multLowLvl(const Ctxt& other, bool destructive = false);
// This is a high-level mul with relinearization
Ctxt& operator*=(const Ctxt& other)
{
multiplyBy(other);
return *this;
}
void automorph(long k); // Apply automorphism F(X) -> F(X^k) (gcd(k,m)=1)
Ctxt& operator>>=(long k)
{
automorph(k);
return *this;
}
void complexConj(); // Complex conjugate, same as automorph(m-1)
//! @brief automorphism with re-linearization
void smartAutomorph(long k);
// Apply F(X)->F(X^k) followed by re-linearization. The automorphism is
// possibly evaluated via a sequence of steps, to ensure that we can
// re-linearize the result of every step.
//! @brief applies the automorphism p^j using smartAutomorphism
void frobeniusAutomorph(long j);
/**
* @brief Times equals operator with a `ZZX`.
* @param poly Element by which to multiply.
* @return Reference to `*this` post multiplication.
* @deprecated This function is deprecated in favor of a new
* `EncodedPtxt`-based API.\n
* Please use `Ctxt::operator*=(const EncodedPtxt& ptxt)` instead.
**/
[[deprecated(
"Please use Ctxt::operator*=(const EncodedPtxt& ptxt) instead.")]] Ctxt&
operator*=(const NTL::ZZX& poly);
//! Add a constant polynomial.
//! If provided, size should be a high-probability bound
//! on the L-infty norm of the canonical embedding
//! Otherwise, for the DoubleCRT variant, a bound based on the assumption
//! that the coefficients are uniformly and independently distributed over
//! [-ptxtSpace/2, ptxtSpace/2].
//! For the other variants, explicit bounds are computed (if not CKKS).
void addConstant(const DoubleCRT& dcrt, double size = -1.0);
void addConstant(const NTL::ZZX& poly, double size = -1.0);
/**
* @brief Add a plaintext to this `Ctxt`.
* @param ptxt Plaintext `Ptxt` object with which to add.
**/
template <typename Scheme>
void addConstant(const Ptxt<Scheme>& ptxt, bool neg = false)
{
EncodedPtxt eptxt;
ptxt.encode(eptxt);
addConstant(eptxt, neg);
}
template <typename Scheme>
Ctxt& operator+=(const Ptxt<Scheme>& ptxt)
{
addConstant(ptxt);
return *this;
}
template <typename Scheme>
Ctxt& operator-=(const Ptxt<Scheme>& ptxt)
{
addConstant(ptxt, true);
return *this;
}
//! add a rational number in the form a/b, a,b are long
/**
* @deprecated This function is deprecated in favor of a newer API.
* Please use `Ctxt::operator+=(double ptxt)` instead.
**/
[[deprecated("Please use Ctxt::operator+=(double ptxt) instead.")]] void
addConstantCKKS(std::pair</*numerator=*/long, /*denominator=*/long>);
/**
* @deprecated This function is deprecated in favor of a newer API.
* Please use `Ctxt::operator+=(double ptxt)` instead.
**/
[[deprecated("Please use Ctxt::operator+=(double ptxt) instead.")]] void
addConstantCKKS(double x)
{ // FIXME: not enough precision when x is large
// This function is deprecated.
// addConstantCKKS(
// rationalApprox(x, /*denomBound=*/1L <<
// getContext().getAlMod().getR()));
auto p =
rationalApprox(x, /*denomBound=*/1L << getContext().getAlMod().getR());
*this += double(p.first) / p.second;
}
/**
* @deprecated This function is deprecated in favor of a newer API.
* Please use `Ctxt::operator+=(const FatEncodedPtxt& ptxt)` instead.
**/
// [[deprecated]]
void addConstantCKKS(const DoubleCRT& dcrt,
NTL::xdouble size = NTL::xdouble(-1.0),
NTL::xdouble factor = NTL::xdouble(-1.0));
/**
* @deprecated This function is deprecated in favor of a newer API.
* Please use `Ctxt::operator+=(const EncodedPtxt& ptxt)` instead.
**/
[[deprecated(
"Please use Ctxt::operator+=(const EncodedPtxt& ptxt) instead.")]] void
addConstantCKKS(const NTL::ZZX& poly,
NTL::xdouble size = NTL::xdouble(-1.0),
NTL::xdouble factor = NTL::xdouble(-1.0));
/**
* @deprecated This function is deprecated in favor of a newer API.
* Please use `Ctxt::operator+=(const PtxtArray& ptxt)` instead.
**/
[[deprecated(
"Please use Ctxt::operator+=(const PtxtArray& ptxt) instead.")]] void
addConstantCKKS(const std::vector<std::complex<double>>& ptxt);
/**
* @brief Add a `CKKS` plaintext to this `Ctxt`.
* @param ptxt Plaintext `Ptxt` object with which to add.
* @deprecated This function is deprecated in favor of a newer API.
* Please use `Ctxt::operator+=(const PtxtArray& ptxt)` instead.
**/
[[deprecated(
"Please use Ctxt::operator+=(const Ptxt<Scheme>& ptxt) instead.")]] void
addConstantCKKS(const Ptxt<CKKS>& ptxt);
/**
* @deprecated This function is deprecated in favor of a newer API.
* Please use `Ctxt::operator+=(const NTL::ZZ& ptxt)` instead.
**/
[[deprecated(
"Please use Ctxt::operator+=(const NTL::ZZ& ptxt) instead.")]] void
addConstantCKKS(const NTL::ZZ& c);
//! Multiply-by-constant.
//! If the size is not given, for the DCRT variant, we use
//! a high probability bound assuming "random" coefficients
//! mod ptxtSpace, while for the other variants, we use
//! explicitly computed bounds (if not CKKS).
void multByConstant(const DoubleCRT& dcrt, double size = -1.0);
void multByConstant(const NTL::ZZX& poly, double size = -1.0);
void multByConstant(const zzX& poly, double size = -1.0);
//=========== new multByConstant interface =========
/**
* @brief Multiply a `Ctxt` with a specified plaintext constant.
* @param ptxt The constant to multiply as a `PtxtArray` object.
**/
void multByConstant(const PtxtArray& ptxt);
/**
* @brief Multiply a `Ctxt` with a specified plaintext constant.
* @param ptxt The constant to multiply as an `EncodedPtxt` object.
* @note `EncodedPtxt` is a plaintext object containing `NTL::ZZX` data.
**/
void multByConstant(const EncodedPtxt& ptxt);
/**
* @brief Multiply a `Ctxt` with a specified plaintext constant.
* @param ptxt The constant to multiply as a `FatEncodedPtxt` object.
* @note `FatEncodedPtxt` is a plaintext object containing
* `helib::DoubleCRT` data.
**/
void multByConstant(const FatEncodedPtxt& ptxt);
/**
* @brief Multiply a `Ctxt` with an `NTL::ZZ` scalar.
* @param ptxt Scalar to multiply.
**/
void multByConstant(const NTL::ZZ& ptxt);
/**
* @brief Multiply a `Ctxt` with a `long` scalar.
* @param ptxt Scalar to multiply.
**/
void multByConstant(long ptxt);
/**
* @brief Multiply a `Ctxt` with a `double` scalar.
* @param ptxt Scalar to multiply.
**/
void multByConstant(double ptxt);
/**
* @brief Multiply a `Ctxt` with an `NTL::xdouble` scalar.
* @param ptxt Scalar to multiply.
**/
void multByConstant(NTL::xdouble ptxt);
/**
* @brief Times equals operator with a plaintext constant.
* @param ptxt Right hand side of multiplication.
* @return Reference to `*this` post multiplication.
**/
Ctxt& operator*=(const PtxtArray& ptxt)
{
multByConstant(ptxt);
return *this;
}
/**
* @brief Times equals operator with a plaintext constant.
* @param ptxt Right hand side of multiplication.
* @return Reference to `*this` post multiplication.
* @note `EncodedPtxt` is a plaintext object containing `NTL::ZZX` data.
**/
Ctxt& operator*=(const EncodedPtxt& ptxt)
{
multByConstant(ptxt);
return *this;
}
/**
* @brief Times equals operator with a plaintext constant.
* @param ptxt Right hand side of multiplication.
* @return Reference to `*this` post multiplication.
* @note `FatEncodedPtxt` is a plaintext object containing
* `helib::DoubleCRT` data.
**/
Ctxt& operator*=(const FatEncodedPtxt& ptxt)
{
multByConstant(ptxt);
return *this;
}
/**
* @brief Times equals operator with an `NTL::ZZ` scalar.
* @param ptxt Right hand side of multiplication.
* @return Reference to `*this` post multiplication.
**/
Ctxt& operator*=(const NTL::ZZ& ptxt)
{
multByConstant(ptxt);
return *this;
}
/**
* @brief Times equals operator with a `long` scalar.
* @param ptxt Right hand side of multiplication.
* @return Reference to `*this` post multiplication.
**/
Ctxt& operator*=(long ptxt)
{
multByConstant(ptxt);
return *this;
}
/**
* @brief Times equals operator with a `double` scalar.
* @param ptxt Right hand side of multiplication.
* @return Reference to `*this` post multiplication.
**/
Ctxt& operator*=(double ptxt)
{
multByConstant(ptxt);
return *this;
}
/**
* @brief Times equals operator with an `NTL::xdouble` scalar.
* @param ptxt Right hand side of multiplication.
* @return Reference to `*this` post multiplication.
**/
Ctxt& operator*=(NTL::xdouble ptxt)
{
multByConstant(ptxt);
return *this;
}
private: // impl only
void multByConstant(const FatEncodedPtxt_BGV& ptxt);
void multByConstant(const FatEncodedPtxt_CKKS& ptxt);
public:
//=========== new addConstant interface ============
/**
* @brief Add to a `Ctxt` a specified plaintext constant.
* @param ptxt The constant to add as a `PtxtArray` object.
* @param neg Flag to specify if the constant is negative. Default is
* `false`.
**/
void addConstant(const PtxtArray& ptxt, bool neg = false);
/**
* @brief Add to a `Ctxt` a specified plaintext constant.
* @param ptxt The constant to add as an `EncodedPtxt` object.
* @param neg Flag to specify if the constant is negative. Default is
* `false`.
* @note `EncodedPtxt` is a plaintext object containing `NTL::ZZX` data.
**/
void addConstant(const EncodedPtxt& ptxt, bool neg = false);
/**
* @brief Add to a `Ctxt` a specified plaintext constant.
* @param ptxt The constant to add as a `FatEncodedPtxt` object.
* @param neg Flag to specify if the constant is negative. Default is
* `false`.
* @note `FatEncodedPtxt` is a plaintext object containing
* `helib::DoubleCRT` data.
**/
void addConstant(const FatEncodedPtxt& ptxt, bool neg = false);
/**
* @brief Add to a `Ctxt` an `NTL::ZZ` scalar.
* @param ptxt The scalar to add.
* @param neg Flag to specify if the constant is negative. Default is
* `false`.
**/
void addConstant(const NTL::ZZ& ptxt, bool neg = false);
/**
* @brief Add to a `Ctxt` a `long` scalar.
* @param ptxt The scalar to add.
* @param neg Flag to specify if the constant is negative. Default is
* `false`.
**/
void addConstant(long ptxt, bool neg = false);
/**
* @brief Add to a `Ctxt` a `double` scalar.
* @param ptxt The scalar to add.
* @param neg Flag to specify if the constant is negative. Default is
* `false`.
**/
void addConstant(double ptxt, bool neg = false);
/**
* @brief Add to a `Ctxt` an `NTL::xdouble` scalar.
* @param ptxt The scalar to add.
* @param neg Flag to specify if the constant is negative. Default is
* `false`.
**/
void addConstant(NTL::xdouble ptxt, bool neg = false);
/**
* @brief Plus equals operator with plaintext constant.
* @param ptxt Right hand side of addition.
* @return Reference to `*this` post addition.
**/
Ctxt& operator+=(const PtxtArray& ptxt)
{
addConstant(ptxt);
return *this;
}
/**
* @brief Plus equals operator with plaintext constant.
* @param ptxt Right hand side of addition.
* @return Reference to `*this` post addition.
* @note `EncodedPtxt` is a plaintext object containing `NTL::ZZX` data.
**/
Ctxt& operator+=(const EncodedPtxt& ptxt)
{
addConstant(ptxt);
return *this;
}
/**
* @brief Plus equals operator with plaintext constant.
* @param ptxt Right hand side of addition.
* @return Reference to `*this` post addition.
* @note `FatEncodedPtxt` is a plaintext object containing
* `helib::DoubleCRT` data.
**/
Ctxt& operator+=(const FatEncodedPtxt& ptxt)
{
addConstant(ptxt);
return *this;
}
/**
* @brief Plus equals operator with an `NTL::ZZ` scalar.
* @param ptxt Right hand side of addition.
* @return Reference to `*this` post addition.
**/
Ctxt& operator+=(const NTL::ZZ& ptxt)
{
addConstant(ptxt);
return *this;
}
/**
* @brief Plus equals operator with a `long` scalar.
* @param ptxt Right hand side of addition.
* @return Reference to `*this` post addition.
**/
Ctxt& operator+=(long ptxt)
{
addConstant(ptxt);
return *this;
}
/**
* @brief Plus equals operator with a `double` scalar.
* @param ptxt Right hand side of addition.
* @return Reference to `*this` post addition.
**/
Ctxt& operator+=(double ptxt)
{
addConstant(ptxt);
return *this;
}
/**
* @brief Plus equals operator with an `NTL::xdouble` scalar.
* @param ptxt Right hand side of addition.
* @return Reference to `*this` post addition.
**/
Ctxt& operator+=(NTL::xdouble ptxt)
{
addConstant(ptxt);
return *this;
}
/**
* @brief Minus equals operator with plaintext constant.
* @param ptxt Right hand side of subtraction.
* @return Reference to `*this` post subtraction.
**/
Ctxt& operator-=(const PtxtArray& ptxt)
{
addConstant(ptxt, true);
return *this;
}
/**
* @brief Minus equals operator with plaintext constant.
* @param ptxt Right hand side of subtraction.
* @return Reference to `*this` post subtraction.