6#ifndef SECP256K1_MODULE_MUSIG_SESSION_IMPL_H
7#define SECP256K1_MODULE_MUSIG_SESSION_IMPL_H
26 secp256k1_scalar_get_b32(&secnonce->
data[4], &
k[0]);
27 secp256k1_scalar_get_b32(&secnonce->
data[36], &
k[1]);
37 secp256k1_declassify(ctx, &is_zero,
sizeof(is_zero));
40 secp256k1_scalar_set_b32(&
k[0], &secnonce->
data[4], NULL);
41 secp256k1_scalar_set_b32(&
k[1], &secnonce->
data[36], NULL);
55 secp256k1_declassify(ctx, &secnonce->
data[68], 64);
65 for (i = 0; i < 2; i++) {
76 for (i = 0; i < 2; i++) {
87 for (i = 0; i < 2; i++) {
96 for (i = 0; i < 2; i++) {
113 unsigned char *ptr = session->
data;
121 secp256k1_scalar_get_b32(ptr, &session_i->
noncecoef);
123 secp256k1_scalar_get_b32(ptr, &session_i->
challenge);
125 secp256k1_scalar_get_b32(ptr, &session_i->
s_part);
129 const unsigned char *ptr = session->
data;
137 secp256k1_scalar_set_b32(&session_i->
noncecoef, ptr, NULL);
139 secp256k1_scalar_set_b32(&session_i->
challenge, ptr, NULL);
141 secp256k1_scalar_set_b32(&session_i->
s_part, ptr, NULL);
149 secp256k1_scalar_get_b32(&sig->
data[4], s);
156 secp256k1_scalar_set_b32(s, &sig->
data[4], &overflow);
170 for (i = 0; i < 2; i++) {
188 memset(out66, 0, 66);
194 for (i = 0; i < 2; i++) {
209 for (i = 0; i < 2; i++) {
224 memset(out66, 0, 66);
230 for (i = 0; i < 2; i++) {
245 memset(sig, 0,
sizeof(*sig));
247 secp256k1_scalar_set_b32(&tmp, in32, &overflow);
261 memcpy(out32, &sig->
data[4], 32);
267 unsigned char zero[7] = { 0 };
286 static const uint32_t midstate[8] = {
287 0xa19e884bul, 0xf463fe7eul, 0x2f18f9a2ul, 0xbeb0f9fful,
288 0x0f37e8b0ul, 0x06ebd26ful, 0xe3b243d2ul, 0x522fb150ul
296 static const uint32_t midstate[8] = {
297 0x07101b64ul, 0x18003414ul, 0x0391bc43ul, 0x0e6258eeul,
298 0x29d26b72ul, 0x8343937eul, 0xb7a0a4fbul, 0xff568a30ul
305 unsigned char rand[32];
307 unsigned char msg_present;
309 if (seckey32 != NULL) {
313 for (i = 0; i < 32; i++) {
314 rand[i] ^= seckey32[i];
317 memcpy(rand, session_secrand,
sizeof(rand));
324 msg_present = msg32 != NULL;
331 for (i = 0; i < 2; i++) {
332 unsigned char buf[32];
336 secp256k1_scalar_set_b32(&
k[i], buf, NULL);
351 unsigned char pk_ser[33];
352 unsigned char aggpk_ser[32];
353 unsigned char *aggpk_ser_ptr = NULL;
358 memset(pubnonce, 0,
sizeof(*pubnonce));
363 if (seckey != NULL) {
365 ret &= secp256k1_scalar_set_b32_seckey(&sk, seckey);
366 secp256k1_scalar_clear(&sk);
369 if (keyagg_cache != NULL) {
376 aggpk_ser_ptr = aggpk_ser;
378 if (!secp256k1_pubkey_load(ctx, &pk, pubkey)) {
391 for (i = 0; i < 2; i++) {
393 secp256k1_scalar_clear(&
k[i]);
398 for (i = 0; i < 2; i++) {
399 secp256k1_gej_clear(&nonce_ptj[i]);
402 for (i = 0; i < 2; i++) {
403 secp256k1_declassify(ctx, &nonce_pts[i],
sizeof(nonce_pts[i]));
416 memset(secnonce, 0,
sizeof(*secnonce));
425 secp256k1_declassify(ctx, &
ret,
sizeof(
ret));
440 unsigned char buf[32] = { 0 };
441 unsigned char seckey[32];
447 memset(secnonce, 0,
sizeof(*secnonce));
469 secp256k1_gej_set_infinity(&summed_pubnonces[0]);
470 secp256k1_gej_set_infinity(&summed_pubnonces[1]);
472 for (i = 0; i < n_pubnonces; i++) {
477 for (j = 0; j < 2; j++) {
478 secp256k1_gej_add_ge_var(&summed_pubnonces[j], &summed_pubnonces[j], &nonce_pts[j], NULL);
493 for (i = 0; i < n_pubnonces; i++) {
500 secp256k1_ge_set_all_gej_var(aggnonce_pts, aggnonce_ptsj, 2);
508 static const uint32_t midstate[8] = {
509 0x2c7d5a45ul, 0x06bf7e53ul, 0x89be68a6ul, 0x971254c0ul,
510 0x60ac12d2ul, 0x72846dcdul, 0x6c81212ful, 0xde7a2500ul
517 unsigned char buf[33];
522 for (i = 0; i < 2; i++) {
535 secp256k1_gej_set_ge(&tmp, &nonce_pts[1]);
536 secp256k1_ecmult(out_nonce, &tmp, b, NULL);
537 secp256k1_gej_add_ge_var(out_nonce, out_nonce, &nonce_pts[0], NULL);
541 unsigned char noncehash[32];
546 secp256k1_scalar_set_b32(b, noncehash, NULL);
549 secp256k1_ge_set_gej(&fin_nonce_pt, &fin_nonce_ptj);
550 if (secp256k1_ge_is_infinity(&fin_nonce_pt)) {
551 fin_nonce_pt = secp256k1_ge_const_g;
563 unsigned char fin_nonce[32];
565 unsigned char agg_pk32[32];
586 secp256k1_scalar_set_int(&session_i.
s_part, 0);
587 if (!secp256k1_scalar_is_zero(&cache_i.
tweak)) {
589 secp256k1_scalar_mul(&e_tmp, &session_i.
challenge, &cache_i.
tweak);
591 secp256k1_scalar_negate(&e_tmp, &e_tmp);
601 secp256k1_scalar_clear(sk);
602 secp256k1_scalar_clear(&
k[0]);
603 secp256k1_scalar_clear(&
k[1]);
649 secp256k1_scalar_negate(&sk, &sk);
654 secp256k1_scalar_mul(&sk, &sk, &mu);
662 secp256k1_scalar_negate(&
k[0], &
k[0]);
663 secp256k1_scalar_negate(&
k[1], &
k[1]);
667 secp256k1_scalar_mul(&s, &session_i.
challenge, &sk);
668 secp256k1_scalar_mul(&
k[1], &session_i.
noncecoef, &
k[1]);
669 secp256k1_scalar_add(&
k[0], &
k[0], &
k[1]);
670 secp256k1_scalar_add(&s, &s, &
k[0]);
704 if (!secp256k1_pubkey_load(ctx, &pkp, pubkey)) {
714 secp256k1_scalar_mul(&e, &session_i.
challenge, &mu);
721 secp256k1_scalar_negate(&e, &e);
728 secp256k1_scalar_negate(&s, &s);
729 secp256k1_gej_set_ge(&pkj, &pkp);
730 secp256k1_ecmult(&tmp, &pkj, &e, &s);
732 secp256k1_gej_neg(&rj, &rj);
734 secp256k1_gej_add_var(&tmp, &tmp, &rj, NULL);
736 return secp256k1_gej_is_infinity(&tmp);
748 for (i = 0; i < n_sigs; i++) {
755 for (i = 0; i < n_sigs; i++) {
760 secp256k1_scalar_add(&session_i.
s_part, &session_i.
s_part, &term);
762 secp256k1_scalar_get_b32(&sig64[32], &session_i.
s_part);
763 memcpy(&sig64[0], session_i.
fin_nonce, 32);
static int secp256k1_ecmult_gen_context_is_built(const secp256k1_ecmult_gen_context *ecmult_gen_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 int secp256k1_ge_parse_ext33(secp256k1_ge *ge, const unsigned char *in33)
Outputs the point at infinity if the given byte array is all zero, otherwise attempts to parse compre...
static void secp256k1_ge_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 void secp256k1_ge_from_bytes_ext(secp256k1_ge *ge, const unsigned char *data)
Convert a 64-byte array into a group element.
static void secp256k1_ge_serialize_ext33(unsigned char *out33, secp256k1_ge *ge)
Outputs 33 zero bytes if the given group element is the point at infinity and otherwise outputs the c...
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 int secp256k1_ge_parse(secp256k1_ge *elem, const unsigned char *pub, size_t size)
Parse a group element from a 33-byte compressed or 65-byte uncompressed public key.
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.
struct secp256k1_context_struct secp256k1_context
Unless explicitly stated all pointer arguments must not be NULL.
static void secp256k1_musig_keyaggcoef(const secp256k1_hash_ctx *hash_ctx, secp256k1_scalar *r, const secp256k1_keyagg_cache_internal *cache_i, secp256k1_ge *pk)
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_schnorrsig_challenge(const secp256k1_hash_ctx *hash_ctx, secp256k1_scalar *e, const unsigned char *r32, const unsigned char *msg, size_t msglen, const unsigned char *pubkey32)
static void secp256k1_sha256_finalize(const secp256k1_hash_ctx *hash_ctx, secp256k1_sha256 *hash, unsigned char *out32)
static void secp256k1_sha256_initialize_midstate(secp256k1_sha256 *hash, uint64_t bytes, const uint32_t state[8])
static void secp256k1_sha256_write(const secp256k1_hash_ctx *hash_ctx, 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)
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_compute_noncehash(const secp256k1_hash_ctx *hash_ctx, unsigned char *noncehash, secp256k1_ge *aggnonce, const unsigned char *agg_pk32, const unsigned char *msg)
static const unsigned char secp256k1_musig_aggnonce_magic[4]
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 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 void secp256k1_nonce_function_musig_helper(const secp256k1_hash_ctx *hash_ctx, secp256k1_sha256 *sha, unsigned int prefix_size, const unsigned char *data, unsigned char len)
static void secp256k1_musig_nonce_process_internal(const secp256k1_context *ctx, 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 void secp256k1_musig_partial_sign_clear(secp256k1_scalar *sk, secp256k1_scalar *k)
static const unsigned char secp256k1_musig_pubnonce_magic[4]
static int secp256k1_musig_aggnonce_load(const secp256k1_context *ctx, secp256k1_ge *ges, const secp256k1_musig_aggnonce *nonce)
static void secp256k1_nonce_function_musig(const secp256k1_hash_ctx *hash_ctx, 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 void secp256k1_musig_pubnonce_save(secp256k1_musig_pubnonce *nonce, const secp256k1_ge *ges)
secp256k1_ecmult_gen_context ecmult_gen_ctx
secp256k1_hash_ctx hash_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.