signSchnorr method

Uint8List signSchnorr({
  1. required Uint8List message,
  2. String aux = '',
})

Implementation

Uint8List signSchnorr({required Uint8List message, String aux = ''}) {
  final d0 = BigInt.parse(HEX.encode(privateKey!), radix: 16);
  if ((d0 < BigInt.one) || (d0 > (secp256k1.n - BigInt.one))) throw Exception('Private key is invalid.');

  final bAux = HEX.decode(aux.padLeft(64, '0'));
  if (bAux.length != 32) throw Exception('Aux is invalid.');

  final P = (secp256k1.G * d0)!;
  final d = (P.y!.toBigInteger()! % BigInt.two == BigInt.zero) ? d0 : secp256k1.n - d0;
  final t = d ^ bigFromBytes(taggedHash('BIP0340/aux', bAux));
  final k0 = bigFromBytes(taggedHash('BIP0340/nonce', bigToBytes(t) + bigToBytes(P.x!.toBigInteger()!) + message)) % secp256k1.n;

  if (k0.sign == 0) throw Exception('Message is invalid.');

  final R = (secp256k1.G * k0)!;

  final k = (R.y!.toBigInteger()! % BigInt.two == BigInt.zero) ? k0 : secp256k1.n - k0;
  final rX = bigToBytes(R.x!.toBigInteger()!);
  final e = getE(P, rX, message);

  final signature = rX + bigToBytes((k + e * d) % secp256k1.n);
  return Uint8List.fromList(signature);
}