Skip to content

4.0.0 #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitattributes

This file was deleted.

78 changes: 38 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ This is a python binding for the Microsoft SEAL library.


## Build
* #### Linux
Clang++ (>= 5.0) or GNU G++ (>= 6.0), CMake (>= 3.12)

* ### Linux

Recommend: Clang++ (>= 10.0) or GNU G++ (>= 9.4), CMake (>= 3.16)

```shell
# Optional
Expand All @@ -30,8 +32,8 @@ This is a python binding for the Microsoft SEAL library.
git clone https://github.com/Huelse/SEAL-Python.git
cd SEAL-Python

# Numpy is essential
pip3 install -r requirements.txt
# Install dependencies
pip3 install numpy pybind11

# Init the SEAL and pybind11
git submodule update --init --recursive
Expand All @@ -40,34 +42,45 @@ This is a python binding for the Microsoft SEAL library.

# Build the SEAL lib
cd SEAL
cmake -S . -B build -DSEAL_USE_MSGSL=OFF -DSEAL_USE_ZLIB=OFF -DSEAL_USE_ZSTD=OFF
cmake -S . -B build -DSEAL_USE_MSGSL=OFF -DSEAL_USE_ZLIB=OFF
cmake --build build
cd ..

# Run the setup.py
python3 setup.py build_ext -i

# Test
cp seal.*.so examples
python3 4_bgv_basics.py
```

