Skip to content

Commit 96201b4

Browse files
jonasnickreal-or-random
authored andcommitted
Require message in musig protocol in an earlier state. In particular,
remove the set_msg function and require the message in get_public_nonce at the latest.
1 parent 4fd0d56 commit 96201b4

File tree

5 files changed

+82
-100
lines changed

5 files changed

+82
-100
lines changed

include/secp256k1_musig.h

+8-21
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ SECP256K1_API int secp256k1_musig_pubkey_combine(
151151
* NULL). If a non-unique session_id32 was given then a partial
152152
* signature will LEAK THE SECRET KEY.
153153
* msg32: the 32-byte message to be signed. Shouldn't be NULL unless you
154-
* require sharing public nonces before the message is known
154+
* require sharing nonce commitments before the message is known
155155
* because it reduces nonce misuse resistance. If NULL, must be
156-
* set with `musig_session_set_msg` before signing and verifying.
156+
* set with `musig_session_get_public_nonce`.
157157
* combined_pk: the combined public key of all signers (cannot be NULL)
158158
* pk_hash32: the 32-byte hash of the signers' individual keys (cannot be
159159
* NULL)
@@ -190,14 +190,17 @@ SECP256K1_API int secp256k1_musig_session_initialize(
190190
* In: commitments: array of 32-byte nonce commitments (cannot be NULL)
191191
* n_commitments: the length of commitments and signers array. Must be the total
192192
* number of signers participating in the MuSig.
193+
* msg32: the 32-byte message to be signed. Must be NULL if already
194+
* set with `musig_session_initialize` otherwise can not be NULL.
193195
*/
194196
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_session_get_public_nonce(
195197
const secp256k1_context* ctx,
196198
secp256k1_musig_session *session,
197199
secp256k1_musig_session_signer_data *signers,
198200
secp256k1_pubkey *nonce,
199201
const unsigned char *const *commitments,
200-
size_t n_commitments
202+
size_t n_commitments,
203+
const unsigned char *msg32
201204
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5);
202205

203206
/** Initializes a verifier session that can be used for verifying nonce commitments
@@ -209,9 +212,7 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_session_get_publi
209212
* Out: session: the session structure to initialize (cannot be NULL)
210213
* signers: an array of signers' data to be initialized. Array length must
211214
* equal to `n_signers`(cannot be NULL)
212-
* In: msg32: the 32-byte message to be signed If NULL, must be set with
213-
* `musig_session_set_msg` before using the session for verifying
214-
* partial signatures.
215+
* In: msg32: the 32-byte message to be signed (cannot be NULL)
215216
* combined_pk: the combined public key of all signers (cannot be NULL)
216217
* pk_hash32: the 32-byte hash of the signers' individual keys (cannot be NULL)
217218
* commitments: array of 32-byte nonce commitments. Array length must equal to
@@ -229,7 +230,7 @@ SECP256K1_API int secp256k1_musig_session_initialize_verifier(
229230
const unsigned char *pk_hash32,
230231
const unsigned char *const *commitments,
231232
size_t n_signers
232-
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(6) SECP256K1_ARG_NONNULL(7);
233+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(6) SECP256K1_ARG_NONNULL(7);
233234

234235
/** Checks a signer's public nonce against a commitment to said nonce, and update
235236
* data structure if they match
@@ -275,20 +276,6 @@ SECP256K1_API int secp256k1_musig_session_combine_nonces(
275276
const secp256k1_pubkey *adaptor
276277
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
277278

278-
/** Sets the message of a session if previously unset
279-
*
280-
* Returns 1 if the message was not set yet and is now successfully set
281-
* 0 otherwise
282-
* Args: ctx: pointer to a context object (cannot be NULL)
283-
* session: the session structure to update with the message (cannot be NULL)
284-
* In: msg32: the 32-byte message to be signed (cannot be NULL)
285-
*/
286-
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_session_set_msg(
287-
const secp256k1_context* ctx,
288-
secp256k1_musig_session *session,
289-
const unsigned char *msg32
290-
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
291-
292279
/** Serialize a MuSig partial signature or adaptor signature
293280
*
294281
* Returns: 1 when the signature could be serialized, 0 otherwise

src/modules/musig/example.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ int sign(const secp256k1_context* ctx, unsigned char seckeys[][32], const secp25
7777
/* Communication round 1: Exchange nonce commitments */
7878
for (i = 0; i < N_SIGNERS; i++) {
7979
/* Set nonce commitments in the signer data and get the own public nonce */
80-
if (!secp256k1_musig_session_get_public_nonce(ctx, &musig_session[i], signer_data[i], &nonce[i], nonce_commitment_ptr, N_SIGNERS)) {
80+
if (!secp256k1_musig_session_get_public_nonce(ctx, &musig_session[i], signer_data[i], &nonce[i], nonce_commitment_ptr, N_SIGNERS, NULL)) {
8181
return 0;
8282
}
8383
}

src/modules/musig/main_impl.h

+12-19
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ int secp256k1_musig_session_initialize(const secp256k1_context* ctx, secp256k1_m
211211
return 1;
212212
}
213213

214-
int secp256k1_musig_session_get_public_nonce(const secp256k1_context* ctx, secp256k1_musig_session *session, secp256k1_musig_session_signer_data *signers, secp256k1_pubkey *nonce, const unsigned char *const *commitments, size_t n_commitments) {
214+
int secp256k1_musig_session_get_public_nonce(const secp256k1_context* ctx, secp256k1_musig_session *session, secp256k1_musig_session_signer_data *signers, secp256k1_pubkey *nonce, const unsigned char *const *commitments, size_t n_commitments, const unsigned char *msg32) {
215215
secp256k1_sha256 sha;
216216
unsigned char nonce_commitments_hash[32];
217217
size_t i;
@@ -222,6 +222,10 @@ int secp256k1_musig_session_get_public_nonce(const secp256k1_context* ctx, secp2
222222
ARG_CHECK(signers != NULL);
223223
ARG_CHECK(nonce != NULL);
224224
ARG_CHECK(commitments != NULL);
225+
/* If the message was not set during initialization it must be set now. */
226+
ARG_CHECK(!(!session->msg_is_set && msg32 == NULL));
227+
/* The message can only be set once. */
228+
ARG_CHECK(!(session->msg_is_set && msg32 != NULL));
225229

226230
if (!session->has_secret_data || n_commitments != session->n_signers) {
227231
return 0;
@@ -230,6 +234,10 @@ int secp256k1_musig_session_get_public_nonce(const secp256k1_context* ctx, secp2
230234
ARG_CHECK(commitments[i] != NULL);
231235
}
232236

237+
if (msg32 != NULL) {
238+
memcpy(session->msg, msg32, 32);
239+
session->msg_is_set = 1;
240+
}
233241
secp256k1_sha256_initialize(&sha);
234242
for (i = 0; i < n_commitments; i++) {
235243
memcpy(signers[i].nonce_commitment, commitments[i], 32);
@@ -254,6 +262,7 @@ int secp256k1_musig_session_initialize_verifier(const secp256k1_context* ctx, se
254262
VERIFY_CHECK(ctx != NULL);
255263
ARG_CHECK(session != NULL);
256264
ARG_CHECK(signers != NULL);
265+
ARG_CHECK(msg32 != NULL);
257266
ARG_CHECK(combined_pk != NULL);
258267
ARG_CHECK(pk_hash32 != NULL);
259268
ARG_CHECK(commitments != NULL);
@@ -278,11 +287,8 @@ int secp256k1_musig_session_initialize_verifier(const secp256k1_context* ctx, se
278287

279288
memcpy(session->pk_hash, pk_hash32, 32);
280289
session->nonce_is_set = 0;
281-
session->msg_is_set = 0;
282-
if (msg32 != NULL) {
283-
memcpy(session->msg, msg32, 32);
284-
session->msg_is_set = 1;
285-
}
290+
session->msg_is_set = 1;
291+
memcpy(session->msg, msg32, 32);
286292
session->has_secret_data = 0;
287293
session->nonce_commitments_hash_is_set = 0;
288294

@@ -373,19 +379,6 @@ int secp256k1_musig_session_combine_nonces(const secp256k1_context* ctx, secp256
373379
return 1;
374380
}
375381

376-
int secp256k1_musig_session_set_msg(const secp256k1_context* ctx, secp256k1_musig_session *session, const unsigned char *msg32) {
377-
VERIFY_CHECK(ctx != NULL);
378-
ARG_CHECK(session != NULL);
379-
ARG_CHECK(msg32 != NULL);
380-
381-
if (session->msg_is_set) {
382-
return 0;
383-
}
384-
memcpy(session->msg, msg32, 32);
385-
session->msg_is_set = 1;
386-
return 1;
387-
}
388-
389382
int secp256k1_musig_partial_signature_serialize(const secp256k1_context* ctx, unsigned char *out32, const secp256k1_musig_partial_signature* sig) {
390383
VERIFY_CHECK(ctx != NULL);
391384
ARG_CHECK(out32 != NULL);

src/modules/musig/musig.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ signature process, which is also a supported mode) acts as follows.
9191
length-32 byte arrays which can be communicated however is communicated.
9292
3. Once all signers nonce commitments have been received, the signer records
9393
these commitments with the function `secp256k1_musig_session_get_public_nonce`.
94+
If the signer did not provide a message to `secp256k1_musig_session_initialize`,
95+
a message must be provided now.
9496
This function updates in place
9597
- the session state `session`
9698
- the array of signer data `signers`
@@ -111,9 +113,6 @@ signature process, which is also a supported mode) acts as follows.
111113
- the array of signer data `signers`
112114
It outputs an auxiliary integer `nonce_is_negated` and has an auxiliary input
113115
`adaptor`. Both of these may be set to NULL for ordinary signing purposes.
114-
If the signer did not provide a message to `secp256k1_musig_session_initialize`,
115-
a message must be provided now by calling `secp256k1_musig_session_set_msg` which
116-
updates the session state in place.
117116
6. The signer computes a partial signature `s_i` using the function
118117
`secp256k1_musig_partial_sign` which takes the session state as input and
119118
partial signature as output.

0 commit comments

Comments
 (0)