6#ifndef SECP256K1_MODULE_MUSIG_SESSION_IMPL_H
7#define SECP256K1_MODULE_MUSIG_SESSION_IMPL_H
11#include "../../../include/secp256k1.h"
12#include "../../../include/secp256k1_extrakeys.h"
13#include "../../../include/secp256k1_musig.h"
17#include "../../eckey.h"
18#include "../../hash.h"
19#include "../../scalar.h"
20#include "../../util.h"
43 unsigned char zeros[33] = { 0 };
97 for (i = 0; i < 2; i++) {
108 for (i = 0; i < 2; i++) {
119 for (i = 0; i < 2; i++) {
128 for (i = 0; i < 2; i++) {
145 unsigned char *ptr = session->
data;
161 const unsigned char *ptr = session->
data;
202 for (i = 0; i < 2; i++) {
220 memset(out66, 0, 66);
226 for (i = 0; i < 2; i++) {
248 for (i = 0; i < 2; i++) {
263 memset(out66, 0, 66);
269 for (i = 0; i < 2; i++) {
284 memset(sig, 0,
sizeof(*sig));
300 memcpy(out32, &sig->
data[4], 32);
306 unsigned char zero[7] = { 0 };
326 sha->
s[0] = 0xa19e884bul;
327 sha->
s[1] = 0xf463fe7eul;
328 sha->
s[2] = 0x2f18f9a2ul;
329 sha->
s[3] = 0xbeb0f9fful;
330 sha->
s[4] = 0x0f37e8b0ul;
331 sha->
s[5] = 0x06ebd26ful;
332 sha->
s[6] = 0xe3b243d2ul;
333 sha->
s[7] = 0x522fb150ul;
341 sha->
s[0] = 0x07101b64ul;
342 sha->
s[1] = 0x18003414ul;
343 sha->
s[2] = 0x0391bc43ul;
344 sha->
s[3] = 0x0e6258eeul;
345 sha->
s[4] = 0x29d26b72ul;
346 sha->
s[5] = 0x8343937eul;
347 sha->
s[6] = 0xb7a0a4fbul;
348 sha->
s[7] = 0xff568a30ul;
352static 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) {
354 unsigned char rand[32];
356 unsigned char msg_present;
358 if (seckey32 != NULL) {
362 for (i = 0; i < 32; i++) {
363 rand[i] ^= seckey32[i];
366 memcpy(rand, session_secrand,
sizeof(rand));
373 msg_present = msg32 != NULL;
380 for (i = 0; i < 2; i++) {
381 unsigned char buf[32];
399 unsigned char pk_ser[33];
400 size_t pk_ser_len =
sizeof(pk_ser);
401 unsigned char aggpk_ser[32];
402 unsigned char *aggpk_ser_ptr = NULL;
404 int pk_serialize_success;
408 memset(pubnonce, 0,
sizeof(*pubnonce));
413 if (seckey != NULL) {
419 if (keyagg_cache != NULL) {
426 aggpk_ser_ptr = aggpk_ser;
438 (void) pk_serialize_success;
447 for (i = 0; i < 2; i++) {
466 memset(secnonce, 0,
sizeof(*secnonce));
490 unsigned char buf[32] = { 0 };
491 unsigned char seckey[32];
497 memset(secnonce, 0,
sizeof(*secnonce));
524 for (i = 0; i < n_pubnonces; i++) {
529 for (j = 0; j < 2; j++) {
556 sha->
s[0] = 0x2c7d5a45ul;
557 sha->
s[1] = 0x06bf7e53ul;
558 sha->
s[2] = 0x89be68a6ul;
559 sha->
s[3] = 0x971254c0ul;
560 sha->
s[4] = 0x60ac12d2ul;
561 sha->
s[5] = 0x72846dcdul;
562 sha->
s[6] = 0x6c81212ful;
563 sha->
s[7] = 0xde7a2500ul;
569 unsigned char buf[33];
574 for (i = 0; i < 2; i++) {
593 unsigned char noncehash[32];
615 unsigned char fin_nonce[32];
617 unsigned char agg_pk32[32];
674 memset(secnonce, 0,
sizeof(*secnonce));
804 for (i = 0; i < n_sigs; i++) {
812 memcpy(&sig64[0], session_i.
fin_nonce, 32);
static int secp256k1_eckey_pubkey_parse(secp256k1_ge *elem, const unsigned char *pub, size_t size)
static int secp256k1_eckey_pubkey_serialize(secp256k1_ge *elem, unsigned char *pub, size_t *size, int compressed)
static void secp256k1_ecmult(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_scalar *na, const secp256k1_scalar *ng)
Double multiply: R = na*A + ng*G.
static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp256k1_gej *r, const secp256k1_scalar *a)
Multiply with the generator: R = a*G.
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_gej_clear(secp256k1_gej *r)
Clear a secp256k1_gej to prevent leaking sensitive information.
static void secp256k1_gej_set_infinity(secp256k1_gej *r)
Set a group element (jacobian) equal to the point at infinity.
static int secp256k1_gej_is_infinity(const secp256k1_gej *a)
Check whether a group element is the point at infinity.
static void secp256k1_gej_add_ge_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, secp256k1_fe *rzr)
Set r equal to the sum of a and b (with b given in affine coordinates).
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 void secp256k1_gej_add_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_gej *b, secp256k1_fe *rzr)
Set r equal to the sum of a and b.
static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a)
Set a group element equal to another which is given in jacobian coordinates.
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 int secp256k1_ge_is_infinity(const secp256k1_ge *a)
Check whether a group element is the point at infinity.
static void secp256k1_ge_set_infinity(secp256k1_ge *r)
Set a group element (affine) equal to the point at infinity.
static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a, size_t len)
Set a batch of group elements equal to the inputs given in jacobian coordinates.
static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a)
Set a group element (jacobian) equal to another which is given in affine coordinates.
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_gej_neg(secp256k1_gej *r, const secp256k1_gej *a)
Set r equal to the inverse of a (i.e., mirrored around the X axis)
static void secp256k1_ge_from_bytes(secp256k1_ge *r, const unsigned char *buf)
Convert a 64-byte array into group element.
static const secp256k1_ge secp256k1_ge_const_g
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_scalar_set_b32(secp256k1_scalar *r, const unsigned char *bin, int *overflow)
Set a scalar from a big endian byte array.
static int secp256k1_scalar_set_b32_seckey(secp256k1_scalar *r, const unsigned char *bin)
Set a scalar from a big endian byte array and returns 1 if it is a valid seckey and 0 otherwise.
static int secp256k1_scalar_is_zero(const secp256k1_scalar *a)
Check whether a scalar equals zero.
static void secp256k1_scalar_set_int(secp256k1_scalar *r, unsigned int v)
Set a scalar to an unsigned integer.
static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar *a)
Convert a scalar to a byte array.
static int secp256k1_scalar_add(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
Add two scalars together (modulo the group order).
static void secp256k1_scalar_mul(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
Multiply two scalars (modulo the group order).
static void secp256k1_scalar_negate(secp256k1_scalar *r, const secp256k1_scalar *a)
Compute the complement of a scalar (modulo the group order).
static void secp256k1_scalar_clear(secp256k1_scalar *r)
Clear a scalar to prevent the leak of sensitive data.
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 int secp256k1_memcmp_var(const void *s1, const void *s2, size_t n)
Semantics like memcmp.
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)
static SECP256K1_INLINE void secp256k1_memclear(void *ptr, size_t len)
#define VERIFY_CHECK(cond)
static SECP256K1_INLINE void secp256k1_memczero(void *s, size_t len, int flag)
static SECP256K1_INLINE void secp256k1_declassify(const secp256k1_context *ctx, const void *p, size_t len)
static int secp256k1_pubkey_load(const secp256k1_context *ctx, secp256k1_ge *ge, const secp256k1_pubkey *pubkey)
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)
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)
Produces a partial signature.
static const unsigned char secp256k1_musig_aggnonce_magic[4]
static void secp256k1_musig_ge_serialize_ext(unsigned char *out33, secp256k1_ge *ge)
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)
Starts a signing session by generating a nonce.
int secp256k1_musig_aggnonce_serialize(const secp256k1_context *ctx, unsigned char *out66, const secp256k1_musig_aggnonce *nonce)
Serialize an aggregate public nonce.
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)
int secp256k1_musig_partial_sig_serialize(const secp256k1_context *ctx, unsigned char *out32, const secp256k1_musig_partial_sig *sig)
Serialize a MuSig partial signature.
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)
Alternative way to generate a nonce and start a signing session.
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)
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)
Aggregates partial signatures.
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)
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)
Verifies an individual signer's partial signature.
static void secp256k1_musig_session_save(secp256k1_musig_session *session, const secp256k1_musig_session_internal *session_i)
int secp256k1_musig_aggnonce_parse(const secp256k1_context *ctx, secp256k1_musig_aggnonce *nonce, const unsigned char *in66)
Parse an aggregate public nonce.
static void secp256k1_effective_nonce(secp256k1_gej *out_nonce, const secp256k1_ge *nonce_pts, const secp256k1_scalar *b)
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)
Takes the aggregate nonce and creates a session that is required for signing and verification of part...
static int secp256k1_musig_ge_parse_ext(secp256k1_ge *ge, const unsigned char *in33)
static int secp256k1_musig_session_load(const secp256k1_context *ctx, secp256k1_musig_session_internal *session_i, const secp256k1_musig_session *session)
int secp256k1_musig_nonce_agg(const secp256k1_context *ctx, secp256k1_musig_aggnonce *aggnonce, const secp256k1_musig_pubnonce *const *pubnonces, size_t n_pubnonces)
Aggregates the nonces of all signers into a single nonce.
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]
int secp256k1_musig_partial_sig_parse(const secp256k1_context *ctx, secp256k1_musig_partial_sig *sig, const unsigned char *in32)
Parse a MuSig partial signature.
static void secp256k1_musig_compute_noncehash(unsigned char *noncehash, secp256k1_ge *aggnonce, const unsigned char *agg_pk32, const unsigned char *msg)
int secp256k1_musig_pubnonce_parse(const secp256k1_context *ctx, secp256k1_musig_pubnonce *nonce, const unsigned char *in66)
Parse a signer's public nonce.
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)
int secp256k1_musig_pubnonce_serialize(const secp256k1_context *ctx, unsigned char *out66, const secp256k1_musig_pubnonce *nonce)
Serialize a signer's public nonce.
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-compatible Multi-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.