thedonutfactory/rs-tfhe
GitHub: thedonutfactory/rs-tfhe
纯 Rust 编写的 TFHE 全同态加密库,支持在不解密的情况下对加密数据执行布尔逻辑与算术运算。
Stars: 42 | Forks: 5
# rs-tfhe: Rust TFHE 库
[](https://crates.io/crates/rs_tfhe)
[](https://docs.rs/rs_tfhe)
[](LICENSE)
[](https://www.rust-lang.org)
[](https://github.com/thedonutfactory/rs-tfhe)
一个高性能的 Rust TFHE (Torus 全同态加密) 实现。
## 概述
rs-tfhe 是一个综合性的同态加密库,允许在不解密的情况下对加密数据进行计算,使用 Rust 构建以实现高性能和安全性。
### 主要特性




- **多安全级别**:80位、110位和128位安全参数
- **专用 Uint 参数**:针对不同消息模数(1-8位)优化的参数集
- **同态门电路**:完整的布尔运算集(AND、OR、NAND、NOR、XOR、XNOR、NOT、MUX)
- **快速算术运算**:使用基于 nibble 的加法进行高效的多位算术运算
- **并行处理**:基于 Rayon 的批处理并行化
- **优化的 FFT**:包含 SIMD 优化的多种 FFT 实现
- **Feature Flags**:带有可选功能的模块化编译
## 安装说明
将 rs-tfhe 添加到您的 `Cargo.toml`:
```
[dependencies]
rs_tfhe = "0.1.1"
```
### Feature Flags
```
[dependencies]
rs_tfhe = { version = "0.2.0", features = ["lut-bootstrap", "fft_fma"] }
```
可用功能:
- `bootstrapping`:启用 bootstrapping 操作(默认)
- `lut-bootstrap`:启用带有查找表的可编程 bootstrapping
- `fft_avx`:启用 AVX 优化的 FFT(仅限 x86_64)
- `fft_fma`:启用 FMA 优化的 FFT(默认)
## 快速开始
### 基本同态操作
```
use rs_tfhe::key;
use rs_tfhe::gates::Gates;
use rs_tfhe::utils::Ciphertext;
// Generate keys
let secret_key = key::SecretKey::new();
let cloud_key = key::CloudKey::new(&secret_key);
// Encrypt boolean values
let ct_true = Ciphertext::encrypt(true, &secret_key.key_lv0);
let ct_false = Ciphertext::encrypt(false, &secret_key.key_lv0);
// Perform homomorphic operations
let gates = Gates::new(&cloud_key);
let result = gates.hom_and(&ct_true, &ct_false);
// Decrypt result
let decrypted = result.decrypt(&secret_key.key_lv0);
assert_eq!(decrypted, false);
```
### 可编程 Bootstrapping
```
#[cfg(feature = "lut-bootstrap")]
use rs_tfhe::bootstrap::lut::LutBootstrap;
#[cfg(feature = "lut-bootstrap")]
use rs_tfhe::lut::Generator;
#[cfg(feature = "lut-bootstrap")]
fn programmable_bootstrap_example() {
let secret_key = key::SecretKey::new();
let cloud_key = key::CloudKey::new(&secret_key);
let bootstrap = LutBootstrap::new();
// Encrypt a value
let encrypted = Ciphertext::encrypt_lwe_message(5, 8, 0.0001, &secret_key.key_lv0);
// Define a function to evaluate (square function)
let square_func = |x: usize| (x * x) % 8;
// Apply function during bootstrapping
let result = bootstrap.bootstrap_func(&encrypted, square_func, 8, &cloud_key);
// Decrypt result
let decrypted = result.decrypt_lwe_message(8, &secret_key.key_lv0);
assert_eq!(decrypted, 1); // 5^2 mod 8 = 25 mod 8 = 1
}
```
### 使用 LUT Bootstrapping 的快速算术运算
```
#[cfg(feature = "lut-bootstrap")]
fn fast_addition_example() {
use rs_tfhe::params;
// Use specialized parameters for arithmetic
let current_params = params::SECURITY_128_BIT;
let secret_key = key::SecretKey::new();
let cloud_key = key::CloudKey::new(&secret_key);
let bootstrap = LutBootstrap::new();
// Encrypt two 4-bit values
let a = 5;
let b = 7;
let ct_a = Ciphertext::encrypt_lwe_message(a, 16, current_params.tlwe_lv0.alpha, &secret_key.key_lv0);
let ct_b = Ciphertext::encrypt_lwe_message(b, 16, current_params.tlwe_lv0.alpha, &secret_key.key_lv0);
// Homomorphic addition
let ct_sum = &ct_a + &ct_b;
// Extract result using LUT bootstrapping
let mod_func = |x: usize| x % 16;
let result = bootstrap.bootstrap_func(&ct_sum, mod_func, 16, &cloud_key);
let decrypted = result.decrypt_lwe_message(16, &secret_key.key_lv0);
assert_eq!(decrypted, (a + b) % 16);
}
```
## 架构
### 核心组件
#### 加密方案
- **TLWE**:用于 level-0 密文的 Torus Learning With Errors
- **TRLWE**:用于 level-1 密文的 Torus Ring Learning With Errors
- **TRGSW**:用于 bootstrapping 密钥的 Torus GSW
#### Bootstrapping 策略
- **Vanilla Bootstrap**:传统的噪声刷新
- **LUT Bootstrap**:带有查找表的可编程 bootstrapping
#### FFT 实现
- **标准 FFT**:纯 Rust 实现
- **SIMD FFT**:针对 x86_64 的 AVX/FMA 优化
- **Real FFT**:针对实值多项式进行了优化
### 参数集
#### 标准安全参数
- `SECURITY_80_BIT`:80位安全级别
- `SECURITY_110_BIT`:110位安全级别
- `SECURITY_128_BIT`:128位安全级别(默认)
#### 专用 Uint 参数(需要 `lut-bootstrap` 功能)
- `SECURITY_UINT1`:二进制运算 (messageModulus=2)
- `SECURITY_UINT2`:2位算术运算 (messageModulus=4)
- `SECURITY_UINT3`:3位算术运算 (messageModulus=8)
- `SECURITY_UINT4`:4位算术运算 (messageModulus=16)
- `SECURITY_UINT5`:5位算术运算 (messageModulus=32) - 推荐用于复杂操作
- `SECURITY_UINT6`:6位算术运算 (messageModulus=64)
- `SECURITY_UINT7`:7位算术运算 (messageModulus=128)
- `SECURITY_UINT8`:8位算术运算 (messageModulus=256)
## 示例
`examples/` 目录包含全面的示例:
### 基础示例
- `add_two_numbers.rs`:简单的同态加法
- `gates_with_strategies.rs`:布尔门操作
- `security_levels.rs`:不同安全参数对比
### LUT Bootstrapping 示例
- `lut_bootstrapping.rs`:完整的可编程 bootstrapping 演示
- `lut_bootstrapping_simple.rs`:最小化的 LUT 示例
- `lut_add_two_numbers.rs`:使用 nibble 操作的快速 8 位加法
- `lut_arithmetic_demo.rs`:各种算术运算
- `lut_uint_parameters_demo.rs`:参数集对比
### 性能示例
- `batch_gates.rs`:并行门处理
- `custom_railgun.rs`:自定义并行化策略
- `fft_diagnostics.rs`:FFT 性能分析
## 性能


### 基准测试
运行基准测试:
```
cargo bench
```
### 性能特征
| 操作 | 时间 | 备注 |
|-----------|-----------|-------|
| 密钥生成 | ~135 | 一次性设置 |
| 布尔门 | ~15 | 单个门操作 |
| Bootstrap | ~15-20 | 噪声刷新 |
| LUT Bootstrap | ~15-20 | 函数求值 + 噪声刷新 |
| 8 位加法 | ~50 | 3 次 bootstrap,对比按位运算的 8 次 |
### 优化特性
- **并行处理**:基于 Rayon 的批处理操作
- **SIMD FFT**:针对 x86_64 的 AVX/FMA 优化
- **专用参数**:针对特定消息模数进行了优化
- **LUT 重用**:为重复函数预计算查找表
## API 参考
### 核心类型
#### `Ciphertext`
支持同态操作的主密文类型。
```
impl Ciphertext {
pub fn encrypt(plaintext: bool, key: &SecretKey) -> Self;
pub fn decrypt(&self, key: &SecretKey) -> bool;
pub fn encrypt_lwe_message(msg: usize, modulus: usize, alpha: f64, key: &SecretKey) -> Self;
pub fn decrypt_lwe_message(&self, modulus: usize, key: &SecretKey) -> usize;
}
```
#### `Gates`
布尔门操作。
```
impl Gates {
pub fn hom_and(&self, a: &Ciphertext, b: &Ciphertext) -> Ciphertext;
pub fn hom_or(&self, a: &Ciphertext, b: &Ciphertext) -> Ciphertext;
pub fn hom_xor(&self, a: &Ciphertext, b: &Ciphertext) -> Ciphertext;
pub fn hom_not(&self, a: &Ciphertext) -> Ciphertext;
pub fn mux(&self, cond: &Ciphertext, a: &Ciphertext, b: &Ciphertext) -> Ciphertext;
}
```
#### `LutBootstrap`(需要 `lut-bootstrap` 功能)
带有查找表的可编程 bootstrapping。
```
impl LutBootstrap {
pub fn bootstrap_func(&self, ct: &Ciphertext, f: F, modulus: usize, key: &CloudKey) -> Ciphertext
where F: Fn(usize) -> usize;
pub fn bootstrap_lut(&self, ct: &Ciphertext, lut: &LookupTable, key: &CloudKey) -> Ciphertext;
}
```
#### `Generator`(需要 `lut-bootstrap` 功能)
查找表生成。
```
impl Generator {
pub fn new(message_modulus: usize) -> Self;
pub fn generate_lookup_table(&self, f: F) -> LookupTable
where F: Fn(usize) -> usize;
}
```
### 开发环境设置
```
git clone
cd rs-tfhe
cargo test
cargo test --features "lut-bootstrap"
cargo bench
```
### 运行示例
```
# 基础示例
cargo run --example add_two_numbers --release
cargo run --example gates_with_strategies --release
# LUT bootstrapping 示例(需要 feature flag)
cargo run --example lut_bootstrapping --features "lut-bootstrap" --release
cargo run --example lut_add_two_numbers --features "lut-bootstrap" --release
```
## 许可证
该项目基于与原始 TFHE 库相同的条款进行许可。详情请参阅 [LICENSE](LICENSE)。
## 致谢
- 基于 Ilaria Chillotti、Nicolas Gama、Mariya Georgieva 和 Malika Izabachène 编写的 TFHE 库
- 受到 go-tfhe 实现的启发
- FFT 优化源自 tfhe-go 参考实现
## 概述
rs-tfhe 是一个综合性的同态加密库,允许在不解密的情况下对加密数据进行计算,使用 Rust 构建以实现高性能和安全性。
### 主要特性




- **多安全级别**:80位、110位和128位安全参数
- **专用 Uint 参数**:针对不同消息模数(1-8位)优化的参数集
- **同态门电路**:完整的布尔运算集(AND、OR、NAND、NOR、XOR、XNOR、NOT、MUX)
- **快速算术运算**:使用基于 nibble 的加法进行高效的多位算术运算
- **并行处理**:基于 Rayon 的批处理并行化
- **优化的 FFT**:包含 SIMD 优化的多种 FFT 实现
- **Feature Flags**:带有可选功能的模块化编译
## 安装说明
将 rs-tfhe 添加到您的 `Cargo.toml`:
```
[dependencies]
rs_tfhe = "0.1.1"
```
### Feature Flags
```
[dependencies]
rs_tfhe = { version = "0.2.0", features = ["lut-bootstrap", "fft_fma"] }
```
可用功能:
- `bootstrapping`:启用 bootstrapping 操作(默认)
- `lut-bootstrap`:启用带有查找表的可编程 bootstrapping
- `fft_avx`:启用 AVX 优化的 FFT(仅限 x86_64)
- `fft_fma`:启用 FMA 优化的 FFT(默认)
## 快速开始
### 基本同态操作
```
use rs_tfhe::key;
use rs_tfhe::gates::Gates;
use rs_tfhe::utils::Ciphertext;
// Generate keys
let secret_key = key::SecretKey::new();
let cloud_key = key::CloudKey::new(&secret_key);
// Encrypt boolean values
let ct_true = Ciphertext::encrypt(true, &secret_key.key_lv0);
let ct_false = Ciphertext::encrypt(false, &secret_key.key_lv0);
// Perform homomorphic operations
let gates = Gates::new(&cloud_key);
let result = gates.hom_and(&ct_true, &ct_false);
// Decrypt result
let decrypted = result.decrypt(&secret_key.key_lv0);
assert_eq!(decrypted, false);
```
### 可编程 Bootstrapping
```
#[cfg(feature = "lut-bootstrap")]
use rs_tfhe::bootstrap::lut::LutBootstrap;
#[cfg(feature = "lut-bootstrap")]
use rs_tfhe::lut::Generator;
#[cfg(feature = "lut-bootstrap")]
fn programmable_bootstrap_example() {
let secret_key = key::SecretKey::new();
let cloud_key = key::CloudKey::new(&secret_key);
let bootstrap = LutBootstrap::new();
// Encrypt a value
let encrypted = Ciphertext::encrypt_lwe_message(5, 8, 0.0001, &secret_key.key_lv0);
// Define a function to evaluate (square function)
let square_func = |x: usize| (x * x) % 8;
// Apply function during bootstrapping
let result = bootstrap.bootstrap_func(&encrypted, square_func, 8, &cloud_key);
// Decrypt result
let decrypted = result.decrypt_lwe_message(8, &secret_key.key_lv0);
assert_eq!(decrypted, 1); // 5^2 mod 8 = 25 mod 8 = 1
}
```
### 使用 LUT Bootstrapping 的快速算术运算
```
#[cfg(feature = "lut-bootstrap")]
fn fast_addition_example() {
use rs_tfhe::params;
// Use specialized parameters for arithmetic
let current_params = params::SECURITY_128_BIT;
let secret_key = key::SecretKey::new();
let cloud_key = key::CloudKey::new(&secret_key);
let bootstrap = LutBootstrap::new();
// Encrypt two 4-bit values
let a = 5;
let b = 7;
let ct_a = Ciphertext::encrypt_lwe_message(a, 16, current_params.tlwe_lv0.alpha, &secret_key.key_lv0);
let ct_b = Ciphertext::encrypt_lwe_message(b, 16, current_params.tlwe_lv0.alpha, &secret_key.key_lv0);
// Homomorphic addition
let ct_sum = &ct_a + &ct_b;
// Extract result using LUT bootstrapping
let mod_func = |x: usize| x % 16;
let result = bootstrap.bootstrap_func(&ct_sum, mod_func, 16, &cloud_key);
let decrypted = result.decrypt_lwe_message(16, &secret_key.key_lv0);
assert_eq!(decrypted, (a + b) % 16);
}
```
## 架构
### 核心组件
#### 加密方案
- **TLWE**:用于 level-0 密文的 Torus Learning With Errors
- **TRLWE**:用于 level-1 密文的 Torus Ring Learning With Errors
- **TRGSW**:用于 bootstrapping 密钥的 Torus GSW
#### Bootstrapping 策略
- **Vanilla Bootstrap**:传统的噪声刷新
- **LUT Bootstrap**:带有查找表的可编程 bootstrapping
#### FFT 实现
- **标准 FFT**:纯 Rust 实现
- **SIMD FFT**:针对 x86_64 的 AVX/FMA 优化
- **Real FFT**:针对实值多项式进行了优化
### 参数集
#### 标准安全参数
- `SECURITY_80_BIT`:80位安全级别
- `SECURITY_110_BIT`:110位安全级别
- `SECURITY_128_BIT`:128位安全级别(默认)
#### 专用 Uint 参数(需要 `lut-bootstrap` 功能)
- `SECURITY_UINT1`:二进制运算 (messageModulus=2)
- `SECURITY_UINT2`:2位算术运算 (messageModulus=4)
- `SECURITY_UINT3`:3位算术运算 (messageModulus=8)
- `SECURITY_UINT4`:4位算术运算 (messageModulus=16)
- `SECURITY_UINT5`:5位算术运算 (messageModulus=32) - 推荐用于复杂操作
- `SECURITY_UINT6`:6位算术运算 (messageModulus=64)
- `SECURITY_UINT7`:7位算术运算 (messageModulus=128)
- `SECURITY_UINT8`:8位算术运算 (messageModulus=256)
## 示例
`examples/` 目录包含全面的示例:
### 基础示例
- `add_two_numbers.rs`:简单的同态加法
- `gates_with_strategies.rs`:布尔门操作
- `security_levels.rs`:不同安全参数对比
### LUT Bootstrapping 示例
- `lut_bootstrapping.rs`:完整的可编程 bootstrapping 演示
- `lut_bootstrapping_simple.rs`:最小化的 LUT 示例
- `lut_add_two_numbers.rs`:使用 nibble 操作的快速 8 位加法
- `lut_arithmetic_demo.rs`:各种算术运算
- `lut_uint_parameters_demo.rs`:参数集对比
### 性能示例
- `batch_gates.rs`:并行门处理
- `custom_railgun.rs`:自定义并行化策略
- `fft_diagnostics.rs`:FFT 性能分析
## 性能


### 基准测试
运行基准测试:
```
cargo bench
```
### 性能特征
| 操作 | 时间 | 备注 |
|-----------|-----------|-------|
| 密钥生成 | ~135 | 一次性设置 |
| 布尔门 | ~15 | 单个门操作 |
| Bootstrap | ~15-20 | 噪声刷新 |
| LUT Bootstrap | ~15-20 | 函数求值 + 噪声刷新 |
| 8 位加法 | ~50 | 3 次 bootstrap,对比按位运算的 8 次 |
### 优化特性
- **并行处理**:基于 Rayon 的批处理操作
- **SIMD FFT**:针对 x86_64 的 AVX/FMA 优化
- **专用参数**:针对特定消息模数进行了优化
- **LUT 重用**:为重复函数预计算查找表
## API 参考
### 核心类型
#### `Ciphertext`
支持同态操作的主密文类型。
```
impl Ciphertext {
pub fn encrypt(plaintext: bool, key: &SecretKey) -> Self;
pub fn decrypt(&self, key: &SecretKey) -> bool;
pub fn encrypt_lwe_message(msg: usize, modulus: usize, alpha: f64, key: &SecretKey) -> Self;
pub fn decrypt_lwe_message(&self, modulus: usize, key: &SecretKey) -> usize;
}
```
#### `Gates`
布尔门操作。
```
impl Gates {
pub fn hom_and(&self, a: &Ciphertext, b: &Ciphertext) -> Ciphertext;
pub fn hom_or(&self, a: &Ciphertext, b: &Ciphertext) -> Ciphertext;
pub fn hom_xor(&self, a: &Ciphertext, b: &Ciphertext) -> Ciphertext;
pub fn hom_not(&self, a: &Ciphertext) -> Ciphertext;
pub fn mux(&self, cond: &Ciphertext, a: &Ciphertext, b: &Ciphertext) -> Ciphertext;
}
```
#### `LutBootstrap`(需要 `lut-bootstrap` 功能)
带有查找表的可编程 bootstrapping。
```
impl LutBootstrap {
pub fn bootstrap_func标签:Rust, TFHE, 全同态加密, 加密库, 可视化界面, 密码学, 手动系统调用, 网络流量审计, 通知系统, 隐私计算