From feabe529909994c3759c6548cc43e0fe9deca992 Mon Sep 17 00:00:00 2001 From: "Zhao, Pengfei" Date: Fri, 4 Aug 2023 17:04:37 +0800 Subject: [PATCH 1/3] pt, ct, base_text: Add support for serialization and deserialization Signed-off-by: Zhao, Pengfei --- ipcl/include/ipcl/base_text.hpp | 9 +++++++++ ipcl/include/ipcl/ciphertext.hpp | 7 +++++++ ipcl/include/ipcl/plaintext.hpp | 8 ++++++++ 3 files changed, 24 insertions(+) diff --git a/ipcl/include/ipcl/base_text.hpp b/ipcl/include/ipcl/base_text.hpp index 8c027b4..b147d6e 100644 --- a/ipcl/include/ipcl/base_text.hpp +++ b/ipcl/include/ipcl/base_text.hpp @@ -103,6 +103,15 @@ class BaseText { protected: std::vector m_texts; ///< Container used to store BigNumber std::size_t m_size; ///< Container size + + private: + // Serialization and desirealization + friend class ::cereal::access; + template + void serialize(Archive& ar, const Ipp32u version) { + ar(::cereal::make_nvp("size", m_size)); + ar(::cereal::make_nvp("texts", m_texts)); + } }; } // namespace ipcl diff --git a/ipcl/include/ipcl/ciphertext.hpp b/ipcl/include/ipcl/ciphertext.hpp index 8c116af..326b7df 100644 --- a/ipcl/include/ipcl/ciphertext.hpp +++ b/ipcl/include/ipcl/ciphertext.hpp @@ -65,6 +65,13 @@ class CipherText : public BaseText { const std::vector& b) const; std::shared_ptr m_pk; ///< Public key used to encrypt big number + + // Serialization and desirealization + friend class ::cereal::access; + template + void serialize(Archive& ar, const Ipp32u version) { + ar(::cereal::base_class(this), ::cereal::make_nvp("pk", *m_pk)); + } }; } // namespace ipcl diff --git a/ipcl/include/ipcl/plaintext.hpp b/ipcl/include/ipcl/plaintext.hpp index 63618f7..cdfbb23 100644 --- a/ipcl/include/ipcl/plaintext.hpp +++ b/ipcl/include/ipcl/plaintext.hpp @@ -87,6 +87,14 @@ class PlainText : public BaseText { * @param[in] shift rotate length */ PlainText rotate(int shift) const; + + private: + // Serialization + friend class ::cereal::access; + template + void serialize(Archive& ar, const Ipp32u version) { + ar(::cereal::base_class(this)); + } }; } // namespace ipcl From 1702da07701e53ecbe8340dd48db46cdbae45949 Mon Sep 17 00:00:00 2001 From: "Zhao, Pengfei" Date: Fri, 4 Aug 2023 17:06:29 +0800 Subject: [PATCH 2/3] unit test: Add serialization test for pt and ct. Signed-off-by: Zhao, Pengfei --- test/test_serialization.cpp | 61 +++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/test/test_serialization.cpp b/test/test_serialization.cpp index df4ed83..fe3064d 100644 --- a/test/test_serialization.cpp +++ b/test/test_serialization.cpp @@ -1,9 +1,15 @@ // Copyright (C) 2021 Intel Corporation // SPDX-License-Identifier: Apache-2.0 +#include +#include + #include "gtest/gtest.h" #include "ipcl/ipcl.hpp" +constexpr int SELF_DEF_NUM_VALUES = 14; +constexpr int SELF_DEF_KEY_SIZE = 2048; + TEST(SerialTest, PublicKeyTest) { const int key_bits = 2048; ipcl::KeyPair key = ipcl::generateKeypair(key_bits); @@ -23,3 +29,58 @@ TEST(SerialTest, PublicKeyTest) { ipcl::PlainText dt = sk.decrypt(ct); EXPECT_EQ(pt.getElement(0), dt.getElement(0)); } + +TEST(SerialTest, PlaintextTest) { + const uint32_t num_values = SELF_DEF_NUM_VALUES; + + std::random_device dev; + std::mt19937 rng(dev()); + std::uniform_int_distribution dist(0, UINT_MAX); + + std::vector exp_value(num_values); + std::for_each(exp_value.begin(), exp_value.end(), + [&](uint32_t& n) { n = dist(rng); }); + + ipcl::PlainText pt = ipcl::PlainText(exp_value); + std::ostringstream os; + ipcl::serializer::serialize(os, pt); + + ipcl::PlainText pt_after; + std::istringstream is(os.str()); + ipcl::serializer::deserialize(is, pt_after); + + for (int i = 0; i < num_values; i++) { + std::vector v = pt.getElementVec(i); + std::vector v_after = pt_after.getElementVec(i); + + EXPECT_EQ(v[0], v_after[0]); + } +} + +TEST(SerialTest, CipherText) { + const uint32_t num_values = SELF_DEF_NUM_VALUES; + ipcl::KeyPair keys = ipcl::generateKeypair(SELF_DEF_KEY_SIZE); + + std::random_device dev; + std::mt19937 rng(dev()); + std::uniform_int_distribution dist(0, UINT_MAX); + + std::vector exp_value(num_values); + std::for_each(exp_value.begin(), exp_value.end(), + [&](uint32_t& n) { n = dist(rng); }); + + ipcl::CipherText ct = ipcl::CipherText(keys.pub_key, exp_value); + std::ostringstream os; + ipcl::serializer::serialize(os, ct); + + ipcl::PlainText ct_after; + std::istringstream is(os.str()); + ipcl::serializer::deserialize(is, ct_after); + + for (int i = 0; i < num_values; i++) { + std::vector v = ct.getElementVec(i); + std::vector v_after = ct_after.getElementVec(i); + + EXPECT_EQ(v[0], v_after[0]); + } +} From a50759af74798284a905e2b14bec870ef7d4ba39 Mon Sep 17 00:00:00 2001 From: "Zhao, Pengfei" Date: Fri, 4 Aug 2023 17:07:36 +0800 Subject: [PATCH 3/3] utils:serialize: Fix serializeToFile and deserializeFromFile functions. Signed-off-by: Zhao, Pengfei --- ipcl/include/ipcl/utils/serialize.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ipcl/include/ipcl/utils/serialize.hpp b/ipcl/include/ipcl/utils/serialize.hpp index 59863d7..b763e34 100644 --- a/ipcl/include/ipcl/utils/serialize.hpp +++ b/ipcl/include/ipcl/utils/serialize.hpp @@ -38,7 +38,7 @@ template bool serializeToFile(const std::string& fn, const T& obj) { std::ofstream ofs(fn, std::ios::out | std::ios::binary); if (ofs.is_open()) { - serializer::serialize(obj, ofs); + serializer::serialize(ofs, obj); ofs.close(); return true; } @@ -49,7 +49,7 @@ template bool deserializeFromFile(const std::string& fn, T& obj) { std::ifstream ifs(fn, std::ios::in | std::ios::binary); if (ifs.is_open()) { - serializer::deserialize(obj, ifs); + serializer::deserialize(ifs, obj); ifs.close(); return true; }