The table below contains an overview of the available operations in TFHE-rs. More details, and further examples, are given in the following sections.
name
symbol
FheUint/FheUint
FheUint/Uint
Uint/FheUint
Neg
-
Add
+
Sub
-
Mul
*
Div
/
Rem
%
Not
!
BitAnd
&
BitOr
|
BitXor
^
Shr
>>
Shl
<<
Min
min
Max
max
Greater than
gt
Greater or equal than
ge
Lower than
lt
Lower or equal than
le
Equal
eq
Cast (into dest type)
cast_into
Cast (from src type)
cast_from
Boolean Operations
Native homomorphic Booleans support common Boolean operations.
The list of supported operations is:
name
symbol
type
&
Binary
|
Binary
^
Binary
!
Unary
ShortInt Operations
Native small homomorphic integer types (e.g., FheUint3 or FheUint4) easily compute various operations. In general, computing over encrypted data is as easy as computing over clear data, since the same operation symbol is used. The addition between two ciphertexts is done using the symbol + between two FheUint values. Many operations can be computed between a clear value (i.e. a scalar) and a ciphertext.
In Rust, operations on native types are modular. For example, computations on u8 are carried out modulo 28. A similar idea applies for FheUintX, where operations are done modulo 2X. For FheUint3, operations are done modulo 8=23.
Arithmetic operations.
Small homomorphic integer types support all common arithmetic operations, meaning +, -, x, /, mod.
The division operation implements a subtlety: since data is encrypted, it is possible to compute a division by 0. In this case, the division is tweaked so that dividing by 0 returns the max possible value for the message.
The list of supported operations is:
name
symbol
type
+
Binary
-
Binary
*
Binary
/
Binary
%
Binary
-
Unary
A simple example on how to use these operations:
use tfhe::prelude::*;use tfhe::{generate_keys, set_server_key, ConfigBuilder, FheUint3};fnmain() ->Result<(), Box<dyn std::error::Error>> {let config =ConfigBuilder::all_disabled().enable_default_uint3().build();let (keys, server_keys) =generate_keys(config);set_server_key(server_keys);let clear_a =7;let clear_b =3;let clear_c =2;letmut a =FheUint3::try_encrypt(clear_a, &keys)?;letmut b =FheUint3::try_encrypt(clear_b, &keys)?;letmut c =FheUint3::try_encrypt(clear_c, &keys)?; a = a *&b; // Clear equivalent computations: 7 * 3 mod 8 = 5 b =&b +&c; // Clear equivalent computations: 3 + 2 mod 8 = 5 b = b -5; // Clear equivalent computations: 5 - 5 mod 8 = 0let dec_a = a.decrypt(&keys);let dec_b = b.decrypt(&keys);// We homomorphically swapped values using bitwise operationsassert_eq!(dec_a, (clear_a * clear_b) %8);assert_eq!(dec_b, ((clear_b + clear_c) -5) %8);Ok(())}
Bitwise operations.
Small homomorphic integer types support some bitwise operations.
The list of supported operations is:
name
symbol
type
!
Unary
&
Binary
|
Binary
^
Binary
>>
Binary
<<
Binary
rotate_right
Binary
rotate_left
Binary
A simple example on how to use these operations:
use tfhe::prelude::*;use tfhe::{generate_keys, set_server_key, ConfigBuilder, FheUint3};fnmain() ->Result<(), Box<dyn std::error::Error>> {let config =ConfigBuilder::all_disabled().enable_default_uint3().build();let (keys, server_keys) =generate_keys(config);set_server_key(server_keys);let clear_a =7;let clear_b =3;letmut a =FheUint3::try_encrypt(clear_a, &keys)?;letmut b =FheUint3::try_encrypt(clear_b, &keys)?; a = a ^&b; b = b ^&a; a = a ^&b;let dec_a = a.decrypt(&keys);let dec_b = b.decrypt(&keys);// We homomorphically swapped values using bitwise operationsassert_eq!(dec_a, clear_b);assert_eq!(dec_b, clear_a);Ok(())}
Comparisons.
Small homomorphic integer types support comparison operations.
Due to some Rust limitations, it is not possible to overload the comparison symbols because of the inner definition of the operations. Rust expects to have a Boolean as an output, whereas a ciphertext is returned when using homomorphic types.
You will need to use different methods instead of using symbols for the comparisons. These methods follow the same naming conventions as the two standard Rust traits:
Using the shortint type allows you to evaluate bivariate functions (i.e., functions that take two ciphertexts as input).
A simple code example:
use tfhe::prelude::*;use tfhe::{generate_keys, set_server_key, ConfigBuilder, FheUint2};fnmain() ->Result<(), Box<dyn std::error::Error>> {let config =ConfigBuilder::all_disabled().enable_default_uint2().build();let (keys, server_keys) =generate_keys(config);set_server_key(server_keys);let clear_a =1;let clear_b =3;let a =FheUint2::try_encrypt(clear_a, &keys)?;let b =FheUint2::try_encrypt(clear_b, &keys)?;let c = a.bivariate_function(&b, std::cmp::max);let decrypted = c.decrypt(&keys);assert_eq!(decrypted, std::cmp::max(clear_a, clear_b) asu8);Ok(())}
Integer
In TFHE-rs, integers are used to encrypt any messages larger than 4 bits. All supported operations are listed below.
Arithmetic operations.
Homomorphic integer types support arithmetic operations.
The list of supported operations is:
name
symbol
type
-
Unary
+
Binary
-
Binary
*
Binary
/
Binary
%
Binary
For the division operator, the convention is to return the modulus - 1. For instance, for FheUint8, the modulus is 28=256, so a division by 0 will return an encryption of 255. For the remainder operator, the convention is to return the first input without any modification. For instance, for ct1 = FheUint8(63) and ct2 = FheUint8(0), then ct1 % ct2 will return FheUint8(63).
A simple example on how to use these operations:
use tfhe::prelude::*;use tfhe::{generate_keys, set_server_key, ConfigBuilder, FheUint8};fnmain() ->Result<(), Box<dyn std::error::Error>> {let config =ConfigBuilder::all_disabled().enable_default_integers().build();let (keys, server_keys) =generate_keys(config);set_server_key(server_keys);let clear_a =15_u64;let clear_b =27_u64;let clear_c =43_u64;letmut a =FheUint8::try_encrypt(clear_a, &keys)?;letmut b =FheUint8::try_encrypt(clear_b, &keys)?;letmut c =FheUint8::try_encrypt(clear_c, &keys)?; a = a *&b; // Clear equivalent computations: 15 * 27 mod 256 = 149 b =&b +&c; // Clear equivalent computations: 27 + 43 mod 256 = 70 b = b -76u8; // Clear equivalent computations: 70 - 76 mod 256 = 250let dec_a:u8= a.decrypt(&keys);let dec_b:u8= b.decrypt(&keys);assert_eq!(dec_a, ((clear_a * clear_b) %256_u64) asu8);assert_eq!(dec_b, (((clear_b + clear_c).wrapping_sub(76_u64)) %256_u64) asu8);Ok(())}
Bitwise operations.
Homomorphic integer types support some bitwise operations.
The list of supported operations is:
name
symbol
type
!
Unary
&
Binary
|
Binary
^
Binary
>>
Binary
<<
Binary
rotate_right
Binary
rotate_left
Binary
A simple example on how to use these operations:
use tfhe::prelude::*;use tfhe::{generate_keys, set_server_key, ConfigBuilder, FheUint8};fnmain() ->Result<(), Box<dyn std::error::Error>> {let config =ConfigBuilder::all_disabled().enable_default_integers().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(())}
Comparisons.
Homomorphic integers support comparison operations. Since Rust does not allow the overloading of these operations, a simple function has been associated to each one.