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

Update to HEXL version 1.2.1 #375

Merged
merged 3 commits into from
Sep 15, 2021
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
32 changes: 17 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ endif()
# Required files and directories
include(GNUInstallDirs)

# Runtime path setup
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

# Source Tree
set(SEAL_INCLUDES_DIR ${CMAKE_CURRENT_LIST_DIR}/native/src)
set(SEAL_THIRDPARTY_DIR ${CMAKE_CURRENT_LIST_DIR}/thirdparty)
Expand Down Expand Up @@ -219,9 +223,9 @@ if(SEAL_USE_INTEL_HEXL)
message(STATUS "Intel HEXL: download ...")
seal_fetch_thirdparty_content(ExternalIntelHEXL)
else()
find_package(HEXL 1.1.0)
find_package(HEXL 1.2.1)
if (NOT TARGET HEXL::hexl)
FATAL_ERROR("Intel HEXL: not found")
message(FATAL_ERROR "Intel HEXL: not found")
endif()
endif()
endif()
Expand Down Expand Up @@ -412,15 +416,13 @@ if(NOT BUILD_SHARED_LIBS)
if(SEAL_USE_INTEL_HEXL)
if(SEAL_BUILD_DEPS)
add_dependencies(seal HEXL::hexl)
target_include_directories(seal PUBLIC $<BUILD_INTERFACE:${hexl_SOURCE_DIR}/hexl/include>)
target_include_directories(seal PUBLIC
$<BUILD_INTERFACE:$<TARGET_PROPERTY:HEXL::hexl,INTERFACE_INCLUDE_DIRECTORIES>>)
seal_combine_archives(seal HEXL::hexl)
target_compile_options(seal PUBLIC $<TARGET_PROPERTY:HEXL::hexl,INTERFACE_COMPILE_OPTIONS>)
target_link_options(seal PUBLIC $<TARGET_PROPERTY:HEXL::hexl,INTERFACE_LINK_OPTIONS>)
else()
target_link_libraries(seal PRIVATE HEXL::hexl)
get_target_property(
HEXL_INCLUDE_DIR
HEXL::hexl
INTERFACE_INCLUDE_DIRECTORIES)
target_include_directories(seal PUBLIC ${HEXL_INCLUDE_DIR})
target_link_libraries(seal PUBLIC HEXL::hexl)
endif()
endif()

Expand Down Expand Up @@ -461,12 +463,12 @@ else()
endif()

if(SEAL_USE_INTEL_HEXL)
get_target_property(
HEXL_INCLUDE_DIR
HEXL::hexl
INTERFACE_INCLUDE_DIRECTORIES)
target_include_directories(seal_shared PUBLIC ${HEXL_INCLUDE_DIR})
target_link_libraries(seal_shared PRIVATE HEXL::hexl)
target_link_libraries(seal_shared PUBLIC HEXL::hexl)
# Install shared HEXL library to installation directory
install(DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_LIBRARY_PREFIX}
FILES_MATCHING PATTERN "*hexl*"
)
endif()
endif()

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ The optional dependencies and their tested versions (other versions may work as

| Optional dependency | Tested version | Use |
| ------------------------------------------------------ | -------------- | ------------------------------------------------ |
| [Intel HEXL](https://github.com/intel/hexl) | 1.1.0 | Acceleration of low-level kernels |
| [Intel HEXL](https://github.com/intel/hexl) | 1.2.1 | Acceleration of low-level kernels |
| [Microsoft GSL](https://github.com/microsoft/GSL) | 3.1.0 | API extensions |
| [ZLIB](https://github.com/madler/zlib) | 1.2.11 | Compressed serialization |
| [Zstandard](https://github.com/facebook/zstd) | 1.4.5 | Compressed serialization (much faster than ZLIB) |
Expand Down
8 changes: 2 additions & 6 deletions cmake/ExternalIntelHEXL.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@ FetchContent_Declare(
hexl
PREFIX hexl
GIT_REPOSITORY https://github.com/intel/hexl
GIT_TAG 2dc1db # v1.1.0
GIT_TAG df6c26f # 1.2.1
)
FetchContent_GetProperties(hexl)

if(NOT hexl_POPULATED)
FetchContent_Populate(hexl)
set(HEXL_DEBUG OFF) # Set to ON/OFF to toggle debugging

set(CMAKE_C_COMPILER ${CMAKE_C_COMPILER} CACHE STRING "" FORCE)
set(CMAKE_CXX_COMPILER ${CMAKE_CXX_COMPILER} CACHE STRING "" FORCE)
set(CMAKE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX} CACHE STRING "" FORCE)
set(HEXL_DEBUG ${HEXL_DEBUG} CACHE BOOL "" FORCE)
set(HEXL_BENCHMARK OFF CACHE BOOL "" FORCE)
set(HEXL_EXPORT OFF CACHE BOOL "" FORCE)
set(HEXL_COVERAGE OFF CACHE BOOL "" FORCE)
set(HEXL_TESTING OFF CACHE BOOL "" FORCE)
set(HEXL_SHARED_LIB OFF CACHE BOOL "" FORCE)
set(HEXL_SHARED_LIB ${BUILD_SHARED_LIBS} CACHE BOOL "" FORCE)
set(EXCLUDE_FROM_ALL TRUE)

mark_as_advanced(BUILD_HEXL)
Expand Down
8 changes: 4 additions & 4 deletions native/src/seal/evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ namespace seal
// x = (x[0] * y[0], x[0] * y[1] + x[1] * y[0], x[1] * y[1])
// with appropriate modular reduction
SEAL_ITERATE(coeff_modulus, coeff_modulus_size, [&](auto I) {
SEAL_ITERATE(iter(size_t(0)), num_tiles, [&](auto J) {
SEAL_ITERATE(iter(size_t(0)), num_tiles, [&](SEAL_MAYBE_UNUSED auto J) {
// Compute third output polynomial, overwriting input
// x[2] = x[1] * y[1]
dyadic_product_coeffmod(
Expand Down Expand Up @@ -617,7 +617,7 @@ namespace seal
encrypted1.scale() = new_scale;
}

void Evaluator::bgv_multiply(Ciphertext &encrypted1, Ciphertext &encrypted2, MemoryPoolHandle pool)
void Evaluator::bgv_multiply(Ciphertext &encrypted1, Ciphertext &encrypted2, MemoryPoolHandle pool) const
{
if (encrypted1.is_ntt_form() || encrypted2.is_ntt_form())
{
Expand Down Expand Up @@ -939,7 +939,7 @@ namespace seal
dyadic_product_coeffmod(encrypted_iter[0], encrypted_iter[1], coeff_modulus_size, coeff_modulus, encrypted_iter[1]);
add_poly_coeffmod(encrypted_iter[1], encrypted_iter[1], coeff_modulus_size, coeff_modulus, encrypted_iter[1]);

// Compute c0^2
// Compute c0^2
dyadic_product_coeffmod(encrypted_iter[0], encrypted_iter[0], coeff_modulus_size, coeff_modulus, encrypted_iter[0]);

// Set the scale
Expand Down Expand Up @@ -2596,7 +2596,7 @@ namespace seal
inverse_ntt_negacyclic_harvey_lazy(get<0, 1>(J), get<2>(J));
}

// ((ct mod qi) - (ct mod qk)) mod qi
// ((ct mod qi) - (ct mod qk)) mod qi with output in [0, 2 * qi_lazy)
SEAL_ITERATE(
iter(get<0, 1>(J), t_ntt), coeff_count, [&](auto K) { get<0>(K) += qi_lazy - get<1>(K); });

Expand Down
4 changes: 2 additions & 2 deletions native/src/seal/evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ namespace seal
*/
inline void rotate_columns_inplace(
Ciphertext &encrypted, const GaloisKeys &galois_keys, MemoryPoolHandle pool = MemoryManager::GetPool()) const
{
{
auto scheme = context_.key_context_data()->parms().scheme();
if (scheme != scheme_type::bfv && scheme != scheme_type::bgv)
{
Expand Down Expand Up @@ -1204,7 +1204,7 @@ namespace seal

void ckks_multiply(Ciphertext &encrypted1, const Ciphertext &encrypted2, MemoryPoolHandle pool) const;

void bgv_multiply(Ciphertext &encrypted1, Ciphertext &encrypted2, MemoryPoolHandle pool);
void bgv_multiply(Ciphertext &encrypted1, Ciphertext &encrypted2, MemoryPoolHandle pool) const;

void bfv_square(Ciphertext &encrypted, MemoryPoolHandle pool) const;

Expand Down
4 changes: 2 additions & 2 deletions native/src/seal/serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ namespace seal

streamoff Serialization::Save(
function<void(ostream &)> save_members, streamoff raw_size, ostream &stream, compr_mode_type compr_mode,
bool clear_buffers)
SEAL_MAYBE_UNUSED bool clear_buffers)
{
if (!save_members)
{
Expand Down Expand Up @@ -339,7 +339,7 @@ namespace seal
}

streamoff Serialization::Load(
function<void(istream &, SEALVersion)> load_members, istream &stream, bool clear_buffers)
function<void(istream &, SEALVersion)> load_members, istream &stream, SEAL_MAYBE_UNUSED bool clear_buffers)
{
if (!load_members)
{
Expand Down
2 changes: 1 addition & 1 deletion native/src/seal/util/intel_seal_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace intel
{
namespace seal_ext
{
hexl::NTT get_ntt(size_t N, uint64_t modulus, uint64_t root)
intel::hexl::NTT &get_ntt(size_t N, uint64_t modulus, uint64_t root)
{
static unordered_map<pair<uint64_t, uint64_t>, hexl::NTT, HashPair> ntt_cache_;

Expand Down
8 changes: 3 additions & 5 deletions native/src/seal/util/intel_seal_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ namespace intel
return cache_.back().get();
}

void deallocate_impl(void *p, std::size_t n)
void deallocate_impl(void *p, SEAL_MAYBE_UNUSED std::size_t n)
{
(void)n;
auto it = std::remove_if(
cache_.begin(), cache_.end(),
[p](const seal::util::Pointer<seal::seal_byte> &seal_pointer) { return p == seal_pointer.get(); });
Expand Down Expand Up @@ -97,9 +96,8 @@ namespace intel
}
}

void deallocate_impl(void *p, std::size_t n)
void deallocate_impl(void *p, SEAL_MAYBE_UNUSED std::size_t n)
{
(void)n;
{
// to prevent inline optimization with deadlock
auto accessor = policy_.locker();
Expand Down Expand Up @@ -151,7 +149,7 @@ namespace intel
@param[in] modulus The modulus
@param[in] root The root of unity
*/
hexl::NTT get_ntt(std::size_t N, std::uint64_t modulus, std::uint64_t root);
intel::hexl::NTT &get_ntt(size_t N, uint64_t modulus, uint64_t root);

/**
Computes the forward negacyclic NTT from the given parameters.
Expand Down
8 changes: 8 additions & 0 deletions native/src/seal/util/scalingvariant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ namespace seal
plain.data(), plain.data() + coeff_count, get<0>(I), get<0>(I),
[&](uint64_t m, uint64_t c) -> uint64_t {
m = barrett_reduce_64(m, plain_modulus);
if (plain_modulus > cipher_modulus)
{
m = barrett_reduce_64(m, cipher_modulus);
WeiDaiWD marked this conversation as resolved.
Show resolved Hide resolved
}
return add_uint_mod(c, m, cipher_modulus);
});
});
Expand Down Expand Up @@ -66,6 +70,10 @@ namespace seal
plain.data(), plain.data() + coeff_count, get<0>(I), get<0>(I),
[&](uint64_t m, uint64_t c) -> uint64_t {
m = barrett_reduce_64(m, plain_modulus);
if (plain_modulus > cipher_modulus)
{
m = barrett_reduce_64(m, cipher_modulus);
}
return sub_uint_mod(c, m, cipher_modulus);
});
});
Expand Down
3 changes: 3 additions & 0 deletions native/tests/seal/util/rns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,8 @@ namespace sealtest
}

// The size of Bsk is 2. Both values here are multiples of 35 (i.e., q).
// Skip tests exceeding input bound when using HEXL in DEBUG mode
#if !defined(SEAL_DEBUG) || !defined(SEAL_USE_INTEL_HEXL)
in[0] = 35;
in[1] = 70;
in[2] = 35;
Expand All @@ -896,6 +898,7 @@ namespace sealtest
rns_tool->decrypt_scale_and_round(in_iter, out_iter, pool);
ASSERT_EQ(2ULL, out[0]);
ASSERT_EQ(0ULL, out[1]);
#endif
}

TEST(RNSToolTest, DivideAndRoundQLastInplace)
Expand Down