private static void ZKPCracker() { System.Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); System.Console.Write("\nn: "); Integer n = Tools.ToInteger(Console.ReadLine()); Integer p; while (true) { var t = MathI.RandomI(); var y = t.ModPow(2, n); System.Console.WriteLine($"y: {y.ToHexString()}"); System.Console.Write("z: "); var z = Tools.ToInteger(Console.ReadLine()); if (y != z || y != -z) { p = NumberTheory.GCD(t + z, n); if (p != 1) { break; } } System.Console.WriteLine("___________________"); } System.Console.WriteLine("\n\nEnd of attack"); var q = n / p; System.Console.WriteLine($"p: {p.ToHexString()}"); System.Console.WriteLine($"q: {q.ToHexString()}"); System.Console.WriteLine($"Test n: {(p * q).ToHexString()}"); }
public Fraction(long numerator, long denominator) { long GreatestCommonDivisor = NumberTheory.GCD(numerator, denominator); m_numerator = numerator / GreatestCommonDivisor; m_denominator = denominator / GreatestCommonDivisor; }
public void GCDTest(long a, long b) { //action long gcd = NumberTheory.GCD(a, b); //assert (a % gcd).Should().Be(0L); (b % gcd).Should().Be(0L); }
private void f4_button_GetRandomCb_Click(object sender, EventArgs e) { BigInteger P = BigInteger.Parse(f4_textP.Text); RandomNumberGenerator rng = RandomNumberGenerator.Create(); BigInteger Db, Cb = BigIntegerRandomUtils.RandomInRange(rng, 2, P - 1); while (!(NumberTheory.GCD(Cb, P - 1, out BigInteger x, out BigInteger y) == 1)) { Cb = BigIntegerRandomUtils.RandomInRange(rng, 2, P - 1); } ; f4_textCb.Text = Convert.ToString(Cb); }
private void f9_button_GetRandomSecret_Click(object sender, EventArgs e) { RandomNumberGenerator rng = RandomNumberGenerator.Create(); BigInteger N = BigInteger.Parse(f9_textN.Text); BigInteger P = BigInteger.Parse(f9_textP.Text); BigInteger Q = BigInteger.Parse(f9_textQ.Text); BigInteger fi = (P - 1) * (Q - 1); // Функция Эйлера от N BigInteger SecretKey = BigIntegerRandomUtils.RandomInRange(rng, 2, N - 1); while (NumberTheory.GCD(SecretKey, fi, out BigInteger x, out BigInteger y) != 1) { SecretKey = BigIntegerRandomUtils.RandomInRange(rng, 2, N - 1); } f9_textSecretKey.Text = Convert.ToString(SecretKey); }
private void f4_button_SetCb_Click(object sender, EventArgs e) { f4_textCb.Text = new string(f4_textCb.Text.Where(t => char.IsDigit(t)).ToArray()); if (f4_textCb.TextLength > 0) { BigInteger P = BigInteger.Parse(f4_textP.Text); BigInteger Cb = BigInteger.Parse(f4_textCb.Text); if ((NumberTheory.GCD(Cb, P - 1, out BigInteger x, out BigInteger y) == 1)) { f4_textDb.Text = Convert.ToString(NumberTheory.Foo(Cb, P - 1)); f4_textCb.ReadOnly = true; f4_button_GetRandomCb.Enabled = false; f4_button_SetCb.Enabled = false; f4_button_ChangeCb.Enabled = true; } } }
private void f9_button_SetSecretKey_Click(object sender, EventArgs e) { f9_textSecretKey.Text = new string(f9_textSecretKey.Text.Where(t => char.IsDigit(t)).ToArray()); BigInteger SecretKey; BigInteger P = BigInteger.Parse(f9_textP.Text); BigInteger Q = BigInteger.Parse(f9_textQ.Text); BigInteger N = BigInteger.Parse(f9_textN.Text); BigInteger fi = (P - 1) * (Q - 1); // Функция Эйлера от N if (f9_textSecretKey.TextLength > 0) { SecretKey = BigInteger.Parse(f9_textSecretKey.Text); SecretKey = SecretKey % N; if (SecretKey > 1 && NumberTheory.GCD(SecretKey, fi, out BigInteger x, out BigInteger y) == 1) { f9_textOpenKey.Text = Convert.ToString(NumberTheory.Foo(SecretKey, fi)); f9_button_SetSecretKey.Enabled = false; f9_button_GetRandomSecret.Enabled = false; f9_button_ClearSecretKey.Enabled = true; f9_buttonSign.Enabled = true; } } }
static void Main(String[] args) { Console.WriteLine(NumberTheory.GCD(123, 12)); Console.ReadKey(); }
/// <summary> /// A.1.2.1.2 /// </summary> /// <param name="L"></param> /// <param name="N"></param> /// <param name="firstSeed"></param> /// <returns></returns> private PQGenerateResult Generate(int L, int N, BigInteger firstSeed) { // 1 if (!DSAHelper.VerifyLenPair(L, N)) { return(new PQGenerateResult("Bad L, N pair")); } // 2 var qResult = PrimeGen186_4.ShaweTaylorRandomPrime(N, firstSeed, _sha); if (!qResult.Success) { return(new PQGenerateResult("Failed to generate q from ShaweTaylor")); } var q = qResult.Prime; var qSeed = qResult.PrimeSeed; var qCounter = qResult.PrimeGenCounter; // 3 var pLen = L.CeilingDivide(2) + 1; var pResult = PrimeGen186_4.ShaweTaylorRandomPrime(pLen, qSeed, _sha); if (!pResult.Success) { return(new PQGenerateResult("Failed to generate p0 from ShaweTaylor")); } var p0 = pResult.Prime; var pSeed = pResult.PrimeSeed; var pCounter = pResult.PrimeGenCounter; // 4, 5 var outLen = _sha.HashFunction.OutputLen; var iterations = L.CeilingDivide(outLen) - 1; var oldCounter = pCounter; // 6, 7 BigInteger x = 0; for (var i = 0; i <= iterations; i++) { x += _sha.HashNumber(pSeed + i).ToBigInteger() * NumberTheory.Pow2(i * outLen); } // 8 pSeed += iterations + 1; // 9 x = NumberTheory.Pow2(L - 1) + (x % NumberTheory.Pow2(L - 1)); // 10 var t = x.CeilingDivide(2 * q * p0); do { // 11 if (2 * t * q * p0 + 1 > NumberTheory.Pow2(L)) { t = NumberTheory.Pow2(L - 1).CeilingDivide(2 * q * p0); } // 12, 13 var p = 2 * t * q * p0 + 1; pCounter++; // 14, 15 BigInteger a = 0; for (var i = 0; i <= iterations; i++) { a += _sha.HashNumber(pSeed + i).ToBigInteger() * NumberTheory.Pow2(i * outLen); } // 16 pSeed += iterations + 1; // 17 a = 2 + (a % (p - 3)); // 18 var z = BigInteger.ModPow(a, 2 * t * q, p); // 19 if (1 == NumberTheory.GCD(z - 1, p) && 1 == BigInteger.ModPow(z, p0, p)) { return(new PQGenerateResult(p, q, new DomainSeed(firstSeed, pSeed, qSeed), new Counter(pCounter, qCounter))); } // 20 if (pCounter > 4 * L + oldCounter) { return(new PQGenerateResult("Too many iterations")); } // 21 t++; // 22 } while (true); }
public override object Evaluate() { return((Complex)NumberTheory.GCD(Expressions.Select(x => x.EvaluateAsInt64()).ToArray())); }
private PrimeGeneratorResult GeneratePrimes(PrimeGeneratorParameters param) { // 1, 2, 3 performed by guards // 4, 4.1 var i = 0; BigInteger p = 0; var pqLowerBound = GetBound(param.Modulus); do { do { // 4.2 if (p != 0 && _kat) { return(new PrimeGeneratorResult("Given p less than sqrt(2) * 2 ^ (n/2) - 1, need to get a new random number.")); } p = _entropyProvider.GetEntropy(param.Modulus / 2).ToPositiveBigInteger(); // 4.3 if (_performAShift) { p += (param.A - p).PosMod(8); } else if (p.IsEven) { p++; } // 4.4 } while (p < pqLowerBound); // 4.5 if (NumberTheory.GCD(p - 1, param.PublicE) == 1) { if (PrimeGeneratorHelper.MillerRabin(_primeTestMode, param.Modulus, p, false)) { break; } } // 4.6, 4.7 i++; if (i >= _iBoundForP * (param.Modulus / 2)) { return(new PrimeGeneratorResult("Too many iterations for p")); } if (_kat) { return(new PrimeGeneratorResult("Given p is not prime")); } } while (!_kat); // 5, 5.1 i = 0; BigInteger q = 0; do { do { // 5.2 if (q != 0 && _kat) { return(new PrimeGeneratorResult("Given q less than sqrt(2) * 2 ^ (n/2) - 1, need to get a new random number.")); } q = _entropyProvider.GetEntropy(param.Modulus / 2).ToPositiveBigInteger(); // 5.3 if (_performBShift) { q += (param.B - q).PosMod(8); } else if (q.IsEven) { q++; } // 5.4 // 5.5 } while (BigInteger.Abs(p - q) <= NumberTheory.Pow2(param.Modulus / 2 - 100) || q < pqLowerBound); // 5.6 if (NumberTheory.GCD(q - 1, param.PublicE) == 1) { if (PrimeGeneratorHelper.MillerRabin(_primeTestMode, param.Modulus, q, false)) { break; } } // 5.7, 5.8 i++; if (i >= _iBoundForQ * (param.Modulus / 2)) { return(new PrimeGeneratorResult("Too many iterations for q")); } if (_kat) { return(new PrimeGeneratorResult("Given q is not prime")); } } while (!_kat); var auxValues = new AuxiliaryResult(); var primePair = new PrimePair { P = p, Q = q }; return(new PrimeGeneratorResult(primePair, auxValues)); }