mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-08-01 13:37:48 +00:00
fixup! factor::numeric::Montgomery: Fix overflow bug
This commit is contained in:
parent
4851619d62
commit
f1788d9e70
1 changed files with 9 additions and 9 deletions
|
@ -80,16 +80,16 @@ impl Montgomery {
|
||||||
let (xnm, overflow) = (x as u128).overflowing_add(nm); // x + n*m
|
let (xnm, overflow) = (x as u128).overflowing_add(nm); // x + n*m
|
||||||
debug_assert_eq!(xnm % (1 << 64), 0);
|
debug_assert_eq!(xnm % (1 << 64), 0);
|
||||||
|
|
||||||
if !overflow {
|
// (x + n*m) / R
|
||||||
let y = (xnm >> 64) as u64 + overflow as u64; // (x + n*m) / R
|
// in case of overflow, this is (2¹²⁸ + xnm)/2⁶⁴ - n = xnm/2⁶⁴ + (2⁶⁴ - n)
|
||||||
if y >= *n {
|
let y =
|
||||||
y - n
|
(xnm >> 64) as u64 +
|
||||||
} else {
|
if !overflow { 0 } else { n.wrapping_neg() };
|
||||||
y
|
|
||||||
}
|
if y >= *n {
|
||||||
|
y - n
|
||||||
} else {
|
} else {
|
||||||
// y = (2¹²⁸ + xnm)/2⁶⁴ - n = xnm/2⁶⁴ + (2⁶⁴ - n)
|
y
|
||||||
(xnm >> 64) as u64 + n.wrapping_neg()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue