From 98987dcef9af30a6302147096ad41abd6420c60c Mon Sep 17 00:00:00 2001 From: Zhao Pengfei Date: Wed, 23 Nov 2022 18:32:34 +0800 Subject: [PATCH 01/12] pk, sk, ct: Refactor pk & sk with shared_ptr. Signed-off-by: Zhao Pengfei --- ipcl/ciphertext.cpp | 10 +++--- ipcl/include/ipcl/pri_key.hpp | 44 ++++++++++++------------- ipcl/include/ipcl/pub_key.hpp | 32 +++++++++++++------ ipcl/pri_key.cpp | 60 +++++++++++++++++------------------ ipcl/pub_key.cpp | 42 +++++++++++++----------- 5 files changed, 102 insertions(+), 86 deletions(-) diff --git a/ipcl/ciphertext.cpp b/ipcl/ciphertext.cpp index 440e1d1..d8349ac 100644 --- a/ipcl/ciphertext.cpp +++ b/ipcl/ciphertext.cpp @@ -38,7 +38,7 @@ CipherText CipherText::operator+(const CipherText& other) const { std::size_t b_size = other.getSize(); ERROR_CHECK(this->m_size == b_size || b_size == 1, "CT + CT error: Size mismatch!"); - ERROR_CHECK(m_pubkey->getN() == other.m_pubkey->getN(), + ERROR_CHECK(*(m_pubkey->getN()) == *(other.m_pubkey->getN()), "CT + CT error: 2 different public keys detected!"); const auto& a = *this; @@ -136,19 +136,21 @@ CipherText CipherText::rotate(int shift) const { BigNumber CipherText::raw_add(const BigNumber& a, const BigNumber& b) const { // Hold a copy of nsquare for multi-threaded - const BigNumber& sq = m_pubkey->getNSQ(); + // The BigNumber % operator is not thread safe + // const BigNumber& sq = *(m_pubkey->getNSQ()); + const BigNumber sq = *(m_pubkey->getNSQ()); return a * b % sq; } BigNumber CipherText::raw_mul(const BigNumber& a, const BigNumber& b) const { - const BigNumber& sq = m_pubkey->getNSQ(); + const BigNumber& sq = *(m_pubkey->getNSQ()); return modExp(a, b, sq); } std::vector CipherText::raw_mul( const std::vector& a, const std::vector& b) const { std::size_t v_size = a.size(); - std::vector sq(v_size, m_pubkey->getNSQ()); + std::vector sq(v_size, *(m_pubkey->getNSQ())); // If hybrid OPTIMAL mode is used, use a special ratio if (isHybridOptimal()) { diff --git a/ipcl/include/ipcl/pri_key.hpp b/ipcl/include/ipcl/pri_key.hpp index d351d03..c191874 100644 --- a/ipcl/include/ipcl/pri_key.hpp +++ b/ipcl/include/ipcl/pri_key.hpp @@ -4,6 +4,8 @@ #ifndef IPCL_INCLUDE_IPCL_PRI_KEY_HPP_ #define IPCL_INCLUDE_IPCL_PRI_KEY_HPP_ +#include +#include #include #include "ipcl/ciphertext.hpp" @@ -13,6 +15,9 @@ namespace ipcl { class PrivateKey { public: + PrivateKey() = default; + ~PrivateKey() = default; + /** * PrivateKey constructor * @param[in] public_key paillier public key @@ -40,27 +45,17 @@ class PrivateKey { /** * Get N of public key in paillier scheme */ - BigNumber getN() const { return m_n; } + std::shared_ptr getN() const { return m_n; } /** * Get p of private key in paillier scheme */ - BigNumber getP() const { return m_p; } + std::shared_ptr getP() const { return m_p; } /** * Get q of private key in paillier scheme */ - BigNumber getQ() const { return m_q; } - - /** - * Get bits of key - */ - int getBits() const { return m_bits; } - - /** - * Get public key - */ - const PublicKey* getPubKey() const { return m_pubkey; } + std::shared_ptr getQ() const { return m_q; } /** * @brief Support function for ISO/IEC 18033-6 compliance check @@ -69,13 +64,21 @@ class PrivateKey { */ BigNumber getLambda() const { return m_lambda; } + /** + * Check whether priv key is initialized + */ + bool isInitialized() { return m_isInitialized; } + private: - const PublicKey* m_pubkey; - BigNumber m_n; - BigNumber m_nsquare; - BigNumber m_g; - BigNumber m_p; - BigNumber m_q; + bool m_isInitialized = false; + bool m_enable_crt = false; + + std::shared_ptr m_n; + std::shared_ptr m_nsquare; + std::shared_ptr m_g; + std::shared_ptr m_p; + std::shared_ptr m_q; + BigNumber m_pminusone; BigNumber m_qminusone; BigNumber m_psquare; @@ -85,9 +88,6 @@ class PrivateKey { BigNumber m_hq; BigNumber m_lambda; BigNumber m_x; - int m_bits; - int m_dwords; - bool m_enable_crt; /** * Compute L function in paillier scheme diff --git a/ipcl/include/ipcl/pub_key.hpp b/ipcl/include/ipcl/pub_key.hpp index 88d57cf..5309919 100644 --- a/ipcl/include/ipcl/pub_key.hpp +++ b/ipcl/include/ipcl/pub_key.hpp @@ -4,6 +4,8 @@ #ifndef IPCL_INCLUDE_IPCL_PUB_KEY_HPP_ #define IPCL_INCLUDE_IPCL_PUB_KEY_HPP_ +#include +#include #include #include "ipcl/bignum.h" @@ -15,6 +17,9 @@ class CipherText; class PublicKey { public: + PublicKey() = default; + ~PublicKey() = default; + /** * PublicKey constructor * @param[in] n n of public key in paillier scheme @@ -30,6 +35,7 @@ class PublicKey { * @param[in] bits bit length of public key(default value is 1024) * @param[in] enableDJN_ enables DJN scheme(default value is false) */ + explicit PublicKey(const Ipp32u n, int bits = 1024, bool enableDJN_ = false) : PublicKey(BigNumber(n), bits, enableDJN_) {} @@ -56,17 +62,17 @@ class PublicKey { /** * Get N of public key in paillier scheme */ - BigNumber getN() const { return m_n; } + std::shared_ptr getN() const { return m_n; } /** * Get NSQ of public key in paillier scheme */ - BigNumber getNSQ() const { return m_nsquare; } + std::shared_ptr getNSQ() const { return m_nsquare; } /** * Get G of public key in paillier scheme */ - BigNumber getG() const { return m_g; } + std::shared_ptr getG() const { return m_g; } /** * Get bits of key @@ -113,13 +119,18 @@ class PublicKey { return -1; } + /** + * Check whether pub key is initialized + */ + bool isInitialized() { return m_isInitialized; } + const void* addr = static_cast(this); private: friend class cereal::access; template void save(Archive& ar, const Ipp32u version) const { - ar(::cereal::make_nvp("n", m_n)); + ar(::cereal::make_nvp("n", *m_n)); ar(::cereal::make_nvp("bits", m_bits)); ar(::cereal::make_nvp("enable_DJN", m_enable_DJN)); ar(::cereal::make_nvp("hs", m_hs)); @@ -132,7 +143,7 @@ class PublicKey { bool enable_DJN; int bits, randbits; - ar(::cereal::make_nvp("n", m_n)); + ar(::cereal::make_nvp("n", *m_n)); ar(::cereal::make_nvp("bits", bits)); ar(::cereal::make_nvp("enable_DJN", enable_DJN)); ar(::cereal::make_nvp("hs", m_hs)); @@ -144,13 +155,14 @@ class PublicKey { create(n, bits); } - BigNumber m_n; - BigNumber m_g; - BigNumber m_nsquare; - BigNumber m_hs; + bool m_isInitialized = false; + std::shared_ptr m_n; + std::shared_ptr m_g; + std::shared_ptr m_nsquare; int m_bits; - int m_randbits; int m_dwords; + BigNumber m_hs; + int m_randbits; bool m_enable_DJN; std::vector m_r; bool m_testv; diff --git a/ipcl/pri_key.cpp b/ipcl/pri_key.cpp index 9ab1fd9..bf624f9 100644 --- a/ipcl/pri_key.cpp +++ b/ipcl/pri_key.cpp @@ -24,35 +24,33 @@ static inline BigNumber lcm(const BigNumber& p, const BigNumber& q) { PrivateKey::PrivateKey(const PublicKey* public_key, const BigNumber& p, const BigNumber& q) - : m_pubkey(public_key), - m_n(m_pubkey->getN()), - m_nsquare(m_pubkey->getNSQ()), - m_g(m_pubkey->getG()), - m_p((q < p) ? q : p), - m_q((q < p) ? p : q), - m_pminusone(m_p - 1), - m_qminusone(m_q - 1), - m_psquare(m_p * m_p), - m_qsquare(m_q * m_q), - m_pinverse(m_q.InverseMul(m_p)), - m_hp(computeHfun(m_p, m_psquare)), - m_hq(computeHfun(m_q, m_qsquare)), - // lcm(P-1,Q-1) = (P-1)*(Q-1)/gcd(P-1,Q-1), gcd in ipp-crypto is - // ippsGcd_BN + : m_n(public_key->getN()), + m_nsquare(public_key->getNSQ()), + m_g(public_key->getG()), + m_enable_crt(true), + m_p((q < p) ? std::make_shared(q) + : std::make_shared(p)), + m_q((q < p) ? std::make_shared(p) + : std::make_shared(q)), + m_pminusone(*m_p - 1), + m_qminusone(*m_q - 1), + m_psquare((*m_p) * (*m_p)), + m_qsquare((*m_q) * (*m_q)), + m_pinverse((*m_q).InverseMul(*m_p)), + m_hp(computeHfun(*m_p, m_psquare)), + m_hq(computeHfun(*m_q, m_qsquare)), m_lambda(lcm(m_pminusone, m_qminusone)), - // TODO(bwang30): check if ippsModInv_BN does the same thing with - // mpz_invert - m_x(m_n.InverseMul((modExp(m_g, m_lambda, m_nsquare) - 1) / m_n)), - m_bits(m_pubkey->getBits()), - m_dwords(m_pubkey->getDwords()), - m_enable_crt(true) { - ERROR_CHECK(p * q == m_n, + m_x((*m_n).InverseMul((modExp(*m_g, m_lambda, *m_nsquare) - 1) / + (*m_n))) { + ERROR_CHECK((*m_p) * (*m_q) == *m_n, "PrivateKey ctor: Public key does not match p * q."); - ERROR_CHECK(p != q, "PrivateKey ctor: p and q are same"); + ERROR_CHECK(*m_p != *m_q, "PrivateKey ctor: p and q are same"); + m_isInitialized = true; } PlainText PrivateKey::decrypt(const CipherText& ct) const { - ERROR_CHECK(ct.getPubKey()->getN() == m_pubkey->getN(), + ERROR_CHECK(m_isInitialized, "decrypt: Private key is NOT initialized."); + ERROR_CHECK(*(ct.getPubKey()->getN()) == *(this->getN()), "decrypt: The value of N in public key mismatch."); std::size_t ct_size = ct.getSize(); @@ -82,7 +80,7 @@ void PrivateKey::decryptRAW(std::vector& plaintext, std::size_t v_size = plaintext.size(); std::vector pow_lambda(v_size, m_lambda); - std::vector modulo(v_size, m_nsquare); + std::vector modulo(v_size, *m_nsquare); std::vector res = modExp(ciphertext, pow_lambda, modulo); #ifdef IPCL_USE_OMP @@ -91,7 +89,7 @@ void PrivateKey::decryptRAW(std::vector& plaintext, OMPUtilities::assignOMPThreads(omp_remaining_threads, v_size)) #endif // IPCL_USE_OMP for (int i = 0; i < v_size; i++) { - BigNumber nn = m_n; + BigNumber nn = *m_n; BigNumber xx = m_x; BigNumber m = ((res[i] - 1) / nn) * xx; plaintext[i] = m % nn; @@ -127,16 +125,16 @@ void PrivateKey::decryptCRT(std::vector& plaintext, OMPUtilities::assignOMPThreads(omp_remaining_threads, v_size)) #endif // IPCL_USE_OMP for (int i = 0; i < v_size; i++) { - BigNumber dp = computeLfun(resp[i], m_p) * m_hp % m_p; - BigNumber dq = computeLfun(resq[i], m_q) * m_hq % m_q; + BigNumber dp = computeLfun(resp[i], *m_p) * m_hp % (*m_p); + BigNumber dq = computeLfun(resq[i], *m_q) * m_hq % (*m_q); plaintext[i] = computeCRT(dp, dq); } } BigNumber PrivateKey::computeCRT(const BigNumber& mp, const BigNumber& mq) const { - BigNumber u = (mq - mp) * m_pinverse % m_q; - return mp + (u * m_p); + BigNumber u = (mq - mp) * m_pinverse % (*m_q); + return mp + (u * (*m_p)); } BigNumber PrivateKey::computeLfun(const BigNumber& a, @@ -148,7 +146,7 @@ BigNumber PrivateKey::computeHfun(const BigNumber& a, const BigNumber& b) const { // Based on the fact a^b mod n = (a mod n)^b mod n BigNumber xm = a - 1; - BigNumber base = m_g % b; + BigNumber base = *m_g % b; BigNumber pm = modExp(base, xm, b); BigNumber lcrt = computeLfun(pm, a); return a.InverseMul(lcrt); diff --git a/ipcl/pub_key.cpp b/ipcl/pub_key.cpp index 5dc9311..056e6d2 100644 --- a/ipcl/pub_key.cpp +++ b/ipcl/pub_key.cpp @@ -16,32 +16,33 @@ namespace ipcl { PublicKey::PublicKey(const BigNumber& n, int bits, bool enableDJN_) - : m_n(n), - m_g(n + 1), - m_nsquare(n * n), + : m_n(std::make_shared(n)), + m_g(std::make_shared(*m_n + 1)), + m_nsquare(std::make_shared((*m_n) * (*m_n))), m_bits(bits), - m_dwords(BITSIZE_DWORD(bits * 2)), + m_dwords(BITSIZE_DWORD(m_bits * 2)), m_enable_DJN(false), m_testv(false), m_hs(0), m_randbits(0) { if (enableDJN_) this->enableDJN(); // sets m_enable_DJN + m_isInitialized = true; } void PublicKey::enableDJN() { BigNumber gcd; BigNumber rmod; do { - int rand_bit = m_n.BitSize(); + int rand_bit = (*m_n).BitSize(); BigNumber rand = getRandomBN(rand_bit + 128); - rmod = rand % m_n; - gcd = rand.gcd(m_n); + rmod = rand % (*m_n); + gcd = rand.gcd(*m_n); } while (gcd.compare(1)); BigNumber rmod_sq = rmod * rmod; BigNumber rmod_neg = rmod_sq * -1; - BigNumber h = rmod_neg % m_n; - m_hs = modExp(h, m_n, m_nsquare); + BigNumber h = rmod_neg % (*m_n); + m_hs = modExp(h, *m_n, *m_nsquare); m_randbits = m_bits >> 1; // bits/2 m_enable_DJN = true; @@ -50,7 +51,7 @@ void PublicKey::enableDJN() { std::vector PublicKey::getDJNObfuscator(std::size_t sz) const { std::vector r(sz); std::vector base(sz, m_hs); - std::vector sq(sz, m_nsquare); + std::vector sq(sz, *m_nsquare); if (m_testv) { r = m_r; @@ -64,15 +65,15 @@ std::vector PublicKey::getDJNObfuscator(std::size_t sz) const { std::vector PublicKey::getNormalObfuscator(std::size_t sz) const { std::vector r(sz); - std::vector sq(sz, m_nsquare); - std::vector pown(sz, m_n); + std::vector sq(sz, *m_nsquare); + std::vector pown(sz, *m_n); if (m_testv) { r = m_r; } else { for (int i = 0; i < sz; i++) { r[i] = getRandomBN(m_bits); - r[i] = r[i] % (m_n - 1) + 1; + r[i] = r[i] % (*m_n - 1) + 1; } } return modExp(r, pown, sq); @@ -82,7 +83,7 @@ void PublicKey::applyObfuscator(std::vector& ciphertext) const { std::size_t sz = ciphertext.size(); std::vector obfuscator = m_enable_DJN ? getDJNObfuscator(sz) : getNormalObfuscator(sz); - BigNumber sq = m_nsquare; + BigNumber sq = *m_nsquare; for (std::size_t i = 0; i < sz; ++i) ciphertext[i] = sq.ModMul(ciphertext[i], obfuscator[i]); @@ -102,7 +103,7 @@ std::vector PublicKey::raw_encrypt(const std::vector& pt, std::vector ct(pt_size); for (std::size_t i = 0; i < pt_size; i++) - ct[i] = (m_n * pt[i] + 1) % m_nsquare; + ct[i] = (*m_n * pt[i] + 1) % (*m_nsquare); if (make_secure) applyObfuscator(ct); @@ -110,6 +111,8 @@ std::vector PublicKey::raw_encrypt(const std::vector& pt, } CipherText PublicKey::encrypt(const PlainText& pt, bool make_secure) const { + ERROR_CHECK(m_isInitialized, "encrypt: Public key is NOT initialized."); + std::size_t pt_size = pt.getSize(); ERROR_CHECK(pt_size > 0, "encrypt: Cannot encrypt empty PlainText"); std::vector ct_bn_v(pt_size); @@ -135,11 +138,11 @@ void PublicKey::setDJN(const BigNumber& hs, int randbit) { } void PublicKey::create(const BigNumber& n, int bits, bool enableDJN_) { - m_n = n; - m_g = n + 1; - m_nsquare = n * n; + m_n = std::make_shared(n); + m_g = std::make_shared(*m_n + 1); + m_nsquare = std::make_shared((*m_n) * (*m_n)); m_bits = bits; - m_dwords = BITSIZE_DWORD(bits * 2); + m_dwords = BITSIZE_DWORD(m_bits * 2); m_enable_DJN = enableDJN_; if (enableDJN_) { this->enableDJN(); @@ -148,6 +151,7 @@ void PublicKey::create(const BigNumber& n, int bits, bool enableDJN_) { m_randbits = 0; } m_testv = false; + m_isInitialized = true; std::cout << "create complete" << std::endl; } From a994314f2c03435bc637c4f92485af3347509d8b Mon Sep 17 00:00:00 2001 From: Zhao Pengfei Date: Wed, 23 Nov 2022 20:07:33 +0800 Subject: [PATCH 02/12] keypair: refactor KeyPair structure Signed-off-by: Zhao Pengfei --- ipcl/include/ipcl/ipcl.hpp | 12 ++++++------ ipcl/include/ipcl/keygen.hpp | 12 ++++++------ ipcl/keygen.cpp | 8 ++++---- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/ipcl/include/ipcl/ipcl.hpp b/ipcl/include/ipcl/ipcl.hpp index e06159e..2b1dea3 100644 --- a/ipcl/include/ipcl/ipcl.hpp +++ b/ipcl/include/ipcl/ipcl.hpp @@ -13,12 +13,12 @@ namespace ipcl { /** * Paillier key structure contains a public key and private key - * pub_key: paillier public key - * priv_key: paillier private key + * pk: paillier public key + * sk: paillier private key */ -struct keyPair { - PublicKey* pub_key; - PrivateKey* priv_key; +struct KeyPair { + PublicKey pk; + PrivateKey sk; }; /** @@ -34,7 +34,7 @@ BigNumber getPrimeBN(int maxBitSize); * @param[in] enable_DJN Enable DJN (default=true) * @return The function return the public and private key pair */ -keyPair generateKeypair(int64_t n_length, bool enable_DJN = true); +KeyPair generateKeypair(int64_t n_length, bool enable_DJN = true); } // namespace ipcl #endif // IPCL_INCLUDE_IPCL_IPCL_HPP_ diff --git a/ipcl/include/ipcl/keygen.hpp b/ipcl/include/ipcl/keygen.hpp index c254db8..50b77cb 100644 --- a/ipcl/include/ipcl/keygen.hpp +++ b/ipcl/include/ipcl/keygen.hpp @@ -10,12 +10,12 @@ namespace ipcl { /** * Paillier key structure contains a public key and private key - * pub_key: paillier public key - * priv_key: paillier private key + * pk: paillier public key + * sk: paillier private key */ -struct keyPair { - PublicKey* pub_key; - PrivateKey* priv_key; +struct KeyPair { + PublicKey pk; + PrivateKey sk; }; /** @@ -31,7 +31,7 @@ BigNumber getPrimeBN(int maxBitSize); * @param[in] enable_DJN Enable DJN (default=true) * @return The function return the public and private key pair */ -keyPair generateKeypair(int64_t n_length, bool enable_DJN = true); +KeyPair generateKeypair(int64_t n_length, bool enable_DJN = true); } // namespace ipcl #endif // IPCL_INCLUDE_IPCL_KEYGEN_HPP_ diff --git a/ipcl/keygen.cpp b/ipcl/keygen.cpp index 41301d0..65ae297 100644 --- a/ipcl/keygen.cpp +++ b/ipcl/keygen.cpp @@ -83,7 +83,7 @@ static void getDJNBN(int64_t n_length, BigNumber& p, BigNumber& q, BigNumber& n, isClosePrimeBN(p, q, ref_dist)); // gcd(p-1,q-1)=2 } -keyPair generateKeypair(int64_t n_length, bool enable_DJN) { +KeyPair generateKeypair(int64_t n_length, bool enable_DJN) { /* https://www.intel.com/content/www/us/en/develop/documentation/ipp-crypto-reference/top/multi-buffer-cryptography-functions/modular-exponentiation/mbx-exp-1024-2048-3072-4096-mb8.html modulus size = n * n (keySize * keySize ) @@ -104,10 +104,10 @@ keyPair generateKeypair(int64_t n_length, bool enable_DJN) { else getNormalBN(n_length, p, q, n, ref_dist); - PublicKey* public_key = new PublicKey(n, n_length, enable_DJN); - PrivateKey* private_key = new PrivateKey(public_key, p, q); + PublicKey pk(n, n_length, enable_DJN); + PrivateKey sk(&pk, p, q); - return keyPair{public_key, private_key}; + return KeyPair{pk, sk}; } } // namespace ipcl From cc3c58fd9ac2f81ff43b5586ad94e6bef72572a0 Mon Sep 17 00:00:00 2001 From: Zhao Pengfei Date: Wed, 23 Nov 2022 20:08:47 +0800 Subject: [PATCH 03/12] unittest, benchmark: Updated with smart ptr version pk, sk and keypair Signed-off-by: Zhao Pengfei --- benchmark/bench_cryptography.cpp | 30 ++--- benchmark/bench_hybrid.cpp | 58 ++++------ benchmark/bench_ops.cpp | 41 +++---- test/test_cryptography.cpp | 30 ++--- test/test_ops.cpp | 188 +++++++++++++------------------ 5 files changed, 139 insertions(+), 208 deletions(-) diff --git a/benchmark/bench_cryptography.cpp b/benchmark/bench_cryptography.cpp index a2febf0..5416a01 100644 --- a/benchmark/bench_cryptography.cpp +++ b/benchmark/bench_cryptography.cpp @@ -65,7 +65,7 @@ const BigNumber HS_BN = static void BM_KeyGen(benchmark::State& state) { int64_t n_length = state.range(0); for (auto _ : state) { - ipcl::keyPair key = ipcl::generateKeypair(n_length, Enable_DJN); + ipcl::KeyPair key = ipcl::generateKeypair(n_length, Enable_DJN); } } BENCHMARK(BM_KeyGen)->Unit(benchmark::kMicrosecond)->ADD_SAMPLE_KEY_LENGTH_ARGS; @@ -75,12 +75,12 @@ static void BM_Encrypt(benchmark::State& state) { BigNumber n = P_BN * Q_BN; int n_length = n.BitSize(); - ipcl::PublicKey* pub_key = new ipcl::PublicKey(n, n_length, Enable_DJN); - ipcl::PrivateKey* priv_key = new ipcl::PrivateKey(pub_key, P_BN, Q_BN); + ipcl::PublicKey pk(n, n_length, Enable_DJN); + ipcl::PrivateKey sk(&pk, P_BN, Q_BN); std::vector r_bn_v(dsize, R_BN); - pub_key->setRandom(r_bn_v); - pub_key->setHS(HS_BN); + pk.setRandom(r_bn_v); + pk.setHS(HS_BN); std::vector exp_bn_v(dsize); for (size_t i = 0; i < dsize; i++) @@ -89,10 +89,7 @@ static void BM_Encrypt(benchmark::State& state) { ipcl::PlainText pt(exp_bn_v); ipcl::CipherText ct; - for (auto _ : state) ct = pub_key->encrypt(pt); - - delete pub_key; - delete priv_key; + for (auto _ : state) ct = pk.encrypt(pt); } BENCHMARK(BM_Encrypt) ->Unit(benchmark::kMicrosecond) @@ -103,23 +100,20 @@ static void BM_Decrypt(benchmark::State& state) { BigNumber n = P_BN * Q_BN; int n_length = n.BitSize(); - ipcl::PublicKey* pub_key = new ipcl::PublicKey(n, n_length, Enable_DJN); - ipcl::PrivateKey* priv_key = new ipcl::PrivateKey(pub_key, P_BN, Q_BN); + ipcl::PublicKey pk(n, n_length, Enable_DJN); + ipcl::PrivateKey sk(&pk, P_BN, Q_BN); std::vector r_bn_v(dsize, R_BN); - pub_key->setRandom(r_bn_v); - pub_key->setHS(HS_BN); + pk.setRandom(r_bn_v); + pk.setHS(HS_BN); std::vector exp_bn_v(dsize); for (size_t i = 0; i < dsize; i++) exp_bn_v[i] = P_BN - BigNumber((unsigned int)(i * 1024)); ipcl::PlainText pt(exp_bn_v), dt; - ipcl::CipherText ct = pub_key->encrypt(pt); - for (auto _ : state) dt = priv_key->decrypt(ct); - - delete pub_key; - delete priv_key; + ipcl::CipherText ct = pk.encrypt(pt); + for (auto _ : state) dt = sk.decrypt(ct); } BENCHMARK(BM_Decrypt) diff --git a/benchmark/bench_hybrid.cpp b/benchmark/bench_hybrid.cpp index d911301..ad24a90 100644 --- a/benchmark/bench_hybrid.cpp +++ b/benchmark/bench_hybrid.cpp @@ -7,7 +7,7 @@ #include "ipcl/ipcl.hpp" -#define BENCH_HYBRID_DETAIL 1 +#define BENCH_HYBRID_DETAIL 0 #define INPUT_BN_NUM_MAX 256 #define INPUT_BN_NUM_MIN 16 @@ -85,12 +85,12 @@ static void BM_Hybrid_ModExp(benchmark::State& state) { BigNumber n = P_BN * Q_BN; int n_length = n.BitSize(); - ipcl::PublicKey* pub_key = new ipcl::PublicKey(n, n_length, Enable_DJN); - ipcl::PrivateKey* priv_key = new ipcl::PrivateKey(pub_key, P_BN, Q_BN); + ipcl::PublicKey pk(n, n_length, Enable_DJN); + ipcl::PrivateKey sk(&pk, P_BN, Q_BN); std::vector r_bn_v(dsize, R_BN); - pub_key->setRandom(r_bn_v); - pub_key->setHS(HS_BN); + pk.setRandom(r_bn_v); + pk.setHS(HS_BN); std::vector exp_bn_v(dsize); for (size_t i = 0; i < dsize; i++) @@ -98,11 +98,11 @@ static void BM_Hybrid_ModExp(benchmark::State& state) { ipcl::PlainText pt(exp_bn_v); - BigNumber lambda = priv_key->getLambda(); + BigNumber lambda = sk.getLambda(); std::vector pow(dsize, lambda); std::vector m(dsize, n * n); - ipcl::CipherText ct = pub_key->encrypt(pt); + ipcl::CipherText ct = pk.encrypt(pt); std::vector res(dsize); #if BENCH_HYBRID_DETAIL @@ -112,9 +112,6 @@ static void BM_Hybrid_ModExp(benchmark::State& state) { #endif for (auto _ : state) res = ipcl::modExp(ct.getTexts(), pow, m); // decryptRAW - - delete pub_key; - delete priv_key; } BENCHMARK(BM_Hybrid_ModExp)->Unit(benchmark::kMicrosecond)->Apply(customArgs); @@ -128,12 +125,12 @@ static void BM_Hybrid_Encrypt(benchmark::State& state) { BigNumber n = P_BN * Q_BN; int n_length = n.BitSize(); - ipcl::PublicKey* pub_key = new ipcl::PublicKey(n, n_length, Enable_DJN); - ipcl::PrivateKey* priv_key = new ipcl::PrivateKey(pub_key, P_BN, Q_BN); + ipcl::PublicKey pk(n, n_length, Enable_DJN); + ipcl::PrivateKey sk(&pk, P_BN, Q_BN); std::vector r_bn_v(dsize, R_BN); - pub_key->setRandom(r_bn_v); - pub_key->setHS(HS_BN); + pk.setRandom(r_bn_v); + pk.setHS(HS_BN); std::vector exp_bn_v(dsize); for (size_t i = 0; i < dsize; i++) @@ -147,10 +144,7 @@ static void BM_Hybrid_Encrypt(benchmark::State& state) { ipcl::setHybridMode(ipcl::HybridMode::OPTIMAL); #endif - for (auto _ : state) ct = pub_key->encrypt(pt); - - delete pub_key; - delete priv_key; + for (auto _ : state) ct = pk.encrypt(pt); } BENCHMARK(BM_Hybrid_Encrypt)->Unit(benchmark::kMicrosecond)->Apply(customArgs); @@ -164,19 +158,19 @@ static void BM_Hybrid_Decrypt(benchmark::State& state) { BigNumber n = P_BN * Q_BN; int n_length = n.BitSize(); - ipcl::PublicKey* pub_key = new ipcl::PublicKey(n, n_length, Enable_DJN); - ipcl::PrivateKey* priv_key = new ipcl::PrivateKey(pub_key, P_BN, Q_BN); + ipcl::PublicKey pk(n, n_length, Enable_DJN); + ipcl::PrivateKey sk(&pk, P_BN, Q_BN); std::vector r_bn_v(dsize, R_BN); - pub_key->setRandom(r_bn_v); - pub_key->setHS(HS_BN); + pk.setRandom(r_bn_v); + pk.setHS(HS_BN); std::vector exp_bn_v(dsize); for (size_t i = 0; i < dsize; i++) exp_bn_v[i] = P_BN - BigNumber((unsigned int)(i * 1024)); ipcl::PlainText pt(exp_bn_v), dt; - ipcl::CipherText ct = pub_key->encrypt(pt); + ipcl::CipherText ct = pk.encrypt(pt); #if BENCH_HYBRID_DETAIL ipcl::setHybridRatio(qat_ratio); @@ -184,10 +178,7 @@ static void BM_Hybrid_Decrypt(benchmark::State& state) { ipcl::setHybridMode(ipcl::HybridMode::OPTIMAL); #endif - for (auto _ : state) dt = priv_key->decrypt(ct); - - delete pub_key; - delete priv_key; + for (auto _ : state) dt = sk.decrypt(ct); } BENCHMARK(BM_Hybrid_Decrypt)->Unit(benchmark::kMicrosecond)->Apply(customArgs); @@ -201,12 +192,12 @@ static void BM_Hybrid_MulCTPT(benchmark::State& state) { BigNumber n = P_BN * Q_BN; int n_length = n.BitSize(); - ipcl::PublicKey* pub_key = new ipcl::PublicKey(n, n_length, Enable_DJN); - ipcl::PrivateKey* priv_key = new ipcl::PrivateKey(pub_key, P_BN, Q_BN); + ipcl::PublicKey pk(n, n_length, Enable_DJN); + ipcl::PrivateKey sk(&pk, P_BN, Q_BN); std::vector r_bn_v(dsize, R_BN); - pub_key->setRandom(r_bn_v); - pub_key->setHS(HS_BN); + pk.setRandom(r_bn_v); + pk.setHS(HS_BN); std::vector exp_bn1_v(dsize), exp_bn2_v(dsize); for (int i = 0; i < dsize; i++) { @@ -217,7 +208,7 @@ static void BM_Hybrid_MulCTPT(benchmark::State& state) { ipcl::PlainText pt1(exp_bn1_v); ipcl::PlainText pt2(exp_bn2_v); - ipcl::CipherText ct1 = pub_key->encrypt(pt1); + ipcl::CipherText ct1 = pk.encrypt(pt1); ipcl::CipherText product; #if BENCH_HYBRID_DETAIL @@ -227,8 +218,5 @@ static void BM_Hybrid_MulCTPT(benchmark::State& state) { #endif for (auto _ : state) product = ct1 * pt2; - - delete pub_key; - delete priv_key; } BENCHMARK(BM_Hybrid_MulCTPT)->Unit(benchmark::kMicrosecond)->Apply(customArgs); diff --git a/benchmark/bench_ops.cpp b/benchmark/bench_ops.cpp index cb0acfd..3aa9de6 100644 --- a/benchmark/bench_ops.cpp +++ b/benchmark/bench_ops.cpp @@ -67,12 +67,12 @@ static void BM_Add_CTCT(benchmark::State& state) { BigNumber n = P_BN * Q_BN; int n_length = n.BitSize(); - ipcl::PublicKey* pub_key = new ipcl::PublicKey(n, n_length, Enable_DJN); - ipcl::PrivateKey* priv_key = new ipcl::PrivateKey(pub_key, P_BN, Q_BN); + ipcl::PublicKey pk(n, n_length, Enable_DJN); + ipcl::PrivateKey sk(&pk, P_BN, Q_BN); std::vector r_bn_v(dsize, R_BN); - pub_key->setRandom(r_bn_v); - pub_key->setHS(HS_BN); + pk.setRandom(r_bn_v); + pk.setHS(HS_BN); std::vector exp_bn1_v(dsize), exp_bn2_v(dsize); for (int i = 0; i < dsize; i++) { @@ -83,14 +83,11 @@ static void BM_Add_CTCT(benchmark::State& state) { ipcl::PlainText pt1(exp_bn1_v); ipcl::PlainText pt2(exp_bn2_v); - ipcl::CipherText ct1 = pub_key->encrypt(pt1); - ipcl::CipherText ct2 = pub_key->encrypt(pt2); + ipcl::CipherText ct1 = pk.encrypt(pt1); + ipcl::CipherText ct2 = pk.encrypt(pt2); ipcl::CipherText sum; for (auto _ : state) sum = ct1 + ct2; - - delete pub_key; - delete priv_key; } BENCHMARK(BM_Add_CTCT) ->Unit(benchmark::kMicrosecond) @@ -101,12 +98,12 @@ static void BM_Add_CTPT(benchmark::State& state) { BigNumber n = P_BN * Q_BN; int n_length = n.BitSize(); - ipcl::PublicKey* pub_key = new ipcl::PublicKey(n, n_length, Enable_DJN); - ipcl::PrivateKey* priv_key = new ipcl::PrivateKey(pub_key, P_BN, Q_BN); + ipcl::PublicKey pk(n, n_length, Enable_DJN); + ipcl::PrivateKey sk(&pk, P_BN, Q_BN); std::vector r_bn_v(dsize, R_BN); - pub_key->setRandom(r_bn_v); - pub_key->setHS(HS_BN); + pk.setRandom(r_bn_v); + pk.setHS(HS_BN); std::vector exp_bn1_v(dsize), exp_bn2_v(dsize); for (int i = 0; i < dsize; i++) { @@ -117,13 +114,10 @@ static void BM_Add_CTPT(benchmark::State& state) { ipcl::PlainText pt1(exp_bn1_v); ipcl::PlainText pt2(exp_bn2_v); - ipcl::CipherText ct1 = pub_key->encrypt(pt1); + ipcl::CipherText ct1 = pk.encrypt(pt1); ipcl::CipherText sum; for (auto _ : state) sum = ct1 + pt2; - - delete pub_key; - delete priv_key; } BENCHMARK(BM_Add_CTPT) ->Unit(benchmark::kMicrosecond) @@ -133,12 +127,12 @@ static void BM_Mul_CTPT(benchmark::State& state) { size_t dsize = state.range(0); BigNumber n = P_BN * Q_BN; int n_length = n.BitSize(); - ipcl::PublicKey* pub_key = new ipcl::PublicKey(n, n_length, Enable_DJN); - ipcl::PrivateKey* priv_key = new ipcl::PrivateKey(pub_key, P_BN, Q_BN); + ipcl::PublicKey pk(n, n_length, Enable_DJN); + ipcl::PrivateKey sk(&pk, P_BN, Q_BN); std::vector r_bn_v(dsize, R_BN); - pub_key->setRandom(r_bn_v); - pub_key->setHS(HS_BN); + pk.setRandom(r_bn_v); + pk.setHS(HS_BN); std::vector exp_bn1_v(dsize), exp_bn2_v(dsize); for (int i = 0; i < dsize; i++) { @@ -149,13 +143,10 @@ static void BM_Mul_CTPT(benchmark::State& state) { ipcl::PlainText pt1(exp_bn1_v); ipcl::PlainText pt2(exp_bn2_v); - ipcl::CipherText ct1 = pub_key->encrypt(pt1); + ipcl::CipherText ct1 = pk.encrypt(pt1); ipcl::CipherText product; for (auto _ : state) product = ct1 * pt2; - - delete pub_key; - delete priv_key; } BENCHMARK(BM_Mul_CTPT) ->Unit(benchmark::kMicrosecond) diff --git a/test/test_cryptography.cpp b/test/test_cryptography.cpp index 24d4fb9..6598ab8 100644 --- a/test/test_cryptography.cpp +++ b/test/test_cryptography.cpp @@ -15,7 +15,7 @@ TEST(CryptoTest, CryptoTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::keyPair key = ipcl::generateKeypair(2048, true); + ipcl::KeyPair keys = ipcl::generateKeypair(2048, true); std::vector exp_value(num_values); ipcl::PlainText pt; @@ -34,16 +34,13 @@ TEST(CryptoTest, CryptoTest) { ipcl::setHybridRatio(qat_ratio); - ct = key.pub_key->encrypt(pt); - dt = key.priv_key->decrypt(ct); + ct = keys.pk.encrypt(pt); + dt = keys.sk.decrypt(ct); for (int i = 0; i < num_values; i++) { std::vector v = dt.getElementVec(i); EXPECT_EQ(v[0], exp_value[i]); } - - delete key.pub_key; - delete key.priv_key; } TEST(CryptoTest, ISO_IEC_18033_6_ComplianceTest) { @@ -65,10 +62,10 @@ TEST(CryptoTest, ISO_IEC_18033_6_ComplianceTest) { BigNumber n = p * q; int n_length = n.BitSize(); - ipcl::PublicKey* public_key = new ipcl::PublicKey(n, n_length); - ipcl::PrivateKey* private_key = new ipcl::PrivateKey(public_key, p, q); + ipcl::PublicKey pk(n, n_length); + ipcl::PrivateKey sk(&pk, p, q); - ipcl::keyPair key = {public_key, private_key}; + ipcl::KeyPair keys = {pk, sk}; std::vector pt_bn_v(num_values); std::vector ir_bn_v(num_values); @@ -157,13 +154,13 @@ TEST(CryptoTest, ISO_IEC_18033_6_ComplianceTest) { ipcl::setHybridOff(); - key.pub_key->setRandom(ir_bn_v); + keys.pk.setRandom(ir_bn_v); pt = ipcl::PlainText(pt_bn_v); - ct = key.pub_key->encrypt(pt); + ct = keys.pk.encrypt(pt); ipcl::PlainText dt; - dt = key.priv_key->decrypt(ct); + dt = keys.sk.decrypt(ct); for (int i = 0; i < num_values; i++) { EXPECT_EQ(dt.getElement(i), pt_bn_v[i]); } @@ -174,8 +171,8 @@ TEST(CryptoTest, ISO_IEC_18033_6_ComplianceTest) { c2.num2hex(str2); EXPECT_EQ(str2, ct.getElementHex(1)); - ipcl::CipherText a(key.pub_key, ct.getElement(0)); - ipcl::CipherText b(key.pub_key, ct.getElement(1)); + ipcl::CipherText a(&(keys.pk), ct.getElement(0)); + ipcl::CipherText b(&(keys.pk), ct.getElement(1)); ipcl::CipherText sum = a + b; std::string str3; @@ -185,10 +182,7 @@ TEST(CryptoTest, ISO_IEC_18033_6_ComplianceTest) { std::string str4; ipcl::PlainText dt_sum; - dt_sum = key.priv_key->decrypt(sum); + dt_sum = keys.sk.decrypt(sum); m1m2.num2hex(str4); EXPECT_EQ(str4, dt_sum.getElementHex(0)); - - delete key.pub_key; - delete key.priv_key; } diff --git a/test/test_ops.cpp b/test/test_ops.cpp index 7a33a1e..035c9d3 100644 --- a/test/test_ops.cpp +++ b/test/test_ops.cpp @@ -12,114 +12,114 @@ constexpr int SELF_DEF_NUM_VALUES = 14; constexpr float SELF_DEF_HYBRID_QAT_RATIO = 0.5; void CtPlusCt(ipcl::CipherText& res, const ipcl::CipherText& ct1, - const ipcl::CipherText& ct2, const ipcl::keyPair key) { + const ipcl::CipherText& ct2, const ipcl::KeyPair keys) { int size = ct1.getSize(); std::vector sum_bn_v(size); for (int i = 0; i < size; i++) { - ipcl::CipherText a(key.pub_key, ct1.getElement(i)); - ipcl::CipherText b(key.pub_key, ct2.getElement(i)); + ipcl::CipherText a(&(keys.pk), ct1.getElement(i)); + ipcl::CipherText b(&(keys.pk), ct2.getElement(i)); ipcl::CipherText sum = a + b; sum_bn_v[i] = sum.getElement(0); } - res = ipcl::CipherText(key.pub_key, sum_bn_v); + res = ipcl::CipherText(&(keys.pk), sum_bn_v); } void CtPlusCtArray(ipcl::CipherText& res, const ipcl::CipherText& ct1, - const ipcl::CipherText& ct2, const ipcl::keyPair key) { + const ipcl::CipherText& ct2) { res = ct1 + ct2; } void CtPlusPt(ipcl::CipherText& res, const ipcl::CipherText& ct1, - const ipcl::PlainText& pt2, const ipcl::keyPair key) { + const ipcl::PlainText& pt2, const ipcl::KeyPair keys) { int size = ct1.getSize(); std::vector sum_bn_v(size); for (int i = 0; i < size; i++) { - ipcl::CipherText a(key.pub_key, ct1.getElement(i)); + ipcl::CipherText a(&(keys.pk), ct1.getElement(i)); ipcl::PlainText b(pt2.getElement(i)); ipcl::CipherText sum = a + b; sum_bn_v[i] = sum.getElement(0); } - res = ipcl::CipherText(key.pub_key, sum_bn_v); + res = ipcl::CipherText(&(keys.pk), sum_bn_v); } void CtPlusPtArray(ipcl::CipherText& res, const ipcl::CipherText& ct1, - const ipcl::PlainText& pt2, const ipcl::keyPair key) { + const ipcl::PlainText& pt2) { res = ct1 + pt2; } void CtMultiplyPt(ipcl::CipherText& res, const ipcl::CipherText& ct1, - const ipcl::PlainText& pt2, const ipcl::keyPair key) { + const ipcl::PlainText& pt2, const ipcl::KeyPair keys) { int size = ct1.getSize(); std::vector product_bn_v(size); for (int i = 0; i < size; i++) { - ipcl::CipherText a(key.pub_key, ct1.getElement(i)); + ipcl::CipherText a(&(keys.pk), ct1.getElement(i)); ipcl::PlainText b(pt2.getElement(i)); ipcl::CipherText product = a * b; product_bn_v[i] = product.getElement(0); } - res = ipcl::CipherText(key.pub_key, product_bn_v); + res = ipcl::CipherText(&(keys.pk), product_bn_v); } void CtMultiplyPtArray(ipcl::CipherText& res, const ipcl::CipherText& ct1, - const ipcl::PlainText& pt2, const ipcl::keyPair key) { + const ipcl::PlainText& pt2) { res = ct1 * pt2; } void AddSub(ipcl::CipherText& res, const ipcl::CipherText& ct1, - const ipcl::CipherText& ct2, const ipcl::keyPair key) { + const ipcl::CipherText& ct2, const ipcl::KeyPair keys) { int size = ct1.getSize(); std::vector sum_bn_v(size); for (int i = 0; i < size; i++) { - ipcl::CipherText a(key.pub_key, ct1.getElement(i)); - ipcl::CipherText b(key.pub_key, ct2.getElement(i)); + ipcl::CipherText a(&(keys.pk), ct1.getElement(i)); + ipcl::CipherText b(&(keys.pk), ct2.getElement(i)); ipcl::PlainText m1(2); a = a + b * m1; ipcl::CipherText sum = a + b; sum_bn_v[i] = sum.getElement(0); } - res = ipcl::CipherText(key.pub_key, sum_bn_v); + res = ipcl::CipherText(&(keys.pk), sum_bn_v); } void PtPlusCt(ipcl::CipherText& res, const ipcl::PlainText& pt2, - const ipcl::CipherText& ct1, const ipcl::keyPair key) { + const ipcl::CipherText& ct1, const ipcl::KeyPair keys) { int size = ct1.getSize(); std::vector sum_bn_v(size); for (int i = 0; i < size; i++) { - ipcl::CipherText a(key.pub_key, ct1.getElement(i)); + ipcl::CipherText a(&(keys.pk), ct1.getElement(i)); ipcl::PlainText b(pt2.getElement(i)); ipcl::CipherText sum = b + a; sum_bn_v[i] = sum.getElement(0); } - res = ipcl::CipherText(key.pub_key, sum_bn_v); + res = ipcl::CipherText(&(keys.pk), sum_bn_v); } void PtPlusCtArray(ipcl::CipherText& res, const ipcl::PlainText& pt2, - const ipcl::CipherText& ct1, const ipcl::keyPair key) { + const ipcl::CipherText& ct1) { res = pt2 + ct1; } void PtMultiplyCt(ipcl::CipherText& res, const ipcl::PlainText& pt2, - const ipcl::CipherText& ct1, const ipcl::keyPair key) { + const ipcl::CipherText& ct1, const ipcl::KeyPair keys) { int size = ct1.getSize(); std::vector product_bn_v(size); for (int i = 0; i < size; i++) { - ipcl::CipherText a(key.pub_key, ct1.getElement(i)); + ipcl::CipherText a(&(keys.pk), ct1.getElement(i)); ipcl::PlainText b(pt2.getElement(i)); ipcl::CipherText product = b * a; product_bn_v[i] = product.getElement(0); } - res = ipcl::CipherText(key.pub_key, product_bn_v); + res = ipcl::CipherText(&(keys.pk), product_bn_v); } void PtMultiplyCtArray(ipcl::CipherText& res, const ipcl::PlainText& pt2, - const ipcl::CipherText& ct1, const ipcl::keyPair key) { + const ipcl::CipherText& ct1) { res = pt2 * ct1; } @@ -127,7 +127,7 @@ TEST(OperationTest, CtPlusCtTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::keyPair key = ipcl::generateKeypair(2048); + ipcl::KeyPair keys = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_sum; @@ -146,12 +146,12 @@ TEST(OperationTest, CtPlusCtTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = key.pub_key->encrypt(pt1); - ct2 = key.pub_key->encrypt(pt2); + ct1 = keys.pk.encrypt(pt1); + ct2 = keys.pk.encrypt(pt2); - CtPlusCt(ct_sum, ct1, ct2, key); + CtPlusCt(ct_sum, ct1, ct2, keys); - dt_sum = key.priv_key->decrypt(ct_sum); + dt_sum = keys.sk.decrypt(ct_sum); for (int i = 0; i < num_values; i++) { std::vector v = dt_sum.getElementVec(i); @@ -162,16 +162,13 @@ TEST(OperationTest, CtPlusCtTest) { EXPECT_EQ(sum, exp_sum); } - - delete key.pub_key; - delete key.priv_key; } TEST(OperationTest, CtPlusCtArrayTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::keyPair key = ipcl::generateKeypair(2048); + ipcl::KeyPair keys = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_sum; @@ -190,12 +187,12 @@ TEST(OperationTest, CtPlusCtArrayTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = key.pub_key->encrypt(pt1); - ct2 = key.pub_key->encrypt(pt2); + ct1 = keys.pk.encrypt(pt1); + ct2 = keys.pk.encrypt(pt2); - CtPlusCtArray(ct_sum, ct1, ct2, key); + CtPlusCtArray(ct_sum, ct1, ct2); - dt_sum = key.priv_key->decrypt(ct_sum); + dt_sum = keys.sk.decrypt(ct_sum); for (int i = 0; i < num_values; i++) { std::vector v = dt_sum.getElementVec(i); @@ -206,16 +203,13 @@ TEST(OperationTest, CtPlusCtArrayTest) { EXPECT_EQ(sum, exp_sum); } - - delete key.pub_key; - delete key.priv_key; } TEST(OperationTest, CtPlusPtTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::keyPair key = ipcl::generateKeypair(2048); + ipcl::KeyPair keys = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_sum; @@ -234,11 +228,11 @@ TEST(OperationTest, CtPlusPtTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = key.pub_key->encrypt(pt1); + ct1 = keys.pk.encrypt(pt1); - CtPlusPt(ct_sum, ct1, pt2, key); + CtPlusPt(ct_sum, ct1, pt2, keys); - dt_sum = key.priv_key->decrypt(ct_sum); + dt_sum = keys.sk.decrypt(ct_sum); for (int i = 0; i < num_values; i++) { std::vector v = dt_sum.getElementVec(i); @@ -249,16 +243,13 @@ TEST(OperationTest, CtPlusPtTest) { EXPECT_EQ(sum, exp_sum); } - - delete key.pub_key; - delete key.priv_key; } TEST(OperationTest, CtPlusPtArrayTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::keyPair key = ipcl::generateKeypair(2048); + ipcl::KeyPair keys = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_sum; @@ -277,11 +268,11 @@ TEST(OperationTest, CtPlusPtArrayTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = key.pub_key->encrypt(pt1); + ct1 = keys.pk.encrypt(pt1); - CtPlusPtArray(ct_sum, ct1, pt2, key); + CtPlusPtArray(ct_sum, ct1, pt2); - dt_sum = key.priv_key->decrypt(ct_sum); + dt_sum = keys.sk.decrypt(ct_sum); for (int i = 0; i < num_values; i++) { std::vector v = dt_sum.getElementVec(i); @@ -292,16 +283,13 @@ TEST(OperationTest, CtPlusPtArrayTest) { EXPECT_EQ(sum, exp_sum); } - - delete key.pub_key; - delete key.priv_key; } TEST(OperationTest, CtMultiplyPtTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::keyPair key = ipcl::generateKeypair(2048); + ipcl::KeyPair keys = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_product; @@ -320,11 +308,11 @@ TEST(OperationTest, CtMultiplyPtTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = key.pub_key->encrypt(pt1); + ct1 = keys.pk.encrypt(pt1); - CtMultiplyPt(ct_product, ct1, pt2, key); + CtMultiplyPt(ct_product, ct1, pt2, keys); - dt_product = key.priv_key->decrypt(ct_product); + dt_product = keys.sk.decrypt(ct_product); for (int i = 0; i < num_values; i++) { std::vector v = dt_product.getElementVec(i); @@ -335,16 +323,13 @@ TEST(OperationTest, CtMultiplyPtTest) { EXPECT_EQ(product, exp_product); } - - delete key.pub_key; - delete key.priv_key; } TEST(OperationTest, CtMultiplyZeroPtTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::keyPair key = ipcl::generateKeypair(2048); + ipcl::KeyPair keys = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_product; @@ -364,11 +349,11 @@ TEST(OperationTest, CtMultiplyZeroPtTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = key.pub_key->encrypt(pt1); + ct1 = keys.pk.encrypt(pt1); - CtMultiplyPt(ct_product, ct1, pt2, key); + CtMultiplyPt(ct_product, ct1, pt2, keys); - dt_product = key.priv_key->decrypt(ct_product); + dt_product = keys.sk.decrypt(ct_product); for (int i = 0; i < num_values; i++) { std::vector v = dt_product.getElementVec(i); @@ -379,16 +364,13 @@ TEST(OperationTest, CtMultiplyZeroPtTest) { EXPECT_EQ(product, exp_product); } - - delete key.pub_key; - delete key.priv_key; } TEST(OperationTest, CtMultiplyPtArrayTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::keyPair key = ipcl::generateKeypair(2048); + ipcl::KeyPair keys = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_product; @@ -407,11 +389,11 @@ TEST(OperationTest, CtMultiplyPtArrayTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = key.pub_key->encrypt(pt1); + ct1 = keys.pk.encrypt(pt1); - CtMultiplyPtArray(ct_product, ct1, pt2, key); + CtMultiplyPtArray(ct_product, ct1, pt2); - dt_product = key.priv_key->decrypt(ct_product); + dt_product = keys.sk.decrypt(ct_product); for (int i = 0; i < num_values; i++) { std::vector v = dt_product.getElementVec(i); @@ -422,16 +404,13 @@ TEST(OperationTest, CtMultiplyPtArrayTest) { EXPECT_EQ(product, exp_product); } - - delete key.pub_key; - delete key.priv_key; } TEST(OperationTest, AddSubTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::keyPair key = ipcl::generateKeypair(2048); + ipcl::KeyPair keys = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_sum; @@ -450,12 +429,12 @@ TEST(OperationTest, AddSubTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = key.pub_key->encrypt(pt1); - ct2 = key.pub_key->encrypt(pt2); + ct1 = keys.pk.encrypt(pt1); + ct2 = keys.pk.encrypt(pt2); - AddSub(ct_sum, ct1, ct2, key); + AddSub(ct_sum, ct1, ct2, keys); - dt_sum = key.priv_key->decrypt(ct_sum); + dt_sum = keys.sk.decrypt(ct_sum); for (int i = 0; i < num_values; i++) { std::vector v = dt_sum.getElementVec(i); @@ -466,16 +445,13 @@ TEST(OperationTest, AddSubTest) { EXPECT_EQ(sum, exp_sum); } - - delete key.pub_key; - delete key.priv_key; } TEST(OperationTest, PtPlusCtTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::keyPair key = ipcl::generateKeypair(2048); + ipcl::KeyPair keys = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_sum; @@ -494,11 +470,11 @@ TEST(OperationTest, PtPlusCtTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = key.pub_key->encrypt(pt1); + ct1 = keys.pk.encrypt(pt1); - PtPlusCt(ct_sum, pt2, ct1, key); + PtPlusCt(ct_sum, pt2, ct1, keys); - dt_sum = key.priv_key->decrypt(ct_sum); + dt_sum = keys.sk.decrypt(ct_sum); for (int i = 0; i < num_values; i++) { std::vector v = dt_sum.getElementVec(i); @@ -509,16 +485,13 @@ TEST(OperationTest, PtPlusCtTest) { EXPECT_EQ(sum, exp_sum); } - - delete key.pub_key; - delete key.priv_key; } TEST(OperationTest, PtPlusCtArrayTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::keyPair key = ipcl::generateKeypair(2048); + ipcl::KeyPair keys = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_sum; @@ -537,11 +510,11 @@ TEST(OperationTest, PtPlusCtArrayTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = key.pub_key->encrypt(pt1); + ct1 = keys.pk.encrypt(pt1); - PtPlusCtArray(ct_sum, pt2, ct1, key); + PtPlusCtArray(ct_sum, pt2, ct1); - dt_sum = key.priv_key->decrypt(ct_sum); + dt_sum = keys.sk.decrypt(ct_sum); for (int i = 0; i < num_values; i++) { std::vector v = dt_sum.getElementVec(i); @@ -552,16 +525,13 @@ TEST(OperationTest, PtPlusCtArrayTest) { EXPECT_EQ(sum, exp_sum); } - - delete key.pub_key; - delete key.priv_key; } TEST(OperationTest, PtMultiplyCtTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::keyPair key = ipcl::generateKeypair(2048); + ipcl::KeyPair keys = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_product; @@ -580,11 +550,11 @@ TEST(OperationTest, PtMultiplyCtTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = key.pub_key->encrypt(pt1); + ct1 = keys.pk.encrypt(pt1); - PtMultiplyCt(ct_product, pt2, ct1, key); + PtMultiplyCt(ct_product, pt2, ct1, keys); - dt_product = key.priv_key->decrypt(ct_product); + dt_product = keys.sk.decrypt(ct_product); for (int i = 0; i < num_values; i++) { std::vector v = dt_product.getElementVec(i); @@ -595,16 +565,13 @@ TEST(OperationTest, PtMultiplyCtTest) { EXPECT_EQ(product, exp_product); } - - delete key.pub_key; - delete key.priv_key; } TEST(OperationTest, PtMultiplyCtArrayTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::keyPair key = ipcl::generateKeypair(2048); + ipcl::KeyPair keys = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_product; @@ -623,11 +590,11 @@ TEST(OperationTest, PtMultiplyCtArrayTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = key.pub_key->encrypt(pt1); + ct1 = keys.pk.encrypt(pt1); - PtMultiplyCtArray(ct_product, pt2, ct1, key); + PtMultiplyCtArray(ct_product, pt2, ct1); - dt_product = key.priv_key->decrypt(ct_product); + dt_product = keys.sk.decrypt(ct_product); for (int i = 0; i < num_values; i++) { std::vector v = dt_product.getElementVec(i); @@ -638,7 +605,4 @@ TEST(OperationTest, PtMultiplyCtArrayTest) { EXPECT_EQ(product, exp_product); } - - delete key.pub_key; - delete key.priv_key; } From dc6c3a95e813197bb206e182bc1c3eaedc3c8f31 Mon Sep 17 00:00:00 2001 From: Zhao Pengfei Date: Wed, 23 Nov 2022 20:22:55 +0800 Subject: [PATCH 04/12] example: Updated with smart ptr version pk, sk and keypair Signed-off-by: Zhao Pengfei --- example/example_add_mul.cpp | 14 ++++++-------- example/example_encrypt_decrypt.cpp | 8 +++----- example/example_hybridmode.cpp | 16 +++++++--------- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/example/example_add_mul.cpp b/example/example_add_mul.cpp index d53a61f..32a680a 100644 --- a/example/example_add_mul.cpp +++ b/example/example_add_mul.cpp @@ -21,7 +21,7 @@ int main() { const uint32_t num_total = 20; - ipcl::keyPair key = ipcl::generateKeypair(2048, true); + ipcl::KeyPair keys = ipcl::generateKeypair(2048, true); std::vector x(num_total); std::vector y(num_total); @@ -41,13 +41,13 @@ int main() { ipcl::setHybridMode(ipcl::HybridMode::OPTIMAL); - ipcl::CipherText ct_x = key.pub_key->encrypt(pt_x); - ipcl::CipherText ct_y = key.pub_key->encrypt(pt_y); + ipcl::CipherText ct_x = keys.pk.encrypt(pt_x); + ipcl::CipherText ct_y = keys.sk.encrypt(pt_y); // Perform enc(x) + enc(y) std::cout << "--- IPCL CipherText + CipherText ---" << std::endl; ipcl::CipherText ct_add_ctx_cty = ct_x + ct_y; - ipcl::PlainText dt_add_ctx_cty = key.priv_key->decrypt(ct_add_ctx_cty); + ipcl::PlainText dt_add_ctx_cty = keys.sk.decrypt(ct_add_ctx_cty); // verify result bool verify = true; @@ -65,7 +65,7 @@ int main() { // Perform enc(x) + y std::cout << "--- IPCL CipherText + PlainText ---" << std::endl; ipcl::CipherText ct_add_ctx_pty = ct_x + pt_y; - ipcl::PlainText dt_add_ctx_pty = key.priv_key->decrypt(ct_add_ctx_pty); + ipcl::PlainText dt_add_ctx_pty = keys.sk.decrypt(ct_add_ctx_pty); // verify result verify = true; @@ -83,7 +83,7 @@ int main() { // Perform enc(x) * y std::cout << "--- IPCL CipherText * PlainText ---" << std::endl; ipcl::CipherText ct_mul_ctx_pty = ct_x * pt_y; - ipcl::PlainText dt_mul_ctx_pty = key.priv_key->decrypt(ct_mul_ctx_pty); + ipcl::PlainText dt_mul_ctx_pty = keys.sk.decrypt(ct_mul_ctx_pty); // verify result verify = true; @@ -99,8 +99,6 @@ int main() { ipcl::setHybridOff(); - delete key.pub_key; - delete key.priv_key; ipcl::terminateContext(); std::cout << "Complete!" << std::endl; } diff --git a/example/example_encrypt_decrypt.cpp b/example/example_encrypt_decrypt.cpp index 50f08c9..ab47259 100644 --- a/example/example_encrypt_decrypt.cpp +++ b/example/example_encrypt_decrypt.cpp @@ -21,7 +21,7 @@ int main() { const uint32_t num_total = 20; - ipcl::keyPair key = ipcl::generateKeypair(2048, true); + ipcl::KeyPair keys = ipcl::generateKeypair(2048, true); std::vector exp_value(num_total); @@ -37,8 +37,8 @@ int main() { ipcl::setHybridMode(ipcl::HybridMode::OPTIMAL); - ipcl::CipherText ct = key.pub_key->encrypt(pt); - ipcl::PlainText dt = key.priv_key->decrypt(ct); + ipcl::CipherText ct = keys.pk.encrypt(pt); + ipcl::PlainText dt = keys.sk.decrypt(ct); ipcl::setHybridOff(); @@ -54,8 +54,6 @@ int main() { std::cout << "Test pt == dec(enc(pt)) -- " << (verify ? "pass" : "fail") << std::endl; - delete key.pub_key; - delete key.priv_key; ipcl::terminateContext(); std::cout << "Complete!" << std::endl << std::endl; } diff --git a/example/example_hybridmode.cpp b/example/example_hybridmode.cpp index 66da0e5..c0692db 100644 --- a/example/example_hybridmode.cpp +++ b/example/example_hybridmode.cpp @@ -30,7 +30,7 @@ int main() { const uint32_t num_total = 64; - ipcl::keyPair key = ipcl::generateKeypair(2048, true); + ipcl::KeyPair keys = ipcl::generateKeypair(2048, true); std::vector exp_value(num_total); @@ -56,12 +56,12 @@ int main() { // Encrypt/Decrypt - IPP-Crypto only mode ipcl::setHybridMode(ipcl::HybridMode::IPP); tStart(t); - ipcl::CipherText ct = key.pub_key->encrypt(pt); + ipcl::CipherText ct = keys.pk.encrypt(pt); elapsed = tEnd(t); std::cout << " Encrypt - HybridMode::IPP = " << elapsed << "ms" << std::endl; tStart(t); - ipcl::PlainText dt = key.priv_key->decrypt(ct); + ipcl::PlainText dt = keys.sk.decrypt(ct); elapsed = tEnd(t); std::cout << " Decrypt - HybridMode::IPP = " << elapsed << "ms" << std::endl @@ -70,12 +70,12 @@ int main() { // Encrypt/Decrypt - QAT only mode ipcl::setHybridMode(ipcl::HybridMode::QAT); tStart(t); - ct = key.pub_key->encrypt(pt); + ct = keys.pk.encrypt(pt); elapsed = tEnd(t); std::cout << " Encrypt - HybridMode::QAT = " << elapsed << "ms" << std::endl; tStart(t); - dt = key.priv_key->decrypt(ct); + dt = keys.sk.decrypt(ct); elapsed = tEnd(t); std::cout << " Decrypt - HybridMode::QAT = " << elapsed << "ms" << std::endl @@ -84,19 +84,17 @@ int main() { // Encrypt/Decrypt - OPTIMAL mode ipcl::setHybridMode(ipcl::HybridMode::OPTIMAL); tStart(t); - ct = key.pub_key->encrypt(pt); + ct = keys.pk.encrypt(pt); elapsed = tEnd(t); std::cout << " Encrypt - HybridMode::OPTIMAL = " << elapsed << "ms" << std::endl; tStart(t); - dt = key.priv_key->decrypt(ct); + dt = keys.sk.decrypt(ct); elapsed = tEnd(t); std::cout << " Decrypt - HybridMode::OPTIMAL = " << elapsed << "ms" << std::endl << std::endl; - delete key.pub_key; - delete key.priv_key; ipcl::terminateContext(); std::cout << "Complete!" << std::endl << std::endl; } From 66de7410f64b0b8a89484bad8e1580eefa0739b3 Mon Sep 17 00:00:00 2001 From: Zhao Pengfei Date: Thu, 24 Nov 2022 16:08:02 +0800 Subject: [PATCH 05/12] sk: Change pk parameter type in ctor. ct: Change pk parameter type in ctor and m_pk type. pk, unittest, benchmark: Updated with new sk & ct structure. Signed-off-by: Zhao Pengfei --- benchmark/bench_cryptography.cpp | 4 +-- benchmark/bench_hybrid.cpp | 8 ++--- benchmark/bench_ops.cpp | 6 ++-- ipcl/ciphertext.cpp | 50 +++++++++++++++----------------- ipcl/include/ipcl/ciphertext.hpp | 13 +++++---- ipcl/include/ipcl/pri_key.hpp | 5 ++-- ipcl/keygen.cpp | 2 +- ipcl/pri_key.cpp | 8 ++--- ipcl/pub_key.cpp | 2 +- test/test_cryptography.cpp | 6 ++-- test/test_ops.cpp | 28 +++++++++--------- 11 files changed, 65 insertions(+), 67 deletions(-) diff --git a/benchmark/bench_cryptography.cpp b/benchmark/bench_cryptography.cpp index 5416a01..33b0d12 100644 --- a/benchmark/bench_cryptography.cpp +++ b/benchmark/bench_cryptography.cpp @@ -76,7 +76,7 @@ static void BM_Encrypt(benchmark::State& state) { BigNumber n = P_BN * Q_BN; int n_length = n.BitSize(); ipcl::PublicKey pk(n, n_length, Enable_DJN); - ipcl::PrivateKey sk(&pk, P_BN, Q_BN); + ipcl::PrivateKey sk(pk, P_BN, Q_BN); std::vector r_bn_v(dsize, R_BN); pk.setRandom(r_bn_v); @@ -101,7 +101,7 @@ static void BM_Decrypt(benchmark::State& state) { BigNumber n = P_BN * Q_BN; int n_length = n.BitSize(); ipcl::PublicKey pk(n, n_length, Enable_DJN); - ipcl::PrivateKey sk(&pk, P_BN, Q_BN); + ipcl::PrivateKey sk(pk, P_BN, Q_BN); std::vector r_bn_v(dsize, R_BN); pk.setRandom(r_bn_v); diff --git a/benchmark/bench_hybrid.cpp b/benchmark/bench_hybrid.cpp index ad24a90..ecab201 100644 --- a/benchmark/bench_hybrid.cpp +++ b/benchmark/bench_hybrid.cpp @@ -86,7 +86,7 @@ static void BM_Hybrid_ModExp(benchmark::State& state) { BigNumber n = P_BN * Q_BN; int n_length = n.BitSize(); ipcl::PublicKey pk(n, n_length, Enable_DJN); - ipcl::PrivateKey sk(&pk, P_BN, Q_BN); + ipcl::PrivateKey sk(pk, P_BN, Q_BN); std::vector r_bn_v(dsize, R_BN); pk.setRandom(r_bn_v); @@ -126,7 +126,7 @@ static void BM_Hybrid_Encrypt(benchmark::State& state) { BigNumber n = P_BN * Q_BN; int n_length = n.BitSize(); ipcl::PublicKey pk(n, n_length, Enable_DJN); - ipcl::PrivateKey sk(&pk, P_BN, Q_BN); + ipcl::PrivateKey sk(pk, P_BN, Q_BN); std::vector r_bn_v(dsize, R_BN); pk.setRandom(r_bn_v); @@ -159,7 +159,7 @@ static void BM_Hybrid_Decrypt(benchmark::State& state) { BigNumber n = P_BN * Q_BN; int n_length = n.BitSize(); ipcl::PublicKey pk(n, n_length, Enable_DJN); - ipcl::PrivateKey sk(&pk, P_BN, Q_BN); + ipcl::PrivateKey sk(pk, P_BN, Q_BN); std::vector r_bn_v(dsize, R_BN); pk.setRandom(r_bn_v); @@ -193,7 +193,7 @@ static void BM_Hybrid_MulCTPT(benchmark::State& state) { BigNumber n = P_BN * Q_BN; int n_length = n.BitSize(); ipcl::PublicKey pk(n, n_length, Enable_DJN); - ipcl::PrivateKey sk(&pk, P_BN, Q_BN); + ipcl::PrivateKey sk(pk, P_BN, Q_BN); std::vector r_bn_v(dsize, R_BN); pk.setRandom(r_bn_v); diff --git a/benchmark/bench_ops.cpp b/benchmark/bench_ops.cpp index 3aa9de6..de97b06 100644 --- a/benchmark/bench_ops.cpp +++ b/benchmark/bench_ops.cpp @@ -68,7 +68,7 @@ static void BM_Add_CTCT(benchmark::State& state) { BigNumber n = P_BN * Q_BN; int n_length = n.BitSize(); ipcl::PublicKey pk(n, n_length, Enable_DJN); - ipcl::PrivateKey sk(&pk, P_BN, Q_BN); + ipcl::PrivateKey sk(pk, P_BN, Q_BN); std::vector r_bn_v(dsize, R_BN); pk.setRandom(r_bn_v); @@ -99,7 +99,7 @@ static void BM_Add_CTPT(benchmark::State& state) { BigNumber n = P_BN * Q_BN; int n_length = n.BitSize(); ipcl::PublicKey pk(n, n_length, Enable_DJN); - ipcl::PrivateKey sk(&pk, P_BN, Q_BN); + ipcl::PrivateKey sk(pk, P_BN, Q_BN); std::vector r_bn_v(dsize, R_BN); pk.setRandom(r_bn_v); @@ -128,7 +128,7 @@ static void BM_Mul_CTPT(benchmark::State& state) { BigNumber n = P_BN * Q_BN; int n_length = n.BitSize(); ipcl::PublicKey pk(n, n_length, Enable_DJN); - ipcl::PrivateKey sk(&pk, P_BN, Q_BN); + ipcl::PrivateKey sk(pk, P_BN, Q_BN); std::vector r_bn_v(dsize, R_BN); pk.setRandom(r_bn_v); diff --git a/ipcl/ciphertext.cpp b/ipcl/ciphertext.cpp index d8349ac..e0d4b23 100644 --- a/ipcl/ciphertext.cpp +++ b/ipcl/ciphertext.cpp @@ -8,27 +8,25 @@ #include "ipcl/mod_exp.hpp" namespace ipcl { -CipherText::CipherText(const PublicKey* pub_key, const uint32_t& n) - : BaseText(n), m_pubkey(pub_key) {} +CipherText::CipherText(const PublicKey& pk, const uint32_t& n) + : BaseText(n), m_pk(std::make_shared(pk)) {} -CipherText::CipherText(const PublicKey* pub_key, - const std::vector& n_v) - : BaseText(n_v), m_pubkey(pub_key) {} +CipherText::CipherText(const PublicKey& pk, const std::vector& n_v) + : BaseText(n_v), m_pk(std::make_shared(pk)) {} -CipherText::CipherText(const PublicKey* pub_key, const BigNumber& bn) - : BaseText(bn), m_pubkey(pub_key) {} +CipherText::CipherText(const PublicKey& pk, const BigNumber& bn) + : BaseText(bn), m_pk(std::make_shared(pk)) {} -CipherText::CipherText(const PublicKey* pub_key, - const std::vector& bn_v) - : BaseText(bn_v), m_pubkey(pub_key) {} +CipherText::CipherText(const PublicKey& pk, const std::vector& bn_v) + : BaseText(bn_v), m_pk(std::make_shared(pk)) {} CipherText::CipherText(const CipherText& ct) : BaseText(ct) { - this->m_pubkey = ct.m_pubkey; + this->m_pk = ct.m_pk; } CipherText& CipherText::operator=(const CipherText& other) { BaseText::operator=(other); - this->m_pubkey = other.m_pubkey; + this->m_pk = other.m_pk; return *this; } @@ -38,7 +36,7 @@ CipherText CipherText::operator+(const CipherText& other) const { std::size_t b_size = other.getSize(); ERROR_CHECK(this->m_size == b_size || b_size == 1, "CT + CT error: Size mismatch!"); - ERROR_CHECK(*(m_pubkey->getN()) == *(other.m_pubkey->getN()), + ERROR_CHECK(*(m_pk->getN()) == *(other.m_pk->getN()), "CT + CT error: 2 different public keys detected!"); const auto& a = *this; @@ -46,7 +44,7 @@ CipherText CipherText::operator+(const CipherText& other) const { if (m_size == 1) { BigNumber sum = a.raw_add(a.m_texts.front(), b.getTexts().front()); - return CipherText(m_pubkey, sum); + return CipherText(*m_pk, sum); } else { std::vector sum(m_size); @@ -69,14 +67,14 @@ CipherText CipherText::operator+(const CipherText& other) const { for (std::size_t i = 0; i < m_size; i++) sum[i] = a.raw_add(a.m_texts[i], b.m_texts[i]); } - return CipherText(m_pubkey, sum); + return CipherText(*m_pk, sum); } } // CT + PT CipherText CipherText::operator+(const PlainText& other) const { // convert PT to CT - CipherText b = this->m_pubkey->encrypt(other, false); + CipherText b = this->m_pk->encrypt(other, false); // calculate CT + CT return this->operator+(b); } @@ -92,7 +90,7 @@ CipherText CipherText::operator*(const PlainText& other) const { if (m_size == 1) { BigNumber product = a.raw_mul(a.m_texts.front(), b.getTexts().front()); - return CipherText(m_pubkey, product); + return CipherText(*m_pk, product); } else { std::vector product; if (b_size == 1) { @@ -103,7 +101,7 @@ CipherText CipherText::operator*(const PlainText& other) const { // multiply vector by vector product = a.raw_mul(a.m_texts, b.getTexts()); } - return CipherText(m_pubkey, product); + return CipherText(*m_pk, product); } } @@ -111,10 +109,10 @@ CipherText CipherText::getCipherText(const size_t& idx) const { ERROR_CHECK((idx >= 0) && (idx < m_size), "CipherText::getCipherText index is out of range"); - return CipherText(m_pubkey, m_texts[idx]); + return CipherText(*m_pk, m_texts[idx]); } -const PublicKey* CipherText::getPubKey() const { return m_pubkey; } +std::shared_ptr CipherText::getPubKey() const { return m_pk; } CipherText CipherText::rotate(int shift) const { ERROR_CHECK(m_size != 1, "rotate: Cannot rotate single CipherText"); @@ -122,7 +120,7 @@ CipherText CipherText::rotate(int shift) const { "rotate: Cannot shift more than the test size"); if (shift == 0 || shift == m_size || shift == (-1) * static_cast(m_size)) - return CipherText(m_pubkey, m_texts); + return CipherText(*m_pk, m_texts); if (shift > 0) shift = m_size - shift; @@ -131,26 +129,26 @@ CipherText CipherText::rotate(int shift) const { std::vector new_bn = getTexts(); std::rotate(std::begin(new_bn), std::begin(new_bn) + shift, std::end(new_bn)); - return CipherText(m_pubkey, new_bn); + return CipherText(*m_pk, new_bn); } BigNumber CipherText::raw_add(const BigNumber& a, const BigNumber& b) const { // Hold a copy of nsquare for multi-threaded // The BigNumber % operator is not thread safe - // const BigNumber& sq = *(m_pubkey->getNSQ()); - const BigNumber sq = *(m_pubkey->getNSQ()); + // const BigNumber& sq = *(m_pk->getNSQ()); + const BigNumber sq = *(m_pk->getNSQ()); return a * b % sq; } BigNumber CipherText::raw_mul(const BigNumber& a, const BigNumber& b) const { - const BigNumber& sq = *(m_pubkey->getNSQ()); + const BigNumber& sq = *(m_pk->getNSQ()); return modExp(a, b, sq); } std::vector CipherText::raw_mul( const std::vector& a, const std::vector& b) const { std::size_t v_size = a.size(); - std::vector sq(v_size, *(m_pubkey->getNSQ())); + std::vector sq(v_size, *(m_pk->getNSQ())); // If hybrid OPTIMAL mode is used, use a special ratio if (isHybridOptimal()) { diff --git a/ipcl/include/ipcl/ciphertext.hpp b/ipcl/include/ipcl/ciphertext.hpp index b344ad0..8c116af 100644 --- a/ipcl/include/ipcl/ciphertext.hpp +++ b/ipcl/include/ipcl/ciphertext.hpp @@ -4,6 +4,7 @@ #ifndef IPCL_INCLUDE_IPCL_CIPHERTEXT_HPP_ #define IPCL_INCLUDE_IPCL_CIPHERTEXT_HPP_ +#include #include #include "ipcl/plaintext.hpp" @@ -20,10 +21,10 @@ class CipherText : public BaseText { /** * CipherText constructors */ - CipherText(const PublicKey* pub_key, const uint32_t& n); - CipherText(const PublicKey* pub_key, const std::vector& n_v); - CipherText(const PublicKey* pub_key, const BigNumber& bn); - CipherText(const PublicKey* pub_key, const std::vector& bn_vec); + CipherText(const PublicKey& pk, const uint32_t& n); + CipherText(const PublicKey& pk, const std::vector& n_v); + CipherText(const PublicKey& pk, const BigNumber& bn); + CipherText(const PublicKey& pk, const std::vector& bn_vec); /** * CipherText copy constructor @@ -49,7 +50,7 @@ class CipherText : public BaseText { /** * Get public key */ - const PublicKey* getPubKey() const; + std::shared_ptr getPubKey() const; /** * Rotate CipherText @@ -63,7 +64,7 @@ class CipherText : public BaseText { std::vector raw_mul(const std::vector& a, const std::vector& b) const; - const PublicKey* m_pubkey; ///< Public key used to encrypt big number + std::shared_ptr m_pk; ///< Public key used to encrypt big number }; } // namespace ipcl diff --git a/ipcl/include/ipcl/pri_key.hpp b/ipcl/include/ipcl/pri_key.hpp index c191874..2b457c6 100644 --- a/ipcl/include/ipcl/pri_key.hpp +++ b/ipcl/include/ipcl/pri_key.hpp @@ -20,12 +20,11 @@ class PrivateKey { /** * PrivateKey constructor - * @param[in] public_key paillier public key + * @param[in] pk paillier public key * @param[in] p p of private key in paillier scheme * @param[in] q q of private key in paillier scheme */ - PrivateKey(const PublicKey* public_key, const BigNumber& p, - const BigNumber& q); + PrivateKey(const PublicKey& pk, const BigNumber& p, const BigNumber& q); /** * Enable Chinese Remainder Theorem diff --git a/ipcl/keygen.cpp b/ipcl/keygen.cpp index 65ae297..fe064bb 100644 --- a/ipcl/keygen.cpp +++ b/ipcl/keygen.cpp @@ -105,7 +105,7 @@ KeyPair generateKeypair(int64_t n_length, bool enable_DJN) { getNormalBN(n_length, p, q, n, ref_dist); PublicKey pk(n, n_length, enable_DJN); - PrivateKey sk(&pk, p, q); + PrivateKey sk(pk, p, q); return KeyPair{pk, sk}; } diff --git a/ipcl/pri_key.cpp b/ipcl/pri_key.cpp index bf624f9..796e3b1 100644 --- a/ipcl/pri_key.cpp +++ b/ipcl/pri_key.cpp @@ -22,11 +22,11 @@ static inline BigNumber lcm(const BigNumber& p, const BigNumber& q) { return p * q / gcd; } -PrivateKey::PrivateKey(const PublicKey* public_key, const BigNumber& p, +PrivateKey::PrivateKey(const PublicKey& pk, const BigNumber& p, const BigNumber& q) - : m_n(public_key->getN()), - m_nsquare(public_key->getNSQ()), - m_g(public_key->getG()), + : m_n(pk.getN()), + m_nsquare(pk.getNSQ()), + m_g(pk.getG()), m_enable_crt(true), m_p((q < p) ? std::make_shared(q) : std::make_shared(p)), diff --git a/ipcl/pub_key.cpp b/ipcl/pub_key.cpp index 056e6d2..9f1a685 100644 --- a/ipcl/pub_key.cpp +++ b/ipcl/pub_key.cpp @@ -126,7 +126,7 @@ CipherText PublicKey::encrypt(const PlainText& pt, bool make_secure) const { } ct_bn_v = raw_encrypt(pt.getTexts(), make_secure); - return CipherText(this, ct_bn_v); + return CipherText(*this, ct_bn_v); } void PublicKey::setDJN(const BigNumber& hs, int randbit) { diff --git a/test/test_cryptography.cpp b/test/test_cryptography.cpp index 6598ab8..2b903fd 100644 --- a/test/test_cryptography.cpp +++ b/test/test_cryptography.cpp @@ -63,7 +63,7 @@ TEST(CryptoTest, ISO_IEC_18033_6_ComplianceTest) { int n_length = n.BitSize(); ipcl::PublicKey pk(n, n_length); - ipcl::PrivateKey sk(&pk, p, q); + ipcl::PrivateKey sk(pk, p, q); ipcl::KeyPair keys = {pk, sk}; @@ -171,8 +171,8 @@ TEST(CryptoTest, ISO_IEC_18033_6_ComplianceTest) { c2.num2hex(str2); EXPECT_EQ(str2, ct.getElementHex(1)); - ipcl::CipherText a(&(keys.pk), ct.getElement(0)); - ipcl::CipherText b(&(keys.pk), ct.getElement(1)); + ipcl::CipherText a(keys.pk, ct.getElement(0)); + ipcl::CipherText b(keys.pk, ct.getElement(1)); ipcl::CipherText sum = a + b; std::string str3; diff --git a/test/test_ops.cpp b/test/test_ops.cpp index 035c9d3..2c7af3d 100644 --- a/test/test_ops.cpp +++ b/test/test_ops.cpp @@ -17,12 +17,12 @@ void CtPlusCt(ipcl::CipherText& res, const ipcl::CipherText& ct1, std::vector sum_bn_v(size); for (int i = 0; i < size; i++) { - ipcl::CipherText a(&(keys.pk), ct1.getElement(i)); - ipcl::CipherText b(&(keys.pk), ct2.getElement(i)); + ipcl::CipherText a(keys.pk, ct1.getElement(i)); + ipcl::CipherText b(keys.pk, ct2.getElement(i)); ipcl::CipherText sum = a + b; sum_bn_v[i] = sum.getElement(0); } - res = ipcl::CipherText(&(keys.pk), sum_bn_v); + res = ipcl::CipherText(keys.pk, sum_bn_v); } void CtPlusCtArray(ipcl::CipherText& res, const ipcl::CipherText& ct1, @@ -36,12 +36,12 @@ void CtPlusPt(ipcl::CipherText& res, const ipcl::CipherText& ct1, std::vector sum_bn_v(size); for (int i = 0; i < size; i++) { - ipcl::CipherText a(&(keys.pk), ct1.getElement(i)); + ipcl::CipherText a(keys.pk, ct1.getElement(i)); ipcl::PlainText b(pt2.getElement(i)); ipcl::CipherText sum = a + b; sum_bn_v[i] = sum.getElement(0); } - res = ipcl::CipherText(&(keys.pk), sum_bn_v); + res = ipcl::CipherText(keys.pk, sum_bn_v); } void CtPlusPtArray(ipcl::CipherText& res, const ipcl::CipherText& ct1, @@ -55,12 +55,12 @@ void CtMultiplyPt(ipcl::CipherText& res, const ipcl::CipherText& ct1, std::vector product_bn_v(size); for (int i = 0; i < size; i++) { - ipcl::CipherText a(&(keys.pk), ct1.getElement(i)); + ipcl::CipherText a(keys.pk, ct1.getElement(i)); ipcl::PlainText b(pt2.getElement(i)); ipcl::CipherText product = a * b; product_bn_v[i] = product.getElement(0); } - res = ipcl::CipherText(&(keys.pk), product_bn_v); + res = ipcl::CipherText(keys.pk, product_bn_v); } void CtMultiplyPtArray(ipcl::CipherText& res, const ipcl::CipherText& ct1, @@ -74,15 +74,15 @@ void AddSub(ipcl::CipherText& res, const ipcl::CipherText& ct1, std::vector sum_bn_v(size); for (int i = 0; i < size; i++) { - ipcl::CipherText a(&(keys.pk), ct1.getElement(i)); - ipcl::CipherText b(&(keys.pk), ct2.getElement(i)); + ipcl::CipherText a(keys.pk, ct1.getElement(i)); + ipcl::CipherText b(keys.pk, ct2.getElement(i)); ipcl::PlainText m1(2); a = a + b * m1; ipcl::CipherText sum = a + b; sum_bn_v[i] = sum.getElement(0); } - res = ipcl::CipherText(&(keys.pk), sum_bn_v); + res = ipcl::CipherText(keys.pk, sum_bn_v); } void PtPlusCt(ipcl::CipherText& res, const ipcl::PlainText& pt2, @@ -91,12 +91,12 @@ void PtPlusCt(ipcl::CipherText& res, const ipcl::PlainText& pt2, std::vector sum_bn_v(size); for (int i = 0; i < size; i++) { - ipcl::CipherText a(&(keys.pk), ct1.getElement(i)); + ipcl::CipherText a(keys.pk, ct1.getElement(i)); ipcl::PlainText b(pt2.getElement(i)); ipcl::CipherText sum = b + a; sum_bn_v[i] = sum.getElement(0); } - res = ipcl::CipherText(&(keys.pk), sum_bn_v); + res = ipcl::CipherText(keys.pk, sum_bn_v); } void PtPlusCtArray(ipcl::CipherText& res, const ipcl::PlainText& pt2, @@ -110,12 +110,12 @@ void PtMultiplyCt(ipcl::CipherText& res, const ipcl::PlainText& pt2, std::vector product_bn_v(size); for (int i = 0; i < size; i++) { - ipcl::CipherText a(&(keys.pk), ct1.getElement(i)); + ipcl::CipherText a(keys.pk, ct1.getElement(i)); ipcl::PlainText b(pt2.getElement(i)); ipcl::CipherText product = b * a; product_bn_v[i] = product.getElement(0); } - res = ipcl::CipherText(&(keys.pk), product_bn_v); + res = ipcl::CipherText(keys.pk, product_bn_v); } void PtMultiplyCtArray(ipcl::CipherText& res, const ipcl::PlainText& pt2, From 4e1fa65222d74dcb9b5cb7fdd9a53b0a9dfa2643 Mon Sep 17 00:00:00 2001 From: skmono Date: Thu, 24 Nov 2022 07:27:28 -0800 Subject: [PATCH 06/12] Updated cmake changes to branch --- CMakeLists.txt | 7 ++++++- cmake/ippcrypto.cmake | 15 +++++++-------- ipcl/include/ipcl/keygen.hpp | 37 ------------------------------------ 3 files changed, 13 insertions(+), 46 deletions(-) delete mode 100644 ipcl/include/ipcl/keygen.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 8303faa..15090aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,6 +43,11 @@ if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) set(CMAKE_INSTALL_PREFIX "/opt/intel/ipcl") endif() +if(NOT CMAKE_PREFIX_PATH) + set(CMAKE_PREFIX_PATH $ENV{HOME}/intel /opt/intel) +endif() + + set(CMAKE_C_FLAGS "-O2 -Wno-error=deprecated-declarations -Wno-error=deprecated-copy") set(CMAKE_CXX_FLAGS "-O2 -fpermissive -Wno-error=deprecated-declarations -Wno-error=deprecated-copy") set(CMAKE_INSTALL_RPATH "$ORIGIN;$ORIGIN/${CMAKE_INSTALL_LIBDIR};$ORIGIN/ippcrypto") @@ -70,7 +75,6 @@ option(IPCL_DOCS "Enable document building" OFF) option(IPCL_SHARED "Build shared library" ON) option(IPCL_DETECT_CPU_RUNTIME "Detect CPU supported instructions during runtime" OFF) option(IPCL_INTERNAL_PYTHON_BUILD "Additional steps for IPCL_Python build" OFF) -option(IPCL_IPPCRYPTO_PATH "Use pre-installed IPP-Crypto library" OFF) # Used only for ipcl_python IPCL_INTERNAL_PYTHON_BUILD - additional check if invalid parameters if(IPCL_INTERNAL_PYTHON_BUILD) @@ -148,6 +152,7 @@ message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}") message(STATUS "CMAKE_C_COMPILER: ${CMAKE_C_COMPILER}") message(STATUS "CMAKE_CXX_COMPILER: ${CMAKE_CXX_COMPILER}") message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}") +message(STATUS "CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}") message(STATUS "IPCL_TEST: ${IPCL_TEST}") message(STATUS "IPCL_BENCHMARK: ${IPCL_BENCHMARK}") message(STATUS "IPCL_ENABLE_OMP: ${IPCL_ENABLE_OMP}") diff --git a/cmake/ippcrypto.cmake b/cmake/ippcrypto.cmake index 0952b4c..e0dc364 100644 --- a/cmake/ippcrypto.cmake +++ b/cmake/ippcrypto.cmake @@ -4,23 +4,23 @@ include(ExternalProject) message(STATUS "Configuring ipp-crypto") -if(IPCL_IPPCRYPTO_PATH) +set(IPPCRYPTO_VERSION 11.4) +set(IPPCRYPTO_GIT_LABEL ippcp_2021.6) #ippcp version 11.4 + +if(CMAKE_PREFIX_PATH) if(IPCL_SHARED) set(IPPCP_SHARED ON) else() set(IPPCP_SHARED OFF) endif() - - # ippcp version 11.4 - inline with ippcp_2021.6 - find_package(ippcp 11.4 HINTS ${IPCL_IPPCRYPTO_PATH}/lib/cmake/ippcp) + find_package(ippcp ${IPPCRYPTO_VERSION}) endif() if(ippcp_FOUND) - message(STATUS "IPP-Crypto Found - using pre-installed IPP-Crypto library") + message(STATUS "IPP-Crypto ${IPPCRYPTO_VERSION} found at ${ippcp_DIR}") get_target_property(IPPCRYPTO_INC_DIR IPPCP::ippcp INTERFACE_INCLUDE_DIRECTORIES) get_target_property(IPPCRYPTO_IMPORTED_LOCATION IPPCP::ippcp IMPORTED_LOCATION) get_filename_component(IPPCRYPTO_LIB_DIR ${IPPCRYPTO_IMPORTED_LOCATION} DIRECTORY) - install( DIRECTORY ${IPPCRYPTO_LIB_DIR}/ DESTINATION "${IPCL_INSTALL_LIBDIR}/ippcrypto" @@ -28,13 +28,12 @@ if(ippcp_FOUND) ) else() - message(STATUS "IPP-Crypto NOT Found - building from source") + message(STATUS "IPP-Crypto NOT found - building from source") set(IPPCRYPTO_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/ext_ipp-crypto) set(IPPCRYPTO_DESTDIR ${IPPCRYPTO_PREFIX}/ippcrypto_install) set(IPPCRYPTO_DEST_INCLUDE_DIR include/ippcrypto) set(IPPCRYPTO_GIT_REPO_URL https://github.com/intel/ipp-crypto.git) - set(IPPCRYPTO_GIT_LABEL ippcp_2021.6) #ippcp version 11.4 set(IPPCRYPTO_SRC_DIR ${IPPCRYPTO_PREFIX}/src/ext_ipp-crypto/) set(IPPCRYPTO_CXX_FLAGS "${IPCL_FORWARD_CMAKE_ARGS} -DNONPIC_LIB:BOOL=off -DMERGED_BLD:BOOL=on") diff --git a/ipcl/include/ipcl/keygen.hpp b/ipcl/include/ipcl/keygen.hpp deleted file mode 100644 index 50b77cb..0000000 --- a/ipcl/include/ipcl/keygen.hpp +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright (C) 2021 Intel Corporation -// SPDX-License-Identifier: Apache-2.0 - -#ifndef IPCL_INCLUDE_IPCL_KEYGEN_HPP_ -#define IPCL_INCLUDE_IPCL_KEYGEN_HPP_ - -#include "ipcl/pri_key.hpp" - -namespace ipcl { - -/** - * Paillier key structure contains a public key and private key - * pk: paillier public key - * sk: paillier private key - */ -struct KeyPair { - PublicKey pk; - PrivateKey sk; -}; - -/** - * Generate prime number - * @param[in] maxBitSize Maximum bit length of to be generated prime number - * @return The function return a prime big number - */ -BigNumber getPrimeBN(int maxBitSize); - -/** - * Generate a public/private key pair - * @param[in] n_length Bit length of key size - * @param[in] enable_DJN Enable DJN (default=true) - * @return The function return the public and private key pair - */ -KeyPair generateKeypair(int64_t n_length, bool enable_DJN = true); - -} // namespace ipcl -#endif // IPCL_INCLUDE_IPCL_KEYGEN_HPP_ From 25f62ba77f4b613038acf654e486ba7e6939ecba Mon Sep 17 00:00:00 2001 From: skmono Date: Sun, 27 Nov 2022 17:58:23 -0800 Subject: [PATCH 07/12] Minor updates: - Reverted keyPair names to pub_key, priv_key (same as previous implementation) to avoid confusion - can change after v2.1 with deprecation warning - Fully functional BigNumber and PublicKey serialization - PrivateKey and CipherText serialization WIP --- example/example_add_mul.cpp | 12 +-- example/example_encrypt_decrypt.cpp | 6 +- example/example_hybridmode.cpp | 14 +-- ipcl/include/ipcl/bignum.h | 2 +- ipcl/include/ipcl/ipcl.hpp | 4 +- ipcl/include/ipcl/utils/serialize.hpp | 19 ++-- ipcl/pub_key.cpp | 1 + test/test_cryptography.cpp | 20 ++-- test/test_ops.cpp | 132 +++++++++++++------------- 9 files changed, 108 insertions(+), 102 deletions(-) diff --git a/example/example_add_mul.cpp b/example/example_add_mul.cpp index 32a680a..1b345d8 100644 --- a/example/example_add_mul.cpp +++ b/example/example_add_mul.cpp @@ -21,7 +21,7 @@ int main() { const uint32_t num_total = 20; - ipcl::KeyPair keys = ipcl::generateKeypair(2048, true); + ipcl::KeyPair key = ipcl::generateKeypair(2048, true); std::vector x(num_total); std::vector y(num_total); @@ -41,13 +41,13 @@ int main() { ipcl::setHybridMode(ipcl::HybridMode::OPTIMAL); - ipcl::CipherText ct_x = keys.pk.encrypt(pt_x); - ipcl::CipherText ct_y = keys.sk.encrypt(pt_y); + ipcl::CipherText ct_x = key.pub_key.encrypt(pt_x); + ipcl::CipherText ct_y = key.priv_key.encrypt(pt_y); // Perform enc(x) + enc(y) std::cout << "--- IPCL CipherText + CipherText ---" << std::endl; ipcl::CipherText ct_add_ctx_cty = ct_x + ct_y; - ipcl::PlainText dt_add_ctx_cty = keys.sk.decrypt(ct_add_ctx_cty); + ipcl::PlainText dt_add_ctx_cty = key.priv_key.decrypt(ct_add_ctx_cty); // verify result bool verify = true; @@ -65,7 +65,7 @@ int main() { // Perform enc(x) + y std::cout << "--- IPCL CipherText + PlainText ---" << std::endl; ipcl::CipherText ct_add_ctx_pty = ct_x + pt_y; - ipcl::PlainText dt_add_ctx_pty = keys.sk.decrypt(ct_add_ctx_pty); + ipcl::PlainText dt_add_ctx_pty = key.priv_key.decrypt(ct_add_ctx_pty); // verify result verify = true; @@ -83,7 +83,7 @@ int main() { // Perform enc(x) * y std::cout << "--- IPCL CipherText * PlainText ---" << std::endl; ipcl::CipherText ct_mul_ctx_pty = ct_x * pt_y; - ipcl::PlainText dt_mul_ctx_pty = keys.sk.decrypt(ct_mul_ctx_pty); + ipcl::PlainText dt_mul_ctx_pty = key.priv_key.decrypt(ct_mul_ctx_pty); // verify result verify = true; diff --git a/example/example_encrypt_decrypt.cpp b/example/example_encrypt_decrypt.cpp index ab47259..e959a01 100644 --- a/example/example_encrypt_decrypt.cpp +++ b/example/example_encrypt_decrypt.cpp @@ -21,7 +21,7 @@ int main() { const uint32_t num_total = 20; - ipcl::KeyPair keys = ipcl::generateKeypair(2048, true); + ipcl::KeyPair key = ipcl::generateKeypair(2048, true); std::vector exp_value(num_total); @@ -37,8 +37,8 @@ int main() { ipcl::setHybridMode(ipcl::HybridMode::OPTIMAL); - ipcl::CipherText ct = keys.pk.encrypt(pt); - ipcl::PlainText dt = keys.sk.decrypt(ct); + ipcl::CipherText ct = key.pub_key.encrypt(pt); + ipcl::PlainText dt = key.priv_key.decrypt(ct); ipcl::setHybridOff(); diff --git a/example/example_hybridmode.cpp b/example/example_hybridmode.cpp index c0692db..4ac1f91 100644 --- a/example/example_hybridmode.cpp +++ b/example/example_hybridmode.cpp @@ -30,7 +30,7 @@ int main() { const uint32_t num_total = 64; - ipcl::KeyPair keys = ipcl::generateKeypair(2048, true); + ipcl::KeyPair key = ipcl::generateKeypair(2048, true); std::vector exp_value(num_total); @@ -56,12 +56,12 @@ int main() { // Encrypt/Decrypt - IPP-Crypto only mode ipcl::setHybridMode(ipcl::HybridMode::IPP); tStart(t); - ipcl::CipherText ct = keys.pk.encrypt(pt); + ipcl::CipherText ct = key.pub_key.encrypt(pt); elapsed = tEnd(t); std::cout << " Encrypt - HybridMode::IPP = " << elapsed << "ms" << std::endl; tStart(t); - ipcl::PlainText dt = keys.sk.decrypt(ct); + ipcl::PlainText dt = key.priv_key.decrypt(ct); elapsed = tEnd(t); std::cout << " Decrypt - HybridMode::IPP = " << elapsed << "ms" << std::endl @@ -70,12 +70,12 @@ int main() { // Encrypt/Decrypt - QAT only mode ipcl::setHybridMode(ipcl::HybridMode::QAT); tStart(t); - ct = keys.pk.encrypt(pt); + ct = key.pub_key.encrypt(pt); elapsed = tEnd(t); std::cout << " Encrypt - HybridMode::QAT = " << elapsed << "ms" << std::endl; tStart(t); - dt = keys.sk.decrypt(ct); + dt = key.priv_key.decrypt(ct); elapsed = tEnd(t); std::cout << " Decrypt - HybridMode::QAT = " << elapsed << "ms" << std::endl @@ -84,12 +84,12 @@ int main() { // Encrypt/Decrypt - OPTIMAL mode ipcl::setHybridMode(ipcl::HybridMode::OPTIMAL); tStart(t); - ct = keys.pk.encrypt(pt); + ct = key.pub_key.encrypt(pt); elapsed = tEnd(t); std::cout << " Encrypt - HybridMode::OPTIMAL = " << elapsed << "ms" << std::endl; tStart(t); - dt = keys.sk.decrypt(ct); + dt = key.priv_key.decrypt(ct); elapsed = tEnd(t); std::cout << " Decrypt - HybridMode::OPTIMAL = " << elapsed << "ms" << std::endl diff --git a/ipcl/include/ipcl/bignum.h b/ipcl/include/ipcl/bignum.h index 2a069cd..27760a0 100644 --- a/ipcl/include/ipcl/bignum.h +++ b/ipcl/include/ipcl/bignum.h @@ -25,7 +25,7 @@ #include "ipcl/utils/serialize.hpp" #include "ippcp.h" -class BigNumber : public ipcl::serialize::serializerBase { +class BigNumber : public ipcl::serializer::serializerBase { public: BigNumber(Ipp32u value = 0); BigNumber(Ipp32s value); diff --git a/ipcl/include/ipcl/ipcl.hpp b/ipcl/include/ipcl/ipcl.hpp index 2b1dea3..4aca630 100644 --- a/ipcl/include/ipcl/ipcl.hpp +++ b/ipcl/include/ipcl/ipcl.hpp @@ -17,8 +17,8 @@ namespace ipcl { * sk: paillier private key */ struct KeyPair { - PublicKey pk; - PrivateKey sk; + PublicKey pub_key; + PrivateKey priv_key; }; /** diff --git a/ipcl/include/ipcl/utils/serialize.hpp b/ipcl/include/ipcl/utils/serialize.hpp index 92a3e2c..59863d7 100644 --- a/ipcl/include/ipcl/utils/serialize.hpp +++ b/ipcl/include/ipcl/utils/serialize.hpp @@ -16,15 +16,20 @@ namespace ipcl { -namespace serialize { +class PublicKey; +class PrivateKey; +class CipherText; + +namespace serializer { + template -void serialize(const T& obj, std::ostream& ss) { +void serialize(std::ostream& ss, const T& obj) { cereal::PortableBinaryOutputArchive archive(ss); archive(obj); } template -void deserialize(T& obj, std::istream& ss) { +void deserialize(std::istream& ss, T& obj) { cereal::PortableBinaryInputArchive archive(ss); archive(obj); } @@ -33,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()) { - serialize::serialize(obj, ofs); + serializer::serialize(obj, ofs); ofs.close(); return true; } @@ -41,10 +46,10 @@ bool serializeToFile(const std::string& fn, const T& obj) { } template -bool deserializeFromFile(const std::string& fn, const T& obj) { +bool deserializeFromFile(const std::string& fn, T& obj) { std::ifstream ifs(fn, std::ios::in | std::ios::binary); if (ifs.is_open()) { - serialize::deserialize(obj, ifs); + serializer::deserialize(obj, ifs); ifs.close(); return true; } @@ -57,7 +62,7 @@ class serializerBase { virtual std::string serializedName() const = 0; }; -}; // namespace serialize +}; // namespace serializer } // namespace ipcl diff --git a/ipcl/pub_key.cpp b/ipcl/pub_key.cpp index 9f1a685..d5b2c88 100644 --- a/ipcl/pub_key.cpp +++ b/ipcl/pub_key.cpp @@ -158,6 +158,7 @@ void PublicKey::create(const BigNumber& n, int bits, bool enableDJN_) { void PublicKey::create(const BigNumber& n, int bits, const BigNumber& hs, int randbits) { create(n, bits, false); // set DJN to false and manually set + m_enable_DJN = true; m_hs = hs; m_randbits = randbits; } diff --git a/test/test_cryptography.cpp b/test/test_cryptography.cpp index 2b903fd..db233e1 100644 --- a/test/test_cryptography.cpp +++ b/test/test_cryptography.cpp @@ -15,7 +15,7 @@ TEST(CryptoTest, CryptoTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::KeyPair keys = ipcl::generateKeypair(2048, true); + ipcl::KeyPair key = ipcl::generateKeypair(2048, true); std::vector exp_value(num_values); ipcl::PlainText pt; @@ -34,8 +34,8 @@ TEST(CryptoTest, CryptoTest) { ipcl::setHybridRatio(qat_ratio); - ct = keys.pk.encrypt(pt); - dt = keys.sk.decrypt(ct); + ct = key.pub_key.encrypt(pt); + dt = key.priv_key.decrypt(ct); for (int i = 0; i < num_values; i++) { std::vector v = dt.getElementVec(i); @@ -65,7 +65,7 @@ TEST(CryptoTest, ISO_IEC_18033_6_ComplianceTest) { ipcl::PublicKey pk(n, n_length); ipcl::PrivateKey sk(pk, p, q); - ipcl::KeyPair keys = {pk, sk}; + ipcl::KeyPair key = {pk, sk}; std::vector pt_bn_v(num_values); std::vector ir_bn_v(num_values); @@ -154,13 +154,13 @@ TEST(CryptoTest, ISO_IEC_18033_6_ComplianceTest) { ipcl::setHybridOff(); - keys.pk.setRandom(ir_bn_v); + key.pub_key.setRandom(ir_bn_v); pt = ipcl::PlainText(pt_bn_v); - ct = keys.pk.encrypt(pt); + ct = key.pub_key.encrypt(pt); ipcl::PlainText dt; - dt = keys.sk.decrypt(ct); + dt = key.priv_key.decrypt(ct); for (int i = 0; i < num_values; i++) { EXPECT_EQ(dt.getElement(i), pt_bn_v[i]); } @@ -171,8 +171,8 @@ TEST(CryptoTest, ISO_IEC_18033_6_ComplianceTest) { c2.num2hex(str2); EXPECT_EQ(str2, ct.getElementHex(1)); - ipcl::CipherText a(keys.pk, ct.getElement(0)); - ipcl::CipherText b(keys.pk, ct.getElement(1)); + ipcl::CipherText a(key.pub_key, ct.getElement(0)); + ipcl::CipherText b(key.pub_key, ct.getElement(1)); ipcl::CipherText sum = a + b; std::string str3; @@ -182,7 +182,7 @@ TEST(CryptoTest, ISO_IEC_18033_6_ComplianceTest) { std::string str4; ipcl::PlainText dt_sum; - dt_sum = keys.sk.decrypt(sum); + dt_sum = key.priv_key.decrypt(sum); m1m2.num2hex(str4); EXPECT_EQ(str4, dt_sum.getElementHex(0)); } diff --git a/test/test_ops.cpp b/test/test_ops.cpp index 2c7af3d..2484c59 100644 --- a/test/test_ops.cpp +++ b/test/test_ops.cpp @@ -12,17 +12,17 @@ constexpr int SELF_DEF_NUM_VALUES = 14; constexpr float SELF_DEF_HYBRID_QAT_RATIO = 0.5; void CtPlusCt(ipcl::CipherText& res, const ipcl::CipherText& ct1, - const ipcl::CipherText& ct2, const ipcl::KeyPair keys) { + const ipcl::CipherText& ct2, const ipcl::KeyPair key) { int size = ct1.getSize(); std::vector sum_bn_v(size); for (int i = 0; i < size; i++) { - ipcl::CipherText a(keys.pk, ct1.getElement(i)); - ipcl::CipherText b(keys.pk, ct2.getElement(i)); + ipcl::CipherText a(key.pub_key, ct1.getElement(i)); + ipcl::CipherText b(key.pub_key, ct2.getElement(i)); ipcl::CipherText sum = a + b; sum_bn_v[i] = sum.getElement(0); } - res = ipcl::CipherText(keys.pk, sum_bn_v); + res = ipcl::CipherText(key.pub_key, sum_bn_v); } void CtPlusCtArray(ipcl::CipherText& res, const ipcl::CipherText& ct1, @@ -31,17 +31,17 @@ void CtPlusCtArray(ipcl::CipherText& res, const ipcl::CipherText& ct1, } void CtPlusPt(ipcl::CipherText& res, const ipcl::CipherText& ct1, - const ipcl::PlainText& pt2, const ipcl::KeyPair keys) { + const ipcl::PlainText& pt2, const ipcl::KeyPair key) { int size = ct1.getSize(); std::vector sum_bn_v(size); for (int i = 0; i < size; i++) { - ipcl::CipherText a(keys.pk, ct1.getElement(i)); + ipcl::CipherText a(key.pub_key, ct1.getElement(i)); ipcl::PlainText b(pt2.getElement(i)); ipcl::CipherText sum = a + b; sum_bn_v[i] = sum.getElement(0); } - res = ipcl::CipherText(keys.pk, sum_bn_v); + res = ipcl::CipherText(key.pub_key, sum_bn_v); } void CtPlusPtArray(ipcl::CipherText& res, const ipcl::CipherText& ct1, @@ -50,17 +50,17 @@ void CtPlusPtArray(ipcl::CipherText& res, const ipcl::CipherText& ct1, } void CtMultiplyPt(ipcl::CipherText& res, const ipcl::CipherText& ct1, - const ipcl::PlainText& pt2, const ipcl::KeyPair keys) { + const ipcl::PlainText& pt2, const ipcl::KeyPair key) { int size = ct1.getSize(); std::vector product_bn_v(size); for (int i = 0; i < size; i++) { - ipcl::CipherText a(keys.pk, ct1.getElement(i)); + ipcl::CipherText a(key.pub_key, ct1.getElement(i)); ipcl::PlainText b(pt2.getElement(i)); ipcl::CipherText product = a * b; product_bn_v[i] = product.getElement(0); } - res = ipcl::CipherText(keys.pk, product_bn_v); + res = ipcl::CipherText(key.pub_key, product_bn_v); } void CtMultiplyPtArray(ipcl::CipherText& res, const ipcl::CipherText& ct1, @@ -69,34 +69,34 @@ void CtMultiplyPtArray(ipcl::CipherText& res, const ipcl::CipherText& ct1, } void AddSub(ipcl::CipherText& res, const ipcl::CipherText& ct1, - const ipcl::CipherText& ct2, const ipcl::KeyPair keys) { + const ipcl::CipherText& ct2, const ipcl::KeyPair key) { int size = ct1.getSize(); std::vector sum_bn_v(size); for (int i = 0; i < size; i++) { - ipcl::CipherText a(keys.pk, ct1.getElement(i)); - ipcl::CipherText b(keys.pk, ct2.getElement(i)); + ipcl::CipherText a(key.pub_key, ct1.getElement(i)); + ipcl::CipherText b(key.pub_key, ct2.getElement(i)); ipcl::PlainText m1(2); a = a + b * m1; ipcl::CipherText sum = a + b; sum_bn_v[i] = sum.getElement(0); } - res = ipcl::CipherText(keys.pk, sum_bn_v); + res = ipcl::CipherText(key.pub_key, sum_bn_v); } void PtPlusCt(ipcl::CipherText& res, const ipcl::PlainText& pt2, - const ipcl::CipherText& ct1, const ipcl::KeyPair keys) { + const ipcl::CipherText& ct1, const ipcl::KeyPair key) { int size = ct1.getSize(); std::vector sum_bn_v(size); for (int i = 0; i < size; i++) { - ipcl::CipherText a(keys.pk, ct1.getElement(i)); + ipcl::CipherText a(key.pub_key, ct1.getElement(i)); ipcl::PlainText b(pt2.getElement(i)); ipcl::CipherText sum = b + a; sum_bn_v[i] = sum.getElement(0); } - res = ipcl::CipherText(keys.pk, sum_bn_v); + res = ipcl::CipherText(key.pub_key, sum_bn_v); } void PtPlusCtArray(ipcl::CipherText& res, const ipcl::PlainText& pt2, @@ -105,17 +105,17 @@ void PtPlusCtArray(ipcl::CipherText& res, const ipcl::PlainText& pt2, } void PtMultiplyCt(ipcl::CipherText& res, const ipcl::PlainText& pt2, - const ipcl::CipherText& ct1, const ipcl::KeyPair keys) { + const ipcl::CipherText& ct1, const ipcl::KeyPair key) { int size = ct1.getSize(); std::vector product_bn_v(size); for (int i = 0; i < size; i++) { - ipcl::CipherText a(keys.pk, ct1.getElement(i)); + ipcl::CipherText a(key.pub_key, ct1.getElement(i)); ipcl::PlainText b(pt2.getElement(i)); ipcl::CipherText product = b * a; product_bn_v[i] = product.getElement(0); } - res = ipcl::CipherText(keys.pk, product_bn_v); + res = ipcl::CipherText(key.pub_key, product_bn_v); } void PtMultiplyCtArray(ipcl::CipherText& res, const ipcl::PlainText& pt2, @@ -127,7 +127,7 @@ TEST(OperationTest, CtPlusCtTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::KeyPair keys = ipcl::generateKeypair(2048); + ipcl::KeyPair key = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_sum; @@ -146,12 +146,12 @@ TEST(OperationTest, CtPlusCtTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = keys.pk.encrypt(pt1); - ct2 = keys.pk.encrypt(pt2); + ct1 = key.pub_key.encrypt(pt1); + ct2 = key.pub_key.encrypt(pt2); - CtPlusCt(ct_sum, ct1, ct2, keys); + CtPlusCt(ct_sum, ct1, ct2, key); - dt_sum = keys.sk.decrypt(ct_sum); + dt_sum = key.priv_key.decrypt(ct_sum); for (int i = 0; i < num_values; i++) { std::vector v = dt_sum.getElementVec(i); @@ -168,7 +168,7 @@ TEST(OperationTest, CtPlusCtArrayTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::KeyPair keys = ipcl::generateKeypair(2048); + ipcl::KeyPair key = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_sum; @@ -187,12 +187,12 @@ TEST(OperationTest, CtPlusCtArrayTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = keys.pk.encrypt(pt1); - ct2 = keys.pk.encrypt(pt2); + ct1 = key.pub_key.encrypt(pt1); + ct2 = key.pub_key.encrypt(pt2); CtPlusCtArray(ct_sum, ct1, ct2); - dt_sum = keys.sk.decrypt(ct_sum); + dt_sum = key.priv_key.decrypt(ct_sum); for (int i = 0; i < num_values; i++) { std::vector v = dt_sum.getElementVec(i); @@ -209,7 +209,7 @@ TEST(OperationTest, CtPlusPtTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::KeyPair keys = ipcl::generateKeypair(2048); + ipcl::KeyPair key = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_sum; @@ -228,11 +228,11 @@ TEST(OperationTest, CtPlusPtTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = keys.pk.encrypt(pt1); + ct1 = key.pub_key.encrypt(pt1); - CtPlusPt(ct_sum, ct1, pt2, keys); + CtPlusPt(ct_sum, ct1, pt2, key); - dt_sum = keys.sk.decrypt(ct_sum); + dt_sum = key.priv_key.decrypt(ct_sum); for (int i = 0; i < num_values; i++) { std::vector v = dt_sum.getElementVec(i); @@ -249,7 +249,7 @@ TEST(OperationTest, CtPlusPtArrayTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::KeyPair keys = ipcl::generateKeypair(2048); + ipcl::KeyPair key = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_sum; @@ -268,11 +268,11 @@ TEST(OperationTest, CtPlusPtArrayTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = keys.pk.encrypt(pt1); + ct1 = key.pub_key.encrypt(pt1); CtPlusPtArray(ct_sum, ct1, pt2); - dt_sum = keys.sk.decrypt(ct_sum); + dt_sum = key.priv_key.decrypt(ct_sum); for (int i = 0; i < num_values; i++) { std::vector v = dt_sum.getElementVec(i); @@ -289,7 +289,7 @@ TEST(OperationTest, CtMultiplyPtTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::KeyPair keys = ipcl::generateKeypair(2048); + ipcl::KeyPair key = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_product; @@ -308,11 +308,11 @@ TEST(OperationTest, CtMultiplyPtTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = keys.pk.encrypt(pt1); + ct1 = key.pub_key.encrypt(pt1); - CtMultiplyPt(ct_product, ct1, pt2, keys); + CtMultiplyPt(ct_product, ct1, pt2, key); - dt_product = keys.sk.decrypt(ct_product); + dt_product = key.priv_key.decrypt(ct_product); for (int i = 0; i < num_values; i++) { std::vector v = dt_product.getElementVec(i); @@ -329,7 +329,7 @@ TEST(OperationTest, CtMultiplyZeroPtTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::KeyPair keys = ipcl::generateKeypair(2048); + ipcl::KeyPair key = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_product; @@ -349,11 +349,11 @@ TEST(OperationTest, CtMultiplyZeroPtTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = keys.pk.encrypt(pt1); + ct1 = key.pub_key.encrypt(pt1); - CtMultiplyPt(ct_product, ct1, pt2, keys); + CtMultiplyPt(ct_product, ct1, pt2, key); - dt_product = keys.sk.decrypt(ct_product); + dt_product = key.priv_key.decrypt(ct_product); for (int i = 0; i < num_values; i++) { std::vector v = dt_product.getElementVec(i); @@ -370,7 +370,7 @@ TEST(OperationTest, CtMultiplyPtArrayTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::KeyPair keys = ipcl::generateKeypair(2048); + ipcl::KeyPair key = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_product; @@ -389,11 +389,11 @@ TEST(OperationTest, CtMultiplyPtArrayTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = keys.pk.encrypt(pt1); + ct1 = key.pub_key.encrypt(pt1); CtMultiplyPtArray(ct_product, ct1, pt2); - dt_product = keys.sk.decrypt(ct_product); + dt_product = key.priv_key.decrypt(ct_product); for (int i = 0; i < num_values; i++) { std::vector v = dt_product.getElementVec(i); @@ -410,7 +410,7 @@ TEST(OperationTest, AddSubTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::KeyPair keys = ipcl::generateKeypair(2048); + ipcl::KeyPair key = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_sum; @@ -429,12 +429,12 @@ TEST(OperationTest, AddSubTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = keys.pk.encrypt(pt1); - ct2 = keys.pk.encrypt(pt2); + ct1 = key.pub_key.encrypt(pt1); + ct2 = key.pub_key.encrypt(pt2); - AddSub(ct_sum, ct1, ct2, keys); + AddSub(ct_sum, ct1, ct2, key); - dt_sum = keys.sk.decrypt(ct_sum); + dt_sum = key.priv_key.decrypt(ct_sum); for (int i = 0; i < num_values; i++) { std::vector v = dt_sum.getElementVec(i); @@ -451,7 +451,7 @@ TEST(OperationTest, PtPlusCtTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::KeyPair keys = ipcl::generateKeypair(2048); + ipcl::KeyPair key = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_sum; @@ -470,11 +470,11 @@ TEST(OperationTest, PtPlusCtTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = keys.pk.encrypt(pt1); + ct1 = key.pub_key.encrypt(pt1); - PtPlusCt(ct_sum, pt2, ct1, keys); + PtPlusCt(ct_sum, pt2, ct1, key); - dt_sum = keys.sk.decrypt(ct_sum); + dt_sum = key.priv_key.decrypt(ct_sum); for (int i = 0; i < num_values; i++) { std::vector v = dt_sum.getElementVec(i); @@ -491,7 +491,7 @@ TEST(OperationTest, PtPlusCtArrayTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::KeyPair keys = ipcl::generateKeypair(2048); + ipcl::KeyPair key = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_sum; @@ -510,11 +510,11 @@ TEST(OperationTest, PtPlusCtArrayTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = keys.pk.encrypt(pt1); + ct1 = key.pub_key.encrypt(pt1); PtPlusCtArray(ct_sum, pt2, ct1); - dt_sum = keys.sk.decrypt(ct_sum); + dt_sum = key.priv_key.decrypt(ct_sum); for (int i = 0; i < num_values; i++) { std::vector v = dt_sum.getElementVec(i); @@ -531,7 +531,7 @@ TEST(OperationTest, PtMultiplyCtTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::KeyPair keys = ipcl::generateKeypair(2048); + ipcl::KeyPair key = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_product; @@ -550,11 +550,11 @@ TEST(OperationTest, PtMultiplyCtTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = keys.pk.encrypt(pt1); + ct1 = key.pub_key.encrypt(pt1); - PtMultiplyCt(ct_product, pt2, ct1, keys); + PtMultiplyCt(ct_product, pt2, ct1, key); - dt_product = keys.sk.decrypt(ct_product); + dt_product = key.priv_key.decrypt(ct_product); for (int i = 0; i < num_values; i++) { std::vector v = dt_product.getElementVec(i); @@ -571,7 +571,7 @@ TEST(OperationTest, PtMultiplyCtArrayTest) { const uint32_t num_values = SELF_DEF_NUM_VALUES; const float qat_ratio = SELF_DEF_HYBRID_QAT_RATIO; - ipcl::KeyPair keys = ipcl::generateKeypair(2048); + ipcl::KeyPair key = ipcl::generateKeypair(2048); std::vector exp_value1(num_values), exp_value2(num_values); ipcl::PlainText pt1, pt2, dt_product; @@ -590,11 +590,11 @@ TEST(OperationTest, PtMultiplyCtArrayTest) { ipcl::setHybridRatio(qat_ratio); - ct1 = keys.pk.encrypt(pt1); + ct1 = key.pub_key.encrypt(pt1); PtMultiplyCtArray(ct_product, pt2, ct1); - dt_product = keys.sk.decrypt(ct_product); + dt_product = key.priv_key.decrypt(ct_product); for (int i = 0; i < num_values; i++) { std::vector v = dt_product.getElementVec(i); From 69d888d06e875b3af31f048564ee60e8827f0358 Mon Sep 17 00:00:00 2001 From: skmono Date: Sun, 27 Nov 2022 21:34:31 -0800 Subject: [PATCH 08/12] Add GNU version check for "deprecated-copy" flag - address #40 --- CMakeLists.txt | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 15090aa..b9b9030 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,13 +47,6 @@ if(NOT CMAKE_PREFIX_PATH) set(CMAKE_PREFIX_PATH $ENV{HOME}/intel /opt/intel) endif() - -set(CMAKE_C_FLAGS "-O2 -Wno-error=deprecated-declarations -Wno-error=deprecated-copy") -set(CMAKE_CXX_FLAGS "-O2 -fpermissive -Wno-error=deprecated-declarations -Wno-error=deprecated-copy") -set(CMAKE_INSTALL_RPATH "$ORIGIN;$ORIGIN/${CMAKE_INSTALL_LIBDIR};$ORIGIN/ippcrypto") - -set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_INSTALL_LIBDIR}) - # Compiler version check - icx/icpx-2021.3.0 is supported if(CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM") if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 2021.3.0) @@ -64,6 +57,20 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM") endif() endif() +set(CMAKE_C_FLAGS "-O2 -Wno-error=deprecated-declarations") +set(CMAKE_CXX_FLAGS "-O2 -fpermissive -Wno-error=deprecated-declarations") + +# Add -Wno-error=deprecated-copy if GNU>=9.1 +if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 9.1.0) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=deprecated-copy") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=deprecated-copy") + endif() +endif() + +set(CMAKE_INSTALL_RPATH "$ORIGIN;$ORIGIN/${CMAKE_INSTALL_LIBDIR};$ORIGIN/ippcrypto") +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_INSTALL_LIBDIR}) + #--------------------------------------------------- option(IPCL_TEST "Enable testing" ON) option(IPCL_BENCHMARK "Enable benchmark" ON) From cce69c21c3b9c8e5a133a12632f1957b67cf9526 Mon Sep 17 00:00:00 2001 From: skmono Date: Sun, 27 Nov 2022 23:16:12 -0800 Subject: [PATCH 09/12] Add private key consutrctor ipcl::PrivateKey(n,p,q) --- ipcl/include/ipcl/pri_key.hpp | 8 ++++++++ ipcl/pri_key.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/ipcl/include/ipcl/pri_key.hpp b/ipcl/include/ipcl/pri_key.hpp index 2b457c6..6e79e0c 100644 --- a/ipcl/include/ipcl/pri_key.hpp +++ b/ipcl/include/ipcl/pri_key.hpp @@ -26,6 +26,14 @@ class PrivateKey { */ PrivateKey(const PublicKey& pk, const BigNumber& p, const BigNumber& q); + /** + * PrivateKey constructor + * @param[in] pk paillier public key + * @param[in] p p of private key in paillier scheme + * @param[in] q q of private key in paillier scheme + */ + PrivateKey(const BigNumber& n, const BigNumber& p, const BigNumber& q); + /** * Enable Chinese Remainder Theorem * @param[in] crt Apply Chinese Remainder Theorem diff --git a/ipcl/pri_key.cpp b/ipcl/pri_key.cpp index 796e3b1..923686d 100644 --- a/ipcl/pri_key.cpp +++ b/ipcl/pri_key.cpp @@ -48,6 +48,32 @@ PrivateKey::PrivateKey(const PublicKey& pk, const BigNumber& p, m_isInitialized = true; } +PrivateKey::PrivateKey(const BigNumber& n, const BigNumber& p, + const BigNumber& q) + : m_n(std::make_shared(n)), + m_nsquare(std::make_shared((*m_n) * (*m_n))), + m_g(std::make_shared((*m_n) + 1)), + m_enable_crt(true), + m_p((q < p) ? std::make_shared(q) + : std::make_shared(p)), + m_q((q < p) ? std::make_shared(p) + : std::make_shared(q)), + m_pminusone(*m_p - 1), + m_qminusone(*m_q - 1), + m_psquare((*m_p) * (*m_p)), + m_qsquare((*m_q) * (*m_q)), + m_pinverse((*m_q).InverseMul(*m_p)), + m_hp(computeHfun(*m_p, m_psquare)), + m_hq(computeHfun(*m_q, m_qsquare)), + m_lambda(lcm(m_pminusone, m_qminusone)), + m_x((*m_n).InverseMul((modExp(*m_g, m_lambda, *m_nsquare) - 1) / + (*m_n))) { + ERROR_CHECK((*m_p) * (*m_q) == *m_n, + "PrivateKey ctor: Public key does not match p * q."); + ERROR_CHECK(*m_p != *m_q, "PrivateKey ctor: p and q are same"); + m_isInitialized = true; +} + PlainText PrivateKey::decrypt(const CipherText& ct) const { ERROR_CHECK(m_isInitialized, "decrypt: Private key is NOT initialized."); ERROR_CHECK(*(ct.getPubKey()->getN()) == *(this->getN()), From c9250e03061390314eeb87ea73db113d20d31745 Mon Sep 17 00:00:00 2001 From: skmono Date: Mon, 28 Nov 2022 00:18:28 -0800 Subject: [PATCH 10/12] Expose create function to public --- ipcl/include/ipcl/pub_key.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ipcl/include/ipcl/pub_key.hpp b/ipcl/include/ipcl/pub_key.hpp index 5309919..377a916 100644 --- a/ipcl/include/ipcl/pub_key.hpp +++ b/ipcl/include/ipcl/pub_key.hpp @@ -124,6 +124,9 @@ class PublicKey { */ bool isInitialized() { return m_isInitialized; } + void create(const BigNumber& n, int bits, bool enableDJN_ = false); + void create(const BigNumber& n, int bits, const BigNumber& hs, int randbits); + const void* addr = static_cast(this); private: @@ -167,9 +170,6 @@ class PublicKey { std::vector m_r; bool m_testv; - void create(const BigNumber& n, int bits, bool enableDJN_ = false); - void create(const BigNumber& n, int bits, const BigNumber& hs, int randbits); - /** * Big number vector multi buffer encryption * @param[in] pt plaintext of BigNumber vector type From 19382087e4487aa40271640b4b4f917456c92302 Mon Sep 17 00:00:00 2001 From: skmono Date: Mon, 28 Nov 2022 22:50:15 -0800 Subject: [PATCH 11/12] Updated example/README markdown to reflect changes to ipcl::KeyPair and its members. --- example/README.md | 80 +++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 41 deletions(-) diff --git a/example/README.md b/example/README.md index b117c41..ca69b52 100644 --- a/example/README.md +++ b/example/README.md @@ -53,35 +53,13 @@ If the library is installed globally, `IPCL_DIR` or `IPCL_HINT_DIR` flag is not ## Using Intel Paillier Cryptosystem Library -### Enabling QAT usage -When QAT is enabled while building the library with the flag ```IPCL_ENABLE_QAT=ON```, it is essential to initialize and release the HE QAT context. -```C++ -// Initialize HE QAT context -ipcl::initializeContext("QAT"); - -// perform IPCL operations -auto ct = key.pub_key->encrypt(pt); -auto dec_pt = key.priv_key->decrypt(ct); - -// Release HE QAT context -ipcl::terminateContext(); -``` -If QAT is disabled, ```ipcl::initializeContext("QAT")``` statement will not do anything, thus safe to include in any codes using the library. - -### Hybrid mode configuration -The main accelerated operation - modular exponentiation - can be performed by either IPP-Crypto or the HE QAT. Our library provides a configurable method to distribute the workload between these two methods. +### Key Generation +The public key and private key pair can be generated by using the ```ipcl::generateKeypair``` function. ```C++ -// Use optimal mode -ipcl::setHybridMode(ipcl::HybridMode::OPTIMAL); - -// Use IPP-Crypto modexp only -ipcl::setHybridMode(ipcl::HybridMode::IPP); - -// Use QAT modexp only -ipcl::setHybridMode(ipcl::HybridMode::QAT); +// key.pub_key, key.priv_key +ipcl::KeyPair key = ipcl::generateKeypair(2048, true); // previously ipcl::keyPair ``` -By default, the hybrid mode is set to ```ipcl::HybridMode::OPTIMAL```. For more details about the modes, please refer to [```mod_exp.hpp```](../ipcl/include/ipcl/mod_exp.hpp#L16). - +- Note: With version ```v2.0.0``` and beyond, the key pair struct type has been changed from ```ipcl::keyPair``` to ```ipcl::KeyPair``` (uppercase ```K```). ### Data handling The library uses a container - ```ipcl::PlainText``` for encryption inputs and decryption outputs as well as plaintext HE operations. @@ -120,24 +98,15 @@ pt_copy.clear(); // empty the container ``` FOr more details, please refer to the [```base_text.hpp```](../ipcl/include/ipcl/base_text.hpp) and [```plaintext.hpp```](../ipcl/include/ipcl/plaintext.hpp). -### Key Generation -The public key and private key pair can be generated by using the ```ipcl::generateKeypair``` function. -```C++ -// key.pub_key, key.priv_key -ipcl::keyPair key = ipcl::generateKeypair(2048, true); -// After computation, need to delete the key objects -delete key.pub_key; -delete key.priv_key; -``` - ### Encryption and Decryption The public key is used to encrypt ```ipcl::PlainText``` objects for ```ipcl::CipherText``` outputs. In the same way, the private key is used to decrypt ```ipcl::CipherText``` objects for ```ipcl::PlainText``` outputs. ```C++ ipcl::PlainText pt(raw_data); -ipcl::CipherText ct = key.pub_key->encrypt(pt); -ipcl::PlainText dec_pt = key.priv_key->decrypt(ct); +ipcl::CipherText ct = key.pub_key.encrypt(pt); // previously key.pub_key->encrypt(pt) +ipcl::PlainText dec_pt = key.priv_key.decrypt(ct); // previously key.priv_key->decrypt(ct) ``` +- Note: With version ```v2.0.0``` and beyond, the keys in ```ipcl::KeyPair``` have been changed to be objects, hence the changes from ```key.pub_key->encrypt(pt)``` and ``` key.priv_key->decrypt(ct)``` to ```key.pub_key.encrypt(pt)``` and ```key.priv_key.decrypt(ct)```, respectively. ### HE Operations Since the Intel Paillier Cryptosystem Library being a partially homomorphic encryption scheme, while addition is supports both ciphertext operands, multiplication only supports single ciphertext operand. @@ -146,8 +115,8 @@ Since the Intel Paillier Cryptosystem Library being a partially homomorphic encr // setup ipcl::PlainText a, b; -ipcl::CipherText ct_a = key.pub_key->encrypt(a); -ipcl::CipherText ct_b = key.pub_key->encrypt(b); +ipcl::CipherText ct_a = key.pub_key.encrypt(a); +ipcl::CipherText ct_b = key.pub_key.encrypt(b); // Addition (all three end up being same values after decryption) ipcl::CipherText ct_c1 = ct_a + ct_b; // ciphertext + ciphertext @@ -158,3 +127,32 @@ ipcl::CipherText ct_c3 = ct_b + a; // ciphertext + plaintext ipcl::CipherText ct_d1 = ct_a * b; // ciphertext * plaintext ipcl::CipherText ct_d2 = ct_b * a; ``` + +### Enabling QAT usage +When QAT is enabled while building the library with the flag ```IPCL_ENABLE_QAT=ON```, it is essential to initialize and release the HE QAT context. +```C++ +// Initialize HE QAT context +ipcl::initializeContext("QAT"); + +// perform IPCL operations +auto ct = key.pub_key.encrypt(pt); +auto dec_pt = key.priv_key.decrypt(ct); + +// Release HE QAT context +ipcl::terminateContext(); +``` +If QAT is disabled, ```ipcl::initializeContext("QAT")``` statement will not do anything, thus safe to include in any codes using the library. + +### Hybrid mode configuration +The main accelerated operation - modular exponentiation - can be performed by either IPP-Crypto or the HE QAT. Our library provides a configurable method to distribute the workload between these two methods. +```C++ +// Use optimal mode +ipcl::setHybridMode(ipcl::HybridMode::OPTIMAL); + +// Use IPP-Crypto modexp only +ipcl::setHybridMode(ipcl::HybridMode::IPP); + +// Use QAT modexp only +ipcl::setHybridMode(ipcl::HybridMode::QAT); +``` +By default, the hybrid mode is set to ```ipcl::HybridMode::OPTIMAL```. For more details about the modes, please refer to [```mod_exp.hpp```](../ipcl/include/ipcl/mod_exp.hpp#L16). From 90523e882724a1329f6860cc46bd7a5bf804623b Mon Sep 17 00:00:00 2001 From: skmono Date: Tue, 29 Nov 2022 17:05:22 -0800 Subject: [PATCH 12/12] Example typo fix --- example/example_add_mul.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/example_add_mul.cpp b/example/example_add_mul.cpp index 1b345d8..b06fd15 100644 --- a/example/example_add_mul.cpp +++ b/example/example_add_mul.cpp @@ -42,7 +42,7 @@ int main() { ipcl::setHybridMode(ipcl::HybridMode::OPTIMAL); ipcl::CipherText ct_x = key.pub_key.encrypt(pt_x); - ipcl::CipherText ct_y = key.priv_key.encrypt(pt_y); + ipcl::CipherText ct_y = key.pub_key.encrypt(pt_y); // Perform enc(x) + enc(y) std::cout << "--- IPCL CipherText + CipherText ---" << std::endl;