using AngouriMath.Extensions;
using AngouriMath.Functions;
BigInteger n = BigInteger.Parse("115792089237316195423570985008687907852837564279074904382605163141518161494337");
for (int i = 0; i < 1000; i++)
privateKey = GenerateRandomBigInteger(256);
} while (privateKey <= 0 || privateKey >= n);
Console.WriteLine($"Private Key: {privateKey}");
Console.WriteLine("Clave Privada (hex): " + privateKey.ToString("X"));
var y = MathS.Log(2, privateKey);
Console.WriteLine($"Log Base 2: {y.EvalNumerical()}");
bool isPrime = IsProbablyPrime(privateKey, 20);
Console.WriteLine($"{privateKey} es primo: {isPrime}");
Console.WriteLine("--------------------------------------------------------------");
Console.WriteLine($"Total de claves privadas que son números primos: {primeCount}");
static BigInteger GenerateRandomBigInteger(int bitLength)
Random random = new Random();
byte[] bytes = new byte[(bitLength + 7) / 8];
bytes[bytes.Length - 1] &= (byte)(0xFF >> (bytes.Length * 8 - bitLength));
return new BigInteger(bytes);
static bool IsProbablyPrime(BigInteger n, int k)
if (n <= 1) return false;
if (n == 2 || n == 3) return true;
if (n % 2 == 0) return false;
Random rng = new Random();
for (int i = 0; i < k; i++)
BigInteger a = RandomBigInteger(2, n - 2, rng);
BigInteger x = BigInteger.ModPow(a, d, n);
if (x == 1 || x == n - 1)
bool continueOuterLoop = false;
for (int r = 1; r < s; r++)
x = BigInteger.ModPow(x, 2, n);
continueOuterLoop = true;
static BigInteger RandomBigInteger(BigInteger min, BigInteger max, Random rng)
byte[] bytes = max.ToByteArray();
bytes[bytes.Length - 1] &= 0x7F;
result = new BigInteger(bytes);
} while (result < min || result > max);