randomPrimeBigInt function

BigInt randomPrimeBigInt(
  1. int bits, {
  2. Random? random,
})

Implementation

BigInt randomPrimeBigInt(int bits, {Random? random}) {
  random ??= Random.secure();

  while (true) {
    var next = randomBigInt(bits, random: random);
    if (next.isEven) next = next | BigInt.one;

    while (next.bitLength == bits) {
      if (next.isPrime()) {
        return next;
      }
      next += BigInt.two;
    }
  }
}