跳转到主要内容

热门内容

今日:


总体:


最近浏览:


Chinese, Simplified
SEO Title

category

Microsoft SEAL是一个易于使用的开源(麻省理工学院许可)同态加密库,由微软密码学和隐私研究小组开发。Microsoft SEAL是用现代标准C++编写的,易于在许多不同的环境中编译和运行。有关Microsoft SEAL项目的更多信息,请参阅sealcrypto.org。
本文档适用于Microsoft SEAL 4.1版本。使用该库以前版本的用户应该查看更改列表。
 

新闻资讯


BGV方案现在可以在Microsoft SEAL中使用。本文描述了实现细节。我们衷心感谢阿里巴巴双子座实验室为开发BGV方案并将其整合到微软SEAL中所做的巨大努力。我们要感谢英特尔实验室隐私技术研究所持续的测试和报告问题。
从3.7.2版本开始,Microsoft SEAL将在不创建新版本的情况下将新更改推送到主分支、主分支和控制分支。我们采用这种方法来合并社区贡献并及时解决问题。这些分支将领先于最新版本的分支/标签。当有重要的错误修复或新功能时,将创建新版本。
CKKS的EVA编译器可以在GitHub.com/Microsoft/EVA上找到。有关更多信息,请参阅下面的CKKS EVA编程。
用于CKKS加密的SEAL Embedded可在Github.com/Microsoft/SEAL-Embedded上找到。
非对称PSI的APSI库可在Github.com/Microsoft/APSI上找到。

Contents

介绍
核心概念
大多数加密方案由三个功能组成:密钥生成、加密和解密。对称密钥加密方案使用相同的密钥进行加密和解密;公钥加密方案分别使用用于加密的公钥和用于解密的密钥。因此,公钥加密方案允许任何知道公钥的人加密数据,但只有知道私钥的人才能解密和读取数据。对称密钥加密可用于高效加密大量数据,并实现安全的外包云存储。公钥加密是当今实现安全在线通信的基本概念,但通常比对称密钥加密效率低得多。

虽然传统的对称和公钥加密可用于安全存储和通信,但任何外包计算都必然需要在计算之前删除这些加密层。因此,提供外包计算能力的云服务必须能够访问密钥,并实施访问策略以防止未经授权的员工访问这些密钥。

同态加密
同态加密是指允许云直接对加密数据进行计算的加密方案,而不需要先解密数据。这种加密计算的结果仍然是加密的,并且只能用密钥(由数据所有者)解密。在过去十年中,已经发明了具有不同能力和权衡的多种同态加密方案;其中大多数是公钥加密方案,尽管并不总是需要公钥功能。

同态加密不是一种通用技术:只能对加密数据进行一些计算。它还带来了巨大的性能开销,因此对未加密数据执行已经非常昂贵的计算在加密数据上可能是不可行的。此外,用同态加密的数据比未加密的数据大很多倍,因此用这种技术加密整个大型数据库可能没有意义。相反,有意义的用例是在严格的隐私要求完全禁止未加密的云计算的情况下,但计算本身相当轻量级。

通常,同态加密方案具有由数据所有者持有的单个密钥。对于多个不同的私有数据所有者希望参与协作计算的场景,同态加密可能不是一个合理的解决方案。

同态加密不能用于使数据科学家规避GDPR。例如,云服务无法使用同态加密从加密的客户数据中提取见解。相反,加密计算的结果仍然是加密的,只能由数据所有者(例如云服务客户)解密。

大多数同态加密方案提供的安全保证比传统加密方案弱。如果你想使用Microsoft SEAL构建生产软件,你需要阅读SECURITY.md。

微软海豹突击队
Microsoft SEAL是一个同态加密库,允许对加密的整数或实数执行加法和乘法。其他操作,如加密比较、排序或正则表达式,在大多数情况下使用此技术对加密数据进行评估是不可行的。因此,只应使用Microsoft SEAL实现程序中特定的隐私关键云计算部分。

将未加密的计算转换为对加密数据的计算并不总是那么容易或直接,例如,不可能对加密数据进行分支。Microsoft SEAL本身有一个陡峭的学习曲线,需要用户理解许多特定于同构加密的概念,尽管最终API并不太复杂。即使用户能够使用Microsoft SEAL编程和运行特定的计算,高效和低效实现之间的差异也可能是几个数量级,新用户很难知道如何提高他们的计算性能。

