TFHE-rs
WebsiteLibrariesProduct & ServicesDevelopersSupport
1.0
1.0
  • Welcome to TFHE-rs
  • Get Started
    • What is TFHE-rs?
    • Installation
    • Quick start
    • Benchmarks
      • CPU Benchmarks
        • Integer
        • Programmable bootstrapping
      • GPU Benchmarks
        • Integer
        • Programmable bootstrapping
      • Zero-knowledge proof benchmarks
    • Security and cryptography
  • FHE Computation
    • Types
      • Integer
      • Strings
      • Array
    • Operations
      • Arithmetic operations
      • Bitwise operations
      • Comparison operations
      • Min/Max operations
      • Ternary conditional operations
      • Casting operations
      • Boolean Operations
      • String Operations
    • Core workflow
      • Configuration and key generation
      • Server key
      • Encryption
      • Decryption
      • Parameters
    • Data handling
      • Compressing ciphertexts/keys
      • Serialization/deserialization
      • Data versioning
    • Advanced features
      • Encrypted pseudo random values
      • Overflow detection
      • Public key encryption
      • Trivial ciphertexts
      • Zero-knowledge proofs
      • Multi-threading with Rayon crate
    • Tooling
      • PBS statistics
      • Generic trait bounds
      • Debugging
  • Configuration
    • Advanced Rust setup
    • GPU acceleration
    • Parallelized PBS
  • Integration
    • JS on WASM API
    • High-level API in C
  • Tutorials
    • Homomorphic parity bit
    • Homomorphic case changing on Ascii string
    • SHA256 with Boolean API
    • All tutorials
  • References
    • API references
    • Fine-grained APIs
      • Quick start
      • Boolean
        • Operations
        • Cryptographic parameters
        • Serialization/Deserialization
      • Shortint
        • Operations
        • Cryptographic parameters
        • Serialization/Deserialization
      • Integer
        • Operations
        • Cryptographic parameters
        • Serialization/Deserialization
    • Core crypto API
      • Quick start
      • Tutorial
  • Explanations
    • TFHE deep dive
  • Developers
    • Contributing
    • Release note
    • Feature request
    • Bug report
Powered by GitBook

Libraries

  • TFHE-rs
  • Concrete
  • Concrete ML
  • fhEVM

Developers

  • Blog
  • Documentation
  • Github
  • FHE resources

Company

  • About
  • Introduction to FHE
  • Media
  • Careers
On this page
  • Setting up a Rust project
  • Using TFHE-rs and its APIs

Was this helpful?

Export as PDF
  1. Get Started

Quick start

PreviousInstallationNextBenchmarks

Last updated 2 months ago

Was this helpful?

This document explains the basic steps of using the high-level API of TFHE-rs.

Setting up a Rust project

If you already know how to set up a Rust project, feel free to go directly to the next .

First, install the Rust programming language tools. Visit https://rustup.rs/ and follow the instructions. For alternative installation methods, refer to the .

After installing Rust, you can call the build and package manager Cargo:

$ cargo --version
cargo 1.81.0 (2dbb1af80 2024-08-20)

Your version may differ depending on when you installed Rust. To update your installation, invoke rustup update.

Now you can invoke Cargo and create a new default Rust project:

$ cargo new tfhe-example
    Creating binary (application) `tfhe-example` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

This will create a tfhe-example directory and populate it with the following:

$ tree tfhe-example/
tfhe-example/
├── Cargo.toml
└── src
    └── main.rs

1 directory, 2 files

You now have a minimal Rust project.

In the next section, we'll explain how to add TFHE-rs as a dependency to the project and start using it to perform FHE computations.

Using TFHE-rs and its APIs

To use TFHE-rs, you need to add it as a dependency to tfhe-example.

The Cargo.toml file is located at the root of the project. Initially, the file is minimal and doesn't contain any dependencies:

[package]
name = "tfhe-example"
version = "0.1.0"
edition = "2021"

[dependencies]

Then add the following configuration to include TFHE-rs:

tfhe = { version = "~1.0.1", features = ["integer"] }

Your updated Cargo.toml file should look like this:

[package]
name = "tfhe-example"
version = "0.1.0"
edition = "2021"

[dependencies]
tfhe = { version = "~1.0.1", features = ["integer"] }

Now that the project has TFHE-rs as a dependency here are the detailed steps to use its high-level API:

  1. Import the TFHE-rs prelude with the following Rust code: use tfhe::prelude::*;

This example demonstrates the basic workflow combining the client and server parts:

use tfhe::{ConfigBuilder, generate_keys, set_server_key, FheUint8};
use tfhe::prelude::*;

fn main() {
    let config = ConfigBuilder::default().build();

    // Client-side
    let (client_key, server_key) = generate_keys(config);

    let clear_a = 27u8;
    let clear_b = 128u8;

    let a = FheUint8::encrypt(clear_a, &client_key);
    let b = FheUint8::encrypt(clear_b, &client_key);

    //Server-side
    set_server_key(server_key);
    let result = a + b;

    //Client-side
    let decrypted_result: u8 = result.decrypt(&client_key);

    let clear_result = clear_a + clear_b;

    assert_eq!(decrypted_result, clear_result);
}

If you are on a different platform please refer to the for configuration options of other supported platforms.

Client-side:

Client-side:

Server-side:

Server-side:

Client-side:

You can learn more about homomorphic types and associated compilation features in the

installation documentation
configure and generate keys
encrypt data
set the server key
compute over encrypted data
decrypt data
configuration documentation.
official Rust installation page
section