Skip to content

Commit

Permalink
Merge branch 'fboemer/hexl' into 'intel-v0.3'
Browse files Browse the repository at this point in the history
Fboemer/hexl

See merge request DBIO/glade/intel-seal!56
  • Loading branch information
fboemer committed Mar 25, 2021
2 parents ec23ba5 + 2025ef0 commit 6d50a19
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 95 deletions.
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ variables:
before_script:
- whoami
- export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$(pwd)/build/third-party/lib:$(pwd)/build/lib
- export CMAKE_COMPILER_FLAGS="-DCMAKE_CXX_COMPILER=clang++-10 -DCMAKE_C_COMPILER=clang-10 -DSEAL_BUILD_TESTS=ON -DSEAL_BUILD_BENCH=ON -DSEAL_USE_INTEL_LATTICE=ON -DSEAL_BUILD_SEAL_C=ON -DCMAKE_INSTALL_PREFIX=../"
- export CMAKE_COMPILER_FLAGS="-DCMAKE_CXX_COMPILER=clang++-10 -DCMAKE_C_COMPILER=clang-10 -DSEAL_BUILD_TESTS=ON -DSEAL_BUILD_BENCH=ON -DSEAL_USE_INTEL_HEXL=ON -DSEAL_BUILD_SEAL_C=ON -DCMAKE_INSTALL_PREFIX=../"
- cmake --version

format:
Expand Down
50 changes: 25 additions & 25 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,17 @@ if(SEAL_USE_ZSTD)
endif()
endif()

# [option] SEAL_USE_INTEL_LATTICE
set(SEAL_USE_INTEL_LATTICE_OPTION_STR "Use Intel Lattice library")
option(SEAL_USE_INTEL_LATTICE ${SEAL_USE_INTEL_LATTICE_OPTION_STR} OFF)
if(SEAL_USE_INTEL_LATTICE)
# [option] SEAL_USE_INTEL_HEXL
set(SEAL_USE_INTEL_HEXL_OPTION_STR "Use Intel HEXL library")
option(SEAL_USE_INTEL_HEXL ${SEAL_USE_INTEL_HEXL_OPTION_STR} OFF)
if(SEAL_USE_INTEL_HEXL)
if(SEAL_BUILD_DEPS)
message(STATUS "Intel Lattice: download ...")
seal_fetch_thirdparty_content(ExternalIntelLATTICE)
message(STATUS "Intel HEXL: download ...")
seal_fetch_thirdparty_content(ExternalIntelHEXL)
else()
find_package(IntelLattice REQUIRED)
if (NOT TARGET intel_lattice)
FATAL_ERROR("Intel Lattice: not found")
find_package(IntelHEXL REQUIRED)
if (NOT TARGET intel_hexl)
FATAL_ERROR("Intel HEXL: not found")
endif()
endif()
endif()
Expand Down Expand Up @@ -379,18 +379,18 @@ if(NOT BUILD_SHARED_LIBS)
endif()
endif()

if(SEAL_USE_INTEL_LATTICE)
if(SEAL_USE_INTEL_HEXL)
if(SEAL_BUILD_DEPS)
add_dependencies(seal intel_lattice)
target_include_directories(seal PUBLIC $<BUILD_INTERFACE:${intel_lattice_SOURCE_DIR}/intel-lattice/include>)
seal_combine_archives(seal intel_lattice)
add_dependencies(seal intel_hexl)
target_include_directories(seal PUBLIC $<BUILD_INTERFACE:${intel_hexl_SOURCE_DIR}/intel-hexl/include>)
seal_combine_archives(seal intel_hexl)
else()
target_link_libraries(seal PRIVATE intel_lattice)
target_link_libraries(seal PRIVATE intel_hexl)
get_target_property(
INTEL_LATTICE_INCLUDE_DIR
intel_lattice
INTEL_HEXL_INCLUDE_DIR
intel_hexl
INTERFACE_INCLUDE_DIRECTORIES)
target_include_directories(seal PUBLIC ${INTEL_LATTICE_INCLUDE_DIR})
target_include_directories(seal PUBLIC ${INTEL_HEXL_INCLUDE_DIR})
endif()
endif()