Microsoft SEAL提供了两种不同的同态加密方案,具有非常不同的属性。BFV和BGV方案允许对加密整数执行模运算。CKKS方案允许对加密的实数或复数进行加法和乘法运算,但只产生近似结果。在诸如对加密实数求和、对加密数据评估机器学习模型或计算加密位置距离等应用中,CKKS将是迄今为止最好的选择。对于需要精确值的应用,BFV和BGV方案更合适。

入门指南
安装Microsoft SEAL并开始使用它有多种方法。最简单的方法是使用包管理器下载、构建和安装库。例如,vcpkg可在大多数平台上运行,并将与最新版本的Microsoft SEAL(仅限C++17)保持同步。在macOS上,您还可以使用Homebrew。在FreeBSD上,您可以使用pkg install seal安装security/seal。这个。NET库作为多平台NuGet包提供。最后,可以使用多平台CMake构建系统手动构建Microsoft SEAL;有关详细信息,请参阅手动构建Microsoft SEAL。

可选依赖关系
Microsoft SEAL没有必需的依赖关系,但在编译时可以启用某些可选功能,并支持特定的第三方库。

手动构建时,可以选择让Microsoft SEAL构建系统下载并构建依赖关系,或者在系统目录中搜索预安装的依赖关系。在另一个极端,根本无法配置可下载的NuGet包,但始终可以构建自定义的NuGet包。其他包管理器为配置依赖关系和其他构建选项提供了不同数量的机会。

可选依赖项及其测试版本(其他版本也可能适用)如下:

Optional dependency Tested version Use
Intel HEXL 1.2.5 Acceleration of low-level kernels
Microsoft GSL 4.0.0 API extensions
ZLIB 1.2.13 Compressed serialization
Zstandard 1.5.2 Compressed serialization (much faster than ZLIB)
GoogleTest 1.12.1 For running tests
GoogleBenchmark 1.7.1 For running benchmarks

英特尔HEXL
Intel HEXL是一个库,提供同态加密中常见的加密原语的有效实现。这种加速在具有Intel AVX512-IFMA52指令集的Intel处理器上尤为明显。

微软GSL
Microsoft GSL(Guidelines Support Library)是一个仅包含标头的库,它实现了GSL::span:一种视图类型,提供对内存的安全(边界检查)数组访问。

例如,如果Microsoft GSL可用,Microsoft SEAL可以允许BatchEncoder和CKKSEncoder对GSL::span而不是std::vector进行编码和解码,这在某些情况下可以带来显著的性能优势。

ZLIB和Z标准
ZLIB和Zstandard是广泛使用的压缩库。Microsoft SEAL可以选择使用这些库来压缩序列化的数据。

有人可能会问,当密文和密钥数据应该与随机数据无法区分时,压缩会有什么帮助。在Microsoft SEAL中,密文对象由大量整数模特定素数(coeff_modulus素数)组成。特别是在使用CKKS方案时,这些素数可能很小(例如,30位),但数据仍然被序列化为64位整数。因此,几乎一半的密文字节为零的情况并不罕见,应用通用压缩算法是消除这种浪费空间的一种方便方法。BFV方案通常从这种技术中获益较少,因为用于coeff_modulus加密参数的素数往往更大,对这些素数进行模运算的整数会占据每个64位字的更多部分。压缩序列化可以应用于任何可序列化的Microsoft SEAL对象,而不仅仅是密文和密钥。

如果Microsoft SEAL是在ZLIB或Z标准支持下编译的,则压缩将自动用于序列化;请参阅native/src/seale/serialize.h中的序列化::compr_mode_default。但是,始终可以将compr_mode_type:none显式传递给序列化方法以禁用压缩。如果同时启用ZLIB和Zstandard支持,则默认使用Zstandard,因为它的性能要好得多。

注意:SecretKey的压缩率可以(至少在理论上)揭示有关密钥的信息。在Microsoft SEAL的大多数常见应用程序中,SecretKey的大小不会故意泄露给不受信任的各方。如果这是一个问题,人们总是可以以未压缩的形式保存SecretKey。

从NuGet包安装(Windows、Linux、macOS、Android、iOS)
为了。NET开发人员安装Microsoft SEAL的最简单方法是使用NuGet.org上提供的多平台NuGet包。只需将此包添加到您的。NET项目作为依赖项,您就可以开始了。

