Skip to content

Commit

Permalink
Remove the last use of SignedDoubleBigDigit
Browse files Browse the repository at this point in the history
The inverse for `MontyReducer` doesn't need a wider type at all!
  • Loading branch information
cuviper committed May 11, 2024
1 parent d9fb6e6 commit 98bea13
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
9 changes: 5 additions & 4 deletions src/biguint/monty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::mem;
use core::ops::Shl;
use num_traits::One;

use crate::big_digit::{self, BigDigit, DoubleBigDigit, SignedDoubleBigDigit};
use crate::big_digit::{self, BigDigit, DoubleBigDigit};
use crate::biguint::BigUint;

struct MontyReducer {
Expand All @@ -15,16 +15,17 @@ struct MontyReducer {
fn inv_mod_alt(b: BigDigit) -> BigDigit {
assert_ne!(b & 1, 0);

let mut k0 = 2 - b as SignedDoubleBigDigit;
let mut t = (b - 1) as SignedDoubleBigDigit;
let mut k0 = BigDigit::wrapping_sub(2, b);
let mut t = b - 1;
let mut i = 1;
while i < big_digit::BITS {
t = t.wrapping_mul(t);
k0 = k0.wrapping_mul(t + 1);

i <<= 1;
}
-k0 as BigDigit
debug_assert_eq!(k0.wrapping_mul(b), 1);
k0.wrapping_neg()
}

impl MontyReducer {
Expand Down
6 changes: 0 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,6 @@ mod big_digit {
pub(crate) type DoubleBigDigit = u128;
);

// A [`SignedDoubleBigDigit`] is the signed version of [`DoubleBigDigit`].
cfg_digit!(
pub(crate) type SignedDoubleBigDigit = i64;
pub(crate) type SignedDoubleBigDigit = i128;
);

pub(crate) const BITS: u8 = BigDigit::BITS as u8;
pub(crate) const HALF_BITS: u8 = BITS / 2;
pub(crate) const HALF: BigDigit = (1 << HALF_BITS) - 1;
Expand Down

0 comments on commit 98bea13

Please sign in to comment.