Skip to content

Commit 1823594

Browse files
committed
Verify compressed argument in secp256k1_eckey_pubkey_serialize
Due to similarity to the public API function `secp256k1_ec_pubkey_serialize`, public API flags like `SECP256K1_EC_COMPRESSED` are sometimes mistakingly passed to newly proposed code (this is currently the case for several modules in secp256k1-zkp, see BlockstreamResearch/secp256k1-zkp#300). which is currently not detected. To avoid this in the future, a VERIFY_CHECK is added to check that the `compressed` argument is either 0 or 1.
1 parent 8deef00 commit 1823594

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/eckey_impl.h

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "eckey.h"
1111

12+
#include "util.h"
1213
#include "scalar.h"
1314
#include "field.h"
1415
#include "group.h"
@@ -35,6 +36,8 @@ static int secp256k1_eckey_pubkey_parse(secp256k1_ge *elem, const unsigned char
3536
}
3637

3738
static int secp256k1_eckey_pubkey_serialize(secp256k1_ge *elem, unsigned char *pub, size_t *size, int compressed) {
39+
VERIFY_CHECK(compressed == 0 || compressed == 1);
40+
3841
if (secp256k1_ge_is_infinity(elem)) {
3942
return 0;
4043
}

src/secp256k1.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ int secp256k1_ec_pubkey_serialize(const secp256k1_context* ctx, unsigned char *o
280280
ARG_CHECK(pubkey != NULL);
281281
ARG_CHECK((flags & SECP256K1_FLAGS_TYPE_MASK) == SECP256K1_FLAGS_TYPE_COMPRESSION);
282282
if (secp256k1_pubkey_load(ctx, &Q, pubkey)) {
283-
ret = secp256k1_eckey_pubkey_serialize(&Q, output, &len, flags & SECP256K1_FLAGS_BIT_COMPRESSION);
283+
ret = secp256k1_eckey_pubkey_serialize(&Q, output, &len, !!(flags & SECP256K1_FLAGS_BIT_COMPRESSION));
284284
if (ret) {
285285
*outputlen = len;
286286
}

0 commit comments

Comments
 (0)