diff --git a/NBitcoin.Tests/Secp256k1Tests.cs b/NBitcoin.Tests/Secp256k1Tests.cs index c30223074..a44959e47 100644 --- a/NBitcoin.Tests/Secp256k1Tests.cs +++ b/NBitcoin.Tests/Secp256k1Tests.cs @@ -4114,9 +4114,7 @@ public void musig_tweak_vectors() private MusigPrivNonce ToMusigPrivNonce(string hex) { var b = Encoders.Hex.DecodeData(hex); - return new MusigPrivNonce(ECPrivKey.Create(b.AsSpan().Slice(0, 32)), - ECPrivKey.Create(b.AsSpan().Slice(32, 32)), - ECPubKey.Create(b.AsSpan().Slice(64, 33))); + return MusigPrivNonce.Load(b); } [Fact] diff --git a/NBitcoin/Secp256k1/Musig/MusigPrivNonce.cs b/NBitcoin/Secp256k1/Musig/MusigPrivNonce.cs index 43415d266..4a61fab98 100644 --- a/NBitcoin/Secp256k1/Musig/MusigPrivNonce.cs +++ b/NBitcoin/Secp256k1/Musig/MusigPrivNonce.cs @@ -17,7 +17,6 @@ class MusigPrivNonce : IDisposable { readonly static RandomNumberGenerator rand = RandomNumberGenerator.Create(); - /// /// This function derives a secret nonce that will be required for signing and /// creates a private nonce whose public part intended to be sent to other signers. @@ -175,6 +174,16 @@ internal MusigPrivNonce(ECPrivKey k1, ECPrivKey k2, ECPubKey pk) this.pk = pk; } + public static MusigPrivNonce Load(Span bytes) + { + if(bytes.Length != 97) + throw new FormatException("Invalid private nonce, expected 97 bytes"); + var k1 = ECPrivKey.Create(bytes[..32]); + var k2 = ECPrivKey.Create(bytes[32..64]); + var pk = ECPubKey.Create(bytes[64..]); + return new MusigPrivNonce(k1, k2, pk); + } + public void Dispose() { k1.Dispose();