/// <summary> /// Return the greatest common divisor of X and Y /// </summary> /// /// <param name="X">Operand 1, must be greater than zero</param> /// <param name="Y">Operand 2, must be greater than zero</param> /// /// <returns>Returns <c>GCD(X, Y)</c></returns> internal static BigInteger GcdBinary(BigInteger X, BigInteger Y) { // Divide both number the maximal possible times by 2 without rounding * gcd(2*a, 2*b) = 2 * gcd(a,b) int lsb1 = X.LowestSetBit; int lsb2 = Y.LowestSetBit; int pow2Count = System.Math.Min(lsb1, lsb2); BitLevel.InplaceShiftRight(X, lsb1); BitLevel.InplaceShiftRight(Y, lsb2); BigInteger swap; // I want op2 > op1 if (X.CompareTo(Y) == BigInteger.GREATER) { swap = X; X = Y; Y = swap; } do { // INV: op2 >= op1 && both are odd unless op1 = 0 // Optimization for small operands (op2.bitLength() < 64) implies by INV (op1.bitLength() < 64) if ((Y._numberLength == 1) || ((Y._numberLength == 2) && (Y._digits[1] > 0))) { Y = BigInteger.ValueOf(Division.GcdBinary(X.ToInt64(), Y.ToInt64())); break; } // Implements one step of the Euclidean algorithm // To reduce one operand if it's much smaller than the other one if (Y._numberLength > X._numberLength * 1.2) { Y = Y.Remainder(X); if (Y.Signum() != 0) { BitLevel.InplaceShiftRight(Y, Y.LowestSetBit); } } else { // Use Knuth's algorithm of successive subtract and shifting do { Elementary.InplaceSubtract(Y, X); // both are odd BitLevel.InplaceShiftRight(Y, Y.LowestSetBit); // op2 is even } while (Y.CompareTo(X) >= BigInteger.EQUALS); } // now op1 >= op2 swap = Y; Y = X; X = swap; } while (X._sign != 0); return(Y.ShiftLeft(pow2Count)); }
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); }