Skip to content

Commit e9390a3

Browse files
authored
Merge pull request #88 from Huelse/main
Sync main
2 parents 453f62f + 2248348 commit e9390a3

File tree

12 files changed

+204
-990
lines changed

12 files changed

+204
-990
lines changed

.gitattributes

Lines changed: 0 additions & 1 deletion
This file was deleted.

README.md

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ This is a python binding for the Microsoft SEAL library.
1919

2020

2121
## Build
22-
* #### Linux
23-
Clang++ (>= 5.0) or GNU G++ (>= 6.0), CMake (>= 3.12)
22+
23+
* ### Linux
24+
25+
Recommend: Clang++ (>= 10.0) or GNU G++ (>= 9.4), CMake (>= 3.16)
2426

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

33-
# Numpy is essential
34-
pip3 install -r requirements.txt
35+
# Install dependencies
36+
pip3 install numpy pybind11
3537

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

4143
# Build the SEAL lib
4244
cd SEAL
43-
cmake -S . -B build -DSEAL_USE_MSGSL=OFF -DSEAL_USE_ZLIB=OFF -DSEAL_USE_ZSTD=OFF
45+
cmake -S . -B build -DSEAL_USE_MSGSL=OFF -DSEAL_USE_ZLIB=OFF
4446
cmake --build build
4547
cd ..
4648

4749
# Run the setup.py
4850
python3 setup.py build_ext -i
51+
52+
# Test
53+
cp seal.*.so examples
54+
python3 4_bgv_basics.py
4955
```
50-
51-
CMake Options: `-DSEAL_BUILD_EXAMPLES=ON` and [more](https://github.com/microsoft/SEAL#basic-cmake-options)
5256

53-
* #### Windows
57+
Build examples (after `cmake -S . -B`): `-DSEAL_BUILD_EXAMPLES=ON`
58+
59+
Zstandard compression off: `-DSEAL_USE_ZSTD=OFF`
60+
61+
[More cmake options](https://github.com/microsoft/SEAL#basic-cmake-options)
62+
63+
64+
* ### Windows
5465

55-
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.
66+
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.
5667

5768
```shell
5869
# Same as above
59-
# Build the SEAL library
60-
cmake -S . -B build -G Ninja -DSEAL_USE_MSGSL=OFF -DSEAL_USE_ZLIB=OFF -DSEAL_USE_ZSTD=OFF
70+
# Run in "x64 Native Tools Command Prompt for VS" command prompt
71+
cmake -S . -B build -G Ninja -DSEAL_USE_MSGSL=OFF -DSEAL_USE_ZLIB=OFF
6172
cmake --build build
6273

63-
# Run the setup.py
74+
pip install numpy pybind11
75+
6476
python setup.py build_ext -i
6577
```
6678

67-
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).
79+
Microsoft SEAL official [docs](https://github.com/microsoft/SEAL#building-microsoft-seal-manually).
6880

6981

70-
* #### Docker
82+
* ### Docker
83+
7184
requires: [Docker](https://www.docker.com/)
7285

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

96+
97+
8398
## Note
8499

85-
* #### Serialize
100+
* ### Serialize
86101

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

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

96-
Support type: `Encryptionparams, Ciphertext, Plaintext, SecretKey, Publickey, Relinkeys, Galoiskeys`
111+
Supported classes: `EncryptionParameters, Ciphertext, Plaintext, SecretKey, PublicKey, RelinKeys, GaloisKeys`
97112

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

100-
```shell
101-
# 1. Modify the serializable object's header file in SEAL and switch the wrapper.
102-
python helper.py
114+
* ### Other
103115

