Skip to content

Commit

Permalink
Merge pull request #90 from Huelse/pickle_opt
Browse files Browse the repository at this point in the history
Pickle opt
  • Loading branch information
Huelse authored May 5, 2022
2 parents 9308aab + 85787b9 commit 8aee24b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ build
*.pyc
temp
.idea
*.bin
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ This is a python binding for the Microsoft SEAL library.

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

Expand All @@ -66,16 +67,18 @@ This is a python binding for the Microsoft SEAL library.
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.

```shell
# Same as above
# 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
# Build
pip install numpy pybind11
python setup.py build_ext -i
cp *.so examples
# Test
cp seal.*.pyd examples
cd examples
python 4_bgv_basics.py
```

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

* ### Serialize

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

```python
cipher.save('cipher')
load_cipher = Ciphertext()
load_cipher.load(context, 'cipher') # work if the context is valid.
```
Expand Down
73 changes: 73 additions & 0 deletions examples/7_serialization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from seal import *
import pickle
import time


def get_seal():
parms = EncryptionParameters(scheme_type.ckks)
poly_modulus_degree = 8192
parms.set_poly_modulus_degree(poly_modulus_degree)
parms.set_coeff_modulus(CoeffModulus.Create(poly_modulus_degree, [60, 40, 40, 60]))
scale = 2.0 ** 40

context = SEALContext(parms)
ckks_encoder = CKKSEncoder(context)
slot_count = ckks_encoder.slot_count()

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

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

data = [3.1415926] * slot_count
plain = ckks_encoder.encode(data, scale)
cipher = encryptor.encrypt(plain)

return cipher, context, ckks_encoder, decryptor


def serialization_example():
print('serialization example')
print('-' * 70)
cipher2, context2, ckks_encoder2, decryptor2 = get_seal()
cipher2.save('cipher2.bin')
print('save cipher2 data success')

time.sleep(.5)

cipher3 = Ciphertext()
cipher3.load(context2, 'cipher2.bin')
print('load cipher2 data success')
plain3 = decryptor2.decrypt(cipher3)
data3 = ckks_encoder2.decode(plain3)
print(data3)
print('-' * 70)


def pickle_example():
print('pickle example')
print('-' * 70)
cipher1, context1, ckks_encoder1, decryptor1 = get_seal()
with open('cipher1.bin', 'wb') as f:
pickle.dump(cipher1.to_string(), f)
print('write cipher1 data success')

time.sleep(.5)

with open('cipher1.bin', 'rb') as f:
temp = pickle.load(f)
cipher2 = context1.from_cipher_str(temp)
plain2 = decryptor1.decrypt(cipher2)
data = ckks_encoder1.decode(plain2)
print('read cipher1 data success')
print(data)

print('-' * 70)


if __name__ == "__main__":
serialization_example()
pickle_example()

0 comments on commit 8aee24b

Please sign in to comment.