Skip to content

Commit

Permalink
Add pk serialization test and fix the std::bad_alloc issue
Browse files Browse the repository at this point in the history
Signed-off-by: Huang, Xiaojun <xiaojun.huang@intel.com>
  • Loading branch information
xhuan28 committed Mar 28, 2023
1 parent 47a7fab commit 1a03364
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
15 changes: 10 additions & 5 deletions ipcl/include/ipcl/pub_key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,19 @@ class PublicKey {
ar(::cereal::make_nvp("enable_DJN", enable_DJN));
ar(::cereal::make_nvp("randbits", randbits));

std::vector<Ipp32u> n_v, hs_v;
ar(::cereal::make_nvp("n", n_v));
ar(::cereal::make_nvp("hs", hs_v));
int bn_len = bits / 32;
std::vector<Ipp32u> n_v(bn_len, 0);
std::vector<Ipp32u> hs_v(bn_len * 2, 0);
BigNumber n(n_v.data(), bn_len);
BigNumber hs(hs_v.data(), bn_len * 2);

ar(::cereal::make_nvp("n", n));
ar(::cereal::make_nvp("hs", hs));

if (enable_DJN)
create(n_v.data(), bits, hs_v.data(), randbits);
create(n, bits, hs, randbits);
else
create(n_v.data(), bits);
create(n, bits);
}

bool m_isInitialized = false;
Expand Down
1 change: 0 additions & 1 deletion ipcl/pub_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ void PublicKey::create(const BigNumber& n, int bits, bool enableDJN_) {
}
m_testv = false;
m_isInitialized = true;
std::cout << "create complete" << std::endl;
}

void PublicKey::create(const BigNumber& n, int bits, const BigNumber& hs,
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(IPCL_UNITTEST_SRC
main.cpp
test_cryptography.cpp
test_ops.cpp
test_serialization.cpp
)

add_executable(unittest_ipcl ${IPCL_UNITTEST_SRC})
Expand Down
25 changes: 25 additions & 0 deletions test/test_serialization.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (C) 2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#include "gtest/gtest.h"
#include "ipcl/ipcl.hpp"

TEST(SerialTest, PublicKeyTest) {
const int key_bits = 2048;
ipcl::KeyPair key = ipcl::generateKeypair(key_bits);
ipcl::PublicKey exp_pk = key.pub_key;
ipcl::PrivateKey sk = key.priv_key;
std::vector<Ipp32u> vec(key_bits / 32, 0);
BigNumber bn(vec.data(), key_bits / 32);
ipcl::PublicKey ret_pk(bn, key_bits);

std::ostringstream os;
ipcl::serializer::serialize(os, exp_pk);
std::istringstream is(os.str());
ipcl::serializer::deserialize(is, ret_pk);

ipcl::PlainText pt(123);
ipcl::CipherText ct = ret_pk.encrypt(pt);
ipcl::PlainText dt = sk.decrypt(ct);
EXPECT_EQ(pt.getElement(0), dt.getElement(0));
}

0 comments on commit 1a03364

Please sign in to comment.