Types & Operations

Types

TFHE-rs includes two main types to represent encrypted data:

  • FheUint: this is the homomorphic equivalent of Rust unsigned integers u8, u16, ...

  • FheInt: this is the homomorphic equivalent of Rust (signed) integers i8, i16, ...

In the same manner as many programming languages, the number of bits used to represent the data must be chosen when declaring a variable. For instance:

    // 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)?;

Operation list

The table below contains an overview of the available operations in TFHE-rs. The notation Enc (for Encypted) either refers to FheInt or FheUint, for any size between 1 and 256-bits.

More details, and further examples, are given in the following sections.

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

✔️

✔️

Lower than

lt

✔️

✔️

Lower or equal than

le

✔️

✔️

Equal

eq

✔️

✔️

Cast (into dest type)

cast_into

✔️

✖️

Cast (from src type)

cast_from

✔️

✖️

Ternary operator

if_then_else

✔️

✖️

Integer

In TFHE-rs, integers are used to encrypt all messages which are larger than 4 bits. All supported operations are listed below.

Arithmetic operations.

Homomorphic integer types support arithmetic operations.

The list of supported operations is:

For division by 0, the convention is to return modulus - 1. For instance, for FheUint8, the modulus is 28=2562^8=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, if ct1 = FheUint8(63) and ct2 = FheUint8(0) then ct1 % ct2 will return FheUint8(63).

A simple example of how to use these operations:

Bitwise operations.

Homomorphic integer types support some bitwise operations.

The list of supported operations is:

A simple example of how to use these operations:

Comparisons.

Homomorphic integers 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. This is because 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:

The list of supported operations is:

A simple example of how to use these operations:

Min/Max.

Homomorphic integers support the min/max operations.

name
symbol
type

Min

min

Binary

Max

max

Binary

A simple example of how to use these operations:

Ternary conditional operator.

The ternary conditional operator allows computing conditional instructions of 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 encrypted_condition should be an encryption of 0 or 1 in order to be valid.

Casting.

Casting between integer types is possible via the cast_from associated function or the cast_into method.

ShortInt Operations

Native small homomorphic integer types (e.g., FheUint3 or FheUint4) can easily compute various operations. In general, computing over encrypted data in TFHE-rs 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 282^{8}. A similar idea applies for FheUintX, where operations are done modulo 2X2^{X}. For FheUint3, operations are done modulo 8=238 = 2^{3}.

Arithmetic operations.

Small homomorphic integer types support all common arithmetic operations, meaning +, -, x, /, mod.

The division operation presents 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 maximum possible value for the message.

The list of supported operations is:

A simple example of how to use these operations:

Bitwise operations.

Small homomorphic integer types support some bitwise operations.

The list of supported operations is:

A simple example of how to use these operations:

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:

The list of supported operations is:

A simple example of how to use these operations:

Univariate function evaluation.

The shortint type also supports the computation of univariate functions, which make use of TFHE's programmable bootstrapping.

A simple example of how to use these operations:

Bivariate function evaluations.

Using the shortint type allows you to evaluate bivariate functions (i.e., functions that take two ciphertexts as input).

A simple code example:

Boolean Operations

Native homomorphic Booleans support common Boolean operations.

The list of supported operations is:

Last updated

Was this helpful?