使用Microsoft SEAL和开发移动应用程序。NET for Android和iOS,只需将此包添加到您的Xamarin项目中即可。与Microsoft SEAL C++库不同。NET包装库仅适用于64位平台,因此仅支持arm64-v8a/x86_64 Android ABI和arm64/x86_64 iOS架构。

从vcpkg安装
您可以使用vcpkg依赖关系管理器下载并安装seal。

git克隆https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh#/bootstrap-vcpkg.bat for Windows
./vcpkg集成安装
./vcpkg安装密封件
vcpkg中的“seal”端口由Microsoft团队成员和社区贡献者保持最新。如果版本已过期,请在vcpkg存储库中创建问题或拉取请求。

示例
使用Microsoft SEAL需要用户投入一些时间学习同态加密的基本概念。该代码附带了大量注释的示例,这些示例旨在逐步教授此类概念,并演示API的很大一部分。这些示例在C++和C#中都是可用的(并且是相同的),并在native/example.(C++)和dotnet/examples/(C#)中分为几个源文件,如下所示:

C++ C# Description
examples.cpp Examples.cs The example runner application
1_bfv_basics.cpp 1_BFV_Basics.cs Encrypted modular arithmetic using the BFV scheme
2_encoders.cpp 2_Encoders.cs Encoding more complex data into Microsoft SEAL plaintext objects
3_levels.cpp 3_Levels.cs Introduces the concept of levels; prerequisite for using the CKKS scheme
4_bgv_basics.cpp 4_BGV_Basics.cs Encrypted modular arithmetic using the BGV scheme
5_ckks_basics.cpp 5_CKKS_Basics.cs Encrypted real number arithmetic using the CKKS scheme
6_rotation.cpp 6_Rotation.cs Performing cyclic rotations on encrypted vectors in the BFV and CKKS schemes
7_serialization.cpp 7_Serialization.cs Serializing objects in Microsoft SEAL
8_performance.cpp 8_Performance.cs Performance tests

