BigInteger.modPow C# (CSharp) Method

modPow() public method

public modPow ( BigInteger exp, BigInteger n ) : BigInteger
exp BigInteger
n BigInteger
return BigInteger
        public BigInteger modPow(BigInteger exp, BigInteger n)
        {
                if((exp.data[maxLength-1] & 0x80000000) != 0)
                        throw (new ArithmeticException("Positive exponents only."));

                BigInteger resultNum = 1;
	        BigInteger tempNum;
	        bool thisNegative = false;

	        if((this.data[maxLength-1] & 0x80000000) != 0)   // negative this
	        {
	                tempNum = -this % n;
	                thisNegative = true;
	        }
	        else
	                tempNum = this % n;  // ensures (tempNum * tempNum) < b^(2k)

	        if((n.data[maxLength-1] & 0x80000000) != 0)   // negative n
	                n = -n;

                // calculate constant = b^(2k) / m
                BigInteger constant = new BigInteger();

                int i = n.dataLength << 1;
                constant.data[i] = 0x00000001;
                constant.dataLength = i + 1;

                constant = constant / n;
                int totalBits = exp.bitCount();
                int count = 0;

                // perform squaring and multiply exponentiation
                for(int pos = 0; pos < exp.dataLength; pos++)
                {
                        uint mask = 0x01;
                        //Console.WriteLine("pos = " + pos);

                        for(int index = 0; index < 32; index++)
                        {
                                if((exp.data[pos] & mask) != 0)
                                        resultNum = BarrettReduction(resultNum * tempNum, n, constant);

                                mask <<= 1;

                                tempNum = BarrettReduction(tempNum * tempNum, n, constant);


                                if(tempNum.dataLength == 1 && tempNum.data[0] == 1)
                                {
                                        if(thisNegative && (exp.data[0] & 0x1) != 0)    //odd exp
                                                return -resultNum;
                                        return resultNum;
                                }
                                count++;
                                if(count == totalBits)
                                        break;
                        }
                }

                if(thisNegative && (exp.data[0] & 0x1) != 0)    //odd exp
                        return -resultNum;

	        return resultNum;
        }

Usage Example

Ejemplo n.º 1
0
        public static bool Decrypt(ref byte[] buffer, int position, int length)
        {
            if (length - position != 128)
            {
                return(false);
            }

            byte[] temp = new byte[128];
            Array.Copy(buffer, position, temp, 0, 128);

            BigInteger input = new BigInteger(temp);
            BigInteger output;

            BigInteger m1 = input.modPow(otServerDP, otServerP);
            BigInteger m2 = input.modPow(otServerDQ, otServerQ);
            BigInteger h;

            if (m2 > m1)
            {
                h      = otServerP - ((m2 - m1) * otServerInverseQ % otServerP);
                output = m2 + otServerQ * h;
            }
            else
            {
                h = (m1 - m2) * otServerInverseQ % otServerP;

                output = m2 + otServerQ * h;
            }

            Array.Copy(GetPaddedValue(output), 0, buffer, position, 128);
            return(true);
        }
All Usage Examples Of BigInteger::modPow