1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-31 13:07:46 +00:00

fixup! factor::numeric::Montgomery: Fix overflow bug

This commit is contained in:
nicoo 2020-06-16 01:17:16 +02:00
parent 4851619d62
commit f1788d9e70

View file

@ -80,16 +80,16 @@ impl Montgomery {
let (xnm, overflow) = (x as u128).overflowing_add(nm); // x + n*m
debug_assert_eq!(xnm % (1 << 64), 0);
if !overflow {
let y = (xnm >> 64) as u64 + overflow as u64; // (x + n*m) / R
if y >= *n {
y - n
} else {
y
}
// (x + n*m) / R
// in case of overflow, this is (2¹²⁸ + xnm)/2⁶⁴ - n = xnm/2⁶⁴ + (2⁶⁴ - n)
let y =
(xnm >> 64) as u64 +
if !overflow { 0 } else { n.wrapping_neg() };
if y >= *n {
y - n
} else {
// y = (2¹²⁸ + xnm)/2⁶⁴ - n = xnm/2⁶⁴ + (2⁶⁴ - n)
(xnm >> 64) as u64 + n.wrapping_neg()
y
}
}
}