From e544dfc08e53776957d490a98977fbcccd7993e4 Mon Sep 17 00:00:00 2001 From: Nicolas Sarlin Date: Wed, 10 Dec 2025 10:27:51 +0100 Subject: [PATCH] fix(integer): handle large string size in DataKind::num_blocks --- tfhe/src/integer/ciphertext/utils.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/tfhe/src/integer/ciphertext/utils.rs b/tfhe/src/integer/ciphertext/utils.rs index b2dbeb503..7506cd2e2 100644 --- a/tfhe/src/integer/ciphertext/utils.rs +++ b/tfhe/src/integer/ciphertext/utils.rs @@ -25,11 +25,14 @@ impl DataKind { Self::Unsigned(n) | Self::Signed(n) => n.get(), Self::Boolean => 1, Self::String { n_chars, .. } => { - if message_modulus.0 == 0 { + if message_modulus.0 == 0 || message_modulus.0 == 1 { return 0; } + let blocks_per_char = 7u32.div_ceil(message_modulus.0.ilog2()); - (n_chars * blocks_per_char) as usize + // Use saturating mul to avoid panic here, maybe on the long run this function + // should return a Result + n_chars.saturating_mul(blocks_per_char) as usize } } } @@ -103,6 +106,7 @@ impl Expandable for BooleanBlock { mod test { use super::*; + /// Check that strings num_blocks does not panic #[test] fn test_string_num_blocks() { let kind = DataKind::String { @@ -113,5 +117,17 @@ mod test { let num_blocks = kind.num_blocks(MessageModulus(0)); assert_eq!(num_blocks, 0); + + let num_blocks = kind.num_blocks(MessageModulus(1)); + + assert_eq!(num_blocks, 0); + + let kind = DataKind::String { + n_chars: u32::MAX, + padded: true, + }; + + let num_blocks = kind.num_blocks(MessageModulus(2)); + assert_eq!(num_blocks, u32::MAX as usize); } }