Types & Operations

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

select

✔️

✖️

Arithmetic operations

Homomorphic integer types (FheUint and FheInt) support the following arithmetic operations:

Specifications for operations with zero:

  • Division by zero: returns modulus - 1.

    • Example: for FheUint8 (modulus = 28=2562^8=256), dividing by zero returns an ecryption of 255.

  • Remainder operator: returns the first input unchanged.

    • Example: if ct1 = FheUint8(63) and ct2 = FheUint8(0), then ct1 % ct2 returns FheUint8(63).

The following example shows how to perform arithmetic operations:

Bitwise operations

Homomorphic integer types support the following bitwise operations:

The following example shows how to perform bitwise operations:

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:

Supported operations:

The following example shows how to perform comparison operations:

Min/Max operations

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:

Ternary conditional operations

The ternary conditional operator execute conditional instructions in the form if cond { choice_if_true } else { choice_if_false }.

name
symbol
type

Ternary operator

select

Ternary

The syntax is encrypted_condition.select(encrypted_choice_if_true, encrypted_choice_if_false). The valid encrypted_condition must be an encryption of 0 or 1.

The following example shows how to perform ternary conditional operations:

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:

Boolean Operations

Native homomorphic Booleans support the following common Boolean operations:

Last updated

Was this helpful?