Skip to content

Sync main #91

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 10 commits into from
May 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: 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
14 changes: 9 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 @@ -63,17 +64,21 @@ This is a python binding for the Microsoft SEAL library.

* ### Windows

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.
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

# 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 @@ -99,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()
128 changes: 108 additions & 20 deletions src/wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,31 @@ PYBIND11_MODULE(seal, m)
.def("coeff_modulus", &EncryptionParameters::coeff_modulus)
.def("plain_modulus", &EncryptionParameters::plain_modulus)
.def("save", [](const EncryptionParameters &parms, std::string &path){
std::ofstream out(path, std::ofstream::binary);
std::ofstream out(path, std::ios::binary);
parms.save(out);
out.close();
})
.def("load", [](EncryptionParameters &parms, std::string &path){
std::ifstream in(path, std::ifstream::binary);
std::ifstream in(path, std::ios::binary);
parms.load(in);
in.close();
});
})
.def(py::pickle(
[](const EncryptionParameters &parms){
std::stringstream out(std::ios::binary | std::ios::out);
parms.save(out);
return py::make_tuple(py::bytes(out.str()));
},
[](py::tuple t){
if (t.size() != 1)
throw std::runtime_error("(Pickle) Invalid input tuple!");
std::string str = t[0].cast<std::string>();
std::stringstream in(std::ios::binary | std::ios::in);
EncryptionParameters parms;
parms.load(in);
return parms;
}
));

// modulus.h
py::enum_<sec_level_type>(m, "sec_level_type")
Expand Down Expand Up @@ -105,7 +121,49 @@ PYBIND11_MODULE(seal, m)
.def("parameters_set", &SEALContext::parameters_set)
.def("first_parms_id", &SEALContext::first_parms_id)
.def("last_parms_id", &SEALContext::last_parms_id)
.def("using_keyswitching", &SEALContext::using_keyswitching);
.def("using_keyswitching", &SEALContext::using_keyswitching)
.def("from_cipher_str", [](const SEALContext &context, const std::string &str){
Ciphertext cipher;
std::stringstream in(std::ios::binary | std::ios::in);
in.str(str);
cipher.load(context, in);
return cipher;
})
.def("from_plain_str", [](const SEALContext &context, const std::string &str){
Plaintext plain;
std::stringstream in(std::ios::binary | std::ios::in);
in.str(str);
plain.load(context, in);
return plain;
})
.def("from_secret_str", [](const SEALContext &context, const std::string &str){
SecretKey secret;
std::stringstream in(std::ios::binary | std::ios::in);
in.str(str);
secret.load(context, in);
return secret;
})
.def("from_public_str", [](const SEALContext &context, const std::string &str){
PublicKey public_;
std::stringstream in(std::ios::binary | std::ios::in);
in.str(str);
public_.load(context, in);
return public_;
})
.def("from_relin_str", [](const SEALContext &context, const std::string &str){
RelinKeys relin;
std::stringstream in(std::ios::binary | std::ios::in);
in.str(str);
relin.load(context, in);
return relin;
})
.def("from_galois_str", [](const SEALContext &context, const std::string &str){
GaloisKeys galois;
std::stringstream in(std::ios::binary | std::ios::in);
in.str(str);
galois.load(context, in);
return galois;
});

// modulus.h
py::class_<Modulus>(m, "Modulus")
Expand Down Expand Up @@ -151,17 +209,22 @@ PYBIND11_MODULE(seal, m)
plain.scale() = scale;
})
.def("save", [](const Plaintext &plain, const std::string &path){
std::ofstream out(path, std::ofstream::binary);
std::ofstream out(path, std::ios::binary);
plain.save(out);
out.close();
})
.def("load", [](Plaintext &plain, const SEALContext &context, const std::string &path){
std::ifstream in(path, std::ifstream::binary);
std::ifstream in(path, std::ios::binary);
plain.load(context, in);
in.close();
})
.def("save_size", [](const Plaintext &plain){
return plain.save_size();
})
.def("to_string", [](const Plaintext &plain){
std::stringstream out(std::ios::binary | std::ios::out);
plain.save(out);
return py::bytes(out.str());
});

// ciphertext.h
Expand All @@ -183,17 +246,22 @@ PYBIND11_MODULE(seal, m)
cipher.scale() = scale;
})
.def("save", [](const Ciphertext &cipher, const std::string &path){
std::ofstream out(path, std::ofstream::binary);
std::ofstream out(path, std::ios::binary);
cipher.save(out);
out.close();
})
.def("load", [](Ciphertext &cipher, const SEALContext &context, const std::string &path){
std::ifstream in(path, std::ifstream::binary);
std::ifstream in(path, std::ios::binary);
cipher.load(context, in);
in.close();
})
.def("save_size", [](const Ciphertext &cipher){
return cipher.save_size();
})
.def("to_string", [](const Ciphertext &cipher){
std::stringstream out(std::ios::binary | std::ios::out);
cipher.save(out);
return py::bytes(out.str());
});

