Skip to content

Commit b673a43

Browse files
jonasnickreal-or-random
authored andcommitted
musig: new upstream def of VERIFY_CHECK (empty in non-VERIFY)
Remove explicity VERIFY_CHECKs in keyaggcoef_internal since normalization should be checked in the fe_* functions.
1 parent cd17368 commit b673a43

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

src/modules/musig/keyagg_impl.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,12 @@ static void secp256k1_musig_keyaggcoef_sha256(secp256k1_sha256 *sha) {
131131
/* Compute KeyAgg coefficient which is constant 1 for the second pubkey and
132132
* otherwise tagged_hash(pk_hash, x) where pk_hash is the hash of public keys.
133133
* second_pk is the point at infinity in case there is no second_pk. Assumes
134-
* that pk is not the point at infinity and that the coordinates of pk and
134+
* that pk is not the point at infinity and that the Y-coordinates of pk and
135135
* second_pk are normalized. */
136136
static void secp256k1_musig_keyaggcoef_internal(secp256k1_scalar *r, const unsigned char *pk_hash, secp256k1_ge *pk, const secp256k1_ge *second_pk) {
137137
secp256k1_sha256 sha;
138138

139139
VERIFY_CHECK(!secp256k1_ge_is_infinity(pk));
140-
VERIFY_CHECK(pk->x.normalized && pk->y.normalized);
141-
VERIFY_CHECK(secp256k1_ge_is_infinity(second_pk) || (second_pk->x.normalized && second_pk->y.normalized));
142140

143141
if (!secp256k1_ge_is_infinity(second_pk)
144142
&& secp256k1_fe_equal(&pk->x, &second_pk->x)
@@ -151,9 +149,13 @@ static void secp256k1_musig_keyaggcoef_internal(secp256k1_scalar *r, const unsig
151149
secp256k1_musig_keyaggcoef_sha256(&sha);
152150
secp256k1_sha256_write(&sha, pk_hash, 32);
153151
ret = secp256k1_eckey_pubkey_serialize(pk, buf, &buflen, 1);
152+
#ifdef VERIFY
154153
/* Serialization does not fail since the pk is not the point at infinity
155154
* (according to this function's precondition). */
156155
VERIFY_CHECK(ret && buflen == sizeof(buf));
156+
#else
157+
(void) ret;
158+
#endif
157159
secp256k1_sha256_write(&sha, buf, sizeof(buf));
158160
secp256k1_sha256_finalize(&sha, buf);
159161
secp256k1_scalar_set_b32(r, buf, NULL);
@@ -178,9 +180,13 @@ static int secp256k1_musig_pubkey_agg_callback(secp256k1_scalar *sc, secp256k1_g
178180
secp256k1_musig_pubkey_agg_ecmult_data *ctx = (secp256k1_musig_pubkey_agg_ecmult_data *) data;
179181
int ret;
180182
ret = secp256k1_pubkey_load(ctx->ctx, pt, ctx->pks[idx]);
183+
#ifdef VERIFY
181184
/* pubkey_load can't fail because the same pks have already been loaded in
182185
* `musig_compute_pk_hash` (and we test this). */
183186
VERIFY_CHECK(ret);
187+
#else
188+
(void) ret;
189+
#endif
184190
secp256k1_musig_keyaggcoef_internal(sc, ctx->pk_hash, pt, &ctx->second_pk);
185191
return 1;
186192
}

src/modules/musig/session_impl.h

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,12 @@ int secp256k1_musig_pubnonce_serialize(const secp256k1_context* ctx, unsigned ch
174174
int ret;
175175
size_t size = 33;
176176
ret = secp256k1_eckey_pubkey_serialize(&ge[i], &out66[33*i], &size, 1);
177+
#ifdef VERIFY
177178
/* serialize must succeed because the point was just loaded */
178179
VERIFY_CHECK(ret && size == 33);
180+
#else
181+
(void) ret;
182+
#endif
179183
}
180184
return 1;
181185
}
@@ -258,16 +262,6 @@ int secp256k1_musig_partial_sig_parse(const secp256k1_context* ctx, secp256k1_mu
258262
return 1;
259263
}
260264

261-
/* Normalizes the x-coordinate of the given group element. */
262-
static int secp256k1_xonly_ge_serialize(unsigned char *output32, secp256k1_ge *ge) {
263-
if (secp256k1_ge_is_infinity(ge)) {
264-
return 0;
265-
}
266-
secp256k1_fe_normalize_var(&ge->x);
267-
secp256k1_fe_get_b32(output32, &ge->x);
268-
return 1;
269-
}
270-
271265
/* Write optional inputs into the hash */
272266
static void secp256k1_nonce_function_musig_helper(secp256k1_sha256 *sha, unsigned int prefix_size, const unsigned char *data, unsigned char len) {
273267
unsigned char zero[7] = { 0 };
@@ -364,22 +358,25 @@ int secp256k1_musig_nonce_gen(const secp256k1_context* ctx, secp256k1_musig_secn
364358
}
365359

366360
if (keyagg_cache != NULL) {
367-
int ret_tmp;
368361
if (!secp256k1_keyagg_cache_load(ctx, &cache_i, keyagg_cache)) {
369362
return 0;
370363
}
371-
ret_tmp = secp256k1_xonly_ge_serialize(aggpk_ser, &cache_i.pk);
372-
/* Serialization can not fail because the loaded point can not be infinity. */
373-
VERIFY_CHECK(ret_tmp);
364+
/* The loaded point cache_i.pk can not be the point at infinity. */
365+
secp256k1_fe_get_b32(aggpk_ser, &cache_i.pk.x);
374366
aggpk_ser_ptr = aggpk_ser;
375367
}
376368
if (!secp256k1_pubkey_load(ctx, &pk, pubkey)) {
377369
return 0;
378370
}
379371
pk_serialize_success = secp256k1_eckey_pubkey_serialize(&pk, pk_ser, &pk_ser_len, SECP256K1_EC_COMPRESSED);
372+
373+
#ifdef VERIFY
380374
/* A pubkey cannot be the point at infinity */
381375
VERIFY_CHECK(pk_serialize_success);
382376
VERIFY_CHECK(pk_ser_len == sizeof(pk_ser));
377+
#else
378+
(void) pk_serialize_success;
379+
#endif
383380

384381
secp256k1_nonce_function_musig(k, session_id32, msg32, seckey, pk_ser, aggpk_ser_ptr, extra_input32);
385382
VERIFY_CHECK(!secp256k1_scalar_is_zero(&k[0]));
@@ -460,7 +457,6 @@ static int secp256k1_musig_nonce_process_internal(int *fin_nonce_parity, unsigne
460457
secp256k1_ge fin_nonce_pt;
461458
secp256k1_gej fin_nonce_ptj;
462459
secp256k1_ge aggnonce[2];
463-
int ret;
464460

465461
secp256k1_ge_set_gej(&aggnonce[0], &aggnoncej[0]);
466462
secp256k1_ge_set_gej(&aggnonce[1], &aggnoncej[1]);
@@ -476,9 +472,9 @@ static int secp256k1_musig_nonce_process_internal(int *fin_nonce_parity, unsigne
476472
if (secp256k1_ge_is_infinity(&fin_nonce_pt)) {
477473
fin_nonce_pt = secp256k1_ge_const_g;
478474
}
479-
ret = secp256k1_xonly_ge_serialize(fin_nonce, &fin_nonce_pt);
480-
/* Can't fail since fin_nonce_pt is not infinity */
481-
VERIFY_CHECK(ret);
475+
/* fin_nonce_pt is not the point at infinity */
476+
secp256k1_fe_normalize_var(&fin_nonce_pt.x);
477+
secp256k1_fe_get_b32(fin_nonce, &fin_nonce_pt.x);
482478
secp256k1_fe_normalize_var(&fin_nonce_pt.y);
483479
*fin_nonce_parity = secp256k1_fe_is_odd(&fin_nonce_pt.y);
484480
return 1;

0 commit comments

Comments
 (0)