PALISADE Lattice Crypto Library  1.11.9
A lattice crypto library for software engineers by software engineers.
cryptocontext-ser.h
1 // @file cryptocontext-ser.h - serialize cryptocontext; include this in any app
2 // that needs to serialize them
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_CRYPTO_CRYPTOCONTEXTSER_H
26 #define LBCRYPTO_CRYPTO_CRYPTOCONTEXTSER_H
27 
28 #include <map>
29 #include <memory>
30 #include <string>
31 #include <vector>
32 
33 #include "cryptocontext.h"
34 #include "palisade.h"
35 
36 #include "utils/serial.h"
37 
38 CEREAL_CLASS_VERSION(
41 CEREAL_CLASS_VERSION(
44 CEREAL_CLASS_VERSION(
47 
48 // the routines below are only instantiated if the user includes the appropriate
49 // serialize-*.h file
50 
51 namespace lbcrypto {
52 
53 template <typename Element>
54 template <typename ST>
56  const ST& sertype,
57  string id) {
58  std::map<string, std::vector<LPEvalKey<Element>>>* smap;
59  std::map<string, std::vector<LPEvalKey<Element>>> omap;
60 
61  if (id.length() == 0) {
62  smap = &GetAllEvalMultKeys();
63  } else {
64  const auto k = GetAllEvalMultKeys().find(id);
65 
66  if (k == GetAllEvalMultKeys().end()) return false; // no such id
67 
68  smap = &omap;
69  omap[k->first] = k->second;
70  }
71 
72  Serial::Serialize(*smap, ser, sertype);
73  return true;
74 }
75 
76 } // namespace lbcrypto
77 
78 namespace lbcrypto {
79 // ================================= JSON serialization/deserialization
80  namespace Serial {
90  template <typename T>
91  void Deserialize(CryptoContext<T>& obj, std::istream& stream, const SerType::SERJSON&) {
92  CryptoContext<T> newob;
93 
94  try {
95  cereal::JSONInputArchive archive(stream);
96  archive(newob);
97  }
98  catch (std::exception& e) {
99  // std::cout << e.what() << std::endl;
100  return;
101  }
102 
103  obj = CryptoContextFactory<T>::GetContext(newob->GetCryptoParameters(),
104  newob->GetEncryptionAlgorithm(),
105  newob->getSchemeId());
106  }
107 
108  template <typename T>
109  bool SerializeToFile(const std::string& filename, const CryptoContext<T>& obj, const SerType::SERJSON& sertype) {
110  std::ofstream file(filename, std::ios::out | std::ios::binary);
111  if (file.is_open()) {
112  Serial::Serialize(obj, file, sertype);
113  file.close();
114  return true;
115  }
116  return false;
117  }
118 
119  template <typename T>
120  bool DeserializeFromFile(const std::string& filename, CryptoContext<T>& obj, const SerType::SERJSON& sertype) {
121  std::ifstream file(filename, std::ios::in | std::ios::binary);
122  if (file.is_open()) {
123  Serial::Deserialize(obj, file, sertype);
124  file.close();
125  return true;
126  }
127  return false;
128  }
129  }
130 template void Serial::Deserialize(std::shared_ptr<CryptoContextImpl<Poly>>& obj,
131  std::istream& stream,
132  const SerType::SERJSON&);
134  std::ostream& ser, const SerType::SERJSON&, string id);
135 template bool CryptoContextImpl<Poly>::SerializeEvalMultKey<SerType::SERJSON>(
136  std::ostream& ser, const SerType::SERJSON&, const CryptoContext<Poly> cc);
137 template bool CryptoContextImpl<Poly>::DeserializeEvalMultKey<SerType::SERJSON>(
138  std::istream& ser, const SerType::SERJSON&);
139 template bool CryptoContextImpl<Poly>::SerializeEvalSumKey<SerType::SERJSON>(
140  std::ostream& ser, const SerType::SERJSON&, string id);
141 template bool CryptoContextImpl<Poly>::SerializeEvalSumKey<SerType::SERJSON>(
142  std::ostream& ser, const SerType::SERJSON&, const CryptoContext<Poly> cc);
143 template bool CryptoContextImpl<Poly>::DeserializeEvalSumKey<SerType::SERJSON>(
144  std::istream& ser, const SerType::SERJSON&);
146  SerType::SERJSON>(std::ostream& ser, const SerType::SERJSON&, string id);
147 template bool
149  std::ostream& ser, const SerType::SERJSON&, const CryptoContext<Poly> cc);
151  SerType::SERJSON>(std::istream& ser, const SerType::SERJSON&);
152 
153 template void Serial::Deserialize(
154  std::shared_ptr<CryptoContextImpl<NativePoly>>& obj, std::istream& stream,
155  const SerType::SERJSON&);
157  SerType::SERJSON>(std::ostream& ser, const SerType::SERJSON&, string id);
159  SerType::SERJSON>(std::ostream& ser, const SerType::SERJSON&,
160  const CryptoContext<NativePoly> cc);
162  SerType::SERJSON>(std::istream& ser, const SerType::SERJSON&);
164  SerType::SERJSON>(std::ostream& ser, const SerType::SERJSON&, string id);
166  SerType::SERJSON>(std::ostream& ser, const SerType::SERJSON&,
167  const CryptoContext<NativePoly> cc);
169  SerType::SERJSON>(std::istream& ser, const SerType::SERJSON&);
171  SerType::SERJSON>(std::ostream& ser, const SerType::SERJSON&, string id);
173  SerType::SERJSON>(std::ostream& ser, const SerType::SERJSON&,
174  const CryptoContext<NativePoly> cc);
176  SerType::SERJSON>(std::istream& ser, const SerType::SERJSON&);
177 
178 template void Serial::Deserialize(
179  std::shared_ptr<CryptoContextImpl<DCRTPoly>>& obj, std::istream& stream,
180  const SerType::SERJSON&);
182  SerType::SERJSON>(std::ostream& ser, const SerType::SERJSON&, string id);
184  SerType::SERJSON>(std::ostream& ser, const SerType::SERJSON&,
185  const CryptoContext<DCRTPoly> cc);
187  SerType::SERJSON>(std::istream& ser, const SerType::SERJSON&);
189  SerType::SERJSON>(std::ostream& ser, const SerType::SERJSON&, string id);
191  SerType::SERJSON>(std::ostream& ser, const SerType::SERJSON&,
192  const CryptoContext<DCRTPoly> cc);
194  SerType::SERJSON>(std::istream& ser, const SerType::SERJSON&);
196  SerType::SERJSON>(std::ostream& ser, const SerType::SERJSON&, string id);
198  SerType::SERJSON>(std::ostream& ser, const SerType::SERJSON&,
199  const CryptoContext<DCRTPoly> cc);
201  SerType::SERJSON>(std::istream& ser, const SerType::SERJSON&);
202 
203 
204 // ================================= BINARY serialization/deserialization
205 namespace Serial {
215  template <typename T>
216  void Deserialize(CryptoContext<T>& obj, std::istream& stream, const SerType::SERBINARY&) {
217  CryptoContext<T> newob;
218 
219  try {
220  cereal::PortableBinaryInputArchive archive(stream);
221  archive(newob);
222  }
223  catch (std::exception& e) {
224  // std::cout << e.what() << std::endl;
225  return;
226  }
227 
228  obj = CryptoContextFactory<T>::GetContext(newob->GetCryptoParameters(),
229  newob->GetEncryptionAlgorithm(),
230  newob->getSchemeId());
231  }
232 
233  template <typename T>
234  bool SerializeToFile(const std::string& filename, const CryptoContext<T>& obj, const SerType::SERBINARY& sertype) {
235  std::ofstream file(filename, std::ios::out | std::ios::binary);
236  if (file.is_open()) {
237  Serial::Serialize(obj, file, sertype);
238  file.close();
239  return true;
240  }
241  return false;
242  }
243 
244  template <typename T>
245  bool DeserializeFromFile(const std::string& filename, CryptoContext<T>& obj, const SerType::SERBINARY& sertype) {
246  std::ifstream file(filename, std::ios::in | std::ios::binary);
247  if (file.is_open()) {
248  Serial::Deserialize(obj, file, sertype);
249  file.close();
250  return true;
251  }
252  return false;
253  }
254 }
255 template void Serial::Deserialize(std::shared_ptr<CryptoContextImpl<Poly>>& obj,
256  std::istream& stream,
257  const SerType::SERBINARY&);
259  std::ostream& ser, const SerType::SERBINARY&, string id);
260 template bool CryptoContextImpl<Poly>::SerializeEvalMultKey<SerType::SERBINARY>(
261  std::ostream& ser, const SerType::SERBINARY&, const CryptoContext<Poly> cc);
263  SerType::SERBINARY>(std::istream& ser, const SerType::SERBINARY&);
264 template bool CryptoContextImpl<Poly>::SerializeEvalSumKey<SerType::SERBINARY>(
265  std::ostream& ser, const SerType::SERBINARY&, string id);
266 template bool CryptoContextImpl<Poly>::SerializeEvalSumKey<SerType::SERBINARY>(
267  std::ostream& ser, const SerType::SERBINARY&, const CryptoContext<Poly> cc);
269  SerType::SERBINARY>(std::istream& ser, const SerType::SERBINARY&);
270 template bool
272  std::ostream& ser, const SerType::SERBINARY&, string id);
273 template bool
275  std::ostream& ser, const SerType::SERBINARY&, const CryptoContext<Poly> cc);
277  SerType::SERBINARY>(std::istream& ser, const SerType::SERBINARY&);
278 
279 template void Serial::Deserialize(
280  std::shared_ptr<CryptoContextImpl<NativePoly>>& obj, std::istream& stream,
281  const SerType::SERBINARY&);
282 template bool
284  std::ostream& ser, const SerType::SERBINARY&, string id);
286  SerType::SERBINARY>(std::ostream& ser, const SerType::SERBINARY&,
287  const CryptoContext<NativePoly> cc);
289  SerType::SERBINARY>(std::istream& ser, const SerType::SERBINARY&);
290 template bool
292  std::ostream& ser, const SerType::SERBINARY&, string id);
294  SerType::SERBINARY>(std::ostream& ser, const SerType::SERBINARY&,
295  const CryptoContext<NativePoly> cc);
297  SerType::SERBINARY>(std::istream& ser, const SerType::SERBINARY&);
298 template bool
300  std::ostream& ser, const SerType::SERBINARY&, string id);
302  SerType::SERBINARY>(std::ostream& ser, const SerType::SERBINARY&,
303  const CryptoContext<NativePoly> cc);
305  SerType::SERBINARY>(std::istream& ser, const SerType::SERBINARY&);
306 
307 template void Serial::Deserialize(
308  std::shared_ptr<CryptoContextImpl<DCRTPoly>>& obj, std::istream& stream,
309  const SerType::SERBINARY&);
310 template bool
312  std::ostream& ser, const SerType::SERBINARY&, string id);
314  SerType::SERBINARY>(std::ostream& ser, const SerType::SERBINARY&,
315  const CryptoContext<DCRTPoly> cc);
317  SerType::SERBINARY>(std::istream& ser, const SerType::SERBINARY&);
318 template bool
320  std::ostream& ser, const SerType::SERBINARY&, string id);
322  SerType::SERBINARY>(std::ostream& ser, const SerType::SERBINARY&,
323  const CryptoContext<DCRTPoly> cc);
325  SerType::SERBINARY>(std::istream& ser, const SerType::SERBINARY&);
326 template bool
328  std::ostream& ser, const SerType::SERBINARY&, string id);
330  SerType::SERBINARY>(std::ostream& ser, const SerType::SERBINARY&,
331  const CryptoContext<DCRTPoly> cc);
333  SerType::SERBINARY>(std::istream& ser, const SerType::SERBINARY&);
334 
335 } // namespace lbcrypto
336 
337 #endif
CryptoContextImpl.
Definition: cryptocontext.h:56
CryptoContextFactory.
Definition: cryptocontext.h:53
static bool SerializeEvalMultKey(std::ostream &ser, const ST &sertype, string id="")
Definition: cryptocontext-ser.h:55
Definition: sertype.h:35
Definition: binfhecontext.h:36
Definition: sertype.h:32