// secretkey.h
Expand All @@ -202,14 +270,19 @@ PYBIND11_MODULE(seal, m)
.def(py::init<const SecretKey &>())
.def("parms_id", py::overload_cast<>(&SecretKey::parms_id, py::const_))
.def("save", [](const SecretKey &sk, const std::string &path){
std::ofstream out(path, std::ofstream::binary);
std::ofstream out(path, std::ios::binary);
sk.save(out);
out.close();
})
.def("load", [](SecretKey &sk, const SEALContext &context, const std::string &path){
std::ifstream in(path, std::ifstream::binary);
std::ifstream in(path, std::ios::binary);
sk.load(context, in);
in.close();
})
.def("to_string", [](const SecretKey &secret){
std::stringstream out(std::ios::binary | std::ios::out);
secret.save(out);
return py::bytes(out.str());
});

// publickey.h
Expand All @@ -218,14 +291,19 @@ PYBIND11_MODULE(seal, m)
.def(py::init<const PublicKey &>())
.def("parms_id", py::overload_cast<>(&PublicKey::parms_id, py::const_))
.def("save", [](const PublicKey &pk, const std::string &path){
std::ofstream out(path, std::ofstream::binary);
std::ofstream out(path, std::ios::binary);
pk.save(out);
out.close();
})
.def("load", [](PublicKey &pk, const SEALContext &context, const std::string &path){
std::ifstream in(path, std::ifstream::binary);
std::ifstream in(path, std::ios::binary);
pk.load(context, in);
in.close();
})
.def("to_string", [](const PublicKey &public_){
std::stringstream out(std::ios::binary | std::ios::out);
public_.save(out);
return py::bytes(out.str());
});

// kswitchkeys.h
Expand All @@ -235,17 +313,17 @@ PYBIND11_MODULE(seal, m)
.def("size", &KSwitchKeys::size)
.def("parms_id", py::overload_cast<>(&KSwitchKeys::parms_id, py::const_))
.def("save", [](const KSwitchKeys &ksk, const std::string &path){
std::ofstream out(path, std::ofstream::binary);
std::ofstream out(path, std::ios::binary);
ksk.save(out);
out.close();
})
.def("load", [](KSwitchKeys &ksk, const SEALContext &context, const std::string &path){
std::ifstream in(path, std::ifstream::binary);
std::ifstream in(path, std::ios::binary);
ksk.load(context, in);
in.close();
});

// relinKeys.h
// relinkeys.h
py::class_<RelinKeys, KSwitchKeys>(m, "RelinKeys")
.def(py::init<>())
.def(py::init<const RelinKeys::KSwitchKeys &>())
Expand All @@ -254,17 +332,22 @@ PYBIND11_MODULE(seal, m)
.def_static("get_index", &RelinKeys::get_index)
.def("has_key", &RelinKeys::has_key)
.def("save", [](const RelinKeys &rk, const std::string &path){
std::ofstream out(path, std::ofstream::binary);
std::ofstream out(path, std::ios::binary);
rk.save(out);
out.close();
})
.def("load", [](RelinKeys &rk, const SEALContext &context, const std::string &path){
std::ifstream in(path, std::ifstream::binary);
std::ifstream in(path, std::ios::binary);
rk.load(context, in);
in.close();
})
.def("to_string", [](const RelinKeys &relin){
std::stringstream out(std::ios::binary | std::ios::out);
relin.save(out);
return py::bytes(out.str());
});

// galoisKeys.h
// galoiskeys.h
py::class_<GaloisKeys, KSwitchKeys>(m, "GaloisKeys")
.def(py::init<>())
.def(py::init<const GaloisKeys::KSwitchKeys &>())
Expand All @@ -273,14 +356,19 @@ PYBIND11_MODULE(seal, m)
.def_static("get_index", &GaloisKeys::get_index)
.def("has_key", &GaloisKeys::has_key)
.def("save", [](const GaloisKeys &gk, const std::string &path){
std::ofstream out(path, std::ofstream::binary);
std::ofstream out(path, std::ios::binary);
gk.save(out);
out.close();
})
.def("load", [](GaloisKeys &gk, const SEALContext &context, const std::string &path){
std::ifstream in(path, std::ifstream::binary);
std::ifstream in(path, std::ios::binary);
gk.load(context, in);
in.close();
})
.def("to_string", [](const GaloisKeys &galois){
std::stringstream out(std::ios::binary | std::ios::out);
galois.save(out);
return py::bytes(out.str());
});

// keygenerator.h
Expand Down