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.8.7", features = ["integer","x86_64-unix"]}
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:
#![allow(dead_code)]const UP_LOW_DISTANCE:u8=32;fnto_lower(c:u8) ->u8 {if c >64&& c <91 { c + UP_LOW_DISTANCE } else { c }}