Native small homomorphic integer types (e.g., FheUint3 or FheUint4) easily compute various operations. In general, computing over encrypted data is as easy as computing over clear data, since the same operation symbol is used. The addition between two ciphertexts is done using the symbol + between two FheUint. Many operations can be computed between a clear value (i.e. a scalar) and a ciphertext.
In Rust native types, any operation is modular. In Rust, u8, computations are done modulus 2^8. The similar idea is applied for FheUintX, where operations are done modulus 2^X. In the type FheUint3, operations are done modulo 8.
Arithmetic operations.
Small homomorphic integer types support all common arithmetic operations, meaning +, -, x, /, mod.
The division operation implements a subtlety: since data is encrypted, it might be possible to compute a division by 0. In this case, the division is tweaked so that dividing by 0 returns 0.
use tfhe::prelude::*;use tfhe::{generate_keys, set_server_key, ConfigBuilder, FheUint3};fnmain() ->Result<(), Box<dyn std::error::Error>> {let config =ConfigBuilder::all_disabled().enable_default_uint3().build();let (keys, server_keys) =generate_keys(config);set_server_key(server_keys);let clear_a =7;let clear_b =3;letmut a =FheUint3::try_encrypt(clear_a, &keys)?;letmut b =FheUint3::try_encrypt(clear_b, &keys)?; a = a ^&b; b = b ^&a; a = a ^&b;let dec_a = a.decrypt(&keys);let dec_b = b.decrypt(&keys);// We homomorphically swapped values using bitwise operationsassert_eq!(dec_a, clear_b);assert_eq!(dec_b, clear_a);Ok(())}
Comparisons.
Small homomorphic integer types support comparison operations.
Due to some Rust limitations, it is not possible to overload the comparison symbols because of the inner definition of the operations. Rust expects to have a Boolean as an output, whereas a ciphertext encrypted result is returned when using homomorphic types.
You will need to use the different methods instead of using symbols for the comparisons. These methods follow the same naming conventions as the two standard Rust traits:
use tfhe::prelude::*;use tfhe::{generate_keys, set_server_key, ConfigBuilder, FheUint8};fnmain() ->Result<(), Box<dyn std::error::Error>> {let config =ConfigBuilder::all_disabled().enable_default_uint8().build();let (keys, server_keys) =generate_keys(config);set_server_key(server_keys);let clear_a =164;let clear_b =212;letmut a =FheUint8::try_encrypt(clear_a, &keys)?;letmut b =FheUint8::try_encrypt(clear_b, &keys)?; a = a ^&b; b = b ^&a; a = a ^&b;let dec_a:u8= a.decrypt(&keys);let dec_b:u8= b.decrypt(&keys);// We homomorphically swapped values using bitwise operationsassert_eq!(dec_a, clear_b);assert_eq!(dec_b, clear_a);Ok(())}
Comparisons.
Homomorphic integers support comparison operations. Since Rust does not allow the overloading of these operations, a simple function has been associated to each one.