Esempio n. 1
0
        private static void setFromString(BigInteger bi, String val, int radix)
        {
            int sign;

            int[] digits;
            int   numberLength;
            int   stringLength = val.Length;
            int   startChar;
            int   endChar = stringLength;

            if (val[0] == '-')
            {
                sign      = -1;
                startChar = 1;
                stringLength--;
            }
            else
            {
                sign      = 1;
                startChar = 0;
            }

            /*
             * We use the following algorithm: split a string into portions of n
             * characters and convert each portion to an integer according to the
             * radix. Then convert an exp(radix, n) based number to binary using the
             * multiplication method. See D. Knuth, The Art of Computer Programming,
             * vol. 2.
             */

            int charsPerInt          = Conversion.digitFitInInt[radix];
            int bigRadixDigitsLength = stringLength / charsPerInt;
            int topChars             = stringLength % charsPerInt;

            if (topChars != 0)
            {
                bigRadixDigitsLength++;
            }
            digits = new int[bigRadixDigitsLength];
            // Get the maximal power of radix that fits in int
            int bigRadix = Conversion.bigRadices[radix - 2];
            // Parse an input string and accumulate the BigInteger's magnitude
            int digitIndex = 0; // index of digits array
            int substrEnd  = startChar + ((topChars == 0) ? charsPerInt : topChars);
            int newDigit;

            for (int substrStart = startChar; substrStart < endChar; substrStart = substrEnd, substrEnd = substrStart
                                                                                                          + charsPerInt)
            {
                int bigRadixDigit = Convert.ToInt32(val.Substring(substrStart,
                                                                  substrEnd - substrStart), radix);
                newDigit = Multiplication.multiplyByInt(digits, digitIndex,
                                                        bigRadix);
                newDigit += Elementary
                            .inplaceAdd(digits, digitIndex, bigRadixDigit);
                digits[digitIndex++] = newDigit;
            }
            numberLength    = digitIndex;
            bi.sign         = sign;
            bi.numberLength = numberLength;
            bi.digits       = digits;
            bi.cutOffLeadingZeroes();
        }
Esempio n. 2
0
        internal static BigInteger nextProbablePrime(BigInteger n)
        {
            // PRE: n >= 0
            int i, j;
            int certainty;
            int gapSize = 1024; // for searching of the next probable prime number

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

            // If n < "last prime of table" searches next prime in the table
            if ((n.numberLength == 1) && (n.digits[0] >= 0) &&
                (n.digits[0] < primes[primes.Length - 1]))
            {
                for (i = 0; n.digits[0] >= primes[i]; i++)
                {
                    ;
                }
                return(BIprimes[i]);
            }

            /*
             * Creates a "N" enough big to hold the next probable prime Note that: N <
             * "next prime" < 2*N
             */
            startPoint = new BigInteger(1, n.numberLength,
                                        new int[n.numberLength + 1]);
            Array.Copy(n.digits, startPoint.digits, n.numberLength);
            // To fix N to the "next odd number"
            if (n.testBit(0))
            {
                Elementary.inplaceAdd(startPoint, 2);
            }
            else
            {
                startPoint.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 < primes.Length; i++)
            {
                modules[i] = Division.remainder(startPoint, primes[i]) - gapSize;
            }
            while (true)
            {
                // At this point, all numbers in the gap are initialized as
                // probably primes
                Array.Clear(isDivisible, 0, isDivisible.Length);
                // To discard multiples of first primes
                for (i = 0; i < primes.Length; i++)
                {
                    modules[i] = (modules[i] + gapSize) % primes[i];
                    j          = (modules[i] == 0) ? 0 : (primes[i] - modules[i]);
                    for (; j < gapSize; j += 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);
            }
        }
Esempio n. 3
0
        internal static BigInteger modInverseMontgomery(BigInteger a, BigInteger p)
        {
            if (a.sign == 0)
            {
                // ZERO hasn't inverse
                throw new ArithmeticException("BigInteger not invertible");
            }


            if (!p.testBit(0))
            {
                // montgomery inverse require even modulo
                return(modInverseLorencz(a, 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 = a.copy();
            int max = 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;
            // s == 1 && v == 0

            int k = 0;

            int lsbu = u.getLowestSetBit();
            int lsbv = v.getLowestSetBit();
            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.getLowestSetBit();
                    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.getLowestSetBit();
                    BitLevel.inplaceShiftRight(v, toShift);
                    Elementary.inplaceAdd(s, r);
                    BitLevel.inplaceShiftLeft(r, toShift);
                    k += toShift;
                }
            }
            if (!u.isOne())
            {
                // in u is stored the gcd
                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);
        }