Homomorphic case changing on Ascii string

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

An ASCII character is stored in 7 bits. To store an encrypted ASCII, we use the FheUint8:

  • 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]
# Default configuration for x86 Unix machines:
tfhe = { version = "0.7.5", features = ["integer", "x86_64-unix"]}

Refer to the installation guide for other configurations.

The FheAsciiString::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:

Last updated

Was this helpful?