recoverPublicKeyFromSignature static method

Uint8List? recoverPublicKeyFromSignature(
  1. int recId,
  2. BigInt r,
  3. BigInt s,
  4. Uint8List message,
)

Implementation

static Uint8List? recoverPublicKeyFromSignature(
  int recId,
  BigInt r,
  BigInt s,
  Uint8List message,
) {
  final n = _params.n;
  final i = BigInt.from(recId ~/ 2);
  final x = r + (i * n);

  //Parameter q of curve
  final prime = BigInt.parse(
      'fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f',
      radix: 16);
  if (x.compareTo(prime) >= 0) return null;

  final R = _decompressKey(x, (recId & 1) == 1, _params.curve);
  final ECPoint? ecPoint = R * n;
  if (ecPoint == null || !ecPoint.isInfinity) return null;

  // print(bytesToHex(message));
  // final e = BigInt.parse(bytesToHex(message).substring(1));
  final e = decodeBigInt(message.toList());

  final eInv = (BigInt.zero - e) % n;
  final rInv = r.modInverse(n);
  final srInv = (rInv * s) % n;
  final eInvrInv = (rInv * eInv) % n;

  final preQ = (_params.G * eInvrInv);
  if (preQ == null) return null;
  final q = preQ + (R * srInv);

  final bytes = q?.getEncoded(false);
  return bytes?.sublist(1);
}