tfhe 1.6.1

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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
use super::client_key::ClientKey;
use super::server_key::ServerKey;
use crate::integer::ciphertext::{Compactable, DataKind};
use crate::integer::encryption::{encrypt_words_radix_impl, KnowsMessageModulus};
use crate::integer::{
    ClientKey as IntegerClientKey, IntegerCiphertext, IntegerRadixCiphertext, RadixCiphertext,
    ServerKey as IntegerServerKey,
};
use crate::shortint::ciphertext::NotTrivialCiphertextError;
use crate::shortint::MessageModulus;
use crate::strings::backward_compatibility::{FheAsciiCharVersions, FheStringVersions};
use crate::strings::client_key::EncU16;
use crate::strings::N;
use rayon::iter::{IndexedParallelIterator, ParallelIterator};
use rayon::slice::ParallelSlice;
use serde::{Deserialize, Serialize};
use std::borrow::Borrow;
use tfhe_versionable::Versionize;

/// Represents a encrypted ASCII character.
#[derive(Clone, Serialize, Deserialize, Versionize)]
#[versionize(FheAsciiCharVersions)]
pub struct FheAsciiChar {
    pub enc_char: RadixCiphertext,
}

/// Represents a encrypted string made up of [`FheAsciiChar`]s.
#[derive(Clone, Serialize, Deserialize, Versionize)]
#[versionize(FheStringVersions)]
pub struct FheString {
    pub enc_string: Vec<FheAsciiChar>,
    pub padded: bool,
}

// For str functions that require unsigned integers as arguments

#[derive(Clone)]
pub enum UIntArg {
    Clear(u16),
    Enc(EncU16),
}

#[derive(Clone)]
pub struct ClearString {
    str: String,
}

impl ClearString {
    pub fn new(str: String) -> Self {
        assert!(str.is_ascii() && !str.contains('\0'));
        assert!(str.len() <= N);

        Self { str }
    }

    pub fn str(&self) -> &str {
        &self.str
    }
}

impl Compactable for &ClearString {
    fn compact_into(
        self,
        messages: &mut Vec<u64>,
        message_modulus: MessageModulus,
        num_blocks: Option<usize>,
    ) -> Option<DataKind> {
        let blocks_per_char = 7u32.div_ceil(message_modulus.0.ilog2());

        if let Some(n) = num_blocks {
            assert!(
                (n as u32).is_multiple_of(blocks_per_char),
                "Inconsistent num block would split the string inside a a character"
            );
        }

        // How many chars we have to write
        let n_chars = num_blocks.map_or(self.str.len(), |n_blocks| {
            n_blocks / blocks_per_char as usize
        });

        // First, write the chars we have at hand
        let n_real_chars = n_chars.min(self.str().len());
        for byte in &self.str.as_bytes()[..n_real_chars] {
            let mut byte = u64::from(*byte);
            for _ in 0..blocks_per_char {
                messages.push(byte % message_modulus.0);
                byte /= message_modulus.0;
            }
        }

        // Pad if necessary
        let padded = n_real_chars < n_chars;
        for _ in 0..n_chars.saturating_sub(n_real_chars) * blocks_per_char as usize {
            messages.push(0);
        }

        Some(DataKind::String {
            n_chars: n_chars as u32,
            padded,
        })
    }
}

impl crate::integer::ciphertext::CompactCiphertextListBuilder {
    pub fn push_string_with_padding(
        &mut self,
        clear_string: &ClearString,
        padding_count: u32,
    ) -> &mut Self {
        let message_modulus = self.pk.key.message_modulus();
        let blocks_per_char = 7u32.div_ceil(message_modulus.0.ilog2());
        let n = self.messages.len();

        let kind = clear_string
            .compact_into(
                &mut self.messages,
                message_modulus,
                Some((clear_string.str.len() + padding_count as usize) * blocks_per_char as usize),
            )
            .expect("Internal error: compact_into should return a kind");
        self.info.push(kind);

        let added_count = kind.num_blocks(message_modulus);
        assert_eq!(
            n + added_count,
            self.messages.len(),
            "Internal error: Incoherent number of blocks added"
        );

        self
    }

