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
use crate::integer::{BooleanBlock, ServerKey as IntegerServerKey};
use crate::strings::ciphertext::{FheString, GenericPattern, GenericPatternRef};
use crate::strings::server_key::{FheStringIsEmpty, ServerKey};
use crate::ClearString;
use std::borrow::Borrow;
impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
fn eq_length_checks(&self, lhs: &FheString, rhs: &FheString) -> Option<BooleanBlock> {
let sk = self.inner();
// If lhs is empty, rhs must also be empty in order to be equal (the case where lhs is
// empty with > 1 padding zeros is handled next)
if lhs.is_empty() {
return match self.is_empty(rhs) {
FheStringIsEmpty::Padding(enc_val) => Some(enc_val),
FheStringIsEmpty::NoPadding(val) => Some(sk.create_trivial_boolean_block(val)),
};
}
// If rhs is empty, lhs must also be empty in order to be equal (only case remaining is if
// lhs padding zeros > 1)
if rhs.is_empty() {
return match self.is_empty(lhs) {
FheStringIsEmpty::Padding(enc_val) => Some(enc_val),
FheStringIsEmpty::NoPadding(_) => Some(sk.create_trivial_boolean_block(false)),
};
}
// Two strings without padding that have different lengths cannot be equal
if (!lhs.is_padded() && !rhs.is_padded()) && (lhs.len() != rhs.len()) {
return Some(sk.create_trivial_boolean_block(false));
}
// A string without padding cannot be equal to a string with padding that has the same or
// lower length
if (!lhs.is_padded() && rhs.is_padded()) && (rhs.len() <= lhs.len())
|| (!rhs.is_padded() && lhs.is_padded()) && (lhs.len() <= rhs.len())
{
return Some(sk.create_trivial_boolean_block(false));
}
None
}
/// Returns `true` if an encrypted string and a pattern (either encrypted or clear) are equal.
///
/// Returns `false` if they are not equal.
///
/// The pattern for comparison (`rhs`) 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};
///
/// 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 (s1, s2) = ("hello", "hello");
///
/// let enc_s1 = FheString::new(&ck, s1, None);
/// let enc_s2 = GenericPattern::Enc(FheString::new(&ck, s2, None));
///
/// let result = sk.eq(&enc_s1, enc_s2.as_ref());
/// let are_equal = ck.inner().decrypt_bool(&result);
///
/// assert!(are_equal);
/// ```
pub fn eq(&self, lhs: &FheString, rhs: GenericPatternRef<'_>) -> BooleanBlock {
let sk = self.inner();
let early_return = match rhs {
GenericPatternRef::Clear(rhs) => {
self.eq_length_checks(lhs, &FheString::trivial(self, rhs.str()))
}
GenericPatternRef::Enc(rhs) => self.eq_length_checks(lhs, rhs),
};
if let Some(val) = early_return {
return val;
}
let mut lhs_uint = lhs.to_uint();
match rhs {
GenericPatternRef::Clear(rhs) => {
let rhs_clear_uint = self.pad_cipher_and_cleartext_lsb(&mut lhs_uint, rhs.str());
sk.scalar_eq_parallelized(&lhs_uint, rhs_clear_uint)
}
GenericPatternRef::Enc(rhs) => {
let mut rhs_uint = rhs.to_uint();
self.pad_ciphertexts_lsb(&mut lhs_uint, &mut rhs_uint);
sk.eq_parallelized(&lhs_uint, &rhs_uint)
}
}
}
/// Returns `true` if an encrypted string and a pattern (either encrypted or clear) are not
/// equal.
///
/// Returns `false` if they are equal.
///
/// The pattern for comparison (`rhs`) 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};
///
/// 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 (s1, s2) = ("hello", "world");
///
/// let enc_s1 = FheString::new(&ck, s1, None);
/// let enc_s2 = GenericPattern::Enc(FheString::new(&ck, s2, None));
///
/// let result = sk.ne(&enc_s1, enc_s2.as_ref());
/// let are_not_equal = ck.inner().decrypt_bool(&result);
///
/// assert!(are_not_equal);
/// ```
pub fn ne(&self, lhs: &FheString, rhs: GenericPatternRef<'_>) -> BooleanBlock {
let sk = self.inner();
let eq = self.eq(lhs, rhs);
sk.boolean_bitnot(&eq)
}
/// Returns `true` if the first encrypted string is less than the second encrypted string.
///
/// Returns `false` otherwise.
///
/// # 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};
///
/// 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 (s1, s2) = ("apple", "banana");
///
/// let enc_s1 = FheString::new(&ck, s1, None);
/// let enc_s2 = GenericPattern::Enc(FheString::new(&ck, s2, None));
///
/// let result = sk.lt(&enc_s1, enc_s2.as_ref());
/// let is_lt = ck.inner().decrypt_bool(&result);
///
/// assert!(is_lt); // "apple" is less than "banana"
/// ```
pub fn lt(&self, lhs: &FheString, rhs: GenericPatternRef<'_>) -> BooleanBlock {
let sk = self.inner();
let mut lhs_uint = lhs.to_uint();
let mut rhs_uint = match rhs {
GenericPatternRef::Clear(rhs) => FheString::trivial(self, rhs.str()).to_uint(),
GenericPatternRef::Enc(rhs) => rhs.to_uint(),
};
self.pad_ciphertexts_lsb(&mut lhs_uint, &mut rhs_uint);
sk.lt_parallelized(&lhs_uint, &rhs_uint)
}
/// Returns `true` if the first encrypted string is greater than the second encrypted string.
///
/// Returns `false` otherwise.
///
/// # 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};
///
/// 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 (s1, s2) = ("banana", "apple");
///
/// let enc_s1 = FheString::new(&ck, s1, None);
/// let enc_s2 = GenericPattern::Enc(FheString::new(&ck, s2, None));
///
/// let result = sk.gt(&enc_s1, enc_s2.as_ref());
/// let is_gt = ck.inner().decrypt_bool(&result);
///
/// assert!(is_gt); // "banana" is greater than "apple"
/// ```
pub fn gt(&self, lhs: &FheString, rhs: GenericPatternRef<'_>) -> BooleanBlock {
let sk = self.inner();
let mut lhs_uint = lhs.to_uint();
let mut rhs_uint = match rhs {
GenericPatternRef::Clear(rhs) => FheString::trivial(self, rhs.str()).to_uint(),
GenericPatternRef::Enc(rhs) => rhs.to_uint(),
};
self.pad_ciphertexts_lsb(&mut lhs_uint, &mut rhs_uint);
sk.gt_parallelized(&lhs_uint, &rhs_uint)
}
/// Returns `true` if the first encrypted string is less than or equal to the second encrypted
/// string.
///
/// Returns `false` otherwise.
///
/// # 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};
///
/// 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 (s1, s2) = ("apple", "banana");
///
/// let enc_s1 = FheString::new(&ck, s1, None);
/// let enc_s2 = GenericPattern::Enc(FheString::new(&ck, s2, None));
///
/// let result = sk.le(&enc_s1, enc_s2.as_ref());
/// let is_le = ck.inner().decrypt_bool(&result);
///
/// assert!(is_le); // "apple" is less than or equal to "banana"
/// ```
pub fn le(&self, lhs: &FheString, rhs: GenericPatternRef<'_>) -> BooleanBlock {
let sk = self.inner();
let mut lhs_uint = lhs.to_uint();
let mut rhs_uint = match rhs {
GenericPatternRef::Clear(rhs) => FheString::trivial(self, rhs.str()).to_uint(),
GenericPatternRef::Enc(rhs) => rhs.to_uint(),
};
self.pad_ciphertexts_lsb(&mut lhs_uint, &mut rhs_uint);
sk.le_parallelized(&lhs_uint, &rhs_uint)
}
/// Returns `true` if the first encrypted string is greater than or equal to the second
/// encrypted string.
///
/// Returns `false` otherwise.
///
/// # 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};
///
/// 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 (s1, s2) = ("banana", "apple");
///
/// let enc_s1 = FheString::new(&ck, s1, None);
/// let enc_s2 = GenericPattern::Enc(FheString::new(&ck, s2, None));
///
/// let result = sk.ge(&enc_s1, enc_s2.as_ref());
/// let is_ge = ck.inner().decrypt_bool(&result);
///
/// assert!(is_ge); // "banana" is greater than or equal to "apple"
/// ```
pub fn ge(&self, lhs: &FheString, rhs: GenericPatternRef<'_>) -> BooleanBlock {
let sk = self.inner();
let mut lhs_uint = lhs.to_uint();
let mut rhs_uint = match rhs {
GenericPatternRef::Clear(rhs) => FheString::trivial(self, rhs.str()).to_uint(),
GenericPatternRef::Enc(rhs) => rhs.to_uint(),
};
self.pad_ciphertexts_lsb(&mut lhs_uint, &mut rhs_uint);
sk.ge_parallelized(&lhs_uint, &rhs_uint)
}
/// Returns `true` if an encrypted string and a pattern (either encrypted or clear) are equal,
/// ignoring case differences.
///
/// Returns `false` if they are not equal.
///
/// The pattern for comparison (`rhs`) 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};
///
/// 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 (s1, s2) = ("Hello", "hello");
///
/// let enc_s1 = FheString::new(&ck, s1, None);
/// let enc_s2 = GenericPattern::Enc(FheString::new(&ck, s2, None));
///
/// let result = sk.eq_ignore_case(&enc_s1, enc_s2.as_ref());
/// let are_equal = ck.inner().decrypt_bool(&result);
///
/// assert!(are_equal);
/// ```
pub fn eq_ignore_case(&self, lhs: &FheString, rhs: GenericPatternRef<'_>) -> BooleanBlock {
let (lhs, rhs) = rayon::join(
|| self.to_lowercase(lhs),
|| match rhs {
GenericPatternRef::Clear(rhs) => {
GenericPattern::Clear(ClearString::new(rhs.str().to_lowercase()))
}
GenericPatternRef::Enc(rhs) => GenericPattern::Enc(self.to_lowercase(rhs)),
},
);
self.eq(&lhs, rhs.as_ref())
}
}