CMake Options: `-DSEAL_BUILD_EXAMPLES=ON` and [more](https://github.com/microsoft/SEAL#basic-cmake-options)

* #### Windows
Build examples (after `cmake -S . -B`): `-DSEAL_BUILD_EXAMPLES=ON`

Zstandard compression off: `-DSEAL_USE_ZSTD=OFF`

[More cmake options](https://github.com/microsoft/SEAL#basic-cmake-options)


* ### 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.
Visual Studio 2019 or newer is required. And use the **x64 Native Tools Command Prompt for VS** command prompt to configure and build the Microsoft SEAL library. It's usually can be found in your Start Menu.

```shell
# 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
# Run in "x64 Native Tools Command Prompt for VS" command prompt
cmake -S . -B build -G Ninja -DSEAL_USE_MSGSL=OFF -DSEAL_USE_ZLIB=OFF
cmake --build build

# Run the setup.py
pip install numpy pybind11

python setup.py build_ext -i
```

In general, the Ninja generator is better than the "Visual Studio 16 2019" generator, and there is more information in the Microsoft SEAL official [illustrate](https://github.com/microsoft/SEAL#building-microsoft-seal-manually).
Microsoft SEAL official [docs](https://github.com/microsoft/SEAL#building-microsoft-seal-manually).


* #### Docker
* ### Docker

requires: [Docker](https://www.docker.com/)

To build source code into a docker image (from this directory):
Expand All @@ -80,9 +93,11 @@ This is a python binding for the Microsoft SEAL library.
docker run -it huelse/seal
```



## Note

* #### Serialize
* ### Serialize

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

Expand All @@ -93,34 +108,12 @@ This is a python binding for the Microsoft SEAL library.
load_cipher.load(context, 'cipher') # work if the context is valid.
```

Support type: `Encryptionparams, Ciphertext, Plaintext, SecretKey, Publickey, Relinkeys, Galoiskeys`
Supported classes: `EncryptionParameters, Ciphertext, Plaintext, SecretKey, PublicKey, RelinKeys, GaloisKeys`

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

```shell
# 1. Modify the serializable object's header file in SEAL and switch the wrapper.
python helper.py
* ### Other

# 2. Rebuild the SEAL lib like above
cmake --build build

# 3. Run the setup.py
python setup.py build_ext -i
```

And serialize the data object like this:

```python
import pickle

cipher.set_parms(parms) # necessary
cipher_dump = pickle.dumps(cipher)
cipher_load = pickle.loads(cipher_dump)
```

* #### 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](https://github.com/Huelse/SEAL-Python/issues) please.
There are a lot of changes in the latest SEAL lib, we try to make the API in python can be used easier, but it may remain some problems unknown, if any problems or bugs, report [issues](https://github.com/Huelse/SEAL-Python/issues).

Email: [topmaxz@protonmail.com](mailto:topmaxz@protonmail.com?subject=Github-SEAL-Python-Issues)

Expand Down Expand Up @@ -148,9 +141,14 @@ This is a python binding for the Microsoft SEAL library.

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

5. Windows Error LNK2001, RuntimeLibrary and MT_StaticRelease mismatch

Only `x64` is supported, Choose `x64 Native Tools Command Prompt for VS`.



## Contributing

* Professor: [Dr. Chen](https://zhigang-chen.github.io/)

* [Contributors](https://github.com/Huelse/SEAL-Python/graphs/contributors)
2 changes: 1 addition & 1 deletion SEAL
Submodule SEAL updated 125 files
113 changes: 113 additions & 0 deletions examples/4_bgv_basics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
from seal import *
import numpy as np

def print_vector(vector):
print('[ ', end='')
for i in range(0, 8):
print(vector[i], end=', ')
print('... ]')


def example_bgv_basics():
parms = EncryptionParameters (scheme_type.bgv)
poly_modulus_degree = 8192
parms.set_poly_modulus_degree(poly_modulus_degree)
parms.set_coeff_modulus(CoeffModulus.BFVDefault(poly_modulus_degree))
parms.set_plain_modulus(PlainModulus.Batching(poly_modulus_degree, 20))
context = SEALContext(parms)

keygen = KeyGenerator(context)
secret_key = keygen.secret_key()
public_key = keygen.create_public_key()
relin_keys = keygen.create_relin_keys()

encryptor = Encryptor(context, public_key)
evaluator = Evaluator(context)
decryptor = Decryptor(context, secret_key)

batch_encoder = BatchEncoder(context)
slot_count = batch_encoder.slot_count()
row_size = slot_count / 2
print(f'Plaintext matrix row size: {row_size}')

pod_matrix = [0] * slot_count
pod_matrix[0] = 1
pod_matrix[1] = 2
pod_matrix[2] = 3
pod_matrix[3] = 4

x_plain = batch_encoder.encode(pod_matrix)

x_encrypted = encryptor.encrypt(x_plain)
print(f'noise budget in freshly encrypted x: {decryptor.invariant_noise_budget(x_encrypted)}')
print('-'*50)

x_squared = evaluator.square(x_encrypted)
print(f'size of x_squared: {x_squared.size()}')
evaluator.relinearize_inplace(x_squared, relin_keys)
print(f'size of x_squared (after relinearization): {x_squared.size()}')
print(f'noise budget in x_squared: {decryptor.invariant_noise_budget(x_squared)} bits')
decrypted_result = decryptor.decrypt(x_squared)
pod_result = batch_encoder.decode(decrypted_result)
print_vector(pod_result)
print('-'*50)

x_4th = evaluator.square(x_squared)
print(f'size of x_4th: {x_4th.size()}')
evaluator.relinearize_inplace(x_4th, relin_keys)
print(f'size of x_4th (after relinearization): { x_4th.size()}')
print(f'noise budget in x_4th: {decryptor.invariant_noise_budget(x_4th)} bits')
decrypted_result = decryptor.decrypt(x_4th)
pod_result = batch_encoder.decode(decrypted_result)
print_vector(pod_result)
print('-'*50)

x_8th = evaluator.square(x_4th)
print(f'size of x_8th: {x_8th.size()}')
evaluator.relinearize_inplace(x_8th, relin_keys)
print(f'size of x_8th (after relinearization): { x_8th.size()}')
print(f'noise budget in x_8th: {decryptor.invariant_noise_budget(x_8th)} bits')
decrypted_result = decryptor.decrypt(x_8th)
pod_result = batch_encoder.decode(decrypted_result)
print_vector(pod_result)
print('run out of noise budget')
print('-'*100)

x_encrypted = encryptor.encrypt(x_plain)
print(f'noise budget in freshly encrypted x: {decryptor.invariant_noise_budget(x_encrypted)}')
print('-'*50)

x_squared = evaluator.square(x_encrypted)
print(f'size of x_squared: {x_squared.size()}')
evaluator.relinearize_inplace(x_squared, relin_keys)
evaluator.mod_switch_to_next_inplace(x_squared)
print(f'noise budget in x_squared (with modulus switching): {decryptor.invariant_noise_budget(x_squared)} bits')
decrypted_result = decryptor.decrypt(x_squared)
pod_result = batch_encoder.decode(decrypted_result)
print_vector(pod_result)
print('-'*50)

x_4th = evaluator.square(x_squared)
print(f'size of x_4th: {x_4th.size()}')
evaluator.relinearize_inplace(x_4th, relin_keys)
evaluator.mod_switch_to_next_inplace(x_4th)
print(f'size of x_4th (after relinearization): { x_4th.size()}')
print(f'noise budget in x_4th (with modulus switching): {decryptor.invariant_noise_budget(x_4th)} bits')
decrypted_result = decryptor.decrypt(x_4th)
pod_result = batch_encoder.decode(decrypted_result)
print_vector(pod_result)
print('-'*50)

x_8th = evaluator.square(x_4th)
print(f'size of x_8th: {x_8th.size()}')
evaluator.relinearize_inplace(x_8th, relin_keys)
evaluator.mod_switch_to_next_inplace(x_8th)
print(f'size of x_8th (after relinearization): { x_8th.size()}')
print(f'noise budget in x_8th (with modulus switching): {decryptor.invariant_noise_budget(x_8th)} bits')
decrypted_result = decryptor.decrypt(x_8th)
pod_result = batch_encoder.decode(decrypted_result)
print_vector(pod_result)


if __name__ == "__main__":
example_bgv_basics()
51 changes: 0 additions & 51 deletions helper.py

This file was deleted.

9 changes: 9 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

[build-system]
requires = [
"setuptools>=42",
"wheel",
"pybind11>=2.9.1",
]

build-backend = "setuptools.build_meta"
1 change: 0 additions & 1 deletion requirements.txt

This file was deleted.

Loading