From 621cea4f02553e5f0b0cab6274c492948da496ea Mon Sep 17 00:00:00 2001 From: Fabian Boemer Date: Tue, 3 Aug 2021 16:02:08 -0700 Subject: [PATCH 1/3] Update to HEXL version 1.2.0 --- CMakeLists.txt | 32 +++++++++++++------------ README.md | 2 +- cmake/ExternalIntelHEXL.cmake | 8 ++----- native/src/seal/evaluator.cpp | 8 +++---- native/src/seal/evaluator.h | 4 ++-- native/src/seal/serialization.cpp | 4 ++-- native/src/seal/util/intel_seal_ext.cpp | 2 +- native/src/seal/util/intel_seal_ext.h | 8 +++---- native/tests/seal/util/rns.cpp | 3 +++ 9 files changed, 35 insertions(+), 36 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 55c00e44c..025368cab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -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.0) if (NOT TARGET HEXL::hexl) - FATAL_ERROR("Intel HEXL: not found") + message(FATAL_ERROR "Intel HEXL: not found") endif() endif() endif() @@ -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 $) + target_include_directories(seal PUBLIC + $>) seal_combine_archives(seal HEXL::hexl) + target_compile_options(seal PUBLIC $) + target_link_options(seal PUBLIC $) 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() @@ -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() diff --git a/README.md b/README.md index f855c0b2b..3896df466 100644 --- a/README.md +++ b/README.md @@ -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.0 | 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) | diff --git a/cmake/ExternalIntelHEXL.cmake b/cmake/ExternalIntelHEXL.cmake index da1a9227d..cc1aff902 100644 --- a/cmake/ExternalIntelHEXL.cmake +++ b/cmake/ExternalIntelHEXL.cmake @@ -5,23 +5,19 @@ FetchContent_Declare( hexl PREFIX hexl GIT_REPOSITORY https://github.com/intel/hexl - GIT_TAG 2dc1db # v1.1.0 + GIT_TAG 25fd8a9 # 1.2.0 ) 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) diff --git a/native/src/seal/evaluator.cpp b/native/src/seal/evaluator.cpp index 5db693ea4..8517c8227 100644 --- a/native/src/seal/evaluator.cpp +++ b/native/src/seal/evaluator.cpp @@ -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( @@ -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()) { @@ -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 @@ -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); }); diff --git a/native/src/seal/evaluator.h b/native/src/seal/evaluator.h index b31419c2d..bc50b34c9 100644 --- a/native/src/seal/evaluator.h +++ b/native/src/seal/evaluator.h @@ -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) { @@ -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; diff --git a/native/src/seal/serialization.cpp b/native/src/seal/serialization.cpp index 0e16457d2..4e80e3671 100644 --- a/native/src/seal/serialization.cpp +++ b/native/src/seal/serialization.cpp @@ -223,7 +223,7 @@ namespace seal streamoff Serialization::Save( function save_members, streamoff raw_size, ostream &stream, compr_mode_type compr_mode, - bool clear_buffers) + SEAL_MAYBE_UNUSED bool clear_buffers) { if (!save_members) { @@ -339,7 +339,7 @@ namespace seal } streamoff Serialization::Load( - function load_members, istream &stream, bool clear_buffers) + function load_members, istream &stream, SEAL_MAYBE_UNUSED bool clear_buffers) { if (!load_members) { diff --git a/native/src/seal/util/intel_seal_ext.cpp b/native/src/seal/util/intel_seal_ext.cpp index dd349da13..8b22daebf 100644 --- a/native/src/seal/util/intel_seal_ext.cpp +++ b/native/src/seal/util/intel_seal_ext.cpp @@ -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, hexl::NTT, HashPair> ntt_cache_; diff --git a/native/src/seal/util/intel_seal_ext.h b/native/src/seal/util/intel_seal_ext.h index 306e468e6..e453713ac 100644 --- a/native/src/seal/util/intel_seal_ext.h +++ b/native/src/seal/util/intel_seal_ext.h @@ -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_pointer) { return p == seal_pointer.get(); }); @@ -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(); @@ -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. diff --git a/native/tests/seal/util/rns.cpp b/native/tests/seal/util/rns.cpp index fff0112a7..bd6844490 100644 --- a/native/tests/seal/util/rns.cpp +++ b/native/tests/seal/util/rns.cpp @@ -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; @@ -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) From 2da99de1a50063d7d63241001054a93bd70be215 Mon Sep 17 00:00:00 2001 From: Fabian Boemer Date: Mon, 30 Aug 2021 14:07:46 -0700 Subject: [PATCH 2/3] Fix failing unit test --- native/src/seal/util/scalingvariant.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/native/src/seal/util/scalingvariant.cpp b/native/src/seal/util/scalingvariant.cpp index 9c05a3aa4..f659ee13f 100644 --- a/native/src/seal/util/scalingvariant.cpp +++ b/native/src/seal/util/scalingvariant.cpp @@ -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); + } return add_uint_mod(c, m, cipher_modulus); }); }); @@ -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); }); }); From 89edcf1d6d1f44bd7f7f98a8354db82bf7d63126 Mon Sep 17 00:00:00 2001 From: Fabian Boemer Date: Wed, 1 Sep 2021 17:58:31 -0700 Subject: [PATCH 3/3] Update to HEXL v1.2.1 --- CMakeLists.txt | 2 +- README.md | 2 +- cmake/ExternalIntelHEXL.cmake | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 025368cab..41e576e52 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -223,7 +223,7 @@ if(SEAL_USE_INTEL_HEXL) message(STATUS "Intel HEXL: download ...") seal_fetch_thirdparty_content(ExternalIntelHEXL) else() - find_package(HEXL 1.2.0) + find_package(HEXL 1.2.1) if (NOT TARGET HEXL::hexl) message(FATAL_ERROR "Intel HEXL: not found") endif() diff --git a/README.md b/README.md index 3896df466..dee433d01 100644 --- a/README.md +++ b/README.md @@ -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.2.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) | diff --git a/cmake/ExternalIntelHEXL.cmake b/cmake/ExternalIntelHEXL.cmake index cc1aff902..a72143e0f 100644 --- a/cmake/ExternalIntelHEXL.cmake +++ b/cmake/ExternalIntelHEXL.cmake @@ -5,7 +5,7 @@ FetchContent_Declare( hexl PREFIX hexl GIT_REPOSITORY https://github.com/intel/hexl - GIT_TAG 25fd8a9 # 1.2.0 + GIT_TAG df6c26f # 1.2.1 ) FetchContent_GetProperties(hexl)