Skip to content

Commit 63179d0

Browse files
committed
Scope the ECDSA constant sizes to CPubKey / CKey classes
1 parent 1ce9f0a commit 63179d0

File tree

4 files changed

+43
-41
lines changed

4 files changed

+43
-41
lines changed

src/key.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,13 @@ static int ec_privkey_import_der(const secp256k1_context* ctx, unsigned char *ou
8585
* <http://www.secg.org/sec1-v2.pdf>. The optional parameters and publicKey fields are
8686
* included.
8787
*
88-
* privkey must point to an output buffer of length at least PRIVATE_KEY_SIZE bytes.
88+
* privkey must point to an output buffer of length at least CKey::PRIVATE_KEY_SIZE bytes.
8989
* privkeylen must initially be set to the size of the privkey buffer. Upon return it
9090
* will be set to the number of bytes used in the buffer.
9191
* key32 must point to a 32-byte raw private key.
9292
*/
9393
static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *privkey, size_t *privkeylen, const unsigned char *key32, int compressed) {
94-
assert(*privkeylen >= PRIVATE_KEY_SIZE);
95-
static_assert(
96-
PRIVATE_KEY_SIZE >= COMPRESSED_PRIVATE_KEY_SIZE,
97-
"COMPRESSED_PRIVATE_KEY_SIZE is larger than PRIVATE_KEY_SIZE");
94+
assert(*privkeylen >= CKey::PRIVATE_KEY_SIZE);
9895
secp256k1_pubkey pubkey;
9996
size_t pubkeylen = 0;
10097
if (!secp256k1_ec_pubkey_create(ctx, &pubkey, key32)) {
@@ -120,11 +117,11 @@ static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *pr
120117
memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
121118
memcpy(ptr, key32, 32); ptr += 32;
122119
memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
123-
pubkeylen = COMPRESSED_PUBLIC_KEY_SIZE;
120+
pubkeylen = CPubKey::COMPRESSED_PUBLIC_KEY_SIZE;
124121
secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED);
125122
ptr += pubkeylen;
126123
*privkeylen = ptr - privkey;
127-
assert(*privkeylen == COMPRESSED_PRIVATE_KEY_SIZE);
124+
assert(*privkeylen == CKey::COMPRESSED_PRIVATE_KEY_SIZE);
128125
} else {
129126
static const unsigned char begin[] = {
130127
0x30,0x82,0x01,0x13,0x02,0x01,0x01,0x04,0x20
@@ -146,11 +143,11 @@ static int ec_privkey_export_der(const secp256k1_context *ctx, unsigned char *pr
146143
memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
147144
memcpy(ptr, key32, 32); ptr += 32;
148145
memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
149-
pubkeylen = PUBLIC_KEY_SIZE;
146+
pubkeylen = CPubKey::PUBLIC_KEY_SIZE;
150147
secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
151148
ptr += pubkeylen;
152149
*privkeylen = ptr - privkey;
153-
assert(*privkeylen == PRIVATE_KEY_SIZE);
150+
assert(*privkeylen == CKey::PRIVATE_KEY_SIZE);
154151
}
155152
return 1;
156153
}
@@ -183,7 +180,7 @@ CPrivKey CKey::GetPrivKey() const {
183180
CPubKey CKey::GetPubKey() const {
184181
assert(fValid);
185182
secp256k1_pubkey pubkey;
186-
size_t clen = PUBLIC_KEY_SIZE;
183+
size_t clen = CPubKey::PUBLIC_KEY_SIZE;
187184
CPubKey result;
188185
int ret = secp256k1_ec_pubkey_create(secp256k1_context_sign, &pubkey, begin());
189186
assert(ret);
@@ -196,8 +193,8 @@ CPubKey CKey::GetPubKey() const {
196193
bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_t test_case) const {
197194
if (!fValid)
198195
return false;
199-
vchSig.resize(SIGNATURE_SIZE);
200-
size_t nSigLen = SIGNATURE_SIZE;
196+
vchSig.resize(CPubKey::SIGNATURE_SIZE);
197+
size_t nSigLen = CPubKey::SIGNATURE_SIZE;
201198
unsigned char extra_entropy[32] = {0};
202199
WriteLE32(extra_entropy, test_case);
203200
secp256k1_ecdsa_signature sig;
@@ -225,7 +222,7 @@ bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
225222
bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
226223
if (!fValid)
227224
return false;
228-
vchSig.resize(COMPACT_SIGNATURE_SIZE);
225+
vchSig.resize(CPubKey::COMPACT_SIGNATURE_SIZE);
229226
int rec = -1;
230227
secp256k1_ecdsa_recoverable_signature sig;
231228
int ret = secp256k1_ecdsa_sign_recoverable(secp256k1_context_sign, &sig, hash.begin(), begin(), secp256k1_nonce_function_rfc6979, NULL);
@@ -255,7 +252,7 @@ bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const
255252
std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
256253
if ((nChild >> 31) == 0) {
257254
CPubKey pubkey = GetPubKey();
258-
assert(pubkey.size() == COMPRESSED_PUBLIC_KEY_SIZE);
255+
assert(pubkey.size() == CPubKey::COMPRESSED_PUBLIC_KEY_SIZE);
259256
BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, vout.data());
260257
} else {
261258
assert(size() == 32);

src/key.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@
1616
#include <vector>
1717

1818

19-
/**
20-
* secp256k1:
21-
*/
22-
const unsigned int PRIVATE_KEY_SIZE = 279;
23-
const unsigned int COMPRESSED_PRIVATE_KEY_SIZE = 214;
24-
/**
25-
* see www.keylength.com
26-
* script supports up to 75 for single byte push
27-
*/
28-
2919
/**
3020
* secure_allocator is defined in allocators.h
3121
* CPrivKey is a serialized private key, with all parameters included
@@ -36,6 +26,20 @@ typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
3626
/** An encapsulated private key. */
3727
class CKey
3828
{
29+
public:
30+
/**
31+
* secp256k1:
32+
*/
33+
static const unsigned int PRIVATE_KEY_SIZE = 279;
34+
static const unsigned int COMPRESSED_PRIVATE_KEY_SIZE = 214;
35+
/**
36+
* see www.keylength.com
37+
* script supports up to 75 for single byte push
38+
*/
39+
static_assert(
40+
PRIVATE_KEY_SIZE >= COMPRESSED_PRIVATE_KEY_SIZE,
41+
"COMPRESSED_PRIVATE_KEY_SIZE is larger than PRIVATE_KEY_SIZE");
42+
3943
private:
4044
//! Whether this private key is valid. We check for correctness when modifying the key
4145
//! data, so fValid should always correspond to the actual state.

src/pubkey.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ void CExtPubKey::Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const {
252252
code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
253253
code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
254254
memcpy(code+9, chaincode.begin(), 32);
255-
assert(pubkey.size() == COMPRESSED_PUBLIC_KEY_SIZE);
256-
memcpy(code+41, pubkey.begin(), COMPRESSED_PUBLIC_KEY_SIZE);
255+
assert(pubkey.size() == CPubKey::COMPRESSED_PUBLIC_KEY_SIZE);
256+
memcpy(code+41, pubkey.begin(), CPubKey::COMPRESSED_PUBLIC_KEY_SIZE);
257257
}
258258

259259
void CExtPubKey::Decode(const unsigned char code[BIP32_EXTKEY_SIZE]) {

src/pubkey.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,6 @@
1414
#include <stdexcept>
1515
#include <vector>
1616

17-
/**
18-
* secp256k1:
19-
*/
20-
const unsigned int PUBLIC_KEY_SIZE = 65;
21-
const unsigned int COMPRESSED_PUBLIC_KEY_SIZE = 33;
22-
const unsigned int SIGNATURE_SIZE = 72;
23-
const unsigned int COMPACT_SIGNATURE_SIZE = 65;
24-
/**
25-
* see www.keylength.com
26-
* script supports up to 75 for single byte push
27-
*/
28-
2917
const unsigned int BIP32_EXTKEY_SIZE = 74;
3018

3119
/** A reference to a CKey: the Hash160 of its serialized public key */
@@ -41,16 +29,29 @@ typedef uint256 ChainCode;
4129
/** An encapsulated public key. */
4230
class CPubKey
4331
{
32+
public:
33+
/**
34+
* secp256k1:
35+
*/
36+
static const unsigned int PUBLIC_KEY_SIZE = 65;
37+
static const unsigned int COMPRESSED_PUBLIC_KEY_SIZE = 33;
38+
static const unsigned int SIGNATURE_SIZE = 72;
39+
static const unsigned int COMPACT_SIGNATURE_SIZE = 65;
40+
/**
41+
* see www.keylength.com
42+
* script supports up to 75 for single byte push
43+
*/
44+
static_assert(
45+
PUBLIC_KEY_SIZE >= COMPRESSED_PUBLIC_KEY_SIZE,
46+
"COMPRESSED_PUBLIC_KEY_SIZE is larger than PUBLIC_KEY_SIZE");
47+
4448
private:
4549

4650
/**
4751
* Just store the serialized data.
4852
* Its length can very cheaply be computed from the first byte.
4953
*/
5054
unsigned char vch[PUBLIC_KEY_SIZE];
51-
static_assert(
52-
PUBLIC_KEY_SIZE >= COMPRESSED_PUBLIC_KEY_SIZE,
53-
"COMPRESSED_PUBLIC_KEY_SIZE is larger than PUBLIC_KEY_SIZE");
5455

5556
//! Compute the length of a pubkey with a given first byte.
5657
unsigned int static GetLen(unsigned char chHeader)

0 commit comments

Comments
 (0)