104-
# 2. Rebuild the SEAL lib like above
105-
cmake --build build
106-
107-
# 3. Run the setup.py
108-
python setup.py build_ext -i
109-
```
110-
111-
And serialize the data object like this:
112-
113-
```python
114-
import pickle
115-
116-
cipher.set_parms(parms) # necessary
117-
cipher_dump = pickle.dumps(cipher)
118-
cipher_load = pickle.loads(cipher_dump)
119-
```
120-
121-
* #### Other
122-
123-
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.
116+
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).
124117

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

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

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

144+
5. Windows Error LNK2001, RuntimeLibrary and MT_StaticRelease mismatch
145+
146+
Only `x64` is supported, Choose `x64 Native Tools Command Prompt for VS`.
147+
151148

152149

153150
## Contributing
151+
154152
* Professor: [Dr. Chen](https://zhigang-chen.github.io/)
155153

156154
* [Contributors](https://github.com/Huelse/SEAL-Python/graphs/contributors)

SEAL

Submodule SEAL updated 125 files

examples/4_bgv_basics.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
from seal import *
2+
import numpy as np
3+
4+
def print_vector(vector):
5+
print('[ ', end='')
6+
for i in range(0, 8):
7+
print(vector[i], end=', ')
8+
print('... ]')
9+
10+
11+
def example_bgv_basics():
12+
parms = EncryptionParameters (scheme_type.bgv)
13+
poly_modulus_degree = 8192
14+
parms.set_poly_modulus_degree(poly_modulus_degree)
15+
parms.set_coeff_modulus(CoeffModulus.BFVDefault(poly_modulus_degree))
16+
parms.set_plain_modulus(PlainModulus.Batching(poly_modulus_degree, 20))
17+
context = SEALContext(parms)
18+
19+
keygen = KeyGenerator(context)
20+
secret_key = keygen.secret_key()
21+
public_key = keygen.create_public_key()
22+
relin_keys = keygen.create_relin_keys()
23+
24+
encryptor = Encryptor(context, public_key)
25+
evaluator = Evaluator(context)
26+
decryptor = Decryptor(context, secret_key)
27+
28+
batch_encoder = BatchEncoder(context)
29+
slot_count = batch_encoder.slot_count()
30+
row_size = slot_count / 2
31+
print(f'Plaintext matrix row size: {row_size}')
32+
33+
pod_matrix = [0] * slot_count
34+
pod_matrix[0] = 1
35+
pod_matrix[1] = 2
36+
pod_matrix[2] = 3
37+
pod_matrix[3] = 4
38+
39+
x_plain = batch_encoder.encode(pod_matrix)
40+
41+
x_encrypted = encryptor.encrypt(x_plain)
42+
print(f'noise budget in freshly encrypted x: {decryptor.invariant_noise_budget(x_encrypted)}')
43+
print('-'*50)
44+
45+
x_squared = evaluator.square(x_encrypted)
46+
print(f'size of x_squared: {x_squared.size()}')
47+
evaluator.relinearize_inplace(x_squared, relin_keys)
48+
print(f'size of x_squared (after relinearization): {x_squared.size()}')
49+
print(f'noise budget in x_squared: {decryptor.invariant_noise_budget(x_squared)} bits')
50+
decrypted_result = decryptor.decrypt(x_squared)
51+
pod_result = batch_encoder.decode(decrypted_result)
52+
print_vector(pod_result)
53+
print('-'*50)
54+
55+
x_4th = evaluator.square(x_squared)
56+
print(f'size of x_4th: {x_4th.size()}')
57+
evaluator.relinearize_inplace(x_4th, relin_keys)
58+
print(f'size of x_4th (after relinearization): { x_4th.size()}')
59+
print(f'noise budget in x_4th: {decryptor.invariant_noise_budget(x_4th)} bits')
60+
decrypted_result = decryptor.decrypt(x_4th)
61+
pod_result = batch_encoder.decode(decrypted_result)
62+
print_vector(pod_result)
63+
print('-'*50)
64+
65+
x_8th = evaluator.square(x_4th)
66+
print(f'size of x_8th: {x_8th.size()}')
67+
evaluator.relinearize_inplace(x_8th, relin_keys)
68+
print(f'size of x_8th (after relinearization): { x_8th.size()}')
69+
print(f'noise budget in x_8th: {decryptor.invariant_noise_budget(x_8th)} bits')
70+
decrypted_result = decryptor.decrypt(x_8th)
71+
pod_result = batch_encoder.decode(decrypted_result)
72+
print_vector(pod_result)
73+
print('run out of noise budget')
74+
print('-'*100)
75+
76+
x_encrypted = encryptor.encrypt(x_plain)
77+
print(f'noise budget in freshly encrypted x: {decryptor.invariant_noise_budget(x_encrypted)}')
78+
print('-'*50)
79+
80+
x_squared = evaluator.square(x_encrypted)
81+
print(f'size of x_squared: {x_squared.size()}')
82+
evaluator.relinearize_inplace(x_squared, relin_keys)
83+
evaluator.mod_switch_to_next_inplace(x_squared)
84+
print(f'noise budget in x_squared (with modulus switching): {decryptor.invariant_noise_budget(x_squared)} bits')
85+
decrypted_result = decryptor.decrypt(x_squared)
86+
pod_result = batch_encoder.decode(decrypted_result)
87+
print_vector(pod_result)
88+
print('-'*50)
89+
90+
x_4th = evaluator.square(x_squared)
91+
print(f'size of x_4th: {x_4th.size()}')
92+
evaluator.relinearize_inplace(x_4th, relin_keys)
93+
evaluator.mod_switch_to_next_inplace(x_4th)
94+
print(f'size of x_4th (after relinearization): { x_4th.size()}')
95+
print(f'noise budget in x_4th (with modulus switching): {decryptor.invariant_noise_budget(x_4th)} bits')
96+
decrypted_result = decryptor.decrypt(x_4th)
97+
pod_result = batch_encoder.decode(decrypted_result)
98+
print_vector(pod_result)
99+
print('-'*50)
100+
101+
x_8th = evaluator.square(x_4th)
102+
print(f'size of x_8th: {x_8th.size()}')
103+
evaluator.relinearize_inplace(x_8th, relin_keys)
104+
evaluator.mod_switch_to_next_inplace(x_8th)
105+
print(f'size of x_8th (after relinearization): { x_8th.size()}')
106+
print(f'noise budget in x_8th (with modulus switching): {decryptor.invariant_noise_budget(x_8th)} bits')
107+
decrypted_result = decryptor.decrypt(x_8th)
108+
pod_result = batch_encoder.decode(decrypted_result)
109+
print_vector(pod_result)
110+
111+
112+
if __name__ == "__main__":
113+
example_bgv_basics()

helper.py

Lines changed: 0 additions & 51 deletions
This file was deleted.

pyproject.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
[build-system]
3+
requires = [
4+
"setuptools>=42",
5+
"wheel",
6+
"pybind11>=2.9.1",
7+
]
8+
9+
build-backend = "setuptools.build_meta"

requirements.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)