Skip to content

Commit 8aee24b

Browse files
authored
Merge pull request #90 from Huelse/pickle_opt
Pickle opt
2 parents 9308aab + 85787b9 commit 8aee24b

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ build
4141
*.pyc
4242
temp
4343
.idea
44+
*.bin

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ This is a python binding for the Microsoft SEAL library.
5151

5252
# Test
5353
cp seal.*.so examples
54+
cd examples
5455
python3 4_bgv_basics.py
5556
```
5657

@@ -66,16 +67,18 @@ This is a python binding for the Microsoft SEAL library.
6667
Visual Studio 2019 or newer is required. x64 support only! 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.
6768

6869
```shell
69-
# Same as above
7070
# Run in "x64 Native Tools Command Prompt for VS" command prompt
7171
cmake -S . -B build -G Ninja -DSEAL_USE_MSGSL=OFF -DSEAL_USE_ZLIB=OFF
7272
cmake --build build
7373

74+
# Build
7475
pip install numpy pybind11
75-
7676
python setup.py build_ext -i
7777

78-
cp *.so examples
78+
# Test
79+
cp seal.*.pyd examples
80+
cd examples
81+
python 4_bgv_basics.py
7982
```
8083

8184
Microsoft SEAL official [docs](https://github.com/microsoft/SEAL#building-microsoft-seal-manually).
@@ -101,11 +104,10 @@ This is a python binding for the Microsoft SEAL library.
101104

102105
* ### Serialize
103106

104-
In most situations, you can use the SEAL's native serialize API to save the data, here is an example:
107+
See more in `examples/7_serialization.py`, here is a simple example:
105108

106109
```python
107110
cipher.save('cipher')
108-
109111
load_cipher = Ciphertext()
110112
load_cipher.load(context, 'cipher') # work if the context is valid.
111113
```

examples/7_serialization.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from seal import *
2+
import pickle
3+
import time
4+
5+
6+
def get_seal():
7+
parms = EncryptionParameters(scheme_type.ckks)
8+
poly_modulus_degree = 8192
9+
parms.set_poly_modulus_degree(poly_modulus_degree)
10+
parms.set_coeff_modulus(CoeffModulus.Create(poly_modulus_degree, [60, 40, 40, 60]))
11+
scale = 2.0 ** 40
12+
13+
context = SEALContext(parms)
14+
ckks_encoder = CKKSEncoder(context)
15+
slot_count = ckks_encoder.slot_count()
16+
17+
keygen = KeyGenerator(context)
18+
public_key = keygen.create_public_key()
19+
secret_key = keygen.secret_key()
20+
21+
encryptor = Encryptor(context, public_key)
22+
# evaluator = Evaluator(context)
23+
decryptor = Decryptor(context, secret_key)
24+
25+
data = [3.1415926] * slot_count
26+
plain = ckks_encoder.encode(data, scale)
27+
cipher = encryptor.encrypt(plain)
28+
29+
return cipher, context, ckks_encoder, decryptor
30+
31+
32+
def serialization_example():
33+
print('serialization example')
34+
print('-' * 70)
35+
cipher2, context2, ckks_encoder2, decryptor2 = get_seal()
36+
cipher2.save('cipher2.bin')
37+
print('save cipher2 data success')
38+
39+
time.sleep(.5)
40+
41+
cipher3 = Ciphertext()
42+
cipher3.load(context2, 'cipher2.bin')
43+
print('load cipher2 data success')
44+
plain3 = decryptor2.decrypt(cipher3)
45+
data3 = ckks_encoder2.decode(plain3)
46+
print(data3)
47+
print('-' * 70)
48+
49+
50+
def pickle_example():
51+
print('pickle example')
52+
print('-' * 70)
53+
cipher1, context1, ckks_encoder1, decryptor1 = get_seal()
54+
with open('cipher1.bin', 'wb') as f:
55+
pickle.dump(cipher1.to_string(), f)
56+
print('write cipher1 data success')
57+
58+
time.sleep(.5)
59+
60+
with open('cipher1.bin', 'rb') as f:
61+
temp = pickle.load(f)
62+
cipher2 = context1.from_cipher_str(temp)
63+
plain2 = decryptor1.decrypt(cipher2)
64+
data = ckks_encoder1.decode(plain2)
65+
print('read cipher1 data success')
66+
print(data)
67+
68+
print('-' * 70)
69+
70+
71+
if __name__ == "__main__":
72+
serialization_example()
73+
pickle_example()

0 commit comments

Comments
 (0)