multi: add version to MuSig2 API, bump btcd/btcec to v2.3.2

With this commit we bump the github.com/btcd/btcec/v2 library to v2.3.2
which implements the MuSig2 BIP version v1.0.0rc2. With this the
github.com/btcsuite/btcd/btcec/v2/schnorr/musig2 package becomes
v1.0.0rc2 and the github.com/lightningnetwork/lnd/internal/musig2v040
stays at the old v0.4.0 version.
This commit is contained in:
Oliver Gugger committed 2023-02-03 18:30:10 +01:00
1 parent 0e5ce71b33
commit ce5fa2e043
10 files changed
+323 -39

No files matched your search

+219 -11
View File
@@ -8,9 +8,26 @@ import (
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/btcec/v2/schnorr/musig2"
"github.com/lightningnetwork/lnd/internal/musig2v040"
"github.com/lightningnetwork/lnd/keychain"
)
// MuSig2Version is a type that defines the different versions of the MuSig2
// as defined in the BIP draft:
// (https://github.com/jonasnick/bips/blob/musig2/bip-musig2.mediawiki)
type MuSig2Version uint8
const (
// MuSig2Version040 is version 0.4.0 of the MuSig2 BIP draft. This will
// use the lnd internal/musig2v040 package.
MuSig2Version040 MuSig2Version = 0
// MuSig2Version100RC2 is version 1.0.0rc2 of the MuSig2 BIP draft. This
// uses the github.com/btcsuite/btcd/btcec/v2/schnorr/musig2 package
// at git tag `btcec/v2.3.1`.
MuSig2Version100RC2 MuSig2Version = 1
)
const (
// MuSig2PartialSigSize is the size of a MuSig2 partial signature.
// Because a partial signature is just the s value, this corresponds to
@@ -31,9 +48,9 @@ type MuSig2Signer interface {
// public key of the local signing key. If nonces of other parties are
// already known, they can be submitted as well to reduce the number of
// method calls necessary later on.
MuSig2CreateSession(keychain.KeyLocator, []*btcec.PublicKey,
*MuSig2Tweaks, [][musig2.PubNonceSize]byte) (*MuSig2SessionInfo,
error)
MuSig2CreateSession(MuSig2Version, keychain.KeyLocator,
[]*btcec.PublicKey, *MuSig2Tweaks,
[][musig2.PubNonceSize]byte) (*MuSig2SessionInfo, error)
// MuSig2RegisterNonces registers one or more public nonces of other
// signing participants for a session identified by its ID. This method
@@ -117,6 +134,10 @@ type MuSig2SessionInfo struct {
// is the hash over the combined public key and the local public nonces.
SessionID [32]byte
// Version is the version of the MuSig2 BIP this signing session is
// using.
Version MuSig2Version
// PublicNonce contains the public nonce of the local signer session.
PublicNonce [musig2.PubNonceSize]byte
@@ -192,21 +213,71 @@ func (t *MuSig2Tweaks) ToContextOptions() []musig2.ContextOption {
return tweakOpts
}
// ToV040ContextOptions converts the tweak descriptor to v0.4.0 context options.
func (t *MuSig2Tweaks) ToV040ContextOptions() []musig2v040.ContextOption {
var tweakOpts []musig2v040.ContextOption
if len(t.GenericTweaks) > 0 {
genericTweaksCopy := make(
[]musig2v040.KeyTweakDesc, len(t.GenericTweaks),
)
for idx := range t.GenericTweaks {
genericTweaksCopy[idx] = musig2v040.KeyTweakDesc{
Tweak: t.GenericTweaks[idx].Tweak,
IsXOnly: t.GenericTweaks[idx].IsXOnly,
}
}
tweakOpts = append(tweakOpts, musig2v040.WithTweakedContext(
genericTweaksCopy...,
))
}
// The BIP0086 tweak and the taproot script tweak are mutually
// exclusive.
if t.TaprootBIP0086Tweak {
tweakOpts = append(tweakOpts, musig2v040.WithBip86TweakCtx())
} else if len(t.TaprootTweak) > 0 {
tweakOpts = append(tweakOpts, musig2v040.WithTaprootTweakCtx(
t.TaprootTweak,
))
}
return tweakOpts
}
// MuSig2ParsePubKeys parses a list of raw public keys as the signing keys of a
// MuSig2 signing session.
func MuSig2ParsePubKeys(rawPubKeys [][]byte) ([]*btcec.PublicKey, error) {
func MuSig2ParsePubKeys(bipVersion MuSig2Version,
rawPubKeys [][]byte) ([]*btcec.PublicKey, error) {
allSignerPubKeys := make([]*btcec.PublicKey, len(rawPubKeys))
if len(rawPubKeys) < 2 {
return nil, fmt.Errorf("need at least two signing public keys")
}
for idx, pubKeyBytes := range rawPubKeys {
pubKey, err := schnorr.ParsePubKey(pubKeyBytes)
if err != nil {
return nil, fmt.Errorf("error parsing signer public "+
"key %d: %v", idx, err)
switch bipVersion {
case MuSig2Version040:
pubKey, err := schnorr.ParsePubKey(pubKeyBytes)
if err != nil {
return nil, fmt.Errorf("error parsing signer "+
"public key %d for v0.4.0 (x-only "+
"format): %v", idx, err)
}
allSignerPubKeys[idx] = pubKey
case MuSig2Version100RC2:
pubKey, err := btcec.ParsePubKey(pubKeyBytes)
if err != nil {
return nil, fmt.Errorf("error parsing signer "+
"public key %d for v1.0.0rc2 ("+
"compressed format): %v", idx, err)
}
allSignerPubKeys[idx] = pubKey
default:
return nil, fmt.Errorf("unknown MuSig2 version: <%d>",
bipVersion)
}
allSignerPubKeys[idx] = pubKey
}
return allSignerPubKeys, nil
@@ -214,7 +285,26 @@ func MuSig2ParsePubKeys(rawPubKeys [][]byte) ([]*btcec.PublicKey, error) {
// MuSig2CombineKeys combines the given set of public keys into a single
// combined MuSig2 combined public key, applying the given tweaks.
func MuSig2CombineKeys(allSignerPubKeys []*btcec.PublicKey, sortKeys bool,
func MuSig2CombineKeys(bipVersion MuSig2Version,
allSignerPubKeys []*btcec.PublicKey, sortKeys bool,
tweaks *MuSig2Tweaks) (*musig2.AggregateKey, error) {
switch bipVersion {
case MuSig2Version040:
return combineKeysV040(allSignerPubKeys, sortKeys, tweaks)
case MuSig2Version100RC2:
return combineKeysV100RC2(allSignerPubKeys, sortKeys, tweaks)
default:
return nil, fmt.Errorf("unknown MuSig2 version: <%d>",
bipVersion)
}
}
// combineKeysV100rc1 implements the MuSigCombineKeys logic for the MuSig2 BIP
// draft version 1.0.0rc2.
func combineKeysV100RC2(allSignerPubKeys []*btcec.PublicKey, sortKeys bool,
tweaks *MuSig2Tweaks) (*musig2.AggregateKey, error) {
// Convert the tweak options into the appropriate MuSig2 API functional
@@ -240,8 +330,69 @@ func MuSig2CombineKeys(allSignerPubKeys []*btcec.PublicKey, sortKeys bool,
return combinedKey, err
}
// combineKeysV040 implements the MuSigCombineKeys logic for the MuSig2 BIP
// draft version 0.4.0.
func combineKeysV040(allSignerPubKeys []*btcec.PublicKey, sortKeys bool,
tweaks *MuSig2Tweaks) (*musig2.AggregateKey, error) {
// Convert the tweak options into the appropriate MuSig2 API functional
// options.
var keyAggOpts []musig2v040.KeyAggOption
switch {
case tweaks.TaprootBIP0086Tweak:
keyAggOpts = append(keyAggOpts, musig2v040.WithBIP86KeyTweak())
case len(tweaks.TaprootTweak) > 0:
keyAggOpts = append(keyAggOpts, musig2v040.WithTaprootKeyTweak(
tweaks.TaprootTweak,
))
case len(tweaks.GenericTweaks) > 0:
genericTweaksCopy := make(
[]musig2v040.KeyTweakDesc, len(tweaks.GenericTweaks),
)
for idx := range tweaks.GenericTweaks {
genericTweaksCopy[idx] = musig2v040.KeyTweakDesc{
Tweak: tweaks.GenericTweaks[idx].Tweak,
IsXOnly: tweaks.GenericTweaks[idx].IsXOnly,
}
}
keyAggOpts = append(keyAggOpts, musig2v040.WithKeyTweaks(
genericTweaksCopy...,
))
}
// Then we'll use this information to compute the aggregated public key.
combinedKey, _, _, err := musig2v040.AggregateKeys(
allSignerPubKeys, sortKeys, keyAggOpts...,
)
// Copy the result back into the default version's native type.
return &musig2.AggregateKey{
FinalKey: combinedKey.FinalKey,
PreTweakedKey: combinedKey.PreTweakedKey,
}, err
}
// MuSig2CreateContext creates a new MuSig2 signing context.
func MuSig2CreateContext(privKey *btcec.PrivateKey,
func MuSig2CreateContext(bipVersion MuSig2Version, privKey *btcec.PrivateKey,
allSignerPubKeys []*btcec.PublicKey,
tweaks *MuSig2Tweaks) (MuSig2Context, MuSig2Session, error) {
switch bipVersion {
case MuSig2Version040:
return createContextV040(privKey, allSignerPubKeys, tweaks)
case MuSig2Version100RC2:
return createContextV100RC2(privKey, allSignerPubKeys, tweaks)
default:
return nil, nil, fmt.Errorf("unknown MuSig2 version: <%d>",
bipVersion)
}
}
// createContextV100RC2 implements the MuSig2CreateContext logic for the MuSig2
// BIP draft version 1.0.0rc2.
func createContextV100RC2(privKey *btcec.PrivateKey,
allSignerPubKeys []*btcec.PublicKey,
tweaks *MuSig2Tweaks) (*musig2.Context, *musig2.Session, error) {
@@ -267,6 +418,35 @@ func MuSig2CreateContext(privKey *btcec.PrivateKey,
return muSigContext, muSigSession, nil
}
// createContextV040 implements the MuSig2CreateContext logic for the MuSig2 BIP
// draft version 0.4.0.
func createContextV040(privKey *btcec.PrivateKey,
allSignerPubKeys []*btcec.PublicKey,
tweaks *MuSig2Tweaks) (*musig2v040.Context, *musig2v040.Session,
error) {
// The context keeps track of all signing keys and our local key.
allOpts := append(
[]musig2v040.ContextOption{
musig2v040.WithKnownSigners(allSignerPubKeys),
},
tweaks.ToV040ContextOptions()...,
)
muSigContext, err := musig2v040.NewContext(privKey, true, allOpts...)
if err != nil {
return nil, nil, fmt.Errorf("error creating MuSig2 signing "+
"context: %v", err)
}
muSigSession, err := muSigContext.NewSession()
if err != nil {
return nil, nil, fmt.Errorf("error creating MuSig2 signing "+
"session: %v", err)
}
return muSigContext, muSigSession, nil
}
// MuSig2Sign calls the Sign() method on the given versioned signing session and
// returns the result in the most recent version of the MuSig2 API.
func MuSig2Sign(session MuSig2Session, msg [32]byte,
@@ -286,6 +466,22 @@ func MuSig2Sign(session MuSig2Session, msg [32]byte,
return partialSig, nil
case *musig2v040.Session:
var opts []musig2v040.SignOption
if withSortedKeys {
opts = append(opts, musig2v040.WithSortedKeys())
}
partialSig, err := s.Sign(msg, opts...)
if err != nil {
return nil, fmt.Errorf("error signing with local key: "+
"%v", err)
}
return &musig2.PartialSignature{
S: partialSig.S,
R: partialSig.R,
}, nil
default:
return nil, fmt.Errorf("invalid session type <%T>", s)
}
@@ -306,6 +502,18 @@ func MuSig2CombineSig(session MuSig2Session,
return haveAllSigs, nil
case *musig2v040.Session:
haveAllSigs, err := s.CombineSig(&musig2v040.PartialSignature{
S: otherPartialSig.S,
R: otherPartialSig.R,
})
if err != nil {
return false, fmt.Errorf("error combining partial "+
"signature: %v", err)
}
return haveAllSigs, nil
default:
return false, fmt.Errorf("invalid session type <%T>", s)
}
+74 -6
View File
@@ -40,6 +40,19 @@ var (
"CA66",
))
testVector100Key1, _ = btcec.ParsePubKey(hexDecode(
"02F9308A019258C31049344F85F89D5229B531C845836F99B08601F113BC" +
"E036F9",
))
testVector100Key2, _ = btcec.ParsePubKey(hexDecode(
"03DFF1D77F2A671C5F36183726DB2341BE58FEAE1DA2DECED843240F7B50" +
"2BA659",
))
testVector100Key3, _ = btcec.ParsePubKey(hexDecode(
"023590A94E768F8E1815C2F24B4D80A8E3149316C3518CE7B7AD338368D0" +
"38CA66",
))
bip86Tweak = &MuSig2Tweaks{TaprootBIP0086Tweak: true}
)
@@ -48,6 +61,7 @@ func TestMuSig2CombineKeys(t *testing.T) {
testCases := []struct {
name string
version MuSig2Version
keys []*btcec.PublicKey
sortKeys bool
tweak *MuSig2Tweaks
@@ -55,16 +69,21 @@ func TestMuSig2CombineKeys(t *testing.T) {
expectedFinalKey string
expectedPreTweakKey string
}{{
name: "invalid version",
version: 7,
expectedErr: "unknown MuSig2 version: <7>",
}, {
name: "v0.4.0 two dummy keys BIP86",
version: MuSig2Version040,
keys: []*btcec.PublicKey{dummyPubKey1, dummyPubKey2},
sortKeys: true,
tweak: bip86Tweak,
sortKeys: true, tweak: bip86Tweak,
expectedFinalKey: "03b54fb320a8fc3589e86a1559c6aaa774fbab4e4d" +
"9fbf31e2fd836b661ac6a132",
expectedPreTweakKey: "0279c76a15dcf6786058a571e4022b78633e1bf" +
"8a7a4ca440bcbbeeaea772228a2",
}, {
name: "v0.4.0 three dummy keys BIP86",
name: "v0.4.0 three dummy keys BIP86",
version: MuSig2Version040,
keys: []*btcec.PublicKey{
dummyPubKey1, dummyPubKey2, dummyPubKey3,
},
@@ -75,12 +94,13 @@ func TestMuSig2CombineKeys(t *testing.T) {
expectedPreTweakKey: "03e615b8aad4ed10544537bc48b1d6600e15773" +
"476a675c6cbba6808f21b1988e5",
}, {
name: "v0.4.0 three test vector keys BIP86",
name: "v0.4.0 three test vector keys BIP86",
version: MuSig2Version040,
keys: []*btcec.PublicKey{
testVector040Key1, testVector040Key2, testVector040Key3,
},
sortKeys: true,
tweak: bip86Tweak,
sortKeys: true,
expectedFinalKey: "025b257b4e785d61157ef5303051f45184bd5cb47b" +
"c4b4069ed4dd4536459cb83b",
expectedPreTweakKey: "02d70cd69a2647f7390973df48cbfa2ccc407b8" +
@@ -107,6 +127,54 @@ func TestMuSig2CombineKeys(t *testing.T) {
"dfdb788b3735b76a55538179",
expectedPreTweakKey: "03e5830140512195d74c8307e39637cbe5fb730" +
"ebeab80ec514cf88a877ceeee0b",
}, {
name: "v1.0.0rc2 three dummy keys BIP86",
version: MuSig2Version100RC2,
keys: []*btcec.PublicKey{
dummyPubKey1, dummyPubKey2, dummyPubKey3,
},
sortKeys: true,
tweak: bip86Tweak,
expectedFinalKey: "029d11a433e446276c88ee099cecfda1e4234dc486" +
"49e2b07f9b159176731eb68a",
expectedPreTweakKey: "03f0bacfef76086c519b6db216a252f1435336f" +
"d23fc86cfb51f67056439c256d4",
}, {
name: "v1.0.0rc2 three test vector keys BIP86",
version: MuSig2Version100RC2,
keys: []*btcec.PublicKey{
testVector100Key1, testVector100Key2, testVector100Key3,
},
sortKeys: false,
tweak: bip86Tweak,
expectedFinalKey: "03f79d14149ecd4bb74921865906a8e4f1333439a9" +
"1b96610d72caa7495dcf2376",
expectedPreTweakKey: "0290539eede565f5d054f32cc0c220126889ed1" +
"e5d193baf15aef344fe59d4610c",
}, {
name: "v1.0.0rc2 three test vector keys BIP86 sorted",
version: MuSig2Version100RC2,
keys: []*btcec.PublicKey{
testVector100Key1, testVector100Key2, testVector100Key3,
},
sortKeys: true,
tweak: bip86Tweak,
expectedFinalKey: "0379e6c3e628c9bfbce91de6b7fb28e2aec7713d37" +
"7cf260ab599dcbc40e542312",
expectedPreTweakKey: "03789d937bade6673538f3e28d8368dda4d0512" +
"f94da44cf477a505716d26a1575",
}, {
name: "v1.0.0rc2 three test vector keys BIP86 reverse order",
version: MuSig2Version100RC2,
keys: []*btcec.PublicKey{
testVector100Key3, testVector100Key2, testVector100Key1,
},
sortKeys: false,
tweak: bip86Tweak,
expectedFinalKey: "03d61d333ab8c53c330290c144f406ce0c0dc3564b" +
"8e3dee6d1daa6288609bfc75",
expectedPreTweakKey: "036204de8b083426dc6eaf9502d27024d53fc82" +
"6bf7d2012148a0575435df54b2b",
}}
for _, tc := range testCases {
@@ -115,7 +183,7 @@ func TestMuSig2CombineKeys(t *testing.T) {
tt.Parallel()
res, err := MuSig2CombineKeys(
tc.keys, tc.sortKeys, tc.tweak,
tc.version, tc.keys, tc.sortKeys, tc.tweak,
)
if tc.expectedErr != "" {
+1 -1
View File
@@ -139,7 +139,7 @@ func (m *MockSigner) ComputeInputScript(tx *wire.MsgTx, signDesc *SignDescriptor
// all signing parties must be provided, including the public key of the local
// signing key. If nonces of other parties are already known, they can be
// submitted as well to reduce the number of method calls necessary later on.
func (m *MockSigner) MuSig2CreateSession(keychain.KeyLocator,
func (m *MockSigner) MuSig2CreateSession(MuSig2Version, keychain.KeyLocator,
[]*btcec.PublicKey, *MuSig2Tweaks,
[][musig2.PubNonceSize]byte) (*MuSig2SessionInfo, error) {