Ejemplo n.º 1
0
        /// <summary>
        /// It uses the sieve of Eratosthenes to discard several composite numbers in
        /// some appropriate range (at the moment [this, this + 1024]).
        /// <para>After this process it applies the Miller-Rabin test to the numbers that were not discarded in the sieve.</para>
        /// </summary>
        internal static BigInteger NextProbablePrime(BigInteger X)
        {
            // PRE: n >= 0
            int i, j;
            int certainty;
            int gapSize = 1024; // for searching of the next probable prime number

            int[]      modules     = new int[m_primes.Length];
            bool[]     isDivisible = new bool[gapSize];
            BigInteger startPoint;
            BigInteger probPrime;

            // If n < "last prime of table" searches next prime in the table
            if ((X.m_numberLength == 1) && (X.m_digits[0] >= 0) && (X.m_digits[0] < m_primes[m_primes.Length - 1]))
            {
                for (i = 0; X.m_digits[0] >= m_primes[i]; i++)
                {
                    ;
                }

                return(m_biPrimes[i]);
            }
            // Creates a "N" enough big to hold the next probable prime Note that: N < "next prime" < 2*N
            startPoint = new BigInteger(1, X.m_numberLength, new int[X.m_numberLength + 1]);
            Array.Copy(X.m_digits, 0, startPoint.m_digits, 0, X.m_numberLength);

            // To fix N to the "next odd number"
            if (X.TestBit(0))
            {
                Elementary.InplaceAdd(startPoint, 2);
            }
            else
            {
                startPoint.m_digits[0] |= 1;
            }

            // To set the improved certainly of Miller-Rabin
            j = startPoint.BitLength;
            for (certainty = 2; j < BITS[certainty]; certainty++)
            {
                ;
            }
            // To calculate modules: N mod p1, N mod p2, ... for first primes.
            for (i = 0; i < m_primes.Length; i++)
            {
                modules[i] = Division.Remainder(startPoint, m_primes[i]) - gapSize;
            }

            while (true)
            {
                // At this point, all numbers in the gap are initialized as probably primes
                for (int k = 0; k < isDivisible.Length; k++)
                {
                    isDivisible[k] = false;
                }

                // To discard multiples of first primes
                for (i = 0; i < m_primes.Length; i++)
                {
                    modules[i] = (modules[i] + gapSize) % m_primes[i];
                    j          = (modules[i] == 0) ? 0 : (m_primes[i] - modules[i]);

                    for (; j < gapSize; j += m_primes[i])
                    {
                        isDivisible[j] = true;
                    }
                }
                // To execute Miller-Rabin for non-divisible numbers by all first
                // primes
                for (j = 0; j < gapSize; j++)
                {
                    if (!isDivisible[j])
                    {
                        probPrime = startPoint.Copy();
                        Elementary.InplaceAdd(probPrime, j);

                        if (MillerRabin(probPrime, certainty))
                        {
                            return(probPrime);
                        }
                    }
                }
                Elementary.InplaceAdd(startPoint, gapSize);
            }
        }
Ejemplo n.º 2
0
        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.m_numberLength, Modulo.m_numberLength);

            int[] uDigits = new int[max + 1]; // enough place to make all the inplace operation
            int[] vDigits = new int[max + 1];
            Array.Copy(Modulo.m_digits, 0, uDigits, 0, Modulo.m_numberLength);
            Array.Copy(X.m_digits, 0, vDigits, 0, X.m_numberLength);

            BigInteger u = new BigInteger(Modulo.m_sign, Modulo.m_numberLength, uDigits);
            BigInteger v = new BigInteger(X.m_sign, X.m_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.m_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);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// See BigInteger#subtract(BigInteger)
        /// </summary>
        internal static BigInteger Subtract(BigInteger A, BigInteger B)
        {
            int resSign;

            int[] resDigits;
            int   op1Sign = A._sign;
            int   op2Sign = B._sign;

            if (op2Sign == 0)
            {
                return(A);
            }
            if (op1Sign == 0)
            {
                return(B.Negate());
            }

            int op1Len = A._numberLength;
            int op2Len = B._numberLength;

            if (op1Len + op2Len == 2)
            {
                long a = (A._digits[0] & 0xFFFFFFFFL);
                long b = (B._digits[0] & 0xFFFFFFFFL);

                if (op1Sign < 0)
                {
                    a = -a;
                }
                if (op2Sign < 0)
                {
                    b = -b;
                }

                return(BigInteger.ValueOf(a - b));
            }
            int cmp = ((op1Len != op2Len) ?
                       ((op1Len > op2Len) ? 1 : -1) :
                       Elementary.CompareArrays(A._digits, B._digits, op1Len));

            if (cmp == BigInteger.LESS)
            {
                resSign   = -op2Sign;
                resDigits = (op1Sign == op2Sign) ?
                            Subtract(B._digits, op2Len, A._digits, op1Len) :
                            Add(B._digits, op2Len, A._digits, op1Len);
            }
            else
            {
                resSign = op1Sign;
                if (op1Sign == op2Sign)
                {
                    if (cmp == BigInteger.EQUALS)
                    {
                        return(BigInteger.Zero);
                    }

                    resDigits = Subtract(A._digits, op1Len, B._digits, op2Len);
                }
                else
                {
                    resDigits = Add(A._digits, op1Len, B._digits, op2Len);
                }
            }
            BigInteger res = new BigInteger(resSign, resDigits.Length, resDigits);

            res.CutOffLeadingZeroes();

            return(res);
        }
Ejemplo n.º 4
0
        /// <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.m_sign == 0)
            {
                throw new ArithmeticException("BigInteger not invertible!");
            }

            // montgomery inverse require even modulo
            if (!P.TestBit(0))
            {
                return(ModInverseLorencz(X, P));
            }

            int m = P.m_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.m_numberLength, u.m_numberLength);

            r             = new BigInteger(1, 1, new int[max + 1]);
            s             = new BigInteger(1, 1, new int[max + 1]);
            s.m_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.m_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);
        }