input+lnwallet: modify musig2 interfaces use explicit optional local nonces

In this commit, we modify the musig2 interfaces to instead use an
explicit value for the local nonces. Before this commit, we used the
functional option, but we want to also support specifying this value
over RPC for the remote signer. The functional option pattern is opaque,
so we can't get the nonce value we need. To get around this, we'll just
make this an explicit pointer, then map this to the functional option at
the very last moment.
This commit is contained in:
Olaoluwa Osuntokun 2023-09-15 17:51:45 -07:00
parent 2d5c0c9b4e
commit ce93b236aa
No known key found for this signature in database
GPG Key ID: 3BBD59E99B280306
6 changed files with 23 additions and 15 deletions

View File

@ -49,12 +49,13 @@ type MuSig2Signer interface {
// already known, they can be submitted as well to reduce the number of // already known, they can be submitted as well to reduce the number of
// method calls necessary later on. // method calls necessary later on.
// //
// The set of sessionOpts are _optional_ and allow a caller to modify // The localNonces field is optional. If it is set, then the specified
// the generated sessions. As an example the local nonce might already // nonces will be used instead of generating from scratch. This is
// be generated ahead of time. // useful in instances where the nonces are generated ahead of time
// before the set of signers is known.
MuSig2CreateSession(MuSig2Version, keychain.KeyLocator, MuSig2CreateSession(MuSig2Version, keychain.KeyLocator,
[]*btcec.PublicKey, *MuSig2Tweaks, [][musig2.PubNonceSize]byte, []*btcec.PublicKey, *MuSig2Tweaks, [][musig2.PubNonceSize]byte,
...musig2.SessionOption) (*MuSig2SessionInfo, error) *musig2.Nonces) (*MuSig2SessionInfo, error)
// MuSig2RegisterNonces registers one or more public nonces of other // MuSig2RegisterNonces registers one or more public nonces of other
// signing participants for a session identified by its ID. This method // signing participants for a session identified by its ID. This method
@ -379,18 +380,18 @@ func combineKeysV040(allSignerPubKeys []*btcec.PublicKey, sortKeys bool,
// MuSig2CreateContext creates a new MuSig2 signing context. // MuSig2CreateContext creates a new MuSig2 signing context.
func MuSig2CreateContext(bipVersion MuSig2Version, privKey *btcec.PrivateKey, func MuSig2CreateContext(bipVersion MuSig2Version, privKey *btcec.PrivateKey,
allSignerPubKeys []*btcec.PublicKey, tweaks *MuSig2Tweaks, allSignerPubKeys []*btcec.PublicKey, tweaks *MuSig2Tweaks,
sessionOpts ...musig2.SessionOption, localNonces *musig2.Nonces,
) (MuSig2Context, MuSig2Session, error) { ) (MuSig2Context, MuSig2Session, error) {
switch bipVersion { switch bipVersion {
case MuSig2Version040: case MuSig2Version040:
return createContextV040( return createContextV040(
privKey, allSignerPubKeys, tweaks, sessionOpts..., privKey, allSignerPubKeys, tweaks, localNonces,
) )
case MuSig2Version100RC2: case MuSig2Version100RC2:
return createContextV100RC2( return createContextV100RC2(
privKey, allSignerPubKeys, tweaks, sessionOpts..., privKey, allSignerPubKeys, tweaks, localNonces,
) )
default: default:
@ -403,7 +404,7 @@ func MuSig2CreateContext(bipVersion MuSig2Version, privKey *btcec.PrivateKey,
// BIP draft version 1.0.0rc2. // BIP draft version 1.0.0rc2.
func createContextV100RC2(privKey *btcec.PrivateKey, func createContextV100RC2(privKey *btcec.PrivateKey,
allSignerPubKeys []*btcec.PublicKey, tweaks *MuSig2Tweaks, allSignerPubKeys []*btcec.PublicKey, tweaks *MuSig2Tweaks,
sessionOpts ...musig2.SessionOption, localNonces *musig2.Nonces,
) (*musig2.Context, *musig2.Session, error) { ) (*musig2.Context, *musig2.Session, error) {
// The context keeps track of all signing keys and our local key. // The context keeps track of all signing keys and our local key.
@ -419,6 +420,13 @@ func createContextV100RC2(privKey *btcec.PrivateKey,
"context: %v", err) "context: %v", err)
} }
var sessionOpts []musig2.SessionOption
if localNonces != nil {
sessionOpts = append(
sessionOpts, musig2.WithPreGeneratedNonce(localNonces),
)
}
muSigSession, err := muSigContext.NewSession(sessionOpts...) muSigSession, err := muSigContext.NewSession(sessionOpts...)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("error creating MuSig2 signing "+ return nil, nil, fmt.Errorf("error creating MuSig2 signing "+
@ -432,7 +440,7 @@ func createContextV100RC2(privKey *btcec.PrivateKey,
// draft version 0.4.0. // draft version 0.4.0.
func createContextV040(privKey *btcec.PrivateKey, func createContextV040(privKey *btcec.PrivateKey,
allSignerPubKeys []*btcec.PublicKey, tweaks *MuSig2Tweaks, allSignerPubKeys []*btcec.PublicKey, tweaks *MuSig2Tweaks,
_ ...musig2.SessionOption, _ *musig2.Nonces,
) (*musig2v040.Context, *musig2v040.Session, error) { ) (*musig2v040.Context, *musig2v040.Session, error) {
// The context keeps track of all signing keys and our local key. // The context keeps track of all signing keys and our local key.

View File

@ -63,7 +63,7 @@ func NewMusigSessionManager(keyFetcher PrivKeyFetcher) *MusigSessionManager {
func (m *MusigSessionManager) MuSig2CreateSession(bipVersion MuSig2Version, func (m *MusigSessionManager) MuSig2CreateSession(bipVersion MuSig2Version,
keyLoc keychain.KeyLocator, allSignerPubKeys []*btcec.PublicKey, keyLoc keychain.KeyLocator, allSignerPubKeys []*btcec.PublicKey,
tweaks *MuSig2Tweaks, otherSignerNonces [][musig2.PubNonceSize]byte, tweaks *MuSig2Tweaks, otherSignerNonces [][musig2.PubNonceSize]byte,
sessionOpts ...musig2.SessionOption) (*MuSig2SessionInfo, error) { localNonces *musig2.Nonces) (*MuSig2SessionInfo, error) {
// We need to derive the private key for signing. In the remote signing // We need to derive the private key for signing. In the remote signing
// setup, this whole RPC call will be forwarded to the signing // setup, this whole RPC call will be forwarded to the signing
@ -78,7 +78,7 @@ func (m *MusigSessionManager) MuSig2CreateSession(bipVersion MuSig2Version,
// Create a signing context and session with the given private key and // Create a signing context and session with the given private key and
// list of all known signer public keys. // list of all known signer public keys.
musigContext, musigSession, err := MuSig2CreateContext( musigContext, musigSession, err := MuSig2CreateContext(
bipVersion, privKey, allSignerPubKeys, tweaks, sessionOpts..., bipVersion, privKey, allSignerPubKeys, tweaks, localNonces,
) )
if err != nil { if err != nil {
return nil, fmt.Errorf("error creating signing context: %w", return nil, fmt.Errorf("error creating signing context: %w",

View File

@ -57,7 +57,7 @@ func (d *DummySigner) ComputeInputScript(tx *wire.MsgTx,
// submitted as well to reduce the number of method calls necessary later on. // submitted as well to reduce the number of method calls necessary later on.
func (d *DummySigner) MuSig2CreateSession(input.MuSig2Version, func (d *DummySigner) MuSig2CreateSession(input.MuSig2Version,
keychain.KeyLocator, []*btcec.PublicKey, *input.MuSig2Tweaks, keychain.KeyLocator, []*btcec.PublicKey, *input.MuSig2Tweaks,
[][musig2.PubNonceSize]byte, ...musig2.SessionOption, [][musig2.PubNonceSize]byte, *musig2.Nonces,
) (*input.MuSig2SessionInfo, error) { ) (*input.MuSig2SessionInfo, error) {
return nil, nil return nil, nil

View File

@ -260,7 +260,7 @@ func (m *MusigSession) FinalizeSession(signingNonce musig2.Nonces) error {
m.session, err = m.signer.MuSig2CreateSession( m.session, err = m.signer.MuSig2CreateSession(
input.MuSig2Version100RC2, m.localKey.KeyLocator, m.signerKeys, input.MuSig2Version100RC2, m.localKey.KeyLocator, m.signerKeys,
&tweakDesc, [][musig2.PubNonceSize]byte{remoteNonce.PubNonce}, &tweakDesc, [][musig2.PubNonceSize]byte{remoteNonce.PubNonce},
musig2.WithPreGeneratedNonce(&localNonce), &localNonce,
) )
if err != nil { if err != nil {
return err return err

View File

@ -657,7 +657,7 @@ func (r *RPCKeyRing) ComputeInputScript(tx *wire.MsgTx,
func (r *RPCKeyRing) MuSig2CreateSession(bipVersion input.MuSig2Version, func (r *RPCKeyRing) MuSig2CreateSession(bipVersion input.MuSig2Version,
keyLoc keychain.KeyLocator, pubKeys []*btcec.PublicKey, keyLoc keychain.KeyLocator, pubKeys []*btcec.PublicKey,
tweaks *input.MuSig2Tweaks, otherNonces [][musig2.PubNonceSize]byte, tweaks *input.MuSig2Tweaks, otherNonces [][musig2.PubNonceSize]byte,
_ ...musig2.SessionOption) (*input.MuSig2SessionInfo, error) { localNonces *musig2.Nonces) (*input.MuSig2SessionInfo, error) {
apiVersion, err := signrpc.MarshalMuSig2Version(bipVersion) apiVersion, err := signrpc.MarshalMuSig2Version(bipVersion)
if err != nil { if err != nil {

View File

@ -72,7 +72,7 @@ func (s *MockSigner) ComputeInputScript(tx *wire.MsgTx,
func (s *MockSigner) MuSig2CreateSession(input.MuSig2Version, func (s *MockSigner) MuSig2CreateSession(input.MuSig2Version,
keychain.KeyLocator, []*btcec.PublicKey, *input.MuSig2Tweaks, keychain.KeyLocator, []*btcec.PublicKey, *input.MuSig2Tweaks,
[][musig2.PubNonceSize]byte, [][musig2.PubNonceSize]byte,
...musig2.SessionOption) (*input.MuSig2SessionInfo, error) { *musig2.Nonces) (*input.MuSig2SessionInfo, error) {
return nil, nil return nil, nil
} }