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
use crate::integer::{BooleanBlock, ServerKey as IntegerServerKey};
use crate::strings::ciphertext::{FheString, GenericPatternRef, UIntArg};
use crate::strings::server_key::pattern::split::{
SplitInternal, SplitNInternal, SplitNoLeading, SplitNoTrailing, SplitType,
};
use crate::strings::server_key::{FheStringIterator, ServerKey};
use std::borrow::Borrow;
pub struct RSplit {
internal: SplitInternal,
}
pub struct Split {
internal: SplitInternal,
}
pub struct SplitInclusive {
internal: SplitNoTrailing,
}
pub struct RSplitN {
internal: SplitNInternal,
}
pub struct SplitN {
internal: SplitNInternal,
}
pub struct SplitTerminator {
internal: SplitNoTrailing,
}
pub struct RSplitTerminator {
internal: SplitNoLeading,
}
impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
/// Creates an iterator of encrypted substrings by splitting the original encrypted string based
/// on a specified pattern (either encrypted or clear).
///
/// The iterator, of type `Split`, can be used to sequentially retrieve the substrings. Each
/// call to `next` on the iterator returns a tuple with the next split substring as an encrypted
/// string and a boolean indicating `Some` (true) or `None` (false).
///
/// The pattern to search for can be specified as either `GenericPatternRef::Clear` for a clear
/// string or `GenericPatternRef::Enc` for an encrypted string.
///
/// # Examples
///
/// ```rust
/// use tfhe::integer::{ClientKey, ServerKey};
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128;
/// use tfhe::strings::ciphertext::{FheString, GenericPattern};
/// use tfhe::strings::server_key::FheStringIterator;
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128);
/// let sk = ServerKey::new_radix_server_key(&ck);
/// let ck = tfhe::strings::ClientKey::new(ck);
/// let sk = tfhe::strings::ServerKey::new(sk);
/// let (s, pat) = ("hello ", " ");
///
/// let enc_s = FheString::new(&ck, s, None);
/// let enc_pat = GenericPattern::Enc(FheString::new(&ck, pat, None));
///
/// let mut split_iter = sk.split(&enc_s, enc_pat.as_ref());
/// let (first_item, first_is_some) = split_iter.next(&sk);
/// let (second_item, second_is_some) = split_iter.next(&sk);
/// let (_, no_more_items) = split_iter.next(&sk); // Attempting to get a third item
///
/// let first_decrypted = ck.decrypt_ascii(&first_item);
/// let first_is_some = ck.inner().decrypt_bool(&first_is_some);
/// let second_decrypted = ck.decrypt_ascii(&second_item);
/// let second_is_some = ck.inner().decrypt_bool(&second_is_some);
/// let no_more_items = ck.inner().decrypt_bool(&no_more_items);
///
/// assert_eq!(first_decrypted, "hello");
/// assert!(first_is_some); // There is a first item
/// assert_eq!(second_decrypted, "");
/// assert!(second_is_some); // There is a second item
/// assert!(!no_more_items); // No more items in the iterator
/// ```
pub fn split(&self, str: &FheString, pat: GenericPatternRef<'_>) -> Split {
let internal = self.split_internal(str, pat, SplitType::Split);
Split { internal }
}
/// Creates an iterator of encrypted substrings by splitting the original encrypted string from
/// the end based on a specified pattern (either encrypted or clear).
///
/// The iterator, of type `RSplit`, can be used to sequentially retrieve the substrings in
/// reverse order. Each call to `next` on the iterator returns a tuple with the next split
/// substring as an encrypted string and a boolean indicating `Some` (true) or `None` (false).
///
/// The pattern to search for can be specified as either `GenericPatternRef::Clear` for a clear
/// string or `GenericPatternRef::Enc` for an encrypted string.
///
/// # Examples
///
/// ```rust
/// use tfhe::integer::{ClientKey, ServerKey};
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128;
/// use tfhe::strings::ciphertext::{FheString, GenericPattern};
/// use tfhe::strings::server_key::FheStringIterator;
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128);
/// let sk = ServerKey::new_radix_server_key(&ck);
/// let ck = tfhe::strings::ClientKey::new(ck);
/// let sk = tfhe::strings::ServerKey::new(sk);
/// let (s, pat) = ("hello ", " ");
///
/// let enc_s = FheString::new(&ck, s, None);
/// let enc_pat = GenericPattern::Enc(FheString::new(&ck, pat, None));
///
/// let mut rsplit_iter = sk.rsplit(&enc_s, enc_pat.as_ref());
/// let (last_item, last_is_some) = rsplit_iter.next(&sk);
/// let (second_last_item, second_last_is_some) = rsplit_iter.next(&sk);
/// let (_, no_more_items) = rsplit_iter.next(&sk); // Attempting to get a third item
///
/// let last_decrypted = ck.decrypt_ascii(&last_item);
/// let last_is_some = ck.inner().decrypt_bool(&last_is_some);
/// let second_last_decrypted = ck.decrypt_ascii(&second_last_item);
/// let second_last_is_some = ck.inner().decrypt_bool(&second_last_is_some);
/// let no_more_items = ck.inner().decrypt_bool(&no_more_items);
///
/// assert_eq!(last_decrypted, "");
/// assert!(last_is_some); // The last item is empty
/// assert_eq!(second_last_decrypted, "hello");
/// assert!(second_last_is_some); // The second last item is "hello"
/// assert!(!no_more_items); // No more items in the reverse iterator
/// ```
pub fn rsplit(&self, str: &FheString, pat: GenericPatternRef<'_>) -> RSplit {
let internal = self.split_internal(str, pat, SplitType::RSplit);
RSplit { internal }
}
/// Creates an iterator of encrypted substrings by splitting the original encrypted string based
/// on a specified pattern (either encrypted or clear), limited to at most `n` results.
///
/// The `n` is specified by a `UIntArg`, which can be either `Clear` or `Enc`. The iterator, of
/// type `SplitN`, can be used to sequentially retrieve the substrings. Each call to `next` on
/// the iterator returns a tuple with the next split substring as an encrypted string and a
/// boolean indicating `Some` (true) or `None` (false).
///
/// The pattern to search for can be specified as either `GenericPatternRef::Clear` for a clear
/// string or `GenericPatternRef::Enc` for an encrypted string.
///
/// # Examples
///
/// ```rust
/// use tfhe::integer::{ClientKey, ServerKey};
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128;
/// use tfhe::strings::ciphertext::{FheString, GenericPattern, UIntArg};
/// use tfhe::strings::server_key::FheStringIterator;
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128);
/// let sk = ServerKey::new_radix_server_key(&ck);
/// let ck = tfhe::strings::ClientKey::new(ck);
/// let sk = tfhe::strings::ServerKey::new(sk);
/// let (s, pat) = ("hello world", " ");
///
/// let enc_s = FheString::new(&ck, s, None);
/// let enc_pat = GenericPattern::Enc(FheString::new(&ck, pat, None));
///
/// // Using Clear count
/// let clear_count = UIntArg::Clear(1);
/// let mut splitn_iter = sk.splitn(&enc_s, enc_pat.as_ref(), clear_count);
/// let (first_item, first_is_some) = splitn_iter.next(&sk);
/// let (_, no_more_items) = splitn_iter.next(&sk); // Attempting to get a second item
///
/// let first_decrypted = ck.decrypt_ascii(&first_item);
/// let first_is_some = ck.inner().decrypt_bool(&first_is_some);
/// let no_more_items = ck.inner().decrypt_bool(&no_more_items);
///
/// // We get the whole str as n is 1
/// assert_eq!(first_decrypted, "hello world");
/// assert!(first_is_some);
/// assert!(!no_more_items);
///
/// // Using Encrypted count
/// let max = 2; // Restricts the range of enc_n to 0..=max
/// let enc_n = ck.encrypt_u16(1, Some(max));
/// let enc_count = UIntArg::Enc(enc_n);
/// let _splitn_iter_enc = sk.splitn(&enc_s, enc_pat.as_ref(), enc_count);
/// // Similar usage as with Clear count
/// ```
pub fn splitn(&self, str: &FheString, pat: GenericPatternRef<'_>, n: UIntArg) -> SplitN {
let internal = self.splitn_internal(str, pat, n, SplitType::Split);
SplitN { internal }
}
/// Creates an iterator of encrypted substrings by splitting the original encrypted string from
/// the end based on a specified pattern (either encrypted or clear), limited to at most `n`
/// results.
///
/// The `n` is specified by a `UIntArg`, which can be either `Clear` or `Enc`. The iterator, of
/// type `RSplitN`, can be used to sequentially retrieve the substrings in reverse order. Each
/// call to `next` on the iterator returns a tuple with the next split substring as an encrypted
/// string and a boolean indicating `Some` (true) or `None` (false).
///
/// The pattern to search for can be specified as either `GenericPatternRef::Clear` for a clear
/// string or `GenericPatternRef::Enc` for an encrypted string.
///
/// # Examples
///
/// ```rust
/// use tfhe::integer::{ClientKey, ServerKey};
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128;
/// use tfhe::strings::ciphertext::{FheString, GenericPattern, UIntArg};
/// use tfhe::strings::server_key::FheStringIterator;
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128);
/// let sk = ServerKey::new_radix_server_key(&ck);
/// let ck = tfhe::strings::ClientKey::new(ck);
/// let sk = tfhe::strings::ServerKey::new(sk);
/// let (s, pat) = ("hello world", " ");
///
/// let enc_s = FheString::new(&ck, s, None);
/// let enc_pat = GenericPattern::Enc(FheString::new(&ck, pat, None));
///
/// // Using Clear count
/// let clear_count = UIntArg::Clear(1);
/// let mut rsplitn_iter = sk.rsplitn(&enc_s, enc_pat.as_ref(), clear_count);
/// let (last_item, last_is_some) = rsplitn_iter.next(&sk);
/// let (_, no_more_items) = rsplitn_iter.next(&sk); // Attempting to get a second item
///
/// let last_decrypted = ck.decrypt_ascii(&last_item);
/// let last_is_some = ck.inner().decrypt_bool(&last_is_some);
/// let no_more_items = ck.inner().decrypt_bool(&no_more_items);
///
/// // We get the whole str as n is 1
/// assert_eq!(last_decrypted, "hello world");
/// assert!(last_is_some);
/// assert!(!no_more_items);
///
/// // Using Encrypted count
/// let max = 2; // Restricts the range of enc_n to 0..=max
/// let enc_n = ck.encrypt_u16(1, Some(max));
/// let enc_count = UIntArg::Enc(enc_n);
/// let _rsplitn_iter_enc = sk.rsplitn(&enc_s, enc_pat.as_ref(), enc_count);
/// // Similar usage as with Clear count
/// ```
pub fn rsplitn(&self, str: &FheString, pat: GenericPatternRef<'_>, n: UIntArg) -> RSplitN {
let internal = self.splitn_internal(str, pat, n, SplitType::RSplit);
RSplitN { internal }
}
/// Creates an iterator of encrypted substrings by splitting the original encrypted string based
/// on a specified pattern (either encrypted or clear), excluding trailing empty substrings.
///
/// The iterator, of type `SplitTerminator`, can be used to sequentially retrieve the
/// substrings. Each call to `next` on the iterator returns a tuple with the next split
/// substring as an encrypted string and a boolean indicating `Some` (true) or `None` (false).
///
/// The pattern to search for can be specified as either `GenericPatternRef::Clear` for a clear
/// string or `GenericPatternRef::Enc` for an encrypted string.
///
/// # Examples
///
/// ```rust
/// use tfhe::integer::{ClientKey, ServerKey};
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128;
/// use tfhe::strings::ciphertext::{FheString, GenericPattern};
/// use tfhe::strings::server_key::FheStringIterator;
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128);
/// let sk = ServerKey::new_radix_server_key(&ck);
/// let ck = tfhe::strings::ClientKey::new(ck);
/// let sk = tfhe::strings::ServerKey::new(sk);
/// let (s, pat) = ("hello world ", " ");
///
/// let enc_s = FheString::new(&ck, s, None);
/// let enc_pat = GenericPattern::Enc(FheString::new(&ck, pat, None));
///
/// let mut split_terminator_iter = sk.split_terminator(&enc_s, enc_pat.as_ref());
/// let (first_item, first_is_some) = split_terminator_iter.next(&sk);
/// let (second_item, second_is_some) = split_terminator_iter.next(&sk);
/// let (_, no_more_items) = split_terminator_iter.next(&sk); // Attempting to get a third item
///
/// let first_decrypted = ck.decrypt_ascii(&first_item);
/// let first_is_some = ck.inner().decrypt_bool(&first_is_some);
/// let second_decrypted = ck.decrypt_ascii(&second_item);
/// let second_is_some = ck.inner().decrypt_bool(&second_is_some);
/// let no_more_items = ck.inner().decrypt_bool(&no_more_items);
///
/// assert_eq!(first_decrypted, "hello");
/// assert!(first_is_some); // There is a first item
/// assert_eq!(second_decrypted, "world");
/// assert!(second_is_some); // There is a second item
/// assert!(!no_more_items); // No more items in the iterator
/// ```
pub fn split_terminator(&self, str: &FheString, pat: GenericPatternRef<'_>) -> SplitTerminator {
let internal = self.split_no_trailing(str, pat, SplitType::Split);
SplitTerminator { internal }
}
/// Creates an iterator of encrypted substrings by splitting the original encrypted string from
/// the end based on a specified pattern (either encrypted or clear), excluding leading empty
/// substrings in the reverse order.
///
/// The iterator, of type `RSplitTerminator`, can be used to sequentially retrieve the
/// substrings in reverse order, ignoring any leading empty substring that would result from
/// splitting at the end of the string. Each call to `next` on the iterator returns a tuple with
/// the next split substring as an encrypted string and a boolean indicating `Some` (true) or
/// `None` (false).
///
/// The pattern to search for can be specified as either `GenericPatternRef::Clear` for a clear
/// string or `GenericPatternRef::Enc` for an encrypted string.
///
/// # Examples
///
/// ```rust
/// use tfhe::integer::{ClientKey, ServerKey};
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128;
/// use tfhe::strings::ciphertext::{FheString, GenericPattern};
/// use tfhe::strings::server_key::FheStringIterator;
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128);
/// let sk = ServerKey::new_radix_server_key(&ck);
/// let ck = tfhe::strings::ClientKey::new(ck);
/// let sk = tfhe::strings::ServerKey::new(sk);
/// let (s, pat) = ("hello world ", " ");
///
/// let enc_s = FheString::new(&ck, s, None);
/// let enc_pat = GenericPattern::Enc(FheString::new(&ck, pat, None));
///
/// let mut rsplit_terminator_iter = sk.rsplit_terminator(&enc_s, enc_pat.as_ref());
/// let (last_item, last_is_some) = rsplit_terminator_iter.next(&sk);
/// let (second_last_item, second_last_is_some) = rsplit_terminator_iter.next(&sk);
/// let (_, no_more_items) = rsplit_terminator_iter.next(&sk); // Attempting to get a third item
///
/// let last_decrypted = ck.decrypt_ascii(&last_item);
/// let last_is_some = ck.inner().decrypt_bool(&last_is_some);
/// let second_last_decrypted = ck.decrypt_ascii(&second_last_item);
/// let second_last_is_some = ck.inner().decrypt_bool(&second_last_is_some);
/// let no_more_items = ck.inner().decrypt_bool(&no_more_items);
///
/// assert_eq!(last_decrypted, "world");
/// assert!(last_is_some); // The last item is "world" instead of ""
/// assert_eq!(second_last_decrypted, "hello");
/// assert!(second_last_is_some); // The second last item is "hello"
/// assert!(!no_more_items); // No more items in the reverse iterator
/// ```
pub fn rsplit_terminator(
&self,
str: &FheString,
pat: GenericPatternRef<'_>,
) -> RSplitTerminator {
let internal = self.split_no_leading(str, pat);
RSplitTerminator { internal }
}
/// Creates an iterator of encrypted substrings by splitting the original encrypted string based
/// on a specified pattern (either encrypted or clear), where each substring includes the
/// delimiter. If the string ends with the delimiter, it does not create a trailing empty
/// substring.
///
/// The iterator, of type `SplitInclusive`, can be used to sequentially retrieve the substrings.
/// Each call to `next` on the iterator returns a tuple with the next split substring as an
/// encrypted string and a boolean indicating `Some` (true) or `None` (false).
///
/// The pattern to search for can be specified as either `GenericPatternRef::Clear` for a clear
/// string or `GenericPatternRef::Enc` for an encrypted string.
///
/// # Examples
///
/// ```rust
/// use tfhe::integer::{ClientKey, ServerKey};
/// use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128;
/// use tfhe::strings::ciphertext::{FheString, GenericPattern};
/// use tfhe::strings::server_key::FheStringIterator;
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128);
/// let sk = ServerKey::new_radix_server_key(&ck);
/// let ck = tfhe::strings::ClientKey::new(ck);
/// let sk = tfhe::strings::ServerKey::new(sk);
/// let (s, pat) = ("hello world ", " ");
///
/// let enc_s = FheString::new(&ck, s, None);
/// let enc_pat = GenericPattern::Enc(FheString::new(&ck, pat, None));
///
/// let mut split_inclusive_iter = sk.split_inclusive(&enc_s, enc_pat.as_ref());
/// let (first_item, first_is_some) = split_inclusive_iter.next(&sk);
/// let (second_item, second_is_some) = split_inclusive_iter.next(&sk);
/// let (_, no_more_items) = split_inclusive_iter.next(&sk); // Attempting to get a third item
///
/// let first_decrypted = ck.decrypt_ascii(&first_item);
/// let first_is_some = ck.inner().decrypt_bool(&first_is_some);
/// let second_decrypted = ck.decrypt_ascii(&second_item);
/// let second_is_some = ck.inner().decrypt_bool(&second_is_some);
/// let no_more_items = ck.inner().decrypt_bool(&no_more_items);
///
/// assert_eq!(first_decrypted, "hello ");
/// assert!(first_is_some); // The first item includes the delimiter
/// assert_eq!(second_decrypted, "world ");
/// assert!(second_is_some); // The second item includes the delimiter
/// assert!(!no_more_items); // No more items in the iterator, no trailing empty string
/// ```
pub fn split_inclusive(&self, str: &FheString, pat: GenericPatternRef<'_>) -> SplitInclusive {
let internal = self.split_no_trailing(str, pat, SplitType::SplitInclusive);
SplitInclusive { internal }
}
}
impl<T: Borrow<IntegerServerKey> + Sync> FheStringIterator<T> for Split {
fn next(&mut self, sk: &ServerKey<T>) -> (FheString, BooleanBlock) {
self.internal.next(sk)
}
}
impl<T: Borrow<IntegerServerKey> + Sync> FheStringIterator<T> for RSplit {
fn next(&mut self, sk: &ServerKey<T>) -> (FheString, BooleanBlock) {
self.internal.next(sk)
}
}
impl<T: Borrow<IntegerServerKey> + Sync> FheStringIterator<T> for SplitN {
fn next(&mut self, sk: &ServerKey<T>) -> (FheString, BooleanBlock) {
self.internal.next(sk)
}
}
impl<T: Borrow<IntegerServerKey> + Sync> FheStringIterator<T> for RSplitN {
fn next(&mut self, sk: &ServerKey<T>) -> (FheString, BooleanBlock) {
self.internal.next(sk)
}
}
impl<T: Borrow<IntegerServerKey> + Sync> FheStringIterator<T> for SplitTerminator {
fn next(&mut self, sk: &ServerKey<T>) -> (FheString, BooleanBlock) {
self.internal.next(sk)
}
}
impl<T: Borrow<IntegerServerKey> + Sync> FheStringIterator<T> for RSplitTerminator {
fn next(&mut self, sk: &ServerKey<T>) -> (FheString, BooleanBlock) {
self.internal.next(sk)
}
}
impl<T: Borrow<IntegerServerKey> + Sync> FheStringIterator<T> for SplitInclusive {
fn next(&mut self, sk: &ServerKey<T>) -> (FheString, BooleanBlock) {
self.internal.next(sk)
}
}