tfhe 1.6.0

TFHE-rs is a fully homomorphic encryption (FHE) library that implements Zama's variant of TFHE.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
use std::marker::PhantomData;

use tfhe_versionable::Versionize;

use crate::backward_compatibility::integers::{
    CompressedFheUintVersions, CompressedRadixCiphertextVersions,
};
use crate::conformance::ParameterSetConformant;
use crate::core_crypto::prelude::UnsignedNumeric;
use crate::high_level_api::integers::unsigned::base::{FheUint, FheUintId};
use crate::high_level_api::keys::InternalServerKey;
use crate::high_level_api::re_randomization::ReRandomizationMetadata;
use crate::high_level_api::traits::{FheTryEncrypt, Tagged};
use crate::high_level_api::{global_state, ClientKey};
use crate::integer::block_decomposition::DecomposableInto;
use crate::integer::ciphertext::{
    CompressedModulusSwitchedRadixCiphertext,
    CompressedModulusSwitchedRadixCiphertextConformanceParams,
    CompressedRadixCiphertext as IntegerCompressedRadixCiphertext,
};
use crate::named::Named;
use crate::shortint::AtomicPatternParameters;
use crate::{ServerKey, Tag};

/// Compressed [FheUint]
///
/// Meant to save in storage space / transfer.
///
/// - A Compressed type must be decompressed using [decompress](Self::decompress) before it can be
///   used.
/// - It is not possible to compress an existing [FheUint]. compression can only be achieved at
///   encryption time by a [ClientKey]
///
/// # Example
///
/// ```rust
/// use tfhe::prelude::*;
/// use tfhe::{generate_keys, CompressedFheUint32, ConfigBuilder};
///
/// let (client_key, _) = generate_keys(ConfigBuilder::default());
/// let compressed = CompressedFheUint32::encrypt(u32::MAX, &client_key);
///
/// let decompressed = compressed.decompress();
/// let decrypted: u32 = decompressed.decrypt(&client_key);
/// assert_eq!(decrypted, u32::MAX);
/// ```
#[derive(Clone, serde::Serialize, serde::Deserialize, Versionize)]
#[versionize(CompressedFheUintVersions)]
pub struct CompressedFheUint<Id>
where
    Id: FheUintId,
{
    pub(in crate::high_level_api::integers) ciphertext: CompressedRadixCiphertext,
    pub(in crate::high_level_api::integers) id: Id,
    pub(crate) tag: Tag,
}

impl<Id> Tagged for CompressedFheUint<Id>
where
    Id: FheUintId,
{
    fn tag(&self) -> &Tag {
        &self.tag
    }

    fn tag_mut(&mut self) -> &mut Tag {
        &mut self.tag
    }
}

impl<Id> CompressedFheUint<Id>
where
    Id: FheUintId,
{
    pub(in crate::high_level_api) fn new(inner: CompressedRadixCiphertext, tag: Tag) -> Self {
        Self {
            ciphertext: inner,
            id: Id::default(),
            tag,
        }
    }

    pub fn into_raw_parts(self) -> (CompressedRadixCiphertext, Id, Tag) {
        let Self {
            ciphertext,
            id,
            tag,
        } = self;
        (ciphertext, id, tag)
    }

    pub fn from_raw_parts(ciphertext: CompressedRadixCiphertext, id: Id, tag: Tag) -> Self {
        Self {
            ciphertext,
            id,
            tag,
        }
    }
}

impl<Id> CompressedFheUint<Id>
where
    Id: FheUintId,
{
    /// Decompress to a [FheUint]
    ///
    /// See [CompressedFheUint] example.
    pub fn decompress(&self) -> FheUint<Id> {
        let inner = match &self.ciphertext {
            CompressedRadixCiphertext::Seeded(ct) => ct.decompress(),
            CompressedRadixCiphertext::ModulusSwitched(ct) => {
                global_state::with_internal_keys(|keys| match keys {
                    InternalServerKey::Cpu(cpu_key) => {
                        cpu_key.pbs_key().decompress_parallelized(ct)
                    }
                    #[cfg(feature = "gpu")]
                    InternalServerKey::Cuda(_) => {
                        panic!("decompress() on FheUint is not supported on GPU, use a CompressedCiphertextList instead");
                    }
                    #[cfg(feature = "hpu")]
                    InternalServerKey::Hpu(_) => {
                        panic!("decompress() on FheUint is not supported on HPU devices");
                    }
                })
            }
        };

        let mut ciphertext =
            FheUint::new(inner, self.tag.clone(), ReRandomizationMetadata::default());

        ciphertext.move_to_device_of_server_key_if_set();
        ciphertext
    }
}

impl<Id, T> FheTryEncrypt<T, ClientKey> for CompressedFheUint<Id>
where
    Id: FheUintId,
    T: DecomposableInto<u64> + UnsignedNumeric,
{
    type Error = crate::Error;

    fn try_encrypt(value: T, key: &ClientKey) -> Result<Self, Self::Error> {
        let inner = key
            .key
            .key
            .encrypt_radix_compressed(value, Id::num_blocks(key.message_modulus()));
        Ok(Self::new(
            CompressedRadixCiphertext::Seeded(inner),
            key.tag.clone(),
        ))
    }
}

