Search Results

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 #include <helib/PolyModRing.h>
20 
25 namespace helib {
26 
46 class PolyMod
47 {
48 public:
55  PolyMod();
56 
64  explicit PolyMod(const std::shared_ptr<PolyModRing>& ringDescriptor);
65 
74  PolyMod(long input, const std::shared_ptr<PolyModRing>& ringDescriptor);
75 
85  PolyMod(const std::vector<long>& input,
86  const std::shared_ptr<PolyModRing>& ringDescriptor);
87 
95  PolyMod(const NTL::ZZX& input,
96  const std::shared_ptr<PolyModRing>& ringDescriptor);
97 
102  PolyMod(const PolyMod& input) = default;
103 
107  PolyMod(PolyMod&& input) noexcept = default;
108 
112  ~PolyMod() = default;
113 
118  PolyMod& operator=(const PolyMod& input) = default;
119 
124  default; // noexcept removed as NTL has non-noexcept move constructors
130  PolyMod& operator=(long input);
131 
138  PolyMod& operator=(const std::vector<long>& input);
139 
146  PolyMod& operator=(const std::initializer_list<long>& input);
147 
152  PolyMod& operator=(const NTL::ZZX& input);
153 
159  explicit operator long() const;
160 
164  explicit operator std::vector<long>() const;
165 
169  explicit operator NTL::ZZX() const;
170 
176  bool isValid() const;
177 
182  long getp2r() const;
183 
188  NTL::ZZX getG() const;
189 
194  const NTL::ZZX& getData() const;
195 
201  bool operator==(const PolyMod& rhs) const;
202 
209  bool operator==(long rhs) const;
210 
217  bool operator==(const std::vector<long>& rhs) const;
218 
225  bool operator==(const NTL::ZZX& rhs) const;
226 
233  template <typename T>
234  bool operator!=(T&& rhs) const
235  {
236  return !operator==(std::forward<T>(rhs));
237  }
238 
243  PolyMod& negate();
244 
249  PolyMod operator-() const;
250 
256  PolyMod operator*(const PolyMod& rhs) const;
257 
263  PolyMod operator*(long rhs) const;
264 
270  PolyMod operator*(const NTL::ZZX& rhs) const;
271 
277  PolyMod operator+(const PolyMod& rhs) const;
278 
284  PolyMod operator+(long rhs) const;
285 
291  PolyMod operator+(const NTL::ZZX& rhs) const;
292 
298  PolyMod operator-(const PolyMod& rhs) const;
299 
305  PolyMod operator-(long rhs) const;
306 
312  PolyMod operator-(const NTL::ZZX& rhs) const;
313 
319  PolyMod& operator*=(const PolyMod& otherPoly);
320 
326  PolyMod& operator*=(long scalar);
327 
333  PolyMod& operator*=(const NTL::ZZX& otherPoly);
334 
340  PolyMod& operator+=(const PolyMod& otherPoly);
341 
347  PolyMod& operator+=(long scalar);
348 
354  PolyMod& operator+=(const NTL::ZZX& otherPoly);
355 
361  PolyMod& operator-=(const PolyMod& otherPoly);
362 
368  PolyMod& operator-=(long scalar);
369 
375  PolyMod& operator-=(const NTL::ZZX& otherPoly);
376 
398  friend void deserialize(std::istream& is, PolyMod& poly);
399 
415  friend void serialize(std::ostream& os, const PolyMod& slot);
416 
439  friend std::istream& operator>>(std::istream& is, PolyMod& poly);
440 
456  friend std::ostream& operator<<(std::ostream& os, const PolyMod& poly);
457 
458 private:
463  std::shared_ptr<PolyModRing> ringDescriptor;
464 
468  NTL::ZZX data;
469 
474  void modularReduce();
475 
481  static void assertValidity(const PolyMod& poly);
482 
486  static void assertInterop(const PolyMod& lhs, const PolyMod& rhs);
487 };
488 
489 } // namespace helib
490 #endif
friend std::ostream & operator<<(std::ostream &os, const PolyMod &poly)
Output shift operator.
Definition: PolyMod.cpp:344
PolyMod operator*(const PolyMod &rhs) const
Infix multiplication operator.
Definition: PolyMod.cpp:161
PolyMod()
Default constructor.
Definition: PolyMod.cpp:22
const NTL::ZZX & getData() const
Getter function that returns the data of PolyMod as an NTL::ZZX const reference.
Definition: PolyMod.cpp:105
PolyMod operator+(const PolyMod &rhs) const
Infix plus operator.
Definition: PolyMod.cpp:179
bool operator==(const PolyMod &rhs) const
Equals operator between two PolyMod objects.
Definition: PolyMod.cpp:111
PolyMod & operator=(const PolyMod &input)=default
Assignment operator.
long getp2r() const
Get current p^r value.
Definition: PolyMod.cpp:101
friend std::istream & operator>>(std::istream &is, PolyMod &poly)
Input shift operator.
Definition: PolyMod.cpp:336
NTL::ZZX getG() const
Get current G value.
Definition: PolyMod.cpp:103
PolyMod(const PolyMod &input)=default
Default copy constructor.
PolyMod & operator=(PolyMod &&input)=default
default move assignment operator
PolyMod & operator+=(const PolyMod &otherPoly)
Plus equals operator with PolyMod rhs.
Definition: PolyMod.cpp:239
PolyMod operator-() const
Unary minus operator.
Definition: PolyMod.cpp:153
bool operator!=(T &&rhs) const
Not equals operator.
Definition: PolyMod.h:234
friend void serialize(std::ostream &os, const PolyMod &slot)
Serialize a PolyMod to the output stream os.
Definition: PolyMod.cpp:315
~PolyMod()=default
Default destructor.
PolyMod & operator-=(const PolyMod &otherPoly)
Minus equals operator with PolyMod rhs.
Definition: PolyMod.cpp:263
PolyMod(PolyMod &&input) noexcept=default
Default move constructor.
Definition: apiAttributes.h:21
friend void deserialize(std::istream &is, PolyMod &poly)
Deserialize a PolyMod object from the input stream is.
Definition: PolyMod.cpp:287
PolyMod & negate()
Negate function.
Definition: PolyMod.cpp:146
PolyMod & operator*=(const PolyMod &otherPoly)
Times equals operator with PolyMod rhs.
Definition: PolyMod.cpp:215
An object that contains an NTL::ZZX polynomial along with a coefficient modulus p2r and a polynomial ...
Definition: PolyMod.h:47
bool isValid() const
Gets the validity of this. This will be false iff this was default constructed.
Definition: PolyMod.cpp:99