Skip to content

Commit

Permalink
fix(integer): fix parallel carry propagation on empty input
Browse files Browse the repository at this point in the history
  • Loading branch information
tmontaigu committed Mar 13, 2024
1 parent 20d92af commit a55acf6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tfhe/src/integer/server_key/radix_parallel/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,10 @@ impl ServerKey {
&self,
generates_or_propagates: Vec<Ciphertext>,
) -> (Vec<Ciphertext>, Ciphertext) {
if generates_or_propagates.is_empty() {
return (vec![], self.key.create_trivial(0));
}

let lut_carry_propagation_sum = self
.key
.generate_lookup_table_bivariate(prefix_sum_carry_propagation);
Expand Down Expand Up @@ -581,6 +585,10 @@ impl ServerKey {
{
debug_assert!(self.key.message_modulus.0 * self.key.carry_modulus.0 >= (1 << 4));

if blocks.is_empty() {
return vec![];
}

let num_blocks = blocks.len();
let num_steps = blocks.len().ceil_ilog2() as usize;

Expand Down Expand Up @@ -1274,6 +1282,19 @@ impl ServerKey {
#[cfg(test)]
mod tests {
use super::should_hillis_steele_propagation_be_faster;
use crate::integer::gen_keys_radix;
use crate::shortint::prelude::PARAM_MESSAGE_2_CARRY_2_KS_PBS;

#[test]
fn test_propagate_single_carry_on_empty_input_ci_run_filter() {
// Parameters and num blocks do not matter here
let (_, sks) = gen_keys_radix(PARAM_MESSAGE_2_CARRY_2_KS_PBS, 4);

let carry = sks.propagate_single_carry_parallelized_low_latency([].as_mut_slice());

// The most interesting part we test is that the code does not panic
assert!(carry.is_trivial());
}

#[test]
fn test_hillis_steele_choice_128_threads() {
Expand Down
1 change: 1 addition & 0 deletions tfhe/src/integer/server_key/radix_parallel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ impl ServerKey {
}

let len = ctxt.blocks().len();
// If start_index >= len, the range is considered empty
for i in start_index..len {
let _ = self.propagate_parallelized(ctxt, i);
}
Expand Down

0 comments on commit a55acf6

Please sign in to comment.