public Signature SignatureMessage(BigInteger M) { BigInteger k; do { k = BIGenerator.GenerateBigInteger(q - 2) + 1; } while (BigInteger.GreatestCommonDivisor(k, p - 1) != 1); BigInteger r = BigInteger.ModPow(g, k, p); if (r < 0) { r += p; } BigInteger m = Hash(M, p); BigInteger s = (m - x * r) * k.ReverseElement(p - 1); s %= p - 1; if (s < 0) { s += p - 1; } return(new Signature(r, s)); }
//Тест Соловея-Штрассена. У тебя явно здесь проблемы. 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 EGSAEncoder() { BigInteger k; p = BIGenerator.GeneratePrimeBigInteger(1024, out q, out k); do { BigInteger h = BIGenerator.GenerateBigInteger(p - 3) + 1; g = BigInteger.ModPow(h, k, p); } while (g == 1); //g = p.Generator(new List<BigInteger>() { 2, q }, k / 2); x = BIGenerator.GenerateBigInteger(p - 2) + 1; y = BigInteger.ModPow(g, x, p); Key = new PublicKey(p, q, g, y); }
//Вероятностный тест Ферма, основанный на малой теореме Ферма. 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); }