Skip to content

Commit

Permalink
fix(integer): StaticSignedBigInt right shift
Browse files Browse the repository at this point in the history
  • Loading branch information
tmontaigu committed Sep 27, 2023
1 parent 37be751 commit 7025d1e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion tfhe/src/integer/bigint/algorithms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,11 @@ pub(crate) fn shr_assign(lhs: &mut [u64], shift: u32, shift_type: ShiftType) {
let value_mask = u64::MAX >> shift_in_words;
let carry_mask = ((1u64 << shift_in_words) - 1u64).rotate_right(shift_in_words);

let mut carry = if sign_bit == 1 { u64::MAX } else { 0 };
let mut carry = if sign_bit == 1 {
u64::MAX & carry_mask
} else {
0
};
for word in &mut head.iter_mut().rev() {
let rotated = word.rotate_right(shift_in_words);
let value = (rotated & value_mask) | carry;
Expand Down
15 changes: 15 additions & 0 deletions tfhe/src/integer/bigint/i256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ mod tests {
assert_eq!(a << 1u32, I256::from(input << 1));
}

#[test]
fn test_shr() {
let input = i128::MIN;
let a = I256::from(input);
assert_eq!(a >> 1u32, I256::from(input >> 1));

let a = I256::MIN;
// We expect (MSB) 110............0 (LSB)
assert_eq!(
a >> 1u32,
// 3 is '11'
I256::from([0, 0, 0, 3 << 62])
);
}

#[test]
fn test_div_rem() {
let i64_max = I256::from(i64::MAX);
Expand Down

0 comments on commit 7025d1e

Please sign in to comment.