#[derive(Copy, Clone)]
pub struct CompressedFheUintConformanceParams<Id: FheUintId> {
    pub(crate) params: CompressedRadixCiphertextConformanceParams,
    pub(crate) id: PhantomData<Id>,
}

impl<Id: FheUintId, P: Into<AtomicPatternParameters>> From<P>
    for CompressedFheUintConformanceParams<Id>
{
    fn from(params: P) -> Self {
        let params = params.into();
        Self {
            params: CompressedRadixCiphertextConformanceParams(
                CompressedModulusSwitchedRadixCiphertextConformanceParams {
                    shortint_params: params.to_compressed_modswitched_conformance_param(),
                    num_blocks_per_integer: Id::num_blocks(params.message_modulus()),
                },
            ),
            id: PhantomData,
        }
    }
}

impl<Id: FheUintId> From<&ServerKey> for CompressedFheUintConformanceParams<Id> {
    fn from(sk: &ServerKey) -> Self {
        Self {
            params: CompressedRadixCiphertextConformanceParams(
                CompressedModulusSwitchedRadixCiphertextConformanceParams {
                    shortint_params: sk
                        .key
                        .pbs_key()
                        .key
                        .compressed_modswitched_conformance_params(),
                    num_blocks_per_integer: Id::num_blocks(sk.key.pbs_key().message_modulus()),
                },
            ),
            id: PhantomData,
        }
    }
}

impl<Id: FheUintId> ParameterSetConformant for CompressedFheUint<Id> {
    type ParameterSet = CompressedFheUintConformanceParams<Id>;

    fn is_conformant(&self, params: &CompressedFheUintConformanceParams<Id>) -> bool {
        let Self {
            ciphertext,
            id: _,
            tag: _,
        } = self;

        ciphertext.is_conformant(&params.params)
    }
}

impl<Id: FheUintId> Named for CompressedFheUint<Id> {
    const NAME: &'static str = "high_level_api::CompressedFheUint";
}

#[derive(Clone, serde::Serialize, serde::Deserialize, Versionize)]
#[versionize(CompressedRadixCiphertextVersions)]
pub enum CompressedRadixCiphertext {
    Seeded(IntegerCompressedRadixCiphertext),
    ModulusSwitched(CompressedModulusSwitchedRadixCiphertext),
}

#[derive(Copy, Clone)]
pub struct CompressedRadixCiphertextConformanceParams(
    pub(crate) CompressedModulusSwitchedRadixCiphertextConformanceParams,
);

impl ParameterSetConformant for CompressedRadixCiphertext {
    type ParameterSet = CompressedRadixCiphertextConformanceParams;
    fn is_conformant(&self, params: &CompressedRadixCiphertextConformanceParams) -> bool {
        match self {
            Self::Seeded(ct) => ct.is_conformant(&params.0.into()),
            Self::ModulusSwitched(ct) => ct.is_conformant(&params.0),
        }
    }
}

