PALISADE Lattice Crypto Library  1.11.9
A lattice crypto library for software engineers by software engineers.
parmfactory.h
1 // @file parmfactory.h parameter factory.
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_UTILS_PARMFACTORY_H_
25 #define SRC_CORE_LIB_UTILS_PARMFACTORY_H_
26 
27 // useful for testing
28 
29 #include <memory>
30 #include <vector>
31 
32 #include "lattice/dcrtpoly.h"
33 #include "math/backend.h"
34 #include "math/distrgen.h"
35 
36 #include "utils/inttypes.h"
37 
38 #include "lattice/elemparams.h"
39 #include "lattice/ildcrtparams.h"
40 #include "lattice/ilelement.h"
41 #include "lattice/ilparams.h"
42 #include "lattice/poly.h"
43 
44 using namespace lbcrypto;
45 
54 template <typename I>
55 inline shared_ptr<ILDCRTParams<I>> GenerateDCRTParams(usint m, usint numOfTower,
56  usint pbits) {
57  DEBUG_FLAG(false);
58  DEBUG("in GenerateDCRTParams");
59  DEBUGEXP(m);
60  DEBUGEXP(numOfTower);
61  DEBUGEXP(pbits);
62  if (numOfTower == 0) {
63  PALISADE_THROW(math_error, "Can't make parms with numOfTower == 0");
64  }
65 
66  std::vector<NativeInteger> moduli(numOfTower);
67  std::vector<NativeInteger> rootsOfUnity(numOfTower);
68 
69  NativeInteger q = FirstPrime<NativeInteger>(pbits, m);
70  I modulus(1);
71 
72  usint j = 0;
73  DEBUGEXP(q);
74 
75  for (;;) {
76  moduli[j] = q;
77  rootsOfUnity[j] = RootOfUnity(m, q);
78  modulus = modulus * I(q.ConvertToInt());
79  DEBUG("j " << j << " modulus " << q << " rou " << rootsOfUnity[j]);
80  if (++j == numOfTower) break;
81 
82  q = NextPrime(q, m);
83  }
84 
85  auto params = std::make_shared<ILDCRTParams<I>>(m, moduli, rootsOfUnity);
86 
87  return params;
88 }
89 
90 #endif /* SRC_CORE_LIB_UTILS_PARMFACTORY_H_ */
Ideal lattice using a vector representation.
Definition: exception.h:113
IntType NextPrime(const IntType &q, uint64_t cyclotomicOrder)
Definition: nbtheory.cpp:537
Wrapper class to hold the parameters for Element types and their inheritors.
OutputType ConvertToInt() const
Definition: ubintnat.h:1886
IntType RootOfUnity(usint m, const IntType &modulo)
Definition: nbtheory.cpp:270
Definition: binfhecontext.h:36
Wrapper class to hold the parameters for integer lattice operations and their inheritors.
Main class for big integers represented as an array of native (primitive) unsigned integers...
Definition: backend.h:60