Skip to content

Commit

Permalink
Add tweak_vectors musig test
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier committed Oct 4, 2022
1 parent fe7a177 commit 602dea5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
35 changes: 35 additions & 0 deletions NBitcoin.Tests/Secp256k1Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4033,6 +4033,41 @@ public void musig_det_sign_vectors()
}
}

[Fact]
[Trait("UnitTest", "UnitTest")]
public void musig_tweak_vectors()
{
var root = JObject.Parse(File.ReadAllText("data/musig/tweak_vectors.json"));
var sk = new ECPrivKey(Encoders.Hex.DecodeData(root["sk"].Value<string>()), null);
var pubkeys = GetArray<string>(root["pubkeys"]).ToArray();
var secnonce = root["secnonce"].Value<string>();
var pnonces = GetArray<string>(root["pnonces"]).ToArray();
var ptweaks = GetArray<string>(root["tweaks"]).ToArray();
var msg = Encoders.Hex.DecodeData(root["msg"].Value<string>());
foreach (var item in (JArray)root["valid_test_cases"])
{
var keys = GetArray<int>(item["key_indices"]).Select(p => ECPubKey.Create(Encoders.Hex.DecodeData(pubkeys[p]))).ToArray();
var nonces = GetArray<int>(item["nonce_indices"]).Select(p => new MusigPubNonce(Encoders.Hex.DecodeData(pnonces[p]))).ToArray();
var tweaks = GetArray<int>(item["tweak_indices"]).Select(p => Encoders.Hex.DecodeData(ptweaks[p])).ToArray();
var is_xonly = GetArray<bool>(item["is_xonly"]);

var ctx = new MusigContext(keys, msg);
for (int i = 0; i < tweaks.Length; i++)
{
ctx.Tweak(tweaks[i], is_xonly[i]);
}
ctx.ProcessNonces(nonces);
var result = ctx.Sign(sk, ToMusigPrivNonce(secnonce));
AssertEx.EqualBytes(item["expected"].Value<string>(), result.ToBytes());
}
}

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)));
}

[Fact]
[Trait("UnitTest", "UnitTest")]
Expand Down
11 changes: 10 additions & 1 deletion NBitcoin.Tests/data/musig/tweak_vectors.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,23 @@
"expected": "408A0A21C4A0F5DACAF9646AD6EB6FECD7F7A11F03ED1F48DFFF2185BC2C2408",
"comment": "A plain tweak followed by an x-only tweak"
},
{
"key_indices": [1, 2, 0],
"nonce_indices": [1, 2, 0],
"tweak_indices": [0, 1, 2, 3],
"is_xonly": [false, false, true, true],
"signer_index": 2,
"expected": "45ABD206E61E3DF2EC9E264A6FEC8292141A633C28586388235541F9ADE75435",
"comment": "Four tweaks: plain, plain, x-only, x-only."
},
{
"key_indices": [1, 2, 0],
"nonce_indices": [1, 2, 0],
"tweak_indices": [0, 1, 2, 3],
"is_xonly": [true, false, true, false],
"signer_index": 2,
"expected": "B255FDCAC27B40C7CE7848E2D3B7BF5EA0ED756DA81565AC804CCCA3E1D5D239",
"comment": "Four tweaks: x-only, plain, x-only, plain"
"comment": "Four tweaks: x-only, plain, x-only, plain. If an implementation prohibits applying plain tweaks after x-only tweaks, it can skip this test vector or return an error."
}
],
"error_test_cases": [
Expand Down

0 comments on commit 602dea5

Please sign in to comment.