๐ Documentation | ๐ Community support | ๐ FHE resources by Zama
TFHE-rs is a pure Rust implementation of TFHE for boolean and integer arithmetics over encrypted data.
It includes:
- a Rust API
- a C API
- and a client-side WASM API
TFHE-rs is designed for developers and researchers who want full control over
what they can do with TFHE, while not having to worry about the low-level
implementation. The goal is to have a stable, simple, high-performance, and
production-ready library for all the advanced features of TFHE.
- Low-level cryptographic library that implements Zamaโs variant of TFHE, including programmable bootstrapping
- Implementation of the original TFHE boolean API that can be used as a drop-in replacement for other TFHE libraries
- Short integer API that enables exact, unbounded FHE integer arithmetics with up to 8 bits of message space
- Size-efficient public key encryption
- Ciphertext and server key compression for efficient data transfer
- Full Rust API, C bindings to the Rust High-Level API, and client-side Javascript API using WASM.
Learn more about TFHE-rs features in the documentation.
To use the latest version of TFHE-rs
in your project, you first need to add it as a dependency in your Cargo.toml
:
- For x86_64-based machines running Unix-like OSes:
tfhe = { version = "*", features = ["boolean", "shortint", "integer", "x86_64-unix"] }
- For Apple Silicon or aarch64-based machines running Unix-like OSes:
tfhe = { version = "*", features = ["boolean", "shortint", "integer", "aarch64-unix"] }
- For x86_64-based machines with the
rdseed instruction
running Windows:
tfhe = { version = "*", features = ["boolean", "shortint", "integer", "x86_64"] }
Note
Note: You need to use a Rust version >= 1.73 to compile TFHE-rs.
Note
Note: aarch64-based machines are not yet supported for Windows as it's currently missing an entropy source to be able to seed the CSPRNGs used in TFHE-rs.
Here is a full example:
use tfhe::prelude::*;
use tfhe::{generate_keys, set_server_key, ConfigBuilder, FheUint32, FheUint8};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Basic configuration to use homomorphic integers
let config = ConfigBuilder::default().build();
// Key generation
let (client_key, server_keys) = generate_keys(config);
let clear_a = 1344u32;
let clear_b = 5u32;
let clear_c = 7u8;
// Encrypting the input data using the (private) client_key
// FheUint32: Encrypted equivalent to u32
let mut encrypted_a = FheUint32::try_encrypt(clear_a, &client_key)?;
let encrypted_b = FheUint32::try_encrypt(clear_b, &client_key)?;
// FheUint8: Encrypted equivalent to u8
let encrypted_c = FheUint8::try_encrypt(clear_c, &client_key)?;
// On the server side:
set_server_key(server_keys);
// Clear equivalent computations: 1344 * 5 = 6720
let encrypted_res_mul = &encrypted_a * &encrypted_b;
// Clear equivalent computations: 1344 >> 5 = 42
encrypted_a = &encrypted_res_mul >> &encrypted_b;
// Clear equivalent computations: let casted_a = a as u8;
let casted_a: FheUint8 = encrypted_a.cast_into();
// Clear equivalent computations: min(42, 7) = 7
let encrypted_res_min = &casted_a.min(&encrypted_c);
// Operation between clear and encrypted data:
// Clear equivalent computations: 7 & 1 = 1
let encrypted_res = encrypted_res_min & 1_u8;
// Decrypting on the client side:
let clear_res: u8 = encrypted_res.decrypt(&client_key);
assert_eq!(clear_res, 1_u8);
Ok(())
}
To run this code, use the following command:
cargo run --release
Note
Note that when running code that uses TFHE-rs
, it is highly recommended
to run in release mode with cargo's --release
flag to have the best performances possible.
Find an example with more explanations in this part of the documentation
- TFHE Deep Dive - Part I - Ciphertext types
- TFHE Deep Dive - Part II - Encodings and linear leveled operations
- TFHE Deep Dive - Part III - Key switching and leveled multiplications
- TFHE Deep Dive - Part IV - Programmable Bootstrapping
- Homomorphic Parity Bit
- Homomorphic Case Changing on Ascii String
- Boolean SHA256 with TFHE-rs
- Dark Market with TFHE-rs
- Regular Expression Engine with TFHE-rs
Explore more useful resources in TFHE-rs tutorials and Awesome Zama repo
Full, comprehensive documentation is available here: https://docs.zama.ai/tfhe-rs.
Security estimations are done using the
Lattice Estimator
with red_cost_model = reduction.RC.BDGL16
.
When a new update is published in the Lattice Estimator, we update parameters accordingly.
Mitigation for side-channel attacks has not yet been implemented in TFHE-rs,
and will be released in upcoming versions.
To cite TFHE-rs in academic papers, please use the following entry:
@Misc{TFHE-rs,
title={{TFHE-rs: A Pure Rust Implementation of the TFHE Scheme for Boolean and Integer Arithmetics Over Encrypted Data}},
author={Zama},
year={2022},
note={\url{https://github.com/zama-ai/tfhe-rs}},
}
There are two ways to contribute to TFHE-rs:
- Open issues to report bugs and typos, or to suggest new ideas
- Request to become an official contributor by emailing [email protected].
Becoming an approved contributor involves signing our Contributor License Agreement (CLA). Only approved contributors can send pull requests, so please make sure to get in touch before you do!
This software is distributed under the BSD-3-Clause-Clear license. If you have any questions, please contact us at [email protected].
![](https://private-user-images.githubusercontent.com/157474013/303649450-8da6cf5b-51a0-4c86-9e75-fd0e4a4c64a4.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQxMDM0ODIsIm5iZiI6MTcyNDEwMzE4MiwicGF0aCI6Ii8xNTc0NzQwMTMvMzAzNjQ5NDUwLThkYTZjZjViLTUxYTAtNGM4Ni05ZTc1LWZkMGU0YTRjNjRhNC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwODE5JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDgxOVQyMTMzMDJaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0xZDYyMjEyMTQzMGE1Y2JmNmVmMGQ3YWQzMzU2ZWNjNmY2NmJmMTY1ZjJlNjJjMDBmYTU4MjZkZWNhNzFjZjM2JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.4hdKYkhKZ1tCmyONYQoPHN7tSGlOw6xE1aEwdQB5c8E)
๐ If you find this project helpful or interesting, please consider giving it a star on GitHub! Your support helps to grow the community and motivates further development.