This document explains the encryption types and operations supported by TFHE-rs.
Types
TFHE-rs supports two main types of encrypted data:
FheUint: homomorphic equivalent of Rust unsigned integers u8, u16, ...
FheInt: homomorphic equivalent of Rust signed integers i8, i16, ...
Integer
TFHE-rs uses integers to encrypt all messages which are larger than 4 bits.
Similar to Rust integers, you need to specify the bit size of data when declaring a variable:
// let clear_a: u64 = 7;
let mut a = FheUint64::try_encrypt(clear_a, &keys)?;
// let clear_b: i8 = 3;
let mut b = FheInt8::try_encrypt(clear_b, &keys)?;
// let clear_c: u128 = 2;
let mut c = FheUint128::try_encrypt(clear_c, &keys)?;
Operations
TFHE-rs supports various operations on encrypted integers (Enc) of any size between 1 and 256 bits. These operations can also work between encrypted integers and clear integers (Int).
name
symbol
Enc/Enc
Enc/ Int
Neg
-
Add
+
Sub
-
Mul
*
Div
/
Rem
%
Not
!
BitAnd
&
BitOr
|
BitXor
^
Shr
>>
Shl
<<
Min
min
Max
max
Greater than
gt
Greater or equal than
ge
Less than
lt
Less or equal than
le
Equal
eq
Cast (into dest type)
cast_into
Cast (from src type)
cast_from
Ternary operator
if_then_else
Arithmetic operations
Homomorphic integer types (FheUint and FheInt) support the following arithmetic operations:
The following example shows how to perform bitwise operations:
use tfhe::prelude::*;use tfhe::{generate_keys, set_server_key, ConfigBuilder, FheUint8};fnmain() ->Result<(), Box<dyn std::error::Error>> {let config =ConfigBuilder::default().build();let (keys, server_keys) =generate_keys(config);set_server_key(server_keys);let clear_a =164;let clear_b =212;letmut a =FheUint8::try_encrypt(clear_a, &keys)?;letmut b =FheUint8::try_encrypt(clear_b, &keys)?; a = a ^&b; b = b ^&a; a = a ^&b;let dec_a:u8= a.decrypt(&keys);let dec_b:u8= b.decrypt(&keys);// We homomorphically swapped values using bitwise operationsassert_eq!(dec_a, clear_b);assert_eq!(dec_b, clear_a);Ok(())}
Comparison operations
Homomorphic integers support comparison operations. However, due to Rust's limitations, you cannot overload comparison symbols. This is because Rust requires Boolean outputs from such operations, but homomorphic types return ciphertexts. Therefore, you should use the following methods, which conform to the naming conventions of Rust’s standard traits:
Homomorphic integers support the min/max operations:
name
symbol
type
Min
min
Binary
Max
max
Binary
The following example shows how to perform min/max operations:
use tfhe::prelude::*;use tfhe::{generate_keys, set_server_key, ConfigBuilder, FheUint8};fnmain() ->Result<(), Box<dyn std::error::Error>> {let config =ConfigBuilder::default().build();let (keys, server_keys) =generate_keys(config);set_server_key(server_keys);let clear_a:u8=164;let clear_b:u8=212;letmut a =FheUint8::try_encrypt(clear_a, &keys)?;letmut b =FheUint8::try_encrypt(clear_b, &keys)?;let min = a.min(&b);let max = a.max(&b);let dec_min :u8= min.decrypt(&keys);let dec_max :u8= max.decrypt(&keys);assert_eq!(dec_min, u8::min(clear_a, clear_b));assert_eq!(dec_max, u8::max(clear_a, clear_b));Ok(())}
Ternary conditional operations
The ternary conditional operator execute conditional instructions in the form if cond { choice_if } else { choice_else }.
name
symbol
type
Ternary operator
if_then_else
Ternary
The syntax is encrypted_condition.if_then_else(encrypted_choice_if, encrypted_choice_else). The valid encrypted_condition must be an encryption of 0 or 1.
The following example shows how to perform ternary conditional operations:
use tfhe::prelude::*;use tfhe::{generate_keys, set_server_key, ConfigBuilder, FheInt32};fnmain() ->Result<(), Box<dyn std::error::Error>> {// Basic configuration to use homomorphic integerslet config =ConfigBuilder::default().build();// Key generationlet (client_key, server_keys) =generate_keys(config);let clear_a =32i32;let clear_b =-45i32;// Encrypting the input data using the (private) client_key// FheInt32: Encrypted equivalent to i32let encrypted_a =FheInt32::try_encrypt(clear_a, &client_key)?;let encrypted_b =FheInt32::try_encrypt(clear_b, &client_key)?;// On the server side:set_server_key(server_keys);// Clear equivalent computations: 32 > -45let encrypted_comp =&encrypted_a.gt(&encrypted_b);let clear_res = encrypted_comp.decrypt(&client_key);assert_eq!(clear_res, clear_a > clear_b);// `encrypted_comp` is a FheBool, thus it encrypts a boolean value.// This acts as a condition on which the// `if_then_else` function can be applied on.// Clear equivalent computations:// if 32 > -45 {result = 32} else {result = -45}let encrypted_res =&encrypted_comp.if_then_else(&encrypted_a, &encrypted_b);let clear_res:i32= encrypted_res.decrypt(&client_key);assert_eq!(clear_res, clear_a);Ok(())}
Casting operations
You can cast between integer types using either the cast_from associated function or the cast_into method.
The following example shows how to perform casting operations:
use tfhe::prelude::*;use tfhe::{generate_keys, set_server_key, ConfigBuilder, FheInt16, FheUint8, FheUint32, FheUint16};fnmain() ->Result<(), Box<dyn std::error::Error>> {let config =ConfigBuilder::default().build();let (client_key, server_key) =generate_keys(config);// Casting requires server_key to set// (encryptions/decryptions do not need server_key to be set)set_server_key(server_key); {let clear =12_837u16;let a =FheUint16::encrypt(clear, &client_key);// Downcastinglet a:FheUint8= a.cast_into();let da:u8= a.decrypt(&client_key);assert_eq!(da, clear asu8);// Upcastinglet a:FheUint32= a.cast_into();let da:u32= a.decrypt(&client_key);assert_eq!(da, (clear asu8) asu32); } {let clear =12_837u16;let a =FheUint16::encrypt(clear, &client_key);// Upcastinglet a =FheUint32::cast_from(a);let da:u32= a.decrypt(&client_key);assert_eq!(da, clear asu32);// Downcastinglet a =FheUint8::cast_from(a);let da:u8= a.decrypt(&client_key);assert_eq!(da, (clear asu32) asu8); } {let clear =12_837i16;let a =FheInt16::encrypt(clear, &client_key);// Casting from FheInt16 to FheUint16let a =FheUint16::cast_from(a);let da:u16= a.decrypt(&client_key);assert_eq!(da, clear asu16); }Ok(())}
Boolean Operations
Native homomorphic Booleans support the following common Boolean operations: