PALISADE Lattice Crypto Library  1.11.9
A lattice crypto library for software engineers by software engineers.
cryptocontextgen.h
1 // @file ciphertextgen.h -- Generator for crypto contexts.
2 // @author TPOC: contact@palisade-crypto.org
3 //
4 // @copyright Copyright (c) 2019, New Jersey Institute of Technology (NJIT))
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 SRC_PKE_LIB_CRYPTOCONTEXTGEN_H_
25 #define SRC_PKE_LIB_CRYPTOCONTEXTGEN_H_
26 
27 #include <memory>
28 #include <string>
29 
30 #include "cryptocontext.h"
31 #include "lattice/elemparamfactory.h"
32 #include "palisade.h"
33 #include "utils/parmfactory.h"
34 
35 using namespace lbcrypto;
36 
37 static const usint DefaultQbits = 59;
38 static const usint DefaultT = 3;
39 
40 template <typename Element>
41 inline CryptoContext<Element> GenCryptoContextNull(usint ORDER,
42  PlaintextModulus ptm,
43  usint bits = DefaultQbits,
44  usint towers = DefaultT) {
45  auto p = ElemParamFactory::GenElemParams<typename Element::Params>(
46  ORDER, bits, towers);
47 
48  CryptoContext<Element> cc =
50  cc->Enable(ENCRYPTION);
51  cc->Enable(PRE);
52  cc->Enable(SHE);
53 
54  return cc;
55 }
56 
57 template <typename Element>
58 inline CryptoContext<Element> GenCryptoContextBFV(usint ORDER,
59  PlaintextModulus ptm,
60  usint bits = DefaultQbits,
61  usint towers = DefaultT,
62  MODE mode = RLWE);
63 
64 template <>
65 inline CryptoContext<Poly> GenCryptoContextBFV(usint ORDER,
66  PlaintextModulus ptm, usint bits,
67  usint towers, MODE mode) {
68  shared_ptr<typename Poly::Params> p =
69  ElemParamFactory::GenElemParams<typename Poly::Params>(ORDER, bits,
70  towers);
71 
72  CryptoContext<Poly> cc = CryptoContextFactory<Poly>::genCryptoContextBFV(
73  ptm, 1.06, 8, 4, 0, 1, 0, mode);
74  cc->Enable(ENCRYPTION);
75  cc->Enable(PRE);
76  cc->Enable(SHE);
77  return cc;
78 }
79 
80 template <>
81 inline CryptoContext<NativePoly> GenCryptoContextBFV(usint ORDER,
82  PlaintextModulus ptm,
83  usint bits, usint towers,
84  MODE mode) {
85  CryptoContext<NativePoly> cc =
87  1, 0, mode);
88  cc->Enable(ENCRYPTION);
89  cc->Enable(PRE);
90  cc->Enable(SHE);
91  return cc;
92 }
93 
94 template <>
95 inline CryptoContext<DCRTPoly> GenCryptoContextBFV(usint ORDER,
96  PlaintextModulus ptm,
97  usint bits, usint towers,
98  MODE mode) {
99  PALISADE_THROW(not_implemented_error, "DCRT is not supported for BFV");
100 }
101 
102 template <typename Element>
103 inline CryptoContext<Element> GenCryptoContextBFVrns(PlaintextModulus ptm,
104  MODE mode = RLWE,
105  uint32_t batchSize = 0);
106 
107 template <>
108 inline CryptoContext<Poly> GenCryptoContextBFVrns(PlaintextModulus ptm,
109  MODE mode,
110  uint32_t batchSize) {
111  PALISADE_THROW(not_implemented_error, "Poly is not supported for BFVrns");
112 }
113 
114 template <>
115 inline CryptoContext<NativePoly> GenCryptoContextBFVrns(PlaintextModulus ptm,
116  MODE mode,
117  uint32_t batchSize) {
118  PALISADE_THROW(not_implemented_error,
119  "NativePoly is not supported for BFVrns");
120 }
121 
122 template <>
123 inline CryptoContext<DCRTPoly> GenCryptoContextBFVrns(PlaintextModulus ptm,
124  MODE mode,
125  uint32_t batchSize) {
126  EncodingParams encodingParams(std::make_shared<EncodingParamsImpl>(ptm));
127  encodingParams->SetBatchSize(batchSize);
128  CryptoContext<DCRTPoly> cc =
130  encodingParams, HEStd_128_classic, 3.2, 0, 2, 0, mode, 2, 20, 60);
131  cc->Enable(ENCRYPTION);
132  cc->Enable(PRE);
133  cc->Enable(SHE);
134  cc->Enable(MULTIPARTY);
135  return cc;
136 }
137 
138 template <typename Element>
139 inline CryptoContext<Element> GenCryptoContextBFVrnsB(PlaintextModulus ptm,
140  MODE mode = RLWE,
141  uint32_t batchSize = 0);
142 
143 template <>
144 inline CryptoContext<Poly> GenCryptoContextBFVrnsB(PlaintextModulus ptm,
145  MODE mode,
146  uint32_t batchSize) {
147  PALISADE_THROW(not_implemented_error, "Poly is not supported for BFVrnsB");
148 }
149 
150 template <>
151 inline CryptoContext<NativePoly> GenCryptoContextBFVrnsB(PlaintextModulus ptm,
152  MODE mode,
153  uint32_t batchSize) {
154  PALISADE_THROW(not_implemented_error,
155  "NativePoly is not supported for BFVrnsB");
156 }
157 
158 template <>
159 inline CryptoContext<DCRTPoly> GenCryptoContextBFVrnsB(PlaintextModulus ptm,
160  MODE mode,
161  uint32_t batchSize) {
162  EncodingParams encodingParams(std::make_shared<EncodingParamsImpl>(ptm));
163  encodingParams->SetBatchSize(batchSize);
164  CryptoContext<DCRTPoly> cc =
166  encodingParams, HEStd_128_classic, 3.2, 0, 2, 0, mode, 2, 20, 60);
167  cc->Enable(ENCRYPTION);
168  cc->Enable(PRE);
169  cc->Enable(SHE);
170  cc->Enable(MULTIPARTY);
171  return cc;
172 }
173 
174 template <typename Element>
175 inline CryptoContext<Element> GenCryptoContextCKKS(
176  usint cyclOrder, usint numPrimes, usint scaleExp, usint relinWindow,
177  usint batchSize, MODE mode, KeySwitchTechnique ksTech,
178  RescalingTechnique rsTech);
179 
180 template <>
181 inline CryptoContext<Poly> GenCryptoContextCKKS(usint cyclOrder,
182  usint numPrimes, usint scaleExp,
183  usint relinWindow,
184  usint batchSize, MODE mode,
185  KeySwitchTechnique ksTech,
186  RescalingTechnique rsTech) {
187  PALISADE_THROW(not_implemented_error, "Poly is not supported for CKKS");
188 }
189 
190 /* *
191  * Generate a CryptoContext for the CKKS scheme.
192  *
193  * @param m Cyclotomic order. Must be a power of 2.
194  * @param init_size Number of co-primes comprising the ciphertext modulus.
195  * It is equal to the desired depth of the computation.
196  * @param dcrtBits Size of each co-prime in bits. Should fit into a
197  * machine word, i.e., less than 64.
198  * @param p Scaling parameter 2^p. p should usually be equal to dcrtBits.
199  * @param relinWin The bit decomposition count used in relinearization.
200  * Use 0 to go with max possible. Use small values (3-4?)
201  * if you need rotations before any multiplications.
202  * @param batchSize The length of the packed vectors to be used with CKKS.
203  * @param mode (e.g., RLWE or OPTIMIZED)
204  * @param ksTech key switching technique to use (e.g., GHS or BV)
205  * @param rsTech rescaling technique to use (e.g., APPROXRESCALE or
206  * EXACTRESCALE)
207  */
208 template <>
209 inline CryptoContext<NativePoly> GenCryptoContextCKKS(
210  usint cyclOrder, usint numPrimes, usint scaleExp, usint relinWindow,
211  usint batchSize, MODE mode, KeySwitchTechnique ksTech,
212  RescalingTechnique rsTech) {
213  usint m = cyclOrder;
214  usint init_size = numPrimes;
215  usint dcrtBits = scaleExp;
216  uint64_t p = scaleExp;
217  usint relinWin = relinWindow;
218  float stdDev = 3.19;
219  usint batch = batchSize;
220 
221  NativeInteger q = FirstPrime<NativeInteger>(dcrtBits, m);
222  NativeInteger rootOfUnity = RootOfUnity<NativeInteger>(m, q);
223 
224  auto params = std::make_shared<ILNativeParams>(m, q, rootOfUnity);
225 
226  EncodingParams encodingParams(
227  std::make_shared<EncodingParamsImpl>((int64_t)1 << p));
228  encodingParams->SetBatchSize(batch);
229 
230  CryptoContext<NativePoly> cc =
232  params, encodingParams, relinWin, stdDev, mode, init_size);
233 
234  cc->Enable(ENCRYPTION);
235  cc->Enable(SHE);
236 
237  return cc;
238 }
239 
240 /* *
241  * Generate a CryptoContext for the CKKS scheme.
242  *
243  * @param m Cyclotomic order. Must be a power of 2.
244  * @param init_size Number of co-primes comprising the ciphertext modulus.
245  * It is equal to the desired depth of the computation.
246  * @param dcrtBits Size of each co-prime in bits. Should fit into a
247  * machine word, i.e., less than 64.
248  * @param p Scaling parameter 2^p. p should usually be equal to dcrtBits.
249  * @param relinWin The bit decomposition count used in relinearization.
250  * Use 0 to go with max possible. Use small values (3-4?)
251  * if you need rotations before any multiplications.
252  * @param batchSize The length of the packed vectors to be used with CKKS.
253  * @param mode (e.g., RLWE or OPTIMIZED)
254  * @param ksTech key switching technique to use (e.g., GHS or BV)
255  * @param rsTech rescaling technique to use (e.g., APPROXRESCALE or
256  * EXACTRESCALE)
257  */
258 template <>
259 inline CryptoContext<DCRTPoly> GenCryptoContextCKKS(
260  usint cyclOrder, usint numPrimes, usint scaleExp, usint relinWindow,
261  usint batchSize, MODE mode, KeySwitchTechnique ksTech,
262  RescalingTechnique rsTech) {
263  usint n = cyclOrder / 2;
264  usint dcrtBits = scaleExp;
265  usint relinWin = relinWindow;
266  usint batch = batchSize;
267 
268  CryptoContext<DCRTPoly> cc =
270  numPrimes - 1, dcrtBits, batch, HEStd_NotSet, n, /*ringDimension*/
271  rsTech, ksTech, 0, /*numLargeDigits*/
272  2, /*maxDepth*/
273  FIRSTMODSIZE, /*firstMod*/
274  relinWin, mode);
275 
276  cc->Enable(ENCRYPTION);
277  cc->Enable(SHE);
278  cc->Enable(LEVELEDSHE);
279  cc->Enable(MULTIPARTY);
280  return cc;
281 }
282 
283 /* *
284  * Generate a CryptoContext for the BGVrns scheme.
285  *
286  * @param m Cyclotomic order. Must be a power of 2.
287  * @param init_size Number of co-primes comprising the ciphertext modulus.
288  * It is equal to the desired depth of the computation.
289  * @param dcrtBits Size of each co-prime in bits. Should fit into a
290  * machine word, i.e., less than 64.
291  * @param ptm the plaintext modulus.
292  * @param relinWin The bit decomposition count used in relinearization.
293  * Use 0 to go with max possible. Use small values (3-4?)
294  * if you need rotations before any multiplications.
295  * @param mode (e.g., RLWE or OPTIMIZED)
296  * @param ksTech key switching technique to use (e.g., GHS or BV)
297  * @param batchSize The length of the packed vectors to be used with CKKS.
298  * @param msMethod mod switching method
299  */
300 
301 template <typename Element>
302 inline CryptoContext<Element> GenCryptoContextBGVrns(
303  usint cyclOrder, usint numPrimes, usint dcrtBits, usint ptm,
304  usint relinWindow, MODE mode, KeySwitchTechnique ksTech, usint batchSize,
305  ModSwitchMethod msMethod);
306 
307 template <>
308 inline CryptoContext<Poly> GenCryptoContextBGVrns(
309  usint cyclOrder, usint numPrimes, usint dcrtBits, usint ptm,
310  usint relinWindow, MODE mode, KeySwitchTechnique ksTech, usint batchSize,
311  ModSwitchMethod msMethod) {
312  PALISADE_THROW(not_available_error, "Poly is not supported for BGVrns");
313 }
314 
315 template <>
316 inline CryptoContext<NativePoly> GenCryptoContextBGVrns(
317  usint cyclOrder, usint numPrimes, usint dcrtBits, usint ptm,
318  usint relinWindow, MODE mode, KeySwitchTechnique ksTech, usint batchSize,
319  ModSwitchMethod msMethod) {
320  PALISADE_THROW(not_available_error, "NativePoly is not supported for BGVrns");
321 }
322 
323 template <>
324 inline CryptoContext<DCRTPoly> GenCryptoContextBGVrns(
325  usint cyclOrder, usint numPrimes, usint dcrtBits, usint ptm,
326  usint relinWindow, MODE mode, KeySwitchTechnique ksTech, usint batchSize,
327  ModSwitchMethod msMethod) {
328  usint n = cyclOrder / 2;
329  usint relinWin = relinWindow;
330  float stdDev = 3.2;
331 
332  CryptoContext<DCRTPoly> cc =
334  numPrimes - 1, ptm, HEStd_NotSet, stdDev, 1, /* maxDepth */
335  mode, ksTech, n, /*ringDimension*/
336  0, /*numLargeDigits*/
337  60, /*firstMod*/
338  dcrtBits, relinWin, batchSize, msMethod);
339 
340  cc->Enable(ENCRYPTION);
341  cc->Enable(SHE);
342  cc->Enable(LEVELEDSHE);
343  cc->Enable(MULTIPARTY);
344  cc->Enable(PRE);
345  return cc;
346 }
347 
348 template <typename Element>
349 inline CryptoContext<Element> GenTestCryptoContext(
350  const string& name, usint ORDER, PlaintextModulus ptm,
351  usint bits = DefaultQbits, usint towers = DefaultT, usint relinWin = 0,
352  usint batchSize = 16, KeySwitchTechnique ksTech = BV,
353  RescalingTechnique rsTech = APPROXRESCALE,
354  ModSwitchMethod msMethod = MANUAL) {
355  shared_ptr<typename Element::Params> p =
356  ElemParamFactory::GenElemParams<typename Element::Params>(ORDER, bits,
357  towers);
358 
359  CryptoContext<Element> cc;
360 
361  if (name == "Null") {
363  } else if (name == "BFV_rlwe") {
364  cc = GenCryptoContextBFV<Element>(ORDER, ptm, bits, towers, RLWE);
365  } else if (name == "BFV_opt") {
366  cc = GenCryptoContextBFV<Element>(ORDER, ptm, bits, towers, OPTIMIZED);
367  } else if (name == "BFVrns_rlwe") {
368  cc = GenCryptoContextBFVrns<Element>(ptm, RLWE, batchSize);
369  } else if (name == "BFVrns_opt") {
370  cc = GenCryptoContextBFVrns<Element>(ptm, OPTIMIZED, batchSize);
371  } else if (name == "BFVrnsB_rlwe") {
372  cc = GenCryptoContextBFVrnsB<Element>(ptm, RLWE, batchSize);
373  } else if (name == "BFVrnsB_opt") {
374  cc = GenCryptoContextBFVrnsB<Element>(ptm, OPTIMIZED, batchSize);
375  } else if (name == "CKKS_sparse") {
376  cc = GenCryptoContextCKKS<Element>(ORDER, towers, ptm, relinWin, batchSize,
377  SPARSE, ksTech, rsTech);
378  } else if (name == "CKKS") {
379  cc = GenCryptoContextCKKS<Element>(ORDER, towers, ptm, relinWin, batchSize,
380  OPTIMIZED, ksTech, rsTech);
381  } else if (name == "BGVrns_rlwe") {
382  cc = GenCryptoContextBGVrns<Element>(ORDER, towers, bits, ptm, relinWin,
383  RLWE, ksTech, batchSize, msMethod);
384  } else if ((name == "BGVrns_opt") || (name == "BGVrns")) {
385  cc =
386  GenCryptoContextBGVrns<Element>(ORDER, towers, bits, ptm, relinWin,
387  OPTIMIZED, ksTech, batchSize, msMethod);
388  } else if (name == "BGVrns_sparse") {
389  cc = GenCryptoContextBGVrns<Element>(ORDER, towers, bits, ptm, relinWin,
390  SPARSE, ksTech, batchSize, msMethod);
391  } else {
392  std::cout << "nothing for " << name << std::endl;
393  PALISADE_THROW(not_available_error, "No generator for " + name);
394  }
395 
396  cc->Enable(ENCRYPTION);
397  cc->Enable(PRE);
398  cc->Enable(SHE);
399 
400  return cc;
401 }
402 
403 #endif /* SRC_PKE_LIB_CRYPTOCONTEXTGEN_H_ */
static CryptoContext< Element > genCryptoContextBFVrns(const PlaintextModulus plaintextModulus, float securityLevel, float dist, unsigned int numAdds, unsigned int numMults, unsigned int numKeyswitches, MODE mode=OPTIMIZED, int maxDepth=2, uint32_t relinWindow=0, size_t dcrtBits=60, uint32_t n=0)
Definition: cryptocontextfactory.cpp:194
static CryptoContext< Element > genCryptoContextBGVrns(shared_ptr< ParmType > params, const PlaintextModulus plaintextmodulus, usint relinWindow, float stDev, MODE mode=RLWE, int depth=1, int maxDepth=2, KeySwitchTechnique ksTech=BV, enum ModSwitchMethod msMethod=MANUAL)
Definition: cryptocontextfactory.cpp:460
static CryptoContext< Element > genCryptoContextBFV(shared_ptr< ParmType > params, const PlaintextModulus plaintextmodulus, usint relinWindow, float stDev, const std::string &delta, MODE mode=RLWE, const std::string &bigmodulus="0", const std::string &bigrootofunity="0", int depth=0, int assuranceMeasure=0, float securityLevel=0, const std::string &bigmodulusarb="0", const std::string &bigrootofunityarb="0", int maxDepth=2)
Definition: cryptocontextfactory.cpp:89
Definition: exception.h:119
static CryptoContext< Element > genCryptoContextCKKS(shared_ptr< ParmType > params, const PlaintextModulus plaintextmodulus, usint relinWindow, float stDev, MODE mode=RLWE, int depth=1, int maxDepth=2, KeySwitchTechnique ksTech=BV, RescalingTechnique rsTech=APPROXRESCALE)
Definition: cryptocontextfactory.cpp:402
static CryptoContext< Element > genCryptoContextBFVrnsB(const PlaintextModulus plaintextModulus, float securityLevel, float dist, unsigned int numAdds, unsigned int numMults, unsigned int numKeyswitches, MODE mode=OPTIMIZED, int maxDepth=2, uint32_t relinWindow=0, size_t dcrtBits=60, uint32_t n=0)
Definition: cryptocontextfactory.cpp:298
static CryptoContext< Element > genCryptoContextNull(unsigned int m, const PlaintextModulus ptModulus)
Definition: cryptocontextfactory.cpp:690
Definition: binfhecontext.h:36
Main class for big integers represented as an array of native (primitive) unsigned integers...
Definition: backend.h:60
Definition: exception.h:126