This repository contains the fhe.rs
library, an experimental cryptographic library in Rust for Ring-LWE-based homomorphic encryption, developed by Tancrède Lepoint. For more information about the library, see fhe.rs.
The library features:
- An implementation of a RNS-variant of the Brakerski-Fan-Vercauteren (BFV) homomorphic encryption scheme;
- Performances comparable or better than state-of-the-art libraries in C++ and Go.
Warning
fhe.rs
is a beta library, and should be considered unstable with potential breaking API changes until version 1.0.0 is released!
Note This library is not related to the
concrete
ecosystems (Zama's fully homomorphic encryption in Rust), available at concrete.rs.
fhe.rs
is implemented using the Rust programming language. The ecosystem is composed of four public crates (packages):
fhe
: This crate contains the implementations of the homomorphic encryption schemes;fhe-math
: This crate contains the core mathematical operations for thefhe
crate;fhe-traits
: This crate contains traits for homomorphic encryption schemes.fhe-util
: This crate contains utility functions for thefhe
crate.
The fhe
crate requires the nightly
toolchain as it uses multiple unstable features. The minimal supported version will be changed to a stable version in a future update.
To use the latest published crate(s), add one or both of the following to your Cargo.toml
file:
[dependencies]
fhe = "0.1.0-beta.2"
fhe-traits = "0.1.0-beta.1"
Below is a simple example of an homomorphic multiplication modulo 1024 (= 1 << 10)
, where one ciphertext is encrypted using the secret key, and one ciphertext encrypted using the public key. The poly()
encoding means that the vector being encoded corresponds to the coefficients of a polynomial in (ZZ / (1024))[x] / (x^2048+1)
.
use fhe::bfv;
use fhe_traits::*;
use std::sync::Arc;
fn main() {
let parameters = Arc::new(
bfv::BfvParametersBuilder::new()
.set_degree(2048)
.set_moduli(&[0x3fffffff000001])
.set_plaintext_modulus(1 << 10)
.build()
.unwrap(),
);
let secret_key = bfv::SecretKey::random(¶meters);
let public_key = bfv::PublicKey::new(&secret_key).unwrap();
let plaintext_1 =
bfv::Plaintext::try_encode(&[20_u64] as &[u64], bfv::Encoding::poly(), ¶meters)
.unwrap();
let plaintext_2 =
bfv::Plaintext::try_encode(&[-7_i64] as &[i64], bfv::Encoding::poly(), ¶meters)
.unwrap();
let ciphertext_1: bfv::Ciphertext = secret_key.try_encrypt(&plaintext_1).unwrap();
let ciphertext_2: bfv::Ciphertext = public_key.try_encrypt(&plaintext_2).unwrap();
let result = &ciphertext_1 * &ciphertext_2;
let decrypted_plaintext = secret_key.try_decrypt(&result).unwrap();
let decrypted_vector =
Vec::<u64>::try_decode(&decrypted_plaintext, bfv::Encoding::poly()).unwrap();
assert_eq!(decrypted_vector[0], ((1 << 10) - 20 * 7));
}
The implementations contained in the fhe.rs
ecosystem have never been independently audited for security.
Additionally, no promise on the API and ABI stability will be made until version 1.0.0
of the crates.
Use at your own risk.