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(integer): implement sub_assign_with borrow #1474

Merged
merged 1 commit into from
Aug 21, 2024
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
25 changes: 13 additions & 12 deletions tfhe/src/integer/server_key/radix/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,18 +404,19 @@ impl ServerKey {
lhs.blocks.len(),
rhs.blocks.len()
);
// Here we have to use manual unchecked_sub on shortint blocks
// rather than calling integer's unchecked_sub as we need each subtraction
// to be independent from other blocks. And we don't want to do subtraction by
// adding negation
let result = lhs
.blocks
.iter()
.zip(rhs.blocks.iter())
.map(|(lhs_block, rhs_block)| self.key.unchecked_sub(lhs_block, rhs_block))
.collect::<Vec<_>>();
let mut result = RadixCiphertext::from(result);
let overflowed = self.unsigned_overflowing_propagate_subtraction_borrow(&mut result);

const INPUT_BORROW: Option<&BooleanBlock> = None;
const COMPUTE_OVERFLOW: bool = true;

let mut result = lhs.clone();
let overflowed = self
.advanced_sub_assign_with_borrow_sequential(
&mut result,
rhs,
INPUT_BORROW,
COMPUTE_OVERFLOW,
)
.expect("overflow computation was requested");
(result, overflowed)
}

Expand Down
12 changes: 11 additions & 1 deletion tfhe/src/integer/server_key/radix_parallel/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,12 @@ impl ServerKey {
grouping_size: usize,
block_states: &[Ciphertext],
) -> (Vec<Ciphertext>, Vec<Ciphertext>) {
if block_states.is_empty() {
return (
vec![self.key.create_trivial(0)],
vec![self.key.create_trivial(0)],
);
}
let message_modulus = self.key.message_modulus.0 as u64;
let block_modulus = message_modulus * self.carry_modulus().0 as u64;
let num_bits_in_block = block_modulus.ilog2();
Expand Down Expand Up @@ -1325,7 +1331,7 @@ impl ServerKey {
} else if block == message_modulus - 1 {
1 // Propagates a carry
} else {
0 // Does not borrow
0 // Does not generate carry
};

r << (i - 1)
Expand Down Expand Up @@ -1354,6 +1360,10 @@ impl ServerKey {
})
.collect::<Vec<_>>();

// For the last block we do something a bit different because the
// state we compute will be used (if needed) to compute the output carry
// of the whole addition. And this computation will be done during the 'cleaning'
// phase
let last_block_luts = {
if blocks.len() == 1 {
let first_block_state_fn = |block| {
Expand Down
Loading
Loading