예제 #1
0
        internal static BigInteger evenModPow(BigInteger _base, BigInteger exponent,
                                              BigInteger modulus)
        {
            // PRE: (base > 0), (exponent > 0), (modulus > 0) and (modulus even)
            // STEP 1: Obtain the factorization 'modulus'= q * 2^j.
            int        j = modulus.getLowestSetBit();
            BigInteger q = modulus.shiftRight(j);

            // STEP 2: Compute x1 := base^exponent (mod q).
            BigInteger x1 = oddModPow(_base, exponent, q);

            // STEP 3: Compute x2 := base^exponent (mod 2^j).
            BigInteger x2 = pow2ModPow(_base, exponent, j);

            // STEP 4: Compute q^(-1) (mod 2^j) and y := (x2-x1) * q^(-1) (mod 2^j)
            BigInteger qInv = modPow2Inverse(q, j);
            BigInteger y    = (x2.subtract(x1)).multiply(qInv);

            inplaceModPow2(y, j);
            if (y.sign < 0)
            {
                y = y.add(BigInteger.getPowerOfTwo(j));
            }
            // STEP 5: Compute and return: x1 + q * y
            return(x1.add(q.multiply(y)));
        }
예제 #2
0
        public BigInteger mod(BigInteger m)
        {
            if (m.sign <= 0)
            {
                throw new ArithmeticException("BigInteger: modulus not positive");
            }
            BigInteger rem = remainder(m);

            return((rem.sign < 0) ? rem.add(m) : rem);
        }
예제 #3
0
        public static BigInteger karatsuba(BigInteger op1, BigInteger op2)
        {
            BigInteger temp;

            if (op2.numberLength > op1.numberLength)
            {
                temp = op1;
                op1  = op2;
                op2  = temp;
            }
            if (op2.numberLength < whenUseKaratsuba)
            {
                return(multiplyPAP(op1, op2));
            }

            /*  Karatsuba:  u = u1*B + u0
             *              v = v1*B + v0
             *  u*v = (u1*v1)*B^2 + ((u1-u0)*(v0-v1) + u1*v1 + u0*v0)*B + u0*v0
             */
            // ndiv2 = (op1.numberLength / 2) * 32
            int        ndiv2    = (int)(op1.numberLength & 0xFFFFFFFE) << 4;
            BigInteger upperOp1 = op1.shiftRight(ndiv2);
            BigInteger upperOp2 = op2.shiftRight(ndiv2);
            BigInteger lowerOp1 = op1.subtract(upperOp1.shiftLeft(ndiv2));
            BigInteger lowerOp2 = op2.subtract(upperOp2.shiftLeft(ndiv2));

            BigInteger upper  = karatsuba(upperOp1, upperOp2);
            BigInteger lower  = karatsuba(lowerOp1, lowerOp2);
            BigInteger middle = karatsuba(upperOp1.subtract(lowerOp1),
                                          lowerOp2.subtract(upperOp2));

            middle = middle.add(upper).add(lower);
            middle = middle.shiftLeft(ndiv2);
            upper  = upper.shiftLeft(ndiv2 << 1);

            return(upper.add(middle).add(lower));
        }
예제 #4
0
파일: Division.cs 프로젝트: vic/ioke
        internal static BigInteger modInverseLorencz(BigInteger a, BigInteger modulo)
        {
            int max = Math.Max(a.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, uDigits, modulo.numberLength);
            Array.Copy(a.digits, vDigits, a.numberLength);
            BigInteger u = new BigInteger(modulo.sign, modulo.numberLength,
                                          uDigits);
            BigInteger v = new BigInteger(a.sign, a.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, 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, 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;
        }
예제 #5
0
        internal static BigInteger modInverseLorencz(BigInteger a, BigInteger modulo)
        {
            int max = Math.Max(a.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, uDigits, modulo.numberLength);
            Array.Copy(a.digits, vDigits, a.numberLength);
            BigInteger u = new BigInteger(modulo.sign, modulo.numberLength,
                                          uDigits);
            BigInteger v = new BigInteger(a.sign, a.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, 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, 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);
        }