rs-tfhe: Rust TFHE Library
A high-performance Rust implementation of TFHE (Torus Fully Homomorphic Encryption).
Overview
rs-tfhe is a comprehensive homomorphic encryption library that enables computation on encrypted data without decryption, built in Rust for performance and safety.
(Looking to build in golang? Check out our go-tfhe Sister Project)
Key Features
- Multiple Security Levels: 80-bit, 110-bit, and 128-bit security parameters
- Specialized Uint Parameters: Optimized parameter sets for different message moduli (1-8 bits)
- Homomorphic Gates: Complete set of boolean operations (AND, OR, NAND, NOR, XOR, XNOR, NOT, MUX)
- Fast Arithmetic: Efficient multi-bit arithmetic operations using nibble-based addition
- Parallel Processing: Rayon-based parallelization for batch operations
- Optimized FFT: Multiple FFT implementations including SIMD optimizations
- Feature Flags: Modular compilation with optional features
Installation
Add rs-tfhe to your Cargo.toml:
[]
= "0.1.0"
Feature Flags
[]
= { = "0.1.0", = ["lut-bootstrap", "fft_fma"] }
Available features:
bootstrapping: Enable bootstrapping operations (default)lut-bootstrap: Enable programmable bootstrapping with lookup tablesfft_avx: Enable AVX-optimized FFT (x86_64 only)fft_fma: Enable FMA-optimized FFT (default)
Quick Start
Basic Homomorphic Operations
use key;
use Gates;
use Ciphertext;
// Generate keys
let secret_key = new;
let cloud_key = new;
// Encrypt boolean values
let ct_true = encrypt;
let ct_false = encrypt;
// Perform homomorphic operations
let gates = new;
let result = gates.hom_and;
// Decrypt result
let decrypted = result.decrypt;
assert_eq!;
Programmable Bootstrapping
use LutBootstrap;
use Generator;
Fast Arithmetic with LUT Bootstrapping
Architecture
Core Components
Encryption Schemes
- TLWE: Torus Learning With Errors for level-0 ciphertexts
- TRLWE: Torus Ring Learning With Errors for level-1 ciphertexts
- TRGSW: Torus GSW for bootstrapping keys
Bootstrapping Strategies
- Vanilla Bootstrap: Traditional noise refreshing
- LUT Bootstrap: Programmable bootstrapping with lookup tables
FFT Implementations
- Standard FFT: Pure Rust implementation
- SIMD FFT: AVX/FMA optimized for x86_64
- Real FFT: Optimized for real-valued polynomials
Parameter Sets
Standard Security Parameters
SECURITY_80_BIT: 80-bit security levelSECURITY_110_BIT: 110-bit security levelSECURITY_128_BIT: 128-bit security level (default)
Specialized Uint Parameters (requires lut-bootstrap feature)
SECURITY_UINT1: Binary operations (messageModulus=2)SECURITY_UINT2: 2-bit arithmetic (messageModulus=4)SECURITY_UINT3: 3-bit arithmetic (messageModulus=8)SECURITY_UINT4: 4-bit arithmetic (messageModulus=16)SECURITY_UINT5: 5-bit arithmetic (messageModulus=32) - Recommended for complex operationsSECURITY_UINT6: 6-bit arithmetic (messageModulus=64)SECURITY_UINT7: 7-bit arithmetic (messageModulus=128)SECURITY_UINT8: 8-bit arithmetic (messageModulus=256)
Examples
The examples/ directory contains comprehensive examples:
Basic Examples
add_two_numbers.rs: Simple homomorphic additiongates_with_strategies.rs: Boolean gate operationssecurity_levels.rs: Different security parameter comparisons
LUT Bootstrapping Examples
lut_bootstrapping.rs: Complete programmable bootstrapping demolut_bootstrapping_simple.rs: Minimal LUT examplelut_add_two_numbers.rs: Fast 8-bit addition using nibble operationslut_arithmetic_demo.rs: Various arithmetic operationslut_uint_parameters_demo.rs: Parameter set comparisons
Performance Examples
batch_gates.rs: Parallel gate processingcustom_railgun.rs: Custom parallelization strategiesfft_diagnostics.rs: FFT performance analysis
Performance
Benchmarks
Run benchmarks with:
Performance Characteristics
| Operation | Time (ms) | Notes |
|---|---|---|
| Key Generation | ~135 | One-time setup |
| Boolean Gate | ~15 | Per gate operation |
| Bootstrap | ~15-20 | Noise refreshing |
| LUT Bootstrap | ~15-20 | Function evaluation + noise refreshing |
| 8-bit Addition | ~50 | 3 bootstraps vs 8 for bit-by-bit |
Optimization Features
- Parallel Processing: Rayon-based batch operations
- SIMD FFT: AVX/FMA optimizations for x86_64
- Specialized Parameters: Optimized for specific message moduli
- LUT Reuse: Pre-computed lookup tables for repeated functions
API Reference
Core Types
Ciphertext
Main ciphertext type supporting homomorphic operations.
Gates
Boolean gate operations.
LutBootstrap (requires lut-bootstrap feature)
Programmable bootstrapping with lookup tables.
Generator (requires lut-bootstrap feature)
Lookup table generation.
Contributing
Contributions are welcome! Please see the existing code style and add tests for new functionality.
Development Setup
Running Examples
# Basic examples
# LUT bootstrapping examples (requires feature flag)
License
This project is licensed under the same terms as the original TFHE library. See LICENSE for details.
Acknowledgments
- Based on the TFHE library by Ilaria Chillotti, Nicolas Gama, Mariya Georgieva, and Malika Izabachène
- Inspired by the go-tfhe implementation
- FFT optimizations from tfhe-go reference implementation