shiftLeft() private static method

private static shiftLeft ( uint buffer, int shiftVal ) : int
buffer uint
shiftVal int
return int
Ejemplo n.º 1
0
        /*
         * Performs modular exponentiation using the Montgomery Reduction. It
         * requires that all parameters be positive and the modulus be odd. >
         *
         * @see BigInteger#modPow(BigInteger, BigInteger)
         * @see #monPro(BigInteger, BigInteger, BigInteger, int)
         * @see #slidingWindow(BigInteger, BigInteger, BigInteger, BigInteger,
         *                      int)
         * @see #squareAndMultiply(BigInteger, BigInteger, BigInteger, BigInteger,
         *                      int)
         */
        internal static BigInteger oddModPow(BigInteger baseJ, BigInteger exponent,
                                             BigInteger modulus)
        {
            // PRE: (base > 0), (exponent > 0), (modulus > 0) and (odd modulus)
            int k = (modulus.numberLength << 5); // r = 2^k
            // n-residue of base [base * r (mod modulus)]
            BigInteger a2 = baseJ.shiftLeft(k).mod(modulus);
            // n-residue of base [1 * r (mod modulus)]
            BigInteger x2 = BigInteger.getPowerOfTwo(k).mod(modulus);
            BigInteger res;
            // Compute (modulus[0]^(-1)) (mod 2^32) for odd modulus

            int n2 = calcN(modulus);

            if (modulus.numberLength == 1)
            {
                res = squareAndMultiply(x2, a2, exponent, modulus, n2);
            }
            else
            {
                res = slidingWindow(x2, a2, exponent, modulus, n2);
            }

            return(monPro(res, BigInteger.ONE, modulus, n2));
        }
Ejemplo n.º 2
0
        public int ensureCapacity(BigInteger range)
        {
            BigInteger calcsize = BigInteger.valueOf(2L);

            if (range.signum() < 0)
            {
                throw new BDDException();
            }
            if (range.compareTo(realsize) < 0)
            {
                return(ivar.Length);
            }
            this.realsize = range.add(BigInteger.ONE);
            int binsize = 1;

            while (calcsize.compareTo(range) <= 0)
            {
                binsize++;
                calcsize = calcsize.shiftLeft(1);
            }
            if (ivar.Length == binsize)
            {
                return(binsize);
            }

            int[] new_ivar = new int[binsize];
            System.arraycopy(ivar, 0, new_ivar, 0, ivar.Length);
            BDDFactory factory = getFactory();

            for (int i = ivar.Length; i < new_ivar.Length; ++i)
            {
                //System.out.println("Domain "+this+" Duplicating var#"+new_ivar[i-1]);
                int newVar = factory.duplicateVar(new_ivar[i - 1]);
                factory.firstbddvar++;
                new_ivar[i] = newVar;
                //System.out.println("Domain "+this+" var#"+i+" = "+newVar);
            }
            this.ivar = new_ivar;
            //System.out.println("Domain "+this+" old var = "+var);
            this.var.free();
            BDD nvar = factory.one();

            for (int i = 0; i < ivar.Length; ++i)
            {
                nvar.andWith(factory.ithVar(ivar[i]));
            }
            this.var = nvar;
            //System.out.println("Domain "+this+" new var = "+var);
            return(binsize);
        }
Ejemplo n.º 3
0
        public static BigInteger padPKCS1(BigInteger input, int type,
                                          int padLen, RandomNumberGenerator rand)
        {
            BigInteger result;
            BigInteger rndInt;
            int        inByteLen = (input.bitLength() + 7) / 8;

            if (inByteLen > padLen - 11)
            {
                throw new Exception("PKCS1 failed to pad input! "
                                    + "input=" + inByteLen
                                    + " padding=" + padLen);
            }

            byte[] padBytes = new byte[(padLen - inByteLen - 3) + 1];
            padBytes[0] = 0;

            for (int i = 1; i < (padLen - inByteLen - 3) + 1; i++)
            {
                if (type == 0x01)
                {
                    padBytes[i] = (byte)0xff;
                }
                else
                {
                    byte[] b = new byte[1];
                    do
                    {
                        rand.GetBytes(b);
                    }while (b[0] == 0);
                    padBytes[i] = b[0];
                }
            }

            rndInt = new BigInteger(1, padBytes);
            rndInt = rndInt.shiftLeft((inByteLen + 1) * 8);
            result = BigInteger.valueOf(type);
            result = result.shiftLeft((padLen - 2) * 8);
            result = result.or(rndInt);
            result = result.or(input);

            return(result);
        }
Ejemplo n.º 4
0
        /**
         * Default constructor.
         *
         * @param index  index of this domain
         * @param range  size of this domain
         */
        protected BDDDomain(int index, BigInteger range)
        {
            BigInteger calcsize = BigInteger.valueOf(2L);

            if (range.signum() <= 0)
            {
                throw new BDDException();
            }
            this.name     = Integer.toString(index);
            this.index    = index;
            this.realsize = range;
            int binsize = 1;

            while (calcsize.compareTo(range) < 0)
            {
                binsize++;
                calcsize = calcsize.shiftLeft(1);
            }
            this.ivar = new int[binsize];
        }