Expand Down Expand Up @@ -430,9 +430,9 @@ else()
target_link_libraries(seal_shared PRIVATE ${zstd_static})
endif()

if(SEAL_USE_INTEL_LATTICE)
target_include_directories(seal_shared PRIVATE $<BUILD_INTERFACE:${intel_lattice_SOURCE_DIR}/intel-lattice/include>)
target_link_libraries(seal_shared PRIVATE intel_lattice)
if(SEAL_USE_INTEL_HEXL)
target_include_directories(seal_shared PRIVATE $<BUILD_INTERFACE:${intel_hexl_SOURCE_DIR}/intel-hexl/include>)
target_link_libraries(seal_shared PRIVATE intel_hexl)
endif()
endif()

Expand Down Expand Up @@ -493,8 +493,8 @@ if(SEAL_BUILD_SEAL_C)
endif()
seal_set_language(sealc)
seal_set_include_directories(sealc)
if (SEAL_USE_INTEL_LATTICE)
target_include_directories(sealc PRIVATE $<BUILD_INTERFACE:${intel_lattice_SOURCE_DIR}/intel-lattice/include>)
if (SEAL_USE_INTEL_HEXL)
target_include_directories(sealc PRIVATE $<BUILD_INTERFACE:${intel_hexl_SOURCE_DIR}/intel-hexl/include>)
endif()

target_link_libraries(sealc PUBLIC seal)
Expand Down Expand Up @@ -550,10 +550,10 @@ if(SEAL_USE_MSGSL AND SEAL_BUILD_DEPS)
DESTINATION ${SEAL_INCLUDES_INSTALL_DIR})
endif()

# Install Intel Lattice header files if SEAL_BUILD_DEPS is ON
if(SEAL_USE_INTEL_LATTICE AND SEAL_BUILD_DEPS)
# Install Intel HEXL header files if SEAL_BUILD_DEPS is ON
if(SEAL_USE_INTEL_HEXL AND SEAL_BUILD_DEPS)
install(
DIRECTORY ${intel_lattice_SOURCE_DIR}/intel-lattice/include/intel-lattice
DIRECTORY ${intel_hexl_SOURCE_DIR}/intel-hexl/include/intel-HEXL
DESTINATION ${SEAL_INCLUDES_INSTALL_DIR})
endif()

