PALISADE Lattice Crypto Library  1.11.9
A lattice crypto library for software engineers by software engineers.
elemparams.h
Go to the documentation of this file.
1 // @file elemparams.h base class for parameters for a lattice element
2 // @author TPOC: contact@palisade-crypto.org
3 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution. THIS SOFTWARE IS
11 // PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
12 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
13 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
14 // EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
15 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
17 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
18 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
19 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
20 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21 
22 #ifndef LBCRYPTO_LATTICE_ELEMPARAMS_H
23 #define LBCRYPTO_LATTICE_ELEMPARAMS_H
24 
25 #include <iostream>
26 #include <string>
27 #include <utility>
28 
29 #include "math/backend.h"
30 #include "math/nbtheory.h"
31 #include "utils/inttypes.h"
32 #include "utils/serializable.h"
33 
34 namespace lbcrypto {
35 
42 template <typename IntegerType>
43 class ElemParams : public Serializable {
44  public:
56  ElemParams(usint order, const IntegerType& ctModulus,
57  const IntegerType& rUnity = IntegerType(0),
58  const IntegerType& bigCtModulus = IntegerType(0),
59  const IntegerType& bigRUnity = IntegerType(0)) {
60  cyclotomicOrder = order;
61  ringDimension = GetTotient(order);
62  isPowerOfTwo = ringDimension == cyclotomicOrder / 2;
63  ciphertextModulus = ctModulus;
64  rootOfUnity = rUnity;
65  bigCiphertextModulus = bigCtModulus;
66  bigRootOfUnity = bigRUnity;
67  }
68 
74  ElemParams(const ElemParams& rhs) {
75  cyclotomicOrder = rhs.cyclotomicOrder;
76  ringDimension = rhs.ringDimension;
77  isPowerOfTwo = rhs.isPowerOfTwo;
78  ciphertextModulus = rhs.ciphertextModulus;
79  rootOfUnity = rhs.rootOfUnity;
80  bigCiphertextModulus = rhs.bigCiphertextModulus;
81  bigRootOfUnity = rhs.bigRootOfUnity;
82  }
83 
89  ElemParams(const ElemParams&& rhs) {
90  cyclotomicOrder = rhs.cyclotomicOrder;
91  ringDimension = rhs.ringDimension;
92  isPowerOfTwo = rhs.isPowerOfTwo;
93  ciphertextModulus = std::move(rhs.ciphertextModulus);
94  rootOfUnity = std::move(rhs.rootOfUnity);
95  bigCiphertextModulus = std::move(rhs.bigCiphertextModulus);
96  bigRootOfUnity = std::move(rhs.bigRootOfUnity);
97  }
98 
103  const ElemParams& operator=(const ElemParams& rhs) {
104  cyclotomicOrder = rhs.cyclotomicOrder;
105  ringDimension = rhs.ringDimension;
106  isPowerOfTwo = rhs.isPowerOfTwo;
107  ciphertextModulus = rhs.ciphertextModulus;
108  rootOfUnity = rhs.rootOfUnity;
109  bigCiphertextModulus = rhs.bigCiphertextModulus;
110  bigRootOfUnity = rhs.bigRootOfUnity;
111  return *this;
112  }
113 
118  virtual ~ElemParams() {}
119 
124  usint GetCyclotomicOrder() const { return cyclotomicOrder; }
125 
131  usint GetRingDimension() const { return ringDimension; }
132 
139  bool OrderIsPowerOfTwo() const { return isPowerOfTwo; }
140 
146  const IntegerType& GetModulus() const { return ciphertextModulus; }
147 
153  const IntegerType& GetBigModulus() const { return bigCiphertextModulus; }
154 
160  const IntegerType& GetRootOfUnity() const { return rootOfUnity; }
161 
166  const IntegerType& GetBigRootOfUnity() const { return bigRootOfUnity; }
167 
174  friend std::ostream& operator<<(std::ostream& out, const ElemParams& item) {
175  return item.doprint(out);
176  }
177 
183  virtual bool operator==(const ElemParams<IntegerType>& other) const {
184  return cyclotomicOrder == other.cyclotomicOrder &&
185  ringDimension == other.ringDimension &&
186  ciphertextModulus == other.ciphertextModulus &&
187  rootOfUnity == other.rootOfUnity &&
188  bigCiphertextModulus == other.bigCiphertextModulus &&
189  bigRootOfUnity == other.bigRootOfUnity;
190  }
191 
197  bool operator!=(const ElemParams<IntegerType>& other) const {
198  return !(*this == other);
199  }
200 
201  template <class Archive>
202  void save(Archive& ar, std::uint32_t const version) const {
203  ar(::cereal::make_nvp("co", cyclotomicOrder));
204  ar(::cereal::make_nvp("rd", ringDimension));
205  ar(::cereal::make_nvp("2n", isPowerOfTwo));
206  ar(::cereal::make_nvp("cm", ciphertextModulus));
207  ar(::cereal::make_nvp("ru", rootOfUnity));
208  ar(::cereal::make_nvp("bm", bigCiphertextModulus));
209  ar(::cereal::make_nvp("br", bigRootOfUnity));
210  }
211 
212  template <class Archive>
213  void load(Archive& ar, std::uint32_t const version) {
214  if (version > SerializedVersion()) {
215  PALISADE_THROW(deserialize_error,
216  "serialized object version " + std::to_string(version) +
217  " is from a later version of the library");
218  }
219  ar(::cereal::make_nvp("co", cyclotomicOrder));
220  ar(::cereal::make_nvp("rd", ringDimension));
221  ar(::cereal::make_nvp("2n", isPowerOfTwo));
222  ar(::cereal::make_nvp("cm", ciphertextModulus));
223  ar(::cereal::make_nvp("ru", rootOfUnity));
224  ar(::cereal::make_nvp("bm", bigCiphertextModulus));
225  ar(::cereal::make_nvp("br", bigRootOfUnity));
226  }
227 
228  std::string SerializedObjectName() const { return "ElemParams"; }
229  static uint32_t SerializedVersion() { return 1; }
230 
231  protected:
232  usint cyclotomicOrder;
233  usint ringDimension; // True iff the Ring Dimension is a power of 2.
234 
235  bool isPowerOfTwo;
236  IntegerType ciphertextModulus;
237  IntegerType rootOfUnity;
238  IntegerType bigCiphertextModulus; // Used for only some applications.
239  IntegerType bigRootOfUnity; // Used for only some applications.
240 
246  virtual std::ostream& doprint(std::ostream& out) const {
247  out << "[m=" << cyclotomicOrder << (isPowerOfTwo ? "* " : " ")
248  << "n=" << ringDimension << " q=" << ciphertextModulus
249  << " ru=" << rootOfUnity << " bigq=" << bigCiphertextModulus
250  << " bigru=" << bigRootOfUnity << "]";
251  return out;
252  }
253 };
254 
255 } // namespace lbcrypto
256 
257 #endif
usint GetCyclotomicOrder() const
Simple getter method for cyclotomic order.
Definition: elemparams.h:124
Base class for PALISADE serialization.
Definition: serializable.h:76
const IntegerType & GetBigRootOfUnity() const
Simple getter method for the big root of unity.
Definition: elemparams.h:166
usint GetRingDimension() const
Simple ring dimension getter method. The ring dimension is the evaluation of the totient function of ...
Definition: elemparams.h:131
friend std::ostream & operator<<(std::ostream &out, const ElemParams &item)
Output strem operator.
Definition: elemparams.h:174
virtual ~ElemParams()
Simple destructor method.
Definition: elemparams.h:118
uint64_t GetTotient(const uint64_t n)
Definition: nbtheory2.cpp:87
ElemParams(const ElemParams &&rhs)
Copy constructor using move semnantics to copy wrapped elements.
Definition: elemparams.h:89
ElemParams(const ElemParams &rhs)
Copy constructor using assignment to copy wrapped elements.
Definition: elemparams.h:74
Definition: elemparams.h:43
const ElemParams & operator=(const ElemParams &rhs)
Assignment operator using assignment operations of wrapped elements.
Definition: elemparams.h:103
bool operator!=(const ElemParams< IntegerType > &other) const
Inequality operator that tests the equality of all wrapped values.
Definition: elemparams.h:197
Definition: exception.h:147
ElemParams(usint order, const IntegerType &ctModulus, const IntegerType &rUnity=IntegerType(0), const IntegerType &bigCtModulus=IntegerType(0), const IntegerType &bigRUnity=IntegerType(0))
Simple constructor method that takes as input root of unity, big root of unity, cyclotomic order and ...
Definition: elemparams.h:56
bool OrderIsPowerOfTwo() const
Returns True if the cyclotomic order or ring dimension is a power of 2.
Definition: elemparams.h:139
const IntegerType & GetModulus() const
Simple getter method for the ciphertext modulus, not the big ciphertext modulus.
Definition: elemparams.h:146
const IntegerType & GetRootOfUnity() const
Simple getter method for the root of unity, not the big root of unity.
Definition: elemparams.h:160
virtual bool operator==(const ElemParams< IntegerType > &other) const
Equality operator that tests the equality of all wrapped values.
Definition: elemparams.h:183
virtual std::ostream & doprint(std::ostream &out) const
Pretty print operator for the ElemParams type.
Definition: elemparams.h:246
const IntegerType & GetBigModulus() const
Simpler getter method for the big ciphertext modulus. This is not relevant for all applications...
Definition: elemparams.h:153
Definition: binfhecontext.h:36