建议在运行示例时阅读注释和代码片段以及命令行打印输出。为了便于导航,命令行打印输出提供了相关源文件中相关代码段开始的行号。要构建示例,请参阅示例、测试和基准测试(C++)和构建。NET组件(C#)。

注意:如果不学习示例1-6,就不可能知道如何正确使用Microsoft SEAL。它们旨在为读者提供关于同态加密的必要概念背景。直接从示例中重用代码效果不佳,因为示例通常演示单个功能,并且没有针对性能进行优化。编写Microsoft SEAL代码而不深入研究示例,将不可避免地导致代码易受攻击、出现故障或速度极慢。

CKKS编程与EVA
在研究上述示例时,很明显CKKS方案对初学者可能不友好。由于重新缩放操作的局限性和在不同级别对齐标尺的要求,即使是相对简单的计算也可能难以进行。

我们创建了一个名为EVA的新编译器工具,可以在很大程度上帮助解决这些挑战。EVA允许程序员用Python表达所需的加密计算。它优化了Microsoft SEAL的计算,选择了适当的加密参数,并提供了一个方便的Python API来加密输入、执行计算和解密结果。EVA可以在GitHub.com/Microsoft/EVA上找到。试试看,让我们知道你的想法!

注意:EVA仅支持CKKS方案。目前没有支持BFV或BGV计划的计划。

手动构建Microsoft SEAL
构建C++组件
在所有平台上,Microsoft SEAL都是用CMake构建的。我们建议使用源代码外构建,尽管源代码内构建是可行的。下面我们给出了如何全局(系统范围)或本地(单个用户)配置、构建和安装Microsoft SEAL的说明。全局安装需要提升的(root或管理员)权限。

要求

System Toolchain
Windows Visual Studio 2022 with C++ CMake Tools for Windows
Linux Clang++ (>= 5.0) or GNU G++ (>= 6.0), CMake (>= 3.13)
macOS/iOS Xcode toolchain (>= 9.3), CMake (>= 3.13)
Android Android Studio
FreeBSD CMake (>= 3.13)

 

Note: Microsoft SEAL compiled with Clang++ has much better runtime performance than one compiled with GNU G++.

Building Microsoft SEAL

We assume that Microsoft SEAL has been cloned into a directory called SEAL and all commands presented below are assumed to be executed in the directory SEAL.

You can build the Microsoft SEAL library (out-of-source) for your machine by executing the following commands:

cmake -S . -B build
cmake --build build

After the build completes, the output binaries can be found in build/lib/ and build/bin/ directories.

Various configuration options can be specified and passed to the CMake build system. These are described below in sections Basic CMake Options and Advanced CMake Options.

Installing Microsoft SEAL

If you have root access to the system you can install Microsoft SEAL globally as follows:

cmake -S . -B build
cmake --build build
sudo cmake --install build

To instead install Microsoft SEAL locally, e.g., to ~/mylibs/, do the following:

cmake -S . -B build -DCMAKE_INSTALL_PREFIX=~/mylibs
cmake --build build
sudo cmake --install build

 

Building and Installing on Windows

On Windows the same scripts above work in a developer command prompt for Visual Studio using either the Ninja or "Visual Studio 17 2022" generators.

When using the Ninja generator, please use the appropriate command prompt depending on the platform you want to build for. If you want to build for x64, please use the x64 Native Tools Command Prompt for Visual Studio 2022 command prompt to configure and build the library. If you want to build for x86, please use the x86 Native Tools Command Prompt for Visual Studio 2022 command prompt to configure and build the library. To build using Ninja, type

cmake -S . -B build -G Ninja
cmake --build build

When using the "Visual Studio 17 2022" generator you can use the Developer Command Prompt for VS 2022 command prompt to configure and build the library. By default the generated platform will be x64. You can specify the desired platform using the architecture flag -A <x64|Win32> and the desired configuration using --config <Debug|Release>.

# Generate and build for x64 in Release mode
cmake -S . -B build -G "Visual Studio 17 2022" -A x64
cmake --build build --config Release

# Generate and build for x86 in Release mode
cmake -S . -B build -G "Visual Studio 17 2022" -A Win32
cmake --build build --config Release

Installing the library in Windows works as well. Instead of using the sudo command, however, you need to run cmake --install build from a command prompt with Administrator permissions. Files will be installed by default to C:\Program Files (x86)\SEAL\.

Visual Studio 2022 provides support for CMake-based projects. You can select the menu option File / Open / Folder... and navigate to the folder where the Microsoft SEAL repository is located. After opening the folder, Visual Studio will detect that this is a CMake-based project and will enable the menu command Project / CMake settings for SEAL. This will open the CMake settings editor that provides a user interface where you can create different configurations and set different CMake options.

After the build completes, the output static library seal-<version>.lib can be found in build\lib\ or build\lib\Release\. When linking with applications, using CMake as is explained in Linking with Microsoft SEAL through CMake is highly recommended. Alternatively, you need to add native\src\ (full path) and build\native\src\ as include directories to locate the Microsoft SEAL header files.

Building for Android and iOS

Microsoft SEAL can be compiled for Android and iOS. Under the android/ directory of the source tree you will find an Android Studio project that you can use to compile the library for Android.

To build the library for iOS, use the following scripts:

# Configure CMake
cmake -S . -B build -GXcode -DSEAL_BUILD_SEAL_C=ON -DSEAL_BUILD_STATIC_SEAL_C=ON -DCMAKE_SYSTEM_NAME=iOS "-DCMAKE_OSX_ARCHITECTURES=arm64;x86_64" -C cmake/memset_s.iOS.cmake

# Build libseal*.a for x86_64
xcodebuild -project build/SEAL.xcodeproj -sdk iphonesimulator -arch x86_64 -configuration Release clean build
mkdir -p build/lib/x86_64
cp build/lib/Release/libseal*.a build/lib/x86_64

# Build libseal*.a for arm64
xcodebuild -project SEAL.xcodeproj -sdk iphoneos -arch arm64 -configuration Release clean build
mkdir -p build/lib/arm64
cp build/lib/Release/libseal*.a build/lib/arm64

# Combine libseal-*.a into libseal.a and libsealc-*.a into libsealc.a
lipo -create -output build/lib/libseal.a build/lib/x86_64/libseal-*.a arm64/libseal-*.a
lipo -create -output build/lib/libsealc.a build/lib/x86_64/libsealc-*.a build/lib/arm64/libsealc-*.a

The native libraries generated through these methods are meant to be called only through the .NET library described in the following sections. Specifically, they do not contain any wrappers that can be used from Java (for Android) or Objective C (for iOS).

Building for WebAssembly

Microsoft SEAL can be compiled for JavaScript and WebAssembly using emscripten on Windows, Linux, and macOS. Building for the Web means SEAL can be run in any client/server environment such as all the major browsers (e.g. Edge, Chrome, Firefox, Safari) and NodeJS.

Building for WebAssembly requires the emscripten toolchain to be installed. The easiest way to configure the toolchain is to clone emsdk and follow the instructions (with system-specific notes). For examples, on Linux and macOS, inside the emsdk repo, run the following:

# Install the latest toolchain
./emsdk install latest
./emsdk activate latest
# Source the environment
source ./emsdk_env.sh

On Windows, better run from a developer command prompt for Visual Studio; and replace ./emsdk and source ./emsdk_env.sh with emsdk and emsdk_env.bat, respectively. In other environments, cmake must be added to the path, and either "Ninja" or "MinGW Makefiles" should be specified as generator in the following configuration step. emcmake does not work with Visual Studio 17 2022 generator.

Within the same shell, navigate to the root directory of Microsoft SEAL, run the following commands to build for WebAssembly:

# Configure CMake. Example flags for a release build
emcmake cmake -S . -B build \
 -DBUILD_SHARED_LIBS=OFF \
 -DCMAKE_BUILD_TYPE=Release \
 -DCMAKE_CXX_FLAGS_RELEASE="-DNDEBUG -flto -O3" \
 -DCMAKE_C_FLAGS_RELEASE="-DNDEBUG -flto -O3" \
 -DSEAL_BUILD_BENCH=OFF \ # Benchmark can be built for WASM. Change this to ON.
 -DSEAL_BUILD_EXAMPLES=OFF \
 -DSEAL_BUILD_TESTS=OFF \
 -DSEAL_USE_CXX17=ON \
 -DSEAL_USE_INTRIN=ON \
 -DSEAL_USE_MSGSL=OFF \
 -DSEAL_USE_ZLIB=ON \
 -DSEAL_THROW_ON_TRANSPARENT_CIPHERTEXT=ON

# Make the static library (shared libs are not supported with emscripten)
emmake make -C build -j

# Build the WebAssembly module
emcc \
 -Wall \
 -flto \
 -O3 \
 build/lib/libseal-4.1.a \
 --bind \
 -o "build/bin/seal_wasm.js" \
 -s WASM=1 \
 -s ALLOW_MEMORY_GROWTH=1

Note: There are many flags to consider when building a WebAssembly module. Please refer to the settings.js file for advanced build flags.

Building will generate two output files, seal_wasm.js and seal_wasm.wasm, in the build/bin/ directory. The file sizes for the artifacts are very small. This is because that the optimization flags perform dead code elimination (DCE) as there are no bindings generated to JavaScript. Defining these bindings is necessary in order to call into WebAssembly from the JavaScript domain; however, Microsoft SEAL does not include any definitions at this time. The build flag --bind expects the bindings to be specified using the embind syntax.

Basic CMake Options

The following options can be used with CMake to configure the build. The default value for each option is denoted with boldface in the Values column.

 

CMake option Values Information
CMAKE_BUILD_TYPE Release
Debug
RelWithDebInfo
MinSizeRel
 

Debug and MinSizeRel have worse run-time performance. 

Debug inserts additional assertion code. 

Set to Release unless you are developing 

Microsoft SEAL itself or debugging some complex issue.

SEAL_BUILD_EXAMPLES ON / OFF Build the C++ examples in native/examples.
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; otherwise 

CMake will attempt to locate pre-installed dependencies.

SEAL_USE_INTEL_HEXL ON / OFF Set to ON to use Intel HEXL for low-level kernels.
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.
BUILD_SHARED_LIBS ON / OFF

Set to ON to build a shared library instead of a static library. 

Not supported in Windows.

SEAL_BUILD_SEAL_C ON / OFF

Build the C wrapper library SEAL_C. This is used by the C# wrapper and 

most users should have no reason to build it.

SEAL_USE_CXX17 ON / OFF Set to ON to build Microsoft SEAL as C++17 for a positive performance impact.
SEAL_USE_INTRIN ON / OFF

Set to ON to use compiler intrinsics for improved performance. 

CMake will automatically 

detect which intrinsics are available and enable them accordingly.

 

As usual, these options can be passed to CMake with the -D flag. For example, one could run

cmake -S . -B build -DSEAL_BUILD_EXAMPLES=ON

to configure a release build of a static Microsoft SEAL library and also build the examples.

本文地址
最后修改
星期日, 一月 26, 2025 - 18:54
Article