Skip to content

Commit

Permalink
implement ipcl public key and secret key serialization and deserializ… (
Browse files Browse the repository at this point in the history
#97)

* implement ipcl public key and secret key serialization and deserialization

Signed-off-by: Huang, Xiaojun <[email protected]>

* update ipcl code

Signed-off-by: Huang, Xiaojun <[email protected]>

---------

Signed-off-by: Huang, Xiaojun <[email protected]>
  • Loading branch information
xhuan28 authored Nov 14, 2023
1 parent 5cc6810 commit b8e2f0d
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 137 deletions.
16 changes: 11 additions & 5 deletions heu/library/algorithms/paillier_ipcl/public_key.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@

#include "cereal/archives/portable_binary.hpp"

namespace {
bool IsEqual(const std::shared_ptr<BigNumber> &a,
const std::shared_ptr<BigNumber> &b) {
return a && b && *a == *b;
}
} // namespace

namespace heu::lib::algorithms::paillier_ipcl {

bool PublicKey::operator==(const PublicKey &other) const {
return ipcl_pubkey_.getN() == other.ipcl_pubkey_.getN() &&
ipcl_pubkey_.getG() == other.ipcl_pubkey_.getG() &&
return IsEqual(ipcl_pubkey_.getN(), other.ipcl_pubkey_.getN()) &&
IsEqual(ipcl_pubkey_.getG(), other.ipcl_pubkey_.getG()) &&
ipcl_pubkey_.getHS() == other.ipcl_pubkey_.getHS();
}

bool PublicKey::operator!=(const PublicKey &other) const {
return !this->operator==(other);
return !(*this == other);
}

std::string PublicKey::ToString() const {
Expand All @@ -38,14 +45,13 @@ yacl::Buffer PublicKey::Serialize() const {
return buf;
}

// todo: UT NOT PASSED
// see heu/library/phe/phe_test.cc:40
void PublicKey::Deserialize(yacl::ByteContainerView in) {
std::istringstream is((std::string)in);
{
cereal::PortableBinaryInputArchive archive(is);
archive(ipcl_pubkey_);
}
pt_bound_.bn_ = *ipcl_pubkey_.getN() / 2;
}

} // namespace heu::lib::algorithms::paillier_ipcl
31 changes: 28 additions & 3 deletions heu/library/algorithms/paillier_ipcl/secret_key.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@

#include "heu/library/algorithms/paillier_ipcl/secret_key.h"

namespace {
bool IsEqual(const std::shared_ptr<BigNumber> &a,
const std::shared_ptr<BigNumber> &b) {
return a && b && *a == *b;
}
} // namespace

namespace heu::lib::algorithms::paillier_ipcl {
bool SecretKey::operator==(const SecretKey &other) const {
return ipcl_prikey_.getP() == other.ipcl_prikey_.getP() &&
ipcl_prikey_.getQ() == other.ipcl_prikey_.getQ() &&
return IsEqual(ipcl_prikey_.getP(), other.ipcl_prikey_.getP()) &&
IsEqual(ipcl_prikey_.getQ(), other.ipcl_prikey_.getQ()) &&
ipcl_prikey_.getLambda() == other.ipcl_prikey_.getLambda();
}

bool SecretKey::operator!=(const SecretKey &other) const {
return !this->operator==(other);
return !(*this == other);
}

std::string SecretKey::ToString() const {
Expand All @@ -24,4 +31,22 @@ std::string SecretKey::ToString() const {
ipcl_prikey_.getQ()->BitSize());
}

yacl::Buffer SecretKey::Serialize() const {
std::ostringstream os;
{
cereal::PortableBinaryOutputArchive archive(os);
archive(ipcl_prikey_);
}
yacl::Buffer buf(os.str().data(), os.str().size());
return buf;
}

void SecretKey::Deserialize(yacl::ByteContainerView in) {
std::istringstream is((std::string)in);
{
cereal::PortableBinaryInputArchive archive(is);
archive(ipcl_prikey_);
}
}

} // namespace heu::lib::algorithms::paillier_ipcl
5 changes: 2 additions & 3 deletions heu/library/algorithms/paillier_ipcl/secret_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ class SecretKey {

std::string ToString() const;

yacl::Buffer Serialize() const { YACL_THROW("Not implemented."); }

void Deserialize(yacl::ByteContainerView) { YACL_THROW("Not implemented."); }
yacl::Buffer Serialize() const;

void Deserialize(yacl::ByteContainerView in);
ipcl::PrivateKey ipcl_prikey_;
};

Expand Down
7 changes: 0 additions & 7 deletions heu/library/phe/phe_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ class PheTest : public ::testing::TestWithParam<SchemaType> {
INSTANTIATE_TEST_SUITE_P(Schema, PheTest, ::testing::ValuesIn(GetAllSchema()));

TEST_P(PheTest, KeySerialize) {
#if ENABLE_IPCL == true // Apple clang on M1 doesn't support `if constexpr`
if (he_kit_.GetSchemaType() == SchemaType::IPCL) {
// todo: IPCL should support pk/sk serialize
GTEST_SKIP() << "Wait for IPCL to impl";
}
#endif

// test pk
auto buffer_pk = he_kit_.GetPublicKey()->Serialize();
EXPECT_NE(*he_kit_.GetPublicKey(), PublicKey());
Expand Down
156 changes: 40 additions & 116 deletions third_party/bazel_cpp/patches/ipcl.patch
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
From 0be997ae7f046aba742a85df9d13b8b129406bd7 Mon Sep 17 00:00:00 2001
From 7f338ed9308ee0364dd371ad022506f42ff8c89d Mon Sep 17 00:00:00 2001
From: "Huang, Xiaojun" <[email protected]>
Date: Tue, 28 Feb 2023 04:18:18 -0500
Date: Mon, 13 Nov 2023 21:08:25 +0800
Subject: [PATCH] ipcl patch

Signed-off-by: Huang, Xiaojun <[email protected]>
---
CMakeLists.txt | 21 ++++++++++++++++----
ipcl/CMakeLists.txt | 26 ++++++++++++-------------
ipcl/include/ipcl/pub_key.hpp | 18 +++++++++++++-----
ipcl/utils/common.cpp | 36 +++++++++++++++++------------------
4 files changed, 61 insertions(+), 40 deletions(-)
CMakeLists.txt | 21 +++++++++++++++++----
ipcl/CMakeLists.txt | 32 ++++++++++++++++----------------
2 files changed, 33 insertions(+), 20 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 737d4be..a9875a3 100644
index db92edf..88e3fe3 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -14,8 +14,21 @@ include(GNUInstallDirs)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/ipcl)

@@ -194,8 +194,8 @@ set(IPCL_FORWARD_CMAKE_ARGS
set(IPCL_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR})
set(IPCL_SRC_DIR ${IPCL_ROOT_DIR}/ipcl)
set(IPCL_INC_DIR ${IPCL_SRC_DIR}/include)
-set(IPCL_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/ipcl)
-set(IPCL_INSTALL_LIBDIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/ipcl)
+set(IPCL_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR})
+set(IPCL_INSTALL_LIBDIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
set(IPCL_CMAKE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/ipcl")
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(IPCL_DEBUG ON)
@@ -211,8 +211,21 @@ find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)

# External dependencies
-include(cmake/ippcrypto.cmake)
-include(cmake/cereal.cmake)
Expand All @@ -36,35 +45,24 @@ index 737d4be..a9875a3 100644
+ IMPORTED_LOCATION ${IPPCRYPTO_LIB_DIR}/libcrypto_mb.a
+ INCLUDE_DIRECTORIES ${IPPCRYPTO_INC_DIR}
+)

include(ipcl-util)

@@ -198,8 +211,8 @@ set(IPCL_FORWARD_CMAKE_ARGS
set(IPCL_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR})
set(IPCL_SRC_DIR ${IPCL_ROOT_DIR}/ipcl)
set(IPCL_INC_DIR ${IPCL_SRC_DIR}/include)
-set(IPCL_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/ipcl)
-set(IPCL_INSTALL_LIBDIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/ipcl)
+set(IPCL_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR})
+set(IPCL_INSTALL_LIBDIR ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})
set(IPCL_CMAKE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/ipcl")
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(IPCL_DEBUG ON)

if(IPCL_DETECT_CPU_RUNTIME)
include(cmake/cpufeatures.cmake)
diff --git a/ipcl/CMakeLists.txt b/ipcl/CMakeLists.txt
index a601caf..31d1aa4 100644
index a338de1..c933631 100644
--- a/ipcl/CMakeLists.txt
+++ b/ipcl/CMakeLists.txt
@@ -43,18 +43,18 @@ install(DIRECTORY ${IPCL_INC_DIR}/
@@ -43,35 +43,35 @@ install(DIRECTORY ${IPCL_INC_DIR}/
)

# CEREAL (third party dep): include and install definition
-add_dependencies(ipcl ext_cereal)
+# add_dependencies(ipcl ext_cereal)
target_include_directories(ipcl
PUBLIC $<BUILD_INTERFACE:${CEREAL_INC_DIR}>
PUBLIC $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/ipcl>
)

-install(DIRECTORY ${CEREAL_INC_DIR}/
- DESTINATION ${IPCL_INSTALL_INCLUDEDIR}
- FILES_MATCHING
Expand All @@ -77,13 +75,20 @@ index a601caf..31d1aa4 100644
+# PATTERN "*.hpp"
+# PATTERN "*.h"
+# )

# IPP-Crypto (third party dep): include and install definition
-if(NOT ippcp_FOUND)
- add_dependencies(ipcl ext_ipp-crypto)
-endif()
+# if(NOT ippcp_FOUND)
+# add_dependencies(ipcl ext_ipp-crypto)
+# endif()

target_include_directories(ipcl
@@ -62,12 +62,12 @@ target_include_directories(ipcl
PUBLIC $<BUILD_INTERFACE:${IPPCRYPTO_INC_DIR}>
PUBLIC $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/ipcl/ippcrypto>
)

-install(DIRECTORY ${IPPCRYPTO_INC_DIR}/
- DESTINATION ${IPCL_INSTALL_INCLUDEDIR}/ippcrypto
- FILES_MATCHING
Expand All @@ -96,90 +101,9 @@ index a601caf..31d1aa4 100644
+# PATTERN "*.hpp"
+# PATTERN "*.h"
+# )

# include and install definition of cpu_features
if(IPCL_DETECT_CPU_RUNTIME)
diff --git a/ipcl/include/ipcl/pub_key.hpp b/ipcl/include/ipcl/pub_key.hpp
index f399c40..da4428c 100644
--- a/ipcl/include/ipcl/pub_key.hpp
+++ b/ipcl/include/ipcl/pub_key.hpp
@@ -148,14 +148,22 @@ 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));
+ // 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;
+ Ipp32u n_data[bn_len];
+ Ipp32u hs_data[bn_len * 2];
+ BigNumber n(n_data, bn_len);
+ BigNumber hs(hs_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;
diff --git a/ipcl/utils/common.cpp b/ipcl/utils/common.cpp
index 4a0830f..f63b59f 100644
--- a/ipcl/utils/common.cpp
+++ b/ipcl/utils/common.cpp
@@ -27,26 +27,26 @@ IppStatus ippGenRandom(Ipp32u* rand, int bits, void* ctx) {
}

IppStatus ippGenRandomBN(IppsBigNumState* rand, int bits, void* ctx) {
- if (kRNGenType == RNGenType::RDSEED) {
- return ippsTRNGenRDSEED_BN(rand, bits, ctx);
- } else if (kRNGenType == RNGenType::RDRAND) {
+ // if (kRNGenType == RNGenType::RDSEED) {
+ // return ippsTRNGenRDSEED_BN(rand, bits, ctx);
+ // } else if (kRNGenType == RNGenType::RDRAND) {
return ippsPRNGenRDRAND_BN(rand, bits, ctx);
- } else if (kRNGenType == RNGenType::PSEUDO) {
- int seed_size = 160;
- int size;
- ippsPRNGGetSize(&size);
- auto prng = std::vector<Ipp8u>(size);
- ippsPRNGInit(seed_size, reinterpret_cast<IppsPRNGState*>(prng.data()));
+ // } else if (kRNGenType == RNGenType::PSEUDO) {
+ // int seed_size = 160;
+ // int size;
+ // ippsPRNGGetSize(&size);
+ // auto prng = std::vector<Ipp8u>(size);
+ // ippsPRNGInit(seed_size, reinterpret_cast<IppsPRNGState*>(prng.data()));

- auto seed = std::vector<Ipp32u>(seed_size);
- rand32u(seed);
- BigNumber seed_bn(seed.data(), seed_size, IppsBigNumPOS);
- ippsPRNGSetSeed(BN(seed_bn), reinterpret_cast<IppsPRNGState*>(prng.data()));
- return ippsPRNGen_BN(rand, bits,
- reinterpret_cast<IppsPRNGState*>(prng.data()));
- } else {
- ERROR_CHECK(false, "ippGenRandomBN: RNGenType does not exist.");
- }
+ // auto seed = std::vector<Ipp32u>(seed_size);
+ // rand32u(seed);
+ // BigNumber seed_bn(seed.data(), seed_size, IppsBigNumPOS);
+ // ippsPRNGSetSeed(BN(seed_bn), reinterpret_cast<IppsPRNGState*>(prng.data()));
+ // return ippsPRNGen_BN(rand, bits,
+ // reinterpret_cast<IppsPRNGState*>(prng.data()));
+ // } else {
+ // ERROR_CHECK(false, "ippGenRandomBN: RNGenType does not exist.");
+ // }
}

BigNumber getRandomBN(int bits) {
--
2.31.1
--
2.39.3

6 changes: 3 additions & 3 deletions third_party/bazel_cpp/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ def _com_github_intel_ipcl():
http_archive,
name = "com_github_intel_ipcl",
patch_args = ["-p1"],
sha256 = "13bbfaec1d54a582555c54bb9e7e344f9d122f756481666b9e992b143d567227",
sha256 = "1a6ecb6cb830e45e501eace9b499ce776ab97aa6fddaeeb5b22dc3c340446467",
patches = [
"@com_alipay_sf_heu//third_party/bazel_cpp:patches/ipcl.patch",
],
strip_prefix = "pailliercryptolib-0ad1297c2932cceddb3c5953ed7865c22d7df780",
strip_prefix = "pailliercryptolib-fdc21350302117103452968ababc2f9676f0d383",
build_file = "@com_alipay_sf_heu//third_party/bazel_cpp:ipcl.BUILD",
urls = [
"https://github.com/intel/pailliercryptolib/archive/0ad1297c2932cceddb3c5953ed7865c22d7df780.tar.gz",
"https://github.com/intel/pailliercryptolib/archive/fdc21350302117103452968ababc2f9676f0d383.tar.gz",
],
)

Expand Down

0 comments on commit b8e2f0d

Please sign in to comment.