Microsoft SEAL

repository·main·Indexed 26 days ago

https://github.com/microsoft/seal

An open-source homomorphic encryption library that enables computations on encrypted data. It supports the BFV and BGV schemes for exact integer arithmetic and the CKKS scheme for approximate real or complex number arithmetic. The library is written in C++ (requiring C++17) and provides a .NET Standard library wrapper.

Tokens
10.9K
Snippets
13
Records
68
Agent score
84%

What's inside Microsoft SEAL

  1. Overview of Microsoft SEAL

    main

    Microsoft SEAL is an open-source (MIT licensed) homomorphic encryption library developed by Microsoft. It allows performing additions and multiplications on encrypted integers or real numbers.

    Key features include:

    • BFV and BGV schemes: Used for modular arithmetic on encrypted integers where exact values are required.
    • CKKS scheme: Used for additions and multiplications on encrypted real or complex numbers, providing approximate results. This is ideal for machine learning models or summing real numbers.

    Note: Microsoft SEAL 4.4.0 is a critical security update addressing untrusted-input handling, native memory safety, and managed/native interoperability. All users should upgrade to this version immediately.

  2. Build C++ components manually with CMake

    main

    Microsoft SEAL is built using CMake. You can perform an out-of-source build on all supported platforms.

    Requirements:

    • Windows: Visual Studio 2022 with C++ CMake Tools.
    • Linux: Clang++ (>= 5.0) or GNU G++ (>= 6.0), CMake (>= 3.22).
    • macOS/iOS: Xcode toolchain (>= 9.3), CMake (>= 3.22).
    • Android: Android Studio.
    • FreeBSD: CMake (>= 3.22).

    Note: Compiling with Clang++ provides better runtime performance than GNU G++ on Linux.

    Basic Build Steps: Assuming you are in the SEAL directory:

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

    Output binaries are located in build/lib/ and build/bin/.

    cmake -S . -B build
    cmake --build build
  3. Install Microsoft SEAL via vcpkg

    main

    You can use the vcpkg dependency manager to install Microsoft SEAL on most platforms. Note that the C++ library requires C++17.

    To install, clone the vcpkg repository, bootstrap it, and then install the seal port. You can also select optional features using the seal[<feature>,...] syntax.

    git clone https://github.com/Microsoft/vcpkg.git
    cd vcpkg
    ./bootstrap-vcpkg.sh  # ./bootstrap-vcpkg.bat for Windows
    ./vcpkg integrate install
    ./vcpkg install seal
  4. Create a Microsoft SEAL NuGet package

    main

    To create a NuGet package for use in .NET projects, you must first compile both the SEAL and SEAL_C projects using CMake, and then build the dotnet\src\SEALNet.csproj project. Once the binaries are compiled, use the NuGet command line tool to pack the package.

    Prerequisites:

    1. Compiled SEAL and SEAL_C projects via CMake.
    2. Built dotnet\src\SEALNet.csproj.
    3. Installed NuGet command line tool.
    cd dotnet\nuget
    nuget pack SEALNet.nuspec -properties Configuration=Release -Verbosity detailed -OutputDir Release
    cd ..\..
  5. Build Microsoft SEAL for iOS (XCFrameworks)

    main

    The easiest way to build XCFrameworks for iOS is using the provided CMake script. This builds for both device (iphoneos) and simulator (iphonesimulator) targeting arm64.

    Release build (outputs to project root):

    cmake -P cmake/ios_xcframework.cmake

    Debug build (custom output directory):

    cmake -DBUILD_TYPE=Debug -DOUTPUT_DIR=./out -P cmake/ios_xcframework.cmake

    Once generated, include libseal-4.4.xcframework and libsealc-4.4.xcframework in your Xcode project as framework dependencies.

    cmake -P cmake/ios_xcframework.cmake
  6. Install Microsoft SEAL via NuGet (.NET)

    main

    For .NET developers, a multiplatform NuGet package is available.

    Important Notice: No further releases of Microsoft SEAL will be published to NuGet.org. The existing package Microsoft.Research.SEALNet remains available but will not be updated to match newer versions of Microsoft SEAL. To use a newer version in a .NET project, you must build your own NuGet package from source.

    Mobile Support (MAUI):

    • The .NET wrapper works only on 64-bit platforms.
    • Supported Android ABIs: arm64-v8a, x86_64.
    • Supported iOS: arm64 for devices and arm64 for the iOS simulator (Apple Silicon Mac hosts only).
  7. Build Microsoft SEAL for Windows

    main

    On Windows, use a developer command prompt for Visual Studio with either the Ninja or "Visual Studio 17 2022" generator.

    Using Ninja: Use the x64 Native Tools Command Prompt for x64 builds or the x86 Native Tools Command Prompt for x86 builds.

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

    Using Visual Studio 17 2022 Generator: Use the Developer Command Prompt for VS 2022. By default, it generates for x64. Use -A <x64|Win32> to specify architecture and --config <Debug|Release> for configuration.

    # x64 Release
    cmake -S . -B build -G "Visual Studio 17 2022" -A x64
    cmake --build build --config Release
    
    # x86 Release
    cmake -S . -B build -G "Visual Studio 17 2022" -A Win32
    cmake --build build --config Release

    Installation on Windows: Run cmake --install build from a command prompt with Administrator permissions. Default install path is C:\Program Files (x86)\SEAL\.

    cmake -S . -B build -G "Visual Studio 17 2022" -A x64
    cmake --build build --config Release
  8. Use Compressed Serialization in Microsoft SEAL

    main

    If Microsoft SEAL is compiled with ZLIB or Zstandard support, compression is used automatically for serialization (defaulting to Zstandard if both are available).

    To explicitly disable compression, pass compr_mode_type::none to serialization methods.

    Security Note: The compression rate of a SecretKey can theoretically reveal information about the key. If this is a security concern, save the SecretKey in an uncompressed form.

  9. Configure Visual Studio to use a local NuGet package source

    main

    After creating the NuGet package, copy it from dotnet\nuget\Release to a local directory. To use this package in Visual Studio, you must add that directory as a package source:

    1. Open Visual Studio and select Tools / Options....
    2. Navigate to NuGet Package Manager / Package Sources in the left pane.
    3. In the right pane, add a new package source pointing to the directory where you copied the NuGet package.
    4. Once configured, you can add the package to your project by right-clicking your project in Solution Explorer, selecting Manage NuGet packages..., and choosing the Microsoft.Research.SEAL package.
  10. Install Microsoft SEAL globally or locally

    main

    After building the library, you can install it to your system.

    Global Installation (requires root/administrator privileges):

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

    Local Installation (e.g., to a specific directory): To install to a custom path like ~/mylibs/:

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