Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit a2e2f82

Browse files
author
Todd Stavish
committed
port BugUInt, ChooserEvaluator, and EncryptionParams to python
1 parent 2a9a459 commit a2e2f82

File tree

5 files changed

+89
-24
lines changed

5 files changed

+89
-24
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
This is an unofficial fork of the Microsoft SEAL homomorphic encryption library demonstrating a python wrapper proof of concept that is dockerized
1+
This repository is a proof of concept for adding a Python wrapper to the (Simple Encrypted Arithmetic Library (SEAL))[https://sealcrypto.org/], a homomorphic encryption library, developed by researchers in the Cryptography Research Group at Microsoft Research. Currently, this uses the version 2.2 codebase and dockerizes the library build (including a shared runtime), C++ example build, and Python wrapper build. The Python wrapper is using pybind11.
22

33
Original home: https://www.microsoft.com/en-us/research/project/simple-encrypted-arithmetic-library-seal-2/
4-
5-
This repository is a proof of concept for adding a Python wrapper to the (Simple Encrypted Arithmetic Library (SEAL))[https://sealcrypto.org/], a homomorphic encryption library, developed by researchers in the Cryptography Research Group at Microsoft Research. Currently, this uses the version 2.2 codebase and dockerizes the library build (including a shared runtime), C++ example build, and Python wrapper build. The Python wrapper is using pybind11.

SEALPython/SEALWrapper.cpp

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

SEALPython/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
ext_modules = [
99
Extension(
1010
'seal',
11-
['SEALWrapper.cpp'],
11+
['wrapper.cpp'],
1212
include_dirs=['/usr/include/python3.5', 'pybind11/include', '/SEAL/SEAL'],
1313
language='c++',
1414
extra_compile_args = cpp_args,

SEALPython/wrapper.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <pybind11/pybind11.h>
2+
#include <pybind11/stl.h>
3+
#include "biguint.h"
4+
#include "chooser.h"
5+
#include "encryptionparams.h"
6+
7+
namespace py = pybind11;
8+
9+
using namespace pybind11::literals;
10+
using namespace seal;
11+
using namespace std;
12+
13+
// http://pybind11.readthedocs.io/en/stable/classes.html
14+
15+
PYBIND11_MODULE(seal, m) {
16+
17+
py::class_<BigUInt>(m, "BigUInt")
18+
.def(py::init<>())
19+
.def("to_double", &BigUInt::to_double,
20+
"Returns the BigUInt value as a double. Note that precision may be lost during the conversion.");
21+
22+
py::class_<ChooserEvaluator>(m, "ChooserEvaluator")
23+
.def_static("default_parameter_options", &ChooserEvaluator::default_parameter_options,
24+
"Retrieve default parameter options");
25+
26+
py::class_<EncryptionParameters>(m, "EncryptionParameters")
27+
.def(py::init<>())
28+
.def(py::init<const MemoryPoolHandle &>())
29+
.def("set_coeff_modulus",
30+
(void (EncryptionParameters::*)(const BigUInt &)) &EncryptionParameters::set_coeff_modulus,
31+
"Set coefficient modulus parameter")
32+
.def("set_coeff_modulus",
33+
(void (EncryptionParameters::*)(std::uint64_t)) &EncryptionParameters::set_coeff_modulus,
34+
"Set coefficient modulus parameter")
35+
.def("set_coeff_modulus",
36+
(void (EncryptionParameters::*)(const std::string &)) &EncryptionParameters::set_coeff_modulus,
37+
"Set coefficient modulus parameter")
38+
.def("set_poly_modulus",
39+
(void (EncryptionParameters::*)(const std::string &)) &EncryptionParameters::set_poly_modulus,
40+
"Set polynomial modulus parameter");
41+
}

SEALPythonExamples/examples.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from seal import EncryptionParameters
1+
from seal import EncryptionParameters, ChooserEvaluator
22

33
def example_basics():
44
"""
@@ -19,6 +19,51 @@ def example_basics():
1919
# We recommend using polynomials of degree at least 1024.
2020
parms.set_poly_modulus('1x^2048 + 1')
2121

22+
# Next we choose the coefficient modulus. SEAL comes with default values for
23+
# the coefficient modulus for some of the most reasonable choices of
24+
# poly_modulus. They are as follows:
25+
#
26+
# /----------------------------------------------------------------------\
27+
# | poly_modulus | default coeff_modulus | security |
28+
# | -------------|--------------------------------------------|----------|
29+
# | 1x^2048 + 1 | 2^60 - 2^14 + 1 (60 bits) | 119 bit |
30+
# | 1x^4096 + 1 | 2^116 - 2^18 + 1 (116 bits) | 122 bit |
31+
# | 1x^8192 + 1 | 2^226 - 2^26 + 1 (226 bits) | 124 bit |
32+
# | 1x^16384 + 1 | 2^435 - 2^33 + 1 (435 bits) | 130 bit |
33+
# | 1x^32768 + 1 | 2^889 - 2^54 - 2^53 - 2^52 + 1 (889 bits) | 127 bit |
34+
# \----------------------------------------------------------------------/
35+
#
36+
# These can be conveniently accessed using
37+
# ChooserEvaluator::default_parameter_options(), which returns the above
38+
# list of options as an std::map, keyed by the degree of the polynomial
39+
# modulus. The security levels are estimated based on
40+
# https://eprint.iacr.org/2015/046 and https://eprint.iacr.org/2017/047. We
41+
# strongly recommend that the user consult an expert in the security of
42+
# RLWE-based cryptography to estimate the security of a particular choice
43+
# of parameters.
44+
#
45+
# The user can also easily choose their custom coefficient modulus. For best
46+
# performance, it should be a prime of the form 2^A - B, where B is
47+
# congruent to 1 modulo 2*degree(poly_modulus), and as small as possible.
48+
# Roughly speaking, When the rest of the parameters are held fixed,
49+
# increasing coeff_modulus decreases the security level. Thus we would not
50+
# recommend using a value for coeff_modulus much larger than those listed
51+
# above (the defaults). In general, we highly recommend the user to consult
52+
# with an expert in the security of RLWE-based cryptography when selecting
53+
# their parameters to ensure an appropriate level of security.
54+
#
55+
# The size of coeff_modulus affects the total noise budget that a freshly
56+
# encrypted ciphertext has. More precisely, every ciphertext starts with a
57+
# certain amount of noise budget, which is consumed in homomorphic
58+
# operations - in particular in multiplication. Once the noise budget
59+
# reaches 0, the ciphertext becomes impossible to decrypt. The total noise
60+
# budget in a freshly encrypted ciphertext is very roughly given by
61+
# log2(coeff_modulus/plain_modulus), so increasing coeff_modulus will allow
62+
# the user to perform more homomorphic operations on the ciphertexts without
63+
# corrupting them. However, we must again warn that increasing
64+
# coeff_modulus has a strong negative effect on the security level.
65+
parms.set_coeff_modulus(ChooserEvaluator.default_parameter_options()[2048]);
66+
2267
def main():
2368
# Example: Basics
2469
example_basics()

0 commit comments

Comments
 (0)