Homomorphic case changing on Ascii string

This tutorial demonstrates how to build your own data type that represents an ASCII string in Fully Homomorphic Encryption (FHE) by implementing to_lower and to_upper functions.

Since version 0.11, TFHE-rs has introduced the strings feature, which provides an easy to use FHE strings API. See the fhe strings guide for more information.

An ASCII character is stored in 7 bits. In this tutorial, we use the FheUint8 to store an encrypted ASCII:

  • The uppercase letters are in the range [65, 90]

  • The lowercase letters are in the range [97, 122]

The relationship between uppercase and lowercase letters is defined as follows:

  • lower_case = upper_case + UP_LOW_DISTANCE

  • upper_case = lower_case - UP_LOW_DISTANCE

Where UP_LOW_DISTANCE = 32

Types and methods

This type stores the encrypted characters as a Vec<FheUint8> to implement case conversion functions.

To use the FheUint8 type, enable the integer feature:

# Cargo.toml

[dependencies]
tfhe = { version = "~1.4.2", features = ["integer"] }

The MyFheString::encrypt function performs data validation to ensure the input string contains only ASCII characters.

In FHE operations, direct branching on encrypted values is not possible. However, you can evaluate a boolean condition to obtain the desired outcome. Here is an example to check and convert the 'char' to a lowercase without using a branch:

You can remove the branch this way:

This method can adapt to operations on homomorphic integers:

Full example:

Using TFHE-rs strings feature

This code can be greatly simplified by using the strings feature from TFHE-rs.

First, add the feature in your Cargo.toml

The FheAsciiString type allows to simply do homomorphic case changing of encrypted strings (and much more!):

You can read more about this in the FHE strings documentation

Last updated

Was this helpful?