Implementation blueprint for Microsoft SEAL code

This commit is contained in:
York Jasper Niebuhr 2025-11-27 12:34:08 +01:00
parent ff8386e903
commit 93acf2839c
6 changed files with 213 additions and 10 deletions

View File

@ -51,6 +51,7 @@ using ciphertext = int;
class bfv_exception : public std::exception {
public:
enum class REASON {
CTX_NOT_IMPLEMENTED,
CTX_NO_PRIVATE,
CTX_NO_PUBLIC,
CTX_ARITHMETIC,
@ -105,6 +106,34 @@ Additionally, a graph of running operations is maintained to handle data depende
Only when data is required to actually be present (e.g. decrypt), does the implementation wait
*/
// BFV implementation using Microsoft SEAL
struct seal_context : public context {
seal_context();
~seal_context();
void new_components(int components) override;
void has_components(int components) const override;
void clone_components(int components, std::shared_ptr<context>& ptr) const override;
void allocate(ciphertext& ct) override;
void free(ciphertext ct) override;
void serialize(ciphertext ct, void* buf, std::size_t& n) const override;
void deserialize(ciphertext ct, const void* buf, std::size_t n) const override;
void encrypt(const plaintext& pt, ciphertext ct) const override;
void decrypt(ciphertext ct, plaintext& pt) const override;
void add_cipher_plain(ciphertext a, const plaintext& b, ciphertext res) const override;
void add_cipher_cipher(ciphertext a, ciphertext b, ciphertext res) const override;
void sub_cipher_plain(ciphertext a, const plaintext& b, ciphertext res) const override;
void sub_cipher_cipher(ciphertext a, ciphertext b, ciphertext res) const override;
void mul_cipher_plain(ciphertext a, const plaintext& b, ciphertext res) const override;
void mul_cipher_cipher(ciphertext a, ciphertext b, ciphertext res) const override;
void rot_cipher_rows(ciphertext ct, int r, ciphertext res) const override;
void swap_cipher_rows(ciphertext ct, ciphertext res) const override;
void noise_budget(ciphertext ct, std::size_t& budget) const override;
private:
int m_id;
};
/*
activate_context(std::shared_ptr<bfv::context> ctx) -> thread local pointer is set
Raw ciphertext and plaintext classes always have the full 8192 coefficients (defined in context as static constexpr)

View File

@ -1 +1 @@
target_sources(homcert PRIVATE bfv.cpp)
target_sources(homcert PRIVATE bfv.cpp seal_bfv.cpp)

View File

@ -13,4 +13,4 @@ const char* bfv_exception::what() const noexcept {
return m_message.c_str();
}
};
}

175
src/seal_bfv.cpp Normal file
View File

@ -0,0 +1,175 @@
#include <bfv.hpp>
#ifdef HOMCERT_BFV_SEAL
namespace homcert::bfv {
seal_context::seal_context() {
}
seal_context::~seal_context() {
}
void seal_context::new_components(int components) {
}
void seal_context::has_components(int components) const {
}
void seal_context::clone_components(int components, std::shared_ptr<context>& ptr) const {
}
void seal_context::allocate(ciphertext& ct) {
}
void seal_context::free(ciphertext ct) {
}
void seal_context::serialize(ciphertext ct, void* buf, std::size_t& n) const {
}
void seal_context::deserialize(ciphertext ct, const void* buf, std::size_t n) const {
}
void seal_context::encrypt(const plaintext& pt, ciphertext ct) const {
}
void seal_context::decrypt(ciphertext ct, plaintext& pt) const {
}
void seal_context::add_cipher_plain(ciphertext a, const plaintext& b, ciphertext res) const {
}
void seal_context::add_cipher_cipher(ciphertext a, ciphertext b, ciphertext res) const {
}
void seal_context::sub_cipher_plain(ciphertext a, const plaintext& b, ciphertext res) const {
}
void seal_context::sub_cipher_cipher(ciphertext a, ciphertext b, ciphertext res) const {
}
void seal_context::mul_cipher_plain(ciphertext a, const plaintext& b, ciphertext res) const {
}
void seal_context::mul_cipher_cipher(ciphertext a, ciphertext b, ciphertext res) const {
}
void seal_context::rot_cipher_rows(ciphertext ct, int r, ciphertext res) const {
}
void seal_context::swap_cipher_rows(ciphertext ct, ciphertext res) const {
}
void seal_context::noise_budget(ciphertext ct, std::size_t& budget) const {
}
}
#else
namespace homcert::bfv {
seal_context::seal_context() {
throw bfv_exception(bfv_exception::REASON::CTX_NOT_IMPLEMENTED, "BFV context for Microsoft SEAL is not implemented");
}
seal_context::~seal_context() {
throw bfv_exception(bfv_exception::REASON::CTX_NOT_IMPLEMENTED, "BFV context for Microsoft SEAL is not implemented");
}
void seal_context::new_components(int components) {
throw bfv_exception(bfv_exception::REASON::CTX_NOT_IMPLEMENTED, "BFV context for Microsoft SEAL is not implemented");
}
void seal_context::has_components(int components) const {
throw bfv_exception(bfv_exception::REASON::CTX_NOT_IMPLEMENTED, "BFV context for Microsoft SEAL is not implemented");
}
void seal_context::clone_components(int components, std::shared_ptr<context>& ptr) const {
throw bfv_exception(bfv_exception::REASON::CTX_NOT_IMPLEMENTED, "BFV context for Microsoft SEAL is not implemented");
}
void seal_context::allocate(ciphertext& ct) {
throw bfv_exception(bfv_exception::REASON::CTX_NOT_IMPLEMENTED, "BFV context for Microsoft SEAL is not implemented");
}
void seal_context::free(ciphertext ct) {
throw bfv_exception(bfv_exception::REASON::CTX_NOT_IMPLEMENTED, "BFV context for Microsoft SEAL is not implemented");
}
void seal_context::serialize(ciphertext ct, void* buf, std::size_t& n) const {
throw bfv_exception(bfv_exception::REASON::CTX_NOT_IMPLEMENTED, "BFV context for Microsoft SEAL is not implemented");
}
void seal_context::deserialize(ciphertext ct, const void* buf, std::size_t n) const {
throw bfv_exception(bfv_exception::REASON::CTX_NOT_IMPLEMENTED, "BFV context for Microsoft SEAL is not implemented");
}
void seal_context::encrypt(const plaintext& pt, ciphertext ct) const {
throw bfv_exception(bfv_exception::REASON::CTX_NOT_IMPLEMENTED, "BFV context for Microsoft SEAL is not implemented");
}
void seal_context::decrypt(ciphertext ct, plaintext& pt) const {
throw bfv_exception(bfv_exception::REASON::CTX_NOT_IMPLEMENTED, "BFV context for Microsoft SEAL is not implemented");
}
void seal_context::add_cipher_plain(ciphertext a, const plaintext& b, ciphertext res) const {
throw bfv_exception(bfv_exception::REASON::CTX_NOT_IMPLEMENTED, "BFV context for Microsoft SEAL is not implemented");
}
void seal_context::add_cipher_cipher(ciphertext a, ciphertext b, ciphertext res) const {
throw bfv_exception(bfv_exception::REASON::CTX_NOT_IMPLEMENTED, "BFV context for Microsoft SEAL is not implemented");
}
void seal_context::sub_cipher_plain(ciphertext a, const plaintext& b, ciphertext res) const {
throw bfv_exception(bfv_exception::REASON::CTX_NOT_IMPLEMENTED, "BFV context for Microsoft SEAL is not implemented");
}
void seal_context::sub_cipher_cipher(ciphertext a, ciphertext b, ciphertext res) const {
throw bfv_exception(bfv_exception::REASON::CTX_NOT_IMPLEMENTED, "BFV context for Microsoft SEAL is not implemented");
}
void seal_context::mul_cipher_plain(ciphertext a, const plaintext& b, ciphertext res) const {
throw bfv_exception(bfv_exception::REASON::CTX_NOT_IMPLEMENTED, "BFV context for Microsoft SEAL is not implemented");
}
void seal_context::mul_cipher_cipher(ciphertext a, ciphertext b, ciphertext res) const {
throw bfv_exception(bfv_exception::REASON::CTX_NOT_IMPLEMENTED, "BFV context for Microsoft SEAL is not implemented");
}
void seal_context::rot_cipher_rows(ciphertext ct, int r, ciphertext res) const {
throw bfv_exception(bfv_exception::REASON::CTX_NOT_IMPLEMENTED, "BFV context for Microsoft SEAL is not implemented");
}
void seal_context::swap_cipher_rows(ciphertext ct, ciphertext res) const {
throw bfv_exception(bfv_exception::REASON::CTX_NOT_IMPLEMENTED, "BFV context for Microsoft SEAL is not implemented");
}
void seal_context::noise_budget(ciphertext ct, std::size_t& budget) const {
throw bfv_exception(bfv_exception::REASON::CTX_NOT_IMPLEMENTED, "BFV context for Microsoft SEAL is not implemented");
}
}
#endif

View File

@ -1,6 +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)
add_executable(test_bfv_seal bfv/bfv_seal.cpp)
target_link_libraries(test_bfv_seal PRIVATE homcert GTest::GTest)
endif()

View File

@ -7,15 +7,14 @@
using namespace homcert;
class BFV : public ::testing::Test {
class BFV_SEAL : public ::testing::Test {
protected:
std::shared_ptr<bfv::context> m_context;
};
inline static std::shared_ptr<bfv::context> CTX0;
class BFV_SEAL : public BFV {
protected:
static void SetUpTestSuite() {
// GTEST_SKIP() << "Test environment not available";
CTX0 = std::make_shared<bfv::seal_context>();
if (!CTX0)
GTEST_SKIP() << "Microsoft SEAL BFV implementation is not available!";
}
static void TearDownTestSuite() {}