Ejemplo n.º 5
0
        /*
         * Performs the multiplication with the Karatsuba's algorithm.
         * <b>Karatsuba's algorithm:</b>
         *<tt>
         *             u = u<sub>1</sub> * B + u<sub>0</sub><br>
         *             v = v<sub>1</sub> * B + v<sub>0</sub><br>
         *
         *
         *  u*v = (u<sub>1</sub> * v<sub>1</sub>) * B<sub>2</sub> + ((u<sub>1</sub> - u<sub>0</sub>) * (v<sub>0</sub> - v<sub>1</sub>) + u<sub>1</sub> * v<sub>1</sub> +
         *  u<sub>0</sub> * v<sub>0</sub> ) * B + u<sub>0</sub> * v<sub>0</sub><br>
         *</tt>
         * @param op1 first factor of the product
         * @param op2 second factor of the product
         * @return {@code op1 * op2}
         * @see #multiply(BigInteger, BigInteger)
         */
        internal 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));
        }
Ejemplo n.º 6
0
        /**
         * Performs modular exponentiation using the Montgomery Reduction. It
         * requires that all parameters be positive and the modulus be odd. >
         *
         * @see BigInteger#modPow(BigInteger, BigInteger)
         * @see #monPro(BigInteger, BigInteger, BigInteger, int)
         * @see #slidingWindow(BigInteger, BigInteger, BigInteger, BigInteger,
         *                      int)
         * @see #squareAndMultiply(BigInteger, BigInteger, BigInteger, BigInteger,
         *                      int)
         */
        internal static BigInteger oddModPow(BigInteger baseJ, BigInteger exponent,
                BigInteger modulus)
        {
            // PRE: (base > 0), (exponent > 0), (modulus > 0) and (odd modulus)
            int k = (modulus.numberLength << 5); // r = 2^k
            // n-residue of base [base * r (mod modulus)]
            BigInteger a2 = baseJ.shiftLeft(k).mod(modulus);
            // n-residue of base [1 * r (mod modulus)]
            BigInteger x2 = BigInteger.getPowerOfTwo(k).mod(modulus);
            BigInteger res;
            // Compute (modulus[0]^(-1)) (mod 2^32) for odd modulus

            int n2 = calcN(modulus);
            if( modulus.numberLength == 1 ){
                res = squareAndMultiply(x2,a2, exponent, modulus,n2);
            } else {
                res = slidingWindow(x2, a2, exponent, modulus, n2);
            }

            return monPro(res, BigInteger.ONE, modulus, n2);
        }
Ejemplo n.º 7
0
        /**
         * @param m a positive modulus
         * Return the greatest common divisor of op1 and op2,
         *
         * @param op1
         *            must be greater than zero
         * @param op2
         *            must be greater than zero
         * @see BigInteger#gcd(BigInteger)
         * @return {@code GCD(op1, op2)}
         */
        internal static BigInteger gcdBinary(BigInteger op1, BigInteger op2)
        {
            // PRE: (op1 > 0) and (op2 > 0)

            /*
             * Divide both number the maximal possible times by 2 without rounding
                     * gcd(2*a, 2*b) = 2 * gcd(a,b)
             */
            int lsb1 = op1.getLowestSetBit();
            int lsb2 = op2.getLowestSetBit();
            int pow2Count = java.lang.Math.min(lsb1, lsb2);

                BitLevel.inplaceShiftRight(op1, lsb1);
                BitLevel.inplaceShiftRight(op2, lsb2);

            BigInteger swap;
            // I want op2 > op1
            if (op1.compareTo(op2) == BigInteger.GREATER) {
                swap = op1;
                op1 = op2;
                op2 = 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 (( op2.numberLength == 1 )
                || ( ( op2.numberLength == 2 ) && ( op2.digits[1] > 0 ) )) {
                    op2 = BigInteger.valueOf(Division.gcdBinary(op1.longValue(),
                            op2.longValue()));
                    break;
            }

                // Implements one step of the Euclidean algorithm
                // To reduce one operand if it's much smaller than the other one
                if (op2.numberLength > op1.numberLength * 1.2) {
                    op2 = op2.remainder(op1);
                    if (op2.signum() != 0) {
                        BitLevel.inplaceShiftRight(op2, op2.getLowestSetBit());
                }
                } else {

                    // Use Knuth's algorithm of successive subtract and shifting
                    do {
                        Elementary.inplaceSubtract(op2, op1); // both are odd
                        BitLevel.inplaceShiftRight(op2, op2.getLowestSetBit()); // op2 is even
                    } while (op2.compareTo(op1) >= BigInteger.EQUALS);
                }
                // now op1 >= op2
                swap = op2;
                op2 = op1;
                op1 = swap;
            } while (op1.sign != 0);
            return op2.shiftLeft(pow2Count);
        }
