|
| 1 | +# Noise squashing |
| 2 | + |
| 3 | +For security reasons, threshold decryption may require adding large amounts of random noise to ciphertext. Noise squashing is a technique applied beforehand to make space for that additional noise. Even after adding this extra random noise, decryption remains correct. |
| 4 | + |
| 5 | +**TFHE-rs**' High Level API provides APIs to do just that. In [advanced features](../../fhe-computation/advanced-features/noise-squashing.md), you can read about the CPU implementation of noise squashing. However, that operation can be accelerated through GPUs. This document describes how one can do that. |
| 6 | + |
| 7 | +## Configuration |
| 8 | + |
| 9 | +{% hint style="info" %} |
| 10 | +You can enable this feature using the flag: `--features=gpu` when building **TFHE-rs**. |
| 11 | +{% endhint %} |
| 12 | + |
| 13 | +## Example |
| 14 | + |
| 15 | +As with other operations, to enable GPU acceleration, one just needs to set the GPU server key as follows: |
| 16 | + |
| 17 | +```rust |
| 18 | +use tfhe::prelude::*; |
| 19 | +use tfhe::shortint::parameters::{ |
| 20 | + NOISE_SQUASHING_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, |
| 21 | + PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, |
| 22 | +}; |
| 23 | +use tfhe::*; |
| 24 | + |
| 25 | +// We use an identity function to verify FHE operations, it is fine in this context |
| 26 | +#[allow(clippy::eq_op)] |
| 27 | +pub fn main() { |
| 28 | + // Configure computations enabling the noise squashing capability. |
| 29 | + let config = |
| 30 | + ConfigBuilder::with_custom_parameters(PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128) |
| 31 | + .enable_noise_squashing(NOISE_SQUASHING_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128) |
| 32 | + .build(); |
| 33 | + |
| 34 | + // Generate the keys |
| 35 | + let cks = crate::ClientKey::generate(config); |
| 36 | + let sks = cks.generate_compressed_server_key(); |
| 37 | + |
| 38 | + // Set the GPU key once for our various examples |
| 39 | + set_server_key(sks.decompress_to_gpu()); |
| 40 | + |
| 41 | + // FheUint32 case |
| 42 | + let clear: u32 = 42; |
| 43 | + // Encrypt |
| 44 | + let enc = FheUint32::encrypt(clear, &cks); |
| 45 | + // Simulate a bitand on the blockchain |
| 46 | + let bitand = &enc & &enc; |
| 47 | + // Perform the noise squashing |
| 48 | + let squashed = bitand.squash_noise().unwrap(); |
| 49 | + |
| 50 | + // We don't perform the noise flooding, but here verify that the noise squashing preserves our |
| 51 | + // data |
| 52 | + let recovered: u32 = squashed.decrypt(&cks); |
| 53 | + |
| 54 | + assert_eq!(clear, recovered); |
| 55 | + |
| 56 | + // FheInt16 case |
| 57 | + let clear: i16 = -42; |
| 58 | + let enc = FheInt10::encrypt(clear, &cks); |
| 59 | + let bitand = &enc & &enc; |
| 60 | + let squashed = bitand.squash_noise().unwrap(); |
| 61 | + |
| 62 | + let recovered: i16 = squashed.decrypt(&cks); |
| 63 | + assert_eq!(clear, recovered); |
| 64 | + |
| 65 | + // Boolean case |
| 66 | + for clear in [false, true] { |
| 67 | + let enc = FheBool::encrypt(clear, &cks); |
| 68 | + let bitand = &enc & &enc; |
| 69 | + let squashed = bitand.squash_noise().unwrap(); |
| 70 | + |
| 71 | + let recovered: bool = squashed.decrypt(&cks); |
| 72 | + assert_eq!(clear, recovered); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +``` |
0 commit comments