PALISADE Lattice Crypto Library  1.11.9
A lattice crypto library for software engineers by software engineers.
field2n.h
1 // @file field2n.h Represents and defines power-of-2 fields.
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_LIB_LATTICE_SIGNATURE_FIELD2N_H
25 #define _SRC_LIB_LATTICE_SIGNATURE_FIELD2N_H
26 
27 #include <limits>
28 #include <string>
29 #include <vector>
30 
31 #include "lattice/backend.h"
32 #include "math/backend.h"
33 #include "math/dftransfrm.h"
34 #include "math/matrix.h"
35 #include "math/transfrm.h"
36 
37 namespace lbcrypto {
42 class Field2n : public std::vector<std::complex<double>>, public Serializable {
43  public:
47  Field2n() : format(Format::COEFFICIENT) {}
48 
49  explicit Field2n(Format f) : format(f) {}
50 
59  Field2n(int size, Format f = Format::EVALUATION,
60  bool initializeElementToZero = false)
61  : std::vector<std::complex<double>>(
62  size,
63  initializeElementToZero ? 0 : -std::numeric_limits<double>::max()) {
64  this->format = f;
65  }
66 
71  explicit Field2n(const Poly &element);
72 
77  explicit Field2n(const NativePoly &element);
78 
83  explicit Field2n(const DCRTPoly &element);
84 
89  explicit Field2n(const Matrix<int64_t> &element);
90 
96  Format GetFormat() const { return format; }
97 
103  Field2n Inverse() const;
104 
111  Field2n Plus(const Field2n &rhs) const;
112 
119  Field2n Plus(double rhs) const;
120 
127  Field2n Minus(const Field2n &rhs) const;
128 
135  Field2n Times(const Field2n &rhs) const;
136 
143 
150  Field2n AutomorphismTransform(size_t i) const;
151 
158  Field2n Transpose() const;
159 
165  Field2n ExtractOdd() const;
166 
172  Field2n ExtractEven() const;
173 
180  Field2n Permute() const;
181 
188  Field2n InversePermute() const;
189 
196  Field2n ScalarMult(double d);
197 
201  void SwitchFormat();
202 
209  inline void SetFormat(Format format) {
210  if (this->format != format) {
211  SwitchFormat();
212  }
213  }
214 
220  size_t Size() const { return this->size(); }
221 
228  inline std::complex<double> &operator[](std::size_t idx) {
229  return (this->at(idx));
230  }
231 
238  inline const std::complex<double> &operator[](std::size_t idx) const {
239  return (this->at(idx));
240  }
241 
248  const Field2n &operator+=(const Field2n &element) {
249  return *this = this->Plus(element);
250  }
251 
258  const Field2n &operator-=(const Field2n &element) {
259  return *this = this->Minus(element);
260  }
261 
266  Field2n operator-() const {
267  Field2n all0(size(), this->GetFormat(), true);
268  return all0 - *this;
269  }
270 
278  friend inline Field2n operator-(const Field2n &a, const Field2n &b) {
279  return a.Minus(b);
280  }
281 
289  friend inline Field2n operator+(const Field2n &a, const Field2n &b) {
290  return a.Plus(b);
291  }
292 
300  friend inline Field2n operator+(const Field2n &a, double scalar) {
301  return a.Plus(scalar);
302  }
303 
311  friend inline Field2n operator*(const Field2n &a, const Field2n &b) {
312  return a.Times(b);
313  }
314 
315  template <class Archive>
316  void save(Archive &ar, std::uint32_t const version) const {
317  ar(::cereal::base_class<std::vector<std::complex<double>>>(this));
318  ar(::cereal::make_nvp("f", format));
319  }
320 
321  template <class Archive>
322  void load(Archive &ar, std::uint32_t const version) {
323  if (version > SerializedVersion()) {
324  PALISADE_THROW(deserialize_error,
325  "serialized object version " + std::to_string(version) +
326  " is from a later version of the library");
327  }
328  ar(::cereal::base_class<std::vector<std::complex<double>>>(this));
329  ar(::cereal::make_nvp("f", format));
330  }
331 
332  std::string SerializedObjectName() const { return "Field2n"; }
333  static uint32_t SerializedVersion() { return 1; }
334 
335  private:
336  // Format of the field element
337  Format format;
338 };
339 
347 inline std::ostream &operator<<(std::ostream &os, const Field2n &m) {
348  os << "[ ";
349  for (size_t row = 0; row < m.size(); ++row) {
350  os << m.at(row) << " ";
351  }
352  os << " ]\n";
353  return os;
354 }
355 
356 } // namespace lbcrypto
357 #endif
Field2n AutomorphismTransform(size_t i) const
Performs an automorphism transform operation and returns the result.
Definition: field2n.cpp:197
friend Field2n operator+(const Field2n &a, double scalar)
Scalar addition operator for field elements.
Definition: field2n.h:300
Field2n ExtractEven() const
Function for extracting even factors of the field element.
Definition: field2n.cpp:251
Base class for PALISADE serialization.
Definition: serializable.h:76
Field2n Permute() const
Permutation operation defined in Algorithm 4 of https://eprint.iacr.org/2017/844.pdf.
Definition: field2n.cpp:266
Field2n Inverse() const
Inverse operation for the field elements.
Definition: field2n.cpp:109
Field2n InversePermute() const
Inverse operation for permutation operation defined in Algorithm 4 of https://eprint.iacr.org/2017/844.pdf.
Definition: field2n.cpp:289
Field2n Times(const Field2n &rhs) const
Multiplication operation for field elements.
Definition: field2n.cpp:165
A class to represent field elements with power-of-2 dimension.
Definition: field2n.h:42
friend Field2n operator-(const Field2n &a, const Field2n &b)
Substraction operator for field elements.
Definition: field2n.h:278
Definition: stl_allocator.h:124
size_t Size() const
Method for getting the size of the element.
Definition: field2n.h:220
void SetFormat(Format format)
Sets the evaluation or coefficient representation of the field elements.
Definition: field2n.h:209
Definition: exception.h:147
Field2n operator-() const
Unary minus on a field element.
Definition: field2n.h:266
Field2n ScalarMult(double d)
Operation for scalar multiplication.
Definition: field2n.cpp:308
Format GetFormat() const
Method for getting the format/representation of the element.
Definition: field2n.h:96
Field2n Minus(const Field2n &rhs) const
Substraction operation for field elements.
Definition: field2n.cpp:152
std::complex< double > & operator[](std::size_t idx)
Indexing operator for field elements.
Definition: field2n.h:228
Definition: matrix.h:47
Field2n Transpose() const
Transpose operation defined in section VI.B4 of https://eprint.iacr.org/2017/844.pdf.
Definition: field2n.cpp:221
Field2n ShiftRight()
Right shift operation for the field element.
Definition: field2n.cpp:180
const std::complex< double > & operator[](std::size_t idx) const
Indexing operator for field elements.
Definition: field2n.h:238
Field2n(int size, Format f=Format::EVALUATION, bool initializeElementToZero=false)
Constructor for field element.
Definition: field2n.h:59
Field2n Plus(const Field2n &rhs) const
Addition operation for field elements.
Definition: field2n.cpp:126
friend Field2n operator+(const Field2n &a, const Field2n &b)
Addition operator for field elements.
Definition: field2n.h:289
const Field2n & operator+=(const Field2n &element)
In-place addition operation for field elements.
Definition: field2n.h:248
friend Field2n operator*(const Field2n &a, const Field2n &b)
Multiplication operator for field elements.
Definition: field2n.h:311
void SwitchFormat()
Method for switching format of the field elements.
Definition: field2n.cpp:317
Definition: binfhecontext.h:36
const Field2n & operator-=(const Field2n &element)
In-place subtraction operation for field elements.
Definition: field2n.h:258
Field2n()
Default Constructor.
Definition: field2n.h:47
Field2n ExtractOdd() const
Function for extracting odd factors of the field element.
Definition: field2n.cpp:237