From d114a5dc521fccc6a87b963fc41194eca6c68feb Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Mon, 23 Dec 2024 12:00:43 +0900 Subject: [PATCH] Can sort pubkeys prior to aggregation in musig --- NBitcoin/Secp256k1/Musig/ECPubKey.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/NBitcoin/Secp256k1/Musig/ECPubKey.cs b/NBitcoin/Secp256k1/Musig/ECPubKey.cs index 882262924b..3ce0b6e1b9 100644 --- a/NBitcoin/Secp256k1/Musig/ECPubKey.cs +++ b/NBitcoin/Secp256k1/Musig/ECPubKey.cs @@ -74,17 +74,28 @@ internal static Scalar secp256k1_musig_keyaggcoef(MusigContext pre_session, ECPu const string MusigTag = "KeyAgg coefficient"; - public static ECPubKey MusigAggregate(ECPubKey[] pubkeys) + /// + /// Aggregate the public keys into a single one + /// + /// The public keys to aggregate + /// If true, the pubkeys will be sorted before being aggregated + /// + public static ECPubKey MusigAggregate(ECPubKey[] pubkeys, bool sort = false) { - return MusigAggregate(pubkeys, null); + return MusigAggregate(pubkeys, null, sort); } - internal static ECPubKey MusigAggregate(ECPubKey[] pubkeys, MusigContext? preSession) + internal static ECPubKey MusigAggregate(ECPubKey[] pubkeys, MusigContext? preSession, bool sort) { if (pubkeys == null) throw new ArgumentNullException(nameof(pubkeys)); if (pubkeys.Length is 0) throw new ArgumentNullException(nameof(pubkeys), "At least one pubkey should be passed"); + if (sort) + { + pubkeys = pubkeys.ToArray(); + Array.Sort(pubkeys); + } /* No point on the curve has an X coordinate equal to 0 */ var second_pk_x = FE.Zero; for (int i = 1; i < pubkeys.Length; i++)