8225603: Enhancement for big integers

Reviewed-by: darcy, ahgross, rhalade
This commit is contained in:
Brian Burkhalter 2019-10-29 14:07:27 -07:00
parent 14c0c19539
commit 833a3897dc
3 changed files with 79 additions and 9 deletions

View file

@ -2178,8 +2178,8 @@ class MutableBigInteger {
}
/**
* Calculate the multiplicative inverse of this mod mod, where mod is odd.
* This and mod are not changed by the calculation.
* Calculate the multiplicative inverse of this modulo mod, where the mod
* argument is odd. This and mod are not changed by the calculation.
*
* This method implements an algorithm due to Richard Schroeppel, that uses
* the same intermediate representation as Montgomery Reduction
@ -2233,8 +2233,18 @@ class MutableBigInteger {
k += trailingZeros;
}
while (c.sign < 0)
c.signedAdd(p);
if (c.compare(p) >= 0) { // c has a larger magnitude than p
MutableBigInteger remainder = c.divide(p,
new MutableBigInteger());
// The previous line ignores the sign so we copy the data back
// into c which will restore the sign as needed (and converts
// it back to a SignedMutableBigInteger)
c.copyValue(remainder);
}
if (c.sign < 0) {
c.signedAdd(p);
}
return fixup(c, p, k);
}
@ -2272,8 +2282,8 @@ class MutableBigInteger {
}
// In theory, c may be greater than p at this point (Very rare!)
while (c.compare(p) >= 0)
c.subtract(p);
if (c.compare(p) >= 0)
c = c.divide(p, new MutableBigInteger());
return c;
}