PALISADE Lattice Crypto Library  1.11.9
A lattice crypto library for software engineers by software engineers.
coefpackedencoding.h
1 // @file coefpackedencoding.h Represents and defines packing integers of
2 // plaintext objects into polynomial coefficients in Palisade.
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 SRC_CORE_LIB_ENCODING_COEFPACKEDENCODING_H_
26 #define SRC_CORE_LIB_ENCODING_COEFPACKEDENCODING_H_
27 
28 #include <initializer_list>
29 #include <memory>
30 #include <vector>
31 
32 #include "encoding/plaintext.h"
33 
34 namespace lbcrypto {
35 
37  vector<int64_t> value;
38 
39  public:
40  // these two constructors are used inside of Decrypt
41  CoefPackedEncoding(shared_ptr<Poly::Params> vp, EncodingParams ep)
42  : PlaintextImpl(vp, ep) {}
43 
44  CoefPackedEncoding(shared_ptr<NativePoly::Params> vp, EncodingParams ep)
45  : PlaintextImpl(vp, ep) {}
46 
47  CoefPackedEncoding(shared_ptr<DCRTPoly::Params> vp, EncodingParams ep)
48  : PlaintextImpl(vp, ep) {}
49 
50  CoefPackedEncoding(shared_ptr<Poly::Params> vp, EncodingParams ep,
51  vector<int64_t> coeffs)
52  : PlaintextImpl(vp, ep), value(coeffs) {}
53 
54  CoefPackedEncoding(shared_ptr<NativePoly::Params> vp, EncodingParams ep,
55  vector<int64_t> coeffs)
56  : PlaintextImpl(vp, ep), value(coeffs) {}
57 
58  CoefPackedEncoding(shared_ptr<DCRTPoly::Params> vp, EncodingParams ep,
59  vector<int64_t> coeffs)
60  : PlaintextImpl(vp, ep), value(coeffs) {}
61 
62  virtual ~CoefPackedEncoding() {}
63 
68  const vector<int64_t>& GetCoefPackedValue() const { return value; }
69 
74  void SetIntVectorValue(const vector<int64_t>& val) { value = val; }
75 
80  bool Encode();
81 
86  bool Decode();
87 
92  PlaintextEncodings GetEncodingType() const { return CoefPacked; }
93 
99  size_t GetLength() const { return value.size(); }
100 
105  void SetLength(size_t siz) { value.resize(siz); }
106 
114  bool CompareTo(const PlaintextImpl& other) const {
115  const auto& oth = static_cast<const CoefPackedEncoding&>(other);
116  return oth.value == this->value;
117  }
118 
123  void PrintValue(std::ostream& out) const {
124  // for sanity's sake, trailing zeros get elided into "..."
125  out << "(";
126  size_t i = value.size();
127  while (--i > 0)
128  if (value[i] != 0) break;
129 
130  for (size_t j = 0; j <= i; j++) out << ' ' << value[j];
131 
132  out << " ... )";
133  }
134 };
135 
136 } /* namespace lbcrypto */
137 
138 #endif /* SRC_CORE_LIB_ENCODING_COEFPACKEDENCODING_H_ */
void PrintValue(std::ostream &out) const
Definition: coefpackedencoding.h:123
const vector< int64_t > & GetCoefPackedValue() const
Definition: coefpackedencoding.h:68
This class represents plaintext in the Palisade library.
Definition: plaintext.h:87
Definition: coefpackedencoding.h:36
size_t GetLength() const
Definition: coefpackedencoding.h:99
bool Decode()
Definition: coefpackedencoding.cpp:100
void SetIntVectorValue(const vector< int64_t > &val)
Definition: coefpackedencoding.h:74
bool Encode()
Definition: coefpackedencoding.cpp:61
bool CompareTo(const PlaintextImpl &other) const
Definition: coefpackedencoding.h:114
PlaintextEncodings GetEncodingType() const
Definition: coefpackedencoding.h:92
void SetLength(size_t siz)
Definition: coefpackedencoding.h:105
Definition: binfhecontext.h:36