Example #1
0
        //Тест Соловея-Штрассена. У тебя явно здесь проблемы.
        public static bool SolovayStrassenTest(BigInteger num, int secureParam)
        {
            bool isPrime = true;

            Parallel.For(0, secureParam, (i, pls) =>
            {
                BigInteger a = BIGenerator.GenerateBigInteger(num - 3) + 2;

                if (BigInteger.GreatestCommonDivisor(a, num) <= 1)
                {
                    BigInteger
                    mod    = BigInteger.ModPow(a, (num - 1) / 2, num),
                    jacobi = JacobiSymbol(a, num);

                    //a^((num - 1)/2) = jacobi(a, num) (mod num)

                    if (jacobi < 0)
                    {
                        jacobi += num;
                    }

                    if (mod == jacobi % num)
                    {
                        return;
                    }

                    isPrime = false;
                    pls.Break();
                }
            });

            return(isPrime);
        }
Example #2
0
        //Тест Миллера-Рабина.
        public static bool MillerRabinTest(BigInteger num, int secureParam)
        {
            // num = 2^s * t
            BigInteger t = num - 1;
            long       s = 0;

            while (t % 2 == 0)
            {
                t /= 2;
                s++;
            }

            bool isPrime = true;

            Parallel.For(0, secureParam, (i, pls) =>
            {
                BigInteger
                a = BIGenerator.GenerateBigInteger(num - 4) + 2,
                x = BigInteger.ModPow(a, t, num);

                //a = (2, num - 2)
                //x = a^t (mod num)

                if (x == 1 || x == num - 1)
                {
                    return;
                }

                for (long j = 1; j < s; j++)
                {
                    //x = x^2 (mod num)
                    x = BigInteger.ModPow(x, 2, num);

                    if (x == num - 1)
                    {
                        return;
                    }
                }

                isPrime = false;
                pls.Break();
            });

            return(isPrime);
        }
Example #3
0
        //Вероятностный тест Ферма, основанный на малой теореме Ферма.
        public static bool FermatTest(BigInteger num, int secureParam)
        {
            bool isPrime = true;

            Parallel.For(0, secureParam, (i, pls) =>
            {
                BigInteger a = BIGenerator.GenerateBigInteger(num - 4) + 2;

                //a^(num - 1) = 1 (mod num)
                if (BigInteger.ModPow(a, num - 1, num) == 1)
                {
                    return;
                }

                isPrime = false;
                pls.Break();
            });

            return(isPrime);
        }
Example #4
0
        public RSAEncoder()
        {
            p = BIGenerator.GeneratePrimeBigInteger(1024);
            q = BIGenerator.GeneratePrimeBigInteger(1024);

            n = p * q;
            BigInteger phi = (p - 1) * (q - 1);

            do
            {
                e = BIGenerator.GeneratePrimeBigInteger(1024);
            } while (BigInteger.GreatestCommonDivisor(phi, e) != 1 ||
                     e >= phi);

            d = e.ReverseElement(phi);

            if (d < 0)
            {
                d += phi;
            }
        }