BigInteger.shiftLeft C# (CSharp) Method

shiftLeft() private static method

private static shiftLeft ( uint buffer, int shiftVal ) : int
buffer uint
shiftVal int
return int
        private static int shiftLeft(uint[] buffer, int shiftVal)
        {
                int shiftAmount = 32;
                int bufLen = buffer.Length;

                while(bufLen > 1 && buffer[bufLen-1] == 0)
                        bufLen--;

                for(int count = shiftVal; count > 0;)
                {
                        if(count < shiftAmount)
                                shiftAmount = count;

                        //Console.WriteLine("shiftAmount = {0}", shiftAmount);

                        ulong carry = 0;
                        for(int i = 0; i < bufLen; i++)
                        {
                                ulong val = ((ulong)buffer[i]) << shiftAmount;
                                val |= carry;

                                buffer[i] = (uint)(val & 0xFFFFFFFF);
                                carry = val >> 32;
                        }

                        if(carry != 0)
                        {
                                if(bufLen + 1 <= buffer.Length)
                                {
                                        buffer[bufLen] = (uint)carry;
                                        bufLen++;
                                }
                        }
                        count -= shiftAmount;
                }
                return bufLen;
        }

Usage Example

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));
        }
All Usage Examples Of BigInteger::shiftLeft