nextBigInteger method

BigInt nextBigInteger(
  1. int bitLength
)

Implementation

BigInt nextBigInteger(int bitLength) {
  final int fullBytes = bitLength ~/ 8;
  // Generate a number from the full bytes.
  // Then, prepend a smaller number covering the remaining bits.
  final BigInt main = bytesToInt(nextBytes(fullBytes));
  // Forcing remaining bits to be calculate with bits length.
  final int remainingBits = bitLength - main.bitLength;
  final int additional = remainingBits < 4
      ? dartRandom.nextInt(math.pow(2, remainingBits).toInt())
      : remainingBits;
  final BigInt additionalBit = BigInt.from(additional) << (fullBytes * 8);
  final BigInt result = main + additionalBit;
  return result;
}