Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ipcl/include/ipcl/base_text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ class BaseText {
protected:
std::vector<BigNumber> m_texts; ///< Container used to store BigNumber
std::size_t m_size; ///< Container size

private:
// Serialization and desirealization
friend class ::cereal::access;
template <class Archive>
void serialize(Archive& ar, const Ipp32u version) {
ar(::cereal::make_nvp("size", m_size));
ar(::cereal::make_nvp("texts", m_texts));
}
};

} // namespace ipcl
Expand Down
7 changes: 7 additions & 0 deletions ipcl/include/ipcl/ciphertext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ class CipherText : public BaseText {
const std::vector<BigNumber>& b) const;

std::shared_ptr<PublicKey> m_pk; ///< Public key used to encrypt big number

// Serialization and desirealization
friend class ::cereal::access;
template <class Archive>
void serialize(Archive& ar, const Ipp32u version) {
ar(::cereal::base_class<BaseText>(this), ::cereal::make_nvp("pk", *m_pk));
}
};

} // namespace ipcl
Expand Down
8 changes: 8 additions & 0 deletions ipcl/include/ipcl/plaintext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <class Archive>
void serialize(Archive& ar, const Ipp32u version) {
ar(::cereal::base_class<BaseText>(this));
}
};

} // namespace ipcl
Expand Down
4 changes: 2 additions & 2 deletions ipcl/include/ipcl/utils/serialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ template <typename T>
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;
}
Expand All @@ -49,7 +49,7 @@ template <typename T>
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;
}
Expand Down
61 changes: 61 additions & 0 deletions test/test_serialization.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
// Copyright (C) 2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#include <climits>
#include <random>

#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);
Expand All @@ -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<std::mt19937::result_type> dist(0, UINT_MAX);

std::vector<uint32_t> 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<uint32_t> v = pt.getElementVec(i);
std::vector<uint32_t> 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<std::mt19937::result_type> dist(0, UINT_MAX);

std::vector<uint32_t> 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<uint32_t> v = ct.getElementVec(i);
std::vector<uint32_t> v_after = ct_after.getElementVec(i);

EXPECT_EQ(v[0], v_after[0]);
}
}