calculatePrivateTweek static method

List<int> calculatePrivateTweek(
  1. List<int> secret,
  2. List<int> tapTweakHash
)

Calculates a private tweak for a given secret and tweak value.

The tweak is applied to the negation of the secret key, and the result is adjusted based on the parity of the corresponding public key.

Implementation

static List<int> calculatePrivateTweek(
    List<int> secret, List<int> tapTweakHash) {
  if (tapTweakHash.length != 32) {
    throw const CryptoSignException(
        "The tap tweak hash must be 32-byte array.");
  }
  final tweakBig = BigintUtils.fromBytes(tapTweakHash);
  BigInt negatedKey = BigintUtils.fromBytes(secret);
  final publicBytes =
      (generator * negatedKey).toBytes(EncodeType.uncompressed);
  final toBigInt = BigintUtils.fromBytes(publicBytes.sublist(33));
  if (toBigInt.isOdd) {
    negatedKey = order - negatedKey;
  }
  final tw = (negatedKey + tweakBig) % order;
  return BigintUtils.toBytes(tw, length: baselen);
}