Skip to content

Commit f532bdc

Browse files
committed
Merge bitcoin#459: Add pubkey prefix constants to include/secp256k1.h
bc61b91 add pubkey prefix constants to include/secp256k1.h (Andrew Poelstra) Pull request description: In future multisig implementations we will need to pass nonces around, which are algebraically pubkeys but should not be decodable as pubkeys. The way to do this is to change the prefix byte from the ordinary 0x02/0x03 to something else. However, some forks (notably `secp256k1-zkp`) have started using some bytes for their own encodings, and if we continue to use hardcoded constants the risk of conflict is increased. This commit puts the prefixes used by the main library into the `include/secp256k1.h` so that the constants we're using will at least be in a standard easy-to-reference place. Tree-SHA512: 37fa25be5074b7c519a9c69421320a62f32a3818f144254eb57f96c6657b993fc01962a5c670574275d1c59b095a6c89e60736123f032d6736907284eac526d7
2 parents cac7c55 + bc61b91 commit f532bdc

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

include/secp256k1.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ typedef int (*secp256k1_nonce_function)(
159159
#define SECP256K1_EC_COMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION | SECP256K1_FLAGS_BIT_COMPRESSION)
160160
#define SECP256K1_EC_UNCOMPRESSED (SECP256K1_FLAGS_TYPE_COMPRESSION)
161161

162+
/** Prefix byte used to tag various encoded curvepoints for specific purposes */
163+
#define SECP256K1_TAG_PUBKEY_EVEN 0x02
164+
#define SECP256K1_TAG_PUBKEY_ODD 0x03
165+
#define SECP256K1_TAG_PUBKEY_UNCOMPRESSED 0x04
166+
#define SECP256K1_TAG_PUBKEY_HYBRID_EVEN 0x06
167+
#define SECP256K1_TAG_PUBKEY_HYBRID_ODD 0x07
168+
162169
/** Create a secp256k1 context object.
163170
*
164171
* Returns: a newly created context object.

src/eckey_impl.h

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

1717
static int secp256k1_eckey_pubkey_parse(secp256k1_ge *elem, const unsigned char *pub, size_t size) {
18-
if (size == 33 && (pub[0] == 0x02 || pub[0] == 0x03)) {
18+
if (size == 33 && (pub[0] == SECP256K1_TAG_PUBKEY_EVEN || pub[0] == SECP256K1_TAG_PUBKEY_ODD)) {
1919
secp256k1_fe x;
20-
return secp256k1_fe_set_b32(&x, pub+1) && secp256k1_ge_set_xo_var(elem, &x, pub[0] == 0x03);
20+
return secp256k1_fe_set_b32(&x, pub+1) && secp256k1_ge_set_xo_var(elem, &x, pub[0] == SECP256K1_TAG_PUBKEY_ODD);
2121
} else if (size == 65 && (pub[0] == 0x04 || pub[0] == 0x06 || pub[0] == 0x07)) {
2222
secp256k1_fe x, y;
2323
if (!secp256k1_fe_set_b32(&x, pub+1) || !secp256k1_fe_set_b32(&y, pub+33)) {
2424
return 0;
2525
}
2626
secp256k1_ge_set_xy(elem, &x, &y);
27-
if ((pub[0] == 0x06 || pub[0] == 0x07) && secp256k1_fe_is_odd(&y) != (pub[0] == 0x07)) {
27+
if ((pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_EVEN || pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_ODD) &&
28+
secp256k1_fe_is_odd(&y) != (pub[0] == SECP256K1_TAG_PUBKEY_HYBRID_ODD)) {
2829
return 0;
2930
}
3031
return secp256k1_ge_is_valid_var(elem);
@@ -42,10 +43,10 @@ static int secp256k1_eckey_pubkey_serialize(secp256k1_ge *elem, unsigned char *p
4243
secp256k1_fe_get_b32(&pub[1], &elem->x);
4344
if (compressed) {
4445
*size = 33;
45-
pub[0] = 0x02 | (secp256k1_fe_is_odd(&elem->y) ? 0x01 : 0x00);
46+
pub[0] = secp256k1_fe_is_odd(&elem->y) ? SECP256K1_TAG_PUBKEY_ODD : SECP256K1_TAG_PUBKEY_EVEN;
4647
} else {
4748
*size = 65;
48-
pub[0] = 0x04;
49+
pub[0] = SECP256K1_TAG_PUBKEY_UNCOMPRESSED;
4950
secp256k1_fe_get_b32(&pub[33], &elem->y);
5051
}
5152
return 1;

0 commit comments

Comments
 (0)