Ejemplo n.º 8
0
        /*
         * @param m a positive modulus
         * Return the greatest common divisor of op1 and op2,
         *
         * @param op1
         *            must be greater than zero
         * @param op2
         *            must be greater than zero
         * @see BigInteger#gcd(BigInteger)
         * @return {@code GCD(op1, op2)}
         */
        internal static BigInteger gcdBinary(BigInteger op1, BigInteger op2)
        {
            // PRE: (op1 > 0) and (op2 > 0)

            /*
             * Divide both number the maximal possible times by 2 without rounding
             * gcd(2*a, 2*b) = 2 * gcd(a,b)
             */
            int lsb1      = op1.getLowestSetBit();
            int lsb2      = op2.getLowestSetBit();
            int pow2Count = java.lang.Math.min(lsb1, lsb2);

            BitLevel.inplaceShiftRight(op1, lsb1);
            BitLevel.inplaceShiftRight(op2, lsb2);

            BigInteger swap;

            // I want op2 > op1
            if (op1.compareTo(op2) == BigInteger.GREATER)
            {
                swap = op1;
                op1  = op2;
                op2  = 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 ((op2.numberLength == 1) ||
                    ((op2.numberLength == 2) && (op2.digits[1] > 0)))
                {
                    op2 = BigInteger.valueOf(Division.gcdBinary(op1.longValue(),
                                                                op2.longValue()));
                    break;
                }

                // Implements one step of the Euclidean algorithm
                // To reduce one operand if it's much smaller than the other one
                if (op2.numberLength > op1.numberLength * 1.2)
                {
                    op2 = op2.remainder(op1);
                    if (op2.signum() != 0)
                    {
                        BitLevel.inplaceShiftRight(op2, op2.getLowestSetBit());
                    }
                }
                else
                {
                    // Use Knuth's algorithm of successive subtract and shifting
                    do
                    {
                        Elementary.inplaceSubtract(op2, op1);                   // both are odd
                        BitLevel.inplaceShiftRight(op2, op2.getLowestSetBit()); // op2 is even
                    } while (op2.compareTo(op1) >= BigInteger.EQUALS);
                }
                // now op1 >= op2
                swap = op2;
                op2  = op1;
                op1  = swap;
            } while (op1.sign != 0);
            return(op2.shiftLeft(pow2Count));
        }
Ejemplo n.º 9
0
        /*
         * It calculates a power of ten, which exponent could be out of 32-bit range.
         * Note that internally this method will be used in the worst case with
         * an exponent equals to: {@code Integer.MAX_VALUE - Integer.MIN_VALUE}.
         * @param exp the exponent of power of ten, it must be positive.
         * @return a {@code BigInteger} with value {@code 10<sup>exp</sup>}.
         */
        internal static BigInteger powerOf10(long exp)
        {
            // PRE: exp >= 0
            int intExp = (int)exp;

            // "SMALL POWERS"
            if (exp < bigTenPows.Length)
            {
                // The largest power that fit in 'long' type
                return(bigTenPows[intExp]);
            }
            else if (exp <= 50)
            {
                // To calculate:    10^exp
                return(BigInteger.TEN.pow(intExp));
            }
            else if (exp <= 1000)
            {
                // To calculate:    5^exp * 2^exp
                return(bigFivePows[1].pow(intExp).shiftLeft(intExp));
            }
            // "LARGE POWERS"

            /*
             * To check if there is free memory to allocate a BigInteger of the
             * estimated size, measured in bytes: 1 + [exp / log10(2)]
             */
            long byteArraySize = 1 + (long)(exp / 2.4082399653118496);

            if (byteArraySize > java.lang.Runtime.getRuntime().freeMemory())
            {
                // math.01=power of ten too big
                throw new ArithmeticException("power of ten too big"); //$NON-NLS-1$
            }
            if (exp <= java.lang.Integer.MAX_VALUE)
            {
                // To calculate:    5^exp * 2^exp
                return(bigFivePows[1].pow(intExp).shiftLeft(intExp));
            }

            /*
             * "HUGE POWERS"
             *
             * This branch probably won't be executed since the power of ten is too
             * big.
             */
            // To calculate:    5^exp
            BigInteger powerOfFive = bigFivePows[1].pow(java.lang.Integer.MAX_VALUE);
            BigInteger res         = powerOfFive;
            long       longExp     = exp - java.lang.Integer.MAX_VALUE;

            intExp = (int)(exp % java.lang.Integer.MAX_VALUE);
            while (longExp > java.lang.Integer.MAX_VALUE)
            {
                res      = res.multiply(powerOfFive);
                longExp -= java.lang.Integer.MAX_VALUE;
            }
            res = res.multiply(bigFivePows[1].pow(intExp));
            // To calculate:    5^exp << exp
            res     = res.shiftLeft(java.lang.Integer.MAX_VALUE);
            longExp = exp - java.lang.Integer.MAX_VALUE;
            while (longExp > java.lang.Integer.MAX_VALUE)
            {
                res      = res.shiftLeft(java.lang.Integer.MAX_VALUE);
                longExp -= java.lang.Integer.MAX_VALUE;
            }
            res = res.shiftLeft(intExp);
            return(res);
        }