6#ifndef SECP256K1_MODULE_MUSIG_SESSION_IMPL_H
7#define SECP256K1_MODULE_MUSIG_SESSION_IMPL_H
25 if (secp256k1_ge_is_infinity(ge)) {
36 unsigned char zeros[33] = { 0 };
52 secp256k1_scalar_get_b32(&secnonce->
data[4], &
k[0]);
53 secp256k1_scalar_get_b32(&secnonce->
data[36], &
k[1]);
63 secp256k1_declassify(ctx, &is_zero,
sizeof(is_zero));
66 secp256k1_scalar_set_b32(&
k[0], &secnonce->
data[4], NULL);
67 secp256k1_scalar_set_b32(&
k[1], &secnonce->
data[36], NULL);
81 secp256k1_declassify(ctx, &secnonce->
data[68], 64);
91 for (i = 0; i < 2; i++) {
102 for (i = 0; i < 2; i++) {
113 for (i = 0; i < 2; i++) {
122 for (i = 0; i < 2; i++) {
139 unsigned char *ptr = session->
data;
147 secp256k1_scalar_get_b32(ptr, &session_i->
noncecoef);
149 secp256k1_scalar_get_b32(ptr, &session_i->
challenge);
151 secp256k1_scalar_get_b32(ptr, &session_i->
s_part);
155 const unsigned char *ptr = session->
data;
163 secp256k1_scalar_set_b32(&session_i->
noncecoef, ptr, NULL);
165 secp256k1_scalar_set_b32(&session_i->
challenge, ptr, NULL);
167 secp256k1_scalar_set_b32(&session_i->
s_part, ptr, NULL);
175 secp256k1_scalar_get_b32(&sig->
data[4], s);
182 secp256k1_scalar_set_b32(s, &sig->
data[4], &overflow);
196 for (i = 0; i < 2; i++) {
214 memset(out66, 0, 66);
220 for (i = 0; i < 2; i++) {
235 for (i = 0; i < 2; i++) {
250 memset(out66, 0, 66);
256 for (i = 0; i < 2; i++) {
271 memset(sig, 0,
sizeof(*sig));
273 secp256k1_scalar_set_b32(&tmp, in32, &overflow);
287 memcpy(out32, &sig->
data[4], 32);
293 unsigned char zero[7] = { 0 };
313 sha->
s[0] = 0xa19e884bul;
314 sha->
s[1] = 0xf463fe7eul;
315 sha->
s[2] = 0x2f18f9a2ul;
316 sha->
s[3] = 0xbeb0f9fful;
317 sha->
s[4] = 0x0f37e8b0ul;
318 sha->
s[5] = 0x06ebd26ful;
319 sha->
s[6] = 0xe3b243d2ul;
320 sha->
s[7] = 0x522fb150ul;
328 sha->
s[0] = 0x07101b64ul;
329 sha->
s[1] = 0x18003414ul;
330 sha->
s[2] = 0x0391bc43ul;
331 sha->
s[3] = 0x0e6258eeul;
332 sha->
s[4] = 0x29d26b72ul;
333 sha->
s[5] = 0x8343937eul;
334 sha->
s[6] = 0xb7a0a4fbul;
335 sha->
s[7] = 0xff568a30ul;
339static void secp256k1_nonce_function_musig(
secp256k1_scalar *k,
const unsigned char *session_secrand,
const unsigned char *msg32,
const unsigned char *seckey32,
const unsigned char *pk33,
const unsigned char *agg_pk32,
const unsigned char *extra_input32) {
341 unsigned char rand[32];
343 unsigned char msg_present;
345 if (seckey32 != NULL) {
349 for (i = 0; i < 32; i++) {
350 rand[i] ^= seckey32[i];
353 memcpy(rand, session_secrand,
sizeof(rand));
360 msg_present = msg32 != NULL;
367 for (i = 0; i < 2; i++) {
368 unsigned char buf[32];
372 secp256k1_scalar_set_b32(&
k[i], buf, NULL);
387 unsigned char pk_ser[33];
388 unsigned char aggpk_ser[32];
389 unsigned char *aggpk_ser_ptr = NULL;
394 memset(pubnonce, 0,
sizeof(*pubnonce));
399 if (seckey != NULL) {
401 ret &= secp256k1_scalar_set_b32_seckey(&sk, seckey);
402 secp256k1_scalar_clear(&sk);
405 if (keyagg_cache != NULL) {
412 aggpk_ser_ptr = aggpk_ser;
414 if (!secp256k1_pubkey_load(ctx, &pk, pubkey)) {
427 for (i = 0; i < 2; i++) {
429 secp256k1_scalar_clear(&
k[i]);
434 for (i = 0; i < 2; i++) {
435 secp256k1_gej_clear(&nonce_ptj[i]);
438 for (i = 0; i < 2; i++) {
439 secp256k1_declassify(ctx, &nonce_pts[i],
sizeof(nonce_pts[i]));
452 memset(secnonce, 0,
sizeof(*secnonce));
461 secp256k1_declassify(ctx, &
ret,
sizeof(
ret));
476 unsigned char buf[32] = { 0 };
477 unsigned char seckey[32];
483 memset(secnonce, 0,
sizeof(*secnonce));
507 secp256k1_gej_set_infinity(&summed_pubnonces[0]);
508 secp256k1_gej_set_infinity(&summed_pubnonces[1]);
510 for (i = 0; i < n_pubnonces; i++) {
515 for (j = 0; j < 2; j++) {
516 secp256k1_gej_add_ge_var(&summed_pubnonces[j], &summed_pubnonces[j], &nonce_pts[j], NULL);
531 for (i = 0; i < n_pubnonces; i++) {
538 secp256k1_ge_set_all_gej_var(aggnonce_pts, aggnonce_ptsj, 2);
547 sha->
s[0] = 0x2c7d5a45ul;
548 sha->
s[1] = 0x06bf7e53ul;
549 sha->
s[2] = 0x89be68a6ul;
550 sha->
s[3] = 0x971254c0ul;
551 sha->
s[4] = 0x60ac12d2ul;
552 sha->
s[5] = 0x72846dcdul;
553 sha->
s[6] = 0x6c81212ful;
554 sha->
s[7] = 0xde7a2500ul;
560 unsigned char buf[33];
565 for (i = 0; i < 2; i++) {
578 secp256k1_gej_set_ge(&tmp, &nonce_pts[1]);
579 secp256k1_ecmult(out_nonce, &tmp, b, NULL);
580 secp256k1_gej_add_ge_var(out_nonce, out_nonce, &nonce_pts[0], NULL);
584 unsigned char noncehash[32];
589 secp256k1_scalar_set_b32(b, noncehash, NULL);
592 secp256k1_ge_set_gej(&fin_nonce_pt, &fin_nonce_ptj);
593 if (secp256k1_ge_is_infinity(&fin_nonce_pt)) {
594 fin_nonce_pt = secp256k1_ge_const_g;
606 unsigned char fin_nonce[32];
608 unsigned char agg_pk32[32];
629 secp256k1_scalar_set_int(&session_i.
s_part, 0);
630 if (!secp256k1_scalar_is_zero(&cache_i.
tweak)) {
632 secp256k1_scalar_mul(&e_tmp, &session_i.
challenge, &cache_i.
tweak);
634 secp256k1_scalar_negate(&e_tmp, &e_tmp);
644 secp256k1_scalar_clear(sk);
645 secp256k1_scalar_clear(&
k[0]);
646 secp256k1_scalar_clear(&
k[1]);
692 secp256k1_scalar_negate(&sk, &sk);
697 secp256k1_scalar_mul(&sk, &sk, &mu);
705 secp256k1_scalar_negate(&
k[0], &
k[0]);
706 secp256k1_scalar_negate(&
k[1], &
k[1]);
710 secp256k1_scalar_mul(&s, &session_i.
challenge, &sk);
711 secp256k1_scalar_mul(&
k[1], &session_i.
noncecoef, &
k[1]);
712 secp256k1_scalar_add(&
k[0], &
k[0], &
k[1]);
713 secp256k1_scalar_add(&s, &s, &
k[0]);
747 if (!secp256k1_pubkey_load(ctx, &pkp, pubkey)) {
757 secp256k1_scalar_mul(&e, &session_i.
challenge, &mu);
764 secp256k1_scalar_negate(&e, &e);
771 secp256k1_scalar_negate(&s, &s);
772 secp256k1_gej_set_ge(&pkj, &pkp);
773 secp256k1_ecmult(&tmp, &pkj, &e, &s);
775 secp256k1_gej_neg(&rj, &rj);
777 secp256k1_gej_add_var(&tmp, &tmp, &rj, NULL);
779 return secp256k1_gej_is_infinity(&tmp);
791 for (i = 0; i < n_sigs; i++) {
798 for (i = 0; i < n_sigs; i++) {
803 secp256k1_scalar_add(&session_i.
s_part, &session_i.
s_part, &term);
805 secp256k1_scalar_get_b32(&sig64[32], &session_i.
s_part);
806 memcpy(&sig64[0], session_i.
fin_nonce, 32);
static void secp256k1_eckey_pubkey_serialize33(secp256k1_ge *elem, unsigned char *pub33)
Serialize a group element (that is not allowed to be infinity) to a compressed public key (33 bytes).
static int secp256k1_eckey_pubkey_parse(secp256k1_ge *elem, const unsigned char *pub, size_t size)
static int secp256k1_ecmult_gen_context_is_built(const secp256k1_ecmult_gen_context *ctx)
static int secp256k1_keypair_load(const secp256k1_context *ctx, secp256k1_scalar *sk, secp256k1_ge *pk, const secp256k1_keypair *keypair)
#define secp256k1_fe_is_odd
#define secp256k1_fe_normalize_var
#define secp256k1_fe_get_b32
static int secp256k1_fe_equal(const secp256k1_fe *a, const secp256k1_fe *b)
Determine whether two field elements are equal.
static void secp256k1_ge_to_bytes_ext(unsigned char *data, const secp256k1_ge *ge)
Convert a group element (that is allowed to be infinity) to a 64-byte array.
static void secp256k1_ge_from_bytes_ext(secp256k1_ge *ge, const unsigned char *data)
Convert a 64-byte array into a group element.
static int secp256k1_ge_is_in_correct_subgroup(const secp256k1_ge *ge)
Determine if a point (which is assumed to be on the curve) is in the correct (sub)group of the curve.
static void secp256k1_ge_set_all_gej(secp256k1_ge *r, const secp256k1_gej *a, size_t len)
Set group elements r[0:len] (affine) equal to group elements a[0:len] (jacobian).
static void secp256k1_ge_set_infinity(secp256k1_ge *r)
Set a group element (affine) equal to the point at infinity.
static void secp256k1_ge_to_bytes(unsigned char *buf, const secp256k1_ge *a)
Convert a group element that is not infinity to a 64-byte array.
static void secp256k1_ge_from_bytes(secp256k1_ge *r, const unsigned char *buf)
Convert a 64-byte array into group element.
static int secp256k1_keyagg_cache_load(const secp256k1_context *ctx, secp256k1_keyagg_cache_internal *cache_i, const secp256k1_musig_keyagg_cache *cache)
static void secp256k1_musig_keyaggcoef(secp256k1_scalar *r, const secp256k1_keyagg_cache_internal *cache_i, secp256k1_ge *pk)
static void secp256k1_schnorrsig_challenge(secp256k1_scalar *e, const unsigned char *r32, const unsigned char *msg, size_t msglen, const unsigned char *pubkey32)
static void secp256k1_sha256_initialize(secp256k1_sha256 *hash)
static void secp256k1_sha256_finalize(secp256k1_sha256 *hash, unsigned char *out32)
static void secp256k1_sha256_write(secp256k1_sha256 *hash, const unsigned char *data, size_t size)
static void secp256k1_sha256_clear(secp256k1_sha256 *hash)
static SECP256K1_INLINE void secp256k1_memclear_explicit(void *ptr, size_t len)
static SECP256K1_INLINE int secp256k1_memcmp_var(const void *s1, const void *s2, size_t n)
Semantics like memcmp.
static SECP256K1_INLINE void secp256k1_memzero_explicit(void *ptr, size_t len)
static SECP256K1_INLINE int secp256k1_is_zero_array(const unsigned char *s, size_t len)
static SECP256K1_INLINE void secp256k1_write_be64(unsigned char *p, uint64_t x)
#define VERIFY_CHECK(cond)
static SECP256K1_INLINE void secp256k1_memczero(void *s, size_t len, int flag)
struct secp256k1_context_struct secp256k1_context
Unless explicitly stated all pointer arguments must not be NULL.
SECP256K1_API int secp256k1_musig_aggnonce_serialize(const secp256k1_context *ctx, unsigned char *out66, const secp256k1_musig_aggnonce *nonce) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Serialize an aggregate public nonce.
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_partial_sig_parse(const secp256k1_context *ctx, secp256k1_musig_partial_sig *sig, const unsigned char *in32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse a MuSig partial signature.
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_pubnonce_parse(const secp256k1_context *ctx, secp256k1_musig_pubnonce *nonce, const unsigned char *in66) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse a signer's public nonce.
SECP256K1_API int secp256k1_musig_nonce_agg(const secp256k1_context *ctx, secp256k1_musig_aggnonce *aggnonce, const secp256k1_musig_pubnonce *const *pubnonces, size_t n_pubnonces) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Aggregates the nonces of all signers into a single nonce.
SECP256K1_API int secp256k1_musig_partial_sign(const secp256k1_context *ctx, secp256k1_musig_partial_sig *partial_sig, secp256k1_musig_secnonce *secnonce, const secp256k1_keypair *keypair, const secp256k1_musig_keyagg_cache *keyagg_cache, const secp256k1_musig_session *session) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(6)
Produces a partial signature.
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_aggnonce_parse(const secp256k1_context *ctx, secp256k1_musig_aggnonce *nonce, const unsigned char *in66) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse an aggregate public nonce.
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_nonce_gen(const secp256k1_context *ctx, secp256k1_musig_secnonce *secnonce, secp256k1_musig_pubnonce *pubnonce, unsigned char *session_secrand32, const unsigned char *seckey, const secp256k1_pubkey *pubkey, const unsigned char *msg32, const secp256k1_musig_keyagg_cache *keyagg_cache, const unsigned char *extra_input32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(6)
Starts a signing session by generating a nonce.
SECP256K1_API int secp256k1_musig_partial_sig_serialize(const secp256k1_context *ctx, unsigned char *out32, const secp256k1_musig_partial_sig *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Serialize a MuSig partial signature.
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_partial_sig_verify(const secp256k1_context *ctx, const secp256k1_musig_partial_sig *partial_sig, const secp256k1_musig_pubnonce *pubnonce, const secp256k1_pubkey *pubkey, const secp256k1_musig_keyagg_cache *keyagg_cache, const secp256k1_musig_session *session) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(6)
Verifies an individual signer's partial signature.
SECP256K1_API int secp256k1_musig_partial_sig_agg(const secp256k1_context *ctx, unsigned char *sig64, const secp256k1_musig_session *session, const secp256k1_musig_partial_sig *const *partial_sigs, size_t n_sigs) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Aggregates partial signatures.
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_nonce_gen_counter(const secp256k1_context *ctx, secp256k1_musig_secnonce *secnonce, secp256k1_musig_pubnonce *pubnonce, uint64_t nonrepeating_cnt, const secp256k1_keypair *keypair, const unsigned char *msg32, const secp256k1_musig_keyagg_cache *keyagg_cache, const unsigned char *extra_input32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(5)
Alternative way to generate a nonce and start a signing session.
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_nonce_process(const secp256k1_context *ctx, secp256k1_musig_session *session, const secp256k1_musig_aggnonce *aggnonce, const unsigned char *msg32, const secp256k1_musig_keyagg_cache *keyagg_cache) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5)
Takes the aggregate nonce and creates a session that is required for signing and verification of part...
SECP256K1_API int secp256k1_musig_pubnonce_serialize(const secp256k1_context *ctx, unsigned char *out66, const secp256k1_musig_pubnonce *nonce) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Serialize a signer's public nonce.
static int secp256k1_musig_session_load(const secp256k1_context *ctx, secp256k1_musig_session_internal *session_i, const secp256k1_musig_session *session)
static void secp256k1_musig_secnonce_save(secp256k1_musig_secnonce *secnonce, const secp256k1_scalar *k, const secp256k1_ge *pk)
static int secp256k1_musig_nonce_gen_internal(const secp256k1_context *ctx, secp256k1_musig_secnonce *secnonce, secp256k1_musig_pubnonce *pubnonce, const unsigned char *input_nonce, const unsigned char *seckey, const secp256k1_pubkey *pubkey, const unsigned char *msg32, const secp256k1_musig_keyagg_cache *keyagg_cache, const unsigned char *extra_input32)
static void secp256k1_musig_nonce_process_internal(int *fin_nonce_parity, unsigned char *fin_nonce, secp256k1_scalar *b, secp256k1_ge *aggnonce_pts, const unsigned char *agg_pk32, const unsigned char *msg)
static const unsigned char secp256k1_musig_aggnonce_magic[4]
static void secp256k1_musig_ge_serialize_ext(unsigned char *out33, secp256k1_ge *ge)
static const unsigned char secp256k1_musig_secnonce_magic[4]
static int secp256k1_musig_sum_pubnonces(const secp256k1_context *ctx, secp256k1_gej *summed_pubnonces, const secp256k1_musig_pubnonce *const *pubnonces, size_t n_pubnonces)
static int secp256k1_musig_partial_sig_load(const secp256k1_context *ctx, secp256k1_scalar *s, const secp256k1_musig_partial_sig *sig)
static void secp256k1_musig_aggnonce_save(secp256k1_musig_aggnonce *nonce, const secp256k1_ge *ges)
static const unsigned char secp256k1_musig_partial_sig_magic[4]
static void secp256k1_nonce_function_musig(secp256k1_scalar *k, const unsigned char *session_secrand, const unsigned char *msg32, const unsigned char *seckey32, const unsigned char *pk33, const unsigned char *agg_pk32, const unsigned char *extra_input32)
static int secp256k1_musig_pubnonce_load(const secp256k1_context *ctx, secp256k1_ge *ges, const secp256k1_musig_pubnonce *nonce)
static const unsigned char secp256k1_musig_session_cache_magic[4]
static void secp256k1_musig_compute_noncehash_sha256_tagged(secp256k1_sha256 *sha)
static void secp256k1_nonce_function_musig_sha256_tagged_aux(secp256k1_sha256 *sha)
static int secp256k1_musig_secnonce_load(const secp256k1_context *ctx, secp256k1_scalar *k, secp256k1_ge *pk, const secp256k1_musig_secnonce *secnonce)
static void secp256k1_nonce_function_musig_sha256_tagged(secp256k1_sha256 *sha)
static void secp256k1_musig_partial_sig_save(secp256k1_musig_partial_sig *sig, secp256k1_scalar *s)
static void secp256k1_musig_secnonce_invalidate(const secp256k1_context *ctx, secp256k1_musig_secnonce *secnonce, int flag)
static void secp256k1_musig_session_save(secp256k1_musig_session *session, const secp256k1_musig_session_internal *session_i)
static void secp256k1_effective_nonce(secp256k1_gej *out_nonce, const secp256k1_ge *nonce_pts, const secp256k1_scalar *b)
static int secp256k1_musig_ge_parse_ext(secp256k1_ge *ge, const unsigned char *in33)
static void secp256k1_nonce_function_musig_helper(secp256k1_sha256 *sha, unsigned int prefix_size, const unsigned char *data, unsigned char len)
static void secp256k1_musig_partial_sign_clear(secp256k1_scalar *sk, secp256k1_scalar *k)
static const unsigned char secp256k1_musig_pubnonce_magic[4]
static void secp256k1_musig_compute_noncehash(unsigned char *noncehash, secp256k1_ge *aggnonce, const unsigned char *agg_pk32, const unsigned char *msg)
static int secp256k1_musig_aggnonce_load(const secp256k1_context *ctx, secp256k1_ge *ges, const secp256k1_musig_aggnonce *nonce)
static void secp256k1_musig_pubnonce_save(secp256k1_musig_pubnonce *nonce, const secp256k1_ge *ges)
secp256k1_ecmult_gen_context ecmult_gen_ctx
A group element in affine coordinates on the secp256k1 curve, or occasionally on an isomorphic curve ...
A group element of the secp256k1 curve, in jacobian coordinates.
Opaque data structure that holds a keypair consisting of a secret and a public key.
Opaque data structure that holds an aggregate public nonce.
This module implements BIP 327 "MuSig2 for BIP340-compatibleMulti-Signatures" (https://github....
Opaque data structure that holds a partial MuSig signature.
Opaque data structure that holds a signer's public nonce.
Opaque data structure that holds a signer's secret nonce.
secp256k1_scalar noncecoef
secp256k1_scalar challenge
unsigned char fin_nonce[32]
Opaque data structure that holds a MuSig session.
Opaque data structure that holds a parsed and valid public key.
A scalar modulo the group order of the secp256k1 curve.