tfhe-rs

repository·main·Indexed 23 days ago

https://github.com/zama-ai/tfhe-rs

A high-performance, pure Rust implementation of the TFHE scheme for boolean and integer arithmetic on encrypted data. It provides a high-level API for developers and low-level control for researchers. The library includes support for GPU acceleration via the tfhe-cuda-backend for NVIDIA GPUs (Compute Capability >= 3.0), and implementations of Trivium and Kreyvium for trans-ciphering.

Tokens
127.2K
Snippets
222
Records
433
Agent score
81%

What's inside tfhe-rs

  1. Overview of TFHE-rs

    main

    TFHE-rs is a pure Rust implementation of the TFHE (Torus Fully Homomorphic Encryption) scheme. It enables performing Boolean and integer arithmetic directly on encrypted data.

    Key capabilities include:

    • Multi-language support: Provides a Rust API, a C API, and a client-side WASM API for JavaScript environments.
    • Hardware Acceleration: Supports accelerated backends for GPUs and HPUs to improve computation performance.
    • Abstraction Levels: Offers various API layers ranging from high-level abstractions (simplifying cryptographic complexities) to low-level core crypto primitives.
  2. Overview of TFHE-CSPRNG

    main

    TFHE-CSPRNG is a fast Cryptographically Secure Pseudorandom Number Generator used within the tfhe library. It implements an AES blockcipher in CTR mode, following the ISO/IEC 18033-4 standard.

    It provides two implementation paths:

    1. Accelerated implementation: Available on x86_64 CPUs when using the aes and sse2 features.
    2. Pure software implementation: Available for use on other platforms.

    It also provides two seeding mechanisms:

    • A seeder utilizing the x86_64 rdseed instruction.
    • A seeder based on the Unix /dev/random device (requires the user to provide a secret).
  3. Introduction to TFHE CUDA backend

    main

    The tfhe-cuda-backend provides GPU acceleration for Zama's variant of TFHE. It implements CUDA/C++ functions to perform homomorphic operations on LWE ciphertexts.

    Key capabilities include:

    • Memory Management: Allocating GPU memory (cuda_malloc, cuda_check_valid_malloc) and copying data between CPU and GPU (cuda_memcpy_async_to_cpu, cuda_memcpy_async_to_gpu).
    • Stream Management: Creating and destroying CUDA streams (cuda_create_stream, cuda_destroy_stream).
    • Device Control: Querying GPU count (cuda_get_number_of_gpus) and synchronizing the device (cuda_synchronize_device).
    • Cryptographic Operations:
      • Classical and multi-bit TFHE programmable bootstrap.
      • Keyswitching.
      • Acceleration for leveled operations.
      • Acceleration for arithmetic over encrypted integers of arbitrary size.
      • Acceleration for integer compression/decompression.
  4. What is TFHE-rs and its main features

    main

    TFHE-rs is a pure Rust implementation of TFHE for boolean and integer arithmetics over encrypted data. It provides a Rust API, a C API, and a client-side WASM API.

    Key Features:

    • Low-level cryptographic library: Implements Zama's variant of TFHE, including programmable bootstrapping.
    • Original TFHE boolean API: Can be used as a drop-in replacement for other TFHE libraries.
    • Short integer API: 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.
    • Multi-language support: Full Rust API, C bindings, and client-side JavaScript via WASM.
  5. Overview of wasm-par-mq

    main

    What is wasm-par-mq?

    wasm-par-mq provides Rayon-like parallel iterators for WebAssembly environments where SharedArrayBuffer is unavailable (e.g., when COOP/COEP headers cannot be set).

    How it works

    Instead of shared memory, it uses Web Workers and message passing to distribute work via message queues. This incurs some serialization overhead but allows for parallel computation in restricted web environments.

    Key Features

    • Parallel Iterators: API similar to Rayon (par_iter(), into_par_iter(), map(), collect_vec()).
    • Universal: Works without specific server-side headers.
    • Type-safe Registration: Uses macros for compile-time function registration.
    • Worker Management: Automatic worker pool with round-robin distribution.
    • Execution Modes: Supports both non-blocking async/await and blocking Sync Mode via a specialized executor.
  6. Understand Zero-knowledge proof benchmarks in TFHE-rs

    main

    TFHE-rs provides performance benchmarks for zero-knowledge proofs (ZKP) used in compact public key encryption. These benchmarks evaluate two primary computation scenarios:

    1. Server-side computation: Proving and verification are performed using the tfhe-rs native executable on high-performance server hardware (e.g., AWS hpc8a.96xlarge with AMD EPYC CPUs).
    2. Client-side computation: Proving is performed on a client machine using a WASM executable running in a Chrome browser to simulate typical user environments (e.g., AWS m6i.4xlarge with Intel Xeon CPUs).

    Benchmarks are categorized by the relationship between proof generation speed and verification speed:

    • Fast proof / slow verify: Optimized for scenarios where the prover (client) is fast, but the verifier (server) is computationally intensive.
    • Slow proof / fast verify: Optimized for scenarios where the prover is slow, but the verifier can quickly validate the proof.

    Performance is measured in terms of latency (time per operation) and throughput (operations per unit of time).

  7. Compare operation latency across CPU, GPU, and HPU

    main

    TFHE-rs benchmarks measure the timing of homomorphic operations over 64-bit encrypted integers across different hardware backends. The benchmarks use a tweaked uniform (TUniform) noise distribution, which is bounded, unlike the standard Gaussian distribution.

    Hardware environments used for the comparison benchmarks:

    • CPU: AWS hpc8a.96xlarge instance (two 96-core AMD EPYC 9R45 CPU @ 2.30GHz, 768GB RAM).
    • GPU: 8xH100 GPU using the multithreaded PBS algorithm.
    • HPU: 1x v80 Alveo board.
    • Zero-Knowledge Proofs: Specific benchmarks are available in zk-proof-benchmarks.md.
  8. View CPU benchmarks for specific TFHE operations

    main

    CPU performance benchmarks for TFHE-rs operations are categorized into the following areas:

    • Integer operations: Performance of homomorphic integer arithmetic.
    • ERC7984: Benchmarks related to the ERC7984 standard.
    • KVStore: Performance of Key-Value Store operations.
    • Programmable Bootstrapping: Benchmarks for the Programmable Bootstrapping (PBS) functionality.
  9. GPU Performance Benchmarks Overview

    main

    This guide provides access to GPU performance benchmarks for homomorphic operations using TFHE-rs. Note that homomorphic operations are inherently slower than cleartext operations.

    Benchmark Environment & Methodology:

    • Hardware: All benchmarks were conducted using H100 GPUs.
    • Algorithm: Benchmarks rely on the multithreaded PBS (Programmable Bootstrapping) algorithm.
  10. What is the Key-Value Store (KVStore)?

    main

    The Key-Value Store (KVStore) is a specialized encrypted HashMap implementation in TFHE-rs. It is designed for scenarios where:

    • Keys are clear (unencrypted) numbers.
    • Values are encrypted, specifically using FheInt (signed integers) or FheUint (unsigned integers).

    Note: While the documentation title mentions 'encrypted keys', the definition specifies keys as 'clear numbers'.

  11. Overview of tfhe-ntt NTT types

    main

    The tfhe-ntt library provides three types of Number Theoretic Transform (NTT) for vectors whose sizes are powers of two:

    1. Prime NTT: Computes the transform in a field $\mathbb{Z}/p \mathbb{Z}$ where $p$ is prime. This allows for polynomial arithmetic modulo $p$.
    2. Native NTT: Computes the transform using several primes to simulate arithmetic modulo the product of those primes. It is suitable for multiplying two polynomials with arbitrary coefficients and returns the result in wrapping arithmetic (provided the full integer result is smaller than half the product of the primes in absolute value).
    3. Native Binary NTT: An optimization of the Native NTT specifically for cases where one of the operands in a multiplication has coefficients in $\lbrace 0, 1 \rbrace$.
  12. Understand the TFHE-rs API levels

    main

    TFHE-rs is organized into several layers of abstraction:

    • core_crypto module: The lowest level API containing primitive functions and types of the TFHE scheme.
    • Boolean, shortint, and integer modules: Intermediate APIs that provide easy-to-use modules for evaluating specific types of circuits.
    • High-level API: Built on top of the shortint and integer modules. It abstracts cryptographic complexities by providing datatypes that match Rust's native types using overloaded operators (e.g., +, -). This is the recommended entry point for developers without deep cryptographic knowledge.