Skip to main content

TF-Shell: Privacy preserving machine learning with Tensorflow and the SHELL encryption library, built for python 3.10.

Project description

tf-shell

Build Formatting

Train models with strong privacy, even when you can't trust anyone.

tf-shell is a TensorFlow extension that uses Homomorphic Encryption (HE) to train models with centralized label differential privacy (Label DP) guarantees, without requiring a trusted third party.

It's built for the "vertically partitioned" scenario where one party has the features and another party has the labels. This library implements the protocols from the Hadal research paper to securely train a model without the feature-holder ever seeing the plain-text labels.

This is not an officially supported Google product.

Getting Started

pip install tf-shell

See ./examples/ for how to use the library.

The Problem: Centralized DP without a Trusted Party

When training with high privacy requirements (e.g., $\epsilon \le 1$), centralized DP (like DP-SGD) provides much higher model accuracy than local DP (like randomized response). However, centralized DP traditionally requires a trusted third-party "curator" that can see all the data (or at least the secret labels) to compute and noise the gradients. This is a problem when data is vertically partitioned: Party F has the features and wants to train the model. Party L has the secret labels and cannot share them. How can Party F train its model using Party L's labels without a trusted intermediary?

The Solution: HE-based Backpropagation

tf-shell uses Homomorphic Encryption (via Google's SHELL library) to cryptographically "simulate" the trusted curator. The core technical idea is based on the Features-And-Model-vs-Labels (FAML) data partitioning:

  1. Forward Pass (Plaintext): Party F (with features and the model) computes the entire forward pass in plaintext, right up to the final layer's logits.
  2. Encrypted Labels: Party L encrypts its batch of labels using HE and sends the single ciphertext to Party F.
  3. Backward Pass (Encrypted): The gradient of the loss (e.g., CCE with Softmax) is often a simple affine function of the labels (like $\hat{y} - y$). Party F can compute this step homomorphically using its plaintext logits and Party L's encrypted labels.
  4. Model Update: Party F finishes the backpropagation, adds the required DP noise, and updates its model weights.

The result is a model trained with the high utility of centralized DP, but Party F never sees Party L's individual labels.

What's Included?

The library is split into two packages:

  • tf_shell: The base package. It integrates TensorFlow with the SHELL library, providing a ShellTensor type for basic HE-enabled computations.
  • tf_shell_ml: The machine learning library. It implements two different protocols for the encrypted backpropagation step:
    • POSTSCALE: A novel protocol that is highly efficient for models with a low number of output classes (e.g., binary classification).
    • HE-DP-SGD: A more direct HE implementation of backpropagation, which is better suited for models with many output classes.

Building

Build From Source

  1. Install bazel and python3 or use the devcontainer.

  2. Run the tests.

    bazel test //tf_shell/...
    bazel test //tf_shell_ml/...  # Large tests, requires 128GB of memory.
    
  3. Build the code.

    bazel build //:wheel
    bazel run //:wheel_rename
    
  4. (Optional) Install the wheel, e.g. to try out the ./examples/. You may first need to copy the wheel out of the devcontainer's filesystem.

    cp -f bazel-bin/*.whl ./  # Run in devcontainer if using.
    

    Then install.

    pip install --force-reinstall tf_shell-*.whl  # Run in target environment.
    

Note the cpython api is not compatible across minor python versions (e.g. 3.10, 3.11) so the wheel must be rebuilt for each python version.

Code Formatters and Counters

bazel run //:bazel_formatter
bazel run //:python_formatter
bazel run //:clang_formatter
cloc ./ --fullpath --not-match-d='/(bazel-.*|.*\.venv)/'

Update Python Dependencies

Update requirements.in and run the following to update the requirements files for each python version.

for ver in 3_9 3_10 3_11 3_12 3_13; do
  rm requirements_${ver}.txt
  touch requirements_${ver}.txt
  bazel run //:requirements_${ver}.update
done

bazel clean --expunge

If updating the tensorflow dependency, other dependencies may also need to change, e.g. abseil (see MODULE.bazel). This issue usually manifests as a missing symbols error in the tests when trying to import the tensorflow DSO. In this case, c++filt will help to decode the mangled symbol name and nm --defined-only .../libtensorflow_framework.so | grep ... may help find what the symbol changed to, and which dependency is causing the error.

Contributing

See CONTRIBUTING.md for details.

License

Apache 2.0; see LICENSE for details.

Disclaimer

Convolutions on AMD-based platforms may fail due to known limitations of TensorFlow. This will resulting in the following error when running tests:

CPU implementation of Conv3D currently only supports dilated rates of 1.

This project is not an official Google project. It is not supported by Google and Google specifically disclaims all warranties as to its quality, merchantability, or fitness for a particular purpose.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

tf_shell-0.5.0-cp313-cp313-manylinux_2_39_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.39+ x86-64

tf_shell-0.5.0-cp313-cp313-manylinux_2_35_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.35+ x86-64

tf_shell-0.5.0-cp312-cp312-manylinux_2_39_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.39+ x86-64

tf_shell-0.5.0-cp312-cp312-manylinux_2_35_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.35+ x86-64

tf_shell-0.5.0-cp311-cp311-manylinux_2_39_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.39+ x86-64

tf_shell-0.5.0-cp311-cp311-manylinux_2_35_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.35+ x86-64

tf_shell-0.5.0-cp310-cp310-manylinux_2_39_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.39+ x86-64

tf_shell-0.5.0-cp310-cp310-manylinux_2_35_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.35+ x86-64

tf_shell-0.5.0-cp39-cp39-manylinux_2_39_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.39+ x86-64

tf_shell-0.5.0-cp39-cp39-manylinux_2_35_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.35+ x86-64

File details

Details for the file tf_shell-0.5.0-cp313-cp313-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for tf_shell-0.5.0-cp313-cp313-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 7a3b97809a260efbebb46567cd49555f97bff5f4a46c2b04041a4c40b6eee3a0
MD5 7d351d2a292f3d6bc8070f9ab9e59a7f
BLAKE2b-256 b951d0e67fc0e97195bd8c3ea3e690b703e0ab04b4338104120ae2b93351342a

See more details on using hashes here.

Provenance

The following attestation bundles were made for tf_shell-0.5.0-cp313-cp313-manylinux_2_39_x86_64.whl:

Publisher: wheel.yaml on google/tf-shell

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tf_shell-0.5.0-cp313-cp313-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for tf_shell-0.5.0-cp313-cp313-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 4ff315c0a2027558f8d14fce736c491db7a182a104508455d7d1b948a4ff1ce0
MD5 21fddc5056b2806ca7c2c6a9f0c34865
BLAKE2b-256 9760d28214cdd34f0909661f3b2d111e0117e5dfc675c952e220dd679b6df2c7

See more details on using hashes here.

Provenance

The following attestation bundles were made for tf_shell-0.5.0-cp313-cp313-manylinux_2_35_x86_64.whl:

Publisher: wheel.yaml on google/tf-shell

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tf_shell-0.5.0-cp312-cp312-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for tf_shell-0.5.0-cp312-cp312-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 a1722043ed62455a24569e76dc37b3df25f35a1d2febb1e2b591cd8a4e53a62f
MD5 81f1e44aff6a76123b83a9b4ca767112
BLAKE2b-256 c0abdc5cf947455bfbf558d60a37edc7b0887b528f568e30af64be96b5b03273

See more details on using hashes here.

Provenance

The following attestation bundles were made for tf_shell-0.5.0-cp312-cp312-manylinux_2_39_x86_64.whl:

Publisher: wheel.yaml on google/tf-shell

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tf_shell-0.5.0-cp312-cp312-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for tf_shell-0.5.0-cp312-cp312-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 e6c068fbcb4c5961dcee9d94cec84224c2d76bc364f680ce92bdaa1aaf506000
MD5 cacf7dd89c4a5e0075de08c44500120a
BLAKE2b-256 23e1117cd657a311c36f9c75c093c169950316912a2cbc4a7f9509c0ed4a477c

See more details on using hashes here.

Provenance

The following attestation bundles were made for tf_shell-0.5.0-cp312-cp312-manylinux_2_35_x86_64.whl:

Publisher: wheel.yaml on google/tf-shell

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tf_shell-0.5.0-cp311-cp311-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for tf_shell-0.5.0-cp311-cp311-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 b7ea035153742b4bdfb6062530a26fd940432542bd12a233966dc8d0f4200f54
MD5 712a82f51113c2f9a119fa957743056e
BLAKE2b-256 c01e18ef572d4b02ed3f55d4c7b4d8d937d5cf8038e275b70f82228009e81711

See more details on using hashes here.

Provenance

The following attestation bundles were made for tf_shell-0.5.0-cp311-cp311-manylinux_2_39_x86_64.whl:

Publisher: wheel.yaml on google/tf-shell

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tf_shell-0.5.0-cp311-cp311-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for tf_shell-0.5.0-cp311-cp311-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 68935046f074fb938ce41e686ac4fb0fcd9483e4b16d6ea951049203a64a0efb
MD5 48cdf05f7ab425356b3bac84620943e2
BLAKE2b-256 d124fda75e79abe73605ffa58990266e47f4172efadc9b4b8b2eb8ecb08adf69

See more details on using hashes here.

Provenance

The following attestation bundles were made for tf_shell-0.5.0-cp311-cp311-manylinux_2_35_x86_64.whl:

Publisher: wheel.yaml on google/tf-shell

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tf_shell-0.5.0-cp310-cp310-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for tf_shell-0.5.0-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 35df59cda9a9bb08419f3ebb4800bf0181a3d01efd938147a5f0b53de32856b2
MD5 388559648fcdcce78f7a719b78af0a76
BLAKE2b-256 fd3205f13577a331a1baec50d0e864fc45de1e9403b137b541ed911f5202d3e6

See more details on using hashes here.

Provenance

The following attestation bundles were made for tf_shell-0.5.0-cp310-cp310-manylinux_2_39_x86_64.whl:

Publisher: wheel.yaml on google/tf-shell

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tf_shell-0.5.0-cp310-cp310-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for tf_shell-0.5.0-cp310-cp310-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 a7c7bac8fa9ae4ccc605c2d612c7e34717b9e48225bafbf9aff37953dd7481dd
MD5 e8ae7357362a65876ff5dd616e73624e
BLAKE2b-256 e9ba75fb1fc3ddaedadf9ed323e79feb8134c3f022c9cac9156e85108752ad65

See more details on using hashes here.

Provenance

The following attestation bundles were made for tf_shell-0.5.0-cp310-cp310-manylinux_2_35_x86_64.whl:

Publisher: wheel.yaml on google/tf-shell

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tf_shell-0.5.0-cp39-cp39-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for tf_shell-0.5.0-cp39-cp39-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 8e1e65ef39dd867b289e6d15e7ef42bc549fb10ca2cd035117d1f043e01f2267
MD5 dc8797ec42b4fc19ac27061fdd3a5bd3
BLAKE2b-256 4b128e93d17ab818d2d8af3e7f6c493a8d5811c1871e3e30aa25bf4913c55d81

See more details on using hashes here.

Provenance

The following attestation bundles were made for tf_shell-0.5.0-cp39-cp39-manylinux_2_39_x86_64.whl:

Publisher: wheel.yaml on google/tf-shell

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tf_shell-0.5.0-cp39-cp39-manylinux_2_35_x86_64.whl.

File metadata

File hashes

Hashes for tf_shell-0.5.0-cp39-cp39-manylinux_2_35_x86_64.whl
Algorithm Hash digest
SHA256 3183561621db74e2c64317ddf5b5199750f11d1d1807ead74f13ef5b3480ac61
MD5 ccaa80f80771697f002c2544220ddf4b
BLAKE2b-256 d72dc3f2d049e93814f577d52884f9cbd500596209107e690a886f1b4bc52c3a

See more details on using hashes here.

Provenance

The following attestation bundles were made for tf_shell-0.5.0-cp39-cp39-manylinux_2_35_x86_64.whl:

Publisher: wheel.yaml on google/tf-shell

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page