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 ="0.11.1", 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:
#![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 }}