Example #1
0
        private static BigInteger[] LucasSequenceHelper(BigInteger P, BigInteger Q, BigInteger k, BigInteger n, BigInteger constant, int s)
        {
            BigInteger[] result = new BigInteger[3];

            if ((k._data[0] & 0x00000001) == 0)
                throw (new ArgumentException("Argument k must be odd."));

            int numbits = k.BitCount();
            uint mask = (uint)0x1 << ((numbits & 0x1F) - 1);

            BigInteger v = 2 % n, Q_k = 1 % n,
                       v1 = P % n, u1 = Q_k;
            bool flag = true;

            for (int i = k.DataLength - 1; i >= 0; i--)
            {
                while (mask != 0)
                {
                    if (i == 0 && mask == 0x00000001)
                        break;

                    if ((k._data[i] & mask) != 0)
                    {
                        u1 = (u1 * v1) % n;

                        v = ((v * v1) - (P * Q_k)) % n;
                        v1 = n.BarrettReduction(v1 * v1, n, constant);
                        v1 = (v1 - ((Q_k * Q) << 1)) % n;

                        if (flag)
                            flag = false;
                        else
                            Q_k = n.BarrettReduction(Q_k * Q_k, n, constant);

                        Q_k = (Q_k * Q) % n;
                    }
                    else
                    {
                        u1 = ((u1 * v) - Q_k) % n;

                        v1 = ((v * v1) - (P * Q_k)) % n;
                        v = n.BarrettReduction(v * v, n, constant);
                        v = (v - (Q_k << 1)) % n;

                        if (flag)
                        {
                            Q_k = Q % n;
                            flag = false;
                        }
                        else
                            Q_k = n.BarrettReduction(Q_k * Q_k, n, constant);
                    }

                    mask >>= 1;
                }
                mask = 0x80000000;
            }

            u1 = ((u1 * v) - Q_k) % n;
            v = ((v * v1) - (P * Q_k)) % n;
            if (flag)
                flag = false;
            else
                Q_k = n.BarrettReduction(Q_k * Q_k, n, constant);

            Q_k = (Q_k * Q) % n;


            for (int i = 0; i < s; i++)
            {
                u1 = (u1 * v) % n;
                v = ((v * v) - (Q_k << 1)) % n;

                if (flag)
                {
                    Q_k = Q % n;
                    flag = false;
                }
                else
                    Q_k = n.BarrettReduction(Q_k * Q_k, n, constant);
            }

            result[0] = u1;
            result[1] = v;
            result[2] = Q_k;

            return result;
        }
Example #2
0
        /// <summary>
        /// Modulo Exponentiation.
        /// </summary>
        /// <param name="exponent"></param>
        /// <param name="modulus"></param>
        /// <returns></returns>
        public BigInteger ModPow(BigInteger exponent, BigInteger modulus)
        {
            if ((exponent._data[MAX_LENGTH - 1] & 0x80000000) != 0)
                throw (new ArithmeticException("Positive exponents only."));

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

            if ((_data[MAX_LENGTH - 1] & 0x80000000) != 0)
            {
                tempNum = -this % modulus;
                thisNegative = true;
            }
            else
                tempNum = this % modulus;

            if ((modulus._data[MAX_LENGTH - 1] & 0x80000000) != 0)
                modulus = -modulus;

            BigInteger constant = new BigInteger();

            int i = modulus.DataLength << 1;
            constant._data[i] = 0x00000001;
            constant.DataLength = i + 1;

            constant = constant / modulus;
            int totalBits = exponent.BitCount();
            int count = 0;

            for (int pos = 0; pos < exponent.DataLength; pos++)
            {
                uint mask = 0x01;

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

                    mask <<= 1;

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


                    if (tempNum.DataLength == 1 && tempNum._data[0] == 1)
                    {
                        if (thisNegative && (exponent._data[0] & 0x1) != 0)
                            return -resultNum;
                        return resultNum;
                    }
                    count++;
                    if (count == totalBits)
                        break;
                }
            }

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

            return resultNum;
        }
Example #3
0
        public RsaKey(BigInteger e, BigInteger n, BigInteger d, BigInteger p,
            BigInteger q, BigInteger dmp1, BigInteger dmq1, BigInteger iqmp)
        {
            E = e;
            N = n;
            D = d;
            P = p;
            Q = q;
            Dmp1 = dmp1;
            Dmq1 = dmq1;
            Iqmp = iqmp;

            CanEncrypt = (e != null && n != null);
            CanDecrypt = (CanEncrypt && d != null);

            BlockSize = (N.BitCount() + 7) / 8;
        }