    pub fn push_string_with_fixed_size(
        &mut self,
        clear_string: &ClearString,
        size: u32,
    ) -> &mut Self {
        let message_modulus = self.pk.key.message_modulus();
        let blocks_per_char = 7u32.div_ceil(message_modulus.0.ilog2());
        let n = self.messages.len();

        let kind = clear_string
            .compact_into(
                &mut self.messages,
                message_modulus,
                Some((size * blocks_per_char) as usize),
            )
            .expect("Internal error: compact_into should return a kind");
        self.info.push(kind);

        let added_count = kind.num_blocks(message_modulus);
        assert_eq!(
            n + added_count,
            self.messages.len(),
            "Internal error: Incoherent number of blocks added"
        );

        self
    }
}

impl crate::integer::ciphertext::Expandable for FheString {
    fn from_expanded_blocks(
        mut blocks: Vec<crate::shortint::Ciphertext>,
        kind: DataKind,
    ) -> crate::Result<Self> {
        match kind {
            DataKind::String { n_chars, padded } => {
                if n_chars == 0 {
                    return Ok(Self::empty());
                }

                let Some(first_block) = blocks.first() else {
                    return Err(crate::error!(
                        "Invalid number of blocks for a string of {n_chars} chars, got 0 blocks"
                    ));
                };
                let n_blocks_per_chars = 7u32.div_ceil(first_block.message_modulus.0.ilog2());
                let expected_num_blocks = n_chars * n_blocks_per_chars;
                if expected_num_blocks != blocks.len() as u32 {
                    return Err(crate::error!("Invalid number of blocks for a string of {n_chars} chars, expected {expected_num_blocks}, got {}", blocks.len()));
                }

                let mut chars = Vec::with_capacity(n_chars as usize);
                for _ in 0..n_chars {
                    let char: Vec<_> = blocks.drain(..n_blocks_per_chars as usize).collect();
                    chars.push(FheAsciiChar {
                        enc_char: RadixCiphertext::from(char),
                    });
                }
                Ok(Self {
                    enc_string: chars,
                    padded,
                })
            }
            DataKind::Unsigned(_) => Err(crate::Error::new(
                "Tried to expand a string while a unsigned integer was stored".to_string(),
            )),
            DataKind::Signed(_) => Err(crate::Error::new(
                "Tried to expand a string while a signed integer was stored".to_string(),
            )),
            DataKind::Boolean => Err(crate::Error::new(
                "Tried to expand a string while a boolean was stored".to_string(),
            )),
        }
    }
}

impl crate::integer::ciphertext::Compressible for FheString {
    fn compress_into(self, messages: &mut Vec<crate::shortint::Ciphertext>) -> Option<DataKind> {
        let n_chars = self.chars().len() as u32;
        let padded = self.is_padded();

        for char in self.enc_string {
            for block in char.enc_char.blocks {
                messages.push(block);
            }
        }

        Some(DataKind::String { n_chars, padded })
    }
}

#[derive(Clone)]
pub enum GenericPattern {
    Clear(ClearString),
    Enc(FheString),
}

impl GenericPattern {
    pub fn as_ref(&self) -> GenericPatternRef<'_> {
        match self {
            Self::Clear(clear_string) => GenericPatternRef::Clear(clear_string),
            Self::Enc(fhe_string) => GenericPatternRef::Enc(fhe_string),
        }
    }
}

#[derive(Copy, Clone)]
pub enum GenericPatternRef<'a> {
    Clear(&'a ClearString),
    Enc(&'a FheString),
}

impl<'a> From<&'a ClearString> for GenericPatternRef<'a> {
    fn from(value: &'a ClearString) -> Self {
        Self::Clear(value)
    }
}

impl<'a> From<&'a FheString> for GenericPatternRef<'a> {
    fn from(value: &'a FheString) -> Self {
        Self::Enc(value)
    }
}

