Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
cmake_minimum_required(VERSION 3.21)
project(
deb
VERSION 0.3.1
VERSION 0.4.0
LANGUAGES CXX
DESCRIPTION "CryptoLab's official cryptosystem library for FHE.")

Expand Down Expand Up @@ -113,6 +113,7 @@ set(DEB_SRC
src/KeyGenerator.cpp
src/ModArith.cpp
src/NTT.cpp
src/NTTConfig.cpp
src/OmpUtils.cpp
src/Preset.cpp
src/RandomGenerator.cpp
Expand Down
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ cmake --build build --target install
- `DEB_INSTALL_ALEA`: Install the alea library when installing deb. (default: OFF)
- `DEB_INSTALL_FLATBUFFERS`: Install the flatbuffers library when installing deb. (default: OFF)
- `DEB_RUNTIME_RESOURCE_CHECK`: Enable runtime resource check. (default: ON)
- `DEB_SERIALIZE_API`: Download FlatBuffers and enable serialization api. (default: ON)
- `DEB_SUPPORT_U64`: Compile u64 coefficient word type support. (default: ON)
- `DEB_SUPPORT_U32`: Compile u32 coefficient word type support. (default: OFF)


## Testing

Expand Down
43 changes: 23 additions & 20 deletions benchmark/benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <chrono>
#include <iostream>
#include <optional>
#include <random>
#include <vector>

Expand Down Expand Up @@ -60,7 +61,7 @@ static void bm_seckey_encryption(benchmark::State &state) {
}

SecretKey sk = SecretKeyGenerator::GenSecretKey(preset);
EncryptorT<T> encryptor;
Encryptor encryptor(preset);
Ciphertext ctxt(preset);

for (auto _ : state) {
Expand All @@ -82,7 +83,7 @@ static void bm_enckey_encryption(benchmark::State &state) {
SecretKey sk = SecretKeyGenerator::GenSecretKey(preset);
KeyGenerator keygen(preset);
SwitchKey enckey = keygen.genEncKey(sk);
EncryptorT<T> encryptor;
Encryptor encryptor(preset);
Ciphertext ctxt(preset);

for (auto _ : state) {
Expand All @@ -101,13 +102,13 @@ template <Preset T> static void bm_decryption(benchmark::State &state) {
}

SecretKey sk = SecretKeyGenerator::GenSecretKey(preset);
EncryptorT<T> encryptor;
DecryptorT<T> decryptor;
Encryptor encryptor(preset);
Decryptor decryptor(preset);
Ciphertext ctxt(preset);
encryptor.encrypt(msg_v, sk, ctxt);

for (auto _ : state) {
decryptor.decrypt(ctxt, sk, msg_v);
decryptor.decrypt(ctxt, sk, msg_v.data());
benchmark::DoNotOptimize(msg_v.data());
benchmark::ClobberMemory();
}
Expand All @@ -122,21 +123,22 @@ template <Preset T> static void bm_decryption_inplace(benchmark::State &state) {
}

SecretKey sk = SecretKeyGenerator::GenSecretKey(preset);
EncryptorT<T> encryptor;
DecryptorT<T> decryptor;
Encryptor encryptor(preset);
Decryptor decryptor(preset);
Ciphertext ctxt(preset);
encryptor.encrypt(msg_v, sk, ctxt);

std::optional<Ciphertext> ctxt_copy;
std::optional<Ciphertext> ctxt_tmp;
for (auto _ : state) {
state.PauseTiming();
ctxt_copy.emplace(ctxt.deepCopy());
ctxt_tmp.emplace(ctxt.deepCopy());
state.ResumeTiming();
decryptor.decryptInplace(ctxt_copy.value(), sk, msg_v.data());
decryptor.decryptInplace(ctxt_tmp.value(), sk, msg_v.data());
benchmark::DoNotOptimize(msg_v.data());
benchmark::ClobberMemory();
}
}

template <Preset T>
static void bm_seckey_coeff_encryption(benchmark::State &state) {
const Preset preset = T;
Expand All @@ -147,7 +149,7 @@ static void bm_seckey_coeff_encryption(benchmark::State &state) {
}

SecretKey sk = SecretKeyGenerator::GenSecretKey(preset);
EncryptorT<T> encryptor;
Encryptor encryptor(preset);
Ciphertext ctxt(preset);

for (auto _ : state) {
Expand All @@ -168,7 +170,7 @@ static void bm_enckey_coeff_encryption(benchmark::State &state) {
SecretKey sk = SecretKeyGenerator::GenSecretKey(preset);
KeyGenerator keygen(preset);
SwitchKey enckey = keygen.genEncKey(sk);
EncryptorT<T> encryptor;
Encryptor encryptor(preset);
Ciphertext ctxt(preset);

for (auto _ : state) {
Expand All @@ -187,14 +189,14 @@ static void bm_coeff_decryption(benchmark::State &state) {
msg_v.push_back(gen_random_coeff(get_degree(preset)));
}
SecretKey sk = SecretKeyGenerator::GenSecretKey(preset);
EncryptorT<T> encryptor(preset);
DecryptorT<T> decryptor(preset);
Encryptor encryptor(preset);
Decryptor decryptor(preset);

Ciphertext ctxt(preset);
encryptor.encrypt(msg_v, sk, ctxt);

for (auto _ : state) {
decryptor.decrypt(ctxt, sk, msg_v);
decryptor.decrypt(ctxt, sk, msg_v.data());
benchmark::DoNotOptimize(msg_v.data());
benchmark::ClobberMemory();
}
Expand All @@ -209,22 +211,23 @@ static void bm_coeff_decryption_inplace(benchmark::State &state) {
msg_v.push_back(gen_random_coeff(get_degree(preset)));
}
SecretKey sk = SecretKeyGenerator::GenSecretKey(preset);
EncryptorT<T> encryptor(preset);
DecryptorT<T> decryptor(preset);
Encryptor encryptor(preset);
Decryptor decryptor(preset);

Ciphertext ctxt(preset);
encryptor.encrypt(msg_v, sk, ctxt);

std::optional<Ciphertext> ctxt_copy;
std::optional<Ciphertext> ctxt_tmp;
for (auto _ : state) {
state.PauseTiming();
ctxt_copy.emplace(ctxt.deepCopy());
ctxt_tmp.emplace(ctxt.deepCopy());
state.ResumeTiming();
decryptor.decryptInplace(ctxt_copy.value(), sk, msg_v.data());
decryptor.decryptInplace(ctxt_tmp.value(), sk, msg_v.data());
benchmark::DoNotOptimize(msg_v.data());
benchmark::ClobberMemory();
}
}

template <Size degree, u64 prime>
static void bm_forward_ntt(benchmark::State &state) {
utils::NTT ntt(degree, prime);
Expand Down
2 changes: 1 addition & 1 deletion benchmark/benchmark_blake3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
std::random_device rd;
std::mt19937 gen{rd()};
std::uniform_real_distribution<double> dist{-1.0, 1.0};
std::uniform_int_distribution<deb::u64> dist_u64{0, UINT64_MAX};
std::uniform_int_distribution<deb::u64> dist_u64;

using namespace deb;

Expand Down
10 changes: 7 additions & 3 deletions cmake/debConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@

@PACKAGE_INIT@

include("${CMAKE_CURRENT_LIST_DIR}/debTargets.cmake")

include(CMakeFindDependencyMacro)

find_dependency(alea REQUIRED)
find_dependency(flatbuffers REQUIRED)

set(DEB_BUILD_WITH_OMP @DEB_BUILD_WITH_OMP@)
if(DEB_BUILD_WITH_OMP)
find_dependency(OpenMP REQUIRED COMPONENTS CXX C)
find_dependency(OpenMP REQUIRED COMPONENTS CXX)
endif()
unset(DEB_BUILD_WITH_OMP)

include("${CMAKE_CURRENT_LIST_DIR}/debTargets.cmake")
check_required_components(deb)
4 changes: 2 additions & 2 deletions cmake/warnings.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function(set_deb_warnings target)
target_compile_options(
${target}
PRIVATE
$<$<OR:$<C_COMPILER_ID:Clang>,$<C_COMPILER_ID:AppleClang>,$<C_COMPILER_ID:GNU>>:
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
-Wall
-Wconversion
-Wextra
Expand All @@ -28,6 +28,6 @@ function(set_deb_warnings target)
-Wunused
-Wvla
>
$<$<C_COMPILER_ID:MSVC>:
$<$<CXX_COMPILER_ID:MSVC>:
/W4>)
endfunction()
3 changes: 3 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ set(${PROJECT_NAME}_example example_utils ${PROJECT_NAME})
add_executable(EnDecryption EnDecryption.cpp)
target_link_libraries(EnDecryption PRIVATE ${${PROJECT_NAME}_example})

add_executable(EnDecryption-Real EnDecryption-Real.cpp)
target_link_libraries(EnDecryption-Real PRIVATE ${${PROJECT_NAME}_example})

add_executable(EnDecryption-MultiSecret EnDecryption-MultiSecret.cpp)
target_link_libraries(EnDecryption-MultiSecret
PRIVATE ${${PROJECT_NAME}_example})
Expand Down
11 changes: 5 additions & 6 deletions examples/EnDecryption-MultiSecret.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include "ExampleUtils.hpp"

using namespace std;
using namespace deb;

int main() {
Expand Down Expand Up @@ -86,7 +85,7 @@ int main() {

// Encrypt with iNTT output
{
auto opt = EncryptOptions().NttOut(false);
auto opt = EncryptOptions().NTTOut(false);
DebTimer::start("iNTT Output EnDecryption");
enc.encrypt(msg, sk, ctxt, opt);
dec.decrypt(ctxt, sk, decrypted_msg);
Expand All @@ -97,7 +96,7 @@ int main() {
// Encrypt with all custom options
{
DebTimer::start("All Custom Options EnDecryption");
enc.encrypt(msg, sk, ctxt, EncryptOptions().Scale(scale).Level(custom_level).NttOut(false));
enc.encrypt(msg, sk, ctxt, EncryptOptions().Scale(scale).Level(custom_level).NTTOut(false));
dec.decrypt(ctxt, sk, decrypted_msg, scale);
DebTimer::end();
std::cout << "log2 error = " << compareMessages(msg, decrypted_msg) << " bits" << std::endl;
Expand Down Expand Up @@ -127,7 +126,7 @@ int main() {
// Encrypt with all custom options
{
DebTimer::start("All Custom Options Coeff EnDecryption");
enc.encrypt(cmsg, sk, ctxt, EncryptOptions().Scale(scale).Level(custom_level).NttOut(false));
enc.encrypt(cmsg, sk, ctxt, EncryptOptions().Scale(scale).Level(custom_level).NTTOut(false));
dec.decrypt(ctxt, sk, decrypted_cmsg, scale);
DebTimer::end();
std::cout << "log2 error = " << compareCoeffs(cmsg, decrypted_cmsg) << " bits" << std::endl;
Expand Down Expand Up @@ -161,7 +160,7 @@ int main() {
// Encrypt with all custom options
{
DebTimer::start("All Custom Options EnDecryption with EncKey");
enc.encrypt(msg, ek, ctxt, EncryptOptions().Scale(scale).Level(custom_level).NttOut(false));
enc.encrypt(msg, ek, ctxt, EncryptOptions().Scale(scale).Level(custom_level).NTTOut(false));
dec.decrypt(ctxt, sk, decrypted_msg, scale);
DebTimer::end();
std::cout << "log2 error = " << compareMessages(msg, decrypted_msg) << " bits" << std::endl;
Expand All @@ -170,7 +169,7 @@ int main() {
// Encrypt with all custom options
{
DebTimer::start("All Custom Options Coeff EnDecryption with EncKey");
enc.encrypt(cmsg, ek, ctxt, EncryptOptions().Scale(scale).Level(custom_level).NttOut(false));
enc.encrypt(cmsg, ek, ctxt, EncryptOptions().Scale(scale).Level(custom_level).NTTOut(false));
dec.decrypt(ctxt, sk, decrypted_cmsg, scale);
DebTimer::end();
std::cout << "log2 error = " << compareCoeffs(cmsg, decrypted_cmsg) << " bits" << std::endl;
Expand Down
Loading
Loading