Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
mayeul-zama committed Nov 26, 2024
1 parent 79ad648 commit f265684
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 47 deletions.
3 changes: 3 additions & 0 deletions tfhe/src/strings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ mod test_functions;
// Used as the const argument for StaticUnsignedBigInt, specifying the max chars length of a
// ClearString
const N: usize = 32;

pub use client_key::ClientKey;
pub use server_key::ServerKey;
24 changes: 18 additions & 6 deletions tfhe/src/strings/server_key/comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);
/// 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.string_eq(&enc_s1, enc_s2.as_ref());
/// let are_equal = ck.decrypt_bool(&result);
/// let are_equal = ck.inner().decrypt_bool(&result);
///
/// assert!(are_equal);
/// ```
Expand Down Expand Up @@ -115,13 +117,15 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);
/// 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.string_ne(&enc_s1, enc_s2.as_ref());
/// let are_not_equal = ck.decrypt_bool(&result);
/// let are_not_equal = ck.inner().decrypt_bool(&result);
///
/// assert!(are_not_equal);
/// ```
Expand All @@ -146,13 +150,15 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);
/// 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.string_lt(&enc_s1, enc_s2.as_ref());
/// let is_lt = ck.decrypt_bool(&result);
/// let is_lt = ck.inner().decrypt_bool(&result);
///
/// assert!(is_lt); // "apple" is less than "banana"
/// ```
Expand Down Expand Up @@ -184,13 +190,15 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);
/// 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.string_gt(&enc_s1, enc_s2.as_ref());
/// let is_gt = ck.decrypt_bool(&result);
/// let is_gt = ck.inner().decrypt_bool(&result);
///
/// assert!(is_gt); // "banana" is greater than "apple"
/// ```
Expand Down Expand Up @@ -222,13 +230,15 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);
/// 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.string_le(&enc_s1, enc_s2.as_ref());
/// let is_le = ck.decrypt_bool(&result);
/// let is_le = ck.inner().decrypt_bool(&result);
///
/// assert!(is_le); // "apple" is less than or equal to "banana"
/// ```
Expand Down Expand Up @@ -259,13 +269,15 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);
/// 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.string_ge(&enc_s1, enc_s2.as_ref());
/// let is_ge = ck.decrypt_bool(&result);
/// let is_ge = ck.inner().decrypt_bool(&result);
///
/// assert!(is_ge); // "banana" is greater than or equal to "apple"
/// ```
Expand Down
20 changes: 17 additions & 3 deletions tfhe/src/strings/server_key/no_patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
/// use tfhe::strings::server_key::FheStringLen;
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);
/// let sk = ServerKey::new_radix_server_key(&ck);
/// let ck = tfhe::strings::ClientKey::new(ck);
/// let sk = tfhe::strings::ServerKey::new(sk);
/// let s = "hello";
/// let number_of_nulls = 3;
///
Expand All @@ -40,7 +42,7 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
/// FheStringLen::NoPadding(_) => panic!("Unexpected no padding"),
/// FheStringLen::Padding(ciphertext) => {
/// // Homomorphically computed length, requires decryption for actual length
/// let length = ck.decrypt_radix::<u32>(&ciphertext);
/// let length = ck.inner().decrypt_radix::<u32>(&ciphertext);
/// assert_eq!(length, 5)
/// }
/// }
Expand Down Expand Up @@ -85,6 +87,8 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);
/// let sk = ServerKey::new_radix_server_key(&ck);
/// let ck = tfhe::strings::ClientKey::new(ck);
/// let sk = tfhe::strings::ServerKey::new(sk);
/// let s = "";
/// let number_of_nulls = 2;
///
Expand All @@ -103,7 +107,7 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
/// FheStringIsEmpty::NoPadding(_) => panic!("Unexpected no padding"),
/// FheStringIsEmpty::Padding(ciphertext) => {
/// // Homomorphically computed emptiness, requires decryption for actual value
/// let is_empty = ck.decrypt_bool(&ciphertext);
/// let is_empty = ck.inner().decrypt_bool(&ciphertext);
/// assert!(is_empty)
/// }
/// }
Expand Down Expand Up @@ -136,6 +140,8 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);
/// let sk = ServerKey::new_radix_server_key(&ck);
/// let ck = tfhe::strings::ClientKey::new(ck);
/// let sk = tfhe::strings::ServerKey::new(sk);
/// let s = "Hello World";
///
/// let enc_s = FheString::new(&ck, s, None);
Expand Down Expand Up @@ -191,6 +197,8 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);
/// let sk = ServerKey::new_radix_server_key(&ck);
/// let ck = tfhe::strings::ClientKey::new(ck);
/// let sk = tfhe::strings::ServerKey::new(sk);
/// let s = "Hello World";
///
/// let enc_s = FheString::new(&ck, s, None);
Expand Down Expand Up @@ -252,13 +260,15 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);
/// 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.decrypt_bool(&result);
/// let are_equal = ck.inner().decrypt_bool(&result);
///
/// assert!(are_equal);
/// ```
Expand Down Expand Up @@ -289,6 +299,8 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);
/// let sk = ServerKey::new_radix_server_key(&ck);
/// let ck = tfhe::strings::ClientKey::new(ck);
/// let sk = tfhe::strings::ServerKey::new(sk);
/// let (lhs, rhs) = ("Hello, ", "world!");
///
/// let enc_lhs = FheString::new(&ck, lhs, None);
Expand Down Expand Up @@ -344,6 +356,8 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);
/// let sk = ServerKey::new_radix_server_key(&ck);
/// let ck = tfhe::strings::ClientKey::new(ck);
/// let sk = tfhe::strings::ServerKey::new(sk);
/// let s = "hi";
///
/// let enc_s = FheString::new(&ck, s, None);
Expand Down
18 changes: 12 additions & 6 deletions tfhe/src/strings/server_key/pattern/contains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);
/// let sk = ServerKey::new_radix_server_key(&ck);
/// let ck = tfhe::strings::ClientKey::new(ck);
/// let sk = tfhe::strings::ServerKey::new(sk);
/// let (bananas, nana, apples) = ("bananas", "nana", "apples");
///
/// let enc_bananas = FheString::new(&ck, bananas, None);
Expand All @@ -102,8 +104,8 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
/// let result1 = sk.contains(&enc_bananas, enc_nana.as_ref());
/// let result2 = sk.contains(&enc_bananas, clear_apples.as_ref());
///
/// let should_be_true = ck.decrypt_bool(&result1);
/// let should_be_false = ck.decrypt_bool(&result2);
/// let should_be_true = ck.inner().decrypt_bool(&result1);
/// let should_be_false = ck.inner().decrypt_bool(&result2);
///
/// assert!(should_be_true);
/// assert!(!should_be_false);
Expand Down Expand Up @@ -156,6 +158,8 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);
/// let sk = ServerKey::new_radix_server_key(&ck);
/// let ck = tfhe::strings::ClientKey::new(ck);
/// let sk = tfhe::strings::ServerKey::new(sk);
/// let (bananas, ba, nan) = ("bananas", "ba", "nan");
///
/// let enc_bananas = FheString::new(&ck, bananas, None);
Expand All @@ -165,8 +169,8 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
/// let result1 = sk.starts_with(&enc_bananas, enc_ba.as_ref());
/// let result2 = sk.starts_with(&enc_bananas, clear_nan.as_ref());
///
/// let should_be_true = ck.decrypt_bool(&result1);
/// let should_be_false = ck.decrypt_bool(&result2);
/// let should_be_true = ck.inner().decrypt_bool(&result1);
/// let should_be_false = ck.inner().decrypt_bool(&result2);
///
/// assert!(should_be_true);
/// assert!(!should_be_false);
Expand Down Expand Up @@ -236,6 +240,8 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);
/// let sk = ServerKey::new_radix_server_key(&ck);
/// let ck = tfhe::strings::ClientKey::new(ck);
/// let sk = tfhe::strings::ServerKey::new(sk);
/// let (bananas, anas, nana) = ("bananas", "anas", "nana");
///
/// let enc_bananas = FheString::new(&ck, bananas, None);
Expand All @@ -245,8 +251,8 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
/// let result1 = sk.ends_with(&enc_bananas, enc_anas.as_ref());
/// let result2 = sk.ends_with(&enc_bananas, clear_nana.as_ref());
///
/// let should_be_true = ck.decrypt_bool(&result1);
/// let should_be_false = ck.decrypt_bool(&result2);
/// let should_be_true = ck.inner().decrypt_bool(&result1);
/// let should_be_false = ck.inner().decrypt_bool(&result2);
///
/// assert!(should_be_true);
/// assert!(!should_be_false);
Expand Down
12 changes: 8 additions & 4 deletions tfhe/src/strings/server_key/pattern/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,17 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);
/// let sk = ServerKey::new_radix_server_key(&ck);
/// let ck = tfhe::strings::ClientKey::new(ck);
/// let sk = tfhe::strings::ServerKey::new(sk);
/// let (haystack, needle) = ("hello world", "world");
///
/// let enc_haystack = FheString::new(&ck, haystack, None);
/// let enc_needle = GenericPattern::Enc(FheString::new(&ck, needle, None));
///
/// let (index, found) = sk.find(&enc_haystack, enc_needle.as_ref());
///
/// let index = ck.decrypt_radix::<u32>(&index);
/// let found = ck.decrypt_bool(&found);
/// let index = ck.inner().decrypt_radix::<u32>(&index);
/// let found = ck.inner().decrypt_bool(&found);
///
/// assert!(found);
/// assert_eq!(index, 6); // "world" starts at index 6 in "hello world"
Expand Down Expand Up @@ -185,15 +187,17 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);
/// let sk = ServerKey::new_radix_server_key(&ck);
/// let ck = tfhe::strings::ClientKey::new(ck);
/// let sk = tfhe::strings::ServerKey::new(sk);
/// let (haystack, needle) = ("hello world world", "world");
///
/// let enc_haystack = FheString::new(&ck, haystack, None);
/// let enc_needle = GenericPattern::Enc(FheString::new(&ck, needle, None));
///
/// let (index, found) = sk.rfind(&enc_haystack, enc_needle.as_ref());
///
/// let index = ck.decrypt_radix::<u32>(&index);
/// let found = ck.decrypt_bool(&found);
/// let index = ck.inner().decrypt_radix::<u32>(&index);
/// let found = ck.inner().decrypt_bool(&found);
///
/// assert!(found);
/// assert_eq!(index, 12); // The last "world" starts at index 12 in "hello world world"
Expand Down
4 changes: 4 additions & 0 deletions tfhe/src/strings/server_key/pattern/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);
/// let sk = ServerKey::new_radix_server_key(&ck);
/// let ck = tfhe::strings::ClientKey::new(ck);
/// let sk = tfhe::strings::ServerKey::new(sk);
/// let (s, from, to) = ("hello", "l", "r");
///
/// let enc_s = FheString::new(&ck, s, None);
Expand Down Expand Up @@ -348,6 +350,8 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);
/// let sk = ServerKey::new_radix_server_key(&ck);
/// let ck = tfhe::strings::ClientKey::new(ck);
/// let sk = tfhe::strings::ServerKey::new(sk);
/// let (s, from, to) = ("hi", "i", "o");
///
/// let enc_s = FheString::new(&ck, s, None);
Expand Down
8 changes: 6 additions & 2 deletions tfhe/src/strings/server_key/pattern/split/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);
/// 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));
Expand All @@ -90,7 +92,7 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
///
/// let lhs_decrypted = ck.decrypt_ascii(&lhs);
/// let rhs_decrypted = ck.decrypt_ascii(&rhs);
/// let split_occurred = ck.decrypt_bool(&split_occurred);
/// let split_occurred = ck.inner().decrypt_bool(&split_occurred);
///
/// assert_eq!(lhs_decrypted, " hello");
/// assert_eq!(rhs_decrypted, "world");
Expand Down Expand Up @@ -156,6 +158,8 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
///
/// let ck = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);
/// 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));
Expand All @@ -164,7 +168,7 @@ impl<T: Borrow<IntegerServerKey> + Sync> ServerKey<T> {
///
/// let lhs_decrypted = ck.decrypt_ascii(&lhs);
/// let rhs_decrypted = ck.decrypt_ascii(&rhs);
/// let split_occurred = ck.decrypt_bool(&split_occurred);
/// let split_occurred = ck.inner().decrypt_bool(&split_occurred);
///
/// assert_eq!(lhs_decrypted, "");
/// assert_eq!(rhs_decrypted, "hello world");
Expand Down
Loading

0 comments on commit f265684

Please sign in to comment.