//Тест Соловея-Штрассена. У тебя явно здесь проблемы. 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); }
//Тест Миллера-Рабина. 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); }
//Вероятностный тест Ферма, основанный на малой теореме Ферма. 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); }