-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy pathdcrtpoly-interface.h
More file actions
1602 lines (1464 loc) · 61.2 KB
/
dcrtpoly-interface.h
File metadata and controls
1602 lines (1464 loc) · 61.2 KB
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
//==================================================================================
// BSD 2-Clause License
//
// Copyright (c) 2014-2023, NJIT, Duality Technologies Inc. and other contributors
//
// All rights reserved.
//
// Author TPOC: contact@openfhe.org
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//==================================================================================
/*
Defines an interface that any DCRT Polynomial implmentation must implement in order to work in OpenFHE.
*/
#ifndef LBCRYPTO_INC_LATTICE_HAL_DCRTPOLYINTERFACE_H
#define LBCRYPTO_INC_LATTICE_HAL_DCRTPOLYINTERFACE_H
#include "lattice/hal/default/ildcrtparams.h"
#include "lattice/ilelement.h"
#include "math/math-hal.h"
#include "math/distrgen.h"
#include "utils/inttypes.h"
#include "utils/exception.h"
#include <functional>
#include <memory>
#include <string>
#include <utility>
#include <vector>
namespace lbcrypto {
/**
* @brief Ideal lattice for the double-CRT interface representation.
* The interface contains a methods required for computations on lattices
* The double-CRT representation of polynomials is a common optimization for
* lattice encryption operations. Basically, it allows large-modulus polynomials
* to be represented as multiple smaller-modulus polynomials. The double-CRT
* representations are discussed theoretically here:
* - Gentry C., Halevi S., Smart N.P. (2012) Homomorphic Evaluation of the AES
* Circuit. In: Safavi-Naini R., Canetti R. (eds) Advances in Cryptology –
* CRYPTO 2012. Lecture Notes in Computer Science, vol 7417. Springer, Berlin,
* Heidelberg
*
*
* @tparam DerivedType Curiously-Recurring-Template-Pattern
* @tparam BigVecType The Vector type before decomposing the polynomial into CRT
* @tparam LilVecType The underlaying RNS data structure, a vectors type structure, that will compose the CRT data
* @tparam RNSContainer The container of LilVecType, a lbcrypto::PolyImpl or vector typically
*
* example for the default DerivedType the template types would be...
* DerivedType - DCRTPolyImpl<BigVector>
* BigVecType - BigVector
* LilVecType - NativeVector
* RNSContainer<LVT> - PolyImpl
*/
// TODO: CRTP with ILElement to remove virtual overhead
template <typename DerivedType, typename BigVecType, typename LilVecType,
template <typename LVT> typename RNSContainerType>
class DCRTPolyInterface : public ILElement<DerivedType, BigVecType> {
constexpr static std::string_view NOT_IMPLEMENTED_ERROR = "This function is not implemented for DCRTPoly";
public:
using BigIntType = typename BigVecType::Integer;
using Params = ILDCRTParams<BigIntType>;
using LilIntType = typename LilVecType::Integer;
using TowerType = RNSContainerType<LilVecType>;
using PolyLargeType = RNSContainerType<BigVecType>;
using DggType = DiscreteGaussianGeneratorImpl<LilVecType>;
using DugType = DiscreteUniformGeneratorImpl<LilVecType>;
using TugType = TernaryUniformGeneratorImpl<LilVecType>;
using BugType = BinaryUniformGeneratorImpl<LilVecType>;
/**
* @brief Get the Derived object, this is apart of the CRTP software design pattern
* it allows the base class (this one) to implement methods that call the derived
* objects implementation.
*
* @ref Chapter 21.2 "C++ Templates The Complete Guide" by David Vandevoorde and Nicolai M. Josuttis
* http://www.informit.com/articles/article.asp?p=31473
*
* @return DerivedType&
*/
DerivedType& GetDerived() {
return static_cast<DerivedType&>(*this);
}
const DerivedType& GetDerived() const {
return static_cast<DerivedType const&>(*this);
}
/**
* @brief Create lambda that allocates a zeroed element for the case when it
* is called from a templated class
*
* @param params the params to use.
* @param format - EVALUATION or COEFFICIENT
*/
static std::function<DerivedType()> Allocator(const std::shared_ptr<Params>& params, Format format) {
return [=]() {
return DerivedType(params, format, true);
};
}
/**
* @brief Allocator for discrete uniform distribution.
*
* @param params Params instance that is is passed.
* @param resultFormat resultFormat for the polynomials generated.
* @param stddev standard deviation for the discrete gaussian generator.
* @return the resulting vector.
*/
static std::function<DerivedType()> MakeDiscreteGaussianCoefficientAllocator(const std::shared_ptr<Params>& params,
Format resultFormat, double stddev) {
return [=]() {
DggType dgg(stddev);
return DerivedType(dgg, params, resultFormat);
};
}
/**
* @brief Allocator for discrete uniform distribution.
*
* @param params Params instance that is is passed.
* @param format format for the polynomials generated.
* @return the resulting vector.
*/
static std::function<DerivedType()> MakeDiscreteUniformAllocator(const std::shared_ptr<Params>& params,
Format format) {
return [=]() {
DugType dug;
return DerivedType(dug, params, format);
};
}
/**
* @brief Makes a copy of the DCRTPoly, but it includes only a sequential
* subset of the towers that the original holds.
*
* @param startTower The index number of the first tower to clone
* @param endTower The index number of the last tower to clone
* @return new Element
*/
DerivedType CloneTowers(uint32_t startTower, uint32_t endTower) {
return this->GetDerived().CloneTowers(startTower, endTower);
}
DerivedType Clone() const final {
return DerivedType(this->GetDerived());
}
DerivedType CloneEmpty() const final {
return DerivedType();
}
DerivedType CloneParametersOnly() const final {
return DerivedType(this->GetDerived().GetParams(), this->GetDerived().GetFormat());
}
/**
* @brief Clone with noise. This method creates a new DCRTPoly and clones the
* params. The tower values will be filled up with noise based on the discrete
* gaussian.
*
* @param &dgg the input discrete Gaussian generator. The dgg will be the seed
* to populate the towers of the DCRTPoly with random numbers.
* @param format the input format fixed to EVALUATION. Format is a enum type
* that indicates if the polynomial is in Evaluation representation or
* Coefficient representation. It is defined in inttypes.h.
*/
DerivedType CloneWithNoise(const DiscreteGaussianGeneratorImpl<BigVecType>& dgg, Format format) const override = 0;
/**
* @brief Get method of the format.
*
* @return the format, either COEFFICIENT or EVALUATION
*/
Format GetFormat() const override = 0;
// return this->GetDerived().GetFormat();
/**
* @brief returns the parameters of the element.
* @return the element parameter set.
*/
const std::shared_ptr<Params>& GetParams() const {
return this->GetDerived().GetParams();
}
/**
* @brief returns the element's cyclotomic order
* @return returns the cyclotomic order of the element.
*/
usint GetCyclotomicOrder() const final {
return this->GetDerived().GetParams()->GetCyclotomicOrder();
}
/**
* @brief returns the element's ring dimension
* @return returns the ring dimension of the element.
*/
usint GetRingDimension() const {
return this->GetDerived().GetParams()->GetRingDimension();
}
/**
* @brief returns the element's modulus
* @return returns the modulus of the element.
*/
const BigIntType& GetModulus() const final {
return this->GetDerived().GetParams()->GetModulus();
}
/**
* @brief returns the element's root of unity.
* @return the element's root of unity.
*/
const BigIntType& GetRootOfUnity() const {
return this->GetDerived().GetParams()->GetRootOfUnity();
}
/**
* @brief Get method for length of each component element.
* NOTE assumes all components are the same size. (Ring Dimension)
*
* @return length of the component element
*/
usint GetLength() const final {
return this->GetDerived().GetParams()->GetRingDimension();
}
/**
* @brief Get interpolated value of elements at all tower index i.
* Note this operation is computationally intense. Does bound checking
* @return interpolated value at index i.
*/
BigIntType& at(usint i) final {
OPENFHE_THROW(NOT_IMPLEMENTED_ERROR);
}
const BigIntType& at(usint i) const final {
OPENFHE_THROW(NOT_IMPLEMENTED_ERROR);
}
/**
* @brief Get interpolated value of element at index i.
* Note this operation is computationally intense. No bound checking
* @return interpolated value at index i.
*/
BigIntType& operator[](usint i) final {
OPENFHE_THROW(NOT_IMPLEMENTED_ERROR);
}
const BigIntType& operator[](usint i) const final {
OPENFHE_THROW(NOT_IMPLEMENTED_ERROR);
}
/**
* @brief Get method that returns a vector of all component elements.
*
* @returns a vector of the component elements.
*/
const std::vector<TowerType>& GetAllElements() const {
return this->GetDerived().GetAllElements();
}
std::vector<TowerType>& GetAllElements() {
return this->GetDerived().GetAllElements();
}
/**
* @brief Get method of the number of component elements, also known as the
* number of towers.
*
* @return the number of component elements.
*/
usint GetNumOfElements() const {
return this->GetDerived().GetAllElements().size();
}
/**
* @brief Get method of individual tower of elements.
* Note this behavior is different than poly
* @param i index of tower to be returned.
* @returns a reference to the returned tower
*/
const TowerType& GetElementAtIndex(usint i) const {
return this->GetDerived().GetAllElements()[i];
}
/**
* @brief Sets element at index
*
* @param index where the element should be set
* @param element The element to store
*/
void SetElementAtIndex(usint index, const TowerType& element) {
return this->GetDerived().SetElementAtIndex(index, element);
}
/**
* @brief Sets element at index
*
* @param index where the element should be set
* @param element The element to store
*/
void SetElementAtIndex(usint index, TowerType&& element) {
return this->GetDerived().SetElementAtIndex(index, std::move(element));
}
/***********************************************************************
* Yuriy and I stopped here!
**********************************************************************/
/**
* @brief Write the element as \f$ \sum\limits{i=0}^{\lfloor {\log q/base}
* \rfloor} {(base^i u_i)} \f$ and return the vector of \f$ \left\{u_0,
* u_1,...,u_{\lfloor {\log q/base} \rfloor} \right\} \in R_{{base}^{\lceil
* {\log q/base} \rceil}} \f$; This is used as a subroutine in the
* relinearization procedure.
*
* @param baseBits is the number of bits in the base, i.e., \f$ base =
* 2^{baseBits} \f$.
* @return is the pointer where the base decomposition vector is stored
*
* @warning not efficient and not fast, uses multiprecision arithmetic and
* will be removed in future. Use @see DCRTPolyInterface::CRTDecompose instead.
*/
std::vector<DerivedType> BaseDecompose(usint baseBits, bool evalModeAnswer) const override = 0;
/**
* @brief Generate a vector of PolyImpl's as \f$ \left\{x, {base}*x,
* {base}^2*x, ..., {base}^{\lfloor {\log q/{base}} \rfloor} \right\}*x \f$,
* where \f$ x \f$ is the current PolyImpl object;
* used as a subroutine in the relinearization procedure to get powers of a
* certain "base" for the secret key element.
*
* @param baseBits is the number of bits in the base, i.e., \f$ base =
* 2^{baseBits} \f$.
* @return is the pointer where the base decomposition vector is stored
*
* @warning not efficient and not fast, uses multiprecision arithmetic and
* will be removed in future. Use @see DCRTPolyInterface::CRTDecompose instead.
*/
std::vector<DerivedType> PowersOfBase(usint baseBits) const override = 0;
/**
* CRT basis decomposition of c as [c qi/q]_qi
*
* @param &baseBits bits in the base for additional digit decomposition if
* base > 0
* @return is the pointer where the resulting vector is stored
*/
std::vector<DerivedType> CRTDecompose(uint32_t baseBits) const {
return this->GetDerived().CRTDecompose(baseBits);
}
DerivedType& operator=(const TowerType& rhs) {
return this->GetDerived().operator=(rhs);
}
/**
* @brief Assignment Operator.
*
* @param &rhs the copied element.
* @return the resulting element.
*/
DerivedType& operator=(const DerivedType& rhs) override = 0;
/**
* @brief Move Assignment Operator.
*
* @param &rhs the copied element.
* @return the resulting element.
*/
DerivedType& operator=(DerivedType&& rhs) override = 0;
/**
* @brief Initalizer list
*
* @param &rhs the list to initalized the element.
* @return the resulting element.
*/
DerivedType& operator=(std::initializer_list<uint64_t> rhs) override = 0;
/**
* @brief Assignment Operator. The usint val will be set at index zero and all
* other indices will be set to zero.
*
* @param val is the usint to assign to index zero.
* @return the resulting vector.
*/
DerivedType& operator=(uint64_t val) {
return this->GetDerived().operator=(val);
}
/**
* @brief Creates a Poly from a vector of signed integers (used for trapdoor
* sampling)
*
* @param &rhs the vector to set the PolyImpl to.
* @return the resulting PolyImpl.
*/
DerivedType& operator=(const std::vector<int64_t>& rhs) {
return this->GetDerived().operator=(rhs);
}
/**
* @brief Creates a Poly from a vector of signed integers (used for trapdoor
* sampling)
*
* @param &rhs the vector to set the PolyImpl to.
* @return the resulting PolyImpl.
*/
DerivedType& operator=(const std::vector<int32_t>& rhs) {
return this->GetDerived().operator=(rhs);
}
/**
* @brief Initalizer list
*
* @param &rhs the list to set the PolyImpl to.
* @return the resulting PolyImpl.
*/
DerivedType& operator=(std::initializer_list<std::string> rhs) {
return this->GetDerived().operator=(rhs);
}
/**
* @brief Unary minus on a element.
* @return additive inverse of the an element.
*/
DerivedType operator-() const override = 0;
/**
* @brief Equality operator.
*
* @param &rhs is the specified element to be compared with this element.
* @return true if this element represents the same values as the specified
* element, false otherwise.
*/
bool operator==(const DerivedType& rhs) const override = 0;
/**
* @brief Performs an entry-wise addition over all elements of each tower with
* the towers of the element on the right hand side.
*
* @param &rhs is the element to add with.
* @return is the result of the addition.
*/
DerivedType& operator+=(const DerivedType& rhs) override = 0;
/**
* @brief Performs an entry-wise subtraction over all elements of each tower
* with the towers of the element on the right hand side.
*
* @param &rhs is the element to subtract from.
* @return is the result of the addition.
*/
DerivedType& operator-=(const DerivedType& rhs) override = 0;
/**
* @brief Permutes coefficients in a polynomial. Moves the ith index to the
* first one, it only supports odd indices.
*
* @param &i is the element to perform the automorphism transform with.
* @return is the result of the automorphism transform.
*/
DerivedType AutomorphismTransform(uint32_t i) const override = 0;
/**
* @brief Performs an automorphism transform operation using precomputed bit
* reversal indices.
*
* @param &i is the element to perform the automorphism transform with.
* @param &vec a vector with precomputed indices
* @return is the result of the automorphism transform.
*/
DerivedType AutomorphismTransform(uint32_t i, const std::vector<uint32_t>& vec) const override = 0;
/**
* @brief Transpose the ring element using the automorphism operation
*
* @return is the result of the transposition.
*/
DerivedType Transpose() const final {
if (this->GetDerived().GetFormat() == Format::COEFFICIENT)
OPENFHE_THROW(
"DCRTPolyInterface element transposition is currently "
"implemented only in the Evaluation representation.");
return this->GetDerived().AutomorphismTransform(this->GetDerived().GetCyclotomicOrder() - 1);
}
/**
* @brief Performs an addition operation and returns the result.
*
* @param &element is the element to add with.
* @return is the result of the addition.
*/
DerivedType Plus(const DerivedType& rhs) const override = 0;
// return this->GetDerived().Plus(rhs);
/**
* @brief Performs a multiplication operation and returns the result.
*
* @param &element is the element to multiply with.
* @return is the result of the multiplication.
*/
DerivedType Times(const DerivedType& rhs) const override = 0;
// return this->GetDerived().Times(rhs);
/**
* @brief Performs a subtraction operation and returns the result.
*
* @param &element is the element to subtract from.
* @return is the result of the subtraction.
*/
DerivedType Minus(const DerivedType& rhs) const override = 0;
/**
* @brief Scalar addition - add an element to the first index of each tower.
*
* @param &element is the element to add entry-wise.
* @return is the result of the addition operation.
*/
DerivedType Plus(const BigIntType& rhs) const override = 0;
/**
* @brief Scalar addition for elements in CRT format.
* CRT elements are represented as vector of integer elements which
* correspond to the represented number modulo the primes in the
* tower chain (in same order).
*
* @param &element is the element to add entry-wise.
* @return is the result of the addition operation.
*/
DerivedType Plus(const std::vector<BigIntType>& rhs) const {
return this->GetDerived().Plus(rhs);
}
/**
* @brief Scalar subtraction - subtract an element to all entries.
*
* @param &element is the element to subtract entry-wise.
* @return is the return value of the minus operation.
*/
DerivedType Minus(const BigIntType& rhs) const override = 0;
/**
* @brief Scalar subtraction for elements in CRT format.
* CRT elements are represented as vector of integer elements which
* correspond to the represented number modulo the primes in the
* tower chain (in same order).
*
* @param &element is the element to subtract entry-wise.
* @return is the result of the subtraction operation.
*/
DerivedType Minus(const std::vector<BigIntType>& rhs) const {
return this->GetDerived().Minus(rhs);
}
/**
* @brief Scalar multiplication - multiply all entries.
*
* @param &element is the element to multiply entry-wise.
* @return is the return value of the times operation.
*/
DerivedType Times(const BigIntType& rhs) const override = 0;
/**
* @brief Scalar multiplication - multiply by a signed integer
*
* @param &element is the element to multiply entry-wise.
* @return is the return value of the times operation.
*/
DerivedType Times(NativeInteger::SignedNativeInt rhs) const override = 0;
#if NATIVEINT != 64
/**
* @brief Scalar multiplication - multiply by a signed integer
*
* @param &element is the element to multiply entry-wise.
* @return is the return value of the times operation.
*
* @note this is need for 128-bit so that the 64-bit inputs can be used.
*/
DerivedType Times(int64_t rhs) const {
return this->GetDerived().Times(rhs);
}
#endif
/**
* @brief Scalar multiplication by an integer represented in CRT Basis.
*
* @param &element is the element to multiply entry-wise.
* @return is the return value of the times operation.
*/
DerivedType Times(const std::vector<NativeInteger>& rhs) const {
return this->GetDerived().Times(rhs);
}
/**
* @brief Performs a multiplication operation even when the multiplicands
* have a different number of towers.
*
* @param &element is the element to multiply with.
* @return is the result of the multiplication.
*/
DerivedType TimesNoCheck(const std::vector<NativeInteger>& rhs) const {
return this->GetDerived().TimesNoCheck(rhs);
}
/**
* @brief Scalar modular multiplication by an integer represented in CRT
* Basis.
*
* @param &element is the element to multiply entry-wise.
* @return is the return value of the times operation.
*
* @warning Should remove this, data is truncated to native-word size.
*/
DerivedType Times(const std::vector<BigIntType>& rhs) const {
return this->GetDerived().Times(rhs);
}
/**
* @brief Scalar multiplication followed by division and rounding operation -
* operation on all entries.
*
* @param &p is the element to multiply entry-wise.
* @param &q is the element to divide entry-wise.
* @return is the return value of the multiply, divide and followed by
* rounding operation.
*
* @warning Will remove, this is only inplace because of BFV
*/
DerivedType MultiplyAndRound(const BigIntType& p, const BigIntType& q) const final {
OPENFHE_THROW(NOT_IMPLEMENTED_ERROR);
}
/**
* @brief Scalar division followed by rounding operation - operation on all
* entries.
*
* @param &q is the element to divide entry-wise.
* @return is the return value of the divide, followed by rounding operation.
*
* @warning Will remove, this is only inplace because of BFV
*/
DerivedType DivideAndRound(const BigIntType& q) const final {
OPENFHE_THROW(NOT_IMPLEMENTED_ERROR);
}
/**
* @brief Performs a negation operation and returns the result.
*
* @return is the result of the negation.
*/
virtual DerivedType Negate() const = 0;
DerivedType& operator+=(const BigIntType& rhs) override = 0;
virtual DerivedType& operator+=(const LilIntType& rhs) = 0;
/**
* @brief Performs a subtraction operation and returns the result.
*
* @param &element is the element to subtract from.
* @return is the result of the subtraction.
*/
DerivedType& operator-=(const BigIntType& rhs) override = 0;
virtual DerivedType& operator-=(const LilIntType& rhs) = 0;
/**
* @brief Performs a multiplication operation and returns the result.
*
* @param &element is the element to multiply by.
* @return is the result of the multiplication.
*/
DerivedType& operator*=(const BigIntType& rhs) override = 0;
virtual DerivedType& operator*=(const LilIntType& rhs) = 0;
/**
* @brief Performs a multiplication operation and returns the result.
*
* @param &element is the element to multiply with.
* @return is the result of the multiplication.
*/
DerivedType& operator*=(const DerivedType& rhs) override = 0;
// multiplicative inverse operation
/**
* @brief Performs a multiplicative inverse operation and returns the result.
*
* @return is the result of the multiplicative inverse.
*/
DerivedType MultiplicativeInverse() const override = 0;
/**
* @brief Perform a modulus by 2 operation. Returns the least significant
* bit.
*
* @return is the resulting value.
*
* @warning Doesn't make sense for DCRT
*/
DerivedType ModByTwo() const final {
OPENFHE_THROW(NOT_IMPLEMENTED_ERROR);
}
/**
* @brief Modulus - perform a modulus operation. Does proper mapping of
* [-modulus/2, modulus/2) to [0, modulus)
*
* @param modulus is the modulus to use.
* @return is the return value of the modulus.
*
* @warning Doesn't make sense for DCRT
*/
DerivedType Mod(const BigIntType& modulus) const final {
OPENFHE_THROW(NOT_IMPLEMENTED_ERROR);
}
/**
* @brief Get method that should not be used
*
* @return will throw an error.
*
* @warning Doesn't make sense for DCRT
*/
const BigVecType& GetValues() const final {
OPENFHE_THROW(NOT_IMPLEMENTED_ERROR);
}
/**
* @brief Set method that should not be used, will throw an error.
*
* @param &values
* @param format
*
* @warning Doesn't make sense for DCRT
*/
void SetValues(const BigVecType& values, Format format) {
OPENFHE_THROW(NOT_IMPLEMENTED_ERROR);
}
/**
* @brief Sets all values of element to zero.
*/
virtual void SetValuesToZero() = 0;
/**
* @brief Sets values with a different modulus
*/
virtual void SetValuesModSwitch(const DerivedType& element, const NativeInteger& modulus) = 0;
/**
* @brief Adds "1" to every entry in every tower.
*/
void AddILElementOne() override = 0;
/**
* @brief Add uniformly random values to all components except for the first
* one
*
* @warning Doesn't make sense for DCRT
*/
DerivedType AddRandomNoise(const BigIntType& modulus) const {
OPENFHE_THROW(NOT_IMPLEMENTED_ERROR);
}
/**
* @brief Make DCRTPoly Sparse. Sets every index of each tower not equal to
* zero mod the wFactor to zero.
*
* @param &wFactor ratio between the sparse and none-sparse values.
*
* @warning Only used by RingSwitching, which is no longer supported. Will be removed in future.
*/
void MakeSparse(uint32_t wFactor) final {
OPENFHE_THROW(NOT_IMPLEMENTED_ERROR);
}
/**
* @brief Returns true if ALL the tower(s) are empty.
* @return true if all towers are empty
*/
bool IsEmpty() const override = 0;
/**
* @brief Drops the last element in the double-CRT representation. The
* resulting DCRTPoly element will have one less tower.
*/
virtual void DropLastElement() = 0;
/**
* @brief Drops the last i elements in the double-CRT representation.
*/
virtual void DropLastElements(size_t i) = 0;
/**
* @brief Drops the last element in the double-CRT representation and scales
* down by the last CRT modulus. The resulting DCRTPoly element will have one
* less tower.
* @param &QlQlInvModqlDivqlModq precomputed values for
* [Q^(l)*[Q^(l)^{-1}]_{q_l}/q_l]_{q_i}
* @param &QlQlInvModqlDivqlModqPrecon NTL-specific precomputations
* @param &qlInvModq precomputed values for [q_l^{-1}]_{q_i}
* @param &qlInvModqPrecon NTL-specific precomputations
*/
virtual void DropLastElementAndScale(const std::vector<NativeInteger>& QlQlInvModqlDivqlModq,
const std::vector<NativeInteger>& qlInvModq) = 0;
/**
* @brief ModReduces reduces the DCRTPoly element's composite modulus by
* dropping the last modulus from the chain of moduli as well as dropping the
* last tower.
*
* @param &t is the plaintextModulus used for the DCRTPoly
* @param &tModqPrecon NTL-specific precomputations for [t]_{q_i}
* @param &negtInvModq precomputed values for [-t^{-1}]_{q_i}
* @param &negtInvModqPrecon NTL-specific precomputations for [-t^{-1}]_{q_i}
* @param &qlInvModq precomputed values for [q_{l}^{-1}]_{q_i}
* @param &qlInvModqPrecon NTL-specific precomputations for [q_{l}^{-1}]_{q_i}
*/
virtual void ModReduce(const NativeInteger& t, const std::vector<NativeInteger>& tModqPrecon,
const NativeInteger& negtInvModq, const NativeInteger& negtInvModqPrecon,
const std::vector<NativeInteger>& qlInvModq,
const std::vector<NativeInteger>& qlInvModqPrecon) = 0;
/**
* @brief Interpolates the DCRTPoly to a Poly based on the Chinese Remainder
* Transform Interpolation. and then returns a Poly with that single element
*
* @return the interpolated ring element as a Poly object.
*/
virtual PolyLargeType CRTInterpolate() const = 0;
virtual TowerType DecryptionCRTInterpolate(PlaintextModulus ptm) const = 0;
/**
* @brief If the values are small enough this is used for efficiency
*
* @return NativePoly
*
* @warning This will be replaced with a non-member utility function.
*/
virtual TowerType ToNativePoly() const = 0;
/**
* @brief Interpolates the DCRTPoly to a Poly based on the Chinese Remainder
* Transform Interpolation, only at element index i, all other elements are
* zero. and then returns a Poly with that single element
*
* @return the interpolated ring element as a Poly object.
*/
virtual PolyLargeType CRTInterpolateIndex(usint i) const = 0;
/**
* @brief Computes and returns the product of primes in the current moduli
* chain. Compared to GetModulus, which always returns the product of all
* primes in the crypto parameters, this method will return a different
* modulus, based on the towers/moduli that are currently in the chain (some
* towers are dropped along the way).
*
* @return the product of moduli in the current towers.
*/
virtual BigIntType GetWorkingModulus() const = 0;
/**
* @brief Returns the element parameters for DCRTPoly elements in an extended
* CRT basis, which is the concatenation of the towers currently in "this"
* DCRTPoly, and the moduli in ParamsP.
*
* @return element parameters of the extended basis.
*/
virtual std::shared_ptr<Params> GetExtendedCRTBasis(const std::shared_ptr<Params>& paramsP) const = 0;
virtual void TimesQovert(const std::shared_ptr<Params>& paramsQ, const std::vector<NativeInteger>& tInvModq,
const NativeInteger& t, const NativeInteger& NegQModt,
const NativeInteger& NegQModtPrecon) = 0;
/**
* @brief Performs approximate CRT basis switching:
* {X}_{Q} -> {X'}_{P}
* X' = X + alpha*Q for small alpha
* {Q} = {q_1,...,q_l}
* {P} = {p_1,...,p_k}
*
* Brief algorithm:
* [X']_{p_j} = [\sum_i([x_i*(Q/q_i)^{-1}]_{q_i}*(Q/q_i)]_{p_j}
*
* Source: "A full RNS variant of approximate homomorphic encryption" by
* Cheon, et. al.
*
* @param ¶msQ parameters for the CRT basis {q_1,...,q_l}
* @param ¶msP parameters for the CRT basis {p_1,...,p_k}
* @param &QHatinvModq precomputed values for [(Q/q_i)^{-1}]_{q_i}
* @param &QHatinvModqPrecon NTL-specific precomputations
* @param &QHatModp precomputed values for [Q/q_i]_{p_j}
* @param &modpBarrettMu 128-bit Barrett reduction precomputed values
* @return the representation of {X + alpha*Q} in basis {P}.
*/
virtual DerivedType ApproxSwitchCRTBasis(const std::shared_ptr<Params>& paramsQ,
const std::shared_ptr<Params>& paramsP,
const std::vector<NativeInteger>& QHatInvModq,
const std::vector<NativeInteger>& QHatInvModqPrecon,
const std::vector<std::vector<NativeInteger>>& QHatModp,
const std::vector<DoubleNativeInt>& modpBarrettMu) const = 0;
/**
* @brief Performs approximate modulus raising:
* {X}_{Q} -> {X'}_{Q,P}.
* X' = X + alpha*Q for small alpha
* {Q} = {q_1,...,q_l}
* {P} = {p_1,...,p_k}
*
* Brief algorithm:
* {X}_{Q} -> {X'}_Q : trivial
* {X}_{Q} -> {X'}_P : use DCRTPoly::ApproxSwitchCRTBasis
*
* Source: "A full RNS variant of approximate homomorphic encryption" by
* Cheon, et. al.
*
* @param ¶msQ parameters for the CRT basis {q_1,...,q_l}
* @param ¶msP parameters for the CRT basis {p_1,...,p_k}
* @param &QHatInvModq precomputed values for [(Q/q_i)^{-1}]_{q_i}
* @param &QHatInvModqPrecon NTL-specific precomputations
* @param &QHatModp precomputed values for [Q/q_i]_{p_j}
* @param &modpBarrettMu 128-bit Barrett reduction precomputed values for
* p_j
* @return the representation of {X + alpha*Q} in basis {Q,P}.
*/
virtual void ApproxModUp(const std::shared_ptr<Params>& paramsQ, const std::shared_ptr<Params>& paramsP,
const std::shared_ptr<Params>& paramsQP, const std::vector<NativeInteger>& QHatInvModq,
const std::vector<NativeInteger>& QHatInvModqPrecon,
const std::vector<std::vector<NativeInteger>>& QHatModp,
const std::vector<DoubleNativeInt>& modpBarrettMu) = 0;
/**
* @brief Performs approximate modulus reduction:
* {X}_{Q,P} -> {\approx(X/P)}_{Q}.
* {Q} = {q_1,...,q_l}
* {P} = {p_1,...,p_k}
*
* Brief algorithm:
* 1) use DCRTPoly::ApproxSwitchCRTBasis : {X}_{P} -> {X'}_{Q}
* 2) compute : {(X-X') * P^{-1}}_{Q}
*
* Source: "A full RNS variant of approximate homomorphic encryption" by
* Cheon, et. al.
*
* @param ¶msQ parameters for the CRT basis {q_1,...,q_l}
* @param ¶msP parameters for the CRT basis {p_1,...,p_k}
* @param &PInvModq precomputed values for (P^{-1} mod q_j)
* @param &PInvModqPrecon NTL-specific precomputations
* @param &PHatInvModp precomputed values for [(P/p_j)^{-1}]_{p_j}
* @param &PHatInvModpPrecon NTL-specific precomputations
* @param &PHatModq precomputed values for [P/p_j]_{q_i}
* @param &modqBarrettMu 128-bit Barrett reduction precomputed values for
* q_i
* @param &tInvModp precomputed values for [t^{-1}]_{p_j}
* used in BGVrns
* @param t often corresponds to the plaintext modulus
* used in BGVrns
* @return the representation of {\approx(X/P)}_{Q}
*/
virtual DerivedType ApproxModDown(
const std::shared_ptr<Params>& paramsQ, const std::shared_ptr<Params>& paramsP,
const std::vector<NativeInteger>& PInvModq, const std::vector<NativeInteger>& PInvModqPrecon,
const std::vector<NativeInteger>& PHatInvModp, const std::vector<NativeInteger>& PHatInvModpPrecon,
const std::vector<std::vector<NativeInteger>>& PHatModq, const std::vector<DoubleNativeInt>& modqBarrettMu,
const std::vector<NativeInteger>& tInvModp, const std::vector<NativeInteger>& tInvModpPrecon,
const NativeInteger& t, const std::vector<NativeInteger>& tModqPrecon) const = 0;
/**
* @brief Performs CRT basis switching:
* {X}_{Q} -> {X}_{P}
* {Q} = {q_1,...,q_l}
* {P} = {p_1,...,p_k}