PolyMod.h
1 /* Copyright (C) 2019 IBM Corp.
2  * This program is Licensed under the Apache License, Version 2.0
3  * (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software
7  * distributed under the License is distributed on an "AS IS" BASIS,
8  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9  * See the License for the specific language governing permissions and
10  * limitations under the License. See accompanying LICENSE file.
11  */
12 
13 #ifndef HELIB_POLYMOD_H
14 #define HELIB_POLYMOD_H
15 
16 #include <NTL/ZZX.h>
17 #include <vector>
18 #include <memory>
19 
20 #include <helib/PolyModRing.h>
21 #include <helib/JsonWrapper.h>
22 
27 namespace helib {
28 
48 class PolyMod
49 {
50 public:
57  PolyMod();
58 
66  explicit PolyMod(const std::shared_ptr<PolyModRing>& ringDescriptor);
67 
76  PolyMod(long input, const std::shared_ptr<PolyModRing>& ringDescriptor);
77 
87  PolyMod(const std::vector<long>& input,
88  const std::shared_ptr<PolyModRing>& ringDescriptor);
89 
97  PolyMod(const NTL::ZZX& input,
98  const std::shared_ptr<PolyModRing>& ringDescriptor);
99 
104  PolyMod(const PolyMod& input) = default;
105 
109  PolyMod(PolyMod&& input) noexcept = default;
110 
114  ~PolyMod() = default;
115 
120  PolyMod& operator=(const PolyMod& input) = default;
121 
126  default; // noexcept removed as NTL has non-noexcept move constructors
132  PolyMod& operator=(long input);
133 
140  PolyMod& operator=(const std::vector<long>& input);
141 
148  PolyMod& operator=(const std::initializer_list<long>& input);
149 
154  PolyMod& operator=(const NTL::ZZX& input);
155 
161  explicit operator long() const;
162 
166  explicit operator std::vector<long>() const;
167 
171  explicit operator NTL::ZZX() const;
172 
178  bool isValid() const;
179 
184  long getp2r() const;
185 
190  NTL::ZZX getG() const;
191 
196  const NTL::ZZX& getData() const;
197 
203  bool operator==(const PolyMod& rhs) const;
204 
211  bool operator==(long rhs) const;
212 
219  bool operator==(const std::vector<long>& rhs) const;
220 
227  bool operator==(const NTL::ZZX& rhs) const;
228 
235  template <typename T>
236  bool operator!=(T&& rhs) const
237  {
238  return !operator==(std::forward<T>(rhs));
239  }
240 
245  PolyMod& negate();
246 
251  PolyMod operator-() const;
252 
258  PolyMod operator*(const PolyMod& rhs) const;
259 
265  PolyMod operator*(long rhs) const;
266 
272  PolyMod operator*(const NTL::ZZX& rhs) const;
273 
279  PolyMod operator+(const PolyMod& rhs) const;
280 
286  PolyMod operator+(long rhs) const;
287 
293  PolyMod operator+(const NTL::ZZX& rhs) const;
294 
300  PolyMod operator-(const PolyMod& rhs) const;
301 
307  PolyMod operator-(long rhs) const;
308 
314  PolyMod operator-(const NTL::ZZX& rhs) const;
315 
321  PolyMod& operator*=(const PolyMod& otherPoly);
322 
328  PolyMod& operator*=(long scalar);
329 
335  PolyMod& operator*=(const NTL::ZZX& otherPoly);
336 
342  PolyMod& operator+=(const PolyMod& otherPoly);
343 
349  PolyMod& operator+=(long scalar);
350 
356  PolyMod& operator+=(const NTL::ZZX& otherPoly);
357 
363  PolyMod& operator-=(const PolyMod& otherPoly);
364 
370  PolyMod& operator-=(long scalar);
371 
377  PolyMod& operator-=(const NTL::ZZX& otherPoly);
378 
390  void writeToJSON(std::ostream& os) const;
391 
403  JsonWrapper writeToJSON() const;
404 
422  static PolyMod readFromJSON(
423  std::istream& is,
424  const std::shared_ptr<PolyModRing>& ringDescriptor);
425 
442  static PolyMod readFromJSON(
443  const JsonWrapper& jw,
444  const std::shared_ptr<PolyModRing>& ringDescriptor);
445 
464  void readJSON(std::istream& is);
465 
484  void readJSON(const JsonWrapper& jw);
485 
486  // TODO: serialization 2.0: fix the following comment
509  friend std::istream& operator>>(std::istream& is, PolyMod& poly);
510 
511  // TODO: serialization 2.0: fix the following comment
527  friend std::ostream& operator<<(std::ostream& os, const PolyMod& poly);
528 
529 private:
534  std::shared_ptr<PolyModRing> ringDescriptor;
535 
539  NTL::ZZX data;
540 
545  void modularReduce();
546 
552  static void assertValidity(const PolyMod& poly);
553 
557  static void assertInterop(const PolyMod& lhs, const PolyMod& rhs);
558 };
559 
560 } // namespace helib
561 #endif
An object that contains an NTL::ZZX polynomial along with a coefficient modulus p2r and a polynomial ...
Definition: PolyMod.h:49
friend std::istream & operator>>(std::istream &is, PolyMod &poly)
Input shift operator.
Definition: PolyMod.cpp:357
bool isValid() const
Gets the validity of this. This will be false iff this was default constructed.
Definition: PolyMod.cpp:101
static PolyMod readFromJSON(std::istream &is, const std::shared_ptr< PolyModRing > &ringDescriptor)
Deserialize a PolyMod object from the input stream is.
Definition: PolyMod.cpp:303
PolyMod & operator*=(const PolyMod &otherPoly)
Times equals operator with PolyMod rhs.
Definition: PolyMod.cpp:217
long getp2r() const
Get current p^r value.
Definition: PolyMod.cpp:103
PolyMod & operator+=(const PolyMod &otherPoly)
Plus equals operator with PolyMod rhs.
Definition: PolyMod.cpp:241
PolyMod & operator=(const PolyMod &input)=default
Assignment operator.
PolyMod(PolyMod &&input) noexcept=default
Default move constructor.
PolyMod operator*(const PolyMod &rhs) const
Infix multiplication operator.
Definition: PolyMod.cpp:163
PolyMod operator+(const PolyMod &rhs) const
Infix plus operator.
Definition: PolyMod.cpp:181
void readJSON(std::istream &is)
In-place deserialize a PolyMod object from the input stream is.
Definition: PolyMod.cpp:321
JsonWrapper writeToJSON() const
Serialize this PolyMod to the JsonWrapper object.
Definition: PolyMod.cpp:295
PolyMod & negate()
Negate function.
Definition: PolyMod.cpp:148
PolyMod operator-() const
Unary minus operator.
Definition: PolyMod.cpp:155
NTL::ZZX getG() const
Get current G value.
Definition: PolyMod.cpp:105
PolyMod(const PolyMod &input)=default
Default copy constructor.
PolyMod & operator=(PolyMod &&input)=default
default move assignment operator
bool operator!=(T &&rhs) const
Not equals operator.
Definition: PolyMod.h:236
~PolyMod()=default
Default destructor.
const NTL::ZZX & getData() const
Getter function that returns the data of PolyMod as an NTL::ZZX const reference.
Definition: PolyMod.cpp:107
friend std::ostream & operator<<(std::ostream &os, const PolyMod &poly)
Output shift operator.
Definition: PolyMod.cpp:365
bool operator==(const PolyMod &rhs) const
Equals operator between two PolyMod objects.
Definition: PolyMod.cpp:113
PolyMod()
Default constructor.
Definition: PolyMod.cpp:24
PolyMod & operator-=(const PolyMod &otherPoly)
Minus equals operator with PolyMod rhs.
Definition: PolyMod.cpp:265
Definition: apiAttributes.h:21
Definition: JsonWrapper.h:9