Skip to content

Commit 2bfb82b

Browse files
committed
Merge pull request bitcoin#351
06aeea5 Turn secp256k1_ec_pubkey_serialize outlen to in/out (Pieter Wuille)
2 parents 970164d + 06aeea5 commit 2bfb82b

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

contrib/lax_der_privatekey_parsing.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *privkey,
7979
memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
8080
memcpy(ptr, key32, 32); ptr += 32;
8181
memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
82+
pubkeylen = 33;
8283
secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED);
8384
ptr += pubkeylen;
8485
*privkeylen = ptr - privkey;
@@ -103,10 +104,10 @@ int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *privkey,
103104
memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
104105
memcpy(ptr, key32, 32); ptr += 32;
105106
memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
107+
pubkeylen = 65;
106108
secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
107109
ptr += pubkeylen;
108110
*privkeylen = ptr - privkey;
109111
}
110112
return 1;
111113
}
112-

include/secp256k1.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,17 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(
264264
/** Serialize a pubkey object into a serialized byte sequence.
265265
*
266266
* Returns: 1 always.
267-
* Args: ctx: a secp256k1 context object.
268-
* Out: output: a pointer to a 65-byte (if compressed==0) or 33-byte (if
269-
* compressed==1) byte array to place the serialized key in.
270-
* outputlen: a pointer to an integer which will contain the serialized
271-
* size.
272-
* In: pubkey: a pointer to a secp256k1_pubkey containing an initialized
273-
* public key.
274-
* flags: SECP256K1_EC_COMPRESSED if serialization should be in
275-
* compressed format, otherwise SECP256K1_EC_UNCOMPRESSED.
267+
* Args: ctx: a secp256k1 context object.
268+
* Out: output: a pointer to a 65-byte (if compressed==0) or 33-byte (if
269+
* compressed==1) byte array to place the serialized key
270+
* in.
271+
* In/Out: outputlen: a pointer to an integer which is initially set to the
272+
* size of output, and is overwritten with the written
273+
* size.
274+
* In: pubkey: a pointer to a secp256k1_pubkey containing an
275+
* initialized public key.
276+
* flags: SECP256K1_EC_COMPRESSED if serialization should be in
277+
* compressed format, otherwise SECP256K1_EC_UNCOMPRESSED.
276278
*/
277279
SECP256K1_API int secp256k1_ec_pubkey_serialize(
278280
const secp256k1_context* ctx,

src/bench_verify.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ int main(void) {
5858
CHECK(secp256k1_ecdsa_sign(data.ctx, &sig, data.msg, data.key, NULL, NULL));
5959
CHECK(secp256k1_ecdsa_signature_serialize_der(data.ctx, data.sig, &data.siglen, &sig));
6060
CHECK(secp256k1_ec_pubkey_create(data.ctx, &pubkey, data.key));
61+
data.pubkeylen = 33;
6162
CHECK(secp256k1_ec_pubkey_serialize(data.ctx, data.pubkey, &data.pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
6263

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

src/secp256k1.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ int secp256k1_ec_pubkey_serialize(const secp256k1_context* ctx, unsigned char *o
173173
(void)ctx;
174174
VERIFY_CHECK(ctx != NULL);
175175
ARG_CHECK(outputlen != NULL);
176+
ARG_CHECK(*outputlen >= ((flags & SECP256K1_FLAGS_BIT_COMPRESSION) ? 33 : 65));
176177
len = *outputlen;
177178
*outputlen = 0;
178179
ARG_CHECK(output != NULL);

0 commit comments

Comments
 (0)