Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor pub priv key with smark pointers #38

Merged
merged 12 commits into from
Nov 30, 2022
24 changes: 18 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "/opt/intel/ipcl")
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})
if(NOT CMAKE_PREFIX_PATH)
set(CMAKE_PREFIX_PATH $ENV{HOME}/intel /opt/intel)
endif()

# Compiler version check - icx/icpx-2021.3.0 is supported
if(CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM")
Expand All @@ -59,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)
Expand All @@ -70,7 +82,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)
Expand Down Expand Up @@ -148,6 +159,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}")
Expand Down
30 changes: 12 additions & 18 deletions benchmark/bench_cryptography.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<BigNumber> 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<BigNumber> exp_bn_v(dsize);
for (size_t i = 0; i < dsize; i++)
Expand All @@ -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)
Expand All @@ -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<BigNumber> 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<BigNumber> 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)
Expand Down
58 changes: 23 additions & 35 deletions benchmark/bench_hybrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -85,24 +85,24 @@ 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<BigNumber> 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<BigNumber> 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);

BigNumber lambda = priv_key->getLambda();
BigNumber lambda = sk.getLambda();
std::vector<BigNumber> pow(dsize, lambda);
std::vector<BigNumber> m(dsize, n * n);

ipcl::CipherText ct = pub_key->encrypt(pt);
ipcl::CipherText ct = pk.encrypt(pt);
std::vector<BigNumber> res(dsize);

#if BENCH_HYBRID_DETAIL
Expand All @@ -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);

Expand All @@ -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<BigNumber> 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<BigNumber> exp_bn_v(dsize);
for (size_t i = 0; i < dsize; i++)
Expand All @@ -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);

Expand All @@ -164,30 +158,27 @@ 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<BigNumber> 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<BigNumber> 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);
#else
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);

Expand All @@ -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<BigNumber> 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<BigNumber> exp_bn1_v(dsize), exp_bn2_v(dsize);
for (int i = 0; i < dsize; i++) {
Expand All @@ -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
Expand All @@ -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);
41 changes: 16 additions & 25 deletions benchmark/bench_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<BigNumber> 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<BigNumber> exp_bn1_v(dsize), exp_bn2_v(dsize);
for (int i = 0; i < dsize; i++) {
Expand All @@ -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)
Expand All @@ -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<BigNumber> 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<BigNumber> exp_bn1_v(dsize), exp_bn2_v(dsize);
for (int i = 0; i < dsize; i++) {
Expand All @@ -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)
Expand All @@ -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<BigNumber> 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<BigNumber> exp_bn1_v(dsize), exp_bn2_v(dsize);
for (int i = 0; i < dsize; i++) {
Expand All @@ -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)
Expand Down
Loading