Expand Down
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ The [EVA compiler for CKKS](https://arxiv.org/abs/1912.11951) is available at [G
- [Microsoft SEAL](#microsoft-seal-1)
- [Getting Started](#getting-started)
- [Optional Dependencies](#optional-dependencies)
- [Intel HEXL](#intel-hexl)
- [Microsoft GSL](#microsoft-gsl)
- [ZLIB and Zstandard](#zlib-and-zstandard)
- [Intel Lattice](#intel-lattice)
- [Installing from NuGet Package](#installing-from-nuget-package-windows-linux-macos-android-ios)
- [Examples](#examples)
- [CKKS Programming with EVA](#ckks-programming-with-eva)
Expand Down Expand Up @@ -115,17 +115,15 @@ The optional dependencies and their tested versions (other versions may work as

| Optional dependency | Tested version | Use |
| ------------------------------------------------------ | -------------- | ------------------------------------------------ |
| [Intel Lattice](TODO) | TODO | Acceleration of low-level kernels |
| [Intel HEXL](TODO) | TODO | 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) |
| [GoogleTest](https://github.com/google/googletest) | 1.10.0 | For running tests |
| [GoogleBenchmark](https://github.com/google/benchmark) | 1.5.2 | For running benchmarks |

#### Intel Lattice
Intel Lattice is a library providing efficient implementations of cryptographic primitives common in homomorphic encryption. The acceleration is particularly evident on Intel Xeon IceLake processors on primes less than 50 bits.

Intel Lattice is used to accelerate the number-theoretic transform, and polynomial addition and multiplication.
#### Intel HEXL
Intel HEXL is a library providing efficient implementations of cryptographic primitives common in homomorphic encryption. The acceleration is particularly evident on Intel processors with the AVX512-IMA52 instruction set.

#### Microsoft GSL

Expand Down Expand Up @@ -330,7 +328,7 @@ The following options can be used with CMake to configure the build. The default
| SEAL_BUILD_TESTS | ON / **OFF** | Build the tests to check that Microsoft SEAL works correctly. |
| SEAL_BUILD_BENCH | ON / **OFF** | Build the performance benchmark. |
| SEAL_BUILD_DEPS | **ON** / OFF | Set to `ON` to automatically download and build [optional dependencies](#optional-dependencies); otherwise CMake will attempt to locate pre-installed dependencies. |
| SEAL_USE_INTEL_LATTICE | ON / **OFF** | Set to `ON` to use Intel Lattice for improved performance. |
| SEAL_USE_INTEL_HEXL | ON / **OFF** | Set to `ON` to use Intel HEXL for improved performance. |
| SEAL_USE_MSGSL | **ON** / OFF | Build with Microsoft GSL support. |
| SEAL_USE_ZLIB | **ON** / OFF | Build with ZLIB support. |
| SEAL_USE_ZSTD | **ON** / OFF | Build with Zstandard support. |
Expand Down Expand Up @@ -359,7 +357,7 @@ The following options can be used with CMake to further configure the build. Mos
| SEAL_DEFAULT_PRNG | **Blake2xb**</br>Shake256 | Microsoft SEAL supports both Blake2xb and Shake256 XOFs for generating random bytes. Blake2xb is much faster, but it is not standardized, whereas Shake256 is a FIPS standard. |
| SEAL_USE_GAUSSIAN_NOISE | ON / **OFF** | Set to `ON` to use a non-constant time rounded continuous Gaussian for the error distribution; otherwise a centered binomial distribution &ndash; with slightly larger standard deviation &ndash; is used. |
| SEAL_SECURE_COMPILE_OPTIONS | ON / **OFF** | Set to `ON` to compile/link with Control-Flow Guard (`/guard:cf`) and Spectre mitigations (`/Qspectre`). This has an effect only when compiling with MSVC. |
| SEAL_USE_ALIGN_64 | **ON** / OFF | Set to `ON` to use 64-byte aligned memory allocations. This can improve performance of AVX512 primitives when Intel Lattice is enabled. |
| SEAL_USE_ALIGN_64 | **ON** / OFF | Set to `ON` to use 64-byte aligned memory allocations. This can improve performance of AVX512 primitives when Intel HEXL is enabled. |

#### Linking with Microsoft SEAL through CMake

Expand Down
36 changes: 36 additions & 0 deletions cmake/ExternalIntelHEXL.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT license.

FetchContent_Declare(
intel_hexl
PREFIX intel_hexl
GIT_REPOSITORY ssh://git@gitlab.devtools.intel.com:29418/DBIO/glade/intel-hexl.git
GIT_TAG v0.3
)
FetchContent_GetProperties(intel_hexl)

if(NOT intel_hexl_POPULATED)
FetchContent_Populate(intel_hexl)
set(INTEL_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 ${INTEL_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(EXCLUDE_FROM_ALL TRUE)

mark_as_advanced(BUILD_INTEL_HEXL)
mark_as_advanced(INSTALL_INTEL_HEXL)
mark_as_advanced(FETCHCONTENT_SOURCE_DIR_INTEL_HEXL)
mark_as_advanced(FETCHCONTENT_UPDATES_DISCONNECTED_INTEL_HEXL)

add_subdirectory(
${intel_hexl_SOURCE_DIR}
EXCLUDE_FROM_ALL
)
endif()
36 changes: 0 additions & 36 deletions cmake/ExternalIntelLATTICE.cmake

This file was deleted.

4 changes: 2 additions & 2 deletions cmake/SEALConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# SEAL_USE_CXX17 : Set to non-zero value if library is compiled as C++17 instead of C++14
# SEAL_ENFORCE_HE_STD_SECURITY : Set to non-zero value if library is compiled to enforce at least
# a 128-bit security level based on HomomorphicEncryption.org security estimates
# SEAL_USE_INTEL_LATTICE: Set to non-zero value if library is compiled with Intel Lattice support
# SEAL_USE_INTEL_HEXL: Set to non-zero value if library is compiled with Intel HEXL support
# SEAL_USE_MSGSL : Set to non-zero value if library is compiled with Microsoft GSL support
# SEAL_USE_ZLIB : Set to non-zero value if library is compiled with ZLIB support
# SEAL_USE_ZSTD : Set to non-zero value if library is compiled with Zstandard support
Expand Down Expand Up @@ -52,7 +52,7 @@ set(SEAL_DEBUG @SEAL_DEBUG@)
set(SEAL_USE_CXX17 @SEAL_USE_CXX17@)
set(SEAL_ENFORCE_HE_STD_SECURITY @SEAL_ENFORCE_HE_STD_SECURITY@)

set(SEAL_USE_INTEL_LATTICE @SEAL_USE_INTEL_LATTICE@)
set(SEAL_USE_INTEL_HEXL @SEAL_USE_INTEL_HEXL@)
set(SEAL_USE_MSGSL @SEAL_USE_MSGSL@)
set(SEAL_USE_ZLIB @SEAL_USE_ZLIB@)
set(SEAL_USE_ZSTD @SEAL_USE_ZSTD@)
Expand Down
2 changes: 1 addition & 1 deletion native/src/seal/util/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@
#cmakedefine SEAL_USE_MSGSL
#cmakedefine SEAL_USE_ZLIB
#cmakedefine SEAL_USE_ZSTD
#cmakedefine SEAL_USE_INTEL_LATTICE
#cmakedefine SEAL_USE_INTEL_HEXL
10 changes: 5 additions & 5 deletions native/src/seal/util/intel_seal_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

#pragma once

#ifdef SEAL_USE_INTEL_LATTICE
#ifdef SEAL_USE_INTEL_HEXL
#include "seal/util/locks.h"
#include <unordered_map>
#include "intel-lattice/intel-lattice.hpp"
#include "intel-hexl/intel-hexl.hpp"

namespace intel
{
Expand All @@ -29,11 +29,11 @@ namespace intel
}
};

static std::unordered_map<std::pair<uint64_t, uint64_t>, intel::lattice::NTT, HashPair> ntt_cache_;
static std::unordered_map<std::pair<uint64_t, uint64_t>, intel::hexl::NTT, HashPair> ntt_cache_;

static seal::util::ReaderWriterLocker ntt_cache_locker_;

static intel::lattice::NTT get_ntt(size_t N, uint64_t p, uint64_t root)
static intel::hexl::NTT get_ntt(size_t N, uint64_t p, uint64_t root)
{
std::pair<uint64_t, uint64_t> key{ N, p };

Expand All @@ -54,7 +54,7 @@ namespace intel
auto ntt_it = ntt_cache_.find(key);
if (ntt_it == ntt_cache_.end())
{
ntt_it = ntt_cache_.emplace(std::move(key), intel::lattice::NTT(N, p, root)).first;
ntt_it = ntt_cache_.emplace(std::move(key), intel::hexl::NTT(N, p, root)).first;
}
return ntt_it->second;
}
Expand Down
8 changes: 4 additions & 4 deletions native/src/seal/util/ntt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include "seal/util/uintarithsmallmod.h"
#include <algorithm>

#ifdef SEAL_USE_INTEL_LATTICE
#include "intel-lattice/intel-lattice.hpp"
#ifdef SEAL_USE_INTEL_HEXL
#include "intel-hexl/intel-hexl.hpp"
#endif

using namespace std;
Expand Down Expand Up @@ -177,7 +177,7 @@ namespace seal

void ntt_negacyclic_harvey_lazy(CoeffIter operand, const NTTTables &tables)
{
#ifdef SEAL_USE_INTEL_LATTICE
#ifdef SEAL_USE_INTEL_HEXL
size_t N = size_t(1) << tables.coeff_count_power();
uint64_t p = tables.modulus().value();
uint64_t root = tables.get_root();
Expand All @@ -191,7 +191,7 @@ namespace seal

void inverse_ntt_negacyclic_harvey_lazy(CoeffIter operand, const NTTTables &tables)
{
#ifdef SEAL_USE_INTEL_LATTICE
#ifdef SEAL_USE_INTEL_HEXL
size_t N = size_t(1) << tables.coeff_count_power();
uint64_t p = tables.modulus().value();
uint64_t root = tables.get_root();
Expand Down
6 changes: 3 additions & 3 deletions native/src/seal/util/ntt.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "seal/util/uintcore.h"
#include <stdexcept>

#ifdef SEAL_USE_INTEL_LATTICE
#ifdef SEAL_USE_INTEL_HEXL
#include "seal/util/intel_seal_ext.h"
#endif

Expand Down Expand Up @@ -234,7 +234,7 @@ namespace seal

inline void ntt_negacyclic_harvey(CoeffIter operand, const NTTTables &tables)
{
#ifdef SEAL_USE_INTEL_LATTICE
#ifdef SEAL_USE_INTEL_HEXL
size_t N = size_t(1) << tables.coeff_count_power();
uint64_t p = tables.modulus().value();
uint64_t root = tables.get_root();
Expand Down Expand Up @@ -335,7 +335,7 @@ namespace seal

inline void inverse_ntt_negacyclic_harvey(CoeffIter operand, const NTTTables &tables)
{
#ifdef SEAL_USE_INTEL_LATTICE
#ifdef SEAL_USE_INTEL_HEXL
size_t N = size_t(1) << tables.coeff_count_power();
uint64_t p = tables.modulus().value();
uint64_t root = tables.get_root();
Expand Down
12 changes: 6 additions & 6 deletions native/src/seal/util/polyarithsmallmod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include "seal/util/uintarith.h"
#include "seal/util/uintcore.h"

#ifdef SEAL_USE_INTEL_LATTICE
#include "intel-lattice/intel-lattice.hpp"
#ifdef SEAL_USE_INTEL_HEXL
#include "intel-hexl/intel-hexl.hpp"
#endif

using namespace std;
Expand Down Expand Up @@ -90,10 +90,10 @@ namespace seal
}
#endif

#ifdef SEAL_USE_INTEL_LATTICE
#ifdef SEAL_USE_INTEL_HEXL
const uint64_t *op1 = reinterpret_cast<const uint64_t *>(&poly[0]);
uint64_t *rs = reinterpret_cast<uint64_t *>(&result[0]);
intel::lattice::EltwiseFMAMod(rs, op1, scalar.operand, nullptr, coeff_count, modulus.value(), 8);
intel::hexl::EltwiseFMAMod(rs, op1, scalar.operand, nullptr, coeff_count, modulus.value(), 8);
#else
SEAL_ITERATE(iter(poly, result), coeff_count, [&](auto I) {
const uint64_t x = get<0>(I);
Expand Down Expand Up @@ -128,11 +128,11 @@ namespace seal
throw invalid_argument("modulus");
}
#endif
#ifdef SEAL_USE_INTEL_LATTICE
#ifdef SEAL_USE_INTEL_HEXL
const uint64_t *op1 = reinterpret_cast<const uint64_t *>(&operand1[0]);
const uint64_t *op2 = reinterpret_cast<const uint64_t *>(&operand2[0]);
uint64_t *rs = reinterpret_cast<uint64_t *>(&result[0]);
intel::lattice::EltwiseMultMod(rs, op1, op2, coeff_count, modulus.value(), 4);
intel::hexl::EltwiseMultMod(rs, op1, op2, coeff_count, modulus.value(), 4);
#else
const uint64_t modulus_value = modulus.value();
const uint64_t const_ratio_0 = modulus.const_ratio()[0];
Expand Down
Loading

0 comments on commit 6d50a19

Please sign in to comment.