EncodedPtxt.h
1 /* Copyright (C) 2020 IBM Corp.
2  * This program is Licensed under the Apache License, Version 2.0
3  * (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software
7  * distributed under the License is distributed on an "AS IS" BASIS,
8  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9  * See the License for the specific language governing permissions and
10  * limitations under the License. See accompanying LICENSE file.
11  */
12 #ifndef HELIB_ENCODED_PTXT_H
13 #define HELIB_ENCODED_PTXT_H
14 
15 #include <helib/DoubleCRT.h>
16 #include <helib/norms.h>
17 
18 namespace helib {
19 
21 {
22 
23 private:
24  zzX poly;
25  long ptxtSpace;
26  const Context& context;
27 
28 public:
29  const zzX& getPoly() const { return poly; }
30  long getPtxtSpace() const { return ptxtSpace; }
31  const Context& getContext() const { return context; }
32 
33  EncodedPtxt_BGV(const zzX& poly_, long ptxtSpace_, const Context& context_) :
34  poly(poly_), ptxtSpace(ptxtSpace_), context(context_)
35  {}
36 };
37 
39 {
40 
41 private:
42  zzX poly;
43  double mag, scale, err;
44  const Context& context;
45 
46 public:
47  const zzX& getPoly() const { return poly; }
48  double getMag() const { return mag; }
49  double getScale() const { return scale; }
50  double getErr() const { return err; }
51  const Context& getContext() const { return context; }
52 
53  EncodedPtxt_CKKS(const zzX& poly_,
54  double mag_,
55  double scale_,
56  double err_,
57  const Context& context_) :
58  poly(poly_), mag(mag_), scale(scale_), err(err_), context(context_)
59  {}
60 };
61 
62 /*
63 
64 The following is a lot of "boilerplate" code that
65 gives a type-safe union of the above two EncodedPtxt types.
66 
67 The idea is that an EncodedPtxt object eptxt contains
68 either an EncodedPtxt_BGV object, an EncodedPtxt_CKKS object,
69 or nothing at all.
70 
71 * eptxt.isBGV() return true if it contains an EncodedPtxt_BGV object
72 
73 * eptxt.isCKKS() return true if it contains an EncodedPtxt_CKKS object
74 
75 * eptxt.getBGV() returns a read-only reference to the contained
76  EncodedPtxt_BGV object (or throws a std::bad_cast exception)
77 
78 * eptxt.getCKKS() returns a read-only reference to the contained
79  EncodedPtxt_CKKS object (or throws a std::bad_cast exception)
80 
81 * eptxt.resetBGV(poly, ptxtSpace, context) replaces the contents
82  of eptxt with a new EncodedPtxt_BGV object
83 
84 * eptxt.resetCKKS(poly, mag, scale, err, context) replaces the contents
85  of eptxt with a new EncodedPtxt_CKKS object
86 
87 * eptxt.reset() empties the contents
88 
89 The default constructor creates an empty container.
90 Copy constructors and assignment operators are available,
91  and provide "deep" copy semantics
92 
93 */
94 
96 {
97 public:
98  virtual ~EncodedPtxt_base() {}
99 
100  virtual EncodedPtxt_base* clone() const = 0;
101  // makes this usable with ClonedPtr
102 
103  virtual bool isBGV() const { return false; }
104  virtual bool isCKKS() const { return false; }
105 
106  virtual const EncodedPtxt_BGV& getBGV() const { throw std::bad_cast(); }
107  virtual const EncodedPtxt_CKKS& getCKKS() const { throw std::bad_cast(); }
108 };
109 
111 {
112 public:
113  virtual EncodedPtxt_base* clone() const override
114  {
115  return new EncodedPtxt_derived_BGV(*this);
116  }
117 
118  virtual bool isBGV() const override { return true; }
119 
120  virtual const EncodedPtxt_BGV& getBGV() const override { return *this; }
121 
123 };
124 
126  public EncodedPtxt_base,
127  public EncodedPtxt_CKKS
128 {
129 public:
130  virtual EncodedPtxt_base* clone() const override
131  {
132  return new EncodedPtxt_derived_CKKS(*this);
133  }
134 
135  virtual bool isCKKS() const override { return true; }
136 
137  virtual const EncodedPtxt_CKKS& getCKKS() const override { return *this; }
138 
140 };
141 
143 {
145 
146 public:
147  bool isBGV() const { return rep && rep->isBGV(); }
148  bool isCKKS() const { return rep && rep->isCKKS(); }
149 
150  const EncodedPtxt_BGV& getBGV() const
151  {
152  if (!rep)
153  throw std::bad_cast();
154  return rep->getBGV();
155  }
156 
157  const EncodedPtxt_CKKS& getCKKS() const
158  {
159  if (!rep)
160  throw std::bad_cast();
161  return rep->getCKKS();
162  }
163 
164  void resetBGV(const zzX& poly, long ptxtSpace, const Context& context)
165  {
166  rep.reset(new EncodedPtxt_derived_BGV(poly, ptxtSpace, context));
167  }
168 
169  void resetCKKS(const zzX& poly,
170  double mag,
171  double scale,
172  double err,
173  const Context& context)
174  {
175  rep.reset(new EncodedPtxt_derived_CKKS(poly, mag, scale, err, context));
176  }
177 
178  void reset() { rep.reset(); }
179 };
180 
181 //=========================================================
182 //
183 // "fat" encodings...same as above, but with DCRT's instead.
184 
186 {
187 
188 private:
189  DoubleCRT dcrt;
190  long ptxtSpace;
191  double size;
192 
193 public:
194  const DoubleCRT& getDCRT() const { return dcrt; }
195  long getPtxtSpace() const { return ptxtSpace; }
196  const Context& getContext() const { return dcrt.getContext(); }
197  double getSize() const { return size; }
198 
199  FatEncodedPtxt_BGV(const EncodedPtxt_BGV& eptxt, const IndexSet& s) :
200  dcrt(eptxt.getPoly(), eptxt.getContext(), s),
201  ptxtSpace(eptxt.getPtxtSpace()),
202  size(embeddingLargestCoeff(eptxt.getPoly(),
203  eptxt.getContext().getZMStar()))
204  {}
205 
206  FatEncodedPtxt_BGV(const DoubleCRT& dcrt_, long ptxtSpace_, double size_) :
207  dcrt(dcrt_), ptxtSpace(ptxtSpace_), size(size_)
208  {}
209 };
210 
212 {
213 
214 private:
215  DoubleCRT dcrt;
216  double mag, scale, err;
217 
218 public:
219  const DoubleCRT& getDCRT() const { return dcrt; }
220  double getMag() const { return mag; }
221  double getScale() const { return scale; }
222  double getErr() const { return err; }
223  const Context& getContext() const { return dcrt.getContext(); }
224 
226  dcrt(eptxt.getPoly(), eptxt.getContext(), s),
227  mag(eptxt.getMag()),
228  scale(eptxt.getScale()),
229  err(eptxt.getErr())
230  {}
231 
233  double mag_,
234  double scale_,
235  double err_) :
236  dcrt(dcrt_), mag(mag_), scale(scale_), err(err_)
237  {}
238 };
239 
241 {
242 public:
243  virtual ~FatEncodedPtxt_base() {}
244 
245  // TODO make this usable with ClonedPtr
246  virtual FatEncodedPtxt_base* clone() const = 0;
247 
248  virtual bool isBGV() const { return false; }
249  virtual bool isCKKS() const { return false; }
250 
251  virtual const FatEncodedPtxt_BGV& getBGV() const { throw std::bad_cast(); }
252  virtual const FatEncodedPtxt_CKKS& getCKKS() const { throw std::bad_cast(); }
253 };
254 
256  public FatEncodedPtxt_base,
257  public FatEncodedPtxt_BGV
258 {
259 public:
260  virtual FatEncodedPtxt_base* clone() const override
261  {
262  return new FatEncodedPtxt_derived_BGV(*this);
263  }
264 
265  virtual bool isBGV() const override { return true; }
266 
267  virtual const FatEncodedPtxt_BGV& getBGV() const override { return *this; }
268 
270 };
271 
273  public FatEncodedPtxt_base,
274  public FatEncodedPtxt_CKKS
275 {
276 public:
277  virtual FatEncodedPtxt_base* clone() const override
278  {
279  return new FatEncodedPtxt_derived_CKKS(*this);
280  }
281 
282  virtual bool isCKKS() const override { return true; }
283 
284  virtual const FatEncodedPtxt_CKKS& getCKKS() const override { return *this; }
285 
287 };
288 
289 /*
290 
291 Usage:
292 
293  FatEncryptedPtxt feptxt;
294  EncryptedPtxt eptxt;
295  IndexSet s;
296 
297  feptxt.expand(eptxt, s);
298  // sets feptxt to an expanded (DCRT) version of eptxt
299  // with the given IndexSet
300 
301  feptxt.reset();
302  // empties out feptxt
303 
304  // Also supports methods isBGV(), isCKKS(), getBGV(), and getCKKS(),
305  // analogous to EncodedPtxt.
306 
307  // One can also construct and expand in one step:
308  FatEncryptedPtxt feptxt(eptxt, s);
309 
310 */
311 
313 {
315 
316 public:
318  FatEncodedPtxt(const EncodedPtxt& eptxt, const IndexSet& s)
319  {
320  expand(eptxt, s);
321  }
322 
323  bool isBGV() const { return rep && rep->isBGV(); }
324  bool isCKKS() const { return rep && rep->isCKKS(); }
325 
326  const FatEncodedPtxt_BGV& getBGV() const
327  {
328  if (!rep)
329  throw std::bad_cast();
330  return rep->getBGV();
331  }
332 
334  {
335  if (!rep)
336  throw std::bad_cast();
337  return rep->getCKKS();
338  }
339 
340  void expand(const EncodedPtxt& eptxt, const IndexSet& s)
341  {
342  if (eptxt.isBGV())
343  rep.reset(new FatEncodedPtxt_derived_BGV(eptxt.getBGV(), s));
344  else if (eptxt.isCKKS())
345  rep.reset(new FatEncodedPtxt_derived_CKKS(eptxt.getCKKS(), s));
346  else
347  rep.reset();
348  }
349 
350  void reset() { rep.reset(); }
351 };
352 
353 } // namespace helib
354 
355 #endif
A smart pointer that clones the object it holds when it is copied.
Definition: ClonedPtr.h:101
void reset(T *p=nullptr)
Reset method deletes the object that it currently managed and manages the object given by a raw point...
Definition: ClonedPtr.h:180
Maintaining the HE scheme parameters.
Definition: Context.h:100
Implementing polynomials (elements in the ring R_Q) in double-CRT form.
Definition: DoubleCRT.h:76
const Context & getContext() const
Definition: DoubleCRT.h:376
Definition: EncodedPtxt.h:21
const Context & getContext() const
Definition: EncodedPtxt.h:31
long getPtxtSpace() const
Definition: EncodedPtxt.h:30
EncodedPtxt_BGV(const zzX &poly_, long ptxtSpace_, const Context &context_)
Definition: EncodedPtxt.h:33
const zzX & getPoly() const
Definition: EncodedPtxt.h:29
Definition: EncodedPtxt.h:39
const Context & getContext() const
Definition: EncodedPtxt.h:51
double getErr() const
Definition: EncodedPtxt.h:50
double getMag() const
Definition: EncodedPtxt.h:48
const zzX & getPoly() const
Definition: EncodedPtxt.h:47
double getScale() const
Definition: EncodedPtxt.h:49
EncodedPtxt_CKKS(const zzX &poly_, double mag_, double scale_, double err_, const Context &context_)
Definition: EncodedPtxt.h:53
Definition: EncodedPtxt.h:96
virtual const EncodedPtxt_CKKS & getCKKS() const
Definition: EncodedPtxt.h:107
virtual bool isCKKS() const
Definition: EncodedPtxt.h:104
virtual const EncodedPtxt_BGV & getBGV() const
Definition: EncodedPtxt.h:106
virtual ~EncodedPtxt_base()
Definition: EncodedPtxt.h:98
virtual EncodedPtxt_base * clone() const =0
virtual bool isBGV() const
Definition: EncodedPtxt.h:103
Definition: EncodedPtxt.h:111
virtual EncodedPtxt_base * clone() const override
Definition: EncodedPtxt.h:113
virtual bool isBGV() const override
Definition: EncodedPtxt.h:118
virtual const EncodedPtxt_BGV & getBGV() const override
Definition: EncodedPtxt.h:120
Definition: EncodedPtxt.h:128
virtual EncodedPtxt_base * clone() const override
Definition: EncodedPtxt.h:130
virtual const EncodedPtxt_CKKS & getCKKS() const override
Definition: EncodedPtxt.h:137
virtual bool isCKKS() const override
Definition: EncodedPtxt.h:135
Definition: EncodedPtxt.h:143
void resetBGV(const zzX &poly, long ptxtSpace, const Context &context)
Definition: EncodedPtxt.h:164
bool isCKKS() const
Definition: EncodedPtxt.h:148
void resetCKKS(const zzX &poly, double mag, double scale, double err, const Context &context)
Definition: EncodedPtxt.h:169
bool isBGV() const
Definition: EncodedPtxt.h:147
const EncodedPtxt_CKKS & getCKKS() const
Definition: EncodedPtxt.h:157
void reset()
Definition: EncodedPtxt.h:178
const EncodedPtxt_BGV & getBGV() const
Definition: EncodedPtxt.h:150
Definition: EncodedPtxt.h:186
FatEncodedPtxt_BGV(const DoubleCRT &dcrt_, long ptxtSpace_, double size_)
Definition: EncodedPtxt.h:206
long getPtxtSpace() const
Definition: EncodedPtxt.h:195
const Context & getContext() const
Definition: EncodedPtxt.h:196
FatEncodedPtxt_BGV(const EncodedPtxt_BGV &eptxt, const IndexSet &s)
Definition: EncodedPtxt.h:199
const DoubleCRT & getDCRT() const
Definition: EncodedPtxt.h:194
double getSize() const
Definition: EncodedPtxt.h:197
Definition: EncodedPtxt.h:212
FatEncodedPtxt_CKKS(const EncodedPtxt_CKKS &eptxt, const IndexSet &s)
Definition: EncodedPtxt.h:225
const Context & getContext() const
Definition: EncodedPtxt.h:223
const DoubleCRT & getDCRT() const
Definition: EncodedPtxt.h:219
double getErr() const
Definition: EncodedPtxt.h:222
double getMag() const
Definition: EncodedPtxt.h:220
FatEncodedPtxt_CKKS(const DoubleCRT &dcrt_, double mag_, double scale_, double err_)
Definition: EncodedPtxt.h:232
double getScale() const
Definition: EncodedPtxt.h:221
Definition: EncodedPtxt.h:241
virtual const FatEncodedPtxt_CKKS & getCKKS() const
Definition: EncodedPtxt.h:252
virtual ~FatEncodedPtxt_base()
Definition: EncodedPtxt.h:243
virtual const FatEncodedPtxt_BGV & getBGV() const
Definition: EncodedPtxt.h:251
virtual bool isBGV() const
Definition: EncodedPtxt.h:248
virtual bool isCKKS() const
Definition: EncodedPtxt.h:249
virtual FatEncodedPtxt_base * clone() const =0
Definition: EncodedPtxt.h:258
virtual const FatEncodedPtxt_BGV & getBGV() const override
Definition: EncodedPtxt.h:267
virtual FatEncodedPtxt_base * clone() const override
Definition: EncodedPtxt.h:260
virtual bool isBGV() const override
Definition: EncodedPtxt.h:265
Definition: EncodedPtxt.h:275
virtual FatEncodedPtxt_base * clone() const override
Definition: EncodedPtxt.h:277
virtual bool isCKKS() const override
Definition: EncodedPtxt.h:282
virtual const FatEncodedPtxt_CKKS & getCKKS() const override
Definition: EncodedPtxt.h:284
Definition: EncodedPtxt.h:313
bool isCKKS() const
Definition: EncodedPtxt.h:324
bool isBGV() const
Definition: EncodedPtxt.h:323
const FatEncodedPtxt_CKKS & getCKKS() const
Definition: EncodedPtxt.h:333
FatEncodedPtxt(const EncodedPtxt &eptxt, const IndexSet &s)
Definition: EncodedPtxt.h:318
FatEncodedPtxt()
Definition: EncodedPtxt.h:317
void expand(const EncodedPtxt &eptxt, const IndexSet &s)
Definition: EncodedPtxt.h:340
void reset()
Definition: EncodedPtxt.h:350
const FatEncodedPtxt_BGV & getBGV() const
Definition: EncodedPtxt.h:326
A dynamic set of non-negative integers.
Definition: IndexSet.h:33
Definition: apiAttributes.h:21
NTL::xdouble embeddingLargestCoeff(const Ctxt &ctxt, const SecKey &sk)
Definition: debugging.cpp:56
NTL::Vec< long > zzX
Definition: zzX.h:24