Add tests of P2TR BIP322
MODIFY
WalletWasabi.Tests/UnitTests/Crypto/Bip322SignatureTest.cs
WalletWasabi.Tests/UnitTests/Crypto/Bip322SignatureTest.cs
@@ -65,4 +65,53 @@ public class Bip322SignatureTest
invalidBip322Signature = new Bip322Signature(bip322Signature.ScriptSig, new WitScript(new List<byte[]> { signature, invalidPubKey }));
Assert.False(invalidBip322Signature.Verify(message, scriptPubKey));
}
+
+ [Fact]
+ public void P2trBip322SignatureEncodingDecoding()
+ {
+ // [all all all all all all all all all all all all]/86'/0'/0'/1/0
+ using var key = new Key(Encoders.Hex.DecodeData("E4E13C6ACF002C94D5EE25E89F419531A52A39F93B00B71349C4889E046BBA3C"));
+ var message = new uint256(Encoders.Hex.DecodeData("BB06B6D65C9F4A1D245F203EB039A735D94D3ECA3E19B04DA59956413F5D5C8F"), false);
+ var bip322Signature = Bip322Signature.Generate(key, message, ScriptPubKeyType.TaprootBIP86);
+
+ var bip322SignatureBytes = Encoders.Hex.DecodeData("000140671520C782E52D070309579BA1FEE3889EB32D2E812A32B4899193233E5856E824014C17F68AB354BC4964AABBC957FE33AC557E2B3386923F3ED7A019DCC0C6");
+
+ Assert.True(bip322SignatureBytes.SequenceEqual(bip322Signature.ToBytes()));
+ var bip322SignatureDeserialized = Bip322Signature.FromBytes(bip322SignatureBytes);
+ Assert.True(bip322SignatureBytes.SequenceEqual(bip322SignatureDeserialized.ToBytes()));
+ }
+
+ [Fact]
+ public void P2trBip322SignatureVerification()
+ {
+ // [all all all all all all all all all all all all]/86'/0'/0'/1/0
+ using var key = new Key(Encoders.Hex.DecodeData("E4E13C6ACF002C94D5EE25E89F419531A52A39F93B00B71349C4889E046BBA3C"));
+ var message = new uint256(Encoders.Hex.DecodeData("BB06B6D65C9F4A1D245F203EB039A735D94D3ECA3E19B04DA59956413F5D5C8F"), false);
+
+ var bip322Signature = Bip322Signature.Generate(key, message, ScriptPubKeyType.TaprootBIP86);
+ var signature = bip322Signature.Witness[0];
+ var scriptPubKey = key.PubKey.GetScriptPubKey(ScriptPubKeyType.TaprootBIP86);
+
+ // Valid signature
+ Assert.True(bip322Signature.Verify(message, scriptPubKey));
+
+ byte[] invalidSignature;
+ Bip322Signature invalidBip322Signature;
+
+ // Invalid signature, modified scriptsig
+ invalidBip322Signature = new Bip322Signature(new Script(new byte[] { 0x00 }), bip322Signature.Witness);
+ Assert.False(invalidBip322Signature.Verify(message, scriptPubKey));
+
+ // Invalid signature, modified length field of signature in witness
+ invalidSignature = signature.ToArray();
+ invalidSignature[2] = 30;
+ invalidBip322Signature = new Bip322Signature(bip322Signature.ScriptSig, new WitScript(new List<byte[]> { invalidSignature }));
+ Assert.False(invalidBip322Signature.Verify(message, scriptPubKey));
+
+ // Invalid signature, Modified r part of signature in witness
+ invalidSignature = signature.ToArray();
+ invalidSignature[4] ^= 1;
+ invalidBip322Signature = new Bip322Signature(bip322Signature.ScriptSig, new WitScript(new List<byte[]> { invalidSignature }));
+ Assert.False(invalidBip322Signature.Verify(message, scriptPubKey));
+ }
}

Add tests of P2TR BIP322