PALISADE Lattice Crypto Library  1.11.9
A lattice crypto library for software engineers by software engineers.
ckkspackedencoding.h
1 // @file ckkspackedencoding.h
2 // @author TPOC: contact@palisade-crypto.org
3 //
4 // @copyright Copyright (c) 2019, Duality Technologies Inc.
5 // All rights reserved.
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution. THIS SOFTWARE IS
13 // PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
14 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
16 // EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
17 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 
24 #ifndef LBCRYPTO_UTILS_CKKSPACKEDEXTENCODING_H
25 #define LBCRYPTO_UTILS_CKKSPACKEDEXTENCODING_H
26 
27 #include <functional>
28 #include <initializer_list>
29 #include <memory>
30 #include <numeric>
31 #include <utility>
32 #include <vector>
33 
34 #include "encoding/encodingparams.h"
35 #include "encoding/plaintext.h"
36 #include "utils/inttypes.h"
37 
38 namespace lbcrypto {
39 
40 enum RescalingTechnique { APPROXRESCALE, EXACTRESCALE, APPROXAUTO };
41 
42 // STL pair used as a key for some tables in CKKSPackedEncoding
43 using ModulusM = std::pair<NativeInteger, uint64_t>;
44 
54  public:
55  // these two constructors are used inside of Decrypt
56  CKKSPackedEncoding(shared_ptr<Poly::Params> vp, EncodingParams ep)
57  : PlaintextImpl(vp, ep) {
58  depth = 1;
59  m_logError = 0.0;
60  }
61 
62  CKKSPackedEncoding(shared_ptr<NativePoly::Params> vp, EncodingParams ep)
63  : PlaintextImpl(vp, ep) {
64  depth = 1;
65  m_logError = 0.0;
66  }
67 
68  CKKSPackedEncoding(shared_ptr<DCRTPoly::Params> vp, EncodingParams ep)
69  : PlaintextImpl(vp, ep) {
70  depth = 1;
71  m_logError = 0.0;
72  }
73 
74  CKKSPackedEncoding(shared_ptr<Poly::Params> vp, EncodingParams ep,
75  const std::vector<std::complex<double>> &coeffs,
76  size_t depth, uint32_t level, double scFact)
77  : PlaintextImpl(vp, ep), value(coeffs) {
78  this->depth = depth;
79  this->level = level;
80  this->scalingFactor = scFact;
81  m_logError = 0.0;
82  }
83 
84  CKKSPackedEncoding(shared_ptr<NativePoly::Params> vp, EncodingParams ep,
85  const std::vector<std::complex<double>> &coeffs,
86  size_t depth, uint32_t level, double scFact)
87  : PlaintextImpl(vp, ep), value(coeffs) {
88  this->depth = depth;
89  this->level = level;
90  this->scalingFactor = scFact;
91  m_logError = 0.0;
92  }
93 
94  /*
95  * @param depth depth of plaintext to create.
96  * @param level level of plaintext to create.
97  * @param scFact scaling factor of a plaintext of this level at depth 1.
98  *
99  */
100  CKKSPackedEncoding(shared_ptr<DCRTPoly::Params> vp, EncodingParams ep,
101  const std::vector<std::complex<double>> &coeffs,
102  size_t depth, uint32_t level, double scFact)
103  : PlaintextImpl(vp, ep), value(coeffs) {
104  this->depth = depth;
105  this->level = level;
106  this->scalingFactor = scFact;
107  m_logError = 0.0;
108  }
109 
115  explicit CKKSPackedEncoding(const std::vector<std::complex<double>> &rhs)
116  : PlaintextImpl(shared_ptr<Poly::Params>(0), nullptr), value(rhs) {
117  depth = 1;
118  m_logError = 0.0;
119  }
120 
125  : PlaintextImpl(shared_ptr<Poly::Params>(0), nullptr), value() {
126  depth = 1;
127  m_logError = 0.0;
128  }
129 
131  : PlaintextImpl(rhs), value(rhs.value), m_logError(rhs.m_logError) {}
132 
134  : PlaintextImpl(rhs),
135  value(std::move(rhs.value)),
136  m_logError(rhs.m_logError) {}
137 
138  bool Encode();
139 
140  bool Decode() {
141  PALISADE_THROW(
143  "CKKSPackedEncoding::Decode() is not implemented. "
144  "Use CKKSPackedEncoding::Decode(depth,scalingFactor,rstech) instead.");
145  }
146 
147  bool Decode(size_t depth, double scalingFactor, RescalingTechnique rsTech);
148 
149  const std::vector<std::complex<double>> &GetCKKSPackedValue() const {
150  return value;
151  }
152 
153  const std::vector<double> GetRealPackedValue() const {
154  std::vector<double> realValue(value.size());
155  std::transform(value.begin(), value.end(), realValue.begin(),
156  [](std::complex<double> da) { return da.real(); });
157 
158  return realValue;
159  }
160 
171  static std::vector<DCRTPoly::Integer> CRTMult(
172  const std::vector<DCRTPoly::Integer> &a,
173  const std::vector<DCRTPoly::Integer> &b,
174  const std::vector<DCRTPoly::Integer> &mods);
175 
180  PlaintextEncodings GetEncodingType() const { return CKKSPacked; }
181 
187  size_t GetLength() const { return value.size(); }
188 
193  double GetLogError() const { return m_logError; }
194 
198  double GetLogPrecision() const {
199  return encodingParams->GetPlaintextModulus() - m_logError;
200  }
201 
206  void SetLength(size_t siz) { value.resize(siz); }
207 
215  bool CompareTo(const PlaintextImpl &other) const {
216  const auto &rv = static_cast<const CKKSPackedEncoding &>(other);
217  return this->value == rv.value;
218  }
219 
223  static void Destroy();
224 
225  void PrintValue(std::ostream &out) const {
226  // for sanity's sake, trailing zeros get elided into "..."
227  // out.precision(15);
228  out << "(";
229  size_t i = value.size();
230  while (--i > 0)
231  if (value[i] != std::complex<double>(0, 0)) break;
232 
233  for (size_t j = 0; j <= i; j++) {
234  out << value[j].real() << ", ";
235  }
236 
237  out << " ... ); ";
238  out << "Estimated precision: "
239  << encodingParams->GetPlaintextModulus() - m_logError << " bits"
240  << std::endl;
241  }
242 
243  private:
244  std::vector<std::complex<double>> value;
245 
246  double m_logError;
247 
248  protected:
256  void FitToNativeVector(const std::vector<int64_t> &vec, int64_t bigBound,
257  NativeVector *nativeVec) const;
258 
259 #if NATIVEINT == 128
260 
267  void FitToNativeVector(const std::vector<__int128> &vec, __int128 bigBound,
268  NativeVector *nativeVec) const;
269 
270  constexpr __int128 Max128BitValue() const {
271  // 2^127-2^73-1 - max value that could be rounded to int128_t
272  return ((unsigned __int128)1 << 127) - ((unsigned __int128)1 << 73) -
273  (unsigned __int128)1;
274  }
275 
276  inline bool is128BitOverflow(double d) const {
277  const double EPSILON = 0.000001;
278 
279  return EPSILON < (std::abs(d) - Max128BitValue());
280  }
281 #else // NATIVEINT == 64
282  constexpr int64_t Max64BitValue() const {
283  // 2^63-2^9-1 - max value that could be rounded to int64_t
284  return 9223372036854775295;
285  }
286 
287  inline bool is64BitOverflow(double d) const {
288  const double EPSILON = 0.000001;
289 
290  return EPSILON < (std::abs(d) - Max64BitValue());
291  }
292 #endif
293 };
294 
295 } // namespace lbcrypto
296 
297 #endif
bool Decode()
Definition: ckkspackedencoding.h:140
void SetLength(size_t siz)
Definition: ckkspackedencoding.h:206
bool Encode()
Definition: ckkspackedencoding.cpp:114
bool CompareTo(const PlaintextImpl &other) const
Definition: ckkspackedencoding.h:215
PlaintextEncodings GetEncodingType() const
Definition: ckkspackedencoding.h:180
Type used for representing IntArray types. Provides conversion functions to encode and decode plainte...
Definition: ckkspackedencoding.h:53
double GetLogPrecision() const
Definition: ckkspackedencoding.h:198
size_t GetLength() const
Definition: ckkspackedencoding.h:187
This class represents plaintext in the Palisade library.
Definition: plaintext.h:87
static void Destroy()
Destructor method.
Definition: ckkspackedencoding.cpp:536
CKKSPackedEncoding(const std::vector< std::complex< double >> &rhs)
Constructs a container with a copy of each of the elements in rhs, in the same order.
Definition: ckkspackedencoding.h:115
static std::vector< DCRTPoly::Integer > CRTMult(const std::vector< DCRTPoly::Integer > &a, const std::vector< DCRTPoly::Integer > &b, const std::vector< DCRTPoly::Integer > &mods)
Definition: ckkspackedencoding.cpp:100
Definition: mubintvecnat.h:102
double GetLogError() const
Definition: ckkspackedencoding.h:193
CKKSPackedEncoding()
Default empty constructor with empty uninitialized data elements.
Definition: ckkspackedencoding.h:124
void PrintValue(std::ostream &out) const
Definition: ckkspackedencoding.h:225
Definition: binfhecontext.h:36
Definition: exception.h:126
void FitToNativeVector(const std::vector< int64_t > &vec, int64_t bigBound, NativeVector *nativeVec) const
Definition: ckkspackedencoding.cpp:538