PALISADE Lattice Crypto Library  1.11.9
A lattice crypto library for software engineers by software engineers.
elemparamfactory.h
1 // @file elemparamfactory.h Creates ElemParams objects for PALISADE.
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_CORE_LIB_LATTICE_ELEMPARAMFACTORY_H_
25 #define SRC_CORE_LIB_LATTICE_ELEMPARAMFACTORY_H_
26 
27 #include <memory>
28 using std::shared_ptr;
29 
30 #include <string>
31 using std::string;
32 
33 #include "lattice/ildcrtparams.h"
34 #include "lattice/ilparams.h"
35 #include "math/backend.h"
36 #include "utils/parmfactory.h"
37 
38 namespace lbcrypto {
39 
40 // predefined values of m are 16, 1024, 2048, 4096, 8192, 16384, 32768 and 65536
41 
42 // the items in ElementOrder are an index into DefaultSet[]
43 enum ElementOrder { M16 = 0, M1024, M2048, M4096, M8192, M16384, M32768 };
44 
46  public:
47  static struct ElemParmSet {
48  usint m; // cyclotomic order
49  usint n; // ring dimension
50  string q; // ciphertext modulus
51  string ru; // root of unity
52  } DefaultSet[];
53 
54  static size_t GetNearestIndex(usint m) {
55  size_t sIdx = 0;
56  if (DefaultSet[0].m < m) {
57  for (sIdx = 1; DefaultSet[sIdx].m != 0; sIdx++) {
58  if (m <= DefaultSet[sIdx].m) break;
59  }
60  }
61  if (DefaultSet[sIdx].m == 0) sIdx--;
62 
63  return sIdx;
64  }
65 
72  template <typename P>
73  static shared_ptr<P> GenElemParams(ElementOrder o) {
74  DEBUG_FLAG(false);
75  DEBUG("in GenElemParams(ElementOrder o)");
76  return std::make_shared<P>(
77  DefaultSet[static_cast<int>(o)].m,
78  typename P::Integer(DefaultSet[static_cast<int>(o)].q),
79  typename P::Integer(DefaultSet[static_cast<int>(o)].ru));
80  }
81 
89  template <typename P>
90  static shared_ptr<P> GenElemParams(usint m) {
91  DEBUG_FLAG(false);
92  DEBUG("in GenElemParams(usint m)");
93  size_t sIdx = GetNearestIndex(m);
94 
95  return std::make_shared<P>(DefaultSet[sIdx].m,
96  typename P::Integer(DefaultSet[sIdx].q),
97  typename P::Integer(DefaultSet[sIdx].ru));
98  }
99 
109  template <typename P>
110  static shared_ptr<P> GenElemParams(usint m, usint bits, usint towersize = 1) {
111  DEBUG_FLAG(false);
112  DEBUG("in GenElemParams(usint m, usint bits, usint towers)");
113  typename P::Integer q = FirstPrime<typename P::Integer>(bits, m);
114  typename P::Integer ru = RootOfUnity<typename P::Integer>(m, q);
115  return std::make_shared<P>(m, q, ru);
116  }
117 
126  template <typename P>
127  static shared_ptr<P> GenElemParams(usint m,
128  const typename P::Integer& ctModulus,
129  const typename P::Integer& rootUnity) {
130  DEBUG_FLAG(false);
131  DEBUG("in GenElemParams(usint m, const typename P::Integer etc)");
132  return std::make_shared<P>(m, ctModulus, rootUnity);
133  }
134 };
135 
136 template <>
137 inline shared_ptr<ILDCRTParams<M2Integer>>
138 ElemParamFactory::GenElemParams<ILDCRTParams<M2Integer>>(usint m, usint bits,
139  usint towersize) {
140  DEBUG_FLAG(false);
141  DEBUG(
142  "in GenElemParams<ILDCRTParams<M2Integer>>(usint m, usint bits, usint "
143  "towersize)");
144  DEBUGEXP(m);
145  DEBUGEXP(bits);
146  DEBUGEXP(towersize);
147  return GenerateDCRTParams<M2Integer>(m, towersize, bits);
148 }
149 
150 template <>
151 inline shared_ptr<ILDCRTParams<M4Integer>>
152 ElemParamFactory::GenElemParams<ILDCRTParams<M4Integer>>(usint m, usint bits,
153  usint towersize) {
154  DEBUG_FLAG(false);
155  DEBUG(
156  "in GenElemParams<ILDCRTParams<M4Integer>>(usint m, usint bits, usint "
157  "towersize)");
158  DEBUGEXP(m);
159  DEBUGEXP(bits);
160  DEBUGEXP(towersize);
161  return GenerateDCRTParams<M4Integer>(m, towersize, bits);
162 }
163 #ifdef WITH_NTL
164 template <>
165 inline shared_ptr<ILDCRTParams<M6Integer>>
166 ElemParamFactory::GenElemParams<ILDCRTParams<M6Integer>>(usint m, usint bits,
167  usint towersize) {
168  DEBUG_FLAG(false);
169  DEBUG(
170  "in GenElemParams<ILDCRTParams<M6Integer>>(usint m, usint bits, usint "
171  "towersize)");
172  DEBUGEXP(m);
173  DEBUGEXP(bits);
174  DEBUGEXP(towersize);
175  return GenerateDCRTParams<M6Integer>(m, towersize, bits);
176 }
177 #endif
178 } /* namespace lbcrypto */
179 
180 #endif /* SRC_CORE_LIB_LATTICE_ELEMPARAMFACTORY_H_ */
static shared_ptr< P > GenElemParams(usint m, usint bits, usint towersize=1)
Definition: elemparamfactory.h:110
static shared_ptr< P > GenElemParams(usint m)
Definition: elemparamfactory.h:90
static shared_ptr< P > GenElemParams(ElementOrder o)
Definition: elemparamfactory.h:73
Definition: binfhecontext.h:36
Wrapper class to hold the parameters for integer lattice operations and their inheritors.
Definition: elemparamfactory.h:45
static shared_ptr< P > GenElemParams(usint m, const typename P::Integer &ctModulus, const typename P::Integer &rootUnity)
Definition: elemparamfactory.h:127