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.

Boolean Operations

Native homomorphic Booleans support common Boolean operations.

The list of supported operations is:

Last updated

Was this helpful?