Skip to content

Commit 873a453

Browse files
committed
Merge pull request bitcoin#250
210ffed Use separate in and out pointers in `secp256k1_ec_pubkey_decompress` (Andrew Poelstra)
2 parents 91eb0da + 210ffed commit 873a453

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

include/secp256k1.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -262,18 +262,20 @@ SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(
262262

263263
/** Decompress a public key.
264264
* In: ctx: pointer to a context object (cannot be NULL)
265-
* In/Out: pubkey: pointer to a 65-byte array to put the decompressed public key.
266-
* It must contain a 33-byte or 65-byte public key already (cannot be NULL)
267-
* pubkeylen: pointer to the size of the public key pointed to by pubkey (cannot be NULL)
268-
* It will be updated to reflect the new size.
269-
* Returns: 0: pubkey was invalid
270-
* 1: pubkey was valid, and was replaced with its decompressed version
265+
* In: pubkeyin: pointer to a 33-byte or 65-byte public key (cannot be NULL)
266+
* In/Out: pubkeyout: pointer to a 65-byte array to put the decompressed public key (cannot be NULL)
267+
* May alias pubkeyin.
268+
* pubkeylen: pointer to the size of the public key pointed to by pubkeyin (cannot be NULL)
269+
* It will be updated to reflect the size of the public key in pubkeyout.
270+
* Returns: 0: pubkeyin was invalid
271+
* 1: pubkeyin was valid, and pubkeyout is its decompressed version
271272
*/
272273
SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_decompress(
273274
const secp256k1_context_t* ctx,
274-
unsigned char *pubkey,
275+
const unsigned char *pubkeyin,
276+
unsigned char *pubkeyout,
275277
int *pubkeylen
276-
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
278+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
277279

278280
/** Export a private key in DER format.
279281
* In: ctx: pointer to a context object, initialized for signing (cannot be NULL)

src/secp256k1.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,16 @@ int secp256k1_ec_pubkey_create(const secp256k1_context_t* ctx, unsigned char *pu
271271
return ret;
272272
}
273273

274-
int secp256k1_ec_pubkey_decompress(const secp256k1_context_t* ctx, unsigned char *pubkey, int *pubkeylen) {
274+
int secp256k1_ec_pubkey_decompress(const secp256k1_context_t* ctx, const unsigned char *pubkeyin, unsigned char *pubkeyout, int *pubkeylen) {
275275
secp256k1_ge_t p;
276276
int ret = 0;
277-
DEBUG_CHECK(pubkey != NULL);
277+
DEBUG_CHECK(pubkeyin != NULL);
278+
DEBUG_CHECK(pubkeyout != NULL);
278279
DEBUG_CHECK(pubkeylen != NULL);
279280
(void)ctx;
280281

281-
if (secp256k1_eckey_pubkey_parse(&p, pubkey, *pubkeylen)) {
282-
ret = secp256k1_eckey_pubkey_serialize(&p, pubkey, pubkeylen, 0);
282+
if (secp256k1_eckey_pubkey_parse(&p, pubkeyin, *pubkeylen)) {
283+
ret = secp256k1_eckey_pubkey_serialize(&p, pubkeyout, pubkeylen, 0);
283284
}
284285
return ret;
285286
}

src/tests.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1511,7 +1511,19 @@ void test_ecdsa_end_to_end(void) {
15111511
CHECK(secp256k1_ec_seckey_verify(ctx, privkey) == 1);
15121512
CHECK(secp256k1_ec_pubkey_create(ctx, pubkey, &pubkeylen, privkey, (secp256k1_rand32() & 3) != 0) == 1);
15131513
if (secp256k1_rand32() & 1) {
1514-
CHECK(secp256k1_ec_pubkey_decompress(ctx, pubkey, &pubkeylen));
1514+
unsigned char pubkey2[65] = {0};
1515+
int pubkey2len = pubkeylen;
1516+
/* Decompress into a new array */
1517+
CHECK(secp256k1_ec_pubkey_decompress(ctx, pubkey, pubkey2, &pubkey2len));
1518+
/* Check that the key was changed iff it was originally compressed */
1519+
if (pubkeylen == 65) {
1520+
CHECK(memcmp(pubkey, pubkey2, 65) == 0);
1521+
} else {
1522+
CHECK(memcmp(pubkey, pubkey2, 65) != 0);
1523+
}
1524+
/* Decompress in place */
1525+
CHECK(secp256k1_ec_pubkey_decompress(ctx, pubkey, pubkey, &pubkeylen));
1526+
CHECK(memcmp(pubkey, pubkey2, 65) == 0);
15151527
}
15161528
CHECK(secp256k1_ec_pubkey_verify(ctx, pubkey, pubkeylen));
15171529

0 commit comments

Comments
 (0)