From 1479315725850eb2564b17b34c914f48c552d0a8 Mon Sep 17 00:00:00 2001 From: Nicolas Sarlin Date: Thu, 4 Dec 2025 11:33:10 +0100 Subject: [PATCH] fix(integer): handles num_blocks_per_integer is 0 in ct list upgrade --- .../backward_compatibility/ciphertext/mod.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/tfhe/src/integer/backward_compatibility/ciphertext/mod.rs b/tfhe/src/integer/backward_compatibility/ciphertext/mod.rs index 4f74fdd94..422865f7f 100644 --- a/tfhe/src/integer/backward_compatibility/ciphertext/mod.rs +++ b/tfhe/src/integer/backward_compatibility/ciphertext/mod.rs @@ -10,7 +10,6 @@ use crate::integer::BooleanBlock; #[cfg(feature = "zk-pok")] use crate::integer::ProvenCompactCiphertextList; use crate::shortint::ciphertext::CompressedModulusSwitchedCiphertext; -use std::convert::Infallible; use std::num::NonZero; use tfhe_versionable::{Upgrade, Version, VersionsDispatch}; @@ -36,15 +35,24 @@ pub struct CompactCiphertextListV0 { } impl Upgrade for CompactCiphertextListV0 { - type Error = Infallible; + type Error = crate::Error; fn upgrade(self) -> Result { - let radix_count = - self.ct_list.ct_list.lwe_ciphertext_count().0 / self.num_blocks_per_integer; + let lwe_count = self.ct_list.ct_list.lwe_ciphertext_count().0; + + if !lwe_count.is_multiple_of(self.num_blocks_per_integer) { + return Err(crate::error!("Invalid CompactCiphertextListV0, the total lwe count should be a multiple of num_blocks_per_integer")); + } + + let radix_count = lwe_count + .checked_div(self.num_blocks_per_integer) + .ok_or_else(|| { + crate::error!("Invalid CompactCiphertextListV0, num_blocks_per_integer is 0") + })?; + // Since we can't guess the type of data here, we set them by default as unsigned integer. // Since it this data comes from 0.6, if it is included in a homogeneous compact list it // will be converted to the right type at expand time. - let info = NonZero::new(self.num_blocks_per_integer) .map(|n| vec![DataKind::Unsigned(n); radix_count]) .unwrap_or_default();