SerialDeserial.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #include "SerialDeserial.h"
  2. #include "openfhe/pke/cryptocontext-ser.h"
  3. #include "Ciphertext.h"
  4. #include "CryptoContext.h"
  5. #include "PublicKey.h"
  6. #include "PrivateKey.h"
  7. namespace openfhe
  8. {
  9. bool SerializeCryptoContextToFile(const std::string& ccLocation,
  10. const CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode)
  11. {
  12. if (serialMode == SerialMode::BINARY)
  13. {
  14. return lbcrypto::Serial::SerializeToFile(ccLocation,
  15. cryptoContext.m_cryptoContextImplSharedPtr, lbcrypto::SerType::BINARY);
  16. }
  17. if (serialMode == SerialMode::JSON)
  18. {
  19. return lbcrypto::Serial::SerializeToFile(ccLocation,
  20. cryptoContext.m_cryptoContextImplSharedPtr, lbcrypto::SerType::JSON);
  21. }
  22. return false;
  23. }
  24. bool DeserializeCryptoContextFromFile(const std::string& ccLocation,
  25. CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode)
  26. {
  27. if (serialMode == SerialMode::BINARY)
  28. {
  29. return lbcrypto::Serial::DeserializeFromFile(ccLocation,
  30. cryptoContext.m_cryptoContextImplSharedPtr, lbcrypto::SerType::BINARY);
  31. }
  32. if (serialMode == SerialMode::JSON)
  33. {
  34. return lbcrypto::Serial::DeserializeFromFile(ccLocation,
  35. cryptoContext.m_cryptoContextImplSharedPtr, lbcrypto::SerType::JSON);
  36. }
  37. return false;
  38. }
  39. bool SerializeEvalMultKeyToFile(const std::string& multKeyLocation,
  40. const CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode)
  41. {
  42. const auto close = [](std::ofstream* const ofs){ if (ofs->is_open()) { ofs->close(); } };
  43. const std::unique_ptr<std::ofstream, decltype(close)> ofs(
  44. new std::ofstream(multKeyLocation, std::ios::out | std::ios::binary), close);
  45. if (ofs->is_open())
  46. {
  47. if (serialMode == SerialMode::BINARY)
  48. {
  49. return CryptoContextImpl::SerializeEvalMultKey(*ofs,
  50. lbcrypto::SerType::BINARY, cryptoContext.m_cryptoContextImplSharedPtr);
  51. }
  52. if (serialMode == SerialMode::JSON)
  53. {
  54. return CryptoContextImpl::SerializeEvalMultKey(*ofs,
  55. lbcrypto::SerType::JSON, cryptoContext.m_cryptoContextImplSharedPtr);
  56. }
  57. }
  58. return false;
  59. }
  60. bool SerializeEvalMultKeyByIdToFile(const std::string& multKeyLocation,
  61. const SerialMode serialMode, const std::string& id)
  62. {
  63. const auto close = [](std::ofstream* const ofs){ if (ofs->is_open()) { ofs->close(); } };
  64. const std::unique_ptr<std::ofstream, decltype(close)> ofs(
  65. new std::ofstream(multKeyLocation, std::ios::out | std::ios::binary), close);
  66. if (ofs->is_open())
  67. {
  68. if (serialMode == SerialMode::BINARY)
  69. {
  70. return CryptoContextImpl::SerializeEvalMultKey(*ofs, lbcrypto::SerType::BINARY, id);
  71. }
  72. if (serialMode == SerialMode::JSON)
  73. {
  74. return CryptoContextImpl::SerializeEvalMultKey(*ofs, lbcrypto::SerType::JSON, id);
  75. }
  76. }
  77. return false;
  78. }
  79. bool DeserializeEvalMultKeyFromFile(const std::string& multKeyLocation,
  80. const SerialMode serialMode)
  81. {
  82. const auto close = [](std::ifstream* const ifs){ if (ifs->is_open()) { ifs->close(); } };
  83. const std::unique_ptr<std::ifstream, decltype(close)> ifs(
  84. new std::ifstream(multKeyLocation, std::ios::in | std::ios::binary), close);
  85. if (ifs->is_open())
  86. {
  87. if (serialMode == SerialMode::BINARY)
  88. {
  89. return CryptoContextImpl::DeserializeEvalMultKey(*ifs, lbcrypto::SerType::BINARY);
  90. }
  91. if (serialMode == SerialMode::JSON)
  92. {
  93. return CryptoContextImpl::DeserializeEvalMultKey(*ifs, lbcrypto::SerType::JSON);
  94. }
  95. }
  96. return false;
  97. }
  98. bool SerializeEvalSumKeyToFile(const std::string& sumKeyLocation,
  99. const CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode)
  100. {
  101. const auto close = [](std::ofstream* const ofs){ if (ofs->is_open()) { ofs->close(); } };
  102. const std::unique_ptr<std::ofstream, decltype(close)> ofs(
  103. new std::ofstream(sumKeyLocation, std::ios::out | std::ios::binary), close);
  104. if (ofs->is_open())
  105. {
  106. if (serialMode == SerialMode::BINARY)
  107. {
  108. return CryptoContextImpl::SerializeEvalAutomorphismKey(*ofs, lbcrypto::SerType::BINARY,
  109. cryptoContext.m_cryptoContextImplSharedPtr);
  110. }
  111. if (serialMode == SerialMode::JSON)
  112. {
  113. return CryptoContextImpl::SerializeEvalAutomorphismKey(*ofs, lbcrypto::SerType::JSON,
  114. cryptoContext.m_cryptoContextImplSharedPtr);
  115. }
  116. }
  117. return false;
  118. }
  119. bool SerializeEvalSumKeyByIdToFile(const std::string& sumKeyLocation,
  120. const SerialMode serialMode, const std::string& id)
  121. {
  122. const auto close = [](std::ofstream* const ofs){ if (ofs->is_open()) { ofs->close(); } };
  123. const std::unique_ptr<std::ofstream, decltype(close)> ofs(
  124. new std::ofstream(sumKeyLocation, std::ios::out | std::ios::binary), close);
  125. if (ofs->is_open())
  126. {
  127. if (serialMode == SerialMode::BINARY)
  128. {
  129. return CryptoContextImpl::SerializeEvalSumKey(*ofs, lbcrypto::SerType::BINARY, id);
  130. }
  131. if (serialMode == SerialMode::JSON)
  132. {
  133. return CryptoContextImpl::SerializeEvalSumKey(*ofs, lbcrypto::SerType::JSON, id);
  134. }
  135. }
  136. return false;
  137. }
  138. bool DeserializeEvalSumKeyFromFile(const std::string& sumKeyLocation, const SerialMode serialMode)
  139. {
  140. const auto close = [](std::ifstream* const ifs){ if (ifs->is_open()) { ifs->close(); } };
  141. const std::unique_ptr<std::ifstream, decltype(close)> ifs(
  142. new std::ifstream(sumKeyLocation, std::ios::in | std::ios::binary), close);
  143. if (ifs->is_open())
  144. {
  145. if (serialMode == SerialMode::BINARY)
  146. {
  147. return CryptoContextImpl::DeserializeEvalAutomorphismKey(*ifs,
  148. lbcrypto::SerType::BINARY);
  149. }
  150. if (serialMode == SerialMode::JSON)
  151. {
  152. return CryptoContextImpl::DeserializeEvalAutomorphismKey(*ifs,
  153. lbcrypto::SerType::JSON);
  154. }
  155. }
  156. return false;
  157. }
  158. bool SerializeEvalAutomorphismKeyToFile(const std::string& automorphismKeyLocation,
  159. const CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode)
  160. {
  161. const auto close = [](std::ofstream* const ofs){ if (ofs->is_open()) { ofs->close(); } };
  162. const std::unique_ptr<std::ofstream, decltype(close)> ofs(
  163. new std::ofstream(automorphismKeyLocation, std::ios::out | std::ios::binary), close);
  164. if (ofs->is_open())
  165. {
  166. if (serialMode == SerialMode::BINARY)
  167. {
  168. return CryptoContextImpl::SerializeEvalAutomorphismKey(*ofs, lbcrypto::SerType::BINARY,
  169. cryptoContext.m_cryptoContextImplSharedPtr);
  170. }
  171. if (serialMode == SerialMode::JSON)
  172. {
  173. return CryptoContextImpl::SerializeEvalAutomorphismKey(*ofs, lbcrypto::SerType::JSON,
  174. cryptoContext.m_cryptoContextImplSharedPtr);
  175. }
  176. }
  177. return false;
  178. }
  179. bool SerializeEvalAutomorphismKeyByIdToFile(const std::string& automorphismKeyLocation,
  180. const SerialMode serialMode, const std::string& id)
  181. {
  182. const auto close = [](std::ofstream* const ofs){ if (ofs->is_open()) { ofs->close(); } };
  183. const std::unique_ptr<std::ofstream, decltype(close)> ofs(
  184. new std::ofstream(automorphismKeyLocation, std::ios::out | std::ios::binary), close);
  185. if (ofs->is_open())
  186. {
  187. if (serialMode == SerialMode::BINARY)
  188. {
  189. return CryptoContextImpl::SerializeEvalAutomorphismKey(*ofs, lbcrypto::SerType::BINARY,
  190. id);
  191. }
  192. if (serialMode == SerialMode::JSON)
  193. {
  194. return CryptoContextImpl::SerializeEvalAutomorphismKey(*ofs, lbcrypto::SerType::JSON,
  195. id);
  196. }
  197. }
  198. return false;
  199. }
  200. bool DeserializeEvalAutomorphismKeyFromFile(const std::string& automorphismKeyLocation,
  201. const SerialMode serialMode)
  202. {
  203. const auto close = [](std::ifstream* const ifs){ if (ifs->is_open()) { ifs->close(); } };
  204. const std::unique_ptr<std::ifstream, decltype(close)> ifs(
  205. new std::ifstream(automorphismKeyLocation, std::ios::in | std::ios::binary), close);
  206. if (ifs->is_open())
  207. {
  208. if (serialMode == SerialMode::BINARY)
  209. {
  210. return CryptoContextImpl::DeserializeEvalAutomorphismKey(*ifs,
  211. lbcrypto::SerType::BINARY);
  212. }
  213. if (serialMode == SerialMode::JSON)
  214. {
  215. return CryptoContextImpl::DeserializeEvalAutomorphismKey(*ifs,
  216. lbcrypto::SerType::JSON);
  217. }
  218. }
  219. return false;
  220. }
  221. bool SerializePublicKeyToFile(const std::string& publicKeyLocation,
  222. const PublicKeyDCRTPoly& publicKey, const SerialMode serialMode)
  223. {
  224. if (serialMode == SerialMode::BINARY)
  225. {
  226. return lbcrypto::Serial::SerializeToFile(publicKeyLocation,
  227. publicKey.m_publicKey, lbcrypto::SerType::BINARY);
  228. }
  229. if (serialMode == SerialMode::JSON)
  230. {
  231. return lbcrypto::Serial::SerializeToFile(publicKeyLocation,
  232. publicKey.m_publicKey, lbcrypto::SerType::JSON);
  233. }
  234. return false;
  235. }
  236. bool DeserializePublicKeyFromFile(const std::string& publicKeyLocation,
  237. PublicKeyDCRTPoly& publicKey, const SerialMode serialMode)
  238. {
  239. if (serialMode == SerialMode::BINARY)
  240. {
  241. return lbcrypto::Serial::DeserializeFromFile(publicKeyLocation,
  242. publicKey.m_publicKey, lbcrypto::SerType::BINARY);
  243. }
  244. if (serialMode == SerialMode::JSON)
  245. {
  246. return lbcrypto::Serial::DeserializeFromFile(publicKeyLocation,
  247. publicKey.m_publicKey, lbcrypto::SerType::JSON);
  248. }
  249. return false;
  250. }
  251. bool SerializePrivateKeyToFile(const std::string& privateKeyLocation,
  252. const PrivateKeyDCRTPoly& privateKey, const SerialMode serialMode)
  253. {
  254. if (serialMode == SerialMode::BINARY)
  255. {
  256. return lbcrypto::Serial::SerializeToFile(privateKeyLocation,
  257. privateKey.m_privateKey, lbcrypto::SerType::BINARY);
  258. }
  259. if (serialMode == SerialMode::JSON)
  260. {
  261. return lbcrypto::Serial::SerializeToFile(privateKeyLocation,
  262. privateKey.m_privateKey, lbcrypto::SerType::JSON);
  263. }
  264. return false;
  265. }
  266. bool DeserializePrivateKeyFromFile(const std::string& privateKeyLocation,
  267. PrivateKeyDCRTPoly& privateKey, const SerialMode serialMode)
  268. {
  269. if (serialMode == SerialMode::BINARY)
  270. {
  271. return lbcrypto::Serial::DeserializeFromFile(privateKeyLocation,
  272. privateKey.m_privateKey, lbcrypto::SerType::BINARY);
  273. }
  274. if (serialMode == SerialMode::JSON)
  275. {
  276. return lbcrypto::Serial::DeserializeFromFile(privateKeyLocation,
  277. privateKey.m_privateKey, lbcrypto::SerType::JSON);
  278. }
  279. return false;
  280. }
  281. bool SerializeCiphertextToFile(const std::string& ciphertextLocation,
  282. const CiphertextDCRTPoly& ciphertext, const SerialMode serialMode)
  283. {
  284. if (serialMode == SerialMode::BINARY)
  285. {
  286. return lbcrypto::Serial::SerializeToFile(ciphertextLocation,
  287. ciphertext.m_ciphertext, lbcrypto::SerType::BINARY);
  288. }
  289. if (serialMode == SerialMode::JSON)
  290. {
  291. return lbcrypto::Serial::SerializeToFile(ciphertextLocation,
  292. ciphertext.m_ciphertext, lbcrypto::SerType::JSON);
  293. }
  294. return false;
  295. }
  296. bool DeserializeCiphertextFromFile(const std::string& ciphertextLocation,
  297. CiphertextDCRTPoly& ciphertext, const SerialMode serialMode)
  298. {
  299. if (serialMode == SerialMode::BINARY)
  300. {
  301. return lbcrypto::Serial::DeserializeFromFile(ciphertextLocation,
  302. ciphertext.m_ciphertext, lbcrypto::SerType::BINARY);
  303. }
  304. if (serialMode == SerialMode::JSON)
  305. {
  306. return lbcrypto::Serial::DeserializeFromFile(ciphertextLocation,
  307. ciphertext.m_ciphertext, lbcrypto::SerType::JSON);
  308. }
  309. return false;
  310. }
  311. } // openfhe