private static BigInteger ModInverseLorencz(BigInteger X, BigInteger Modulo) { // Based on "New Algorithm for Classical Modular Inverse" Róbert Lórencz. LNCS 2523 (2002) // PRE: a is coprime with modulo, a < modulo int max = System.Math.Max(X._numberLength, Modulo._numberLength); int[] uDigits = new int[max + 1]; // enough place to make all the inplace operation int[] vDigits = new int[max + 1]; Array.Copy(Modulo._digits, 0, uDigits, 0, Modulo._numberLength); Array.Copy(X._digits, 0, vDigits, 0, X._numberLength); BigInteger u = new BigInteger(Modulo._sign, Modulo._numberLength, uDigits); BigInteger v = new BigInteger(X._sign, X._numberLength, vDigits); BigInteger r = new BigInteger(0, 1, new int[max + 1]); // BigInteger.ZERO; BigInteger s = new BigInteger(1, 1, new int[max + 1]); s._digits[0] = 1; // r == 0 && s == 1, but with enough place int coefU = 0, coefV = 0; int n = Modulo.BitLength; int k; while (!IsPowerOfTwo(u, coefU) && !IsPowerOfTwo(v, coefV)) { // modification of original algorithm: I calculate how many times the algorithm will enter in the same branch of if k = HowManyIterations(u, n); if (k != 0) { BitLevel.InplaceShiftLeft(u, k); if (coefU >= coefV) { BitLevel.InplaceShiftLeft(r, k); } else { BitLevel.InplaceShiftRight(s, System.Math.Min(coefV - coefU, k)); if (k - (coefV - coefU) > 0) { BitLevel.InplaceShiftLeft(r, k - coefV + coefU); } } coefU += k; } k = HowManyIterations(v, n); if (k != 0) { BitLevel.InplaceShiftLeft(v, k); if (coefV >= coefU) { BitLevel.InplaceShiftLeft(s, k); } else { BitLevel.InplaceShiftRight(r, System.Math.Min(coefU - coefV, k)); if (k - (coefU - coefV) > 0) { BitLevel.InplaceShiftLeft(s, k - coefU + coefV); } } coefV += k; } if (u.Signum() == v.Signum()) { if (coefU <= coefV) { Elementary.CompleteInPlaceSubtract(u, v); Elementary.CompleteInPlaceSubtract(r, s); } else { Elementary.CompleteInPlaceSubtract(v, u); Elementary.CompleteInPlaceSubtract(s, r); } } else { if (coefU <= coefV) { Elementary.CompleteInPlaceAdd(u, v); Elementary.CompleteInPlaceAdd(r, s); } else { Elementary.CompleteInPlaceAdd(v, u); Elementary.CompleteInPlaceAdd(s, r); } } if (v.Signum() == 0 || u.Signum() == 0) { throw new ArithmeticException("BigInteger not invertible"); } } if (IsPowerOfTwo(v, coefV)) { r = s; if (v.Signum() != u.Signum()) { u = u.Negate(); } } if (u.TestBit(n)) { if (r.Signum() < 0) { r = r.Negate(); } else { r = Modulo.Subtract(r); } } if (r.Signum() < 0) { r = r.Add(Modulo); } return(r); }
/// <summary> /// Calculates x.modInverse(p) Based on: Savas, E; Koc, C "The Montgomery Modular Inverse - Revised" /// </summary> /// /// <param name="X">BigInteger X</param> /// <param name="P">BigInteger P</param> /// /// <returns>Returns <c>1/X Mod M</c></returns> internal static BigInteger ModInverseMontgomery(BigInteger X, BigInteger P) { // ZERO hasn't inverse if (X._sign == 0) { throw new ArithmeticException("BigInteger not invertible!"); } // montgomery inverse require even modulo if (!P.TestBit(0)) { return(ModInverseLorencz(X, P)); } int m = P._numberLength * 32; // PRE: a \in [1, p - 1] BigInteger u, v, r, s; u = P.Copy(); // make copy to use inplace method v = X.Copy(); int max = System.Math.Max(v._numberLength, u._numberLength); r = new BigInteger(1, 1, new int[max + 1]); s = new BigInteger(1, 1, new int[max + 1]); s._digits[0] = 1; int k = 0; int lsbu = u.LowestSetBit; int lsbv = v.LowestSetBit; int toShift; if (lsbu > lsbv) { BitLevel.InplaceShiftRight(u, lsbu); BitLevel.InplaceShiftRight(v, lsbv); BitLevel.InplaceShiftLeft(r, lsbv); k += lsbu - lsbv; } else { BitLevel.InplaceShiftRight(u, lsbu); BitLevel.InplaceShiftRight(v, lsbv); BitLevel.InplaceShiftLeft(s, lsbu); k += lsbv - lsbu; } r._sign = 1; while (v.Signum() > 0) { // INV v >= 0, u >= 0, v odd, u odd (except last iteration when v is even (0)) while (u.CompareTo(v) > BigInteger.EQUALS) { Elementary.InplaceSubtract(u, v); toShift = u.LowestSetBit; BitLevel.InplaceShiftRight(u, toShift); Elementary.InplaceAdd(r, s); BitLevel.InplaceShiftLeft(s, toShift); k += toShift; } while (u.CompareTo(v) <= BigInteger.EQUALS) { Elementary.InplaceSubtract(v, u); if (v.Signum() == 0) { break; } toShift = v.LowestSetBit; BitLevel.InplaceShiftRight(v, toShift); Elementary.InplaceAdd(s, r); BitLevel.InplaceShiftLeft(r, toShift); k += toShift; } } // in u is stored the gcd if (!u.IsOne()) { throw new ArithmeticException("BigInteger not invertible."); } if (r.CompareTo(P) >= BigInteger.EQUALS) { Elementary.InplaceSubtract(r, P); } r = P.Subtract(r); // Have pair: ((BigInteger)r, (Integer)k) where r == a^(-1) * 2^k mod (module) int n1 = CalcN(P); if (k > m) { r = MonPro(r, BigInteger.One, P, n1); k = k - m; } r = MonPro(r, BigInteger.GetPowerOfTwo(m - k), P, n1); return(r); }