Skip to content

Commit

Permalink
Can sort pubkeys prior to aggregation in musig
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier committed Dec 23, 2024
1 parent 9529a9a commit d114a5d
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions NBitcoin/Secp256k1/Musig/ECPubKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
/// <summary>
/// Aggregate the public keys into a single one
/// </summary>
/// <param name="pubkeys">The public keys to aggregate</param>
/// <param name="sort">If true, the pubkeys will be sorted before being aggregated</param>
/// <returns></returns>
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++)
Expand Down

0 comments on commit d114a5d

Please sign in to comment.