To use the TFHE-rs GPU backend in your project, add the following dependency in your Cargo.toml.
tfhe = { version ="0.11.1", features = ["boolean","shortint","integer","gpu"] }
For optimal performance when using TFHE-rs, run your code in release mode with the --release flag.
Supported platforms
TFHE-rs GPU backend is supported on Linux (x86, aarch64).
OS
x86
aarch64
Linux
Supported
Supported*
macOS
Unsupported
Unsupported*
Windows
Unsupported
Unsupported
A first example
Configuring and creating keys.
Comparing to the CPU example, GPU set up differs in the key creation, as detailed here
Here is a full example (combining the client and server parts):
use tfhe::{ConfigBuilder, set_server_key, FheUint8, ClientKey, CompressedServerKey};use tfhe::prelude::*;fnmain() {let config =ConfigBuilder::default().build();let client_key=ClientKey::generate(config);let compressed_server_key =CompressedServerKey::new(&client_key);let gpu_key = compressed_server_key.decompress_to_gpu();let clear_a =27u8;let clear_b =128u8;let a =FheUint8::encrypt(clear_a, &client_key);let b =FheUint8::encrypt(clear_b, &client_key);//Server-sideset_server_key(gpu_key);let result = a + b;//Client-sidelet decrypted_result:u8= result.decrypt(&client_key);let clear_result = clear_a + clear_b;assert_eq!(decrypted_result, clear_result);}
Beware that when the GPU feature is activated, when calling: let config = ConfigBuilder::default().build();, the cryptographic parameters differ from the CPU ones, used when the GPU feature is not activated. Indeed, TFHE-rs uses dedicated parameters for the GPU in order to achieve better performance.
Setting the keys
The configuration of the key is different from the CPU. More precisely, if both client and server keys are still generated by the client (which is assumed to run on a CPU), the server key has then to be decompressed by the server to be converted into the right format. To do so, the server should run this function: decompressed_to_gpu().
Once decompressed, the operations between CPU and GPU are identical.
Encryption
On the client-side, the method to encrypt the data is exactly the same than the CPU one, as shown in the following example:
let clear_a =27u8;let clear_b =128u8;let a =FheUint8::encrypt(clear_a, &client_key);let b =FheUint8::encrypt(clear_b, &client_key);
Computation
The server first need to set up its keys with set_server_key(gpu_key).
Then, homomorphic computations are performed using the same approach as the CPU operations.
//Server-sideset_server_key(gpu_key);let result = a + b;//Client-sidelet decrypted_result:u8= result.decrypt(&client_key);let clear_result = clear_a + clear_b;assert_eq!(decrypted_result, clear_result);
Decryption
Finally, the client decrypts the results using:
let decrypted_result:u8= result.decrypt(&client_key);
List of available operations
The GPU backend includes the following operations for both signed and unsigned encrypted integers:
name
symbol
Enc/Enc
Enc/ Int
Neg
-
N/A
Add
+
Sub
-
Mul
*
Div
/
Rem
%
Not
!
N/A
BitAnd
&
BitOr
|
BitXor
^
Shr
>>
Shl
<<
Rotate right
rotate_right
Rotate left
rotate_left
Min
min
Max
max
Greater than
gt
Greater or equal than
ge
Lower than
lt
Lower or equal than
le
Equal
eq
Cast (into dest type)
cast_into
N/A
Cast (from src type)
cast_from
N/A
Ternary operator
select
All operations follow the same syntax than the one described in here.
Multi-GPU support
TFHE-rs supports platforms with multiple GPUs. There is nothing to change in the code to execute on such platforms. To keep the API as user-friendly as possible, the configuration is automatically set, i.e., the user has no fine-grained control over the number of GPUs to be used.
Benchmark
Please refer to the GPU benchmarks for detailed performance benchmark results.
Warning
When measuring GPU times on your own on Linux, set the environment variable CUDA_MODULE_LOADING=EAGER to avoid CUDA API overheads during the first kernel execution.
Compressing ciphertexts after some homomorphic computation on the GPU
You can compress ciphertexts using the GPU, even after computations, just like on the CPU.
The way to do it is very similar to how it's done on the CPU. The following example shows how to compress and decompress a list containing 4 messages: