Skip to content

Commit db9f6f0

Browse files
committed
Auto merge of zcash#2821 - str4d:2335-scope-ecdsa-constants, r=str4d
Scope the ECDSA constant sizes to CPubKey / CKey classes Cherry-picked from bitcoin/bitcoin#10657, upstreaming our patches from zcash#2335.
2 parents 1ddecf6 + c4c7c66 commit db9f6f0

File tree

4 files changed

+43
-41
lines changed

4 files changed

+43
-41
lines changed

src/key.cpp

+11-14
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
}
@@ -191,7 +188,7 @@ CPrivKey CKey::GetPrivKey() const {
191188
CPubKey CKey::GetPubKey() const {
192189
assert(fValid);
193190
secp256k1_pubkey pubkey;
194-
size_t clen = PUBLIC_KEY_SIZE;
191+
size_t clen = CPubKey::PUBLIC_KEY_SIZE;
195192
CPubKey result;
196193
int ret = secp256k1_ec_pubkey_create(secp256k1_context_sign, &pubkey, begin());
197194
assert(ret);
@@ -204,8 +201,8 @@ CPubKey CKey::GetPubKey() const {
204201
bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, uint32_t test_case) const {
205202
if (!fValid)
206203
return false;
207-
vchSig.resize(SIGNATURE_SIZE);
208-
size_t nSigLen = SIGNATURE_SIZE;
204+
vchSig.resize(CPubKey::SIGNATURE_SIZE);
205+
size_t nSigLen = CPubKey::SIGNATURE_SIZE;
209206
unsigned char extra_entropy[32] = {0};
210207
WriteLE32(extra_entropy, test_case);
211208
secp256k1_ecdsa_signature sig;
@@ -233,7 +230,7 @@ bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
233230
bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
234231
if (!fValid)
235232
return false;
236-
vchSig.resize(COMPACT_SIGNATURE_SIZE);
233+
vchSig.resize(CPubKey::COMPACT_SIGNATURE_SIZE);
237234
int rec = -1;
238235
secp256k1_ecdsa_recoverable_signature sig;
239236
int ret = secp256k1_ecdsa_sign_recoverable(secp256k1_context_sign, &sig, hash.begin(), begin(), secp256k1_nonce_function_rfc6979, NULL);
@@ -264,7 +261,7 @@ bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const
264261
LockObject(out);
265262
if ((nChild >> 31) == 0) {
266263
CPubKey pubkey = GetPubKey();
267-
assert(pubkey.size() == COMPRESSED_PUBLIC_KEY_SIZE);
264+
assert(pubkey.size() == CPubKey::COMPRESSED_PUBLIC_KEY_SIZE);
268265
BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, out);
269266
} else {
270267
assert(size() == 32);

src/key.h

+14-10
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ void CExtPubKey::Encode(unsigned char code[74]) const {
104104
code[5] = (nChild >> 24) & 0xFF; code[6] = (nChild >> 16) & 0xFF;
105105
code[7] = (nChild >> 8) & 0xFF; code[8] = (nChild >> 0) & 0xFF;
106106
memcpy(code+9, chaincode.begin(), 32);
107-
assert(pubkey.size() == COMPRESSED_PUBLIC_KEY_SIZE);
108-
memcpy(code+41, pubkey.begin(), COMPRESSED_PUBLIC_KEY_SIZE);
107+
assert(pubkey.size() == CPubKey::COMPRESSED_PUBLIC_KEY_SIZE);
108+
memcpy(code+41, pubkey.begin(), CPubKey::COMPRESSED_PUBLIC_KEY_SIZE);
109109
}
110110

111111
void CExtPubKey::Decode(const unsigned char code[74]) {

src/pubkey.h

+16-15
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
/** A reference to a CKey: the Hash160 of its serialized public key */
3018
class CKeyID : public uint160
3119
{
@@ -39,16 +27,29 @@ typedef uint256 ChainCode;
3927
/** An encapsulated public key. */
4028
class CPubKey
4129
{
30+
public:
31+
/**
32+
* secp256k1:
33+
*/
34+
static const unsigned int PUBLIC_KEY_SIZE = 65;
35+
static const unsigned int COMPRESSED_PUBLIC_KEY_SIZE = 33;
36+
static const unsigned int SIGNATURE_SIZE = 72;
37+
static const unsigned int COMPACT_SIGNATURE_SIZE = 65;
38+
/**
39+
* see www.keylength.com
40+
* script supports up to 75 for single byte push
41+
*/
42+
static_assert(
43+
PUBLIC_KEY_SIZE >= COMPRESSED_PUBLIC_KEY_SIZE,
44+
"COMPRESSED_PUBLIC_KEY_SIZE is larger than PUBLIC_KEY_SIZE");
45+
4246
private:
4347

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

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

0 commit comments

Comments
 (0)