impl<Id> FheUint<Id>
where
    Id: FheUintId,
{
    pub fn compress(&self) -> CompressedFheUint<Id> {
        global_state::with_internal_keys(|keys| match keys {
            InternalServerKey::Cpu(cpu_key) => {
                let ciphertext = CompressedRadixCiphertext::ModulusSwitched(
                    cpu_key
                        .pbs_key()
                        .switch_modulus_and_compress_parallelized(&self.ciphertext.on_cpu()),
                );
                CompressedFheUint::new(ciphertext, self.tag.clone())
            }
            #[cfg(feature = "gpu")]
            InternalServerKey::Cuda(_) => {
                panic!("compress() on FheUint is not supported on GPU, use a CompressedCiphertextList instead");
            }
            #[cfg(feature = "hpu")]
            InternalServerKey::Hpu(_) => {
                panic!("compress() on FheUint is not supported on HPU devices");
            }
        })
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::core_crypto::prelude::UnsignedInteger;
    use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128;
    use crate::shortint::{CiphertextModulus, CompressedCiphertext};
    use crate::{generate_keys, set_server_key, CompressedFheUint8, ConfigBuilder};
    use rand::{thread_rng, Rng};

    impl<Id> CompressedFheUint<Id>
    where
        Id: FheUintId,
    {
        fn seeded_blocks(&self) -> &Vec<CompressedCiphertext> {
            match &self.ciphertext {
                CompressedRadixCiphertext::Seeded(ciphertext) => &ciphertext.blocks,
                CompressedRadixCiphertext::ModulusSwitched(_) => {
                    panic!("Accessor does not support ModulusSwitched variant")
                }
            }
        }
        fn seeded_blocks_mut(&mut self) -> &mut Vec<CompressedCiphertext> {
            match &mut self.ciphertext {
                CompressedRadixCiphertext::Seeded(ciphertext) => &mut ciphertext.blocks,
                CompressedRadixCiphertext::ModulusSwitched(_) => {
                    panic!("Accessor does not support ModulusSwitched variant")
                }
            }
        }
    }

    type IndexedParameterAccessor<Ct, T> = dyn Fn(usize, &mut Ct) -> &mut T;

    type IndexedParameterModifier<'a, Ct> = dyn Fn(usize, &mut Ct) + 'a;

    fn change_parameters<Ct, T: UnsignedInteger>(
        func: &IndexedParameterAccessor<Ct, T>,
    ) -> [Box<IndexedParameterModifier<'_, Ct>>; 3] {
        [
            Box::new(|i, ct| *func(i, ct) = T::ZERO),
            Box::new(|i, ct| *func(i, ct) = func(i, ct).wrapping_add(T::ONE)),
            Box::new(|i, ct| *func(i, ct) = func(i, ct).wrapping_sub(T::ONE)),
        ]
    }

    #[test]
    fn test_invalid_generic_compressed_integer() {
        type Ct = CompressedFheUint8;

        let config = ConfigBuilder::default().build();

        let (client_key, _server_key) = generate_keys(config);

        let ct = CompressedFheUint8::try_encrypt(0_u64, &client_key).unwrap();

        assert!(ct.is_conformant(&CompressedFheUintConformanceParams::from(
            PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128
        )));

        let breaker_lists = [
            change_parameters(&|i: usize, ct: &mut Ct| {
                &mut ct.seeded_blocks_mut()[i].ct.get_mut_lwe_size().0
            }),
            change_parameters(&|i: usize, ct: &mut Ct| {
                &mut ct.seeded_blocks_mut()[i].message_modulus.0
            }),
            change_parameters(&|i: usize, ct: &mut Ct| {
                &mut ct.seeded_blocks_mut()[i].carry_modulus.0
            }),
            change_parameters(&|i: usize, ct: &mut Ct| ct.seeded_blocks_mut()[i].degree.as_mut()),
        ];

        for breaker_list in breaker_lists {
            for breaker in breaker_list {
                for i in 0..ct.seeded_blocks().len() {
                    let mut ct_clone = ct.clone();

                    breaker(i, &mut ct_clone);

                    assert!(
                        !ct_clone.is_conformant(&CompressedFheUintConformanceParams::from(
                            PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128
                        ))
                    );
                }
            }
        }

        let breakers2: Vec<&IndexedParameterModifier<'_, Ct>> = vec![
            &|i, ct: &mut Ct| {
                *ct.seeded_blocks_mut()[i].ct.get_mut_ciphertext_modulus() =
                    CiphertextModulus::try_new_power_of_2(1).unwrap();
            },
            &|i, ct: &mut Ct| {
                *ct.seeded_blocks_mut()[i].ct.get_mut_ciphertext_modulus() =
                    CiphertextModulus::try_new(3).unwrap();
            },
            &|_i, ct: &mut Ct| {
                ct.seeded_blocks_mut().pop();
            },
            &|i, ct: &mut Ct| {
                let value = ct.seeded_blocks_mut()[i].clone();
                ct.seeded_blocks_mut().push(value);
            },
        ];

        for breaker in breakers2 {
            for i in 0..ct.seeded_blocks().len() {
                let mut ct_clone = ct.clone();

                breaker(i, &mut ct_clone);

                assert!(
                    !ct_clone.is_conformant(&CompressedFheUintConformanceParams::from(
                        PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128
                    ))
                );
            }
        }
    }

    #[test]
    fn test_valid_generic_compressed_integer() {
        let config = ConfigBuilder::default().build();

        let (client_key, server_key) = generate_keys(config);

        set_server_key(server_key);

        let ct = CompressedFheUint8::try_encrypt(0_u64, &client_key).unwrap();

        assert!(ct.is_conformant(&CompressedFheUintConformanceParams::from(
            PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128
        )));

        let mut rng = thread_rng();

        let num_blocks = ct.seeded_blocks().len();

        for _ in 0..10 {
            let mut ct_clone = ct.clone();

            for i in 0..num_blocks {
                *ct_clone.seeded_blocks_mut()[i].ct.get_mut_data() = rng.gen::<u64>();

                match &mut ct_clone.seeded_blocks_mut()[i]
                    .ct
                    .get_mut_compressed_seed()
                    .inner
                    .seed
                {
                    tfhe_csprng::seeders::SeedKind::Ctr(seed) => {
                        seed.0 = rng.gen::<u128>();
                    }
                    tfhe_csprng::seeders::SeedKind::Xof(xof_seed) => {
                        *xof_seed = tfhe_csprng::seeders::XofSeed::new_u128(
                            rng.gen::<u128>(),
                            *b"TFHEtest",
                        );
                    }
                }
            }
            assert!(
                ct_clone.is_conformant(&CompressedFheUintConformanceParams::from(
                    PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128
                ))
            );

            let mut ct_clone_decompressed = ct_clone.decompress();

            ct_clone_decompressed += &ct_clone_decompressed.clone();
        }
    }
}