public static Task <bool> IsPrime(this BigInteger source, IPrimalityTest primalityTest) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (primalityTest == null) { throw new ArgumentNullException(nameof(primalityTest)); } return(primalityTest.TestAsync(source)); }
public static async Task <BigInteger> GeneratePrime(uint bitLength, IRandomProvider randomProvider, IPrimalityTest primalityTest) { if (randomProvider == null) { throw new ArgumentNullException(nameof(randomProvider)); } if (bitLength < 8) { throw new ArgumentOutOfRangeException(nameof(bitLength), "Bit length must be at least of length 8."); } var bytes = bitLength / 8; var randomNumberBytes = new byte[bytes]; randomProvider.NextBytes(randomNumberBytes); var result = new BigInteger(randomNumberBytes); if (result.Sign < 0) { result = BigInteger.Negate(result); } if (result.IsEven) { result = result + 1; } while (!await primalityTest.TestAsync(result)) { result = result + 2; } return(result); }