generatePrivateKey method

  1. @override
PrivateKey generatePrivateKey()
override

Implementation

@override
PrivateKey generatePrivateKey() {
  var random = Random.secure();
  var byteLen = (bitSize + 7) >> 3;
  var _p = AffinePoint();
  BigInt D;
  late List<int> rand;
  while (_p.X == BigInt.zero) {
    rand = List<int>.generate(byteLen, (i) => random.nextInt(256));
    // We have to mask off any excess bits in the case that the size of the
    // underlying field is not a whole number of bytes.
    rand[0] &= mask[bitSize % 8];
    // This is because, in tests, rand will return all zeros and we don't
    // want to get the point at infinity and loop forever.
    rand[1] ^= 0x42;
    D = BigInt.parse(
        List<String>.generate(byteLen, (i) => rand[i].toRadixString(16))
            .join(),
        radix: 16);

    // If the scalar is out of range, sample another random number.
    if (D >= n) {
      continue;
    }

    _p = scalarBaseMul(rand);
  }

  return PrivateKey.fromBytes(this, rand);
}