Homomorphic Case Changing on Ascii String

The goal of this tutorial is to build a data type that represents a ASCII string in FHE while implementing the 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]

lower_case = upper_case + UP_LOW_DISTANCE <=> upper_case = lower_case - UP_LOW_DISTANCE

Where UP_LOW_DISTANCE = 32

Types and methods.

This type will hold the encrypted characters as a Vec<FheUint8> to implement the functions that change the case.

To use the FheUint8 type, the integer feature must be activated:

# Cargo.toml

[dependencies]
# Default configuration for x86 Unix machines:
tfhe = { version = "0.5.5", features = ["integer", "x86_64-unix"]}

Other configurations can be found here.

In the FheAsciiString::encrypt function, some data validation is done:

  • The input string can only contain ascii characters.

It is not possible to branch on an encrypted value, however it is possible to evaluate a boolean condition and use it to get the desired result. Checking if the 'char' is an uppercase letter to modify it to a lowercase can be done without using a branch, like this:

We can remove the branch this way:

On an homomorphic integer, this gives

The whole code is:

Last updated

Was this helpful?