generateTweek function

Uint8List? generateTweek(
  1. Uint8List point,
  2. Uint8List tweak
)

This function generates a tweaked private key from a private key and a tweak. It performs validation checks and returns the tweaked private key as a Uint8List. If the inputs are invalid or the resulting tweaked private key is invalid, it returns null.

Implementation

Uint8List? generateTweek(Uint8List point, Uint8List tweak) {
  /// Check if 'point' is a valid private key; if not, raise an error.
  if (!isPrivate(point)) {
    throw ArgumentError("Bad Private");
  }

  /// Check if 'tweak' is a valid scalar; if not, raise an error.
  if (!isOrderScalar(tweak)) {
    throw ArgumentError("Bad Tweek");
  }

  /// Decode 'point' and 'tweak' into BigInt values.
  BigInt dd = decodeBigInt(point);
  BigInt tt = decodeBigInt(tweak);

  /// Calculate the new private key by adding 'dd' and 'tt', and take the result modulo 'n'.
  BigInt newPrivateKey = (dd + tt) % n;

  /// Encode the new private key as a Uint8List.
  Uint8List dt = encodeBigInt(newPrivateKey);

  /// Ensure that the resulting 'dt' has a length of at least 32 bytes.
  if (dt.length < 32) {
    Uint8List padLeadingZero = Uint8List(32 - dt.length);
    dt = Uint8List.fromList(padLeadingZero + dt);
  }

  /// Check if the resulting 'dt' is a valid private key; if not, return null.
  if (!isPrivate(dt)) {
    return null;
  }

  /// Return the valid tweaked private key as a Uint8List.
  return dt;
}