impl GenericPatternRef<'_> {
    pub fn to_owned(self) -> GenericPattern {
        match self {
            GenericPatternRef::Clear(clear_string) => GenericPattern::Clear(clear_string.clone()),
            GenericPatternRef::Enc(fhe_string) => GenericPattern::Enc(fhe_string.clone()),
        }
    }
}

impl FheAsciiChar {
    pub fn ciphertext(&self) -> &RadixCiphertext {
        &self.enc_char
    }

    pub fn ciphertext_mut(&mut self) -> &mut RadixCiphertext {
        &mut self.enc_char
    }

    pub fn null<T: Borrow<IntegerServerKey> + Sync>(sk: &ServerKey<T>) -> Self {
        let sk_integer = sk.inner();

        Self {
            enc_char: sk_integer.create_trivial_zero_radix(sk.num_ascii_blocks()),
        }
    }

    pub fn is_trivial(&self) -> bool {
        self.enc_char.is_trivial()
    }

    pub fn decrypt_trivial(&self) -> Result<u8, NotTrivialCiphertextError> {
        self.enc_char.decrypt_trivial()
    }
}

impl FheString {
    #[cfg(test)]
    pub fn new_trivial<T: Borrow<IntegerClientKey>>(
        client_key: &ClientKey<T>,
        str: &str,
        padding: Option<u32>,
    ) -> Self {
        client_key.trivial_encrypt_ascii(str, padding)
    }

    /// Constructs a new `FheString` from a plaintext string, a [`ClientKey`] and an optional
    /// padding length.
    ///
    /// Utilizes [`ClientKey::encrypt_ascii`] for the encryption.
    ///
    /// # Panics
    ///
    /// This function will panic if the provided string is not ASCII.
    pub fn new<T: Borrow<IntegerClientKey>>(
        client_key: &ClientKey<T>,
        str: &str,
        padding: Option<u32>,
    ) -> Self {
        client_key.encrypt_ascii(str, padding)
    }

    #[cfg(test)]
    pub fn print_trivial(&self) {
        print!("pad: {}, chars: [", self.padded);

        for i in &self.enc_string {
            print!("[");
            for j in &i.enc_char.blocks {
                let k = j.decrypt_trivial().unwrap();

                print!("{k},");
            }
            print!("], ");
        }

        println!("]");
    }

    pub fn trivial<T: Borrow<IntegerServerKey> + Sync>(
        server_key: &ServerKey<T>,
        str: &str,
    ) -> Self {
        server_key.trivial_encrypt_ascii(str, None)
    }

    pub fn is_trivial(&self) -> bool {
        self.chars().iter().all(FheAsciiChar::is_trivial)
    }

    pub fn decrypt_trivial(&self) -> Result<String, NotTrivialCiphertextError> {
        let mut bytes = Vec::with_capacity(self.chars().len());
        for enc_char in self.chars() {
            let clear = enc_char.decrypt_trivial()?;
            if clear == b'\0' {
                break;
            }
            bytes.push(clear);
        }
        Ok(String::from_utf8(bytes).expect("String is not valid ASCII"))
    }

    pub fn chars(&self) -> &[FheAsciiChar] {
        &self.enc_string
    }

    pub fn chars_mut(&mut self) -> &mut [FheAsciiChar] {
        &mut self.enc_string
    }

    pub fn chars_vec(&mut self) -> &mut Vec<FheAsciiChar> {
        &mut self.enc_string
    }

    pub fn is_padded(&self) -> bool {
        self.padded
    }

    pub fn set_is_padded(&mut self, to: bool) {
        self.padded = to;
    }

    // Converts a `RadixCiphertext` to a `FheString`, building a `FheAsciiChar` for each
    // num_ascii_blocks blocks.
    pub fn from_uint(uint: RadixCiphertext, padded: bool) -> Self {
        if uint.blocks().is_empty() {
            return Self {
                enc_string: vec![],
                padded,
            };
        }

        assert_eq!(
            uint.blocks()[0].message_modulus.0,
            uint.blocks()[0].carry_modulus.0
        );

        let num_blocks = num_ascii_blocks(uint.blocks()[0].message_modulus);

        assert_eq!(uint.blocks.len() % num_blocks, 0);

        let enc_string = uint
            .into_blocks()
            .par_chunks_exact(num_blocks)
            .rev()
            .map(|bytes| FheAsciiChar {
                enc_char: RadixCiphertext::from_blocks(bytes.to_vec()),
            })
            .collect();

        Self { enc_string, padded }
    }

