-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
1,902 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,260 @@ | ||
use crate::high_level_api::global_state::with_internal_keys; | ||
use crate::high_level_api::keys::InternalServerKey; | ||
use crate::high_level_api::strings::ascii::FheAsciiString; | ||
use crate::prelude::{FheEq, FheEqIgnoreCase, FheOrd}; | ||
use crate::strings::ciphertext::ClearString; | ||
use crate::FheBool; | ||
|
||
impl FheEq<&Self> for FheAsciiString { | ||
fn eq(&self, other: &Self) -> FheBool { | ||
with_internal_keys(|keys| match keys { | ||
InternalServerKey::Cpu(cpu_key) => { | ||
let inner = cpu_key | ||
.string_key() | ||
.eq(&self.inner.on_cpu(), (&*other.inner.on_cpu()).into()); | ||
FheBool::new(inner, cpu_key.tag.clone()) | ||
} | ||
#[cfg(feature = "gpu")] | ||
InternalServerKey::Cuda(_) => { | ||
panic!("gpu does not support strings eq"); | ||
} | ||
}) | ||
} | ||
|
||
fn ne(&self, other: &Self) -> FheBool { | ||
with_internal_keys(|keys| match keys { | ||
InternalServerKey::Cpu(cpu_key) => { | ||
let inner = cpu_key | ||
.string_key() | ||
.ne(&self.inner.on_cpu(), (&*other.inner.on_cpu()).into()); | ||
FheBool::new(inner, cpu_key.tag.clone()) | ||
} | ||
#[cfg(feature = "gpu")] | ||
InternalServerKey::Cuda(_) => { | ||
panic!("gpu does not support strings ne"); | ||
} | ||
}) | ||
} | ||
} | ||
|
||
impl FheEq<&ClearString> for FheAsciiString { | ||
fn eq(&self, other: &ClearString) -> FheBool { | ||
with_internal_keys(|keys| match keys { | ||
InternalServerKey::Cpu(cpu_key) => { | ||
let inner = cpu_key.string_key().eq(&self.inner.on_cpu(), other.into()); | ||
FheBool::new(inner, cpu_key.tag.clone()) | ||
} | ||
#[cfg(feature = "gpu")] | ||
InternalServerKey::Cuda(_) => { | ||
panic!("gpu does not support strings eq"); | ||
} | ||
}) | ||
} | ||
|
||
fn ne(&self, other: &ClearString) -> FheBool { | ||
with_internal_keys(|keys| match keys { | ||
InternalServerKey::Cpu(cpu_key) => { | ||
let inner = cpu_key.string_key().ne(&self.inner.on_cpu(), other.into()); | ||
FheBool::new(inner, cpu_key.tag.clone()) | ||
} | ||
#[cfg(feature = "gpu")] | ||
InternalServerKey::Cuda(_) => { | ||
panic!("gpu does not support strings ne"); | ||
} | ||
}) | ||
} | ||
} | ||
|
||
impl FheOrd<&Self> for FheAsciiString { | ||
fn lt(&self, other: &Self) -> FheBool { | ||
with_internal_keys(|keys| match keys { | ||
InternalServerKey::Cpu(cpu_key) => { | ||
let inner = cpu_key | ||
.string_key() | ||
.lt(&self.inner.on_cpu(), (&*other.inner.on_cpu()).into()); | ||
FheBool::new(inner, cpu_key.tag.clone()) | ||
} | ||
#[cfg(feature = "gpu")] | ||
InternalServerKey::Cuda(_) => { | ||
panic!("gpu does not support strings lt"); | ||
} | ||
}) | ||
} | ||
|
||
fn le(&self, other: &Self) -> FheBool { | ||
with_internal_keys(|keys| match keys { | ||
InternalServerKey::Cpu(cpu_key) => { | ||
let inner = cpu_key | ||
.string_key() | ||
.le(&self.inner.on_cpu(), (&*other.inner.on_cpu()).into()); | ||
FheBool::new(inner, cpu_key.tag.clone()) | ||
} | ||
#[cfg(feature = "gpu")] | ||
InternalServerKey::Cuda(_) => { | ||
panic!("gpu does not support strings le"); | ||
} | ||
}) | ||
} | ||
|
||
fn gt(&self, other: &Self) -> FheBool { | ||
with_internal_keys(|keys| match keys { | ||
InternalServerKey::Cpu(cpu_key) => { | ||
let inner = cpu_key | ||
.string_key() | ||
.gt(&self.inner.on_cpu(), (&*other.inner.on_cpu()).into()); | ||
FheBool::new(inner, cpu_key.tag.clone()) | ||
} | ||
#[cfg(feature = "gpu")] | ||
InternalServerKey::Cuda(_) => { | ||
panic!("gpu does not support strings gt"); | ||
} | ||
}) | ||
} | ||
|
||
fn ge(&self, other: &Self) -> FheBool { | ||
with_internal_keys(|keys| match keys { | ||
InternalServerKey::Cpu(cpu_key) => { | ||
let inner = cpu_key | ||
.string_key() | ||
.ge(&self.inner.on_cpu(), (&*other.inner.on_cpu()).into()); | ||
FheBool::new(inner, cpu_key.tag.clone()) | ||
} | ||
#[cfg(feature = "gpu")] | ||
InternalServerKey::Cuda(_) => { | ||
panic!("gpu does not support strings ge"); | ||
} | ||
}) | ||
} | ||
} | ||
|
||
impl FheOrd<&ClearString> for FheAsciiString { | ||
fn lt(&self, other: &ClearString) -> FheBool { | ||
with_internal_keys(|keys| match keys { | ||
InternalServerKey::Cpu(cpu_key) => { | ||
let inner = cpu_key.string_key().lt(&self.inner.on_cpu(), other.into()); | ||
FheBool::new(inner, cpu_key.tag.clone()) | ||
} | ||
#[cfg(feature = "gpu")] | ||
InternalServerKey::Cuda(_) => { | ||
panic!("gpu does not support strings lt"); | ||
} | ||
}) | ||
} | ||
|
||
fn le(&self, other: &ClearString) -> FheBool { | ||
with_internal_keys(|keys| match keys { | ||
InternalServerKey::Cpu(cpu_key) => { | ||
let inner = cpu_key.string_key().le(&self.inner.on_cpu(), other.into()); | ||
FheBool::new(inner, cpu_key.tag.clone()) | ||
} | ||
#[cfg(feature = "gpu")] | ||
InternalServerKey::Cuda(_) => { | ||
panic!("gpu does not support strings le"); | ||
} | ||
}) | ||
} | ||
|
||
fn gt(&self, other: &ClearString) -> FheBool { | ||
with_internal_keys(|keys| match keys { | ||
InternalServerKey::Cpu(cpu_key) => { | ||
let inner = cpu_key.string_key().gt(&self.inner.on_cpu(), other.into()); | ||
FheBool::new(inner, cpu_key.tag.clone()) | ||
} | ||
#[cfg(feature = "gpu")] | ||
InternalServerKey::Cuda(_) => { | ||
panic!("gpu does not support strings gt"); | ||
} | ||
}) | ||
} | ||
|
||
fn ge(&self, other: &ClearString) -> FheBool { | ||
with_internal_keys(|keys| match keys { | ||
InternalServerKey::Cpu(cpu_key) => { | ||
let inner = cpu_key.string_key().ge(&self.inner.on_cpu(), other.into()); | ||
FheBool::new(inner, cpu_key.tag.clone()) | ||
} | ||
#[cfg(feature = "gpu")] | ||
InternalServerKey::Cuda(_) => { | ||
panic!("gpu does not support strings ge"); | ||
} | ||
}) | ||
} | ||
} | ||
|
||
impl FheEqIgnoreCase for FheAsciiString { | ||
/// checks if the strings are equal, ignoring the case | ||
/// | ||
/// Returns a [FheBool] that encrypts `true` if the substring was found. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// use tfhe::prelude::*; | ||
/// use tfhe::{ | ||
/// generate_keys, set_server_key, ConfigBuilder, FheAsciiString, FheStringIsEmpty, | ||
/// FheStringLen, | ||
/// }; | ||
/// | ||
/// let (client_key, server_key) = generate_keys(ConfigBuilder::default()); | ||
/// set_server_key(server_key); | ||
/// | ||
/// let string1 = FheAsciiString::try_encrypt("tfhe-RS", &client_key).unwrap(); | ||
/// let string2 = FheAsciiString::try_encrypt("TFHE-rs", &client_key).unwrap(); | ||
/// let is_eq = string1.eq_ignore_case(&string2); | ||
/// | ||
/// assert!(is_eq.decrypt(&client_key)); | ||
/// ``` | ||
fn eq_ignore_case(&self, rhs: &Self) -> FheBool { | ||
with_internal_keys(|keys| match keys { | ||
InternalServerKey::Cpu(cpu_key) => { | ||
let inner = cpu_key | ||
.string_key() | ||
.eq_ignore_case(&self.inner.on_cpu(), (&*rhs.inner.on_cpu()).into()); | ||
FheBool::new(inner, cpu_key.tag.clone()) | ||
} | ||
#[cfg(feature = "gpu")] | ||
InternalServerKey::Cuda(_) => { | ||
panic!("gpu does not support strings eq_ignore_case"); | ||
} | ||
}) | ||
} | ||
} | ||
|
||
impl FheEqIgnoreCase<ClearString> for FheAsciiString { | ||
/// checks if the strings are equal, ignoring the case | ||
/// | ||
/// Returns a [FheBool] that encrypts `true` if the substring was found. | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// use tfhe::prelude::*; | ||
/// use tfhe::{ | ||
/// generate_keys, set_server_key, ClearString, ConfigBuilder, FheAsciiString, | ||
/// FheStringIsEmpty, FheStringLen, | ||
/// }; | ||
/// | ||
/// let (client_key, server_key) = generate_keys(ConfigBuilder::default()); | ||
/// set_server_key(server_key); | ||
/// | ||
/// let string1 = FheAsciiString::try_encrypt("tfhe-RS", &client_key).unwrap(); | ||
/// let string2 = ClearString::new("TFHE-rs".into()); | ||
/// let is_eq = string1.eq_ignore_case(&string2); | ||
/// | ||
/// assert!(is_eq.decrypt(&client_key)); | ||
/// ``` | ||
fn eq_ignore_case(&self, rhs: &ClearString) -> FheBool { | ||
with_internal_keys(|keys| match keys { | ||
InternalServerKey::Cpu(cpu_key) => { | ||
let inner = cpu_key | ||
.string_key() | ||
.eq_ignore_case(&self.inner.on_cpu(), rhs.into()); | ||
FheBool::new(inner, cpu_key.tag.clone()) | ||
} | ||
#[cfg(feature = "gpu")] | ||
InternalServerKey::Cuda(_) => { | ||
panic!("gpu does not support strings eq_ignore_case"); | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.