Skip to content

Commit e953993

Browse files
committed
chore(doc): add strings feature to doctests
1 parent f3e3c2d commit e953993

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

tfhe/docs/tutorials/ascii_fhe_string.md

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# Homomorphic case changing on Ascii string
22

3-
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.
3+
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.
44

5-
An ASCII character is stored in 7 bits. To store an encrypted ASCII, we use the `FheUint8`:
5+
{% hint style="info" %}
6+
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](../guides/strings.md) for more information.
7+
{% endhint %}
8+
9+
10+
An ASCII character is stored in 7 bits. In this tutorial, we use the `FheUint8` to store an encrypted ASCII:
611

712
* The uppercase letters are in the range \[65, 90]
813
* The lowercase letters are in the range \[97, 122]
@@ -24,13 +29,10 @@ To use the `FheUint8` type, enable the `integer` feature:
2429
# Cargo.toml
2530

2631
[dependencies]
27-
# Default configuration for x86 Unix machines:
2832
tfhe = { version = "0.11.1", features = ["integer"] }
2933
```
3034

31-
Refer to the [installation guide](../getting\_started/installation.md) for other configurations.
32-
33-
The `FheAsciiString::encrypt` function performs data validation to ensure the input string contains only ASCII characters.
35+
The `MyFheString::encrypt` function performs data validation to ensure the input string contains only ASCII characters.
3436

3537
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:
3638

@@ -83,7 +85,7 @@ use tfhe::{generate_keys, set_server_key, ClientKey, ConfigBuilder, FheUint8};
8385

8486
const UP_LOW_DISTANCE: u8 = 32;
8587

86-
struct FheAsciiString {
88+
struct MyFheString {
8789
bytes: Vec<FheUint8>,
8890
}
8991

@@ -95,7 +97,7 @@ fn to_lower(c: &FheUint8) -> FheUint8 {
9597
c + FheUint8::cast_from(c.gt(64) & c.lt(91)) * UP_LOW_DISTANCE
9698
}
9799

98-
impl FheAsciiString {
100+
impl MyFheString {
99101
fn encrypt(string: &str, client_key: &ClientKey) -> Self {
100102
assert!(
101103
string.is_ascii(),
@@ -140,7 +142,7 @@ fn main() {
140142

141143
set_server_key(server_key);
142144

143-
let my_string = FheAsciiString::encrypt("Hello Zama, how is it going?", &client_key);
145+
let my_string = MyFheString::encrypt("Hello Zama, how is it going?", &client_key);
144146
let verif_string = my_string.decrypt(&client_key);
145147
println!("Start string: {verif_string}");
146148

@@ -155,3 +157,45 @@ fn main() {
155157
assert_eq!(verif_string, "hello zama, how is it going?");
156158
}
157159
```
160+
161+
## Using **TFHE-rs** strings feature
162+
This code can be greatly simplified by using the `strings` feature from **TFHE-rs**.
163+
164+
First, add the feature in your `Cargo.toml`
165+
```toml
166+
# Cargo.toml
167+
168+
[dependencies]
169+
tfhe = { version = "0.11.1", features = ["strings"] }
170+
```
171+
172+
The `FheAsciiString` type allows to simply do homomorphic case changing of encrypted strings (and much more!):
173+
```rust
174+
use tfhe::prelude::*;
175+
use tfhe::{generate_keys, set_server_key, ConfigBuilder, FheAsciiString};
176+
177+
fn main() {
178+
let config = ConfigBuilder::default().build();
179+
180+
let (client_key, server_key) = generate_keys(config);
181+
182+
set_server_key(server_key);
183+
184+
let my_string =
185+
FheAsciiString::try_encrypt("Hello Zama, how is it going?", &client_key).unwrap();
186+
let verif_string = my_string.decrypt(&client_key);
187+
println!("Start string: {verif_string}");
188+
189+
let my_string_upper = my_string.to_uppercase();
190+
let verif_string = my_string_upper.decrypt(&client_key);
191+
println!("Upper string: {verif_string}");
192+
assert_eq!(verif_string, "HELLO ZAMA, HOW IS IT GOING?");
193+
194+
let my_string_lower = my_string_upper.to_lowercase();
195+
let verif_string = my_string_lower.decrypt(&client_key);
196+
println!("Lower string: {verif_string}");
197+
assert_eq!(verif_string, "hello zama, how is it going?");
198+
}
199+
```
200+
201+
You can read more about this in the [FHE strings documentation](../guides/strings.md)

tfhe/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ mod js_on_wasm_api;
120120
feature = "shortint",
121121
feature = "boolean",
122122
feature = "integer",
123-
feature = "zk-pok"
123+
feature = "zk-pok",
124+
feature = "strings"
124125
))]
125126
mod test_user_docs;
126127

0 commit comments

Comments
 (0)