input+lnrpc: make key sorting optional in input pkg

To allow us to properly test all test vectors, we can't default to true
on key sorting. Instead we add a parameter to the input package and move
the default value to the RPC server.
This commit is contained in:
Oliver Gugger
2023-01-27 16:13:14 +01:00
parent 13789f5d95
commit cb9f2d0398
3 changed files with 30 additions and 10 deletions

View File

@@ -167,7 +167,7 @@ func MuSig2ParsePubKeys(rawPubKeys [][]byte) ([]*btcec.PublicKey, error) {
// MuSig2CombineKeys combines the given set of public keys into a single // MuSig2CombineKeys combines the given set of public keys into a single
// combined MuSig2 combined public key, applying the given tweaks. // combined MuSig2 combined public key, applying the given tweaks.
func MuSig2CombineKeys(allSignerPubKeys []*btcec.PublicKey, func MuSig2CombineKeys(allSignerPubKeys []*btcec.PublicKey, sortKeys bool,
tweaks *MuSig2Tweaks) (*musig2.AggregateKey, error) { tweaks *MuSig2Tweaks) (*musig2.AggregateKey, error) {
// Convert the tweak options into the appropriate MuSig2 API functional // Convert the tweak options into the appropriate MuSig2 API functional
@@ -188,7 +188,7 @@ func MuSig2CombineKeys(allSignerPubKeys []*btcec.PublicKey,
// Then we'll use this information to compute the aggregated public key. // Then we'll use this information to compute the aggregated public key.
combinedKey, _, _, err := musig2.AggregateKeys( combinedKey, _, _, err := musig2.AggregateKeys(
allSignerPubKeys, true, keyAggOpts..., allSignerPubKeys, sortKeys, keyAggOpts...,
) )
return combinedKey, err return combinedKey, err
} }

View File

@@ -49,14 +49,16 @@ func TestMuSig2CombineKeys(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
keys []*btcec.PublicKey keys []*btcec.PublicKey
sortKeys bool
tweak *MuSig2Tweaks tweak *MuSig2Tweaks
expectedErr string expectedErr string
expectedFinalKey string expectedFinalKey string
expectedPreTweakKey string expectedPreTweakKey string
}{{ }{{
name: "v0.4.0 two dummy keys BIP86", name: "v0.4.0 two dummy keys BIP86",
keys: []*btcec.PublicKey{dummyPubKey1, dummyPubKey2}, keys: []*btcec.PublicKey{dummyPubKey1, dummyPubKey2},
tweak: bip86Tweak, sortKeys: true,
tweak: bip86Tweak,
expectedFinalKey: "03b54fb320a8fc3589e86a1559c6aaa774fbab4e4d" + expectedFinalKey: "03b54fb320a8fc3589e86a1559c6aaa774fbab4e4d" +
"9fbf31e2fd836b661ac6a132", "9fbf31e2fd836b661ac6a132",
expectedPreTweakKey: "0279c76a15dcf6786058a571e4022b78633e1bf" + expectedPreTweakKey: "0279c76a15dcf6786058a571e4022b78633e1bf" +
@@ -66,7 +68,8 @@ func TestMuSig2CombineKeys(t *testing.T) {
keys: []*btcec.PublicKey{ keys: []*btcec.PublicKey{
dummyPubKey1, dummyPubKey2, dummyPubKey3, dummyPubKey1, dummyPubKey2, dummyPubKey3,
}, },
tweak: bip86Tweak, sortKeys: true,
tweak: bip86Tweak,
expectedFinalKey: "03fa8195d584b195476f20e2fe978fd7312f4b08f2" + expectedFinalKey: "03fa8195d584b195476f20e2fe978fd7312f4b08f2" +
"777f080bcdfc9350603cd6e7", "777f080bcdfc9350603cd6e7",
expectedPreTweakKey: "03e615b8aad4ed10544537bc48b1d6600e15773" + expectedPreTweakKey: "03e615b8aad4ed10544537bc48b1d6600e15773" +
@@ -76,7 +79,8 @@ func TestMuSig2CombineKeys(t *testing.T) {
keys: []*btcec.PublicKey{ keys: []*btcec.PublicKey{
testVector040Key1, testVector040Key2, testVector040Key3, testVector040Key1, testVector040Key2, testVector040Key3,
}, },
tweak: bip86Tweak, sortKeys: true,
tweak: bip86Tweak,
expectedFinalKey: "025b257b4e785d61157ef5303051f45184bd5cb47b" + expectedFinalKey: "025b257b4e785d61157ef5303051f45184bd5cb47b" +
"c4b4069ed4dd4536459cb83b", "c4b4069ed4dd4536459cb83b",
expectedPreTweakKey: "02d70cd69a2647f7390973df48cbfa2ccc407b8" + expectedPreTweakKey: "02d70cd69a2647f7390973df48cbfa2ccc407b8" +
@@ -86,11 +90,23 @@ func TestMuSig2CombineKeys(t *testing.T) {
keys: []*btcec.PublicKey{ keys: []*btcec.PublicKey{
testVector040Key3, testVector040Key2, testVector040Key1, testVector040Key3, testVector040Key2, testVector040Key1,
}, },
tweak: bip86Tweak, sortKeys: true,
tweak: bip86Tweak,
expectedFinalKey: "025b257b4e785d61157ef5303051f45184bd5cb47b" + expectedFinalKey: "025b257b4e785d61157ef5303051f45184bd5cb47b" +
"c4b4069ed4dd4536459cb83b", "c4b4069ed4dd4536459cb83b",
expectedPreTweakKey: "02d70cd69a2647f7390973df48cbfa2ccc407b8" + expectedPreTweakKey: "02d70cd69a2647f7390973df48cbfa2ccc407b8" +
"b2d60b08c5f1641185c7998a290", "b2d60b08c5f1641185c7998a290",
}, {
name: "v0.4.0 three test vector keys BIP86 no sort",
keys: []*btcec.PublicKey{
testVector040Key1, testVector040Key2, testVector040Key3,
},
sortKeys: false,
tweak: bip86Tweak,
expectedFinalKey: "0223e0c640e96000e8e92699ec3802e5c39edf47db" +
"dfdb788b3735b76a55538179",
expectedPreTweakKey: "03e5830140512195d74c8307e39637cbe5fb730" +
"ebeab80ec514cf88a877ceeee0b",
}} }}
for _, tc := range testCases { for _, tc := range testCases {
@@ -98,7 +114,9 @@ func TestMuSig2CombineKeys(t *testing.T) {
t.Run(tc.name, func(tt *testing.T) { t.Run(tc.name, func(tt *testing.T) {
tt.Parallel() tt.Parallel()
res, err := MuSig2CombineKeys(tc.keys, tc.tweak) res, err := MuSig2CombineKeys(
tc.keys, tc.sortKeys, tc.tweak,
)
if tc.expectedErr != "" { if tc.expectedErr != "" {
require.ErrorContains(tt, err, tc.expectedErr) require.ErrorContains(tt, err, tc.expectedErr)

View File

@@ -840,7 +840,9 @@ func (s *Server) MuSig2CombineKeys(_ context.Context,
} }
// Combine the keys now without creating a session in memory. // Combine the keys now without creating a session in memory.
combinedKey, err := input.MuSig2CombineKeys(allSignerPubKeys, tweaks) combinedKey, err := input.MuSig2CombineKeys(
allSignerPubKeys, true, tweaks,
)
if err != nil { if err != nil {
return nil, fmt.Errorf("error combining keys: %v", err) return nil, fmt.Errorf("error combining keys: %v", err)
} }