Skip to content

Commit e023651

Browse files
committed
refactor: introduce and use _eckey_pubkey_serialize33 helper
1 parent c8206b1 commit e023651

File tree

5 files changed

+16
-42
lines changed

5 files changed

+16
-42
lines changed

src/eckey.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
static int secp256k1_eckey_pubkey_parse(secp256k1_ge *elem, const unsigned char *pub, size_t size);
1818
static int secp256k1_eckey_pubkey_serialize(secp256k1_ge *elem, unsigned char *pub, size_t *size, int compressed);
19+
static void secp256k1_eckey_pubkey_serialize33(secp256k1_ge *elem, unsigned char *pub33);
1920

2021
static int secp256k1_eckey_privkey_tweak_add(secp256k1_scalar *key, const secp256k1_scalar *tweak);
2122
static int secp256k1_eckey_pubkey_tweak_add(secp256k1_ge *key, const secp256k1_scalar *tweak);

src/eckey_impl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ static int secp256k1_eckey_pubkey_serialize(secp256k1_ge *elem, unsigned char *p
5555
return 1;
5656
}
5757

58+
static void secp256k1_eckey_pubkey_serialize33(secp256k1_ge *elem, unsigned char *pub33) {
59+
size_t ser_size = 33;
60+
int ser_ret = secp256k1_eckey_pubkey_serialize(elem, pub33, &ser_size, 1);
61+
#ifdef VERIFY
62+
VERIFY_CHECK(ser_ret && ser_size == 33);
63+
#else
64+
(void)ser_ret;
65+
#endif
66+
}
67+
5868
static int secp256k1_eckey_privkey_tweak_add(secp256k1_scalar *key, const secp256k1_scalar *tweak) {
5969
secp256k1_scalar_add(key, key, tweak);
6070
return !secp256k1_scalar_is_zero(key);

src/modules/ellswift/main_impl.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -406,19 +406,12 @@ int secp256k1_ellswift_encode(const secp256k1_context *ctx, unsigned char *ell64
406406
if (secp256k1_pubkey_load(ctx, &p, pubkey)) {
407407
secp256k1_fe t;
408408
unsigned char p64[64] = {0};
409-
size_t ser_size;
410-
int ser_ret;
411409
secp256k1_sha256 hash;
412410

413411
/* Set up hasher state; the used RNG is H(pubkey || "\x00"*31 || rnd32 || cnt++), using
414412
* BIP340 tagged hash with tag "secp256k1_ellswift_encode". */
415413
secp256k1_ellswift_sha256_init_encode(&hash);
416-
ser_ret = secp256k1_eckey_pubkey_serialize(&p, p64, &ser_size, 1);
417-
#ifdef VERIFY
418-
VERIFY_CHECK(ser_ret && ser_size == 33);
419-
#else
420-
(void)ser_ret;
421-
#endif
414+
secp256k1_eckey_pubkey_serialize33(&p, p64);
422415
secp256k1_sha256_write(&hash, p64, sizeof(p64));
423416
secp256k1_sha256_write(&hash, rnd32, 32);
424417

src/modules/musig/keyagg_impl.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,11 @@ static void secp256k1_musig_keyaggcoef_internal(secp256k1_scalar *r, const unsig
124124
} else {
125125
secp256k1_sha256 sha;
126126
unsigned char buf[33];
127-
size_t buflen = sizeof(buf);
128-
int ret;
129127
secp256k1_musig_keyaggcoef_sha256(&sha);
130128
secp256k1_sha256_write(&sha, pks_hash, 32);
131-
ret = secp256k1_eckey_pubkey_serialize(pk, buf, &buflen, 1);
132-
#ifdef VERIFY
133129
/* Serialization does not fail since the pk is not the point at infinity
134130
* (according to this function's precondition). */
135-
VERIFY_CHECK(ret && buflen == sizeof(buf));
136-
#else
137-
(void) ret;
138-
#endif
131+
secp256k1_eckey_pubkey_serialize33(pk, buf);
139132
secp256k1_sha256_write(&sha, buf, sizeof(buf));
140133
secp256k1_sha256_finalize(&sha, buf);
141134
secp256k1_scalar_set_b32(r, buf, NULL);

src/modules/musig/session_impl.h

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,8 @@ static void secp256k1_musig_ge_serialize_ext(unsigned char *out33, secp256k1_ge*
2525
if (secp256k1_ge_is_infinity(ge)) {
2626
memset(out33, 0, 33);
2727
} else {
28-
int ret;
29-
size_t size = 33;
30-
ret = secp256k1_eckey_pubkey_serialize(ge, out33, &size, 1);
31-
#ifdef VERIFY
3228
/* Serialize must succeed because the point is not at infinity */
33-
VERIFY_CHECK(ret && size == 33);
34-
#else
35-
(void) ret;
36-
#endif
29+
secp256k1_eckey_pubkey_serialize33(ge, out33);
3730
}
3831
}
3932

@@ -224,15 +217,8 @@ int secp256k1_musig_pubnonce_serialize(const secp256k1_context* ctx, unsigned ch
224217
return 0;
225218
}
226219
for (i = 0; i < 2; i++) {
227-
int ret;
228-
size_t size = 33;
229-
ret = secp256k1_eckey_pubkey_serialize(&ges[i], &out66[33*i], &size, 1);
230-
#ifdef VERIFY
231220
/* serialize must succeed because the point was just loaded */
232-
VERIFY_CHECK(ret && size == 33);
233-
#else
234-
(void) ret;
235-
#endif
221+
secp256k1_eckey_pubkey_serialize33(&ges[i], &out66[33*i]);
236222
}
237223
return 1;
238224
}
@@ -398,11 +384,9 @@ static int secp256k1_musig_nonce_gen_internal(const secp256k1_context* ctx, secp
398384
secp256k1_gej nonce_ptj[2];
399385
int i;
400386
unsigned char pk_ser[33];
401-
size_t pk_ser_len = sizeof(pk_ser);
402387
unsigned char aggpk_ser[32];
403388
unsigned char *aggpk_ser_ptr = NULL;
404389
secp256k1_ge pk;
405-
int pk_serialize_success;
406390
int ret = 1;
407391

408392
ARG_CHECK(pubnonce != NULL);
@@ -429,15 +413,8 @@ static int secp256k1_musig_nonce_gen_internal(const secp256k1_context* ctx, secp
429413
if (!secp256k1_pubkey_load(ctx, &pk, pubkey)) {
430414
return 0;
431415
}
432-
pk_serialize_success = secp256k1_eckey_pubkey_serialize(&pk, pk_ser, &pk_ser_len, 1);
433-
434-
#ifdef VERIFY
435416
/* A pubkey cannot be the point at infinity */
436-
VERIFY_CHECK(pk_serialize_success);
437-
VERIFY_CHECK(pk_ser_len == sizeof(pk_ser));
438-
#else
439-
(void) pk_serialize_success;
440-
#endif
417+
secp256k1_eckey_pubkey_serialize33(&pk, pk_ser);
441418

442419
secp256k1_nonce_function_musig(k, input_nonce, msg32, seckey, pk_ser, aggpk_ser_ptr, extra_input32);
443420
VERIFY_CHECK(!secp256k1_scalar_is_zero(&k[0]));

0 commit comments

Comments
 (0)