Skip to content

Commit 4d8b4f5

Browse files
committed
signingprovider: Add musig2 secnonces
Adds GetMuSig2SecNonces which returns secp256k1_musig_secnonce*, and DeleteMuSig2Session which removes the MuSig2 secnonce from wherever it was retrieved. FlatSigningProvider stores it as a pointer to a map of session id to secnonce so that deletion will actually delete from the object that actually owns the secnonces. The session id is just a unique identifier for the caller to determine what secnonces have been created.
1 parent c06a1dc commit 4d8b4f5

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/script/signingprovider.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ std::vector<CPubKey> HidingSigningProvider::GetMuSig2ParticipantPubkeys(const CP
5858
return m_provider->GetMuSig2ParticipantPubkeys(pubkey);
5959
}
6060

61+
void HidingSigningProvider::SetMuSig2SecNonce(const uint256& id, MuSig2SecNonce&& nonce) const
62+
{
63+
m_provider->SetMuSig2SecNonce(id, std::move(nonce));
64+
}
65+
66+
std::optional<std::reference_wrapper<MuSig2SecNonce>> HidingSigningProvider::GetMuSig2SecNonce(const uint256& session_id) const
67+
{
68+
return m_provider->GetMuSig2SecNonce(session_id);
69+
}
70+
71+
void HidingSigningProvider::DeleteMuSig2Session(const uint256& session_id) const
72+
{
73+
m_provider->DeleteMuSig2Session(session_id);
74+
}
75+
6176
bool FlatSigningProvider::GetCScript(const CScriptID& scriptid, CScript& script) const { return LookupHelper(scripts, scriptid, script); }
6277
bool FlatSigningProvider::GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const { return LookupHelper(pubkeys, keyid, pubkey); }
6378
bool FlatSigningProvider::GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const
@@ -94,6 +109,26 @@ std::vector<CPubKey> FlatSigningProvider::GetMuSig2ParticipantPubkeys(const CPub
94109
return participant_pubkeys;
95110
}
96111

112+
void FlatSigningProvider::SetMuSig2SecNonce(const uint256& session_id, MuSig2SecNonce&& nonce) const
113+
{
114+
if (!Assume(musig2_secnonces)) return;
115+
musig2_secnonces->emplace(session_id, std::move(nonce));
116+
}
117+
118+
std::optional<std::reference_wrapper<MuSig2SecNonce>> FlatSigningProvider::GetMuSig2SecNonce(const uint256& session_id) const
119+
{
120+
if (!Assume(musig2_secnonces)) return std::nullopt;
121+
const auto& it = musig2_secnonces->find(session_id);
122+
if (it == musig2_secnonces->end()) return std::nullopt;
123+
return it->second;
124+
}
125+
126+
void FlatSigningProvider::DeleteMuSig2Session(const uint256& session_id) const
127+
{
128+
if (!Assume(musig2_secnonces)) return;
129+
musig2_secnonces->erase(session_id);
130+
}
131+
97132
FlatSigningProvider& FlatSigningProvider::Merge(FlatSigningProvider&& b)
98133
{
99134
scripts.merge(b.scripts);
@@ -102,6 +137,8 @@ FlatSigningProvider& FlatSigningProvider::Merge(FlatSigningProvider&& b)
102137
origins.merge(b.origins);
103138
tr_trees.merge(b.tr_trees);
104139
aggregate_pubkeys.merge(b.aggregate_pubkeys);
140+
// We shouldn't be merging 2 different sessions, just overwrite with b's sessions.
141+
if (!musig2_secnonces) musig2_secnonces = b.musig2_secnonces;
105142
return *this;
106143
}
107144

src/script/signingprovider.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
99
#include <addresstype.h>
1010
#include <attributes.h>
1111
#include <key.h>
12+
#include <musig.h>
1213
#include <pubkey.h>
1314
#include <script/keyorigin.h>
1415
#include <script/script.h>
1516
#include <sync.h>
1617

18+
#include <functional>
19+
#include <optional>
20+
1721
struct ShortestVectorFirstComparator
1822
{
1923
bool operator()(const std::vector<unsigned char>& a, const std::vector<unsigned char>& b) const
@@ -162,6 +166,9 @@ class SigningProvider
162166
virtual bool GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const { return false; }
163167
virtual bool GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const { return false; }
164168
virtual std::vector<CPubKey> GetMuSig2ParticipantPubkeys(const CPubKey& pubkey) const { return {}; }
169+
virtual void SetMuSig2SecNonce(const uint256& id, MuSig2SecNonce&& nonce) const {}
170+
virtual std::optional<std::reference_wrapper<MuSig2SecNonce>> GetMuSig2SecNonce(const uint256& session_id) const { return std::nullopt; }
171+
virtual void DeleteMuSig2Session(const uint256& session_id) const {}
165172

166173
bool GetKeyByXOnly(const XOnlyPubKey& pubkey, CKey& key) const
167174
{
@@ -206,6 +213,9 @@ class HidingSigningProvider : public SigningProvider
206213
bool GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const override;
207214
bool GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const override;
208215
std::vector<CPubKey> GetMuSig2ParticipantPubkeys(const CPubKey& pubkey) const override;
216+
void SetMuSig2SecNonce(const uint256& id, MuSig2SecNonce&& nonce) const override;
217+
std::optional<std::reference_wrapper<MuSig2SecNonce>> GetMuSig2SecNonce(const uint256& session_id) const override;
218+
void DeleteMuSig2Session(const uint256& session_id) const override;
209219
};
210220

211221
struct FlatSigningProvider final : public SigningProvider
@@ -216,6 +226,7 @@ struct FlatSigningProvider final : public SigningProvider
216226
std::map<CKeyID, CKey> keys;
217227
std::map<XOnlyPubKey, TaprootBuilder> tr_trees; /** Map from output key to Taproot tree (which can then make the TaprootSpendData */
218228
std::map<CPubKey, std::vector<CPubKey>> aggregate_pubkeys; /** MuSig2 aggregate pubkeys */
229+
std::map<uint256, MuSig2SecNonce>* musig2_secnonces{nullptr};
219230

220231
bool GetCScript(const CScriptID& scriptid, CScript& script) const override;
221232
bool GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const override;
@@ -225,6 +236,9 @@ struct FlatSigningProvider final : public SigningProvider
225236
bool GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const override;
226237
bool GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const override;
227238
std::vector<CPubKey> GetMuSig2ParticipantPubkeys(const CPubKey& pubkey) const override;
239+
void SetMuSig2SecNonce(const uint256& id, MuSig2SecNonce&& nonce) const override;
240+
std::optional<std::reference_wrapper<MuSig2SecNonce>> GetMuSig2SecNonce(const uint256& session_id) const override;
241+
void DeleteMuSig2Session(const uint256& session_id) const override;
228242

229243
FlatSigningProvider& Merge(FlatSigningProvider&& b) LIFETIMEBOUND;
230244
};

0 commit comments

Comments
 (0)