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

factor::numeric::Montgomery::add: Deal with rare overflow case

This commit is contained in:
nicoo 2020-06-18 13:01:55 +02:00
parent d1470dadf8
commit fb08d9ff9e

View file

@ -123,7 +123,17 @@ impl Arithmetic for Montgomery {
}
fn add(&self, a: Self::I, b: Self::I) -> Self::I {
let r = a + b;
let (r, overflow) = a.overflowing_add(b);
// In case of overflow, a+b = 2⁶⁴ + r = (2⁶⁴ - n) + r (working mod n)
let r = if !overflow {
r
} else {
r + self.n.wrapping_neg()
};
// Normalise to [0; n[
let r = if r < self.n { r } else { r - self.n };
// Check that r (reduced back to the usual representation) equals
// a+b % n