diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..6230862 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "SEAL"] + path = SEAL + url = https://github.com/microsoft/SEAL.git diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..2caf2af --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,40 @@ +cmake_minimum_required(VERSION 3.28) +project(homcert LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +# Allow customizing what components to build +option(HOMCERT_BUILD_TESTS "Build homcert tests" OFF) +option(HOMCERT_BFV_SEAL "Provide BFV implementation using Microsoft SEAL" ON) + +if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) + set(HOMCERT_BUILD_TESTS ON CACHE BOOL "Enable homcert tests" FORCE) +endif() + +# Include lib crypto +find_package(OpenSSL REQUIRED) + +# Make homcert main library +add_library(homcert "") +target_link_libraries(homcert PRIVATE OpenSSL::Crypto) +target_include_directories(homcert PUBLIC include) + +target_compile_definitions(homcert PRIVATE + $<$:HOMCERT_BFV_SEAL> +) + +if (HOMCERT_BFV_SEAL) + set(SEAL_BUILD_TESTS OFF CACHE BOOL "Disable SEAL tests" FORCE) + set(SEAL_BUILD_BENCH OFF CACHE BOOL "Disable SEAL benchmarks" FORCE) + set(SEAL_BUILD_EXAMPLES OFF CACHE BOOL "Disable SEAL examples" FORCE) + add_subdirectory(SEAL) + + target_link_libraries(homcert PRIVATE SEAL::seal) +endif() + +add_subdirectory(src) + +if (HOMCERT_BUILD_TESTS) + add_subdirectory(test) +endif() diff --git a/SEAL b/SEAL new file mode 160000 index 0000000..7a931d5 --- /dev/null +++ b/SEAL @@ -0,0 +1 @@ +Subproject commit 7a931d55ba84a40b85938f6ca3ac206f18654093 diff --git a/include/bfv.hpp b/include/bfv.hpp index 21ed5d9..0c21c17 100644 --- a/include/bfv.hpp +++ b/include/bfv.hpp @@ -3,6 +3,10 @@ #include #include #include +#include +#include +#include +#include namespace homcert::bfv { @@ -42,7 +46,26 @@ struct plaintext { using ciphertext = int; -// TODO -> Exception with enum that differentiates reason (e.g. CONTEXT_NO_PRIVATE) +// Exception for BFV errors in context + +class bfv_exception : public std::exception { +public: + enum class REASON { + CTX_NO_PRIVATE, + CTX_NO_PUBLIC, + CTX_ARITHMETIC, + CTX_MEMORY, + CTX_INVALID_ARGUMENT, + CTX_COMPONENT + }; +private: + REASON m_reason; + std::string m_message; +public: + bfv_exception(REASON reason, std::string message); + REASON reason() const noexcept; + const char* what() const noexcept override; +}; // Inheriting from context class allows pluggability of implementations (e.g. SEAL vs. GPU) @@ -50,7 +73,7 @@ struct context { static constexpr int PUBLIC_COMPONENT = 1; static constexpr int PRIVATE_COMPONENT = 2; - virtual ~context(); + virtual ~context() = default; virtual void new_components(int components) = 0; // context default constructs empty virtual void has_components(int components) const = 0; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..cc5664e --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1 @@ +target_sources(homcert PRIVATE bfv.cpp) diff --git a/src/bfv.cpp b/src/bfv.cpp new file mode 100644 index 0000000..7e6e7ae --- /dev/null +++ b/src/bfv.cpp @@ -0,0 +1,16 @@ +#include "bfv.hpp" + +namespace homcert::bfv { + +bfv_exception::bfv_exception(REASON reason, std::string message) + : m_reason(reason), m_message(std::move(message)) {} + +bfv_exception::REASON bfv_exception::reason() const noexcept { + return m_reason; +} + +const char* bfv_exception::what() const noexcept { + return m_message.c_str(); +} + +}; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..af52a7e --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,6 @@ +find_package(GTest REQUIRED) + +if (HOMCERT_BFV_SEAL) + add_executable(test_bfv bfv/main.cpp) + target_link_libraries(test_bfv PRIVATE homcert GTest::GTest) +endif() diff --git a/test/bfv/main.cpp b/test/bfv/main.cpp new file mode 100644 index 0000000..aed80f0 --- /dev/null +++ b/test/bfv/main.cpp @@ -0,0 +1,31 @@ +#include +#include +#include +#include + +#include + +using namespace homcert; + +class BFV : public ::testing::Test { +protected: + std::shared_ptr m_context; +}; + +class BFV_SEAL : public BFV { +protected: + static void SetUpTestSuite() { + // GTEST_SKIP() << "Test environment not available"; + } + + static void TearDownTestSuite() {} +}; + +TEST_F(BFV_SEAL, XXX) { + +} + +int main(int argc, char** argv) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}