Skip to content

Commit 486b9bb

Browse files
committed
Use a flags bitfield for compressed option to secp256k1_ec_pubkey_serialize and secp256k1_ec_privkey_export
1 parent 05732c5 commit 486b9bb

File tree

9 files changed

+27
-22
lines changed

9 files changed

+27
-22
lines changed

include/secp256k1.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ typedef int (*secp256k1_nonce_function_t)(
137137
# define SECP256K1_CONTEXT_VERIFY (1 << 0)
138138
# define SECP256K1_CONTEXT_SIGN (1 << 1)
139139

140+
/** Flag to pass to secp256k1_ec_pubkey_serialize and secp256k1_ec_privkey_export. */
141+
# define SECP256K1_EC_COMPRESSED (1 << 0)
142+
140143
/** Create a secp256k1 context object.
141144
*
142145
* Returns: a newly created context object.
@@ -243,14 +246,15 @@ SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(
243246
* size.
244247
* In: pubkey: a pointer to a secp256k1_pubkey_t containing an initialized
245248
* public key.
246-
* compressed: whether to serialize in compressed format.
249+
* flags: SECP256K1_EC_COMPRESSED if serialization should be in
250+
* compressed format.
247251
*/
248252
int secp256k1_ec_pubkey_serialize(
249253
const secp256k1_context_t* ctx,
250254
unsigned char *output,
251255
size_t *outputlen,
252256
const secp256k1_pubkey_t* pubkey,
253-
int compressed
257+
unsigned int flags
254258
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
255259

256260
/** Parse a DER ECDSA signature.
@@ -396,7 +400,8 @@ SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(
396400
* privkeylen: Pointer to an int where the length of the private key in
397401
* privkey will be stored.
398402
* In: seckey: pointer to a 32-byte secret key to export.
399-
* compressed: whether the key should be exported in compressed format.
403+
* flags: SECP256K1_EC_COMPRESSED if the key should be exported in
404+
* compressed format.
400405
*
401406
* This function is purely meant for compatibility with applications that
402407
* require BER encoded keys. When working with secp256k1-specific code, the
@@ -410,7 +415,7 @@ SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_export(
410415
unsigned char *privkey,
411416
size_t *privkeylen,
412417
const unsigned char *seckey,
413-
int compressed
418+
unsigned int flags
414419
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
415420

416421
/** Import a private key in DER format.

src/bench_recover.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void bench_recover(void* arg) {
2727
secp256k1_ecdsa_recoverable_signature_t sig;
2828
CHECK(secp256k1_ecdsa_recoverable_signature_parse_compact(data->ctx, &sig, data->sig, i % 2));
2929
CHECK(secp256k1_ecdsa_recover(data->ctx, &pubkey, &sig, data->msg));
30-
CHECK(secp256k1_ec_pubkey_serialize(data->ctx, pubkeyc, &pubkeylen, &pubkey, 1));
30+
CHECK(secp256k1_ec_pubkey_serialize(data->ctx, pubkeyc, &pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED));
3131
for (j = 0; j < 32; j++) {
3232
data->sig[j + 32] = data->msg[j]; /* Move former message to S. */
3333
data->msg[j] = data->sig[j]; /* Move former R to message. */

src/bench_schnorr_verify.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static void benchmark_schnorr_init(void* arg) {
3737
secp256k1_schnorr_sign(data->ctx, data->sigs[k].sig, data->msg, data->sigs[k].key, NULL, NULL);
3838
data->sigs[k].pubkeylen = 33;
3939
CHECK(secp256k1_ec_pubkey_create(data->ctx, &pubkey, data->sigs[k].key));
40-
CHECK(secp256k1_ec_pubkey_serialize(data->ctx, data->sigs[k].pubkey, &data->sigs[k].pubkeylen, &pubkey, 1));
40+
CHECK(secp256k1_ec_pubkey_serialize(data->ctx, data->sigs[k].pubkey, &data->sigs[k].pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED));
4141
}
4242
}
4343

src/bench_verify.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ int main(void) {
5454
CHECK(secp256k1_ecdsa_sign(data.ctx, &sig, data.msg, data.key, NULL, NULL));
5555
CHECK(secp256k1_ecdsa_signature_serialize_der(data.ctx, data.sig, &data.siglen, &sig));
5656
CHECK(secp256k1_ec_pubkey_create(data.ctx, &pubkey, data.key));
57-
CHECK(secp256k1_ec_pubkey_serialize(data.ctx, data.pubkey, &data.pubkeylen, &pubkey, 1) == 1);
57+
CHECK(secp256k1_ec_pubkey_serialize(data.ctx, data.pubkey, &data.pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
5858

5959
run_benchmark("ecdsa_verify", benchmark_verify, NULL, NULL, &data, 10, 20000);
6060

src/eckey.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
#include "ecmult_gen.h"
1616

1717
static int secp256k1_eckey_pubkey_parse(secp256k1_ge_t *elem, const unsigned char *pub, size_t size);
18-
static int secp256k1_eckey_pubkey_serialize(secp256k1_ge_t *elem, unsigned char *pub, size_t *size, int compressed);
18+
static int secp256k1_eckey_pubkey_serialize(secp256k1_ge_t *elem, unsigned char *pub, size_t *size, unsigned int flags);
1919

2020
static int secp256k1_eckey_privkey_parse(secp256k1_scalar_t *key, const unsigned char *privkey, size_t privkeylen);
21-
static int secp256k1_eckey_privkey_serialize(const secp256k1_ecmult_gen_context_t *ctx, unsigned char *privkey, size_t *privkeylen, const secp256k1_scalar_t *key, int compressed);
21+
static int secp256k1_eckey_privkey_serialize(const secp256k1_ecmult_gen_context_t *ctx, unsigned char *privkey, size_t *privkeylen, const secp256k1_scalar_t *key, unsigned int flags);
2222

2323
static int secp256k1_eckey_privkey_tweak_add(secp256k1_scalar_t *key, const secp256k1_scalar_t *tweak);
2424
static int secp256k1_eckey_pubkey_tweak_add(const secp256k1_ecmult_context_t *ctx, secp256k1_ge_t *key, const secp256k1_scalar_t *tweak);

src/eckey_impl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ static int secp256k1_eckey_pubkey_parse(secp256k1_ge_t *elem, const unsigned cha
3333
}
3434
}
3535

36-
static int secp256k1_eckey_pubkey_serialize(secp256k1_ge_t *elem, unsigned char *pub, size_t *size, int compressed) {
36+
static int secp256k1_eckey_pubkey_serialize(secp256k1_ge_t *elem, unsigned char *pub, size_t *size, unsigned int flags) {
3737
if (secp256k1_ge_is_infinity(elem)) {
3838
return 0;
3939
}
4040
secp256k1_fe_normalize_var(&elem->x);
4141
secp256k1_fe_normalize_var(&elem->y);
4242
secp256k1_fe_get_b32(&pub[1], &elem->x);
43-
if (compressed) {
43+
if (flags & SECP256K1_EC_COMPRESSED) {
4444
*size = 33;
4545
pub[0] = 0x02 | (secp256k1_fe_is_odd(&elem->y) ? 0x01 : 0x00);
4646
} else {
@@ -94,13 +94,13 @@ static int secp256k1_eckey_privkey_parse(secp256k1_scalar_t *key, const unsigned
9494
return !overflow;
9595
}
9696

97-
static int secp256k1_eckey_privkey_serialize(const secp256k1_ecmult_gen_context_t *ctx, unsigned char *privkey, size_t *privkeylen, const secp256k1_scalar_t *key, int compressed) {
97+
static int secp256k1_eckey_privkey_serialize(const secp256k1_ecmult_gen_context_t *ctx, unsigned char *privkey, size_t *privkeylen, const secp256k1_scalar_t *key, unsigned int flags) {
9898
secp256k1_gej_t rp;
9999
secp256k1_ge_t r;
100100
size_t pubkeylen = 0;
101101
secp256k1_ecmult_gen(ctx, &rp, key);
102102
secp256k1_ge_set_gej(&r, &rp);
103-
if (compressed) {
103+
if (flags & SECP256K1_EC_COMPRESSED) {
104104
static const unsigned char begin[] = {
105105
0x30,0x81,0xD3,0x02,0x01,0x01,0x04,0x20
106106
};

src/modules/ecdh/tests_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void test_ecdh_generator_basepoint(void) {
3131
CHECK(secp256k1_ecdh(ctx, output_ecdh, &point[0], s_b32) == 1);
3232
/* compute "explicitly" */
3333
CHECK(secp256k1_ec_pubkey_create(ctx, &point[1], s_b32) == 1);
34-
CHECK(secp256k1_ec_pubkey_serialize(ctx, point_ser, &point_ser_len, &point[1], 1) == 1);
34+
CHECK(secp256k1_ec_pubkey_serialize(ctx, point_ser, &point_ser_len, &point[1], SECP256K1_EC_COMPRESSED) == 1);
3535
CHECK(point_ser_len == sizeof(point_ser));
3636
secp256k1_sha256_initialize(&sha);
3737
secp256k1_sha256_write(&sha, point_ser, point_ser_len);

src/secp256k1.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,12 @@ int secp256k1_ec_pubkey_parse(const secp256k1_context_t* ctx, secp256k1_pubkey_t
154154
return 1;
155155
}
156156

157-
int secp256k1_ec_pubkey_serialize(const secp256k1_context_t* ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey_t* pubkey, int compressed) {
157+
int secp256k1_ec_pubkey_serialize(const secp256k1_context_t* ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey_t* pubkey, unsigned int flags) {
158158
secp256k1_ge_t Q;
159159

160160
(void)ctx;
161161
return (secp256k1_pubkey_load(ctx, &Q, pubkey) &&
162-
secp256k1_eckey_pubkey_serialize(&Q, output, outputlen, compressed));
162+
secp256k1_eckey_pubkey_serialize(&Q, output, outputlen, flags));
163163
}
164164

165165
static void secp256k1_ecdsa_signature_load(const secp256k1_context_t* ctx, secp256k1_scalar_t* r, secp256k1_scalar_t* s, const secp256k1_ecdsa_signature_t* sig) {
@@ -438,7 +438,7 @@ int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context_t* ctx, secp256k1_pubk
438438
return ret;
439439
}
440440

441-
int secp256k1_ec_privkey_export(const secp256k1_context_t* ctx, unsigned char *privkey, size_t *privkeylen, const unsigned char *seckey, int compressed) {
441+
int secp256k1_ec_privkey_export(const secp256k1_context_t* ctx, unsigned char *privkey, size_t *privkeylen, const unsigned char *seckey, unsigned int flags) {
442442
secp256k1_scalar_t key;
443443
int ret = 0;
444444
VERIFY_CHECK(ctx != NULL);
@@ -448,7 +448,7 @@ int secp256k1_ec_privkey_export(const secp256k1_context_t* ctx, unsigned char *p
448448
ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx));
449449

450450
secp256k1_scalar_set_b32(&key, seckey, NULL);
451-
ret = secp256k1_eckey_privkey_serialize(&ctx->ecmult_gen_ctx, privkey, privkeylen, &key, compressed);
451+
ret = secp256k1_eckey_privkey_serialize(&ctx->ecmult_gen_ctx, privkey, privkeylen, &key, flags);
452452
secp256k1_scalar_clear(&key);
453453
return ret;
454454
}

src/tests.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,7 +1871,7 @@ void test_ecdsa_end_to_end(void) {
18711871
CHECK(secp256k1_ec_pubkey_parse(ctx, &pubkey, pubkeyc, pubkeyclen) == 1);
18721872

18731873
/* Verify private key import and export. */
1874-
CHECK(secp256k1_ec_privkey_export(ctx, seckey, &seckeylen, privkey, secp256k1_rand32() % 2) == 1);
1874+
CHECK(secp256k1_ec_privkey_export(ctx, seckey, &seckeylen, privkey, (secp256k1_rand32() % 2) == 1) ? SECP256K1_EC_COMPRESSED : 0);
18751875
CHECK(secp256k1_ec_privkey_import(ctx, privkey2, seckey, seckeylen) == 1);
18761876
CHECK(memcmp(privkey, privkey2, 32) == 0);
18771877

@@ -1979,7 +1979,7 @@ void test_random_pubkeys(void) {
19791979
size_t size = len;
19801980
firstb = in[0];
19811981
/* If the pubkey can be parsed, it should round-trip... */
1982-
CHECK(secp256k1_eckey_pubkey_serialize(&elem, out, &size, len == 33));
1982+
CHECK(secp256k1_eckey_pubkey_serialize(&elem, out, &size, (len == 33) ? SECP256K1_EC_COMPRESSED : 0));
19831983
CHECK(size == len);
19841984
CHECK(memcmp(&in[1], &out[1], len-1) == 0);
19851985
/* ... except for the type of hybrid inputs. */
@@ -2156,7 +2156,7 @@ void test_ecdsa_edge_cases(void) {
21562156
size_t outlen = 300;
21572157
CHECK(!secp256k1_ec_privkey_export(ctx, privkey, &outlen, seckey, 0));
21582158
outlen = 300;
2159-
CHECK(!secp256k1_ec_privkey_export(ctx, privkey, &outlen, seckey, 1));
2159+
CHECK(!secp256k1_ec_privkey_export(ctx, privkey, &outlen, seckey, SECP256K1_EC_COMPRESSED));
21602160
}
21612161
}
21622162

@@ -2171,7 +2171,7 @@ EC_KEY *get_openssl_key(const secp256k1_scalar_t *key) {
21712171
const unsigned char* pbegin = privkey;
21722172
int compr = secp256k1_rand32() & 1;
21732173
EC_KEY *ec_key = EC_KEY_new_by_curve_name(NID_secp256k1);
2174-
CHECK(secp256k1_eckey_privkey_serialize(&ctx->ecmult_gen_ctx, privkey, &privkeylen, key, compr));
2174+
CHECK(secp256k1_eckey_privkey_serialize(&ctx->ecmult_gen_ctx, privkey, &privkeylen, key, compr ? SECP256K1_EC_COMPRESSED : 0));
21752175
CHECK(d2i_ECPrivateKey(&ec_key, &pbegin, privkeylen));
21762176
CHECK(EC_KEY_check_key(ec_key));
21772177
return ec_key;

0 commit comments

Comments
 (0)