PALISADE Lattice Crypto Library  1.11.9
A lattice crypto library for software engineers by software engineers.
encodingparams.h
1 // @file encodingparams.h Represents and defines parameters for plaintext
2 // encoding.
3 // @author TPOC: contact@palisade-crypto.org
4 //
5 // @copyright Copyright (c) 2019, New Jersey Institute of Technology (NJIT)
6 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are met:
9 // 1. Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution. THIS SOFTWARE IS
14 // PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
15 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17 // EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 
25 #ifndef LBCRYPTO_ENCODING_ENCODINGPARAMS_H
26 #define LBCRYPTO_ENCODING_ENCODINGPARAMS_H
27 
28 #include <memory>
29 #include <string>
30 #include <utility>
31 
32 #include "math/backend.h"
33 
34 namespace lbcrypto {
35 class EncodingParamsImpl;
36 
37 typedef std::shared_ptr<EncodingParamsImpl> EncodingParams;
38 typedef uint64_t PlaintextModulus;
39 
45  public:
58  EncodingParamsImpl(PlaintextModulus plaintextModulus = 0,
59  uint32_t batchSize = 0, uint32_t plaintextGenerator = 0,
60  NativeInteger plaintextRootOfUnity = 0,
61  NativeInteger plaintextBigModulus = 0,
62  NativeInteger plaintextBigRootOfUnity = 0) {
63  m_plaintextModulus = plaintextModulus;
64  m_plaintextRootOfUnity = plaintextRootOfUnity;
65  m_plaintextBigModulus = plaintextBigModulus;
66  m_plaintextBigRootOfUnity = plaintextBigRootOfUnity;
67  m_batchSize = batchSize;
68  m_plaintextGenerator = plaintextGenerator;
69  }
70 
77  m_plaintextModulus = rhs.m_plaintextModulus;
78  m_plaintextRootOfUnity = rhs.m_plaintextRootOfUnity;
79  m_plaintextBigModulus = rhs.m_plaintextBigModulus;
80  m_plaintextBigRootOfUnity = rhs.m_plaintextBigRootOfUnity;
81  m_plaintextGenerator = rhs.m_plaintextGenerator;
82  m_batchSize = rhs.m_batchSize;
83  }
84 
91  m_plaintextModulus = std::move(rhs.m_plaintextModulus);
92  m_plaintextRootOfUnity = std::move(rhs.m_plaintextRootOfUnity);
93  m_plaintextBigModulus = std::move(rhs.m_plaintextBigModulus);
94  m_plaintextBigRootOfUnity = std::move(rhs.m_plaintextBigRootOfUnity);
95  m_plaintextGenerator = std::move(rhs.m_plaintextGenerator);
96  m_batchSize = rhs.m_batchSize;
97  }
98 
106  m_plaintextModulus = rhs.m_plaintextModulus;
107  m_plaintextRootOfUnity = rhs.m_plaintextRootOfUnity;
108  m_plaintextBigModulus = rhs.m_plaintextBigModulus;
109  m_plaintextBigRootOfUnity = rhs.m_plaintextBigRootOfUnity;
110  m_plaintextGenerator = rhs.m_plaintextGenerator;
111  m_batchSize = rhs.m_batchSize;
112  return *this;
113  }
114 
118  virtual ~EncodingParamsImpl() {}
119 
120  // ACCESSORS
121 
122  // Get accessors
123 
128  const PlaintextModulus &GetPlaintextModulus() const {
129  return m_plaintextModulus;
130  }
131 
135  void SetPlaintextModulus(PlaintextModulus plaintextModulus) {
136  m_plaintextModulus = plaintextModulus;
137  }
138 
144  return m_plaintextRootOfUnity;
145  }
146 
150  void SetPlaintextRootOfUnity(const NativeInteger &plaintextRootOfUnity) {
151  m_plaintextRootOfUnity = plaintextRootOfUnity;
152  }
153 
159  return m_plaintextBigModulus;
160  }
161 
165  void SetPlaintextBigModulus(const NativeInteger &plaintextBigModulus) {
166  m_plaintextBigModulus = plaintextBigModulus;
167  }
168 
174  return m_plaintextBigRootOfUnity;
175  }
176 
181  const NativeInteger &plaintextBigRootOfUnity) {
182  m_plaintextBigRootOfUnity = plaintextBigRootOfUnity;
183  }
184 
189  usint GetPlaintextGenerator() const { return m_plaintextGenerator; }
190 
194  void SetPlaintextGenerator(usint &plaintextGenerator) {
195  m_plaintextGenerator = plaintextGenerator;
196  }
197 
202  uint32_t GetBatchSize() const { return m_batchSize; }
203 
207  void SetBatchSize(usint batchSize) { m_batchSize = batchSize; }
208 
209  // Operators
216  friend std::ostream &operator<<(std::ostream &out,
217  const EncodingParamsImpl &item) {
218  return item.doprint(out);
219  }
226  bool operator==(const EncodingParamsImpl &other) const {
227  return m_plaintextModulus == other.m_plaintextModulus &&
228  m_plaintextRootOfUnity == other.m_plaintextRootOfUnity &&
229  m_plaintextBigModulus == other.m_plaintextBigModulus &&
230  m_plaintextBigRootOfUnity == other.m_plaintextBigRootOfUnity &&
231  m_plaintextGenerator == other.m_plaintextGenerator &&
232  m_batchSize == other.m_batchSize;
233  }
240  bool operator!=(const EncodingParamsImpl &other) const {
241  return !(*this == other);
242  }
243 
244  private:
245  std::ostream &doprint(std::ostream &out) const {
246  out << "[p=" << m_plaintextModulus << " rootP =" << m_plaintextRootOfUnity
247  << " bigP =" << m_plaintextBigModulus
248  << " rootBigP =" << m_plaintextBigRootOfUnity
249  << " g=" << m_plaintextGenerator << " L=" << m_batchSize << "]";
250  return out;
251  }
252 
253  // plaintext modulus that is used by all schemes
254  PlaintextModulus m_plaintextModulus;
255  // root of unity for plaintext modulus
256  NativeInteger m_plaintextRootOfUnity;
257  // big plaintext modulus that is used for arbitrary cyclotomics
258  NativeInteger m_plaintextBigModulus;
259  // root of unity for big plaintext modulus
260  NativeInteger m_plaintextBigRootOfUnity;
261  // plaintext generator is used for packed encoding (to find the correct
262  // automorphism index)
263  uint32_t m_plaintextGenerator;
264  // maximum batch size used by EvalSumKeyGen for packed encoding
265  uint32_t m_batchSize;
266 
267  public:
268  template <class Archive>
269  void save(Archive &ar, std::uint32_t const version) const {
270  ar(::cereal::make_nvp("m", m_plaintextModulus));
271  ar(::cereal::make_nvp("ru", m_plaintextRootOfUnity));
272  ar(::cereal::make_nvp("bm", m_plaintextBigModulus));
273  ar(::cereal::make_nvp("bru", m_plaintextBigRootOfUnity));
274  ar(::cereal::make_nvp("g", m_plaintextGenerator));
275  ar(::cereal::make_nvp("bs", m_batchSize));
276  }
277 
278  template <class Archive>
279  void load(Archive &ar, std::uint32_t const version) {
280  if (version > SerializedVersion()) {
281  PALISADE_THROW(deserialize_error,
282  "serialized object version " + std::to_string(version) +
283  " is from a later version of the library");
284  }
285  ar(::cereal::make_nvp("m", m_plaintextModulus));
286  ar(::cereal::make_nvp("ru", m_plaintextRootOfUnity));
287  ar(::cereal::make_nvp("bm", m_plaintextBigModulus));
288  ar(::cereal::make_nvp("bru", m_plaintextBigRootOfUnity));
289  ar(::cereal::make_nvp("g", m_plaintextGenerator));
290  ar(::cereal::make_nvp("bs", m_batchSize));
291  }
292 
293  std::string SerializedObjectName() const { return "EncodingParms"; }
294  static uint32_t SerializedVersion() { return 1; }
295 };
296 
297 inline std::ostream &operator<<(std::ostream &out,
298  std::shared_ptr<EncodingParamsImpl> o) {
299  if (o) out << *o;
300  return out;
301 }
302 inline bool operator==(std::shared_ptr<EncodingParamsImpl> o1,
303  std::shared_ptr<EncodingParamsImpl> o2) {
304  if (o1 && o2) return *o1 == *o2;
305  if (!o1 && !o2) return true;
306  return false;
307 }
308 
309 } // namespace lbcrypto
310 
311 #endif
Base class for PALISADE serialization.
Definition: serializable.h:76
bool operator==(const EncodingParamsImpl &other) const
Equality operator for the parameters. Tests that all the parameters are equal.
Definition: encodingparams.h:226
EncodingParamsImpl(const EncodingParamsImpl &&rhs)
Definition: encodingparams.h:90
void SetPlaintextGenerator(usint &plaintextGenerator)
Setter for the plaintext generator.
Definition: encodingparams.h:194
void SetPlaintextBigModulus(const NativeInteger &plaintextBigModulus)
Setter for the big plaintext modulus.
Definition: encodingparams.h:165
All parameters for plaintext encodings into ciphertext space.
Definition: encodingparams.h:44
void SetPlaintextModulus(PlaintextModulus plaintextModulus)
Setter for the plaintext modulus.
Definition: encodingparams.h:135
EncodingParamsImpl(const EncodingParamsImpl &rhs)
Definition: encodingparams.h:76
uint32_t GetBatchSize() const
Getter for the plaintext batch size.
Definition: encodingparams.h:202
Definition: exception.h:147
void SetPlaintextRootOfUnity(const NativeInteger &plaintextRootOfUnity)
Setter for the plaintext modulus root of unity.
Definition: encodingparams.h:150
usint GetPlaintextGenerator() const
Getter for the plaintext generator.
Definition: encodingparams.h:189
void SetBatchSize(usint batchSize)
Setter for the batch size.
Definition: encodingparams.h:207
const PlaintextModulus & GetPlaintextModulus() const
Getter for the plaintext modulus.
Definition: encodingparams.h:128
friend std::ostream & operator<<(std::ostream &out, const EncodingParamsImpl &item)
output stream operator.
Definition: encodingparams.h:216
const NativeInteger & GetPlaintextBigModulus() const
Getter for the big plaintext modulus.
Definition: encodingparams.h:158
const NativeInteger & GetPlaintextRootOfUnity() const
Getter for the plaintext modulus root of unity.
Definition: encodingparams.h:143
const EncodingParamsImpl & operator=(const EncodingParamsImpl &rhs)
Definition: encodingparams.h:105
virtual ~EncodingParamsImpl()
Definition: encodingparams.h:118
const NativeInteger & GetPlaintextBigRootOfUnity() const
Getter for the big plaintext modulus root of unity.
Definition: encodingparams.h:173
EncodingParamsImpl(PlaintextModulus plaintextModulus=0, uint32_t batchSize=0, uint32_t plaintextGenerator=0, NativeInteger plaintextRootOfUnity=0, NativeInteger plaintextBigModulus=0, NativeInteger plaintextBigRootOfUnity=0)
Definition: encodingparams.h:58
void SetPlaintextBigRootOfUnity(const NativeInteger &plaintextBigRootOfUnity)
Setter for the big plaintext modulus root of unity.
Definition: encodingparams.h:180
Definition: binfhecontext.h:36
bool operator!=(const EncodingParamsImpl &other) const
Inequality operator for the parameters. Tests that all the parameters are not equal.
Definition: encodingparams.h:240
Main class for big integers represented as an array of native (primitive) unsigned integers...
Definition: backend.h:60