    // Converts a `FheString` to a `RadixCiphertext`, taking 4 blocks for each `FheAsciiChar`.
    // We can then use a single large uint, that represents a string, in tfhe-rs operations.
    pub fn to_uint(&self) -> RadixCiphertext {
        self.clone().into_uint()
    }

    pub fn into_uint(self) -> RadixCiphertext {
        let blocks: Vec<_> = self
            .enc_string
            .into_iter()
            .rev()
            .flat_map(|c| c.enc_char.into_blocks())
            .collect();

        RadixCiphertext::from_blocks(blocks)
    }

    /// Makes the string padded. Useful for when a string is potentially padded and we need to
    /// ensure it's actually padded.
    pub fn append_null<T: Borrow<IntegerServerKey> + Sync>(&mut self, sk: &ServerKey<T>) {
        let null = FheAsciiChar::null(sk);

        self.enc_string.push(null);

        self.padded = true;
    }

    pub fn len(&self) -> usize {
        self.chars().len()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0 || (self.is_padded() && self.len() == 1)
    }

    pub fn empty() -> Self {
        Self {
            enc_string: vec![],
            padded: false,
        }
    }
}

pub(super) fn num_ascii_blocks(message_modulus: MessageModulus) -> usize {
    let message_modulus = message_modulus.0;

    assert!(message_modulus.is_power_of_two());

    assert_eq!(8 % message_modulus.ilog2(), 0);

    8 / message_modulus.ilog2() as usize
}

/// Creates a trivial encryption of the ascii string `str`
///
/// * key: typically a shortint::ClientKey/ServerKey
/// * encrypt: the method of the `key` used to create a trivial block
///
/// # Panics
///
/// If the string is not ascii or contains null chars ('\0')
pub(in crate::strings) fn trivial_encrypt_ascii<BlockKey, F>(
    key: &BlockKey,
    encrypt_block: &F,
    str: &str,
    padding: Option<u32>,
) -> FheString
where
    BlockKey: KnowsMessageModulus,
    F: Fn(&BlockKey, u64) -> crate::shortint::Ciphertext,
{
    assert!(str.is_ascii() & !str.contains('\0'));

    let padded = padding.is_some_and(|p| p != 0);

    let num_blocks = num_ascii_blocks(key.message_modulus());

    let mut enc_string: Vec<_> = str
        .bytes()
        .map(|char| FheAsciiChar {
            enc_char: encrypt_words_radix_impl(key, char, num_blocks, encrypt_block),
        })
        .collect();

    // Optional padding
    if let Some(count) = padding {
        let null = (0..count).map(|_| FheAsciiChar {
            enc_char: encrypt_words_radix_impl(key, 0u8, num_blocks, encrypt_block),
        });

        enc_string.extend(null);
    }

    FheString { enc_string, padded }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::integer::ClientKey as IntegerClientKey;
    use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128;

    #[test]
    fn test_uint_conversion() {
        let ck = IntegerClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128);

        let ck = ClientKey::new(ck);

        let str =
            "Los Sheikah fueron originalmente criados de la Diosa Hylia antes del sellado del \
            Heraldo de la Muerte.";

        let enc = FheString::new(&ck, str, Some(7));

        let uint = enc.to_uint();

        let converted = FheString::from_uint(uint, true);

        let dec = ck.decrypt_ascii(&converted);

        assert_eq!(dec, str);

        let uint_into = enc.into_uint();

        let converted = FheString::from_uint(uint_into, true);

        let dec = ck.decrypt_ascii(&converted);

        assert_eq!(dec, str);
    }
}