nonceAgg method

  1. @override
List<int> nonceAgg(
  1. List<List<int>> pubnonces
)
override

Aggregates public nonces for MuSig2

Implementation

@override
List<int> nonceAgg(List<List<int>> pubnonces) {
  if (pubnonces.length < MuSig2Constants.minimumRequiredKey) {
    throw ArgumentException.invalidOperationArguments(
      "nonceAgg",
      name: "pubnonces",
      reason: "Invalid public nonce length.",
    );
  }
  for (final i in pubnonces) {
    if (i.length != MuSig2Constants.pubnonceLength) {
      throw ArgumentException.invalidOperationArguments(
        "nonceAgg",
        name: "pubnonces",
        reason: "Invalid public nonce length.",
        expecteLen: MuSig2Constants.pubnonceLength,
      );
    }
  }
  final nonce = DynamicByteTracker();
  for (int i = 1; i < 3; i++) {
    BaseProjectivePointNative? rJ;
    for (final n in pubnonces) {
      final offset = (i - 1) * EcdsaKeysConst.pubKeyCompressedByteLen;
      final key = MuSig2Utils.encodePointAsEven(
        n.sublist(offset, offset + EcdsaKeysConst.pubKeyCompressedByteLen),
      );
      if (rJ != null) {
        rJ = (rJ + key);
      } else {
        rJ = key;
      }
    }
    if (rJ!.isZero()) {
      nonce.add(MuSig2Utils.zeroPk());
    } else {
      nonce.add(rJ.toBytes());
    }
  }

  return nonce.toBytes();
}