Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(strings): make strings compatible with compact list #2002

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tfhe/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ impl Error {
}
}

#[cfg(feature = "integer")]
macro_rules! error{
($($arg:tt)*) => {
$crate::error::Error::new(::std::format!($($arg)*))
}
}

#[cfg(feature = "integer")]
pub(crate) use error;

impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self.kind() {
Expand Down
1 change: 1 addition & 0 deletions tfhe/src/high_level_api/compact_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ impl crate::FheTypes {
}
}
DataKind::Boolean => Self::Bool,
DataKind::String { .. } => return None,
})
}
}
Expand Down
45 changes: 28 additions & 17 deletions tfhe/src/high_level_api/compressed_ciphertext_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,25 +590,29 @@ pub mod gpu {
Tag::default(),
))
} else {
Err(crate::Error::new(format!(
Err(crate::error!(
"Tried to expand a FheUint{} while a FheUint{} is stored in this slot",
Id::num_bits(),
stored_num_bits
)))
))
}
}
DataKind::Signed(_) => {
let stored_num_bits = cuda_num_bits_of_blocks(&blocks) as usize;
Err(crate::Error::new(format!(
Err(crate::error!(
"Tried to expand a FheUint{} while a FheInt{} is stored in this slot",
Id::num_bits(),
stored_num_bits
)))
))
}
DataKind::Boolean => Err(crate::Error::new(format!(
DataKind::Boolean => Err(crate::error!(
"Tried to expand a FheUint{} while a FheBool is stored in this slot",
Id::num_bits(),
))),
)),
DataKind::String { .. } => Err(crate::error!(
"Tried to expand a FheUint{} while a FheString is stored in this slot",
Id::num_bits()
)),
}
}
}
Expand All @@ -621,11 +625,11 @@ pub mod gpu {
match kind {
DataKind::Unsigned(_) => {
let stored_num_bits = cuda_num_bits_of_blocks(&blocks) as usize;
Err(crate::Error::new(format!(
Err(crate::error!(
"Tried to expand a FheInt{} while a FheUint{} is stored in this slot",
Id::num_bits(),
stored_num_bits
)))
))
}
DataKind::Signed(_) => {
let stored_num_bits = cuda_num_bits_of_blocks(&blocks) as usize;
Expand All @@ -638,17 +642,21 @@ pub mod gpu {
Tag::default(),
))
} else {
Err(crate::Error::new(format!(
Err(crate::error!(
"Tried to expand a FheInt{} while a FheInt{} is stored in this slot",
Id::num_bits(),
stored_num_bits
)))
))
}
}
DataKind::Boolean => Err(crate::Error::new(format!(
"Tried to expand a FheUint{} while a FheBool is stored in this slot",
DataKind::Boolean => Err(crate::error!(
"Tried to expand a FheInt{} while a FheBool is stored in this slot",
Id::num_bits(),
))),
)),
DataKind::String { .. } => Err(crate::error!(
"Tried to expand a FheInt{} while a FheString is stored in this slot",
Id::num_bits()
)),
}
}
}
Expand All @@ -661,15 +669,15 @@ pub mod gpu {
match kind {
DataKind::Unsigned(_) => {
let stored_num_bits = cuda_num_bits_of_blocks(&blocks) as usize;
Err(crate::Error::new(format!(
Err(crate::error!(
"Tried to expand a FheBool while a FheUint{stored_num_bits} is stored in this slot",
)))
))
}
DataKind::Signed(_) => {
let stored_num_bits = cuda_num_bits_of_blocks(&blocks) as usize;
Err(crate::Error::new(format!(
Err(crate::error!(
"Tried to expand a FheBool while a FheInt{stored_num_bits} is stored in this slot",
)))
))
}
DataKind::Boolean => {
let mut boolean_block = CudaBooleanBlock::from_cuda_radix_ciphertext(blocks);
Expand All @@ -680,6 +688,9 @@ pub mod gpu {
// The expander will be responsible for setting the correct tag
Ok(Self::new(boolean_block, Tag::default()))
}
DataKind::String { .. } => Err(crate::error!(
"Tried to expand a FheBool while a FheString is stored in this slot"
)),
}
}
}
Expand Down
45 changes: 28 additions & 17 deletions tfhe/src/high_level_api/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,29 @@ impl<Id: FheUintId> Expandable for FheUint<Id> {
Tag::default(),
))
} else {
Err(crate::Error::new(format!(
Err(crate::error!(
"Tried to expand a FheUint{} while a FheUint{} is stored in this slot",
Id::num_bits(),
stored_num_bits
)))
))
}
}
DataKind::Signed(_) => {
let stored_num_bits = num_bits_of_blocks(&blocks) as usize;
Err(crate::Error::new(format!(
Err(crate::error!(
"Tried to expand a FheUint{} while a FheInt{} is stored in this slot",
Id::num_bits(),
stored_num_bits
)))
))
}
DataKind::Boolean => Err(crate::Error::new(format!(
DataKind::Boolean => Err(crate::error!(
"Tried to expand a FheUint{} while a FheBool is stored in this slot",
Id::num_bits(),
))),
)),
DataKind::String { .. } => Err(crate::error!(
"Tried to expand a FheUint{} while a string is stored in this slot",
Id::num_bits()
)),
}
}
}
Expand All @@ -52,11 +56,11 @@ impl<Id: FheIntId> Expandable for FheInt<Id> {
match kind {
DataKind::Unsigned(_) => {
let stored_num_bits = num_bits_of_blocks(&blocks) as usize;
Err(crate::Error::new(format!(
Err(crate::error!(
"Tried to expand a FheInt{} while a FheUint{} is stored in this slot",
Id::num_bits(),
stored_num_bits
)))
))
}
DataKind::Signed(_) => {
let stored_num_bits = num_bits_of_blocks(&blocks) as usize;
Expand All @@ -67,17 +71,21 @@ impl<Id: FheIntId> Expandable for FheInt<Id> {
Tag::default(),
))
} else {
Err(crate::Error::new(format!(
Err(crate::error!(
"Tried to expand a FheInt{} while a FheInt{} is stored in this slot",
Id::num_bits(),
stored_num_bits
)))
))
}
}
DataKind::Boolean => Err(crate::Error::new(format!(
"Tried to expand a FheUint{} while a FheBool is stored in this slot",
DataKind::Boolean => Err(crate::error!(
"Tried to expand a FheInt{} while a FheBool is stored in this slot",
Id::num_bits(),
))),
)),
DataKind::String { .. } => Err(crate::error!(
"Tried to expand a FheInt{} while a string is stored in this slot",
Id::num_bits()
)),
}
}
}
Expand All @@ -87,15 +95,15 @@ impl Expandable for FheBool {
match kind {
DataKind::Unsigned(_) => {
let stored_num_bits = num_bits_of_blocks(&blocks) as usize;
Err(crate::Error::new(format!(
Err(crate::error!(
"Tried to expand a FheBool while a FheUint{stored_num_bits} is stored in this slot",
)))
))
}
DataKind::Signed(_) => {
let stored_num_bits = num_bits_of_blocks(&blocks) as usize;
Err(crate::Error::new(format!(
Err(crate::error!(
"Tried to expand a FheBool while a FheInt{stored_num_bits} is stored in this slot",
)))
))
}
DataKind::Boolean => {
let mut boolean_block = BooleanBlock::new_unchecked(blocks[0].clone());
Expand All @@ -105,6 +113,9 @@ impl Expandable for FheBool {
// The expander will be responsible for setting the correct tag
Ok(Self::new(boolean_block, Tag::default()))
}
DataKind::String { .. } => Err(crate::Error::new(
"Tried to expand a FheBool while a string is stored in this slot".to_string(),
)),
}
}
}
Loading
Loading