PALISADE Lattice Crypto Library  1.11.9
A lattice crypto library for software engineers by software engineers.
mubintvecfxd.h
1 // @file mubintvecfxd.h This file contains the vector manipulation
2 // functionality.
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_MATH_BIGINTFXD_MUBINVECFXD_H
26 #define LBCRYPTO_MATH_BIGINTFXD_MUBINVECFXD_H
27 
28 #include <iostream>
29 #include <string>
30 
31 #include "utils/inttypes.h"
32 #include "utils/serializable.h"
33 
34 #include "math/bigintfxd/ubintfxd.h"
35 
40 namespace bigintfxd {
41 
45 template <class IntegerType>
47  : public lbcrypto::BigVectorInterface<BigVectorImpl<IntegerType>,
48  IntegerType>,
49  public lbcrypto::Serializable {
50  public:
51  // CONSTRUCTORS
52 
56  BigVectorImpl();
57 
58  static inline BigVectorImpl Single(const IntegerType &val,
59  const IntegerType &modulus) {
60  BigVectorImpl vec(1, modulus);
61  vec[0] = val;
62  return vec;
63  }
64 
72  explicit BigVectorImpl(usint length, const IntegerType &modulus = 0);
73 
79  BigVectorImpl(const BigVectorImpl &bigVector);
80 
86  BigVectorImpl(BigVectorImpl &&bigVector); // move copy constructor
87 
98  BigVectorImpl(usint length, const IntegerType &modulus,
99  std::initializer_list<std::string> rhs);
100 
110  BigVectorImpl(usint length, const IntegerType &modulus,
111  std::initializer_list<uint64_t> rhs);
112 
116  virtual ~BigVectorImpl();
117 
118  // ASSIGNMENT OPERATORS
119 
126  const BigVectorImpl &operator=(const BigVectorImpl &rhs);
127 
134  const BigVectorImpl &operator=(BigVectorImpl &&rhs);
135 
143  const BigVectorImpl &operator=(std::initializer_list<std::string> rhs);
144 
151  const BigVectorImpl &operator=(std::initializer_list<uint64_t> rhs);
152 
160  const BigVectorImpl &operator=(uint64_t val) {
161  this->m_data[0] = val;
162  if (this->m_modulus != 0) {
163  this->m_data[0] %= this->m_modulus;
164  }
165  for (size_t i = 1; i < GetLength(); ++i) {
166  this->m_data[i] = 0;
167  }
168  return *this;
169  }
170 
171  // ACCESSORS
172 
179  IntegerType &at(size_t i) {
180  if (!this->IndexCheck(i)) {
181  PALISADE_THROW(lbcrypto::math_error, "BigVector index out of range");
182  }
183  return this->m_data[i];
184  }
185 
186  const IntegerType &at(size_t i) const {
187  if (!this->IndexCheck(i)) {
188  PALISADE_THROW(lbcrypto::math_error, "BigVector index out of range");
189  }
190  return this->m_data[i];
191  }
192 
198  IntegerType &operator[](size_t idx) { return (this->m_data[idx]); }
199 
200  const IntegerType &operator[](size_t idx) const {
201  return (this->m_data[idx]);
202  }
203 
210  void SetModulus(const IntegerType &value);
211 
217  void SwitchModulus(const IntegerType &value);
218 
224  const IntegerType &GetModulus() const { return this->m_modulus; }
225 
231  size_t GetLength() const { return this->m_length; }
232 
233  // MODULAR ARITHMETIC OPERATIONS
234 
241  BigVectorImpl Mod(const IntegerType &modulus) const;
242 
249  const BigVectorImpl &ModEq(const IntegerType &modulus);
250 
257  BigVectorImpl ModAdd(const IntegerType &b) const;
258 
265  const BigVectorImpl &ModAddEq(const IntegerType &b);
266 
274  BigVectorImpl ModAddAtIndex(usint i, const IntegerType &b) const;
275 
283  const BigVectorImpl &ModAddAtIndexEq(usint i, const IntegerType &b);
284 
291  BigVectorImpl ModAdd(const BigVectorImpl &b) const;
292 
299  const BigVectorImpl &ModAddEq(const BigVectorImpl &b);
300 
307  BigVectorImpl ModSub(const IntegerType &b) const;
308 
315  const BigVectorImpl &ModSubEq(const IntegerType &b);
316 
323  BigVectorImpl ModSub(const BigVectorImpl &b) const;
324 
331  const BigVectorImpl &ModSubEq(const BigVectorImpl &b);
332 
340  BigVectorImpl ModMul(const IntegerType &b) const;
341 
349  const BigVectorImpl &ModMulEq(const IntegerType &b);
350 
358  BigVectorImpl ModMul(const BigVectorImpl &b) const;
359 
367  const BigVectorImpl &ModMulEq(const BigVectorImpl &b);
368 
375  BigVectorImpl ModExp(const IntegerType &b) const;
376 
383  const BigVectorImpl &ModExpEq(const IntegerType &b);
384 
390  BigVectorImpl ModInverse() const;
391 
397  const BigVectorImpl &ModInverseEq();
398 
405  BigVectorImpl ModByTwo() const;
406 
413  const BigVectorImpl &ModByTwoEq();
414 
422 
431 
440  BigVectorImpl MultiplyAndRound(const IntegerType &p,
441  const IntegerType &q) const;
442 
451  const BigVectorImpl &MultiplyAndRoundEq(const IntegerType &p,
452  const IntegerType &q);
453 
461  BigVectorImpl DivideAndRound(const IntegerType &q) const;
462 
470  const BigVectorImpl &DivideAndRoundEq(const IntegerType &q);
471 
472  // OTHER FUNCTIONS
473 
491  BigVectorImpl GetDigitAtIndexForBase(usint index, usint base) const;
492 
493  // STRINGS & STREAMS
494 
502  template <class IntegerType_c>
503  friend std::ostream &operator<<(std::ostream &os,
504  const BigVectorImpl<IntegerType_c> &ptr_obj) {
505  auto len = ptr_obj.m_length;
506  os << "[";
507  for (usint i = 0; i < len; i++) {
508  os << ptr_obj.m_data[i];
509  os << ((i == (len - 1)) ? "]" : " ");
510  }
511  os << " modulus: " << ptr_obj.m_modulus;
512  return os;
513  }
514 
515  // SERIALIZATION
516 
517  template <class Archive>
518  typename std::enable_if<!cereal::traits::is_text_archive<Archive>::value,
519  void>::type
520  save(Archive &ar, std::uint32_t const version) const {
521  ar(::cereal::make_nvp("m", m_modulus));
522  ar(::cereal::make_nvp("l", m_length));
523  ar(::cereal::binary_data(m_data, sizeof(IntegerType) * m_length));
524  }
525 
526  template <class Archive>
527  typename std::enable_if<cereal::traits::is_text_archive<Archive>::value,
528  void>::type
529  save(Archive &ar, std::uint32_t const version) const {
530  ar(::cereal::make_nvp("m", m_modulus));
531  ar(::cereal::make_nvp("l", m_length));
532  for (size_t i = 0; i < m_length; i++) {
533  ar(m_data[i]);
534  }
535  }
536 
537  template <class Archive>
538  typename std::enable_if<!cereal::traits::is_text_archive<Archive>::value,
539  void>::type
540  load(Archive &ar, std::uint32_t const version) {
541  if (version > SerializedVersion()) {
542  PALISADE_THROW(lbcrypto::deserialize_error,
543  "serialized object version " + std::to_string(version) +
544  " is from a later version of the library");
545  }
546  ar(::cereal::make_nvp("m", m_modulus));
547  ar(::cereal::make_nvp("l", m_length));
548  m_data = new IntegerType[m_length]();
549  ar(::cereal::binary_data(m_data, sizeof(IntegerType) * m_length));
550  }
551 
552  template <class Archive>
553  typename std::enable_if<cereal::traits::is_text_archive<Archive>::value,
554  void>::type
555  load(Archive &ar, std::uint32_t const version) {
556  if (version > SerializedVersion()) {
557  PALISADE_THROW(lbcrypto::deserialize_error,
558  "serialized object version " + std::to_string(version) +
559  " is from a later version of the library");
560  }
561  ar(::cereal::make_nvp("m", m_modulus));
562  ar(::cereal::make_nvp("l", m_length));
563  m_data = new IntegerType[m_length]();
564  for (size_t i = 0; i < m_length; i++) {
565  ar(m_data[i]);
566  }
567  }
568 
569  std::string SerializedObjectName() const { return "FXDInteger"; }
570 
571  static uint32_t SerializedVersion() { return 1; }
572 
573  private:
574  // m_data is a pointer to the vector
575  IntegerType *m_data;
576  // m_length stores the length of the vector
577  usint m_length;
578  // m_modulus stores the internal modulus of the vector.
579  IntegerType m_modulus = 0;
580 
581  // function to check if the index is a valid index.
582  bool IndexCheck(size_t length) const {
583  if (length > this->m_length) {
584  return false;
585  }
586  return true;
587  }
588 };
589 
590 extern template class BigVectorImpl<
592 
593 } // namespace bigintfxd
594 
595 #endif // LBCRYPTO_MATH_BIGINTFXD_MUBINVECFXD_H
const BigVectorImpl & operator=(uint64_t val)
Definition: mubintvecfxd.h:160
BigVectorImpl ModExp(const IntegerType &b) const
Definition: mubintvecfxd.cpp:439
const BigVectorImpl & ModEq(const IntegerType &modulus)
Definition: mubintvecfxd.cpp:231
Base class for PALISADE serialization.
Definition: serializable.h:76
void SwitchModulus(const IntegerType &value)
Definition: mubintvecfxd.cpp:195
BigVectorImpl GetDigitAtIndexForBase(usint index, usint base) const
Definition: mubintvecfxd.cpp:573
IntegerType & operator[](size_t idx)
Definition: mubintvecfxd.h:198
BigVectorImpl ModByTwo() const
Definition: mubintvecfxd.cpp:471
const BigVectorImpl & MultiplyAndRoundEq(const IntegerType &p, const IntegerType &q)
Definition: mubintvecfxd.cpp:530
BigVectorImpl DivideAndRound(const IntegerType &q) const
Definition: mubintvecfxd.cpp:547
BigVectorImpl ModMul(const IntegerType &b) const
Definition: mubintvecfxd.cpp:349
const BigVectorImpl & ModMulEq(const IntegerType &b)
Definition: mubintvecfxd.cpp:380
size_t GetLength() const
Definition: mubintvecfxd.h:231
Definition: interface.h:588
BigVectorImpl MultWithOutMod(const BigVectorImpl &b) const
Definition: mubintvecfxd.cpp:499
void SetModulus(const IntegerType &value)
Definition: mubintvecfxd.cpp:185
BigVectorImpl ModAddAtIndex(usint i, const IntegerType &b) const
Definition: mubintvecfxd.cpp:267
Definition: exception.h:147
Definition: exception.h:113
const BigVectorImpl & ModAddEq(const IntegerType &b)
Definition: mubintvecfxd.cpp:257
BigVectorImpl()
Definition: mubintvecfxd.cpp:36
const BigVectorImpl & ModAddAtIndexEq(usint i, const IntegerType &b)
Definition: mubintvecfxd.cpp:275
const BigVectorImpl & ModInverseEq()
Definition: mubintvecfxd.cpp:463
BigVectorImpl ModInverse() const
Definition: mubintvecfxd.cpp:456
const BigVectorImpl & MultWithOutModEq(const BigVectorImpl &b)
Definition: mubintvecfxd.cpp:507
Main class for big integers represented as an array of native (primitive) unsigned integers...
Definition: ubintfxd.h:219
const BigVectorImpl & operator=(const BigVectorImpl &rhs)
Definition: mubintvecfxd.cpp:112
const BigVectorImpl & DivideAndRoundEq(const IntegerType &q)
Definition: mubintvecfxd.cpp:555
BigVectorImpl Mod(const IntegerType &modulus) const
Definition: mubintvecfxd.cpp:223
const BigVectorImpl & ModByTwoEq()
Definition: mubintvecfxd.cpp:478
The class for representing vectors of big binary integers.
Definition: mubintvecfxd.h:46
BigVectorImpl ModAdd(const IntegerType &b) const
Definition: mubintvecfxd.cpp:249
BigVectorImpl ModSub(const IntegerType &b) const
Definition: mubintvecfxd.cpp:309
const BigVectorImpl & ModSubEq(const IntegerType &b)
Definition: mubintvecfxd.cpp:317
friend std::ostream & operator<<(std::ostream &os, const BigVectorImpl< IntegerType_c > &ptr_obj)
Definition: mubintvecfxd.h:503
BigVectorImpl MultiplyAndRound(const IntegerType &p, const IntegerType &q) const
Definition: mubintvecfxd.cpp:521
IntegerType & at(size_t i)
Definition: mubintvecfxd.h:179
virtual ~BigVectorImpl()
Definition: mubintvecfxd.cpp:105
const IntegerType & GetModulus() const
Definition: mubintvecfxd.h:224
const BigVectorImpl & ModExpEq(const IntegerType &b)
Definition: mubintvecfxd.cpp:447