当前位置: 首页 > 工具软件 > pyseal > 使用案例 >

Linux下使用SEAL同态加密,pyseal: pyseal 是一个 Python 同态加密库,它使用用于 Python 与 C++ 之间相互暴露类型的 pybind11 库,将微软开源的同态加密库...

仰雅昶
2023-12-01

Microsoft SEAL For Python

Microsoft SEAL is an easy-to-use open-source (MIT licensed) homomorphic encryption library developed by the Cryptography Research group at Microsoft.

pybind11 is a lightweight header-only library that exposes C++ types in Python and vice versa, mainly to create Python bindings of existing C++ code.

This is a python binding for the Microsoft SEAL library.

Contents

Build

Linux

Clang++ (>= 5.0) or GNU G++ (>= 6.0), CMake (>= 3.12)

# Optional

sudoapt-get installbuild-essential cmake python3 python3-dev python3-pip

# Get the repository or download from the releases

git clone https://github.com/Huelse/SEAL-Python.git

cdSEAL-Python

# Numpy is essential

pip3 install -r requirements.txt

# Init the SEAL and pybind11

git submodule init && git submodule update

# Get the newest repositories

git submodule update --remote

# Build the SEAL lib

cdSEAL

cmake -S . -B build -DSEAL_USE_MSGSL=OFF -DSEAL_USE_ZLIB=OFF -DSEAL_USE_ZSTD=OFF

cmake --build build

cd ..

# Run the setup.py

python3 setup.py build_ext -i

Windows

Visual Studio 2019 or newer is required. And use the x64 Native Tools Command Prompt for Visual Studio 2019 command prompt to configure and build the Microsoft SEAL library. It's usually can be found in your Start Menu.

# Same as above

# Build the SEAL library

cmake -S . -B build -G Ninja -DSEAL_USE_MSGSL=OFF -DSEAL_USE_ZLIB=OFF -DSEAL_USE_ZSTD=OFF

cmake --build build

# Run the setup.py

python setup.py build_ext -i

Generally, the Ninja generator is better than the "Visual Studio 16 2019" generator, and there is more information in the Microsoft SEAL official illustrate.

Note

Serialize

In most situations, you can use the SEAL's native serialize API to save the data, here is an example:

cipher.save('cipher')

load_cipher = Ciphertext()

load_cipher.load(context, 'cipher') # work if the context is valid.

Support type: Encryptionparams, Ciphertext, Plaintext, SecretKey, Publickey, Relinkeys, Galoiskeys

Particularly, if you want to use the pickle to serialize your data, you need to do these things like below:

# 1. Modify the serializable object's header file in SEAL and switch the wrapper.

python helper.py

# 2. Rebuild the SEAL lib like above

cmake --build build

# 3. Run the setup.py

python setup.py build_ext -i

Then, you can pickle the data object like this:

import pickle

cipher.set_parms(parms) # necessary

cipher_dump = pickle.dumps(cipher)

cipher_load = pickle.loads(cipher_dump)

Generally, we don't use compression library.

Other

There are a lot of changes in the latest SEAL lib, we try to make the API in python can be used easier, it may remain some problems we unknown, if any problems(bugs), Issue please.

FAQ

ImportError: undefined symbol

Build a shared SEAL library cmake . -DBUILD_SHARED_LIBS=ON, and get the libseal.so,

then change the path in setup.py, and rebuild.

ImportError: libseal.so... cannot find

a. sudo ln -s /path/to/libseal.so /usr/lib

b. add /usr/local/lib or the SEAL/native/lib to /etc/ld.so.conf and refresh it sudo ldconfig

c. build in cmake.

BuildError: C++17 at least

ModuleNotFoundError: No module named 'seal'

The .so or .pyd file must be in the current directory, or you have install it already.

Contributing

 类似资料: