Skip to content

Commit 99fd963

Browse files
committed
Add secp256k1_ec_pubkey_compress(), with test similar to the related decompress() function.
1 parent 873a453 commit 99fd963

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

include/secp256k1.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,27 @@ SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(
260260
int compressed
261261
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
262262

263+
/** Compress a public key.
264+
* In: ctx: pointer to a context object (cannot be NULL)
265+
* pubkeyin: pointer to a 33-byte or 65-byte public key (cannot be NULL)
266+
* Out: pubkeyout: pointer to a 33-byte array to put the compressed 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 compressed version
272+
*/
273+
SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_compress(
274+
const secp256k1_context_t* ctx,
275+
const unsigned char *pubkeyin,
276+
unsigned char *pubkeyout,
277+
int *pubkeylen
278+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
279+
263280
/** Decompress a public key.
264281
* In: ctx: pointer to a context object (cannot be NULL)
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)
282+
* pubkeyin: pointer to a 33-byte or 65-byte public key (cannot be NULL)
283+
* Out: pubkeyout: pointer to a 65-byte array to put the decompressed public key (cannot be NULL)
267284
* May alias pubkeyin.
268285
* pubkeylen: pointer to the size of the public key pointed to by pubkeyin (cannot be NULL)
269286
* It will be updated to reflect the size of the public key in pubkeyout.

src/secp256k1.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,21 @@ int secp256k1_ec_pubkey_decompress(const secp256k1_context_t* ctx, const unsigne
285285
return ret;
286286
}
287287

288+
int secp256k1_ec_pubkey_compress(const secp256k1_context_t* ctx, const unsigned char *pubkeyin, unsigned char *pubkeyout, int *pubkeylen) {
289+
secp256k1_ge_t p;
290+
int ret = 0;
291+
DEBUG_CHECK(pubkeyin != NULL);
292+
DEBUG_CHECK(pubkeyout != NULL);
293+
DEBUG_CHECK(pubkeylen != NULL);
294+
(void)ctx;
295+
296+
if (secp256k1_eckey_pubkey_parse(&p, pubkeyin, *pubkeylen)) {
297+
ret = secp256k1_eckey_pubkey_serialize(&p, pubkeyout, pubkeylen, 1);
298+
}
299+
300+
return ret;
301+
}
302+
288303
int secp256k1_ec_privkey_tweak_add(const secp256k1_context_t* ctx, unsigned char *seckey, const unsigned char *tweak) {
289304
secp256k1_scalar_t term;
290305
secp256k1_scalar_t sec;

src/tests.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,14 +1512,22 @@ void test_ecdsa_end_to_end(void) {
15121512
CHECK(secp256k1_ec_pubkey_create(ctx, pubkey, &pubkeylen, privkey, (secp256k1_rand32() & 3) != 0) == 1);
15131513
if (secp256k1_rand32() & 1) {
15141514
unsigned char pubkey2[65] = {0};
1515-
int pubkey2len = pubkeylen;
1515+
unsigned char pubkey3RE[33] = {0};
1516+
int pubkey2len = pubkeylen, pubkey3len = pubkeylen;
1517+
15161518
/* Decompress into a new array */
15171519
CHECK(secp256k1_ec_pubkey_decompress(ctx, pubkey, pubkey2, &pubkey2len));
1520+
1521+
/* Compress into a new array */
1522+
CHECK(secp256k1_ec_pubkey_compress(ctx, pubkey, pubkey3RE, &pubkey3len));
1523+
15181524
/* Check that the key was changed iff it was originally compressed */
15191525
if (pubkeylen == 65) {
1520-
CHECK(memcmp(pubkey, pubkey2, 65) == 0);
1526+
CHECK(memcmp(pubkey, pubkey2, 65) == 0); /* Values should be the same */
1527+
CHECK(memcmp(pubkey3RE, pubkey, 33) != 0); /* Means it should have been compressed */
15211528
} else {
1522-
CHECK(memcmp(pubkey, pubkey2, 65) != 0);
1529+
CHECK(memcmp(pubkey, pubkey2, 65) != 0); /* Should have been decompressed */
1530+
CHECK(memcmp(pubkey3RE, pubkey, 33) == 0); /* Therefore compressed key should equal initial pubkey */
15231531
}
15241532
/* Decompress in place */
15251533
CHECK(secp256k1_ec_pubkey_decompress(ctx, pubkey, pubkey, &pubkeylen));

0 commit comments

Comments
 (0)