recoverPublicKey method

PublicKey recoverPublicKey(
  1. Uint8List message
)

Implementation

PublicKey recoverPublicKey(Uint8List message) {
  if (![0, 1, 2, 3].contains(recovery)) {
    // check recovery id
    throw Exception('recovery id invalid');
  }
  // Truncate hash
  final BigInt h =
      Utilities.bits2int_modN(Utilities.matchLength(message, 32));
  final BigInt radj = recovery == 2 || recovery == 3 ? r + N : r;
  // If rec was 2 or 3, q.x is bigger than n
  if (radj >= P) {
    // ensure q.x is still a field element
    throw Exception('q.x invalid');
  }
  // head is 0x02 or 0x03
  final String head = (recovery! & 1) == 0 ? '02' : '03';
  // concat head + hex repr of r
  final Point R = Point.fromHex(head + Utilities.bigIntToHex(radj));
  // r^-1
  final ir = Utilities.inverse(radj, N);
  // -hr^-1
  final u1 = Utilities.mod(-h * ir, N);
  // sr^-1
  final u2 = Utilities.mod(s * ir, N);
  // (sr^-1)R-(hr^-1)G = -(hr^-1)G + (sr^-1)
  return PublicKey.fromPoint(Point.G.mulAddQUns(R, u1, u2));
}