Skip to content

Commit c519b46

Browse files
committed
musig: add pubkey_get to obtain a full pubkey from a keyagg_cache
1 parent 21e2d65 commit c519b46

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

include/secp256k1_musig.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,24 @@ SECP256K1_API int secp256k1_musig_pubkey_agg(
223223
size_t n_pubkeys
224224
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(5);
225225

226+
/** Obtain the aggregate public key from a keyagg_cache.
227+
*
228+
* This is only useful if you need the non-xonly public key, in particular for
229+
* ordinary (non-xonly) tweaking or batch-verifying multiple key aggregations
230+
* (not implemented).
231+
*
232+
* Returns: 0 if the arguments are invalid, 1 otherwise
233+
* Args: ctx: pointer to a context object
234+
* Out: agg_pk: the MuSig-aggregated public key.
235+
* In: keyagg_cache: pointer to a `musig_keyagg_cache` struct initialized by
236+
* `musig_pubkey_agg`
237+
*/
238+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_pubkey_get(
239+
const secp256k1_context* ctx,
240+
secp256k1_pubkey *agg_pk,
241+
secp256k1_musig_keyagg_cache *keyagg_cache
242+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
243+
226244
/** Tweak an x-only public key in a given keyagg_cache by adding
227245
* the generator multiplied with `tweak32` to it.
228246
*

src/modules/musig/keyagg_impl.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,20 @@ int secp256k1_musig_pubkey_agg(const secp256k1_context* ctx, secp256k1_scratch_s
244244
return 1;
245245
}
246246

247+
int secp256k1_musig_pubkey_get(const secp256k1_context* ctx, secp256k1_pubkey *agg_pk, secp256k1_musig_keyagg_cache *keyagg_cache) {
248+
secp256k1_keyagg_cache_internal cache_i;
249+
VERIFY_CHECK(ctx != NULL);
250+
ARG_CHECK(agg_pk != NULL);
251+
memset(agg_pk, 0, sizeof(*agg_pk));
252+
ARG_CHECK(keyagg_cache != NULL);
253+
254+
if(!secp256k1_keyagg_cache_load(ctx, &cache_i, keyagg_cache)) {
255+
return 0;
256+
}
257+
secp256k1_pubkey_save(agg_pk, &cache_i.pk);
258+
return 1;
259+
}
260+
247261
int secp256k1_musig_pubkey_tweak_add(const secp256k1_context* ctx, secp256k1_pubkey *output_pubkey, secp256k1_musig_keyagg_cache *keyagg_cache, const unsigned char *tweak32) {
248262
secp256k1_keyagg_cache_internal cache_i;
249263
int overflow = 0;

src/modules/musig/tests_impl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ void musig_api_tests(secp256k1_scratch_space *scratch) {
140140
unsigned char aggnonce_ser[66];
141141
unsigned char msg[32];
142142
secp256k1_xonly_pubkey agg_pk;
143+
secp256k1_pubkey full_agg_pk;
143144
secp256k1_musig_keyagg_cache keyagg_cache;
144145
secp256k1_musig_keyagg_cache invalid_keyagg_cache;
145146
secp256k1_musig_session session;
@@ -243,6 +244,15 @@ void musig_api_tests(secp256k1_scratch_space *scratch) {
243244
CHECK(secp256k1_musig_pubkey_agg(sign, scratch, &agg_pk, &keyagg_cache, pk_ptr, 2) == 1);
244245
CHECK(secp256k1_musig_pubkey_agg(vrfy, scratch, &agg_pk, &keyagg_cache, pk_ptr, 2) == 1);
245246

247+
/* pubkey_get */
248+
ecount = 0;
249+
CHECK(secp256k1_musig_pubkey_get(none, &full_agg_pk, &keyagg_cache) == 1);
250+
CHECK(secp256k1_musig_pubkey_get(none, NULL, &keyagg_cache) == 0);
251+
CHECK(ecount == 1);
252+
CHECK(secp256k1_musig_pubkey_get(none, &full_agg_pk, NULL) == 0);
253+
CHECK(ecount == 2);
254+
CHECK(secp256k1_memcmp_var(&full_agg_pk, zeros68, sizeof(full_agg_pk)) == 0);
255+
246256
/** Tweaking **/
247257
ecount = 0;
248258
{

0 commit comments

Comments
 (0)