TF SEAL provides a bridge between TensorFlow and the Microsoft SEAL homomorphic encryption library, making it easier than ever to use this library to compute on encrypted data. It currently offers low-level operations for interacting with Microsoft SEAL via TensorFlow with work in progress on a high-level integration into TF Encrypted.
The following demonstrates how the low-level interface can be used to perform a matrix multiplication using homomorphic encryption inside of TensorFlow.
import numpy as np import tensorflow as tf import tf_seal as tfs public_keys, secret_key = tfs.seal_key_gen(gen_relin=True, gen_galois=True) # sample inputs in the form of tf.Tensors a = tf.random.normal(shape=(2, 3), dtype=tf.float32) b = tf.random.normal(shape=(2, 3), dtype=tf.float32) # the plaintext equivalent of our computation c = tf.matmul(a, tf.transpose(b)) # encrypt inputs, yielding tf_seal.Tensors a_encrypted = tfs.convert_to_tensor(a, secret_key, public_keys) b_encrypted = tfs.convert_to_tensor(b, secret_key, public_keys) # perform computation on encrypted data # - note that because of how the data is laid out in memory, # tf_seal.matmul expects the right-hand matrix to be ordered # column-major wise, i.e. already transposed c_encrypted = tfs.matmul(a_encrypted, b_encrypted) with tf.Session() as sess: expected, actual = sess.run([c, c_encrypted]) np.testing.assert_almost_equal(actual, expected, decimal=3)
We are currently working on integrating TF SEAL into TF Encrypted such that privacy-preserving machine learning applications can instead access the library through a high-level interface and take advantage of e.g. the Keras API. This includes adding logic that helps optimize homomorphic encryption for a perticular computation, making use even easier.
conda create -n tfseal python=3.7 -y source activate tfseal
pip install tf-seal
There is currently one example displaying how we can run a simple logistic regression prediction with TF SEAL.
Once TF SEAL is installed we can run the example by simplying running:
conda create -n tfseal-dev python=3.7 -y source activate tfseal-dev
The basic requirements are:
The remaining PyPI packages can then be installed using:
pip install -r requirements-dev.txt
All tests can be run via Bazel with:
The pip package can be built using:
with the resulting wheel file placed in
A custom build of TensorFlow is currently needed to run TF SEAL due to a mismatch between the C++ version used by the official TensorFlow build (C++11) and the one needed by Microsoft SEAL (C++17). A patched version of TensorFlow built with C++17 can be installed as shown below.
wget https://storage.googleapis.com/tf-pips/tf-c++17-support/tf_nightly-1.14.0-cp37-cp37m-linux_x86_64.whl pip install tf_nightly-1.14.0-cp37-cp37m-linux_x86_64.whl
wget https://storage.googleapis.com/tf-pips/tf-c++17-support/tf_nightly-1.14.0-cp37-cp37m-macosx_10_7_x86_64.whl pip install tf_nightly-1.14.0-cp37-cp37m-macosx_10_7_x86_64.whl
conda create -n customtf python=3.7 -y source activate customtf
This requires that Bazel (== 0.26.1) has been installed. The patched version of TensorFlow may then be built using:
git clone https://github.com/tf-encrypted/tf-seal.git cd tf-seal pip install -r requirements-customtf.txt make tensorflow pip install -U tf_nightly-1.14.0-cp37-cp37m-*